swarm: worker sandbox can corrupt the shared worktree admin — in-sandbox git worktree repair killed run 1649's sibling worker #239

Closed
opened 2026-07-31 22:24:16 +00:00 by ben · 3 comments
Owner

What happened (healer-diagnosed, human-verified)

Swarm run 1649 (2026-07-31 21:30) went red at stage=sandcastle run (workers):

WorktreeError: fatal: '…/.sandcastle/worktrees/sandcastle-worker-20260731-213014-29e0a8' is not a working tree

The trigger is in the sibling worker's log (-447133, issue #234). Its workspace's recorded worktree path didn't match the container mount, which broke nix develop .#go-ci git evaluation and would have blocked the #198 pre-push gate. To unblock it, the worker ran git worktree repair inside the sandbox. Because every .sandcastle/worktrees/<name> shares the parent repo's .git/worktrees/ admin directory, that rewrote the admin gitdir pointer to the container-only path /home/agent/workspace — so when Sandcastle set up the sibling worker's worktree in the same run, git refused it ("is not a working tree") and the run died.

Impact was partial: #234's commit 18ebc92 had already merged; only the sibling worker was lost. The post-run sweep cleaned the admin; later runs recovered (#233, #238 landed).

Why this is a class, not a one-off

  • Same day, #196's worker recovered from a git worktree prune mishap in-sandbox.
  • An older run shows a failed sandcastle/worker/* → main merge with a preserved branch.
  • Any Go-touching worker that hits the recorded-path-vs-mount mismatch is incentivised to run git worktree repair to get the gate through — this WILL recur.

Evidence: /tmp/matou-heal.YCPvq8 on matou-workstation (worker log lines 53–55 show the repair with before/after gitdir), healer ledger sig 424240d0561a, Mattermost thread z7od1s4w5bffum8yrm8uc5d16a.

The design call (pick one or combine — needs a ruling)

  1. Fix the root mismatch — make the worktree's recorded path and the container mount agree (mount the worktree at its host path inside the container, or point the worktree admin at container-stable paths at creation time). Workers then never need git worktree repair. Recommended: removes the incentive rather than policing it.
  2. Isolate worker git state — give each worker a clone (or a copy) instead of a worktree sharing the parent .git/worktrees/, so no in-sandbox git command can touch shared admin. Costs disk/setup time per worker.
  3. Fence the commands — forbid git worktree repair|prune in the worker prompt/gate on the shared repo. Cheapest, but prompt-level fences are advisory and the underlying mismatch keeps biting (nix evaluation stays broken until repaired).

A pragmatic combination is 1 + 3: fix the mount/path agreement so nix evaluation works out of the box, and fence repair|prune as a backstop.

Acceptance (once ruled)

  • A Go-touching worker passes the #198 pre-push gate (nix develop .#go-ci) without running any git worktree admin command.
  • Two parallel workers in one run cannot break each other via shared .git/worktrees/ state.
  • Regression check: the run-1649 sequence (worker A repairs/mutates git admin, worker B sets up) no longer reds the run.

Related: #235 (healer signature fidelity — validated on this incident), #238 (swarm queue hygiene), #197.

## What happened (healer-diagnosed, human-verified) Swarm run **1649** (2026-07-31 21:30) went red at `stage=sandcastle run (workers)`: ``` WorktreeError: fatal: '…/.sandcastle/worktrees/sandcastle-worker-20260731-213014-29e0a8' is not a working tree ``` The trigger is in the sibling worker's log (`-447133`, issue #234). Its workspace's recorded worktree path didn't match the container mount, which broke `nix develop .#go-ci` git evaluation and would have blocked the #198 pre-push gate. To unblock it, the worker ran **`git worktree repair` inside the sandbox**. Because every `.sandcastle/worktrees/<name>` shares the parent repo's `.git/worktrees/` admin directory, that rewrote the admin `gitdir` pointer to the container-only path `/home/agent/workspace` — so when Sandcastle set up the sibling worker's worktree in the same run, git refused it ("is not a working tree") and the run died. **Impact was partial**: #234's commit `18ebc92` had already merged; only the sibling worker was lost. The post-run sweep cleaned the admin; later runs recovered (#233, #238 landed). ## Why this is a class, not a one-off - Same day, #196's worker recovered from a `git worktree prune` mishap in-sandbox. - An older run shows a failed `sandcastle/worker/*` → `main` merge with a preserved branch. - Any Go-touching worker that hits the recorded-path-vs-mount mismatch is incentivised to run `git worktree repair` to get the gate through — this WILL recur. Evidence: `/tmp/matou-heal.YCPvq8` on matou-workstation (worker log lines 53–55 show the repair with before/after `gitdir`), healer ledger sig `424240d0561a`, Mattermost thread `z7od1s4w5bffum8yrm8uc5d16a`. ## The design call (pick one or combine — needs a ruling) 1. **Fix the root mismatch** — make the worktree's recorded path and the container mount agree (mount the worktree at its host path inside the container, or point the worktree admin at container-stable paths at creation time). Workers then never *need* `git worktree repair`. **Recommended**: removes the incentive rather than policing it. 2. **Isolate worker git state** — give each worker a clone (or a copy) instead of a worktree sharing the parent `.git/worktrees/`, so no in-sandbox git command can touch shared admin. Costs disk/setup time per worker. 3. **Fence the commands** — forbid `git worktree repair|prune` in the worker prompt/gate on the shared repo. Cheapest, but prompt-level fences are advisory and the underlying mismatch keeps biting (nix evaluation stays broken until repaired). A pragmatic combination is **1 + 3**: fix the mount/path agreement so nix evaluation works out of the box, and fence `repair|prune` as a backstop. ## Acceptance (once ruled) - A Go-touching worker passes the #198 pre-push gate (`nix develop .#go-ci`) **without** running any `git worktree` admin command. - Two parallel workers in one run cannot break each other via shared `.git/worktrees/` state. - Regression check: the run-1649 sequence (worker A repairs/mutates git admin, worker B sets up) no longer reds the run. Related: #235 (healer signature fidelity — validated on this incident), #238 (swarm queue hygiene), #197.
Author
Owner

Ruling (Ben, 2026-07-31): go with the suggested combination — options 1 + 3.

  1. Fix the recorded-path-vs-mount agreement so the worker's worktree evaluates cleanly under nix develop .#go-ci out of the box — workers must never need to run git worktree admin commands.
  2. As a backstop, fence git worktree repair|prune (and any mutation of the shared parent .git/worktrees/ admin) out of the worker sandbox via the worker prompt/gate.

Option 2 (per-worker clones) is NOT taken — keep worktrees.

Acceptance criteria in the issue body stand. This is .sandcastle/ harness work; ready-for-agent.

**Ruling (Ben, 2026-07-31): go with the suggested combination — options 1 + 3.** 1. Fix the recorded-path-vs-mount agreement so the worker's worktree evaluates cleanly under `nix develop .#go-ci` out of the box — workers must never need to run `git worktree` admin commands. 3. As a backstop, fence `git worktree repair|prune` (and any mutation of the shared parent `.git/worktrees/` admin) out of the worker sandbox via the worker prompt/gate. Option 2 (per-worker clones) is NOT taken — keep worktrees. Acceptance criteria in the issue body stand. This is `.sandcastle/` harness work; ready-for-agent.
Author
Owner

Shipped in 93c6afb (on main), per Ben's ruling (options 1 + 3).

Root fix (1) — .sandcastle/align-worktree.sh, run first in main.mts's onSandboxReady, re-points ONLY this worker's own worktree admin back-link (.git/worktrees/<name>/gitdir) at the container mount (<toplevel>/.git) via a direct one-file write in .sandcastle/worktree-lib.sh — never a git worktree command, never a sibling's entry. nix develop .#go-ci (the #198 pre-push gate) now evaluates the flake cleanly with no repair needed, and two parallel workers cannot touch each other's shared .git/worktrees/ state.

Backstop (3) — .sandcastle/git-fence installs as /usr/local/bin/git (ahead of apt's git, via the Dockerfile) and refuses the mutating git worktree operations (repair/prune/add/remove/move/lock/unlock) inside the sandbox, passing every other git invocation through. prompt.md rule 8 is the advisory layer.

Acceptance:

  • A Go-touching worker passes the gate without any git worktree admin command — the back-link is aligned at startup, so the flake evaluates; the worker never needs (and, via the fence, cannot run) repair.
  • Two parallel workers cannot break each other via shared admin — realign touches only the own entry; the fence blocks any worktree mutation. Both proven in tests/worktree-lib-test.sh.
  • Run-1649 regression — the test links the fence as git on PATH and runs git worktree repair from a worker checkout while a sibling is mid-setup: the repair is refused, the sibling's admin is byte-identical, and the sibling stays a valid working tree.

Evidence: .sandcastle/tests/worktree-lib-test.sh (21 checks passing offline — no container, no nix). Sibling gate/sweep tests still green. The host post-run sweep is unaffected (it runs on the host, where the shim is not installed).

Shipped in `93c6afb` (on `main`), per Ben's ruling (options 1 + 3). **Root fix (1)** — `.sandcastle/align-worktree.sh`, run first in `main.mts`'s `onSandboxReady`, re-points ONLY this worker's own worktree admin back-link (`.git/worktrees/<name>/gitdir`) at the container mount (`<toplevel>/.git`) via a direct one-file write in `.sandcastle/worktree-lib.sh` — never a `git worktree` command, never a sibling's entry. `nix develop .#go-ci` (the #198 pre-push gate) now evaluates the flake cleanly with no repair needed, and two parallel workers cannot touch each other's shared `.git/worktrees/` state. **Backstop (3)** — `.sandcastle/git-fence` installs as `/usr/local/bin/git` (ahead of apt's git, via the Dockerfile) and refuses the mutating `git worktree` operations (`repair`/`prune`/`add`/`remove`/`move`/`lock`/`unlock`) inside the sandbox, passing every other git invocation through. `prompt.md` rule 8 is the advisory layer. **Acceptance:** - *A Go-touching worker passes the gate without any `git worktree` admin command* — the back-link is aligned at startup, so the flake evaluates; the worker never needs (and, via the fence, cannot run) `repair`. - *Two parallel workers cannot break each other via shared admin* — realign touches only the own entry; the fence blocks any worktree mutation. Both proven in `tests/worktree-lib-test.sh`. - *Run-1649 regression* — the test links the fence as `git` on PATH and runs `git worktree repair` from a worker checkout while a sibling is mid-setup: the repair is refused, the sibling's admin is byte-identical, and the sibling stays a valid working tree. Evidence: `.sandcastle/tests/worktree-lib-test.sh` (21 checks passing offline — no container, no nix). Sibling gate/sweep tests still green. The host post-run sweep is unaffected (it runs on the host, where the shim is not installed).
ben closed this issue 2026-07-31 22:55:42 +00:00
Author
Owner

Follow-up (2026-08-01): the landed fix 93c6afb regressed — replaced by the worktrees mount (0fb0d70).

The in-container back-link write persisted through the bind-mounted .git/worktrees/ admin to the HOST, so the host's own merge-to-head lost the worker's worktree the moment the container exited — every swarm run from 1654 (22:56) reded at the workers stage with WorktreeError: … is not a working tree. Worker pushes still landed; only host bookkeeping threw. The healer diagnosed it six times at high confidence (evidence /tmp/matou-heal-Matou-ourcloud.* on matou-workstation) but each run minted a fresh signature — the worker name's 6-hex suffix defeats normalize_error_line's 7-char hex floor — so the ledger's dedup never engaged and every run posted to Mattermost (fixed in a0f1e02).

The standing fix (still Ben's option 1, implemented without writes): main.mts now mounts .sandcastle/worktrees at its host path inside the sandbox, so the admin back-link resolves identically in both environments and nothing ever rewrites shared admin. align-worktree.sh is unwired and must not return to onSandboxReady; git-fence (option 3) stays as the backstop. Offline suites green (heal-lib 13, worktree-lib 21).

**Follow-up (2026-08-01): the landed fix `93c6afb` regressed — replaced by the worktrees mount (`0fb0d70`).** The in-container back-link write persisted through the bind-mounted `.git/worktrees/` admin to the HOST, so the host's own merge-to-head lost the worker's worktree the moment the container exited — every swarm run from 1654 (22:56) reded at the workers stage with `WorktreeError: … is not a working tree`. Worker pushes still landed; only host bookkeeping threw. The healer diagnosed it six times at high confidence (evidence `/tmp/matou-heal-Matou-ourcloud.*` on matou-workstation) but each run minted a fresh signature — the worker name's 6-hex suffix defeats `normalize_error_line`'s 7-char hex floor — so the ledger's dedup never engaged and every run posted to Mattermost (fixed in `a0f1e02`). **The standing fix (still Ben's option 1, implemented without writes):** `main.mts` now mounts `.sandcastle/worktrees` at its host path inside the sandbox, so the admin back-link resolves identically in both environments and nothing ever rewrites shared admin. `align-worktree.sh` is unwired and must not return to `onSandboxReady`; `git-fence` (option 3) stays as the backstop. Offline suites green (heal-lib 13, worktree-lib 21).
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Matou/idss#239
No description provided.