Skip to content

fix(sandbox): Report a replaced sandbox session and rotate old sessions - #3710

Merged
dcramer merged 8 commits into
mainfrom
fix/sandbox-replaced-notice
Nov 22, 2026
Merged

dcramer merged 8 commits into
mainfrom
fix/sandbox-replaced-notice

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

When a Vercel Sandbox session stops during a Turn, two things go wrong today:

  1. The provider kills the running command with a signal and returns a normal command result, so the call reads as an ordinary non-zero exit and never reaches the sandbox-unavailable tool error.
  2. The next call reacquires the sandbox. A non-persistent sandbox resumes into a new session from its source snapshot, and the agent is not told. Local commits and other files are gone. An agent that then pushes HEAD can push an old commit over its own work.

Vercel stops a session 24 hours after it starts, so a long conversation can lose its session in the middle of a Turn.

Context: #3047. #3703 is the push-guard backstop for the same failure.

Change

  • Kill path. After a SIGTERM or SIGKILL exit, the bash executor probes the session with the probe that cached sessions already use. A stopped session throws the provider error. That marks the session unavailable and goes through the existing createSandboxUnavailableToolError path, which says the outcome is not reliable and the next call uses a fresh session. When the session is live, or the probe fails for another reason, the command's own exit stands.
  • Replacement notice. SandboxRef.toolSession stores the sandbox and session id of the last sandbox tool call (bash, readFile, writeFile, editFile). It is saved in thread state next to app_sandbox_id, so it survives Run and execution slice boundaries. A call on a different session gets a sandbox_notice in its result when the earlier files are gone: the Sandbox changed, or a new session started on a Sandbox that does not keep files across sessions. The notice says that files and local commits are gone and to check HEAD and the branch before acting. This also covers a conversation that stayed idle past the keepalive and resumes into a new session.
    • SandboxSession.keepsFilesAcrossSessions reports this. Vercel maps it from sandbox.persistent. Docker is always false, because a restarted container keeps its id, so a new session id is a new container.
    • Only tool calls update the stored session, so the between-step AGENTS.md read cannot hide a replacement.
    • switchWorkspace clears it, because the switch result already says that files do not carry over.
  • Session rotation at Turn start. SandboxSession.startedAtMs gives the session start time. On Vercel it comes from the session's startedAt, or createdAt when startedAt is not set. On Docker it comes from the container's StartedAt. When the first Run of a Turn reopens a stored sandbox whose session is older than 20 hours, it stops that session and resumes a new one before any work starts. A Run that resumes a paused Turn keeps its session, so rotation does not drop unpushed work in the middle of a Turn. A sandbox that does not keep files gets the replacement notice on its next tool call.
  • The bash, file, and command executors move to sandbox/tool-executors.ts to keep session.ts under the file length limit. The only behavior change there is the probe.

Reused: the sandbox-unavailable tool error and session invalidation, the probe from the reacquire path in #3515, and the stored SandboxRef with its existing onSandboxRefChanged persistence. Unknown outcomes from cancelled commands (#915) stay as they are.

Limits and open questions

  • Turn start after recovery. "Turn start" means the Turn has no paused record. When recovery restarts a Turn after a lost worker without a paused record, that Run can still rotate an old session.
  • Persistent sandboxes. Rotation stops the session and resumes it right away. Conversation sandboxes are non-persistent today. For a persistent sandbox, a resume while the stop still snapshots has not been checked against the live API.
  • Plugin tools. A plugin tool that runs a command through the sandbox records the new session too, and it drops sandbox_notice. If the first call after a replacement is a plugin tool, the agent does not see the notice.
  • Stop timing. The kill check assumes the session API rejects the probe right after the provider kills the command. If a stopping session still accepts the probe, the kill is reported as a normal exit, and the next call still gets the replacement notice.

via @dcramer.

--

View Session [Sentry]

@vercel

vercel Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
junior-docs Ready Ready Preview Nov 22, 2026 12:20am UTC

Request Review

@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

5 screenshot changes — 5 changed · 0 added · 0 removed

Review screenshots in Frameshift

Automations Runs Filters · Desktop
Automations Runs Filters · Desktop
Changed
Conversation Long Transcript Memory · Desktop
Conversation Long Transcript Memory · Desktop
Changed
Conversations Sections Collapsed · Desktop
Conversations Sections Collapsed · Desktop
Changed
Inbox Palette Pages · Desktop
Inbox Palette Pages · Desktop
Changed
System Cache Warming · Desktop
System Cache Warming · Desktop
Changed

A command killed by a session stop came back as a normal exit (143), and
the next call ran on a new sandbox without the old files and said nothing.

- After a 137 or 143 exit, probe the session. A stopped session fails the
  call through the existing sandbox-unavailable tool error.
- When a sandbox tool call runs on a new sandbox that lost the earlier
  files, its result gets a sandbox_notice that says so and asks the agent
  to check its checkout first.

Refs #3047, #3703

Co-Authored-By: David Cramer <[email protected]>
@dcramer

dcramer commented Nov 21, 2026

Copy link
Copy Markdown
Member

With persistence, how do we clean up the persistent volumes? Does that automatically happen, or is there some kind of garbage collection we're going to need or something else

Also, given we have execution slice boundaries, that is the heartbeat and requeuing and everything regularly, any kind of limitation around that is a concern

@sentry-junior

sentry-junior Bot commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Cleanup: there are no persistent volumes. Conversation sandboxes are created with persistent: false (createFreshSandbox and createSandboxCandidate in chat/sandbox/session.ts), so Vercel throws the files away when a session stops. Only the Workspace snapshot build sandbox is persistent, and this PR does not change it. The "persistence" in this PR is two short strings, the sandbox id and session id of the last tool call. They go into the thread-state scratch key that already holds app_sandbox_id, with the same 7-day TTL (JUNIOR_THREAD_STATE_TTL_MS). Nothing new needs garbage collection.

Slice boundaries: storing the tool session is what removes the slice limitation. Before, the "last tool session" lived in per-Run memory, so a requeue or a new slice forgot it, and the notice could only fire inside one slice. Now every Run reads it from the stored sandbox ref, and the first tool call of any slice compares against it. Resuming a new session after the keepalive lapses, or after a requeue, still produces the notice.

One behavior to know about: the ~20h session rotation happens the first time each Run acquires the sandbox, so at the start of every slice, not only at the start of a Turn. A Turn that crosses 20h therefore rotates at its next slice boundary, before any command runs. For a non-persistent sandbox, that drops files in the middle of the Turn, but the agent gets the replaced-sandbox notice on its next call. The alternative is losing them at the 24h cap while a command is running. Checking only at Turn start would leave a long Turn exposed to the cap, so I think the slice boundary is the right point. If you want Turn start only, tell me and I'll change it.

These two pieces aren't pushed yet. They're in the next commit on this branch.

Store the sandbox and session id of the last sandbox tool call in the
conversation's sandbox ref, so a later Run still reports a new session
that lost the earlier files. Replace a resumed session older than 20
hours before work starts, so the provider's 24-hour session cap does not
stop a command mid-Turn.
A Run that resumes a paused Turn keeps its session, so rotation cannot
drop unpushed work in the middle of the Turn.
@sentry-junior sentry-junior Bot changed the title fix(sandbox): Report a sandbox that stopped or was replaced mid-Turn fix(sandbox): Report a replaced sandbox session and rotate old sessions Nov 21, 2026
@dcramer
dcramer marked this pull request as ready for review November 21, 2026 23:46
@sentry-junior
sentry-junior Bot requested a review from dcramer November 21, 2026 23:46
@github-actions github-actions Bot added the risk: high PR risk score: high label Nov 21, 2026
@dcramer

dcramer commented Nov 21, 2026

Copy link
Copy Markdown
Member

Is this sufficiently and correctly covered by integration tests? I want to make sure that whatever the behavior here that we were seeing, we're actually able to regression test against in the future

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit b9a86cf. Configure here.

@dcramer

dcramer commented Nov 21, 2026

Copy link
Copy Markdown
Member

I'm not sure what the open questions are

@sentry-junior

sentry-junior Bot commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Short answer: each part of the failure has a regression test now, but at the component layer, not integration. One link in the chain is only covered by an opt-in suite.

Why component: policies/testing.md doesn't allow faking @vercel/sandbox in integration tests, so sandbox-provider faults go in tests/component/misc/sandbox-executor.test.ts. Those tests run the real createSandbox and tool executors and fake only the SDK.

What fails if it regresses (sandbox-executor.test.ts):

  • A command exits 143 and its session is gone. The tool call fails as sandbox unavailable instead of returning a normal exit, and the next call, on the new session, carries sandbox_notice. Tested with persistent and non-persistent sandboxes.
  • A later Run reopens the sandbox from the stored ref and gets a new session. It gets the notice once.
  • At Turn start, a session older than 20h is stopped and replaced, and the result carries the notice.
  • A resumed Run keeps an old session.
  • A real signal exit while the session is still live stays a normal command result. This is the false-positive guard.
  • thread-state.test.ts round-trips the stored tool session.

Gap I closed in 3ac87cd: nothing checked that the agent run passes startsTurn correctly. agent-run-provider-retry.test.ts now asserts true for a new Turn and false for a resumed one. With startsTurn: true hardcoded, the resume test fails.

Still not covered in CI:

  • Real provider behavior: whether a stopped Vercel session really shows up as a 143 followed by a 410 on the next call. The fake models it that way, and that matches what we saw. The only real-provider suite is docker-sandbox-provider.test.ts, and it's opt-in (JUNIOR_DOCKER_SANDBOX_TEST). I could add a stop-and-resume case there, but CI wouldn't run it.
  • Whether the agent actually re-checks its checkout after the notice. That's an eval question. The push guard in fix(github): Refuse moving a branch to a commit GitHub already has #3703 is the backstop for that step.

Comment thread packages/junior/src/chat/agent/index.ts Resolved
@sentry-junior

sentry-junior Bot commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Fair, that section heading oversells it. None of the four items under "Limits and open questions" needs a decision from you. They're known limits:

  • Turn start after recovery: a Turn restarted after a lost worker, with no paused record, counts as a new Turn and can rotate an old session. Rare, and the agent still gets the notice.
  • Persistent sandboxes: I haven't checked stop-then-resume on a persistent sandbox against the live API. Conversation sandboxes are all non-persistent today, so this doesn't apply yet.
  • Plugin tools: if the first call after a replacement is a plugin tool that runs a sandbox command, its result doesn't carry the notice.
  • Stop timing: if a stopping session still accepts the probe, the kill reads as a normal exit. The next call still gets the notice.

The one real open question came from Bugbot's inline comment "Rotation proceeds after a failed stop". Right now, if stopping the old session fails at Turn start, the Turn keeps running on the old session. That's how it worked before this PR, and the kill path plus the notice cover a later cap kill. The alternative is to fail the Turn start when the stop fails. Do you want it to fall back (current) or fail closed?

@dcramer

dcramer commented Nov 22, 2026

Copy link
Copy Markdown
Member

I don't totally love the way we're passing this information around, but the core is quite a lot of complexity right now, and I don't have an obvious better solution

@dcramer
dcramer merged commit bbb0297 into main Nov 22, 2026
57 checks passed
@dcramer
dcramer deleted the fix/sandbox-replaced-notice branch November 22, 2026 00:39

This branch was successfully deployed

1 active deployment
Preview – junior-docs — b462c313 Deployed Nov 22, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: high PR risk score: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant