foldrun 26.3.0 → 26.3.2

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/browser.mjs +46 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "foldrun",
3
- "version": "26.3.0",
3
+ "version": "26.3.2",
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
@@ -87,6 +87,7 @@ export function callEnv(paths, from = process.env) {
87
87
  out.FOLDRUN_BROWSER_HEADLESS = "0"; // a window on this screen, so a person can take over
88
88
  out.FOLDRUN_BROWSER_LIVE_ROOT = paths.chrome; // one profile that outlives sessions and reboots
89
89
  out.FOLDRUN_BROWSER_LIVE_IDLE_MS = "0"; // never closed for idleness: the person may be away
90
+ out.FOLDRUN_BROWSER_MODULES = paths.home; // the Playwright setup installed, ahead of any global one
90
91
  return out;
91
92
  }
92
93
 
@@ -122,6 +123,45 @@ function liveRecord(paths) {
122
123
  }
123
124
  const pidAlive = (pid) => { if (!pid) return false; try { process.kill(pid, 0); return true; } catch { return false; } };
124
125
 
126
+ /** Whether a running process is the foldrun window's Chrome — its command line
127
+ * names this profile — false when it is something else (a pid in an old
128
+ * live.json may since be anything's), null when that cannot be told. */
129
+ function ourChrome(pid, profileRoot) {
130
+ const r = process.platform === "win32"
131
+ ? spawnSync("powershell", ["-NoProfile", "-NonInteractive", "-Command", `(Get-CimInstance Win32_Process -Filter 'ProcessId=${Number(pid)}').CommandLine`], { encoding: "utf8", timeout: 15_000 })
132
+ : spawnSync("ps", ["-o", "args=", "-p", String(pid)], { encoding: "utf8", timeout: 5_000 });
133
+ if (r.status !== 0 || r.error) return null;
134
+ return r.stdout.includes(`--user-data-dir=${profileRoot}`);
135
+ }
136
+
137
+ /** Stop the window's Chrome before its profile goes: closed through the program
138
+ * when it can be, else asked to quit, else killed — and only when it really is
139
+ * ours. True when it no longer runs. */
140
+ async function stopLive(paths, ctx) {
141
+ const live = liveRecord(paths);
142
+ if (!live?.alive) return true;
143
+ // The one process checked here is the one signalled: its pid and profile
144
+ // are held, and ownership asked again right before each signal.
145
+ const { pid, root } = live;
146
+ const ours = () => ourChrome(pid, root);
147
+ const first = ours();
148
+ if (first === false) return true; // a stale record: our Chrome is not running, and the pid is left alone
149
+ if (ctx) await runBrowse(ctx, ["--live", "end"], { timeoutMs: 30_000 });
150
+ const wait = async () => { for (let i = 0; i < 50 && pidAlive(pid); i++) await new Promise((r) => setTimeout(r, 100)); };
151
+ for (const sig of /** @type {const} */ (["SIGTERM", "SIGKILL"])) {
152
+ if (!pidAlive(pid)) break;
153
+ const now = ours();
154
+ if (now === false) break; // it went, and the pid is another program's now
155
+ if (now === null) return false; // cannot tell it is ours: the person closes it
156
+ try { process.kill(pid, sig); } catch {}
157
+ await wait();
158
+ }
159
+ if (pidAlive(pid) && ours() !== false) return false;
160
+ // A window opened on the profile meanwhile (another call) holds it too.
161
+ const after = liveRecord(paths);
162
+ return !(after?.alive && after.pid !== pid && ourChrome(after.pid, after.root) !== false);
163
+ }
164
+
125
165
  /**
126
166
  * One call of browse.mjs. Never throws: the answer says what happened.
127
167
  * @param {{ browse: string, paths: ReturnType<typeof browserPaths> }} ctx
@@ -521,8 +561,13 @@ export async function browserCmd(positional, flags, deps) {
521
561
  case "reset": {
522
562
  if (!(await deps.sureToDelete(flags, "browser reset", `the foldrun browser's profile at ${paths.chrome} — every site signed in there is signed out`))) return 1;
523
563
  const ctx = await ready({ refresh: false });
524
- if (!("error" in ctx)) await runBrowse(ctx, ["--live", "end"], { timeoutMs: 30_000 });
525
564
  const live = liveRecord(paths);
565
+ // A Chrome still running on the profile keeps its sign-ins and writes
566
+ // them back: it stops first, or nothing is removed.
567
+ if (!(await stopLive(paths, "error" in ctx ? null : ctx))) {
568
+ console.log(`\n ${c.red("✗")} the foldrun Chrome window is still open${live?.pid ? ` (pid ${live.pid})` : ""} — close it, then run \`foldrun browser reset\` again; nothing was removed\n`);
569
+ return 1;
570
+ }
526
571
  fs.rmSync(paths.chrome, { recursive: true, force: true });
527
572
  if (live && live.root !== paths.chrome) fs.rmSync(live.root, { recursive: true, force: true });
528
573
  console.log(`\n ${c.green("✓")} profile removed — the next window starts signed out\n`);