@cloop/runner 0.2.2 → 0.2.4
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-BS6BSQUO.js → box-ZUDKJY2F.js} +103 -15
- package/dist/{chunk-TFZSCXSS.js → chunk-5J4I7DAE.js} +202 -35
- package/dist/{chunk-OJ5AREUC.js → chunk-AEWIRLRS.js} +1 -1
- package/dist/{chunk-M6MHMCV6.js → chunk-KHYA7GEH.js} +1 -1
- package/dist/chunk-M6U2YVEB.js +782 -0
- package/dist/{chunk-7BREDPJD.js → chunk-PBOQMFNN.js} +1 -1
- package/dist/{chunk-7PUGLQL2.js → chunk-VEVDJ3H3.js} +116 -716
- package/dist/{chunk-4LFQXBWP.js → chunk-VK6GV757.js} +1 -0
- package/dist/{chunk-F36GGCXC.js → chunk-ZDUKQWKA.js} +1 -1
- package/dist/{connect-WRQN37SU.js → connect-WXL6JWJN.js} +5 -4
- package/dist/{disconnect-KTII7TXC.js → disconnect-ZAVMFITZ.js} +23 -14
- package/dist/{hook-activity-O2NNKTOE.js → hook-activity-R7OPEYBX.js} +2 -2
- package/dist/{hook-session-start-MT67MMDM.js → hook-session-start-GEBGA63R.js} +1 -1
- package/dist/index.js +30 -11
- package/dist/{login-CUQR2MBW.js → login-MXIM6VMA.js} +2 -2
- package/dist/{mcp-server-YDRR6DYJ.js → mcp-server-ADCSP4SY.js} +3 -3
- package/dist/{runner-WW5OU2YU.js → runner-OVRZFJIY.js} +3 -3
- package/dist/runs-ZTLNHRRJ.js +265 -0
- package/dist/{statusline-SLP4CAFB.js → statusline-ESZZVOBJ.js} +2 -2
- package/package.json +1 -1
|
@@ -1,45 +1,111 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import {
|
|
3
3
|
LocalDriver
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-5J4I7DAE.js";
|
|
5
5
|
import {
|
|
6
6
|
MeshSocket,
|
|
7
7
|
defaultSocketFactory
|
|
8
8
|
} from "./chunk-ZILLDYL4.js";
|
|
9
|
+
import "./chunk-VK6GV757.js";
|
|
9
10
|
import {
|
|
10
11
|
BoxWatch,
|
|
11
12
|
DEFAULT_FALLBACK_MODEL,
|
|
12
13
|
T3Client,
|
|
13
|
-
T3Driver,
|
|
14
14
|
claudeDialect,
|
|
15
15
|
codexDialect,
|
|
16
16
|
connectCommand,
|
|
17
|
-
isLocal,
|
|
18
17
|
lockThreadsFile,
|
|
19
18
|
mintSession,
|
|
20
19
|
probeClaude,
|
|
21
20
|
probeClaudePermissions,
|
|
22
21
|
probeCodex,
|
|
23
|
-
readRunnerConfig,
|
|
24
22
|
runBoxTool,
|
|
25
23
|
threadsFilePath,
|
|
26
|
-
writeBoxConfig,
|
|
27
24
|
writeRunnerState
|
|
28
|
-
} from "./chunk-
|
|
25
|
+
} from "./chunk-VEVDJ3H3.js";
|
|
26
|
+
import {
|
|
27
|
+
T3Driver,
|
|
28
|
+
isLocal,
|
|
29
|
+
readRunnerConfig,
|
|
30
|
+
writeBoxConfig
|
|
31
|
+
} from "./chunk-M6U2YVEB.js";
|
|
29
32
|
import {
|
|
30
33
|
getMachine
|
|
31
34
|
} from "./chunk-JX6VG5FE.js";
|
|
32
35
|
import "./chunk-R3CLO2FB.js";
|
|
33
|
-
import "./chunk-
|
|
34
|
-
import "./chunk-
|
|
36
|
+
import "./chunk-KHYA7GEH.js";
|
|
37
|
+
import "./chunk-ZDUKQWKA.js";
|
|
35
38
|
import "./chunk-AG6SI6I2.js";
|
|
36
|
-
import "./chunk-
|
|
39
|
+
import "./chunk-PBOQMFNN.js";
|
|
37
40
|
import {
|
|
38
41
|
getMeshUrl,
|
|
39
42
|
getToken,
|
|
40
43
|
readCredentials
|
|
41
44
|
} from "./chunk-3DIGCVL4.js";
|
|
42
45
|
|
|
46
|
+
// src/outside-sessions.ts
|
|
47
|
+
import { createReadStream } from "fs";
|
|
48
|
+
import { readdir, stat } from "fs/promises";
|
|
49
|
+
import { homedir } from "os";
|
|
50
|
+
import { join } from "path";
|
|
51
|
+
import { createInterface } from "readline";
|
|
52
|
+
var UUID = /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i;
|
|
53
|
+
async function listOutsideSessions(root = join(homedir(), ".claude", "projects"), own = /* @__PURE__ */ new Set(), now = Date.now()) {
|
|
54
|
+
const files = [];
|
|
55
|
+
for (const project of await readdir(root, { withFileTypes: true }).catch(() => [])) {
|
|
56
|
+
if (!project.isDirectory()) continue;
|
|
57
|
+
for (const entry of await readdir(join(root, project.name), { withFileTypes: true }).catch(
|
|
58
|
+
() => []
|
|
59
|
+
)) {
|
|
60
|
+
if (!entry.isFile() || !entry.name.endsWith(".jsonl")) continue;
|
|
61
|
+
const id = entry.name.slice(0, -6);
|
|
62
|
+
if (!UUID.test(id) || own.has(id)) continue;
|
|
63
|
+
const path = join(root, project.name, entry.name);
|
|
64
|
+
const info = await stat(path).catch(() => null);
|
|
65
|
+
if (info && info.mtimeMs >= now - 7 * 864e5)
|
|
66
|
+
files.push({ id, project: project.name.slice(0, 200), file: path, mtime: info.mtimeMs });
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const results = [];
|
|
70
|
+
for (const file of files.sort((a, b) => b.mtime - a.mtime).slice(0, 20)) {
|
|
71
|
+
let title;
|
|
72
|
+
let cwd;
|
|
73
|
+
const stream = createReadStream(file.file, { encoding: "utf8" });
|
|
74
|
+
const lines = createInterface({ input: stream, crlfDelay: Number.POSITIVE_INFINITY });
|
|
75
|
+
try {
|
|
76
|
+
for await (const line of lines) {
|
|
77
|
+
if (!cwd && line.includes('"cwd"')) {
|
|
78
|
+
try {
|
|
79
|
+
const record = JSON.parse(line);
|
|
80
|
+
if (typeof record.cwd === "string" && record.cwd.startsWith("/") && record.cwd.length <= 500)
|
|
81
|
+
cwd = record.cwd;
|
|
82
|
+
} catch {
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (!/^\s*\{\s*"type"\s*:\s*"(?:summary|custom-title|title)"/.test(line)) continue;
|
|
86
|
+
try {
|
|
87
|
+
const record = JSON.parse(line);
|
|
88
|
+
const value = record.type === "summary" ? record.summary : record.type === "custom-title" ? record.customTitle : record.title;
|
|
89
|
+
if (typeof value === "string" && value.trim()) title = value.trim().slice(0, 120);
|
|
90
|
+
} catch {
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
} finally {
|
|
94
|
+
lines.close();
|
|
95
|
+
stream.destroy();
|
|
96
|
+
}
|
|
97
|
+
results.push({
|
|
98
|
+
id: file.id,
|
|
99
|
+
project: file.project,
|
|
100
|
+
updatedAt: new Date(file.mtime).toISOString(),
|
|
101
|
+
status: "idle",
|
|
102
|
+
...cwd ? { cwd } : {},
|
|
103
|
+
...title ? { title } : {}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
return results;
|
|
107
|
+
}
|
|
108
|
+
|
|
43
109
|
// src/commands/box.ts
|
|
44
110
|
var BEAT_MS = 6e4;
|
|
45
111
|
var RESEND_MS = 5 * 6e4;
|
|
@@ -68,9 +134,8 @@ async function localDriver(cfg, name) {
|
|
|
68
134
|
const bin = cfg.driver === name ? cfg.bin : void 0;
|
|
69
135
|
return new LocalDriver({
|
|
70
136
|
dialect: name === "claude-code" ? claudeDialect(bin, cfg.fallbackModels ?? DEFAULT_FALLBACK_MODEL, cfg.askEverything, {
|
|
71
|
-
...await probeClaudePermissions(bin)
|
|
72
|
-
|
|
73
|
-
}) : codexDialect(bin, cfg.fullAccess),
|
|
137
|
+
...await probeClaudePermissions(bin)
|
|
138
|
+
}) : codexDialect(bin),
|
|
74
139
|
repos: cfg.repos,
|
|
75
140
|
label: cfg.label,
|
|
76
141
|
harnesses: () => machineHarnesses(cfg),
|
|
@@ -130,7 +195,11 @@ async function run() {
|
|
|
130
195
|
const onCall = async (frame) => {
|
|
131
196
|
const started = Date.now();
|
|
132
197
|
const result = await runBoxTool(
|
|
133
|
-
{
|
|
198
|
+
{
|
|
199
|
+
driver,
|
|
200
|
+
onLaunch: (workId, threadId) => watch.add(workId, threadId),
|
|
201
|
+
onResume: (threadId) => watch.resume(threadId)
|
|
202
|
+
},
|
|
134
203
|
frame.tool,
|
|
135
204
|
frame.args
|
|
136
205
|
);
|
|
@@ -186,6 +255,7 @@ async function run() {
|
|
|
186
255
|
...await mintSession(cfg.t3Url, { stored: cfg.t3Bin }),
|
|
187
256
|
// Read from disk: `cloop connect` may have replaced it since.
|
|
188
257
|
deviceToken: disk?.deviceToken ?? cfg.deviceToken,
|
|
258
|
+
showOtherThreads: disk?.showOtherThreads ?? false,
|
|
189
259
|
...disk?.origin ?? cfg.origin ? { origin: disk?.origin ?? cfg.origin } : {}
|
|
190
260
|
};
|
|
191
261
|
cfg = renewed;
|
|
@@ -199,8 +269,26 @@ async function run() {
|
|
|
199
269
|
const beat = async () => {
|
|
200
270
|
await renewT3();
|
|
201
271
|
let running = 0;
|
|
272
|
+
let outsideThreads = [];
|
|
202
273
|
try {
|
|
203
|
-
|
|
274
|
+
const enabled = readRunnerConfig()?.showOtherThreads === true;
|
|
275
|
+
const listed = await driver.listThreads({
|
|
276
|
+
scope: enabled ? "all" : "unsettled",
|
|
277
|
+
limit: enabled ? 200 : 1
|
|
278
|
+
});
|
|
279
|
+
running = listed.running;
|
|
280
|
+
if (enabled && driver.name === "t3")
|
|
281
|
+
outsideThreads = listed.threads.filter((t) => !t.cloop).slice(0, 20).map((t) => ({
|
|
282
|
+
id: t.id,
|
|
283
|
+
title: t.title.slice(0, 120),
|
|
284
|
+
updatedAt: t.updatedAt,
|
|
285
|
+
status: t.status
|
|
286
|
+
}));
|
|
287
|
+
if (enabled && driver.name === "claude-code")
|
|
288
|
+
outsideThreads = await listOutsideSessions(
|
|
289
|
+
void 0,
|
|
290
|
+
new Set(listed.threads.map((t) => t.id))
|
|
291
|
+
);
|
|
204
292
|
} catch (err) {
|
|
205
293
|
log(`${driver.name} unreachable: ${err instanceof Error ? err.message : String(err)}`);
|
|
206
294
|
}
|
|
@@ -211,7 +299,7 @@ async function run() {
|
|
|
211
299
|
capabilities: [...driver.capabilities],
|
|
212
300
|
platform: "workstation",
|
|
213
301
|
machine,
|
|
214
|
-
surface: {},
|
|
302
|
+
surface: { driver: driver.name, outsideThreads },
|
|
215
303
|
status: running > 0 ? "active" : "idle",
|
|
216
304
|
event: running > 0 ? `${running} running` : ""
|
|
217
305
|
});
|
|
@@ -1,17 +1,141 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
stripControl
|
|
4
|
+
} from "./chunk-VK6GV757.js";
|
|
2
5
|
import {
|
|
3
6
|
BoxToolError,
|
|
4
|
-
|
|
5
|
-
|
|
7
|
+
RESULT_INSTRUCTION,
|
|
8
|
+
orderThreads,
|
|
9
|
+
parseRunResult
|
|
10
|
+
} from "./chunk-M6U2YVEB.js";
|
|
6
11
|
import {
|
|
7
12
|
CONFIG_DIR
|
|
8
13
|
} from "./chunk-3DIGCVL4.js";
|
|
9
14
|
|
|
15
|
+
// src/run-log.ts
|
|
16
|
+
import {
|
|
17
|
+
appendFileSync,
|
|
18
|
+
chmodSync,
|
|
19
|
+
mkdirSync,
|
|
20
|
+
readdirSync,
|
|
21
|
+
renameSync,
|
|
22
|
+
statSync,
|
|
23
|
+
unlinkSync
|
|
24
|
+
} from "fs";
|
|
25
|
+
import { dirname, join } from "path";
|
|
26
|
+
var RUN_LOG_LINE_MAX = 500;
|
|
27
|
+
var RUN_LOG_MAX_BYTES = 5 * 1024 * 1024;
|
|
28
|
+
var RUN_LOG_KEEP_MS = 14 * 24 * 36e5;
|
|
29
|
+
var SAFE_ID = /^[\w-]{1,100}$/;
|
|
30
|
+
function runLogDir(runsFile) {
|
|
31
|
+
return join(dirname(runsFile), "runs");
|
|
32
|
+
}
|
|
33
|
+
function runLogPath(dir, workId) {
|
|
34
|
+
return SAFE_ID.test(workId) ? join(dir, `${workId}.log`) : null;
|
|
35
|
+
}
|
|
36
|
+
var SECRETS = [
|
|
37
|
+
[/-----BEGIN [A-Z ]*PRIVATE KEY-----.*/g, "[redacted private key]"],
|
|
38
|
+
[/\b(Bearer|Basic)\s+[A-Za-z0-9._~+/=-]{8,}/gi, "$1 [redacted]"],
|
|
39
|
+
[/(:\/\/)[^\s/@:]+:[^\s/@]+@/g, "$1[redacted]@"],
|
|
40
|
+
[
|
|
41
|
+
/([?&](?:access_token|token|key|api_key|apikey|secret|sig|signature|code|password)=)[^&\s"']+/gi,
|
|
42
|
+
"$1[redacted]"
|
|
43
|
+
],
|
|
44
|
+
[
|
|
45
|
+
/(\b(?:[\w-]*(?:token|secret|passw(?:or)?d|pwd|api[_-]?key|apikey|credential|private[_-]?key)[\w-]*|authorization|auth)["']?\s*[=:]\s*["']?)(?!Bearer\b|Basic\b|\[redacted)[^\s"',;}]{4,}/gi,
|
|
46
|
+
"$1[redacted]"
|
|
47
|
+
],
|
|
48
|
+
[
|
|
49
|
+
/(--?[\w-]*(?:token|secret|password|api-key|apikey)[\w-]*[ =])["']?[^\s"']{4,}["']?/gi,
|
|
50
|
+
"$1[redacted]"
|
|
51
|
+
],
|
|
52
|
+
[/\b(?:sk|pk|rk)-[A-Za-z0-9_-]{16,}/g, "[redacted]"],
|
|
53
|
+
[/\b(?:gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,})/g, "[redacted]"],
|
|
54
|
+
[/\bxox[abprs]-[A-Za-z0-9-]{10,}/g, "[redacted]"],
|
|
55
|
+
[/\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/g, "[redacted]"],
|
|
56
|
+
[/\bAIza[0-9A-Za-z_-]{30,}/g, "[redacted]"],
|
|
57
|
+
[/\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{4,}/g, "[redacted]"]
|
|
58
|
+
];
|
|
59
|
+
var OPAQUE = /[A-Za-z0-9_+=-]{32,}/g;
|
|
60
|
+
function redactSecrets(text) {
|
|
61
|
+
let out = text;
|
|
62
|
+
for (const [pattern, replacement] of SECRETS) out = out.replace(pattern, replacement);
|
|
63
|
+
return out.replace(
|
|
64
|
+
OPAQUE,
|
|
65
|
+
(run2) => /^[0-9a-f-]+$/i.test(run2) || !/\d/.test(run2) || !/[A-Za-z]/.test(run2) ? run2 : "[redacted]"
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
function logText(text) {
|
|
69
|
+
const flat = redactSecrets(stripControl(text).replace(/\s+/g, " ").trim());
|
|
70
|
+
return flat.length > RUN_LOG_LINE_MAX ? `${flat.slice(0, RUN_LOG_LINE_MAX - 1)}\u2026` : flat;
|
|
71
|
+
}
|
|
72
|
+
function logLineFor(item) {
|
|
73
|
+
switch (item.type) {
|
|
74
|
+
case "text":
|
|
75
|
+
return item.text;
|
|
76
|
+
case "tool": {
|
|
77
|
+
const input = item.detail ?? item.summary ?? item.target;
|
|
78
|
+
return `> ${item.name}${input ? `: ${input}` : ""}`;
|
|
79
|
+
}
|
|
80
|
+
case "ask":
|
|
81
|
+
return `? ${item.summary}`;
|
|
82
|
+
case "result":
|
|
83
|
+
return item.ok ? `= Result: ${item.text ?? "done"}` : `= Error: ${item.text ?? "no text"}`;
|
|
84
|
+
default:
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function appendRunLog(dir, workId, text, { now = Date.now(), maxBytes = RUN_LOG_MAX_BYTES } = {}) {
|
|
89
|
+
const path = runLogPath(dir, workId);
|
|
90
|
+
if (!path) return;
|
|
91
|
+
const line = `${stamp(now)} ${logText(text)}
|
|
92
|
+
`;
|
|
93
|
+
mkdirSync(dir, { recursive: true, mode: 448 });
|
|
94
|
+
const size = sizeOf(path);
|
|
95
|
+
if (size > 0 && size + Buffer.byteLength(line) > maxBytes) renameSync(path, `${path}.1`);
|
|
96
|
+
appendFileSync(path, line, { mode: 384 });
|
|
97
|
+
}
|
|
98
|
+
function pruneRunLogs(dir, now = Date.now(), keepMs = RUN_LOG_KEEP_MS) {
|
|
99
|
+
mkdirSync(dir, { recursive: true, mode: 448 });
|
|
100
|
+
chmodSync(dir, 448);
|
|
101
|
+
let removed = 0;
|
|
102
|
+
for (const name of readdirSync(dir)) {
|
|
103
|
+
if (!/\.log(\.1)?$/.test(name)) continue;
|
|
104
|
+
const path = join(dir, name);
|
|
105
|
+
try {
|
|
106
|
+
if (statSync(path).mtimeMs < now - keepMs) {
|
|
107
|
+
unlinkSync(path);
|
|
108
|
+
removed++;
|
|
109
|
+
}
|
|
110
|
+
} catch {
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return removed;
|
|
114
|
+
}
|
|
115
|
+
function sizeOf(path) {
|
|
116
|
+
try {
|
|
117
|
+
return statSync(path).size;
|
|
118
|
+
} catch {
|
|
119
|
+
return 0;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
var STOPPED_SUMMARY = "The run was stopped.";
|
|
123
|
+
function endNote(end) {
|
|
124
|
+
return `= ${end.kind === "settled" ? "Done" : "Ended"}: ${end.summary ?? end.kind}`;
|
|
125
|
+
}
|
|
126
|
+
function isEndLogLine(line) {
|
|
127
|
+
return /^\S+ = (Done|Ended): /.test(line);
|
|
128
|
+
}
|
|
129
|
+
function stamp(now) {
|
|
130
|
+
const d = new Date(now);
|
|
131
|
+
return [d.getHours(), d.getMinutes(), d.getSeconds()].map((n) => String(n).padStart(2, "0")).join(":");
|
|
132
|
+
}
|
|
133
|
+
|
|
10
134
|
// src/drivers/local.ts
|
|
11
135
|
import { execFile, execFileSync, spawn } from "child_process";
|
|
12
|
-
import { chmodSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "fs";
|
|
136
|
+
import { chmodSync as chmodSync2, mkdirSync as mkdirSync2, readFileSync, renameSync as renameSync2, writeFileSync } from "fs";
|
|
13
137
|
import { homedir, hostname } from "os";
|
|
14
|
-
import { basename, dirname, join } from "path";
|
|
138
|
+
import { basename, dirname as dirname2, join as join2 } from "path";
|
|
15
139
|
import { createInterface } from "readline";
|
|
16
140
|
var CONTINUE_PROMPT = "Continue the work from where you stopped. If it is already done, say so in one sentence.";
|
|
17
141
|
var PR_URL = /https:\/\/github\.com\/[\w.-]+\/[\w.-]+\/pull\/\d+/g;
|
|
@@ -37,7 +161,7 @@ function failedEnd(harness, run2) {
|
|
|
37
161
|
return { summary: `${lead}${quoted}"`, ...error };
|
|
38
162
|
}
|
|
39
163
|
function localRunsFilePath() {
|
|
40
|
-
return process.env.CLOOP_LOCAL_RUNS_FILE ??
|
|
164
|
+
return process.env.CLOOP_LOCAL_RUNS_FILE ?? join2(homedir(), CONFIG_DIR, "local-runs.json");
|
|
41
165
|
}
|
|
42
166
|
function pickRepo(repos, repository) {
|
|
43
167
|
if (!repository) {
|
|
@@ -104,6 +228,13 @@ var LocalDriver = class {
|
|
|
104
228
|
this.name = deps.dialect.name;
|
|
105
229
|
this.capabilities = deps.dialect.capabilities;
|
|
106
230
|
this.runs = new Map(this.load().map((r) => [r.work_id, r]));
|
|
231
|
+
this.logDir = deps.logDir ?? runLogDir(this.file);
|
|
232
|
+
try {
|
|
233
|
+
const removed = pruneRunLogs(this.logDir, this.now());
|
|
234
|
+
if (removed) this.log(`run logs removed=${removed} older than 14 days`);
|
|
235
|
+
} catch (err) {
|
|
236
|
+
this.log(`run log prune failed: ${err.code ?? "unknown"}`);
|
|
237
|
+
}
|
|
107
238
|
}
|
|
108
239
|
deps;
|
|
109
240
|
name;
|
|
@@ -113,6 +244,8 @@ var LocalDriver = class {
|
|
|
113
244
|
listeners = /* @__PURE__ */ new Map();
|
|
114
245
|
recovering = /* @__PURE__ */ new Set();
|
|
115
246
|
stopping = false;
|
|
247
|
+
logDir;
|
|
248
|
+
logFailed = false;
|
|
116
249
|
get repo() {
|
|
117
250
|
return this.deps.repo ?? gitProbe;
|
|
118
251
|
}
|
|
@@ -122,6 +255,16 @@ var LocalDriver = class {
|
|
|
122
255
|
log(line) {
|
|
123
256
|
this.deps.log?.(line);
|
|
124
257
|
}
|
|
258
|
+
/** One line in the run's own log, for `cloop watch`. A full disk must not stop the run. */
|
|
259
|
+
note(r, text) {
|
|
260
|
+
try {
|
|
261
|
+
appendRunLog(this.logDir, r.work_id, text, { now: this.now() });
|
|
262
|
+
} catch (err) {
|
|
263
|
+
if (this.logFailed) return;
|
|
264
|
+
this.logFailed = true;
|
|
265
|
+
this.log(`run log write failed: ${err.code ?? "unknown"}`);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
125
268
|
now() {
|
|
126
269
|
return this.deps.now?.() ?? Date.now();
|
|
127
270
|
}
|
|
@@ -172,7 +315,8 @@ var LocalDriver = class {
|
|
|
172
315
|
status: this.statusOf(r),
|
|
173
316
|
settled: Boolean(r.end),
|
|
174
317
|
pr_url: r.end?.pr_url ?? null,
|
|
175
|
-
last_assistant_text: r.end?.summary ?? r.last_text ?? null
|
|
318
|
+
last_assistant_text: r.end?.summary ?? r.last_text ?? null,
|
|
319
|
+
resume: this.deps.dialect.name === "claude-code" && r.session_id ? { cwd: r.repo, session_id: r.session_id } : null
|
|
176
320
|
};
|
|
177
321
|
}
|
|
178
322
|
async launch(a) {
|
|
@@ -237,13 +381,19 @@ var LocalDriver = class {
|
|
|
237
381
|
if (!line2) throw new BoxToolError("cannot_steer");
|
|
238
382
|
live.asks = live.asks.slice(1);
|
|
239
383
|
this.write(live, line2);
|
|
384
|
+
this.note(r, `< You answered: ${a.text}`);
|
|
240
385
|
this.remember(r, a.command_id, "answer");
|
|
241
386
|
this.log(`answer work=${r.work_id}`);
|
|
242
387
|
this.askNext(r, live);
|
|
243
388
|
return { delivered: "answer", question_id: ask.request_id };
|
|
244
389
|
}
|
|
245
390
|
if (!live) {
|
|
246
|
-
if (r.end) throw new BoxToolError("already_finished");
|
|
391
|
+
if (r.end && !a.resume) throw new BoxToolError("already_finished");
|
|
392
|
+
if (r.end) {
|
|
393
|
+
this.refuseBusy(r.repo);
|
|
394
|
+
r.end = void 0;
|
|
395
|
+
r.last_text = void 0;
|
|
396
|
+
}
|
|
247
397
|
this.spawnRun(r, a.text, true);
|
|
248
398
|
this.remember(r, a.command_id, "start");
|
|
249
399
|
return { delivered: "start" };
|
|
@@ -259,6 +409,7 @@ ${a.text}` : a.text;
|
|
|
259
409
|
if (!line) throw new BoxToolError("cannot_steer");
|
|
260
410
|
live.outstanding++;
|
|
261
411
|
this.write(live, line);
|
|
412
|
+
this.note(r, `< You: ${a.text}`);
|
|
262
413
|
const delivered = a.mode === "queue" ? "queue" : "steer";
|
|
263
414
|
this.remember(r, a.command_id, delivered);
|
|
264
415
|
this.log(`${delivered} work=${r.work_id}`);
|
|
@@ -276,11 +427,12 @@ ${a.text}` : a.text;
|
|
|
276
427
|
} catch {
|
|
277
428
|
}
|
|
278
429
|
}
|
|
279
|
-
await this.end(r, { kind: "failed", summary:
|
|
430
|
+
await this.end(r, { kind: "failed", summary: STOPPED_SUMMARY });
|
|
280
431
|
return { ok: true, was_running: orphan };
|
|
281
432
|
}
|
|
282
433
|
live.interrupted = true;
|
|
283
434
|
live.child.kill("SIGINT");
|
|
435
|
+
this.note(r, "= Stop requested.");
|
|
284
436
|
const timer = setTimeout(() => {
|
|
285
437
|
if (live.child.exitCode === null && live.child.signalCode === null)
|
|
286
438
|
live.child.kill("SIGTERM");
|
|
@@ -365,9 +517,12 @@ ${a.text}` : a.text;
|
|
|
365
517
|
r.pid = child.pid ?? null;
|
|
366
518
|
this.save();
|
|
367
519
|
this.log(`spawn work=${r.work_id} pid=${r.pid ?? "none"} ${resume ? "resume" : "new"}`);
|
|
520
|
+
this.note(r, resume ? "= Resumed." : `= Started "${r.title}" in ${r.repo}`);
|
|
368
521
|
child.stdin?.on("error", () => {
|
|
369
522
|
});
|
|
370
|
-
child.stdin?.write(dialect.opening(r, prompt
|
|
523
|
+
child.stdin?.write(dialect.opening(r, `${prompt}
|
|
524
|
+
|
|
525
|
+
${RESULT_INSTRUCTION}`));
|
|
371
526
|
if (!dialect.keepsStdin) child.stdin?.end();
|
|
372
527
|
child.stderr?.on("data", (chunk) => {
|
|
373
528
|
live.stderr = (live.stderr + chunk.toString("utf-8")).slice(-2e3);
|
|
@@ -382,6 +537,8 @@ ${a.text}` : a.text;
|
|
|
382
537
|
child.on("close", (code, signal) => void this.onExit(r, live, code, signal));
|
|
383
538
|
}
|
|
384
539
|
onItem(r, live, item) {
|
|
540
|
+
const logged = logLineFor(item);
|
|
541
|
+
if (logged) this.note(r, logged);
|
|
385
542
|
switch (item.type) {
|
|
386
543
|
case "session":
|
|
387
544
|
if (r.session_id !== item.id) {
|
|
@@ -390,18 +547,15 @@ ${a.text}` : a.text;
|
|
|
390
547
|
}
|
|
391
548
|
return;
|
|
392
549
|
case "text":
|
|
393
|
-
r.last_text = item.text.slice(0,
|
|
550
|
+
r.last_text = item.text.slice(0, 1e5);
|
|
394
551
|
this.progress(r, live, item.text, false);
|
|
395
552
|
return;
|
|
396
553
|
case "tool": {
|
|
554
|
+
const line = item.summary ?? (item.target ? `${item.name} ${item.target}` : item.name);
|
|
555
|
+
if (!isProgressSentence(line)) return;
|
|
397
556
|
const changed = item.name !== live.lastTool;
|
|
398
557
|
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
|
-
);
|
|
558
|
+
this.progress(r, live, line, changed);
|
|
405
559
|
return;
|
|
406
560
|
}
|
|
407
561
|
case "ask":
|
|
@@ -417,19 +571,18 @@ ${a.text}` : a.text;
|
|
|
417
571
|
case "result":
|
|
418
572
|
live.outstanding--;
|
|
419
573
|
live.result = { ok: item.ok, ...item.text ? { text: item.text } : {} };
|
|
420
|
-
if (item.text) r.last_text = item.text.slice(0,
|
|
574
|
+
if (item.text) r.last_text = item.text.slice(0, 1e5);
|
|
421
575
|
if (live.outstanding <= 0 && live.asks.length === 0) live.child.stdin?.end();
|
|
422
576
|
return;
|
|
423
577
|
}
|
|
424
578
|
}
|
|
425
579
|
progress(r, live, line, force) {
|
|
580
|
+
const summary = (parseRunResult(line)?.summary ?? line.split("```cloop-result")[0] ?? "").replace(/\s+/g, " ").trim().slice(0, 200);
|
|
581
|
+
if (!isProgressSentence(summary)) return;
|
|
426
582
|
const now = this.now();
|
|
427
583
|
if (!force && now - live.lastProgressAt < (this.deps.progressMs ?? 3e4)) return;
|
|
428
584
|
live.lastProgressAt = now;
|
|
429
|
-
this.emit(r.work_id, {
|
|
430
|
-
kind: "progress",
|
|
431
|
-
summary: line.replace(/\s+/g, " ").trim().slice(0, 200)
|
|
432
|
-
});
|
|
585
|
+
this.emit(r.work_id, { kind: "progress", summary });
|
|
433
586
|
}
|
|
434
587
|
async onExit(r, live, code, signal) {
|
|
435
588
|
if (this.lives.get(r.work_id) === live) this.lives.delete(r.work_id);
|
|
@@ -439,7 +592,7 @@ ${a.text}` : a.text;
|
|
|
439
592
|
`exit work=${r.work_id} ${exit} result=${live.result ? live.result.ok ? "ok" : "error" : "none"}`
|
|
440
593
|
);
|
|
441
594
|
if (live.interrupted) {
|
|
442
|
-
await this.end(r, { kind: "failed", summary:
|
|
595
|
+
await this.end(r, { kind: "failed", summary: STOPPED_SUMMARY });
|
|
443
596
|
return;
|
|
444
597
|
}
|
|
445
598
|
if (live.next) {
|
|
@@ -448,10 +601,12 @@ ${a.text}` : a.text;
|
|
|
448
601
|
}
|
|
449
602
|
if (code === 0 && live.result?.ok) {
|
|
450
603
|
const text = live.result.text?.trim() || r.last_text;
|
|
604
|
+
const result = parseRunResult(text);
|
|
451
605
|
const { pr, note } = await this.outcome(r, text);
|
|
452
606
|
const summary = [r.switched, note, text].filter(Boolean).join(" ").replace(/\s+/g, " ");
|
|
453
607
|
await this.end(r, {
|
|
454
608
|
kind: "settled",
|
|
609
|
+
...result ? { result } : {},
|
|
455
610
|
...summary ? { summary: summary.slice(0, SUMMARY_MAX) } : {},
|
|
456
611
|
...pr ? { pr_url: pr } : {}
|
|
457
612
|
});
|
|
@@ -517,6 +672,7 @@ ${a.text}` : a.text;
|
|
|
517
672
|
r.end = { ...end, at: new Date(this.now()).toISOString() };
|
|
518
673
|
r.pid = null;
|
|
519
674
|
this.save();
|
|
675
|
+
this.note(r, endNote(end));
|
|
520
676
|
this.emit(r.work_id, end);
|
|
521
677
|
}
|
|
522
678
|
emit(id, event) {
|
|
@@ -543,27 +699,38 @@ ${a.text}` : a.text;
|
|
|
543
699
|
return r.end.kind === "settled" ? "completed" : "failed";
|
|
544
700
|
}
|
|
545
701
|
load() {
|
|
546
|
-
|
|
547
|
-
|
|
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
|
-
}
|
|
702
|
+
const cutoff = this.now() - KEEP_MS;
|
|
703
|
+
return readRunRecords(this.file).filter((r) => !(r.end && Date.parse(r.end.at) < cutoff));
|
|
555
704
|
}
|
|
556
705
|
save() {
|
|
557
|
-
|
|
706
|
+
mkdirSync2(dirname2(this.file), { recursive: true, mode: 448 });
|
|
558
707
|
const tmp = `${this.file}.tmp`;
|
|
559
708
|
writeFileSync(tmp, `${JSON.stringify([...this.runs.values()], null, 2)}
|
|
560
709
|
`, { mode: 384 });
|
|
561
|
-
|
|
562
|
-
|
|
710
|
+
chmodSync2(tmp, 384);
|
|
711
|
+
renameSync2(tmp, this.file);
|
|
563
712
|
}
|
|
564
713
|
};
|
|
714
|
+
function readRunRecords(file = localRunsFilePath()) {
|
|
715
|
+
try {
|
|
716
|
+
const raw = JSON.parse(readFileSync(file, "utf-8"));
|
|
717
|
+
return Array.isArray(raw) ? raw.filter(
|
|
718
|
+
(r) => typeof r?.work_id === "string" && typeof r?.repo === "string"
|
|
719
|
+
) : [];
|
|
720
|
+
} catch {
|
|
721
|
+
return [];
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
function isProgressSentence(value) {
|
|
725
|
+
return typeof value === "string" && /\S[ \t]+\S/.test(value.trim().split(/\r?\n/)[0] ?? "");
|
|
726
|
+
}
|
|
565
727
|
|
|
566
728
|
export {
|
|
729
|
+
runLogDir,
|
|
730
|
+
runLogPath,
|
|
731
|
+
STOPPED_SUMMARY,
|
|
732
|
+
isEndLogLine,
|
|
567
733
|
localRunsFilePath,
|
|
568
|
-
LocalDriver
|
|
734
|
+
LocalDriver,
|
|
735
|
+
readRunRecords
|
|
569
736
|
};
|