foldrun 2.6.0 → 2.7.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/docs/tools.md CHANGED
@@ -594,7 +594,7 @@ web:
594
594
  | `parallel` | extract | full-page markdown; handles JavaScript pages and PDFs | up to 20 | `PARALLEL_API_KEY` |
595
595
  | `zyte` | **unblocker** | the page rendered in a real browser behind a proxy pool; pays per success | 1 | `ZYTE_API_KEY_BASIC` |
596
596
  | `scrapingbee` | **unblocker** | the page rendered in a real browser (`render_js`) behind a proxy pool; credits per call | 1 | `SCRAPINGBEE_API_KEY` |
597
- | `anthropic` | model provider | a small model's reading of the page, on Anthropic's servers | 1 | — |
597
+ | `anthropic` | ours | accepted, and answered by foldrun's own fetch: Claude Code's `WebFetch` it used to hand over to fails its domain check behind the egress proxy | 1, or up to 20 with `urls=` | — |
598
598
 
599
599
  A provider answers `markdown`, `text` and `html`. The modes only a raw read
600
600
  can answer — `meta`, `links`, `raw` — are always read directly, and the tool
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "foldrun",
3
- "version": "2.6.0",
3
+ "version": "2.7.0",
4
4
  "description": "Agents are just folders. Write them, check them, run them — on your machine.",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "type": "module",
@@ -11,7 +11,7 @@
11
11
  "node": ">=22"
12
12
  },
13
13
  "dependencies": {
14
- "@foldrun/core": "^2.8.0"
14
+ "@foldrun/core": "^2.11.0"
15
15
  },
16
16
  "files": [
17
17
  "bin",
@@ -21,7 +21,7 @@
21
21
  ],
22
22
  "scripts": {
23
23
  "e2e": "FOLDRUN_E2E=1 FOLDRUN_DATA=$(mktemp -d /tmp/foldrun-e2e-XXXXXX) node --test --test-timeout=300000 tests/e2e.test.ts",
24
- "test": "FOLDRUN_DATA=$(mktemp -d /tmp/foldrun-test-XXXXXX) node --test tests/cli-account.test.ts tests/model-credential.test.ts tests/cli-approvals.test.ts tests/cli-issues.test.ts tests/cli-capture.test.ts tests/cli-stop.test.ts tests/cli-account-settings.test.ts tests/cli-schedule.test.ts tests/cli-billing.test.ts tests/cli-storage.test.ts tests/cli-scaffold.test.ts tests/cli-secrets.test.ts tests/cli-site-login.test.ts tests/cli-check-library.test.ts tests/cli-check-timezone.test.ts tests/cli-check-frontmatter.test.ts tests/cli-agent-import.test.ts tests/cli-check-subagents.test.ts tests/cli-connect.test.ts tests/cli-accounts.test.ts tests/cli-remote.test.ts tests/cli-stream.test.ts tests/cli-doctor.test.ts tests/cli-tool-test.test.ts tests/cli-agent-run.test.ts tests/cli-check-remote.test.ts tests/cli-shares.test.ts tests/cli-triggers.test.ts tests/cli-providers.test.ts tests/cli-rerun-put.test.ts tests/cli-guide.test.ts tests/cli-runtimes.test.ts tests/cli-deploy-safety.test.ts tests/cli-operator.test.ts tests/cli-flow-patterns.test.ts tests/cli-flow-rm-step.test.ts tests/cli-flow-dup-step.test.ts tests/cli-flow-write-stale.test.ts tests/cli-agent-link-stale.test.ts tests/cli-agent-link-isolated.test.ts tests/cli-parity.test.ts tests/cli-invoke-inputs.test.ts tests/cli-audit-keys.test.ts tests/cli-version.test.ts tests/cli-check-web-warnings.test.ts tests/cli-report-image.test.ts tests/cli-report-cache.test.ts tests/cli-check-workspace-paths.test.ts tests/cli-parity-dashboard.test.ts tests/cli-api-standards.test.ts tests/cli-backups-onboarding.test.ts tests/cli-check-limits.test.ts tests/cli-gallery.test.ts tests/cli-status-platform.test.ts tests/cli-webhooks-notifications.test.ts tests/cli-flags.test.ts tests/cli-api-coverage.test.ts tests/cli-hooks.test.ts tests/cli-release.test.ts tests/cli-terminal.test.ts tests/cli-network.test.ts tests/cli-assistant.test.ts tests/cli-check-scan.test.ts tests/cli-published.test.ts tests/cli-init-target.test.ts",
24
+ "test": "FOLDRUN_DATA=$(mktemp -d /tmp/foldrun-test-XXXXXX) node --test tests/cli-account.test.ts tests/model-credential.test.ts tests/cli-approvals.test.ts tests/cli-issues.test.ts tests/cli-capture.test.ts tests/cli-stop.test.ts tests/cli-account-settings.test.ts tests/cli-schedule.test.ts tests/cli-billing.test.ts tests/cli-storage.test.ts tests/cli-scaffold.test.ts tests/cli-secrets.test.ts tests/cli-site-login.test.ts tests/cli-check-library.test.ts tests/cli-check-timezone.test.ts tests/cli-check-frontmatter.test.ts tests/cli-agent-import.test.ts tests/cli-check-subagents.test.ts tests/cli-connect.test.ts tests/cli-accounts.test.ts tests/cli-remote.test.ts tests/cli-stream.test.ts tests/cli-doctor.test.ts tests/cli-tool-test.test.ts tests/cli-agent-run.test.ts tests/cli-check-remote.test.ts tests/cli-shares.test.ts tests/cli-triggers.test.ts tests/cli-providers.test.ts tests/cli-rerun-put.test.ts tests/cli-guide.test.ts tests/cli-runtimes.test.ts tests/cli-deploy-safety.test.ts tests/cli-operator.test.ts tests/cli-flow-patterns.test.ts tests/cli-flow-rm-step.test.ts tests/cli-flow-dup-step.test.ts tests/cli-flow-write-stale.test.ts tests/cli-agent-link-stale.test.ts tests/cli-agent-link-isolated.test.ts tests/cli-parity.test.ts tests/cli-invoke-inputs.test.ts tests/cli-audit-keys.test.ts tests/cli-version.test.ts tests/cli-check-web-warnings.test.ts tests/cli-report-image.test.ts tests/cli-report-cache.test.ts tests/cli-check-workspace-paths.test.ts tests/cli-parity-dashboard.test.ts tests/cli-api-standards.test.ts tests/cli-backups-onboarding.test.ts tests/cli-check-limits.test.ts tests/cli-gallery.test.ts tests/cli-status-platform.test.ts tests/cli-webhooks-notifications.test.ts tests/cli-flags.test.ts tests/cli-api-coverage.test.ts tests/cli-hooks.test.ts tests/cli-release.test.ts tests/cli-terminal.test.ts tests/cli-network.test.ts tests/cli-assistant.test.ts tests/cli-check-scan.test.ts tests/cli-published.test.ts tests/cli-init-target.test.ts tests/cli-flow-filter.test.ts tests/cli-eval-grades.test.ts tests/cli-eval-compare.test.ts tests/cli-proposals.test.ts tests/cli-inbox.test.ts",
25
25
  "release": "node scripts/release.mjs",
26
26
  "typecheck": "tsc -p tsconfig.json",
27
27
  "docs:sync": "node scripts/sync-docs.mjs",
@@ -47,5 +47,8 @@
47
47
  "@types/node": "^26.6.4",
48
48
  "gray-matter": "^4.0.3",
49
49
  "typescript": "^6.0.3"
50
+ },
51
+ "overrides": {
52
+ "zod": "4.6.7"
50
53
  }
51
54
  }
package/src/commands.mjs CHANGED
@@ -1889,13 +1889,23 @@ async function probeCmd(modelArg) {
1889
1889
  async function runEvals(name, flags = {}) {
1890
1890
  assertCredentials();
1891
1891
  await useGallery(flags);
1892
- const { listEvals, runEval } = await core();
1892
+ const { listEvals, runEval, workspaceDir } = await core();
1893
1893
  const T = "default";
1894
1894
  const P = "workspace";
1895
+ // Where this workspace's evals are, for telling a grader by its file when
1896
+ // the installed core predates graders — the workspace chosen, not the cwd.
1897
+ const evalsDir = path.join(workspaceDir(T, P), "evals");
1895
1898
  // An `inputs: true` file is a flow's saved inputs, not a test: it has
1896
1899
  // nothing to assert, so it is never run as one (see evals.md).
1897
- const all = listEvals(T, P).filter((e) => (!name || e.name === name) && !e.inputs);
1900
+ const named = listEvals(T, P).filter((e) => (!name || e.name === name) && !e.inputs);
1901
+ // A grader (`trigger: runs`) grades real runs; it has no cases to run here.
1902
+ const all = named.filter((e) => !isGrader(e, evalsDir));
1903
+ for (const g of named.filter((e) => isGrader(e, evalsDir))) {
1904
+ console.log(`\n ${c.bold(g.name)} ${c.dim(`grades live runs — \`foldrun eval grades ${g.name} --to <workspace>\` shows what it found`)}`);
1905
+ }
1898
1906
  if (all.length === 0) {
1907
+ // Only graders here (or the one named is a grader): listed above, nothing to run — not an error.
1908
+ if (named.length) return 0;
1899
1909
  if (name && listEvals(T, P).some((e) => e.name === name)) {
1900
1910
  throw new Error(`"${name}" is a file of saved inputs, not an eval — \`foldrun invoke <flow> --inputs <set>\` runs one`);
1901
1911
  }
@@ -6324,6 +6334,57 @@ function issueDoctor(issue) {
6324
6334
  * on one — the dashboard's Issues page and its buttons, same route.
6325
6335
  * `foldrun issues doctor <id>` is the card's Doctor: the run doctor on the
6326
6336
  * issue's newest run, in that run's workspace. */
6337
+ /**
6338
+ * `foldrun inbox` — what is waiting on you across your workspaces (GET
6339
+ * /api/me/inbox): gates, questions agents ask, open issues, proposed fixes,
6340
+ * your runs that went wrong this week. Assigned to you and started by you by
6341
+ * default; `--team` for the rest of what waits in your workspaces, `--all`
6342
+ * for every bucket, `--dismissed` for what you set aside, `--kind
6343
+ * approval|question|issue|proposal|run`, `--unread`, `--json`. `read`,
6344
+ * `unread`, `dismiss` (set aside — it still waits where it lives) and
6345
+ * `restore` take item ids. A key reads the inbox of the person who minted it.
6346
+ * The dashboard's Inbox. Exit 1 when anything assigned to you is unread.
6347
+ */
6348
+ async function inboxCmd(positional, flags) {
6349
+ const url = platformFor(flags, "inbox");
6350
+ const [verb, ...ids] = positional;
6351
+ const ACTIONS = { read: "read", unread: "unread", dismiss: "dismiss", restore: "restore" };
6352
+ if (verb) {
6353
+ const action = ACTIONS[verb];
6354
+ if (!action) throw new Error(`\`foldrun inbox\` lists what waits on you; its verbs are read, unread, dismiss and restore — not ${verb}`);
6355
+ if (!ids.length) throw new Error(`\`foldrun inbox ${verb} <id>…\` — the ids are the first column of \`foldrun inbox\``);
6356
+ await remoteCall(url, flags, "/api/me/inbox", { method: "POST", body: JSON.stringify({ ids, action }) });
6357
+ console.log(`\n ${c.green(action === "dismiss" ? "set aside" : action === "restore" ? "put back" : `marked ${action}`)} ${ids.length} item${ids.length === 1 ? "" : "s"}\n`);
6358
+ return 0;
6359
+ }
6360
+ const q = new URLSearchParams();
6361
+ if (flags.all === true) q.set("bucket", "all");
6362
+ else if (flags.team === true) q.set("bucket", "team");
6363
+ else if (flags.started === true) q.set("bucket", "started");
6364
+ else if (flags.mine === true) q.set("bucket", "mine");
6365
+ if (flags.dismissed === true) q.set("dismissed", "1");
6366
+ if (typeof flags.kind === "string") q.set("kind", flags.kind);
6367
+ if (typeof flags.workspace === "string") q.set("workspace", flags.workspace);
6368
+ const view = await remoteCall(url, flags, `/api/me/inbox${q.size ? `?${q}` : ""}`);
6369
+ const items = (view.items ?? []).filter((i) => (flags.unread === true ? !i.read : true));
6370
+ if (flags.json) {
6371
+ console.log(JSON.stringify({ ...view, items }, null, 2));
6372
+ return view.counts?.unread ? 1 : 0;
6373
+ }
6374
+ const k = view.counts ?? {};
6375
+ console.log(`\n ${c.bold(`inbox of ${view.person}`)} ${c.dim(`· for you ${k.mine ?? 0} · started by you ${k.started ?? 0} · team ${k.team ?? 0} · ${k.unread ?? 0} unread`)}`);
6376
+ if (!items.length) console.log(`\n ${c.dim(flags.dismissed ? "nothing set aside" : "nothing waiting")}`);
6377
+ const BUCKET = { mine: "for you", started: "yours", team: "team" };
6378
+ for (const i of items) {
6379
+ const dot = i.read ? " " : c.amber("•");
6380
+ console.log(`\n ${dot} ${c.bold(i.title)} ${c.dim(`· ${i.kind} · ${BUCKET[i.bucket] ?? i.bucket} · ${i.workspace} · ${String(i.at).slice(0, 16).replace("T", " ")}`)}`);
6381
+ if (i.detail) console.log(` ${i.detail}`);
6382
+ console.log(` ${c.dim(`${i.id} — ${i.why}`)}`);
6383
+ }
6384
+ console.log("");
6385
+ return k.unread ? 1 : 0;
6386
+ }
6387
+
6327
6388
  async function issuesCmd(positional, flags, layout) {
6328
6389
  const url = platformFor(flags, "issues");
6329
6390
  const [verb, id] = positional;
@@ -8189,14 +8250,22 @@ function ownSandboxOf(agent) {
8189
8250
  return /** @type {{ sandbox?: string } | undefined} */ (agent)?.sandbox === "own";
8190
8251
  }
8191
8252
 
8192
- /** An agent file's frontmatter as a run reads it (core's configMatter, which
8193
- * folds the job blocks), from its text. {} when it does not parse. */
8253
+ /** An agent file's frontmatter as a run reads it, from its text. A run folds
8254
+ * the four job blocks' keys up to the top (core's configMatter, kept private
8255
+ * — not in @foldrun/core's publicApi), so `guardrail: { sandbox: own }` reads
8256
+ * as `sandbox: own`. The CLI reads only two foldable keys off this — `sandbox`
8257
+ * and `network`, both in an agent's `guardrail:` block — so it lifts just
8258
+ * those, the same way a run does: a top-level value wins, else the block's is
8259
+ * lifted. {} when it does not parse. */
8194
8260
  async function effectiveFront(raw) {
8195
8261
  const { readFrontmatter } = await core();
8196
8262
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), "foldrun-front-"));
8197
8263
  try {
8198
8264
  fs.writeFileSync(path.join(dir, "agent.md"), raw);
8199
- return readFrontmatter(path.join(dir, "agent.md")) ?? {};
8265
+ const front = /** @type {Record<string, any>} */ (readFrontmatter(path.join(dir, "agent.md")) ?? {});
8266
+ const guardrail = front.guardrail && typeof front.guardrail === "object" ? front.guardrail : {};
8267
+ for (const key of ["sandbox", "network"]) if (!(key in front) && key in guardrail) front[key] = guardrail[key];
8268
+ return front;
8200
8269
  } catch {
8201
8270
  return {};
8202
8271
  } finally {
@@ -8443,10 +8512,12 @@ async function agentNetworkCmd(positional, flags, layout) {
8443
8512
  }
8444
8513
  const resolve = (files) =>
8445
8514
  inTree(desk, files, async (dir) => {
8446
- const { listAgents, readFrontmatter } = await core();
8515
+ const { listAgents } = await core();
8516
+ // The folded view a run reads: `network` set inside a `guardrail:` block
8517
+ // counts the same as a top-level one (effectiveFront lifts it).
8447
8518
  return {
8448
8519
  info: listAgents("default", "workspace").find((a) => a.name === name),
8449
- own: net.readNetwork(readFrontmatter(path.join(dir, rel)).network).setting,
8520
+ own: net.readNetwork((await effectiveFront(fs.readFileSync(path.join(dir, rel), "utf8"))).network).setting,
8450
8521
  };
8451
8522
  });
8452
8523
  const show = (info) => {
@@ -8717,10 +8788,16 @@ async function remoteEvalsCmd(name, flags, layout) {
8717
8788
  if (!name) throw new Error("--run reads one eval's run — name it: `foldrun eval <name> --to <ws> --run <id>`");
8718
8789
  }
8719
8790
  const { evals = [] } = await remoteCall(url, flags, `/api/workspaces/${enc(ws)}/evals`);
8720
- const all = evals.filter((e) => !name || e.name === name);
8721
- if (!all.length) {
8791
+ const named = evals.filter((e) => !name || e.name === name);
8792
+ if (!named.length) {
8722
8793
  throw new Error(name ? `no eval "${name}" in ${ws} — it has ${evals.map((e) => e.name).join(", ") || "none"}` : `no evals in ${ws} on ${url}`);
8723
8794
  }
8795
+ // Graders grade real runs; `eval grades` shows what they found.
8796
+ const all = named.filter((e) => e.trigger !== "runs");
8797
+ for (const g of named.filter((e) => e.trigger === "runs")) {
8798
+ console.log(`\n ${c.bold(g.name)} ${c.dim(`grades live runs — \`foldrun eval grades ${g.name} --to ${ws}\``)}`);
8799
+ }
8800
+ if (!all.length) return 0;
8724
8801
  let failed = 0;
8725
8802
  for (const info of all) {
8726
8803
  console.log(`\n ${c.bold(info.name)} ${c.dim(`${info.cases?.length ?? "?"} cases · ${ws}`)}`);
@@ -8761,6 +8838,341 @@ async function remoteEvalsCmd(name, flags, layout) {
8761
8838
  return failed ? 1 : 0;
8762
8839
  }
8763
8840
 
8841
+ /** Is this eval a grader (`trigger: runs`)? A core that predates graders
8842
+ * reads the trigger as `deploy`, so the file is asked too. */
8843
+ function isGrader(info, dir) {
8844
+ if (info.trigger === "runs") return true;
8845
+ try {
8846
+ const raw = fs.readFileSync(path.join(dir, info.file), "utf8");
8847
+ const front = /^---\r?\n([\s\S]*?)\r?\n---/.exec(raw)?.[1] ?? "";
8848
+ return /^trigger:\s*["']?runs["']?\s*$/m.test(front);
8849
+ } catch {
8850
+ return false;
8851
+ }
8852
+ }
8853
+
8854
+ /** One grade, the way `eval grades` and `eval grade` print it. */
8855
+ function printGrade(g) {
8856
+ const mark = g.status === "pass" ? c.green("✓ pass") : g.status === "fail" ? c.red("✗ fail") : c.amber("? not decided");
8857
+ const miss = (g.checks ?? []).find((x) => !x.passed);
8858
+ const why = miss ? ` — ${miss.name}: ${miss.error ?? (miss.assertions ?? []).find((a) => !a.passed)?.detail ?? "failed"}` : "";
8859
+ console.log(` ${mark} ${c.bold(g.runId)} ${c.dim(String(g.at ?? "").slice(0, 16).replace("T", " "))}${why}`);
8860
+ }
8861
+
8862
+ /**
8863
+ * `foldrun eval grades <eval> --to <workspace>` — what a grader (an eval with
8864
+ * `trigger: runs`) found in the real runs it graded: the pass rate over
8865
+ * `--days` (default 30), then each graded run, newest first (`--status
8866
+ * fail` for the failures alone, `--limit`). GET …/evals/<e>/grades — the
8867
+ * Evals page's grader card. Exit 1 when any graded run in the window failed.
8868
+ */
8869
+ async function evalGradesCmd(name, flags, layout) {
8870
+ if (!name) throw new Error("which grader? `foldrun eval grades <eval> --to <workspace>`");
8871
+ const url = platformFor(flags, "eval grades");
8872
+ const ws = platformWorkspace(flags, layout, `eval grades ${name}`);
8873
+ const q = new URLSearchParams();
8874
+ if (flags.days !== undefined) q.set("days", String(flags.days));
8875
+ if (typeof flags.status === "string") q.set("status", flags.status);
8876
+ if (flags.limit !== undefined) q.set("limit", String(flags.limit));
8877
+ const r = await remoteCall(url, flags, `/api/workspaces/${enc(ws)}/evals/${enc(name)}/grades${q.size ? `?${q}` : ""}`);
8878
+ if (flags.json) {
8879
+ console.log(JSON.stringify(r, null, 2));
8880
+ return r.summary?.failed ? 1 : 0;
8881
+ }
8882
+ const s = r.summary ?? {};
8883
+ const rate = s.rate === null || s.rate === undefined ? "—" : `${Math.round(s.rate * 100)}%`;
8884
+ console.log(`\n ${c.bold(r.eval)} ${c.dim(`grades ${r.sample}% of ${r.flow ? `flow ${r.flow}` : `agent ${r.agent}`} runs · ${ws}`)}`);
8885
+ console.log(` ${rate} passing over ${r.days} days — ${s.graded ?? 0} graded, ${s.passed ?? 0} passed, ${s.failed ?? 0} failed${s.errors ? `, ${s.errors} not decided` : ""} · judges $${Number(s.costUsd ?? 0).toFixed(4)}\n`);
8886
+ for (const g of r.grades ?? []) printGrade(g);
8887
+ if (!(r.grades ?? []).length) console.log(` ${c.dim("no graded runs in the window")}`);
8888
+ console.log("");
8889
+ return s.failed ? 1 : 0;
8890
+ }
8891
+
8892
+ /**
8893
+ * `foldrun eval grade <eval> --run <id> --to <workspace>` — grade one finished
8894
+ * run now, sampled or not (POST …/evals/<e>/grade { run }). The grade is kept
8895
+ * like any other; a run graded again shows its newer grade. Exit 1 unless it
8896
+ * passed.
8897
+ */
8898
+ async function evalGradeCmd(name, flags, layout) {
8899
+ if (!name || typeof flags.run !== "string" || !flags.run.trim()) {
8900
+ throw new Error("`foldrun eval grade <eval> --run <run-id> --to <workspace>`");
8901
+ }
8902
+ const url = platformFor(flags, "eval grade");
8903
+ const ws = platformWorkspace(flags, layout, `eval grade ${name}`);
8904
+ const g = await remoteCall(url, flags, `/api/workspaces/${enc(ws)}/evals/${enc(name)}/grade`, { method: "POST", body: JSON.stringify({ run: flags.run.trim() }) });
8905
+ if (flags.json) console.log(JSON.stringify(g, null, 2));
8906
+ else {
8907
+ console.log("");
8908
+ printGrade(g);
8909
+ for (const ch of g.checks ?? []) {
8910
+ console.log(` ${ch.passed ? c.green("✓") : ch.error ? c.amber("?") : c.red("✗")} ${ch.name}${ch.error ? c.dim(` — ${ch.error}`) : ""}`);
8911
+ for (const a of ch.assertions ?? []) if (!a.passed) console.log(` ${c.dim(`${a.type}: ${a.value}`)} — ${a.detail}`);
8912
+ }
8913
+ console.log(` ${c.dim(`judges $${Number(g.costUsd ?? 0).toFixed(4)}`)}\n`);
8914
+ }
8915
+ return g.status === "pass" ? 0 : 1;
8916
+ }
8917
+
8918
+ /** One comparison, as a table on a terminal. */
8919
+ function printComparison(r) {
8920
+ const pct = (x) => (x === null || x === undefined ? "—" : `${Math.round(x * 100)}%`);
8921
+ const w = Math.max(...(r.configs ?? []).map((x) => String(x.label).length), 7);
8922
+ console.log(`\n ${c.bold(r.eval)} ${c.dim(`judge ${r.judge?.model ?? "?"} · spent $${Number(r.spentUsd ?? 0).toFixed(2)} of $${Number(r.maxUsd ?? 0).toFixed(2)}${r.repeat > 1 ? ` · ${r.repeat}× each` : ""}`)}`);
8923
+ if (r.stopped) console.log(` ${c.amber(`stopped early: ${r.stopped}`)}`);
8924
+ console.log(`\n ${c.dim(`${"setting".padEnd(w)} passing cost per run time`)}`);
8925
+ for (const x of r.configs ?? []) {
8926
+ const decided = x.trials - (x.notRun ?? 0);
8927
+ console.log(` ${String(x.label).padEnd(w)} ${pct(x.rate).padStart(4)} ${`${x.passed}/${decided}`.padEnd(7)} ${`$${Number(x.costUsd).toFixed(4)}`.padStart(9)} ${x.avgCostUsd === null ? " —" : `$${Number(x.avgCostUsd).toFixed(4)}`.padStart(8)} ${x.avgSecs === null ? "—" : `${x.avgSecs}s`}${x.notRun ? c.dim(` (${x.notRun} not run)`) : ""}`);
8928
+ }
8929
+ const differ = (r.cases ?? []).filter((row) => new Set(row.passed.map((p, i) => p / (row.trials[i] || 1))).size > 1);
8930
+ if (differ.length) {
8931
+ console.log(`\n ${c.dim("where they differ:")}`);
8932
+ for (const row of differ) console.log(` ${row.case}: ${row.passed.map((p, i) => `${r.configs[i].label} ${p}/${row.trials[i]}`).join(" · ")}`);
8933
+ }
8934
+ console.log("");
8935
+ }
8936
+
8937
+ /**
8938
+ * `foldrun eval compare <eval> --models fast,default,max --to <workspace>` —
8939
+ * an eval's cases under two to four models, side by side (POST
8940
+ * …/evals/<e>/compare, then polled). `--models` takes model[:effort] items
8941
+ * (`fast:low,max:high`); `--effort` is the effort for the items that name
8942
+ * none; `--agent <name>` compares only that agent's steps of a flow eval;
8943
+ * `--repeat` (1–5) runs each case more than once; `--max-usd` is the spend
8944
+ * limit (default 5). Every run is a test run. Waits for the result
8945
+ * (`--timeout <s>`, default 3600; past it, the id to read it with).
8946
+ * `--run <id>` reads one comparison; `--list` the kept ones. The Evals
8947
+ * page's Compare models.
8948
+ */
8949
+ async function evalCompareCmd(name, flags, layout) {
8950
+ if (!name) throw new Error("which eval? `foldrun eval compare <eval> --models fast,default --to <workspace>`");
8951
+ const url = platformFor(flags, "eval compare");
8952
+ const ws = platformWorkspace(flags, layout, `eval compare ${name}`);
8953
+ const base = `/api/workspaces/${enc(ws)}/evals/${enc(name)}/compare`;
8954
+ if (flags.list === true) {
8955
+ const r = await remoteCall(url, flags, base);
8956
+ if (flags.json) return console.log(JSON.stringify(r, null, 2)), 0;
8957
+ if (r.running) console.log(`\n ${c.dim(`running: ${r.runId}, since ${r.startedAt}${r.progress ? ` — ${r.progress.done}/${r.progress.total} runs` : ""}`)}`);
8958
+ for (const p of r.comparisons ?? []) {
8959
+ const line = p.status === "done" && p.result ? p.result.configs.map((x) => `${x.label} ${x.rate === null ? "—" : `${Math.round(x.rate * 100)}%`}`).join(" · ") : c.red(p.error ?? "failed");
8960
+ console.log(` ${c.bold(p.runId)} ${c.dim(String(p.startedAt).slice(0, 16).replace("T", " "))} ${line}`);
8961
+ }
8962
+ if (!(r.comparisons ?? []).length && !r.running) console.log(`\n ${c.dim(`no comparisons of ${name} yet`)}`);
8963
+ console.log("");
8964
+ return 0;
8965
+ }
8966
+ const finish = (view) => {
8967
+ if (view.state === "done" && view.result) {
8968
+ if (flags.json) console.log(JSON.stringify(view.result, null, 2));
8969
+ else printComparison(view.result);
8970
+ return 0;
8971
+ }
8972
+ throw new Error(view.error ?? `the comparison ended as ${view.state ?? "unknown"}, with no result`);
8973
+ };
8974
+ // --run reads one comparison and starts nothing: a bare --run (an empty
8975
+ // variable in a script) is refused, never read as "start a new one".
8976
+ if (flags.run !== undefined && (typeof flags.run !== "string" || !flags.run.trim())) {
8977
+ throw new Error("--run takes the comparison id it printed — `foldrun eval compare <eval> --to <ws> --run <id>`");
8978
+ }
8979
+ if (typeof flags.run === "string") {
8980
+ const view = await remoteCall(url, flags, `${base}?run=${encodeURIComponent(flags.run)}`);
8981
+ if (view.state === "running") {
8982
+ if (flags.json) console.log(JSON.stringify(view, null, 2));
8983
+ else console.log(`\n ${c.dim(`still running, since ${view.startedAt}${view.progress ? ` — ${view.progress.done}/${view.progress.total} runs` : ""}`)}\n`);
8984
+ return 1;
8985
+ }
8986
+ return finish(view);
8987
+ }
8988
+ if (typeof flags.models !== "string" || !flags.models.trim()) {
8989
+ throw new Error("--models takes two to four settings, e.g. --models fast,default or --models fast:low,max:high");
8990
+ }
8991
+ const effort = typeof flags.effort === "string" ? flags.effort : undefined;
8992
+ const agent = typeof flags.agent === "string" ? flags.agent : undefined;
8993
+ const configs = flags.models.split(",").map((item) => item.trim()).filter(Boolean).map((item) => {
8994
+ // Only a trailing known effort is an effort: llama3.2:3b stays one model id.
8995
+ const m = /^(.*):(low|medium|high|xhigh|max)$/.exec(item);
8996
+ const model = m ? m[1] : item;
8997
+ const e = m ? m[2] : undefined;
8998
+ return { model, ...((e ?? effort) ? { effort: e ?? effort } : {}), ...(agent ? { agent } : {}) };
8999
+ });
9000
+ const body = { configs, ...(flags.repeat !== undefined ? { repeat: Number(flags.repeat) } : {}), ...(flags["max-usd"] !== undefined ? { maxUsd: Number(flags["max-usd"]) } : {}) };
9001
+ const started = await remoteCall(url, flags, base, { method: "POST", body: JSON.stringify(body) });
9002
+ // --json is one JSON document on stdout; what happens on the way goes to stderr.
9003
+ const say = flags.json ? (s) => console.error(s) : (s) => console.log(s);
9004
+ say(`\n ${c.dim(`comparison ${started.runId}: ${started.trials} test runs, at most $${Number(started.maxUsd).toFixed(2)}`)}`);
9005
+ const wait = explicitTimeout(flags) ?? 3600;
9006
+ const deadline = Date.now() + wait * 1000;
9007
+ let said = -1;
9008
+ for (;;) {
9009
+ if (Date.now() > deadline) {
9010
+ throw new Error(`still running after ${wait}s — \`foldrun eval compare ${name} --to ${ws} --run ${started.runId}\` reads it when it is done`);
9011
+ }
9012
+ await new Promise((r) => setTimeout(r, Number(process.env.FOLDRUN_EVAL_POLL_MS) || 5000));
9013
+ const view = await remoteCall(url, flags, `${base}?run=${encodeURIComponent(started.runId)}`);
9014
+ if (view.state === "running") {
9015
+ if (view.progress && view.progress.done !== said) {
9016
+ said = view.progress.done;
9017
+ say(` ${c.dim(`${view.progress.done}/${view.progress.total} runs done`)}`);
9018
+ }
9019
+ continue;
9020
+ }
9021
+ return finish(view);
9022
+ }
9023
+ }
9024
+
9025
+ // ------------------------------------------------------------- proposals
9026
+
9027
+ /**
9028
+ * A file's change, as ordered -/+ lines with a little context — the same
9029
+ * reading the dashboard's diff gives. An LCS over the lines, so a reordered
9030
+ * step or a removed duplicate shows; a file too long for that (over 2,000
9031
+ * lines a side) is shown whole as removed and added rather than guessed at.
9032
+ */
9033
+ function lineChanges(before, after) {
9034
+ const a = before === null || before === undefined ? [] : String(before).split("\n");
9035
+ const b = String(after ?? "").split("\n");
9036
+ if (a.length > 2000 || b.length > 2000) return [...a.map((l) => ["-", l]), ...b.map((l) => ["+", l])];
9037
+ const n = a.length;
9038
+ const m = b.length;
9039
+ const lcs = Array.from({ length: n + 1 }, () => new Uint16Array(m + 1));
9040
+ for (let i = n - 1; i >= 0; i--) for (let j = m - 1; j >= 0; j--) lcs[i][j] = a[i] === b[j] ? lcs[i + 1][j + 1] + 1 : Math.max(lcs[i + 1][j], lcs[i][j + 1]);
9041
+ const out = [];
9042
+ let i = 0;
9043
+ let j = 0;
9044
+ while (i < n || j < m) {
9045
+ if (i < n && j < m && a[i] === b[j]) {
9046
+ out.push([" ", a[i]]);
9047
+ i++;
9048
+ j++;
9049
+ } else if (j < m && (i === n || lcs[i][j + 1] >= lcs[i + 1][j])) out.push(["+", b[j++]]);
9050
+ else out.push(["-", a[i++]]);
9051
+ }
9052
+ return out;
9053
+ }
9054
+
9055
+ function printFileChange(f) {
9056
+ console.log(`\n ${c.bold(f.path)}${f.before === null ? c.dim(" (new)") : ""}`);
9057
+ const ops = lineChanges(f.before, f.content);
9058
+ const near = (k) => ops.slice(Math.max(0, k - 2), k + 3).some(([op]) => op !== " ");
9059
+ let skipped = false;
9060
+ ops.forEach(([op, line], k) => {
9061
+ if (op === " " && !near(k)) {
9062
+ if (!skipped) console.log(` ${c.dim(" …")}`);
9063
+ skipped = true;
9064
+ return;
9065
+ }
9066
+ skipped = false;
9067
+ console.log(` ${op === "+" ? c.green(`+ ${line}`) : op === "-" ? c.red(`- ${line}`) : c.dim(` ${line}`)}`);
9068
+ });
9069
+ }
9070
+
9071
+ const PROPOSAL_LABEL = { drafting: "being drafted", ready: "ready to review", applying: "applying", applied: "applied", partial: "partly applied", failed: "draft failed", dismissed: "dismissed" };
9072
+
9073
+ /**
9074
+ * `foldrun proposals --to <workspace>` — the proposed fixes in a workspace
9075
+ * (GET …/proposals), newest first: status, the eval, how many failures each
9076
+ * covers. `--status ready` for the ones waiting on a person, `--eval <name>`,
9077
+ * `--json`. `show <id>` prints one (the assistant's reasoning, the failures,
9078
+ * the change per file); `apply <id>` writes its files after asking (`--yes`
9079
+ * without a terminal); `dismiss <id>` sets it aside. The Evals page's
9080
+ * Proposed fixes.
9081
+ */
9082
+ async function proposalsCmd(positional, flags, layout) {
9083
+ const url = platformFor(flags, "proposals");
9084
+ const ws = platformWorkspace(flags, layout, "proposals");
9085
+ const verb = positional[0];
9086
+ const id = positional[1];
9087
+ if (verb === "show" || verb === "apply" || verb === "dismiss") {
9088
+ if (!id) throw new Error(`which proposal? \`foldrun proposals ${verb} <id> --to ${ws}\``);
9089
+ const p = await remoteCall(url, flags, `/api/workspaces/${enc(ws)}/proposals/${enc(id)}`);
9090
+ if (verb === "show") {
9091
+ if (flags.json) return console.log(JSON.stringify(p, null, 2)), 0;
9092
+ console.log(`\n ${c.bold(p.id)} ${PROPOSAL_LABEL[p.status] ?? p.status} · ${p.eval} · ${c.dim(`${String(p.createdAt).slice(0, 16).replace("T", " ")} · ${p.by === "auto" ? "opened after repeated failures" : `asked by ${p.by}`}`)}`);
9093
+ if (p.error) console.log(` ${c.red(p.error)}`);
9094
+ for (const n of p.notes ?? []) console.log(` ${c.amber(n)}`);
9095
+ console.log(`\n ${c.dim("covers:")}`);
9096
+ for (const f of p.failures ?? []) console.log(` · ${f.runId ?? f.case} — ${f.check}: ${f.detail}`);
9097
+ if (p.reply) console.log(`\n${p.reply.split("\n").map((l) => ` ${l}`).join("\n")}`);
9098
+ for (const f of p.files ?? []) printFileChange(f);
9099
+ for (const i of p.issues ?? []) console.log(` ${i.level === "error" ? c.red("✗") : c.amber("!")} ${i.where}: ${i.message}`);
9100
+ console.log("");
9101
+ return 0;
9102
+ }
9103
+ if (verb === "apply") {
9104
+ if (p.status !== "ready") throw new Error(`${id} is ${p.status} — only a ready proposal can be applied`);
9105
+ // With --json the change is still shown before the yes — on stderr, so stdout stays one document.
9106
+ if (flags.json) {
9107
+ const log = console.log;
9108
+ console.log = (...a) => console.error(...a);
9109
+ try {
9110
+ for (const f of p.files ?? []) printFileChange(f);
9111
+ } finally {
9112
+ console.log = log;
9113
+ }
9114
+ } else for (const f of p.files ?? []) printFileChange(f);
9115
+ const files = (p.files ?? []).map((f) => f.path).join(", ");
9116
+ if (!(await confirmed(flags, "proposals apply", `\n Write ${files} in ${ws}? [y/N] `))) {
9117
+ console.log(`\n ${c.dim("nothing written")}\n`);
9118
+ return 1;
9119
+ }
9120
+ }
9121
+ const r = await remoteCall(url, flags, `/api/workspaces/${enc(ws)}/proposals/${enc(id)}`, { method: "POST", body: JSON.stringify({ action: verb }) });
9122
+ if (flags.json) console.log(JSON.stringify(r, null, 2));
9123
+ else console.log(`\n ${c.green(PROPOSAL_LABEL[r.status] ?? r.status)} ${r.id}${r.applied?.length ? c.dim(` — wrote ${r.applied.join(", ")}`) : ""}\n`);
9124
+ return 0;
9125
+ }
9126
+ if (verb && verb !== "ls" && verb !== "list") throw new Error(`\`foldrun proposals\` lists them; show, apply and dismiss take an id — not "${verb}"`);
9127
+ const q = new URLSearchParams();
9128
+ if (typeof flags.status === "string") q.set("status", flags.status);
9129
+ if (typeof flags.eval === "string") q.set("eval", flags.eval);
9130
+ const { proposals = [] } = await remoteCall(url, flags, `/api/workspaces/${enc(ws)}/proposals${q.size ? `?${q}` : ""}`);
9131
+ if (flags.json) return console.log(JSON.stringify(proposals, null, 2)), 0;
9132
+ if (!proposals.length) {
9133
+ console.log(`\n ${c.dim(`no proposed fixes in ${ws}`)}\n`);
9134
+ return 0;
9135
+ }
9136
+ console.log("");
9137
+ for (const p of proposals) {
9138
+ const tone = p.status === "ready" ? c.amber : p.status === "applied" ? c.green : p.status === "failed" || p.status === "partial" ? c.red : c.dim;
9139
+ console.log(` ${c.bold(p.id)} ${tone((PROPOSAL_LABEL[p.status] ?? p.status).padEnd(15))} ${p.eval} ${c.dim(`· ${p.failures?.length ?? 0} failure(s) · ${(p.files ?? []).map((f) => f.path).join(", ") || "no files"} · ${String(p.createdAt).slice(0, 10)}`)}`);
9140
+ }
9141
+ console.log("");
9142
+ return 0;
9143
+ }
9144
+
9145
+ /**
9146
+ * `foldrun eval improve <eval> --to <workspace>` — ask for a proposed fix now
9147
+ * (POST …/evals/<e>/improve): from a grader's failed grades no proposal has
9148
+ * covered, or an eval's last failed cases. Waits for the draft (`--timeout`,
9149
+ * default 300) and prints it; `--no-wait` returns at once with the id.
9150
+ */
9151
+ async function evalImproveCmd(name, flags, layout) {
9152
+ if (!name) throw new Error("which eval? `foldrun eval improve <eval> --to <workspace>`");
9153
+ const url = platformFor(flags, "eval improve");
9154
+ const ws = platformWorkspace(flags, layout, `eval improve ${name}`);
9155
+ const p = await remoteCall(url, flags, `/api/workspaces/${enc(ws)}/evals/${enc(name)}/improve`, { method: "POST", body: "{}" });
9156
+ const say = flags.json ? (s) => console.error(s) : (s) => console.log(s);
9157
+ say(`\n ${c.dim(`proposal ${p.id} — the assistant is drafting it from ${p.failures?.length ?? 0} failure(s)`)}`);
9158
+ if (flags.wait === false || flags["no-wait"] === true) {
9159
+ if (flags.json) console.log(JSON.stringify(p, null, 2));
9160
+ else say(` ${c.dim(`\`foldrun proposals show ${p.id} --to ${ws}\` when it is ready`)}\n`);
9161
+ return 0;
9162
+ }
9163
+ const deadline = Date.now() + (explicitTimeout(flags) ?? 300) * 1000;
9164
+ for (;;) {
9165
+ await new Promise((r) => setTimeout(r, Number(process.env.FOLDRUN_EVAL_POLL_MS) || 4000));
9166
+ const now = await remoteCall(url, flags, `/api/workspaces/${enc(ws)}/proposals/${enc(p.id)}`);
9167
+ if (now.status !== "drafting") return proposalsCmd(["show", p.id], flags, layout).then(() => (now.status === "ready" ? 0 : 1));
9168
+ if (Date.now() > deadline) {
9169
+ if (flags.json) console.log(JSON.stringify(now, null, 2));
9170
+ else say(` ${c.dim(`still drafting — \`foldrun proposals show ${p.id} --to ${ws}\` later`)}\n`);
9171
+ return 1;
9172
+ }
9173
+ }
9174
+ }
9175
+
8764
9176
  /**
8765
9177
  * `foldrun promote <run-id>` — a finished run, kept as a regression case:
8766
9178
  * its task, and what the next run must still say.
@@ -9980,6 +10392,54 @@ async function flowTriggerCmd(positional, flags, layout) {
9980
10392
  });
9981
10393
  }
9982
10394
 
10395
+ /**
10396
+ * `foldrun flow filter <flow> --payload <file|-|json> [--to <workspace>]` —
10397
+ * would this delivery start the flow? Its `filter:` (core trigger-filter.ts)
10398
+ * against a payload: a webhook's JSON body, an email's { from, to, subject,
10399
+ * text }, a chained run's { run, flow, status, ending, verdict, summary }, a
10400
+ * storage event's { by, paths }. With --to, the deployed flow answers (POST
10401
+ * …/flows/<flow>/filter — the Triggers page's Test box); without, the local
10402
+ * file. Nothing starts. Exit 0 when it would start a run, 1 when not.
10403
+ */
10404
+ async function flowFilterCmd(positional, flags, layout) {
10405
+ const flow = positional[1];
10406
+ if (!flow || flags.payload === undefined || flags.payload === true) {
10407
+ throw new Error("`foldrun flow filter <flow> --payload <file.json|->` (or inline JSON); --to <workspace> tests the deployed flow");
10408
+ }
10409
+ const spec = String(flags.payload);
10410
+ const text = spec === "-" ? fs.readFileSync(0, "utf8") : /^\s*[{[]/.test(spec) ? spec : fs.readFileSync(spec, "utf8");
10411
+ let payload;
10412
+ try {
10413
+ payload = JSON.parse(text);
10414
+ } catch {
10415
+ payload = text; // read as a webhook body: not JSON, so no filter matches
10416
+ }
10417
+ let answer;
10418
+ if (namesPlatform(flags)) {
10419
+ const url = platformFor(flags, "flow filter");
10420
+ const ws = platformWorkspace(flags, layout, "flow filter");
10421
+ answer = await remoteCall(url, flags, `/api/workspaces/${enc(ws)}/flows/${enc(flow)}/filter`, { method: "POST", body: JSON.stringify({ payload }) });
10422
+ } else {
10423
+ const desk = await openDesk(flags, layout, `flow filter ${flow}`, flow);
10424
+ const rel = await flowFileOf(desk, flow);
10425
+ const lib = /** @type {any} */ (await core());
10426
+ if (typeof lib.matchFilter !== "function") {
10427
+ throw new Error("this install's @foldrun/core predates filter: — update foldrun, or test the deployed flow with --to <workspace>");
10428
+ }
10429
+ const f = lib.parseFlow(path.basename(rel), desk.files.get(rel));
10430
+ const read = typeof payload === "string" ? lib.webhookFilterPayload(payload) : payload;
10431
+ answer = { flow: f.name, trigger: f.trigger, filter: f.filter, applies: lib.FILTERABLE_TRIGGERS.includes(f.trigger), ...(f.filter != null ? lib.matchFilter(f.filter, read) : { match: true, reason: "this flow has no filter: — every delivery starts it" }) };
10432
+ }
10433
+ if (flags.json) {
10434
+ console.log(JSON.stringify(answer, null, 2));
10435
+ } else {
10436
+ console.log(`\n ${answer.match ? c.green("would start a run") : c.amber("would not start a run")} — ${answer.reason}`);
10437
+ if (answer.applies === false) console.log(` ${c.dim(`trigger: ${answer.trigger} delivers no payload, so its filter never applies`)}`);
10438
+ console.log("");
10439
+ }
10440
+ return answer.match ? 0 : 1;
10441
+ }
10442
+
9983
10443
  /**
9984
10444
  * `foldrun flow move-step <flow> --step <n|agent> --group <g> | --after <g>`
9985
10445
  * — dragging a card on the board: into group g, in parallel with what is
@@ -11290,6 +11750,13 @@ export async function run(command, positional, flags, workspace, layout) {
11290
11750
  // `eval new <name>` makes one on the platform; an eval called "new"
11291
11751
  // still runs as `foldrun eval new` with nothing after it.
11292
11752
  if (positional[0] === "new" && positional[1]) return evalNewCmd(positional[1], flags, layout);
11753
+ // `eval grades <name>` / `eval grade <name> --run <id>`: a grader's
11754
+ // findings, or one run graded now (an eval called "grades" or "grade"
11755
+ // still runs as `foldrun eval grades` with nothing after it).
11756
+ if (positional[0] === "grades" && positional[1]) return evalGradesCmd(positional[1], flags, layout);
11757
+ if (positional[0] === "grade" && positional[1]) return evalGradeCmd(positional[1], flags, layout);
11758
+ if (positional[0] === "compare" && positional[1]) return evalCompareCmd(positional[1], flags, layout);
11759
+ if (positional[0] === "improve" && positional[1]) return evalImproveCmd(positional[1], flags, layout);
11293
11760
  // --to names a deployed workspace: its evals run there.
11294
11761
  if (typeof flags.to === "string") return remoteEvalsCmd(positional[0], flags, layout);
11295
11762
  // --last reads the platform's results; a local eval has none to read,
@@ -11299,6 +11766,9 @@ export async function run(command, positional, flags, workspace, layout) {
11299
11766
  return runEvals(positional[0], flags);
11300
11767
  case "promote":
11301
11768
  return promoteCmd(positional[0], flags, layout);
11769
+ case "proposals":
11770
+ case "proposal":
11771
+ return proposalsCmd(positional, flags, layout);
11302
11772
  case "observe":
11303
11773
  return observeCmd(flags, layout);
11304
11774
  case "usage":
@@ -11360,6 +11830,8 @@ export async function run(command, positional, flags, workspace, layout) {
11360
11830
  return approvalsCmd(flags, layout);
11361
11831
  case "issues":
11362
11832
  return issuesCmd(positional, flags, layout);
11833
+ case "inbox":
11834
+ return inboxCmd(positional, flags);
11363
11835
  case "insights":
11364
11836
  return insightsCmd(flags);
11365
11837
  case "approve":
@@ -11437,6 +11909,7 @@ export async function run(command, positional, flags, workspace, layout) {
11437
11909
  if (positional[0] === "hook") return flowHookCmd(positional, flags, layout);
11438
11910
  if (positional[0] === "set") return flowSetCmd(positional, flags, layout);
11439
11911
  if (positional[0] === "trigger") return flowTriggerCmd(positional, flags, layout);
11912
+ if (positional[0] === "filter") return flowFilterCmd(positional, flags, layout);
11440
11913
  if (positional[0] === "move-step") return flowMoveStepCmd(positional, flags, layout);
11441
11914
  if (positional[0] === "copy-step") return flowCopyStepCmd(positional, flags, layout);
11442
11915
  if (positional[0] === "paste") return flowPasteCmd(positional, flags, layout);