@cloop/runner 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{box-SQVB3OUM.js → box-BS6BSQUO.js} +24 -15
- package/dist/{chunk-SQJ7ZO4G.js → chunk-3DIGCVL4.js} +1 -0
- package/dist/{chunk-7K5W6ELQ.js → chunk-7BREDPJD.js} +1 -1
- package/dist/{chunk-IIUHGZKX.js → chunk-7PUGLQL2.js} +1277 -1337
- package/dist/{chunk-4UE33DYW.js → chunk-AG6SI6I2.js} +1 -1
- package/dist/{chunk-CMOMRXTA.js → chunk-F36GGCXC.js} +92 -14
- package/dist/{chunk-UYTSOW2C.js → chunk-J6AEO3GT.js} +1 -1
- package/dist/{chunk-HS5CIDQF.js → chunk-JX6VG5FE.js} +5 -1
- package/dist/chunk-M6MHMCV6.js +336 -0
- package/dist/{chunk-IR3P526R.js → chunk-OJ5AREUC.js} +1 -1
- package/dist/chunk-R3CLO2FB.js +301 -0
- package/dist/chunk-TFZSCXSS.js +569 -0
- package/dist/connect-WRQN37SU.js +68 -0
- package/dist/disconnect-KTII7TXC.js +147 -0
- package/dist/{hook-activity-JHLSVNFG.js → hook-activity-O2NNKTOE.js} +4 -4
- package/dist/{hook-session-start-BIO5QJKT.js → hook-session-start-MT67MMDM.js} +3 -3
- package/dist/index.js +28 -14
- package/dist/{install-2AYAXFYF.js → install-HTDTCELB.js} +1 -1
- package/dist/{login-TLO4ZXLI.js → login-CUQR2MBW.js} +4 -3
- package/dist/{mcp-server-VGCZ6AGJ.js → mcp-server-YDRR6DYJ.js} +12 -12
- package/dist/read-permissions-SPOKGVLO.js +17 -0
- package/dist/runner-WW5OU2YU.js +13 -0
- package/dist/{statusline-YIRFJQWY.js → statusline-SLP4CAFB.js} +3 -3
- package/package.json +1 -1
- package/dist/connect-SFXN3UHJ.js +0 -41
- package/dist/runner-TOCZ4RXC.js +0 -147
|
@@ -0,0 +1,569 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
BoxToolError,
|
|
4
|
+
orderThreads
|
|
5
|
+
} from "./chunk-7PUGLQL2.js";
|
|
6
|
+
import {
|
|
7
|
+
CONFIG_DIR
|
|
8
|
+
} from "./chunk-3DIGCVL4.js";
|
|
9
|
+
|
|
10
|
+
// src/drivers/local.ts
|
|
11
|
+
import { execFile, execFileSync, spawn } from "child_process";
|
|
12
|
+
import { chmodSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "fs";
|
|
13
|
+
import { homedir, hostname } from "os";
|
|
14
|
+
import { basename, dirname, join } from "path";
|
|
15
|
+
import { createInterface } from "readline";
|
|
16
|
+
var CONTINUE_PROMPT = "Continue the work from where you stopped. If it is already done, say so in one sentence.";
|
|
17
|
+
var PR_URL = /https:\/\/github\.com\/[\w.-]+\/[\w.-]+\/pull\/\d+/g;
|
|
18
|
+
var SHARED_BRANCHES = /* @__PURE__ */ new Set(["staging", "main", "master", "HEAD"]);
|
|
19
|
+
var COMMANDS_KEPT = 50;
|
|
20
|
+
var SUMMARY_MAX = 200;
|
|
21
|
+
var HARNESS_NAMES = { "claude-code": "Claude Code", codex: "Codex" };
|
|
22
|
+
var HARNESS_LIMIT = /usage limit|reached your [\w .-]{0,40}limit|hit your [\w .-]{0,40}limit|out of (usage )?credits/i;
|
|
23
|
+
function failedEnd(harness, run2) {
|
|
24
|
+
const said = run2.result?.text?.trim() || run2.lastText;
|
|
25
|
+
const limited = HARNESS_LIMIT.test(run2.result?.text ?? "");
|
|
26
|
+
const words = said?.replace(/\s+/g, " ").trim();
|
|
27
|
+
const error = limited ? { error: "harness_limit" } : {};
|
|
28
|
+
const opening = run2.switched ? `${run2.switched} ` : "";
|
|
29
|
+
if (!words)
|
|
30
|
+
return {
|
|
31
|
+
summary: `${opening}${run2.result ? "The run ended with an error." : `The run exited with ${run2.exit}.`}`,
|
|
32
|
+
...error
|
|
33
|
+
};
|
|
34
|
+
const lead = `${opening}${HARNESS_NAMES[harness] ?? harness} said: "`;
|
|
35
|
+
const room = SUMMARY_MAX - lead.length - 1;
|
|
36
|
+
const quoted = words.length > room ? `${words.slice(0, room - 1)}\u2026` : words;
|
|
37
|
+
return { summary: `${lead}${quoted}"`, ...error };
|
|
38
|
+
}
|
|
39
|
+
function localRunsFilePath() {
|
|
40
|
+
return process.env.CLOOP_LOCAL_RUNS_FILE ?? join(homedir(), CONFIG_DIR, "local-runs.json");
|
|
41
|
+
}
|
|
42
|
+
function pickRepo(repos, repository) {
|
|
43
|
+
if (!repository) {
|
|
44
|
+
if (repos.length === 1 && repos[0]) return repos[0];
|
|
45
|
+
throw new BoxToolError("no_project");
|
|
46
|
+
}
|
|
47
|
+
const want = repository.toLowerCase().split("/").at(-1) ?? "";
|
|
48
|
+
const found = repos.find(
|
|
49
|
+
(r) => r.label.toLowerCase() === want || basename(r.path).toLowerCase() === want
|
|
50
|
+
);
|
|
51
|
+
if (!found) throw new BoxToolError("no_project");
|
|
52
|
+
return found;
|
|
53
|
+
}
|
|
54
|
+
function prUrlIn(text) {
|
|
55
|
+
return text?.match(PR_URL)?.at(-1) ?? null;
|
|
56
|
+
}
|
|
57
|
+
function run(cmd, args, cwd) {
|
|
58
|
+
return new Promise(
|
|
59
|
+
(resolve, reject) => execFile(
|
|
60
|
+
cmd,
|
|
61
|
+
args,
|
|
62
|
+
{ cwd, timeout: 1e4, encoding: "utf-8" },
|
|
63
|
+
(err, out) => err ? reject(err) : resolve(String(out).trim())
|
|
64
|
+
)
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
var gitProbe = {
|
|
68
|
+
head: (repo) => run("git", ["rev-parse", "HEAD"], repo).catch(() => null),
|
|
69
|
+
pushed: (repo) => run("git", ["branch", "-r", "--contains", "HEAD"], repo).then(
|
|
70
|
+
(out) => out.length > 0,
|
|
71
|
+
() => false
|
|
72
|
+
),
|
|
73
|
+
async branchPr(repo) {
|
|
74
|
+
try {
|
|
75
|
+
const branch = await run("git", ["rev-parse", "--abbrev-ref", "HEAD"], repo);
|
|
76
|
+
if (SHARED_BRANCHES.has(branch)) return null;
|
|
77
|
+
return prUrlIn(await run("gh", ["pr", "view", "--json", "url", "-q", ".url"], repo));
|
|
78
|
+
} catch {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
function harnessAlive(pid, r, dialect) {
|
|
84
|
+
try {
|
|
85
|
+
process.kill(pid, 0);
|
|
86
|
+
} catch (err) {
|
|
87
|
+
if (err.code !== "EPERM") return false;
|
|
88
|
+
}
|
|
89
|
+
try {
|
|
90
|
+
const command = execFileSync("ps", ["-ww", "-o", "command=", "-p", String(pid)], {
|
|
91
|
+
encoding: "utf-8",
|
|
92
|
+
timeout: 2e3
|
|
93
|
+
});
|
|
94
|
+
return dialect.owns(r, command);
|
|
95
|
+
} catch {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
var KEEP_MS = 7 * 24 * 36e5;
|
|
100
|
+
var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
101
|
+
var LocalDriver = class {
|
|
102
|
+
constructor(deps) {
|
|
103
|
+
this.deps = deps;
|
|
104
|
+
this.name = deps.dialect.name;
|
|
105
|
+
this.capabilities = deps.dialect.capabilities;
|
|
106
|
+
this.runs = new Map(this.load().map((r) => [r.work_id, r]));
|
|
107
|
+
}
|
|
108
|
+
deps;
|
|
109
|
+
name;
|
|
110
|
+
capabilities;
|
|
111
|
+
runs;
|
|
112
|
+
lives = /* @__PURE__ */ new Map();
|
|
113
|
+
listeners = /* @__PURE__ */ new Map();
|
|
114
|
+
recovering = /* @__PURE__ */ new Set();
|
|
115
|
+
stopping = false;
|
|
116
|
+
get repo() {
|
|
117
|
+
return this.deps.repo ?? gitProbe;
|
|
118
|
+
}
|
|
119
|
+
get file() {
|
|
120
|
+
return this.deps.file ?? localRunsFilePath();
|
|
121
|
+
}
|
|
122
|
+
log(line) {
|
|
123
|
+
this.deps.log?.(line);
|
|
124
|
+
}
|
|
125
|
+
now() {
|
|
126
|
+
return this.deps.now?.() ?? Date.now();
|
|
127
|
+
}
|
|
128
|
+
async status() {
|
|
129
|
+
const harnesses = this.deps.harnesses ? await this.deps.harnesses() : [await this.deps.dialect.probe()].filter((h) => h !== null);
|
|
130
|
+
const all = [...this.runs.values()];
|
|
131
|
+
const unsettled = all.filter((r) => !r.end);
|
|
132
|
+
return {
|
|
133
|
+
box: { label: this.deps.label ?? hostname(), driver: this.name },
|
|
134
|
+
harnesses,
|
|
135
|
+
clients: null,
|
|
136
|
+
projects: this.deps.repos.map((r) => r.label),
|
|
137
|
+
threads: {
|
|
138
|
+
unsettled: unsettled.length,
|
|
139
|
+
running: unsettled.filter((r) => this.lives.has(r.work_id)).length,
|
|
140
|
+
settled: all.length - unsettled.length,
|
|
141
|
+
cloop: unsettled.length
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
async listThreads({
|
|
146
|
+
scope,
|
|
147
|
+
limit
|
|
148
|
+
}) {
|
|
149
|
+
const all = [...this.runs.values()];
|
|
150
|
+
const unsettled = all.filter((r) => !r.end);
|
|
151
|
+
const shown = (scope === "all" ? all : unsettled).map((r) => ({
|
|
152
|
+
id: r.work_id,
|
|
153
|
+
title: r.title,
|
|
154
|
+
status: this.statusOf(r),
|
|
155
|
+
branch: null,
|
|
156
|
+
source: "cloop",
|
|
157
|
+
cloop: true,
|
|
158
|
+
settled: Boolean(r.end),
|
|
159
|
+
updatedAt: r.end?.at ?? r.started_at
|
|
160
|
+
}));
|
|
161
|
+
return {
|
|
162
|
+
scope,
|
|
163
|
+
unsettled: unsettled.length,
|
|
164
|
+
settled: all.length - unsettled.length,
|
|
165
|
+
running: shown.filter((t) => t.status === "running").length,
|
|
166
|
+
threads: orderThreads(shown, limit)
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
async readThread(id) {
|
|
170
|
+
const r = this.record(id);
|
|
171
|
+
return {
|
|
172
|
+
status: this.statusOf(r),
|
|
173
|
+
settled: Boolean(r.end),
|
|
174
|
+
pr_url: r.end?.pr_url ?? null,
|
|
175
|
+
last_assistant_text: r.end?.summary ?? r.last_text ?? null
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
async launch(a) {
|
|
179
|
+
const existing = this.runs.get(a.work_id);
|
|
180
|
+
if (existing)
|
|
181
|
+
return {
|
|
182
|
+
thread_id: a.work_id,
|
|
183
|
+
resumed: true,
|
|
184
|
+
permission: existing.used_permission
|
|
185
|
+
};
|
|
186
|
+
const { dialect } = this.deps;
|
|
187
|
+
if (a.harness && !dialect.aliases.includes(a.harness))
|
|
188
|
+
throw new BoxToolError("harness_unavailable");
|
|
189
|
+
const repo = pickRepo(this.deps.repos, a.repository);
|
|
190
|
+
this.refuseBusy(repo.path);
|
|
191
|
+
const harness = await dialect.probe();
|
|
192
|
+
if (!harness) throw new BoxToolError("harness_unavailable");
|
|
193
|
+
if (!harness.signedIn) throw new BoxToolError("harness_signed_out");
|
|
194
|
+
const head = await this.repo.head(repo.path);
|
|
195
|
+
const concurrent = this.runs.get(a.work_id);
|
|
196
|
+
if (concurrent)
|
|
197
|
+
return { thread_id: a.work_id, resumed: true, permission: concurrent.used_permission };
|
|
198
|
+
this.refuseBusy(repo.path);
|
|
199
|
+
const record = {
|
|
200
|
+
work_id: a.work_id,
|
|
201
|
+
session_id: dialect.name === "claude-code" ? a.work_id : null,
|
|
202
|
+
repo: repo.path,
|
|
203
|
+
title: a.title,
|
|
204
|
+
...a.model ? { model: a.model } : {},
|
|
205
|
+
...a.runtime_mode ? { runtime_mode: a.runtime_mode } : {},
|
|
206
|
+
pid: null,
|
|
207
|
+
started_at: new Date(this.now()).toISOString(),
|
|
208
|
+
head_at_launch: head
|
|
209
|
+
};
|
|
210
|
+
this.runs.set(a.work_id, record);
|
|
211
|
+
this.spawnRun(record, a.prompt, false);
|
|
212
|
+
return { thread_id: a.work_id, resumed: false, permission: record.used_permission };
|
|
213
|
+
}
|
|
214
|
+
async send(a) {
|
|
215
|
+
const r = this.record(a.thread_id);
|
|
216
|
+
const seen = r.commands?.find(([id]) => id === a.command_id);
|
|
217
|
+
if (seen) return { delivered: seen[1] };
|
|
218
|
+
const live = this.lives.get(r.work_id);
|
|
219
|
+
const ask = live?.asks[0];
|
|
220
|
+
if (a.answer && (!ask || ask.request_id !== a.question_id)) {
|
|
221
|
+
this.log(`question_changed work=${r.work_id}`);
|
|
222
|
+
return {
|
|
223
|
+
delivered: "question_changed",
|
|
224
|
+
question: ask?.summary ?? null,
|
|
225
|
+
question_id: ask?.request_id ?? null
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
if (live && ask) {
|
|
229
|
+
if (!a.answer) {
|
|
230
|
+
return {
|
|
231
|
+
delivered: "question_pending",
|
|
232
|
+
question: ask.summary,
|
|
233
|
+
questions: live.asks.length
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
const line2 = this.deps.dialect.answerLine?.(ask, a.text);
|
|
237
|
+
if (!line2) throw new BoxToolError("cannot_steer");
|
|
238
|
+
live.asks = live.asks.slice(1);
|
|
239
|
+
this.write(live, line2);
|
|
240
|
+
this.remember(r, a.command_id, "answer");
|
|
241
|
+
this.log(`answer work=${r.work_id}`);
|
|
242
|
+
this.askNext(r, live);
|
|
243
|
+
return { delivered: "answer", question_id: ask.request_id };
|
|
244
|
+
}
|
|
245
|
+
if (!live) {
|
|
246
|
+
if (r.end) throw new BoxToolError("already_finished");
|
|
247
|
+
this.spawnRun(r, a.text, true);
|
|
248
|
+
this.remember(r, a.command_id, "start");
|
|
249
|
+
return { delivered: "start" };
|
|
250
|
+
}
|
|
251
|
+
if (live.child.stdin?.writableEnded) {
|
|
252
|
+
live.next = live.next ? `${live.next}
|
|
253
|
+
|
|
254
|
+
${a.text}` : a.text;
|
|
255
|
+
this.remember(r, a.command_id, "queue");
|
|
256
|
+
return { delivered: "queue" };
|
|
257
|
+
}
|
|
258
|
+
const line = this.deps.dialect.userLine?.(r, a.text, a.mode);
|
|
259
|
+
if (!line) throw new BoxToolError("cannot_steer");
|
|
260
|
+
live.outstanding++;
|
|
261
|
+
this.write(live, line);
|
|
262
|
+
const delivered = a.mode === "queue" ? "queue" : "steer";
|
|
263
|
+
this.remember(r, a.command_id, delivered);
|
|
264
|
+
this.log(`${delivered} work=${r.work_id}`);
|
|
265
|
+
return { delivered };
|
|
266
|
+
}
|
|
267
|
+
async interrupt(id) {
|
|
268
|
+
const r = this.record(id);
|
|
269
|
+
const live = this.lives.get(r.work_id);
|
|
270
|
+
if (!live) {
|
|
271
|
+
if (r.end) return { ok: true, was_running: false };
|
|
272
|
+
const orphan = r.pid !== null && this.alive(r.pid, r);
|
|
273
|
+
if (orphan && r.pid !== null) {
|
|
274
|
+
try {
|
|
275
|
+
process.kill(r.pid, "SIGINT");
|
|
276
|
+
} catch {
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
await this.end(r, { kind: "failed", summary: "The run was stopped." });
|
|
280
|
+
return { ok: true, was_running: orphan };
|
|
281
|
+
}
|
|
282
|
+
live.interrupted = true;
|
|
283
|
+
live.child.kill("SIGINT");
|
|
284
|
+
const timer = setTimeout(() => {
|
|
285
|
+
if (live.child.exitCode === null && live.child.signalCode === null)
|
|
286
|
+
live.child.kill("SIGTERM");
|
|
287
|
+
}, this.deps.interruptGraceMs ?? 1e4);
|
|
288
|
+
timer.unref();
|
|
289
|
+
this.log(`interrupt work=${r.work_id}`);
|
|
290
|
+
return { ok: true, was_running: true };
|
|
291
|
+
}
|
|
292
|
+
watch(id, onEvent) {
|
|
293
|
+
const set = this.listeners.get(id) ?? /* @__PURE__ */ new Set();
|
|
294
|
+
set.add(onEvent);
|
|
295
|
+
this.listeners.set(id, set);
|
|
296
|
+
const r = this.runs.get(id);
|
|
297
|
+
const unwatch = () => set.delete(onEvent);
|
|
298
|
+
if (!r) {
|
|
299
|
+
onEvent({ kind: "failed", summary: "This run is not on the runner any more." });
|
|
300
|
+
return unwatch;
|
|
301
|
+
}
|
|
302
|
+
if (r.end) {
|
|
303
|
+
const { at: _at, ...end } = r.end;
|
|
304
|
+
onEvent(end);
|
|
305
|
+
return unwatch;
|
|
306
|
+
}
|
|
307
|
+
const live = this.lives.get(id);
|
|
308
|
+
const ask = live?.asks[0];
|
|
309
|
+
if (ask) onEvent({ kind: "question", question_id: ask.request_id, summary: ask.summary });
|
|
310
|
+
if (!live) void this.recover(r);
|
|
311
|
+
return unwatch;
|
|
312
|
+
}
|
|
313
|
+
/** On a daemon stop: end every process, and leave its run open so the next start resumes it. */
|
|
314
|
+
stop() {
|
|
315
|
+
this.stopping = true;
|
|
316
|
+
for (const live of this.lives.values()) live.child.kill("SIGTERM");
|
|
317
|
+
}
|
|
318
|
+
alive(pid, r) {
|
|
319
|
+
return this.deps.alive ? this.deps.alive(pid, r) : harnessAlive(pid, r, this.deps.dialect);
|
|
320
|
+
}
|
|
321
|
+
/** One checkout, one run: two harnesses editing the same files at once would undo each other. */
|
|
322
|
+
refuseBusy(repo) {
|
|
323
|
+
if ([...this.runs.values()].some((r) => r.repo === repo && !r.end))
|
|
324
|
+
throw new BoxToolError("repo_busy");
|
|
325
|
+
}
|
|
326
|
+
/** One question at a time, oldest first, so a yes answers the prompt the owner read. */
|
|
327
|
+
askNext(r, live) {
|
|
328
|
+
const next = live.asks[0];
|
|
329
|
+
if (!next) return;
|
|
330
|
+
this.log(`question work=${r.work_id} tool=${next.tool}`);
|
|
331
|
+
this.emit(r.work_id, { kind: "question", question_id: next.request_id, summary: next.summary });
|
|
332
|
+
}
|
|
333
|
+
/** A run left open by a daemon that died: wait for its old process to go, then resume it once. */
|
|
334
|
+
async recover(r) {
|
|
335
|
+
if (this.recovering.has(r.work_id)) return;
|
|
336
|
+
this.recovering.add(r.work_id);
|
|
337
|
+
try {
|
|
338
|
+
while (r.pid !== null && this.alive(r.pid, r) && !r.end && !this.stopping) {
|
|
339
|
+
await sleep(this.deps.orphanPollMs ?? 2e3);
|
|
340
|
+
}
|
|
341
|
+
if (r.end || this.stopping || this.lives.has(r.work_id)) return;
|
|
342
|
+
this.log(`resume work=${r.work_id} after a restart`);
|
|
343
|
+
this.spawnRun(r, CONTINUE_PROMPT, true);
|
|
344
|
+
} finally {
|
|
345
|
+
this.recovering.delete(r.work_id);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
spawnRun(r, prompt, resume) {
|
|
349
|
+
const { dialect } = this.deps;
|
|
350
|
+
r.used_permission = dialect.permission?.(r);
|
|
351
|
+
const child = spawn(dialect.bin, dialect.args(r, resume), {
|
|
352
|
+
cwd: r.repo,
|
|
353
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
354
|
+
env: process.env
|
|
355
|
+
});
|
|
356
|
+
const live = {
|
|
357
|
+
child,
|
|
358
|
+
outstanding: 1,
|
|
359
|
+
asks: [],
|
|
360
|
+
lastProgressAt: 0,
|
|
361
|
+
interrupted: false,
|
|
362
|
+
stderr: ""
|
|
363
|
+
};
|
|
364
|
+
this.lives.set(r.work_id, live);
|
|
365
|
+
r.pid = child.pid ?? null;
|
|
366
|
+
this.save();
|
|
367
|
+
this.log(`spawn work=${r.work_id} pid=${r.pid ?? "none"} ${resume ? "resume" : "new"}`);
|
|
368
|
+
child.stdin?.on("error", () => {
|
|
369
|
+
});
|
|
370
|
+
child.stdin?.write(dialect.opening(r, prompt));
|
|
371
|
+
if (!dialect.keepsStdin) child.stdin?.end();
|
|
372
|
+
child.stderr?.on("data", (chunk) => {
|
|
373
|
+
live.stderr = (live.stderr + chunk.toString("utf-8")).slice(-2e3);
|
|
374
|
+
});
|
|
375
|
+
const lines = createInterface({ input: child.stdout });
|
|
376
|
+
lines.on("line", (line) => {
|
|
377
|
+
for (const item of dialect.parse(line, r)) this.onItem(r, live, item);
|
|
378
|
+
});
|
|
379
|
+
child.on("error", (err) => {
|
|
380
|
+
this.log(`spawn failed work=${r.work_id}: ${err.message.slice(0, 200)}`);
|
|
381
|
+
});
|
|
382
|
+
child.on("close", (code, signal) => void this.onExit(r, live, code, signal));
|
|
383
|
+
}
|
|
384
|
+
onItem(r, live, item) {
|
|
385
|
+
switch (item.type) {
|
|
386
|
+
case "session":
|
|
387
|
+
if (r.session_id !== item.id) {
|
|
388
|
+
r.session_id = item.id;
|
|
389
|
+
this.save();
|
|
390
|
+
}
|
|
391
|
+
return;
|
|
392
|
+
case "text":
|
|
393
|
+
r.last_text = item.text.slice(0, 300);
|
|
394
|
+
this.progress(r, live, item.text, false);
|
|
395
|
+
return;
|
|
396
|
+
case "tool": {
|
|
397
|
+
const changed = item.name !== live.lastTool;
|
|
398
|
+
live.lastTool = item.name;
|
|
399
|
+
this.progress(
|
|
400
|
+
r,
|
|
401
|
+
live,
|
|
402
|
+
item.summary ?? (item.target ? `${item.name} ${item.target}` : item.name),
|
|
403
|
+
changed
|
|
404
|
+
);
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
case "ask":
|
|
408
|
+
live.asks.push(item);
|
|
409
|
+
if (live.asks.length === 1) this.askNext(r, live);
|
|
410
|
+
return;
|
|
411
|
+
case "cancel": {
|
|
412
|
+
const wasAsked = live.asks[0]?.request_id === item.request_id;
|
|
413
|
+
live.asks = live.asks.filter((x) => x.request_id !== item.request_id);
|
|
414
|
+
if (wasAsked) this.askNext(r, live);
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
case "result":
|
|
418
|
+
live.outstanding--;
|
|
419
|
+
live.result = { ok: item.ok, ...item.text ? { text: item.text } : {} };
|
|
420
|
+
if (item.text) r.last_text = item.text.slice(0, 300);
|
|
421
|
+
if (live.outstanding <= 0 && live.asks.length === 0) live.child.stdin?.end();
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
progress(r, live, line, force) {
|
|
426
|
+
const now = this.now();
|
|
427
|
+
if (!force && now - live.lastProgressAt < (this.deps.progressMs ?? 3e4)) return;
|
|
428
|
+
live.lastProgressAt = now;
|
|
429
|
+
this.emit(r.work_id, {
|
|
430
|
+
kind: "progress",
|
|
431
|
+
summary: line.replace(/\s+/g, " ").trim().slice(0, 200)
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
async onExit(r, live, code, signal) {
|
|
435
|
+
if (this.lives.get(r.work_id) === live) this.lives.delete(r.work_id);
|
|
436
|
+
if (this.stopping || r.end) return;
|
|
437
|
+
const exit = signal ? `signal ${signal}` : `code ${code}`;
|
|
438
|
+
this.log(
|
|
439
|
+
`exit work=${r.work_id} ${exit} result=${live.result ? live.result.ok ? "ok" : "error" : "none"}`
|
|
440
|
+
);
|
|
441
|
+
if (live.interrupted) {
|
|
442
|
+
await this.end(r, { kind: "failed", summary: "The run was stopped." });
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
if (live.next) {
|
|
446
|
+
this.spawnRun(r, live.next, true);
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
if (code === 0 && live.result?.ok) {
|
|
450
|
+
const text = live.result.text?.trim() || r.last_text;
|
|
451
|
+
const { pr, note } = await this.outcome(r, text);
|
|
452
|
+
const summary = [r.switched, note, text].filter(Boolean).join(" ").replace(/\s+/g, " ");
|
|
453
|
+
await this.end(r, {
|
|
454
|
+
kind: "settled",
|
|
455
|
+
...summary ? { summary: summary.slice(0, SUMMARY_MAX) } : {},
|
|
456
|
+
...pr ? { pr_url: pr } : {}
|
|
457
|
+
});
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
if (live.stderr)
|
|
461
|
+
this.log(`stderr work=${r.work_id}: ${live.stderr.trim().split("\n").at(-1)?.slice(0, 200)}`);
|
|
462
|
+
const end = failedEnd(this.name, {
|
|
463
|
+
result: live.result,
|
|
464
|
+
lastText: r.last_text,
|
|
465
|
+
exit,
|
|
466
|
+
...r.switched ? { switched: r.switched } : {}
|
|
467
|
+
});
|
|
468
|
+
if (end.error) this.log(`limit work=${r.work_id} error=${end.error}`);
|
|
469
|
+
if (end.error === "harness_limit" && this.fallBack(r, live.result?.text)) return;
|
|
470
|
+
await this.end(r, { kind: "failed", ...end });
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* Once per run: a run stopped by its model's usage limit resumes its own
|
|
474
|
+
* session on the first fallback model, with the continue prompt, so its
|
|
475
|
+
* steers, answers and edits carry over. It does not when the Task named its
|
|
476
|
+
* model, when the limit is the fallback model's own, or after one switch.
|
|
477
|
+
* `--fallback-model` may already cover this; it is not documented whether a
|
|
478
|
+
* plan's limit counts. The switch opens the end summary, because a progress
|
|
479
|
+
* line is dropped while the socket is down.
|
|
480
|
+
*/
|
|
481
|
+
fallBack(r, said) {
|
|
482
|
+
const model = this.deps.dialect.fallback?.split(",")[0]?.trim();
|
|
483
|
+
if (!model || !r.session_id || r.switched || r.model) return false;
|
|
484
|
+
const named = new RegExp(`\\b${model.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}\\b`, "i");
|
|
485
|
+
if (named.test(said ?? "")) return false;
|
|
486
|
+
const from = /reached your ([\w .-]{1,40}?) limit/i.exec(said ?? "")?.[1] ?? "The default model";
|
|
487
|
+
const to = `${model.charAt(0).toUpperCase()}${model.slice(1)}`;
|
|
488
|
+
r.switched = `${from} was at its usage limit, so this ran on ${to}.`;
|
|
489
|
+
r.model = model;
|
|
490
|
+
this.save();
|
|
491
|
+
this.log(`fallback work=${r.work_id} model=${model}`);
|
|
492
|
+
this.emit(r.work_id, {
|
|
493
|
+
kind: "progress",
|
|
494
|
+
summary: `${from} is at its usage limit, so I switched to ${to}`
|
|
495
|
+
});
|
|
496
|
+
this.spawnRun(r, CONTINUE_PROMPT, true);
|
|
497
|
+
return true;
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* The PR a run produced: one its last text names, else the PR of the
|
|
501
|
+
* branch, but only when the run moved HEAD. A run that left HEAD where it
|
|
502
|
+
* started produced nothing, and the branch's PR belongs to someone else's
|
|
503
|
+
* work. A run that committed without pushing says so.
|
|
504
|
+
*/
|
|
505
|
+
async outcome(r, text) {
|
|
506
|
+
const named = prUrlIn(text);
|
|
507
|
+
if (named) return { pr: named };
|
|
508
|
+
if (!r.head_at_launch) return { pr: null };
|
|
509
|
+
const head = await this.repo.head(r.repo);
|
|
510
|
+
if (!head || head === r.head_at_launch) return { pr: null };
|
|
511
|
+
const pr = await this.repo.branchPr(r.repo);
|
|
512
|
+
if (pr) return { pr };
|
|
513
|
+
return await this.repo.pushed(r.repo) ? { pr: null } : { pr: null, note: `Committed ${head.slice(0, 7)}, not pushed.` };
|
|
514
|
+
}
|
|
515
|
+
async end(r, end) {
|
|
516
|
+
if (r.end) return;
|
|
517
|
+
r.end = { ...end, at: new Date(this.now()).toISOString() };
|
|
518
|
+
r.pid = null;
|
|
519
|
+
this.save();
|
|
520
|
+
this.emit(r.work_id, end);
|
|
521
|
+
}
|
|
522
|
+
emit(id, event) {
|
|
523
|
+
for (const listener of [...this.listeners.get(id) ?? []]) listener(event);
|
|
524
|
+
}
|
|
525
|
+
write(live, line) {
|
|
526
|
+
live.child.stdin?.write(line);
|
|
527
|
+
}
|
|
528
|
+
remember(r, commandId, delivered) {
|
|
529
|
+
r.commands = [
|
|
530
|
+
...r.commands ?? [],
|
|
531
|
+
[commandId, delivered]
|
|
532
|
+
].slice(-COMMANDS_KEPT);
|
|
533
|
+
this.save();
|
|
534
|
+
}
|
|
535
|
+
record(id) {
|
|
536
|
+
const r = this.runs.get(id);
|
|
537
|
+
if (!r) throw new BoxToolError("bad_args");
|
|
538
|
+
return r;
|
|
539
|
+
}
|
|
540
|
+
statusOf(r) {
|
|
541
|
+
if (this.lives.has(r.work_id)) return "running";
|
|
542
|
+
if (!r.end) return "pending";
|
|
543
|
+
return r.end.kind === "settled" ? "completed" : "failed";
|
|
544
|
+
}
|
|
545
|
+
load() {
|
|
546
|
+
try {
|
|
547
|
+
const raw = JSON.parse(readFileSync(this.file, "utf-8"));
|
|
548
|
+
const cutoff = this.now() - KEEP_MS;
|
|
549
|
+
return Array.isArray(raw) ? raw.filter(
|
|
550
|
+
(r) => typeof r?.work_id === "string" && typeof r?.repo === "string" && !(r.end && Date.parse(r.end.at) < cutoff)
|
|
551
|
+
) : [];
|
|
552
|
+
} catch {
|
|
553
|
+
return [];
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
save() {
|
|
557
|
+
mkdirSync(dirname(this.file), { recursive: true, mode: 448 });
|
|
558
|
+
const tmp = `${this.file}.tmp`;
|
|
559
|
+
writeFileSync(tmp, `${JSON.stringify([...this.runs.values()], null, 2)}
|
|
560
|
+
`, { mode: 384 });
|
|
561
|
+
chmodSync(tmp, 384);
|
|
562
|
+
renameSync(tmp, this.file);
|
|
563
|
+
}
|
|
564
|
+
};
|
|
565
|
+
|
|
566
|
+
export {
|
|
567
|
+
localRunsFilePath,
|
|
568
|
+
LocalDriver
|
|
569
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
DEFAULT_T3_URL,
|
|
4
|
+
askOrClosed,
|
|
5
|
+
askRepos,
|
|
6
|
+
awaitDaemon,
|
|
7
|
+
chooseDriver,
|
|
8
|
+
connectBox,
|
|
9
|
+
connectChosen,
|
|
10
|
+
connectCommand,
|
|
11
|
+
detect,
|
|
12
|
+
fallbackDriver,
|
|
13
|
+
fallbackLine,
|
|
14
|
+
findCheckouts,
|
|
15
|
+
harnessesFound,
|
|
16
|
+
installedService,
|
|
17
|
+
isWorkstation,
|
|
18
|
+
mintSession,
|
|
19
|
+
notACheckoutLine,
|
|
20
|
+
parsePicks,
|
|
21
|
+
programFor,
|
|
22
|
+
rerunCommand,
|
|
23
|
+
resolveRepos,
|
|
24
|
+
revokeReplaced,
|
|
25
|
+
runSteps,
|
|
26
|
+
systemdAssignment,
|
|
27
|
+
systemdWord,
|
|
28
|
+
tilde,
|
|
29
|
+
unitEnv,
|
|
30
|
+
unitFor
|
|
31
|
+
} from "./chunk-7PUGLQL2.js";
|
|
32
|
+
import "./chunk-JX6VG5FE.js";
|
|
33
|
+
import "./chunk-R3CLO2FB.js";
|
|
34
|
+
import "./chunk-M6MHMCV6.js";
|
|
35
|
+
import "./chunk-F36GGCXC.js";
|
|
36
|
+
import "./chunk-AG6SI6I2.js";
|
|
37
|
+
import "./chunk-7BREDPJD.js";
|
|
38
|
+
import "./chunk-3DIGCVL4.js";
|
|
39
|
+
export {
|
|
40
|
+
DEFAULT_T3_URL,
|
|
41
|
+
askOrClosed,
|
|
42
|
+
askRepos,
|
|
43
|
+
awaitDaemon,
|
|
44
|
+
chooseDriver,
|
|
45
|
+
connectBox,
|
|
46
|
+
connectChosen,
|
|
47
|
+
connectCommand,
|
|
48
|
+
detect,
|
|
49
|
+
fallbackDriver,
|
|
50
|
+
fallbackLine,
|
|
51
|
+
findCheckouts,
|
|
52
|
+
harnessesFound,
|
|
53
|
+
installedService,
|
|
54
|
+
isWorkstation,
|
|
55
|
+
mintSession,
|
|
56
|
+
notACheckoutLine,
|
|
57
|
+
parsePicks,
|
|
58
|
+
programFor,
|
|
59
|
+
rerunCommand,
|
|
60
|
+
resolveRepos,
|
|
61
|
+
revokeReplaced,
|
|
62
|
+
runSteps,
|
|
63
|
+
systemdAssignment,
|
|
64
|
+
systemdWord,
|
|
65
|
+
tilde,
|
|
66
|
+
unitEnv,
|
|
67
|
+
unitFor
|
|
68
|
+
};
|