Skip to content

fix(bin): read status file identity and size with one stat call in the wake drain - #6271

Merged
kunchenguid merged 1 commit into
kunchenguid:mainfrom
Marsjohn-11:perf/drain-fewer-forks
Oct 31, 2026
Merged

kunchenguid merged 1 commit into
kunchenguid:mainfrom
Marsjohn-11:perf/drain-fewer-forks

Conversation

@Marsjohn-11

@Marsjohn-11 Marsjohn-11 commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

What Changed

  • Added _fm_status_stat_fields to bin/fm-classify-lib.sh. It runs one stat per status file and sets both _FM_STAT_IDENTITY and _FM_STAT_SIZE. Before this change, every identity-plus-size read ran up to four stat calls. _fm_open_decisions_file_ident now uses the new helper, and the strong:/weak: identity format stays the same.
  • Added _fm_status_read_ident_size, which honors the injected FM_STATUS_IDENTITY_READER / FM_STATUS_SIZE_READER test hooks. The five drain paths that used to call the identity and size helpers separately now call it: incremental open-decisions, the outcome cursor, the backstop cursor, the presentation snapshot commit, and the open-decisions cursor. A failed read still takes each path's existing fallback.
  • Added test_status_identity_and_size_read_with_one_stat to tests/fm-fork-free-helpers.test.sh. It checks that the paired read matches the single helpers and fails on a missing file. It also checks that the injected readers are honored. On non-Darwin hosts it also checks that the paired read runs stat exactly once and that a zero birth epoch produces a weak: identity.

Risk Assessment

✅ Low: The change folds two stat execs into one. It keeps the same strong/weak identity and size semantics on Darwin and Linux, honors injected test readers, and every caller still fails closed on a read error; it contains no Amazon-internal content.

Testing

The fork-free helper suite passed all 7 cases on stock Bash 3.2. A base-vs-target check on real files showed the identity and size values are byte-identical, so persisted cursors stay valid. The missing-file failure is unchanged, and the target is about 2.4x faster on this loaded host. All four wake-drain suites passed in disposable state. They drive the real cursor, outcome-backstop, presentation-snapshot and open-decision paths. The Linux weak-identity branch can't run on macOS and no Docker is available, so Linux CI covers that branch.

  • Live validation: ✅ go - 5 of 6 scenarios driven live against the product
Scenario Result Live Evidence
Combined stat read returns the same identity and size as the old separate helpers on macOS ✅ pass live ident-size-equivalence.txt: base and target print identical strong:<dev>:<ino>:<birth> identities and sizes for a non-empty and an empty file
A missing status file still fails the read, so the drain keeps its trusted state ✅ pass live ident-size-equivalence.txt: missing.status gives ERR on base and target, and the paired read also gives ERR
Wake drain reads identity and size with fewer stat execs and runs faster ✅ pass live ident-size-bench.txt: 52s old vs 22s new for 200 reads
Helper works on stock macOS Bash 3.2 ✅ pass live fork-free-helpers.log under /bin/bash 3.2.57: 7/7 ok
Wake drain cursor, backstop, snapshot and open-decision paths still behave end to end ✅ pass live drain-summary.txt: all four fm-wake-drain-* suites rc=0, 50 ok and 0 not ok
Weak identity when the filesystem reports birth epoch 0 (Linux branch) ⏸️ untested no This host is macOS, where the library calls /usr/bin/stat by absolute path, so a PATH shim can't fake epoch 0. Docker isn't running, so no Linux environment is available. The Linux CI job runs this su…
Evidence: fork-free helper suite transcript

Source: fork-free helper suite transcript

1..7
ok - fm_dirname_to and fm_basename_to match dirname and basename on every edge case
ok - fm_epoch_seconds_to reads the clock like date +%s and forks date at most as often
ok - signal seen paths and absolute lock paths are byte-identical to the dirname/basename/tr forms
ok - recovery marker reads accept exactly the one-line records wc -l accepted
ok - window_to_task resolves every recorded window exactly as the grep/tail/cut pipeline did
ok - classify stat helpers resolve the kernel name once per process
ok - status identity and size come from one stat read and match the single helpers
FM_TEST_COMPLETE
bash tests/fm-fork-free-helpers.test.sh  1.78s user 4.76s system 32% cpu 20.354 total
Evidence: base vs target identity/size equivalence

Source: base vs target identity/size equivalence

\### base 095db3bc (bash 3.2)
s.status ident=strong:16777233:1730196799:1793421813.402235914 size=18
empty.status ident=strong:16777233:1730196800:1793421813.402430537 size=0
missing.status ident=ERR size=ERR
\### target 65166e9e (bash 3.2)
s.status ident=strong:16777233:1730196799:1793421813.402235914 size=18
  paired ident=strong:16777233:1730196799:1793421813.402235914 size=18
empty.status ident=strong:16777233:1730196800:1793421813.402430537 size=0
  paired ident=strong:16777233:1730196800:1793421813.402430537 size=0
missing.status ident=ERR size=ERR
  paired ERR
Evidence: stat fork benchmark

Source: stat fork benchmark

old: 200 identity+size reads in 52s new: 200 identity+size reads in 22s

old: 200 identity+size reads in 52s
new: 200 identity+size reads in 22s
Evidence: wake-drain suite exit codes

Source: wake-drain suite exit codes

fm-wake-drain-open-decisions rc=0
fm-wake-drain-unread-status rc=0
fm-wake-drain-outcome-backstop rc=0
fm-wake-drain-open-decisions-cursor rc=0
Evidence: wake-drain open-decisions-cursor log

Source: wake-drain open-decisions-cursor log

ok - terminal supersession reaches whole-file reads, incremental drains, old caches, and captured spans
ok - folded decisions are rebuilt when task-kind evidence changes
ok - a truncated/rewritten log falls back to a full re-fold instead of dropping or misreading the decision
ok - a same-size file rotation (new inode) is detected and falls back to a full re-fold
ok - a failed presentation read preserves status state for retry
ok - a cursor-cache read failure refolds decisions without replaying handled unread status
ok - a pre-fix cursor is rebuilt so a previously skipped corr-tagged decision surfaces
ok - an old fold cache is rebuilt once before same-version incremental reads resume
ok - a buried decision survives many growing drains with bounded read cost, and resolution durably clears it at bounded cost too
Evidence: wake-drain open-decisions log

Source: wake-drain open-decisions log

ok - a needs-decision buried under later routine/other-key lines still reports as open
ok - an over-long open decision is cut to its per-item budget with the shared truncation marker
ok - an explicit resolved [key=X] closes the keyed decision
ok - a later unrelated terminal line never clears an open decision
ok - a reserved decision key can only be opened or closed by its owning library
ok - no open decisions across the fleet prints nothing
WAKE_ACK_REQUIRED: after handling completes run bin/fm-wake-drain.sh --ack-through 1 --recovery-generation 48719.1793421900.UfGq47
ok - the open-decision section is fleet-wide, not scoped to this drain's own queued records
ok - a buried open decision surfaces even when the wake queue itself is empty
ok - the fleet-wide decision scan does not follow status symlinks
Evidence: wake-drain outcome-backstop log

Source: wake-drain outcome-backstop log

ok - a newest keyless done, blocked, or needs-decision event with no newer branch outcome surfaces on the next main drain
ok - a newer task-matching branch outcome suppresses the backstop and routine latest events stay silent
ok - only a strictly newer outcome for the same task can suppress its latest captain event
ok - a branch annotation and queue acknowledgement cannot consume the main drain's loss backstop
ok - status byte position distinguishes handled and later same-second events
ok - drain cost and suppression are independent of append-only outcome history
ok - backstop receipts prevent repeats without consuming delayed signal annotations
ok - a failed output consumer leaves the backstop unacknowledged for retry
ok - receipt failure may repeat a presented backstop but cannot lose it
ok - captain-facing decisions rejected by the fold surface once
ok - a missing outcome index self-heals on the first drain and suppresses its handled status
ok - a fresh home with a status log and no index surfaces the backstop on its first drain
ok - a genuine outcome-store fault fails closed without Pi-specific restart advice
ok - held-lock initialization rejects callers outside the lock owner's process tree
ok - held-lock initialization accepts a descendant of the outcome lock owner
ok - index self-heal runs only while the outcome lock is held
ok - an over-bound unclassifiable routine event stays silent
ok - the outcome backstop caps each item and its total task output deterministically
Evidence: wake-drain unread-status log

Source: wake-drain unread-status log

ok - a note: answer buried under a later routine note: is surfaced with both lines
ok - already-presented note: lines are not re-surfaced on the next drain
ok - a brand-new note: after presentation is surfaced without replaying handled lines
ok - a queued status signal annotates every unread note, not only the newest
WAKE_ACK_REQUIRED: after handling completes run bin/fm-wake-drain.sh --ack-through 1 --recovery-generation 90113.1793421932.pglwxW
WAKE_ACK_REQUIRED: after handling completes run bin/fm-wake-drain.sh --ack-through 1 --recovery-generation 90113.1793421932.pglwxW
WAKE_ACK_REQUIRED: after handling completes run bin/fm-wake-drain.sh --ack-through 1 --recovery-generation 90113.1793421932.pglwxW
ok - a pending-reply resolution buried under a later note surfaces once and closes OPEN DECISIONS
WAKE_ACK_REQUIRED: after handling completes run bin/fm-wake-drain.sh --ack-through 1 --recovery-generation 26073.1793421968.hdLLlP
WAKE_ACK_REQUIRED: after handling completes run bin/fm-wake-drain.sh --ack-through 2 --recovery-generation 26073.1793421968.hdLLlP
WAKE_ACK_REQUIRED: after handling completes run bin/fm-wake-drain.sh --ack-through 2 --recovery-generation 26073.1793421968.hdLLlP
WAKE_ACK_REQUIRED: after handling completes run bin/fm-wake-drain.sh --ack-through 2 --recovery-generation 26073.1793421968.hdLLlP
ok - a self-announced pending-reply close does not wake yet still surfaces once in UNREAD STATUS
ok - unread status over the former byte cap preserves every line
ok - presentation cursor advances only through its captured endpoint
ok - a reused task id starts its replacement status log unread at byte zero
ok - fallback identity still presents and advances status state
ok - snapshot failures are reported visibly
ok - OPEN DECISIONS still folds needs-decision/blocked independently of unread notes
WAKE_ACK_REQUIRED: after handling completes run bin/fm-wake-drain.sh --ack-through 1 --recovery-generation 77320.1793422095.SOiMM5
ok - an empty-queue backstop presentation still preserves the status for its later signal annotation
ok - routine working and branch-covered done lines print nothing on an empty-queue drain

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

⚠️ **Review** - 1 info
  • ℹ️ bin/fm-classify-lib.sh:1240 - _fm_open_decisions_file_ident still declares local f=$1 epoch birth ident, but epoch, birth and ident are no longer used now that parsing moved into _fm_status_stat_fields. Trim the declaration to local f=$1.
✅ **Test** - passed

✅ No issues found.

  • Live validation: ✅ go - 5 of 6 scenarios driven live against the product
Scenario Result Live Evidence
Combined stat read returns the same identity and size as the old separate helpers on macOS ✅ pass live ident-size-equivalence.txt: base and target print identical strong:<dev>:<ino>:<birth> identities and sizes for a non-empty and an empty file
A missing status file still fails the read, so the drain keeps its trusted state ✅ pass live ident-size-equivalence.txt: missing.status gives ERR on base and target, and the paired read also gives ERR
Wake drain reads identity and size with fewer stat execs and runs faster ✅ pass live ident-size-bench.txt: 52s old vs 22s new for 200 reads
Helper works on stock macOS Bash 3.2 ✅ pass live fork-free-helpers.log under /bin/bash 3.2.57: 7/7 ok
Wake drain cursor, backstop, snapshot and open-decision paths still behave end to end ✅ pass live drain-summary.txt: all four fm-wake-drain-* suites rc=0, 50 ok and 0 not ok
Weak identity when the filesystem reports birth epoch 0 (Linux branch) ⏸️ untested no This host is macOS, where the library calls /usr/bin/stat by absolute path, so a PATH shim can't fake epoch 0. Docker isn't running, so no Linux environment is available. The Linux CI job runs this su…
  • bash tests/fm-fork-free-helpers.test.sh (all 7 cases pass, including the new test_status_identity_and_size_read_with_one_stat, run under stock /bin/bash 3.2)
  • Manual equivalence check: sourced bin/fm-classify-lib.sh from base 095db3bc and target 65166e9e under /bin/bash 3.2 and compared identity and size for a non-empty file, an empty file and a missing file
  • Manual benchmark: 200 identity+size reads, base pair of helpers vs target _fm_status_read_ident_size
  • bash tests/fm-wake-drain-open-decisions-cursor.test.sh (rc=0, 9 ok)
  • bash tests/fm-wake-drain-open-decisions.test.sh (rc=0, 9 ok)
  • bash tests/fm-wake-drain-outcome-backstop.test.sh (rc=0, 18 ok)
  • bash tests/fm-wake-drain-unread-status.test.sh (rc=0, 14 ok)
✅ **Document** - passed

✅ No issues found.

🔧 **Lint** - 1 issue found → no changes applied ✅
  • ⚠️ linter found issues (exit code 1)

🔧 No changes applied.
✅ Re-checked - no issues remain.

✅ **Push** - passed

✅ No issues found.

@greptile-apps

greptile-apps Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The reviewed changes appear safe to merge.

Reviews (4) · Last reviewed commit: "fix(bin): cut stat forks in the wake dra..." · Reviewed by Greptile

Comment thread tests/fm-fork-free-helpers.test.sh Resolved
@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate: first stamp vs tip 549e07f37fd73aa01d74cd126b1111c99175abed. No GitHub closing issue (intent is wake-drain fork cost under load). Tip still does separate identity+size stat calls in the incremental open-decisions path.

HEAD 9d3ab97c150f0fdacaa90b90055fc92bce26316e. Attestation: MISMATCH — body attests 2be50097fcea... (pre-lint-cap push); NM FAILURE asks for a fresh git push no-mistakes on this head. Behavior portable + Herdr + Lint green. Greptile SUCCESS (earlier P2 weak-identity; latest head adds weak-path coverage). MERGEABLE/UNSTABLE. Author Marsjohn-11 not blocked.

contract-class: restore — wake drain already incremental; one-stat identity+size restores intended drain completion under load (timeouts at 15–100s). Lint default address-space 12→16 GiB and CI fork-free count 6→7 are supporting CI envelope for growing ShellCheck roots / new test — not a new captain-facing surface (not #4627 new-default).

VISION (per-rule)

  • One captain / Authority: aligns — no new autonomy or captain surface.
  • Scripts own mechanics / token efficiency: aligns — fewer forks, same identity/size semantics.
  • Restart is a non-event: aligns — cursor/identity format preserved (strong:/weak:).
  • Scope: aligns — classify helpers + lint envelope + tests; not workshop growth.

Waiting on you: re-run git push no-mistakes so attestation binds to 9d3ab97c. Not otherwise-ready → no Firstmate flag.

@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate: thanks for the rebase and the fresh no-mistakes raise, @Marsjohn-11. I re-read head 0185e9a3 against main fb75c1f9.

Delta since my 10-01 stamp: attestation now binds this head (MATCH) and "PR must be raised via no-mistakes" is green. CI is green except Behavior portable serial 2, which was cancelled at the 30-minute shard limit, a known CI flake family (#6790), not your code. I re-ran the failed job.

contract-class: restore (my own read). One stat now yields the same strong:/weak: identity and size the separate calls gave. The injected readers and failure paths are kept. There's no new surface or default. The lint cap 12→16 GiB and the CI helper count 6→7 are CI envelope.

VISION: One captain: aligns (no captain surface). Authority: aligns (no new autonomy). Scripts/mechanics + token efficiency: aligns (fewer forks, same semantics). Restart non-event: aligns (cursor/identity format unchanged). Delegation: aligns. Vendor: aligns (Darwin + GNU stat both handled). Scope: aligns.

One gentle ask, so this is waiting on you: the PR body says the Test step ran out of budget before live validation, so there's no live evidence for this head. When you have a moment, could you live-test the wake drain on a real home (for example, a drain with several status files, confirming identities and cursors carry over and fewer stat forks run) and post what you saw? Or re-raise through git push no-mistakes so the Test step can finish. Once there's evidence and CI is green, this is set up to merge as a restore.

…ze together

Local cherry-pick of kunchenguid#6271 pending upstream merge.
@Marsjohn-11
Marsjohn-11 force-pushed the perf/drain-fewer-forks branch from 0185e9a to 65166e9 Compare October 31, 2026 04:33
@Marsjohn-11 Marsjohn-11 changed the title fix(bin): cut stat forks in the wake drain by reading identity and size together fix(bin): read status file identity and size with one stat call in the wake drain Oct 31, 2026
@Marsjohn-11

Copy link
Copy Markdown
Contributor Author

Rebased onto current main and revalidated through no-mistakes. Full suite awaits the maintainer's full-ci label; no full-suite lane failed (see CONTRIBUTING.md, The full-ci label).

@kunchenguid kunchenguid added the full-ci Maintainer go-ahead: run the full CI suite on this outside pull request label Oct 31, 2026
@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate: thank you @Marsjohn-11 for the rebase and the fresh no-mistakes run. I re-read head 65166e9e against main 095db3bc. The branch is now just bin/fm-classify-lib.sh and tests/fm-fork-free-helpers.test.sh. The earlier lint-cap and helper-count changes are gone. One stat per status file now fills both _FM_STAT_IDENTITY and _FM_STAT_SIZE, with the same strong:/weak: format and the same fail-closed fallbacks. The injected readers are still honored, and all five drain paths use the paired read.

The live evidence I asked for on 10-10 is in: 5 of 6 scenarios were driven live, with byte-identical identities and sizes against base and a missing file still failing the read. The no-mistakes attestation binds this head with no skipped steps, the quick checks pass, and Greptile's thread is resolved. I've added full-ci, so the full suite is running now. If it's green, this lands as a restore. Nothing is needed from you.

Contract-class: restore (my own read): no new surface or default, just fewer forks with the same semantics. VISION: every rule aligns, as in my 10-10 note.

@kunchenguid
kunchenguid merged commit 0593d83 into kunchenguid:main Oct 31, 2026
50 of 54 checks passed
@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate: this is merged. Thank you @Marsjohn-11 — really appreciate you taking the time on this.