fix(ui): use current context usage in Control UI#71462
Conversation
Greptile SummaryThis PR fixes All three changes are backed by new tests and the arithmetic is consistent throughout. Confidence Score: 4/5Safe to merge; logic is sound and well-tested, with one minor style redundancy. Only P2 style findings (redundant No files require special attention. Prompt To Fix All With AIThis 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 |
| 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; |
There was a problem hiding this 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.
| 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.25cb7e3 to
16515b2
Compare
16515b2 to
4282c59
Compare
Summary
/usagecalculate context percentage from the fresh session context snapshot instead of cumulative input tokens/usagecontext percentage when the snapshot is stale and mark the cumulative total as approximateFixes #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.tspnpm ui:i18n:checkOPENCLAW_LOCAL_CHECK=0 OPENCLAW_VITEST_FS_MODULE_CACHE=0 pnpm check:changed