Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

test(chatRuntime): cover cost certainty tracking and formatting

Steven Enamakelf0b6d9359ed2
diff --git a/app/src/features/conversations/aui/ContextUsage.test.tsx b/app/src/features/conversations/aui/ContextUsage.test.tsx
index 35a20d0063..e30a0353cb 100644
--- a/app/src/features/conversations/aui/ContextUsage.test.tsx
+++ b/app/src/features/conversations/aui/ContextUsage.test.tsx
@@ -6,7 +6,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
import { callCoreRpc } from '../../../services/coreRpcClient';
import chatRuntimeReducer, { hydrateThreadUsage } from '../../../store/chatRuntimeSlice';
-import { contextBreakdownSegments, ContextUsage } from './ContextUsage';
+import { contextBreakdownSegments, ContextUsage, formatCost } from './ContextUsage';
vi.mock('../../../services/coreRpcClient', () => ({ callCoreRpc: vi.fn() }));
@@ -259,3 +259,12 @@ describe('contextBreakdownSegments', () => {
expect(segments.reduce((sum, s) => sum + s.tokens, 0)).toBe(100_000);
});
});
+
+describe('formatCost', () => {
+ it('shows a charge as is, marks an estimate, and shows no price when unknown', () => {
+ expect(formatCost(0.2986, 'charged')).toBe('$0.2986');
+ expect(formatCost(4.25, 'charged')).toBe('$4.25');
+ expect(formatCost(0.05, 'estimated')).toBe('≈ $0.0500');
+ expect(formatCost(4.25, 'unknown')).toBe('—');
+ });
+});
diff --git a/app/src/store/chatRuntimeSlice.test.ts b/app/src/store/chatRuntimeSlice.test.ts
index 65fb815de9..fdb6b75d3d 100644
--- a/app/src/store/chatRuntimeSlice.test.ts
+++ b/app/src/store/chatRuntimeSlice.test.ts
@@ -1032,3 +1032,70 @@ describe('hydrateRuntimeFromSnapshot — sub-agent transcript fallback (fix 4)',
expect(transcript[0].kind).toBe('tool');
});
});
+
+describe('chatRuntimeSlice cost certainty', () => {
+ it('adds reported charges and keeps the thread charged', () => {
+ const store = makeStore();
+ store.dispatch(
+ recordChatTurnUsage({ inputTokens: 10, outputTokens: 1, costUsd: 0.2, costSource: 'charged' })
+ );
+ store.dispatch(recordChatTurnUsage({ inputTokens: 10, outputTokens: 1, costUsd: 0.1 }));
+ const usage = store.getState().chatRuntime.sessionTokenUsage;
+ expect(usage.costUsd).toBeCloseTo(0.3, 9);
+ expect(usage.costSource).toBe('charged');
+ });
+
+ it('marks the thread unknown when a turn reports no cost, and adds nothing for it', () => {
+ // The core sends `null` instead of a guessed rate; it once sent $4.25 for
+ // a glm-5.3-flash thread the provider billed about $0.30.
+ const store = makeStore();
+ store.dispatch(recordChatTurnUsage({ inputTokens: 10, outputTokens: 1, costUsd: 0.2 }));
+ store.dispatch(
+ recordChatTurnUsage({
+ inputTokens: 5_710_657,
+ outputTokens: 30_790,
+ costUsd: null,
+ costSource: 'unknown',
+ })
+ );
+ const usage = store.getState().chatRuntime.sessionTokenUsage;
+ expect(usage.costSource).toBe('unknown');
+ expect(usage.costUsd).toBeCloseTo(0.2, 9);
+ });
+
+ it('carries an estimate as estimated, and a sub-agent with no cost as unknown', () => {
+ const store = makeStore();
+ store.dispatch(
+ recordChatTurnUsage({
+ inputTokens: 10,
+ outputTokens: 1,
+ costUsd: 0.05,
+ costSource: 'estimated',
+ subAgents: [{ agentId: 'researcher', inputTokens: 3, outputTokens: 1, costUsd: null }],
+ })
+ );
+ const usage = store.getState().chatRuntime.sessionTokenUsage;
+ expect(usage.costSource).toBe('estimated');
+ expect(usage.subAgents.researcher.costSource).toBe('unknown');
+ });
+
+ it('hydrates an unknown thread cost from the usage RPC', () => {
+ const store = makeStore();
+ store.dispatch(
+ hydrateThreadUsage({
+ threadId: 'thr-unknown',
+ inputTokens: 7_021_942,
+ outputTokens: 42_669,
+ cachedTokens: 6_466_688,
+ costUsd: null,
+ turns: 3,
+ contextWindow: 1_048_576,
+ lastTurnInputTokens: 5_710_657,
+ lastTurnOutputTokens: 30_790,
+ })
+ );
+ const bucket = store.getState().chatRuntime.usageByThread['thr-unknown'];
+ expect(bucket.costSource).toBe('unknown');
+ expect(bucket.costUsd).toBe(0);
+ });
+});