foldrun 26.6.0 → 26.6.1

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/commands.mjs +72 -25
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "foldrun",
3
- "version": "26.6.0",
3
+ "version": "26.6.1",
4
4
  "description": "Agents are just folders. Write them, check them, run them — on your machine.",
5
5
  "license": "FSL-1.1-ALv2",
6
6
  "type": "module",
package/src/commands.mjs CHANGED
@@ -2677,8 +2677,37 @@ function rateLimitHint(res, body) {
2677
2677
  const EVENT_MARK = (e) =>
2678
2678
  e.type === "error" ? c.red("✗") : e.type === "tool" ? c.dim("→") : c.dim("·");
2679
2679
 
2680
+ /** Text with no terminal control in it: escape sequences (CSI, OSC and the
2681
+ * rest) and every other C0/C1 control character but the newline are
2682
+ * dropped. Agent output reaches the terminal through here, so it cannot
2683
+ * move the cursor, rewrite a line or dress up the apply prompt (Codex). */
2684
+ export const plainText = (text) =>
2685
+ String(text)
2686
+ .replace(/\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)?|\x1b\[[0-?]*[ -\/]*[@-~]|\x1b[@-Z\\-_]|\x9b[0-?]*[ -\/]*[@-~]/g, "")
2687
+ .replace(/[\x00-\x09\x0b-\x1f\x7f-\x9f]/g, "");
2688
+
2680
2689
  /** An event's first physical line, clipped to fit a terminal row. */
2681
- const clipLine = (text) => text.split("\n")[0].slice(0, 140);
2690
+ const clipLine = (text) => plainText(String(text).split("\n")[0]).slice(0, 140);
2691
+
2692
+ /**
2693
+ * What a tool call was for, in a few words — the command, the file, the
2694
+ * pattern — from its recorded input. Only a project with `capture: full`
2695
+ * records inputs (they can hold private data); otherwise there is none and
2696
+ * the line names the tool alone.
2697
+ * @param {unknown} input the call's input as the trail keeps it: JSON text
2698
+ */
2699
+ export function toolSummary(input) {
2700
+ if (typeof input !== "string" || !input) return "";
2701
+ let o;
2702
+ try {
2703
+ o = JSON.parse(input);
2704
+ } catch {
2705
+ return "";
2706
+ }
2707
+ if (!o || typeof o !== "object") return "";
2708
+ const v = o.command ?? o.file_path ?? o.path ?? o.pattern ?? o.url ?? o.query;
2709
+ return typeof v === "string" ? plainText(v.replace(/\s+/g, " ")).trim().slice(0, 100) : "";
2710
+ }
2682
2711
 
2683
2712
  /**
2684
2713
  * One console line for one event — or null when the event is held back.
@@ -2690,14 +2719,17 @@ const clipLine = (text) => text.split("\n")[0].slice(0, 140);
2690
2719
  * A start whose completion never lands — the stream ended mid-call — is
2691
2720
  * printed later by flushCalls. Every other event prints as it arrives.
2692
2721
  */
2693
- function eventLine(e, agent, open) {
2722
+ function eventLine(e, rawAgent, open) {
2723
+ const agent = plainText(rawAgent ?? "");
2694
2724
  if (e.type === "tool" && e.call != null) {
2695
2725
  if (e.ms == null) {
2696
2726
  open.set(e.call, { agent, e });
2697
2727
  return null;
2698
2728
  }
2729
+ const start = open.get(e.call)?.e;
2699
2730
  open.delete(e.call);
2700
- return ` ${EVENT_MARK(e)} ${c.dim(agent)} ${clipLine(e.text)} ${c.dim(`${e.ms}ms`)}`;
2731
+ const what = toolSummary(start?.io?.input ?? e.io?.input);
2732
+ return ` ${EVENT_MARK(e)} ${c.dim(agent)} ${clipLine(e.text)}${what ? ` ${c.dim(what)}` : ""} ${c.dim(`${Number(e.ms) || 0}ms`)}`;
2701
2733
  }
2702
2734
  return ` ${EVENT_MARK(e)} ${c.dim(agent)} ${clipLine(e.text)}`;
2703
2735
  }
@@ -2707,10 +2739,16 @@ function eventLine(e, agent, open) {
2707
2739
  * tool call is two events — one line per call, printed when it completes.
2708
2740
  * `open` carries the calls still in flight across polls and reconnects.
2709
2741
  */
2710
- function printNew(run, seen, open = new Map()) {
2742
+ function printNew(run, seen, open = new Map(), { skipResult = false } = {}) {
2743
+ // skipResult: the caller prints the last top-level step's reply in full
2744
+ // afterwards (foldrun code), so that one step's reply line is not printed
2745
+ // twice; any other step's (a sub-agent's) stays (Codex).
2746
+ const own = run.steps.filter((x) => !x.subagentOf);
2747
+ const replying = skipResult ? own.at(-1) : undefined;
2711
2748
  run.steps.forEach((step, i) => {
2712
2749
  const from = seen.get(i) ?? 0;
2713
2750
  for (const e of step.events.slice(from)) {
2751
+ if (step === replying && e.type === "text" && step.result && String(e.text).trim() === String(step.result).trim()) continue;
2714
2752
  const line = eventLine(e, step.agent, open);
2715
2753
  if (line !== null) console.log(line);
2716
2754
  }
@@ -2778,7 +2816,7 @@ export function sseFields(frame) {
2778
2816
  * the finished run on `done`, with the last run seen if the server ends it
2779
2817
  * first, and throws StreamIdle when nothing arrives for streamIdleSeconds.
2780
2818
  */
2781
- async function streamOnce(url, flags, ws, runId, seen, open = new Map()) {
2819
+ async function streamOnce(url, flags, ws, runId, seen, open = new Map(), print = {}) {
2782
2820
  const token = await renewedToken(url, flags);
2783
2821
  const idle = streamIdleSeconds();
2784
2822
  const target = new URL(`/api/workspaces/${ws}/runs/${runId}/stream`, url);
@@ -2829,7 +2867,7 @@ async function streamOnce(url, flags, ws, runId, seen, open = new Map()) {
2829
2867
  if (event === "run") {
2830
2868
  try {
2831
2869
  last = JSON.parse(data);
2832
- printNew(last, seen, open);
2870
+ printNew(last, seen, open, print);
2833
2871
  } catch {
2834
2872
  // a partial frame; the next one supersedes it
2835
2873
  }
@@ -2859,10 +2897,10 @@ async function streamOnce(url, flags, ws, runId, seen, open = new Map()) {
2859
2897
  * from printing twice. It used to block `invoke --watch` and `logs
2860
2898
  * --follow` for as long as a stalled tunnel cared to keep the socket.
2861
2899
  */
2862
- async function followRemote(url, flags, ws, runId, seen = new Map(), open = new Map(), { holdOpen = false } = {}) {
2900
+ async function followRemote(url, flags, ws, runId, seen = new Map(), open = new Map(), { holdOpen = false, skipResult = false } = {}) {
2863
2901
  for (let attempt = 1; ; attempt++) {
2864
2902
  try {
2865
- const run = await streamOnce(url, flags, ws, runId, seen, open);
2903
+ const run = await streamOnce(url, flags, ws, runId, seen, open, { skipResult });
2866
2904
  // holdOpen: the caller reads the run once more when the stream ended
2867
2905
  // with it still live, and flushes after — a start flushed here would
2868
2906
  // print a second time when that read brings its completion (Codex).
@@ -2871,7 +2909,7 @@ async function followRemote(url, flags, ws, runId, seen = new Map(), open = new
2871
2909
  } catch (err) {
2872
2910
  if (!(err instanceof StreamIdle)) throw err;
2873
2911
  const run = await remoteCall(url, flags, `/api/workspaces/${ws}/runs/${runId}`);
2874
- printNew(run, seen, open);
2912
+ printNew(run, seen, open, { skipResult });
2875
2913
  if (!isLive(run)) {
2876
2914
  flushCalls(open);
2877
2915
  return run;
@@ -12398,7 +12436,8 @@ async function runPatch(url, flags, ws, run, agentFlag) {
12398
12436
  function patchSummaryLines(meta) {
12399
12437
  const out = [];
12400
12438
  for (const [list, mark] of [[meta?.modified, c.amber("M")], [meta?.added, c.green("A")], [meta?.deleted, c.red("D")]]) {
12401
- for (const f of list ?? []) out.push(` ${mark} ${typeof f === "string" ? f : f?.path ?? JSON.stringify(f)}`);
12439
+ // The names are the agent's files: no terminal control reaches the screen.
12440
+ for (const f of list ?? []) out.push(` ${mark} ${plainText(typeof f === "string" ? f : f?.path ?? JSON.stringify(f))}`);
12402
12441
  }
12403
12442
  return out;
12404
12443
  }
@@ -12428,7 +12467,7 @@ async function applyPatch(dir, patch, flags, label, cancel = null) {
12428
12467
  const git = (...more) => spawnSync("git", ["-C", root, "apply", ...(prefix ? [`--directory=${prefix}`] : []), ...more], { input: patch, encoding: "utf8", maxBuffer: 64 * 1024 * 1024 });
12429
12468
  const check = git("--check");
12430
12469
  if (check.status !== 0 || check.error) {
12431
- throw new Error(`the patch does not apply to ${dir} as it is now — it was made against the folder as it was sent. git said:\n${String(check.stderr).trim()}\nThe patch is kept at ${keepPatch(patch, label)}`);
12470
+ throw new Error(`the patch does not apply to ${dir} as it is now — it was made against the folder as it was sent. git said:\n${plainText(String(check.stderr)).trim()}\nThe patch is kept at ${keepPatch(patch, label)}`);
12432
12471
  }
12433
12472
  // git refuses paths that climb out, name .git, or pass through a link; it
12434
12473
  // would still make or change a link or a submodule entry, which the
@@ -12452,7 +12491,7 @@ async function applyPatch(dir, patch, flags, label, cancel = null) {
12452
12491
  if (st?.isSymbolicLink()) odd.push(`${rel} is a link in this folder`);
12453
12492
  }
12454
12493
  if (odd.length) {
12455
- throw new Error(`the patch makes or changes a link, a submodule, a rename or a copy, which foldrun's patches never do — not applied:\n${odd.join("\n")}\nThe patch is kept at ${keepPatch(patch, label)}`);
12494
+ throw new Error(`the patch makes or changes a link, a submodule, a rename or a copy, which foldrun's patches never do — not applied:\n${plainText(odd.join("\n"))}\nThe patch is kept at ${keepPatch(patch, label)}`);
12456
12495
  }
12457
12496
  for (const line of String(git("--stat").stdout).trimEnd().split("\n")) if (line.trim()) console.log(` ${line.trim()}`);
12458
12497
  if (flags.yes !== true && !process.stdin.isTTY) {
@@ -12473,7 +12512,7 @@ async function applyPatch(dir, patch, flags, label, cancel = null) {
12473
12512
  return false;
12474
12513
  }
12475
12514
  const done = git();
12476
- if (done.status !== 0 || done.error) throw new Error(`git apply failed: ${String(done.stderr).trim()} — the patch is kept at ${keepPatch(patch, label)}`);
12515
+ if (done.status !== 0 || done.error) throw new Error(`git apply failed: ${plainText(String(done.stderr)).trim()} — the patch is kept at ${keepPatch(patch, label)}`);
12477
12516
  console.log(` ${c.green("✓")} applied ${c.dim("— review it with git diff, and commit it when you are happy")}`);
12478
12517
  return true;
12479
12518
  }
@@ -12564,7 +12603,7 @@ async function reportPatchCmd(runId, flags, layout) {
12564
12603
  return 0;
12565
12604
  }
12566
12605
  if (p.status !== "changed") {
12567
- console.error(`\n ${c.red("✗")} no patch: ${p.reason ?? p.status}\n`);
12606
+ console.error(`\n ${c.red("✗")} no patch: ${plainText(p.reason ?? p.status)}\n`);
12568
12607
  return 1;
12569
12608
  }
12570
12609
  if (flags.apply !== true) {
@@ -12595,7 +12634,9 @@ tools: [write, code]
12595
12634
 
12596
12635
  You work on a code folder a person sent from their computer. It is in
12597
12636
  snapshot/ under your working directory. Only what you change there comes
12598
- back to them, as a patch they review and apply.
12637
+ back to them, as a patch they review and apply. Your shell keeps its
12638
+ directory from one command to the next: cd into snapshot/ once, or write
12639
+ every path as snapshot/….
12599
12640
 
12600
12641
  - Read before you change anything: find the files the task is about, and
12601
12642
  the project's own conventions (README, AGENTS.md, CLAUDE.md, the tests).
@@ -12695,7 +12736,7 @@ async function codeTurn(s, task) {
12695
12736
  () => (cancel.stop = "acked"),
12696
12737
  (err) => {
12697
12738
  cancel.stop = "none";
12698
- console.log(` ${c.red("✗")} stop: ${explain(err)} — Ctrl-C again tries once more`);
12739
+ console.log(` ${c.red("✗")} stop: ${plainText(explain(err))} — Ctrl-C again tries once more`);
12699
12740
  },
12700
12741
  );
12701
12742
  };
@@ -12744,27 +12785,33 @@ async function codeTurn(s, task) {
12744
12785
  throw err;
12745
12786
  }
12746
12787
  runId = started?.runId;
12747
- if (!runId) throw new Error(`${url} started no run — ${JSON.stringify(started).slice(0, 200)}`);
12788
+ // Refused, not cleaned: an id with anything else in it is not used for
12789
+ // a stop, a read or a patch, which a cleaned one could aim at another
12790
+ // run (Codex). A refused id is shown as JSON, so it cannot drive the terminal.
12791
+ if (typeof runId !== "string" || !/^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/.test(runId)) {
12792
+ throw new Error(`${url} started no run it could name — ${plainText(JSON.stringify(started ?? null).slice(0, 200))}`);
12793
+ }
12748
12794
  cancel.runId = runId;
12749
12795
  if (cancel.asked) stop(); // pressed while the run was being started
12750
- console.log(` ${c.dim(`run ${runId}${started.test ? " (test)" : ""} — Ctrl-C stops it; the dashboard shows it live`)}\n`);
12796
+ console.log(` ${c.dim(`run ${plainText(runId)}${started.test ? " (test)" : ""} — Ctrl-C stops it; the dashboard shows it live`)}\n`);
12751
12797
  run = await remoteCall(url, flags, `/api/workspaces/${enc(ws)}/runs/${enc(runId)}`);
12752
12798
  const seen = new Map();
12753
12799
  const open = new Map();
12754
- printNew(run, seen, open);
12755
- if (isLive(run)) run = (await followRemote(url, flags, ws, runId, seen, open, { holdOpen: true })) ?? run;
12800
+ printNew(run, seen, open, { skipResult: true });
12801
+ if (isLive(run)) run = (await followRemote(url, flags, ws, runId, seen, open, { holdOpen: true, skipResult: true })) ?? run;
12756
12802
  if (isLive(run)) {
12757
12803
  run = await remoteCall(url, flags, `/api/workspaces/${enc(ws)}/runs/${enc(runId)}`);
12758
- printNew(run, seen, open);
12804
+ printNew(run, seen, open, { skipResult: true });
12759
12805
  }
12760
12806
  flushCalls(open);
12761
12807
  cancel.phase = "patch";
12762
12808
 
12763
12809
  const own = (run.steps ?? []).filter((x) => !x.subagentOf);
12764
12810
  const reply = own.at(-1)?.result;
12765
- if (reply) console.log(`\n${String(reply).trim().split("\n").map((l) => ` ${l}`).join("\n")}`);
12811
+ // The agent's words, right before the apply prompt: no terminal control (Codex).
12812
+ if (reply) console.log(`\n${plainText(String(reply)).trim().split("\n").map((l) => ` ${l}`).join("\n")}`);
12766
12813
  const cost = (run.steps ?? []).reduce((sum, x) => sum + (x.costUsd ?? 0), 0);
12767
- console.log(`\n ${run.status === "completed" ? c.green("✓") : c.red("✗")} ${run.status} ${c.dim(`· $${cost.toFixed(4)} · ${runId}`)}`);
12814
+ console.log(`\n ${run.status === "completed" ? c.green("✓") : c.red("✗")} ${plainText(run.status)} ${c.dim(`· $${cost.toFixed(4)} · ${plainText(runId)}`)}`);
12768
12815
  if (cancel.asked) {
12769
12816
  console.log(` ${c.dim(`stopped by you — nothing applied. What it made, if anything: foldrun report ${runId} patch --to ${ws}`)}\n`);
12770
12817
  return "cancelled";
@@ -12779,7 +12826,7 @@ async function codeTurn(s, task) {
12779
12826
  return run.status;
12780
12827
  }
12781
12828
  if (p.status === "unchanged") console.log(` ${c.dim("no change to the folder")}\n`);
12782
- else if (p.status !== "changed") console.log(` ${c.amber("!")} no patch: ${p.reason ?? p.status}\n`);
12829
+ else if (p.status !== "changed") console.log(` ${c.amber("!")} no patch: ${plainText(p.reason ?? p.status)}\n`);
12783
12830
  else {
12784
12831
  console.log();
12785
12832
  await applyPatch(dir, p.patch, flags, runId, cancel);
@@ -12868,7 +12915,7 @@ async function codeCmd(positional, flags, layout) {
12868
12915
  try {
12869
12916
  await codeTurn(s, line);
12870
12917
  } catch (err) {
12871
- console.log(` ${c.red("✗")} ${explain(err)}\n`);
12918
+ console.log(` ${c.red("✗")} ${plainText(explain(err))}\n`);
12872
12919
  }
12873
12920
  }
12874
12921
  }