warden-agent 0.0.0-stage → 0.4.0

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.
@@ -0,0 +1,226 @@
1
+ // Coding tools the model can call. Each tool: { name, description, parameters (JSON schema), kind, run(args, ctx) }.
2
+ // kind drives permissions: "read" (free inside the project), "edit" (changes files), "bash" (runs commands).
3
+ // run() returns { content: string for the model, display: { title, lines?, diff? } for the terminal }.
4
+ import fs from "node:fs";
5
+ import path from "node:path";
6
+ import { spawn } from "node:child_process";
7
+
8
+ const MAX_OUT = 40_000;
9
+ const SKIP_DIRS = new Set([".git", "node_modules", ".hg", ".svn", "dist", "build", ".next", ".venv", "venv", "__pycache__", ".cache", "target", ".idea"]);
10
+
11
+ const clip = (s, n = MAX_OUT) => (s.length > n ? s.slice(0, n) + `\n[... ${s.length - n} more characters cut]` : s);
12
+ export const resolveIn = (cwd, p) => path.resolve(cwd, String(p ?? "."));
13
+ export const isInside = (cwd, abs) => { const rel = path.relative(cwd, abs); return rel === "" || (!rel.startsWith("..") && !path.isAbsolute(rel)); };
14
+ const relOf = (cwd, abs) => (isInside(cwd, abs) ? path.relative(cwd, abs) || "." : abs);
15
+ const isBinary = (buf) => buf.subarray(0, 8000).includes(0);
16
+
17
+ function walk(root, onFile, limit = 20000) {
18
+ let n = 0;
19
+ const stack = [root];
20
+ while (stack.length && n < limit) {
21
+ const dir = stack.pop();
22
+ let entries;
23
+ try { entries = fs.readdirSync(dir, { withFileTypes: true }); } catch { continue; }
24
+ for (const e of entries) {
25
+ const full = path.join(dir, e.name);
26
+ if (e.isDirectory()) { if (!SKIP_DIRS.has(e.name)) stack.push(full); }
27
+ else if (e.isFile()) { n++; if (onFile(full) === false) return; }
28
+ }
29
+ }
30
+ }
31
+
32
+ export function globToRegex(glob) {
33
+ let re = "", i = 0;
34
+ const g = glob.replace(/\\/g, "/");
35
+ while (i < g.length) {
36
+ const c = g[i];
37
+ if (c === "*") {
38
+ if (g[i + 1] === "*") { re += g[i + 2] === "/" ? "(?:.*/)?" : ".*"; i += g[i + 2] === "/" ? 3 : 2; continue; }
39
+ re += "[^/]*";
40
+ } else if (c === "?") re += "[^/]";
41
+ else if (c === "{") { const end = g.indexOf("}", i); if (end > i) { re += "(?:" + g.slice(i + 1, end).split(",").map((s) => s.replace(/[.+^$()|[\]\\]/g, "\\$&").replace(/\*/g, "[^/]*")).join("|") + ")"; i = end + 1; continue; } re += "\\{"; }
42
+ else re += c.replace(/[.+^$()|[\]\\}]/g, "\\$&");
43
+ i++;
44
+ }
45
+ return new RegExp("^" + re + "$", process.platform === "win32" ? "i" : "");
46
+ }
47
+
48
+ // ---- shell -----------------------------------------------------------------
49
+
50
+ export function detectShell() {
51
+ if (process.platform !== "win32") return { cmd: fs.existsSync("/bin/bash") ? "/bin/bash" : "/bin/sh", args: (c) => ["-c", c], name: "bash" };
52
+ for (const p of [process.env.WARDEN_BASH, "C:\\Program Files\\Git\\bin\\bash.exe", "C:\\Program Files (x86)\\Git\\bin\\bash.exe"]) {
53
+ if (p && fs.existsSync(p)) return { cmd: p, args: (c) => ["-c", c], name: "bash (Git Bash on Windows)" };
54
+ }
55
+ return { cmd: "powershell.exe", args: (c) => ["-NoProfile", "-NonInteractive", "-Command", c], name: "PowerShell (Windows)" };
56
+ }
57
+
58
+ function runShell(command, { cwd, timeoutMs, signal }) {
59
+ const sh = detectShell();
60
+ return new Promise((resolve) => {
61
+ const p = spawn(sh.cmd, sh.args(command), { cwd, windowsHide: true, env: { ...process.env, WARDEN: "1" } });
62
+ let out = "", killed = null;
63
+ const kill = (why) => { killed = why; try { p.kill(); } catch {} };
64
+ const t = setTimeout(() => kill("timeout"), timeoutMs);
65
+ const onAbort = () => kill("interrupted");
66
+ signal?.addEventListener("abort", onAbort, { once: true });
67
+ const add = (d) => { if (out.length < MAX_OUT * 2) out += d; };
68
+ p.stdout.on("data", add); p.stderr.on("data", add);
69
+ p.on("error", (e) => { clearTimeout(t); resolve({ code: -1, out: `failed to start shell: ${e.message}`, killed }); });
70
+ p.on("close", (code) => { clearTimeout(t); signal?.removeEventListener("abort", onAbort); resolve({ code, out, killed }); });
71
+ p.stdin.end();
72
+ });
73
+ }
74
+
75
+ // ---- diff for display ------------------------------------------------------
76
+
77
+ function lineDiff(oldText, newText, startLine) {
78
+ const a = oldText.split("\n"), b = newText.split("\n");
79
+ const lines = [];
80
+ a.forEach((l, i) => lines.push({ t: "-", n: startLine + i, s: l }));
81
+ b.forEach((l, i) => lines.push({ t: "+", n: startLine + i, s: l }));
82
+ return lines;
83
+ }
84
+
85
+ // ---- tools -----------------------------------------------------------------
86
+
87
+ export const TOOLS = [
88
+ {
89
+ name: "read_file", kind: "read",
90
+ description: "Read a text file. Returns numbered lines (`N\\tline`). Use offset/limit for large files. Always read a file before editing it.",
91
+ parameters: { type: "object", properties: { path: { type: "string", description: "File path, relative to the project root or absolute" }, offset: { type: "integer", description: "1-based line to start at" }, limit: { type: "integer", description: "Max lines (default 2000)" } }, required: ["path"] },
92
+ paths: (a) => [a.path],
93
+ run(a, { cwd }) {
94
+ const abs = resolveIn(cwd, a.path);
95
+ const buf = fs.readFileSync(abs);
96
+ if (isBinary(buf)) throw new Error("file looks binary; not shown");
97
+ const all = buf.toString("utf8").replace(/\r\n/g, "\n").split("\n");
98
+ const start = Math.max(1, a.offset || 1), lim = Math.max(1, Math.min(a.limit || 2000, 5000));
99
+ const slice = all.slice(start - 1, start - 1 + lim);
100
+ const body = slice.map((l, i) => `${start + i}\t${l.length > 2000 ? l.slice(0, 2000) + "…" : l}`).join("\n");
101
+ const more = start - 1 + lim < all.length ? `\n[file has ${all.length} lines; showing ${start}-${start + slice.length - 1}]` : "";
102
+ return { content: clip(body + more) || "(empty file)", display: { title: `Read ${relOf(cwd, abs)}`, lines: [`${slice.length} lines`] } };
103
+ },
104
+ },
105
+ {
106
+ name: "write_file", kind: "edit",
107
+ description: "Create a file or completely replace its contents. Prefer edit_file for changing existing files.",
108
+ parameters: { type: "object", properties: { path: { type: "string" }, content: { type: "string" } }, required: ["path", "content"] },
109
+ paths: (a) => [a.path],
110
+ run(a, { cwd }) {
111
+ const abs = resolveIn(cwd, a.path);
112
+ const existed = fs.existsSync(abs);
113
+ fs.mkdirSync(path.dirname(abs), { recursive: true });
114
+ fs.writeFileSync(abs, a.content ?? "");
115
+ const n = String(a.content ?? "").split("\n").length;
116
+ return { content: `${existed ? "Overwrote" : "Created"} ${relOf(cwd, abs)} (${n} lines)`, display: { title: `${existed ? "Overwrite" : "Create"} ${relOf(cwd, abs)}`, diff: String(a.content ?? "").split("\n").slice(0, 40).map((s, i) => ({ t: "+", n: i + 1, s })), lines: n > 40 ? [`… ${n - 40} more lines`] : [] } };
117
+ },
118
+ },
119
+ {
120
+ name: "edit_file", kind: "edit",
121
+ description: "Replace an exact string in a file. old_string must match the file exactly (including indentation) and be unique unless replace_all is true. Read the file first.",
122
+ parameters: { type: "object", properties: { path: { type: "string" }, old_string: { type: "string" }, new_string: { type: "string" }, replace_all: { type: "boolean" } }, required: ["path", "old_string", "new_string"] },
123
+ paths: (a) => [a.path],
124
+ run(a, { cwd }) {
125
+ const abs = resolveIn(cwd, a.path);
126
+ if (!fs.existsSync(abs)) throw new Error(`file not found: ${relOf(cwd, abs)} (use write_file to create it)`);
127
+ let text = fs.readFileSync(abs, "utf8");
128
+ let oldS = String(a.old_string), newS = String(a.new_string ?? "");
129
+ if (!oldS) throw new Error("old_string is empty; use write_file to create or replace a whole file");
130
+ // Windows files: let the model write \n while the file uses \r\n
131
+ if (text.includes("\r\n") && !oldS.includes("\r\n")) { oldS = oldS.replace(/\n/g, "\r\n"); newS = newS.replace(/\n/g, "\r\n"); }
132
+ const count = text.split(oldS).length - 1;
133
+ if (count === 0) throw new Error("old_string was not found in the file. Re-read the file and copy the exact text, including whitespace.");
134
+ if (count > 1 && !a.replace_all) throw new Error(`old_string appears ${count} times; add more surrounding context to make it unique, or set replace_all`);
135
+ const startLine = text.slice(0, text.indexOf(oldS)).split("\n").length;
136
+ text = a.replace_all ? text.split(oldS).join(newS) : text.replace(oldS, () => newS);
137
+ fs.writeFileSync(abs, text);
138
+ const norm = (s) => s.replace(/\r\n/g, "\n");
139
+ return { content: `Edited ${relOf(cwd, abs)} (${a.replace_all ? count : 1} replacement${count > 1 && a.replace_all ? "s" : ""})`, display: { title: `Edit ${relOf(cwd, abs)}`, diff: lineDiff(norm(oldS), norm(newS), startLine).slice(0, 60) } };
140
+ },
141
+ },
142
+ {
143
+ name: "list_dir", kind: "read",
144
+ description: "List a directory (folders end with /).",
145
+ parameters: { type: "object", properties: { path: { type: "string", description: "Directory, default the project root" } } },
146
+ paths: (a) => [a.path || "."],
147
+ run(a, { cwd }) {
148
+ const abs = resolveIn(cwd, a.path || ".");
149
+ const items = fs.readdirSync(abs, { withFileTypes: true }).filter((e) => e.name !== ".git").sort((x, y) => (y.isDirectory() - x.isDirectory()) || x.name.localeCompare(y.name)).map((e) => e.name + (e.isDirectory() ? "/" : ""));
150
+ return { content: clip(items.slice(0, 1000).join("\n") + (items.length > 1000 ? `\n[${items.length - 1000} more]` : "")) || "(empty)", display: { title: `List ${relOf(cwd, abs)}`, lines: [`${items.length} entries`] } };
151
+ },
152
+ },
153
+ {
154
+ name: "glob", kind: "read",
155
+ description: "Find files by glob pattern, e.g. **/*.ts or src/**/test_*.py. Skips .git, node_modules and build folders.",
156
+ parameters: { type: "object", properties: { pattern: { type: "string" }, path: { type: "string", description: "Folder to search, default the project root" } }, required: ["pattern"] },
157
+ paths: (a) => [a.path || "."],
158
+ run(a, { cwd }) {
159
+ const root = resolveIn(cwd, a.path || ".");
160
+ const re = globToRegex(a.pattern);
161
+ const hits = [];
162
+ walk(root, (f) => { const rel = path.relative(root, f).replace(/\\/g, "/"); if (re.test(rel)) hits.push(rel); if (hits.length >= 500) return false; });
163
+ return { content: hits.length ? hits.sort().join("\n") + (hits.length >= 500 ? "\n[stopped at 500 results]" : "") : "no files matched", display: { title: `Glob ${a.pattern}`, lines: [`${hits.length} files`] } };
164
+ },
165
+ },
166
+ {
167
+ name: "grep", kind: "read",
168
+ description: "Search file contents with a regular expression (JavaScript syntax). Returns path:line: text.",
169
+ parameters: { type: "object", properties: { pattern: { type: "string" }, path: { type: "string", description: "File or folder, default the project root" }, glob: { type: "string", description: "Only files matching this glob, e.g. **/*.py" }, ignore_case: { type: "boolean" } }, required: ["pattern"] },
170
+ paths: (a) => [a.path || "."],
171
+ run(a, { cwd }) {
172
+ const root = resolveIn(cwd, a.path || ".");
173
+ let re;
174
+ try { re = new RegExp(a.pattern, a.ignore_case ? "i" : ""); } catch (e) { throw new Error(`invalid regex: ${e.message}`); }
175
+ const only = a.glob ? globToRegex(a.glob) : null;
176
+ const hits = [];
177
+ const scan = (f) => {
178
+ const rel = path.relative(cwd, f).replace(/\\/g, "/") || path.basename(f);
179
+ if (only && !only.test(path.relative(root, f).replace(/\\/g, "/"))) return;
180
+ let buf; try { const st = fs.statSync(f); if (st.size > 1_000_000) return; buf = fs.readFileSync(f); } catch { return; }
181
+ if (isBinary(buf)) return;
182
+ const lines = buf.toString("utf8").split(/\r?\n/);
183
+ for (let i = 0; i < lines.length; i++) if (re.test(lines[i])) { hits.push(`${rel}:${i + 1}: ${lines[i].slice(0, 300)}`); if (hits.length >= 300) return false; }
184
+ };
185
+ if (fs.statSync(root).isFile()) scan(root); else walk(root, scan);
186
+ return { content: hits.length ? clip(hits.join("\n")) + (hits.length >= 300 ? "\n[stopped at 300 matches]" : "") : "no matches", display: { title: `Grep ${a.pattern}`, lines: [`${hits.length} matches`] } };
187
+ },
188
+ },
189
+ {
190
+ name: "bash", kind: "bash",
191
+ description: "Run a shell command in the project root and return its combined stdout/stderr and exit code. Use for tests, builds, git, installing packages. Not for reading or editing files (use the file tools).",
192
+ parameters: { type: "object", properties: { command: { type: "string" }, timeout_ms: { type: "integer", description: "Default 120000, max 600000" } }, required: ["command"] },
193
+ paths: () => [],
194
+ async run(a, { cwd, signal }) {
195
+ const timeoutMs = Math.min(Math.max(a.timeout_ms || 120_000, 1000), 600_000);
196
+ const r = await runShell(String(a.command), { cwd, timeoutMs, signal });
197
+ const status = r.killed === "timeout" ? `timed out after ${timeoutMs} ms` : r.killed === "interrupted" ? "interrupted by the user" : `exit code ${r.code}`;
198
+ const out = r.out.trim();
199
+ const lines = out ? out.split("\n") : [];
200
+ const shown = lines.length > 12 ? [...lines.slice(0, 6), `… ${lines.length - 10} more lines`, ...lines.slice(-4)] : lines;
201
+ return { content: clip(`${out || "(no output)"}\n[${status}]`), display: { title: `Bash ${a.command.length > 80 ? a.command.slice(0, 80) + "…" : a.command}`, lines: [...shown, `[${status}]`] }, isError: r.code !== 0 };
202
+ },
203
+ },
204
+ ];
205
+
206
+ export const toolByName = (n) => TOOLS.find((t) => t.name === n);
207
+
208
+ // ---- permissions -----------------------------------------------------------
209
+ // modes: "ask" (edits and commands need approval), "auto-edit" (edits inside the project are automatic), "yolo" (everything automatic)
210
+ export const MODES = ["ask", "auto-edit", "yolo"];
211
+
212
+ export function needsApproval(tool, args, { cwd, mode, allowed }) {
213
+ if (mode === "yolo") return false;
214
+ const outside = (tool.paths?.(args) || []).some((p) => !isInside(cwd, resolveIn(cwd, p)));
215
+ if (allowed.has(tool.name) && !outside) return false;
216
+ if (tool.kind === "read") return outside; // reading outside the project needs approval
217
+ if (tool.kind === "edit") return mode !== "auto-edit" || outside;
218
+ return true; // bash
219
+ }
220
+
221
+ export function describeCall(tool, args, cwd) {
222
+ if (!tool) return "unknown tool";
223
+ if (tool.name === "bash") return `run: ${args.command}`;
224
+ const p = args.path ? relOf(cwd, resolveIn(cwd, args.path)) : ".";
225
+ return `${tool.name.replace("_", " ")} ${p}${tool.name === "grep" || tool.name === "glob" ? ` (${args.pattern})` : ""}`;
226
+ }
@@ -0,0 +1,62 @@
1
+ // Update notice: at most once a day Warden asks the npm registry for the latest version number of warden-agent.
2
+ // Nothing about you or your code is sent; it is a plain GET of a public URL. Turn it off with
3
+ // WARDEN_NO_UPDATE_CHECK=1 or "updateCheck": false under "agent" in ~/.warden/config.json.
4
+ import fs from "node:fs";
5
+ import path from "node:path";
6
+ import { spawn } from "node:child_process";
7
+ import { isSea } from "node:sea";
8
+ import { HOME } from "./config.mjs";
9
+ import { VERSION, PACKAGE, REPO } from "../version.mjs";
10
+
11
+ const FILE = path.join(HOME, "update.json");
12
+ const DAY = 24 * 3600 * 1000;
13
+
14
+ export function newer(a, b) { // is a newer than b? (x.y.z)
15
+ const pa = String(a).split(".").map(Number), pb = String(b).split(".").map(Number);
16
+ for (let i = 0; i < 3; i++) { if ((pa[i] || 0) !== (pb[i] || 0)) return (pa[i] || 0) > (pb[i] || 0); }
17
+ return false;
18
+ }
19
+
20
+ export const updateCheckEnabled = (cfg) => !process.env.WARDEN_NO_UPDATE_CHECK && cfg.updateCheck !== false;
21
+
22
+ function read() { try { return JSON.parse(fs.readFileSync(FILE, "utf8")); } catch { return {}; } }
23
+
24
+ // Returns the newer version seen on the last check (or null), and refreshes in the background when due.
25
+ export function updateNotice(cfg) {
26
+ if (!updateCheckEnabled(cfg)) return null;
27
+ const st = read();
28
+ if (!st.checked || Date.now() - st.checked > DAY) refresh().catch(() => {});
29
+ return st.latest && newer(st.latest, VERSION) ? st.latest : null;
30
+ }
31
+
32
+ export async function fetchLatest(timeoutMs = 4000) {
33
+ const r = await fetch(`https://registry.npmjs.org/${PACKAGE}/latest`, { signal: AbortSignal.timeout(timeoutMs), headers: { accept: "application/json" } });
34
+ if (!r.ok) throw new Error(`registry answered ${r.status}`);
35
+ return (await r.json()).version;
36
+ }
37
+
38
+ async function refresh() {
39
+ let latest = null;
40
+ try { latest = await fetchLatest(); } catch {}
41
+ try { fs.mkdirSync(HOME, { recursive: true }); fs.writeFileSync(FILE, JSON.stringify({ checked: Date.now(), latest })); } catch {}
42
+ }
43
+
44
+ // Standalone builds (warden.exe) cannot update through npm.
45
+ export const isStandalone = () => { try { return isSea(); } catch { return false; } };
46
+
47
+ export async function runUpdate() {
48
+ let latest;
49
+ try { latest = await fetchLatest(8000); } catch (e) { console.error(`Could not reach the npm registry: ${e.message}`); return 1; }
50
+ if (!newer(latest, VERSION)) { console.log(`Warden ${VERSION} is the latest version.`); return 0; }
51
+ if (isStandalone()) {
52
+ console.log(`Warden ${latest} is available (you have ${VERSION}). Download it from https://github.com/${REPO}/releases/latest`);
53
+ return 0;
54
+ }
55
+ console.log(`Updating Warden ${VERSION} -> ${latest} with: npm install -g ${PACKAGE}@latest`);
56
+ const npm = process.platform === "win32" ? "npm.cmd" : "npm";
57
+ return new Promise((res) => {
58
+ const p = spawn(npm, ["install", "-g", `${PACKAGE}@latest`], { stdio: "inherit", shell: process.platform === "win32" });
59
+ p.on("close", (code) => res(code ?? 1));
60
+ p.on("error", (e) => { console.error(`Could not run npm: ${e.message}`); res(1); });
61
+ });
62
+ }
package/src/cli.mjs ADDED
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/env node
2
+ // warden interactive coding agent in this folder
3
+ // warden "fix the tests" same, starting with that message
4
+ // warden -p "task" one-shot, no interface (answer on stdout)
5
+ // warden doctor | update | --version
6
+ // warden daemon|watch|review|arena|... the supervisor tools (watch and review Claude Code, compare models)
7
+ // (No top-level await here: the standalone build bundles this file as CommonJS.)
8
+ import { VERSION } from "./version.mjs";
9
+
10
+ const SUPERVISOR = new Set(["daemon", "watch", "sessions", "replay", "diff", "review", "reviews", "arena", "harnesses"]);
11
+
12
+ const HELP = `Warden ${VERSION}: a coding agent for any model, in your terminal.
13
+
14
+ Usage:
15
+ warden start a conversation in this folder (first run walks you through getting a free key)
16
+ warden "message" start with a message
17
+ warden -p "message" answer once and exit (for scripts); add --json for JSON output
18
+ warden --continue continue the last conversation in this folder
19
+ warden --resume [id] resume a conversation by id (or the last one here)
20
+ warden doctor [--chat] check your setup and say how to fix problems
21
+ warden update update to the latest version
22
+ warden --version
23
+
24
+ Options:
25
+ --model <name or provider/model> e.g. gemini/gemini-2.5-flash, or a name saved with /add
26
+ --mode ask|auto-edit|yolo ask: approve edits and commands (default)
27
+ auto-edit: file edits in this folder run without asking
28
+ yolo: everything runs without asking
29
+
30
+ Inside Warden: /add to add models (free ones too), /model to switch, /help for the rest.
31
+
32
+ Supervisor tools (record and review Claude Code, compare models):
33
+ warden daemon | watch | sessions | replay <id> | diff <id> | review <id> | reviews <id>
34
+ warden arena run <task.json> | board | show <run> | rate ... warden harnesses`;
35
+
36
+ async function main() {
37
+ const argv = process.argv.slice(2);
38
+ if (argv[0] && SUPERVISOR.has(argv[0])) { await import("./supervisor/cli.mjs"); return; } // it reads process.argv itself
39
+ if (argv[0] === "help" || argv.includes("--help") || argv.includes("-h")) { console.log(HELP); return; }
40
+ if (argv[0] === "--version" || argv[0] === "-v" || argv[0] === "version") { console.log(VERSION); return; }
41
+ if (argv[0] === "doctor") { const { runDoctor } = await import("./agent/doctor.mjs"); process.exitCode = await runDoctor({ testChat: argv.includes("--chat") }); return; }
42
+ if (argv[0] === "update") { const { runUpdate } = await import("./agent/update.mjs"); process.exitCode = await runUpdate(); return; }
43
+
44
+ const opt = (name) => { const i = argv.indexOf(name); return i >= 0 ? argv[i + 1] : undefined; };
45
+ const flagsWithValue = new Set(["--model", "-m", "--mode", "-p", "--print", "--resume"]);
46
+ const model = opt("--model") ?? opt("-m");
47
+ const mode = opt("--mode");
48
+ const printIdx = Math.max(argv.indexOf("-p"), argv.indexOf("--print"));
49
+ if (printIdx >= 0) {
50
+ const prompt = argv[printIdx + 1];
51
+ if (!prompt) { console.error('usage: warden -p "message"'); process.exitCode = 2; return; }
52
+ const { runHeadless } = await import("./agent/headless.mjs");
53
+ process.exitCode = await runHeadless({ prompt, model, mode, json: argv.includes("--json") });
54
+ return;
55
+ }
56
+ let resume;
57
+ const ri = argv.indexOf("--resume");
58
+ if (ri >= 0) resume = argv[ri + 1] && !argv[ri + 1].startsWith("-") ? argv[ri + 1] : true;
59
+ if (argv.includes("--continue") || argv.includes("-c")) resume = true;
60
+ // first bare word(s) not belonging to a flag = initial message
61
+ const words = [];
62
+ for (let i = 0; i < argv.length; i++) {
63
+ if (flagsWithValue.has(argv[i])) { i++; continue; }
64
+ if (argv[i].startsWith("-")) continue;
65
+ words.push(argv[i]);
66
+ }
67
+ const { startApp } = await import("./tui/app.mjs");
68
+ await startApp({ model, mode, resume, initialPrompt: words.join(" ") || undefined });
69
+ }
70
+
71
+ main().catch((e) => { console.error(`warden: ${e?.stack || e}`); process.exitCode = 1; });
@@ -0,0 +1,148 @@
1
+ // Arena: same task x N subjects, each in its own git worktree from the same base commit.
2
+ import fs from "node:fs";
3
+ import os from "node:os";
4
+ import path from "node:path";
5
+ import crypto from "node:crypto";
6
+ import { runSubject, run } from "./subjects.mjs";
7
+ import { judgePanel, pickJudges } from "./judge.mjs";
8
+
9
+ const git = (cwd, args, opts = {}) => run("git", args, { cwd, timeoutMs: 120_000, ...opts });
10
+
11
+ export function loadTask(file) {
12
+ const t = JSON.parse(fs.readFileSync(file, "utf8"));
13
+ const dir = path.dirname(path.resolve(file));
14
+ if (!t.id || !t.prompt || !Array.isArray(t.subjects) || t.subjects.length < 2) throw new Error("task needs id, prompt and at least 2 subjects");
15
+ const names = t.subjects.map((s) => s.name);
16
+ if (new Set(names).size !== names.length) throw new Error("subject names must be unique");
17
+ t.repo = path.resolve(dir, t.repo || ".");
18
+ t.checks = t.checks || [];
19
+ return t;
20
+ }
21
+
22
+ export async function setupWorktree(task, runId, subject) {
23
+ const base = task.base || "HEAD";
24
+ const dir = path.join(fs.mkdtempSync(path.join(os.tmpdir(), `warden-arena-${runId}-`)), subject.name.replace(/[^\w.-]/g, "_"));
25
+ const r = await git(task.repo, ["worktree", "add", "--detach", dir, base]);
26
+ if (r.code !== 0) throw new Error(`git worktree add failed: ${r.err.slice(0, 200)}`);
27
+ return dir;
28
+ }
29
+
30
+ // On Windows a just-finished process can keep files locked for a moment, so `git worktree remove` may fail.
31
+ // Retry, then delete the folder ourselves and let git forget the worktree, so none are left behind.
32
+ export async function removeWorktree(task, dir) {
33
+ for (let attempt = 0; attempt < 5; attempt++) {
34
+ const r = await git(task.repo, ["worktree", "remove", "--force", dir]);
35
+ if (r.code === 0 && !fs.existsSync(dir)) break;
36
+ await new Promise((res) => setTimeout(res, 300 * (attempt + 1)));
37
+ }
38
+ if (fs.existsSync(dir)) { try { fs.rmSync(dir, { recursive: true, force: true, maxRetries: 10, retryDelay: 300 }); } catch {} }
39
+ await git(task.repo, ["worktree", "prune"]);
40
+ try { fs.rmSync(path.dirname(dir), { recursive: true, force: true, maxRetries: 5, retryDelay: 200 }); } catch {}
41
+ }
42
+
43
+ async function runChecks(task, cwd) {
44
+ const out = [];
45
+ for (const c of task.checks) {
46
+ const r = await run(c.cmd, [], { cwd, shell: true, timeoutMs: c.timeoutMs || 180_000 });
47
+ out.push({ name: c.name || c.cmd, pass: r.code === 0 && !r.killed, ms: r.ms });
48
+ }
49
+ return out;
50
+ }
51
+
52
+ async function diffStats(cwd, base) {
53
+ await git(cwd, ["add", "-A"]);
54
+ const d = await git(cwd, ["diff", "--cached", base], { timeoutMs: 120_000 });
55
+ const n = await git(cwd, ["diff", "--cached", "--numstat", base]);
56
+ let added = 0, removed = 0, files = 0;
57
+ for (const l of n.out.split("\n").filter(Boolean)) { const [a, r] = l.split("\t"); files++; added += Number(a) || 0; removed += Number(r) || 0; }
58
+ return { diff: d.out, files, added, removed };
59
+ }
60
+
61
+ // Measure one subject end to end.
62
+ export async function evaluateSubject(task, runId, subject, { keep = false, timeoutMs, maxUsd } = {}) {
63
+ let dir;
64
+ const res = { subject: subject.name, model: subject.model ?? null, provider: subject.kind === "claude-code" ? "claude-cli" : "", checks: [] };
65
+ try {
66
+ dir = await setupWorktree(task, runId, subject);
67
+ const base = (await git(dir, ["rev-parse", "HEAD"])).out.trim();
68
+ Object.assign(res, await runSubject(subject, task, dir, { timeoutMs, maxUsd }));
69
+ res.checks = await runChecks(task, dir);
70
+ Object.assign(res, await diffStats(dir, base).then((s) => ({ diff: s.diff, filesChanged: s.files, linesAdded: s.added, linesRemoved: s.removed })));
71
+ } catch (err) {
72
+ res.ok = false; res.error = err.message;
73
+ } finally {
74
+ if (dir && !keep) await removeWorktree(task, dir);
75
+ else if (dir) res.worktree = dir;
76
+ }
77
+ res.checksPassed = res.checks.length > 0 && res.checks.every((c) => c.pass);
78
+ return res;
79
+ }
80
+
81
+ async function pool(items, limit, fn) {
82
+ const results = new Array(items.length); let next = 0;
83
+ await Promise.all(Array.from({ length: Math.min(limit, items.length) }, async () => {
84
+ while (next < items.length) { const i = next++; results[i] = await fn(items[i], i); }
85
+ }));
86
+ return results;
87
+ }
88
+
89
+ // Ranking: passing every check beats failing; then judge score; then cheaper; then faster. Fully visible, no hidden weights.
90
+ export function rank(results) {
91
+ const key = (r) => [r.ok ? 1 : 0, r.checksPassed ? 1 : 0, r.judgeAvg ?? -1, -(r.costUsd ?? Infinity), -(r.durationMs ?? Infinity)];
92
+ return results.slice().sort((a, b) => { const ka = key(a), kb = key(b); for (let i = 0; i < ka.length; i++) if (ka[i] !== kb[i]) return kb[i] - ka[i]; return 0; });
93
+ }
94
+
95
+ export async function runArena({ task, db, cfg, keep = false, concurrency = 3, rng, judgeProvider, onProgress = () => {} }) {
96
+ const runId = new Date().toISOString().replace(/[-:T]/g, "").slice(0, 12) + "-" + crypto.randomBytes(2).toString("hex");
97
+ const results = await pool(task.subjects, concurrency, async (s) => {
98
+ onProgress(s.name, "running");
99
+ const r = await evaluateSubject(task, runId, s, { keep, timeoutMs: task.timeoutMs, maxUsd: task.maxUsdPerSubject });
100
+ onProgress(s.name, r.ok ? (r.checksPassed ? "done, checks pass" : "done, checks FAIL") : `error: ${r.error}`);
101
+ return r;
102
+ });
103
+
104
+ // Blind judging
105
+ const judgeCfg = (task.judges || cfg.judges || []).map((j) => (typeof j === "string" ? { provider: j.split(":")[0], model: j.split(":").slice(1).join(":") || null } : j));
106
+ const contestants = task.subjects.map((s) => ({ subject: s.name, provider: s.kind === "claude-code" ? "claude-cli" : "", model: s.model }));
107
+ const { used, warnings } = pickJudges(judgeCfg, contestants);
108
+ // Only judge subjects that actually finished: errored ones have nothing to compare, and judging costs money.
109
+ // A blind comparison also needs at least two candidates.
110
+ const finished = results.filter((r) => r.ok);
111
+ const doJudge = used.length > 0 && finished.length >= 2;
112
+ onProgress("judges", doJudge ? `${used.length} judge(s), blind, position-swapped` : finished.length < 2 ? "skipped (fewer than 2 subjects finished)" : "none");
113
+ const panel = doJudge
114
+ ? await judgePanel({ task, candidates: finished.map((r) => ({ subject: r.subject, diff: r.diff, checks: r.checks, ok: r.ok })), judges: used, cfg, rng, ...(judgeProvider ? { provider: judgeProvider } : {}) })
115
+ : { scores: {}, detail: [] };
116
+ for (const r of results) r.judgeAvg = panel.scores[r.subject] ?? null;
117
+
118
+ const ranked = rank(results);
119
+ db.prepare("INSERT INTO arena_runs (id, ts, task_id, prompt, repo, base, judges) VALUES (?,?,?,?,?,?,?)")
120
+ .run(runId, Date.now(), task.id, task.prompt, task.repo, task.base || "HEAD", JSON.stringify({ used, warnings }));
121
+ const ins = db.prepare(`INSERT INTO arena_results (run_id, subject, model, ok, error, duration_ms, cost_usd, tokens, turns, checks, checks_passed,
122
+ files_changed, lines_added, lines_removed, diff, judge_avg, judge_detail) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`);
123
+ for (const r of results) {
124
+ ins.run(runId, r.subject, r.model, r.ok ? 1 : 0, r.error ?? null, r.durationMs ?? null, r.costUsd ?? null, r.tokens ?? null, r.turns ?? null,
125
+ JSON.stringify(r.checks), r.checksPassed ? 1 : 0, r.filesChanged ?? null, r.linesAdded ?? null, r.linesRemoved ?? null, r.diff ?? null,
126
+ r.judgeAvg, JSON.stringify(panel.detail.filter((d) => d.scores?.[r.subject] != null || d.error).map((d) => ({ judge: d.judge, order: d.order, score: d.scores?.[r.subject], note: d.notes?.[r.subject], error: d.error }))));
127
+ }
128
+ return { runId, ranked, warnings, judgeErrors: panel.detail.filter((d) => d.error) };
129
+ }
130
+
131
+ // Cross-run leaderboard per subject: wins (rank 1), runs, pass rate, avg judge, avg cost, human votes.
132
+ export function leaderboard(db, taskId) {
133
+ const runs = db.prepare(`SELECT id FROM arena_runs ${taskId ? "WHERE task_id=?" : ""} ORDER BY ts`).all(...(taskId ? [taskId] : []));
134
+ const stats = new Map();
135
+ for (const { id } of runs) {
136
+ const rows = db.prepare("SELECT * FROM arena_results WHERE run_id=?").all(id);
137
+ const ranked = rank(rows.map((r) => ({ ...r, ok: !!r.ok, checksPassed: !!r.checks_passed, judgeAvg: r.judge_avg, costUsd: r.cost_usd, durationMs: r.duration_ms })));
138
+ ranked.forEach((r, i) => {
139
+ const s = stats.get(r.subject) || { subject: r.subject, model: r.model, runs: 0, wins: 0, passes: 0, judge: [], cost: [], ms: [], human: 0 };
140
+ s.runs++; if (i === 0) s.wins++; if (r.checksPassed) s.passes++;
141
+ if (r.judgeAvg != null) s.judge.push(r.judgeAvg); if (r.costUsd != null) s.cost.push(r.costUsd); if (r.durationMs != null) s.ms.push(r.durationMs);
142
+ s.human += r.human ?? 0; stats.set(r.subject, s);
143
+ });
144
+ }
145
+ const avg = (a) => (a.length ? a.reduce((x, y) => x + y, 0) / a.length : null);
146
+ return [...stats.values()].map((s) => ({ ...s, winRate: s.wins / s.runs, passRate: s.passes / s.runs, judgeAvg: avg(s.judge), costAvg: avg(s.cost), msAvg: avg(s.ms) }))
147
+ .sort((a, b) => b.winRate - a.winRate || b.passRate - a.passRate || (b.judgeAvg ?? 0) - (a.judgeAvg ?? 0));
148
+ }
@@ -0,0 +1,112 @@
1
+ // Adapters that let the Arena run other coding-agent CLIs as subjects.
2
+ //
3
+ // Evidence level of each adapter (be honest about what was actually checked):
4
+ // opencode flags read from the installed `opencode run --help` (v2.2.4). Output schema NOT checked, so cost/tokens stay unknown.
5
+ // pi flags read from the installed `pi --help` (0.87.1). Same: cost/tokens unknown.
6
+ // codex flags and the --json event schema taken from OpenAI's docs. NOT installed here, never run. Model flag
7
+ // (-m/--model) comes from third-party docs, not the official page.
8
+ // gemini deliberately NOT an adapter: its docs banner says the CLI was replaced by Antigravity CLI (June 2026) and
9
+ // the model/approval flags could not be confirmed. Use a generic `command` subject instead (see README).
10
+ //
11
+ // The prompt is always passed as a single argv element and never through a shell, so quotes or `&` in a task prompt
12
+ // cannot be interpreted as commands. On Windows npm installs a .cmd shim; we resolve it to the real exe/js it launches.
13
+ import fs from "node:fs";
14
+ import path from "node:path";
15
+
16
+ export function resolveBin(name, env = process.env) {
17
+ if (process.platform !== "win32") return { cmd: name, pre: [] };
18
+ const exts = [".exe", ".cmd"];
19
+ for (const dir of (env.PATH || env.Path || "").split(path.delimiter).filter(Boolean)) {
20
+ for (const ext of exts) {
21
+ const file = path.join(dir, name + ext);
22
+ if (!fs.existsSync(file)) continue;
23
+ if (ext === ".exe") return { cmd: file, pre: [] };
24
+ // npm shim: find the real target it launches
25
+ const txt = fs.readFileSync(file, "utf8");
26
+ const targets = [...txt.matchAll(/"%dp0%\\?([^"]+\.(?:exe|js))"/gi)].map((m) => path.join(dir, m[1]));
27
+ const real = targets.at(-1);
28
+ if (real && fs.existsSync(real)) return real.toLowerCase().endsWith(".js") ? { cmd: process.execPath, pre: [real] } : { cmd: real, pre: [] };
29
+ }
30
+ }
31
+ return { cmd: name, pre: [] }; // not found: spawn will fail with a clear ENOENT
32
+ }
33
+
34
+ const extra = (s) => (Array.isArray(s.extraArgs) ? s.extraArgs.map(String) : []);
35
+
36
+ export const HARNESSES = {
37
+ opencode: {
38
+ bin: "opencode",
39
+ evidence: "flags verified against installed --help; metrics unknown",
40
+ risk: "opencode runs with --auto: ALL tool permissions (including shell commands) are auto-approved and nothing is sandboxed. It is started in the worktree, but nothing stops it from touching other paths.",
41
+ build: (s, prompt) => ({
42
+ // --standalone: private server per run so parallel runs don't share the background service
43
+ args: ["run", "--auto", "--standalone", "--format", "json", ...(s.model ? ["--model", s.model] : []), ...extra(s), "--", prompt],
44
+ }),
45
+ },
46
+ pi: {
47
+ bin: "pi",
48
+ evidence: "run live with Gemini models (works); tokens/cost/turns parsed from `--mode json` as observed; cost is pi's own estimate",
49
+ risk: "pi is limited to read/edit/write tools (no shell) unless the subject sets `tools`. Your personal extensions and skills are disabled so runs are reproducible.",
50
+ build: (s, prompt) => ({
51
+ args: ["-p", "--no-session", "--no-extensions", "--no-skills", "--tools", Array.isArray(s.tools) ? s.tools.join(",") : "read,edit,write",
52
+ "--mode", "json", ...(s.model ? ["--model", s.model] : []), ...extra(s), "--", prompt],
53
+ }),
54
+ // Schema observed from a real `pi --mode json` run (0.87.1, google provider): every model call ends with
55
+ // {"type":"message_end","message":{"role":"assistant","usage":{"totalTokens":..,"cost":{"total":..}},"stopReason":..}}.
56
+ // The cost is computed by pi from its own price list, not from a bill.
57
+ parse: (stdout) => {
58
+ let tokens = 0, cost = 0, turns = 0, err = null;
59
+ for (const line of String(stdout).split("\n")) {
60
+ let ev; try { ev = JSON.parse(line); } catch { continue; }
61
+ const m = ev.type === "message_end" ? ev.message : null;
62
+ if (!m || m.role !== "assistant") continue;
63
+ turns++;
64
+ tokens += m.usage?.totalTokens ?? 0;
65
+ cost += m.usage?.cost?.total ?? 0;
66
+ if (m.stopReason === "error") err = m.errorMessage || "model call failed";
67
+ }
68
+ return turns ? { tokens, costUsd: cost, turns, error: err } : { tokens: null, costUsd: null, turns: null, error: null };
69
+ },
70
+ },
71
+ codex: {
72
+ bin: "codex",
73
+ evidence: "from OpenAI docs; not installed here, never run",
74
+ risk: "codex runs with --sandbox workspace-write (writes limited to the worktree, no network by default per Codex's sandbox).",
75
+ build: (s, prompt) => ({
76
+ args: ["exec", "--sandbox", "workspace-write", "--json", ...(s.model ? ["--model", s.model] : []), ...extra(s), prompt],
77
+ }),
78
+ // JSONL: turn.completed carries usage { input_tokens, cached_input_tokens, output_tokens, reasoning_output_tokens } per docs
79
+ parse: (stdout) => {
80
+ let tokens = 0, seen = false, failed = null;
81
+ for (const line of String(stdout).split("\n")) {
82
+ let ev; try { ev = JSON.parse(line); } catch { continue; }
83
+ if (ev.type === "turn.completed" && ev.usage) { seen = true; tokens += (ev.usage.input_tokens ?? 0) + (ev.usage.output_tokens ?? 0); }
84
+ if (ev.type === "turn.failed" || ev.type === "error") failed = ev.message || ev.error?.message || ev.type;
85
+ }
86
+ return { tokens: seen ? tokens : null, error: failed };
87
+ },
88
+ },
89
+ };
90
+
91
+ // Pull a readable message out of a failed run. Harnesses that stream JSON events (opencode --format json) bury the
92
+ // real error in a nested object; otherwise fall back to the tail of stderr/stdout.
93
+ export function friendlyError(stdout, stderr, code) {
94
+ const messages = [];
95
+ const walk = (v, depth = 0) => {
96
+ if (!v || typeof v !== "object" || depth > 6) return;
97
+ if (typeof v.message === "string" && v.message.trim()) messages.push(v.message.trim());
98
+ for (const k of Object.keys(v)) walk(v[k], depth + 1);
99
+ };
100
+ for (const line of String(stdout).split("\n")) { try { walk(JSON.parse(line)); } catch {} }
101
+ const err = messages.filter((m) => /error|credit|quota|key|auth|denied|limit|rate|not found|invalid/i.test(m)).at(-1) || messages.at(-1);
102
+ return `exit ${code}: ${(err || String(stderr).trim() || String(stdout).trim()).replace(/\s+/g, " ").slice(-240)}`;
103
+ }
104
+
105
+ export function describeHarness(kind) { return HARNESSES[kind] ? { evidence: HARNESSES[kind].evidence, risk: HARNESSES[kind].risk } : null; }
106
+
107
+ export function harnessCommand(subject, prompt) {
108
+ const h = HARNESSES[subject.kind];
109
+ if (!h) return null;
110
+ const { cmd, pre } = resolveBin(h.bin);
111
+ return { cmd, args: [...pre, ...h.build(subject, prompt).args], parse: h.parse };
112
+ }