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.
This commit is contained in:
Aditya Vikram Singh 2026-08-24 18:35:59 +05:30
parent ee6d614557
commit 57e897c74a
2 changed files with 47 additions and 9 deletions

View file

@ -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))

View file

@ -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<typeof usageCall>[]): ProjectSummary {
function usageProjectFromTurns(turns: ReturnType<typeof usageCall>[][]): 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<typeof usageCall>[]): 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([