From 5a12dccc70aab8d62795e87234cde78c689a0c47 Mon Sep 17 00:00:00 2001 From: AgentSeal Date: Sun, 2 Aug 2026 00:35:30 +0200 Subject: [PATCH] fix(parser): classify range-sliced turns from the whole turn on the provider path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Codex/OTel/network provider path classified a date-sliced turn from only its in-range calls (cachedTurnToClassified(slicedTurn)), while the Claude path kept the full-turn classification. So a midnight-straddling turn was categorized and edit-counted differently by provider — a Read at 23:59 plus an Edit at 00:05, sliced to the 23:59 side, read as coding/hasEdits on Claude but exploration/no-edit on Codex. This contradicted the documented intent that category/hasEdits/retries are whole-exchange judgments, not per-call sums. Classify the full turn, then trim its calls to the range (mirroring the Claude path's classifiedTurnSlicedToRange). Cost/calls still come from the retained calls, so conservation is unchanged; only the turn-level judgments are now provider-independent. --- src/parser.ts | 18 ++++++++++-- tests/provider-turn-grouping.test.ts | 41 ++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/parser.ts b/src/parser.ts index 2b4f3f79..7adbea94 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -3054,7 +3054,15 @@ async function parseProviderSources( slicedTurn = sliced } - const classified = cachedTurnToClassified(slicedTurn) + // Classify the FULL turn, then keep only the in-range calls: category / + // hasEdits / retries are whole-exchange judgments, not per-call sums, so a + // midnight-straddling turn is classified identically to the Claude path + // (scanProjectDirs) rather than being re-derived from a partial slice. + // Cost/calls come from the retained calls, unchanged. + const classifiedFull = cachedTurnToClassified(turn) + const classified = dateRange + ? (classifiedTurnSlicedToRange(classifiedFull, dateRange) ?? classifiedFull) + : classifiedFull const project = slicedTurn.calls[0]?.project ?? source.project const key = `${providerName}:${turn.sessionId}:${project}` @@ -3103,7 +3111,13 @@ async function parseProviderSources( slicedTurn = sliced } - const classified = cachedTurnToClassified(slicedTurn) + // Classify the FULL turn, then keep only the in-range calls (same rule + // as the loop above and the Claude path): whole-exchange judgments stay + // whole-turn; cost/calls come from the retained calls. + const classifiedFull = cachedTurnToClassified(turn) + const classified = dateRange + ? (classifiedTurnSlicedToRange(classifiedFull, dateRange) ?? classifiedFull) + : classifiedFull const project = slicedTurn.calls[0]?.project ?? providerName const key = `${providerName}:${turn.sessionId}:${project}` diff --git a/tests/provider-turn-grouping.test.ts b/tests/provider-turn-grouping.test.ts index 71da3e2b..e729c5db 100644 --- a/tests/provider-turn-grouping.test.ts +++ b/tests/provider-turn-grouping.test.ts @@ -98,6 +98,47 @@ describe('provider turn grouping', () => { expect(session.categoryBreakdown[turn.category].oneShotTurns).toBe(0) }) + it('classifies a range-sliced turn from the whole turn, not the surviving calls (#852)', async () => { + const chatsDir = join(home, '.gemini', 'tmp', 'project-b', 'chats') + await mkdir(chatsDir, { recursive: true }) + await writeFile(join(chatsDir, 'session-slice.json'), JSON.stringify({ + sessionId: 'gemini-slice-1', + startTime: '2026-05-16T10:00:00.000Z', + messages: [ + { id: 'u1', timestamp: '2026-05-16T10:00:00.000Z', type: 'user', content: 'read then edit src/parser.ts' }, + { + id: 'g1', timestamp: '2026-05-16T10:00:00.000Z', type: 'gemini', content: 'reading', + model: 'gemini-3.1-pro-preview', tokens: { input: 100, output: 30 }, + toolCalls: [{ id: 't1', name: 'read_file', args: { path: 'src/parser.ts' } }], + }, + { + id: 'g2', timestamp: '2026-05-16T11:00:00.000Z', type: 'gemini', content: 'editing', + model: 'gemini-3.1-pro-preview', tokens: { input: 90, output: 25 }, + toolCalls: [{ id: 't2', name: 'edit_file', args: { path: 'src/parser.ts' } }], + }, + ], + })) + + const parseAllSessions = await loadParser() + // A range that keeps the 10:00 Read call but excludes the 11:00 Edit call, + // so the turn is sliced. `turnSlicedToRange`/`callsInRange` compare absolute + // times, so this is timezone-independent. + const sliceRange: DateRange = { + start: new Date('2026-05-16T10:00:00.000Z'), + end: new Date('2026-05-16T10:30:00.000Z'), + } + const projects = await parseAllSessions(sliceRange, 'gemini') + const turn = projects[0]!.sessions[0]!.turns[0]! + + // Cost/calls are sliced to the range: only the Read call survives. + expect(turn.assistantCalls.map(c => c.deduplicationKey)).toEqual(['gemini:gemini-slice-1:g1']) + // But category/hasEdits are whole-turn judgments — the Edit is part of the + // exchange — so they stay classified from the FULL turn, matching the Claude + // path rather than being re-derived from the partial slice (which alone reads + // as a no-edit exploration turn). + expect(turn.hasEdits).toBe(true) + }) + it('groups Mistral Vibe assistant messages and uses Vibe session_cost when present', async () => { const sessionDir = join(vibeHome, 'logs', 'session', 'session_20260516_100000_vibe') await mkdir(sessionDir, { recursive: true })