@cloop/runner 0.2.3 → 0.2.5
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-G6HEXSWY.js → box-S2KILP32.js} +67 -22
- package/dist/{chunk-KKFRQ4HQ.js → chunk-2FLM3ESE.js} +223 -829
- package/dist/{chunk-F4WCO6Z4.js → chunk-4XDYJZLF.js} +1 -1
- package/dist/{chunk-TIXA2YXR.js → chunk-53M5DK6D.js} +1 -1
- package/dist/{chunk-OJ5AREUC.js → chunk-AEWIRLRS.js} +1 -1
- package/dist/{chunk-FW7MMQQV.js → chunk-BFVL3E2C.js} +1 -1
- package/dist/chunk-G6JAEE2Y.js +783 -0
- package/dist/{chunk-CDG4IEQY.js → chunk-KOOAVO2C.js} +186 -30
- package/dist/{chunk-4LFQXBWP.js → chunk-VK6GV757.js} +1 -0
- package/dist/{connect-B5RZDVIW.js → connect-FXO5ETUM.js} +15 -4
- package/dist/{disconnect-MFHVNESG.js → disconnect-EDQ7KXTR.js} +10 -7
- 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 +35 -12
- package/dist/{login-L6J5HPTR.js → login-3HHGBYJ7.js} +2 -2
- package/dist/{mcp-server-Z6MG3AAS.js → mcp-server-FOICWAYA.js} +3 -3
- package/dist/{runner-D6Z5S3VC.js → runner-R7XTMUHH.js} +3 -3
- package/dist/runs-N6RLOEQG.js +265 -0
- package/dist/{statusline-5HLPYTYU.js → statusline-4HJMU355.js} +2 -2
- package/package.json +1 -1
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
STOPPED_SUMMARY,
|
|
4
|
+
isEndLogLine,
|
|
5
|
+
localRunsFilePath,
|
|
6
|
+
readRunRecords,
|
|
7
|
+
runLogDir,
|
|
8
|
+
runLogPath
|
|
9
|
+
} from "./chunk-KOOAVO2C.js";
|
|
10
|
+
import {
|
|
11
|
+
sanitizeLabel,
|
|
12
|
+
stripControl
|
|
13
|
+
} from "./chunk-VK6GV757.js";
|
|
14
|
+
import {
|
|
15
|
+
isLocal,
|
|
16
|
+
readRunnerConfig
|
|
17
|
+
} from "./chunk-G6JAEE2Y.js";
|
|
18
|
+
import "./chunk-3DIGCVL4.js";
|
|
19
|
+
|
|
20
|
+
// src/commands/runs.ts
|
|
21
|
+
import { spawnSync } from "child_process";
|
|
22
|
+
import {
|
|
23
|
+
closeSync,
|
|
24
|
+
createReadStream,
|
|
25
|
+
existsSync,
|
|
26
|
+
openSync,
|
|
27
|
+
readSync,
|
|
28
|
+
readdirSync,
|
|
29
|
+
statSync
|
|
30
|
+
} from "fs";
|
|
31
|
+
import { homedir } from "os";
|
|
32
|
+
import { basename, join } from "path";
|
|
33
|
+
import { createInterface } from "readline";
|
|
34
|
+
var RECENT_MS = 24 * 36e5;
|
|
35
|
+
var SHORT = 8;
|
|
36
|
+
function resolved(deps) {
|
|
37
|
+
const file = deps.file ?? localRunsFilePath();
|
|
38
|
+
return {
|
|
39
|
+
file,
|
|
40
|
+
logDir: deps.logDir ?? runLogDir(file),
|
|
41
|
+
out: deps.out ?? ((line) => console.log(line)),
|
|
42
|
+
err: deps.err ?? ((line) => console.error(line)),
|
|
43
|
+
now: deps.now ?? Date.now
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function runState(r) {
|
|
47
|
+
if (!r.end) return "running";
|
|
48
|
+
if (r.end.kind === "settled") return "done";
|
|
49
|
+
return r.end.summary === STOPPED_SUMMARY ? "stopped" : "failed";
|
|
50
|
+
}
|
|
51
|
+
function recentRuns(records, now) {
|
|
52
|
+
const since = now - RECENT_MS;
|
|
53
|
+
return records.filter(
|
|
54
|
+
(r) => !r.end || Date.parse(r.started_at) >= since || Date.parse(r.end.at ?? "") >= since
|
|
55
|
+
).sort((a, b) => Date.parse(b.started_at) - Date.parse(a.started_at));
|
|
56
|
+
}
|
|
57
|
+
function findRun(records, id) {
|
|
58
|
+
if (!id) {
|
|
59
|
+
const open = records.filter((r) => !r.end).sort((a, b) => Date.parse(b.started_at) - Date.parse(a.started_at));
|
|
60
|
+
return open[0] ? { run: open[0] } : { error: "No run is working on this machine. List recent runs with: cloop runs" };
|
|
61
|
+
}
|
|
62
|
+
const want = id.toLowerCase();
|
|
63
|
+
const matches = records.filter((r) => r.work_id.toLowerCase().startsWith(want));
|
|
64
|
+
if (matches.length === 1 && matches[0]) return { run: matches[0] };
|
|
65
|
+
if (matches.length > 1)
|
|
66
|
+
return { error: `${matches.length} runs start with ${id}. Give more of the id.` };
|
|
67
|
+
return { error: `No run on this machine starts with ${id}. List them with: cloop runs` };
|
|
68
|
+
}
|
|
69
|
+
function clock(iso, now) {
|
|
70
|
+
const d = new Date(iso);
|
|
71
|
+
if (Number.isNaN(d.getTime())) return "?";
|
|
72
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
73
|
+
const time = `${pad(d.getHours())}:${pad(d.getMinutes())}`;
|
|
74
|
+
return now - d.getTime() < 20 * 36e5 ? time : `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${time}`;
|
|
75
|
+
}
|
|
76
|
+
function lastLines(path, count, maxBytes = 64 * 1024) {
|
|
77
|
+
if (!path) return [];
|
|
78
|
+
let fd = null;
|
|
79
|
+
try {
|
|
80
|
+
const size = statSync(path).size;
|
|
81
|
+
const start = Math.max(0, size - maxBytes);
|
|
82
|
+
const buffer = Buffer.alloc(size - start);
|
|
83
|
+
fd = openSync(path, "r");
|
|
84
|
+
readSync(fd, buffer, 0, buffer.length, start);
|
|
85
|
+
const lines = buffer.toString("utf-8").split("\n").filter(Boolean);
|
|
86
|
+
return (start > 0 ? lines.slice(1) : lines).slice(-count);
|
|
87
|
+
} catch {
|
|
88
|
+
return [];
|
|
89
|
+
} finally {
|
|
90
|
+
if (fd !== null) closeSync(fd);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function runsCommand(deps = {}) {
|
|
94
|
+
const { file, logDir, out, now } = resolved(deps);
|
|
95
|
+
const runs = recentRuns(readRunRecords(file), now());
|
|
96
|
+
if (runs.length === 0) {
|
|
97
|
+
out("No runs on this machine in the last 24 hours.");
|
|
98
|
+
return 0;
|
|
99
|
+
}
|
|
100
|
+
for (const r of runs) {
|
|
101
|
+
out(
|
|
102
|
+
[
|
|
103
|
+
r.work_id.slice(0, SHORT),
|
|
104
|
+
runState(r).padEnd(7),
|
|
105
|
+
clock(r.started_at, now()).padEnd(5),
|
|
106
|
+
basename(r.repo),
|
|
107
|
+
sanitizeLabel(r.title, 80)
|
|
108
|
+
].join(" ")
|
|
109
|
+
);
|
|
110
|
+
const last = lastLines(runLogPath(logDir, r.work_id), 1)[0];
|
|
111
|
+
if (last) out(` ${sanitizeLabel(last, 100)}`);
|
|
112
|
+
}
|
|
113
|
+
out("");
|
|
114
|
+
out("Follow one with: cloop watch <id>. Open a finished one with: cloop resume <id>");
|
|
115
|
+
return 0;
|
|
116
|
+
}
|
|
117
|
+
function endLine(r) {
|
|
118
|
+
const end = r.end;
|
|
119
|
+
if (!end) return "";
|
|
120
|
+
const words = end.summary?.trim() || (end.kind === "settled" ? "Finished." : "Failed.");
|
|
121
|
+
const head = runState(r) === "stopped" ? "Stopped" : end.kind === "settled" ? "Done" : "Failed";
|
|
122
|
+
return `${head}: ${words}${end.pr_url ? ` ${end.pr_url}` : ""}`;
|
|
123
|
+
}
|
|
124
|
+
var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
125
|
+
async function watchCommand(args, deps = {}) {
|
|
126
|
+
const { file, logDir, out, err } = resolved(deps);
|
|
127
|
+
const found = findRun(readRunRecords(file), args[0]);
|
|
128
|
+
if ("error" in found) {
|
|
129
|
+
err(found.error);
|
|
130
|
+
return 1;
|
|
131
|
+
}
|
|
132
|
+
let run = found.run;
|
|
133
|
+
const path = runLogPath(logDir, run.work_id);
|
|
134
|
+
out(`Watching "${sanitizeLabel(run.title, 120)}"`);
|
|
135
|
+
out(`Repository: ${sanitizeLabel(run.repo, 300)}`);
|
|
136
|
+
out("Stop it from Threads.");
|
|
137
|
+
out("");
|
|
138
|
+
const print = (line) => {
|
|
139
|
+
if (!isEndLogLine(line)) out(stripControl(line));
|
|
140
|
+
};
|
|
141
|
+
for (const line of lastLines(path, 20)) print(line);
|
|
142
|
+
let offset = path && existsSync(path) ? statSync(path).size : 0;
|
|
143
|
+
let partial = "";
|
|
144
|
+
const drain = () => {
|
|
145
|
+
if (!path) return;
|
|
146
|
+
let size;
|
|
147
|
+
try {
|
|
148
|
+
size = statSync(path).size;
|
|
149
|
+
} catch {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (size < offset) offset = 0;
|
|
153
|
+
if (size === offset) return;
|
|
154
|
+
const buffer = Buffer.alloc(size - offset);
|
|
155
|
+
const fd = openSync(path, "r");
|
|
156
|
+
try {
|
|
157
|
+
readSync(fd, buffer, 0, buffer.length, offset);
|
|
158
|
+
} finally {
|
|
159
|
+
closeSync(fd);
|
|
160
|
+
}
|
|
161
|
+
offset = size;
|
|
162
|
+
const text = partial + buffer.toString("utf-8");
|
|
163
|
+
const lines = text.split("\n");
|
|
164
|
+
partial = lines.pop() ?? "";
|
|
165
|
+
for (const line of lines) if (line) print(line);
|
|
166
|
+
};
|
|
167
|
+
for (; ; ) {
|
|
168
|
+
if (run.end) {
|
|
169
|
+
drain();
|
|
170
|
+
if (partial) print(partial);
|
|
171
|
+
out(endLine(run));
|
|
172
|
+
return 0;
|
|
173
|
+
}
|
|
174
|
+
await sleep(deps.pollMs ?? 500);
|
|
175
|
+
drain();
|
|
176
|
+
const next = readRunRecords(file).find((r) => r.work_id === run.work_id);
|
|
177
|
+
if (!next) {
|
|
178
|
+
out("This run is no longer on this machine.");
|
|
179
|
+
return 1;
|
|
180
|
+
}
|
|
181
|
+
run = next;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
var UUID = /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i;
|
|
185
|
+
async function sessionFolder(id, root = join(homedir(), ".claude", "projects")) {
|
|
186
|
+
if (!UUID.test(id)) return null;
|
|
187
|
+
let projects;
|
|
188
|
+
try {
|
|
189
|
+
projects = readdirSync(root);
|
|
190
|
+
} catch {
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
for (const project of projects) {
|
|
194
|
+
const transcript = join(root, project, `${id}.jsonl`);
|
|
195
|
+
if (!existsSync(transcript)) continue;
|
|
196
|
+
const stream = createReadStream(transcript, { encoding: "utf-8" });
|
|
197
|
+
const lines = createInterface({ input: stream, crlfDelay: Number.POSITIVE_INFINITY });
|
|
198
|
+
let seen = 0;
|
|
199
|
+
try {
|
|
200
|
+
for await (const line of lines) {
|
|
201
|
+
if (++seen > 200) break;
|
|
202
|
+
if (!line.includes('"cwd"')) continue;
|
|
203
|
+
try {
|
|
204
|
+
const cwd = JSON.parse(line).cwd;
|
|
205
|
+
if (typeof cwd === "string" && cwd) return cwd;
|
|
206
|
+
} catch {
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
} finally {
|
|
210
|
+
lines.close();
|
|
211
|
+
stream.destroy();
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
216
|
+
async function resumeCommand(args, deps = {}) {
|
|
217
|
+
const { file, out, err } = resolved(deps);
|
|
218
|
+
const id = args[0];
|
|
219
|
+
if (!id) {
|
|
220
|
+
err("Usage: cloop resume <id>. List the runs with: cloop runs");
|
|
221
|
+
return 1;
|
|
222
|
+
}
|
|
223
|
+
const cfg = deps.harness ? null : readRunnerConfig();
|
|
224
|
+
const local = cfg && isLocal(cfg) ? cfg : null;
|
|
225
|
+
const harness = deps.harness ?? local?.driver ?? "claude-code";
|
|
226
|
+
const bin = deps.bin ?? local?.bin ?? (harness === "codex" ? "codex" : "claude");
|
|
227
|
+
const spawn = deps.spawn ?? ((b, a, cwd) => spawnSync(b, a, { cwd, stdio: "inherit" }).status ?? 1);
|
|
228
|
+
const records = readRunRecords(file);
|
|
229
|
+
const found = findRun(records, id);
|
|
230
|
+
if ("run" in found) {
|
|
231
|
+
const r = found.run;
|
|
232
|
+
if (!r.end) {
|
|
233
|
+
err(`It is still running. Follow it with: cloop watch ${r.work_id.slice(0, SHORT)}`);
|
|
234
|
+
return 1;
|
|
235
|
+
}
|
|
236
|
+
if (!existsSync(r.repo)) {
|
|
237
|
+
err(`The repository ${r.repo} is not on this machine any more.`);
|
|
238
|
+
return 1;
|
|
239
|
+
}
|
|
240
|
+
const session = r.session_id ?? (harness === "claude-code" ? r.work_id : null);
|
|
241
|
+
if (!session) {
|
|
242
|
+
err("This run has no session to resume.");
|
|
243
|
+
return 1;
|
|
244
|
+
}
|
|
245
|
+
out(`Opening "${sanitizeLabel(r.title, 120)}" in ${r.repo}`);
|
|
246
|
+
return spawn(bin, harness === "codex" ? ["resume", session] : ["--resume", session], r.repo);
|
|
247
|
+
}
|
|
248
|
+
const folder = await sessionFolder(id, deps.claudeProjects);
|
|
249
|
+
if (folder && existsSync(folder)) {
|
|
250
|
+
out(`Opening the session in ${folder}`);
|
|
251
|
+
return spawn(harness === "claude-code" ? bin : "claude", ["--resume", id], folder);
|
|
252
|
+
}
|
|
253
|
+
err(found.error);
|
|
254
|
+
return 1;
|
|
255
|
+
}
|
|
256
|
+
export {
|
|
257
|
+
findRun,
|
|
258
|
+
lastLines,
|
|
259
|
+
recentRuns,
|
|
260
|
+
resumeCommand,
|
|
261
|
+
runState,
|
|
262
|
+
runsCommand,
|
|
263
|
+
sessionFolder,
|
|
264
|
+
watchCommand
|
|
265
|
+
};
|
|
@@ -4,10 +4,10 @@ import {
|
|
|
4
4
|
} from "./chunk-J6AEO3GT.js";
|
|
5
5
|
import {
|
|
6
6
|
sanitizeLabel
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-VK6GV757.js";
|
|
8
8
|
import {
|
|
9
9
|
VERSION
|
|
10
|
-
} from "./chunk-
|
|
10
|
+
} from "./chunk-BFVL3E2C.js";
|
|
11
11
|
import {
|
|
12
12
|
getToken
|
|
13
13
|
} from "./chunk-3DIGCVL4.js";
|
package/package.json
CHANGED