Skip to content

fix(bin): exclude synthetic zero-usage entries from context measurement - #9

Merged
uayyagari merged 2 commits into
mainfrom
fix/context-measure-false-zero
Aug 19, 2026
Merged

uayyagari merged 2 commits into
mainfrom
fix/context-measure-false-zero

Conversation

@uayyagari

Copy link
Copy Markdown

Intent

Fix a live defect where fm_context_measure_transcript (bin/fm-context-measure-lib.sh) could return a false 0 with exit 0 when a transcript's last countable assistant entry is the synthetic all-zero-usage entry Claude Code writes whenever a turn ends abnormally. That false zero let bin/fm-session-pulse.sh silently wipe the state/.handover-due marker even when a handover was genuinely due moments earlier from a real over-threshold measurement.

Scope is deliberately narrow and interim: teach fm_context_measure_transcript to exclude a zero-usage-total candidate before its existing last-entry/dedupe-by-requestId step, matching rule 3 already present in bin/fm-context-budget.sh's own inline measure_context implementation. The dedupe-by-requestId approach and the function's overall return contract (non-zero/absent output = unmeasurable) are intentionally left unchanged. bin/fm-context-budget.sh itself was deliberately NOT touched - it already has this rule and is explicitly out of scope; a separate, bigger backlog item (fm-context-measure-lib-adoption) will later move the guard's full streaming implementation into the lib.

Concretely: added an 'and ((.message.usage | usage_total) > 0)' clause to the jq select() that builds the initial candidate list in fm_context_measure_transcript, so a trailing synthetic zero-usage entry can never become the chosen 'last' candidate. This recovers the real prior measurement (e.g. 300010) rather than merely degrading to 'unmeasurable' - the stronger, preferred outcome, though the brief also would have accepted the weaker outcome.

Added tests/fm-context-measure-lib.test.sh (new file, 8 test cases) proving: the false-zero fixture no longer returns 0/exit 0 and now recovers the real total; a genuinely empty or all-zero-only transcript still reports unmeasurable; ordinary non-zero measurement, dedupe-by-requestId across a multi-block turn, take-last-never-max across compaction, and sidechain exclusion are all unchanged. Also extended the existing tests/fm-session-handover.test.sh with a regression test proving bin/fm-session-pulse.sh's handover-due marker is no longer wiped by this false zero.

Updated docs/session-handover.md (rule count 3 -> 4, documenting the new synthetic-zero-exclusion rule) and docs/verification/context-budget.md (marked the previously-tracked rule-3 gap between the guard and the lib as now fixed, replacing the stale 4-row gap table with a 3-row table of the remaining out-of-scope gaps tracked under fm-context-measure-lib-adoption). docs/context-budget.md and docs/scripts.md were reviewed and intentionally left unchanged since they document the guard's own contract or are only a one-line pointer, not this function's contract.

All new/changed tests pass (tests/fm-context-measure-lib.test.sh: 8/8, tests/fm-session-handover.test.sh: 30/30 including the new regression test), the full existing tests/fm-context-budget.test.sh regression suite still passes (73/73, confirming no behavior change to the untouched guard), and bin/fm-lint.sh (shellcheck) is clean across the canonical file set.

What Changed

  • Added an ((.message.usage | usage_total) > 0) clause to the jq select() candidate list in fm_context_measure_transcript (bin/fm-context-measure-lib.sh), so a trailing synthetic all-zero-usage entry can no longer become the chosen "last" candidate and return a false 0/exit 0. The function now recovers the real prior measurement instead, keeping bin/fm-session-pulse.sh from wiping a genuinely due state/.handover-due marker.
  • Added tests/fm-context-measure-lib.test.sh (8 cases) covering the false-zero fixture, empty/all-zero unmeasurable cases, ordinary measurement, dedupe-by-requestId, take-last-never-max, and sidechain exclusion; extended tests/fm-session-handover.test.sh with a regression test that the false zero no longer wipes the handover marker.
  • Updated docs/session-handover.md (rule count 3 → 4) and docs/verification/context-budget.md to mark the guard/lib rule-3 gap as fixed and trim the gap table to the remaining out-of-scope items.

Risk Assessment

✅ Low: A single well-bounded jq filter clause excludes synthetic zero-usage candidates before the unchanged last/dedupe step, matching the guard's existing rule and fully covered by new and regression tests, with no reachable path left where the false zero recurs.

Testing

Ran the two targeted suites this change touches (tests/fm-context-measure-lib.test.sh 8/8 and tests/fm-session-handover.test.sh 30/30, both including the new false-zero cases) and produced a CLI before/after transcript exercising the real fm_context_measure_transcript function: the base commit emits a false 0 with exit 0 while the fixed commit recovers the genuine prior total (300010), directly demonstrating the defect and its fix as an end user's session-pulse path would experience it. Did not run the full fm-context-budget.test.sh regression suite per the targeted-validation boundary; that broad regression is left to CI. This is a shell/CLI change with no user-facing rendered surface, so no screenshot applies.

Evidence: False-zero before/after repro (real function, base vs target commit)

BASELINE (a6a923f): output=0 exit=0 FIXED (bd90f62): output=300010 exit=0

=== fm_context_measure_transcript on a transcript whose LAST assistant entry is a synthetic all-zero-usage entry ===
(the prior real entry measures 300010 tokens; the trailing entry is Claude Code's abnormal-turn-end synthetic zero)

BASELINE  (base commit a6a923f, before fix):
  output=0 exit=0

FIXED     (target commit bd90f62, after fix):
  output=300010 exit=0
Evidence: Transcript fixture used for the repro (trailing synthetic zero-usage entry)
{"type":"assistant","requestId":"req-real","message":{"usage":{"input_tokens":10,"cache_creation_input_tokens":5000,"cache_read_input_tokens":295000,"output_tokens":0}}}
{"type":"assistant","requestId":"req-abnormal","message":{"usage":{"input_tokens":0,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":0}}}

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-measure-lib.test.sh (8/8 pass: false-zero no longer returns 0, recovers real total, empty/all-zero stays unmeasurable, dedupe-by-requestId, last-never-max, sidechain exclusion, missing transcript)
  • bash tests/fm-session-handover.test.sh (30/30 pass, including regression fm-session-pulse: a trailing synthetic zero-usage entry does not wipe a due handover)
  • Manual repro: sourced fm_context_measure_transcript from base (a6a923f) and target (bd90f62) commits against a crafted transcript ending in a synthetic all-zero entry - base=0/exit0, target=300010/exit0
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

fm_context_measure_transcript could return a false 0/exit 0 when a
transcript's last countable assistant entry was the synthetic
all-zero-usage entry Claude Code writes on an abnormal turn end. That
false 0 let fm-session-pulse.sh silently wipe a handover already due.

Exclude zero-total candidates before the last/dedupe step, matching
fm-context-budget.sh's own rule 3, so a trailing synthetic entry can
no longer mask the last real measurement.
@uayyagari
uayyagari merged commit d5c3a21 into main Aug 19, 2026
10 checks passed
@uayyagari
uayyagari deleted the fix/context-measure-false-zero branch September 26, 2026 15:36
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