state-sync.sh's do_push aborts with SIGPIPE (141) whenever `sort -r | head -n 1` races under pipefail, which is the long-standing state-sync test flake
Id: TD-PPagop-26082513 · Status: resolved · Filed: 2026-08-25 · Resolved: 2026-08-26 · Ref: https://github.com/Poetic-Poems/agent-ops/pull/808
Diagnosed while reviewing PR #774 (issue #662), which does not touch any of
the code below. TD-PPagop-26081408 recorded this flake symptomatically on
2026-08-14 and was resolved by PR #396 — which implemented that record’s own
first suggestion, “assert every sync_as … push’s exit status so a flaky run
names the failing step instead of a downstream symptom”. That instrumentation
is what makes the cause readable now: the assertion no longer reports a stale
mirror, it reports the exit code.
FAIL - the retention push exits 0 expected: 0 actual: 141141 is 128 + SIGPIPE.
scripts/state-sync.sh runs under set -euo pipefail. Inside do_push:
last_cycle="$(find "$state_dir/cycles" -mindepth 1 -maxdepth 1 -printf '%f\n' 2>/dev/null \ | sort -r | head -n 1)"head -n 1 prints its one line and exits immediately. If sort has not
finished writing by then it is killed by SIGPIPE and exits 141; pipefail
makes that the pipeline’s status, the command substitution’s assignment
fails, and set -e aborts do_push — after prune_local has already run
and before anything is mirrored or pushed. Whether sort has finished is a
scheduling race, so the same input fails only some of the time, which is
exactly the “fails every other run on a node, passes 4/4 on another box”
shape TD-PPagop-26081408 measured and could not pin down.
Reproduced in isolation on node poetic-1 (2026-08-25), 201 directories —
the count test/state-sync.test.sh’s retention fixture creates:
for i in $(seq 1 40); do rc=$( set -euo pipefail find "$d/cycles" -mindepth 1 -maxdepth 1 -printf '%f\n' \ | sort -r | head -n 1 >/dev/null; echo $? ) || rc=$? [[ "$rc" == 0 ]] || echo "iteration $i -> exit $rc"done# iteration 12 -> exit 141# iteration 14 -> exit 141# iteration 36 -> exit 141# SIGPIPE hits: 3 / 40Whole-suite rate measured the same day: test/state-sync.test.sh failed 1 of
5 runs on agent/662 and 0 of 3 on origin/main, always with the two
assertions above and always with actual: 141 — the second failure (the mirror keeps cycles_retained cycles, actual: 1) is the knock-on that
TD-PPagop-26081408 described, the clone measuring the previous push’s
mirror because this one aborted before mirroring.
kept_cycles() (scripts/state-sync.sh:250) has the identical
sort -r | head -n "$cycles_retained" shape and takes the same signal, but
it is consumed as done < <(kept_cycles) (:358), whose exit status bash
never examines, and head has by then already emitted every line the
consumer needed. That site is latent rather than harmful; it should still
move, so the idiom does not survive to be copied into a checked context.
Why it matters
Section titled “Why it matters”- In CI.
.github/workflows/build-image.ymlruns each test file once with no retry. Every spuriousstate-syncred costs a human noticing and re-running, or a pipeline cycle reading requirement 38’s CI-green gate and treating a healthy pull request as failing. This is the standing tax TD-PPagop-26081408 was filed about and it is still being paid. - In production, which the earlier record did not consider. This is not
test-only.
do_pushruns from the crontab and from every cycle’s owncleanup(), on realstate_dir/cyclesdirectories bounded bystate_local_cycles_retained— hundreds of entries on a long-lived node, the same order as the fixture. When the race is lost the node publishes nothing that tick: noheartbeat.json, no mirrored cycle, nolog.jsonl. The fleet dashboard then reads that node as increasingly stale, andagent-cycle.sh’s call is|| true, so nothing anywhere records that the push aborted. A monitoring blind spot of exactly the kind requirement 2.8 (the pull request this was found under) exists to close.
Suggested fix
Section titled “Suggested fix”Take the first line without letting the reader close the pipe early:
last_cycle="$(find "$state_dir/cycles" -mindepth 1 -maxdepth 1 -printf '%f\n' 2>/dev/null \ | sort -r | awk 'NR == 1')"awk reads its input to the end regardless of how much it prints, so sort
is never signalled and the pipeline’s status stays honest. Keeping head and
neutralising the status with an explicit || true is a smaller edit but
leaves a genuine sort/find failure indistinguishable from the race, so
prefer the reader that consumes its whole input.
Apply the same to kept_cycles() (:250) so the idiom does not survive
locally, and grep the tree for other | head -n under pipefail in a
checked context before closing this.
Then delete the standing remedy TD-PPagop-26081408 left in place — “a
state-sync red on an otherwise-green PR warrants a re-run before any
diagnosis” — since after this there should be no such red to re-run.
References to remove when this is resolved
Section titled “References to remove when this is resolved”- Nothing cites this record in code. It is named in the review thread of PR #774, and it supersedes the unresolved half of TD-PPagop-26081408 (resolved 2026-08-15), whose body should not be edited — it is a closed record — but which this one should be read alongside.