Files
cleveractors-core/features/llm_cleanup_shared_client.feature
hurui200320 ed9c4dc95d
CI / quality (pull_request) Successful in 34s
CI / build (pull_request) Successful in 57s
CI / integration_tests (pull_request) Successful in 1m26s
CI / unit_tests (pull_request) Successful in 3m28s
CI / lint (pull_request) Successful in 1m1s
CI / typecheck (pull_request) Successful in 1m0s
CI / security (pull_request) Successful in 59s
CI / coverage (pull_request) Successful in 3m7s
CI / status-check (pull_request) Successful in 3s
CI / lint (push) Successful in 40s
CI / quality (push) Successful in 40s
CI / build (push) Successful in 40s
CI / typecheck (push) Successful in 1m5s
CI / security (push) Successful in 1m5s
CI / integration_tests (push) Successful in 1m20s
CI / unit_tests (push) Successful in 3m14s
CI / coverage (push) Successful in 3m2s
CI / status-check (push) Successful in 3s
fix(llmagent): do not close shared cached httpx clients in cleanup()
LLMAgent.cleanup() previously iterated over hard-coded provider SDK client
attributes (root_async_client, root_client, _async_client, _client) and
called close() on each. Recent langchain-anthropic and langchain-openai
versions cache their default httpx clients via module-level lru_cache
functions. Closing those clients poisoned the cache: every subsequent
ChatAnthropic/ChatOpenAI instance in the same process received the same
closed httpx client and failed with a connection error.

Fix: cleanup() now only releases the agent's own reference to the chat
model (self._chat_model = None). The removed _KNOWN_CLIENT_ATTRS class
variable has been deleted and ClassVar removed from the typing import.

The concurrent-idempotency guarantee is preserved: the lock is still
acquired before nulling _chat_model, so two concurrent cleanup() calls
cannot both see a non-None model and attempt conflicting operations.

The four provider SDK client-closing scenarios in credential_injection.feature
and llm_missing_coverage.feature are removed as they tested the old (buggy)
behaviour. Their step definitions are removed from credential_cleanup_steps.py
(now only carries the resolve_class_ref patch step) and
llm_missing_coverage_steps.py is updated with the corrected assertions.

Six new regression BDD scenarios tagged @tdd_issue @tdd_issue_57 are added
in features/llm_cleanup_shared_client.feature, covering all four provider
SDK client attribute paths (Anthropic _async_client/_client, OpenAI
root_async_client/root_client) and two end-to-end two-agent scenarios that
prove a second agent can run successfully after the first is cleaned up.

ISSUES CLOSED: #57
2026-06-17 17:19:44 +00:00

67 lines
3.8 KiB
Gherkin

Feature: LLMAgent.cleanup() must not close shared cached httpx clients
As a developer running multiple LLM requests in the same process
I want LLMAgent.cleanup() to release only the agent's own reference to the chat model
So that shared cached httpx clients remain open for subsequent LLMAgent instances
# Regression tests for issue #57:
# langchain-anthropic and langchain-openai cache their default httpx clients
# via module-level lru_cache functions. Calling close() on those clients
# through cleanup() poisoned the cache and broke all subsequent LLM requests
# in the same process.
#
# Test strategy:
# - Async paths (_async_client, root_async_client): inject a Mock SDK client
# whose close() is an AsyncMock, then assert close() was never called.
# - Sync paths (_client, root_client): inject a real httpx.Client and assert
# it is not closed (httpx.Client.close() is a real sync method, so this
# directly proves the fix).
Background:
Given an LLM agent test environment is ready (scc)
@tdd_issue @tdd_issue_57
Scenario: cleanup does not call close() on the async SDK client at _async_client (Anthropic path)
Given an LLMAgent for "anthropic" with a mock model whose _async_client has a tracked async close (scc)
When I call cleanup on the agent (scc)
Then close() should NOT have been called on the _async_client mock (scc)
And the agent's _chat_model should be None (scc)
@tdd_issue @tdd_issue_57
Scenario: cleanup does not close the shared sync httpx.Client at _client (Anthropic path)
Given an LLMAgent for "anthropic" with a mock model whose _client is a real httpx.Client (scc)
When I call cleanup on the agent (scc)
Then the real httpx.Client should NOT be closed (scc)
And the agent's _chat_model should be None (scc)
@tdd_issue @tdd_issue_57
Scenario: cleanup does not call close() on the async SDK client at root_async_client (OpenAI path)
Given an LLMAgent for "openai" with a mock model whose root_async_client has a tracked async close (scc)
When I call cleanup on the agent (scc)
Then close() should NOT have been called on the root_async_client mock (scc)
And the agent's _chat_model should be None (scc)
@tdd_issue @tdd_issue_57
Scenario: cleanup does not close the shared sync httpx.Client at root_client (OpenAI path)
Given an LLMAgent for "openai" with a mock model whose root_client is a real httpx.Client (scc)
When I call cleanup on the agent (scc)
Then the real httpx.Client should NOT be closed (scc)
And the agent's _chat_model should be None (scc)
@tdd_issue @tdd_issue_57
Scenario: second LLMAgent reusing a shared sync httpx.Client works after the first is cleaned up (Anthropic)
Given a shared real httpx.Client that simulates the lru_cache client (scc)
And a first LLMAgent for "anthropic" whose mock model holds that shared client as _client (scc)
When I call cleanup on the first agent (scc)
And I create a second LLMAgent for "anthropic" whose mock model holds the same shared client as _client (scc)
Then invoking the second agent's mock model should succeed (scc)
And the shared real httpx.Client should still NOT be closed after both agents run (scc)
@tdd_issue @tdd_issue_57
Scenario: second LLMAgent reusing a shared sync httpx.Client works after the first is cleaned up (OpenAI)
Given a shared real httpx.Client that simulates the lru_cache client (scc)
And a first LLMAgent for "openai" whose mock model holds that shared client as root_client (scc)
When I call cleanup on the first agent (scc)
And I create a second LLMAgent for "openai" whose mock model holds the same shared client as root_client (scc)
Then invoking the second agent's mock model should succeed (scc)
And the shared real httpx.Client should still NOT be closed after both agents run (scc)