Repository navigation
test(acp): hoist SecretSource subclasses to module level - #3489
VascoSch92 wants to merge 1 commit into
Conversation
The three test-only SecretSource subclasses (_FakeLookupSecret, _CountingLookupSecret, _BrokenSecret) were defined inside test methods, making them <locals> classes with unstable qualnames. SecretSource is a DiscriminatedUnionMixin that discovers concrete subclasses via cls.__subclasses__(); _get_checked_concrete_subclasses rejects <locals> qualnames. Pydantic kept these locals alive after their tests ran, so any subsequent ConversationState deserialization in the same xdist worker failed with 'Local classes not supported!'. Hoisting them to module level gives them stable qualnames and stops the cross-test pollution that flaked test_state_serialization.py under 'pytest -n auto'. Co-authored-by: openhands <openhands@all-hands.dev>
Python API breakage checks — ✅ PASSEDResult: ✅ PASSED |
REST API breakage checks (OpenAPI) — ✅ PASSEDResult: ✅ PASSED |
|
✅ Review complete. This review was performed through OpenHands Cloud Automation. You can log in and view the conversation here. |
all-hands-bot
left a comment
There was a problem hiding this comment.
Review: test(acp): hoist SecretSource subclasses to module level
This is a clean, minimal fix for #3487. The root cause diagnosis is spot-on: DiscriminatedUnionMixin._get_checked_concrete_subclasses rejects <locals> qualnames, Pydantic's schema/instance references keep those local classes alive after the defining test returns, and any later test on the same xdist worker that touches ConversationState deserialization hits the resulting ValueError. Hoisting the three classes to module level resolves it completely.
What's well done:
- The module-level comment block is exemplary — it cites the exact method (
_get_checked_concrete_subclasses) and file (openhands/sdk/utils/models.py) responsible, explains the Pydantic retention mechanism, and references the issue. Future maintainers will thank you. - The local
import SecretSourceandimport Fieldstatements in each test method have been cleanly removed and consolidated at the top of the file. calls: list[int] = PydanticField(default_factory=list)is correct Pydantic v2 syntax for a mutable default field.- The before/after verification section in the PR description is exactly what's needed to build confidence in the fix.
One minor point (see inline comment): the Field as PydanticField alias on the import line — worth confirming whether the alias is necessary.
This review was generated by an AI agent (OpenHands) on behalf of the user through OpenHands Automation. View conversation
| from acp.exceptions import RequestError as ACPRequestError | ||
| from acp.schema import PromptResponse | ||
| from pydantic import SecretStr | ||
| from pydantic import Field as PydanticField, SecretStr |
There was a problem hiding this comment.
🟡 Suggestion: The PydanticField alias is functional, but it's worth confirming it's necessary. If another Field is already imported at module scope (e.g. from SQLAlchemy, dataclasses, or an internal SDK module) the alias is the right call — but a brief inline comment explaining the naming conflict would help future readers. If no such conflict exists, the standard name Field would be cleaner.
| from pydantic import Field as PydanticField, SecretStr | |
| from pydantic import Field, SecretStr |
(Apply only if no other Field is imported at module scope; otherwise keep the alias.)
all-hands-bot
left a comment
There was a problem hiding this comment.
⚠️ QA Report: PASS WITH ISSUES
Manual before/after QA confirms the PR removes the <locals> SecretSource pollution and the affected SDK schema/state serialization path works on the PR branch; CI is not fully green yet.
Does this PR achieve its stated goal?
Yes. On origin/main, manually running the ACP secret-registry checks in-process left a local SecretSource subclass and SecretSource.model_json_schema() raised ValueError: Local classes not supported! ..., matching the described failure mode. On 1c6fbed71e725f775d898e842b7d1a59f951a966, the identical script left no local subclasses, generated the SecretSource schema successfully, and completed ConversationState serialization smoke checks.
| Phase | Result |
|---|---|
| Environment Setup | ✅ make build completed and installed the uv-managed dev environment |
| CI Status | Agent Server/Build & Push (java-arm64)), 3 pending, 3 skipped at review time |
| Functional Verification | ✅ Manual Python execution reproduced the bug on base and verified the fix on the PR branch; no pytest/test-suite run was used |
Functional Verification
Test 1: ACP SecretSource test helpers no longer poison later schema/state serialization
Step 1 — Reproduce / establish baseline (without the fix):
Ran git checkout --detach origin/main && OPENHANDS_SUPPRESS_BANNER=1 uv run python /tmp/qa_secret_pollution_repro.py.
Filtered output:
local SecretSource subclasses after ACP secret checks: ['TestACPSecretRegistryEnvInjection.test_failing_registry_lookup_swallowed.<locals>._BrokenSecret']
Traceback (most recent call last):
raise ValueError(
ValueError: Local classes not supported! qa_test_acp_agent._BrokenSecret / openhands.sdk.secret.secrets.SecretSource (Since they may not exist at deserialization time)
exit=1
This confirms the bug exists on the base branch: after exercising the ACP secret-registry behavior in the same process, a <locals> SecretSource subclass remains discoverable and schema generation hits the SDK guard that rejects local subclasses.
Step 2 — Apply the PR changes:
Checked out openhands/fix-3487-local-secretsource-pollution at 1c6fbed71e725f775d898e842b7d1a59f951a966.
Step 3 — Re-run with the fix in place:
Ran OPENHANDS_SUPPRESS_BANNER=1 uv run python /tmp/qa_secret_pollution_repro.py.
Filtered output:
local SecretSource subclasses after ACP secret checks: []
SecretSource schema generation: OK
ConversationState serialization smoke checks: OK
exit=0
This shows the PR fixes the stated pollution: the same ACP secret-registry path no longer leaves local SecretSource subclasses behind, and the follow-on schema/state serialization operations complete successfully.
Issues Found
- 🟠 Issue: CI is not fully green at review time:
Agent Server/Build & Push (java-arm64)is failing, whileAgent Server/Build & Push (java-amd64),Agent Server/Build & Push (python-amd64), andQA Changes by OpenHands/qa-changesare still pending. Functional QA did not find a behavior regression, but maintainers should wait for CI completion or investigate the failing build check before merging.
This QA review was created by an AI agent (OpenHands) on behalf of the user.
Fixes #3487.
Summary
The three test-only
SecretSourcesubclasses intests/sdk/agent/test_acp_agent.py—_FakeLookupSecret,_CountingLookupSecret,_BrokenSecret— were defined inside their test methods, giving them<locals>qualnames.SecretSourceis aDiscriminatedUnionMixinthat discovers concrete subclasses viacls.__subclasses__(). Pydantic kept these<locals>classes alive after the tests that defined them returned (via schema / instance refs), and_get_checked_concrete_subclasses(openhands-sdk/openhands/sdk/utils/models.py) explicitly rejects<locals>qualnames:Any subsequent
ConversationStatedeserialization in the same xdist worker therefore raised:Under
pytest -n auto, this surfaced flakily as 5 failures intests/sdk/conversation/local/test_state_serialization.pywhenevertest_acp_agent.pyandtest_state_serialization.pylanded on the same worker with the ACP file scheduled first.Fix (minimal)
Hoist the three subclasses to module level so they have stable qualnames and pass the discriminated-union check. The test methods now just instantiate them; their behavior and assertions are unchanged.
Per the issue author's request, this PR is the minimal solution to deflake the tests — no defensive autouse fixture / registry snapshot is added.
Verification
Before (on
main, single process, ACP first):After this PR:
Pre-commit (
ruff format,ruff check,pyright, etc.) is clean on the touched file.This PR was created by an AI agent (OpenHands) on behalf of @VascoSch92.
@VascoSch92 can click here to continue refining the PR