Skip to content

fix(agent-server): backfill redacted LLM api_key on agent_settings la… - #4539

Closed
vnktadithya wants to merge 1 commit into
OpenHands:mainfrom
vnktadithya:fix/agent-settings-llm-api-key-4533
Closed

vnktadithya wants to merge 1 commit into
OpenHands:mainfrom
vnktadithya:fix/agent-settings-llm-api-key-4533

Conversation

@vnktadithya

Copy link
Copy Markdown
Contributor

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_settings fallback path, as opposed to agent_profile_id) fails with litellm.AuthenticationError even 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/settings without X-Expose-Secrets, secret fields come back redacted
("**********"). If that redacted agent_settings payload is later sent to POST /api/conversations, StartConversationRequest's
_populate_agent_from_settings validator converts it into an Agent via validate_agent_settings(...).create_agent(). During that construction, validate_secret() sees the redaction marker and converts it to None (by design, so redacted round-trips never leak "**********" as a literal credential). Unlike the agent_profile_id launch path — which always re-resolves credentials fresh from the server's persisted settings — the agent_settings fallback path had no equivalent recovery step, so the agent launched with llm.api_key = None and the first LLM call failed authentication.

Fix:
In ConversationService._start_conversation, when the request came in via agent_settings (not agent_profile_id), backfill any still-None LLM secret field (api_key, aws_access_key_id, aws_secret_access_key, aws_session_token) from the server's
persisted 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 existing agent_profile_id path's behavior of trusting server-persisted credentials over a
possibly-redacted client payload.

Note:
Agent and LLM are frozen Pydantic models, so the fix builds the updated LLM and Agent via model_copy(update=...) rather than direct attribute assignment, consistent with how the adjacent agent_profile_id branch already rebuilds request in this function.

Issue Number

Fixes #4533

How to Test

  1. Tests
uv run pytest tests/agent_server/test_conversation_service.py -q -k "backfill or explicit_llm_api_key or decrypts_encrypted_agent_settings"

Result: all passing (3 tests: the 2 new regression tests plus the existing encrypted-agent-settings test, confirming no interference).

uv run pytest tests/agent_server/test_conversation_service.py -q

Result: 110 passed.

  1. Repository checks
uv run pre-commit run --files openhands-agent-server/openhands/agent_server/conversation_service.py tests/agent_server/test_conversation_service.py

Result: Ruff format, Ruff lint, pycodestyle, pyright, import rules, and Tool subclass registration checks all passed.

  1. Two new regression tests were added:
  • test_start_conversation_backfills_redacted_llm_api_key_from_agent_settings —
    reproduces the reported bug (redacted api_key in the agent_settings payload) and asserts the launched agent carries the real, server-persisted key instead of None.
  • 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.
  1. Known environment quirk (not related to this change)

test_restart_resumes_conversations_after_non_graceful_shutdown fails locally on Windows due to a platform difference in os.kill(pid, 0) liveness detection inside conversation_lease.py (Windows raises a generic OSError for an invalid/synthetic PID rather than POSIX's ProcessLookupError, so the conservative fallback reports the forged lease as still alive). This test doesn't touch agent_settings at 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

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

Notes

Verified against the real reported failure mode: I traced the redaction → validate_secret() → None path in
pydantic_secrets.py and request.py, and confirmed empirically that the two agent classes involved (Agent, ACPAgent) are frozen Pydantic models — direct attribute mutation on .llm raises ValidationError: Instance is frozen, which shaped the
model_copy-based approach used here.

Copilot AI lite review requested due to automatic review settings August 19, 2026 03:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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_FIELDS from persisted agent_settings.llm when starting a conversation via agent_settings and the validated agent still has None for 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 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.

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.

@enyst

enyst commented Aug 19, 2026

Copy link
Copy Markdown
Member

When a client fetches settings via GET /api/settings without X-Expose-Secrets, secret fields come back redacted ("**********").

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?

@vnktadithya

Copy link
Copy Markdown
Contributor Author

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.

@vnktadithya
vnktadithya deleted the fix/agent-settings-llm-api-key-4533 branch August 20, 2026 05:26
@vnktadithya
vnktadithya restored the fix/agent-settings-llm-api-key-4533 branch August 20, 2026 05:39
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.

Conversation launch drops LLM api_key when the seeded default agent profile is active (litellm AuthenticationError)

3 participants