Skip to content

fix(ui): use current context usage in Control UI#71462

Merged
vincentkoc merged 1 commit intomainfrom
dedupe-control-ui-context-tokens
Apr 25, 2026
Merged

fix(ui): use current context usage in Control UI#71462
vincentkoc merged 1 commit intomainfrom
dedupe-control-ui-context-tokens

Conversation

@vincentkoc
Copy link
Copy Markdown
Member

@vincentkoc vincentkoc commented Apr 25, 2026

Summary

  • make Control UI /usage calculate context percentage from the fresh session context snapshot instead of cumulative input tokens
  • hide the /usage context percentage when the snapshot is stale and mark the cumulative total as approximate
  • include cache-write tokens in the Usage overview cache-hit-rate denominator
  • sync generated Control UI i18n metadata

Fixes #47885

Related: #48144, #68557, #49917, #45913, #46766, #46620, #48827, #51536

Validation

  • OPENCLAW_LOCAL_CHECK=0 OPENCLAW_VITEST_FS_MODULE_CACHE=0 pnpm test ui/src/ui/chat/slash-command-executor.node.test.ts ui/src/ui/views/usage-render-overview.test.ts ui/src/ui/chat/context-notice.test.ts ui/src/ui/chat/grouped-render.test.ts
  • pnpm ui:i18n:check
  • OPENCLAW_LOCAL_CHECK=0 OPENCLAW_VITEST_FS_MODULE_CACHE=0 pnpm check:changed

@vincentkoc vincentkoc self-assigned this Apr 25, 2026
@vincentkoc vincentkoc marked this pull request as ready for review April 25, 2026 07:28
@openclaw-barnacle openclaw-barnacle Bot added app: web-ui App: web-ui size: S maintainer Maintainer-authored PR labels Apr 25, 2026
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 25, 2026

Greptile Summary

This PR fixes /usage context percentage in the Control UI to use the session's context snapshot total (session.totalTokens) rather than raw cumulative input tokens, hides the percentage when the snapshot is stale (marking the displayed total as approximate with a ~ prefix), and adds cacheWrite tokens to the cache-hit-rate denominator in the Usage overview.

All three changes are backed by new tests and the arithmetic is consistent throughout.

Confidence Score: 4/5

Safe to merge; logic is sound and well-tested, with one minor style redundancy.

Only P2 style findings (redundant ?? 0 / ?? null guards after Number.isFinite() checks). No logic or security issues identified.

No files require special attention.

Prompt To Fix All With AI
This is a comment left during a code review.
Path: ui/src/ui/chat/slash-command-executor.ts
Line: 382-387

Comment:
**Redundant null-coalescing guards after type-narrowing checks**

`Number.isFinite()` guarantees the value is a concrete number (never `null` or `undefined`), so the `?? 0` and `?? null` fallbacks on lines 382–386 can never trigger. The code still works, but the guards create a false impression that those branches are reachable and add noise.

```suggestion
    const hasInputTokens = Number.isFinite(session.inputTokens);
    const hasOutputTokens = Number.isFinite(session.outputTokens);
    const input = hasInputTokens ? (session.inputTokens as number) : 0;
    const output = hasOutputTokens ? (session.outputTokens as number) : 0;
    const cumulativeTotal = hasInputTokens || hasOutputTokens ? input + output : null;
    const contextSnapshotTotal = Number.isFinite(session.totalTokens)
      ? (session.totalTokens as number)
      : cumulativeTotal;
```

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "fix(ui): use current context usage in co..." | Re-trigger Greptile

Comment on lines +382 to +387
const input = hasInputTokens ? (session.inputTokens ?? 0) : 0;
const output = hasOutputTokens ? (session.outputTokens ?? 0) : 0;
const cumulativeTotal = hasInputTokens || hasOutputTokens ? input + output : null;
const contextSnapshotTotal = Number.isFinite(session.totalTokens)
? (session.totalTokens ?? null)
: cumulativeTotal;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Redundant null-coalescing guards after type-narrowing checks

Number.isFinite() guarantees the value is a concrete number (never null or undefined), so the ?? 0 and ?? null fallbacks on lines 382–386 can never trigger. The code still works, but the guards create a false impression that those branches are reachable and add noise.

Suggested change
const input = hasInputTokens ? (session.inputTokens ?? 0) : 0;
const output = hasOutputTokens ? (session.outputTokens ?? 0) : 0;
const cumulativeTotal = hasInputTokens || hasOutputTokens ? input + output : null;
const contextSnapshotTotal = Number.isFinite(session.totalTokens)
? (session.totalTokens ?? null)
: cumulativeTotal;
const hasInputTokens = Number.isFinite(session.inputTokens);
const hasOutputTokens = Number.isFinite(session.outputTokens);
const input = hasInputTokens ? (session.inputTokens as number) : 0;
const output = hasOutputTokens ? (session.outputTokens as number) : 0;
const cumulativeTotal = hasInputTokens || hasOutputTokens ? input + output : null;
const contextSnapshotTotal = Number.isFinite(session.totalTokens)
? (session.totalTokens as number)
: cumulativeTotal;
Prompt To Fix With AI
This is a comment left during a code review.
Path: ui/src/ui/chat/slash-command-executor.ts
Line: 382-387

Comment:
**Redundant null-coalescing guards after type-narrowing checks**

`Number.isFinite()` guarantees the value is a concrete number (never `null` or `undefined`), so the `?? 0` and `?? null` fallbacks on lines 382–386 can never trigger. The code still works, but the guards create a false impression that those branches are reachable and add noise.

```suggestion
    const hasInputTokens = Number.isFinite(session.inputTokens);
    const hasOutputTokens = Number.isFinite(session.outputTokens);
    const input = hasInputTokens ? (session.inputTokens as number) : 0;
    const output = hasOutputTokens ? (session.outputTokens as number) : 0;
    const cumulativeTotal = hasInputTokens || hasOutputTokens ? input + output : null;
    const contextSnapshotTotal = Number.isFinite(session.totalTokens)
      ? (session.totalTokens as number)
      : cumulativeTotal;
```

How can I resolve this? If you propose a fix, please make it concise.

@vincentkoc vincentkoc force-pushed the dedupe-control-ui-context-tokens branch from 25cb7e3 to 16515b2 Compare April 25, 2026 07:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

app: web-ui App: web-ui maintainer Maintainer-authored PR size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Control UI token display shows incorrect values - shows cumulative tokens instead of current context

1 participant