Skip to content

fix(sdk): decrypt encrypted fallback profiles - #4568

Draft
Sehlani042 wants to merge 2 commits into
OpenHands:mainfrom
Sehlani042:codex/fix-fallback-profile-cipher
Draft

Sehlani042 wants to merge 2 commits into
OpenHands:mainfrom
Sehlani042:codex/fix-fallback-profile-cipher

Conversation

@Sehlani042

@Sehlani042 Sehlani042 commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

HUMAN:


AGENT:

Why

Encrypted LLM profiles are decrypted on the main conversation path, but fallback profiles are resolved lazily through a separate LLMProfileStore that did not receive the conversation cipher. As a result, FallbackStrategy could pass a Fernet ciphertext (gAAAAA...) to the provider as an API key. This also affected strategies that had cached a fallback before being attached to a conversation.

Summary

  • Bind the conversation cipher to FallbackStrategy, invalidate previously resolved fallback caches when the cipher changes, and pass the cipher explicitly when lazily loading each fallback profile.
  • Preserve current-main profile_store_dir behavior when initializing the conversation's profile store.
  • Add deterministic regressions for encrypted fallback resolution before/after conversation binding and encrypted profile switching through a custom profile directory.

Issue Number

Fixes #4558

How to Test

Reproduction and regression:

  1. Save a fallback LLM profile with include_secrets=True and a Cipher.
  2. Resolve it through an unbound FallbackStrategy; the old path exposes the encrypted gAAAAA... token.
  3. Create LocalConversation(..., cipher=cipher) with that strategy.
  4. Resolve the fallback again and verify the API key is the original plaintext value.
  5. Load an encrypted profile through LocalConversation(profile_store_dir=..., cipher=...) and verify both the custom directory and decryption remain effective.

Commands and results:

uv run pytest -q \
  tests/sdk/llm/test_llm_profile_store.py \
  tests/sdk/llm/test_llm_fallback.py \
  tests/sdk/conversation/test_switch_model.py
# 117 passed
uv run pre-commit run --files \
  openhands-sdk/openhands/sdk/conversation/impl/local_conversation.py \
  openhands-sdk/openhands/sdk/llm/fallback_strategy.py \
  tests/sdk/conversation/test_switch_model.py \
  tests/sdk/llm/test_llm_fallback.py
# Ruff format/lint, PEP8, Pyright, import rules, and Tool registration passed

The repository-wide dynamic-attribute hook reports four findings in untouched current-main files (stream_context.py and telemetry.py). The same four findings reproduce in a detached origin/main@9c3571a69 worktree, so they are not introduced by this patch.

git diff --check also passes.

Video/Screenshots

Not applicable; this is an SDK behavior fix covered by deterministic executable regressions.

Design Doc

Not included; the change is limited to cipher propagation and cache invalidation across existing SDK objects while preserving current-main profile-store configuration.

Type

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

Notes

Codex assisted with issue triage, implementation, current-main reconciliation, and test execution. The patch was constrained to the fallback-profile cipher path and checked for competing pull requests before publication.

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

github-actions Bot commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor
  ✅ **PR Artifacts Cleaned Up**

  The `.pr/` directory is no longer present.

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

This review was created by an AI agent (OpenHands) on behalf of the repository maintainers.

Formal APPROVED on current head dd0a74bd

This formal APPROVED review is submitted on the current head so branch protection can consume it. It supersedes the prior dismissed APPROVED on 59f82df and the COMMENTED review on dd0a74bd.

Change Since Prior Approval

The only commit difference between the previously approved head (59f82df) and the current head (dd0a74bd) is the automated cleanup removing .pr/test-results.png — a non-functional artifact deletion. No source code, test, or configuration changes were introduced. The implementation and live Docker validation findings from the prior review remain fully applicable.

Summary of Verified Findings

All prior review findings remain resolved on the current head:

  1. Planning conversation hardcoded LocalWorkspace — ✅ Resolved. buildStartPlanningConversationRequest (line 1387) and buildStartPlanningConversationRequestWithEncryptedSettings (line 1575) both accept executionRuntime and select DockerExecutionWorkspace when the server advertises Docker execution. All three entry points (main conversation, planner, encrypted-settings wrapper) are covered.

  2. Type narrowing loss — ✅ Resolved. All executionRuntime parameters and return types use AgentServerInfo["execution_runtime"], resolving to "local" | "docker" | undefined. Typos fail at compile time.

  3. Evidence gap — ✅ Resolved by live validation against SDK head 7096d432:

    • /server_info returns execution_runtime: "docker"
    • Parent conversation creation with DockerExecutionWorkspace + /workspace works against a real Docker execution server
    • Planner child request with parent_conversation_id + plannerparent tag succeeds with DockerExecutionWorkspace, /workspace, and correct parent relationship
    • 73/73 focused tests pass across agent-server-adapter.test.ts (30) and agent-server-conversation-service.test.ts (43)

CI Status

All current-head CI is green: test-and-build (ubuntu), test-and-build (windows), mock-llm-e2e (70/70), mock-llm-docker-e2e (70/70), and build/push pipelines all pass.

Risk Assessment

🟢 LOW — All prior findings are resolved. The only change since the prior approval is an automated artifact cleanup commit. The code is clean, type-safe, and verified at both the unit and live end-to-end level. The remaining action item is merging the SDK dependency (software-agent-sdk#4883), which is outside this PR's control.

Verdict: Approve — the implementation is complete and verified.

Generated by OpenHands AI on behalf of the user.

@all-hands-bot

Copy link
Copy Markdown
Collaborator

Thanks for the PR — I reviewed the implementation. The cipher-binding approach and default-cipher-with-explicit-precedence in LLMProfileStore look correct, and the regression test (unbound→ciphertext, bound→plaintext) is exactly the right assertion.

One blocking issue: stale branch will regress profile_store_dir. The branch is based on 1de2e6d1 (2026-08-20), but main has since merged #4908 (v1.46.0, 2026-09-09), which added a profile_store_dir arg to LocalConversation and changed the store construction to LLMProfileStore(profile_store_dir). This PR replaces it with LLMProfileStore(cipher=cipher) and drops profile_store_dir from __init__. Merged onto current main that silently loses the custom-dir path and breaks tests/sdk/conversation/test_switch_model.py::test_switch_profile_uses_custom_profile_store.

Please rebase onto main and thread both args: LLMProfileStore(profile_store_dir, cipher=cipher).

Minor/optional: the _cipher is cipher identity guard is fine, but cipher == cipher would also catch equal-but-distinct Cipher instances.

@Sehlani042

Copy link
Copy Markdown
Contributor Author

Addressed the current-main compatibility issue in 854463d without force-pushing.

  • merged current main and preserved LocalConversation(profile_store_dir=...) when initializing LLMProfileStore
  • adapted the cipher path to the current API: LLMProfileStore no longer accepts a constructor-level cipher, so fallback resolution now passes the bound conversation cipher explicitly to load(...)
  • tightened the switch-profile regression to cover a custom profile directory and encrypted secret together

Verification on the merged head: both complete related test files pass (53 passed); Ruff format/lint, pycodestyle, Pyright, import-dependency checks, Tool registration, and git diff --check pass. The repository-wide dynamic-attribute hook still reports the same four untouched-current-main findings in stream_context.py and telemetry.py; I reproduced that identical output in a detached origin/main worktree.

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.

Encrypted LLM profiles are decrypted only on the conversation path — FallbackStrategy (and sub-agents) load them without the cipher

2 participants