From 57e897c74a6c10e8f1ad441e7e2be5bcb815ac0a Mon Sep 17 00:00:00 2001 From: Aditya Vikram Singh <247195684+avs-io@users.noreply.github.com> Date: Mon, 24 Aug 2026 18:35:59 +0530 Subject: [PATCH] fix(plan): close Copilot nanoAiu twins once per session foldCopilotSupplementaryTurns will not fold a supplementary twin across a local-day boundary, so the pair can occupy separate turns. Credit spend now groups at session scope. --- src/plan-usage.ts | 11 +++++++--- tests/plan-usage.test.ts | 45 ++++++++++++++++++++++++++++++++++------ 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/src/plan-usage.ts b/src/plan-usage.ts index 72e39bd6..50a14211 100644 --- a/src/plan-usage.ts +++ b/src/plan-usage.ts @@ -83,12 +83,14 @@ function planCallSpend(plan: Plan, call: { costUSD: number; nanoAiu?: number }): return isFiniteNanoAiu(call.nanoAiu) ? creditsToUsd(nanoAiuToCredits(call.nanoAiu)) : 0 } -function forEachPlanTurn(projects: ProjectSummary[], visit: (calls: ParsedApiCall[]) => void): void { +function forEachPlanSession(projects: ProjectSummary[], visit: (calls: ParsedApiCall[]) => void): void { for (const project of projects) { for (const session of project.sessions ?? []) { + const calls: ParsedApiCall[] = [] for (const turn of session.turns ?? []) { - visit(turn.assistantCalls ?? []) + calls.push(...(turn.assistantCalls ?? [])) } + visit(calls) } } } @@ -96,7 +98,10 @@ function forEachPlanTurn(projects: ProjectSummary[], visit: (calls: ParsedApiCal export function copilotCreditSpend(projects: ProjectSummary[]): { spentCredits: number; creditsIncomplete: boolean } { let nanoSum = 0 let creditsIncomplete = false - forEachPlanTurn(projects, calls => { + // Pairing is session-wide. foldCopilotSupplementaryTurns will not fold a + // supplementary twin across a local-day boundary, so both nanoAiu rows can + // occupy separate turns of the same session. Close the twin once here. + forEachPlanSession(projects, calls => { const copilot = calls.filter(call => call.provider === 'copilot') if (copilot.length === 0) return const primaryNano = copilot.filter(call => !call.supplementaryAccounting && isFiniteNanoAiu(call.nanoAiu)) diff --git a/tests/plan-usage.test.ts b/tests/plan-usage.test.ts index 7882c030..91b85ee3 100644 --- a/tests/plan-usage.test.ts +++ b/tests/plan-usage.test.ts @@ -5,7 +5,7 @@ import { join } from 'node:path' import { describe, it, expect, vi, beforeEach } from 'vitest' import { savePlan, type Plan } from '../src/config.js' -import { activePlansFromMap, computePeriodFromResetDay, getPlanScopedProjects, getPlanUsage, getPlanUsageFromProjects, getPlanUsages } from '../src/plan-usage.js' +import { activePlansFromMap, computePeriodFromResetDay, copilotCreditSpend, getPlanScopedProjects, getPlanUsage, getPlanUsageFromProjects, getPlanUsages } from '../src/plan-usage.js' import type { ProjectSummary } from '../src/types.js' const { parseAllSessionsMock } = vi.hoisted(() => ({ @@ -446,21 +446,26 @@ function usageCall(overrides: { } } -function usageProject(calls: ReturnType[]): ProjectSummary { +function usageProjectFromTurns(turns: ReturnType[][]): ProjectSummary { + const calls = turns.flat() return { project: 'codeburn', projectPath: '/tmp/codeburn', totalCostUSD: calls.reduce((sum, call) => sum + call.costUSD, 0), totalApiCalls: calls.length, sessions: [{ - turns: [{ - timestamp: calls[0]!.timestamp, - assistantCalls: calls, - }], + turns: turns.map(assistantCalls => ({ + timestamp: assistantCalls[0]!.timestamp, + assistantCalls, + })), }], } as ProjectSummary } +function usageProject(calls: ReturnType[]): ProjectSummary { + return usageProjectFromTurns([calls]) +} + describe('copilot AI credit plan math', () => { const today = new Date('2026-08-10T12:00:00.000Z') const copilotPro: Plan = { @@ -568,6 +573,34 @@ describe('copilot AI credit plan math', () => { expect(usage.creditsIncomplete).toBe(false) }) + it('does not double credits when nanoAiu twins sit in separate session turns', () => { + // foldCopilotSupplementaryTurns refuses a local-day-boundary pair, so the + // supplementary twin stays on its own turn. Credit math must still close + // once at session scope. + const projects = [ + usageProjectFromTurns([ + [usageCall({ + provider: 'copilot', + costUSD: 0.001605, + timestamp: '2026-08-05T18:25:00.000Z', + nanoAiu: 1_500_000_000, + })], + [usageCall({ + provider: 'copilot', + costUSD: 0.001605, + timestamp: '2026-08-05T18:35:00.000Z', + nanoAiu: 1_500_000_000, + supplementaryAccounting: true, + })], + ]), + ] + + expect(copilotCreditSpend(projects).spentCredits).toBe(1.5) + const usage = getPlanUsageFromProjects(copilotPro, projects, today) + expect(usage.spentCredits).toBe(1.5) + expect(usage.creditsIncomplete).toBe(false) + }) + it('keeps a $0 nanoAiu-only session on copilot plans and drops it on USD plans', () => { const projects = [ usageProject([