xonecas.io
64875
Mc

mini-coder / commit eee0900

fix: estimate status bar context usage

xonecas2026-04-06 eee09007e99bf529cf5cd04c6c550e0178264dcb parent 2064f1e

6 files changed

TODO.md+1−1
@@ -40,7 +40,7 @@ The spec.md and code are the sources of truth, not this file, don't assume anyth
4040 - [ ] `/skill:name` input handling (strip prefix, prepend skill body to user message)
4141 - [ ] Sending images in the prompt
4242 - [ ] Tab file path autocomplete in input
43-- [ ] Context limit compaction (threshold detection, model-generated summary, prompt cache preservation)
43+- [ ] Optional context compaction plugin or extension point (threshold detection, model-generated summary, prompt cache preservation)
4444
4545 ## Future ideas
4646
spec.md+4−4
@@ -221,7 +221,7 @@ The core runtime. This is what happens on each turn:
221221
222222 **No step limit** — the loop runs until the model stops (`stopReason: "stop"`) or the user interrupts. There is no maximum number of tool calls per turn.
223223
224-**Context limit** — when the cumulative token count approaches `Model.contextWindow`, the agent must handle it before the provider rejects the request. Strategy: when context usage exceeds a threshold (e.g., 90%), trigger a compaction step — summarize the conversation history into a condensed form and replace the older messages. The summary is generated by the model itself (a separate, non-streamed call). The compacted history must preserve the system prompt prefix unchanged to maintain prom …
224+**Context limit** — mini-coder does not implement built-in context compaction yet. The status bar still estimates current context usage for the next request, but automatic history summarization is deferred. If we add compaction later, it should be optional and likely plugin-provided rather than hardwired into the core, since users disagree about whether automatic compaction is desirable.
225225
226226 ## Architecture
227227
@@ -483,9 +483,9 @@ anthropic/sonnet-4 · med in:1.2k out:0.8k · 42.0%/200k · $0.0
483483 **Line 2 — session:**
484484
485485 - Left: `provider/model · effort`. Effort shown as `low`, `med`, `high`, `xhigh`.
486-- Right: `in:input out:output · context%/window · $cost`. `in`, `out`, and `$cost` are **cumulative for the session**. `context%/window` shows the most recently loaded context as a percentage of the active model's context window, e.g. `33.1%/272k`.
486+- Right: `in:input out:output · context%/window · $cost`. `in`, `out`, and `$cost` are **cumulative for the session**. `context%/window` shows the **estimated current context usage for the next model request** as a percentage of the active model's context window.
487487
488-Token counts use human-friendly units (1.2k, 45k, 1.2M). Context usage is computed from the most recent assistant message's `usage.totalTokens` vs `Model.contextWindow` — this is the best available approximation of the actual prompt/response footprint without re-tokenizing the conversation locally. The denominator shows the active model's context window in the same compact units. Cost is a running sum of `AssistantMessage.usage.cost.total` across all messages in the session, accumulated by us …
488+Token counts use human-friendly units (1.2k, 45k, 1.2M). Context usage is estimated from the current model-visible conversation, not just the last assistant message. Use the most recent valid assistant `usage` as an anchor (`totalTokens` when present, otherwise `input + output + cacheRead + cacheWrite`) and add heuristic estimates for any later messages. If no assistant `usage` exists yet, estimate the full current model-visible history heuristically. UI-only messages are excluded from this esti …
489489
490490 ### Conversation log
491491
@@ -622,7 +622,7 @@ CREATE INDEX idx_messages_session ON messages(session_id, turn);
622622
623623 **Turn grouping**: the `turn` column groups messages that belong to the same turn. A turn is: one user message + one or more assistant messages + any tool result messages from that agent loop. `/undo` deletes all messages with the highest `turn` value in the session. `/fork` copies all messages to a new session preserving turn numbers.
624624
625-**Cumulative stats are computed, not stored**: token counts and cost for the status bar are computed by summing `usage` from assistant messages on session load (deserialize all messages, filter for `role: "assistant"`, sum their `usage` fields). The message count per session is small (hundreds), so this is fast. No separate counters to keep in sync. During an active session, a running in-memory accumulator is updated after each assistant message to avoid re-scanning.
625+**Cumulative stats are computed, not stored**: the status bar's cumulative `in`, `out`, and `$cost` values are computed by summing `usage` from assistant messages on session load (deserialize all messages, filter for `role: "assistant"`, sum their `usage` fields). The message count per session is small (hundreds), so this is fast. No separate counters to keep in sync. During an active session, a running in-memory accumulator is updated after each assistant message to avoid re-scanning. `context% …
626626
627627 **Turn number assignment**: when a user message is appended, its turn number is `MAX(turn) + 1` for the session (or 1 for the first message). All subsequent messages in the same agent loop (assistant responses, tool results) share that turn number. This is what makes `/undo` atomic — it deletes all messages with the highest turn.
628628
src/index.ts+1−1
@@ -301,7 +301,7 @@ export interface AppState {
301301 effort: ThinkingLevel;
302302 /** Message history for the active session. */
303303 messages: ReturnType<typeof loadMessages>;
304- /** Cumulative usage stats. */
304+ /** Cumulative session input/output/cost stats for the status bar. */
305305 stats: SessionStats;
306306 /** Discovered AGENTS.md files. */
307307 agentsMd: AgentsMdFile[];
src/session.ts+4−2
@@ -42,11 +42,13 @@ export interface Session {
4242 }
4343
4444 /**
45- * Cumulative token and cost statistics for a session.
45+ * Cumulative input/output token and cost statistics for a session.
4646 *
4747 * Computed by summing `usage` fields from all {@link AssistantMessage}s
4848 * in the session's history. Not stored — derived on load and maintained
49- * in-memory during the session.
49+ * in-memory during the session. These feed the status bar's cumulative
50+ * `in`, `out`, and `$cost` values; current context usage is estimated
51+ * separately from the current model-visible history.
5052 */
5153 export interface SessionStats {
5254 /** Total input tokens across all assistant messages. */
src/ui.test.ts+111−10
@@ -14,7 +14,12 @@ import {
1414 DEFAULT_SHOW_REASONING,
1515 DEFAULT_VERBOSE,
1616 } from "./index.ts";
17-import { createSession, loadMessages, openDatabase } from "./session.ts";
17+import {
18+ createSession,
19+ createUiMessage,
20+ loadMessages,
21+ openDatabase,
22+} from "./session.ts";
1823 import { loadSettings } from "./settings.ts";
1924 import { DEFAULT_THEME, type Theme } from "./theme.ts";
2025 import {
@@ -516,7 +521,7 @@ describe("ui rendering", () => {
516521 }
517522 });
518523
519- test("renderStatusBar uses the latest assistant usage for context percentage", () => {
524+ test("renderStatusBar estimates context usage from the latest valid assistant usage plus trailing model-visible messages", () => {
520525 const faux = registerFauxProvider();
521526 const state = createTestState();
522527 state.model = {
@@ -525,17 +530,21 @@ describe("ui rendering", () => {
525530 };
526531 state.messages = [
527532 { role: "user", content: "first", timestamp: 1 },
528- makeAssistantWithUsage("First.", {
529- input: 120,
530- output: 30,
531- totalTokens: 150,
532- }),
533- { role: "user", content: "second", timestamp: 2 },
534533 makeAssistantWithUsage("Second.", {
535534 input: 200,
536535 output: 50,
537536 totalTokens: 250,
538537 }),
538+ createUiMessage("x".repeat(500)),
539+ { role: "user", content: "12345678", timestamp: 2 },
540+ {
541+ role: "toolResult",
542+ toolCallId: "tool-1",
543+ toolName: "shell",
544+ content: [{ type: "text", text: "done" }],
545+ isError: false,
546+ timestamp: 3,
547+ },
539548 ];
540549 state.stats = {
541550 totalInput: 4_000,
@@ -547,8 +556,100 @@ describe("ui rendering", () => {
547556 const node = renderStatusBar(state);
548557 const text = collectText(node);
549558
550- expect(text).toContain("in:4.0k out:2.0k · 25.0%/1k · $1.23");
551- expect(text).not.toContain("in:4.0k out:2.0k · 600.0%/1k · $1.23");
559+ expect(text).toContain("in:4.0k out:2.0k · 25.3%/1k · $1.23");
560+ expect(text).not.toContain("in:4.0k out:2.0k · 75.3%/1k · $1.23");
561+ expect(text).not.toContain("in:4.0k out:2.0k · 25.0%/1k · $1.23");
562+ } finally {
563+ faux.unregister();
564+ state.db.close();
565+ }
566+ });
567+
568+ test("renderStatusBar estimates context usage before the first assistant response", () => {
569+ const faux = registerFauxProvider();
570+ const state = createTestState();
571+ state.model = {
572+ ...faux.getModel(),
573+ contextWindow: 100,
574+ };
575+ state.messages = [
576+ createUiMessage("x".repeat(500)),
577+ { role: "user", content: "12345678", timestamp: 1 },
578+ ];
579+
580+ try {
581+ const node = renderStatusBar(state);
582+ const text = collectText(node);
583+
584+ expect(text).toContain("in:0 out:0 · 2.0%/100 · $0.00");
585+ expect(text).not.toContain("in:0 out:0 · 127.0%/100 · $0.00");
586+ expect(text).not.toContain("in:0 out:0 · 0.0%/100 · $0.00");
587+ } finally {
588+ faux.unregister();
589+ state.db.close();
590+ }
591+ });
592+
593+ test("renderStatusBar skips aborted assistant usage as a context anchor", () => {
594+ const faux = registerFauxProvider();
595+ const state = createTestState();
596+ const aborted = makeAssistantWithUsage("abcdefghi", {
597+ input: 700,
598+ output: 200,
599+ totalTokens: 900,
600+ });
601+ aborted.stopReason = "aborted";
602+
603+ state.model = {
604+ ...faux.getModel(),
605+ contextWindow: 1_000,
606+ };
607+ state.messages = [
608+ { role: "user", content: "first", timestamp: 1 },
609+ makeAssistantWithUsage("valid", {
610+ input: 150,
611+ output: 50,
612+ totalTokens: 200,
613+ }),
614+ aborted,
615+ { role: "user", content: "1234", timestamp: 2 },
616+ ];
617+
618+ try {
619+ const node = renderStatusBar(state);
620+ const text = collectText(node);
621+
622+ expect(text).toContain("in:0 out:0 · 20.4%/1k · $0.00");
623+ expect(text).not.toContain("in:0 out:0 · 90.0%/1k · $0.00");
624+ } finally {
625+ faux.unregister();
626+ state.db.close();
627+ }
628+ });
629+
630+ test("renderStatusBar falls back to usage components when totalTokens is zero", () => {
631+ const faux = registerFauxProvider();
632+ const state = createTestState();
633+ state.model = {
634+ ...faux.getModel(),
635+ contextWindow: 1_000,
636+ };
637+ state.messages = [
638+ makeAssistantWithUsage("fallback", {
639+ input: 120,
640+ output: 30,
641+ cacheRead: 25,
642+ cacheWrite: 25,
643+ totalTokens: 0,
644+ }),
645+ ];
646+
647+ try {
648+ const node = renderStatusBar(state);
649+ const text = collectText(node);
650+
651+ expect(text).toContain("in:0 out:0 · 20.0%/1k · $0.00");
652+ expect(text).not.toContain("in:0 out:0 · 0.0%/1k · $0.00");
552653 } finally {
553654 faux.unregister();
554655 state.db.close();
src/ui.ts+99−9
@@ -72,6 +72,9 @@ const DIVIDER_FRAME_MS = 60;
7272 /** Width of the bright pulse segment in the animated divider. */
7373 const PULSE_WIDTH = 5;
7474
75+/** Conservative fixed estimate for an image block's token footprint. */
76+const ESTIMATED_IMAGE_TOKENS = 1_200;
77+
7578 // ---------------------------------------------------------------------------
7679 // UI state (module-scoped, not in AppState)
7780 // ---------------------------------------------------------------------------
@@ -308,23 +311,110 @@ function formatModelInfo(state: AppState): string {
308311 return `${state.model.provider}/${state.model.id} · ${formatEffort(state.effort)}`;
309312 }
310313
311-/** Get the most recent assistant message's total token usage. */
312-function getLatestContextTokenUsage(state: AppState): number {
313- for (let i = state.messages.length - 1; i >= 0; i--) {
314- const message = state.messages[i];
315- if (message?.role === "assistant") {
316- return (message as AssistantMessage).usage.totalTokens;
314+/** Calculate context tokens from assistant usage, falling back when `totalTokens` is zero. */
315+function calculateUsageTokens(usage: AssistantMessage["usage"]): number {
316+ return (
317+ usage.totalTokens ||
318+ usage.input + usage.output + usage.cacheRead + usage.cacheWrite
319+ );
320+}
321+
322+/** Estimate token usage from a character count using a conservative chars/4 heuristic. */
323+function estimateCharacterTokens(charCount: number): number {
324+ return Math.ceil(charCount / 4);
325+}
326+
327+/** Estimate token usage for a model-visible message. */
328+function estimateMessageTokens(message: Message): number {
329+ switch (message.role) {
330+ case "user": {
331+ if (typeof message.content === "string") {
332+ return estimateCharacterTokens(message.content.length);
333+ }
334+
335+ let chars = 0;
336+ let imageTokens = 0;
337+ for (const block of message.content) {
338+ if (block.type === "text") {
339+ chars += block.text.length;
340+ } else if (block.type === "image") {
341+ imageTokens += ESTIMATED_IMAGE_TOKENS;
342+ }
343+ }
344+ return estimateCharacterTokens(chars) + imageTokens;
345+ }
346+ case "assistant": {
347+ let chars = 0;
348+ for (const block of message.content) {
349+ if (block.type === "text") {
350+ chars += block.text.length;
351+ } else if (block.type === "thinking") {
352+ chars += block.thinking.length;
353+ } else if (block.type === "toolCall") {
354+ chars += block.name.length + JSON.stringify(block.arguments).length;
355+ }
356+ }
357+ return estimateCharacterTokens(chars);
358+ }
359+ case "toolResult": {
360+ let chars = 0;
361+ let imageTokens = 0;
362+ for (const block of message.content) {
363+ if (block.type === "text") {
364+ chars += block.text.length;
365+ } else if (block.type === "image") {
366+ imageTokens += ESTIMATED_IMAGE_TOKENS;
367+ }
368+ }
369+ return estimateCharacterTokens(chars) + imageTokens;
370+ }
371+ }
372+}
373+
374+/** Find the latest assistant usage that can anchor context estimation. */
375+function getLatestValidAssistantUsage(
376+ messages: readonly Message[],
377+): { index: number; tokens: number } | null {
378+ for (let i = messages.length - 1; i >= 0; i--) {
379+ const message = messages[i];
380+ if (
381+ message?.role === "assistant" &&
382+ message.stopReason !== "aborted" &&
383+ message.stopReason !== "error"
384+ ) {
385+ return {
386+ index: i,
387+ tokens: calculateUsageTokens(message.usage),
388+ };
317389 }
318390 }
319- return 0;
391+ return null;
392+}
393+
394+/** Estimate the current model-visible context size for the next request. */
395+function estimateCurrentContextTokens(state: AppState): number {
396+ const messages = filterModelMessages(state.messages);
397+ const latestUsage = getLatestValidAssistantUsage(messages);
398+
399+ if (!latestUsage) {
400+ return messages.reduce((total, message) => {
401+ return total + estimateMessageTokens(message);
402+ }, 0);
403+ }
404+
405+ let total = latestUsage.tokens;
406+ for (let i = latestUsage.index + 1; i < messages.length; i++) {
407+ total += estimateMessageTokens(messages[i]!);
408+ }
409+ return total;
320410 }
321411
322-/** Format usage stats for the status bar right side. */
412+/** Format cumulative session totals plus estimated current context usage for the status bar. */
323413 function formatUsage(state: AppState): string {
324414 if (!state.model) return "";
325415 const inp = formatTokens(state.stats.totalInput);
326416 const out = formatTokens(state.stats.totalOutput);
327- const contextTokens = getLatestContextTokenUsage(state);
417+ const contextTokens = estimateCurrentContextTokens(state);
328418 const ctxPct =
329419 state.model.contextWindow > 0
330420 ? (contextTokens / state.model.contextWindow) * 100