Repository navigation
fix(bin): exclude synthetic zero-usage entries from context measurement - #9
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
((.message.usage | usage_total) > 0)clause to thejq select()candidate list infm_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 false0/exit 0. The function now recovers the real prior measurement instead, keepingbin/fm-session-pulse.shfrom wiping a genuinely duestate/.handover-duemarker.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; extendedtests/fm-session-handover.test.shwith a regression test that the false zero no longer wipes the handover marker.docs/session-handover.md(rule count 3 → 4) anddocs/verification/context-budget.mdto 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=0Evidence: Transcript fixture used for the repro (trailing synthetic zero-usage entry)
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 regressionfm-session-pulse: a trailing synthetic zero-usage entry does not wipe a due handover)Manual repro: sourcedfm_context_measure_transcriptfrom 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.