From 3d342df3349ffb8d155f22653b7eac707ec9b21a Mon Sep 17 00:00:00 2001 From: Pesres Kabo <2389400626lh@gmail.com> Date: Sat, 5 Dec 2026 10:35:01 +0800 Subject: [PATCH] fix(copilot): avoid duplicate residual for cumulative shutdown totals --- src/parser.ts | 23 +++++++++++++++++++++++ tests/parser.test.ts | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/src/parser.ts b/src/parser.ts index e2de25d26..8b2731a6c 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -4899,6 +4899,7 @@ export async function parseProviderSources( for (let legIdx = 0; legIdx < coalesced.length; legIdx++) { const leg = coalesced[legIdx]! const covered = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, reasoning: 0 } + const coveredAll = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, reasoning: 0 } // An in-session compaction RESETS the CLI's rollup counters, so a leg // containing one describes only its post-compaction requests. Starting // its interval at the previous leg would subtract the whole @@ -4909,8 +4910,16 @@ export async function parseProviderSources( // Anchor at the compaction instead: pre-compaction rows still SERVE, // they just stop cancelling usage the rollup never claimed. const intervalStart = Math.max(prevLegTs, leg.compactedAtMs) + const hasCompactionInLeg = leg.compactedAtMs > prevLegTs while (rowIdx < rows.length && rows[rowIdx]!.ts <= leg.ts) { const row = rows[rowIdx]! + if (row.ts > prevLegTs) { + coveredAll.input += row.input + coveredAll.output += row.output + coveredAll.cacheRead += row.cacheRead + coveredAll.cacheWrite += row.cacheWrite + coveredAll.reasoning += row.reasoning + } // A labelled compaction row is the summarization request itself. It // commits just BEFORE the compaction stamp, so the anchor would push // it outside the interval while the post-reset rollup still counts @@ -4928,6 +4937,20 @@ export async function parseProviderSources( rowIdx++ } prevLegTs = leg.ts + // Some Copilot hosts emit cumulative session.shutdown totals covering + // the whole leg (including pre-compaction calls) rather than resetting + // at compaction. When all five token counters of every per-call row in + // (prevLegTs, leg.ts] match the shutdown leg exactly, the leg is already + // completely accounted for on its original call dates and must not emit + // a duplicate residual at shutdown. + if ( + hasCompactionInLeg && + coveredAll.input === leg.input && + coveredAll.output === leg.output && + coveredAll.cacheRead === leg.cacheRead && + coveredAll.cacheWrite === leg.cacheWrite && + coveredAll.reasoning === leg.reasoning + ) continue const input = Math.max(0, leg.input - covered.input) const output = Math.max(0, leg.output - covered.output) const cacheRead = Math.max(0, leg.cacheRead - covered.cacheRead) diff --git a/tests/parser.test.ts b/tests/parser.test.ts index 67e45fefe..5c842cf53 100644 --- a/tests/parser.test.ts +++ b/tests/parser.test.ts @@ -2023,6 +2023,47 @@ describe.skipIf(!isSqliteAvailable())('(c5) compaction-initiated store rows', () expect(storeCalls.reduce((s, c) => s + c.usage.inputTokens, 0)).toBe((272139 - 267005) + 6272) expect(totalOutput(projects)).toBe(400 + 3085) }) + + it('does not emit a duplicate shutdown residual when cumulative totals cover pre- and post-compaction rows (#1968)', async () => { + const { sessionStateDir, dbPath } = await setup('cumulative-shutdown-1968') + const dir = join(sessionStateDir, 'sess-cum') + await mkdir(dir, { recursive: true }) + await writeFile(join(dir, 'workspace.yaml'), 'id: sess-cum\ncwd: /home/user/testproj\n') + + const day1First = '2026-12-01T10:00:00.000Z' + const day1Compact = '2026-12-01T10:01:00.000Z' + const day1Last = '2026-12-01T10:02:00.000Z' + const day2Shutdown = '2026-12-02T10:00:00.000Z' + + await writeFile(join(dir, 'events.jsonl'), [ + JSON.stringify({ type: 'session.start', timestamp: day1First, data: { selectedModel: 'claude-sonnet-4-5' } }), + JSON.stringify({ type: 'session.compaction_complete', timestamp: day1Compact, data: { success: true } }), + JSON.stringify({ + type: 'session.shutdown', + timestamp: day2Shutdown, + data: { + shutdownType: 'routine', + modelMetrics: { + 'claude-sonnet-4-5': { + requests: { count: 2, cost: 2 }, + usage: { inputTokens: 110_000, outputTokens: 1_100, cacheReadTokens: 0, cacheWriteTokens: 0, reasoningTokens: 0 }, + }, + }, + }, + }), + ].join('\n') + '\n') + + insertLabelled(dbPath, 'sess-cum', 100_000, day1First, 'user', { output: 1_000 }) + insertLabelled(dbPath, 'sess-cum', 10_000, day1Last, 'user', { output: 100 }) + + const day1 = await parseAllSessions({ start: new Date('2026-12-01T00:00:00.000Z'), end: new Date('2026-12-01T23:59:59.999Z') }, 'copilot') + const day2 = await parseAllSessions({ start: new Date('2026-12-02T00:00:00.000Z'), end: new Date('2026-12-02T23:59:59.999Z') }, 'copilot') + + expect(totalInput(day1)).toBe(110_000) + expect(totalOutput(day1)).toBe(1_100) + expect(totalInput(day2)).toBe(0) + expect(totalOutput(day2)).toBe(0) + }) }) // ═══════════════════════════════════════════════════════════════════════════