@useavalon/caelence-agent 0.2.5-canary.0 → 0.2.5-canary.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/desktop/README.md CHANGED
@@ -8,7 +8,7 @@ Needs bun and Rust. System `rustup` works. A local toolchain can live in `deskto
8
8
 
9
9
  ```bash
10
10
  # from this repo, or any project
11
- bun bin/harness.ts desktop
11
+ bun run desktop
12
12
  ```
13
13
 
14
14
  Or:
@@ -23,4 +23,4 @@ Paste an OpenRouter key in Settings after the window opens. You do not need to e
23
23
 
24
24
  `HARNESS_CWD` is the project the agent opens.
25
25
 
26
- The Ink TUI: `bun bin/harness.ts` (same as `caelence`).
26
+ The Ink TUI: `bun start` or `caelence` from the package.
@@ -1,6 +1,7 @@
1
1
  import { spawn } from "node:child_process";
2
2
  import { existsSync } from "node:fs";
3
3
  import { resolve } from "node:path";
4
+ import { resolveAgentCwd } from "../../src/agent-cwd.ts";
4
5
 
5
6
  const desktopDir = resolve(import.meta.dir, "..");
6
7
  const cargoHome = resolve(desktopDir, ".toolchain/cargo");
@@ -13,8 +14,8 @@ if (existsSync(resolve(cargoBin, "cargo"))) {
13
14
  env.RUSTUP_HOME = rustupHome;
14
15
  env.PATH = `${cargoBin}:${env.PATH ?? ""}`;
15
16
  }
16
- if (!env.HARNESS_CWD) {
17
- env.HARNESS_CWD = resolve(desktopDir, "..");
17
+ if (!env.HARNESS_CWD?.trim()) {
18
+ env.HARNESS_CWD = resolveAgentCwd({ start: process.cwd() });
18
19
  }
19
20
 
20
21
  function run(bin: string, args: string[]): Promise<number> {
@@ -293,7 +293,24 @@ export function DeskMain(
293
293
  <header className="desk-bar">
294
294
  <div className="desk-id">
295
295
  <h1>{mainViewTitle(view)}</h1>
296
- {view === "chat" && desk.state.spend ? <p>{desk.state.spend}</p> : null}
296
+ {view === "chat" ? (
297
+ <p className="desk-project" title={desk.state.cwd}>
298
+ <button
299
+ type="button"
300
+ className="desk-project-btn"
301
+ onClick={() => {
302
+ void openLocalFile(desk.state.cwd, true, desk.bridge).catch((err: unknown) => {
303
+ desk.noticeError(err);
304
+ });
305
+ }}
306
+ >
307
+ {homePath(desk.state.cwd)}
308
+ </button>
309
+ {desk.state.spend ? (
310
+ <span className="desk-project-spend">{desk.state.spend}</span>
311
+ ) : null}
312
+ </p>
313
+ ) : null}
297
314
  </div>
298
315
  </header>
299
316
  <div className="desk-stage">
@@ -505,6 +505,38 @@ body {
505
505
  white-space: nowrap;
506
506
  }
507
507
 
508
+ .desk-project {
509
+ display: flex;
510
+ align-items: center;
511
+ gap: 8px;
512
+ min-width: 0;
513
+ }
514
+
515
+ .desk-project-btn {
516
+ margin: 0;
517
+ padding: 0;
518
+ border: 0;
519
+ background: transparent;
520
+ font: inherit;
521
+ color: inherit;
522
+ cursor: pointer;
523
+ overflow: hidden;
524
+ text-overflow: ellipsis;
525
+ white-space: nowrap;
526
+ min-width: 0;
527
+ flex: 1;
528
+ text-align: left;
529
+ }
530
+
531
+ .desk-project-btn:hover {
532
+ color: var(--text);
533
+ text-decoration: underline;
534
+ }
535
+
536
+ .desk-project-spend {
537
+ flex-shrink: 0;
538
+ }
539
+
508
540
  .desk-bar .cel-chip {
509
541
  padding: 3px 8px;
510
542
  gap: 6px;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@useavalon/caelence-agent",
3
- "version": "0.2.5-canary.0",
3
+ "version": "0.2.5-canary.2",
4
4
  "description": "Caelence agent: Strands turn loop, local tools, skills, evals, and a themable terminal UI.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -21,8 +21,7 @@
21
21
  },
22
22
  "bin": {
23
23
  "caelence": "./bin/harness.ts",
24
- "caelence-agent": "./bin/harness.ts",
25
- "harness": "./bin/harness.ts"
24
+ "caelence-agent": "./bin/harness.ts"
26
25
  },
27
26
  "exports": {
28
27
  ".": "./src/index.ts",
package/src/agent-cwd.ts CHANGED
@@ -1,9 +1,13 @@
1
- import { existsSync, mkdirSync } from "node:fs";
1
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
2
2
  import { homedir } from "node:os";
3
3
  import { dirname, resolve } from "node:path";
4
4
  import { resolveConfigPath } from "./config.ts";
5
5
  import { harnessHome } from "./desktop/secrets.ts";
6
6
 
7
+ export function lastProjectCwdPath(env: Record<string, string | undefined> = process.env): string {
8
+ return resolve(harnessHome(env), "last-project-cwd");
9
+ }
10
+
7
11
  export function standaloneWorkspaceDir(
8
12
  env: Record<string, string | undefined> = process.env,
9
13
  ): string {
@@ -23,6 +27,38 @@ function isProjectRoot(dir: string): boolean {
23
27
  return existsSync(resolve(dir, ".git"));
24
28
  }
25
29
 
30
+ function readLastProjectCwd(
31
+ env: Record<string, string | undefined> = process.env,
32
+ ): string | undefined {
33
+ const path = lastProjectCwdPath(env);
34
+ if (!existsSync(path)) return undefined;
35
+ try {
36
+ const line = readFileSync(path, "utf8").trim();
37
+ if (!line) return undefined;
38
+ const dir = resolve(line);
39
+ if (!existsSync(dir) || !isProjectRoot(dir)) return undefined;
40
+ return dir;
41
+ } catch {
42
+ return undefined;
43
+ }
44
+ }
45
+
46
+ /** Remember a git repo or harness.config root for the next standalone launch. */
47
+ export function rememberProjectCwd(
48
+ cwd: string,
49
+ env: Record<string, string | undefined> = process.env,
50
+ ): void {
51
+ const dir = resolve(cwd);
52
+ if (dir === resolve(standaloneWorkspaceDir(env))) return;
53
+ if (!isProjectRoot(dir)) return;
54
+ try {
55
+ mkdirSync(harnessHome(env), { recursive: true });
56
+ writeFileSync(lastProjectCwdPath(env), `${dir}\n`, "utf8");
57
+ } catch {
58
+ // best effort
59
+ }
60
+ }
61
+
26
62
  /** Nearest git repo or harness.config, not the home directory. */
27
63
  export function findProjectRoot(
28
64
  start: string,
@@ -38,7 +74,7 @@ export function findProjectRoot(
38
74
  }
39
75
  }
40
76
 
41
- /** `--cwd` wins. Else a project root from `start`. Else `~/.harness/workspace`. */
77
+ /** `--cwd` wins. Else a project root from `start`. Else last project, then `~/.harness/workspace`. */
42
78
  export function resolveAgentCwd(input: {
43
79
  start: string;
44
80
  explicit?: string;
@@ -46,6 +82,17 @@ export function resolveAgentCwd(input: {
46
82
  }): string {
47
83
  const env = input.env ?? process.env;
48
84
  const explicit = input.explicit?.trim();
49
- if (explicit) return resolve(explicit);
50
- return findProjectRoot(input.start, env) ?? ensureStandaloneWorkspace(env);
85
+ if (explicit) {
86
+ const dir = resolve(explicit);
87
+ rememberProjectCwd(dir, env);
88
+ return dir;
89
+ }
90
+ const found = findProjectRoot(input.start, env);
91
+ if (found) {
92
+ rememberProjectCwd(found, env);
93
+ return found;
94
+ }
95
+ const last = readLastProjectCwd(env);
96
+ if (last) return last;
97
+ return ensureStandaloneWorkspace(env);
51
98
  }
@@ -48,6 +48,23 @@ describe("resolveAgentCwd", () => {
48
48
  }
49
49
  });
50
50
 
51
+ test("falls back to the last project cwd before standalone workspace", async () => {
52
+ const last = await tempDir("agent-cwd-last-");
53
+ const harnessHome = await tempDir("agent-cwd-last-harness-");
54
+ const scratch = await tempDir("agent-cwd-last-scratch-");
55
+ try {
56
+ await mkdir(join(last, ".git"));
57
+ const env = { HOME: scratch, HARNESS_HOME: harnessHome };
58
+ const { rememberProjectCwd } = await import("../agent-cwd.ts");
59
+ rememberProjectCwd(last, env);
60
+ expect(resolveAgentCwd({ start: scratch, env })).toBe(last);
61
+ } finally {
62
+ await rm(last, { recursive: true, force: true });
63
+ await rm(harnessHome, { recursive: true, force: true });
64
+ await rm(scratch, { recursive: true, force: true });
65
+ }
66
+ });
67
+
51
68
  test("home is not a project even with .git", async () => {
52
69
  const home = await tempDir("agent-cwd-home-");
53
70
  const harnessHome = await tempDir("agent-cwd-home-harness-");
package/src/cli/main.ts CHANGED
@@ -41,8 +41,8 @@ function printHelp(): void {
41
41
  process.stdout.write(`${PRODUCT_NAME}
42
42
 
43
43
  Usage:
44
- ${CLI_NAME} Terminal UI
45
- ${CLI_NAME} desktop Open the desktop window
44
+ ${CLI_NAME} Terminal UI (same binary as caelence-agent)
45
+ ${CLI_NAME} desktop Desktop window (Tauri)
46
46
  ${CLI_NAME} chat -m <text> One-shot turn
47
47
  ${CLI_NAME} eval [name] Run an eval suite (list if omitted)
48
48
  ${CLI_NAME} experiment Run the local regression dataset (no Langfuse required)
@@ -114,13 +114,18 @@ function printEvent(event: AgentEvent): void {
114
114
  }
115
115
  }
116
116
 
117
- function desktopToolchainEnv(desktopDir: string, cwd: string): NodeJS.ProcessEnv {
117
+ function desktopToolchainEnv(
118
+ desktopDir: string,
119
+ cwd: string,
120
+ cwdExplicit: boolean,
121
+ ): NodeJS.ProcessEnv {
118
122
  const cargoHome = resolve(desktopDir, ".toolchain/cargo");
119
123
  const rustupHome = resolve(desktopDir, ".toolchain/rustup");
120
124
  const cargoBin = resolve(cargoHome, "bin");
121
125
  const bunDir = dirname(process.execPath);
122
126
  const systemCargoBin = resolve(homedir(), ".cargo/bin");
123
127
  const env: NodeJS.ProcessEnv = { ...process.env, HARNESS_CWD: cwd };
128
+ if (cwdExplicit) env.HARNESS_CWD_EXPLICIT = cwd;
124
129
  const pathDirs = [bunDir];
125
130
  if (existsSync(resolve(cargoBin, "cargo"))) {
126
131
  env.CARGO_HOME = cargoHome;
@@ -195,7 +200,8 @@ function agentCwd(parsed: ParsedCli): string {
195
200
  });
196
201
  }
197
202
 
198
- async function runDesktop(cwd: string): Promise<number> {
203
+ async function runDesktop(parsed: ParsedCli): Promise<number> {
204
+ const cwd = agentCwd(parsed);
199
205
  const desktopDir = resolve(fileURLToPath(new URL("../../desktop", import.meta.url)));
200
206
  if (!existsSync(resolve(desktopDir, "package.json"))) {
201
207
  process.stderr.write(
@@ -203,7 +209,7 @@ async function runDesktop(cwd: string): Promise<number> {
203
209
  );
204
210
  return 1;
205
211
  }
206
- const env = desktopToolchainEnv(desktopDir, cwd);
212
+ const env = desktopToolchainEnv(desktopDir, cwd, parsed.cwdExplicit);
207
213
  if (!hasCargo(env)) {
208
214
  process.stderr.write(
209
215
  "Rust is required for the desktop window. Install rustup from https://rustup.rs then run caelence desktop again.\n",
@@ -378,7 +384,7 @@ async function runExperiment(cwd: string): Promise<number> {
378
384
  async function runChat(parsed: ParsedCli, mode: AgentMode | undefined): Promise<number> {
379
385
  const message = parsed.message;
380
386
  if (!message) {
381
- process.stderr.write("harness chat -m <text>\n");
387
+ process.stderr.write(`${CLI_NAME} chat -m <text>\n`);
382
388
  return 1;
383
389
  }
384
390
  const tty = Boolean(process.stdin.isTTY);
@@ -430,7 +436,7 @@ const COMMANDS: Record<string, (parsed: ParsedCli) => Promise<number>> = {
430
436
  printHelp();
431
437
  return 0;
432
438
  },
433
- desktop: async (parsed) => runDesktop(agentCwd(parsed)),
439
+ desktop: async (parsed) => runDesktop(parsed),
434
440
  init: async (parsed) => runInit(parsed),
435
441
  skill: async (parsed) => runSkill(parsed.cwd, parsed.rest),
436
442
  eval: async (parsed) => runEval(parsed.cwd, parsed.rest),
@@ -461,10 +467,10 @@ export async function main(argv = process.argv.slice(2)): Promise<number> {
461
467
  const handler = COMMANDS[cmd];
462
468
  if (handler) return handler(parsed);
463
469
  if (cmd.startsWith("-")) {
464
- process.stderr.write(`Unknown flag ${cmd}. Try harness help.\n`);
470
+ process.stderr.write(`Unknown flag ${cmd}. Try ${CLI_NAME} help.\n`);
465
471
  return 1;
466
472
  }
467
- process.stderr.write(`Unknown command ${cmd}. Try harness help.\n`);
473
+ process.stderr.write(`Unknown command ${cmd}. Try ${CLI_NAME} help.\n`);
468
474
  return 1;
469
475
  }
470
476
 
@@ -1,11 +1,46 @@
1
1
  import { describe, expect, test } from "bun:test";
2
- import { CAELENCE_THEME, PLAIN_THEME, resolveTheme } from "./theme.ts";
2
+ import {
3
+ CAELENCE_LIGHT_THEME,
4
+ CAELENCE_THEME,
5
+ PLAIN_LIGHT_THEME,
6
+ PLAIN_THEME,
7
+ resolveTheme,
8
+ terminalPrefersLightBackground,
9
+ } from "./theme.ts";
3
10
 
4
11
  describe("theme", () => {
5
- test("defaults to caelence", () => {
6
- expect(resolveTheme(undefined).brand).toBe(CAELENCE_THEME.brand);
7
- expect(resolveTheme("plain").name).toBe(PLAIN_THEME.name);
8
- expect(resolveTheme({ ...CAELENCE_THEME, danger: "#ff0000" }).danger).toBe("#ff0000");
12
+ test("defaults to caelence on dark terminals", () => {
13
+ expect(resolveTheme(undefined, { COLORFGBG: "7;0" }).brand).toBe(CAELENCE_THEME.brand);
14
+ expect(resolveTheme("plain", { COLORFGBG: "7;0" }).name).toBe(PLAIN_THEME.name);
15
+ expect(
16
+ resolveTheme({ ...CAELENCE_THEME, danger: "#ff0000" }, { COLORFGBG: "7;0" }).danger,
17
+ ).toBe("#ff0000");
18
+ });
19
+
20
+ test("uses light palette when COLORFGBG reports a pale background", () => {
21
+ expect(resolveTheme(undefined, { COLORFGBG: "0;15" }).ink).toBe(CAELENCE_LIGHT_THEME.ink);
22
+ expect(resolveTheme("plain", { COLORFGBG: "0;15" }).ink).toBe(PLAIN_LIGHT_THEME.ink);
23
+ });
24
+
25
+ test("detects light background from COLORFGBG", () => {
26
+ expect(terminalPrefersLightBackground({ COLORFGBG: "0;15" })).toBe(true);
27
+ expect(terminalPrefersLightBackground({ COLORFGBG: "7;0" })).toBe(false);
28
+ expect(terminalPrefersLightBackground({})).toBe(false);
29
+ });
30
+
31
+ test("honors CAELENCE_LIGHT_TERMINAL override", () => {
32
+ expect(terminalPrefersLightBackground({ CAELENCE_LIGHT_TERMINAL: "1", COLORFGBG: "7;0" })).toBe(
33
+ true,
34
+ );
35
+ expect(
36
+ terminalPrefersLightBackground({ CAELENCE_LIGHT_TERMINAL: "0", COLORFGBG: "0;15" }),
37
+ ).toBe(false);
38
+ expect(resolveTheme(undefined, { CAELENCE_LIGHT_TERMINAL: "0", COLORFGBG: "0;15" }).ink).toBe(
39
+ CAELENCE_THEME.ink,
40
+ );
41
+ expect(resolveTheme(undefined, { CAELENCE_LIGHT_TERMINAL: "1", COLORFGBG: "7;0" }).ink).toBe(
42
+ CAELENCE_LIGHT_THEME.ink,
43
+ );
9
44
  });
10
45
 
11
46
  test("uses instrument blue instead of brand green", () => {
package/src/cli/theme.ts CHANGED
@@ -25,6 +25,19 @@ export const CAELENCE_THEME: Theme = {
25
25
  done: "#6e93b8",
26
26
  };
27
27
 
28
+ /** Light-terminal mapping (dark ink on pale backgrounds). */
29
+ export const CAELENCE_LIGHT_THEME: Theme = {
30
+ name: "caelence",
31
+ brand: "#3d5a73",
32
+ action: "#3d5a73",
33
+ ink: "#1a1814",
34
+ muted: "#5c574f",
35
+ line: "#8a847a",
36
+ danger: "#b54a3f",
37
+ warning: "#9a6b1a",
38
+ done: "#3d5a73",
39
+ };
40
+
28
41
  export const PLAIN_THEME: Theme = {
29
42
  name: "plain",
30
43
  brand: "blue",
@@ -37,8 +50,56 @@ export const PLAIN_THEME: Theme = {
37
50
  done: "blue",
38
51
  };
39
52
 
40
- export function resolveTheme(theme: ThemeName | Theme | undefined): Theme {
41
- if (!theme) return CAELENCE_THEME;
42
- if (typeof theme === "string") return theme === "plain" ? PLAIN_THEME : CAELENCE_THEME;
43
- return { ...CAELENCE_THEME, ...theme };
53
+ export const PLAIN_LIGHT_THEME: Theme = {
54
+ name: "plain",
55
+ brand: "blue",
56
+ action: "blue",
57
+ ink: "black",
58
+ muted: "gray",
59
+ line: "gray",
60
+ danger: "red",
61
+ warning: "yellow",
62
+ done: "blue",
63
+ };
64
+
65
+ /**
66
+ * Explicit CAELENCE_LIGHT_TERMINAL when set; otherwise undefined (auto-detect).
67
+ * `1` = pale terminal background (dark ink). `0` = dark background (pale ink).
68
+ */
69
+ export function readLightTerminalOverride(
70
+ env: Record<string, string | undefined>,
71
+ ): boolean | undefined {
72
+ const raw = env.CAELENCE_LIGHT_TERMINAL?.trim().toLowerCase();
73
+ if (raw === "1" || raw === "true" || raw === "yes") return true;
74
+ if (raw === "0" || raw === "false" || raw === "no") return false;
75
+ return undefined;
76
+ }
77
+
78
+ export function terminalPrefersLightBackground(
79
+ env: Record<string, string | undefined> = process.env,
80
+ ): boolean {
81
+ const override = readLightTerminalOverride(env);
82
+ if (override !== undefined) return override;
83
+ const raw = env.COLORFGBG?.trim();
84
+ if (!raw) return false;
85
+ const parts = raw.split(";").map((part) => Number.parseInt(part, 10));
86
+ const bg = parts.length >= 2 ? parts[1] : parts.at(-1);
87
+ if (bg === undefined || Number.isNaN(bg)) return false;
88
+ return bg >= 7;
89
+ }
90
+
91
+ function baseTheme(name: ThemeName, lightBackground: boolean): Theme {
92
+ if (name === "plain") return lightBackground ? PLAIN_LIGHT_THEME : PLAIN_THEME;
93
+ return lightBackground ? CAELENCE_LIGHT_THEME : CAELENCE_THEME;
94
+ }
95
+
96
+ export function resolveTheme(
97
+ theme: ThemeName | Theme | undefined,
98
+ env: Record<string, string | undefined> = process.env,
99
+ ): Theme {
100
+ const lightBackground = terminalPrefersLightBackground(env);
101
+ if (!theme) return baseTheme("caelence", lightBackground);
102
+ if (typeof theme === "string") return baseTheme(theme, lightBackground);
103
+ const base = baseTheme(theme.name === "plain" ? "plain" : "caelence", lightBackground);
104
+ return { ...base, ...theme };
44
105
  }
@@ -14,7 +14,7 @@ describe("tui layout", () => {
14
14
  });
15
15
 
16
16
  test("keeps a minimum transcript viewport", () => {
17
- expect(visibleTranscriptCount(12, 20)).toBe(3);
17
+ expect(visibleTranscriptCount(12, 20)).toBe(4);
18
18
  expect(visibleTranscriptCount(40, 10)).toBe(30);
19
19
  });
20
20
 
@@ -27,7 +27,9 @@ describe("tui layout", () => {
27
27
  });
28
28
 
29
29
  test("counts chrome for approval and pickers", () => {
30
- expect(tuiChromeRows({ approval: false, pickerCount: 0, slashCount: 0 })).toBe(5);
31
- expect(tuiChromeRows({ approval: true, pickerCount: 3, slashCount: 0 })).toBe(14);
30
+ expect(tuiChromeRows({ approval: false, pickerCount: 0, slashCount: 0 })).toBe(4);
31
+ expect(tuiChromeRows({ approval: true, pickerCount: 3, slashCount: 0, activity: true })).toBe(
32
+ 15,
33
+ );
32
34
  });
33
35
  });
@@ -2,8 +2,17 @@ export function sidebarWidth(columns: number): number {
2
2
  return columns >= 88 ? 26 : 0;
3
3
  }
4
4
 
5
+ export const TUI_HEADER_ROWS = 2;
6
+ export const TUI_COMPOSER_ROWS = 2;
7
+ export const TUI_ACTIVITY_ROWS = 1;
8
+
9
+ export function visibleTranscriptRows(rows: number, chromeRows: number): number {
10
+ return Math.max(4, rows - Math.max(0, chromeRows));
11
+ }
12
+
13
+ /** @deprecated Line count; prefer visibleTranscriptRows with row-budget clipping. */
5
14
  export function visibleTranscriptCount(rows: number, chromeRows: number): number {
6
- return Math.max(3, rows - Math.max(0, chromeRows));
15
+ return visibleTranscriptRows(rows, chromeRows);
7
16
  }
8
17
 
9
18
  export function takeVisibleLines<T>(lines: T[], count: number): T[] {
@@ -22,12 +31,12 @@ export function tuiChromeRows(input: {
22
31
  approval: boolean;
23
32
  pickerCount: number;
24
33
  slashCount: number;
34
+ activity?: boolean;
25
35
  }): number {
26
- return (
27
- 3 +
28
- 2 +
29
- (input.approval ? 4 : 0) +
30
- (input.pickerCount > 0 ? Math.min(input.pickerCount, 8) + 2 : 0) +
31
- (input.slashCount > 0 ? Math.min(input.slashCount, 8) + 1 : 0)
32
- );
36
+ let rows = TUI_HEADER_ROWS + TUI_COMPOSER_ROWS;
37
+ if (input.activity) rows += TUI_ACTIVITY_ROWS;
38
+ if (input.approval) rows += 5;
39
+ if (input.pickerCount > 0) rows += Math.min(input.pickerCount, 8) + 2;
40
+ if (input.slashCount > 0) rows += Math.min(input.slashCount, 8) + 1;
41
+ return rows;
33
42
  }
@@ -0,0 +1,44 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { turnProgressLabel, warmupHeadline } from "./tui-progress.ts";
3
+
4
+ describe("tui progress", () => {
5
+ test("rotates warmup headlines", () => {
6
+ expect(warmupHeadline(0)).toBe("Thinking");
7
+ expect(warmupHeadline(1)).toBe("Looking this over");
8
+ });
9
+
10
+ test("shows warmup before tools or text", () => {
11
+ expect(turnProgressLabel({ tools: [], assistantStarted: false, tick: 0, warmupSeed: 0 })).toBe(
12
+ "Thinking",
13
+ );
14
+ });
15
+
16
+ test("shows tool activity while running", () => {
17
+ expect(
18
+ turnProgressLabel({
19
+ tools: [
20
+ {
21
+ callId: "1",
22
+ name: "read_file",
23
+ status: "running",
24
+ preview: "src/index.ts",
25
+ },
26
+ ],
27
+ assistantStarted: false,
28
+ tick: 1,
29
+ warmupSeed: 0,
30
+ }),
31
+ ).toBe("Reading src/index.ts.");
32
+ });
33
+
34
+ test("shows writing once assistant text starts", () => {
35
+ expect(
36
+ turnProgressLabel({
37
+ tools: [],
38
+ assistantStarted: true,
39
+ tick: 2,
40
+ warmupSeed: 0,
41
+ }),
42
+ ).toBe("Writing..");
43
+ });
44
+ });
@@ -0,0 +1,55 @@
1
+ import { clipLabel } from "./tui-layout.ts";
2
+ import type { TurnToolLine } from "./tui-stream.ts";
3
+
4
+ export const WARMUP_PHRASES = ["Thinking", "Looking this over", "Working it out"] as const;
5
+
6
+ const TICK_FRAMES = ["", ".", "..", "..."] as const;
7
+
8
+ export function warmupHeadline(seed: number): string {
9
+ return WARMUP_PHRASES[Math.abs(seed) % WARMUP_PHRASES.length] ?? "Thinking";
10
+ }
11
+
12
+ const TOOL_ACTIVITY_RULES: ReadonlyArray<{
13
+ match: RegExp;
14
+ verb: string;
15
+ previewWidth: number;
16
+ }> = [
17
+ { match: /read_file|read/i, verb: "Reading", previewWidth: 48 },
18
+ { match: /grep|glob|search/i, verb: "Searching", previewWidth: 40 },
19
+ { match: /write_file|edit_file|str_replace|apply_patch/i, verb: "Editing", previewWidth: 48 },
20
+ { match: /exec|shell|bash/i, verb: "Running", previewWidth: 40 },
21
+ { match: /git_/i, verb: "Git", previewWidth: 40 },
22
+ ];
23
+
24
+ function labelWithPreview(verb: string, preview: string, width: number): string {
25
+ return preview ? `${verb} ${clipLabel(preview, width)}` : verb;
26
+ }
27
+
28
+ function toolActivityLabel(tool: TurnToolLine): string {
29
+ const preview = tool.preview.trim();
30
+ const rule = TOOL_ACTIVITY_RULES.find((entry) => entry.match.test(tool.name));
31
+ if (rule) return labelWithPreview(rule.verb, preview, rule.previewWidth);
32
+ return preview ? `${tool.name} ${clipLabel(preview, 40)}` : tool.name;
33
+ }
34
+
35
+ export function turnProgressLabel(input: {
36
+ tools: readonly TurnToolLine[];
37
+ assistantStarted: boolean;
38
+ tick: number;
39
+ warmupSeed: number;
40
+ }): string {
41
+ if (input.assistantStarted) {
42
+ const frame = TICK_FRAMES[input.tick % TICK_FRAMES.length] ?? "";
43
+ return `Writing${frame}`;
44
+ }
45
+ const running = [...input.tools].reverse().find((tool) => tool.status === "running");
46
+ if (running) {
47
+ const frame = TICK_FRAMES[input.tick % TICK_FRAMES.length] ?? "";
48
+ return `${toolActivityLabel(running)}${frame}`;
49
+ }
50
+ if (input.tools.length > 0) {
51
+ return `${warmupHeadline(input.warmupSeed)}…`;
52
+ }
53
+ const frame = TICK_FRAMES[input.tick % TICK_FRAMES.length] ?? "";
54
+ return `${warmupHeadline(input.warmupSeed)}${frame}`;
55
+ }
@@ -0,0 +1,26 @@
1
+ export type ToolStatus = "running" | "ok" | "fail";
2
+
3
+ export type StreamLine =
4
+ | { key: string; type: "user"; text: string }
5
+ | { key: string; type: "assistant"; text: string }
6
+ | { key: string; type: "thought"; text: string }
7
+ | {
8
+ key: string;
9
+ type: "tool";
10
+ name: string;
11
+ callId: string;
12
+ status: ToolStatus;
13
+ preview: string;
14
+ error?: string;
15
+ }
16
+ | { key: string; type: "system"; text: string }
17
+ | { key: string; type: "error"; text: string }
18
+ | { key: string; type: "todos"; text: string };
19
+
20
+ export type TurnToolLine = {
21
+ callId: string;
22
+ name: string;
23
+ status: ToolStatus;
24
+ preview: string;
25
+ error?: string;
26
+ };
@@ -0,0 +1,19 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { detectLightTerminalBackground, luminanceFromOsc11 } from "./tui-terminal-probe.ts";
3
+
4
+ describe("tui terminal probe", () => {
5
+ test("parses OSC 11 rgb background", () => {
6
+ expect(luminanceFromOsc11("rgb:ffff/ffff/ffff")).toBeCloseTo(1, 1);
7
+ expect(luminanceFromOsc11("rgb:0000/0000/0000")).toBeCloseTo(0, 1);
8
+ });
9
+
10
+ test("parses hex background", () => {
11
+ expect(luminanceFromOsc11("#ffffff")).toBeCloseTo(1, 1);
12
+ expect(luminanceFromOsc11("1a1a1a")).toBeCloseTo(0.1, 1);
13
+ });
14
+
15
+ test("defaults to light palette when detection is inconclusive", async () => {
16
+ expect(await detectLightTerminalBackground({})).toBe(true);
17
+ expect(await detectLightTerminalBackground({ CAELENCE_LIGHT_TERMINAL: "0" })).toBe(false);
18
+ });
19
+ });