Skip to content

fix(context): savings accumulator counts footers, not footers quoted as prose - #853

Draft
hr1juldey wants to merge 1 commit into
trailhq:mainfrom
hr1juldey:fix/624-savings-footer-accounting
Draft

hr1juldey wants to merge 1 commit into
trailhq:mainfrom
hr1juldey:fix/624-savings-footer-accounting

Conversation

@hr1juldey

Copy link
Copy Markdown

Fixes #624 (part 2; part 1 is already fixed on main by #338/#345 — the writer is pinned to toLocaleString('en-US'), so footers never carry decimal commas anymore).

Problem

sumSavingsFooters matched the footer pattern anywhere in the blob the PostToolUse accumulator sweeps (JSON.stringify(tool_response)). Any tool output that merely quotes a footer line — a document that discusses graft, a task description containing "[graft] tokens saved ≈ …" — was added to the session total (the report's session jumped +13,509 from reading such a file).

Fix

The reader keys on where the footer can legitimately sit, via a shared, documented SAVINGS_FOOTER_RE constant:

  • start of raw output (^), after a newline, glued after content (callers …[graft] …);
  • right after a JSON field's opening quote (:"[graft] …") — how a stringified payload renders a string field;
  • right after the opening quote of a stringified bare-string payload ("[graft] …" — payload() unwraps tool_response, so a string result serialises with no field name in front).

It skips a footer preceded by any other quote or a backslash — exactly the prose-quotation shapes (wrote "[graft] …", \"[graft] …" inside stringified payloads). Since the writer is locale-pinned, the reader keeps accepting only comma groups.

Tests

In test/session-metrics.test.ts: the report's exact case (raw and stringified) returns 0; field-boundary, escaped-newline, bare-string, glued-after-content, and multi-footer (both brands) shapes still count. Full suite: 4550 tests, only the six failures already present on clean main (49d36b7) — unchanged and pre-dating this branch.

Note

A document that prints the footer at a LINE START (e.g. a quoted block on its own line) is still indistinguishable from real tool output — the same limit the report's local patch had. Flagging it here rather than over-fitting the regex.

…as prose

The PostToolUse accumulator sweeps JSON.stringify(tool_response) for the
savings footer, and the reader matched the pattern anywhere — a document
that merely mentioned "[graft] tokens saved ≈ …" added its number to the
session total (one session jumped +13,509 from reading such a file). The
reader now keys on where the footer can legitimately sit: start of
output (a stringified payload's opening quote included — payload()
unwraps tool_response, so a string result serialises with no field name
before it), after a newline, or right after a JSON field's `:"` — and
skips a footer preceded by a quote or backslash that is not one of
those. The locale-truncation half of the report is already fixed on
main (trailhq#338 pinned the writer to en-US), so the reader keeps accepting
only comma groups.

Fixes trailhq#624
@trailhq-graft

trailhq-graft Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

🌱 graft blast radius

1 area changed → 1 area can be affected. 4 dependent symbols, depth 2.
Tests: Savings Aggregation has tests the diff did not touch.
Tag: @anirudhkumar-nanonets — Savings Aggregation, Session Metrics · @shhdwi — Session Metrics · Ercan Er — Savings Aggregation

flowchart TB
  A0(("Session Metrics<br/>4 symbols"))
  classDef reached fill:#D9EDF3,stroke:#3AA7C9,stroke-width:1.5px,color:#0E313C;
  class A0 reached;
Loading
Can be affected Symbols Nearest hop Reached from
Session Metrics 4 src/claude/session-metrics.ts:L158-L161 addHookSavings — calls, depth 1 Savings Aggregation
Who knows this code — 3 people across 2 areas
Area Who knows it
Savings Aggregation · changed @anirudhkumar-nanonets — 5 commits, last 1mo ago · Ercan Er — 1 commit, last 9d ago
Session Metrics · affected @anirudhkumar-nanonets — 23 commits, last 10d ago · @shhdwi — 14 commits, last 22d ago

Ownership is git history over each area's own files, weighted towards recent work (120-day half-life). Merge commits and bots are dropped, and you are dropped from your own PR. A name with no @ has no GitHub handle in its commit email — tag them by hand, or add a .mailmap entry. A suggestion from history, not a CODEOWNERS rule.

All 4 dependent symbols, grouped by area

Session Metrics — 4 symbols in 2 files

  • src/claude/session-metrics.ts:L158-L161 — addHookSavings (calls, depth 1)
    159: const saved = sumSavingsFooters(text);
  • src/claude/session-metrics.ts:L105-L107 — parseSavings (calls, depth 1)
    106: return sumSavingsFooters(blob);
  • src/claude/hooks.ts:L276-L286 — classifyAndScore (calls, depth 2)
  • src/claude/hooks.ts:L311-L316 — handleCursorMcp (calls, depth 2)
Test signal per changed area — 1 ⚠

Reached = a node under a test path has a resolved edge into the changed symbol. It undercounts anything called indirectly — through a CLI, a spawned process or a dynamic import — so read a low ratio as “look here”, never as a coverage gate.

  • ⚠ Savings Aggregation — 1 of 1 reached · 3 test files reach it, none changed here
3 test suites also reference this code

4 symbols, kept out of the diagram and the table so they cannot crowd out the areas a reviewer has to look at.

  • test/hook-savings.test.ts
  • test/savings-locale.test.ts
  • test/trail-name.test.ts

graft blast · origin/main...HEAD · depth 2 · 2 changed files

Open the interactive graph → — click an area to see its dependent symbols at file:line.

github-actions Bot added a commit that referenced this pull request Nov 18, 2026
@hr1juldey
hr1juldey marked this pull request as draft November 18, 2026 15:58

This branch has not been deployed

No deployments
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.

sumSavingsFooters: locale thousands separators truncate savings (13.509 -> 13) and quoted footers inflate session totals

1 participant