@cloop/runner 0.2.10 → 0.3.1
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/README.md +4 -2
- package/dist/{box-P6LMNVYG.js → box-2SVXFDHE.js} +16 -15
- package/dist/{chunk-UL2WHOQJ.js → chunk-4H4SIIUH.js} +1 -1
- package/dist/{chunk-J6AEO3GT.js → chunk-5FZAHWPX.js} +1 -1
- package/dist/{chunk-NX2LAKNU.js → chunk-5LDFFQ4O.js} +712 -45
- package/dist/{chunk-HI3HDN35.js → chunk-6LC5XH3S.js} +3 -5
- package/dist/{chunk-C6EODIE5.js → chunk-DH6IJQG6.js} +8 -10
- package/dist/{chunk-AEWIRLRS.js → chunk-E5CKL2Q3.js} +1 -1
- package/dist/{chunk-3DIGCVL4.js → chunk-IKCGVBHA.js} +11 -0
- package/dist/{chunk-PRJBCKRP.js → chunk-N24VQSNB.js} +3 -3
- package/dist/{chunk-VNX4VJAM.js → chunk-NALGGZM6.js} +3 -3
- package/dist/{chunk-JX6VG5FE.js → chunk-NUU4GXGV.js} +1 -1
- package/dist/{chunk-AG6SI6I2.js → chunk-NYGELUTR.js} +1 -1
- package/dist/{chunk-MWWA2VH7.js → chunk-RPWSWVKD.js} +5 -2
- package/dist/{chunk-EN7WRLSI.js → chunk-TJXXWEJW.js} +192 -40
- package/dist/{connect-GURZFURZ.js → connect-NAHKIUBI.js} +9 -9
- package/dist/disconnect-KTTVOO6R.js +28 -0
- package/dist/{hook-activity-LBYV4WJW.js → hook-activity-I7TV7YG7.js} +4 -4
- package/dist/{hook-session-start-YSEV5DCB.js → hook-session-start-MFX6HR3W.js} +3 -3
- package/dist/index.js +17 -17
- package/dist/{install-HTDTCELB.js → install-EMEZBBK7.js} +1 -1
- package/dist/{login-DXPJ3ZOA.js → login-37QE2AGW.js} +4 -4
- package/dist/{mcp-server-4UMYMJ5P.js → mcp-server-FLCNIW4C.js} +6 -6
- package/dist/runner-EHVMTKAA.js +13 -0
- package/dist/{runs-HRDQ7YCY.js → runs-IDFJ3Z2K.js} +3 -4
- package/dist/{statusline-AV2F2IM4.js → statusline-HGIHZJY2.js} +3 -3
- package/dist/{takeover-PDYTQY5D.js → takeover-YUBFKEKJ.js} +7 -8
- package/dist/{use-52LTRRXJ.js → use-OLC3RTIL.js} +14 -10
- package/package.json +1 -1
- package/dist/chunk-DHUZXAK2.js +0 -640
- package/dist/disconnect-HGQW7TVG.js +0 -159
- package/dist/runner-SPOKUMLM.js +0 -13
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
} from "./chunk-VK6GV757.js";
|
|
5
5
|
import {
|
|
6
6
|
CONFIG_DIR
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-IKCGVBHA.js";
|
|
8
8
|
|
|
9
9
|
// src/run-result.ts
|
|
10
10
|
var RESULT_INSTRUCTION = [
|
|
@@ -12,7 +12,8 @@ var RESULT_INSTRUCTION = [
|
|
|
12
12
|
"```cloop-result",
|
|
13
13
|
'{"summary": "One or two sentences on what you did.", "outputs": [{"kind": "pr", "title": "fix: the thing", "url": "https://github.com/org/repo/pull/1"}], "checks": [{"name": "Tests", "outcome": "passed"}]}',
|
|
14
14
|
"```",
|
|
15
|
-
"An output kind is one of: pr or link (title, url), draft (title, body, optional to), doc (title, optional url or body), file (title, path). A check outcome is passed, failed or skipped."
|
|
15
|
+
"An output kind is one of: pr or link (title, url), draft (title, body, optional to), doc (title, optional url or body), file (title, path). A check outcome is passed, failed or skipped.",
|
|
16
|
+
"List as outputs only what this run made. A PR or page you only read, checked or cited goes in the summary, never in outputs."
|
|
16
17
|
].join("\n");
|
|
17
18
|
var text = (v, max) => typeof v === "string" && v.length <= max;
|
|
18
19
|
var optionalText = (v, max) => v === void 0 || text(v, max);
|
|
@@ -24,8 +25,13 @@ var https = (v) => {
|
|
|
24
25
|
}
|
|
25
26
|
};
|
|
26
27
|
var PR_URL = /https:\/\/github\.com\/[\w.-]+\/[\w.-]+\/pull\/\d+/g;
|
|
28
|
+
var prNumber = (url) => Number(url.slice(url.lastIndexOf("/") + 1));
|
|
27
29
|
function prUrlIn(text2) {
|
|
28
|
-
|
|
30
|
+
const urls = text2?.match(PR_URL) ?? [];
|
|
31
|
+
return urls.reduce(
|
|
32
|
+
(newest, url) => newest && prNumber(newest) >= prNumber(url) ? newest : url,
|
|
33
|
+
null
|
|
34
|
+
);
|
|
29
35
|
}
|
|
30
36
|
function resultPrUrl(finalMessage) {
|
|
31
37
|
const result = parseRunResult(finalMessage);
|
|
@@ -114,8 +120,8 @@ function readRunResult(message) {
|
|
|
114
120
|
function parseRunResult(message) {
|
|
115
121
|
return readRunResult(message).result;
|
|
116
122
|
}
|
|
117
|
-
function droppedLine(dropped,
|
|
118
|
-
return dropped.outputs || dropped.checks ? `result_items_dropped run=${
|
|
123
|
+
function droppedLine(dropped, run2) {
|
|
124
|
+
return dropped.outputs || dropped.checks ? `result_items_dropped run=${run2} outputs=${dropped.outputs} checks=${dropped.checks}` : null;
|
|
119
125
|
}
|
|
120
126
|
|
|
121
127
|
// src/drivers/driver.ts
|
|
@@ -143,8 +149,9 @@ function isProgressSentence(value) {
|
|
|
143
149
|
return typeof value === "string" && /\S[ \t]+\S/.test(value.trim().split(/\r?\n/)[0] ?? "");
|
|
144
150
|
}
|
|
145
151
|
var PROGRESS_MS = 3e4;
|
|
152
|
+
var RUN_TEXT_MAX = 4e3;
|
|
146
153
|
function nextProgress(clock, text2, now, ms = PROGRESS_MS, force = false) {
|
|
147
|
-
const summary = (parseRunResult(text2)?.summary ?? text2.split("```cloop-result")[0] ?? "").replace(/\s+/g, " ").trim().slice(0,
|
|
154
|
+
const summary = (parseRunResult(text2)?.summary ?? text2.split("```cloop-result")[0] ?? "").replace(/\s+/g, " ").trim().slice(0, RUN_TEXT_MAX);
|
|
148
155
|
if (!isProgressSentence(summary)) return null;
|
|
149
156
|
if (!force && now - clock.lastProgressAt < ms) return null;
|
|
150
157
|
clock.lastProgressAt = now;
|
|
@@ -221,7 +228,7 @@ function redactSecrets(text2) {
|
|
|
221
228
|
for (const [pattern, replacement] of SECRETS) out = out.replace(pattern, replacement);
|
|
222
229
|
return out.replace(
|
|
223
230
|
OPAQUE,
|
|
224
|
-
(
|
|
231
|
+
(run2) => /^[0-9a-f-]+$/i.test(run2) || !/\d/.test(run2) || !/[A-Za-z]/.test(run2) ? run2 : "[redacted]"
|
|
225
232
|
);
|
|
226
233
|
}
|
|
227
234
|
function logText(text2) {
|
|
@@ -440,36 +447,36 @@ function latestRun(runs) {
|
|
|
440
447
|
);
|
|
441
448
|
}
|
|
442
449
|
function isTerminal(runs) {
|
|
443
|
-
const
|
|
444
|
-
return Boolean(
|
|
450
|
+
const run2 = latestRun(runs);
|
|
451
|
+
return Boolean(run2 && TERMINAL_RUN.has(run2.status));
|
|
445
452
|
}
|
|
446
453
|
function activeRun(runs) {
|
|
447
|
-
const
|
|
448
|
-
return
|
|
454
|
+
const run2 = latestRun(runs);
|
|
455
|
+
return run2 && !TERMINAL_RUN.has(run2.status) ? run2 : null;
|
|
449
456
|
}
|
|
450
457
|
function steerableRun(p) {
|
|
451
|
-
const
|
|
452
|
-
if (!
|
|
453
|
-
const attempts = new Set((p.attempts ?? []).filter((a) => a.runId ===
|
|
458
|
+
const run2 = activeRun(p.runs ?? []);
|
|
459
|
+
if (!run2) return null;
|
|
460
|
+
const attempts = new Set((p.attempts ?? []).filter((a) => a.runId === run2.id).map((a) => a.id));
|
|
454
461
|
const turning = (p.providerTurns ?? []).some(
|
|
455
462
|
(t) => t.status === "running" && t.runAttemptId !== null && attempts.has(t.runAttemptId)
|
|
456
463
|
);
|
|
457
|
-
return turning ?
|
|
464
|
+
return turning ? run2 : null;
|
|
458
465
|
}
|
|
459
466
|
var STEER_WAIT_MS = 8e3;
|
|
460
467
|
var STEER_POLL_MS = 500;
|
|
461
468
|
function outcomeOf(p) {
|
|
462
|
-
const
|
|
463
|
-
const status =
|
|
469
|
+
const run2 = latestRun(p.runs ?? []);
|
|
470
|
+
const status = run2?.status ?? "pending";
|
|
464
471
|
const settled = TERMINAL_RUN.has(status) || Boolean(p.thread.settledAt);
|
|
465
472
|
const text2 = (p.messages ?? []).filter((m) => m.role === "assistant" && m.text).at(-1)?.text?.trim();
|
|
466
473
|
const { result, dropped } = readRunResult(text2);
|
|
467
474
|
return {
|
|
468
475
|
status,
|
|
469
476
|
settled,
|
|
470
|
-
kind: settled ? status === "completed" || !
|
|
477
|
+
kind: settled ? status === "completed" || !run2 ? "settled" : "failed" : null,
|
|
471
478
|
pr_url: resultPrUrl(text2),
|
|
472
|
-
last_assistant_text: text2 ? text2.slice(0,
|
|
479
|
+
last_assistant_text: text2 ? text2.slice(0, RUN_TEXT_MAX) : null,
|
|
473
480
|
...result ? { result } : {},
|
|
474
481
|
...dropped.outputs || dropped.checks ? { dropped } : {}
|
|
475
482
|
};
|
|
@@ -767,8 +774,8 @@ ${RESULT_INSTRUCTION}`, attachments: [] }
|
|
|
767
774
|
* was on its way: T3 refuses that interrupt, and the owner's ask is met.
|
|
768
775
|
*/
|
|
769
776
|
async interrupt(threadId, commandId) {
|
|
770
|
-
const
|
|
771
|
-
if (!
|
|
777
|
+
const run2 = activeRun((await this.readProjection(threadId)).runs ?? []);
|
|
778
|
+
if (!run2) {
|
|
772
779
|
const end = this.held.get(threadId);
|
|
773
780
|
end?.();
|
|
774
781
|
return { ok: true, was_running: Boolean(end) };
|
|
@@ -778,7 +785,7 @@ ${RESULT_INSTRUCTION}`, attachments: [] }
|
|
|
778
785
|
type: "run.interrupt",
|
|
779
786
|
commandId,
|
|
780
787
|
threadId,
|
|
781
|
-
runId:
|
|
788
|
+
runId: run2.id,
|
|
782
789
|
reason: "Stopped from Cloop"
|
|
783
790
|
});
|
|
784
791
|
} catch (err) {
|
|
@@ -882,8 +889,8 @@ ${RESULT_INSTRUCTION}`, attachments: [] }
|
|
|
882
889
|
const speak = (runs, messages) => {
|
|
883
890
|
const latest = [...messages.values()].filter((m) => m.role === "assistant" && m.text?.trim()).at(-1);
|
|
884
891
|
if (!latest?.text || latest.streaming || latest.text === spoken) return;
|
|
885
|
-
const
|
|
886
|
-
if (
|
|
892
|
+
const run2 = latest.runId ? runs.get(latest.runId) : void 0;
|
|
893
|
+
if (run2 && TERMINAL_RUN.has(run2.status)) return;
|
|
887
894
|
spoken = latest.text;
|
|
888
895
|
const now = this.deps.now?.() ?? Date.now();
|
|
889
896
|
const summary = nextProgress(clock, latest.text, now, this.deps.progressMs);
|
|
@@ -1128,13 +1135,680 @@ ${RESULT_INSTRUCTION}`, attachments: [] }
|
|
|
1128
1135
|
}
|
|
1129
1136
|
};
|
|
1130
1137
|
|
|
1131
|
-
// src/
|
|
1138
|
+
// src/drivers/local.ts
|
|
1139
|
+
import { execFile as execFile2, execFileSync as execFileSync3, spawn } from "child_process";
|
|
1132
1140
|
import { chmodSync as chmodSync2, mkdirSync as mkdirSync2, readFileSync, renameSync as renameSync2, writeFileSync } from "fs";
|
|
1133
|
-
import { homedir } from "os";
|
|
1134
|
-
import { dirname as dirname2, join as join3 } from "path";
|
|
1141
|
+
import { homedir, hostname } from "os";
|
|
1142
|
+
import { basename, dirname as dirname2, join as join3 } from "path";
|
|
1143
|
+
import { createInterface } from "readline";
|
|
1144
|
+
|
|
1145
|
+
// src/drivers/harness-check.ts
|
|
1146
|
+
var HARNESS_STALE_MS = 5 * 6e4;
|
|
1147
|
+
var FIRST_CHECK_WAIT_MS = 3e3;
|
|
1148
|
+
var HarnessCheck = class {
|
|
1149
|
+
constructor(probe, opts) {
|
|
1150
|
+
this.probe = probe;
|
|
1151
|
+
this.opts = opts;
|
|
1152
|
+
}
|
|
1153
|
+
probe;
|
|
1154
|
+
opts;
|
|
1155
|
+
last = null;
|
|
1156
|
+
running = null;
|
|
1157
|
+
/** Starts a probe unless one is running. It never rejects. */
|
|
1158
|
+
refresh() {
|
|
1159
|
+
this.running ??= this.probe().then(
|
|
1160
|
+
(harnesses) => {
|
|
1161
|
+
this.last = { harnesses, at: this.opts.now() };
|
|
1162
|
+
},
|
|
1163
|
+
(err) => this.opts.log(`harness probe failed: ${err?.message}`)
|
|
1164
|
+
).finally(() => {
|
|
1165
|
+
this.running = null;
|
|
1166
|
+
});
|
|
1167
|
+
return this.running;
|
|
1168
|
+
}
|
|
1169
|
+
async read() {
|
|
1170
|
+
if (!this.last) {
|
|
1171
|
+
let timer;
|
|
1172
|
+
await Promise.race([
|
|
1173
|
+
this.refresh(),
|
|
1174
|
+
new Promise((resolve) => {
|
|
1175
|
+
timer = setTimeout(resolve, FIRST_CHECK_WAIT_MS);
|
|
1176
|
+
})
|
|
1177
|
+
]);
|
|
1178
|
+
clearTimeout(timer);
|
|
1179
|
+
} else if (this.opts.now() - this.last.at > HARNESS_STALE_MS) {
|
|
1180
|
+
void this.refresh();
|
|
1181
|
+
}
|
|
1182
|
+
const last = this.last;
|
|
1183
|
+
return last ? { harnesses: last.harnesses, checkedAt: new Date(last.at).toISOString(), probing: false } : { harnesses: [], checkedAt: null, probing: true };
|
|
1184
|
+
}
|
|
1185
|
+
};
|
|
1186
|
+
|
|
1187
|
+
// src/drivers/local.ts
|
|
1188
|
+
var STOPPED = { kind: "failed", summary: STOPPED_SUMMARY };
|
|
1189
|
+
function takeoverSummary(machine) {
|
|
1190
|
+
return `You took this over on ${machine}.`;
|
|
1191
|
+
}
|
|
1192
|
+
var CONTINUE_PROMPT = "Continue the work from where you stopped. If it is already done, say so in one sentence.";
|
|
1193
|
+
var SHARED_BRANCHES = /* @__PURE__ */ new Set(["staging", "main", "master", "HEAD"]);
|
|
1194
|
+
var COMMANDS_KEPT = 50;
|
|
1195
|
+
var SUMMARY_MAX2 = 200;
|
|
1196
|
+
var HARNESS_NAMES = { "claude-code": "Claude Code", codex: "Codex" };
|
|
1197
|
+
var HARNESS_LIMIT = /usage limit|reached your [\w .-]{0,40}limit|hit your [\w .-]{0,40}limit|out of (usage )?credits/i;
|
|
1198
|
+
function failedEnd(harness, run2) {
|
|
1199
|
+
const said = run2.result?.text?.trim() || run2.lastText;
|
|
1200
|
+
const limited = HARNESS_LIMIT.test(run2.result?.text ?? "");
|
|
1201
|
+
const words = said?.replace(/\s+/g, " ").trim();
|
|
1202
|
+
const error = limited ? { error: "harness_limit" } : {};
|
|
1203
|
+
const opening = run2.switched ? `${run2.switched} ` : "";
|
|
1204
|
+
if (!words)
|
|
1205
|
+
return {
|
|
1206
|
+
summary: `${opening}${run2.result ? "The run ended with an error." : `The run exited with ${run2.exit}.`}`,
|
|
1207
|
+
...error
|
|
1208
|
+
};
|
|
1209
|
+
const lead = `${opening}${HARNESS_NAMES[harness] ?? harness} said: "`;
|
|
1210
|
+
const room = SUMMARY_MAX2 - lead.length - 1;
|
|
1211
|
+
const quoted = words.length > room ? `${words.slice(0, room - 1)}\u2026` : words;
|
|
1212
|
+
return { summary: `${lead}${quoted}"`, ...error };
|
|
1213
|
+
}
|
|
1214
|
+
function localRunsFilePath() {
|
|
1215
|
+
return process.env.CLOOP_LOCAL_RUNS_FILE ?? join3(homedir(), CONFIG_DIR, "local-runs.json");
|
|
1216
|
+
}
|
|
1217
|
+
function pickRepo(repos, repository) {
|
|
1218
|
+
if (!repository) {
|
|
1219
|
+
if (repos.length === 1 && repos[0]) return repos[0];
|
|
1220
|
+
throw new BoxToolError("no_project");
|
|
1221
|
+
}
|
|
1222
|
+
const want = repository.toLowerCase().split("/").at(-1) ?? "";
|
|
1223
|
+
const found = repos.find(
|
|
1224
|
+
(r) => r.label.toLowerCase() === want || basename(r.path).toLowerCase() === want
|
|
1225
|
+
);
|
|
1226
|
+
if (!found) throw new BoxToolError("no_project");
|
|
1227
|
+
return found;
|
|
1228
|
+
}
|
|
1229
|
+
function run(cmd, args, cwd) {
|
|
1230
|
+
return new Promise(
|
|
1231
|
+
(resolve, reject) => execFile2(
|
|
1232
|
+
cmd,
|
|
1233
|
+
args,
|
|
1234
|
+
{ cwd, timeout: 1e4, encoding: "utf-8" },
|
|
1235
|
+
(err, out) => err ? reject(err) : resolve(String(out).trim())
|
|
1236
|
+
)
|
|
1237
|
+
);
|
|
1238
|
+
}
|
|
1239
|
+
var gitProbe = {
|
|
1240
|
+
head: (repo) => run("git", ["rev-parse", "HEAD"], repo).catch(() => null),
|
|
1241
|
+
pushed: (repo) => run("git", ["branch", "-r", "--contains", "HEAD"], repo).then(
|
|
1242
|
+
(out) => out.length > 0,
|
|
1243
|
+
() => false
|
|
1244
|
+
),
|
|
1245
|
+
async branchPr(repo) {
|
|
1246
|
+
try {
|
|
1247
|
+
const branch = await run("git", ["rev-parse", "--abbrev-ref", "HEAD"], repo);
|
|
1248
|
+
if (SHARED_BRANCHES.has(branch)) return null;
|
|
1249
|
+
return prUrlIn(await run("gh", ["pr", "view", "--json", "url", "-q", ".url"], repo));
|
|
1250
|
+
} catch {
|
|
1251
|
+
return null;
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
};
|
|
1255
|
+
function harnessAlive(pid, r, dialect) {
|
|
1256
|
+
try {
|
|
1257
|
+
process.kill(pid, 0);
|
|
1258
|
+
} catch (err) {
|
|
1259
|
+
if (err.code !== "EPERM") return false;
|
|
1260
|
+
}
|
|
1261
|
+
try {
|
|
1262
|
+
const command = execFileSync3("ps", ["-ww", "-o", "command=", "-p", String(pid)], {
|
|
1263
|
+
encoding: "utf-8",
|
|
1264
|
+
timeout: 2e3
|
|
1265
|
+
});
|
|
1266
|
+
return dialect.owns(r, command);
|
|
1267
|
+
} catch {
|
|
1268
|
+
return false;
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
var KEEP_MS = 7 * 24 * 36e5;
|
|
1272
|
+
var sleep2 = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
1273
|
+
var LocalDriver = class {
|
|
1274
|
+
constructor(deps) {
|
|
1275
|
+
this.deps = deps;
|
|
1276
|
+
this.name = deps.dialect.name;
|
|
1277
|
+
this.harnessCheck = new HarnessCheck(
|
|
1278
|
+
deps.harnesses ?? (async () => [await deps.dialect.probe()].filter((h) => h !== null)),
|
|
1279
|
+
{ now: () => this.now(), log: (line) => this.log(line) }
|
|
1280
|
+
);
|
|
1281
|
+
this.capabilities = deps.dialect.capabilities;
|
|
1282
|
+
this.runs = new Map(this.load().map((r) => [r.work_id, r]));
|
|
1283
|
+
this.logDir = deps.logDir ?? runLogDir(this.file);
|
|
1284
|
+
try {
|
|
1285
|
+
const removed = pruneRunLogs(this.logDir, this.now());
|
|
1286
|
+
if (removed) this.log(`run logs removed=${removed} older than 14 days`);
|
|
1287
|
+
} catch (err) {
|
|
1288
|
+
this.log(`run log prune failed: ${err.code ?? "unknown"}`);
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1291
|
+
deps;
|
|
1292
|
+
name;
|
|
1293
|
+
capabilities;
|
|
1294
|
+
runs;
|
|
1295
|
+
lives = /* @__PURE__ */ new Map();
|
|
1296
|
+
listeners = /* @__PURE__ */ new Map();
|
|
1297
|
+
recovering = /* @__PURE__ */ new Set();
|
|
1298
|
+
stopping = false;
|
|
1299
|
+
logDir;
|
|
1300
|
+
logFailed = false;
|
|
1301
|
+
harnessCheck;
|
|
1302
|
+
get repo() {
|
|
1303
|
+
return this.deps.repo ?? gitProbe;
|
|
1304
|
+
}
|
|
1305
|
+
get file() {
|
|
1306
|
+
return this.deps.file ?? localRunsFilePath();
|
|
1307
|
+
}
|
|
1308
|
+
log(line) {
|
|
1309
|
+
this.deps.log?.(line);
|
|
1310
|
+
}
|
|
1311
|
+
/** One line in the run's own log, for `cloop watch`. A full disk must not stop the run. */
|
|
1312
|
+
note(r, text2) {
|
|
1313
|
+
try {
|
|
1314
|
+
appendRunLog(this.logDir, r.work_id, text2, { now: this.now() });
|
|
1315
|
+
} catch (err) {
|
|
1316
|
+
if (this.logFailed) return;
|
|
1317
|
+
this.logFailed = true;
|
|
1318
|
+
this.log(`run log write failed: ${err.code ?? "unknown"}`);
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
now() {
|
|
1322
|
+
return this.deps.now?.() ?? Date.now();
|
|
1323
|
+
}
|
|
1324
|
+
/** Probes the harnesses now, so the first status after the daemon starts answers at once. */
|
|
1325
|
+
warmHarnesses() {
|
|
1326
|
+
void this.harnessCheck.refresh();
|
|
1327
|
+
}
|
|
1328
|
+
async status() {
|
|
1329
|
+
const { harnesses, checkedAt, probing } = await this.harnessCheck.read();
|
|
1330
|
+
const all = [...this.runs.values()];
|
|
1331
|
+
const unsettled = all.filter((r) => !r.end);
|
|
1332
|
+
return {
|
|
1333
|
+
box: { label: this.deps.label ?? hostname(), driver: this.name },
|
|
1334
|
+
harnesses,
|
|
1335
|
+
harnesses_checked_at: checkedAt,
|
|
1336
|
+
...probing ? { probing: true } : {},
|
|
1337
|
+
clients: null,
|
|
1338
|
+
projects: this.deps.repos.map((r) => r.label),
|
|
1339
|
+
threads: {
|
|
1340
|
+
unsettled: unsettled.length,
|
|
1341
|
+
running: unsettled.filter((r) => this.lives.has(r.work_id)).length,
|
|
1342
|
+
settled: all.length - unsettled.length,
|
|
1343
|
+
cloop: unsettled.length
|
|
1344
|
+
}
|
|
1345
|
+
};
|
|
1346
|
+
}
|
|
1347
|
+
async listThreads({
|
|
1348
|
+
scope,
|
|
1349
|
+
limit
|
|
1350
|
+
}) {
|
|
1351
|
+
const all = [...this.runs.values()];
|
|
1352
|
+
const unsettled = all.filter((r) => !r.end);
|
|
1353
|
+
const shown = (scope === "all" ? all : unsettled).map((r) => ({
|
|
1354
|
+
id: r.work_id,
|
|
1355
|
+
title: r.title,
|
|
1356
|
+
status: this.statusOf(r),
|
|
1357
|
+
branch: null,
|
|
1358
|
+
source: "cloop",
|
|
1359
|
+
cloop: true,
|
|
1360
|
+
settled: Boolean(r.end),
|
|
1361
|
+
updatedAt: r.end?.at ?? r.started_at
|
|
1362
|
+
}));
|
|
1363
|
+
return {
|
|
1364
|
+
scope,
|
|
1365
|
+
unsettled: unsettled.length,
|
|
1366
|
+
settled: all.length - unsettled.length,
|
|
1367
|
+
running: shown.filter((t) => t.status === "running").length,
|
|
1368
|
+
threads: orderThreads(shown, limit)
|
|
1369
|
+
};
|
|
1370
|
+
}
|
|
1371
|
+
async readThread(id) {
|
|
1372
|
+
const r = this.record(id);
|
|
1373
|
+
return {
|
|
1374
|
+
status: this.statusOf(r),
|
|
1375
|
+
settled: Boolean(r.end),
|
|
1376
|
+
pr_url: r.end?.pr_url ?? null,
|
|
1377
|
+
last_assistant_text: r.end?.summary ?? r.last_text ?? null,
|
|
1378
|
+
resume: this.deps.dialect.name === "claude-code" && r.session_id ? { cwd: r.repo, session_id: r.session_id } : null
|
|
1379
|
+
};
|
|
1380
|
+
}
|
|
1381
|
+
async launch(a) {
|
|
1382
|
+
const existing = this.runs.get(a.work_id);
|
|
1383
|
+
if (existing)
|
|
1384
|
+
return {
|
|
1385
|
+
thread_id: a.work_id,
|
|
1386
|
+
resumed: true,
|
|
1387
|
+
permission: existing.used_permission,
|
|
1388
|
+
takeover: true
|
|
1389
|
+
};
|
|
1390
|
+
const { dialect } = this.deps;
|
|
1391
|
+
if (a.harness && !dialect.aliases.includes(a.harness))
|
|
1392
|
+
throw new BoxToolError("harness_unavailable");
|
|
1393
|
+
const repo = pickRepo(this.deps.repos, a.repository);
|
|
1394
|
+
this.refuseBusy(repo.path);
|
|
1395
|
+
const harness = await dialect.probe();
|
|
1396
|
+
if (!harness) throw new BoxToolError("harness_unavailable");
|
|
1397
|
+
if (!harness.signedIn) throw new BoxToolError("harness_signed_out");
|
|
1398
|
+
const head = await this.repo.head(repo.path);
|
|
1399
|
+
const concurrent = this.runs.get(a.work_id);
|
|
1400
|
+
if (concurrent)
|
|
1401
|
+
return {
|
|
1402
|
+
thread_id: a.work_id,
|
|
1403
|
+
resumed: true,
|
|
1404
|
+
permission: concurrent.used_permission,
|
|
1405
|
+
takeover: true
|
|
1406
|
+
};
|
|
1407
|
+
this.refuseBusy(repo.path);
|
|
1408
|
+
const record = {
|
|
1409
|
+
work_id: a.work_id,
|
|
1410
|
+
session_id: dialect.name === "claude-code" ? a.work_id : null,
|
|
1411
|
+
repo: repo.path,
|
|
1412
|
+
title: a.title,
|
|
1413
|
+
...a.model ? { model: a.model } : {},
|
|
1414
|
+
...a.runtime_mode ? { runtime_mode: a.runtime_mode } : {},
|
|
1415
|
+
pid: null,
|
|
1416
|
+
started_at: new Date(this.now()).toISOString(),
|
|
1417
|
+
head_at_launch: head
|
|
1418
|
+
};
|
|
1419
|
+
this.runs.set(a.work_id, record);
|
|
1420
|
+
this.spawnRun(record, a.prompt, false);
|
|
1421
|
+
return {
|
|
1422
|
+
thread_id: a.work_id,
|
|
1423
|
+
resumed: false,
|
|
1424
|
+
permission: record.used_permission,
|
|
1425
|
+
takeover: true
|
|
1426
|
+
};
|
|
1427
|
+
}
|
|
1428
|
+
async send(a) {
|
|
1429
|
+
const r = this.record(a.thread_id);
|
|
1430
|
+
const seen = r.commands?.find(([id]) => id === a.command_id);
|
|
1431
|
+
if (seen) return { delivered: seen[1] };
|
|
1432
|
+
const live = this.lives.get(r.work_id);
|
|
1433
|
+
const ask = live?.asks[0];
|
|
1434
|
+
if (a.answer && (!ask || ask.request_id !== a.question_id)) {
|
|
1435
|
+
this.log(`question_changed work=${r.work_id}`);
|
|
1436
|
+
return {
|
|
1437
|
+
delivered: "question_changed",
|
|
1438
|
+
question: ask?.summary ?? null,
|
|
1439
|
+
question_id: ask?.request_id ?? null
|
|
1440
|
+
};
|
|
1441
|
+
}
|
|
1442
|
+
if (live && ask) {
|
|
1443
|
+
if (!a.answer) {
|
|
1444
|
+
return {
|
|
1445
|
+
delivered: "question_pending",
|
|
1446
|
+
question: ask.summary,
|
|
1447
|
+
questions: live.asks.length
|
|
1448
|
+
};
|
|
1449
|
+
}
|
|
1450
|
+
const line2 = this.deps.dialect.answerLine?.(ask, a.text);
|
|
1451
|
+
if (!line2) throw new BoxToolError("cannot_steer");
|
|
1452
|
+
live.asks = live.asks.slice(1);
|
|
1453
|
+
this.write(live, line2);
|
|
1454
|
+
this.note(r, `< You answered: ${a.text}`);
|
|
1455
|
+
this.remember(r, a.command_id, "answer");
|
|
1456
|
+
this.log(`answer work=${r.work_id}`);
|
|
1457
|
+
this.askNext(r, live);
|
|
1458
|
+
return { delivered: "answer", question_id: ask.request_id };
|
|
1459
|
+
}
|
|
1460
|
+
if (!live) {
|
|
1461
|
+
if (r.end && !a.resume) throw new BoxToolError("already_finished");
|
|
1462
|
+
if (r.end) {
|
|
1463
|
+
this.refuseBusy(r.repo);
|
|
1464
|
+
r.end = void 0;
|
|
1465
|
+
r.last_text = void 0;
|
|
1466
|
+
}
|
|
1467
|
+
this.spawnRun(r, a.text, true);
|
|
1468
|
+
this.remember(r, a.command_id, "start");
|
|
1469
|
+
return { delivered: "start" };
|
|
1470
|
+
}
|
|
1471
|
+
if (live.child.stdin?.writableEnded) {
|
|
1472
|
+
live.next = live.next ? `${live.next}
|
|
1473
|
+
|
|
1474
|
+
${a.text}` : a.text;
|
|
1475
|
+
this.remember(r, a.command_id, "queue");
|
|
1476
|
+
return { delivered: "queue" };
|
|
1477
|
+
}
|
|
1478
|
+
const line = this.deps.dialect.userLine?.(r, a.text, a.mode);
|
|
1479
|
+
if (!line) throw new BoxToolError("cannot_steer");
|
|
1480
|
+
live.outstanding++;
|
|
1481
|
+
this.write(live, line);
|
|
1482
|
+
this.note(r, `< You: ${a.text}`);
|
|
1483
|
+
const delivered = a.mode === "queue" ? "queue" : "steer";
|
|
1484
|
+
this.remember(r, a.command_id, delivered);
|
|
1485
|
+
this.log(`${delivered} work=${r.work_id}`);
|
|
1486
|
+
return { delivered };
|
|
1487
|
+
}
|
|
1488
|
+
async interrupt(id, _commandId, stop = STOPPED) {
|
|
1489
|
+
const r = this.record(id);
|
|
1490
|
+
const live = this.lives.get(r.work_id);
|
|
1491
|
+
if (!live) {
|
|
1492
|
+
if (r.end) return { ok: true, was_running: false };
|
|
1493
|
+
const orphan = r.pid !== null && this.alive(r.pid, r);
|
|
1494
|
+
if (orphan && r.pid !== null) {
|
|
1495
|
+
try {
|
|
1496
|
+
process.kill(r.pid, "SIGINT");
|
|
1497
|
+
} catch {
|
|
1498
|
+
}
|
|
1499
|
+
}
|
|
1500
|
+
await this.end(r, stop);
|
|
1501
|
+
return { ok: true, was_running: orphan };
|
|
1502
|
+
}
|
|
1503
|
+
live.interrupted = stop;
|
|
1504
|
+
live.child.kill("SIGINT");
|
|
1505
|
+
this.note(r, stop === STOPPED ? "= Stop requested." : `= ${stop.summary}`);
|
|
1506
|
+
const timer = setTimeout(() => {
|
|
1507
|
+
if (live.child.exitCode === null && live.child.signalCode === null)
|
|
1508
|
+
live.child.kill("SIGTERM");
|
|
1509
|
+
}, this.deps.interruptGraceMs ?? 1e4);
|
|
1510
|
+
timer.unref();
|
|
1511
|
+
this.log(`interrupt work=${r.work_id}${stop.error ? ` reason=${stop.error}` : ""}`);
|
|
1512
|
+
return { ok: true, was_running: true };
|
|
1513
|
+
}
|
|
1514
|
+
/**
|
|
1515
|
+
* `cloop takeover`: stop the run so the owner can open its session in
|
|
1516
|
+
* their own terminal. It ends as a stop, with the machine in its words, and
|
|
1517
|
+
* never resumes by itself: two processes on one session would fight.
|
|
1518
|
+
*/
|
|
1519
|
+
takeOver(id, machine) {
|
|
1520
|
+
return this.interrupt(id, void 0, {
|
|
1521
|
+
kind: "failed",
|
|
1522
|
+
summary: takeoverSummary(machine),
|
|
1523
|
+
error: "taken_over"
|
|
1524
|
+
});
|
|
1525
|
+
}
|
|
1526
|
+
watch(id, onEvent) {
|
|
1527
|
+
const set = this.listeners.get(id) ?? /* @__PURE__ */ new Set();
|
|
1528
|
+
set.add(onEvent);
|
|
1529
|
+
this.listeners.set(id, set);
|
|
1530
|
+
const r = this.runs.get(id);
|
|
1531
|
+
const unwatch = () => set.delete(onEvent);
|
|
1532
|
+
if (!r) {
|
|
1533
|
+
onEvent({ kind: "failed", summary: "This run is not on the runner any more." });
|
|
1534
|
+
return unwatch;
|
|
1535
|
+
}
|
|
1536
|
+
if (r.end) {
|
|
1537
|
+
const { at: _at, ...end } = r.end;
|
|
1538
|
+
onEvent(end);
|
|
1539
|
+
return unwatch;
|
|
1540
|
+
}
|
|
1541
|
+
const live = this.lives.get(id);
|
|
1542
|
+
const ask = live?.asks[0];
|
|
1543
|
+
if (ask) onEvent({ kind: "question", question_id: ask.request_id, summary: ask.summary });
|
|
1544
|
+
if (!live) void this.recover(r);
|
|
1545
|
+
return unwatch;
|
|
1546
|
+
}
|
|
1547
|
+
/** On a daemon stop: end every process, and leave its run open so the next start resumes it. */
|
|
1548
|
+
stop() {
|
|
1549
|
+
this.stopping = true;
|
|
1550
|
+
for (const live of this.lives.values()) live.child.kill("SIGTERM");
|
|
1551
|
+
}
|
|
1552
|
+
alive(pid, r) {
|
|
1553
|
+
return this.deps.alive ? this.deps.alive(pid, r) : harnessAlive(pid, r, this.deps.dialect);
|
|
1554
|
+
}
|
|
1555
|
+
/** One checkout, one run: two harnesses editing the same files at once would undo each other. */
|
|
1556
|
+
refuseBusy(repo) {
|
|
1557
|
+
if ([...this.runs.values()].some((r) => r.repo === repo && !r.end))
|
|
1558
|
+
throw new BoxToolError("repo_busy");
|
|
1559
|
+
}
|
|
1560
|
+
/** One question at a time, oldest first, so a yes answers the prompt the owner read. */
|
|
1561
|
+
askNext(r, live) {
|
|
1562
|
+
const next = live.asks[0];
|
|
1563
|
+
if (!next) return;
|
|
1564
|
+
this.log(`question work=${r.work_id} tool=${next.tool}`);
|
|
1565
|
+
this.emit(r.work_id, { kind: "question", question_id: next.request_id, summary: next.summary });
|
|
1566
|
+
}
|
|
1567
|
+
/** A run left open by a daemon that died: wait for its old process to go, then resume it once. */
|
|
1568
|
+
async recover(r) {
|
|
1569
|
+
if (this.recovering.has(r.work_id)) return;
|
|
1570
|
+
this.recovering.add(r.work_id);
|
|
1571
|
+
try {
|
|
1572
|
+
while (r.pid !== null && this.alive(r.pid, r) && !r.end && !this.stopping) {
|
|
1573
|
+
await sleep2(this.deps.orphanPollMs ?? 2e3);
|
|
1574
|
+
}
|
|
1575
|
+
if (r.end || this.stopping || this.lives.has(r.work_id)) return;
|
|
1576
|
+
this.log(`resume work=${r.work_id} after a restart`);
|
|
1577
|
+
this.spawnRun(r, CONTINUE_PROMPT, true);
|
|
1578
|
+
} finally {
|
|
1579
|
+
this.recovering.delete(r.work_id);
|
|
1580
|
+
}
|
|
1581
|
+
}
|
|
1582
|
+
spawnRun(r, prompt, resume) {
|
|
1583
|
+
const { dialect } = this.deps;
|
|
1584
|
+
r.used_permission = dialect.permission?.(r);
|
|
1585
|
+
const child = spawn(dialect.bin, dialect.args(r, resume), {
|
|
1586
|
+
cwd: r.repo,
|
|
1587
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
1588
|
+
env: process.env
|
|
1589
|
+
});
|
|
1590
|
+
const live = {
|
|
1591
|
+
child,
|
|
1592
|
+
outstanding: 1,
|
|
1593
|
+
asks: [],
|
|
1594
|
+
lastProgressAt: 0,
|
|
1595
|
+
interrupted: null,
|
|
1596
|
+
stderr: ""
|
|
1597
|
+
};
|
|
1598
|
+
this.lives.set(r.work_id, live);
|
|
1599
|
+
r.pid = child.pid ?? null;
|
|
1600
|
+
this.save();
|
|
1601
|
+
this.log(`spawn work=${r.work_id} pid=${r.pid ?? "none"} ${resume ? "resume" : "new"}`);
|
|
1602
|
+
this.note(r, resume ? "= Resumed." : `= Started "${r.title}" in ${r.repo}`);
|
|
1603
|
+
child.stdin?.on("error", () => {
|
|
1604
|
+
});
|
|
1605
|
+
child.stdin?.write(dialect.opening(r, `${prompt}
|
|
1606
|
+
|
|
1607
|
+
${RESULT_INSTRUCTION}`));
|
|
1608
|
+
if (!dialect.keepsStdin) child.stdin?.end();
|
|
1609
|
+
child.stderr?.on("data", (chunk) => {
|
|
1610
|
+
live.stderr = (live.stderr + chunk.toString("utf-8")).slice(-2e3);
|
|
1611
|
+
});
|
|
1612
|
+
const lines = createInterface({ input: child.stdout });
|
|
1613
|
+
lines.on("line", (line) => {
|
|
1614
|
+
for (const item of dialect.parse(line, r)) this.onItem(r, live, item);
|
|
1615
|
+
});
|
|
1616
|
+
child.on("error", (err) => {
|
|
1617
|
+
this.log(`spawn failed work=${r.work_id}: ${err.message.slice(0, 200)}`);
|
|
1618
|
+
});
|
|
1619
|
+
child.on("close", (code, signal) => void this.onExit(r, live, code, signal));
|
|
1620
|
+
}
|
|
1621
|
+
onItem(r, live, item) {
|
|
1622
|
+
const logged = logLineFor(item);
|
|
1623
|
+
if (logged) this.note(r, logged);
|
|
1624
|
+
switch (item.type) {
|
|
1625
|
+
case "session":
|
|
1626
|
+
if (r.session_id !== item.id) {
|
|
1627
|
+
r.session_id = item.id;
|
|
1628
|
+
this.save();
|
|
1629
|
+
}
|
|
1630
|
+
return;
|
|
1631
|
+
case "text":
|
|
1632
|
+
r.last_text = item.text.slice(0, 1e5);
|
|
1633
|
+
this.progress(r, live, item.text, false);
|
|
1634
|
+
return;
|
|
1635
|
+
case "tool": {
|
|
1636
|
+
const line = item.summary ?? (item.target ? `${item.name} ${item.target}` : item.name);
|
|
1637
|
+
if (!isProgressSentence(line)) return;
|
|
1638
|
+
const changed = item.name !== live.lastTool;
|
|
1639
|
+
live.lastTool = item.name;
|
|
1640
|
+
this.progress(r, live, line, changed);
|
|
1641
|
+
return;
|
|
1642
|
+
}
|
|
1643
|
+
case "ask":
|
|
1644
|
+
live.asks.push(item);
|
|
1645
|
+
if (live.asks.length === 1) this.askNext(r, live);
|
|
1646
|
+
return;
|
|
1647
|
+
case "cancel": {
|
|
1648
|
+
const wasAsked = live.asks[0]?.request_id === item.request_id;
|
|
1649
|
+
live.asks = live.asks.filter((x) => x.request_id !== item.request_id);
|
|
1650
|
+
if (wasAsked) this.askNext(r, live);
|
|
1651
|
+
return;
|
|
1652
|
+
}
|
|
1653
|
+
case "result":
|
|
1654
|
+
live.outstanding--;
|
|
1655
|
+
live.result = { ok: item.ok, ...item.text ? { text: item.text } : {} };
|
|
1656
|
+
if (item.text) r.last_text = item.text.slice(0, 1e5);
|
|
1657
|
+
if (live.outstanding <= 0 && live.asks.length === 0) live.child.stdin?.end();
|
|
1658
|
+
return;
|
|
1659
|
+
}
|
|
1660
|
+
}
|
|
1661
|
+
progress(r, live, line, force) {
|
|
1662
|
+
const summary = nextProgress(live, line, this.now(), this.deps.progressMs, force);
|
|
1663
|
+
if (summary) this.emit(r.work_id, { kind: "progress", summary });
|
|
1664
|
+
}
|
|
1665
|
+
async onExit(r, live, code, signal) {
|
|
1666
|
+
if (this.lives.get(r.work_id) === live) this.lives.delete(r.work_id);
|
|
1667
|
+
if (this.stopping || r.end) return;
|
|
1668
|
+
const exit = signal ? `signal ${signal}` : `code ${code}`;
|
|
1669
|
+
this.log(
|
|
1670
|
+
`exit work=${r.work_id} ${exit} result=${live.result ? live.result.ok ? "ok" : "error" : "none"}`
|
|
1671
|
+
);
|
|
1672
|
+
if (live.interrupted) {
|
|
1673
|
+
await this.end(r, live.interrupted);
|
|
1674
|
+
return;
|
|
1675
|
+
}
|
|
1676
|
+
if (live.next) {
|
|
1677
|
+
this.spawnRun(r, live.next, true);
|
|
1678
|
+
return;
|
|
1679
|
+
}
|
|
1680
|
+
if (code === 0 && live.result?.ok) {
|
|
1681
|
+
const text2 = live.result.text?.trim() || r.last_text;
|
|
1682
|
+
const { result, dropped } = readRunResult(text2);
|
|
1683
|
+
const lost = droppedLine(dropped, r.work_id);
|
|
1684
|
+
if (lost) this.log(lost);
|
|
1685
|
+
const { pr, note } = await this.outcome(r, text2);
|
|
1686
|
+
const summary = [r.switched, note, text2].filter(Boolean).join(" ").replace(/\s+/g, " ");
|
|
1687
|
+
await this.end(r, {
|
|
1688
|
+
kind: "settled",
|
|
1689
|
+
...result ? { result } : {},
|
|
1690
|
+
...summary ? { summary: summary.slice(0, RUN_TEXT_MAX) } : {},
|
|
1691
|
+
...pr ? { pr_url: pr } : {}
|
|
1692
|
+
});
|
|
1693
|
+
return;
|
|
1694
|
+
}
|
|
1695
|
+
if (live.stderr)
|
|
1696
|
+
this.log(`stderr work=${r.work_id}: ${live.stderr.trim().split("\n").at(-1)?.slice(0, 200)}`);
|
|
1697
|
+
const end = failedEnd(this.name, {
|
|
1698
|
+
result: live.result,
|
|
1699
|
+
lastText: r.last_text,
|
|
1700
|
+
exit,
|
|
1701
|
+
...r.switched ? { switched: r.switched } : {}
|
|
1702
|
+
});
|
|
1703
|
+
if (end.error) this.log(`limit work=${r.work_id} error=${end.error}`);
|
|
1704
|
+
if (end.error === "harness_limit" && this.fallBack(r, live.result?.text)) return;
|
|
1705
|
+
await this.end(r, { kind: "failed", ...end });
|
|
1706
|
+
}
|
|
1707
|
+
/**
|
|
1708
|
+
* Once per run: a run stopped by its model's usage limit resumes its own
|
|
1709
|
+
* session on the first fallback model, with the continue prompt, so its
|
|
1710
|
+
* steers, answers and edits carry over. It does not when the Task named its
|
|
1711
|
+
* model, when the limit is the fallback model's own, or after one switch.
|
|
1712
|
+
* `--fallback-model` may already cover this; it is not documented whether a
|
|
1713
|
+
* plan's limit counts. The switch opens the end summary, because a progress
|
|
1714
|
+
* line is dropped while the socket is down.
|
|
1715
|
+
*/
|
|
1716
|
+
fallBack(r, said) {
|
|
1717
|
+
const model = this.deps.dialect.fallback?.split(",")[0]?.trim();
|
|
1718
|
+
if (!model || !r.session_id || r.switched || r.model) return false;
|
|
1719
|
+
const named = new RegExp(`\\b${model.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}\\b`, "i");
|
|
1720
|
+
if (named.test(said ?? "")) return false;
|
|
1721
|
+
const from = /reached your ([\w .-]{1,40}?) limit/i.exec(said ?? "")?.[1] ?? "The default model";
|
|
1722
|
+
const to = `${model.charAt(0).toUpperCase()}${model.slice(1)}`;
|
|
1723
|
+
r.switched = `${from} was at its usage limit, so this ran on ${to}.`;
|
|
1724
|
+
r.model = model;
|
|
1725
|
+
this.save();
|
|
1726
|
+
this.log(`fallback work=${r.work_id} model=${model}`);
|
|
1727
|
+
this.emit(r.work_id, {
|
|
1728
|
+
kind: "progress",
|
|
1729
|
+
summary: `${from} is at its usage limit, so I switched to ${to}`
|
|
1730
|
+
});
|
|
1731
|
+
this.spawnRun(r, CONTINUE_PROMPT, true);
|
|
1732
|
+
return true;
|
|
1733
|
+
}
|
|
1734
|
+
/**
|
|
1735
|
+
* The PR a run produced: one its last text names, else the PR of the
|
|
1736
|
+
* branch, but only when the run moved HEAD. A run that left HEAD where it
|
|
1737
|
+
* started produced nothing, and the branch's PR belongs to someone else's
|
|
1738
|
+
* work. A run that committed without pushing says so.
|
|
1739
|
+
*/
|
|
1740
|
+
async outcome(r, text2) {
|
|
1741
|
+
const named = resultPrUrl(text2);
|
|
1742
|
+
if (named) return { pr: named };
|
|
1743
|
+
if (!r.head_at_launch) return { pr: null };
|
|
1744
|
+
const head = await this.repo.head(r.repo);
|
|
1745
|
+
if (!head || head === r.head_at_launch) return { pr: null };
|
|
1746
|
+
const pr = await this.repo.branchPr(r.repo);
|
|
1747
|
+
if (pr) return { pr };
|
|
1748
|
+
return await this.repo.pushed(r.repo) ? { pr: null } : { pr: null, note: `Committed ${head.slice(0, 7)}, not pushed.` };
|
|
1749
|
+
}
|
|
1750
|
+
async end(r, end) {
|
|
1751
|
+
if (r.end) return;
|
|
1752
|
+
r.end = { ...end, at: new Date(this.now()).toISOString() };
|
|
1753
|
+
r.pid = null;
|
|
1754
|
+
this.save();
|
|
1755
|
+
this.note(r, endNote(end));
|
|
1756
|
+
this.emit(r.work_id, end);
|
|
1757
|
+
}
|
|
1758
|
+
emit(id, event) {
|
|
1759
|
+
for (const listener of [...this.listeners.get(id) ?? []]) listener(event);
|
|
1760
|
+
}
|
|
1761
|
+
write(live, line) {
|
|
1762
|
+
live.child.stdin?.write(line);
|
|
1763
|
+
}
|
|
1764
|
+
remember(r, commandId, delivered) {
|
|
1765
|
+
r.commands = [
|
|
1766
|
+
...r.commands ?? [],
|
|
1767
|
+
[commandId, delivered]
|
|
1768
|
+
].slice(-COMMANDS_KEPT);
|
|
1769
|
+
this.save();
|
|
1770
|
+
}
|
|
1771
|
+
record(id) {
|
|
1772
|
+
const r = this.runs.get(id);
|
|
1773
|
+
if (!r) throw new BoxToolError("bad_args");
|
|
1774
|
+
return r;
|
|
1775
|
+
}
|
|
1776
|
+
statusOf(r) {
|
|
1777
|
+
if (this.lives.has(r.work_id)) return "running";
|
|
1778
|
+
if (!r.end) return "pending";
|
|
1779
|
+
return r.end.kind === "settled" ? "completed" : "failed";
|
|
1780
|
+
}
|
|
1781
|
+
load() {
|
|
1782
|
+
const cutoff = this.now() - KEEP_MS;
|
|
1783
|
+
return readRunRecords(this.file).filter((r) => !(r.end && Date.parse(r.end.at) < cutoff));
|
|
1784
|
+
}
|
|
1785
|
+
save() {
|
|
1786
|
+
mkdirSync2(dirname2(this.file), { recursive: true, mode: 448 });
|
|
1787
|
+
const tmp = `${this.file}.tmp`;
|
|
1788
|
+
writeFileSync(tmp, `${JSON.stringify([...this.runs.values()], null, 2)}
|
|
1789
|
+
`, { mode: 384 });
|
|
1790
|
+
chmodSync2(tmp, 384);
|
|
1791
|
+
renameSync2(tmp, this.file);
|
|
1792
|
+
}
|
|
1793
|
+
};
|
|
1794
|
+
function readRunRecords(file = localRunsFilePath()) {
|
|
1795
|
+
try {
|
|
1796
|
+
const raw = JSON.parse(readFileSync(file, "utf-8"));
|
|
1797
|
+
return Array.isArray(raw) ? raw.filter(
|
|
1798
|
+
(r) => typeof r?.work_id === "string" && typeof r?.repo === "string"
|
|
1799
|
+
) : [];
|
|
1800
|
+
} catch {
|
|
1801
|
+
return [];
|
|
1802
|
+
}
|
|
1803
|
+
}
|
|
1804
|
+
|
|
1805
|
+
// src/runner-config.ts
|
|
1806
|
+
import { chmodSync as chmodSync3, mkdirSync as mkdirSync3, readFileSync as readFileSync2, renameSync as renameSync3, writeFileSync as writeFileSync2 } from "fs";
|
|
1807
|
+
import { homedir as homedir2 } from "os";
|
|
1808
|
+
import { dirname as dirname3, join as join4 } from "path";
|
|
1135
1809
|
var BOX_FILE = "box.json";
|
|
1136
1810
|
function boxPath() {
|
|
1137
|
-
return process.env.CLOOP_BOX_FILE ??
|
|
1811
|
+
return process.env.CLOOP_BOX_FILE ?? join4(homedir2(), CONFIG_DIR, BOX_FILE);
|
|
1138
1812
|
}
|
|
1139
1813
|
function isLocal(cfg) {
|
|
1140
1814
|
return cfg.driver === "claude-code" || cfg.driver === "codex";
|
|
@@ -1150,7 +1824,7 @@ function otherThreadsChoice(argv, previous) {
|
|
|
1150
1824
|
function readRunnerConfig() {
|
|
1151
1825
|
let parsed;
|
|
1152
1826
|
try {
|
|
1153
|
-
parsed = JSON.parse(
|
|
1827
|
+
parsed = JSON.parse(readFileSync2(boxPath(), "utf-8"));
|
|
1154
1828
|
} catch {
|
|
1155
1829
|
return null;
|
|
1156
1830
|
}
|
|
@@ -1163,42 +1837,35 @@ function readRunnerConfig() {
|
|
|
1163
1837
|
}
|
|
1164
1838
|
function writeBoxConfig(cfg) {
|
|
1165
1839
|
const path = boxPath();
|
|
1166
|
-
|
|
1840
|
+
mkdirSync3(dirname3(path), { recursive: true, mode: 448 });
|
|
1167
1841
|
const temp = `${path}.${process.pid}.tmp`;
|
|
1168
|
-
|
|
1842
|
+
writeFileSync2(temp, `${JSON.stringify(cfg, null, 2)}
|
|
1169
1843
|
`, { mode: 384 });
|
|
1170
|
-
|
|
1171
|
-
|
|
1844
|
+
chmodSync3(temp, 384);
|
|
1845
|
+
renameSync3(temp, path);
|
|
1172
1846
|
}
|
|
1173
1847
|
|
|
1174
1848
|
export {
|
|
1175
|
-
RESULT_INSTRUCTION,
|
|
1176
|
-
prUrlIn,
|
|
1177
|
-
resultPrUrl,
|
|
1178
|
-
readRunResult,
|
|
1179
|
-
droppedLine,
|
|
1180
1849
|
RUNTIME_MODES,
|
|
1181
1850
|
BoxToolError,
|
|
1182
1851
|
CLOOP_THREAD_PREFIX,
|
|
1183
1852
|
questionLine,
|
|
1184
|
-
|
|
1185
|
-
nextProgress,
|
|
1853
|
+
RUN_TEXT_MAX,
|
|
1186
1854
|
isYes,
|
|
1187
1855
|
runLogDir,
|
|
1188
1856
|
runLogPath,
|
|
1189
|
-
logLineFor,
|
|
1190
|
-
appendRunLog,
|
|
1191
|
-
pruneRunLogs,
|
|
1192
1857
|
STOPPED_SUMMARY,
|
|
1193
|
-
endNote,
|
|
1194
1858
|
isEndLogLine,
|
|
1195
1859
|
defaultT3BinSources,
|
|
1196
1860
|
t3Candidates,
|
|
1197
1861
|
pickT3Bin,
|
|
1198
1862
|
t3BaseDirArgs,
|
|
1199
1863
|
runT3,
|
|
1200
|
-
orderThreads,
|
|
1201
1864
|
T3Driver,
|
|
1865
|
+
takeoverSummary,
|
|
1866
|
+
localRunsFilePath,
|
|
1867
|
+
LocalDriver,
|
|
1868
|
+
readRunRecords,
|
|
1202
1869
|
boxPath,
|
|
1203
1870
|
isLocal,
|
|
1204
1871
|
showsOtherThreads,
|