Skip to content

fix(bin): trace context-budget stand-down and notice write failures - #8

Merged
uayyagari merged 2 commits into
mainfrom
fix/context-budget-durable-trip-notices
Aug 19, 2026
Merged

uayyagari merged 2 commits into
mainfrom
fix/context-budget-durable-trip-notices

Conversation

@uayyagari

Copy link
Copy Markdown

Intent

Problem

  • Block-budget-exhausted stand-down write ignored its return status, so a failed write left the guard claiming a durability ("stands down for the rest of the session") it never persisted, with no trace.
  • The per-episode notice dedup file swallowed its own write failure, so an unwritable notice file (while the rest of state/ stayed writable) retraced every crossing on every turn end instead of once.

Fix

  • Check the stand-down write; on failure, degrade through stage_enter standdown-unrecorded and correct the session-facing message.
  • Add a fallback marker file for notice dedup fields, consulted only when the primary notice file write fails; trace the fallback itself once under stage_enter notice-unwritable. Preserves the trip record's write-only invariant.

Proof

  • New tests in tests/fm-context-budget.test.sh cover both degrades: one durable trip line and a corrected message for the stand-down case; one ceiling trip line plus one notice-unwritable trip line (not five) for the notice case.
  • Full suite passes, shellcheck (fm-lint.sh) clean, docs/context-budget.md updated.

What Changed

  • Check the block-budget-exhausted stand-down write in bin/fm-context-budget.sh; on write failure, degrade through stage_enter standdown-unrecorded and swap in a session message that says the stand-down could not be persisted, instead of falsely claiming a durability that never landed.
  • Add a NOTICE_DEGRADED_FILE fallback marker for the per-episode notice dedup fields, consulted only when the primary notice file write fails, so an unwritable notice file traces one notice-unwritable trip and keeps deduping crossings instead of retripping on every turn end; the fallback is pruned and reset alongside the other state records.
  • Cover both degrades with new cases in tests/fm-context-budget.test.sh and document the fallback file in docs/context-budget.md and the AGENTS.md state inventory.

Risk Assessment

✅ Low: Both degrades are narrowly scoped, preserve the trip record's write-only invariant, dedup correctly across turns via the new fallback marker, and are covered by targeted tests plus updated docs.

Testing

Baseline plus targeted run of tests/fm-context-budget.test.sh passes fully (75 ok, exit 0), and I demonstrated both fixes end-to-end by invoking the real fm-context-budget.sh Stop hook. Scenario A shows the corrected captain message ("could not be persisted ... may recur", with the false "stands down for the rest of the session" claim gone) and a durable stage=standdown-unrecorded trip line next to the ceiling line. Scenario B shows exactly one ceiling line plus one notice-unwritable line across five turn ends (not five), with the fallback dedup marker created. This is a CLI/hook change with no rendered UI surface, so the reviewer-visible evidence is CLI transcripts of the actual systemMessage and persisted trip-record state rather than screenshots. No issues found.

Evidence: Scenario A - stand-down write failure: honest message + durable trip line

systemMessage: "...the block budget is exhausted, but the stand-down could not be persisted and may recur on later turn ends. Run /stow and hand over..." trips: stage=ceiling total=210000 ... stage=standdown-unrecorded total=210000 ... Claims false durability ('now stands down for the rest of the session')? NO (correct)

=== SCENARIO A: block budget exhausted, stand-down write cannot persist (mode 400) ===
--- systemMessage the captain actually sees (stdout) ---
firstmate context budget: this session measures 210000 tokens, over the 180000 ceiling, and the block budget is exhausted, but the stand-down could not be persisted and may recur on later turn ends. Run /stow and hand over; do not stall the fleet waiting on it.

--- durable trip record (.context-budget-trips) ---
2026-08-17T12:18:15Z stage=ceiling total=210000 ceiling=180000 advisory=150000 enforce=1 session=sessA
2026-08-17T12:18:15Z stage=standdown-unrecorded total=210000 ceiling=180000 advisory=150000 enforce=1 session=sessA

Claims false durability ('now stands down for the rest of the session')?
NO (correct)
Evidence: Scenario B - unwritable notice file: one ceiling + one notice-unwritable line over 5 turn ends, fallback marker created

trips after 5 turn ends: stage=notice-unwritable total=210000 ... enforce=0 session=sessB stage=ceiling total=210000 ... enforce=0 session=sessB ceiling trip lines: 1 notice-unwritable trip lines: 1 fallback marker .context-budget-notice-degraded-sessB: PRESENT

=== SCENARIO B: notice dedup file unwritable, 5 turn ends over ONE ceiling crossing (warning-only default) ===
--- durable trip record after 5 turn ends ---
2026-08-17T12:18:15Z stage=notice-unwritable total=210000 ceiling=180000 advisory=150000 enforce=0 session=sessB
2026-08-17T12:18:15Z stage=ceiling total=210000 ceiling=180000 advisory=150000 enforce=0 session=sessB

ceiling trip lines: 1
notice-unwritable trip lines: 1
fallback marker present?
-rw-r--r--@ 1 uayyagari  staff  97 Aug 17 17:48 /var/folders/vr/_90lw3_s0bs_nw2f7b6t9yrm0000gn/T//fm-context-budget-evid.iGWNgS/B/state/.context-budget-notice-degraded-sessB
PRESENT

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

✅ **Review** - passed

✅ No issues found.

✅ **Test** - passed

✅ No issues found.

  • bash tests/fm-context-budget.test.sh (75 ok, exit 0; includes the two new tests, both ran real assertions, not the mode-400 skip branch)
  • Reproduced Scenario A: enforcing hook with an unwritable block record (mode 400) at the exhausted block budget - captured stdout systemMessage and .context-budget-trips
  • Reproduced Scenario B: warning-only hook run 5 times over one ceiling crossing with an unwritable notice dedup file - counted ceiling vs notice-unwritable trip lines and checked the fallback marker
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

…ing them

The block-budget-exhausted stand-down write ignored its own return status,
so a failed write left the session-facing message falsely claiming the
guard "now stands down for the rest of the session" on every later turn
end, with no durable trace anywhere. It now checks the write, corrects
the message when it fails, and records the failure once under
stage=standdown-unrecorded, mirroring the existing ceiling-unrecorded
degrade.

The per-episode notice dedup record swallowed its own write failure. If
the notice file specifically became unwritable while the rest of state/
stayed writable, stage_enter never deduped again and retraced every
threshold crossing on every turn end, reopening the trip-file inflation
commit a9fb47f removed. A fallback marker file now carries the same dedup
fields when the primary notice file cannot be written, restoring
once-per-crossing behavior without ever reading a decision back out of
the write-only trip record. The fallback write itself is traced once
under stage=notice-unwritable.

Both new cases are covered in tests/fm-context-budget.test.sh and
documented in docs/context-budget.md.
@uayyagari
uayyagari merged commit 8812b86 into main Aug 19, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant