@cloop/runner 0.3.39 → 0.3.41

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.
@@ -1,8 +1,8 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  serveTakeovers
4
- } from "./chunk-TFOUFEU3.js";
5
- import "./chunk-EJXCUHHV.js";
4
+ } from "./chunk-LMW4JHRH.js";
5
+ import "./chunk-NPTLMG3V.js";
6
6
  import {
7
7
  CAPTURE_TICK_MS,
8
8
  DEFAULT_FALLBACK_MODEL,
@@ -38,7 +38,7 @@ import {
38
38
  systemServices,
39
39
  unitFor,
40
40
  which
41
- } from "./chunk-6R6XDZKO.js";
41
+ } from "./chunk-3XQ44U27.js";
42
42
  import {
43
43
  MeshSocket,
44
44
  WAKE_TICK_MS,
@@ -52,10 +52,11 @@ import {
52
52
  hostedThreadsFilePath,
53
53
  lockThreadsFile,
54
54
  readRunnerState,
55
+ runOutputs,
55
56
  runnerStatePath,
56
57
  threadsFilePath,
57
58
  writeRunnerState
58
- } from "./chunk-A27N773G.js";
59
+ } from "./chunk-WIAWWXGP.js";
59
60
  import {
60
61
  BackgroundReading,
61
62
  BoxToolError,
@@ -100,20 +101,20 @@ import {
100
101
  utcWindow,
101
102
  windowsPowerShellEnv,
102
103
  writeBoxConfig
103
- } from "./chunk-FWAHPPAG.js";
104
+ } from "./chunk-SDVTQCW7.js";
104
105
  import "./chunk-VK6GV757.js";
105
106
  import {
106
107
  olderThan
107
108
  } from "./chunk-365HFCQQ.js";
108
109
  import "./chunk-PKIEWBR7.js";
109
- import "./chunk-DCX2BZPA.js";
110
- import "./chunk-H3NH6NQT.js";
110
+ import "./chunk-LGKSLIU2.js";
111
+ import "./chunk-4MMTNAPM.js";
111
112
  import {
112
113
  getMachine
113
114
  } from "./chunk-SBYSVZ7J.js";
114
115
  import {
115
116
  VERSION
116
- } from "./chunk-6TW44BUM.js";
117
+ } from "./chunk-ILLDES3M.js";
117
118
  import {
118
119
  getMeshUrl,
119
120
  getOrigin,
@@ -122,6 +123,10 @@ import {
122
123
  readCredentials
123
124
  } from "./chunk-D5UZW42X.js";
124
125
 
126
+ // src/commands/box.ts
127
+ import { homedir as homedir3 } from "os";
128
+ import { join as join6 } from "path";
129
+
125
130
  // src/cloudroom-core.ts
126
131
  var CoreError = class extends Error {
127
132
  constructor(status, code, message) {
@@ -1007,9 +1012,11 @@ var CloudroomDriver = class {
1007
1012
  * working in the folder, keeps it. A
1008
1013
  * failure here is logged and the end is reported as it was.
1009
1014
  */
1010
- async tidy(id, end) {
1015
+ async tidy(id, ended) {
1016
+ let end = ended;
1011
1017
  try {
1012
1018
  const s = await this.core.session(id);
1019
+ if (s?.workspace_path && end.kind === "settled") end = { ...end, cwd: s.workspace_path };
1013
1020
  const tree = worktreeOf(s?.workspace ?? null);
1014
1021
  if (!s || !tree) return end;
1015
1022
  const path = folder(s);
@@ -2573,11 +2580,21 @@ async function run2() {
2573
2580
  return e && { thread_id: e.thread_id, settled: Boolean(e.settled_at) };
2574
2581
  }
2575
2582
  });
2583
+ const outputs = runOutputs({
2584
+ origin: cfg.origin ?? getOrigin(),
2585
+ deviceToken: () => token,
2586
+ log: log2,
2587
+ dir: runnerDir(),
2588
+ awakeDir: hosted ? join6(homedir3(), ".cloop", "awake") : null
2589
+ });
2590
+ const previews = outputs.connector;
2591
+ void previews.resume();
2576
2592
  const watch = new BoxWatch({
2577
2593
  file: threadsFile,
2578
2594
  watch: (id, onEvent) => driver.watch(id, onEvent),
2579
2595
  send: (frame) => socket.send({ ...frame }),
2580
- log: log2
2596
+ log: log2,
2597
+ attach: outputs.attach
2581
2598
  });
2582
2599
  const setup = hosted ? void 0 : new SetupShare({
2583
2600
  origin: cfg.origin ?? getOrigin(),
@@ -2820,6 +2837,7 @@ async function run2() {
2820
2837
  const stop = (code = 0) => {
2821
2838
  log2("stopping");
2822
2839
  watch.stop();
2840
+ previews.stop();
2823
2841
  unfollow?.();
2824
2842
  if (driver instanceof LocalDriver) driver.stop();
2825
2843
  lock.release();
@@ -9,7 +9,7 @@ import {
9
9
  readRunnerState,
10
10
  runnerStatePath,
11
11
  threadsFilePath
12
- } from "./chunk-A27N773G.js";
12
+ } from "./chunk-WIAWWXGP.js";
13
13
  import {
14
14
  BoxToolError,
15
15
  CLOOP_THREAD_PREFIX,
@@ -48,7 +48,7 @@ import {
48
48
  windowsExecutable,
49
49
  windowsPowerShellEnv,
50
50
  writeBoxConfig
51
- } from "./chunk-FWAHPPAG.js";
51
+ } from "./chunk-SDVTQCW7.js";
52
52
  import {
53
53
  CLAUDE_FLOOR,
54
54
  CODEX_FLOOR,
@@ -62,14 +62,14 @@ import {
62
62
  } from "./chunk-PKIEWBR7.js";
63
63
  import {
64
64
  connectWebhook
65
- } from "./chunk-DCX2BZPA.js";
65
+ } from "./chunk-LGKSLIU2.js";
66
66
  import {
67
67
  ConnectFailure,
68
68
  connectReport,
69
69
  deviceLogin,
70
70
  newAttempt,
71
71
  sendConnectReport
72
- } from "./chunk-H3NH6NQT.js";
72
+ } from "./chunk-4MMTNAPM.js";
73
73
  import {
74
74
  defaultDeviceName,
75
75
  getMachine,
@@ -77,7 +77,7 @@ import {
77
77
  } from "./chunk-SBYSVZ7J.js";
78
78
  import {
79
79
  VERSION
80
- } from "./chunk-6TW44BUM.js";
80
+ } from "./chunk-ILLDES3M.js";
81
81
  import {
82
82
  STAGING_ORIGIN,
83
83
  getOrigin,
@@ -6,7 +6,7 @@ import {
6
6
  } from "./chunk-SBYSVZ7J.js";
7
7
  import {
8
8
  VERSION
9
- } from "./chunk-6TW44BUM.js";
9
+ } from "./chunk-ILLDES3M.js";
10
10
  import {
11
11
  getOrigin,
12
12
  readCredentials,
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  // src/version.ts
4
- var VERSION = true ? "0.3.39" : "0.0.0-dev";
4
+ var VERSION = true ? "0.3.41" : "0.0.0-dev";
5
5
 
6
6
  export {
7
7
  VERSION
@@ -2,7 +2,7 @@
2
2
  import {
3
3
  ConnectFailure,
4
4
  deviceLogin
5
- } from "./chunk-H3NH6NQT.js";
5
+ } from "./chunk-4MMTNAPM.js";
6
6
  import {
7
7
  ApiError,
8
8
  ReauthError,
@@ -2,17 +2,17 @@
2
2
  import {
3
3
  findRun,
4
4
  ownHarness
5
- } from "./chunk-EJXCUHHV.js";
5
+ } from "./chunk-NPTLMG3V.js";
6
6
  import {
7
7
  lockThreadsFile,
8
8
  threadsFilePath
9
- } from "./chunk-A27N773G.js";
9
+ } from "./chunk-WIAWWXGP.js";
10
10
  import {
11
11
  localRunsFilePath,
12
12
  readRunRecords,
13
13
  runHarness,
14
14
  takeoverSummary
15
- } from "./chunk-FWAHPPAG.js";
15
+ } from "./chunk-SDVTQCW7.js";
16
16
  import {
17
17
  sanitizeLabel
18
18
  } from "./chunk-VK6GV757.js";
@@ -9,7 +9,7 @@ import {
9
9
  runHarness,
10
10
  runLogDir,
11
11
  runLogPath
12
- } from "./chunk-FWAHPPAG.js";
12
+ } from "./chunk-SDVTQCW7.js";
13
13
  import {
14
14
  sanitizeLabel,
15
15
  stripControl
@@ -63,6 +63,61 @@ function serviceIdentity(kind, env) {
63
63
  };
64
64
  }
65
65
 
66
+ // src/spawn-command.ts
67
+ import { execFileSync } from "child_process";
68
+ function commandFor(bin, args, platform = process.platform, comspec = process.env.ComSpec) {
69
+ if (platform !== "win32") return { file: bin, args };
70
+ const executable = windowsExecutable(bin) ?? bin;
71
+ if (!/\.(cmd|bat)$/i.test(executable)) return { file: executable, args };
72
+ return {
73
+ file: comspec ?? "cmd.exe",
74
+ args: ["/d", "/s", "/c", `"${[executable, ...args].map(quoteWindowsArg).join(" ")}"`],
75
+ windowsVerbatimArguments: true
76
+ };
77
+ }
78
+ function quoteWindowsArg(arg) {
79
+ if (arg && !/[ \t"]/.test(arg)) return arg;
80
+ let quoted = '"';
81
+ let backslashes = 0;
82
+ for (const ch of arg) {
83
+ if (ch === "\\") {
84
+ backslashes++;
85
+ continue;
86
+ }
87
+ quoted += "\\".repeat(ch === '"' ? backslashes * 2 + 1 : backslashes) + ch;
88
+ backslashes = 0;
89
+ }
90
+ return `${quoted}${"\\".repeat(backslashes * 2)}"`;
91
+ }
92
+ function preferExecutable(lines) {
93
+ return lines.find((line) => /\.exe$/i.test(line)) ?? lines.find((line) => /\.(cmd|bat)$/i.test(line)) ?? null;
94
+ }
95
+ var windowsExecutables = /* @__PURE__ */ new Map();
96
+ function windowsExecutable(bin) {
97
+ if (/[\\/]/.test(bin)) return bin;
98
+ if (process.platform !== "win32") return null;
99
+ const key = JSON.stringify([bin, process.env.PATH, process.env.PATHEXT]);
100
+ if (windowsExecutables.has(key)) return windowsExecutables.get(key) ?? null;
101
+ let resolved = null;
102
+ try {
103
+ resolved = preferExecutable(
104
+ execFileSync("where.exe", [bin], { encoding: "utf-8", timeout: 5e3 }).split(/\r?\n/).map((line) => line.trim()).filter(Boolean)
105
+ );
106
+ } catch {
107
+ }
108
+ windowsExecutables.set(key, resolved);
109
+ if (!resolved)
110
+ console.error(
111
+ `[runner:spawn] Could not resolve ${bin} to an executable on PATH. Install the agent and reconnect the runner.`
112
+ );
113
+ return resolved;
114
+ }
115
+ function windowsPowerShellEnv(env = process.env) {
116
+ return Object.fromEntries(
117
+ Object.entries(env).filter(([key]) => key.toLowerCase() !== "psmodulepath")
118
+ );
119
+ }
120
+
66
121
  // src/runner-config.ts
67
122
  import { chmodSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "fs";
68
123
  import { dirname, join as join2 } from "path";
@@ -432,61 +487,6 @@ function stamp(now) {
432
487
  return [d.getHours(), d.getMinutes(), d.getSeconds()].map((n) => String(n).padStart(2, "0")).join(":");
433
488
  }
434
489
 
435
- // src/spawn-command.ts
436
- import { execFileSync } from "child_process";
437
- function commandFor(bin, args, platform = process.platform, comspec = process.env.ComSpec) {
438
- if (platform !== "win32") return { file: bin, args };
439
- const executable = windowsExecutable(bin) ?? bin;
440
- if (!/\.(cmd|bat)$/i.test(executable)) return { file: executable, args };
441
- return {
442
- file: comspec ?? "cmd.exe",
443
- args: ["/d", "/s", "/c", `"${[executable, ...args].map(quoteWindowsArg).join(" ")}"`],
444
- windowsVerbatimArguments: true
445
- };
446
- }
447
- function quoteWindowsArg(arg) {
448
- if (arg && !/[ \t"]/.test(arg)) return arg;
449
- let quoted = '"';
450
- let backslashes = 0;
451
- for (const ch of arg) {
452
- if (ch === "\\") {
453
- backslashes++;
454
- continue;
455
- }
456
- quoted += "\\".repeat(ch === '"' ? backslashes * 2 + 1 : backslashes) + ch;
457
- backslashes = 0;
458
- }
459
- return `${quoted}${"\\".repeat(backslashes * 2)}"`;
460
- }
461
- function preferExecutable(lines) {
462
- return lines.find((line) => /\.exe$/i.test(line)) ?? lines.find((line) => /\.(cmd|bat)$/i.test(line)) ?? null;
463
- }
464
- var windowsExecutables = /* @__PURE__ */ new Map();
465
- function windowsExecutable(bin) {
466
- if (/[\\/]/.test(bin)) return bin;
467
- if (process.platform !== "win32") return null;
468
- const key = JSON.stringify([bin, process.env.PATH, process.env.PATHEXT]);
469
- if (windowsExecutables.has(key)) return windowsExecutables.get(key) ?? null;
470
- let resolved = null;
471
- try {
472
- resolved = preferExecutable(
473
- execFileSync("where.exe", [bin], { encoding: "utf-8", timeout: 5e3 }).split(/\r?\n/).map((line) => line.trim()).filter(Boolean)
474
- );
475
- } catch {
476
- }
477
- windowsExecutables.set(key, resolved);
478
- if (!resolved)
479
- console.error(
480
- `[runner:spawn] Could not resolve ${bin} to an executable on PATH. Install the agent and reconnect the runner.`
481
- );
482
- return resolved;
483
- }
484
- function windowsPowerShellEnv(env = process.env) {
485
- return Object.fromEntries(
486
- Object.entries(env).filter(([key]) => key.toLowerCase() !== "psmodulepath")
487
- );
488
- }
489
-
490
490
  // src/t3-bin.ts
491
491
  import { execFile, execFileSync as execFileSync2 } from "child_process";
492
492
  import { existsSync, readdirSync as readdirSync2, statSync as statSync2 } from "fs";
@@ -791,6 +791,30 @@ var WIZARD_FOR = "steps only the user can do by hand: a sign-in, an app permissi
791
791
  var CHECK_BACK_MIN_MINUTES = 5;
792
792
  var CHECK_BACK_MAX_MINUTES = 3 * 24 * 60;
793
793
  var CHECK_BACK_WHY_MAX = 300;
794
+ var RUN_IMAGES_MAX = 6;
795
+ var RUN_IMAGE_MAX_BYTES = 5 * 1024 * 1024;
796
+ var RUN_IMAGE_TYPES = { png: "image/png", jpg: "image/jpeg", webp: "image/webp" };
797
+ function imageExt(bytes) {
798
+ const at = (i, ...want) => want.every((b, j) => bytes[i + j] === b);
799
+ if (at(0, 137, 80, 78, 71, 13, 10, 26, 10)) return "png";
800
+ if (at(0, 255, 216, 255)) return "jpg";
801
+ if (at(0, 82, 73, 70, 70) && at(8, 87, 69, 66, 80)) return "webp";
802
+ return null;
803
+ }
804
+ var RUN_MEDIA_KEY = runMediaPattern("[0-9a-f-]{36}\\/[0-9a-f-]{36}\\/");
805
+ var RUN_MEDIA_FILE = runMediaPattern("");
806
+ var RUN_PREVIEWS_MAX = 3;
807
+ var PREVIEW_HOURS = 24;
808
+ var PREVIEW_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
809
+ function isPort(v) {
810
+ return typeof v === "number" && Number.isInteger(v) && v >= 1 && v <= 65535;
811
+ }
812
+ function isPreviewPath(v) {
813
+ return typeof v === "string" && v.length <= 400 && v.startsWith("/") && !v.startsWith("//") && !/[\s\\]/.test(v);
814
+ }
815
+ function runMediaPattern(folder) {
816
+ return new RegExp(`^${folder}[0-9a-f-]{36}\\.(${Object.keys(RUN_IMAGE_TYPES).join("|")})$`);
817
+ }
794
818
  var RESULT_INSTRUCTION = [
795
819
  "When you finish, end your final message with a fenced block tagged cloop-result containing JSON, for example:",
796
820
  "```cloop-result",
@@ -804,6 +828,8 @@ var RESULT_INSTRUCTION = [
804
828
  ].join("\n");
805
829
  var MACHINE_RESULT_INSTRUCTION = [
806
830
  RESULT_INSTRUCTION,
831
+ `To show the owner a picture, such as a screenshot of a UI change or a result a picture explains better than words, save it as a PNG, JPEG or WebP file outside the repository (for example in /tmp) and list it as an output: {"kind": "image", "title": "The new sign-in page", "path": "/tmp/sign-in.png"}, with the file's absolute path. Cloop uploads the file and shows it on the run. At most ${RUN_IMAGES_MAX} images, each under ${RUN_IMAGE_MAX_BYTES / (1024 * 1024)} MB. On a Cloop Box, the Playwright browser tool takes a screenshot of any page, a local server's page too.`,
832
+ `To let the owner open a web app or page you are running on this machine, such as a dev server, leave the server running after your turn ends: start it in the background, detached from your shell (for example \`nohup npm run dev -- --port 5173 > /tmp/dev.log 2>&1 &\`), and check it answers on its port. Then list it as an output: {"kind": "preview", "title": "The new sign-in page", "port": 5173, "path": "/sign-in"}, with the port it listens on and an optional path to open first. Cloop gives the owner a private link to it for ${PREVIEW_HOURS} hours, while this machine is on. At most ${RUN_PREVIEWS_MAX} previews a run.`,
807
833
  `When you started something that keeps running after this turn (a build, a deploy, a long job) or you wait on something outside, do not wait in the turn: add "check_back": {"in_minutes": 120, "why": "Read the training log and report the score."} to the block. Cloop sends you a message in this conversation then, with your why, and you carry on from there. Give ${CHECK_BACK_MIN_MINUTES} to ${CHECK_BACK_MAX_MINUTES} minutes. Leave it out when the work is done.`
808
834
  ].join("\n");
809
835
  var SUMMARY_MAX = 4e3;
@@ -860,6 +886,23 @@ function readOutput(o) {
860
886
  }
861
887
  case "file":
862
888
  return text(out.path, 400) ? { kind: "file", title, path: out.path } : null;
889
+ case "image":
890
+ if (!text(out.path, 400)) return null;
891
+ return {
892
+ kind: "image",
893
+ title,
894
+ path: out.path,
895
+ ...typeof out.media === "string" && RUN_MEDIA_KEY.test(out.media) ? { media: out.media } : {}
896
+ };
897
+ case "preview":
898
+ if (!isPort(out.port) || out.path !== void 0 && !isPreviewPath(out.path)) return null;
899
+ return {
900
+ kind: "preview",
901
+ title,
902
+ port: out.port,
903
+ ...out.path !== void 0 ? { path: out.path } : {},
904
+ ...typeof out.preview === "string" && PREVIEW_ID.test(out.preview) ? { preview: out.preview } : {}
905
+ };
863
906
  case "wizard":
864
907
  return Array.isArray(out.steps) && out.steps.length ? { kind: "wizard", title, steps: out.steps } : null;
865
908
  default:
@@ -2573,6 +2616,9 @@ function pickRepo(repos, repository) {
2573
2616
  if (!picked) throw new BoxToolError("no_project");
2574
2617
  return picked;
2575
2618
  }
2619
+ function withFolder(r, end) {
2620
+ return end.kind === "settled" ? { ...end, cwd: r.repo } : end;
2621
+ }
2576
2622
  function run(cmd, args, cwd) {
2577
2623
  return new Promise(
2578
2624
  (resolve, reject) => execFile3(
@@ -2993,7 +3039,7 @@ ${a.text}` : a.text;
2993
3039
  }
2994
3040
  if (r.end) {
2995
3041
  const { at: _at, ...end } = r.end;
2996
- onEvent(end);
3042
+ onEvent(withFolder(r, end));
2997
3043
  return unwatch;
2998
3044
  }
2999
3045
  const live = this.lives.get(id);
@@ -3366,7 +3412,7 @@ ${MACHINE_RESULT_INSTRUCTION}`, images));
3366
3412
  r.pid = null;
3367
3413
  this.save();
3368
3414
  this.note(r, endNote(end));
3369
- this.emit(r.work_id, end);
3415
+ this.emit(r.work_id, withFolder(r, end));
3370
3416
  }
3371
3417
  emit(id, event) {
3372
3418
  for (const listener of [...this.listeners.get(id) ?? []]) listener(event);
@@ -3416,6 +3462,11 @@ function readRunRecords(file = localRunsFilePath()) {
3416
3462
  }
3417
3463
 
3418
3464
  export {
3465
+ RUN_IMAGES_MAX,
3466
+ RUN_IMAGE_MAX_BYTES,
3467
+ RUN_IMAGE_TYPES,
3468
+ imageExt,
3469
+ RUN_PREVIEWS_MAX,
3419
3470
  MACHINE_RESULT_INSTRUCTION,
3420
3471
  readRunResult,
3421
3472
  hasResultBlock,
@@ -3438,6 +3489,10 @@ export {
3438
3489
  runnerPath,
3439
3490
  cloopCommand,
3440
3491
  serviceIdentity,
3492
+ commandFor,
3493
+ quoteWindowsArg,
3494
+ windowsExecutable,
3495
+ windowsPowerShellEnv,
3441
3496
  boxPath,
3442
3497
  isLocal,
3443
3498
  isHosted,
@@ -3453,10 +3508,6 @@ export {
3453
3508
  redactSecrets,
3454
3509
  STOPPED_SUMMARY,
3455
3510
  isEndLogLine,
3456
- commandFor,
3457
- quoteWindowsArg,
3458
- windowsExecutable,
3459
- windowsPowerShellEnv,
3460
3511
  parseAttachments,
3461
3512
  fetchImages,
3462
3513
  delivery,