Skip to content

test(acp): hoist SecretSource subclasses to module level - #3489

Closed
VascoSch92 wants to merge 1 commit into
mainfrom
openhands/fix-3487-local-secretsource-pollution
Closed

VascoSch92 wants to merge 1 commit into
mainfrom
openhands/fix-3487-local-secretsource-pollution

Conversation

@VascoSch92

Copy link
Copy Markdown
Member

Fixes #3487.

Summary

The three test-only SecretSource subclasses in tests/sdk/agent/test_acp_agent.py — _FakeLookupSecret, _CountingLookupSecret, _BrokenSecret — were defined inside their test methods, giving them <locals> qualnames.

SecretSource is a DiscriminatedUnionMixin that discovers concrete subclasses via cls.__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:

if "<locals>" in sub.__qualname__:
    raise ValueError("Local classes not supported! ...")

Any subsequent ConversationState deserialization in the same xdist worker therefore raised:

Local classes not supported!
tests.sdk.agent.test_acp_agent._CountingLookupSecret / openhands.sdk.secret.secrets.SecretSource

Under pytest -n auto, this surfaced flakily as 5 failures in tests/sdk/conversation/local/test_state_serialization.py whenever test_acp_agent.py and test_state_serialization.py landed 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):

$ uv run pytest tests/sdk/agent/test_acp_agent.py::TestACPSecretRegistryEnvInjection \
                tests/sdk/conversation/local/test_state_serialization.py -q
5 failed, 35 passed

After this PR:

$ uv run pytest tests/sdk/agent/test_acp_agent.py::TestACPSecretRegistryEnvInjection \
                tests/sdk/conversation/local/test_state_serialization.py -q
40 passed

$ uv run pytest tests/sdk/agent/test_acp_agent.py \
                tests/sdk/conversation/local/test_state_serialization.py -n auto -q
338 passed

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

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>
@github-actions

github-actions Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Python API breakage checks — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
TOTAL28889826471% 
report-only-changed-files is enabled. No files were changed during this commit :)

@VascoSch92
VascoSch92 requested a review from all-hands-bot June 3, 2026 16:42

all-hands-bot commented Jun 3, 2026 •

Copy link
Copy Markdown
Collaborator

✅ Review complete.

This review was performed through OpenHands Cloud Automation. You can log in and view the conversation here.

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 SecretSource and import Field statements 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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Suggested change
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 all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ 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 ⚠️ 24 successful, 1 failing (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, while Agent Server/Build & Push (java-amd64), Agent Server/Build & Push (python-amd64), and QA Changes by OpenHands/qa-changes are 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.

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.

Flaky test pollution: local SecretSource subclasses in test_acp_agent.py break test_state_serialization under xdist

3 participants