test/state-sync.test.sh is flaky on main — two mirror assertions fail every other run on a node
Id: TD-PPagop-26081408 · Status: resolved · Filed: 2026-08-14 · Resolved: 2026-08-15 · Ref: https://github.com/Poetic-Poems/agent-ops/pull/396
test/state-sync.test.sh fails intermittently, and it fails on main, so
it can turn any pull request’s build red for a reason that has nothing to do
with the diff under review. Filed from the automated review of PR #381
(2026-08-14), which measured it rather than just hitting it:
- on
agent/360: 3 isolated runs → 2 pass, 1 fail - on
origin/main(clean worktree, same box — nodepoetic-1): 4 runs → 2 pass, 2 fail
The same four-run loop on a WSL2 developer host passed 4/4, so the flake is
timing- or environment-sensitive, not a deterministic fault in
scripts/state-sync.sh — which also means a fix has to start by making it
reproducible, not by staring at the mirror logic.
The two assertions that intermittently fail, always together:
FAIL - the mirror keeps cycles_retained cycles expected: 200 actual: 1FAIL - a stream already in the mirror is deleted from it expected: 0 actual: 1Why it matters
Section titled “Why it matters”.github/workflows/build-image.yml runs each test file once, with no retry,
and fails the job on any failure. At the measured rate, roughly a third to a
half of pull-request builds on an affected box can go red spuriously. Every
such red costs either a human noticing and re-running the job, or — worse —
a pipeline cycle treating a healthy PR as failing CI (requirement 38’s
CI-green gates read exactly this signal). Until fixed, the standing remedy
is: re-run the job before diagnosing any state-sync red.
Where to look first
Section titled “Where to look first”Both failing assertions read the mirror immediately after a push whose exit status the test discards:
test/state-sync.test.sh:209— `sync_as “$active_home” active push/dev/null
, then clone and count (:211, theexpected: 200` failure);test/state-sync.test.sh:302— same shape, then the--delete-excludedassertion (:305).
scripts/state-sync.sh runs under set -euo pipefail and do_push has two
ways to end without touching the mirror while the test carries on regardless:
- any mid-push abort (a failed
rsync,git fetch/reset,jq) exits non-zero — but these two call sites never look, so the subsequent clone measures the previous push’s mirror. The previous push (:184) mirrored exactly one cycle directory, which is precisely theactual: 1the first failure reports; - the mirror lock (
scripts/state-sync.sh:181,flock -n) exits 0 — “another state-sync holds the mirror — nothing to do” — on contention. Sequential test pushes should never contend, but if anything can hold that lock across calls, the push is a silent no-op with a green exit code, indistinguishable at the call site from success.
Other call sites in the same test do capture the exit status (e.g. :192,
assert_eq "a standby push exits 0"), so the first cheap instrumentation is
to assert on $? at every sync_as … push — that alone converts “mystery
stale mirror” into “which command failed”, and may be enough to expose the
mechanism the next time it fires.
The failure shape (a retained-count of 1, a mirror file that should be gone
still present) reads like a timing or ordering assumption in the fixture
rather than a fault in scripts/state-sync.sh itself; the review that filed
this did not diagnose further.
Suggested fix
Section titled “Suggested fix”- Make every
sync_as … push/fetchin the suite assert its exit status, so a flaky run names the failing step instead of a downstream symptom. - Loop the suite (or just the mirror-retention and
--delete-excludedsections) on a box that reproduces — nodepoetic-1did on 2026-08-14 — and capturebash -xofstate-sync.shon the failing iteration. - Fix the cause; if it is a real ordering assumption in the fixture, pin it
with a deterministic regression, and if it is in
do_push, decide deliberately whether the lock-contention path’sexit 0is right for the end-of-cycle push (agent-cycle.sh’scleanupcalls it best-effort, so a non-zero there costs nothing). - Until resolved: a
state-syncred on an otherwise-green PR warrants a re-run before any diagnosis.
Related
Section titled “Related”- PR #381’s review comment of 2026-08-14T06:10Z — the measurement this record preserves.
- Poetic’s TD26072901 is the same class on another repo (mtime-granularity flake in regeneration tests): flaky suites that redden unrelated PRs are a fleet-wide cost, because pipeline cycles read CI as a gate.