Repository navigation
Conversation
…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.
Contributor
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||||||||||||||||||||
hieptl
marked this pull request as ready for review
November 4, 2026 07:35
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
HUMAN:
AGENT:
Why
The runtime API rotates a sandbox's session key on resume and returns the new key only in the
/resumeresponse. 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_sandboxstores the newsession_api_key_hashonly 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_sandboxauthenticates 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
/resumewould 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
POST /api/v1/webhooks/events/{conversation_id},valid_sandboxloads the conversation and calls a newSandboxService.recover_sandbox_record_by_session_api_keyfor its sandbox. A key that is still rejected is now logged with the conversation id.RemoteSandboxServiceaccepts 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.tests/unit/app_server/test_remote_sandbox_service.pyandtests/unit/app_server/test_webhook_router_auth.pycover the reported scenario, the refusals and thevalid_sandboxfallback.Issue Number
OHE-3850. Fixes #1030.
How to Test
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, includingruff0.12.5 and mypy.To check it by hand on a test environment with the remote runtime:
session_api_key_hashinv1_remote_sandboxtoNULL, which is what a paused sandbox whose resume response was lost is left with.mainthe 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 logsUpdated 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
Notes
/resumeresponse as a success. This one leavesresume_sandboxand the read paths as they are and re-syncs the hash where the rotated key arrives.POST /webhooks/conversationsandPOST /webhooks/mcp-oauth-statecarry no conversation id, so they cannot recover by themselves. They are accepted again once the first event post has re-synced the hash, and the agent server retries them.resume_sandboxstill answers 502 when its/resumecall fails, even if the runtime API went on to complete the resume. That is unchanged frommain.Enterprise server image for this PR: