Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/context/savings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,33 @@ export function savingsLine(body: string, saved: Savings | undefined): string {
);
}

/**
* The savings footer, as the reader half of {@link savingsLine} sees it. The
* footer may sit anywhere real output puts it: at the start of raw output
* (`^`), glued after content (`callers …[graft] …`), after a newline, right
* after a JSON field's opening quote (`:"[graft] …"`, how a JSON-stringified
* payload renders a string field), or right after the OPENING quote of a
* stringified bare-string payload (`"[graft] …"` — the hooks unwrap
* `tool_response`, so a string result serialises with no field name before
* it). What must NOT count is a footer quoted as data: a prose quotation is
* preceded by a quote that is NOT the payload's opening one — mid-text after
* a word (`wrote "[graft] …`) or escaped (`\"[graft] …` inside a stringified
* payload) — so the quote alternatives above only fire at text start or after
* a JSON field colon. Real-writer footers are locale-pinned (`groupDigits`),
* so only comma groups. `/g`: a blob can carry several; `matchAll` clones the
* regex, so the shared constant carries no index state between calls.
*/
const SAVINGS_FOOTER_RE = /(?:^"?|(?<=[^"\\])|(?<=:"))\[(?:graft|trail)\] (?:tokens|learnings) saved ≈ ([\d,]+)/g;

/**
* Sum every `[graft] tokens saved ≈ N` footer in a blob of text — the reader
* half of {@link savingsLine}, kept next to the writer so the two never drift.
* A single blob can carry several (an agent that made two graft calls in one
* turn); the nudge from `savingsTurnNudge` deliberately omits the pattern, so its example
* text is not miscounted here.
* See {@link SAVINGS_FOOTER_RE} for what counts and why.
*/
export function sumSavingsFooters(text: string): number {
let total = 0;
// The code a query saved reading, and the digging a reused learning saved.
for (const m of text.matchAll(/\[(?:graft|trail)\] (?:tokens|learnings) saved ≈ ([\d,]+)/g)) {
for (const m of text.matchAll(SAVINGS_FOOTER_RE)) {
total += Number(m[1].replace(/,/g, '')) || 0;
}
return total;
Expand Down
36 changes: 36 additions & 0 deletions test/session-metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,42 @@ test('parseSavings sums every footer, tolerant of commas', () => {
);
});

// Issue #624 part 2: a footer quoted inside prose is data, not savings. The
// PostToolUse accumulator sweeps JSON.stringify(tool_response), so a document
// that merely mentions the footer must not move the session total.
test('parseSavings ignores a footer quoted inside prose', () => {
assert.equal(parseSavings('he wrote "[graft] tokens saved ≈ 13,509" in the doc'), 0);
assert.equal(
parseSavings(JSON.stringify({ tool_response: { stdout: 'he wrote "[graft] tokens saved ≈ 13,509" in the doc' } })),
0,
);
});

test('parseSavings still counts footers at JSON field boundaries and after escaped newlines', () => {
assert.equal(
parseSavings(JSON.stringify({ tool_response: { stdout: '[graft] tokens saved ≈ 1,200 (92%) — …' } })),
1200,
);
// a bare-string tool_response: the hooks stringify the WHOLE payload, so the
// footer sits right after the field's opening quote (`:"`)
assert.equal(
parseSavings(JSON.stringify({ session_id: 's1', tool_response: '[graft] tokens saved ≈ 517 (88%) — …' })),
517,
);
// a real footer of the tally-test family: bare, ending right after the number
assert.equal(parseSavings(JSON.stringify({ session_id: 's1', tool_response: '[graft] tokens saved ≈ 4,000' })), 4000);
assert.equal(
parseSavings(JSON.stringify({ content: [{ type: 'text', text: 'intro\n[graft] tokens saved ≈ 300 (41%) — …' }] })),
300,
);
assert.equal(
parseSavings('[graft] tokens saved ≈ 500 (36%) — …\n[trail] learnings saved ≈ 200 tokens (estimate): …'),
700,
);
// glued right after output content, no separator (claude-hooks.test.ts' real-line shape)
assert.equal(parseSavings('callers …[graft] tokens saved ≈ 1,990 (88%) — …'), 1990);
});

// ── recordToolUse ──────────────────────────────────────────────────────────

test('recordToolUse increments the right counter and accumulates savings', () => {
Expand Down