foldrun 26.3.1 → 26.3.3

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/browser.md CHANGED
@@ -66,7 +66,7 @@ data centre. Clicks move the pointer the way a person's does.
66
66
  | `browser_open` | open a page (the current tab or a new one) and read it |
67
67
  | `browser_read` | read the current page again: the accessibility tree with a `[ref=eN]` on everything that can be acted on, or its text, markdown, links, tables, head |
68
68
  | `browser_act` | steps in order — click, fill, type, press, choose, scroll, wait, go back, switch tabs, upload, download, read a value — then read the page; an element is named by its ref (`@e13`), a CSS selector or `text=…` |
69
- | `browser_screenshot` | a picture of the page or one element |
69
+ | `browser_screenshot` | a picture of what is in view, the whole page (`full_page`) or one element; one too big to send (a very long page) is refused with what to take instead |
70
70
  | `browser_wait_for_person` | hand the page to you; returns when the address changes, or the condition given (`until_url_contains`, `until_gone`, `until_visible`) |
71
71
  | `browser_close` | close the window; the sign-ins stay |
72
72
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "foldrun",
3
- "version": "26.3.1",
3
+ "version": "26.3.3",
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/browser.mjs CHANGED
@@ -123,6 +123,20 @@ function liveRecord(paths) {
123
123
  }
124
124
  const pidAlive = (pid) => { if (!pid) return false; try { process.kill(pid, 0); return true; } catch { return false; } };
125
125
 
126
+ /** Whether the foldrun window is open: its Chrome running AND showing a page.
127
+ * On macOS, closing the last window leaves Chrome running with none, and a
128
+ * call would start a new window in front of the person who just closed it. */
129
+ export async function windowOpen(paths, fetcher = fetch) {
130
+ const live = liveRecord(paths);
131
+ if (!live?.alive) return false;
132
+ try {
133
+ const list = await (await fetcher(`${live.endpoint}/json/list`, { signal: AbortSignal.timeout(2_000) })).json();
134
+ return Array.isArray(list) && list.some((t) => t?.type === "page");
135
+ } catch {
136
+ return true; // not answering is no proof it is closed: the call says what happened
137
+ }
138
+ }
139
+
126
140
  /** Whether a running process is the foldrun window's Chrome — its command line
127
141
  * names this profile — false when it is something else (a pid in an old
128
142
  * live.json may since be anything's), null when that cannot be told. */
@@ -138,19 +152,28 @@ function ourChrome(pid, profileRoot) {
138
152
  * when it can be, else asked to quit, else killed — and only when it really is
139
153
  * ours. True when it no longer runs. */
140
154
  async function stopLive(paths, ctx) {
141
- let live = liveRecord(paths);
155
+ const live = liveRecord(paths);
142
156
  if (!live?.alive) return true;
143
- const ours = ourChrome(live.pid, live.root);
144
- if (ours === false) return true; // a stale record: our Chrome is not running, and the pid is left alone
157
+ // The one process checked here is the one signalled: its pid and profile
158
+ // are held, and ownership asked again right before each signal.
159
+ const { pid, root } = live;
160
+ const ours = () => ourChrome(pid, root);
161
+ const first = ours();
162
+ if (first === false) return true; // a stale record: our Chrome is not running, and the pid is left alone
145
163
  if (ctx) await runBrowse(ctx, ["--live", "end"], { timeoutMs: 30_000 });
146
- live = liveRecord(paths);
147
- if (!live?.alive) return true;
148
- if (ours === null) return false; // cannot tell it is ours: the person closes it
149
- const wait = async () => { for (let i = 0; i < 50 && pidAlive(live.pid); i++) await new Promise((r) => setTimeout(r, 100)); };
150
- try { process.kill(live.pid, "SIGTERM"); } catch {}
151
- await wait();
152
- if (pidAlive(live.pid)) { try { process.kill(live.pid, "SIGKILL"); } catch {} await wait(); }
153
- return !pidAlive(live.pid);
164
+ const wait = async () => { for (let i = 0; i < 50 && pidAlive(pid); i++) await new Promise((r) => setTimeout(r, 100)); };
165
+ for (const sig of /** @type {const} */ (["SIGTERM", "SIGKILL"])) {
166
+ if (!pidAlive(pid)) break;
167
+ const now = ours();
168
+ if (now === false) break; // it went, and the pid is another program's now
169
+ if (now === null) return false; // cannot tell it is ours: the person closes it
170
+ try { process.kill(pid, sig); } catch {}
171
+ await wait();
172
+ }
173
+ if (pidAlive(pid) && ours() !== false) return false;
174
+ // A window opened on the profile meanwhile (another call) holds it too.
175
+ const after = liveRecord(paths);
176
+ return !(after?.alive && after.pid !== pid && ourChrome(after.pid, after.root) !== false);
154
177
  }
155
178
 
156
179
  /**
@@ -251,7 +274,7 @@ export const TOOLS = [
251
274
  inputSchema: {
252
275
  type: "object",
253
276
  properties: {
254
- full_page: { type: "boolean", description: "the whole scrolling page, not just what is in view" },
277
+ full_page: { type: "boolean", description: "the whole scrolling page; without it, only what is in view. A long page can be too big to send" },
255
278
  selector: { type: "string", description: "only this element" },
256
279
  },
257
280
  additionalProperties: false,
@@ -316,10 +339,18 @@ export async function callTool(ctx, name, args = {}, { sleep = (ms) => new Promi
316
339
  }
317
340
  case "browser_screenshot": {
318
341
  const file = `outputs/screenshot-${Date.now()}.png`;
319
- const step = { screenshot: file, ...(args.full_page === true ? { full: true } : {}), ...(typeof args.selector === "string" && args.selector ? { selector: args.selector } : {}) };
342
+ // The program photographs the whole page unless told otherwise; say
343
+ // which, every time. A long page (a 450-comment thread) made a 23 MB
344
+ // picture that closed the app's MCP connection.
345
+ const step = { screenshot: file, full: args.full_page === true, ...(typeof args.selector === "string" && args.selector ? { selector: args.selector } : {}) };
320
346
  const r = await runBrowse(ctx, [...live, "--actions", JSON.stringify([step, { get: "url" }])]);
321
347
  const abs = path.join(ctx.paths.work, file);
322
348
  if (r.code !== 0 || !fs.existsSync(abs)) return textResult(shapeOutput(r), true);
349
+ const bytes = fs.statSync(abs).size;
350
+ if (bytes > MAX_SHOT_BYTES) {
351
+ fs.rmSync(abs, { force: true });
352
+ return textResult(`the picture is ${(bytes / 1e6).toFixed(1)} MB, too big to send (limit ${(MAX_SHOT_BYTES / 1e6).toFixed(1)} MB). Take what is in view (no full_page), one element (selector), or read the page as text with browser_read.`, true);
353
+ }
323
354
  const data = fs.readFileSync(abs).toString("base64");
324
355
  fs.rmSync(abs, { force: true });
325
356
  const url = /"value":\s*"([^"]+)"/.exec(r.stdout)?.[1];
@@ -336,6 +367,11 @@ export async function callTool(ctx, name, args = {}, { sleep = (ms) => new Promi
336
367
  }
337
368
  }
338
369
 
370
+ /** The largest picture browser_screenshot sends: model APIs refuse an image
371
+ * over 5 MB as base64 (about 3.7 MB of file), and a bigger MCP message can
372
+ * close the app's connection. */
373
+ export const MAX_SHOT_BYTES = 3_700_000;
374
+
339
375
  /** Ask the page where it is, without changing it. */
340
376
  async function probe(ctx, args) {
341
377
  const steps = [{ get: "url" }];
@@ -350,8 +386,11 @@ async function probe(ctx, args) {
350
386
  }
351
387
  }
352
388
 
389
+ const CLOSED = "the person closed the Chrome window — ask them whether they finished, then browser_open the page again (logins they made are kept)";
390
+
353
391
  async function waitForPerson(ctx, args, { sleep, now }) {
354
392
  const timeout = Math.min(900, Math.max(15, Number(args.timeout_seconds) || 300)) * 1000;
393
+ if (!(await windowOpen(ctx.paths))) return textResult("the Chrome window is not open — browser_open the page to hand over first", true);
355
394
  const first = await probe(ctx, args);
356
395
  if (!first) return textResult("could not read the page to watch it — is the window open? browser_open a page first", true);
357
396
  const start = first.url;
@@ -367,8 +406,7 @@ async function waitForPerson(ctx, args, { sleep, now }) {
367
406
  const deadline = now() + timeout;
368
407
  while (now() < deadline) {
369
408
  await sleep(3_000);
370
- const live = liveRecord(ctx.paths);
371
- if (live && !live.alive) return textResult("the person closed the Chrome window — ask them whether they finished, then browser_open the page again (logins they made are kept)", true);
409
+ if (!(await windowOpen(ctx.paths))) return textResult(CLOSED, true);
372
410
  const p = await probe(ctx, args);
373
411
  if (p && done(p)) return finish(ctx, `the person is done (${args.reason ?? "their part"}) — now on ${p.url}`);
374
412
  }
@@ -579,7 +617,8 @@ export async function browserCmd(positional, flags, deps) {
579
617
  console.log(` ${ok(p.browse)} browser program ${p.browse ? `${new URL(/** @type {string} */ (p.url)).host} ${c.dim(p.browse)}` : (p.note ?? "not fetched — foldrun browser setup")}`);
580
618
  console.log(` ${ok(have && have === want)} Playwright ${have ?? "not installed"}${want && have !== want ? c.amber(` (the program wants ${want} — foldrun browser setup)`) : ""}`);
581
619
  console.log(` ${c.dim("·")} profile ${live?.root ?? paths.chrome}${live && live.root !== paths.chrome ? c.amber(" (temp folder: this platform's browser program predates a fixed profile)") : ""}`);
582
- console.log(` ${c.dim("·")} window ${live?.alive ? `open ${c.dim(`(pid ${live.pid})`)}` : "closed"}`);
620
+ const open = await windowOpen(paths);
621
+ console.log(` ${c.dim("·")} window ${open ? `open ${c.dim(`(pid ${live?.pid})`)}` : live?.alive ? `closed ${c.dim("(Chrome still running with no window; the next call opens one)")}` : "closed"}`);
583
622
  console.log(`\n ${c.dim("verbs: setup, open [url], close, reset, mcp — `foldrun browser --help`")}\n`);
584
623
  return chrome && p.browse && have === want ? 0 : 1;
585
624
  }