premanmcp 1.1.18 → 1.1.20

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/bin/link.js CHANGED
@@ -253,7 +253,7 @@ function renderLinkBlock({ href, webUrl, label, tone = null, stream }) {
253
253
  const tinted = coloured
254
254
  ? `${tone ? paintTone(tone, stream) : DIM}${label}${RESET}`
255
255
  : label;
256
- const headline = rich ? terminalHyperlink(href, tinted) : label;
256
+ const headline = rich ? terminalHyperlink(href, tinted) : href?.startsWith("preman://") ? `${label}: ${href}` : label;
257
257
  const fallback = coloured ? `${DIM} ${webUrl}${RESET}` : ` ${webUrl}`;
258
258
 
259
259
  return `${headline}\n${fallback}`;
package/bin/runner.js CHANGED
@@ -41,6 +41,7 @@ import { fileURLToPath } from "node:url";
41
41
  import { executeEvalRun, markAgentProcess } from "./eval.js";
42
42
  import { discoverTarget, startAdapter, stepFeed } from "./eval_target.js";
43
43
  import { resolveEvalSurface } from "./link.js";
44
+ import { localDeviceId } from "./task_local.js";
44
45
  import { CREDENTIALS_DIR, backendUrl, callBackendJson, cliInvocation, makeArgs, nowMs, onPath, openUrl, packageVersion, resolveApiKey } from "./shared.js";
45
46
 
46
47
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
@@ -201,7 +202,7 @@ export async function registerRunner(args, { agent, projectPath, label = "", api
201
202
  project_name: path.basename(project),
202
203
  platform: `${process.platform}-${process.arch}`,
203
204
  app_version: packageVersion() || undefined,
204
- capabilities: RUNNER_CAPABILITIES,
205
+ capabilities: { ...RUNNER_CAPABILITIES, local_device_id: localDeviceId() },
205
206
  },
206
207
  }
207
208
  );
@@ -710,7 +711,7 @@ export async function sendHeartbeat(args, state, { busy = false, log = () => {}
710
711
  // it carries expire: an agent restarted on a new port makes the old
711
712
  // URL wrong, not merely stale, and a dashboard offering a Run button
712
713
  // for it would fail on the press.
713
- ...(advertised ? { capabilities: advertised } : {}),
714
+ capabilities: { ...RUNNER_CAPABILITIES, ...(advertised || {}), local_device_id: localDeviceId() },
714
715
  },
715
716
  }
716
717
  );
package/bin/shared.js CHANGED
@@ -22,6 +22,7 @@ import os from "node:os";
22
22
  import path from "node:path";
23
23
  import { createInterface } from "node:readline/promises";
24
24
  import { fileURLToPath } from "node:url";
25
+ import { readCredentials, writeCredentials, clearCredentials } from "./credentials.js";
25
26
 
26
27
  // Generated wrappers import tool_proxy.py from this folder (design spec §6.0.2).
27
28
  // Set here because every entry point imports this module, and the wrapper check
@@ -140,6 +141,8 @@ function shimTarget(shimPath) {
140
141
  // what keeps this from reading an executable into memory to look for paths.
141
142
  if (statSync(shimPath).size > 16 * 1024) return "";
142
143
  const body = readFileSync(shimPath, "utf8");
144
+ const bundled = body.match(/^# preman-cli-entrypoint: (.+)$/m);
145
+ if (body.includes("preman-cli-launcher-v1") && bundled) return realpathSync(bundled[1]);
143
146
  // Covers npm's .cmd and its shell wrapper, and pnpm's, all of which name
144
147
  // the entry point relative to the shim or absolutely.
145
148
  const match = body.match(/["']?([^"'\s]*node_modules[/\\][^"'\s]+\.(?:js|mjs|cjs))["']?/);
@@ -152,6 +155,10 @@ function shimTarget(shimPath) {
152
155
  }
153
156
 
154
157
  export function cliInvocation() {
158
+ if (process.env.PREMAN_CLI_EXECUTABLE && path.isAbsolute(process.env.PREMAN_CLI_EXECUTABLE) && existsSync(process.env.PREMAN_CLI_EXECUTABLE)) {
159
+ const file = process.env.PREMAN_CLI_EXECUTABLE;
160
+ return process.platform === "win32" ? `"${file}"` : `'${file.replaceAll("'", "'\\''")}'`;
161
+ }
155
162
  return (_invocation ||=
156
163
  pathPremanOwner() === "premanmcp" ? "preman" : "npm exec -y premanmcp@latest --");
157
164
  }
@@ -229,22 +236,11 @@ export function writeJsonFile(filePath, value) {
229
236
  }
230
237
 
231
238
  export function readStoredCredentials() {
232
- try {
233
- const raw = readFileSync(CREDENTIALS_FILE, "utf8").trim();
234
- if (!raw) return null;
235
- const creds = JSON.parse(raw);
236
- if (creds && typeof creds.api_key === "string" && creds.api_key.startsWith("pm_live_")) {
237
- return creds;
238
- }
239
- } catch {
240
- // No stored credentials yet.
241
- }
242
- return null;
239
+ return readCredentials({ backend: process.env.PREMAN_BACKEND || undefined, accountId: process.env.PREMAN_ACCOUNT_ID || undefined });
243
240
  }
244
241
 
245
242
  export function saveStoredCredentials(creds) {
246
- mkdirSync(CREDENTIALS_DIR, { recursive: true, mode: 0o700 });
247
- writeFileSync(CREDENTIALS_FILE, `${JSON.stringify(creds, null, 2)}\n`, { mode: 0o600 });
243
+ writeCredentials(creds);
248
244
  warnIfModesAreDecorative();
249
245
  }
250
246
 
@@ -269,9 +265,7 @@ function warnIfModesAreDecorative() {
269
265
 
270
266
  /** Forget the stored key. Returns whether there was one. */
271
267
  export function clearStoredCredentials() {
272
- if (!existsSync(CREDENTIALS_FILE)) return false;
273
- rmSync(CREDENTIALS_FILE, { force: true });
274
- return true;
268
+ return clearCredentials();
275
269
  }
276
270
 
277
271
  /** True when the key in play came from a flag or the environment, not our store. */
@@ -1123,6 +1117,8 @@ export async function authenticateTerminal(args) {
1123
1117
  };
1124
1118
  saveStoredCredentials({
1125
1119
  api_key: creds.api_key,
1120
+ key_id: key.id || key.key_id || null,
1121
+ user_id: sessionUser?.id ?? null,
1126
1122
  backend_url: creds.backend_url,
1127
1123
  frontend_url: creds.frontend_url,
1128
1124
  user_email: creds.user_email,
@@ -1180,6 +1176,10 @@ export const LAUNCHER_ARGS = ["exec", "-y", "premanmcp@latest", "--"];
1180
1176
  * that was right and accepting one that could not start.
1181
1177
  */
1182
1178
  export function launcherFor(platform = process.platform) {
1179
+ if (process.env.PREMAN_CLI_EXECUTABLE && path.isAbsolute(process.env.PREMAN_CLI_EXECUTABLE) && existsSync(process.env.PREMAN_CLI_EXECUTABLE)) {
1180
+ return platform === "win32" ? { command: "cmd", args: ["/c", process.env.PREMAN_CLI_EXECUTABLE, "start"] }
1181
+ : { command: process.env.PREMAN_CLI_EXECUTABLE, args: ["start"] };
1182
+ }
1183
1183
  if (platform === "win32") {
1184
1184
  return { command: "cmd", args: ["/c", LAUNCHER_COMMAND, ...LAUNCHER_ARGS] };
1185
1185
  }
@@ -1214,7 +1214,8 @@ export function buildServerConfig(args, { pairCode = "", codingAgent = "" } = {}
1214
1214
  PREMAN_FRONTEND: frontendUrl(args),
1215
1215
  };
1216
1216
  const apiKey = resolveApiKey(args);
1217
- if (apiKey) env.PREMAN_API_KEY = apiKey;
1217
+ if (apiKey && !process.env.PREMAN_CLI_EXECUTABLE) env.PREMAN_API_KEY = apiKey;
1218
+ if (process.env.PREMAN_ACCOUNT_ID) env.PREMAN_ACCOUNT_ID = process.env.PREMAN_ACCOUNT_ID;
1218
1219
  if (pairCode) env.PREMAN_PAIR_CODE = pairCode;
1219
1220
  if (codingAgent) env.PREMAN_CODING_AGENT = codingAgent;
1220
1221
 
@@ -0,0 +1,127 @@
1
+ import { stripVTControlCharacters } from "node:util";
2
+
3
+ const FINISHED = new Set(["done", "failed", "cancelled"]);
4
+ const MAX_LINES = 24;
5
+ const MAX_BUFFER = 65536;
6
+
7
+ // Only action names and outcomes enter the transcript, never tool arguments,
8
+ // results, credentials, or the agent's private reasoning.
9
+ export function cleanLabel(value, limit = 120) {
10
+ return stripVTControlCharacters(String(value ?? ""))
11
+ .replace(/[\x00-\x1f\x7f-\x9f]/g, " ")
12
+ .replace(/(?:pm_live_|sk-)[A-Za-z0-9_-]+/g, "[redacted]")
13
+ .slice(0, limit);
14
+ }
15
+
16
+ export class SwarmProgress {
17
+ constructor({ write, colour = false }) {
18
+ this.write = write;
19
+ this.colour = colour;
20
+ this.cells = new Map();
21
+ this.lines = [];
22
+ this.omitted = 0;
23
+ }
24
+
25
+ state(cell) {
26
+ if (!this.cells.has(cell.id)) {
27
+ this.cells.set(cell.id, { seen: new Set(), offset: 0, pending: "", tools: new Map() });
28
+ }
29
+ return this.cells.get(cell.id);
30
+ }
31
+
32
+ note(cell, key, text, outcome = "") {
33
+ const state = this.state(cell);
34
+ if (state.seen.has(key)) return;
35
+ // Tool logs are untrusted and a single agent can emit indefinitely.
36
+ if (state.seen.size >= 500) return;
37
+ state.seen.add(key);
38
+ const mark = outcome === "failed" ? "✗" : outcome === "done" ? "✓" : "·";
39
+ const coloured = this.colour && outcome ? `\x1b[${outcome === "failed" ? 31 : 32}m${mark}\x1b[0m` : mark;
40
+ if (this.lines.length < MAX_LINES) this.lines.push(` ${coloured} ${cleanLabel(cell.harness, 24)} ${cleanLabel(cell.id, 8)} ${text}`);
41
+ else this.omitted += 1;
42
+ }
43
+
44
+ observe(cells, { results = false } = {}) {
45
+ for (const cell of cells) {
46
+ const evidence = cell.evidence || {};
47
+ if (!results) {
48
+ if (cell.status === "running") this.note(cell, "running", "Agent starting");
49
+ if (evidence.container_id) this.note(cell, "container", "Container started", "done");
50
+ if (cell.reached === true) this.note(cell, "connected", "Connected to the MCP", "done");
51
+ if (evidence.tools_listed === true) this.note(cell, "listed", "Tools loaded", "done");
52
+ continue;
53
+ }
54
+ for (const tool of (evidence.tools_called || []).slice(0, 100)) {
55
+ this.note(cell, `called:${tool}`, `Called ${cleanLabel(tool)}`, "done");
56
+ }
57
+ if (FINISHED.has(cell.status)) {
58
+ const failed = cell.status !== "done" || cell.selected === false;
59
+ const result = cell.selected === true ? "Expected tool selected" : cell.selected === false ? "Expected tool not selected" : "Not scored";
60
+ const reason = cell.verdict_reason ? ` · ${cleanLabel(cell.verdict_reason).replaceAll("_", " ")}` : "";
61
+ this.note(cell, `finished:${cell.status}`, `${cell.status === "cancelled" ? "Cancelled" : "Finished"} — ${result}${reason}`, failed ? "failed" : "done");
62
+ }
63
+ }
64
+ }
65
+
66
+ console(cell, chunk) {
67
+ const state = this.state(cell);
68
+ if (chunk.truncated) state.pending = "";
69
+ state.offset = Number.isFinite(chunk.total) ? chunk.total : state.offset;
70
+ let text = state.pending + String(chunk.text || "");
71
+ if (text.length > MAX_BUFFER) {
72
+ text = text.slice(-MAX_BUFFER);
73
+ text = text.slice(text.indexOf("\n") + 1);
74
+ }
75
+ const lines = text.split("\n");
76
+ state.pending = lines.pop() || "";
77
+ if (FINISHED.has(cell.status) && state.pending) {
78
+ lines.push(state.pending);
79
+ state.pending = "";
80
+ }
81
+ for (const line of lines) {
82
+ let event;
83
+ try { event = JSON.parse(line); } catch { continue; }
84
+ if (!event || typeof event !== "object") continue;
85
+ const item = event.item;
86
+ if (item?.type === "mcp_tool_call") {
87
+ const name = cleanLabel(item.tool || item.name || "MCP tool");
88
+ const finished = event.type === "item.completed";
89
+ const failed = Boolean(item.error) || item.status === "failed";
90
+ this.note(cell, `event:${item.id}:${name}:${event.type}`, `${finished ? failed ? "Tool failed" : "Tool completed" : "Calling"}: ${name}`, finished ? failed ? "failed" : "done" : "");
91
+ if (finished && !failed && state.seen.size < 500) state.seen.add(`called:${item.tool || item.name}`);
92
+ } else if (item?.type === "command_execution" && event.type === "item.started") {
93
+ this.note(cell, `command:${item.id}`, "Running a shell command");
94
+ }
95
+ const content = event.message?.content || event.content;
96
+ if (Array.isArray(content)) for (const block of content) {
97
+ if (block?.type === "tool_use") {
98
+ const name = cleanLabel(block.name || "tool");
99
+ if (state.tools.size < 100) state.tools.set(block.id, name);
100
+ this.note(cell, `use:${block.id}`, `Calling: ${name}`);
101
+ } else if (block?.type === "tool_result") {
102
+ const name = state.tools.get(block.tool_use_id) || "tool";
103
+ this.note(cell, `result:${block.tool_use_id}`, `Tool ${block.is_error ? "failed" : "completed"}: ${name}`, block.is_error ? "failed" : "done");
104
+ }
105
+ }
106
+ if (event.type === "turn.started") this.note(cell, "turn", "Agent working");
107
+ if (event.type === "turn.failed") this.note(cell, "turn-failed", "Agent turn failed", "failed");
108
+ }
109
+ }
110
+
111
+ flush() {
112
+ if (this.lines.length) this.write(`${this.lines.slice(0, MAX_LINES).join("\n")}\n`);
113
+ if (this.omitted) this.write(` … ${this.omitted} more updates; full details in the swarm page.\n`);
114
+ this.lines = [];
115
+ this.omitted = 0;
116
+ }
117
+ }
118
+
119
+ export function progressLine(run, cells, started) {
120
+ const counts = {};
121
+ for (const cell of cells) counts[cell.status] = (counts[cell.status] || 0) + 1;
122
+ const done = (counts.done || 0) + (counts.failed || 0) + (counts.cancelled || 0);
123
+ const elapsed = Math.max(0, Math.floor((Date.now() - started) / 1000));
124
+ const age = elapsed < 60 ? `${elapsed}s` : `${Math.floor(elapsed / 60)}m ${elapsed % 60}s`;
125
+ const state = run.status === "queued" ? "Waiting for a worker" : FINISHED.has(run.status) ? cleanLabel(run.status) : "Running";
126
+ return `${state} · ${counts.queued || 0} queued · ${counts.running || 0} running · ${done}/${cells.length} finished · watching ${age}\n`;
127
+ }
package/bin/swarm.js CHANGED
@@ -10,6 +10,7 @@
10
10
  */
11
11
 
12
12
  import { setTimeout as wait } from "node:timers/promises";
13
+ import { SwarmProgress, cleanLabel, progressLine } from "./swarm-progress.js";
13
14
 
14
15
  import {
15
16
  backendUrl,
@@ -27,12 +28,16 @@ Swarm options:
27
28
  preman swarm list Recent swarms and what each found
28
29
  preman swarm launch --server <slug|id> --ask "<what a developer asks>" [--ask ...]
29
30
  [--expect tool,tool] [--agents claude_code,codex,...] [--rivals 0,5]
30
- [--size 60] [--name <label>] [--dry-run]
31
- Start a swarm (--dry-run only counts it)
31
+ [--size 60] [--name <label>] [--visibility resident|deferred]
32
+ [--model <model>] [--budget <USD>] [--dry-run] [--no-watch]
33
+ Launch and follow live actions until done
34
+ --no-watch queues and returns immediately
32
35
  preman swarm watch <id> [--once] Follow a swarm until it finishes
33
36
  preman swarm stop <id> Stop a swarm; unfinished agents do not run
34
37
  preman swarm bank [--server <slug|id>] Behaviors PreMan learned and you kept
35
- --json Print the raw JSON instead
38
+ --json Print JSON with result links instead
39
+ --budget Stops new cells at priced spend + reservations;
40
+ cannot cap unpriced provider usage
36
41
  `;
37
42
 
38
43
  const ENVIRONMENTS = [
@@ -51,7 +56,7 @@ const AGENT_NAMES = {
51
56
  };
52
57
 
53
58
  const TERMINAL = new Set(["done", "failed", "cancelled"]);
54
- const POLL_MS = 5000;
59
+ const POLL_MS = 2000;
55
60
 
56
61
  function environmentOf(harness) {
57
62
  return ENVIRONMENTS.find((env) => env.harnesses.includes(harness))?.id || "other";
@@ -89,7 +94,7 @@ export function positionals(raw) {
89
94
  return out;
90
95
  }
91
96
 
92
- const BARE_FLAGS = new Set(["--json", "--once", "--dry-run", "--no-color"]);
97
+ const BARE_FLAGS = new Set(["--json", "--once", "--dry-run", "--no-color", "--no-watch"]);
93
98
 
94
99
  function listOf(value) {
95
100
  return String(value || "")
@@ -179,7 +184,7 @@ export function renderSwarm(run, cells, { colour = false } = {}) {
179
184
  const done = (cells || []).filter((cell) => cell.status === "done" || cell.status === "failed").length;
180
185
  const lines = [];
181
186
  lines.push(
182
- `${paint.bold(run.release_label || run.id)} ${paint.dim(run.hosted_mcp_name || run.hosted_mcp_slug || "")} ${run.status}`
187
+ `${paint.bold(cleanLabel(run.release_label || run.id))} ${paint.dim(cleanLabel(run.hosted_mcp_name || run.hosted_mcp_slug || ""))} ${cleanLabel(run.status)}`
183
188
  );
184
189
  if (summary.scored) {
185
190
  const range = likelyRange(summary.picked, summary.scored);
@@ -198,8 +203,8 @@ export function renderSwarm(run, cells, { colour = false } = {}) {
198
203
  const rate = agent.rate === null ? paint.dim("—") : agent.rate >= 0.75 ? paint.green(pct(agent.rate)) : agent.rate >= 0.4 ? paint.yellow(pct(agent.rate)) : paint.red(pct(agent.rate));
199
204
  const working = agent.running ? paint.dim(` · ${agent.running} working`) : "";
200
205
  const rival = [...agent.lostTo.entries()].sort((a, b) => b[1] - a[1])[0];
201
- const lost = rival ? paint.dim(` · lost to ${rival[0]} ×${rival[1]}`) : "";
202
- lines.push(` ${truncate(agent.name, 30).padEnd(30)} ${String(rate).padStart(colour ? 13 : 4)} ${paint.dim(`${agent.picked}/${agent.scored}`)}${working}${lost}`);
206
+ const lost = rival ? paint.dim(` · lost to ${cleanLabel(rival[0])} ×${rival[1]}`) : "";
207
+ lines.push(` ${truncate(cleanLabel(agent.name), 30).padEnd(30)} ${String(rate).padStart(colour ? 13 : 4)} ${paint.dim(`${agent.picked}/${agent.scored}`)}${working}${lost}`);
203
208
  }
204
209
  }
205
210
  return `${lines.join("\n")}\n`;
@@ -227,7 +232,9 @@ function requireToken(args) {
227
232
  async function call(args, token, method, path, options = {}) {
228
233
  const result = await callBackendJson(args, method, path, { token, ...options });
229
234
  if (!result.ok) {
230
- throw new Error(`${method} ${path} on ${backendUrl(args)}: ${result.status_code} ${describeFailure(result, "request failed")}`);
235
+ const error = new Error(`${method} ${path} on ${backendUrl(args)}: ${result.status_code} ${describeFailure(result, "request failed")}`);
236
+ error.status = result.status_code;
237
+ throw error;
231
238
  }
232
239
  return result;
233
240
  }
@@ -255,9 +262,129 @@ function swarmLink(args, id) {
255
262
  return `${frontendUrl(args)}/workbench/swarms/swarms/${encodeURIComponent(id)}`;
256
263
  }
257
264
 
265
+ function swarmLinks(args, id) {
266
+ const route = `/workbench/swarms/swarms/${encodeURIComponent(id)}`;
267
+ const matrixRoute = `/workbench/harness-matrix/runs/${encodeURIComponent(id)}`;
268
+ return {
269
+ web_url: swarmLink(args, id),
270
+ native_url: `preman://open?${new URLSearchParams({ source: "agent", route })}`,
271
+ matrix_url: `${frontendUrl(args)}${matrixRoute}`,
272
+ matrix_native_url: `preman://open?${new URLSearchParams({ source: "agent", route: matrixRoute })}`,
273
+ };
274
+ }
275
+
276
+ function launchOptions(args, raw) {
277
+ const options = {};
278
+ if (args.has("--visibility")) {
279
+ const values = listOf(args.value("--visibility"));
280
+ if (!values.length || values.some((value) => !["resident", "deferred"].includes(value))) {
281
+ throw new Error("--visibility must be resident, deferred, or resident,deferred.");
282
+ }
283
+ options.tool_visibilities = [...new Set(values)];
284
+ }
285
+ if (args.has("--model")) {
286
+ const models = allValues(raw, "--model").map((value) => value.trim());
287
+ if (models.length !== raw.filter((value) => value === "--model").length || models.some((value) => !value || value.startsWith("--"))) {
288
+ throw new Error("--model requires a model name; repeat the flag to compare models.");
289
+ }
290
+ options.models = [...new Set(models)];
291
+ }
292
+ if (args.has("--budget")) {
293
+ const budget = Number(args.value("--budget"));
294
+ if (!Number.isFinite(budget) || budget <= 0 || budget > 1_000_000) {
295
+ throw new Error("--budget must be a finite dollar amount greater than zero and at most 1000000.");
296
+ }
297
+ options.budget_usd = budget;
298
+ }
299
+ return options;
300
+ }
301
+
302
+ async function followSwarm(id, { args, request, write, colour, signal, pollMs, once = false, json = false }) {
303
+ const controller = new AbortController();
304
+ const interrupt = () => controller.abort();
305
+ if (signal?.aborted) controller.abort();
306
+ signal?.addEventListener("abort", interrupt, { once: true });
307
+ if (!signal) process.on("SIGINT", interrupt);
308
+ const read = (method, path, options) => request(method, path, { ...options, signal: controller.signal });
309
+ const resume = `${cliInvocation()} swarm watch ${id}`;
310
+ const progress = new SwarmProgress({ write, colour });
311
+ const started = Date.now();
312
+ let cursor = 0;
313
+ let failures = 0;
314
+ let consoleWarned = false;
315
+ if (!once && !json) write("Following live actions. Ctrl+C stops watching; the swarm keeps running.\n");
316
+ try {
317
+ for (;;) {
318
+ let snapshot;
319
+ try {
320
+ snapshot = await readSwarm(read, id);
321
+ failures = 0;
322
+ } catch (error) {
323
+ if (controller.signal.aborted) throw error;
324
+ failures += 1;
325
+ const retryable = !error.status || error.status === 429 || error.status >= 500;
326
+ if (once || json || !retryable || failures >= 3) throw error;
327
+ write(`Connection interrupted; retrying status (${failures}/3). Swarm ${id} remains on the server.\n`);
328
+ await wait(pollMs, undefined, { signal: controller.signal });
329
+ continue;
330
+ }
331
+ const { run, cells } = snapshot;
332
+ if (json) {
333
+ write(`${JSON.stringify({ run, cells, summary: summarize(cells), links: swarmLinks(args, id) }, (k, v) => (v instanceof Map ? Object.fromEntries(v) : v), 2)}\n`);
334
+ return;
335
+ }
336
+ if (once) {
337
+ write(renderSwarm(run, cells, { colour }));
338
+ write(`\n${swarmLink(args, id)}\n`);
339
+ return;
340
+ }
341
+ write(progressLine(run, cells, started));
342
+ progress.observe(cells);
343
+ // At most four console reads per poll, rotated across larger swarms.
344
+ // Keep finished cells eligible for their final output after status changes.
345
+ const available = cells.filter((cell) => cell.id && cell.status !== "queued" && !progress.state(cell).consoleFinished);
346
+ const selected = available.length ? Array.from({ length: Math.min(4, available.length) }, (_, i) => available[(cursor + i) % available.length]) : [];
347
+ cursor += selected.length;
348
+ const logs = await Promise.allSettled(selected.map((cell) => read("GET", `/matrix/cells/${encodeURIComponent(cell.id)}/console`, { query: { after: progress.state(cell).offset } })));
349
+ for (let i = 0; i < logs.length; i += 1) {
350
+ const log = logs[i];
351
+ if (log.status === "fulfilled") {
352
+ progress.console(selected[i], log.value);
353
+ if (TERMINAL.has(selected[i].status)) progress.state(selected[i]).consoleFinished = true;
354
+ } else if (!controller.signal.aborted && !consoleWarned) {
355
+ write(" Live action output is unavailable; continuing to follow run status.\n");
356
+ consoleWarned = true;
357
+ }
358
+ }
359
+ if (controller.signal.aborted) throw new Error("Interrupted");
360
+ progress.observe(cells, { results: true });
361
+ progress.flush();
362
+ if (once || TERMINAL.has(run.status)) {
363
+ const eligible = cells.filter((cell) => cell.id && cell.status !== "queued");
364
+ const shown = eligible.filter((cell) => progress.state(cell).consoleFinished).length;
365
+ if (shown < eligible.length) write(`Action logs shown for ${shown} of ${eligible.length} attempts; open the swarm page for all logs.\n`);
366
+ write(renderSwarm(run, cells, { colour }));
367
+ if (run.stop_reason) write(`Stopped: ${cleanLabel(run.stop_reason)}\n`);
368
+ write(`\n${swarmLink(args, id)}\n`);
369
+ return;
370
+ }
371
+ await wait(pollMs, undefined, { signal: controller.signal });
372
+ }
373
+ } catch (error) {
374
+ if (controller.signal.aborted) {
375
+ write(`\nStopped watching. The swarm was not cancelled.\nResume: ${resume}\n`);
376
+ return;
377
+ }
378
+ throw new Error(`Could not follow swarm ${id}: ${cleanLabel(error.message, 240)}\nResume: ${resume}`);
379
+ } finally {
380
+ signal?.removeEventListener("abort", interrupt);
381
+ if (!signal) process.off("SIGINT", interrupt);
382
+ }
383
+ }
384
+
258
385
  export async function swarmCommand(commandArgs = [], {
259
386
  sessionArgs, token: sessionToken, workspaceId, signal,
260
- write = (text) => process.stdout.write(text), clearWatch = true,
387
+ write = (text) => process.stdout.write(text), pollMs = POLL_MS,
261
388
  } = {}) {
262
389
  const [sub = "list", ...rest] = commandArgs;
263
390
  // A session's credential stays with its backend even when slash-command
@@ -269,14 +396,28 @@ export async function swarmCommand(commandArgs = [], {
269
396
  const json = args.has("--json");
270
397
  const colour = colourEnabled(args);
271
398
  const token = sessionToken || requireToken(args);
272
- const request = (method, path, options = {}) => call(args, token, method, path, {
273
- ...options, signal, ...(workspaceId ? { headers: { "x-workspace-id": workspaceId } } : {}),
274
- });
399
+ const request = async (method, path, options = {}) => {
400
+ const parent = options.signal || signal;
401
+ const controller = new AbortController();
402
+ const abort = () => controller.abort();
403
+ if (parent?.aborted) abort();
404
+ parent?.addEventListener("abort", abort, { once: true });
405
+ const timeout = setTimeout(abort, 15000);
406
+ try {
407
+ return await call(args, token, method, path, {
408
+ ...options, signal: controller.signal,
409
+ ...(workspaceId ? { headers: { "x-workspace-id": workspaceId } } : {}),
410
+ });
411
+ } finally {
412
+ clearTimeout(timeout);
413
+ parent?.removeEventListener("abort", abort);
414
+ }
415
+ };
275
416
 
276
417
  if (sub === "list") {
277
418
  const result = await request("GET", "/matrix/runs");
278
419
  const runs = result.runs || result.list || [];
279
- if (json) return void write(`${JSON.stringify(runs, null, 2)}\n`);
420
+ if (json) return void write(`${JSON.stringify(runs.map((run) => ({ ...run, links: swarmLinks(args, run.id) })), null, 2)}\n`);
280
421
  if (!runs.length) {
281
422
  write(`No swarms yet. Start one with \`${cliInvocation()} swarm launch\`.\n`);
282
423
  return;
@@ -296,6 +437,8 @@ export async function swarmCommand(commandArgs = [], {
296
437
  if (!serverRef || !asks.length) {
297
438
  throw new Error(`a swarm needs --server and at least one --ask.${SWARM_HELP}`);
298
439
  }
440
+ const options = launchOptions(args, rest);
441
+ if (!json && !args.has("--dry-run")) write(`Resolving ${cleanLabel(serverRef)}…\n`);
299
442
  const server = await findServer(request, serverRef);
300
443
  const expect = listOf(args.value("--expect"));
301
444
  const payload = {
@@ -304,6 +447,7 @@ export async function swarmCommand(commandArgs = [], {
304
447
  distractor_levels: listOf(args.value("--rivals", "0,5")).map(Number).filter((n) => Number.isFinite(n)),
305
448
  target_size: Number(args.value("--size", "60")),
306
449
  release_label: args.value("--name") || `cli ${new Date().toISOString().slice(0, 16).replace("T", " ")}`,
450
+ ...options,
307
451
  };
308
452
  if (!Number.isInteger(payload.target_size) || payload.target_size < 1) {
309
453
  throw new Error("--size must be a whole number of attempts, 1 or more.");
@@ -311,35 +455,25 @@ export async function swarmCommand(commandArgs = [], {
311
455
  const agents = listOf(args.value("--agents"));
312
456
  if (agents.length) payload.harnesses = agents;
313
457
  if (args.has("--dry-run")) payload.dry_run = true;
458
+ if (!json && !payload.dry_run) write("Queueing swarm…\n");
314
459
  const result = await request("POST", "/matrix/runs", { json: payload });
315
460
  const { status_code, ok, ...body } = result;
316
- if (json) return void write(`${JSON.stringify(body, null, 2)}\n`);
461
+ if (json) return void write(`${JSON.stringify(body.id ? { ...body, links: swarmLinks(args, body.id) } : body, null, 2)}\n`);
317
462
  if (payload.dry_run) {
318
463
  write(`Would send ${body.cell_count ?? "?"} attempts (${body.asks_used ?? asks.length} asks, ${body.replicates ?? 1}× each).\n`);
319
464
  return;
320
465
  }
321
- write(`Swarm ${body.id} launched against ${server.name || server.slug}.\n`);
466
+ write(`Swarm ${cleanLabel(body.id)} launched against ${cleanLabel(server.name || server.slug)}.\n`);
322
467
  write(` Watch: ${cliInvocation()} swarm watch ${body.id}\n Open: ${swarmLink(args, body.id)}\n`);
468
+ if (!args.has("--no-watch")) await followSwarm(body.id, { args, request, write, colour, signal, pollMs });
323
469
  return;
324
470
  }
325
471
 
326
472
  if (sub === "watch" || sub === "show") {
327
473
  const [id] = positionals(rest);
328
474
  if (!id) throw new Error(`which swarm? ${cliInvocation()} swarm watch <id>`);
329
- for (;;) {
330
- const { run, cells } = await readSwarm(request, id);
331
- if (json) {
332
- write(`${JSON.stringify({ run, summary: summarize(cells) }, (k, v) => (v instanceof Map ? Object.fromEntries(v) : v), 2)}\n`);
333
- } else {
334
- if (clearWatch && process.stdout.isTTY && sub === "watch") write("\u001b[2J\u001b[H");
335
- write(renderSwarm(run, cells, { colour }));
336
- }
337
- if (sub === "show" || args.has("--once") || TERMINAL.has(run.status)) {
338
- if (!json) write(`\n${swarmLink(args, id)}\n`);
339
- return;
340
- }
341
- await wait(POLL_MS, undefined, { signal });
342
- }
475
+ await followSwarm(id, { args, request, write, colour, signal, pollMs, json, once: sub === "show" || args.has("--once") || json });
476
+ return;
343
477
  }
344
478
 
345
479
  if (sub === "stop") {