fix(ai-sandbox-boxd): clean up cancelled sandbox startup #1607
main fromrefs/pull/1607/head·merged by tombeckenham View on GitHubIf you abort Boxd create or snapshot restore during startup, the provider returned a handle and left the new machine running. This PR checks the abort signal after each startup call. An abort now deletes the new machine and rejects with the abort reason.
🎯 Changes#
adopt()inpackages/ai-sandbox-boxd/src/provider.tschecks the abort signal after the readiness wait and after the workspacemkdir.- Two unit tests cover the two checks. One E2E case runs against the built package.
docs/sandbox/providers.mddescribes the cancellation behavior.- A patch changeset is included.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested code changes locally with
pnpm run test:pr, or these tests do not apply to this pull request. - I fully understand the code in this pull request, including any code generated with AI assistance.
- Docs: I updated
docs/for this change, or this change is not user-facing. - Changeset: I added a changeset (
pnpm changeset), or this PR does not change a published package.
The full test:pr suite did not run locally. The Testing section lists the focused commands that ran.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Root cause#
Issue. A caller aborts create or restoreSnapshot while the new machine becomes ready, or while the provider makes the workspace directory. The call resolves with a handle, and the machine continues to run.
Cause. adopt() checked the abort signal only before waitUntilReady. It did not check after waitUntilReady or after the mkdir exec. ensure() in @tanstack/ai-sandbox does not check the signal after create returns.
Fix. adopt() checks the signal after each of the two calls. An abort goes to the existing cleanup path, which tries to delete the machine and throws the abort reason again.
Possible alternatives#
- One check before
return handle. This is one line less and stops the same leak. This PR uses two checks, so an abort during readiness does not run amkdiron a machine that the provider then deletes. - A check in
ensure()afterprovider.create. This change would cover each provider. It is a larger change in a different package, so this PR does not include it. - Cancel the SDK call.
waitUntilReadyand the one-shot exec do not accept an abort signal, so this is not possible today.
Testing#
Commands run
A reviewer-written repro test on pinned main
d31e4ebb9and on PR head3d8cb4094. The repro abortscreateduring readiness, then duringmkdir.main (d31e4ebb9) × create: abort while waiting for readiness rejects and deletes the machine × create: abort during workspace mkdir rejects and deletes the machine AssertionError: expected { resolved: 'vm-9' } to deeply equal { rejected: Error: stop-readiness } AssertionError: expected { resolved: 'vm-9' } to deeply equal { rejected: Error: stop-mkdir } Tests 2 failed (2) pr (3d8cb4094) Tests 2 passed (2)On head
be602cd89, frompackages/ai-sandbox-boxd:pnpm exec vitest runpassed (65 passed, 8 skipped).pnpm run test:typesandpnpm run test:oxlintpassed.On head
be602cd89: Playwright ran onlysandbox-boxd-startup-cancellation.spec.tswith a minimal config, afternx run @tanstack/ai-sandbox-boxd:build. 1 passed.With each of the two new checks removed in turn, one unit test failed.
Not run:
pnpm test:prand the full E2E suite. CI runs them.
Manual test
- On
main, add a test that aborts the signal inside a mockedmachines.waitUntilReady. Then callboxdSandbox({ apiKey: 'k' }).create({ signal }). The call resolves with a handle. - On this branch, run
pnpm exec vitest run tests/provider.test.ts -t "cancellation during adoption"frompackages/ai-sandbox-boxd. Both tests pass. - Run
pnpm run build:all, thenpnpm --filter @tanstack/ai-e2e test:e2e -- --grep "boxd create cleans up". The spec passes.
How this PR makes testing easy
packages/ai-sandbox-boxd/tests/provider.test.ts: two tests, one for each new check.testing/e2e/tests/sandbox-boxd-startup-cancellation.spec.ts: one case against the built package.
The SDK machine calls are stubs. No test starts a live Boxd machine.
Risk / rollback#
Risk is low. Two signal checks are added to the startup of new and restored machines. The delete is best effort: if the delete call fails, the machine continues to run and the provider does not log it. This behavior is the same as on main. To undo, revert this PR.
Summary by CodeRabbit#
Bug Fixes
- Machines created during startup are now deleted if creation or snapshot restoration is cancelled, including when cancellation occurs during readiness or workspace setup. Cleanup happens after the current SDK call finishes.
- Cancelled operations return the original cancellation reason. If cancellation occurs before workspace setup, setup and subsequent commands are skipped.
Documentation
- Clarified what happens to newly created machines when startup is cancelled.
- 1
Navigate logical layers of code changes, visualize relationships, and explore their blast radius.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
- Configuration used: Repository: TanStack/ai/.coderabbit.yaml
- Review profile: CHILL
- Plan: Advanced
- Run ID:
f48b14a8-a2b2-450a-adb8-023e5313d776
📥 Commits
Reviewing files that changed from the base of the PR and between 3d8cb4094a1088eaa8bee442feecc553bc17f923 and be602cd8969bd26e28f15c36922254b6d06345c1.
📒 Files selected for processing (3)
docs/sandbox/providers.mdpackages/ai-sandbox-boxd/tests/provider.test.tstesting/e2e/tests/sandbox-boxd-startup-cancellation.spec.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- docs/sandbox/providers.md
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.
📝 Walkthrough
Walkthrough#
BoxdProvider now checks for cancellation after machine readiness and workspace creation. If either check detects cancellation, the provider deletes the machine and rethrows the abort reason. Unit and end-to-end tests cover cancellation during creation and snapshot restoration.
Changes#
Boxd startup cancellation cleanup
Layer / File(s) Summary Cancellation cleanup during startup
packages/ai-sandbox-boxd/src/provider.ts,packages/ai-sandbox-boxd/tests/provider.test.ts,testing/e2e/tests/sandbox-boxd-startup-cancellation.spec.ts,docs/sandbox/providers.md,.changeset/tidy-boxd-cancellation.mdadoptchecks for cancellation after readiness and workspace creation. On cancellation, it deletes the machine and rethrows the abort reason. Unit and end-to-end tests cover cleanup. The documentation and changeset describe the cleanup.Priority: ➖ Normal
Estimated code review effort: 2 (Simple) | ~10 minutes
Change: Bug fix
Merge Risk: ⚪ Minimal · up to
be602This change makes cancelled Boxd startup delete the new machine instead of returning a running handle. No merge-blocking risk was identified.
Architecture Summary#
Architecture risk: 🔵 Low · up to
be602The change affects 3 systems.
Changed systems:
packages/ai-sandbox-boxd,docs,testingArchitecture concerns No architecture-level concerns identified.
Review details
Systems and components
- observed — packages/ai-sandbox-boxd (library) was modified; 2 changed files map to changed impact.
- observed — docs (service) was modified; 1 changed file maps to changed impact.
- observed — testing (service) was modified; 1 changed file maps to changed impact.
Before / after behavior
- observed — Modified behavior in packages/ai-sandbox-boxd/src/provider.ts:
adoptadds abort checks after readiness and workspace creation; an abort at either point enters the existing cleanup path, which deletes the machine and rethrows. - observed — Modified behavior in docs/sandbox/providers.md: The boxd documentation adds cancellation behavior: when create or snapshot restore is aborted during startup, the provider tries to delete the new machine after the current SDK call finishes.
- observed — Modified behavior in packages/ai-sandbox-boxd/tests/provider.test.ts: Added a test in which the readiness mock aborts the create signal and returns a machine; it expects rejection with the abort reason, deletion of
vm-1, and noexeccall. - observed — Modified behavior in packages/ai-sandbox-boxd/tests/provider.test.ts: Added a snapshot-restore test in which the workspace-setup
execmock aborts the signal; it expects rejection with the abort reason and deletion ofvm-1.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 3… Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request. Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request. Title check ✅ Passed The title clearly and concisely describes the main change: cleaning up Boxd sandboxes when startup is cancelled. Description check ✅ Passed The description includes the required Changes, Checklist, and Release Impact sections. It explains the change, documents testing and its limitations, and notes the changeset and documentation updates. ✨ Finishing Touches
🧪 Generate unit tests (beta)
- Create a new PR
- Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Autopilot is currently an internal CodeRabbit preview.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment `@coderabbitai help` to get the list of available commands.github-actions[bot](GitHub profile, opens in a new tab) assignedAlemTuzlak
Thanks for the PR, @Tyagiquamar! 🙌 @AlemTuzlak will take a look.
Automated pre-review checks
- ✅ CI passing
- ✅ No merge conflicts
- ✅ Changeset present
- ✅ E2E test changes included
github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: maintainer
tombeckenham assignedtombeckenham
tombeckenham unassignedAlemTuzlak
tombeckenham approved these changes
Thank you for this.
View your CI Pipeline Execution ↗ for commit be602cd8969bd26e28f15c36922254b6d06345c1
Command Status Duration Result nx run-many --targets=build --exclude=examples/...✅ Succeeded 6s View ↗
☁️ Nx Cloud last updated this comment at
2026-10-04 10:17:59UTC@tanstack/ai
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai@1607@tanstack/ai-acp
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-acp@1607@tanstack/ai-angular
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-angular@1607@tanstack/ai-anthropic
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-anthropic@1607@tanstack/ai-bedrock
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-bedrock@1607@tanstack/ai-byteplus
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-byteplus@1607@tanstack/ai-claude-code
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-claude-code@1607@tanstack/ai-client
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-client@1607@tanstack/ai-cloudflare
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-cloudflare@1607@tanstack/ai-code-mode
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-code-mode@1607@tanstack/ai-code-mode-snippets
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-code-mode-snippets@1607@tanstack/ai-codex
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-codex@1607@tanstack/ai-cohere
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-cohere@1607@tanstack/ai-compaction
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-compaction@1607@tanstack/ai-devtools-core
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-devtools-core@1607@tanstack/ai-durable-stream
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-durable-stream@1607@tanstack/ai-elevenlabs
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-elevenlabs@1607@tanstack/ai-event-client
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-event-client@1607@tanstack/ai-fal
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-fal@1607@tanstack/ai-gemini
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-gemini@1607@tanstack/ai-grok
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-grok@1607@tanstack/ai-grok-build
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-grok-build@1607@tanstack/ai-groq
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-groq@1607@tanstack/ai-isolate-cloudflare
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-cloudflare@1607@tanstack/ai-isolate-daytona
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-daytona@1607@tanstack/ai-isolate-node
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-node@1607@tanstack/ai-isolate-quickjs
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-quickjs@1607@tanstack/ai-isolate-quickjs-bun
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-quickjs-bun@1607@tanstack/ai-llmgateway
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-llmgateway@1607@tanstack/ai-lovable
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-lovable@1607@tanstack/ai-mcp
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-mcp@1607@tanstack/ai-memory
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-memory@1607@tanstack/ai-mistral
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-mistral@1607@tanstack/ai-octane
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-octane@1607@tanstack/ai-ollama
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-ollama@1607@tanstack/ai-ollaya
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-ollaya@1607@tanstack/ai-openai
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-openai@1607@tanstack/ai-opencode
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-opencode@1607@tanstack/ai-openrouter
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-openrouter@1607@tanstack/ai-perplexity
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-perplexity@1607@tanstack/ai-persistence
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-persistence@1607@tanstack/ai-preact
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-preact@1607@tanstack/ai-react
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-react@1607@tanstack/ai-react-ui
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-react-ui@1607@tanstack/ai-reactor
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-reactor@1607@tanstack/ai-remix
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-remix@1607@tanstack/ai-sandbox
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox@1607@tanstack/ai-sandbox-blaxel
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-blaxel@1607@tanstack/ai-sandbox-boxd
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-boxd@1607@tanstack/ai-sandbox-cloudflare
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-cloudflare@1607@tanstack/ai-sandbox-daytona
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-daytona@1607@tanstack/ai-sandbox-docker
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-docker@1607@tanstack/ai-sandbox-e2b
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-e2b@1607@tanstack/ai-sandbox-local-process
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-local-process@1607@tanstack/ai-sandbox-sprites
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-sprites@1607@tanstack/ai-sandbox-upstash-box
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-upstash-box@1607@tanstack/ai-sandbox-vercel
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-vercel@1607@tanstack/ai-skills
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-skills@1607@tanstack/ai-solid
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-solid@1607@tanstack/ai-solid-ui
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-solid-ui@1607@tanstack/ai-svelte
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-svelte@1607@tanstack/ai-typesafe
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-typesafe@1607@tanstack/ai-utils
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-utils@1607@tanstack/ai-vercel-gateway
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vercel-gateway@1607@tanstack/ai-vertex
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vertex@1607@tanstack/ai-vue
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vue@1607@tanstack/ai-vue-ui
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vue-ui@1607@tanstack/ai-worldlabs
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-worldlabs@1607@tanstack/openai-base
npm i https://pkg.pr.new/TanStack/ai/@tanstack/openai-base@1607@tanstack/preact-ai-devtools
npm i https://pkg.pr.new/TanStack/ai/@tanstack/preact-ai-devtools@1607@tanstack/react-ai-devtools
npm i https://pkg.pr.new/TanStack/ai/@tanstack/react-ai-devtools@1607@tanstack/solid-ai-devtools
npm i https://pkg.pr.new/TanStack/ai/@tanstack/solid-ai-devtools@1607@tanstack/svelte-ai-devtools
npm i https://pkg.pr.new/TanStack/ai/@tanstack/svelte-ai-devtools@1607commit:
be602cdtombeckenham merged this7216c7b
tombeckenham closed this
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: be602cd8969bd26e28f15c36922254b6d06345c1
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR