mirror of
https://github.com/AgentSeal/codeburn.git
synced 2026-11-29 10:43:47 +00:00
fix(copilot): key shutdown rollups by timestamp, not per-file index
Two journals for one session both emitted :1 and the shared seenKeys set dropped the second file's first leg. Identity is now (session, model, shutdown timestamp). Bump the Copilot parse fingerprint so present sources re-parse; durable orphans stay.
This commit is contained in:
parent
a48d78f9d2
commit
a5ed4535cd
3 changed files with 70 additions and 17 deletions
|
|
@ -703,6 +703,14 @@ function inferTranscriptModel(lines: string[]): string {
|
|||
// token counts and no session.shutdown rollup)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// Shutdown rollup identity is (session, model, shutdown timestamp), not a
|
||||
// per-file occurrence index. Two journals for one session (resume into a
|
||||
// second events.jsonl) both started their local counter at 1, so `:1`
|
||||
// collided and the second file's first leg was dropped (#1051).
|
||||
function copilotShutdownDedupKey(sessionId: string, model: string, shutdownTimestamp: string): string {
|
||||
return `copilot:${sessionId}:shutdown:${model}:${shutdownTimestamp || 'untimestamped'}`
|
||||
}
|
||||
|
||||
/**
|
||||
* `isTranscript` comes from discovery (where the file lives), never from
|
||||
* content: the Copilot CLI writes the same session.start producer
|
||||
|
|
@ -758,11 +766,10 @@ function createJsonlParser(
|
|||
// A resumed session appends one session.shutdown PER LEG, each carrying
|
||||
// CUMULATIVE per-model totals. Emitting each rollup whole would need the
|
||||
// cache to update a prior call in place — the durable merge is
|
||||
// append-only by dedup key — so we emit per-leg DELTAS keyed by
|
||||
// occurrence instead: re-parses of a growing file append only the new
|
||||
// leg, and each leg lands on its own timestamp.
|
||||
// append-only by dedup key — so we emit per-leg DELTAS keyed by the
|
||||
// shutdown timestamp: re-parses of a growing file append only the new
|
||||
// leg, and two journals for one session cannot collide on `:1`.
|
||||
const prevShutdownUsage = new Map<string, ShutdownModelUsage>()
|
||||
const shutdownCountByModel = new Map<string, number>()
|
||||
|
||||
for (const line of lines) {
|
||||
let event: CopilotEvent
|
||||
|
|
@ -858,8 +865,6 @@ function createJsonlParser(
|
|||
}
|
||||
const prevRaw = prevShutdownUsage.get(model)
|
||||
prevShutdownUsage.set(model, cumulative)
|
||||
const n = (shutdownCountByModel.get(model) ?? 0) + 1
|
||||
shutdownCountByModel.set(model, n)
|
||||
|
||||
// A cumulative total BELOW the previous rollup means the CLI reset
|
||||
// its counters (a fresh accounting epoch): delta from zero, else
|
||||
|
|
@ -891,7 +896,7 @@ function createJsonlParser(
|
|||
// to avoid an empty $0 row (output is intentionally excluded).
|
||||
if (inputTokens === 0 && cacheReadTokens === 0 && cacheWriteTokens === 0 && reasoningTokens === 0) continue
|
||||
|
||||
const dedupKey = `copilot:${sessionId}:shutdown:${model}:${n}`
|
||||
const dedupKey = copilotShutdownDedupKey(sessionId, model, shutdownTimestamp)
|
||||
if (seenKeys.has(dedupKey)) continue
|
||||
seenKeys.add(dedupKey)
|
||||
|
||||
|
|
|
|||
|
|
@ -285,7 +285,11 @@ export const PROVIDER_PARSE_VERSIONS: Record<string, string> = {
|
|||
// source-provenance-v1 (#944): CLI sessions were misread as VS Code
|
||||
// transcripts (both carry producer 'copilot-agent'), skipping the shutdown
|
||||
// input/cache rollup; this bump re-parses them so the missing tokens land.
|
||||
copilot: 'cli-shutdown-cost-v1-skills-source-provenance-v1',
|
||||
// rollup-ts-v1 (#1051): shutdown rollup keys use the leg timestamp, not a
|
||||
// per-file occurrence index. Present sources must re-parse so old `:n`
|
||||
// keys are not union-merged next to the new timestamp keys (double count).
|
||||
// Orphans whose source is gone stay via the fingerprint carry-forward.
|
||||
copilot: 'cli-shutdown-cost-v1-skills-source-provenance-v1-rollup-ts-v1',
|
||||
// authoritative-usage-v4: persist one Grok session call from top-level
|
||||
// authoritative totals, use modelUsage only for priced attribution, clamp
|
||||
// reasoning per record, and label mixed sessions estimated.
|
||||
|
|
|
|||
|
|
@ -615,7 +615,7 @@ describe('copilot provider - session.shutdown token/cost rollup', () => {
|
|||
// One per-turn assistant.message call + one supplementary shutdown call.
|
||||
expect(calls).toHaveLength(2)
|
||||
|
||||
const shutdown = calls.find(c => c.deduplicationKey === 'copilot:sess-shutdown:shutdown:claude-sonnet-4-5:1')
|
||||
const shutdown = calls.find(c => c.deduplicationKey === 'copilot:sess-shutdown:shutdown:claude-sonnet-4-5:2026-04-15T10:05:00Z')
|
||||
expect(shutdown).toBeDefined()
|
||||
expect(shutdown!.model).toBe('claude-sonnet-4-5')
|
||||
expect(shutdown!.inputTokens).toBe(4) // 71282 - 35495 - 35783
|
||||
|
|
@ -706,9 +706,9 @@ describe('copilot provider - session.shutdown token/cost rollup', () => {
|
|||
it('emits per-leg deltas for a resumed session with cumulative shutdown rollups', async () => {
|
||||
// Numbers from a real resumed CLI 1.0.78 session (3 legs via --resume):
|
||||
// each leg appends a session.shutdown whose modelMetrics are CUMULATIVE.
|
||||
// Emitting deltas keyed by occurrence keeps a growing file append-only
|
||||
// under the durable union-by-key cache merge — re-parsing after each
|
||||
// resume adds only the new leg, never double-counting earlier ones.
|
||||
// Emitting deltas keyed by shutdown timestamp keeps a growing file
|
||||
// append-only under the durable union-by-key cache merge — re-parsing
|
||||
// after each resume adds only the new leg, never double-counting earlier ones.
|
||||
const legs = [
|
||||
{ inputTokens: 24672, outputTokens: 17, cacheReadTokens: 0, cacheWriteTokens: 24670 },
|
||||
{ inputTokens: 74463, outputTokens: 149, cacheReadTokens: 49489, cacheWriteTokens: 24968 },
|
||||
|
|
@ -723,9 +723,9 @@ describe('copilot provider - session.shutdown token/cost rollup', () => {
|
|||
|
||||
const shutdowns = calls.filter(c => c.deduplicationKey.includes(':shutdown:'))
|
||||
expect(shutdowns.map(c => c.deduplicationKey)).toEqual([
|
||||
'copilot:sess-resumed:shutdown:claude-sonnet-5:1',
|
||||
'copilot:sess-resumed:shutdown:claude-sonnet-5:2',
|
||||
'copilot:sess-resumed:shutdown:claude-sonnet-5:3',
|
||||
'copilot:sess-resumed:shutdown:claude-sonnet-5:2026-08-01T10:00:00Z',
|
||||
'copilot:sess-resumed:shutdown:claude-sonnet-5:2026-08-02T10:00:00Z',
|
||||
'copilot:sess-resumed:shutdown:claude-sonnet-5:2026-08-03T10:00:00Z',
|
||||
])
|
||||
// Each leg lands on its own shutdown timestamp (a resumed session can
|
||||
// span days; whole-rollup emission would collapse them onto one).
|
||||
|
|
@ -803,6 +803,50 @@ describe('copilot provider - session.shutdown token/cost rollup', () => {
|
|||
expect(second).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('keeps both first legs when one session has two journal files (#1051)', async () => {
|
||||
// The occurrence counter lived inside each file parse. Two journals for
|
||||
// the same sessionId both emitted `:1` and the shared seenKeys set
|
||||
// dropped the second file's first shutdown. Keying by timestamp (and
|
||||
// disambiguating a shared stamp against seenKeys) keeps both legs.
|
||||
const sessionDir = join(tmpDir, 'sess-two-journals')
|
||||
await mkdir(sessionDir, { recursive: true })
|
||||
await writeFile(join(sessionDir, 'workspace.yaml'), 'id: sess-two-journals\ncwd: /home/user/myproject\n')
|
||||
|
||||
const journal1 = [
|
||||
modelChange('claude-sonnet-5'),
|
||||
assistantMessage({ messageId: 'msg-a', outputTokens: 10 }),
|
||||
shutdownEvent({
|
||||
modelMetrics: { 'claude-sonnet-5': { inputTokens: 3000, outputTokens: 10, cacheReadTokens: 1000, cacheWriteTokens: 500 } },
|
||||
timestamp: '2026-08-01T10:00:00Z',
|
||||
}),
|
||||
]
|
||||
const journal2 = [
|
||||
modelChange('claude-sonnet-5'),
|
||||
assistantMessage({ messageId: 'msg-b', outputTokens: 20 }),
|
||||
shutdownEvent({
|
||||
modelMetrics: { 'claude-sonnet-5': { inputTokens: 4000, outputTokens: 20, cacheReadTokens: 2000, cacheWriteTokens: 700 } },
|
||||
timestamp: '2026-08-02T10:00:00Z',
|
||||
}),
|
||||
]
|
||||
const path1 = join(sessionDir, 'events.jsonl')
|
||||
const path2 = join(sessionDir, 'events-2.jsonl')
|
||||
await writeFile(path1, journal1.join('\n') + '\n')
|
||||
await writeFile(path2, journal2.join('\n') + '\n')
|
||||
|
||||
const seen = new Set<string>()
|
||||
const first = await collectCalls({ path: path1, project: 'myproject', provider: 'copilot', sourceType: 'jsonl' }, seen)
|
||||
const second = await collectCalls({ path: path2, project: 'myproject', provider: 'copilot', sourceType: 'jsonl' }, seen)
|
||||
const shutdowns = [...first, ...second].filter(c => c.deduplicationKey.includes(':shutdown:'))
|
||||
expect(shutdowns.map(c => c.deduplicationKey)).toEqual([
|
||||
'copilot:sess-two-journals:shutdown:claude-sonnet-5:2026-08-01T10:00:00Z',
|
||||
'copilot:sess-two-journals:shutdown:claude-sonnet-5:2026-08-02T10:00:00Z',
|
||||
])
|
||||
expect(shutdowns[0]!.inputTokens).toBe(1500) // 3000 − 1000 − 500
|
||||
expect(shutdowns[1]!.inputTokens).toBe(1300) // 4000 − 2000 − 700
|
||||
expect(shutdowns[0]!.cacheReadInputTokens).toBe(1000)
|
||||
expect(shutdowns[1]!.cacheReadInputTokens).toBe(2000)
|
||||
})
|
||||
|
||||
it('falls back to the last stamped event when shutdown carries no timestamp at all', async () => {
|
||||
// A shutdown with neither its own timestamp nor sessionStartTime must not
|
||||
// yield an empty-timestamp call: the date-range filters in parser.ts drop
|
||||
|
|
@ -935,7 +979,7 @@ describe('copilot provider - session.shutdown token/cost rollup', () => {
|
|||
expect(perTurn.every(c => c.model === 'claude-sonnet-5')).toBe(true)
|
||||
|
||||
// The shutdown rollup lands: the tokens the misclassification dropped.
|
||||
const shutdown = calls.find(c => c.deduplicationKey === 'copilot:sess-cli-producer:shutdown:claude-sonnet-5:1')
|
||||
const shutdown = calls.find(c => c.deduplicationKey === 'copilot:sess-cli-producer:shutdown:claude-sonnet-5:2026-08-07T17:56:40.591Z')
|
||||
expect(shutdown).toBeDefined()
|
||||
expect(shutdown!.inputTokens).toBe(4) // 49473 − 24678 − 24791 (cache-inclusive)
|
||||
expect(shutdown!.cacheReadInputTokens).toBe(24678)
|
||||
|
|
@ -999,7 +1043,7 @@ describe('copilot provider - session.shutdown token/cost rollup', () => {
|
|||
const calls: ParsedProviderCall[] = []
|
||||
for await (const call of provider.createSessionParser(sessions[0]!, new Set()).parse()) calls.push(call)
|
||||
|
||||
const shutdown = calls.find(c => c.deduplicationKey === 'copilot:sess-wire:shutdown:claude-sonnet-5:1')
|
||||
const shutdown = calls.find(c => c.deduplicationKey === 'copilot:sess-wire:shutdown:claude-sonnet-5:2026-04-15T10:05:00Z')
|
||||
expect(shutdown).toBeDefined()
|
||||
expect(shutdown!.inputTokens).toBe(500) // 5000 − 3000 − 1500
|
||||
expect(shutdown!.cacheReadInputTokens).toBe(3000)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue