fix(web-shell): restore context usage from persisted replay (#12070)

Co-authored-by: 钉萁 <[email protected]>
This commit is contained in:
ytahdnand钉萁 authored and GitHub committed 2026-09-17 07:35:31 +00:00
1 parent ec86d902c7
commit b17658525f
2 files changed
+38 -2

No files matched your search

@@ -399,6 +399,39 @@ describe('getReplayTokenCount', () => {
).toBe(23_000);
});
it('restores flat persisted usage and ignores newer subagent usage', () => {
const usage = { inputTokens: 23_000, totalTokens: 25_000 };
const events: DaemonEvent[] = [
usageEvent(1, { inputTokens: 11_000 }),
{
id: 2,
v: 1,
type: 'session_update',
data: {
sessionUpdate: 'agent_message_chunk',
content: { type: 'text', text: '' },
_meta: { usage },
},
},
{
id: 3,
v: 1,
type: 'session_update',
data: {
sessionUpdate: 'agent_message_chunk',
content: { type: 'text', text: '' },
_meta: {
parentToolCallId: 'agent-1',
usage: { inputTokens: 1_000 },
},
},
},
];
expect(getReplayTokenUsage(events)).toEqual(usage);
expect(getReplayTokenCount(events)).toBe(23_000);
});
it('returns the latest structured usage fields', () => {
expect(
getReplayTokenUsage([
@@ -5,7 +5,10 @@
*/
import type { Dispatch, SetStateAction } from 'react';
import { parseDaemonBackgroundTurn } from '@qwen-code/sdk/daemon';
import {
getSessionUpdatePayload,
parseDaemonBackgroundTurn,
} from '@qwen-code/sdk/daemon';
import type {
DaemonAvailableCommand,
DaemonEvent,
@@ -826,7 +829,7 @@ export function getReplayTokenUsage(
try {
const event = events[i];
if (event.type !== 'session_update') continue;
const update = getRecord(getRecord(event.data)?.['update']);
const update = getSessionUpdatePayload(event.data);
const tokenUsage = getUsageTokenUsage(update);
if (tokenUsage) return tokenUsage;
} catch {