limit-park unpark edges are double-written: two ticks race claude_limit_sweep on one stale marker #298

Closed
opened 2026-10-21 22:30:59 +00:00 by matou-build-agent · 5 comments

This was generated by AI from a Floor thread on #117.

Symptom

Every real limit-park unpark edge in the live host swarm.db is written twice in the same second. On matou-dev-server-01, all 10 limit-pause | unpark … | window reset rows of 2026-09-01 are pairs: 8234/8235, 8239/8240, 8244/8245, 8249/8250, 8254/8255 (run_id=limit-park). Each pair closes ONE real park from the same day (evidence resets Sep 2, 6pm (UTC) / resets Sep 5, 8am (UTC)).

Likely cause

claude_limit_sweep (limit-lib.sh) records the unpark and only then rm -fs the stale marker, with no lock. Two swarm-db-capable ticks firing on the same minute (e.g. two session-runner/backstop lines) both see the marker present-but-stale and both write the edge. run_id defaults to limit-park because neither caller set SWARM_RUN_ID. Not yet proven: confirm which two callers fire at :50/:20.

Why it matters

q_limit_lost / the Loss tab (#100) pairs park→unpark edges. A duplicate unpark may double-count or mis-pair windows, depending on how the pairing walks the edges.

Ask

  • Make the unpark edge exactly-once: claim the marker atomically (e.g. mv it to a per-tick name, then record only if the mv won) or take a lock around stat→event→rm.
  • A test in tests/ with two concurrent sweeps against one stale marker that asserts ONE unpark row.
  • Check whether q_limit_lost is skewed by the existing pairs. They were deliberately kept by #117 because they are real edges; dedupe only if the query is shown to be wrong.

Related: #117 (fixture sweep, 8868321), #116, #100.

*This was generated by AI from a Floor thread on #117.* ## Symptom Every real limit-park **unpark** edge in the live host `swarm.db` is written **twice in the same second**. On `matou-dev-server-01`, all 10 `limit-pause | unpark … | window reset` rows of 2026-09-01 are pairs: 8234/8235, 8239/8240, 8244/8245, 8249/8250, 8254/8255 (`run_id=limit-park`). Each pair closes ONE real park from the same day (evidence `resets Sep 2, 6pm (UTC)` / `resets Sep 5, 8am (UTC)`). ## Likely cause `claude_limit_sweep` (`limit-lib.sh`) records the unpark and only then `rm -f`s the stale marker, with no lock. Two swarm-db-capable ticks firing on the same minute (e.g. two session-runner/backstop lines) both see the marker present-but-stale and both write the edge. `run_id` defaults to `limit-park` because neither caller set `SWARM_RUN_ID`. Not yet proven: confirm which two callers fire at :50/:20. ## Why it matters `q_limit_lost` / the Loss tab (#100) pairs park→unpark edges. A duplicate unpark may double-count or mis-pair windows, depending on how the pairing walks the edges. ## Ask - Make the unpark edge exactly-once: claim the marker atomically (e.g. `mv` it to a per-tick name, then record only if the `mv` won) or take a lock around stat→event→rm. - A test in `tests/` with two concurrent sweeps against one stale marker that asserts ONE unpark row. - Check whether `q_limit_lost` is skewed by the existing pairs. They were deliberately **kept** by #117 because they are real edges; dedupe only if the query is shown to be wrong. Related: #117 (fixture sweep, `8868321`), #116, #100.
Author
Member

This was generated by AI during triage.

Agent Brief

Category: bug
Summary: The limit-park unpark edge is not exactly-once — two swarm-db-capable ticks sweeping one stale marker on the same minute both write the unpark event before either removes the marker.

Current behavior:
claude_limit_sweep (in limit-lib.sh) walks the base marker and each
fp-keyed sibling; for a marker that is present-but-stale it records the
unpark edge and then rm -fs the marker, with no lock and no atomic claim
between the staleness check, the event write, and the removal. Two ticks that
fire on the same minute both observe the marker as stale and both write the
edge, producing duplicate unpark rows in the same second. The event's
run_id defaults to limit-park because neither caller sets SWARM_RUN_ID.

Confirmed during triage: claude_limit_sweep does stat → _claude_limit_event unpark … → rm -f "$m" in a loop with nothing serializing the three steps,
matching the double-write symptom reported (10 paired rows on
matou-dev-server-01, 2026-09-01).

Desired behavior:
The unpark edge is written at most once per real park exit, even when two
sweeps race on the same stale marker. The winning tick records exactly one
unpark event and clears the marker; the losing tick records nothing. A
later usage hit after the window re-parks cleanly as it does today, and a
still-fresh window is still left untouched. Per-account (fp-keyed) markers
each still close on their own exit edge.

Key interfaces:

  • claude_limit_sweep [run_id] in limit-lib.sh — make the
    stale-marker → unpark-event → clear sequence exactly-once per marker, e.g.
    by atomically claiming the marker (rename it to a per-tick name and record
    only if the rename won) or taking a lock around stat→event→rm. The no-op
    cases (nothing parked, window still fresh) and the per-account marker scan
    must be preserved.

Acceptance criteria:

  • A test in tests/ (alongside limit-lib-test.sh) runs two concurrent
    sweeps against one stale marker and asserts exactly ONE unpark row is
    recorded.
  • A single sweep against a stale marker still records exactly one unpark
    and removes the marker (no regression).
  • A sweep against a still-fresh marker records nothing and leaves the
    marker in place.
  • Per-account (fp-keyed) markers each still produce their own single
    unpark edge.
  • Confirm whether q_limit_lost is skewed by the pre-existing duplicate
    pairs (deliberately kept by #117 as real edges); only change the query
    if it is shown to mis-pair/double-count — otherwise leave the historical
    rows and the query untouched and note the finding.

Out of scope:

  • Deleting or rewriting the historical duplicate rows already in live
    swarm.db (kept intentionally by #117).
  • Changing claude_limit_park's parking behavior or the marker format.
  • The Loss tab / q_limit_lost presentation beyond the correctness check
    above.

Triage ruling (ADR 0174 — veto anytime): routed ready-for-agent. The fix
lives in limit-lib.sh plus an offline test in tests/ — not a protected
path (no prompt skeleton, .sandcastle/, or .forgejo/), so no
ready-for-session standing is needed. It is a two-way door: revertible by a
later commit and provable by the new concurrent-sweep test. No one-way-door
residue (no credentials, security widening, trust accept, data destruction, or
spend) — the destructive-sounding part, touching live rows, is explicitly out
of scope.

> *This was generated by AI during triage.* ## Agent Brief **Category:** bug **Summary:** The limit-park unpark edge is not exactly-once — two swarm-db-capable ticks sweeping one stale marker on the same minute both write the unpark event before either removes the marker. **Current behavior:** `claude_limit_sweep` (in `limit-lib.sh`) walks the base marker and each fp-keyed sibling; for a marker that is present-but-stale it records the `unpark` edge and then `rm -f`s the marker, with no lock and no atomic claim between the staleness check, the event write, and the removal. Two ticks that fire on the same minute both observe the marker as stale and both write the edge, producing duplicate unpark rows in the same second. The event's `run_id` defaults to `limit-park` because neither caller sets `SWARM_RUN_ID`. Confirmed during triage: `claude_limit_sweep` does stat → `_claude_limit_event unpark …` → `rm -f "$m"` in a loop with nothing serializing the three steps, matching the double-write symptom reported (10 paired rows on `matou-dev-server-01`, 2026-09-01). **Desired behavior:** The unpark edge is written at most once per real park exit, even when two sweeps race on the same stale marker. The winning tick records exactly one `unpark` event and clears the marker; the losing tick records nothing. A later usage hit after the window re-parks cleanly as it does today, and a still-fresh window is still left untouched. Per-account (fp-keyed) markers each still close on their own exit edge. **Key interfaces:** - `claude_limit_sweep [run_id]` in `limit-lib.sh` — make the stale-marker → unpark-event → clear sequence exactly-once per marker, e.g. by atomically claiming the marker (rename it to a per-tick name and record only if the rename won) or taking a lock around stat→event→rm. The no-op cases (nothing parked, window still fresh) and the per-account marker scan must be preserved. **Acceptance criteria:** - [ ] A test in `tests/` (alongside `limit-lib-test.sh`) runs two concurrent sweeps against one stale marker and asserts exactly ONE unpark row is recorded. - [ ] A single sweep against a stale marker still records exactly one unpark and removes the marker (no regression). - [ ] A sweep against a still-fresh marker records nothing and leaves the marker in place. - [ ] Per-account (fp-keyed) markers each still produce their own single unpark edge. - [ ] Confirm whether `q_limit_lost` is skewed by the pre-existing duplicate pairs (deliberately kept by #117 as real edges); only change the query if it is shown to mis-pair/double-count — otherwise leave the historical rows and the query untouched and note the finding. **Out of scope:** - Deleting or rewriting the historical duplicate rows already in live `swarm.db` (kept intentionally by #117). - Changing `claude_limit_park`'s parking behavior or the marker format. - The Loss tab / `q_limit_lost` presentation beyond the correctness check above. --- **Triage ruling (ADR 0174 — veto anytime):** routed `ready-for-agent`. The fix lives in `limit-lib.sh` plus an offline test in `tests/` — not a protected path (no prompt skeleton, `.sandcastle/`, or `.forgejo/`), so no `ready-for-session` standing is needed. It is a two-way door: revertible by a later commit and provable by the new concurrent-sweep test. No one-way-door residue (no credentials, security widening, trust accept, data destruction, or spend) — the destructive-sounding part, touching live rows, is explicitly out of scope.
Author
Member

swarm-claim host=elitebook-distributed-server-03 run=23830
(automated multi-host claim — lowest live claim id works this ticket)

swarm-claim host=elitebook-distributed-server-03 run=23830 (automated multi-host claim — lowest live claim id works this ticket)
Author
Member

⏩ independent reviewer for #298 — verdict unauthenticated

review JSON
{
  "verdict": "unauthenticated",
  "findings": []
}
:fast_forward: **independent reviewer** for #298 — verdict `unauthenticated` <details><summary>review JSON</summary> ```json { "verdict": "unauthenticated", "findings": [] } ``` </details>
Author
Member

close-report for #298 — status success

✅ close-report gates passed — every claim verified against the pushed worker branch sandcastle/worker/20261021-233637-f21837 (pre-land — the host promotes it to main only after its post-run verify, #193/#283); issue closed.

envelope
{
  "issue": 298,
  "status": "success",
  "commits": [
    "eb47cac844f2bf27f10d461fd7faea373e1016e4"
  ],
  "changed_files": [
    "docs/GOTCHAS.md",
    "docs/gotchas/79-a-stat-event-rm-marker-exit-double-writes-under-a-race.md",
    "docs/journal/2026-10-22-298.md",
    "limit-lib.sh",
    "tests/limit-lib-test.sh",
    "tests/swarm-db-test.sh"
  ],
  "tests": [
    {
      "command": "bash tests/limit-lib-test.sh",
      "exit_code": 0
    },
    {
      "command": "bash tests/swarm-db-test.sh",
      "exit_code": 0
    },
    {
      "command": "bash tests/docs-budget-test.sh",
      "exit_code": 0
    }
  ],
  "criteria": {
    "A test in tests/ runs two concurrent sweeps against one stale marker and asserts exactly ONE unpark row": "tests/limit-lib-test.sh block (c): 8 concurrent background claude_limit_sweep against one stale marker assert grep -c unpark == 1",
    "A single sweep against a stale marker still records exactly one unpark and removes the marker (no regression)": "tests/limit-lib-test.sh existing block (b) still green (unpark recorded, marker cleared); limit-lib.sh claim-by-rename preserves it",
    "A sweep against a still-fresh marker records nothing and leaves the marker in place": "tests/limit-lib-test.sh block (a) 'sweep must not close a still-fresh window' still green",
    "Per-account (fp-keyed) markers each still produce their own single unpark edge": "tests/limit-lib-test.sh block (d): two fp-keyed stale markers each close with their own fp, grep -c unpark == 2",
    "Confirm whether q_limit_lost is skewed by the pre-existing duplicate pairs; only change the query if shown to mis-pair/double-count, else leave rows+query untouched and note the finding": "NOT skewed: q_limit_lost pops the open window on the FIRST unpark so a duplicate same-second unpark is ignored (setdefault absorbs a duplicate park). Proven by new tests/swarm-db-test.sh case (park + two same-second unparks -> ONE 1200s window). swarm-db.py and the historical rows left untouched; finding noted in docs/journal/2026-10-22-298.md and GOTCHAS 79"
  },
  "summary": "claude_limit_sweep claims each stale marker by atomic rename so the limit-park unpark edge is recorded exactly once even when two ticks race; q_limit_lost was already duplicate-safe and is left untouched.",
  "blockers": []
}
**close-report** for #298 — status `success` :white_check_mark: **close-report gates passed** — every claim verified against the pushed worker branch `sandcastle/worker/20261021-233637-f21837` (pre-land — the host promotes it to `main` only after its post-run verify, #193/#283); issue closed. <details><summary>envelope</summary> ```json { "issue": 298, "status": "success", "commits": [ "eb47cac844f2bf27f10d461fd7faea373e1016e4" ], "changed_files": [ "docs/GOTCHAS.md", "docs/gotchas/79-a-stat-event-rm-marker-exit-double-writes-under-a-race.md", "docs/journal/2026-10-22-298.md", "limit-lib.sh", "tests/limit-lib-test.sh", "tests/swarm-db-test.sh" ], "tests": [ { "command": "bash tests/limit-lib-test.sh", "exit_code": 0 }, { "command": "bash tests/swarm-db-test.sh", "exit_code": 0 }, { "command": "bash tests/docs-budget-test.sh", "exit_code": 0 } ], "criteria": { "A test in tests/ runs two concurrent sweeps against one stale marker and asserts exactly ONE unpark row": "tests/limit-lib-test.sh block (c): 8 concurrent background claude_limit_sweep against one stale marker assert grep -c unpark == 1", "A single sweep against a stale marker still records exactly one unpark and removes the marker (no regression)": "tests/limit-lib-test.sh existing block (b) still green (unpark recorded, marker cleared); limit-lib.sh claim-by-rename preserves it", "A sweep against a still-fresh marker records nothing and leaves the marker in place": "tests/limit-lib-test.sh block (a) 'sweep must not close a still-fresh window' still green", "Per-account (fp-keyed) markers each still produce their own single unpark edge": "tests/limit-lib-test.sh block (d): two fp-keyed stale markers each close with their own fp, grep -c unpark == 2", "Confirm whether q_limit_lost is skewed by the pre-existing duplicate pairs; only change the query if shown to mis-pair/double-count, else leave rows+query untouched and note the finding": "NOT skewed: q_limit_lost pops the open window on the FIRST unpark so a duplicate same-second unpark is ignored (setdefault absorbs a duplicate park). Proven by new tests/swarm-db-test.sh case (park + two same-second unparks -> ONE 1200s window). swarm-db.py and the historical rows left untouched; finding noted in docs/journal/2026-10-22-298.md and GOTCHAS 79" }, "summary": "claude_limit_sweep claims each stale marker by atomic rename so the limit-park unpark edge is recorded exactly once even when two ticks race; q_limit_lost was already duplicate-safe and is left untouched.", "blockers": [] } ``` </details>
Author
Member

Healer note (run 23830, signature 5b8242a74b26) — this closed ticket's work is NOT on main.

The close-report gate passed and closed this ticket against the pushed worker branch (push-mode close-before-land, GOTCHAS 72's documented residual / #288). The subsequent land then failed: merging sandcastle/worker/20261021-233637-f21837 onto main conflicted on docs/GOTCHAS.md (UU was the only conflicted path) because the concurrent #299 worker landed its own hazard row 79 first.

Stranded commit: eb47cac on origin/sandcastle/worker/20261021-233637-f21837, carrying limit-lib.sh, tests/limit-lib-test.sh, tests/swarm-db-test.sh, docs/gotchas/79-a-stat-event-rm-marker-exit-double-writes-under-a-race.md and docs/journal/2026-10-22-298.md.

Landing it needs the hazard renumbered 79 → 80 (79 is taken by #299 on main) — a body-file rename plus the index row and in-body heading. That is a human merge-authority action (MERGE_AUTHORITY=human); the healer deliberately did not do it or reopen this ticket, since #288 leaves the reopen-vs-flag question open.

The underlying conflict surface is ticketed as #300.

**Healer note (run 23830, signature `5b8242a74b26`) — this closed ticket's work is NOT on `main`.** The close-report gate passed and closed this ticket against the *pushed worker branch* (push-mode close-before-land, GOTCHAS 72's documented residual / #288). The subsequent land then failed: merging `sandcastle/worker/20261021-233637-f21837` onto `main` conflicted on `docs/GOTCHAS.md` (`UU` was the only conflicted path) because the concurrent #299 worker landed its own hazard row `79` first. Stranded commit: **`eb47cac`** on `origin/sandcastle/worker/20261021-233637-f21837`, carrying `limit-lib.sh`, `tests/limit-lib-test.sh`, `tests/swarm-db-test.sh`, `docs/gotchas/79-a-stat-event-rm-marker-exit-double-writes-under-a-race.md` and `docs/journal/2026-10-22-298.md`. Landing it needs the hazard renumbered **79 → 80** (79 is taken by #299 on `main`) — a body-file rename plus the index row and in-body heading. That is a human merge-authority action (`MERGE_AUTHORITY=human`); the healer deliberately did not do it or reopen this ticket, since #288 leaves the reopen-vs-flag question open. The underlying conflict surface is ticketed as #300.
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#298
No description provided.