reevesagents 1.2.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.
- package/CHANGELOG.md +104 -0
- package/LICENSE +166 -0
- package/README.md +288 -0
- package/dist/chunk-3IXII4H5.js +129 -0
- package/dist/chunk-4Q3JF22J.js +51 -0
- package/dist/chunk-D24DQQ6V.js +36 -0
- package/dist/chunk-DHAW6OY3.js +40 -0
- package/dist/chunk-G6FN75ZW.js +2393 -0
- package/dist/chunk-JY4ZTTRK.js +172 -0
- package/dist/chunk-LUW6GCPO.js +108 -0
- package/dist/chunk-MVMGFG4W.js +1462 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +355 -0
- package/dist/extras-25AF6TAR.js +12 -0
- package/dist/index.d.ts +236 -0
- package/dist/index.js +89 -0
- package/dist/open-browser-DRSIJQ37.js +26 -0
- package/dist/router-WCFLHJV7.js +5700 -0
- package/dist/server-Z2YTGWSX.js +883 -0
- package/dist/tui-launch-7RGFE5KY.js +10 -0
- package/dist/web/addon-fit.js +2 -0
- package/dist/web/app.css +1742 -0
- package/dist/web/app.js +1435 -0
- package/dist/web/brand-duck.json +1 -0
- package/dist/web/index.html +178 -0
- package/dist/web/xterm.css +285 -0
- package/dist/web/xterm.js +2 -0
- package/package.json +86 -0
|
@@ -0,0 +1,1462 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/launcher/providers.ts
|
|
4
|
+
import { execFileSync, spawnSync } from "child_process";
|
|
5
|
+
var BIN = {
|
|
6
|
+
cc: "claude",
|
|
7
|
+
codex: "codex",
|
|
8
|
+
opencode: "opencode",
|
|
9
|
+
hermes: "hermes",
|
|
10
|
+
kimi: "kimi",
|
|
11
|
+
deepseek: "deepseek",
|
|
12
|
+
pi: "pi",
|
|
13
|
+
qwen: "qwen",
|
|
14
|
+
aider: "aider"
|
|
15
|
+
};
|
|
16
|
+
var PROVIDERS = Object.keys(BIN);
|
|
17
|
+
var PROVIDER_ALIASES = {
|
|
18
|
+
cc: "cc",
|
|
19
|
+
claude: "cc",
|
|
20
|
+
"claude code": "cc",
|
|
21
|
+
"claude-code": "cc",
|
|
22
|
+
claudecode: "cc",
|
|
23
|
+
codex: "codex",
|
|
24
|
+
"codex cli": "codex",
|
|
25
|
+
"codex-cli": "codex",
|
|
26
|
+
opencode: "opencode",
|
|
27
|
+
"opencode cli": "opencode",
|
|
28
|
+
"opencode-cli": "opencode",
|
|
29
|
+
open_code: "opencode",
|
|
30
|
+
hermes: "hermes",
|
|
31
|
+
kimi: "kimi",
|
|
32
|
+
"kimi code": "kimi",
|
|
33
|
+
"kimi-code": "kimi",
|
|
34
|
+
deepseek: "deepseek",
|
|
35
|
+
"deepseek cli": "deepseek",
|
|
36
|
+
"deepseek-cli": "deepseek",
|
|
37
|
+
pi: "pi",
|
|
38
|
+
qwen: "qwen",
|
|
39
|
+
"qwen code": "qwen",
|
|
40
|
+
"qwen-code": "qwen",
|
|
41
|
+
aider: "aider"
|
|
42
|
+
};
|
|
43
|
+
var HELP_INSPECT_TIMEOUT_MS = 3e3;
|
|
44
|
+
var HELP_INSPECT_TOTAL_BUDGET_MS = 8e3;
|
|
45
|
+
var HERMES_PROVIDER_PREFIXES = /* @__PURE__ */ new Set([
|
|
46
|
+
"auto",
|
|
47
|
+
"openrouter",
|
|
48
|
+
"nous",
|
|
49
|
+
"openai-codex",
|
|
50
|
+
"copilot-acp",
|
|
51
|
+
"copilot",
|
|
52
|
+
"anthropic",
|
|
53
|
+
"huggingface",
|
|
54
|
+
"novita",
|
|
55
|
+
"zai",
|
|
56
|
+
"kimi-coding",
|
|
57
|
+
"kimi-coding-cn",
|
|
58
|
+
"minimax",
|
|
59
|
+
"minimax-cn",
|
|
60
|
+
"minimax-oauth",
|
|
61
|
+
"kilocode",
|
|
62
|
+
"xiaomi",
|
|
63
|
+
"arcee",
|
|
64
|
+
"gmi",
|
|
65
|
+
"alibaba",
|
|
66
|
+
"alibaba-coding-plan",
|
|
67
|
+
"deepseek",
|
|
68
|
+
"nvidia",
|
|
69
|
+
"ollama-cloud",
|
|
70
|
+
"xai",
|
|
71
|
+
"xai-oauth",
|
|
72
|
+
"qwen-oauth",
|
|
73
|
+
"bedrock",
|
|
74
|
+
"opencode-zen",
|
|
75
|
+
"opencode-go",
|
|
76
|
+
"azure-foundry",
|
|
77
|
+
"lmstudio",
|
|
78
|
+
"stepfun",
|
|
79
|
+
"tencent-tokenhub",
|
|
80
|
+
"custom"
|
|
81
|
+
]);
|
|
82
|
+
var HELP_REQUIREMENTS = {
|
|
83
|
+
cc: [
|
|
84
|
+
{ feature: "skip permissions", tokens: ["--dangerously-skip-permissions"] }
|
|
85
|
+
],
|
|
86
|
+
codex: [
|
|
87
|
+
{ feature: "skip permissions", tokens: ["--dangerously-bypass-approvals-and-sandbox"] }
|
|
88
|
+
],
|
|
89
|
+
opencode: [
|
|
90
|
+
{ feature: "prompt launch", tokens: ["--prompt"] },
|
|
91
|
+
{ feature: "model selection", tokens: ["--model"] }
|
|
92
|
+
],
|
|
93
|
+
hermes: [
|
|
94
|
+
{ feature: "chat launch", tokens: ["--model"] },
|
|
95
|
+
{ feature: "skip permissions", tokens: ["--yolo"] }
|
|
96
|
+
],
|
|
97
|
+
kimi: [
|
|
98
|
+
{ feature: "model selection", tokens: ["--model"] },
|
|
99
|
+
{ feature: "skip permissions", tokens: ["--yolo"] }
|
|
100
|
+
],
|
|
101
|
+
deepseek: [
|
|
102
|
+
{ feature: "model selection", tokens: ["--model"] }
|
|
103
|
+
],
|
|
104
|
+
pi: [
|
|
105
|
+
{ feature: "model selection", tokens: ["--model"] }
|
|
106
|
+
],
|
|
107
|
+
qwen: [
|
|
108
|
+
{ feature: "model selection", tokens: ["--model"] },
|
|
109
|
+
{ feature: "skip permissions", tokens: ["--approval-mode"] }
|
|
110
|
+
],
|
|
111
|
+
aider: [
|
|
112
|
+
{ feature: "model selection", tokens: ["--model"] },
|
|
113
|
+
{ feature: "skip confirmations", tokens: ["--yes-always"] }
|
|
114
|
+
]
|
|
115
|
+
};
|
|
116
|
+
function isProvider(value) {
|
|
117
|
+
return typeof value === "string" && Object.prototype.hasOwnProperty.call(BIN, value);
|
|
118
|
+
}
|
|
119
|
+
function normalizeProvider(value) {
|
|
120
|
+
if (typeof value !== "string") return null;
|
|
121
|
+
return PROVIDER_ALIASES[value.trim().toLowerCase()] ?? null;
|
|
122
|
+
}
|
|
123
|
+
function buildCommand(opts) {
|
|
124
|
+
const { provider, permissions, model, auth_mode = "default", effort = "default" } = opts;
|
|
125
|
+
if (!isProvider(provider)) {
|
|
126
|
+
throw new Error(`Unsupported provider: ${String(provider)}`);
|
|
127
|
+
}
|
|
128
|
+
const cmd = [BIN[provider]];
|
|
129
|
+
if (provider === "cc") {
|
|
130
|
+
if (permissions === "skip") cmd.push("--dangerously-skip-permissions");
|
|
131
|
+
if (auth_mode === "api-key") cmd.push("--bare");
|
|
132
|
+
if (effort !== "default") cmd.push("--effort", effort);
|
|
133
|
+
if (model) cmd.push("--model", model);
|
|
134
|
+
return cmd;
|
|
135
|
+
}
|
|
136
|
+
if (provider === "codex") {
|
|
137
|
+
if (permissions === "skip") cmd.push("--dangerously-bypass-approvals-and-sandbox");
|
|
138
|
+
if (model) cmd.push("--model", model);
|
|
139
|
+
return cmd;
|
|
140
|
+
}
|
|
141
|
+
if (provider === "opencode") {
|
|
142
|
+
if (model) cmd.push("--model", model);
|
|
143
|
+
return cmd;
|
|
144
|
+
}
|
|
145
|
+
if (provider === "hermes") {
|
|
146
|
+
cmd.push("chat");
|
|
147
|
+
if (permissions === "skip") cmd.push("--yolo");
|
|
148
|
+
if (model) {
|
|
149
|
+
const parsed = parseHermesModel(model);
|
|
150
|
+
if (parsed.provider) cmd.push("--provider", parsed.provider);
|
|
151
|
+
cmd.push("--model", parsed.model);
|
|
152
|
+
}
|
|
153
|
+
return cmd;
|
|
154
|
+
}
|
|
155
|
+
if (provider === "kimi") {
|
|
156
|
+
if (permissions === "skip") cmd.push("--yolo");
|
|
157
|
+
if (model) cmd.push("--model", model);
|
|
158
|
+
return cmd;
|
|
159
|
+
}
|
|
160
|
+
if (provider === "deepseek") {
|
|
161
|
+
if (model) cmd.push("--model", model);
|
|
162
|
+
return cmd;
|
|
163
|
+
}
|
|
164
|
+
if (provider === "pi") {
|
|
165
|
+
if (model) cmd.push("--model", model);
|
|
166
|
+
return cmd;
|
|
167
|
+
}
|
|
168
|
+
if (provider === "qwen") {
|
|
169
|
+
if (permissions === "skip") cmd.push("--approval-mode", "yolo");
|
|
170
|
+
if (model) cmd.push("--model", model);
|
|
171
|
+
return cmd;
|
|
172
|
+
}
|
|
173
|
+
if (provider === "aider") {
|
|
174
|
+
if (permissions === "skip") cmd.push("--yes-always");
|
|
175
|
+
if (model) cmd.push("--model", model);
|
|
176
|
+
return cmd;
|
|
177
|
+
}
|
|
178
|
+
return cmd;
|
|
179
|
+
}
|
|
180
|
+
function parseHermesModel(value) {
|
|
181
|
+
const separator = value.indexOf(":");
|
|
182
|
+
if (separator <= 0) return { model: value };
|
|
183
|
+
const provider = value.slice(0, separator).trim();
|
|
184
|
+
const model = value.slice(separator + 1).trim();
|
|
185
|
+
if (!provider || !model || !HERMES_PROVIDER_PREFIXES.has(provider)) return { model: value };
|
|
186
|
+
return { provider, model };
|
|
187
|
+
}
|
|
188
|
+
function detectAvailable() {
|
|
189
|
+
const result = {};
|
|
190
|
+
for (const provider of PROVIDERS) {
|
|
191
|
+
try {
|
|
192
|
+
execFileSync("which", [BIN[provider]], { stdio: "pipe" });
|
|
193
|
+
result[provider] = true;
|
|
194
|
+
} catch {
|
|
195
|
+
result[provider] = false;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return result;
|
|
199
|
+
}
|
|
200
|
+
function helpCommand(provider) {
|
|
201
|
+
if (provider === "hermes") return [BIN[provider], "chat", "--help"];
|
|
202
|
+
return [BIN[provider], "--help"];
|
|
203
|
+
}
|
|
204
|
+
function missingHelpFeatures(provider, helpText) {
|
|
205
|
+
return HELP_REQUIREMENTS[provider].filter((req) => req.tokens.some((token) => !helpText.includes(token))).map((req) => req.feature);
|
|
206
|
+
}
|
|
207
|
+
function inspectProviderCompatibility() {
|
|
208
|
+
const available = detectAvailable();
|
|
209
|
+
const result = {};
|
|
210
|
+
const deadline = Date.now() + HELP_INSPECT_TOTAL_BUDGET_MS;
|
|
211
|
+
for (const provider of PROVIDERS) {
|
|
212
|
+
if (!available[provider]) {
|
|
213
|
+
result[provider] = {
|
|
214
|
+
provider,
|
|
215
|
+
available: false,
|
|
216
|
+
ok: true,
|
|
217
|
+
detail: "not installed",
|
|
218
|
+
missing: []
|
|
219
|
+
};
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
try {
|
|
223
|
+
const remaining = deadline - Date.now();
|
|
224
|
+
if (remaining <= 0) {
|
|
225
|
+
result[provider] = {
|
|
226
|
+
provider,
|
|
227
|
+
available: true,
|
|
228
|
+
ok: false,
|
|
229
|
+
detail: "help inspection skipped after timeout budget",
|
|
230
|
+
missing: ["help inspection"]
|
|
231
|
+
};
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
const [bin, ...args] = helpCommand(provider);
|
|
235
|
+
const inspected = spawnSync(bin, args, {
|
|
236
|
+
encoding: "utf8",
|
|
237
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
238
|
+
timeout: Math.min(HELP_INSPECT_TIMEOUT_MS, remaining)
|
|
239
|
+
});
|
|
240
|
+
if (inspected.error) throw inspected.error;
|
|
241
|
+
const help = `${inspected.stdout ?? ""}
|
|
242
|
+
${inspected.stderr ?? ""}`;
|
|
243
|
+
if (inspected.status !== 0 && help.trim().length === 0) {
|
|
244
|
+
throw new Error(`help exited ${inspected.status}`);
|
|
245
|
+
}
|
|
246
|
+
const missing = missingHelpFeatures(provider, help);
|
|
247
|
+
result[provider] = {
|
|
248
|
+
provider,
|
|
249
|
+
available: true,
|
|
250
|
+
ok: missing.length === 0,
|
|
251
|
+
detail: missing.length === 0 ? "compatible" : `missing ${missing.join(", ")}`,
|
|
252
|
+
missing
|
|
253
|
+
};
|
|
254
|
+
} catch (err) {
|
|
255
|
+
result[provider] = {
|
|
256
|
+
provider,
|
|
257
|
+
available: true,
|
|
258
|
+
ok: false,
|
|
259
|
+
detail: `could not inspect help: ${err instanceof Error ? err.message : "unknown error"}`,
|
|
260
|
+
missing: ["help inspection"]
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return result;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// src/utils/display.ts
|
|
268
|
+
import chalk from "chalk";
|
|
269
|
+
|
|
270
|
+
// src/utils/tokens.ts
|
|
271
|
+
var colors = {
|
|
272
|
+
text: {
|
|
273
|
+
primary: "#ece7dc",
|
|
274
|
+
// body text
|
|
275
|
+
dim: "#b8b0a3",
|
|
276
|
+
// labels, secondary text
|
|
277
|
+
muted: "#817b70",
|
|
278
|
+
// tertiary text, divider labels
|
|
279
|
+
faint: "#55504a",
|
|
280
|
+
// disabled, dashed border edges
|
|
281
|
+
subtle: "#292622"
|
|
282
|
+
// near-background tints
|
|
283
|
+
},
|
|
284
|
+
accent: {
|
|
285
|
+
bright: "#7eb8f5",
|
|
286
|
+
// selection cursor, focused text, current breadcrumb
|
|
287
|
+
primary: "#5a96e0",
|
|
288
|
+
// panel titles, links, emphasis
|
|
289
|
+
deep: "#4a6fa5",
|
|
290
|
+
// inactive panel borders
|
|
291
|
+
ink: "#1e2d3e"
|
|
292
|
+
// selected button fill
|
|
293
|
+
},
|
|
294
|
+
surface: {
|
|
295
|
+
selected: "#1e2d3e",
|
|
296
|
+
// selected action fill
|
|
297
|
+
rail: "#314253",
|
|
298
|
+
// card rails and active separators
|
|
299
|
+
border: "#3a4b5f",
|
|
300
|
+
// card border strokes
|
|
301
|
+
quiet: "#36383d"
|
|
302
|
+
// outer rules
|
|
303
|
+
},
|
|
304
|
+
status: {
|
|
305
|
+
ok: "#83a36f",
|
|
306
|
+
// running, ok, approved
|
|
307
|
+
warn: "#d7a84f",
|
|
308
|
+
// stale, blocked, warn
|
|
309
|
+
error: "#d56a60",
|
|
310
|
+
// failed, denied, fail
|
|
311
|
+
info: "#7fb4d6"
|
|
312
|
+
// neutral info
|
|
313
|
+
},
|
|
314
|
+
brand: {
|
|
315
|
+
blue: "#3a7ad8",
|
|
316
|
+
// wordmark base
|
|
317
|
+
pale: "#a5cdf7",
|
|
318
|
+
// wordmark highlight
|
|
319
|
+
gradient: ["#a5cdf7", "#7eb8f5", "#5a96e0", "#3a7ad8", "#2457a7"]
|
|
320
|
+
// breath cycle
|
|
321
|
+
},
|
|
322
|
+
// Provider colors are deliberately distinct from accent.bright so a focused
|
|
323
|
+
// provider row does not collide visually with the selection cursor.
|
|
324
|
+
provider: {
|
|
325
|
+
cc: "#d97757",
|
|
326
|
+
// Anthropic Orange (Claude brand); the one warm color
|
|
327
|
+
codex: "#7aa8c4",
|
|
328
|
+
// cool teal-blue
|
|
329
|
+
opencode: "#a890d1",
|
|
330
|
+
// muted purple
|
|
331
|
+
hermes: "#c97b9b",
|
|
332
|
+
// muted rose
|
|
333
|
+
kimi: "#b6a45d",
|
|
334
|
+
// muted gold
|
|
335
|
+
deepseek: "#6fb0a8",
|
|
336
|
+
// muted cyan
|
|
337
|
+
pi: "#86a76b",
|
|
338
|
+
// muted olive
|
|
339
|
+
qwen: "#c58c63",
|
|
340
|
+
// muted copper
|
|
341
|
+
aider: "#8fa6d4"
|
|
342
|
+
// muted periwinkle
|
|
343
|
+
},
|
|
344
|
+
model: {
|
|
345
|
+
default: "#9b9488",
|
|
346
|
+
// CLI default or unset model
|
|
347
|
+
claude: "#e0a06f",
|
|
348
|
+
// Claude family
|
|
349
|
+
openai: "#72b7d6",
|
|
350
|
+
// OpenAI family
|
|
351
|
+
deepseek: "#7ac8bc",
|
|
352
|
+
// DeepSeek family
|
|
353
|
+
qwen: "#d1a25d",
|
|
354
|
+
// Qwen family
|
|
355
|
+
kimi: "#d0c268",
|
|
356
|
+
// Kimi family
|
|
357
|
+
local: "#92b37a",
|
|
358
|
+
// local/open model family
|
|
359
|
+
other: "#9ca8bd"
|
|
360
|
+
// known provider, unclassified model
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
var sep = {
|
|
364
|
+
dot: " \xB7 ",
|
|
365
|
+
pipe: " \u2502 ",
|
|
366
|
+
arr: " \u2192 "
|
|
367
|
+
};
|
|
368
|
+
var space = {
|
|
369
|
+
none: 0,
|
|
370
|
+
sm: 1,
|
|
371
|
+
md: 2,
|
|
372
|
+
lg: 3
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
// src/utils/display.ts
|
|
376
|
+
var PROVIDER_DISPLAY_NAMES = {
|
|
377
|
+
cc: "Claude Code",
|
|
378
|
+
codex: "Codex CLI",
|
|
379
|
+
opencode: "OpenCode CLI",
|
|
380
|
+
hermes: "Hermes",
|
|
381
|
+
kimi: "Kimi Code",
|
|
382
|
+
deepseek: "DeepSeek CLI",
|
|
383
|
+
pi: "Pi",
|
|
384
|
+
qwen: "Qwen Code",
|
|
385
|
+
aider: "Aider"
|
|
386
|
+
};
|
|
387
|
+
function providerDisplayName(provider) {
|
|
388
|
+
return PROVIDER_DISPLAY_NAMES[provider] ?? provider;
|
|
389
|
+
}
|
|
390
|
+
function providerColor(p) {
|
|
391
|
+
return colors.provider[p] ?? "gray";
|
|
392
|
+
}
|
|
393
|
+
function modelColor(model, provider) {
|
|
394
|
+
const lower = model.trim().toLowerCase();
|
|
395
|
+
if (!lower) return colors.model.default;
|
|
396
|
+
if (lower.includes("claude") || lower.includes("sonnet") || lower.includes("opus") || lower.includes("haiku")) return colors.model.claude;
|
|
397
|
+
if (lower.includes("gpt") || lower.includes("codex") || /\bo[134]\b/.test(lower)) return colors.model.openai;
|
|
398
|
+
if (lower.includes("deepseek")) return colors.model.deepseek;
|
|
399
|
+
if (lower.includes("qwen")) return colors.model.qwen;
|
|
400
|
+
if (lower.includes("kimi") || lower.includes("moonshot")) return colors.model.kimi;
|
|
401
|
+
if (lower.includes("llama") || lower.includes("mistral") || lower.includes("mixtral") || lower.includes("ollama") || lower.includes("lmstudio") || lower.includes("local")) return colors.model.local;
|
|
402
|
+
if (provider === "cc") return colors.model.claude;
|
|
403
|
+
if (provider === "codex") return colors.model.openai;
|
|
404
|
+
if (provider === "deepseek") return colors.model.deepseek;
|
|
405
|
+
if (provider === "qwen") return colors.model.qwen;
|
|
406
|
+
if (provider === "kimi") return colors.model.kimi;
|
|
407
|
+
return colors.model.other;
|
|
408
|
+
}
|
|
409
|
+
function modelBadgeLabel(model) {
|
|
410
|
+
const value = model.trim();
|
|
411
|
+
if (!value) return "default";
|
|
412
|
+
const slashIdx = value.lastIndexOf("/");
|
|
413
|
+
const label = slashIdx >= 0 && slashIdx < value.length - 1 ? value.slice(slashIdx + 1) : value;
|
|
414
|
+
return truncateLabel(label, 24);
|
|
415
|
+
}
|
|
416
|
+
function truncateLabel(label, max) {
|
|
417
|
+
if (label.length <= max) return label;
|
|
418
|
+
return `${label.slice(0, Math.max(1, max - 3))}...`;
|
|
419
|
+
}
|
|
420
|
+
var SECRET_PATTERNS = [
|
|
421
|
+
/sk-ant-[A-Za-z0-9\-_]{20,}/g,
|
|
422
|
+
/sk-[A-Za-z0-9\-_]{20,}/g,
|
|
423
|
+
/AIza[A-Za-z0-9\-_]{35}/g,
|
|
424
|
+
/gsk_[A-Za-z0-9\-_]{20,}/g
|
|
425
|
+
];
|
|
426
|
+
function redactSecrets(text) {
|
|
427
|
+
let result = text;
|
|
428
|
+
for (const pattern of SECRET_PATTERNS) result = result.replace(pattern, "[REDACTED]");
|
|
429
|
+
return result;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// src/state/runs.ts
|
|
433
|
+
import {
|
|
434
|
+
chmodSync,
|
|
435
|
+
closeSync,
|
|
436
|
+
existsSync,
|
|
437
|
+
mkdirSync,
|
|
438
|
+
openSync,
|
|
439
|
+
readdirSync,
|
|
440
|
+
readFileSync,
|
|
441
|
+
renameSync,
|
|
442
|
+
rmSync,
|
|
443
|
+
statSync,
|
|
444
|
+
unlinkSync,
|
|
445
|
+
writeFileSync
|
|
446
|
+
} from "fs";
|
|
447
|
+
import { randomUUID } from "crypto";
|
|
448
|
+
import { homedir } from "os";
|
|
449
|
+
import { dirname, join } from "path";
|
|
450
|
+
import { spawnSync as spawnSync2 } from "child_process";
|
|
451
|
+
function stateRoot() {
|
|
452
|
+
return process.env.REEVES_REGISTRY || join(homedir(), ".reeves");
|
|
453
|
+
}
|
|
454
|
+
function runsDir() {
|
|
455
|
+
return join(stateRoot(), "runs");
|
|
456
|
+
}
|
|
457
|
+
function historyDir() {
|
|
458
|
+
return join(stateRoot(), "history", "runs");
|
|
459
|
+
}
|
|
460
|
+
function runDir(runId) {
|
|
461
|
+
return join(runsDir(), runId);
|
|
462
|
+
}
|
|
463
|
+
function agentsDir(runId) {
|
|
464
|
+
return join(runDir(runId), "agents");
|
|
465
|
+
}
|
|
466
|
+
function runPath(runId) {
|
|
467
|
+
return join(runDir(runId), "run.json");
|
|
468
|
+
}
|
|
469
|
+
function agentPath(runId, agentId) {
|
|
470
|
+
return join(agentsDir(runId), `${agentId}.json`);
|
|
471
|
+
}
|
|
472
|
+
function runHistoryPath(runId) {
|
|
473
|
+
return join(historyDir(), `${runId}.json`);
|
|
474
|
+
}
|
|
475
|
+
function nowIso() {
|
|
476
|
+
return (/* @__PURE__ */ new Date()).toISOString();
|
|
477
|
+
}
|
|
478
|
+
function nowMs() {
|
|
479
|
+
return Date.now();
|
|
480
|
+
}
|
|
481
|
+
function sleepSync(ms) {
|
|
482
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
483
|
+
}
|
|
484
|
+
function withRunsLock(fn) {
|
|
485
|
+
mkdirSync(stateRoot(), { recursive: true });
|
|
486
|
+
const lockPath = join(stateRoot(), ".runs.lock");
|
|
487
|
+
const staleMs = 5e3;
|
|
488
|
+
const deadline = Date.now() + staleMs;
|
|
489
|
+
while (true) {
|
|
490
|
+
try {
|
|
491
|
+
const fd = openSync(lockPath, "wx", 384);
|
|
492
|
+
try {
|
|
493
|
+
writeFileSync(fd, JSON.stringify({ pid: process.pid, created_at: nowIso() }));
|
|
494
|
+
} finally {
|
|
495
|
+
closeSync(fd);
|
|
496
|
+
}
|
|
497
|
+
break;
|
|
498
|
+
} catch {
|
|
499
|
+
try {
|
|
500
|
+
const age = Date.now() - statSync(lockPath).mtimeMs;
|
|
501
|
+
if (age > staleMs) unlinkSync(lockPath);
|
|
502
|
+
} catch {
|
|
503
|
+
}
|
|
504
|
+
if (Date.now() > deadline) throw new Error(`Timed out waiting for runs lock: ${lockPath}`);
|
|
505
|
+
sleepSync(25);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
try {
|
|
509
|
+
return fn();
|
|
510
|
+
} finally {
|
|
511
|
+
try {
|
|
512
|
+
unlinkSync(lockPath);
|
|
513
|
+
} catch {
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
function atomicWriteJson(path, data) {
|
|
518
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
519
|
+
const tmpPath = `${path}.tmp`;
|
|
520
|
+
writeFileSync(tmpPath, JSON.stringify(data, null, 2), "utf-8");
|
|
521
|
+
chmodSync(tmpPath, 384);
|
|
522
|
+
renameSync(tmpPath, path);
|
|
523
|
+
}
|
|
524
|
+
function normalizeTaskStatus(value) {
|
|
525
|
+
if (value === "queued" || value === "working" || value === "done" || value === "failed" || value === "blocked") return value;
|
|
526
|
+
return "queued";
|
|
527
|
+
}
|
|
528
|
+
function normalizePermissions(value) {
|
|
529
|
+
return value === "skip" ? "skip" : "ask";
|
|
530
|
+
}
|
|
531
|
+
function normalizeRunMode(value) {
|
|
532
|
+
if (value === "spawner" || value === "orchestrator") return value;
|
|
533
|
+
return void 0;
|
|
534
|
+
}
|
|
535
|
+
function asString(value, fallback = "") {
|
|
536
|
+
return typeof value === "string" ? value : fallback;
|
|
537
|
+
}
|
|
538
|
+
function fallbackRunName(id) {
|
|
539
|
+
return id ? `Run ${id.slice(0, 8)}` : "Untitled run";
|
|
540
|
+
}
|
|
541
|
+
function asNullableString(value) {
|
|
542
|
+
return typeof value === "string" ? value : null;
|
|
543
|
+
}
|
|
544
|
+
function normalizeInbox(value) {
|
|
545
|
+
if (!Array.isArray(value)) return [];
|
|
546
|
+
return value.filter((item) => typeof item === "object" && item !== null).map((item) => ({
|
|
547
|
+
id: asString(item.id, randomUUID()),
|
|
548
|
+
from_id: asString(item.from_id),
|
|
549
|
+
text: asString(item.text),
|
|
550
|
+
sent_at: asString(item.sent_at, nowIso()),
|
|
551
|
+
read: typeof item.read === "boolean" ? item.read : false
|
|
552
|
+
}));
|
|
553
|
+
}
|
|
554
|
+
function normalizeRun(raw) {
|
|
555
|
+
const id = asString(raw.id);
|
|
556
|
+
const run = {
|
|
557
|
+
id,
|
|
558
|
+
name: asString(raw.name).trim() || fallbackRunName(id),
|
|
559
|
+
status: raw.status === "ended" ? "ended" : "running",
|
|
560
|
+
tmux_session: asString(raw.tmux_session),
|
|
561
|
+
reeves_window_id: asString(raw.reeves_window_id),
|
|
562
|
+
reeves_pane_id: asString(raw.reeves_pane_id),
|
|
563
|
+
root_agent_id: asString(raw.root_agent_id),
|
|
564
|
+
working_dir: asString(raw.working_dir),
|
|
565
|
+
preset_name: asNullableString(raw.preset_name),
|
|
566
|
+
started_at: asString(raw.started_at, nowIso()),
|
|
567
|
+
ended_at: asNullableString(raw.ended_at)
|
|
568
|
+
};
|
|
569
|
+
const mode = normalizeRunMode(raw.mode);
|
|
570
|
+
if (mode) run.mode = mode;
|
|
571
|
+
const reevesSession = asString(raw.reeves_session);
|
|
572
|
+
if (reevesSession) run.reeves_session = reevesSession;
|
|
573
|
+
return run;
|
|
574
|
+
}
|
|
575
|
+
function normalizeRunHistory(raw) {
|
|
576
|
+
return {
|
|
577
|
+
id: asString(raw.id),
|
|
578
|
+
name: asString(raw.name).trim() || fallbackRunName(asString(raw.id)),
|
|
579
|
+
mode: normalizeRunMode(raw.mode) ?? "spawner",
|
|
580
|
+
status: raw.status === "stale" ? "stale" : "ended",
|
|
581
|
+
working_dir: asString(raw.working_dir),
|
|
582
|
+
started_at: asString(raw.started_at, nowIso()),
|
|
583
|
+
ended_at: asNullableString(raw.ended_at),
|
|
584
|
+
archived_at: asString(raw.archived_at, nowIso()),
|
|
585
|
+
agent_count: typeof raw.agent_count === "number" && Number.isFinite(raw.agent_count) ? Math.max(0, Math.floor(raw.agent_count)) : 0,
|
|
586
|
+
root_provider: isProvider(raw.root_provider) ? raw.root_provider : null
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
function normalizeAgent(raw) {
|
|
590
|
+
const id = asString(raw.id);
|
|
591
|
+
return {
|
|
592
|
+
id,
|
|
593
|
+
run_id: asString(raw.run_id),
|
|
594
|
+
nickname: asString(raw.nickname, id),
|
|
595
|
+
provider: isProvider(raw.provider) ? raw.provider : "cc",
|
|
596
|
+
model: asString(raw.model),
|
|
597
|
+
role: raw.role === "worker" ? "worker" : "root",
|
|
598
|
+
working_dir: asString(raw.working_dir),
|
|
599
|
+
task: asString(raw.task),
|
|
600
|
+
task_status: normalizeTaskStatus(raw.task_status),
|
|
601
|
+
task_note: asString(raw.task_note),
|
|
602
|
+
tmux_session: asString(raw.tmux_session),
|
|
603
|
+
tmux_window_id: asString(raw.tmux_window_id),
|
|
604
|
+
tmux_pane_id: asString(raw.tmux_pane_id),
|
|
605
|
+
rc_enabled: typeof raw.rc_enabled === "boolean" ? raw.rc_enabled : false,
|
|
606
|
+
permissions: normalizePermissions(raw.permissions),
|
|
607
|
+
headless: raw.headless === true,
|
|
608
|
+
inbox: normalizeInbox(raw.inbox),
|
|
609
|
+
last_seen: typeof raw.last_seen === "number" ? raw.last_seen : 0,
|
|
610
|
+
started_at: asString(raw.started_at, nowIso()),
|
|
611
|
+
ended_at: asNullableString(raw.ended_at)
|
|
612
|
+
};
|
|
613
|
+
}
|
|
614
|
+
function redactAgent(agent) {
|
|
615
|
+
return {
|
|
616
|
+
...agent,
|
|
617
|
+
task: redactSecrets(agent.task),
|
|
618
|
+
task_note: redactSecrets(agent.task_note),
|
|
619
|
+
inbox: agent.inbox.map((message) => ({ ...message, text: redactSecrets(message.text) }))
|
|
620
|
+
};
|
|
621
|
+
}
|
|
622
|
+
function writeRunUnlocked(run) {
|
|
623
|
+
const path = runPath(run.id);
|
|
624
|
+
atomicWriteJson(path, run);
|
|
625
|
+
return path;
|
|
626
|
+
}
|
|
627
|
+
function readRunUnlocked(runId) {
|
|
628
|
+
let raw;
|
|
629
|
+
try {
|
|
630
|
+
raw = readFileSync(runPath(runId), "utf-8");
|
|
631
|
+
} catch {
|
|
632
|
+
throw new Error(`Run not found: ${runId}`);
|
|
633
|
+
}
|
|
634
|
+
return normalizeRun(JSON.parse(raw));
|
|
635
|
+
}
|
|
636
|
+
function writeAgentUnlocked(agent) {
|
|
637
|
+
const path = agentPath(agent.run_id, agent.id);
|
|
638
|
+
atomicWriteJson(path, redactAgent(agent));
|
|
639
|
+
return path;
|
|
640
|
+
}
|
|
641
|
+
function writeRunHistoryUnlocked(record) {
|
|
642
|
+
const path = runHistoryPath(record.id);
|
|
643
|
+
atomicWriteJson(path, record);
|
|
644
|
+
return path;
|
|
645
|
+
}
|
|
646
|
+
function readAgentUnlocked(runId, agentId) {
|
|
647
|
+
return normalizeAgent(JSON.parse(readFileSync(agentPath(runId, agentId), "utf-8")));
|
|
648
|
+
}
|
|
649
|
+
function writeRun(run) {
|
|
650
|
+
return withRunsLock(() => writeRunUnlocked(run));
|
|
651
|
+
}
|
|
652
|
+
function readRun(runId) {
|
|
653
|
+
const run = readRunUnlocked(runId);
|
|
654
|
+
if (run.mode !== "spawner") throw new Error(`Run not found: ${runId}`);
|
|
655
|
+
return run;
|
|
656
|
+
}
|
|
657
|
+
function readRunAny(runId) {
|
|
658
|
+
return readRunUnlocked(runId);
|
|
659
|
+
}
|
|
660
|
+
function updateRun(runId, patch) {
|
|
661
|
+
withRunsLock(() => {
|
|
662
|
+
writeRunUnlocked({ ...readRunUnlocked(runId), ...patch });
|
|
663
|
+
});
|
|
664
|
+
}
|
|
665
|
+
function isRunEnded(run) {
|
|
666
|
+
return run.status === "ended" || run.ended_at !== null;
|
|
667
|
+
}
|
|
668
|
+
function listRunsUnlocked(includeAllModes, includeEnded = false) {
|
|
669
|
+
let entries;
|
|
670
|
+
try {
|
|
671
|
+
entries = readdirSync(runsDir());
|
|
672
|
+
} catch {
|
|
673
|
+
return [];
|
|
674
|
+
}
|
|
675
|
+
const runs = [];
|
|
676
|
+
for (const entry of entries) {
|
|
677
|
+
if (!existsSync(runPath(entry))) continue;
|
|
678
|
+
try {
|
|
679
|
+
const run = readRunUnlocked(entry);
|
|
680
|
+
if (!includeEnded && isRunEnded(run)) continue;
|
|
681
|
+
if (includeAllModes || run.mode === "spawner") runs.push(run);
|
|
682
|
+
} catch {
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
return runs.sort((a, b) => new Date(b.started_at).getTime() - new Date(a.started_at).getTime());
|
|
686
|
+
}
|
|
687
|
+
function listRunHistoryUnlocked(includeAllModes) {
|
|
688
|
+
let entries;
|
|
689
|
+
try {
|
|
690
|
+
entries = readdirSync(historyDir());
|
|
691
|
+
} catch {
|
|
692
|
+
return [];
|
|
693
|
+
}
|
|
694
|
+
const history = [];
|
|
695
|
+
for (const entry of entries) {
|
|
696
|
+
if (!entry.endsWith(".json")) continue;
|
|
697
|
+
try {
|
|
698
|
+
const record = normalizeRunHistory(JSON.parse(readFileSync(join(historyDir(), entry), "utf-8")));
|
|
699
|
+
if (includeAllModes || record.mode === "spawner") history.push(record);
|
|
700
|
+
} catch {
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
return history.sort((a, b) => new Date(b.archived_at).getTime() - new Date(a.archived_at).getTime());
|
|
704
|
+
}
|
|
705
|
+
function listRuns() {
|
|
706
|
+
return listRunsUnlocked(false);
|
|
707
|
+
}
|
|
708
|
+
function listRunsAny() {
|
|
709
|
+
return listRunsUnlocked(true);
|
|
710
|
+
}
|
|
711
|
+
function listRunHistory(options = {}) {
|
|
712
|
+
return listRunHistoryUnlocked(options.includeAllModes === true);
|
|
713
|
+
}
|
|
714
|
+
function removeRun(runId) {
|
|
715
|
+
withRunsLock(() => {
|
|
716
|
+
rmSync(runDir(runId), { recursive: true, force: true });
|
|
717
|
+
});
|
|
718
|
+
}
|
|
719
|
+
function deleteAgent(agentId, options = {}) {
|
|
720
|
+
return withRunsLock(() => {
|
|
721
|
+
const runs = listRunsUnlocked(options.includeAllModes === true);
|
|
722
|
+
for (const run of runs) {
|
|
723
|
+
try {
|
|
724
|
+
const agent = readAgentUnlocked(run.id, agentId);
|
|
725
|
+
if (!agent.ended_at) throw new Error("Stop agent before deleting it");
|
|
726
|
+
rmSync(agentPath(run.id, agent.id), { force: true });
|
|
727
|
+
return agent;
|
|
728
|
+
} catch (err) {
|
|
729
|
+
if (err instanceof Error && err.message === "Stop agent before deleting it") throw err;
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
throw new Error(`Agent not found: ${agentId}`);
|
|
733
|
+
});
|
|
734
|
+
}
|
|
735
|
+
function deleteRunHistory(runId) {
|
|
736
|
+
withRunsLock(() => {
|
|
737
|
+
rmSync(runHistoryPath(runId), { force: true });
|
|
738
|
+
});
|
|
739
|
+
}
|
|
740
|
+
function computeRunStatus(run, tmuxSessionExists = true) {
|
|
741
|
+
if (run.status === "ended" || run.ended_at !== null) return "ended";
|
|
742
|
+
return tmuxSessionExists ? "running" : "stale";
|
|
743
|
+
}
|
|
744
|
+
function writeAgent(agent) {
|
|
745
|
+
return withRunsLock(() => writeAgentUnlocked(agent));
|
|
746
|
+
}
|
|
747
|
+
function readAgent(runId, agentId) {
|
|
748
|
+
return readAgentUnlocked(runId, agentId);
|
|
749
|
+
}
|
|
750
|
+
function updateAgent(runId, agentId, patch) {
|
|
751
|
+
withRunsLock(() => {
|
|
752
|
+
writeAgentUnlocked({ ...readAgentUnlocked(runId, agentId), ...patch });
|
|
753
|
+
});
|
|
754
|
+
}
|
|
755
|
+
function listAgentsForRunIds(runIds) {
|
|
756
|
+
const agents = [];
|
|
757
|
+
for (const id of runIds) {
|
|
758
|
+
let files;
|
|
759
|
+
try {
|
|
760
|
+
files = readdirSync(agentsDir(id));
|
|
761
|
+
} catch {
|
|
762
|
+
continue;
|
|
763
|
+
}
|
|
764
|
+
for (const file of files) {
|
|
765
|
+
if (!file.endsWith(".json")) continue;
|
|
766
|
+
try {
|
|
767
|
+
agents.push(readAgentUnlocked(id, file.slice(0, -5)));
|
|
768
|
+
} catch {
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
return agents.sort((a, b) => {
|
|
773
|
+
if (a.run_id !== b.run_id) return a.run_id.localeCompare(b.run_id);
|
|
774
|
+
if (a.role !== b.role) return a.role === "root" ? -1 : 1;
|
|
775
|
+
return new Date(a.started_at).getTime() - new Date(b.started_at).getTime();
|
|
776
|
+
});
|
|
777
|
+
}
|
|
778
|
+
function buildRunHistoryRecord(run, status) {
|
|
779
|
+
const agents = listAgentsForRunIds([run.id]);
|
|
780
|
+
const root = agents.find((agent) => agent.role === "root" && (!run.root_agent_id || agent.id === run.root_agent_id)) ?? agents.find((agent) => agent.role === "root") ?? null;
|
|
781
|
+
return {
|
|
782
|
+
id: run.id,
|
|
783
|
+
name: run.name,
|
|
784
|
+
mode: run.mode === "orchestrator" ? "orchestrator" : "spawner",
|
|
785
|
+
status,
|
|
786
|
+
working_dir: run.working_dir,
|
|
787
|
+
started_at: run.started_at,
|
|
788
|
+
ended_at: run.ended_at,
|
|
789
|
+
archived_at: nowIso(),
|
|
790
|
+
agent_count: agents.length,
|
|
791
|
+
root_provider: root?.provider ?? null
|
|
792
|
+
};
|
|
793
|
+
}
|
|
794
|
+
function archiveAndRemoveRun(runId, status) {
|
|
795
|
+
return withRunsLock(() => {
|
|
796
|
+
const run = readRunUnlocked(runId);
|
|
797
|
+
const record = buildRunHistoryRecord(run, status);
|
|
798
|
+
writeRunHistoryUnlocked(record);
|
|
799
|
+
rmSync(runDir(runId), { recursive: true, force: true });
|
|
800
|
+
return record;
|
|
801
|
+
});
|
|
802
|
+
}
|
|
803
|
+
function endRunIfNoLiveAgents(runId, endedAt = nowIso()) {
|
|
804
|
+
return withRunsLock(() => {
|
|
805
|
+
const run = readRunUnlocked(runId);
|
|
806
|
+
if (run.status === "ended" || run.ended_at !== null) return run;
|
|
807
|
+
const liveAgents = listAgentsForRunIds([runId]).filter((agent) => !agent.ended_at);
|
|
808
|
+
if (liveAgents.length > 0) return run;
|
|
809
|
+
const endedRun = { ...run, status: "ended", ended_at: endedAt };
|
|
810
|
+
writeRunUnlocked(endedRun);
|
|
811
|
+
return endedRun;
|
|
812
|
+
});
|
|
813
|
+
}
|
|
814
|
+
function listAgents(runId) {
|
|
815
|
+
return listAgentsForRunIds(runId ? [runId] : listRuns().map((run) => run.id));
|
|
816
|
+
}
|
|
817
|
+
function listAgentsAny(runId) {
|
|
818
|
+
return listAgentsForRunIds(runId ? [runId] : listRunsAny().map((run) => run.id));
|
|
819
|
+
}
|
|
820
|
+
function findAgent(agentId) {
|
|
821
|
+
for (const run of listRuns()) {
|
|
822
|
+
try {
|
|
823
|
+
return readAgentUnlocked(run.id, agentId);
|
|
824
|
+
} catch {
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
throw new Error(`Agent not found: ${agentId}`);
|
|
828
|
+
}
|
|
829
|
+
function findAgentAny(agentId) {
|
|
830
|
+
for (const run of listRunsAny()) {
|
|
831
|
+
try {
|
|
832
|
+
return readAgentUnlocked(run.id, agentId);
|
|
833
|
+
} catch {
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
throw new Error(`Agent not found: ${agentId}`);
|
|
837
|
+
}
|
|
838
|
+
function defaultTmuxSessionExists(session) {
|
|
839
|
+
if (!session) return false;
|
|
840
|
+
try {
|
|
841
|
+
const result = spawnSync2("tmux", ["has-session", "-t", session], { stdio: "ignore" });
|
|
842
|
+
return result.status === 0;
|
|
843
|
+
} catch {
|
|
844
|
+
return false;
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
function defaultTmuxAvailable() {
|
|
848
|
+
try {
|
|
849
|
+
const result = spawnSync2("tmux", ["-V"], { stdio: "ignore" });
|
|
850
|
+
return result.status === 0;
|
|
851
|
+
} catch {
|
|
852
|
+
return false;
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
function defaultTmuxTargetExists(target) {
|
|
856
|
+
if (!target) return false;
|
|
857
|
+
try {
|
|
858
|
+
const format = target.startsWith("%") ? "#{pane_id}" : "#{window_id}";
|
|
859
|
+
const result = spawnSync2("tmux", ["display-message", "-p", "-t", target, format], { encoding: "utf8" });
|
|
860
|
+
return result.status === 0 && result.stdout.trim() === target;
|
|
861
|
+
} catch {
|
|
862
|
+
return false;
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
function runHasLiveTmuxTarget(run, options = {}) {
|
|
866
|
+
const targetExists = options.targetExists ?? defaultTmuxTargetExists;
|
|
867
|
+
const sessionExists = options.sessionExists ?? defaultTmuxSessionExists;
|
|
868
|
+
const agents = listAgents(run.id).filter((agent) => !agent.ended_at);
|
|
869
|
+
if (run.mode === "spawner") {
|
|
870
|
+
const windowedAgents2 = agents.filter((agent) => !agent.headless && agent.tmux_window_id);
|
|
871
|
+
if (windowedAgents2.length > 0) return windowedAgents2.some((agent) => targetExists(agent.tmux_window_id));
|
|
872
|
+
return sessionExists(run.tmux_session);
|
|
873
|
+
}
|
|
874
|
+
const rootAgent = agents.find((agent) => agent.role === "root" && (!run.root_agent_id || agent.id === run.root_agent_id));
|
|
875
|
+
if (!rootAgent) return false;
|
|
876
|
+
const windowedAgents = agents.filter((agent) => !agent.headless && agent.tmux_window_id);
|
|
877
|
+
if (windowedAgents.length > 0) {
|
|
878
|
+
return windowedAgents.some((agent) => targetExists(agent.tmux_window_id));
|
|
879
|
+
}
|
|
880
|
+
return sessionExists(run.tmux_session);
|
|
881
|
+
}
|
|
882
|
+
function autoCleanupRuns(options = {}) {
|
|
883
|
+
const sessionExists = options.sessionExists ?? defaultTmuxSessionExists;
|
|
884
|
+
const targetExists = options.targetExists ?? defaultTmuxTargetExists;
|
|
885
|
+
const tmuxAvailable = options.sessionExists || options.targetExists ? () => true : options.tmuxAvailable ?? defaultTmuxAvailable;
|
|
886
|
+
const haveTmux = options.cleanStale === false ? false : tmuxAvailable();
|
|
887
|
+
const removed = [];
|
|
888
|
+
const archived = [];
|
|
889
|
+
const runs = listRunsUnlocked(options.includeAllModes === true, true);
|
|
890
|
+
for (const run of runs) {
|
|
891
|
+
const isEnded = isRunEnded(run);
|
|
892
|
+
const isStale = !isEnded && haveTmux && !runHasLiveTmuxTarget(run, { sessionExists, targetExists });
|
|
893
|
+
if (!isEnded && !isStale) continue;
|
|
894
|
+
try {
|
|
895
|
+
const record = archiveAndRemoveRun(run.id, isStale ? "stale" : "ended");
|
|
896
|
+
removed.push(run.id);
|
|
897
|
+
archived.push(record.id);
|
|
898
|
+
} catch {
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
return { removed, archived };
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
// src/i18n/languages.ts
|
|
905
|
+
var LANGUAGES = ["en", "de", "fr", "es", "pt", "it", "tr", "zh-Hans", "ar"];
|
|
906
|
+
var LANGUAGE_OPTIONS = [
|
|
907
|
+
{ code: "en", flag: "\u{1F1FA}\u{1F1F8}", name: "English", nativeName: "English", dir: "ltr" },
|
|
908
|
+
{ code: "de", flag: "\u{1F1E9}\u{1F1EA}", name: "German", nativeName: "Deutsch", dir: "ltr" },
|
|
909
|
+
{ code: "fr", flag: "\u{1F1EB}\u{1F1F7}", name: "French", nativeName: "Fran\xE7ais", dir: "ltr" },
|
|
910
|
+
{ code: "es", flag: "\u{1F1EA}\u{1F1F8}", name: "Spanish", nativeName: "Espa\xF1ol", dir: "ltr" },
|
|
911
|
+
{ code: "pt", flag: "\u{1F1F5}\u{1F1F9}", name: "Portuguese", nativeName: "Portugu\xEAs", dir: "ltr" },
|
|
912
|
+
{ code: "it", flag: "\u{1F1EE}\u{1F1F9}", name: "Italian", nativeName: "Italiano", dir: "ltr" },
|
|
913
|
+
{ code: "tr", flag: "\u{1F1F9}\u{1F1F7}", name: "Turkish", nativeName: "T\xFCrk\xE7e", dir: "ltr" },
|
|
914
|
+
{ code: "zh-Hans", flag: "\u{1F1E8}\u{1F1F3}", name: "Simplified Chinese", nativeName: "\u7B80\u4F53\u4E2D\u6587", dir: "ltr" },
|
|
915
|
+
{ code: "ar", flag: "\u{1F1F8}\u{1F1E6}", name: "Arabic", nativeName: "\u0627\u0644\u0639\u0631\u0628\u064A\u0629", dir: "rtl" }
|
|
916
|
+
];
|
|
917
|
+
var DEFAULT_LANGUAGE = "en";
|
|
918
|
+
function isLanguageCode(value) {
|
|
919
|
+
return typeof value === "string" && LANGUAGES.includes(value);
|
|
920
|
+
}
|
|
921
|
+
function languageOption(code) {
|
|
922
|
+
return LANGUAGE_OPTIONS.find((option) => option.code === code) ?? LANGUAGE_OPTIONS[0];
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
// src/state/config.ts
|
|
926
|
+
import { readFileSync as readFileSync2, writeFileSync as writeFileSync2, mkdirSync as mkdirSync2, renameSync as renameSync2 } from "fs";
|
|
927
|
+
import { homedir as homedir2 } from "os";
|
|
928
|
+
import { join as join2, dirname as dirname2 } from "path";
|
|
929
|
+
var SCHEMA_VERSION = 2;
|
|
930
|
+
var DEFAULT_GLOBAL = {
|
|
931
|
+
peek_interval_ms: 5e3,
|
|
932
|
+
peek_lines: 10,
|
|
933
|
+
max_depth: 5,
|
|
934
|
+
max_agents: 10,
|
|
935
|
+
ready_delay_ms: 5e3,
|
|
936
|
+
default_permissions: "ask",
|
|
937
|
+
language: DEFAULT_LANGUAGE
|
|
938
|
+
};
|
|
939
|
+
function configPath() {
|
|
940
|
+
const base = process.env.REEVES_CONFIG;
|
|
941
|
+
if (base) return base;
|
|
942
|
+
return join2(homedir2(), ".reeves", "config.json");
|
|
943
|
+
}
|
|
944
|
+
function configExists() {
|
|
945
|
+
try {
|
|
946
|
+
readFileSync2(configPath(), "utf-8");
|
|
947
|
+
return true;
|
|
948
|
+
} catch {
|
|
949
|
+
return false;
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
function defaultConfig() {
|
|
953
|
+
return {
|
|
954
|
+
version: SCHEMA_VERSION,
|
|
955
|
+
global: { ...DEFAULT_GLOBAL }
|
|
956
|
+
};
|
|
957
|
+
}
|
|
958
|
+
function mergeDefaults(raw) {
|
|
959
|
+
if (typeof raw !== "object" || raw === null) return defaultConfig();
|
|
960
|
+
const obj = raw;
|
|
961
|
+
const merged = {
|
|
962
|
+
version: typeof obj.version === "number" ? obj.version : SCHEMA_VERSION,
|
|
963
|
+
global: { ...DEFAULT_GLOBAL }
|
|
964
|
+
};
|
|
965
|
+
if (typeof obj.global === "object" && obj.global !== null) {
|
|
966
|
+
const g = obj.global;
|
|
967
|
+
if (typeof g.peek_interval_ms === "number" && g.peek_interval_ms > 0) merged.global.peek_interval_ms = g.peek_interval_ms;
|
|
968
|
+
if (typeof g.peek_lines === "number" && g.peek_lines > 0) merged.global.peek_lines = g.peek_lines;
|
|
969
|
+
if (typeof g.max_depth === "number" && g.max_depth > 0) merged.global.max_depth = g.max_depth;
|
|
970
|
+
if (typeof g.max_agents === "number" && g.max_agents > 0) merged.global.max_agents = g.max_agents;
|
|
971
|
+
if (typeof g.ready_delay_ms === "number" && g.ready_delay_ms >= 0) merged.global.ready_delay_ms = g.ready_delay_ms;
|
|
972
|
+
if (g.default_permissions === "skip" || g.default_permissions === "ask") merged.global.default_permissions = g.default_permissions;
|
|
973
|
+
if (isLanguageCode(g.language)) merged.global.language = g.language;
|
|
974
|
+
}
|
|
975
|
+
return merged;
|
|
976
|
+
}
|
|
977
|
+
function loadConfig() {
|
|
978
|
+
try {
|
|
979
|
+
const content = readFileSync2(configPath(), "utf-8");
|
|
980
|
+
return mergeDefaults(JSON.parse(content));
|
|
981
|
+
} catch {
|
|
982
|
+
return defaultConfig();
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
function saveConfig(cfg) {
|
|
986
|
+
const path = configPath();
|
|
987
|
+
const dir = dirname2(path);
|
|
988
|
+
mkdirSync2(dir, { recursive: true });
|
|
989
|
+
const tmp = `${path}.tmp`;
|
|
990
|
+
writeFileSync2(tmp, JSON.stringify(cfg, null, 2), "utf-8");
|
|
991
|
+
try {
|
|
992
|
+
renameSync2(tmp, path);
|
|
993
|
+
} catch {
|
|
994
|
+
writeFileSync2(path, JSON.stringify(cfg, null, 2), "utf-8");
|
|
995
|
+
}
|
|
996
|
+
return path;
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
// src/launcher/runtime.ts
|
|
1000
|
+
import { execFileSync as execFileSync2 } from "child_process";
|
|
1001
|
+
import { randomUUID as randomUUID2 } from "crypto";
|
|
1002
|
+
import stripAnsi from "strip-ansi";
|
|
1003
|
+
|
|
1004
|
+
// src/launcher/provider-launch.ts
|
|
1005
|
+
import { existsSync as existsSync2, statSync as statSync2 } from "fs";
|
|
1006
|
+
import { homedir as homedir3 } from "os";
|
|
1007
|
+
import { join as join3 } from "path";
|
|
1008
|
+
function expandHome(p) {
|
|
1009
|
+
if (p === "~") return homedir3();
|
|
1010
|
+
if (p.startsWith("~/")) return join3(homedir3(), p.slice(2));
|
|
1011
|
+
return p;
|
|
1012
|
+
}
|
|
1013
|
+
function resolveWorkingDir(requested, fallback) {
|
|
1014
|
+
if (!requested) return fallback;
|
|
1015
|
+
const expanded = expandHome(requested.trim());
|
|
1016
|
+
if (!expanded) return fallback;
|
|
1017
|
+
if (existsSync2(expanded) && statSync2(expanded).isDirectory()) return expanded;
|
|
1018
|
+
throw new Error(`Working directory does not exist: ${expanded}`);
|
|
1019
|
+
}
|
|
1020
|
+
function shellQuote(s) {
|
|
1021
|
+
return `'${s.replace(/'/g, "'\\''")}'`;
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
// src/launcher/runtime.ts
|
|
1025
|
+
var realDriver = {
|
|
1026
|
+
tmux(args, input) {
|
|
1027
|
+
return execFileSync2("tmux", args, {
|
|
1028
|
+
encoding: "utf8",
|
|
1029
|
+
input,
|
|
1030
|
+
stdio: input === void 0 ? ["ignore", "pipe", "pipe"] : ["pipe", "pipe", "pipe"]
|
|
1031
|
+
}).trim();
|
|
1032
|
+
},
|
|
1033
|
+
delay(fn, ms) {
|
|
1034
|
+
setTimeout(fn, ms);
|
|
1035
|
+
}
|
|
1036
|
+
};
|
|
1037
|
+
var POST_PASTE_ENTER_DELAY_MS = 1e3;
|
|
1038
|
+
var STARTUP_READY_POLL_MS = 1e3;
|
|
1039
|
+
var STARTUP_READY_TIMEOUT_MS = 2e4;
|
|
1040
|
+
var lastPasteAtByPane = /* @__PURE__ */ new Map();
|
|
1041
|
+
function sleepSync2(ms) {
|
|
1042
|
+
const buffer = new SharedArrayBuffer(4);
|
|
1043
|
+
const view = new Int32Array(buffer);
|
|
1044
|
+
Atomics.wait(view, 0, 0, ms);
|
|
1045
|
+
}
|
|
1046
|
+
function waitAfterPaste(driver) {
|
|
1047
|
+
if (driver === realDriver) sleepSync2(POST_PASTE_ENTER_DELAY_MS);
|
|
1048
|
+
else driver.delay(() => {
|
|
1049
|
+
}, POST_PASTE_ENTER_DELAY_MS);
|
|
1050
|
+
}
|
|
1051
|
+
function normalizePaneOutput(output) {
|
|
1052
|
+
return stripAnsi(output).replace(/\s+/g, " ").trim().toLowerCase();
|
|
1053
|
+
}
|
|
1054
|
+
function paneLooksReady(output, previousOutput) {
|
|
1055
|
+
const current = normalizePaneOutput(output);
|
|
1056
|
+
if (!current) return false;
|
|
1057
|
+
if (/\bready\b/.test(current)) return true;
|
|
1058
|
+
if (current.includes("type a message") || current.includes("enter your prompt")) return true;
|
|
1059
|
+
if (current.includes("what would you like") || current.includes("what do you want")) return true;
|
|
1060
|
+
return !!previousOutput && current === normalizePaneOutput(previousOutput);
|
|
1061
|
+
}
|
|
1062
|
+
function pasteTextToPane(driver, paneId, text) {
|
|
1063
|
+
const bufferName = `reeves_${randomUUID2().slice(0, 8)}`;
|
|
1064
|
+
driver.tmux(["load-buffer", "-b", bufferName, "-"], text);
|
|
1065
|
+
try {
|
|
1066
|
+
driver.tmux(["paste-buffer", "-b", bufferName, "-t", paneId]);
|
|
1067
|
+
} finally {
|
|
1068
|
+
try {
|
|
1069
|
+
driver.tmux(["delete-buffer", "-b", bufferName]);
|
|
1070
|
+
} catch {
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
function sanitizeName(raw) {
|
|
1075
|
+
const cleaned = raw.trim().replace(/[^a-zA-Z0-9_-]/g, "-").replace(/-+/g, "-").slice(0, 40);
|
|
1076
|
+
return cleaned || "run";
|
|
1077
|
+
}
|
|
1078
|
+
function tmuxWindowName(provider, nickname) {
|
|
1079
|
+
return sanitizeName(nickname || provider).slice(0, 64);
|
|
1080
|
+
}
|
|
1081
|
+
function tmuxRunSessionName(runId, rawName) {
|
|
1082
|
+
return `reeves-${sanitizeName(rawName).slice(0, 24)}-${runId.slice(0, 8)}`.slice(0, 64);
|
|
1083
|
+
}
|
|
1084
|
+
function childProcessOutput(err) {
|
|
1085
|
+
if (typeof err !== "object" || err === null) return "";
|
|
1086
|
+
const maybe = err;
|
|
1087
|
+
const stderr = Buffer.isBuffer(maybe.stderr) ? maybe.stderr.toString("utf8").trim() : "";
|
|
1088
|
+
if (stderr) return stderr;
|
|
1089
|
+
const stdout = Buffer.isBuffer(maybe.stdout) ? maybe.stdout.toString("utf8").trim() : "";
|
|
1090
|
+
return stdout;
|
|
1091
|
+
}
|
|
1092
|
+
function parseTmuxIds(output) {
|
|
1093
|
+
const [windowId, paneId] = output.trim().split(/\s+/);
|
|
1094
|
+
if (!windowId?.startsWith("@") || !paneId?.startsWith("%")) {
|
|
1095
|
+
throw new Error(`Could not parse tmux ids from: ${output}`);
|
|
1096
|
+
}
|
|
1097
|
+
return { windowId, paneId };
|
|
1098
|
+
}
|
|
1099
|
+
function readDisplayIds(driver, target) {
|
|
1100
|
+
try {
|
|
1101
|
+
const args = ["display-message", "-p"];
|
|
1102
|
+
if (target) args.push("-t", target);
|
|
1103
|
+
args.push("#{window_id} #{pane_id}");
|
|
1104
|
+
return parseTmuxIds(driver.tmux(args));
|
|
1105
|
+
} catch {
|
|
1106
|
+
return null;
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
function requireProvider(provider, available) {
|
|
1110
|
+
if (!isProvider(provider)) throw new Error(`Unsupported provider: ${String(provider)}`);
|
|
1111
|
+
if (!available[provider]) throw new Error(`${providerDisplayName(provider)} not found on PATH`);
|
|
1112
|
+
}
|
|
1113
|
+
function pickReevesAnchor(driver) {
|
|
1114
|
+
if (process.env.TMUX) {
|
|
1115
|
+
try {
|
|
1116
|
+
const sessionName = driver.tmux(["display-message", "-p", "#S"]).trim();
|
|
1117
|
+
const ids2 = readDisplayIds(driver);
|
|
1118
|
+
if (sessionName && ids2) {
|
|
1119
|
+
return { sessionName, windowId: ids2.windowId, paneId: ids2.paneId };
|
|
1120
|
+
}
|
|
1121
|
+
} catch {
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
const fallbackName = "reeves";
|
|
1125
|
+
try {
|
|
1126
|
+
driver.tmux(["new-session", "-d", "-s", fallbackName, "-n", "reeves"]);
|
|
1127
|
+
} catch {
|
|
1128
|
+
}
|
|
1129
|
+
let ids = readDisplayIds(driver, `${fallbackName}:reeves`);
|
|
1130
|
+
if (!ids) {
|
|
1131
|
+
try {
|
|
1132
|
+
driver.tmux(["new-window", "-d", "-t", fallbackName, "-n", "reeves"]);
|
|
1133
|
+
} catch {
|
|
1134
|
+
}
|
|
1135
|
+
ids = readDisplayIds(driver, `${fallbackName}:reeves`);
|
|
1136
|
+
}
|
|
1137
|
+
return { sessionName: fallbackName, windowId: ids?.windowId ?? "", paneId: ids?.paneId ?? "" };
|
|
1138
|
+
}
|
|
1139
|
+
function createRunSessionWithReevesTab(driver, tmuxSession, reevesAnchor, workingDir) {
|
|
1140
|
+
driver.tmux(["new-session", "-d", "-s", tmuxSession, "-n", "reeves", "-c", workingDir]);
|
|
1141
|
+
const source = reevesAnchor.windowId || `${reevesAnchor.sessionName}:reeves`;
|
|
1142
|
+
try {
|
|
1143
|
+
driver.tmux(["link-window", "-k", "-s", source, "-t", `${tmuxSession}:0`]);
|
|
1144
|
+
} catch {
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
function terminalShellCommand(config, permissions) {
|
|
1148
|
+
const cmd = buildCommand({
|
|
1149
|
+
provider: config.provider,
|
|
1150
|
+
permissions,
|
|
1151
|
+
model: config.model,
|
|
1152
|
+
auth_mode: config.auth_mode,
|
|
1153
|
+
effort: config.effort,
|
|
1154
|
+
rc_enabled: false
|
|
1155
|
+
});
|
|
1156
|
+
return `exec ${cmd.map(shellQuote).join(" ")}`;
|
|
1157
|
+
}
|
|
1158
|
+
function sendDelayedStartupInput(driver, agent, config, readyDelayMs) {
|
|
1159
|
+
const task = config.task.trim();
|
|
1160
|
+
function sendStartupTask() {
|
|
1161
|
+
pasteTextToPane(driver, agent.tmux_pane_id, task);
|
|
1162
|
+
lastPasteAtByPane.set(agent.tmux_pane_id, Date.now());
|
|
1163
|
+
waitAfterPaste(driver);
|
|
1164
|
+
driver.tmux(["send-keys", "-t", agent.tmux_pane_id, "Enter"]);
|
|
1165
|
+
const current = readAgent(agent.run_id, agent.id);
|
|
1166
|
+
if (!current.ended_at && current.task_status === "queued") {
|
|
1167
|
+
updateAgent(agent.run_id, agent.id, { task_status: "working" });
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
function waitForReadyThenSend(previousOutput = "", waitedMs = 0) {
|
|
1171
|
+
try {
|
|
1172
|
+
const output = driver.tmux(["capture-pane", "-p", "-e", "-S", "-80", "-t", agent.tmux_pane_id]);
|
|
1173
|
+
if (paneLooksReady(output, previousOutput) || waitedMs >= STARTUP_READY_TIMEOUT_MS) {
|
|
1174
|
+
sendStartupTask();
|
|
1175
|
+
return;
|
|
1176
|
+
}
|
|
1177
|
+
driver.delay(() => waitForReadyThenSend(output, waitedMs + STARTUP_READY_POLL_MS), STARTUP_READY_POLL_MS);
|
|
1178
|
+
} catch {
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1181
|
+
if (!task) return;
|
|
1182
|
+
driver.delay(() => waitForReadyThenSend(), readyDelayMs);
|
|
1183
|
+
}
|
|
1184
|
+
function newAgentRecord(id, runId, tmuxSession, role, config, workingDir, ids, permissions) {
|
|
1185
|
+
return {
|
|
1186
|
+
id,
|
|
1187
|
+
run_id: runId,
|
|
1188
|
+
nickname: config.nickname || (role === "root" ? providerDisplayName(config.provider) : `${providerDisplayName(config.provider)} agent`),
|
|
1189
|
+
provider: config.provider,
|
|
1190
|
+
model: config.model,
|
|
1191
|
+
role,
|
|
1192
|
+
working_dir: workingDir,
|
|
1193
|
+
task: config.task.trim(),
|
|
1194
|
+
task_status: "queued",
|
|
1195
|
+
task_note: "",
|
|
1196
|
+
tmux_session: tmuxSession,
|
|
1197
|
+
tmux_window_id: ids.windowId,
|
|
1198
|
+
tmux_pane_id: ids.paneId,
|
|
1199
|
+
rc_enabled: false,
|
|
1200
|
+
permissions,
|
|
1201
|
+
inbox: [],
|
|
1202
|
+
last_seen: nowMs(),
|
|
1203
|
+
started_at: nowIso(),
|
|
1204
|
+
ended_at: null
|
|
1205
|
+
};
|
|
1206
|
+
}
|
|
1207
|
+
function createAgentWindow(runId, tmuxSession, role, config, inheritedWorkingDir, readyDelayMs, driver) {
|
|
1208
|
+
const cfg = loadConfig();
|
|
1209
|
+
const id = randomUUID2();
|
|
1210
|
+
const workingDir = resolveWorkingDir(config.working_dir, inheritedWorkingDir);
|
|
1211
|
+
const permissions = config.permissions ?? cfg.global.default_permissions;
|
|
1212
|
+
const shellCommand = terminalShellCommand(config, permissions);
|
|
1213
|
+
const nickname = config.nickname || providerDisplayName(config.provider);
|
|
1214
|
+
const output = driver.tmux([
|
|
1215
|
+
"new-window",
|
|
1216
|
+
"-d",
|
|
1217
|
+
"-P",
|
|
1218
|
+
"-F",
|
|
1219
|
+
"#{window_id} #{pane_id}",
|
|
1220
|
+
"-t",
|
|
1221
|
+
`${tmuxSession}:`,
|
|
1222
|
+
"-n",
|
|
1223
|
+
tmuxWindowName(config.provider, nickname),
|
|
1224
|
+
"-c",
|
|
1225
|
+
workingDir,
|
|
1226
|
+
shellCommand
|
|
1227
|
+
]);
|
|
1228
|
+
const agent = newAgentRecord(id, runId, tmuxSession, role, config, workingDir, parseTmuxIds(output), permissions);
|
|
1229
|
+
writeAgent(agent);
|
|
1230
|
+
sendDelayedStartupInput(driver, agent, config, readyDelayMs);
|
|
1231
|
+
return agent;
|
|
1232
|
+
}
|
|
1233
|
+
function validateAgents(configs, available) {
|
|
1234
|
+
for (const config of configs) requireProvider(config.provider, available);
|
|
1235
|
+
}
|
|
1236
|
+
function assertSpawnerMode(mode) {
|
|
1237
|
+
if (mode !== void 0 && mode !== "spawner") {
|
|
1238
|
+
throw new Error("Only agent-run mode is available in this package");
|
|
1239
|
+
}
|
|
1240
|
+
}
|
|
1241
|
+
function startRun(request, options = {}) {
|
|
1242
|
+
assertSpawnerMode(request.mode);
|
|
1243
|
+
const cfg = loadConfig();
|
|
1244
|
+
const driver = options.driver ?? realDriver;
|
|
1245
|
+
const available = options.available ?? detectAvailable();
|
|
1246
|
+
const workers = request.workers ?? [];
|
|
1247
|
+
validateAgents([request.root, ...workers], available);
|
|
1248
|
+
const runId = randomUUID2();
|
|
1249
|
+
const readyDelayMs = request.ready_delay_ms ?? cfg.global.ready_delay_ms;
|
|
1250
|
+
const workingDir = resolveWorkingDir(request.working_dir, process.cwd());
|
|
1251
|
+
const reevesAnchor = pickReevesAnchor(driver);
|
|
1252
|
+
const tmuxSession = tmuxRunSessionName(runId, request.name);
|
|
1253
|
+
let createdRunSession = false;
|
|
1254
|
+
try {
|
|
1255
|
+
createRunSessionWithReevesTab(driver, tmuxSession, reevesAnchor, workingDir);
|
|
1256
|
+
createdRunSession = true;
|
|
1257
|
+
const root = createAgentWindow(runId, tmuxSession, "root", request.root, workingDir, readyDelayMs, driver);
|
|
1258
|
+
const run = {
|
|
1259
|
+
id: runId,
|
|
1260
|
+
mode: "spawner",
|
|
1261
|
+
name: request.name,
|
|
1262
|
+
status: "running",
|
|
1263
|
+
tmux_session: tmuxSession,
|
|
1264
|
+
reeves_session: reevesAnchor.sessionName,
|
|
1265
|
+
reeves_window_id: reevesAnchor.windowId,
|
|
1266
|
+
reeves_pane_id: reevesAnchor.paneId,
|
|
1267
|
+
root_agent_id: root.id,
|
|
1268
|
+
working_dir: workingDir,
|
|
1269
|
+
preset_name: request.preset_name ?? null,
|
|
1270
|
+
started_at: nowIso(),
|
|
1271
|
+
ended_at: null
|
|
1272
|
+
};
|
|
1273
|
+
writeRun(run);
|
|
1274
|
+
const workerAgents = workers.map((worker) => {
|
|
1275
|
+
return createAgentWindow(runId, tmuxSession, "worker", worker, workingDir, readyDelayMs, driver);
|
|
1276
|
+
});
|
|
1277
|
+
return { run, agents: [root, ...workerAgents] };
|
|
1278
|
+
} catch (err) {
|
|
1279
|
+
if (createdRunSession) {
|
|
1280
|
+
try {
|
|
1281
|
+
driver.tmux(["kill-session", "-t", tmuxSession]);
|
|
1282
|
+
} catch {
|
|
1283
|
+
}
|
|
1284
|
+
}
|
|
1285
|
+
removeRun(runId);
|
|
1286
|
+
const detail = childProcessOutput(err) || (err instanceof Error ? err.message : "tmux returned a non-zero exit code");
|
|
1287
|
+
throw new Error(`Failed to start run ${request.name}: ${detail}`, { cause: err });
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1290
|
+
function spawnWorker(request, options = {}) {
|
|
1291
|
+
const cfg = loadConfig();
|
|
1292
|
+
const driver = options.driver ?? realDriver;
|
|
1293
|
+
const available = options.available ?? detectAvailable();
|
|
1294
|
+
requireProvider(request.provider, available);
|
|
1295
|
+
const run = readRun(request.run_id);
|
|
1296
|
+
if (run.status === "ended" || run.ended_at) throw new Error(`Run is ended: ${run.id}`);
|
|
1297
|
+
if (run.mode !== "spawner") {
|
|
1298
|
+
throw new Error("Run not found");
|
|
1299
|
+
}
|
|
1300
|
+
return createAgentWindow(
|
|
1301
|
+
run.id,
|
|
1302
|
+
run.tmux_session,
|
|
1303
|
+
"worker",
|
|
1304
|
+
request,
|
|
1305
|
+
run.working_dir,
|
|
1306
|
+
request.ready_delay_ms ?? cfg.global.ready_delay_ms,
|
|
1307
|
+
driver
|
|
1308
|
+
);
|
|
1309
|
+
}
|
|
1310
|
+
function openReeves(runId, options = {}) {
|
|
1311
|
+
const driver = options.driver ?? realDriver;
|
|
1312
|
+
const run = readRun(runId);
|
|
1313
|
+
if (!run.reeves_window_id) throw new Error("Reeves TUI window is unavailable for this run");
|
|
1314
|
+
const target = `${run.reeves_session ?? run.tmux_session}:${run.reeves_window_id}`;
|
|
1315
|
+
try {
|
|
1316
|
+
driver.tmux(["switch-client", "-t", target]);
|
|
1317
|
+
} catch {
|
|
1318
|
+
}
|
|
1319
|
+
driver.tmux(["select-window", "-t", target]);
|
|
1320
|
+
}
|
|
1321
|
+
function openRunTabs(runId, options = {}) {
|
|
1322
|
+
const driver = options.driver ?? realDriver;
|
|
1323
|
+
const run = readRun(runId);
|
|
1324
|
+
const target = `${run.tmux_session}:reeves`;
|
|
1325
|
+
try {
|
|
1326
|
+
driver.tmux(["switch-client", "-t", target]);
|
|
1327
|
+
} catch {
|
|
1328
|
+
}
|
|
1329
|
+
driver.tmux(["select-window", "-t", target]);
|
|
1330
|
+
}
|
|
1331
|
+
function openAgent(agentId, options = {}) {
|
|
1332
|
+
const driver = options.driver ?? realDriver;
|
|
1333
|
+
const agent = findAgent(agentId);
|
|
1334
|
+
if (agent.headless || !agent.tmux_window_id) throw new Error("This agent is headless - no tmux window exists");
|
|
1335
|
+
const target = `${agent.tmux_session}:${agent.tmux_window_id}`;
|
|
1336
|
+
try {
|
|
1337
|
+
driver.tmux(["switch-client", "-t", target]);
|
|
1338
|
+
} catch {
|
|
1339
|
+
}
|
|
1340
|
+
driver.tmux(["select-window", "-t", target]);
|
|
1341
|
+
}
|
|
1342
|
+
function peekAgent(agentId, lines = 10, options = {}) {
|
|
1343
|
+
const driver = options.driver ?? realDriver;
|
|
1344
|
+
try {
|
|
1345
|
+
const agent = findAgent(agentId);
|
|
1346
|
+
if (agent.headless || !agent.tmux_pane_id) return "(headless agent - no output)";
|
|
1347
|
+
let output = driver.tmux(["capture-pane", "-p", "-e", "-S", String(-lines), "-t", agent.tmux_pane_id]);
|
|
1348
|
+
if (!output.trim()) {
|
|
1349
|
+
try {
|
|
1350
|
+
output = driver.tmux(["capture-pane", "-p", "-e", "-a", "-S", String(-lines), "-t", agent.tmux_pane_id]);
|
|
1351
|
+
} catch {
|
|
1352
|
+
}
|
|
1353
|
+
}
|
|
1354
|
+
return redactSecrets(stripAnsi(output).trim());
|
|
1355
|
+
} catch {
|
|
1356
|
+
return "";
|
|
1357
|
+
}
|
|
1358
|
+
}
|
|
1359
|
+
function killAgent(agentId, options = {}) {
|
|
1360
|
+
const driver = options.driver ?? realDriver;
|
|
1361
|
+
const agent = findAgent(agentId);
|
|
1362
|
+
const run = readRun(agent.run_id);
|
|
1363
|
+
if (run.mode !== "spawner") {
|
|
1364
|
+
throw new Error("Run not found");
|
|
1365
|
+
}
|
|
1366
|
+
try {
|
|
1367
|
+
driver.tmux(["kill-window", "-t", agent.tmux_window_id]);
|
|
1368
|
+
} catch {
|
|
1369
|
+
}
|
|
1370
|
+
const endedAt = nowIso();
|
|
1371
|
+
updateAgent(agent.run_id, agent.id, { ended_at: endedAt, task_status: "done" });
|
|
1372
|
+
const updatedAgent = readAgent(agent.run_id, agent.id);
|
|
1373
|
+
const endedRun = endRunIfNoLiveAgents(agent.run_id, endedAt);
|
|
1374
|
+
if (endedRun.status === "ended" || endedRun.ended_at !== null) {
|
|
1375
|
+
try {
|
|
1376
|
+
driver.tmux(["kill-session", "-t", run.tmux_session]);
|
|
1377
|
+
} catch {
|
|
1378
|
+
}
|
|
1379
|
+
archiveAndRemoveRun(run.id, "ended");
|
|
1380
|
+
}
|
|
1381
|
+
return updatedAgent;
|
|
1382
|
+
}
|
|
1383
|
+
function stopRun(runId, options = {}) {
|
|
1384
|
+
const driver = options.driver ?? realDriver;
|
|
1385
|
+
const run = readRun(runId);
|
|
1386
|
+
const endedAt = nowIso();
|
|
1387
|
+
const runOwnsSession = !!run.reeves_session && run.reeves_session !== run.tmux_session;
|
|
1388
|
+
if (runOwnsSession) {
|
|
1389
|
+
try {
|
|
1390
|
+
driver.tmux(["kill-session", "-t", run.tmux_session]);
|
|
1391
|
+
} catch {
|
|
1392
|
+
}
|
|
1393
|
+
}
|
|
1394
|
+
for (const agent of listAgents(run.id)) {
|
|
1395
|
+
if (agent.ended_at) continue;
|
|
1396
|
+
if (!runOwnsSession && agent.tmux_window_id) {
|
|
1397
|
+
try {
|
|
1398
|
+
driver.tmux(["kill-window", "-t", agent.tmux_window_id]);
|
|
1399
|
+
} catch {
|
|
1400
|
+
}
|
|
1401
|
+
}
|
|
1402
|
+
updateAgent(run.id, agent.id, { ended_at: endedAt });
|
|
1403
|
+
}
|
|
1404
|
+
const endedRun = { ...run, status: "ended", ended_at: endedAt };
|
|
1405
|
+
updateRun(run.id, { status: endedRun.status, ended_at: endedRun.ended_at });
|
|
1406
|
+
archiveAndRemoveRun(run.id, "ended");
|
|
1407
|
+
return endedRun;
|
|
1408
|
+
}
|
|
1409
|
+
|
|
1410
|
+
export {
|
|
1411
|
+
BIN,
|
|
1412
|
+
PROVIDERS,
|
|
1413
|
+
isProvider,
|
|
1414
|
+
normalizeProvider,
|
|
1415
|
+
buildCommand,
|
|
1416
|
+
detectAvailable,
|
|
1417
|
+
inspectProviderCompatibility,
|
|
1418
|
+
colors,
|
|
1419
|
+
sep,
|
|
1420
|
+
space,
|
|
1421
|
+
providerDisplayName,
|
|
1422
|
+
providerColor,
|
|
1423
|
+
modelColor,
|
|
1424
|
+
modelBadgeLabel,
|
|
1425
|
+
stateRoot,
|
|
1426
|
+
runsDir,
|
|
1427
|
+
nowIso,
|
|
1428
|
+
readRun,
|
|
1429
|
+
readRunAny,
|
|
1430
|
+
listRuns,
|
|
1431
|
+
listRunsAny,
|
|
1432
|
+
listRunHistory,
|
|
1433
|
+
deleteAgent,
|
|
1434
|
+
deleteRunHistory,
|
|
1435
|
+
computeRunStatus,
|
|
1436
|
+
readAgent,
|
|
1437
|
+
archiveAndRemoveRun,
|
|
1438
|
+
listAgents,
|
|
1439
|
+
listAgentsAny,
|
|
1440
|
+
findAgent,
|
|
1441
|
+
findAgentAny,
|
|
1442
|
+
runHasLiveTmuxTarget,
|
|
1443
|
+
autoCleanupRuns,
|
|
1444
|
+
LANGUAGE_OPTIONS,
|
|
1445
|
+
DEFAULT_LANGUAGE,
|
|
1446
|
+
isLanguageCode,
|
|
1447
|
+
languageOption,
|
|
1448
|
+
configPath,
|
|
1449
|
+
configExists,
|
|
1450
|
+
defaultConfig,
|
|
1451
|
+
loadConfig,
|
|
1452
|
+
saveConfig,
|
|
1453
|
+
startRun,
|
|
1454
|
+
spawnWorker,
|
|
1455
|
+
openReeves,
|
|
1456
|
+
openRunTabs,
|
|
1457
|
+
openAgent,
|
|
1458
|
+
peekAgent,
|
|
1459
|
+
killAgent,
|
|
1460
|
+
stopRun
|
|
1461
|
+
};
|
|
1462
|
+
//# sourceMappingURL=chunk-MVMGFG4W.js.map
|