Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions src/renderer/utils/sessionAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ export function analyzeSession(detail: SessionDetail): SessionReport {
// Test progression
const testSnapshots: TestSnapshot[] = [];

// Cost tracking
let parentCost = 0;
// Cost tracking — use the pre-computed cost from calculateMetrics() as single source of truth
const parentCost = detail.metrics.costUsd ?? 0;

// Git activity
const gitCommits: GitCommit[] = [];
Expand Down Expand Up @@ -402,7 +402,6 @@ export function analyzeSession(detail: SessionDetail): SessionReport {

const callCost = calculateMessageCost(model, inpTok, outTok, cr, cc);
stats.costUsd += callCost;
parentCost += callCost;

totalCacheCreation += cc;
totalCacheRead += cr;
Expand Down Expand Up @@ -823,14 +822,7 @@ export function analyzeSession(detail: SessionDetail): SessionReport {
const subagentModel =
proc.messages.find((m: ParsedMessage) => m.type === 'assistant' && m.model)?.model ??
'default (inherits parent)';
// Compute cost from subagent token breakdown (proc.metrics.costUsd is not populated upstream)
const computedCost = calculateMessageCost(
subagentModel,
proc.metrics.inputTokens,
proc.metrics.outputTokens,
proc.metrics.cacheReadTokens,
proc.metrics.cacheCreationTokens
);
const computedCost = proc.metrics.costUsd ?? 0;
return {
description: desc,
subagentType: proc.subagentType ?? 'unknown',
Expand Down
87 changes: 84 additions & 3 deletions test/renderer/utils/sessionAnalyzer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ describe('analyzeSession', () => {
createMockDetail({
messages,
session: createMockSession({ messageCount: 4 }),
metrics: createMockMetrics({ costUsd: 0.025 }),
})
);

Expand All @@ -188,9 +189,9 @@ describe('analyzeSession', () => {
expect(report.tokenUsage.totals.cacheCreation).toBe(100);
expect(report.tokenUsage.totals.grandTotal).toBe(4000);

// Cost should be positive (sonnet-4 pricing)
expect(report.costAnalysis.parentCostUsd).toBeGreaterThan(0);
expect(report.costAnalysis.totalSessionCostUsd).toBeGreaterThan(0);
// Cost reads from detail.metrics.costUsd (single source of truth)
expect(report.costAnalysis.parentCostUsd).toBe(0.025);
expect(report.costAnalysis.totalSessionCostUsd).toBe(0.025);

// Message types
expect(report.messageTypes.user).toBe(2);
Expand Down Expand Up @@ -1506,4 +1507,84 @@ describe('analyzeSession', () => {
expect(report.subagentMetrics.byAgent[0].modelMismatch).toBeNull();
});
});

// -------------------------------------------------------------------------
// Unified cost — single source of truth
// -------------------------------------------------------------------------
describe('unified cost computation', () => {
it('parentCostUsd reads from detail.metrics.costUsd', () => {
const report = analyzeSession(
createMockDetail({
metrics: createMockMetrics({ costUsd: 1.2345 }),
})
);
expect(report.costAnalysis.parentCostUsd).toBe(1.2345);
});

it('parentCostUsd defaults to 0 when detail.metrics.costUsd is undefined', () => {
const report = analyzeSession(createMockDetail());
expect(report.costAnalysis.parentCostUsd).toBe(0);
});

it('subagent cost reads from proc.metrics.costUsd', () => {
const processes: Process[] = [
{
id: 'agent-1',
filePath: '/path/to/agent-1.jsonl',
messages: [],
startTime: new Date('2024-01-01T10:00:00Z'),
endTime: new Date('2024-01-01T10:01:00Z'),
durationMs: 60000,
metrics: createMockMetrics({ totalTokens: 5000, costUsd: 0.15 }),
description: 'research task',
subagentType: 'explore',
isParallel: false,
},
{
id: 'agent-2',
filePath: '/path/to/agent-2.jsonl',
messages: [],
startTime: new Date('2024-01-01T10:01:00Z'),
endTime: new Date('2024-01-01T10:02:00Z'),
durationMs: 60000,
metrics: createMockMetrics({ totalTokens: 3000, costUsd: 0.10 }),
description: 'test runner',
subagentType: 'code',
isParallel: false,
},
];

const report = analyzeSession(createMockDetail({ processes }));
expect(report.subagentMetrics.byAgent[0].costUsd).toBe(0.15);
expect(report.subagentMetrics.byAgent[1].costUsd).toBe(0.10);
expect(report.costAnalysis.subagentCostUsd).toBe(0.25);
});

it('totalSessionCostUsd equals parent + subagent costs', () => {
const processes: Process[] = [
{
id: 'agent-1',
filePath: '/path/to/agent-1.jsonl',
messages: [],
startTime: new Date('2024-01-01T10:00:00Z'),
endTime: new Date('2024-01-01T10:01:00Z'),
durationMs: 60000,
metrics: createMockMetrics({ totalTokens: 5000, costUsd: 0.30 }),
description: 'task',
subagentType: 'code',
isParallel: false,
},
];

const report = analyzeSession(
createMockDetail({
metrics: createMockMetrics({ costUsd: 0.50 }),
processes,
})
);
expect(report.costAnalysis.parentCostUsd).toBe(0.50);
expect(report.costAnalysis.subagentCostUsd).toBe(0.30);
expect(report.costAnalysis.totalSessionCostUsd).toBe(0.80);
Comment on lines +1585 to +1587

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While these assertions with toBe are correct for the given test data, it's a good practice to use toBeCloseTo for floating-point number comparisons in tests. This helps prevent brittle tests that might fail due to minor floating-point inaccuracies with different test values. This advice applies to other floating-point cost assertions in this file as well.

Suggested change
expect(report.costAnalysis.parentCostUsd).toBe(0.50);
expect(report.costAnalysis.subagentCostUsd).toBe(0.30);
expect(report.costAnalysis.totalSessionCostUsd).toBe(0.80);
expect(report.costAnalysis.parentCostUsd).toBeCloseTo(0.50);
expect(report.costAnalysis.subagentCostUsd).toBeCloseTo(0.30);
expect(report.costAnalysis.totalSessionCostUsd).toBeCloseTo(0.80);

});
});
});
Loading