Skip to content

Commit 73f24c2

Browse files
fix: preserve context usage after tool-only turns (#134634)
Co-authored-by: Dmitriy <[email protected]>
1 parent 5997629 commit 73f24c2

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

‎src/agents/embedded-agent-runner/run.attempt-normalization.direct.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,25 @@ describe("normalizeEmbeddedRunAttempt", () => {
168168
},
169169
);
170170

171+
it("keeps exact attempt context usage for a tool-only turn", async () => {
172+
const attempt = makeAttempt();
173+
attempt.attemptUsage = {
174+
input: 521,
175+
output: 197,
176+
total: 21_966,
177+
contextUsage: { state: "available", promptTokens: 21_769, totalTokens: 21_966 },
178+
};
179+
180+
const result = await normalizeEmbeddedRunAttempt(
181+
makeNormalizationInput(attempt, makePromptState()),
182+
);
183+
184+
expect(result).toMatchObject({
185+
action: "proceed",
186+
lastRunPromptUsage: attempt.attemptUsage,
187+
});
188+
});
189+
171190
it("waits for pending user-turn persistence before deriving retry suppression", async () => {
172191
let releasePersistence: (() => void) | undefined;
173192
const persistence = new Promise<void>((resolve) => {

‎src/agents/embedded-agent-runner/run/attempt-normalization.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,11 @@ export async function normalizeEmbeddedRunAttempt(input: {
187187
// Current-attempt evidence is newest. The session assistant is only a transcript fallback
188188
// and can predate a carried attempt snapshot after transcript rewrites or compaction.
189189
const callUsage = resolveLatestCallUsage({
190-
currentAttemptCandidates: [currentAttemptAssistantUsage, promptCacheLastCallUsage],
190+
currentAttemptCandidates: [
191+
attempt.attemptUsage?.contextUsage ? attempt.attemptUsage : undefined,
192+
currentAttemptAssistantUsage,
193+
promptCacheLastCallUsage,
194+
],
191195
carriedUsage: input.lastRunPromptUsage,
192196
transcriptFallback: lastAssistantUsage,
193197
});

0 commit comments

Comments
 (0)