xonecas.io
64875
Mc

mini-coder / commit bfa983c

feat: context guards for all tool outputs

Also updates to the latest cel-tui

xonecas2026-07-09 bfa983cccc0a679bb27e717c949692fefa989ab6 parent 8fe8d4c

16 files changed

bin/mc.ts+1−1
@@ -5,7 +5,7 @@ if (import.meta.main) {
55 try {
66 await main();
77 } catch (err) {
8- console.log(err instanceof Error ? err.message : String(err));
8+ console.error(err instanceof Error ? err.message : String(err));
99 process.exit(1);
1010 }
1111 }
bun.lock+8−8
@@ -5,9 +5,9 @@
55 "": {
66 "name": "mini-coder-next",
77 "dependencies": {
8- "@cel-tui/components": "^0.8.3",
9- "@cel-tui/core": "^0.8.3",
10- "@earendil-works/pi-ai": "^0.80.3",
8+ "@cel-tui/components": "0.9.0",
9+ "@cel-tui/core": "0.9.0",
10+ "@earendil-works/pi-ai": "0.80.5",
1111 "diff": "^9.0.0",
1212 "simple-git": "^3.36.0",
1313 "yaml": "^2.9.0",
@@ -93,15 +93,15 @@
9393
9494 "@biomejs/cli-win32-x64": ["@biomejs/cli-win32-x64@2.5.0", "", { "os": "win32", "cpu": "x64" }, "sha512-VT/lF+GId+67j8aDfLkxdxNoVApsPSTbyAtB3jJq0IWTrY77WXfbPfpngxq0bA6JCEv/7k8C9qWjDRKRznDlyw=="],
9595
96- "@cel-tui/clew": ["@cel-tui/clew@0.8.3", "", { "peerDependencies": { "typescript": ">=5.0.0" } }, "sha512-7OFUe8ECleXjq9+vv2EvDcTXsiWNSQqVALD1LC/Gi9DzVp1aw0S6SloJuAv1KUVkwjZxil+mvYlk2LoC28mETQ=="],
96+ "@cel-tui/clew": ["@cel-tui/clew@0.9.0", "", { "peerDependencies": { "typescript": ">=5.0.0" } }, "sha512-uSiQKyUsGbgyLVSZ7PM3O0/oucF9GSpIdMKsWpsNbgj+y/swAgpkZZaNOr5ZF/04Yli0MDQsGmR39O4ppkJcyQ=="],
9797
98- "@cel-tui/components": ["@cel-tui/components@0.8.3", "", { "dependencies": { "@cel-tui/clew": "0.8.3", "@cel-tui/core": "0.8.3", "@cel-tui/types": "0.8.3", "yoctomarkdown": "^0.1.0" }, "peerDependencies": { "typescript": ">=5.0.0" } }, "sha512-FF3WTw9XxapNabb205U0uqu6fu3uRCsbuc3pQr/yixa5/amTWsqrcMvIM8ndC7q3dEJsQk4D5rfyOMVNnmKfnA=="],
98+ "@cel-tui/components": ["@cel-tui/components@0.9.0", "", { "dependencies": { "@cel-tui/clew": "0.9.0", "@cel-tui/core": "0.9.0", "@cel-tui/types": "0.9.0", "yoctomarkdown": "^0.1.0" }, "peerDependencies": { "typescript": ">=5.0.0" } }, "sha512-64U0uk7891jrcuOQCGkNYRIN+sO2pSLCnA2Wmbeww/JSUrgYSIdPj+KPoDM9ubcfpIQsqabuWKmeNRFRb3Pniw=="],
9999
100- "@cel-tui/core": ["@cel-tui/core@0.8.3", "", { "dependencies": { "@cel-tui/types": "0.8.3", "get-east-asian-width": "^1.5.0" }, "peerDependencies": { "typescript": ">=5.0.0" } }, "sha512-rC6nqza70HTvJGoY5cm86nwBMRnfim3g4Ysns+HebYkjwXv4a0VIcLmRNQFATNq4R/Xtg7+ba/qzsqJvTY22IA=="],
100+ "@cel-tui/core": ["@cel-tui/core@0.9.0", "", { "dependencies": { "@cel-tui/types": "0.9.0", "get-east-asian-width": "^1.5.0" }, "peerDependencies": { "typescript": ">=5.0.0" } }, "sha512-rABDmclP2pnVnD/gG3rnow8CqH5Q//3u7v4wuIhqp7LRxo+NoF/mxNJ6/ZywOiboWisQNzHdDulbPM2tLI72vw=="],
101101
102- "@cel-tui/types": ["@cel-tui/types@0.8.3", "", { "peerDependencies": { "typescript": ">=5.0.0" } }, "sha512-yEKXCY16U/W4A4JaVysv2/x/IBEHQAfb93CzL6HugN+N15ETB3Vn8tZrQwdyrOT/9WdlCQkeBH8Vj7BJHs4Hkw=="],
102+ "@cel-tui/types": ["@cel-tui/types@0.9.0", "", { "peerDependencies": { "typescript": ">=5.0.0" } }, "sha512-quABp9TkHrRamfCYbiGWaFHxoEf7tiEW4uaoZIDQyaqQQE7OCfcJvgqrTi2+0BRUxYVnhIsQgp/nnZYahcpSeA=="],
103103
104- "@earendil-works/pi-ai": ["@earendil-works/pi-ai@0.80.3", "", { "dependencies": { "@anthropic-ai/sdk": "0.91.1", "@aws-sdk/client-bedrock-runtime": "3.1048.0", "@google/genai": "1.52.0", "@mistralai/mistralai": "2.2.6", "@opentelemetry/api": "1.9.0", "@smithy/node-http-handler": "4.7.3", "http-proxy-agent": "7.0.2", "https-proxy-agent": "7.0.6", "openai": "6.26.0", "partial-json": "0.1.7", "typebox": "1.1.38" }, "bin": { "pi-ai": "dist/cli.js" } }, "sha512-jPZLMeGL5kkMSEAwAklfXTMHqZvfhsJtCCp …
104+ "@earendil-works/pi-ai": ["@earendil-works/pi-ai@0.80.5", "", { "dependencies": { "@anthropic-ai/sdk": "0.91.1", "@aws-sdk/client-bedrock-runtime": "3.1048.0", "@google/genai": "1.52.0", "@mistralai/mistralai": "2.2.6", "@opentelemetry/api": "1.9.0", "@smithy/node-http-handler": "4.7.3", "http-proxy-agent": "7.0.2", "https-proxy-agent": "7.0.6", "openai": "6.26.0", "partial-json": "0.1.7", "typebox": "1.1.38" }, "bin": { "pi-ai": "dist/cli.js" } }, "sha512-+7UODYKu4o/X8sxTHG8HEV3+O+dOYKg5MeO …
105105
106106 "@google/genai": ["@google/genai@1.52.0", "", { "dependencies": { "google-auth-library": "^10.3.0", "p-retry": "^4.6.2", "protobufjs": "^7.5.4", "ws": "^8.18.0" }, "peerDependencies": { "@modelcontextprotocol/sdk": "^1.25.2" }, "optionalPeers": ["@modelcontextprotocol/sdk"] }, "sha512-gwSvbpiN/17O9TbsqSsE/OzZcpv5Fo4RQjdngGgogtuB9RsyJ8ZHhX5KjHj1bp5N9snN2eK8LDGXSaWW2hof8Q=="],
107107
package.json+3−3
@@ -20,9 +20,9 @@
2020 "typescript": "^6.0.3"
2121 },
2222 "dependencies": {
23- "@cel-tui/components": "^0.8.3",
24- "@cel-tui/core": "^0.8.3",
25- "@earendil-works/pi-ai": "^0.80.3",
23+ "@cel-tui/components": "0.9.0",
24+ "@cel-tui/core": "0.9.0",
25+ "@earendil-works/pi-ai": "0.80.5",
2626 "diff": "^9.0.0",
2727 "simple-git": "^3.36.0",
2828 "yaml": "^2.9.0"
src/agent.ts+42−15
@@ -8,7 +8,8 @@ import type {
88 ToolResultMessage,
99 } from "@earendil-works/pi-ai";
1010 import { createAppModels } from "./models.ts";
11-import { estimateTokens } from "./shared";
11+import { estimateContextTokens, estimateTokens } from "./shared";
12+import { truncateToolOutput } from "./tool-output.ts";
1213 import type {
1314 AgentContex,
1415 AgentEvent,
@@ -19,9 +20,10 @@ import type {
1920 // ### JetBrains Junie: Observation Masking
2021 // Published research found that **simply hiding old tool outputs** matched the quality of full LLM summarization with **zero extra compute**:
2122 // https://blog.jetbrains.com/research/2025/12/efficient-context-management/
22-export function compactContext(messages: Message[]) {
23- const KEEP_OBSERVATIONS = 10;
24-
23+export function compactContext(
24+ messages: Message[],
25+ keepObservations = 10,
26+): boolean {
2527 const skillReadToolCallIds = new Set<string>();
2628 for (const message of messages) {
2729 if (message.role !== "assistant") continue;
@@ -55,9 +57,9 @@ export function compactContext(messages: Message[]) {
5557 toolResultIndices.push(i);
5658 }
5759
58- if (toolResultIndices.length <= KEEP_OBSERVATIONS) return;
60+ if (toolResultIndices.length <= keepObservations) return false;
5961
60- for (let i = 0; i < toolResultIndices.length - KEEP_OBSERVATIONS; i++) {
62+ for (let i = 0; i < toolResultIndices.length - keepObservations; i++) {
6163 const idx = toolResultIndices[i];
6264 const msg = messages[idx] as ToolResultMessage;
6365 let lines = 0;
@@ -75,6 +77,8 @@ export function compactContext(messages: Message[]) {
7577 }
7678 msg.content = [{ type: "text", text }];
7779 }
80+
81+ return true;
7882 }
7983
8084 export async function* streamAgent(
@@ -90,9 +94,27 @@ export async function* streamAgent(
9094
9195 // Main agent loop, continues until llm sends a response other than toolCall or has no tool calls.
9296 while (true) {
93- const estimate = estimateTokens(JSON.stringify(llmCtx));
94- // 80k is the agreed uppon threshold to the DUMB ZONE
95- if (estimate > 80000) compactContext(llmCtx.messages);
97+ const fallbackContext = JSON.stringify(llmCtx);
98+ let estimate = estimateContextTokens(llmCtx.messages, fallbackContext);
99+ // Stay below both the agreed-upon 80k DUMB ZONE and smaller model windows.
100+ const compactionThreshold = Math.min(
101+ 80_000,
102+ Math.floor(agentCtx.options.model.contextWindow * 0.8),
103+ );
104+ if (estimate > compactionThreshold) {
105+ for (
106+ let keepObservations = 10;
107+ keepObservations >= 0;
108+ keepObservations--
109+ ) {
110+ if (estimate <= compactionThreshold) break;
111+ if (!compactContext(llmCtx.messages, keepObservations)) continue;
112+
113+ // Provider usage describes the pre-compaction request. Once messages are
114+ // masked, fall back to estimating the context that will actually be sent.
115+ estimate = estimateTokens(JSON.stringify(llmCtx));
116+ }
117+ }
96118
97119 const s = models.streamSimple(agentCtx.options.model, llmCtx, {
98120 reasoning: agentCtx.options.effort,
@@ -247,11 +269,8 @@ async function* toolRunner(
247269
248270 try {
249271 for await (const e of tool.runner(call.arguments, signal)) {
250- // yield deltas for output, full output on result.
251- const content: TextContent = { type: "text", text: e.text };
252-
253272 if (e.type === "output") {
254- // handle deltas
273+ const content: TextContent = { type: "text", text: e.text };
255274 yield {
256275 type: "tool_update",
257276 partial: {
@@ -264,7 +283,10 @@ async function* toolRunner(
264283 },
265284 };
266285 } else if (e.type === "result") {
267- // handle final message, and check for images
286+ const content: TextContent = {
287+ type: "text",
288+ text: truncateToolOutput(e.text),
289+ };
268290 let img: ImageContent | null = null;
269291 if (e.image) {
270292 img = { ...e.image, type: "image" };
@@ -292,7 +314,12 @@ async function* toolRunner(
292314 role: "toolResult",
293315 toolCallId: call.id,
294316 toolName: call.name,
295- content: [{ type: "text", text: `Error: ${error}` }],
317+ content: [
318+ {
319+ type: "text",
320+ text: truncateToolOutput(`Error: ${error}`),
321+ },
322+ ],
296323 isError: true,
297324 timestamp,
298325 },
src/args.ts+27−0
@@ -19,6 +19,15 @@ import {
1919 const DEFAULT_PROVIDER = "openai-codex";
2020 const DEFAULT_MODEL_ID = "gpt-5.5";
2121 const DEFAULT_EFFORT: ThinkingLevel = "xhigh";
22+const VALUE_OPTIONS = new Set([
23+ "--provider",
24+ "--model",
25+ "--effort",
26+ "--prompt",
27+ "-p",
28+ "--login",
29+ "-l",
30+]);
2231
2332 function formatValidationError(
2433 label: string,
@@ -91,6 +100,24 @@ function requireValue(argv: string[], index: number, flag: string) {
91100 return value;
92101 }
93102
103+export function validateArgv(argv: string[]): void {
104+ for (let i = 0; i < argv.length; i++) {
105+ const argument = argv[i];
106+
107+ if (argument === "--update") {
108+ continue;
109+ }
110+
111+ if (!VALUE_OPTIONS.has(argument)) {
112+ const kind = argument.startsWith("-") ? "option" : "argument";
113+ throw new Error(`Unknown ${kind}: ${argument}`);
114+ }
115+
116+ requireValue(argv, i, argument);
117+ i++;
118+ }
119+}
120+
94121 export async function handleArgv(argv: string[]): Promise<CliOptions> {
95122 const settings = await getSettings();
96123 const customProviders = settings?.customProviders ?? [];
src/error-handling.test.ts+0−163
@@ -1,163 +0,0 @@
1-import { afterAll, beforeEach, describe, expect, test } from "bun:test";
2-import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
3-import { tmpdir } from "node:os";
4-import { join } from "node:path";
5-import { runBashTool } from "./tool-bash.ts";
6-import { runEditTool } from "./tool-edit.ts";
7-import { runReadTool } from "./tool-read.ts";
8-import type { ToolRunnerEvent } from "./types.ts";
9-
10-const originalDataDir = Bun.env.MINI_CODER_DATA_DIR;
11-const testHome = await mkdtemp(join(tmpdir(), "mini-coder-errors-"));
12-const dataDir = join(testHome, "data");
13-Bun.env.MINI_CODER_DATA_DIR = dataDir;
14-
15-const sessionsDir = join(dataDir, "sessions");
16-const { handleArgv } = await import("./args.ts");
17-const { getAvailableProviders } = await import("./oauth.ts");
18-const { getSession, listSessionsForCwd } = await import("./session.ts");
19-
20-type ResultEvent = Extract<ToolRunnerEvent, { type: "result" }>;
21-
22-async function collectToolEvents(
23- events: AsyncGenerator<ToolRunnerEvent>,
24-): Promise<ToolRunnerEvent[]> {
25- const collected: ToolRunnerEvent[] = [];
26-
27- for await (const event of events) {
28- collected.push(event);
29- }
30-
31- return collected;
32-}
33-
34-function resultEvent(events: ToolRunnerEvent[]): ResultEvent {
35- const result = events.findLast(
36- (event): event is ResultEvent => event.type === "result",
37- );
38-
39- if (!result) {
40- throw new Error("Tool did not yield a result");
41- }
42-
43- return result;
44-}
45-
46-async function toolResult(
47- events: AsyncGenerator<ToolRunnerEvent>,
48-): Promise<ResultEvent> {
49- return resultEvent(await collectToolEvents(events));
50-}
51-
52-beforeEach(async () => {
53- await rm(dataDir, { recursive: true, force: true });
54-});
55-
56-afterAll(async () => {
57- if (originalDataDir === undefined) {
58- delete Bun.env.MINI_CODER_DATA_DIR;
59- } else {
60- Bun.env.MINI_CODER_DATA_DIR = originalDataDir;
61- }
62-
63- await rm(testHome, { recursive: true, force: true });
64-});
65-
66-describe("tool runner error handling", () => {
67- test("read and edit report missing files as tool results", async () => {
68- const missingPath = join(testHome, "missing.txt");
69-
70- const readResult = await toolResult(runReadTool({ path: missingPath }));
71- expect(readResult.text).toBe(`File not found: ${missingPath}`);
72-
73- const editResult = await toolResult(
74- runEditTool({ path: missingPath, oldText: "old", newText: "new" }),
75- );
76- expect(editResult.text).toBe(`File not found: ${missingPath}`);
77- });
78-
79- test("edit refuses unsafe writes and preserves the file", async () => {
80- const path = join(testHome, "edit.txt");
81- await writeFile(path, "one\none\n");
82-
83- const ambiguous = await toolResult(
84- runEditTool({ path, oldText: "one", newText: "two" }),
85- );
86- expect(ambiguous.text).toContain("Multiple matches found");
87- expect(await Bun.file(path).text()).toBe("one\none\n");
88-
89- const missing = await toolResult(
90- runEditTool({ path, oldText: "missing", newText: "two" }),
91- );
92- expect(missing.text).toBe(`Old text not found in: ${path}`);
93- expect(await Bun.file(path).text()).toBe("one\none\n");
94-
95- const abortController = new AbortController();
96- abortController.abort();
97- await writeFile(path, "hello");
98-
99- const aborted = await toolResult(
100- runEditTool(
101- { path, oldText: "hello", newText: "bye" },
102- abortController.signal,
103- ),
104- );
105- expect(aborted.text).toBe("Aborted before write.");
106- expect(await Bun.file(path).text()).toBe("hello");
107- });
108-
109- test("bash reports non-zero exits instead of throwing", async () => {
110- const result = await toolResult(
111- runBashTool({ command: 'printf "bad"; exit 7' }),
112- );
113-
114- expect(result.text).toBe("bad\n\nExit code: 7");
115- });
116-});
117-
118-describe("startup file error handling", () => {
119- test("settings JSON parse failures are labelled", async () => {
120- await mkdir(dataDir, { recursive: true });
121- await writeFile(join(dataDir, "settings.json"), "{");
122-
123- await expect(handleArgv([])).rejects.toThrow("Invalid settings JSON:");
124- });
125-
126- test("auth JSON parse failures are labelled", async () => {
127- await mkdir(dataDir, { recursive: true });
128- await writeFile(join(dataDir, "auth.json"), "{");
129-
130- await expect(getAvailableProviders()).rejects.toThrow("Invalid auth JSON:");
131- });
132-});
133-
134-describe("session file error handling", () => {
135- test("listSessionsForCwd ignores missing dirs and bad session files", async () => {
136- expect(await listSessionsForCwd()).toEqual([]);
137-
138- await mkdir(sessionsDir, { recursive: true });
139- await writeFile(join(sessionsDir, "broken.json"), "{");
140- await writeFile(
141- join(sessionsDir, "elsewhere.json"),
142- JSON.stringify({ id: "elsewhere", cwd: "/elsewhere", messages: [] }),
143- );
144- await writeFile(
145- join(sessionsDir, "ok.json"),
146- JSON.stringify({
147- id: "ok",
148- cwd: process.cwd(),
149- messages: [{ role: "user", content: "hi", timestamp: 2 }],
150- }),
151- );
152-
153- const sessions = await listSessionsForCwd();
154- expect(sessions.map((session) => session.id)).toEqual(["ok"]);
155- });
156-
157- test("getSession returns undefined for bad session files", async () => {
158- await mkdir(sessionsDir, { recursive: true });
159- await writeFile(join(sessionsDir, "broken.json"), "{");
160-
161- expect(await getSession("broken")).toBeUndefined();
162- });
163-});
src/index.ts+20−1
@@ -1,19 +1,38 @@
11 import { basename } from "node:path";
2-import { handleArgv } from "./args.ts";
2+import { handleArgv, validateArgv } from "./args.ts";
33 import { getBranchLabel } from "./git.ts";
44 import { streamHeadless } from "./headless.ts";
55 import { initTUI } from "./tui.ts";
66 import type { TUIState } from "./types.ts";
77 import { updateMiniCoder } from "./update.ts";
88
9+function isHeadlessInvocation(argv: string[]): boolean {
10+ return argv.includes("--prompt") || argv.includes("-p");
11+}
12+
13+function requireInteractiveTerminal(): void {
14+ if (process.stdin.isTTY === true && process.stdout.isTTY === true) {
15+ return;
16+ }
17+
18+ throw new Error(
19+ "Interactive mode requires a TTY on stdin and stdout. Use --prompt or -p for headless mode.",
20+ );
21+}
22+
923 export async function main(): Promise<void> {
1024 const argv = process.argv.slice(2);
25+ validateArgv(argv);
1126
1227 if (argv.includes("--update")) {
1328 await updateMiniCoder();
1429 return;
1530 }
1631
32+ if (!isHeadlessInvocation(argv)) {
33+ requireInteractiveTerminal();
34+ }
35+
1736 const cwd = basename(process.cwd());
1837 const options = await handleArgv(argv);
1938
src/shared.ts+34−0
@@ -1,5 +1,6 @@
11 import { homedir } from "node:os";
22 import { join } from "node:path";
3+import type { Message } from "@earendil-works/pi-ai";
34 import { parseDocument } from "yaml";
45
56 // Mixed bag of helpers that can be shared across the codebase
@@ -79,10 +80,43 @@ export function takeTail<T>(arr: T[], x: number): T[] {
7980 return x <= 0 ? [] : arr.slice(-x);
8081 }
8182
83+function reportedContextTokens(message: Message): number | undefined {
84+ if (message.role !== "assistant" || !message.usage) return;
85+
86+ const { usage } = message;
87+ const reported = usage.totalTokens;
88+ if (Number.isFinite(reported) && reported > 0) {
89+ return Math.ceil(reported);
90+ }
91+
92+ const calculated =
93+ usage.input + usage.output + usage.cacheRead + usage.cacheWrite;
94+ if (Number.isFinite(calculated) && calculated > 0) {
95+ return Math.ceil(calculated);
96+ }
97+}
98+
8299 export function estimateTokens(text: string): number {
83100 return Math.ceil(text.length / 4);
84101 }
85102
103+export function estimateContextTokens(
104+ messages: Message[],
105+ fallbackSerializedContext = JSON.stringify(messages),
106+): number {
107+ for (let index = messages.length - 1; index >= 0; index--) {
108+ const providerTokens = reportedContextTokens(messages[index]);
109+ if (providerTokens === undefined) continue;
110+
111+ const trailingMessages = messages.slice(index + 1);
112+ if (trailingMessages.length === 0) return providerTokens;
113+
114+ return providerTokens + estimateTokens(JSON.stringify(trailingMessages));
115+ }
116+
117+ return estimateTokens(fallbackSerializedContext);
118+}
119+
86120 export function parseSkillFrontmatter(content: string) {
87121 const match = /^---\s*\n([\s\S]*?)\n---/.exec(content);
88122
src/themes.ts+0−6
@@ -198,12 +198,6 @@ export const TUI_THEMES = {
198198 },
199199 } as const satisfies Record<TUIThemeId, TUIThemeDefinition>;
200200
201-export const activeTuiTheme: Theme = { ...ansi16Palette };
202-
203-export function applyTUITheme(id: TUIThemeId): void {
204- Object.assign(activeTuiTheme, TUI_THEMES[id].palette);
205-}
206-
207201 export function getTUITheme(id: TUIThemeId): TUIThemeDefinition {
208202 return TUI_THEMES[id];
209203 }
src/tool-bash.ts+15−7
@@ -1,4 +1,9 @@
11 import { type Tool, Type } from "@earendil-works/pi-ai";
2+import {
3+ appendToolOutput,
4+ createToolOutputBuffer,
5+ renderToolOutput,
6+} from "./tool-output.ts";
27 import type { ToolRunnerEvent } from "./types";
38
49 const description = `Bash CLI tool
@@ -42,7 +47,7 @@ export async function* runBashTool(
4247 const decoder = new TextDecoder();
4348 const reader = proc.stdout.getReader();
4449
45- let output = "";
50+ const output = createToolOutputBuffer();
4651 while (true) {
4752 const { done, value } = await reader.read();
4853
@@ -50,8 +55,8 @@ export async function* runBashTool(
5055 const remaining = Bun.stripANSI(decoder.decode());
5156
5257 if (remaining.length) {
53- output += remaining;
54- yield { type: "output", text: remaining };
58+ const liveText = appendToolOutput(output, remaining);
59+ if (liveText) yield { type: "output", text: liveText };
5560 }
5661 break;
5762 }
@@ -59,14 +64,17 @@ export async function* runBashTool(
5964 const text = Bun.stripANSI(decoder.decode(value, { stream: true }));
6065
6166 if (text.length) {
62- output += text;
63- yield { type: "output", text: text };
67+ const liveText = appendToolOutput(output, text);
68+ if (liveText) yield { type: "output", text: liveText };
6469 }
6570 }
6671
6772 const exitCode = await proc.exited;
68-
69- const result = `${output.length ? output : "(no output)"}\n\nExit code: ${exitCode}`;
73+ if (!renderToolOutput(output)) {
74+ appendToolOutput(output, "(no output)");
75+ }
76+ appendToolOutput(output, `\n\nExit code: ${exitCode}`);
77+ const result = renderToolOutput(output);
7078
7179 yield {
7280 type: "result",
src/tool-output.ts+99−0
@@ -0,0 +1,99 @@
1+const MAX_TOOL_OUTPUT_CHARS = 16_000;
2+const TOOL_OUTPUT_TRUNCATION_MARKER = "\n\n... tool output truncated ...\n\n";
3+const TOOL_OUTPUT_PREVIEW_CHARS =
4+ MAX_TOOL_OUTPUT_CHARS - TOOL_OUTPUT_TRUNCATION_MARKER.length;
5+const TOOL_OUTPUT_HEAD_CHARS = Math.ceil((TOOL_OUTPUT_PREVIEW_CHARS * 2) / 3);
6+const TOOL_OUTPUT_TAIL_CHARS =
7+ TOOL_OUTPUT_PREVIEW_CHARS - TOOL_OUTPUT_HEAD_CHARS;
8+
9+type ToolOutputBuffer = {
10+ head: string;
11+ tail: string;
12+ streamedChars: number;
13+ streamTruncated: boolean;
14+ truncated: boolean;
15+};
16+
17+function splitsSurrogatePair(text: string, index: number): boolean {
18+ const previous = text.charCodeAt(index - 1);
19+ const next = text.charCodeAt(index);
20+ return (
21+ previous >= 0xd800 && previous <= 0xdbff && next >= 0xdc00 && next <= 0xdfff
22+ );
23+}
24+
25+function takePrefix(text: string, maxChars: number): string {
26+ if (text.length <= maxChars) return text;
27+
28+ const end = splitsSurrogatePair(text, maxChars) ? maxChars - 1 : maxChars;
29+ return text.slice(0, end);
30+}
31+
32+function takeSuffix(text: string, maxChars: number): string {
33+ if (text.length <= maxChars) return text;
34+
35+ const requestedStart = text.length - maxChars;
36+ const start = splitsSurrogatePair(text, requestedStart)
37+ ? requestedStart + 1
38+ : requestedStart;
39+ return text.slice(start);
40+}
41+
42+function limitStreamDelta(buffer: ToolOutputBuffer, text: string): string {
43+ if (buffer.streamTruncated) return "";
44+
45+ const remaining = TOOL_OUTPUT_PREVIEW_CHARS - buffer.streamedChars;
46+ if (text.length <= remaining) {
47+ buffer.streamedChars += text.length;
48+ return text;
49+ }
50+
51+ const prefix = takePrefix(text, remaining);
52+ buffer.streamedChars += prefix.length;
53+ buffer.streamTruncated = true;
54+ return `${prefix}${TOOL_OUTPUT_TRUNCATION_MARKER}`;
55+}
56+
57+export function createToolOutputBuffer(): ToolOutputBuffer {
58+ return {
59+ head: "",
60+ tail: "",
61+ streamedChars: 0,
62+ streamTruncated: false,
63+ truncated: false,
64+ };
65+}
66+
67+export function appendToolOutput(
68+ buffer: ToolOutputBuffer,
69+ text: string,
70+): string {
71+ const streamDelta = limitStreamDelta(buffer, text);
72+
73+ if (!buffer.truncated) {
74+ const combined = buffer.head + text;
75+ if (combined.length <= MAX_TOOL_OUTPUT_CHARS) {
76+ buffer.head = combined;
77+ return streamDelta;
78+ }
79+
80+ buffer.head = takePrefix(combined, TOOL_OUTPUT_HEAD_CHARS);
81+ buffer.tail = takeSuffix(combined, TOOL_OUTPUT_TAIL_CHARS);
82+ buffer.truncated = true;
83+ return streamDelta;
84+ }
85+
86+ buffer.tail = takeSuffix(`${buffer.tail}${text}`, TOOL_OUTPUT_TAIL_CHARS);
87+ return streamDelta;
88+}
89+
90+export function renderToolOutput(buffer: ToolOutputBuffer): string {
91+ if (!buffer.truncated) return buffer.head;
92+ return `${buffer.head}${TOOL_OUTPUT_TRUNCATION_MARKER}${buffer.tail}`;
93+}
94+
95+export function truncateToolOutput(text: string): string {
96+ const buffer = createToolOutputBuffer();
97+ appendToolOutput(buffer, text);
98+ return renderToolOutput(buffer);
99+}
src/tui-conversation.ts+1−0
@@ -198,6 +198,7 @@ export function Conversation(state: TUIState) {
198198
199199 return VStack(
200200 {
201+ stateKey: "conversation-scroll",
201202 flex: 1,
202203 gap: 2,
203204 overflow: "scroll",
src/tui-editor.ts+3−12
@@ -1,4 +1,4 @@
1-import { cel, Text, TextInput } from "@cel-tui/core";
1+import { Text, TextInput } from "@cel-tui/core";
22 import type { TUIState } from "./types";
33
44 export function Editor(
@@ -6,8 +6,8 @@ export function Editor(
66 onChange: (value: string) => void,
77 onKeyPress: (key: string) => void,
88 ) {
9- let isEditorFocused = !state.overlay;
109 return TextInput({
10+ stateKey: "prompt-input",
1111 value: state.prompt,
1212 minHeight: 3,
1313 maxHeight: 10,
@@ -15,15 +15,6 @@ export function Editor(
1515 placeholder: Text("Message...", { italic: true }),
1616 onChange,
1717 onKeyPress,
18- // TODO: Update `cel-tui` with an autofocus prop to fix this pattern
19- focused: isEditorFocused,
20- onFocus: () => {
21- isEditorFocused = true;
22- cel.render();
23- },
24- onBlur: () => {
25- isEditorFocused = false;
26- cel.render();
27- },
18+ autoFocus: true,
2819 });
2920 }
src/tui-overlay.ts+7−22
@@ -5,8 +5,8 @@ import { saveSettings } from "./args";
55 import { findModelConfig, getProviderModels } from "./models.ts";
66 import { getAvailableProviders } from "./oauth";
77 import { listSessionsForCwd } from "./session";
8-import { estimateTokens, formatTimestamp } from "./shared";
9-import { applyTUITheme, getTUITheme, TUI_THEME_IDS } from "./themes";
8+import { estimateContextTokens, formatTimestamp } from "./shared";
9+import { getTUITheme, TUI_THEME_IDS } from "./themes";
1010 import { TextPill, theme } from "./tui-components";
1111 import type { SelectOptions, SelectState, Session, TUIState } from "./types";
1212
@@ -19,7 +19,6 @@ export function SelectOverlay(
1919 onChange: (newValue: string) => void,
2020 onKeyPress: (key: string) => boolean | undefined,
2121 ) {
22- let isEditorFocused = true;
2322 return VStack(
2423 {
2524 height: "100%",
@@ -49,6 +48,7 @@ export function SelectOverlay(
4948 ]),
5049
5150 TextInput({
51+ stateKey: "menu-filter-input",
5252 value,
5353 minHeight: 3,
5454 maxHeight: 10,
@@ -61,13 +61,7 @@ export function SelectOverlay(
6161 bgColor: theme.white,
6262 onChange,
6363 onKeyPress,
64- focused: isEditorFocused,
65- onFocus: () => {
66- isEditorFocused = true;
67- },
68- onBlur: () => {
69- isEditorFocused = false;
70- },
64+ autoFocus: true,
7165 }),
7266 ],
7367 ),
@@ -392,14 +386,6 @@ export function mainMenu(state: TUIState, initialPane = "main") {
392386 state.overlay = false;
393387 };
394388
395- const requestThemeRefresh = () => {
396- state.forceThemeRefresh = true;
397- setTimeout(() => {
398- state.forceThemeRefresh = false;
399- cel.render();
400- }, 0);
401- };
402-
403389 const toTUIMessage = (msg: Message) => {
404390 const textFromContent = (
405391 content: string | { type: string; text?: string }[],
@@ -493,7 +479,7 @@ export function mainMenu(state: TUIState, initialPane = "main") {
493479 .filter((message) => message.role !== "toolResult")
494480 .map(toTUIMessage);
495481 state.prompt = "";
496- state.contextSize = estimateTokens(JSON.stringify(state.messages));
482+ state.contextSize = estimateContextTokens(state.messages);
497483 state.scrollOffset = 0;
498484 state.stickToBottom = true;
499485 closeMenu(s);
@@ -515,7 +501,7 @@ export function mainMenu(state: TUIState, initialPane = "main") {
515501 .filter((message) => message.role !== "toolResult")
516502 .map(toTUIMessage);
517503 state.prompt = "";
518- state.contextSize = estimateTokens(JSON.stringify(state.messages));
504+ state.contextSize = estimateContextTokens(state.messages);
519505 state.scrollOffset = 0;
520506 state.stickToBottom = true;
521507 closeMenu(s);
@@ -527,7 +513,7 @@ export function mainMenu(state: TUIState, initialPane = "main") {
527513 if (!nextTheme) return;
528514
529515 state.options.theme = nextTheme;
530- applyTUITheme(nextTheme);
516+ cel.setTheme(getTUITheme(nextTheme).palette);
531517 saveSettings({
532518 provider: state.options.provider,
533519 model: state.options.model.id,
@@ -535,7 +521,6 @@ export function mainMenu(state: TUIState, initialPane = "main") {
535521 customProviders: state.options.customProviders,
536522 theme: nextTheme,
537523 });
538- requestThemeRefresh();
539524 closeMenu(s);
540525 return;
541526 }
src/tui.ts+12−7
@@ -12,8 +12,12 @@ import {
1212 MAIN_PROMPT,
1313 } from "./prompt";
1414 import { updateSession } from "./session";
15-import { estimateTokens, formatTimestamp, secureRandomString } from "./shared";
16-import { activeTuiTheme, applyTUITheme, getTUITheme } from "./themes";
15+import {
16+ estimateContextTokens,
17+ formatTimestamp,
18+ secureRandomString,
19+} from "./shared";
20+import { getTUITheme } from "./themes";
1721 import { bash, runBashTool } from "./tool-bash";
1822 import { edit, runEditTool } from "./tool-edit";
1923 import { read, runReadTool } from "./tool-read";
@@ -117,8 +121,8 @@ export function initTUI(state: TUIState, leave: (s: string) => void) {
117121 }
118122 };
119123
120- applyTUITheme(state.options.theme);
121- cel.init(new ProcessTerminal(), { theme: activeTuiTheme });
124+ const initialTheme = getTUITheme(state.options.theme);
125+ cel.init(new ProcessTerminal(), { theme: initialTheme.palette });
122126 cel.viewport(() => {
123127 const activeTheme = getTUITheme(state.options.theme);
124128 const layers = [
@@ -130,7 +134,6 @@ export function initTUI(state: TUIState, leave: (s: string) => void) {
130134 onKeyPress: onWindowKeyPress,
131135 fgColor: activeTheme.rootFgColor,
132136 bgColor: activeTheme.rootBgColor,
133- italic: state.forceThemeRefresh,
134137 },
135138 [
136139 state.messages.length ? Conversation(state) : emptyState(state),
@@ -259,7 +262,8 @@ async function streamAgentTUI(state: TUIState) {
259262 ev.message,
260263 );
261264 const { systemPrompt, tools, messages } = ctx;
262- state.contextSize = estimateTokens(
265+ state.contextSize = estimateContextTokens(
266+ messages,
263267 JSON.stringify({ systemPrompt, tools, messages }),
264268 );
265269 break;
@@ -288,7 +292,8 @@ async function streamAgentTUI(state: TUIState) {
288292 }
289293
290294 const { systemPrompt, tools, messages } = ctx;
291- state.contextSize = estimateTokens(
295+ state.contextSize = estimateContextTokens(
296+ messages,
292297 JSON.stringify({ systemPrompt, tools, messages }),
293298 );
294299 }
src/types.ts+0−1
@@ -117,7 +117,6 @@ export type TUIState = {
117117 gitBranch?: string;
118118 availableUpdate?: AvailableUpdate | undefined;
119119 overlay?: boolean | undefined;
120- forceThemeRefresh?: boolean | undefined;
121120 sessionId?: string | undefined;
122121 };