Skip to content

fix(sandbox): accept a resumed runtime's rotated session key on the events webhook (OHE-3850) - #1154

Closed
hieptl wants to merge 2 commits into
mainfrom
hiep/ohe-3850-resync-session-key-hash-on-webhook-auth
Closed

hieptl wants to merge 2 commits into
mainfrom
hiep/ohe-3850-resync-session-key-hash-on-webhook-auth

Conversation

@hieptl

@hieptl hieptl commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

HUMAN:

  • A human has tested these changes.

AGENT:


Why

The runtime API rotates a sandbox's session key on resume and returns the new key only in the /resume response. It moves the runtime to STARTING first and writes the new key only after it has scaled the pod back up, which can take up to 30 seconds while the previous pod finishes terminating. The app server's client gives up after 15. RemoteSandboxService.resume_sandbox stores the new session_api_key_hash only after a 2xx, so when the response is lost or fails late with a 5xx, the stored hash stays on the old key, or stays empty after the pause cleared it.

valid_sandbox authenticates webhook posts with a database-only hash lookup, so it rejected every event the resumed pod posted with 401 until a later resume succeeded. The agent server gives up after its retries, so those events were lost: the reported conversation has a 155-minute hole in its history.

Re-reading the runtime right after the failed /resume would not have fixed it, because until the rotation lands the runtime record still carries the old key. The rotated key does reliably arrive on the webhook, so this reconciles there.

Summary

  • When the stored hash misses on POST /api/v1/webhooks/events/{conversation_id}, valid_sandbox loads the conversation and calls a new SandboxService.recover_sandbox_record_by_session_api_key for its sandbox. A key that is still rejected is now logged with the conversation id.
  • RemoteSandboxService accepts the key only when the runtime API reports the runtime STARTING or RUNNING with exactly that key, then stores its hash so the indexed lookup hits again. Paused runtimes, rotated-out keys and failed runtime lookups are refused and leave the hash alone. The base class default accepts nothing, so the other backends are unchanged.
  • 9 new tests in tests/unit/app_server/test_remote_sandbox_service.py and tests/unit/app_server/test_webhook_router_auth.py cover the reported scenario, the refusals and the valid_sandbox fallback.

Issue Number

OHE-3850. Fixes #1030.

How to Test

uv run pytest tests/unit/app_server/test_remote_sandbox_service.py tests/unit/app_server/test_webhook_router_auth.py
uv run pytest tests/unit/app_server/test_sandbox_lifecycle_contract.py \
  tests/unit/app_server/test_sandbox_secrets_router.py \
  tests/unit/app_server/test_child_conversation_router.py \
  tests/unit/app_server/test_webhook_router_acp.py \
  tests/unit/app_server/test_webhook_router_auto_title.py \
  tests/unit/app_server/test_webhook_router_mcp_oauth_state.py \
  tests/unit/app_server/test_webhook_router_parent_conversation.py \
  tests/unit/app_server/test_webhook_router_secrets.py \
  tests/unit/app_server/test_webhook_router_stats.py \
  tests/unit/app_server/test_webhook_router_tags.py

344 tests pass across both commands (the database tests start Postgres through Docker). Of the 9 new tests, 7 fail on main. The other 2 pin behavior that must not change: a key the runtime does not confirm, and a request with no conversation id in its path, still get 401. The pre-commit hooks pass on the changed files, including ruff 0.12.5 and mypy.

To check it by hand on a test environment with the remote runtime:

  1. Start a conversation and wait for its sandbox to run.
  2. Set that sandbox's session_api_key_hash in v1_remote_sandbox to NULL, which is what a paused sandbox whose resume response was lost is left with.
  3. Send a message. On main the agent server's event posts get 401 and nothing new is stored for the conversation. With this change the first post is accepted, the app server logs Updated session_api_key_hash for sandbox <id> from the runtime API after a stale-hash webhook rejection, and the column holds the key's hash again.

I have not run this on a deployed environment yet.

Video/Screenshots

Backend only.

Type

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

Notes


Enterprise server image for this PR:

ghcr.io/openhands/enterprise-server:sha-b533485

…vents webhook (OHE-3850)

A /resume that fails on the app server can still complete on the runtime
API. The runtime API moves the runtime to STARTING first and writes the
rotated session key only after it has scaled the pod back up, which can
take longer than the app server's client timeout when the previous pod
is still terminating. resume_sandbox stores the new key's hash only
after a 2xx, so the stored hash stayed on the old key. valid_sandbox
authenticates webhook posts with that hash alone, so it rejected every
event the resumed pod posted with 401 until a later resume succeeded,
and the agent server dropped those events after its retries.

Re-reading the runtime right after the failed /resume is not enough:
until the rotation lands, the runtime record still carries the old key.
The rotated key reliably shows up on the webhook, so the fix is there.

- When the hash lookup misses on /events/{conversation_id}, valid_sandbox
  loads the conversation and asks the sandbox service to re-check the
  key against that conversation's sandbox.
- RemoteSandboxService accepts the key only when the runtime API reports
  the runtime STARTING or RUNNING with exactly that key, and then stores
  its hash so the indexed lookup hits again. Paused runtimes, rotated-out
  keys and failed runtime lookups are refused and leave the hash alone.
  The SandboxService default accepts nothing, so other backends are
  unchanged.
- A key that is still rejected is logged with the conversation id.

Tests cover the reported scenario, where a timed-out resume is followed
by the rotated key being accepted and stored, the refusals, and the
valid_sandbox fallback.
@github-actions github-actions Bot added the type: fix A bug fix label Nov 3, 2026
@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  openhands/app_server/event_callback
  webhook_router.py 275-276, 300
  openhands/app_server/sandbox
  remote_sandbox_service.py 460
  sandbox_service.py 252
Project Total  

This report was generated by python-coverage-comment-action

@hieptl
hieptl marked this pull request as ready for review November 4, 2026 07:35
@hieptl hieptl closed this Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: fix A bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Resume that fails on the app server but completes on the runtime API leaves a stale session key hash, so the runtime's events get 401 for hours

1 participant