harness: run-swarm-ready-list-test.sh is flaky — a race in the shared countdown-file curl shim occasionally flips a 503 to 200 #144

Closed
opened 2026-09-12 09:23:17 +00:00 by matou-build-agent · 3 comments

Symptom. tests/run-swarm-ready-list-test.sh is flaky: run standalone it fails intermittently (observed 2 of 4 runs on bens-mac-04, 2026-09-12) at

FAIL: the buggy variant must still see the lister fail

and its sibling assertion a persistent 503 on the ready-list must fail the listing. The same intermittency shows up under a loaded full-suite run.

Cause (a race in the test curl shim, not production). The shim simulates a degraded forge with a shared countdown file ($FAKE_CURL_DIR/curl-fails), decremented non-atomically in maybe_fail: n="$(cat "$f")" then [ "$n" -gt 0 ] then echo $((n-1)) >"$f". list-ready-tasks.sh issues its ready-for-agent + standing-drive legs concurrently (the #48 OVERLAP posture), so two shim processes read-modify-write the same file at once. A lost update occasionally truncates it to empty; then [ "" -gt 0 ] errors (rc 2), and maybe_fail's || return 0 treats that as "do not fail", so the endpoint serves 200 instead of 503, the listing unexpectedly succeeds, rc=0, and the "must fail" assertions fire.

Fix direction (test-only). Make the countdown decrement atomic (a flock or mkdir-based lock, or give each concurrent leg its own countdown file), and make maybe_fail treat a non-integer/empty count as still-failing (fail-closed) instead of || return 0. Production list-ready-tasks.sh is unaffected — this is purely the fixture's shared-state race.

Provenance. Noticed while re-vendoring for origin idss #1424 (the notify-guard fix); the pin bump 26f96ce -> 72496a4 adopted this test. Inert in consumers that do not run .sandcastle/tests/ in CI (e.g. idss), but it reds dev-factory's own offline suite intermittently.

**Symptom.** `tests/run-swarm-ready-list-test.sh` is flaky: run standalone it fails intermittently (observed 2 of 4 runs on `bens-mac-04`, 2026-09-12) at ``` FAIL: the buggy variant must still see the lister fail ``` and its sibling assertion `a persistent 503 on the ready-list must fail the listing`. The same intermittency shows up under a loaded full-suite run. **Cause (a race in the test curl shim, not production).** The shim simulates a degraded forge with a shared countdown file (`$FAKE_CURL_DIR/curl-fails`), decremented non-atomically in `maybe_fail`: `n="$(cat "$f")"` then `[ "$n" -gt 0 ]` then `echo $((n-1)) >"$f"`. `list-ready-tasks.sh` issues its ready-for-agent + standing-drive legs concurrently (the #48 OVERLAP posture), so two shim processes read-modify-write the same file at once. A lost update occasionally truncates it to empty; then `[ "" -gt 0 ]` errors (rc 2), and `maybe_fail`'s `|| return 0` treats that as "do not fail", so the endpoint serves **200** instead of 503, the listing unexpectedly succeeds, `rc=0`, and the "must fail" assertions fire. **Fix direction (test-only).** Make the countdown decrement atomic (a `flock` or `mkdir`-based lock, or give each concurrent leg its own countdown file), and make `maybe_fail` treat a non-integer/empty count as still-failing (fail-closed) instead of `|| return 0`. Production `list-ready-tasks.sh` is unaffected — this is purely the fixture's shared-state race. **Provenance.** Noticed while re-vendoring for origin idss #1424 (the notify-guard fix); the pin bump 26f96ce -> 72496a4 adopted this test. Inert in consumers that do not run `.sandcastle/tests/` in CI (e.g. idss), but it reds dev-factory's own offline suite intermittently.
Author
Member

This was generated by AI during triage.

Ruled by agent under ADR 0174 — veto anytime.

Ruling: category bug, state ready-for-agent.

Why this is a two-way door: the change is confined to the harness's own offline test fixture (tests/run-swarm-ready-list-test.sh — not a prompt skeleton, not a vendored consumer path, no blast radius past the suite). It is revertible by a later commit and provable by an existing drive: run the test in a loop under CPU load and confirm the two "must fail" assertions no longer flip. It touches none of the one-way-door items (no credentials, no security-posture change, no member-facing trust accept, no data destruction, no non-routine spend).


Verification (claim confirmed).

  • Cause read. maybe_fail <countdown-file> does a non-atomic read-modify-write on $FAKE_CURL_DIR/curl-fails (n="$(cat "$f")" → [ "$n" -gt 0 ] || return 0 → echo $((n-1)) >"$f"). The lister fires its ready-for-agent and standing-drive legs concurrently in the background (reaped off wait, the #48 OVERLAP posture), so two shim curl processes race the same file. A lost update can leave the file empty; [ "" -gt 0 ] then errors (rc 2) and the || return 0 reads that as "do not fail", so the shim serves 200 instead of 503. The listing unexpectedly succeeds and the scenario-1 / scenario-2 "must fail" assertions fire. The reporter's diagnosis matches the code exactly.
  • Reproduced. Clean over 30 standalone runs, but 1/40 failed once the box was put under nproc-wide CPU load — matching the reporter's "shows up under a loaded full-suite run". A genuine timing-dependent fixture race, production list-ready-tasks.sh unaffected.
  • Not already fixed / no prior rejection. The racy maybe_fail is still live; .out-of-scope/ is empty. flock/lock idioms already appear in sibling tests (e.g. swarm-lock-yield-test.sh), so an atomic approach is idiomatic here.

Agent Brief

Category: bug
Summary: The ready-list test's curl-shim countdown decrement races under concurrent legs, occasionally serving 200 where a 503 was scripted and flipping the "must fail" assertions.

Current behavior:
The test simulates a degraded forge with a shared countdown file decremented inside the shim's maybe_fail helper. Because the lister issues its ready-for-agent and standing-drive fetches concurrently, two shim invocations read-modify-write that one file at the same time. A lost update can truncate it to empty; the subsequent integer comparison errors and is swallowed as "healthy", so the endpoint returns 200 instead of the scripted 503. The listing then unexpectedly succeeds and the scenarios that assert a persistent 503 must fail the listing (and the buggy-variant teeth check) fail intermittently — observed ~2/4 standalone on one host and reproduced ~1/40 here under CPU load.

Desired behavior:
The scripted-failure count is honoured deterministically regardless of how many shim processes hit it concurrently. Two things must hold:

  1. The decrement is atomic — no lost update can drop the effective failure count below what the scenario scripted.
  2. A non-integer / empty count is treated as still-failing (fail-closed), never as "serve 200".

After the fix the test passes reliably standalone and under load, and the transient-blip scenarios still absorb exactly one 503 and leave the count at 0.

Key interfaces / behavioural contracts (paths will drift — search by concept):

  • The shim's maybe_fail <countdown-file> helper (returns rc 1 while the scripted count is >0, decrementing it). Make its read-modify-write atomic — e.g. a flock/mkdir-based lock around the whole read-decrement-write, or give each concurrent leg its own countdown file so no two writers share one.
  • The same helper's guard on the count value: an empty/non-integer count must fail-closed (return rc 1, "still failing") instead of the current || return 0.
  • The existing scenarios' contract must be preserved: scenario 3 asserts a single transient 503 is absorbed and curl-fails ends at 0; scenario 4 asserts the same for deps-fails. Any per-leg-file redesign must keep those end-state assertions meaningful (i.e. still able to prove the transient was consumed exactly once).

Acceptance criteria:

  • bash tests/run-swarm-ready-list-test.sh passes deterministically — e.g. 100 consecutive standalone runs, and ≥40 runs under nproc-wide CPU load, all green (0 failures).
  • The concurrency race is closed at the source: no code path can serve 200 for a leg whose scripted countdown is still >0.
  • An empty or non-integer countdown value causes the shim to fail (503), never to succeed.
  • All four existing scenarios still assert what they assert today (persistent-503 re-key, buggy-variant teeth, transient ready-list absorb, transient dependency absorb), including the "count spent to 0" checks.
  • The full offline suite still passes: for t in tests/*.sh onboarding/tests/*.sh; do case "$t" in *fakebin*|*fixtures*) continue;; esac; bash "$t" || echo "FAIL: $t"; done.

Out of scope:

  • Any change to production list-ready-tasks.sh or the real overlap/retry logic — this is a fixture-only race; production is unaffected.
  • Changing the OVERLAP concurrency posture (#48) or the number of legs the lister fires.
  • The other run-swarm-* test files, except as required to keep the full suite green.
> *This was generated by AI during triage.* **Ruled by agent under ADR 0174 — veto anytime.** **Ruling:** category `bug`, state `ready-for-agent`. **Why this is a two-way door:** the change is confined to the harness's own offline test fixture (`tests/run-swarm-ready-list-test.sh` — not a prompt skeleton, not a vendored consumer path, no blast radius past the suite). It is revertible by a later commit and provable by an existing drive: run the test in a loop under CPU load and confirm the two "must fail" assertions no longer flip. It touches none of the one-way-door items (no credentials, no security-posture change, no member-facing trust accept, no data destruction, no non-routine spend). --- **Verification (claim confirmed).** - **Cause read.** `maybe_fail <countdown-file>` does a non-atomic read-modify-write on `$FAKE_CURL_DIR/curl-fails` (`n="$(cat "$f")"` → `[ "$n" -gt 0 ] || return 0` → `echo $((n-1)) >"$f"`). The lister fires its `ready-for-agent` and `standing-drive` legs concurrently in the background (reaped off `wait`, the #48 OVERLAP posture), so two shim `curl` processes race the same file. A lost update can leave the file empty; `[ "" -gt 0 ]` then errors (rc 2) and the `|| return 0` reads that as "do not fail", so the shim serves **200** instead of 503. The listing unexpectedly succeeds and the scenario-1 / scenario-2 "must fail" assertions fire. The reporter's diagnosis matches the code exactly. - **Reproduced.** Clean over 30 standalone runs, but **1/40** failed once the box was put under `nproc`-wide CPU load — matching the reporter's "shows up under a loaded full-suite run". A genuine timing-dependent fixture race, production `list-ready-tasks.sh` unaffected. - **Not already fixed / no prior rejection.** The racy `maybe_fail` is still live; `.out-of-scope/` is empty. `flock`/lock idioms already appear in sibling tests (e.g. `swarm-lock-yield-test.sh`), so an atomic approach is idiomatic here. --- ## Agent Brief **Category:** bug **Summary:** The ready-list test's `curl`-shim countdown decrement races under concurrent legs, occasionally serving 200 where a 503 was scripted and flipping the "must fail" assertions. **Current behavior:** The test simulates a degraded forge with a shared countdown file decremented inside the shim's `maybe_fail` helper. Because the lister issues its ready-for-agent and standing-drive fetches concurrently, two shim invocations read-modify-write that one file at the same time. A lost update can truncate it to empty; the subsequent integer comparison errors and is swallowed as "healthy", so the endpoint returns 200 instead of the scripted 503. The listing then unexpectedly succeeds and the scenarios that assert a persistent 503 must fail the listing (and the buggy-variant teeth check) fail intermittently — observed ~2/4 standalone on one host and reproduced ~1/40 here under CPU load. **Desired behavior:** The scripted-failure count is honoured deterministically regardless of how many shim processes hit it concurrently. Two things must hold: 1. The decrement is atomic — no lost update can drop the effective failure count below what the scenario scripted. 2. A non-integer / empty count is treated as **still-failing** (fail-closed), never as "serve 200". After the fix the test passes reliably standalone and under load, and the transient-blip scenarios still absorb exactly one 503 and leave the count at 0. **Key interfaces / behavioural contracts (paths will drift — search by concept):** - The shim's `maybe_fail <countdown-file>` helper (returns rc 1 while the scripted count is >0, decrementing it). Make its read-modify-write atomic — e.g. a `flock`/`mkdir`-based lock around the whole read-decrement-write, or give each concurrent leg its own countdown file so no two writers share one. - The same helper's guard on the count value: an empty/non-integer count must fail-closed (return rc 1, "still failing") instead of the current `|| return 0`. - The existing scenarios' contract must be preserved: scenario 3 asserts a single transient 503 is absorbed and `curl-fails` ends at `0`; scenario 4 asserts the same for `deps-fails`. Any per-leg-file redesign must keep those end-state assertions meaningful (i.e. still able to prove the transient was consumed exactly once). **Acceptance criteria:** - [ ] `bash tests/run-swarm-ready-list-test.sh` passes deterministically — e.g. 100 consecutive standalone runs, and ≥40 runs under `nproc`-wide CPU load, all green (0 failures). - [ ] The concurrency race is closed at the source: no code path can serve 200 for a leg whose scripted countdown is still >0. - [ ] An empty or non-integer countdown value causes the shim to fail (503), never to succeed. - [ ] All four existing scenarios still assert what they assert today (persistent-503 re-key, buggy-variant teeth, transient ready-list absorb, transient dependency absorb), including the "count spent to 0" checks. - [ ] The full offline suite still passes: `for t in tests/*.sh onboarding/tests/*.sh; do case "$t" in *fakebin*|*fixtures*) continue;; esac; bash "$t" || echo "FAIL: $t"; done`. **Out of scope:** - Any change to production `list-ready-tasks.sh` or the real overlap/retry logic — this is a fixture-only race; production is unaffected. - Changing the OVERLAP concurrency posture (#48) or the number of legs the lister fires. - The other `run-swarm-*` test files, except as required to keep the full suite green.
Author
Member

swarm-claim host=matou-dev-server-01 run=8817
(automated multi-host claim — lowest live claim id works this ticket)

swarm-claim host=matou-dev-server-01 run=8817 (automated multi-host claim — lowest live claim id works this ticket)
Author
Member

close-report for #144 — status success

✅ close-report gates passed — every claim verified against main; issue closed.

envelope
{
  "issue": 144,
  "status": "success",
  "commits": [
    "e76f285"
  ],
  "changed_files": [
    "tests/run-swarm-ready-list-test.sh",
    "STATUS.md"
  ],
  "tests": [
    {
      "command": "bash tests/run-swarm-ready-list-test.sh (x100 standalone)",
      "exit_code": 0
    },
    {
      "command": "bash tests/run-swarm-ready-list-test.sh (x40 under nproc-wide CPU load)",
      "exit_code": 0
    }
  ],
  "criteria": {
    "bash tests/run-swarm-ready-list-test.sh passes deterministically (100 standalone + >=40 under load)": "100/100 standalone green and 40/40 green under nproc-wide CPU load; commit e76f285",
    "The concurrency race is closed at the source: no path serves 200 for a leg whose scripted countdown is still >0": "maybe_fail serializes read-decrement-write behind an atomic mkdir-lock; tests/run-swarm-ready-list-test.sh",
    "An empty or non-integer countdown value causes the shim to fail (503), never succeed": "case ''|*[!0-9]*) rmdir lock; return 1 (fail-closed) replaces the old || return 0; tests/run-swarm-ready-list-test.sh",
    "All four existing scenarios still assert what they assert today, incl. count-spent-to-0": "no scenario body changed; only maybe_fail rewritten; scenarios 3/4 still assert curl-fails/deps-fails end at 0",
    "The full offline suite still passes": "run-swarm-ready-list green; the only two reds (box-vocabulary-test, fleet-tui-test) are pre-existing and fail on a clean tree, outside this slice"
  },
  "summary": "Made the ready-list test's curl-shim countdown decrement atomic (mkdir-lock) and fail-closed, ending the concurrency flake that flipped a scripted 503 to 200.",
  "blockers": []
}
**close-report** for #144 — status `success` :white_check_mark: **close-report gates passed** — every claim verified against main; issue closed. <details><summary>envelope</summary> ```json { "issue": 144, "status": "success", "commits": [ "e76f285" ], "changed_files": [ "tests/run-swarm-ready-list-test.sh", "STATUS.md" ], "tests": [ { "command": "bash tests/run-swarm-ready-list-test.sh (x100 standalone)", "exit_code": 0 }, { "command": "bash tests/run-swarm-ready-list-test.sh (x40 under nproc-wide CPU load)", "exit_code": 0 } ], "criteria": { "bash tests/run-swarm-ready-list-test.sh passes deterministically (100 standalone + >=40 under load)": "100/100 standalone green and 40/40 green under nproc-wide CPU load; commit e76f285", "The concurrency race is closed at the source: no path serves 200 for a leg whose scripted countdown is still >0": "maybe_fail serializes read-decrement-write behind an atomic mkdir-lock; tests/run-swarm-ready-list-test.sh", "An empty or non-integer countdown value causes the shim to fail (503), never succeed": "case ''|*[!0-9]*) rmdir lock; return 1 (fail-closed) replaces the old || return 0; tests/run-swarm-ready-list-test.sh", "All four existing scenarios still assert what they assert today, incl. count-spent-to-0": "no scenario body changed; only maybe_fail rewritten; scenarios 3/4 still assert curl-fails/deps-fails end at 0", "The full offline suite still passes": "run-swarm-ready-list green; the only two reds (box-vocabulary-test, fleet-tui-test) are pre-existing and fail on a clean tree, outside this slice" }, "summary": "Made the ready-list test's curl-shim countdown decrement atomic (mkdir-lock) and fail-closed, ending the concurrency flake that flipped a scripted 503 to 200.", "blockers": [] } ``` </details>
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/dev-factory#144
No description provided.