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