swarm: two runs land from one shared workdir at the same time (the per-repo flock is released before run-swarm.sh) #347

Closed
opened 2026-11-18 04:37:57 +00:00 by ben · 2 comments
Owner

Symptom

On 2026-11-18, two Matou/policy-generators swarm runs used the shared checkout ~/swarm/Matou/policy-generators at the same time:

  • run 182, started 02:56Z, ready=[31];
  • run 205, started 03:32Z, ready=[43].

The workdir's reflog shows each resetting the checkout to its own worker branch seconds apart (03:33:21, then 03:33:24), then rebasing and aborting over each other (03:33:30–36). Run 205 had no commits of its own and still tried to push run 182's commit 5598a13 to main.

Nothing landed wrongly this time, because both pushes were refused for an unrelated reason (see the sibling ticket on the landing alarm). Had they gone through, one run would have landed, and reported on, another run's work.

Cause, as reported

The per-repo workdir flock in swarm.yml is released before run-swarm.sh runs, so it does not cover the land stage. Two runs on one host can therefore be in landing_stage against the same checkout at once. The healer names this the same class as GOTCHAS 62 / idss #638.

What to fix

  • Hold a per-repo, per-host lock across everything in run-swarm.sh that mutates the shared checkout (reset, rebase, push), or give each run its own checkout or worktree for landing.
  • A run with no commits of its own should not push at all.

Evidence

Filed by a Claude session at Ben's request. I did not see the host reflog myself and have not read swarm.yml's lock scope closely enough to confirm the cause; the facts above are the swarm healer's, from the consumer ticket.

## Symptom On 2026-11-18, two `Matou/policy-generators` swarm runs used the shared checkout `~/swarm/Matou/policy-generators` at the same time: - run 182, started 02:56Z, ready=[31]; - run 205, started 03:32Z, ready=[43]. The workdir's reflog shows each resetting the checkout to its own worker branch seconds apart (03:33:21, then 03:33:24), then rebasing and aborting over each other (03:33:30–36). Run 205 had **no commits of its own** and still tried to push run 182's commit `5598a13` to `main`. Nothing landed wrongly this time, because both pushes were refused for an unrelated reason (see the sibling ticket on the landing alarm). Had they gone through, one run would have landed, and reported on, another run's work. ## Cause, as reported The per-repo workdir flock in `swarm.yml` is released before `run-swarm.sh` runs, so it does not cover the land stage. Two runs on one host can therefore be in `landing_stage` against the same checkout at once. The healer names this the same class as GOTCHAS 62 / idss #638. ## What to fix - Hold a per-repo, per-host lock across everything in `run-swarm.sh` that mutates the shared checkout (reset, rebase, push), or give each run its own checkout or worktree for landing. - A run with no commits of its own should not push at all. ## Evidence - Consumer ticket, section "Secondary: two runs landed from the same workdir": https://git.matou.nz/Matou/policy-generators/issues/98 - Runs: https://git.matou.nz/Matou/policy-generators/actions/runs/182 and https://git.matou.nz/Matou/policy-generators/actions/runs/205 - Consumer pin at the time: `FACTORY_REF` `cda3470`. Filed by a Claude session at Ben's request. I did not see the host reflog myself and have not read `swarm.yml`'s lock scope closely enough to confirm the cause; the facts above are the swarm healer's, from the consumer ticket.

This was generated by AI during triage.

Ruled by agent under ADR 0174 — veto anytime

Ruling: bug → ready-for-session (not ready-for-agent).

Why a two-way door: this is a label choice, reverted by a later label edit, and provable by reading the fix surface. It is not on the one-way-door list (no credentials, no security-posture widening, no trust accept, no data destruction, no spend).

What proves the routing: the cause is confirmed in .forgejo/workflows/swarm.yml — the per-repo workdir flock is taken on fd 8 (exec 8>"$workdir_lock" / flock -n 8), then released with exec 8>&- immediately before bash .sandcastle/run-swarm.sh. So the lock covers only the git setup, not the land stage inside run-swarm.sh, and two runs on one host can mutate the shared checkout concurrently. The simplest correct fix — extending that flock to span run-swarm.sh — edits .forgejo/, which per this repo's triage-labels doctrine a swarm worker must not touch (the swarm queue reads only ready-for-agent, so this is kept out of it by construction). Hence ready-for-session, and an interactive session claims it.

Agent Brief (for a session)

Category: bug
Summary: Two swarm runs land from the same shared checkout at once — the per-repo workdir flock is released before run-swarm.sh, so it does not cover the land stage.

Why a session, not a swarm worker: the fix most naturally edits .forgejo/workflows/swarm.yml (a session-only surface in this repo). Even a run-swarm.sh-side variant guards the workflow that spawns workers, so it wants a session's standing.

Current behavior:
The workflow acquires a per-repo workdir lock, runs git setup under it, then closes the lock (exec 8>&-) and invokes run-swarm.sh. The land stage (reset, rebase, push) runs unlocked, so a second run that wins a different host-capacity slot for the same repo can reset the shared checkout to its own branch and rebase/push over the first run. A run with no commits of its own can even push another run's commit to main.

Desired behavior:

  • The shared checkout is mutated by at most one run per repo per host at a time, including the land stage — either by holding the per-repo/per-host lock across all of run-swarm.sh's checkout-mutating work, or by giving each run its own worktree/checkout for landing.
  • A run that produced no commits of its own never pushes.

Key interfaces:

  • The workdir-lock scope in the swarm workflow (the fd-8 flock released by exec 8>&- before run-swarm.sh) — extend it to cover the land stage, or replace it with a lock held inside run-swarm.sh.
  • The land seam in run-swarm.sh/landing-lib.sh — add a guard that refuses to push when the run has zero own-commits relative to origin/main.

Acceptance criteria:

  • Two concurrent same-repo runs on one host cannot both be in the land stage against the shared checkout simultaneously (serialized or isolated worktrees).
  • A run with no commits of its own does not push.
  • A test (offline, shimmed git) proves the land stage is serialized / isolated and that a zero-commit run does not push.
  • Any lib change ships with its test in the same commit; if .forgejo/ or .sandcastle/ is touched, the self-copy pin bump stays a human step (not done in this ticket).

Out of scope:

  • The landing-alarm misdiagnosis on a hook refusal — tracked in sibling ticket #346. (Here, both pushes happened to be refused for that unrelated reason, which is why nothing landed wrongly.)
> *This was generated by AI during triage.* ## Ruled by agent under ADR 0174 — veto anytime **Ruling:** `bug` → `ready-for-session` (not `ready-for-agent`). **Why a two-way door:** this is a label choice, reverted by a later label edit, and provable by reading the fix surface. It is not on the one-way-door list (no credentials, no security-posture widening, no trust accept, no data destruction, no spend). **What proves the routing:** the cause is confirmed in `.forgejo/workflows/swarm.yml` — the per-repo workdir flock is taken on fd 8 (`exec 8>"$workdir_lock"` / `flock -n 8`), then **released with `exec 8>&-` immediately before `bash .sandcastle/run-swarm.sh`**. So the lock covers only the git setup, not the land stage inside `run-swarm.sh`, and two runs on one host can mutate the shared checkout concurrently. The simplest correct fix — extending that flock to span `run-swarm.sh` — edits `.forgejo/`, which per this repo's triage-labels doctrine a swarm worker must not touch (the swarm queue reads only `ready-for-agent`, so this is kept out of it by construction). Hence `ready-for-session`, and an interactive session claims it. ## Agent Brief (for a session) **Category:** bug **Summary:** Two swarm runs land from the same shared checkout at once — the per-repo workdir flock is released before `run-swarm.sh`, so it does not cover the land stage. **Why a session, not a swarm worker:** the fix most naturally edits `.forgejo/workflows/swarm.yml` (a session-only surface in this repo). Even a `run-swarm.sh`-side variant guards the workflow that spawns workers, so it wants a session's standing. **Current behavior:** The workflow acquires a per-repo workdir lock, runs git setup under it, then closes the lock (`exec 8>&-`) and invokes `run-swarm.sh`. The land stage (reset, rebase, push) runs **unlocked**, so a second run that wins a different host-capacity slot for the same repo can reset the shared checkout to its own branch and rebase/push over the first run. A run with no commits of its own can even push another run's commit to `main`. **Desired behavior:** - The shared checkout is mutated by at most one run per repo per host at a time, **including** the land stage — either by holding the per-repo/per-host lock across all of `run-swarm.sh`'s checkout-mutating work, or by giving each run its own worktree/checkout for landing. - A run that produced no commits of its own never pushes. **Key interfaces:** - The workdir-lock scope in the swarm workflow (the fd-8 `flock` released by `exec 8>&-` before `run-swarm.sh`) — extend it to cover the land stage, or replace it with a lock held inside `run-swarm.sh`. - The land seam in `run-swarm.sh`/`landing-lib.sh` — add a guard that refuses to push when the run has zero own-commits relative to `origin/main`. **Acceptance criteria:** - [ ] Two concurrent same-repo runs on one host cannot both be in the land stage against the shared checkout simultaneously (serialized or isolated worktrees). - [ ] A run with no commits of its own does not push. - [ ] A test (offline, shimmed git) proves the land stage is serialized / isolated and that a zero-commit run does not push. - [ ] Any lib change ships with its test in the same commit; if `.forgejo/` or `.sandcastle/` is touched, the self-copy pin bump stays a human step (not done in this ticket). **Out of scope:** - The landing-alarm misdiagnosis on a hook refusal — tracked in sibling ticket #346. (Here, both pushes happened to be refused for that unrelated reason, which is why nothing landed wrongly.)

Close report — landed on main (push mode), commit 50ba92c.

Root cause confirmed exactly as filed: swarm.yml's per-repo workdir flock (/tmp/matou-workdir-<repo>.lock, fd 8) ran exec 8>&- before bash .sandcastle/run-swarm.sh, so it covered git-setup's reset but not the land stage. run-swarm's shared-checkout mutations actually span git-setup (reset) and landing_stage — execute_adopt_worker_branch's git reset --hard <worker-branch> (#329), the rebase ladder, and the push to main, all on one .git. Two same-repo runs on one host, each winning a different host-capacity slot, could both be inside landing_stage against the one checkout — the reflog race you saw (runs 182/205 resetting onto their own worker branches seconds apart). Same class as GOTCHAS 62 / idss #638, one stage later.

Both fixes from "What to fix" landed:

  1. Hold the lock across everything that mutates the shared checkout. swarm.yml no longer closes fd 8 before run-swarm.sh, so the flock spans the whole run and releases only when the step exits. A second same-repo leg loses flock -n 8 and yields in seconds (never camps — idss #238). Different repos lock different inodes, so cross-repo intra-host parallelism is unchanged; only two runs of the same repo on one host serialise, which is correct — they share one checkout. triage.yml keeps its early release (run-triage.sh triages via the API and mutates no checkout). Applied to both the render template (onboarding/templates/workflows/swarm.yml.tmpl) and this repo's rendered .forgejo/workflows/swarm.yml.
  2. A run with no commits of its own should not push. landing_stage (push mode) now refuses the push when git rev-list --count <start-sha>..HEAD is 0 — a provable empty range means the worker committed nothing, so there is nothing of ours to land. Fail-open on an unanswerable rev-list, mirroring the pr-mode empty-shell guard (matou-app#145).

Proof (offline, shimmed git/curl, no docker/network):

  • tests/landing-lib-test.sh — new #347 no-own-commits cases (the push run does NOT push main, nothing parked, returns clean) + a control that a push run with its own commits still lands. 131 passed.
  • onboarding/tests/onboard-lib-test.sh — swarm.yml holds the workdir lock across run-swarm.sh (no early release); triage.yml still releases before run-triage.sh. 280 passed.
  • Full offline suite (tests/*.sh + onboarding/tests/*.sh) green; docs/docs-budget-test still under budget.

Docs: GOTCHAS 347 (docs/gotchas/347-two-runs-land-from-one-shared-workdir.md + index row) and a journal entry (docs/journal/2026-11-18-347-workdir-lock-spans-land.md).

Rollout (pull-only, ADR 0001): consumers get the wider lock when they re-render swarm.yml at their next FACTORY_REF bump — the drift gate does not cover .forgejo/workflows/; the landing guard ships in landing-lib.sh at the same pin. Each half is safe alone.

Ruled by agent under ADR 0174 — veto anytime (two revertible, test-proven changes).

**Close report — landed on `main` (push mode), commit `50ba92c`.** Root cause confirmed exactly as filed: `swarm.yml`'s per-repo workdir flock (`/tmp/matou-workdir-<repo>.lock`, fd 8) ran `exec 8>&-` **before** `bash .sandcastle/run-swarm.sh`, so it covered git-setup's reset but **not** the land stage. run-swarm's shared-checkout mutations actually span git-setup (reset) **and** `landing_stage` — `execute_adopt_worker_branch`'s `git reset --hard <worker-branch>` (#329), the rebase ladder, and the push to `main`, all on one `.git`. Two same-repo runs on one host, each winning a different host-capacity slot, could both be inside `landing_stage` against the one checkout — the reflog race you saw (runs 182/205 resetting onto their own worker branches seconds apart). Same class as GOTCHAS 62 / idss #638, one stage later. **Both fixes from "What to fix" landed:** 1. *Hold the lock across everything that mutates the shared checkout.* `swarm.yml` no longer closes fd 8 before `run-swarm.sh`, so the flock spans the whole run and releases only when the step exits. A second same-repo leg loses `flock -n 8` and yields in seconds (never camps — idss #238). Different repos lock different inodes, so cross-repo intra-host parallelism is unchanged; only two runs of the *same* repo on one host serialise, which is correct — they share one checkout. `triage.yml` keeps its early release (`run-triage.sh` triages via the API and mutates no checkout). Applied to both the render template (`onboarding/templates/workflows/swarm.yml.tmpl`) and this repo's rendered `.forgejo/workflows/swarm.yml`. 2. *A run with no commits of its own should not push.* `landing_stage` (push mode) now refuses the push when `git rev-list --count <start-sha>..HEAD` is 0 — a provable empty range means the worker committed nothing, so there is nothing of ours to land. Fail-open on an unanswerable rev-list, mirroring the pr-mode empty-shell guard (matou-app#145). **Proof (offline, shimmed git/curl, no docker/network):** - `tests/landing-lib-test.sh` — new `#347 no-own-commits` cases (the push run does NOT push `main`, nothing parked, returns clean) + a control that a push run *with* its own commits still lands. 131 passed. - `onboarding/tests/onboard-lib-test.sh` — `swarm.yml` holds the workdir lock across `run-swarm.sh` (no early release); `triage.yml` still releases before `run-triage.sh`. 280 passed. - Full offline suite (`tests/*.sh` + `onboarding/tests/*.sh`) green; `docs/docs-budget-test` still under budget. Docs: GOTCHAS 347 (`docs/gotchas/347-two-runs-land-from-one-shared-workdir.md` + index row) and a journal entry (`docs/journal/2026-11-18-347-workdir-lock-spans-land.md`). **Rollout (pull-only, ADR 0001):** consumers get the wider lock when they re-render `swarm.yml` at their next `FACTORY_REF` bump — the drift gate does not cover `.forgejo/workflows/`; the landing guard ships in `landing-lib.sh` at the same pin. Each half is safe alone. Ruled by agent under ADR 0174 — veto anytime (two revertible, test-proven changes).
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
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/dev-factory#347
No description provided.