Skip to content

Refactor Core Logic to Async-Native with Sync Facade#36

Merged
AdarshaAP merged 4 commits into
commit19from
refactor/async-service-pattern-935374347695940404
Jan 22, 2026
Merged

Refactor Core Logic to Async-Native with Sync Facade#36
AdarshaAP merged 4 commits into
commit19from
refactor/async-service-pattern-935374347695940404

Conversation

@google-labs-jules
Copy link
Copy Markdown

This PR refactors the core client logic to follow the "Async-Native with Sync Facade" pattern. This ensures better resource management and non-blocking I/O for future async features while maintaining backward compatibility for synchronous scripts.

Key changes:

  • Introduced OpenAIClientAsync and OpenAIEmbeddingClientAsync handling AsyncOpenAI interactions.
  • Introduced OpenAIClient and OpenAIEmbeddingClient facades that use anyio.run to bridge synchronous calls to the async core.
  • Updated interfaces to support async methods.
  • Comprehensive test updates to cover both async and sync paths.
  • Added dependency on anyio, httpx, aiofiles.

PR created automatically by Jules for task 935374347695940404 started by @gowthamrao

- Updated `pyproject.toml` with `anyio`, `httpx`, `aiofiles`.
- Refactored `src/coreason_optimizer/core/client.py`:
  - Created `OpenAIClientAsync` and `OpenAIEmbeddingClientAsync` as Async-Native cores.
  - Created `OpenAIClient` and `OpenAIEmbeddingClient` as Sync Facades using `anyio.run`.
  - Updated BudgetAware wrappers to be compatible (async wrappers created, sync wrappers preserved).
- Updated `src/coreason_optimizer/core/interfaces.py` to include Async protocols.
- Updated tests in `tests/core/test_client.py` and `tests/core/test_client_embedding.py` to verify both Async and Sync behavior using `AsyncMock` and `pytest-asyncio`.
- Verified no `threading.local` usage.
- Ensured strict lifecycle management with `__aenter__` and `__enter__`.
@google-labs-jules
Copy link
Copy Markdown
Author

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

- Ran `poetry lock` to synchronize `poetry.lock` with `pyproject.toml` changes (added `anyio`, `httpx`, `aiofiles`).
- Refactored `src/coreason_optimizer/core/client.py`:
  - Created `OpenAIClientAsync` and `OpenAIEmbeddingClientAsync` as Async-Native cores.
  - Created `OpenAIClient` and `OpenAIEmbeddingClient` as Sync Facades using `anyio.run`.
  - Updated BudgetAware wrappers to be compatible (async wrappers created, sync wrappers preserved).
- Updated `src/coreason_optimizer/core/interfaces.py` to include Async protocols.
- Updated tests in `tests/core/test_client.py` and `tests/core/test_client_embedding.py` to verify both Async and Sync behavior.
- Added `pytest-asyncio` to `pyproject.toml` dev dependencies and regenerated `poetry.lock`.
- Updated `tests/core/test_client_edge_cases.py` to use `AsyncMock` for `OpenAIClient` (which uses `AsyncOpenAI` internally) and support async/await patterns properly.
- Updated `tests/test_readme_usage.py` to patch `AsyncOpenAI` instead of `OpenAI` and properly mock async responses.
- Verified local tests pass with `pytest-asyncio` mode auto.
- Added `pytest-asyncio` to `pyproject.toml` dev dependencies and regenerated `poetry.lock`.
- Updated `tests/core/test_client_edge_cases.py` to use `AsyncMock` for `OpenAIClient` (which uses `AsyncOpenAI` internally) and support async/await patterns properly.
- Updated `tests/test_readme_usage.py` to patch `AsyncOpenAI` instead of `OpenAI` and properly mock async responses.
- Updated `tests/core/test_client_budget_wrapper.py` to fully cover Sync/Async budget wrappers and `OpenAIClient` exception handling in `__exit__`.
- Verified local tests pass with `pytest-asyncio` mode auto.
- Ensured strict lifecycle management tests for both sync and async paths.
@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 22, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@AdarshaAP AdarshaAP marked this pull request as ready for review January 22, 2026 00:16
@AdarshaAP AdarshaAP merged commit 93770a1 into commit19 Jan 22, 2026
13 checks passed
@AdarshaAP AdarshaAP deleted the refactor/async-service-pattern-935374347695940404 branch January 22, 2026 00:16
AdarshaAP added a commit that referenced this pull request Jan 22, 2026
* Refactor Core Logic to Async-Native with Sync Facade

- Updated `pyproject.toml` with `anyio`, `httpx`, `aiofiles`.
- Refactored `src/coreason_optimizer/core/client.py`:
  - Created `OpenAIClientAsync` and `OpenAIEmbeddingClientAsync` as Async-Native cores.
  - Created `OpenAIClient` and `OpenAIEmbeddingClient` as Sync Facades using `anyio.run`.
  - Updated BudgetAware wrappers to be compatible (async wrappers created, sync wrappers preserved).
- Updated `src/coreason_optimizer/core/interfaces.py` to include Async protocols.
- Updated tests in `tests/core/test_client.py` and `tests/core/test_client_embedding.py` to verify both Async and Sync behavior using `AsyncMock` and `pytest-asyncio`.
- Verified no `threading.local` usage.
- Ensured strict lifecycle management with `__aenter__` and `__enter__`.

* Fix poetry.lock and Refactor Core Logic

- Ran `poetry lock` to synchronize `poetry.lock` with `pyproject.toml` changes (added `anyio`, `httpx`, `aiofiles`).
- Refactored `src/coreason_optimizer/core/client.py`:
  - Created `OpenAIClientAsync` and `OpenAIEmbeddingClientAsync` as Async-Native cores.
  - Created `OpenAIClient` and `OpenAIEmbeddingClient` as Sync Facades using `anyio.run`.
  - Updated BudgetAware wrappers to be compatible (async wrappers created, sync wrappers preserved).
- Updated `src/coreason_optimizer/core/interfaces.py` to include Async protocols.
- Updated tests in `tests/core/test_client.py` and `tests/core/test_client_embedding.py` to verify both Async and Sync behavior.

* Fix CI issues and Async Mocking

- Added `pytest-asyncio` to `pyproject.toml` dev dependencies and regenerated `poetry.lock`.
- Updated `tests/core/test_client_edge_cases.py` to use `AsyncMock` for `OpenAIClient` (which uses `AsyncOpenAI` internally) and support async/await patterns properly.
- Updated `tests/test_readme_usage.py` to patch `AsyncOpenAI` instead of `OpenAI` and properly mock async responses.
- Verified local tests pass with `pytest-asyncio` mode auto.

93770a1
AdarshaAP added a commit that referenced this pull request Jan 22, 2026
* Refactor Core Logic to Async-Native with Sync Facade

- Updated `pyproject.toml` with `anyio`, `httpx`, `aiofiles`.
- Refactored `src/coreason_optimizer/core/client.py`:
  - Created `OpenAIClientAsync` and `OpenAIEmbeddingClientAsync` as Async-Native cores.
  - Created `OpenAIClient` and `OpenAIEmbeddingClient` as Sync Facades using `anyio.run`.
  - Updated BudgetAware wrappers to be compatible (async wrappers created, sync wrappers preserved).
- Updated `src/coreason_optimizer/core/interfaces.py` to include Async protocols.
- Updated tests in `tests/core/test_client.py` and `tests/core/test_client_embedding.py` to verify both Async and Sync behavior using `AsyncMock` and `pytest-asyncio`.
- Verified no `threading.local` usage.
- Ensured strict lifecycle management with `__aenter__` and `__enter__`.

* Fix poetry.lock and Refactor Core Logic

- Ran `poetry lock` to synchronize `poetry.lock` with `pyproject.toml` changes (added `anyio`, `httpx`, `aiofiles`).
- Refactored `src/coreason_optimizer/core/client.py`:
  - Created `OpenAIClientAsync` and `OpenAIEmbeddingClientAsync` as Async-Native cores.
  - Created `OpenAIClient` and `OpenAIEmbeddingClient` as Sync Facades using `anyio.run`.
  - Updated BudgetAware wrappers to be compatible (async wrappers created, sync wrappers preserved).
- Updated `src/coreason_optimizer/core/interfaces.py` to include Async protocols.
- Updated tests in `tests/core/test_client.py` and `tests/core/test_client_embedding.py` to verify both Async and Sync behavior.

* Fix CI issues and Async Mocking

- Added `pytest-asyncio` to `pyproject.toml` dev dependencies and regenerated `poetry.lock`.
- Updated `tests/core/test_client_edge_cases.py` to use `AsyncMock` for `OpenAIClient` (which uses `AsyncOpenAI` internally) and support async/await patterns properly.
- Updated `tests/test_readme_usage.py` to patch `AsyncOpenAI` instead of `OpenAI` and properly mock async responses.
- Verified local tests pass with `pytest-asyncio` mode auto.

93770a1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant