Skip to content

fix: Include secrets in system prompt when added via update_secrets() - #2171

Merged
rbren merged 4 commits into
mainfrom
fix/secrets-in-prompt
Mar 1, 2026
Merged

rbren merged 4 commits into
mainfrom
fix/secrets-in-prompt

Conversation

@rbren

@rbren rbren commented Feb 22, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes an issue where secrets passed to LocalConversation were not appearing in the system prompt, even though the infrastructure for including them exists.

Problem

Previously, secrets passed to LocalConversation via:

  • The constructor's secrets parameter
  • The update_secrets() method

Were only added to the secret_registry for environment variable injection during command execution. They were NOT added to agent.agent_context.secrets, which meant they did not appear in the system prompt's <CUSTOM_SECRETS> section.

This meant the agent had no awareness of what secrets were available to it, unlike OpenHands web where secrets and their descriptions are always shown in the system prompt.

Solution

Updated update_secrets() in LocalConversation to also update agent.agent_context.secrets, ensuring that:

  1. Secrets are still registered in secret_registry for command execution (env var injection)
  2. Secrets are also added to agent.agent_context.secrets so they appear in the system prompt

Changes

  • local_conversation.py:

    • Updated update_secrets() to call new _update_agent_context_secrets() helper
    • Added _update_agent_context_secrets() helper method that merges secrets into agent_context
    • Added import for AgentContext
  • test_agent_secrets_integration.py: Added 5 new tests:

    • test_update_secrets_adds_to_agent_context
    • test_update_secrets_appears_in_system_prompt
    • test_update_secrets_merges_with_existing_context
    • test_update_secrets_overrides_existing_secret
    • test_secrets_via_constructor_appear_in_prompt

Testing

All 14 tests in test_agent_secrets_integration.py pass, including the 5 new ones.

Before/After

Before: Agent has no knowledge of available secrets in system prompt

# System prompt has no <CUSTOM_SECRETS> section

After: Agent sees available secrets in system prompt

<CUSTOM_SECRETS>
### Credential Access
* Automatic secret injection: When you reference a registered secret key...
...
You have access to the following environment variables
* **$GITHUB_TOKEN** - GitHub authentication token
* **$OPENAI_API_KEY** - OpenAI API key for LLM calls
</CUSTOM_SECRETS>

@rbren can click here to continue refining the PR


Agent Server images for this PR

• 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 amd64, arm64 nikolaik/python-nodejs:python3.12-nodejs22 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:181e94e-python

Run

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

All tags pushed for this build

ghcr.io/openhands/agent-server:181e94e-golang-amd64
ghcr.io/openhands/agent-server:181e94e-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:181e94e-golang-arm64
ghcr.io/openhands/agent-server:181e94e-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:181e94e-java-amd64
ghcr.io/openhands/agent-server:181e94e-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:181e94e-java-arm64
ghcr.io/openhands/agent-server:181e94e-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:181e94e-python-amd64
ghcr.io/openhands/agent-server:181e94e-nikolaik_s_python-nodejs_tag_python3.12-nodejs22-amd64
ghcr.io/openhands/agent-server:181e94e-python-arm64
ghcr.io/openhands/agent-server:181e94e-nikolaik_s_python-nodejs_tag_python3.12-nodejs22-arm64
ghcr.io/openhands/agent-server:181e94e-golang
ghcr.io/openhands/agent-server:181e94e-java
ghcr.io/openhands/agent-server:181e94e-python

About Multi-Architecture Support

  • Each variant tag (e.g., 181e94e-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., 181e94e-python-amd64) are also available if needed

Previously, secrets passed to LocalConversation via the constructor's
secrets parameter or update_secrets() method were only added to the
secret_registry for environment variable injection during command
execution. They were NOT added to agent.agent_context.secrets, which
meant they did not appear in the system prompt.

This fix ensures that when secrets are added:
1. They are still registered in secret_registry for command execution
2. They are also added to agent.agent_context.secrets so they appear
   in the system prompt's <CUSTOM_SECRETS> section

This brings the SDK behavior in line with OpenHands web, where secrets
and their descriptions are always shown in the system prompt to inform
the agent about available credentials.

Changes:
- Updated update_secrets() in LocalConversation to also update
  agent.agent_context.secrets
- Added _update_agent_context_secrets() helper method
- Added tests to verify secrets appear in the system prompt

Co-authored-by: openhands <openhands@all-hands.dev>
@github-actions

github-actions Bot commented Feb 22, 2026 •

Copy link
Copy Markdown
Contributor

API breakage checks (Griffe)

Result: Passed

Action log

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

🟡 Acceptable - Solves a real problem where agents had no visibility into available secrets. Tests are solid and verify actual behavior. One minor state consistency note inline.

@github-actions

github-actions Bot commented Feb 22, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-sdk/openhands/sdk/agent
   agent.py2403685%94, 98, 278–280, 282, 312–313, 320–321, 353, 406–407, 409, 449, 588–589, 594, 606–607, 612–613, 632–633, 635, 663–664, 671–672, 676, 684–685, 722, 728, 740, 747
openhands-sdk/openhands/sdk/context
   agent_context.py1421092%126–127, 148–149, 288–290, 319, 342, 348
openhands-sdk/openhands/sdk/conversation
   secret_registry.py57296%109, 142
openhands-sdk/openhands/sdk/conversation/impl
   local_conversation.py3321994%277, 282, 310, 375, 417, 563–564, 567, 719, 727, 729, 740, 742–744, 769, 931, 938–939
TOTAL18701563569% 

@enyst enyst left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for looking into this! I'd love if we can pause a tiny moment to figure out if this is something we should do in the SDK (seems so) and if so, how, as in the code design and consequences.

I think we are assuming in general that we provide the "infrastructure" and it's up to client applications to use it. That's why we may have things like an API to load skills from the agent-box, but not actually loading them by default anywhere in the SDK, the applications do.

In this case, I think maybe "updates" to the agent context feels a tad suspicious. The agent is supposed to get created with attributes etc, and then the attributes do not really change except in well defined ways. I'd love to know @xingyaoww 's thoughts about this. 🤔

…on.py

Co-authored-by: OpenHands Bot <contact@all-hands.dev>
@rbren

rbren commented Feb 22, 2026 •

Copy link
Copy Markdown
Member Author

Hmm I kind of agree the control flow here is bad.

Probably better for the agent to do something like get_system_context() to pull data about secrets, runtime state, etc into its prompts

But I do think it's a basic assumption that if you define secrets, the agent knows it has access to those secrets

@rbren

rbren commented Feb 22, 2026

Copy link
Copy Markdown
Member Author

@OpenHands change the control flow in the implementation here. have the agent request the secrets and add them to its prompt rather than modifying the agent from outside

@openhands-ai

openhands-ai Bot commented Feb 22, 2026

Copy link
Copy Markdown

I'm on it! rbren can track my progress at all-hands.dev

Changes the control flow for secrets in system prompts:
- Previously: LocalConversation.update_secrets() modified agent.agent_context
  from outside
- Now: Agent requests secrets from conversation state and adds them to its prompt

Implementation:
- Add get_secret_infos() method to SecretRegistry
- Add get_dynamic_context(state) method to Agent that pulls secrets from
  state.secret_registry
- Modify Agent.init_state() to use get_dynamic_context(state) instead of
  dynamic_context property
- Add additional_secret_infos parameter to AgentContext.get_system_message_suffix()
  to allow merging registry secrets with agent_context secrets
- Remove _update_agent_context_secrets() from LocalConversation
- Simplify update_secrets() to only update the registry

This design is cleaner because:
- The agent controls what goes into its own prompt (pull model)
- Secrets are stored in one place (secret_registry)
- No external modification of frozen agent state

Co-authored-by: openhands <openhands@all-hands.dev>
@openhands-ai

openhands-ai Bot commented Feb 22, 2026

Copy link
Copy Markdown

Summary

I've addressed @rbren's feedback by refactoring the control flow for secrets in the system prompt.

Changes Made

Control Flow Change:

  • Before: LocalConversation.update_secrets() pushed secrets into agent.agent_context.secrets (modifying the agent from outside)
  • After: Agent pulls secrets from state.secret_registry when building its system prompt

Files Changed:

  1. secret_registry.py - Added get_secret_infos() method to retrieve secret info for prompt inclusion
  2. agent.py - Added get_dynamic_context(state) method; updated init_state() to use it
  3. agent_context.py - Added additional_secret_infos parameter to get_system_message_suffix(); fixed return type of get_secret_infos() to list[dict[str, str | None]]
  4. local_conversation.py - Removed _update_agent_context_secrets() helper; simplified update_secrets() to only update registry; removed unused import
  5. test_agent_secrets_integration.py - Updated 5 tests to verify new control flow (checking registry and get_dynamic_context() instead of agent_context.secrets)

Checklist

  • ✅ Agent now requests/pulls secrets from conversation state
  • ✅ No external modification of agent's agent_context
  • ✅ Secrets stored in single canonical location (secret_registry)
  • ✅ All 14 tests pass
  • ✅ Pre-commit checks pass (type checking, linting, formatting)
  • ✅ Changes pushed to fix/secrets-in-prompt branch

@enyst
enyst requested a review from all-hands-bot February 22, 2026 18:28

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

🟡 Acceptable - Solves a real problem where agents had no visibility into available secrets. The approach is pragmatic: pull from the single source of truth (secret_registry) when building the prompt rather than duplicating state. Tests verify actual behavior. One efficiency note inline about the merge logic.

@enyst

enyst commented Feb 22, 2026

Copy link
Copy Markdown
Member

@OpenHands Do a /codereview-roasted on this PR. Investigate examples/ files too on this, to see how we work with agent context and secrets.

Once you are done, take a step back and reconsider your review and the PR diff with fresh eyes: are things as major or minor as they seemed at fight sight?

@openhands-ai

openhands-ai Bot commented Feb 22, 2026

Copy link
Copy Markdown

I'm on it! enyst can track my progress at all-hands.dev

@openhands-ai

openhands-ai Bot commented Feb 22, 2026

Copy link
Copy Markdown

Final summary:

  • I performed a /codereview-roasted style review of PR fix: Include secrets in system prompt when added via update_secrets() #2171 (branch fix/secrets-in-prompt) focusing on the new control flow where the agent pulls secret metadata from state.secret_registry and includes it in the system prompt dynamic context, plus the related changes to AgentContext and tests.

  • I audited relevant examples/ to confirm how the repo expects secrets and agent context to be used:

    • examples/01_standalone_sdk/12_custom_secrets.py uses conversation.update_secrets(...) (conversation/runtime-owned secrets).
    • examples/03_github_workflows/02_pr_review/agent_script.py passes secrets=... into Conversation(...) (conversation-owned secrets).
  • Review findings (brutally honest, constructive):

    • The fix addresses a real problem and is likely mergeable, but introduces maintainability “taste” issues:
      • Split-brain dynamic context: Agent.init_state() now uses a new state-dependent get_dynamic_context(state) while AgentBase.dynamic_context / deprecated system_message remain state-independent, creating inconsistency for debugging/consumers.
      • Throwaway AgentContext() to render secrets when agent_context is missing is a code smell (uses AgentContext as a templating utility and may pull in unrelated defaults like datetime).
      • Bad merge data structure: merging secrets as a list[dict] with list rebuilds is awkward, potentially quadratic, and can mishandle duplicates inside additional_secret_infos.
      • Mild duplication risk: both SecretRegistry and AgentContext now have get_secret_infos() concepts.
    • Testing note: current tests validate agent.get_dynamic_context(state) and secret presence, but a more regression-proof approach would assert against the actual emitted SystemPromptEvent.dynamic_context.
  • No code changes were made during this step; I only reviewed and summarized issues.

@enyst

enyst commented Feb 22, 2026

Copy link
Copy Markdown
Member

(OpenHands-GPT-5.2)

Taste Rating: Acceptable (solves a real problem, but the shape is kind of ugly)

You fixed the actual bug: secrets added to a LocalConversation (constructor + update_secrets()) now show up in the system prompt via the existing <CUSTOM_SECRETS> machinery. That’s a real UX gap vs web OpenHands, so the direction is right.

But the implementation introduces some “two sources of truth” and layering weirdness that will age poorly.


[CRITICAL ISSUES]

[openhands-sdk/openhands/sdk/agent/agent.py:187-243] You now have two different “dynamic context” concepts

  • Agent.init_state() uses self.get_dynamic_context(state) (state-dependent).
  • AgentBase.dynamic_context (property) still only reflects agent_context (state-independent).
  • AgentBase.system_message (deprecated, but still used) also won’t include registry secrets.

So: the real system prompt event contains secrets, but the public-ish agent properties do not. That’s inconsistent and will confuse anyone debugging prompts or doing anything custom with agent.dynamic_context / agent.system_message.

This is the core architectural smell: the agent is “stateless” by design, but you need state to compute prompt context. Either embrace that and make a single blessed codepath, or keep the agent stateless and stop pushing stateful data through AgentContext.

[agent.py:225-237] Creating a throwaway AgentContext() just to render secrets is a code smell

When self.agent_context is missing, you do:

  • local import
  • temp_context = AgentContext()
  • call get_system_message_suffix(...)

That’s not “minimal”; it drags in default current_datetime behavior and any future defaults in AgentContext, just because secrets exist. You’re effectively using AgentContext as a templating utility, not as configuration.

If all you need is “render the secrets section”, render the secrets section.

[agent_context.py:260-274] The secret merge algorithm is bad data-structure taste

You represent secrets as list[dict[name, description]] and then merge via:

  • membership set created once (existing_names = {...})
  • per-item list rebuild when overriding (secret_infos = [...])

This is exactly the kind of “list-of-dicts” nonsense that makes simple things complicated:

  • it’s O(n²) in the override case
  • existing_names doesn’t update when you append, so duplicate names within additional_secret_infos can duplicate entries
  • it’s harder to reason about ordering and override semantics than it needs to be

Use a dict[str, SecretInfo] keyed by name for merging, then render in a stable order. You can still preserve insertion order if you care.


[IMPROVEMENT OPPORTUNITIES]

Keep layering clean: AgentContext shouldn’t need “additional secrets”

AgentContext.get_system_message_suffix(..., additional_secret_infos=...) is basically admitting that AgentContext is no longer “agent configuration”, but a place where conversation state leaks in.

Cleaner options:

  1. Compute base_dynamic = agent.agent_context.get_system_message_suffix(...) (if any)
  2. Compute secrets_suffix = render_secrets(secret_registry) (separate, small helper/template)
  3. Concatenate if present

That avoids:

  • changing AgentContext API for a conversation concern
  • the “temp AgentContext” hack
  • the list-merge mess

Duplication: get_secret_infos() exists twice

AgentContext.get_secret_infos() and SecretRegistry.get_secret_infos() are basically the same concept. That’s manageable now, but it’s the start of drift. Pick one canonical representation/shape.

Docstring coupling

LocalConversation.update_secrets() now documents internal details like “agent pulls secrets via get_dynamic_context() during init_state()”. That’s the kind of comment that becomes wrong in 3 months. Describe behavior/contract, not internal call paths.


[TESTING GAPS]

The added tests (around agent.get_dynamic_context(state)) cover the intended behavior, but they test the helper rather than the actual emitted system prompt event.

A more regression-proof test would assert that after initialization, the SystemPromptEvent.dynamic_context contains <CUSTOM_SECRETS> and the secret names/descriptions. Right now it’s “probably correct” because init_state() calls the helper—but it’s indirect.


Examples audit (re: “how we work with agent context and secrets”)

  • examples/01_standalone_sdk/12_custom_secrets.py uses conversation.update_secrets(...) (conversation-level), not AgentContext(secrets=...). That matches the direction of this PR: secrets live with the conversation/runtime, not agent config.
  • examples/03_github_workflows/02_pr_review/agent_script.py passes secrets=... into Conversation(...). With this PR those secret names will now appear in the system prompt. That’s probably desirable (agent knows it can authenticate), but it’s a behavior change worth being aware of (prompt length + potentially different agent decisions).

VERDICT

Worth merging, because it fixes a real and user-visible mismatch.

But with fresh eyes: the problems here are mostly taste / maintainability, not immediate correctness bugs. The one thing I’d strongly reconsider (because it will keep biting) is the split-brain “dynamic context” API and the list-of-dicts merge logic—those are the seeds of future confusion.

@enyst enyst left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks clean to me, thank you! As you can see, OpenHands is not entirely happy, but I feel it's manageable in follow-ups.

Up to you if you wish a second opinion or could address some of the rest.

@enyst

enyst commented Feb 22, 2026

Copy link
Copy Markdown
Member

@OpenHands Look at the https://github.com/OpenHands/software-agent-sdk/blob/65cf6c227f5e56c4d328bfe77a012abba9c7a79b/.github/workflows/api-breakage.yml and its CI log from this PR.

Compare with the PR's diff. Shouldn't we have had some checks on the method signature change in this PR diff? Why did it succeed without reporting signature changes?

@openhands-ai

openhands-ai Bot commented Feb 22, 2026

Copy link
Copy Markdown

I'm on it! enyst can track my progress at all-hands.dev

@openhands-ai

openhands-ai Bot commented Feb 22, 2026

Copy link
Copy Markdown

I investigated the PR’s api-breakage.yml workflow and the check_sdk_api_breakage.py script, and compared them to the PR diff to explain why the job succeeded without reporting “method signature changes”.

What I did / checked

  • Read .github/workflows/api-breakage.yml (both this branch and origin/main): confirmed it’s configured to run only on release PR branches (head-branches: [rel-*]) and is non-blocking (continue-on-error: true).
  • Read .github/scripts/check_sdk_api_breakage.py: confirmed what it considers “public API” and what kinds of changes it detects.
  • Looked at the CI run log for the API breakage job on this PR: confirmed the script output was:
    • Comparing openhands-sdk 1.11.5 against 1.11.4 → No breaking changes detected
    • Same for openhands-workspace and openhands-tools
  • Compared against the PR diff: identified the relevant signature change in AgentContext.get_system_message_suffix(...) and noted it added only an optional trailing parameter.

Restated answer (why this wasn’t reported as breaking)

  • The “API breakage” check is not a general “report all signature changes” checker. It is intentionally scoped to the curated public surface (symbols exported via __all__, plus removed public members of exported classes), and it only flags breaking changes (removals/incompatible changes).
  • In this PR, the method signature change was backwards compatible because it added a new optional parameter (additional_secret_infos=...) at the end. Existing callers continue to work unchanged, so Griffe doesn’t treat it as a breaking change.
  • Also, several changes are outside what the checker treats as “public surface” (e.g., new methods or non-__all__ exports), so they wouldn’t be tracked for breakage at all.
  • Finally, even if there were breakages, the workflow is configured as non-blocking, so “green” doesn’t necessarily mean “no breakage”—but in this specific run, the script itself reported “No breaking changes detected.”

No code changes were made as part of answering this question.

Co-authored-by: OpenHands Bot <contact@all-hands.dev>
@all-hands-bot

Copy link
Copy Markdown
Collaborator

[Automatic Post]: It has been a while since there was any activity on this PR. @rbren, are you still working on it? If so, please go ahead, if not then please request review, close it, or request that someone else follow up.

@rbren
rbren merged commit 2b54375 into main Mar 1, 2026
25 checks passed
@rbren
rbren deleted the fix/secrets-in-prompt branch March 1, 2026 21:58
StressTestor pushed a commit to StressTestor/software-agent-sdk that referenced this pull request Jun 1, 2026
…OpenHands#2171)

Co-authored-by: openhands <openhands@all-hands.dev>
Co-authored-by: OpenHands Bot <contact@all-hands.dev>
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.

4 participants