Skip to content

fix(conversation): pass cipher to forked conversations (#5465) - #5473

Closed
kyletser wants to merge 1 commit into
OpenHands:mainfrom
kyletser:fix/fork-cipher-inheritance-5465
Closed

kyletser wants to merge 1 commit into
OpenHands:mainfrom
kyletser:fix/fork-cipher-inheritance-5465

Conversation

@kyletser

@kyletser kyletser commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

HUMAN: I reviewed the fix, the test evidence, and the maintainer feedback, and I am comfortable submitting this change for review.


AGENT:

This PR was prepared with AI assistance. The fix, tests, and description follow the maintainer's review of the initial submission.

Why

LocalConversation.fork() built the fork without the source conversation's cipher, so the fork's base_state.json stored the LLM api_key redacted (**********) instead of encrypted. The agent-server resumes every fork from that file with agent=None, so the fork's first turn reached the LLM with the redacted placeholder and failed with LLMAuthenticationError, while the source kept working.

Secrets added at runtime through update_secrets() (POST /conversations/{id}/secrets) were dropped as well: they live only in the source's secret_registry, and __init__ re-seeds only agent_context/request secrets on the fork.

Fixes #5465

Summary

  • fork() forwards self._cipher to the fork's LocalConversation(...) constructor, so at-rest secrets are encrypted with the same cipher the source uses.
  • fork() copies the source's secret_registry sources. Reassigning the public field triggers the state autosave, so the fork's base_state.json persists them encrypted.

How to Test

  • tests/sdk/conversation/local/test_fork.py::test_fork_inherits_cipher_for_secret_encryption reads the fork's persisted base_state.json and asserts the key decrypts back to the source's credential (observable behavior: no private-state assertions).
  • tests/sdk/conversation/local/test_fork.py::test_fork_keeps_runtime_added_secrets asserts an update_secrets()-added token survives the fork, in-memory and encrypted at rest.
  • tests/agent_server/test_conversation_service.py::test_cipher_backed_fork_resumes_with_credentials covers the user-facing path: a cipher-backed ConversationService forks a conversation, the server restarts, and the fork resumes with its LLM credentials and runtime secret intact.

All three tests fail on main without the fix and pass with it.

Additional Notes

  • Verification on the rebased head: tests/sdk/conversation/local/test_fork.py 22 passed, tests/agent_server/test_conversation_service.py 139 passed, ruff check/ruff format clean, pyright 0 errors.
  • The branch is rebased on the current main.

Copilot AI balanced review requested due to automatic review settings October 3, 2026 08:09

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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

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

Thank you @kyletser, the fix itself is right. On a local agent-server, ConversationService._cipher_for returns the same server cipher for every conversation id. So the key the fork is now encrypted with is the key the server decrypts with when it resumes the fork with agent=None, both right after the fork and after a restart. Docker mode is the only setup with per-conversation keys, and its fork route returns 501, so that mismatch can't happen. We ran a small script against this head: a ConversationService with a cipher forks a conversation and then restarts. The fork comes back with the real LLM key. With the line reverted, it comes back None. The same line also keeps MCP env secrets and agent_context.secrets intact on the fork, which were silently lost before. The new test fails without the fix, tests/sdk/conversation passes (864 tests), and the branch rebases onto current main without conflicts.

A few things before this can merge:

Needs changes

  • pyright fails on the new test, so Pre-commit checks will go red (see the inline comment on test_fork.py:381).
  • Version bump guard fails only because the branch is 29 commits behind main. It compares the packages as 1.53.0 → 1.51.0. Merging or rebasing onto current main clears it.
  • PR description: the required check wants the repo template, which needs a first line HUMAN: with a short note in your own words, then AGENT:, and the ## Summary and ## How to Test sections.

Suggestions (from #5465's own acceptance criteria; either here or as a follow-up, but then the issue shouldn't close on this PR alone)

  • Runtime secrets are still dropped by fork. Secrets added with POST /conversations/{id}/secrets go into the conversation's secret_registry, and fork() never copies it. In our script the fork lost a secret added that way while the source kept it. The triage asks for those to be preserved too.
  • An agent-server test: the triage asks for coverage under tests/agent_server/, and it doesn't need a live LLM. Building a ConversationService with a cipher, forking, resuming the fork and asserting its llm.api_key would cover the user-facing path.
  • Test style (nit): the test asserts private state (fork._state._cipher is src._state._cipher) and calls _save_base_state directly. AGENTS.md prefers observable behavior. Reloading the fork's persisted state with the cipher and asserting the key would survive refactors.

@kyletser
kyletser force-pushed the fix/fork-cipher-inheritance-5465 branch from bf67b53 to 583fb5e Compare October 12, 2026 08:07
@kyletser

Copy link
Copy Markdown
Contributor Author

Pushed 583fb5e addressing the review, rebased on current main:

  • pyright: the persistence_dir line is gone; the tests compute the fork dir from its public id and pass pyright cleanly.
  • Runtime-added secrets: fork() now copies the source secret_registry (update_secrets additions), persisted encrypted via the field-autosave.
  • agent-server coverage: new test_cipher_backed_fork_resumes_with_credentials builds a cipher-backed ConversationService, forks, restarts the service, and asserts the fork resumes with llm.api_key and the runtime secret.
  • Test style: the SDK test asserts observable behavior only — it reads the fork's persisted base_state.json and decrypts the key back to the source credential.

All three tests fail without the fix. Conversation-local suite (234), agent-server service suite (132), ruff, and pyright all pass. The PR description now follows the repo template.

enyst pushed a commit that referenced this pull request Oct 22, 2026
Adapt the server restart regression test from #5473, source commit 583fb5e. Keep the existing registry merge and deep-copy implementation in #5765.

Co-authored-by: openhands <openhands@all-hands.dev>
…penHands#5465)

LocalConversation.fork() built the fork without the source cipher, so
the fork's base_state.json stored the LLM api_key redacted instead of
encrypted, and its first resumed turn failed with LLMAuthenticationError.
Runtime-added secrets (update_secrets) were dropped as well.

Forward self._cipher to the fork constructor and copy the source
secret_registry, reassigning the public field so the autosave persists
the sources encrypted with the fork's cipher.

Co-authored-by: openhands <openhands@all-hands.dev>
@kyletser
kyletser force-pushed the fix/fork-cipher-inheritance-5465 branch from 583fb5e to 8934ffa Compare October 24, 2026 07:34
@kyletser

Copy link
Copy Markdown
Contributor Author

Rebased onto main at 87bec04de and pushed head 8934ffa3a; the PR is MERGEABLE again (it was CONFLICTING before the rebase).

Also updated in this push:

  • The AGENT section now uses the template's ## Summary heading and the exact AGENT: marker line.
  • Verification notes reflect the rebased head: tests/sdk/conversation/local/test_fork.py 22 passed, tests/agent_server/test_conversation_service.py 139 passed, ruff check/ruff format clean, pyright 0 errors.

The remaining Validate PR description errors are the two HUMAN-section rules (the first visible line must be HUMAN: alone, with the note on following lines). Those are in the human author's section, so they need the human author to apply.

@neubig

neubig commented Nov 13, 2026

Copy link
Copy Markdown
Member

For #5465 we are driving #5765 to merge: it carries the same cipher fix, also copies the source's secret registry, and already incorporates this PR's server-restart regression test (test_cipher_backed_fork_resumes_with_credentials, with authorship preserved). It has been updated with current main and now has before/after Agent Canvas evidence in its description. Since this branch conflicts with main and is superseded, it can be closed once #5765 lands. Thank you for the test!

This comment/PR was created by an AI agent (OpenHands) on behalf of Graham Neubig.

@kyletser

Copy link
Copy Markdown
Contributor Author

Closing as superseded now that #5765 has landed — thanks for pointing it out, and glad the server-restart regression test carried over.

@kyletser kyletser closed this Nov 16, 2026
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 forked conversation loses its LLM API key and fails its first turn with LLMAuthenticationError

4 participants