Skip to content

fix(sandbox): scope the claim auto-retry budget to the active branch - #5221

Merged
pedrofrxncx merged 2 commits into
mainfrom
fix/sandbox-claim-retry-budget-cross-branch-leak-w2
Jul 24, 2026
Merged

pedrofrxncx merged 2 commits into
mainfrom
fix/sandbox-claim-retry-budget-cross-branch-leak-w2

Conversation

@pedrofrxncx

@pedrofrxncx pedrofrxncx commented Jul 24, 2026 •

Copy link
Copy Markdown
Collaborator

Follows #5210 (bounded claim auto-retry) hardening.

The bug: SandboxLifecycleProvider lives above Chat.ActiveTaskProvider in the tree (see VmEventsBridge in apps/web/src/layouts/agent-shell-layout/index.tsx) and is only remounted when virtualMcpId changes (Chat.Provider key={chatVirtualMcpId}) — NOT on a task/branch switch. Its claim auto-retry budget (claimRetryCountRef / claimFailureHandledRef, added in #5210) was tracked in plain refs scoped to the whole provider lifetime, not to the current branch's boot episode.

Failure scenario: open task A on branch foo, its sandbox claim fails with a retryable reason (e.g. scheduling-timeout during a capacity crunch) and both auto-retries are consumed (claimRetryCountRef.current === MAX_CLAIM_AUTO_RETRIES). Switch to task B on a different branch bar (same agent). If bar's own boot also hits a transient claim failure — plausible, since scheduling-timeout/reconciler-error are cluster-wide capacity issues likely to correlate across branches around the same time — claimRetryExhausted reads the already-exhausted counter from foo's episode and bar gets zero retries: the errored card shows immediately instead of self-healing, even though this is bar's first attempt.

The fix: replaced the two refs with a small piece of React state (claimRetryEpisodeState) keyed by branch, reconciled each render through a new pure helper reconcileClaimRetryEpisode(prev, branch) — returns prev unchanged when the branch matches (no extra render), or a fresh { count: 0, handled: false } budget when it doesn't. This mirrors the existing pattern in the same file (e.g. computeOthersThreadGate) of extracting the branching logic into a small tested pure function rather than reasoning about refs inline.

Net effect: each branch's boot episode now gets its own 2-retry budget again, regardless of which other branch's episode last ran in this provider instance.

Regression test: apps/web/src/components/sandbox/hooks/sandbox-lifecycle-context.test.ts — new reconcileClaimRetryEpisode describe block; asserts (1) same branch returns the identical reference (no needless reset/re-render), (2) a different branch resets to a fresh budget (the bug), (3) null → a real branch also resets.

Reviewer verification:

bun test apps/web/src/components/sandbox/hooks/sandbox-lifecycle-context.test.ts
cd apps/web && bunx tsc --noEmit

Both run clean locally (58/58 tests pass, including the 3 new ones). bun run fmt was run with no diffs. Full CI covers lint/build.


Summary by cubic

Fixes a bug where the sandbox claim auto-retry budget leaked across branches, causing fresh boots on a different branch to skip retries. The budget is now scoped to the active branch so transient claim failures self-heal per branch.

  • Bug Fixes
    • Replaced ref-based counters with branch-keyed state (claimRetryEpisodeState) reconciled via reconcileClaimRetryEpisode, which resets on branch change and returns the same reference for same-branch episodes to avoid extra renders.
    • Added unit tests for reconcileClaimRetryEpisode covering same-branch identity, cross-branch reset, and null-to-branch reset.

Written for commit c4de72b. Summary will update on new commits.

Review in cubic

pedrofrxncx and others added 2 commits July 24, 2026 15:37
SandboxLifecycleProvider lives above Chat.ActiveTaskProvider and is not
remounted on a task switch, but its bounded claim-auto-retry budget (added in
#5210) was tracked in plain refs shared across the whole provider lifetime.
Switching to a different task/branch after one branch's boot exhausted its
retry budget carried that exhausted count into the new branch's fresh boot,
so a transient claim failure there skipped auto-retry and surfaced the
errored card immediately instead of retrying.

Replaced the two refs with a small piece of state keyed by branch, reconciled
via the new pure `reconcileClaimRetryEpisode` (reset to a fresh budget
whenever the branch differs from the tracked one), and added unit tests for
it.
@pedrofrxncx
pedrofrxncx merged commit 0fece44 into main Jul 24, 2026
14 checks passed
@pedrofrxncx
pedrofrxncx deleted the fix/sandbox-claim-retry-budget-cross-branch-leak-w2 branch July 24, 2026 19:43
decocms Bot pushed a commit that referenced this pull request Jul 24, 2026
PR: #5221 fix(sandbox): scope the claim auto-retry budget to the active branch
Bump type: patch

- decocms (apps/api/package.json): 4.125.3 -> 4.125.4

Deploy-Scope: web
pedrofrxncx pushed a commit that referenced this pull request Jul 27, 2026
#5240)

Reverts #5112 (share agent sandboxes by branch), #5116 (always share
agent sandboxes) and #5132 (use shared staging branch), restoring the
pre-#5112 behaviour: hosted agent sandboxes are per-user again, and a
new GitHub-backed thread gets a generated branch instead of `staging`.

Reconstructed against current paths rather than reverted: #5120 split
apps/mesh into apps/api + apps/web and hoisted branch-name.ts,
runtime-defaults.ts and thread/schema.ts into packages/shared, and #5174
deleted vm-events.ts outright, so none of the three commits reverse-apply.

Core change: SandboxId goes back to a flat `{ userId, projectRef }` and
sandboxIdKey back to `userId:projectRef`, so every hosted claim handle
changes. Live shared claims must be drained BEFORE this deploys.

Also reverted, because they only exist inside the shared model:
- #5225 withoutLegacyAgentSandboxEntries — left in place it would strip
  the sandboxMap entries the reverted server writes back, leaving
  shouldAutoStart permanently true (silent, compiles fine)
- #5219 resolveSharedThreadVm and the shared-scope branch
- #5183 stale-provisioning reset (lived inside the deleted storage)

Deliberately preserved:
- migrations 137/138 and their index.ts entries — Kysely's
  #ensureNoMissingMigrations runs unconditionally, so deleting an applied
  migration crash-loops every pod. The tables are dropped by a follow-up
  forward migration after the drain, not by this commit.
- #5114/#5118 withClaimGitLock, #5126 org-context guard, #5143 analytics
  gate, #5171 owner-keyed thread graft, #5195/#5207/#5210/#5221 client
  auto-retry, the daemon status-code train and the settings-validation train
- #5112's org-fs least-privilege narrowing and the start dedup key fix
- #5116's squashed queue-tray pluralization (en + pt-br)
- the remote-branch-name validations #5132 deleted, since this restores
  the dynamic origin/HEAD lookup that made them load-bearing

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Pedro França <[email protected]>
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.

1 participant