@neta-art/cohub-cli 10.20.0 → 10.21.0

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/README.md CHANGED
@@ -174,6 +174,7 @@ cohub runtime up ./project --harness pi --harness codex
174
174
  cohub runtime up ./project --space <spaceId> --harness codex
175
175
  cohub -s <spaceId> spaces prompt "Continue" --harness codex
176
176
  cohub runtime status --space <spaceId>
177
+ cohub version # this CLI, the cohub on PATH, and each running Runtime's version
177
178
  cohub runtime logs --space <spaceId> --follow
178
179
  cohub runtime up -d
179
180
  cohub runtime down
@@ -0,0 +1,2 @@
1
+ import type { CliAbout } from "@neta-art/cohub/host-bridge";
2
+ export declare function describeCli(env: NodeJS.ProcessEnv, identity: string | null): Promise<CliAbout>;
package/dist/about.js ADDED
@@ -0,0 +1,71 @@
1
+ import { execFile } from "node:child_process";
2
+ import { constants } from "node:fs";
3
+ import { access, realpath, stat } from "node:fs/promises";
4
+ import { delimiter, join } from "node:path";
5
+ import { controlRuntimeInstance, runtimeInstanceDirectory } from "./runtime/instance.js";
6
+ import { readRuntimeSpaceBindings } from "./runtime/space-binding.js";
7
+ import { CLI_VERSION, cliCommit } from "./version.js";
8
+ const PROBE_TIMEOUT_MS = 5_000;
9
+ const STATUS_TIMEOUT_MS = 1_000;
10
+ const PROBE_TTL_MS = 5 * 60_000;
11
+ let probe = null;
12
+ export async function describeCli(env, identity) {
13
+ const [path, runtimes] = await Promise.all([pathCli(env), identity ? runningRuntimes(identity) : []]);
14
+ return { version: CLI_VERSION, commit: cliCommit(), node: process.versions.node, path, runtimes };
15
+ }
16
+ async function pathCli(env) {
17
+ const path = await findOnPath("cohub", await searchPath(env), env);
18
+ if (!path)
19
+ return null;
20
+ // A shim keeps its file across upgrades, so a remembered answer also expires.
21
+ const target = await realpath(path).catch(() => path);
22
+ const key = `${target}\0${await stat(target).then((info) => info.mtimeMs, () => 0)}`;
23
+ if (probe?.key !== key || Date.now() - probe.at > PROBE_TTL_MS)
24
+ probe = { key, at: Date.now(), version: askVersion(path, env) };
25
+ const current = probe;
26
+ const version = await current.version;
27
+ if (!version && probe === current)
28
+ probe = null;
29
+ return { path, version };
30
+ }
31
+ /** A GUI app's own PATH is stale on Windows; the registry holds what a new terminal gets. */
32
+ async function searchPath(env) {
33
+ const own = (env.PATH ?? env.Path ?? "").split(delimiter);
34
+ if (process.platform !== "win32")
35
+ return own.filter(Boolean);
36
+ const read = (key) => new Promise((resolve) => execFile("reg.exe", ["query", key, "/v", "Path"], { timeout: PROBE_TIMEOUT_MS, windowsHide: true }, (_error, stdout) => {
37
+ resolve(/Path\s+REG_(?:EXPAND_)?SZ\s+(.*)/i.exec(stdout ?? "")?.[1]?.trim() ?? "");
38
+ }));
39
+ const [machine, user] = await Promise.all([read("HKLM\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"), read("HKCU\\Environment")]);
40
+ const expand = (value) => value.replace(/%([^%]+)%/g, (whole, name) => env[name] ?? env[name.toUpperCase()] ?? whole);
41
+ return [...new Set([machine, user].flatMap((value) => expand(value).split(delimiter)).concat(own).filter(Boolean))];
42
+ }
43
+ async function findOnPath(name, directories, env) {
44
+ const windows = process.platform === "win32";
45
+ const extensions = windows ? (env.PATHEXT ?? ".COM;.EXE;.BAT;.CMD").split(";").filter(Boolean) : [""];
46
+ for (const directory of directories) {
47
+ for (const extension of extensions) {
48
+ const candidate = join(directory, name + extension.toLowerCase());
49
+ if (await access(candidate, windows ? constants.F_OK : constants.X_OK).then(() => true, () => false))
50
+ return candidate;
51
+ }
52
+ }
53
+ return null;
54
+ }
55
+ function askVersion(path, env) {
56
+ // Node refuses to spawn batch files directly; cmd.exe runs them with the path quoted verbatim.
57
+ const [file, args, verbatim] = /\.(cmd|bat)$/i.test(path)
58
+ ? [env.ComSpec ?? "cmd.exe", ["/d", "/s", "/c", `""${path}" --version"`], true]
59
+ : [path, ["--version"], false];
60
+ return new Promise((resolve) => execFile(file, args, { env, timeout: PROBE_TIMEOUT_MS, windowsHide: true, windowsVerbatimArguments: verbatim }, (error, stdout) => {
61
+ resolve(error ? null : /\d+\.\d+\.\d+\S*/.exec(stdout)?.[0] ?? null);
62
+ }));
63
+ }
64
+ async function runningRuntimes(identity) {
65
+ const { bindings } = await readRuntimeSpaceBindings().catch(() => ({ bindings: [] }));
66
+ const spaces = new Map(bindings.filter((binding) => binding.key === identity).map((binding) => [binding.spaceId, binding.root]));
67
+ const statuses = await Promise.all([...spaces.keys()].slice(-256).map((spaceId) => controlRuntimeInstance(runtimeInstanceDirectory(identity, spaceId), "status", {}, STATUS_TIMEOUT_MS).catch(() => null)));
68
+ return statuses.flatMap((response) => response
69
+ ? [{ spaceId: response.status.spaceId, root: response.status.root, version: response.status.version ?? null }]
70
+ : []);
71
+ }
@@ -64,6 +64,7 @@ export type BoardExportRunOptions = {
64
64
  renderer?: BoardExportRenderer;
65
65
  signal?: AbortSignal;
66
66
  imageMemoryMiB?: number;
67
+ timeoutMs?: number;
67
68
  onPlan?: (plan: {
68
69
  width: number;
69
70
  height: number;
@@ -105,4 +106,5 @@ export declare function runBoardVideoExport(options: BoardVideoExportOptions): P
105
106
  }>;
106
107
  export declare function runBoardExport(options: BoardExportRunOptions): Promise<BoardExportRunResult | null>;
107
108
  export declare function gpuResources(spaceId: string, document: BoardDocument, framePicture?: (key: string) => Uint8Array | undefined): BoardGpuResources;
109
+ export declare const BOARD_EXPORT_FRAME_TIMEOUT_MS = 120000;
108
110
  export {};
@@ -9,9 +9,9 @@ import { dirname, join } from "node:path";
9
9
  import { fileURLToPath } from "node:url";
10
10
  import { tmpdir } from "node:os";
11
11
  const execFile = promisify(execFileCallback);
12
- import { boardMediaNaturalSize, parseBoardMediaSource } from "@neta-art/cohub/board";
12
+ import { boardExportView, boardMediaNaturalSize, parseBoardMediaSource } from "@neta-art/cohub/board";
13
13
  import { imageVariantUrl, MEDIA_VARIANT_MAX, mediaVariantSize, videoFrameUrl } from "@neta-art/cohub/media";
14
- import { hasBoardScripts, boardAnimatesScriptParams, boardAssetKey, boardAssetKeySource, boardMediaRate, boardMediaSpans, boardRenderTheme, boardTimedMedia, boardTextureEdge, boardTexturePixels, createBoardRenderSampler, buildBoardScene, createNodeBoardRenderer, createNodeBoardScriptHost, describeBoardExportWarning, exportBoardImageBytes, parseBoardDocument, planBoardExport, prepareBoardExport, selectBoardExportAssets, } from "./board-kernel.js";
14
+ import { hasBoardScripts, boardAnimatesScriptParams, boardAssetKey, boardAssetKeySource, boardAutoplayLoops, boardMediaRate, boardMediaSpans, boardRenderTheme, boardTimedMedia, boardTextureEdge, boardTexturePixels, createBoardRenderSampler, buildBoardScene, createNodeBoardRenderer, createNodeBoardScriptHost, describeBoardExportWarning, describeBoardScriptLanes, exportBoardImageBytes, parseBoardDocument, planBoardExport, prepareBoardExport, selectBoardExportAssets, } from "./board-kernel.js";
15
15
  import { settleBoardDocument } from "./board-media.js";
16
16
  import { openBoardGpu } from "./board-gpu/daemon.js";
17
17
  import { findBoardGpuBrowser } from "./board-gpu/index.js";
@@ -243,13 +243,17 @@ export async function runBoardExport(options) {
243
243
  throw new Error(`Animation ${options.animation} does not exist.`);
244
244
  }
245
245
  const times = options.times?.length ? options.times : [null];
246
+ const timeoutMs = options.timeoutMs ?? BOARD_EXPORT_FRAME_TIMEOUT_MS;
246
247
  const imageMemoryBytes = (options.imageMemoryMiB ?? BOARD_EXPORT_IMAGE_MEMORY_MIB) * 1024 ** 2;
247
248
  if (!Number.isSafeInteger(imageMemoryBytes) || imageMemoryBytes <= 0)
248
249
  throw new Error("Image memory budget must be a positive integer.");
249
250
  const nodeRenderer = await createNodeBoardRenderer({ fonts: resolveBundledFonts() });
250
251
  const images = createBoardExportImageBudget(nodeRenderer, imageMemoryBytes);
251
252
  const renderer = { ...nodeRenderer, decodeImage: images.decodeImage };
253
+ const STAGES = { prepare: "running the scripts", gpu: "drawing in the browser", images: "loading images", render: "drawing" };
254
+ let doing = "preparing";
252
255
  const trace = (stage, completed, dimensions) => {
256
+ doing = STAGES[stage] ?? doing;
253
257
  if (!debugExport.enabled || completed % 30 !== 0)
254
258
  return;
255
259
  debugExport("%j", {
@@ -349,6 +353,7 @@ export async function runBoardExport(options) {
349
353
  options.onProgress?.(completed, times.length);
350
354
  };
351
355
  const launchGpu = async (browser) => {
356
+ doing = "starting the browser";
352
357
  const exporter = await openBoardGpu(gpuResources(options.spaceId, document, (key) => framePictures.get(key)), { browser, signal: options.signal });
353
358
  try {
354
359
  await exporter.init({ document, animation: options.animation ?? null, theme: options.theme, ...(options.snapshots ? { snapshots: options.snapshots } : {}) });
@@ -367,21 +372,24 @@ export async function runBoardExport(options) {
367
372
  throw new Error("GPU export needs Chrome, Chromium or Edge. Install one, or set COHUB_CHROME to its path.");
368
373
  gpu = await launchGpu(browser);
369
374
  }
370
- for (const time of times) {
375
+ const renderFrame = async (time) => {
371
376
  options.signal?.throwIfAborted();
372
377
  const host = scriptHost;
373
378
  host?.clearFrames();
374
379
  const frameOptions = { sampler, camera: options.camera, ...(time === null ? {} : { at: { animation: options.animation, time } }), region, scale: options.scale, padding: times.length > 1 ? 0 : options.padding };
375
380
  const initial = prepareBoardExport(document, frameOptions);
376
381
  if (!initial)
377
- continue;
382
+ return;
378
383
  let prepared = initial;
379
384
  trace("prepare", completed, initial.plan);
380
385
  const prepareScripts = async (scripts, shaders) => {
381
386
  await scripts.prepare(initial.scene.items, [time ?? 0], initial.plan.scale, initial.document.board, {
382
387
  shaders,
388
+ view: boardExportView(initial.plan),
383
389
  ...(history ? { history: (at) => documentAt(at) } : {}),
384
390
  });
391
+ for (const [id, message] of scripts.emitFailures)
392
+ report(`${id}: ${message}`);
385
393
  for (const id of [...Object.keys(initial.document.board.scripts ?? {}), ...initial.scene.items.map((item) => item.id)]) {
386
394
  const error = scripts.error(id);
387
395
  if (error)
@@ -392,7 +400,7 @@ export async function runBoardExport(options) {
392
400
  if (host && !gpu) {
393
401
  const scripted = await prepareScripts(host, !decided);
394
402
  if (!scripted)
395
- continue;
403
+ return;
396
404
  prepared = scripted;
397
405
  }
398
406
  if (!decided) {
@@ -406,7 +414,7 @@ export async function runBoardExport(options) {
406
414
  host.clearFrames();
407
415
  const scripted = await prepareScripts(host, false);
408
416
  if (!scripted)
409
- continue;
417
+ return;
410
418
  prepared = scripted;
411
419
  }
412
420
  }
@@ -419,7 +427,8 @@ export async function runBoardExport(options) {
419
427
  framePictures.clear();
420
428
  if (options.withImages) {
421
429
  const planned = new Set(prepared.plan.items.map((item) => item.id));
422
- for (const state of timed) {
430
+ const loops = time === null ? [] : boardAutoplayLoops(prepared.scene, time);
431
+ for (const state of [...timed, ...loops]) {
423
432
  const item = prepared.scene.get(state.id);
424
433
  const base = item && state.kind === "video" && state.shown && planned.has(state.id) ? boardExportAssetKey(item) : null;
425
434
  if (!item || !base)
@@ -460,7 +469,7 @@ export async function runBoardExport(options) {
460
469
  for (const warning of result.warnings)
461
470
  report(options.withImages && warning.kind === "images-missing" ? describeBoardExportWarning(warning) : describeWarning(warning));
462
471
  await emit({ time, bytes: result.bytes, width: result.width, height: result.height, scale: result.scale, itemCount: result.itemCount, format: options.format });
463
- continue;
472
+ return;
464
473
  }
465
474
  trace("images", completed, prepared.plan);
466
475
  if (options.withImages) {
@@ -493,7 +502,7 @@ export async function runBoardExport(options) {
493
502
  quality: options.quality,
494
503
  });
495
504
  if (!result)
496
- continue;
505
+ return;
497
506
  for (const warning of result.warnings) {
498
507
  if (warning.kind === "images-missing" && options.withImages)
499
508
  continue;
@@ -511,6 +520,18 @@ export async function runBoardExport(options) {
511
520
  format: result.format,
512
521
  });
513
522
  trace("rendered", completed, result.plan);
523
+ };
524
+ for (const time of times) {
525
+ let timer;
526
+ const stalled = new Promise((_resolve, reject) => {
527
+ timer = setTimeout(() => reject(new Error(describeStall(time, timeoutMs, doing, scriptHost?.status() ?? null))), timeoutMs);
528
+ });
529
+ try {
530
+ await Promise.race([renderFrame(time), stalled]);
531
+ }
532
+ finally {
533
+ clearTimeout(timer);
534
+ }
514
535
  }
515
536
  return completed ? { frames, warnings, media } : null;
516
537
  }
@@ -549,6 +570,13 @@ export function gpuResources(spaceId, document, framePicture = () => undefined)
549
570
  },
550
571
  };
551
572
  }
573
+ export const BOARD_EXPORT_FRAME_TIMEOUT_MS = 120_000;
574
+ function describeStall(time, timeoutMs, doing, lanes) {
575
+ const at = time === null ? "The export" : `The frame at ${time} ms`;
576
+ const busy = lanes ? describeBoardScriptLanes(lanes) : [];
577
+ const hint = doing === "drawing in the browser" ? " The browser runs the scripts and shaders there; a shader may be too heavy." : "";
578
+ return [`${at} did not finish in ${timeoutMs / 1000} s, while ${doing}.${hint}`, ...busy.map((line) => ` ${line}`)].join("\n");
579
+ }
552
580
  function describeWarning(warning) {
553
581
  if (warning.kind === "scale-clamped") {
554
582
  return `Scale reduced from ${warning.requested}x to ${Number(warning.applied).toFixed(2)}x to stay within the size limit.`;
@@ -1,11 +1,12 @@
1
1
  import { spawn } from "node:child_process";
2
2
  import { createHash } from "node:crypto";
3
- import { lstatSync, mkdirSync, readFileSync, statSync, unlinkSync } from "node:fs";
3
+ import { lstatSync, mkdirSync, statSync, unlinkSync } from "node:fs";
4
4
  import { createConnection, createServer } from "node:net";
5
5
  import { tmpdir, userInfo } from "node:os";
6
6
  import { join } from "node:path";
7
7
  import { fileURLToPath } from "node:url";
8
8
  import { launchBoardGpuExporter } from "./index.js";
9
+ import { CLI_VERSION } from "../version.js";
9
10
  const IDLE_MS = 5 * 60_000;
10
11
  const CONNECT_MS = 20_000;
11
12
  const TURN_MS = 20_000;
@@ -29,14 +30,6 @@ function lines(socket, handle) {
29
30
  });
30
31
  return (message) => socket.write(`${JSON.stringify(message)}\n`);
31
32
  }
32
- function version() {
33
- try {
34
- return JSON.parse(readFileSync(new URL("../../package.json", import.meta.url), "utf8")).version;
35
- }
36
- catch {
37
- return "dev";
38
- }
39
- }
40
33
  export function boardGpuSocket(browser) {
41
34
  let page = 0;
42
35
  try {
@@ -44,7 +37,7 @@ export function boardGpuSocket(browser) {
44
37
  }
45
38
  catch { }
46
39
  const { uid } = userInfo();
47
- const key = createHash("sha256").update(`${version()}\0${browser}\0${page}\0${uid}`).digest("hex").slice(0, 16);
40
+ const key = createHash("sha256").update(`${CLI_VERSION}\0${browser}\0${page}\0${uid}`).digest("hex").slice(0, 16);
48
41
  if (process.platform === "win32")
49
42
  return join("\\\\.\\pipe", `cohub-board-gpu-${key}`);
50
43
  const dir = join(process.env.XDG_RUNTIME_DIR || tmpdir(), `cohub-${uid}`);
@@ -174,7 +174,7 @@ export async function launchBoardGpuExporter(resources, options) {
174
174
  await call(`cohubBoardGpu.init(${JSON.stringify(init)})`);
175
175
  },
176
176
  compile: (shaders) => call(`cohubBoardGpu.compile(${JSON.stringify(shaders)})`),
177
- profile: (input) => call(`cohubBoardGpu.profile(${JSON.stringify(input)})`),
177
+ profile: (input) => call(`cohubBoardGpu.profile(${JSON.stringify(input)})`, Math.max(CALL_TIMEOUT_MS, input.budgetMs * 2 + 60_000)),
178
178
  async render(frame) {
179
179
  const { data, ...rest } = await call(`cohubBoardGpu.render(${JSON.stringify(frame)})`);
180
180
  return { ...rest, bytes: new Uint8Array(Buffer.from(data, "base64")) };