Skip to content

Commit 9652519

Browse files
0rginal_claw0rginal_claw
authored andcommitted
UI: align context usage display with backend and refresh after compaction\n\n- /status percent now uses canonical totalTokens when fresh, matching chat context notice\n- Refresh chat history on compaction completion to clear stale pre-compaction tokens in UI\n\nFixes #66483
1 parent 82364e9 commit 9652519

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

ui/src/ui/app-gateway.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -438,10 +438,23 @@ function handleGatewayEventUnsafe(host: GatewayHost, evt: GatewayEventFrame) {
438438
if (host.onboarding) {
439439
return;
440440
}
441-
handleAgentEvent(
442-
host as unknown as Parameters<typeof handleAgentEvent>[0],
443-
evt.payload as AgentEventPayload | undefined,
444-
);
441+
const payload = evt.payload as AgentEventPayload | undefined;
442+
handleAgentEvent(host as unknown as Parameters<typeof handleAgentEvent>[0], payload);
443+
// If a compaction just completed for the active session, refresh chat history so
444+
// the UI reflects the compacted transcript and cleared token counters immediately.
445+
try {
446+
const data = (payload?.data ?? {}) as Record<string, unknown>;
447+
const phase = typeof data.phase === "string" ? data.phase : "";
448+
const completed = data.completed === true;
449+
const sessionKey = typeof payload?.sessionKey === "string" ? payload.sessionKey : undefined;
450+
if (payload?.stream === "compaction" && phase === "end" && completed) {
451+
if (!sessionKey || sessionKey === host.sessionKey) {
452+
void loadChatHistory(host as unknown as ChatState);
453+
}
454+
}
455+
} catch {
456+
// ignore
457+
}
445458
return;
446459
}
447460

ui/src/ui/chat/slash-command-executor.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,11 @@ async function executeUsage(
380380
const output = session.outputTokens ?? 0;
381381
const total = session.totalTokens ?? input + output;
382382
const ctx = session.contextTokens ?? 0;
383-
const pct = ctx > 0 ? Math.round((input / ctx) * 100) : null;
383+
// Align with backend/UI context notice: prefer canonical totalTokens when fresh.
384+
const pct =
385+
(session.totalTokensFresh === true && typeof session.totalTokens === "number" && ctx > 0)
386+
? Math.round(Math.min((session.totalTokens / ctx) * 100, 100))
387+
: null;
384388

385389
const lines = [
386390
"**Session Usage**",

0 commit comments

Comments
 (0)