Skip to content

fix(sandbox): serialize publish/rebase per claim to stop shared-sandbox races - #5114

Merged
pedrofrxncx merged 1 commit into
mainfrom
fix/sandbox-shared-publish-rebase-race-w6
Jul 23, 2026
Merged

pedrofrxncx merged 1 commit into
mainfrom
fix/sandbox-shared-publish-rebase-race-w6

Conversation

@pedrofrxncx

@pedrofrxncx pedrofrxncx commented Jul 23, 2026 •

Copy link
Copy Markdown
Collaborator

Follows #5112 (share agent sandboxes by branch) hardening.

Bug: git/publish and git/rebase each PUT an operator identity into the daemon's persisted tenant config (patchSandboxOperator), then trigger a commit/push that reads that config back (deps.getOperator() in the daemon, used for the co-author trailer). Before #5112, a sandbox only ever had one active user, so this two-step "set then act" sequence was safe. Now that a sandbox can be shared by branch across teammates, two people can hit publish (or one publish + one rebase) for the same claim concurrently:

  1. Request A PUTs operator=A.
  2. Request B PUTs operator=B (overwrites).
  3. Request A's commit runs — reads operator=B, misattributing A's change as co-authored by B.
  4. Both requests' git add/commit/push can also interleave against the single shared working tree, which git operations were never designed to run concurrently against.

Fix: added withClaimGitLock(claimName, fn) in sandbox-proxy.ts — a small per-claim promise chain (no new dependency) that serializes the whole patch-operator-then-publish (or patch-operator-then-rebase) sequence per claim. Entries are removed once idle, so the map never grows past the number of claims currently mid-publish/rebase. This closes the race for concurrent requests hitting the same mesh process; other read-only git routes (status/diff/discard) are untouched since they don't touch the operator or commit/push.

Verification:

  • Added 3 unit tests to sandbox-proxy.test.ts covering: same-claim calls run in strict order, different-claim calls don't block each other, and a rejection doesn't wedge the lock for later calls on that claim.
  • cd apps/mesh && bunx tsc --noEmit — clean (pre-existing unrelated errors in an untracked link-daemon/ensure-rclone.test.ts file are not from this change).
  • bun test apps/mesh/src/api/routes/sandbox-proxy.test.ts — 8 pass, 0 fail.
  • bun run fmt — no changes needed.

A reviewer can confirm with bun test apps/mesh/src/api/routes/sandbox-proxy.test.ts.


Summary by cubic

Serialize git/publish and git/rebase per claim to stop operator misattribution and concurrent git races in shared sandboxes. Adds a per-claim lock so only one publish/rebase runs at a time for a claim.

  • Bug Fixes
    • Introduced withClaimGitLock(claimName, fn) in sandbox-proxy.ts to serialize the operator patch + commit/push flow for git/publish and git/rebase.
    • Wrapped publish/rebase handlers with the lock to prevent overwriting operator and interleaving git commit/push on the same working tree.
    • Lock is per-claim, promise-chained, and cleans up when idle; different claims run in parallel.
    • Added 3 unit tests validating same-claim ordering, cross-claim parallelism, and recovery after rejection.
    • Preserves existing error handling, including GitPushAuthError responses and 404→410 mapping.

Written for commit 3f092dc. Summary will update on new commits.

Review in cubic

…ox races

The publish and rebase routes PUT an operator identity into the daemon's
persisted tenant config, then trigger a commit/push that reads that config
back. On a shared agent sandbox (feat(sandbox): share agent sandboxes by
branch, #5112) two teammates can hit publish/rebase for the same claim
concurrently: one request's operator PUT can be overwritten before its own
commit runs, misattributing co-authorship, and two commit/push sequences can
interleave against the same working tree. Serialize per claim in the mesh
proxy so at most one publish/rebase runs against a given claim at a time.
@pedrofrxncx
pedrofrxncx merged commit 791700f into main Jul 23, 2026
15 checks passed
@pedrofrxncx
pedrofrxncx deleted the fix/sandbox-shared-publish-rebase-race-w6 branch July 23, 2026 17:34
decocms Bot pushed a commit that referenced this pull request Jul 23, 2026
PR: #5114 fix(sandbox): serialize publish/rebase per claim to stop shared-sandbox races
Bump type: patch

- decocms (apps/mesh/package.json): 4.109.2 -> 4.109.3

Deploy-Scope: server
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