Skip to content

fix(sdk): resolve secret lookups outside the state lock before async tools run - #6662

Merged
enyst merged 1 commit into
mainfrom
fix/secret-lookup-under-state-lock
Nov 9, 2026
Merged

enyst merged 1 commit into
mainfrom
fix/secret-lookup-under-state-lock

Conversation

@enyst

@enyst enyst commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

HUMAN:

This PR proposes a fix for a bug uncovered during the first full agentic run of verify-sdk.


AGENT:

I'm an AI agent (Claude Code) helping Engel Nyst (@enyst) with project maintenance.

Requested by Engel · project thread

Why

Under arun(), a secret looked up for the first time while masking a tool observation could stay in clear text. Each tool masks its observation in a worker thread while the run loop holds the state lock. When the LookupSecret server runs on the same event loop and registers secrets on the conversation while answering (update_secrets, which takes the lock), the request blocked until httpx's 30 s timeout, the lookup failed, and the back-off left the value unmasked. The same lookup made while masking an agent message worked, because astep already releases the lock for it.

Summary

  • Agent._aexecute_actions resolves uncached secret lookups in a worker thread with the state lock released (_released_state_lock_during_io), repeating until the registry is stable, before the tool batch runs. It is the same pattern astep uses before masking an agent message and _arequires_user_confirmation uses for audit events.
  • The resolution loop moves out of mask_secrets_in_output into SecretRegistry.resolve_uncached_values(), unchanged (same back-off), so both callers share it.
  • Masking inside the tool then finds every value cached and makes no lookup under the lock.
  • The verify-sdk note on S13.mask-async now describes the expected behavior instead of the known failure.

Issue Number

Fixes #6642 (umbrella #6660)

How to Test

uv run pytest tests/sdk/agent/test_async_secret_masking.py -q
export PATH="$PWD/.agents/skills/verify-sdk/scripts:$PATH"
sdk-map run --file S13 --only S13.mask-async
  • The new test test_async_tool_observation_lookup_does_not_wait_on_state_lock serves the lookup on the run's own loop and registers a secret while answering. It times out on main and passes here in a fraction of a second.
  • The map bullet: the tool case now reads the values are <secret-hidden> and <secret-hidden> in 0.4 s (it was loop-val in clear text after 30.5 s); the message case is unchanged at 0.1 s.
  • tests/sdk/agent and tests/sdk/conversation: 1985 passed.

Video/Screenshots

Not applicable.

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes

  • A secret registered by a tool during its own execution is still first looked up when that tool's output is masked. That case was not part of the report, and covering it would mean releasing the lock around tool execution, a much larger change.
  • The synchronous run() path is unchanged; the map's sync bullets already pass.
  • Cross-repo dependencies: none.

Generated by Claude Code


🐳 Agent Server images for this PR — GHCR package, pull/run commands, and all pushed tags (click to expand)

• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server

Variants & Base Images

Variant Architectures Base Image Docs / Tags
java amd64, arm64 eclipse-temurin:17-jdk Link
python-slim amd64, arm64 python-node-runtime Link
python-minimal amd64, arm64 python-node-runtime Link
python amd64, arm64 python-node-runtime Link
golang amd64, arm64 golang:1.21-bookworm Link

Pull (multi-arch manifest)

# Each variant is a multi-arch manifest supporting both amd64 and arm64
docker pull ghcr.io/openhands/agent-server:d48eb1f-python

Run

docker run -it --rm \
  -p 8000:8000 \
  --name agent-server-d48eb1f-python \
  ghcr.io/openhands/agent-server:d48eb1f-python

All tags pushed for this build

ghcr.io/openhands/agent-server:d48eb1f-golang-amd64
ghcr.io/openhands/agent-server:d48eb1f095bf350d3355be5b6bd119cdbbc92fe5-golang-amd64
ghcr.io/openhands/agent-server:fix-secret-lookup-under-state-lock-golang-amd64
ghcr.io/openhands/agent-server:d48eb1f-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:d48eb1f-golang-arm64
ghcr.io/openhands/agent-server:d48eb1f095bf350d3355be5b6bd119cdbbc92fe5-golang-arm64
ghcr.io/openhands/agent-server:fix-secret-lookup-under-state-lock-golang-arm64
ghcr.io/openhands/agent-server:d48eb1f-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:d48eb1f-java-amd64
ghcr.io/openhands/agent-server:d48eb1f095bf350d3355be5b6bd119cdbbc92fe5-java-amd64
ghcr.io/openhands/agent-server:fix-secret-lookup-under-state-lock-java-amd64
ghcr.io/openhands/agent-server:d48eb1f-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:d48eb1f-java-arm64
ghcr.io/openhands/agent-server:d48eb1f095bf350d3355be5b6bd119cdbbc92fe5-java-arm64
ghcr.io/openhands/agent-server:fix-secret-lookup-under-state-lock-java-arm64
ghcr.io/openhands/agent-server:d48eb1f-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:d48eb1f-python-amd64
ghcr.io/openhands/agent-server:d48eb1f095bf350d3355be5b6bd119cdbbc92fe5-python-amd64
ghcr.io/openhands/agent-server:fix-secret-lookup-under-state-lock-python-amd64
ghcr.io/openhands/agent-server:d48eb1f-python-node-runtime-amd64
ghcr.io/openhands/agent-server:d48eb1f-python-arm64
ghcr.io/openhands/agent-server:d48eb1f095bf350d3355be5b6bd119cdbbc92fe5-python-arm64
ghcr.io/openhands/agent-server:fix-secret-lookup-under-state-lock-python-arm64
ghcr.io/openhands/agent-server:d48eb1f-python-node-runtime-arm64
ghcr.io/openhands/agent-server:d48eb1f-python-minimal-amd64
ghcr.io/openhands/agent-server:d48eb1f095bf350d3355be5b6bd119cdbbc92fe5-python-minimal-amd64
ghcr.io/openhands/agent-server:fix-secret-lookup-under-state-lock-python-minimal-amd64
ghcr.io/openhands/agent-server:d48eb1f-python-node-runtime-minimal-amd64
ghcr.io/openhands/agent-server:d48eb1f-python-minimal-arm64
ghcr.io/openhands/agent-server:d48eb1f095bf350d3355be5b6bd119cdbbc92fe5-python-minimal-arm64
ghcr.io/openhands/agent-server:fix-secret-lookup-under-state-lock-python-minimal-arm64
ghcr.io/openhands/agent-server:d48eb1f-python-node-runtime-minimal-arm64
ghcr.io/openhands/agent-server:d48eb1f-python-slim-amd64
ghcr.io/openhands/agent-server:d48eb1f095bf350d3355be5b6bd119cdbbc92fe5-python-slim-amd64
ghcr.io/openhands/agent-server:fix-secret-lookup-under-state-lock-python-slim-amd64
ghcr.io/openhands/agent-server:d48eb1f-python-node-runtime-slim-amd64
ghcr.io/openhands/agent-server:d48eb1f-python-slim-arm64
ghcr.io/openhands/agent-server:d48eb1f095bf350d3355be5b6bd119cdbbc92fe5-python-slim-arm64
ghcr.io/openhands/agent-server:fix-secret-lookup-under-state-lock-python-slim-arm64
ghcr.io/openhands/agent-server:d48eb1f-python-node-runtime-slim-arm64
ghcr.io/openhands/agent-server:d48eb1f-golang
ghcr.io/openhands/agent-server:d48eb1f095bf350d3355be5b6bd119cdbbc92fe5-golang
ghcr.io/openhands/agent-server:fix-secret-lookup-under-state-lock-golang
ghcr.io/openhands/agent-server:d48eb1f-golang_tag_1.21-bookworm
ghcr.io/openhands/agent-server:d48eb1f-java
ghcr.io/openhands/agent-server:d48eb1f095bf350d3355be5b6bd119cdbbc92fe5-java
ghcr.io/openhands/agent-server:fix-secret-lookup-under-state-lock-java
ghcr.io/openhands/agent-server:d48eb1f-eclipse-temurin_tag_17-jdk
ghcr.io/openhands/agent-server:d48eb1f-python-minimal
ghcr.io/openhands/agent-server:d48eb1f095bf350d3355be5b6bd119cdbbc92fe5-python-minimal
ghcr.io/openhands/agent-server:fix-secret-lookup-under-state-lock-python-minimal
ghcr.io/openhands/agent-server:d48eb1f-python-node-runtime-minimal
ghcr.io/openhands/agent-server:d48eb1f-python-slim
ghcr.io/openhands/agent-server:d48eb1f095bf350d3355be5b6bd119cdbbc92fe5-python-slim
ghcr.io/openhands/agent-server:fix-secret-lookup-under-state-lock-python-slim
ghcr.io/openhands/agent-server:d48eb1f-python-node-runtime-slim
ghcr.io/openhands/agent-server:d48eb1f-python
ghcr.io/openhands/agent-server:d48eb1f095bf350d3355be5b6bd119cdbbc92fe5-python
ghcr.io/openhands/agent-server:fix-secret-lookup-under-state-lock-python
ghcr.io/openhands/agent-server:d48eb1f-python-node-runtime

About Multi-Architecture Support

  • Each variant tag (e.g., d48eb1f-python) is a multi-arch manifest supporting both amd64 and arm64
  • Docker automatically pulls the correct architecture for your platform
  • Individual architecture tags (e.g., d48eb1f-python-amd64) are also available if needed

…tools run

Under arun() each tool masks its observation in a worker thread while the
run loop holds the state lock. A LookupSecret made there could call a server
on the same event loop that registers secrets on the conversation, which
waits on that lock: the request blocked until httpx's 30 s timeout, the
lookup failed, and the value stayed unmasked in the observation.

Before running a tool batch, astep now resolves uncached lookups in a
worker thread with the lock released, repeating until the registry is
stable, as it already does before masking an agent message. Masking under
the lock then finds every value cached. The resolution loop moves out of
mask_secrets_in_output into SecretRegistry.resolve_uncached_values.

Fixes #6642

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013ArwwLKWzfpSTpRccheeWP
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-sdk/openhands/sdk/agent
   agent.py5173094%334, 490, 494, 843–844, 851–852, 985–987, 989, 1008–1009, 1015, 1020, 1055–1056, 1063–1064, 1109, 1116, 1163–1164, 1448, 1545–1546, 1548, 1578, 1627, 1634
openhands-sdk/openhands/sdk/conversation
   secret_registry.py178597%153, 427, 429, 432, 441
TOTAL54597851284% 

@datadog-official

This comment has been minimized.

@enyst
enyst marked this pull request as ready for review November 9, 2026 09:18
@enyst
enyst requested a review from all-hands-bot November 9, 2026 09:18

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

This review was posted by an AI agent (OpenHands).

Reviewed the diff, surrounding locking code, linked issues, and CI on head d48eb1f0. This is an in-scope Python SDK change (agent/secret behavior in this repository's ownership), so I continued with a technical review.

What I verified in the workspace:

  • The extracted SecretRegistry.resolve_uncached_values() is behavior-preserving: same back-off (FAILED_LOOKUP_RETRY_SECONDS), same _exported_values skip, same failure marking, and mask_secrets_in_output still resolves before masking. No new redaction path or sentinel is introduced — masking still funnels through the existing registry helpers, per the repo's secrets guidance.
  • The new resolution loop in Agent._aexecute_actions matches the two existing patterns in astep (message masking and _arequires_user_confirmation): snapshot secret_sources, run the lookup in a worker thread under conversation._released_state_lock_during_io(), and repeat until the registry is stable. lock.owned() makes the context manager a no-op for direct astep callers that don't hold the lock, and both _aexecute_actions call sites guard against empty batches.
  • The regression test exercises the real path — real arun(), real loopback HTTP server on the run's own event loop, real LookupSecret over httpx, and a real update_secrets() that takes the state lock while answering. No mocks. It passes on head in under a second; the identical scenario blocks on main because the pre-tool resolution does not exist there (30 s httpx timeout vs. immediate registration), which reproduces the issue's root cause rather than forging it.
  • Focused tests: tests/sdk/agent/test_async_secret_masking.py 5/5 pass. Broader: tests/sdk/agent 999 passed, tests/sdk/conversation 985 passed. The one failure I saw (test_local_conversation_mcp.py::test_slow_mcp_servers_start_side_by_side) also fails with main's code in this environment, so it is pre-existing and unrelated.
  • No public API, serialized-event, or persistence surface changes; the sync run() path is untouched, as documented. The known limitation (a secret registered by a tool during its own execution) is explicitly scoped out in the PR notes and was not part of the reported bug.

Non-blocking observations:

  • CI on this exact head is green except "Validate PR description". That check fails only because umbrella issue #6660 carries neither ready-for-dev nor a pre-rollout creation date — a repository issue-readiness gate, not a code defect. The directly linked fix issue #6642 does carry ready-for-dev and priority:high. Worth flagging to the author so the PR can unblock, but it does not affect the technical assessment of this change.
  • The stability loop repeats while lookups keep registering new sources. This matches the pre-existing design of the other two released-lock call sites and is bounded in practice; not a new concern.

✅ APPROVED

@all-hands-bot
all-hands-bot requested a review from neubig November 9, 2026 09:35
@enyst
enyst merged commit 457e730 into main Nov 9, 2026
84 of 89 checks passed
@enyst
enyst deleted the fix/secret-lookup-under-state-lock branch November 9, 2026 14:52
@all-hands-bot
all-hands-bot removed the request for review from neubig November 9, 2026 14:52
@enyst enyst mentioned this pull request Nov 11, 2026
1 of 5 tasks
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.

[Bug]: A secret looked up while masking a tool observation can stay unmasked (lookup blocks 30 s on the state lock)

3 participants