Skip to content

Fix conversation restore failing due to secret serialization/deserialization mismatch - #1672

Merged
tofarr merged 13 commits into
mainfrom
fix/ALL-4846-secret-serialization-deserialization
Jan 12, 2026
Merged

tofarr merged 13 commits into
mainfrom
fix/ALL-4846-secret-serialization-deserialization

Conversation

@tofarr

@tofarr tofarr commented Jan 9, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

This PR fixes an issue where V1 conversations fail to restore from persistence when the conversation state contains secrets in the secret_registry.

Problem

Secrets were serialized as redacted values (**********) but StaticSecret.value was a required field that couldn't accept None after validation converted ********** to None.

Solution (Two-part fix)

Part 1: Make secrets optional (prevents crash)

  • Made StaticSecret.value optional with None default
  • Updated StaticSecret.get_value() to handle None values gracefully
  • Fixed LookupSecret header validators to skip redacted headers instead of crashing

Part 2: Add cipher support (preserves secrets)

  • Added cipher parameter to ConversationState.create() and LocalConversation.__init__()
  • When a cipher is provided, secrets are encrypted on save and decrypted on load
  • EventService now passes its cipher to LocalConversation

How it works now

  1. With cipher (recommended for production):

    • Secrets are encrypted when saving state
    • Secrets are decrypted when loading state
    • Secret values are preserved across restarts
  2. Without cipher (fallback):

    • Secrets are redacted as ********** during serialization
    • Redacted secrets deserialize to StaticSecret(value=None)
    • Conversation loads successfully, but secrets need to be re-provided

Files Changed

  • openhands-sdk/openhands/sdk/secret/secrets.py - Make StaticSecret.value optional, fix LookupSecret headers
  • openhands-sdk/openhands/sdk/conversation/state.py - Add cipher support for encryption/decryption
  • openhands-sdk/openhands/sdk/conversation/impl/local_conversation.py - Add cipher parameter
  • openhands-agent-server/openhands/agent_server/event_service.py - Pass cipher to LocalConversation
  • tests/sdk/conversation/local/test_state_serialization.py - Add regression tests
  • tests/sdk/conversation/test_secret_source.py - Add regression tests

Checklist

  • If the PR is changing/adding functionality, are there tests to reflect this?
  • If there is an example, have you run the example to make sure that it works?
  • If there are instructions on how to run the code, have you followed the instructions and made sure that it works?
  • If the feature is significant enough to require documentation, is there a PR open on the OpenHands/docs repository with the same branch name?
  • Is the github CI passing?

@tofarr can click here to continue refining the PR

Fixes

Testing

image image image image image

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:7cc139d-python

Run

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

All tags pushed for this build

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

About Multi-Architecture Support

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

…ization mismatch (ALL-4846)

## Problem
V1 conversations fail to restore from persistence with a Pydantic validation
error when the conversation state contains secrets in the secret_registry.
The error occurs because secrets are serialized as redacted values ('**********')
but the StaticSecret model requires a non-null 'value' field.

## Root Cause
1. Secrets are serialized without cipher or expose_secrets context, resulting
   in '**********' (redacted) values
2. During deserialization, validate_secret() converts '**********' to None
3. StaticSecret.value was a required field (not Optional), causing validation
   to fail when None was returned

Similar issue existed in LookupSecret where assertions would fail for redacted
secret headers.

## Fix
1. Made StaticSecret.value optional with None default:
   `value: SecretStr | None = None`
2. Updated StaticSecret.get_value() to handle None values
3. Updated LookupSecret header validators to skip redacted headers instead
   of asserting (which would crash)

This allows conversations to be restored successfully, though the secrets
will need to be re-provided since they were redacted during serialization.

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

This enhancement builds on the previous fix (ALL-4846) by adding optional
cipher support to ConversationState and LocalConversation. When a cipher
is provided, secrets are encrypted during serialization and decrypted
during deserialization, preserving the actual secret values across
save/restore cycles.

Changes:
- ConversationState:
  - Added _cipher private attribute
  - Updated _save_base_state() to use cipher context for encryption
  - Updated create() to accept cipher parameter and pass it to
    model_validate() for decryption

- LocalConversation:
  - Added cipher parameter to __init__()
  - Pass cipher to ConversationState.create()

- EventService:
  - Pass cipher from EventService to LocalConversation

This allows the agent server to use the same cipher for both:
1. meta.json (StoredConversation) - already supported
2. base_state.json (ConversationState) - now supported

Without a cipher, secrets are still redacted (as '**********') and the
previous fix ensures redacted secrets can be deserialized to None without
crashing.

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

github-actions Bot commented Jan 9, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-agent-server/openhands/agent_server
   config.py61493%29, 42, 45, 172
   event_service.py31415849%55–56, 75–77, 81–86, 89–92, 107, 123, 127, 131–132, 139, 141, 148–149, 157–160, 167–169, 186, 210–211, 214–215, 217–219, 221, 226, 229–230, 233–235, 238, 242–244, 246, 248, 259–262, 275–276, 279–280, 283, 286–288, 291–292, 295–296, 300, 303, 307, 311–312, 314, 331–332, 349, 351, 355–357, 361, 370–371, 373, 377, 383, 385, 393–398, 448, 450–453, 462, 478, 485, 489, 500–501, 511–514, 516–517, 521, 523, 527–530, 535–537, 539, 543–546, 550–553, 561–564, 583–584, 586–593, 595–596, 605–606, 608–609, 616–617, 619–620, 624, 630, 640–641, 648
openhands-sdk/openhands/sdk/conversation
   state.py173696%144, 283, 329–331, 445
openhands-sdk/openhands/sdk/conversation/impl
   local_conversation.py2601893%251, 256, 298–299, 303–304, 384–385, 388, 534, 542, 544, 554, 556–558, 699–700
openhands-sdk/openhands/sdk/secret
   secrets.py64296%85, 88
TOTAL15004441870% 

@tofarr
tofarr marked this pull request as ready for review January 9, 2026 22:17

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

Overall approach is solid and tests pass. The two-part fix (making secrets optional + adding cipher support) correctly addresses the serialization/deserialization bug. A few suggestions below to improve production safety and debuggability.

@tofarr

tofarr commented Jan 9, 2026

Copy link
Copy Markdown
Collaborator Author

@OpenHands Please implement the suggestions left by all-hands-bot

@openhands-ai

openhands-ai Bot commented Jan 9, 2026

Copy link
Copy Markdown

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

@enyst enyst changed the title Fix conversation restore failing due to secret serialization/deserialization mismatch (ALL-4846) Fix conversation restore failing due to secret serialization/deserialization mismatch Jan 9, 2026
- Add warning when saving state without cipher but with secrets
- Add debug logging when headers are skipped during serialization/deserialization
- Add edge case tests for cipher mismatches:
  - Save with cipher, load without cipher
  - Save without cipher, load with cipher
  - Save with cipher A, load with cipher B

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

openhands-ai Bot commented Jan 9, 2026

Copy link
Copy Markdown

I have successfully implemented all the suggestions from the all-hands-bot PR review. Here's a summary of the changes pushed to PR #1672:

Changes Made

1. Added warning when secrets exist but no cipher is provided (state.py)

When saving conversation state without a cipher but with secrets in the registry, a warning is now logged:

Saving conversation state without cipher - X secret(s) will be redacted and lost on restore. Consider providing a cipher to preserve secrets.

2. Added debug logging for header handling (secrets.py)

  • Added logger import and initialization
  • Added debug logging when redacted headers are skipped during deserialization
  • Added debug logging when redacted headers are skipped during serialization

3. Added edge case tests for cipher mismatches (test_state_serialization.py)

Three new tests covering cipher edge cases:

  • test_conversation_state_save_with_cipher_load_without - Verifies that when state is saved with a cipher but loaded without one, the encrypted values remain as-is (unusable but conversation loads)
  • test_conversation_state_save_without_cipher_load_with - Verifies that when state is saved without a cipher but loaded with one, the redacted secrets deserialize to None
  • test_conversation_state_cipher_mismatch - Verifies that when state is saved with cipher A but loaded with cipher B, decryption fails gracefully (secrets set to None with warning logged)

Verification

  • All pre-commit hooks pass (ruff format, ruff lint, pycodestyle, pyright, import dependency rules)
  • All 31 related tests pass

The changes have been pushed to the branch fix/ALL-4846-secret-serialization-deserialization and are now part of PR #1672.

View full conversation

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

@tofarr thank you! This probably fixes #1505 ?

@enyst

enyst commented Jan 10, 2026

Copy link
Copy Markdown
Member

@OpenHands Read the full diff of this PR and replace ALL occurrences of “ALL-4846” with “issue 1505”. Double check the diff when you’re done. Commit and push.

@openhands-ai

openhands-ai Bot commented Jan 10, 2026

Copy link
Copy Markdown

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

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

openhands-ai Bot commented Jan 10, 2026

Copy link
Copy Markdown

Final summary (new work since my last summary)

Double-checks performed

  • Confirmed working tree is clean (git status)
  • Confirmed no remaining ALL-4846 in the full PR diff (git diff origin/main...HEAD | grep ALL-4846 returned nothing)
  • Ensured changes are concise and scoped to the request: only docstring text replacements in existing test files; no functional/code changes added

Changes made

  • Replaced all occurrences of ALL-4846 with issue 1505 in the PR diff (4 replacements), affecting only:
    • tests/sdk/conversation/local/test_state_serialization.py
    • tests/sdk/conversation/test_secret_source.py

Validation

  • Ran uv run pre-commit run --files ... on the modified files: passed

Version control

  • Committed only the relevant files
  • Commit message includes Co-authored-by: openhands <openhands@all-hands.dev>
  • Pushed updates to the existing branch fix/ALL-4846-secret-serialization-deserialization (PR updated)

View full conversation

@enyst
enyst requested a review from neubig January 10, 2026 15:36

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

Overall, LGTM just one small comment

@neubig neubig 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 was still a little bit confused after reading the comment, and after looking at the full file context I have a suggestion about how to DRY this up a little bit.

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.

Suggested change
SESSION_API_KEY_ENVS = ["SESSION_API_KEY", "OH_SESSION_API_KEYS_0"]

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I've updated the docs again - hopefully to make things clearer.

I think the confusion comes from understanding of the EnvParser which will automatically read pydantic models from the environment. These default methods are fallbacks for when we are doing something unusual - in this case grabbing the session_api_key from the legacy variable if it is not defined.

We use this EnvParser mechanism to configure complex structures like webhooks:
https://github.com/OpenHands/OpenHands/blob/7862e10f038d304ec79cdee46f7fb1e1842325bd/openhands/app_server/sandbox/docker_sandbox_service.py#L320

And we use it heavily in the app server for configuration - All the injected services can literally be replaced using the environment if needed.

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.

These default methods are fallbacks for when we are doing something unusual

We could maybe name those methods something else than default_ since in some sense, they're not actually the default values that just come into place, they're for when unusual handling is needed.

Comment on lines 20 to 24
result = []
session_api_key = os.getenv(SESSION_API_KEY_ENV)
if session_api_key:
result.append(session_api_key)
return result

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.

Suggested change
result = []
for env_var in SESSION_API_KEY_ENVS:
session_api_key = os.getenv(env_var)
if session_api_key:
result.append(session_api_key)
return result

Comment on lines 34 to 40
session_api_key = os.getenv(SESSION_API_KEY_ENV)
if session_api_key:
return SecretStr(session_api_key)
session_api_key = os.getenv("OH_SESSION_API_KEYS_0")
if session_api_key:
return SecretStr(session_api_key)
return None

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.

Suggested change
session_api_key = os.getenv(SESSION_API_KEY_ENV)
if session_api_key:
return SecretStr(session_api_key)
session_api_key = os.getenv("OH_SESSION_API_KEYS_0")
if session_api_key:
return SecretStr(session_api_key)
return None
session_api_keys = _default_session_api_keys()
if len(session_api_keys) > 0:
return SecretStr(session_api_keys[0])
return None

@openhands-ai

openhands-ai Bot commented Jan 11, 2026

Copy link
Copy Markdown

Looks like there are a few issues preventing this PR from being merged!

  • GitHub Actions are failing:
    • Run tests

If you'd like me to help, just leave a comment, like

@OpenHands please fix the failing actions on PR #1672 at branch `fix/ALL-4846-secret-serialization-deserialization`

Feel free to include any additional details that might help me get this PR into a better state.

You can manage your notification settings

@neubig neubig 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 think this works now!

@tofarr
tofarr merged commit c861820 into main Jan 12, 2026
@tofarr
tofarr deleted the fix/ALL-4846-secret-serialization-deserialization branch January 12, 2026 14:57
StressTestor pushed a commit to StressTestor/software-agent-sdk that referenced this pull request Jun 1, 2026
…ization mismatch (OpenHands#1672)

Co-authored-by: openhands <openhands@all-hands.dev>
Co-authored-by: enyst <engel.nyst@gmail.com>
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.

5 participants