fix(copilot): journal-scope shutdown keys without a fingerprint bump

Extra High HOLD on #1054: timestamp-only keys still collided across
journals, and the provider-wide rollup-ts-v1 bump dropped present
OTel sources, erasing conversations already pruned from the DB.
Identity is now (session, model, timestamp, journal basename).
Legacy :n keys migrate on that JSONL file only.
This commit is contained in:
Aditya Vikram Singh 2026-08-20 09:52:56 +05:30
parent a5ed4535cd
commit a3ea24859e
5 changed files with 177 additions and 25 deletions

View file

@ -2822,6 +2822,24 @@ function getOrCreateProviderSection(cache: SessionCache, provider: string): Prov
return section
}
function isLegacyCopilotShutdownKey(key: string, journalId: string): boolean {
return key.includes(':shutdown:') && !key.endsWith(`:${journalId}`)
}
// Drop occurrence-index / timestamp-only shutdown calls from one JSONL cache
// entry so the durable union does not keep `:n` next to the new journal-keyed
// calls (double count). OTel / JetBrains / transcript keys do not include
// `:shutdown:` and are left alone.
function stripLegacyCopilotShutdownCalls(entry: CachedFile, journalId: string): void {
const next: CachedTurn[] = []
for (const turn of entry.turns) {
const calls = turn.calls.filter(call => !isLegacyCopilotShutdownKey(call.deduplicationKey, journalId))
if (calls.length === 0) continue
next.push(calls.length === turn.calls.length ? turn : { ...turn, calls })
}
entry.turns = next
}
function cachedFileNeedsProviderReparse(providerName: string, sourcePath: string, cached: CachedFile): boolean {
// Antigravity data comes from the live server, not from the conversation file.
// A 0-turn cache entry may just mean the server was unavailable last run.
@ -2832,6 +2850,16 @@ function cachedFileNeedsProviderReparse(providerName: string, sourcePath: string
// title, model, and timestamp changes.
if (providerName === 'devin') return true
// #1051: migrate old Copilot CLI shutdown keys (`:n` or timestamp-only)
// without a provider-wide fingerprint bump. A bump would drop the present
// OTel DB source and erase conversations already pruned from that DB.
if (providerName === 'copilot') {
const journalId = basename(sourcePath)
return cached.turns.some(turn =>
turn.calls.some(call => isLegacyCopilotShutdownKey(call.deduplicationKey, journalId)),
)
}
if (providerName !== 'gemini') return false
return cached.turns.some(turn =>
@ -3208,6 +3236,9 @@ async function parseProviderSources(
if (provider.durableSources) {
const existingEntry = section.files[source.path]
if (existingEntry) {
if (providerName === 'copilot') {
stripLegacyCopilotShutdownCalls(existingEntry, basename(source.path))
}
const existingKeys = new Set(
existingEntry.turns.flatMap(t => t.calls.map(c => c.deduplicationKey))
)

View file

@ -703,12 +703,19 @@ 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'}`
// Shutdown rollup identity is (session, model, shutdown timestamp, journal
// basename), 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).
// Timestamp alone is not unique across journals: two files can share a stamp.
// The journal basename is re-parse-stable; do not mint suffixes from seenKeys.
function copilotShutdownDedupKey(
sessionId: string,
model: string,
shutdownTimestamp: string,
journalId: string,
): string {
return `copilot:${sessionId}:shutdown:${model}:${shutdownTimestamp || 'untimestamped'}:${journalId}`
}
/**
@ -767,8 +774,10 @@ function createJsonlParser(
// 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 the
// shutdown timestamp: re-parses of a growing file append only the new
// leg, and two journals for one session cannot collide on `:1`.
// shutdown timestamp plus this journal's basename: re-parses of a
// growing file append only the new leg, and two journals for one
// session cannot collide on `:1` or on a shared timestamp.
const journalId = basename(source.path)
const prevShutdownUsage = new Map<string, ShutdownModelUsage>()
for (const line of lines) {
@ -896,7 +905,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 = copilotShutdownDedupKey(sessionId, model, shutdownTimestamp)
const dedupKey = copilotShutdownDedupKey(sessionId, model, shutdownTimestamp, journalId)
if (seenKeys.has(dedupKey)) continue
seenKeys.add(dedupKey)

View file

@ -285,11 +285,12 @@ 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.
// 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',
// #1051 does NOT bump this again. A fingerprint change drops every present
// Copilot source (parser.ts getOrCreateProviderSection) and would erase
// conversations already pruned from a still-present OTel DB. Old `:n`
// shutdown keys migrate via cachedFileNeedsProviderReparse + a durable
// strip of legacy shutdown calls on that JSONL file only.
copilot: 'cli-shutdown-cost-v1-skills-source-provenance-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.