Skip to content

Commit c857ac6

Browse files
committed
test(agents): cover cumulative usage reporting paths
1 parent 1c3508b commit c857ac6

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

‎src/agents/embedded-agent-runner/run/helpers.test.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,34 @@ describe("buildUsageAgentMetaFields", () => {
253253
expect(fields.promptTokens).toBe(180);
254254
});
255255

256+
it("keeps cumulative usage and the latest call distinct across a zero-usage retry", () => {
257+
const usageAccumulator = createUsageAccumulator();
258+
mergeUsageIntoAccumulator(usageAccumulator, {
259+
input: 100,
260+
output: 50,
261+
total: 150,
262+
});
263+
const latestCallUsage = {
264+
input: 150,
265+
output: 50,
266+
total: 200,
267+
} satisfies NormalizedUsage;
268+
mergeUsageIntoAccumulator(usageAccumulator, latestCallUsage);
269+
270+
const fields = buildUsageAgentMetaFields({
271+
usageAccumulator,
272+
lastAssistantUsage: { input: 0, output: 0, total: 0 },
273+
lastRunPromptUsage: latestCallUsage,
274+
});
275+
276+
expect(fields.usage).toMatchObject({
277+
input: 250,
278+
output: 100,
279+
total: 350,
280+
});
281+
expect(fields.lastCallUsage).toEqual(latestCallUsage);
282+
});
283+
256284
it("does not derive a prompt override from unavailable context usage", () => {
257285
const usageAccumulator = createUsageAccumulator();
258286
const latestCallUsage = {
@@ -297,6 +325,38 @@ describe("buildUsageAgentMetaFields", () => {
297325
});
298326

299327
describe("buildErrorAgentMeta", () => {
328+
it("keeps cumulative usage separate from the latest call on error exits", () => {
329+
const usageAccumulator = createUsageAccumulator();
330+
mergeUsageIntoAccumulator(usageAccumulator, {
331+
input: 100,
332+
output: 50,
333+
total: 150,
334+
});
335+
const latestCallUsage = {
336+
input: 150,
337+
output: 50,
338+
total: 200,
339+
} satisfies NormalizedUsage;
340+
mergeUsageIntoAccumulator(usageAccumulator, latestCallUsage);
341+
342+
const fields = buildErrorAgentMeta({
343+
sessionId: "session-error",
344+
sessionFile: "/tmp/session-error.jsonl",
345+
provider: "anthropic",
346+
model: "claude-opus-4-6",
347+
usageAccumulator,
348+
lastRunPromptUsage: latestCallUsage,
349+
lastAssistant: { usage: latestCallUsage },
350+
});
351+
352+
expect(fields.usage).toMatchObject({
353+
input: 250,
354+
output: 100,
355+
total: 350,
356+
});
357+
expect(fields.lastCallUsage).toEqual(latestCallUsage);
358+
});
359+
300360
it("preserves active session file for error exits after transcript rotation", () => {
301361
// Error metadata follows the active session after transcript rotation so
302362
// diagnostics and resume links point at the file that contains the failure.

‎src/gateway/worker-environments/worker-turn-launcher.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ describe("worker turn launcher", () => {
10461046
output: 40,
10471047
cacheRead: 60,
10481048
cacheWrite: 5,
1049-
total: 270,
1049+
total: 405,
10501050
},
10511051
lastCallUsage: {
10521052
input: 200,

0 commit comments

Comments
 (0)