@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.
@@ -21,6 +21,7 @@ function sanitizeLabel(text, max = 48) {
21
21
  }
22
22
 
23
23
  export {
24
+ stripControl,
24
25
  sanitizeUntrusted,
25
26
  sanitizeLabel
26
27
  };
@@ -5,7 +5,7 @@ import {
5
5
  } from "./chunk-AG6SI6I2.js";
6
6
  import {
7
7
  VERSION
8
- } from "./chunk-7BREDPJD.js";
8
+ } from "./chunk-PBOQMFNN.js";
9
9
  import {
10
10
  getOrigin,
11
11
  readCredentials,
@@ -28,13 +28,14 @@ import {
28
28
  tilde,
29
29
  unitEnv,
30
30
  unitFor
31
- } from "./chunk-7PUGLQL2.js";
31
+ } from "./chunk-VEVDJ3H3.js";
32
+ import "./chunk-M6U2YVEB.js";
32
33
  import "./chunk-JX6VG5FE.js";
33
34
  import "./chunk-R3CLO2FB.js";
34
- import "./chunk-M6MHMCV6.js";
35
- import "./chunk-F36GGCXC.js";
35
+ import "./chunk-KHYA7GEH.js";
36
+ import "./chunk-ZDUKQWKA.js";
36
37
  import "./chunk-AG6SI6I2.js";
37
- import "./chunk-7BREDPJD.js";
38
+ import "./chunk-PBOQMFNN.js";
38
39
  import "./chunk-3DIGCVL4.js";
39
40
  export {
40
41
  DEFAULT_T3_URL,
@@ -1,23 +1,26 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  localRunsFilePath
4
- } from "./chunk-TFZSCXSS.js";
4
+ } from "./chunk-5J4I7DAE.js";
5
+ import "./chunk-VK6GV757.js";
5
6
  import {
6
- boxPath,
7
7
  installedService,
8
- readRunnerConfig,
9
8
  revokeOwnDevice,
10
9
  runSteps,
11
10
  runnerStatePath,
12
11
  threadsFilePath,
13
12
  tokenGone
14
- } from "./chunk-7PUGLQL2.js";
13
+ } from "./chunk-VEVDJ3H3.js";
14
+ import {
15
+ boxPath,
16
+ readRunnerConfig
17
+ } from "./chunk-M6U2YVEB.js";
15
18
  import "./chunk-JX6VG5FE.js";
16
19
  import "./chunk-R3CLO2FB.js";
17
- import "./chunk-M6MHMCV6.js";
18
- import "./chunk-F36GGCXC.js";
20
+ import "./chunk-KHYA7GEH.js";
21
+ import "./chunk-ZDUKQWKA.js";
19
22
  import "./chunk-AG6SI6I2.js";
20
- import "./chunk-7BREDPJD.js";
23
+ import "./chunk-PBOQMFNN.js";
21
24
  import {
22
25
  getOrigin
23
26
  } from "./chunk-3DIGCVL4.js";
@@ -90,13 +93,19 @@ async function disconnect(plan, opts) {
90
93
  }
91
94
  for (const service of plan.services) {
92
95
  await runSteps(service.steps, run);
93
- let stillRunning = true;
94
- try {
95
- run(service.running);
96
- } catch {
97
- stillRunning = false;
98
- }
99
- if (stillRunning) {
96
+ const stillRunning = async () => {
97
+ const until = Date.now() + (opts.stopWaitMs ?? 1e4);
98
+ for (; ; ) {
99
+ try {
100
+ run(service.running);
101
+ } catch {
102
+ return false;
103
+ }
104
+ if (Date.now() >= until) return true;
105
+ await new Promise((resolve) => setTimeout(resolve, 250));
106
+ }
107
+ };
108
+ if (await stillRunning()) {
100
109
  say(
101
110
  `Could not stop the service in ${service.path}. Nothing was removed. Stop it with \`${service.steps.map((s) => s.command.join(" ")).join(" && ")}\`, then run \`cloop disconnect\` again.`
102
111
  );
@@ -4,8 +4,8 @@ import {
4
4
  pruneSessionStates,
5
5
  toolPhrase,
6
6
  writeSessionState
7
- } from "./chunk-OJ5AREUC.js";
8
- import "./chunk-4LFQXBWP.js";
7
+ } from "./chunk-AEWIRLRS.js";
8
+ import "./chunk-VK6GV757.js";
9
9
  import {
10
10
  HeartbeatClient,
11
11
  detectPlatform,
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  sanitizeLabel
4
- } from "./chunk-4LFQXBWP.js";
4
+ } from "./chunk-VK6GV757.js";
5
5
  import {
6
6
  HeartbeatClient,
7
7
  detectPlatform,
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  VERSION
4
- } from "./chunk-7BREDPJD.js";
4
+ } from "./chunk-PBOQMFNN.js";
5
5
  import {
6
6
  applyEnvironmentFlag
7
7
  } from "./chunk-3DIGCVL4.js";
@@ -22,13 +22,17 @@ Usage:
22
22
  cloop hook session-start Claude Code SessionStart hook (reads JSON on stdin)
23
23
  cloop hook activity Lifecycle hook: records what this session is doing
24
24
  cloop connect [--repo <path>]... [--driver t3|claude-code|codex] [--fallback-model <list>]
25
- [--no-service] [--no-start] [--ask-everything] [--full-access]
25
+ [--no-service] [--no-start] [--ask-everything]
26
26
  Make this machine a Cloop runner: T3 Code, else
27
27
  Claude Code or Codex in the repos you name
28
28
  cloop disconnect [--yes] Stop being a Cloop runner: revoke this computer on
29
29
  Cloop, remove its service and runner files
30
30
  cloop box connect cloop connect with T3 Code forced
31
31
  cloop box run Answer the Cloop Agent's runner calls (daemon)
32
+ cloop runs List this machine's runs: open ones, and the last 24 hours
33
+ cloop watch [id] Follow a run as it works. No id: the newest open run.
34
+ Stop it from Threads.
35
+ cloop resume <id> Open a finished run in Claude Code, in its repository
32
36
  cloop runner register --webhook <url> --label <name> [--reactivate] [--rotate-secret]
33
37
  Register a cloud runner endpoint
34
38
  cloop runner verify --label <name>
@@ -43,7 +47,7 @@ async function main() {
43
47
  switch (command) {
44
48
  case "login":
45
49
  case "auth": {
46
- const { loginCommand } = await import("./login-CUQR2MBW.js");
50
+ const { loginCommand } = await import("./login-MXIM6VMA.js");
47
51
  await loginCommand(argv);
48
52
  break;
49
53
  }
@@ -53,32 +57,47 @@ async function main() {
53
57
  break;
54
58
  }
55
59
  case "serve": {
56
- const { startMcpServer } = await import("./mcp-server-YDRR6DYJ.js");
60
+ const { startMcpServer } = await import("./mcp-server-ADCSP4SY.js");
57
61
  await startMcpServer();
58
62
  break;
59
63
  }
60
64
  case "statusline": {
61
- const { statuslineCommand } = await import("./statusline-SLP4CAFB.js");
65
+ const { statuslineCommand } = await import("./statusline-ESZZVOBJ.js");
62
66
  statuslineCommand();
63
67
  break;
64
68
  }
65
69
  case "connect": {
66
- const { connectCommand } = await import("./connect-WRQN37SU.js");
70
+ const { connectCommand } = await import("./connect-WXL6JWJN.js");
67
71
  await connectCommand(argv.slice(1));
68
72
  break;
69
73
  }
70
74
  case "disconnect": {
71
- const { disconnectCommand } = await import("./disconnect-KTII7TXC.js");
75
+ const { disconnectCommand } = await import("./disconnect-ZAVMFITZ.js");
72
76
  await disconnectCommand(argv.slice(1));
73
77
  break;
74
78
  }
75
79
  case "box": {
76
- const { boxCommand } = await import("./box-BS6BSQUO.js");
80
+ const { boxCommand } = await import("./box-ZUDKJY2F.js");
77
81
  await boxCommand(argv.slice(1));
78
82
  break;
79
83
  }
84
+ case "runs": {
85
+ const { runsCommand } = await import("./runs-ZTLNHRRJ.js");
86
+ process.exitCode = runsCommand();
87
+ break;
88
+ }
89
+ case "watch": {
90
+ const { watchCommand } = await import("./runs-ZTLNHRRJ.js");
91
+ process.exitCode = await watchCommand(argv.slice(1));
92
+ break;
93
+ }
94
+ case "resume": {
95
+ const { resumeCommand } = await import("./runs-ZTLNHRRJ.js");
96
+ process.exitCode = await resumeCommand(argv.slice(1));
97
+ break;
98
+ }
80
99
  case "runner": {
81
- const { runnerCommand } = await import("./runner-WW5OU2YU.js");
100
+ const { runnerCommand } = await import("./runner-OVRZFJIY.js");
82
101
  await runnerCommand(argv.slice(1));
83
102
  break;
84
103
  }
@@ -87,10 +106,10 @@ async function main() {
87
106
  const { runnerPermissionHook } = await import("./read-permissions-SPOKGVLO.js");
88
107
  await runnerPermissionHook();
89
108
  } else if (argv[1] === "session-start") {
90
- const { hookSessionStart } = await import("./hook-session-start-MT67MMDM.js");
109
+ const { hookSessionStart } = await import("./hook-session-start-GEBGA63R.js");
91
110
  await hookSessionStart();
92
111
  } else if (argv[1] === "activity") {
93
- const { hookActivity } = await import("./hook-activity-O2NNKTOE.js");
112
+ const { hookActivity } = await import("./hook-activity-R7OPEYBX.js");
94
113
  await hookActivity();
95
114
  } else {
96
115
  console.error(`Unknown hook: ${argv[1] ?? "(none)"}`);
@@ -3,9 +3,9 @@ import {
3
3
  deviceLogin,
4
4
  loginCommand,
5
5
  refusalSentence
6
- } from "./chunk-F36GGCXC.js";
6
+ } from "./chunk-ZDUKQWKA.js";
7
7
  import "./chunk-AG6SI6I2.js";
8
- import "./chunk-7BREDPJD.js";
8
+ import "./chunk-PBOQMFNN.js";
9
9
  import "./chunk-3DIGCVL4.js";
10
10
  export {
11
11
  deviceLogin,
@@ -7,7 +7,7 @@ import {
7
7
  readSessionState,
8
8
  toolPhrase,
9
9
  writeSessionState
10
- } from "./chunk-OJ5AREUC.js";
10
+ } from "./chunk-AEWIRLRS.js";
11
11
  import {
12
12
  appendInbox,
13
13
  drainInbox,
@@ -18,7 +18,7 @@ import {
18
18
  import {
19
19
  sanitizeLabel,
20
20
  sanitizeUntrusted
21
- } from "./chunk-4LFQXBWP.js";
21
+ } from "./chunk-VK6GV757.js";
22
22
  import {
23
23
  HeartbeatClient,
24
24
  detectPlatform,
@@ -33,7 +33,7 @@ import {
33
33
  } from "./chunk-AG6SI6I2.js";
34
34
  import {
35
35
  VERSION
36
- } from "./chunk-7BREDPJD.js";
36
+ } from "./chunk-PBOQMFNN.js";
37
37
  import {
38
38
  getMeshUrl,
39
39
  getToken
@@ -2,10 +2,10 @@
2
2
  import {
3
3
  connectWebhook,
4
4
  runnerCommand
5
- } from "./chunk-M6MHMCV6.js";
6
- import "./chunk-F36GGCXC.js";
5
+ } from "./chunk-KHYA7GEH.js";
6
+ import "./chunk-ZDUKQWKA.js";
7
7
  import "./chunk-AG6SI6I2.js";
8
- import "./chunk-7BREDPJD.js";
8
+ import "./chunk-PBOQMFNN.js";
9
9
  import "./chunk-3DIGCVL4.js";
10
10
  export {
11
11
  connectWebhook,
@@ -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-5J4I7DAE.js";
10
+ import {
11
+ sanitizeLabel,
12
+ stripControl
13
+ } from "./chunk-VK6GV757.js";
14
+ import {
15
+ isLocal,
16
+ readRunnerConfig
17
+ } from "./chunk-M6U2YVEB.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-4LFQXBWP.js";
7
+ } from "./chunk-VK6GV757.js";
8
8
  import {
9
9
  VERSION
10
- } from "./chunk-7BREDPJD.js";
10
+ } from "./chunk-PBOQMFNN.js";
11
11
  import {
12
12
  getToken
13
13
  } from "./chunk-3DIGCVL4.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloop/runner",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "description": "Make your machine and your agents Cloop runners: device login, the runner daemon, the Cloop MCP server, and Claude Code hooks.",