Repository navigation
fix(agent-server): backfill redacted LLM api_key on agent_settings la… - #4539
vnktadithya wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a regression in the agent-server conversation start flow where launching via a raw agent_settings payload (seeded default profile path) could drop redacted LLM secret fields (notably api_key) to None, causing authentication failures. It backfills missing LLM secret fields from server-persisted settings while preserving any explicit client-supplied secret values, and adds regression tests to cover both the redacted-payload and explicit-key scenarios.
Changes:
- Backfill
LLM_SECRET_FIELDSfrom persistedagent_settings.llmwhen starting a conversation viaagent_settingsand the validated agent still hasNonefor those secrets. - Preserve explicit client-provided
api_key(never clobber it with persisted values). - Add two regression tests covering redacted-key backfill and “do not override explicit key”.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
openhands-agent-server/openhands/agent_server/conversation_service.py |
Backfills missing LLM secret fields from persisted settings for the agent_settings launch path. |
tests/agent_server/test_conversation_service.py |
Adds regression tests for redacted secret backfill and for not overriding an explicit client key. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
enyst
left a comment
There was a problem hiding this comment.
Hey @vnktadithya , thank you for the PR. I don’t think this is a good solution though:
- technically, we will likely simplify design to use LLM Profiles only, and thus clean up the full “agent settings” that contain LLM profiles in the future
- it adds code to deal with a case that shouldn’t exist, and I’d love to understand more about it.
I wonder, why did the client fetch without X-Expose-Secrets? My agent may have done that, but that… is suspicious then. Maybe, as long as this API exists, it could document better that if used without this, then it will NOT return the API keys? |
|
Thanks for digging into this, and fair question. I didn't trace the exact call site in Canvas's frontend code myself — my diagnosis was scoped to the backend/SDK side, where I could confirm that once a redacted payload lands, nothing recovers the real value before launch. So I can't say precisely why the fetch happened without X-Expose-Secrets, only that the original report (#4533) reproduced it via the standard self-hosted Canvas + agent-server setup, not a custom client. Given the direction in #4288 (reference-only credentials and safe runtime delivery) and OpenHands/OpenHands#15563 (Agent config UX: building blocks + agent profiles (per-profile behavior, no global defaults)), I agree a backend backfill on agent_settings isn't the right fix — it adds to a mechanism you're moving away from rather than the actual model you're heading toward. Better documenting the GET /api/settings contract, as you suggested. Closing this without merging — appreciate the discussion. |
HUMAN:
Resolves #4533. When a conversation launches via a raw settings payload, the frontend sends that payload to the backend with the API key masked as a string of asterisks — which is expected, since the backend never returns real secrets to the client. What was actually missing was a step to fetch the real API key back from the server once that masked payload reached the backend. Fixed it, added tests, and verified everything locally.
AGENT:
Why
Starting a conversation via the seeded default agent profile (the
agent_settingsfallback path, as opposed toagent_profile_id) fails withlitellm.AuthenticationErroreven when the LLM is fully configured and its API key is saved server-side. The named-agent-profile launch path already works correctly for the same LLM config, so this is a regression specific to one of the two launch paths, not a key/model/cipher problem.Summary
Root cause:
When a client fetches settings via
GET /api/settingswithoutX-Expose-Secrets, secret fields come back redacted(
"**********"). If that redactedagent_settingspayload is later sent toPOST /api/conversations,StartConversationRequest's_populate_agent_from_settingsvalidator converts it into anAgentviavalidate_agent_settings(...).create_agent(). During that construction,validate_secret()sees the redaction marker and converts it toNone(by design, so redacted round-trips never leak"**********"as a literal credential). Unlike theagent_profile_idlaunch path — which always re-resolves credentials fresh from the server's persisted settings — theagent_settingsfallback path had no equivalent recovery step, so the agent launched withllm.api_key = Noneand the first LLM call failed authentication.Fix:
In
ConversationService._start_conversation, when the request came in viaagent_settings(notagent_profile_id), backfill any still-NoneLLM secret field (api_key,aws_access_key_id,aws_secret_access_key,aws_session_token) from the server'spersisted
agent_settings.llm, but only for fields the client didn't already supply a real value for — an explicit client-supplied key is never overridden. This mirrors the existingagent_profile_idpath's behavior of trusting server-persisted credentials over apossibly-redacted client payload.
Note:
AgentandLLMare frozen Pydantic models, so the fix builds the updatedLLMandAgentviamodel_copy(update=...)rather than direct attribute assignment, consistent with how the adjacentagent_profile_idbranch already rebuildsrequestin this function.Issue Number
Fixes #4533
How to Test
Result: all passing (3 tests: the 2 new regression tests plus the existing encrypted-agent-settings test, confirming no interference).
Result: 110 passed.
Result: Ruff format, Ruff lint, pycodestyle, pyright, import rules, and Tool subclass registration checks all passed.
test_start_conversation_backfills_redacted_llm_api_key_from_agent_settings—reproduces the reported bug (redacted
api_keyin theagent_settingspayload) and asserts the launched agent carries the real, server-persisted key instead ofNone.test_start_conversation_agent_settings_does_not_override_explicit_llm_api_key—asserts a client-supplied real key is never clobbered by the persisted server value.
test_restart_resumes_conversations_after_non_graceful_shutdownfails locally on Windows due to a platform difference inos.kill(pid, 0)liveness detection insideconversation_lease.py(Windows raises a genericOSErrorfor an invalid/synthetic PID rather than POSIX'sProcessLookupError, so the conservative fallback reports the forged lease as still alive). This test doesn't touchagent_settingsat all, and passes on Linux both with and without this change; expected to pass on the repo's Linux CI.Video/Screenshots
N/A — backend fix covered by unit tests.
Type
Notes
Verified against the real reported failure mode: I traced the redaction →
validate_secret()→Nonepath inpydantic_secrets.pyandrequest.py, and confirmed empirically that the two agent classes involved (Agent,ACPAgent) are frozen Pydantic models — direct attribute mutation on.llmraisesValidationError: Instance is frozen, which shaped themodel_copy-based approach used here.