claim-next-task.sh: cumulative sequential API latency can exceed the 30s prompt-expansion budget → PromptExpansionTimeoutError (rare, load-sensitive) #77

Closed
opened 2026-08-24 18:45:22 +00:00 by matou-build-agent · 2 comments

Symptom

swarm run 646, worker swarm (2) RED at prompt expansion:

stage=sandcastle run (workers)  exit=1
PromptExpansionTimeoutError: Shell expression `bash .sandcastle/claim-next-task.sh` timed out after 30008ms

Its matrix sibling swarm (1) of the same run SUCCEEDED against the same
queue. 49/50 runs in the evidence window (18:35–18:38) passed; this is a rare,
load-sensitive RED, not a crash — but it is now newly likely (see below).

Root cause — cumulative sequential API latency vs the 30 s prompt-expansion budget

claim-next-task.sh (source at repo root, vendored to .sandcastle/) makes
O(ready-queue) sequential Forgejo calls inside a single Sandcastle
shell-expression, whose budget is 30 s:

  1. list-ready-tasks.sh: page loop over ?labels=ready-for-agent, then a
    10-wide dependency check per issue, then standing-drive + its deps.
  2. claim_alive_runs (/actions/tasks?limit=100).
  3. The claim walk: a matrix loser walks the ready list doing
    claim_post + claim_won (comments fetch) + delete ≈ 3 calls per
    contested ticket
    .

At the live latency probed during this incident (~1.5 s/call; api-timing.txt
also 1 s), a full walk over a non-trivial queue sums to ~20–25 s — inside 30 s
but with no margin. A modest latency bump (concurrent matrix sibling + a triage
run overlapping) tips one worker past 30 s. Worker (1) won the head in one pass
and finished fast; worker (2) walked and timed out — the classic asymmetry.

The internal CLAIM_API_MAX_TIME/LIST_READY_MAX_TIME = 30 s cannot rescue
this: they EQUAL the outer prompt-expansion budget, so a single slow call fires
the outer timeout at the same instant it would have failed closed. The
per-call timeout was designed (#28, GOTCHAS 16) to fail-closed to []; it can
only do so if the sum of the tick's calls stays under the outer budget, which
it does not bound.

Why now

Commits b5dbea0 / 687c064 landed the fleet-monitor DAG: #64–#75 are all
ready-for-agent
(13 issues in queue at incident time), most with native
depends_on edges. Every ready issue costs a dependency GET even when it is
filtered out as blocked, so the per-tick claim-expansion cost scaled up exactly
as this DAG stood in the queue.

Class & scope

Product-class — the logic lives in root source (claim-next-task.sh,
list-ready-tasks.sh, claim-lib.sh). Per the heal charter the healer does not
touch product code. A root fix does not reach the running .sandcastle/ copy
until a human re-vendors
(onboard.sh vendor .sandcastle <sha>), so the storm
window persists until then.

Suggested directions (design decision — not prescribed)

  • Give claim-next-task.sh an overall wall-clock budget (~22–25 s) that
    returns [] gracefully and lets the cron/backstop re-fire, instead of
    relying on per-call --max-time = the outer budget.
  • Drop per-call --max-time below the 30 s outer budget so a genuine single-call
    stall fails closed within budget.
  • Cap the claim-walk depth per tick (a loser need not try every ticket).
  • Reduce dependency-check fan-out cost for large ready DAGs (batch, or only
    dep-check the head candidates).

Evidence

Heal incident dir /tmp/matou-heal-Matou-dev-factory.QTQfiI — run-verdict.txt,
worker-logs.txt (worker (2) tail ending at the timeout), runs.json (20 swarm
runs / 3.3 min, 49 success + this 1 RED), api-timing.txt (1 s), host.txt
(disk 27 %, mem healthy — not resource starvation). Signature 0fd2fa26ccd7.

## Symptom `swarm` run 646, worker `swarm (2)` RED at prompt expansion: stage=sandcastle run (workers) exit=1 PromptExpansionTimeoutError: Shell expression `bash .sandcastle/claim-next-task.sh` timed out after 30008ms Its matrix sibling `swarm (1)` of the **same run** SUCCEEDED against the same queue. 49/50 runs in the evidence window (18:35–18:38) passed; this is a rare, load-sensitive RED, not a crash — but it is now newly likely (see below). ## Root cause — cumulative sequential API latency vs the 30 s prompt-expansion budget `claim-next-task.sh` (source at repo root, vendored to `.sandcastle/`) makes **O(ready-queue) sequential Forgejo calls** inside a single Sandcastle shell-expression, whose budget is **30 s**: 1. `list-ready-tasks.sh`: page loop over `?labels=ready-for-agent`, then a **10-wide dependency check per issue**, then standing-drive + its deps. 2. `claim_alive_runs` (`/actions/tasks?limit=100`). 3. The claim walk: a matrix **loser** walks the ready list doing `claim_post` + `claim_won` (comments fetch) + delete ≈ **3 calls per contested ticket**. At the live latency probed during this incident (~1.5 s/call; `api-timing.txt` also 1 s), a full walk over a non-trivial queue sums to ~20–25 s — inside 30 s but with no margin. A modest latency bump (concurrent matrix sibling + a `triage` run overlapping) tips one worker past 30 s. Worker (1) won the head in one pass and finished fast; worker (2) walked and timed out — the classic asymmetry. The internal `CLAIM_API_MAX_TIME`/`LIST_READY_MAX_TIME` = **30 s** cannot rescue this: they EQUAL the outer prompt-expansion budget, so a single slow call fires the outer timeout at the same instant it would have failed closed. The per-call timeout was designed (#28, GOTCHAS 16) to fail-closed to `[]`; it can only do so if the *sum* of the tick's calls stays under the outer budget, which it does not bound. ## Why now Commits b5dbea0 / 687c064 landed the fleet-monitor DAG: **#64–#75 are all `ready-for-agent`** (13 issues in queue at incident time), most with native `depends_on` edges. Every ready issue costs a dependency GET even when it is filtered out as blocked, so the per-tick claim-expansion cost scaled up exactly as this DAG stood in the queue. ## Class & scope Product-class — the logic lives in root source (`claim-next-task.sh`, `list-ready-tasks.sh`, `claim-lib.sh`). Per the heal charter the healer does not touch product code. **A root fix does not reach the running `.sandcastle/` copy until a human re-vendors** (`onboard.sh vendor .sandcastle <sha>`), so the storm window persists until then. ## Suggested directions (design decision — not prescribed) - Give `claim-next-task.sh` an **overall wall-clock budget** (~22–25 s) that returns `[]` gracefully and lets the cron/backstop re-fire, instead of relying on per-call `--max-time` = the outer budget. - Drop per-call `--max-time` below the 30 s outer budget so a genuine single-call stall fails closed within budget. - Cap the claim-walk depth per tick (a loser need not try every ticket). - Reduce dependency-check fan-out cost for large ready DAGs (batch, or only dep-check the head candidates). ## Evidence Heal incident dir `/tmp/matou-heal-Matou-dev-factory.QTQfiI` — `run-verdict.txt`, `worker-logs.txt` (worker (2) tail ending at the timeout), `runs.json` (20 swarm runs / 3.3 min, 49 success + this 1 RED), `api-timing.txt` (1 s), `host.txt` (disk 27 %, mem healthy — not resource starvation). Signature `0fd2fa26ccd7`.
Author
Member

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

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

close-report for #77 — status success

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

envelope
{
  "issue": 77,
  "status": "success",
  "commits": [
    "26f8ad1c965a412b1365a5524828488a6d11e923"
  ],
  "changed_files": [
    "claim-next-task.sh",
    "tests/claim-next-task-test.sh",
    "docs/GOTCHAS.md",
    "STATUS.md"
  ],
  "tests": [
    {
      "command": "bash tests/claim-next-task-test.sh",
      "exit_code": 0
    },
    {
      "command": "for t in tests/*.sh onboarding/tests/*.sh; do case \"$t\" in *fakebin*|*fixtures*) continue;; esac; bash \"$t\" || echo FAIL: $t; done",
      "exit_code": 0
    }
  ],
  "criteria": {
    "Overall wall-clock budget that returns [] gracefully and lets cron/backstop re-fire": "claim-next-task.sh: SECONDS=0 + CLAIM_NEXT_BUDGET (22s) checked before each claim-walk iteration; on exhaustion emits [] and exits 0. Test T-#77a (budget spent emits [] / posts NO claim comment / warns on stderr).",
    "Drop per-call --max-time below the 30s outer budget so a single stall fails closed within budget": "claim-next-task.sh sets CLAIM_API_MAX_TIME=10 (in-sandbox path) before sourcing claim-lib.sh, which keeps its 30s default for the host-mode janitor. Tests T-#77b (claim calls use --max-time 10, never 30) and T-#77c (explicit override honoured).",
    "Cap the claim-walk depth per tick (a loser need not try every ticket)": "The wall-clock budget bounds the walk adaptively — the loop stops starting new claim attempts once CLAIM_NEXT_BUDGET is spent (latency-adaptive cap rather than a fixed depth). T-#77a proves the walk stops before claiming.",
    "GOTCHAS entry for the operational hazard": "docs/GOTCHAS.md #21 (Symptom/Cause/Rule) records the equal-timeout + cumulative-latency hazard and the two-bound rule; STATUS.md header + milestone entry updated in the same commit.",
    "Every lib change updates its test; full offline suite green": "tests/claim-next-task-test.sh extended (pass=29 fail=0); full suite loop from CLAUDE.md rule 2 printed no FAIL lines."
  },
  "summary": "Bounded claim-next-task.sh with a 22s wall-clock budget and a sub-30s per-call timeout so a deep ready DAG can no longer sum past the 30s prompt-expansion budget and RED the tick.",
  "blockers": []
}
**close-report** for #77 — status `success` :white_check_mark: **close-report gates passed** — every claim verified against main; issue closed. <details><summary>envelope</summary> ```json { "issue": 77, "status": "success", "commits": [ "26f8ad1c965a412b1365a5524828488a6d11e923" ], "changed_files": [ "claim-next-task.sh", "tests/claim-next-task-test.sh", "docs/GOTCHAS.md", "STATUS.md" ], "tests": [ { "command": "bash tests/claim-next-task-test.sh", "exit_code": 0 }, { "command": "for t in tests/*.sh onboarding/tests/*.sh; do case \"$t\" in *fakebin*|*fixtures*) continue;; esac; bash \"$t\" || echo FAIL: $t; done", "exit_code": 0 } ], "criteria": { "Overall wall-clock budget that returns [] gracefully and lets cron/backstop re-fire": "claim-next-task.sh: SECONDS=0 + CLAIM_NEXT_BUDGET (22s) checked before each claim-walk iteration; on exhaustion emits [] and exits 0. Test T-#77a (budget spent emits [] / posts NO claim comment / warns on stderr).", "Drop per-call --max-time below the 30s outer budget so a single stall fails closed within budget": "claim-next-task.sh sets CLAIM_API_MAX_TIME=10 (in-sandbox path) before sourcing claim-lib.sh, which keeps its 30s default for the host-mode janitor. Tests T-#77b (claim calls use --max-time 10, never 30) and T-#77c (explicit override honoured).", "Cap the claim-walk depth per tick (a loser need not try every ticket)": "The wall-clock budget bounds the walk adaptively — the loop stops starting new claim attempts once CLAIM_NEXT_BUDGET is spent (latency-adaptive cap rather than a fixed depth). T-#77a proves the walk stops before claiming.", "GOTCHAS entry for the operational hazard": "docs/GOTCHAS.md #21 (Symptom/Cause/Rule) records the equal-timeout + cumulative-latency hazard and the two-bound rule; STATUS.md header + milestone entry updated in the same commit.", "Every lib change updates its test; full offline suite green": "tests/claim-next-task-test.sh extended (pass=29 fail=0); full suite loop from CLAUDE.md rule 2 printed no FAIL lines." }, "summary": "Bounded claim-next-task.sh with a 22s wall-clock budget and a sub-30s per-call timeout so a deep ready DAG can no longer sum past the 30s prompt-expansion budget and RED the tick.", "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#77
No description provided.