Skip to content

fix(subagent): decrypt named LLM profiles with the conversation cipher - #6461

Merged
hieptl merged 1 commit into
mainfrom
hiep/ohe-3897-subagent-profile-cipher
Nov 4, 2026
Merged

hieptl merged 1 commit into
mainfrom
hiep/ohe-3897-subagent-profile-cipher

Conversation

@hieptl

@hieptl hieptl commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

HUMAN:

Sub-agents that select a saved LLM profile by name were loading it without the cipher, so the encrypted API key was sent to the provider as-is. This threads the owning conversation's cipher through sub-agent registration, mirroring what top-level profile resolution already does. I reviewed the diff and the tests; the new tests fail on main without the source change and pass with it.


AGENT:

Why

When OH_SECRET_KEY is set, LLMProfileStore.save encrypts a profile's secrets at rest. The sub-agent factory in openhands/sdk/subagent/registry.py loaded a model: <profile> profile with store.load(profile_name) and no cipher, so validate_secret left the Fernet token in place and the sub-agent's LLM (and its default condenser) sent the ciphertext as the API key. Providers reject it, for example LiteLLM Virtual Key expected. Received=gAAA****. Top-level profile resolution (profiles/resolver.py, LocalConversation.get_or_create_profile_llm) already passes the cipher, which is why the same profile works for the main agent but not for Markdown or plugin sub-agents.

Summary

  • agent_definition_to_factory, register_file_agents and register_plugin_agents accept a keyword-only cipher (default None) and pass it to LLMProfileStore.load.
  • LocalConversation forwards its cipher when registering file-based and plugin agents; the agent-server forwards the server cipher in _register_agent_definitions for both new and resumed conversations.
  • Regression tests cover the named-profile, Markdown and plugin registration paths, the LocalConversation call sites, and the agent-server create and resume paths.

Issue Number

Fixes #4270, fixes #5564, fixes OpenHands/OpenHands#17384. Covers the sub-agent call site of #4558; the FallbackStrategy call site tracked there is unchanged.

How to Test

  1. uv run pytest -q tests/sdk/subagent/test_subagent_registry.py tests/sdk/conversation/test_local_conversation_agent_plugins.py tests/sdk/conversation/test_local_conversation_file_agents.py tests/agent_server/test_conversation_service.py tests/tools/task tests/tools/delegate tests/tools/test_builtin_agents.py — 343 passed.
  2. uv run pre-commit run --files <changed files> — Ruff, pycodestyle, Pyright and the import-rule checks pass.
  3. The seven new tests fail on main with only the three source files reverted (the sub-agent's api_key is still the gAAAAA… token) and pass with this change.
  4. End to end: start the agent-server with OH_SECRET_KEY set, save a profile through POST /api/profiles/{name} with an API key, add .agents/agents/worker.md with model: <that profile>, and delegate a task to worker. Before this change the sub-agent's first LLM call fails authentication; with it the call uses the plaintext key, and the stored profile JSON keeps the encrypted value.

Video/Screenshots

Not a visual change; see the test and reproduction notes above.

Design Doc

Not needed for a change of this size.

Type

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

Notes

  • The new parameters default to None, so existing callers (including register_builtins_agents, whose agents all use model: inherit) are unaffected, and plaintext profiles load exactly as before.
  • Out of scope and tracked separately: the FallbackStrategy._iter_fallbacks load (Encrypted LLM profiles are decrypted only on the conversation path — FallbackStrategy (and sub-agents) load them without the cipher #4558), and child conversations spawned by the task and delegate tools, which still do not receive the parent cipher. That only matters for nested delegation that discovers new agents inside the child, since the shared registry keeps the parent's cipher-aware factory for names already registered.
  • Downstream consumers pick this up through the next release.

Sub-agents that set `model: <profile>` loaded the profile without the
cipher that encrypted it at rest, so the stored Fernet token reached the
provider as the API key and authentication failed. Top-level profile
resolution already passed the cipher; the sub-agent factory did not.

- Accept a keyword-only `cipher` in `agent_definition_to_factory`,
  `register_file_agents` and `register_plugin_agents`, and pass it to
  `LLMProfileStore.load` so the sub-agent LLM and its default condenser
  receive the decrypted key.
- Forward the conversation cipher from `LocalConversation` when it
  registers file-based and plugin agents, and the server cipher from the
  agent-server when it registers forwarded or persisted definitions.
- Add regression tests for the named-profile, Markdown and plugin paths,
  for `LocalConversation`, and for the agent-server create and resume
  paths.

`model: inherit` and plaintext profiles are unchanged; the new parameter
defaults to `None` for every existing caller.
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-agent-server/openhands/agent_server
   conversation_service.py127310592%186–187, 269, 279–280, 284–285, 290, 461–462, 523, 628, 635–636, 737, 828, 879–880, 887, 919–920, 936, 967, 971, 983, 1003, 1024–1025, 1034, 1103, 1113, 1138, 1144–1145, 1149–1150, 1158, 1185, 1191, 1285, 1291, 1296, 1302, 1310–1311, 1320–1323, 1332, 1345, 1363, 1400, 1404–1408, 1431, 1480, 1493, 1501, 1559–1560, 1564, 1770, 1848, 1972, 1975, 1982–1984, 1987–1988, 1994, 2004, 2008, 2037, 2046, 2105–2107, 2138, 2269, 2272, 2299, 2309, 2314–2317, 2331, 2342, 2354–2355, 2387, 2485, 2545, 2603, 2618–2619, 3025, 3078, 3081, 3094
openhands-sdk/openhands/sdk/conversation/impl
   local_conversation.py11605695%169, 383, 450, 720–721, 816, 1201–1202, 1207, 1222, 1224, 1295, 1393, 1419, 1508, 1513–1518, 1571, 1592–1594, 1643, 1978–1979, 2250, 2253–2254, 2465, 2501, 2505, 2508, 2527, 2579, 2582, 2614, 2621–2622, 2630, 2634–2636, 2643, 2940–2941, 2959–2960, 2993, 3205, 3209, 3279, 3286–3287
openhands-sdk/openhands/sdk/subagent
   registry.py142696%118, 250–251, 255, 327–328
TOTAL53896856684% 

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

🍰

@hieptl
hieptl merged commit 13f21f3 into main Nov 4, 2026
42 of 45 checks passed
@hieptl
hieptl deleted the hiep/ohe-3897-subagent-profile-cipher branch November 4, 2026 19:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants