agentlane 0.4.1 → 0.5.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.
Files changed (74) hide show
  1. package/CHANGELOG.md +76 -0
  2. package/dist/agents/claude-plugin.js +54 -8
  3. package/dist/agents/cli.js +11 -4
  4. package/dist/agents/hook-check.js +290 -0
  5. package/dist/agents/launcher.js +34 -6
  6. package/dist/agents/wiring-check.js +10 -4
  7. package/dist/agents/wiring.js +6 -33
  8. package/dist/browser/env.js +27 -7
  9. package/dist/browser/install.js +144 -6
  10. package/dist/browser/lane-browser.js +6 -3
  11. package/dist/browser/stop.js +2 -17
  12. package/dist/cache/key.js +23 -11
  13. package/dist/cli.js +63 -23
  14. package/dist/devices/backend.js +6 -0
  15. package/dist/devices/emulator.js +15 -5
  16. package/dist/devices/simctl.js +15 -3
  17. package/dist/disk-limits.js +134 -0
  18. package/dist/driver/caller.js +21 -6
  19. package/dist/driver/client.js +9 -5
  20. package/dist/driver/engine.js +29 -14
  21. package/dist/driver/install.js +88 -36
  22. package/dist/driver/paths.js +15 -11
  23. package/dist/driver/session.js +4 -1
  24. package/dist/driver/superseded.js +378 -0
  25. package/dist/driver-cli.js +32 -23
  26. package/dist/evidence/device.js +4 -0
  27. package/dist/evidence/evidence.js +4 -2
  28. package/dist/evidence/files.js +68 -0
  29. package/dist/evidence/strays.js +107 -0
  30. package/dist/hooks/env.js +2 -1
  31. package/dist/mcp/server.js +15 -0
  32. package/dist/mcp/tools.js +9 -1
  33. package/dist/plugin/guard.js +30 -0
  34. package/dist/plugin/hook-cli.js +101 -6
  35. package/dist/plugin/hook-mark.js +73 -0
  36. package/dist/plugin/hook-once.js +78 -0
  37. package/dist/plugin/shell.js +109 -8
  38. package/dist/processes.js +52 -2
  39. package/dist/registry/config.js +12 -8
  40. package/dist/registry/holds.js +37 -0
  41. package/dist/registry/leases.js +109 -0
  42. package/dist/registry/owner-checks.js +55 -0
  43. package/dist/registry/owner.js +18 -0
  44. package/dist/registry/parks.js +60 -0
  45. package/dist/registry/paths.js +28 -4
  46. package/dist/registry/ports.js +5 -1
  47. package/dist/registry/prune.js +643 -24
  48. package/dist/registry/refusal.js +1 -0
  49. package/dist/registry/registry.js +1258 -235
  50. package/dist/registry/rows.js +18 -4
  51. package/dist/registry/status.js +51 -8
  52. package/dist/registry/store.js +63 -1
  53. package/dist/registry/touch.js +180 -0
  54. package/dist/registry-cli.js +197 -36
  55. package/dist/services/services.js +223 -54
  56. package/dist/services/system.js +2 -1
  57. package/dist/up/answers.js +124 -0
  58. package/dist/up/doctor.js +27 -3
  59. package/dist/up/exec.js +29 -4
  60. package/dist/up/lane-files.js +26 -8
  61. package/dist/up/lane-services.js +8 -3
  62. package/dist/up/park.js +146 -0
  63. package/dist/up/preflight.js +2 -10
  64. package/dist/up/release.js +20 -24
  65. package/dist/up/restart.js +9 -2
  66. package/dist/up/sweep.js +437 -0
  67. package/dist/up/up.js +33 -12
  68. package/dist/up/wake.js +124 -0
  69. package/dist/up/waker.js +49 -0
  70. package/dist/up-cli.js +6 -1
  71. package/package.json +2 -2
  72. package/skills/dev-loop/SKILL.md +7 -5
  73. package/skills/drive/SKILL.md +4 -0
  74. package/skills/setup/SKILL.md +2 -2
@@ -4,7 +4,6 @@ import { hookCommand, isToolkitHook, mcpLaunch } from "./launcher.js";
4
4
  import { stubOf } from "./skills.js";
5
5
  export const SKILLS = '.agents/skills';
6
6
  export const CLAUDE_SKILLS = '.claude/skills';
7
- const CLAUDE_HOOK_EVENTS = ['PreToolUse', 'SessionEnd'];
8
7
  function object(doc, key) {
9
8
  const value = doc[key] ?? {};
10
9
  if (typeof value !== 'object' || Array.isArray(value))
@@ -119,6 +118,11 @@ function claudeEdits() {
119
118
  },
120
119
  present: () => false,
121
120
  },
121
+ ...claudeHookEdits(),
122
+ ];
123
+ }
124
+ function claudeHookEdits() {
125
+ return [
122
126
  hookEdit(CLAUDE_SETTINGS, 'claude', 'guard hook', 'PreToolUse', {
123
127
  matcher: 'Bash|Monitor',
124
128
  hooks: [command('claude', 'pre-tool-use', { timeout: 30 })],
@@ -129,15 +133,6 @@ function claudeEdits() {
129
133
  }),
130
134
  ];
131
135
  }
132
- export function hasClaudeHooks(doc) {
133
- const hooks = doc.hooks;
134
- if (typeof hooks !== 'object' || hooks === null)
135
- return false;
136
- return CLAUDE_HOOK_EVENTS.some(event => {
137
- const entries = hooks[event];
138
- return Array.isArray(entries) && entries.some(hasToolkitHook);
139
- });
140
- }
141
136
  function claudePluginEdits(disableServer) {
142
137
  const disable = {
143
138
  kind: 'json',
@@ -157,28 +152,6 @@ function claudePluginEdits(disableServer) {
157
152
  },
158
153
  present: doc => pluginEnabled(doc) || disablesOurServer(doc),
159
154
  };
160
- const removeHooks = {
161
- kind: 'json',
162
- file: CLAUDE_SETTINGS,
163
- agent: 'claude',
164
- part: 'plugin',
165
- apply: doc => {
166
- if (!hasClaudeHooks(doc))
167
- return null;
168
- const kept = Object.entries(doc.hooks).flatMap(([event, entries]) => {
169
- if (!CLAUDE_HOOK_EVENTS.includes(event) || !Array.isArray(entries))
170
- return [[event, entries]];
171
- const rest = entries.filter(entry => !hasToolkitHook(entry));
172
- return rest.length > 0 ? [[event, rest]] : [];
173
- });
174
- if (kept.length > 0)
175
- doc.hooks = Object.fromEntries(kept);
176
- else
177
- delete doc.hooks;
178
- return null;
179
- },
180
- present: hasClaudeHooks,
181
- };
182
155
  const rename = {
183
156
  kind: 'json',
184
157
  file: CLAUDE_SETTINGS,
@@ -190,7 +163,7 @@ function claudePluginEdits(disableServer) {
190
163
  },
191
164
  present: doc => formerPluginKeys(doc).length > 0,
192
165
  };
193
- return disableServer ? [rename, disable, removeHooks] : [rename, removeHooks];
166
+ return [rename, ...(disableServer ? [disable] : []), ...claudeHookEdits()];
194
167
  }
195
168
  export function pluginEnabled(settings) {
196
169
  const enabled = settings.enabledPlugins;
@@ -7,6 +7,10 @@ export const BROWSER_DRIVER_ENV = envName('BROWSER_DRIVER');
7
7
  export const BROWSER_SESSION_ENV = envName('BROWSER_SESSION');
8
8
  export const BROWSER_PROFILE_ENV = envName('BROWSER_PROFILE');
9
9
  export const BROWSER_CDP_ENV = envName('BROWSER_CDP');
10
+ /** The variable agent-browser reads the browser it launches from. */
11
+ export const EXECUTABLE_PATH_ENV = 'AGENT_BROWSER_EXECUTABLE_PATH';
12
+ /** The lane's `bin/` file that names the browsers it was set up with, one executable a line. */
13
+ const BROWSERS_RECORD = '.browsers';
10
14
  export function cdpUrl(browser) {
11
15
  return `http://127.0.0.1:${String(browser.debugPort)}`;
12
16
  }
@@ -30,7 +34,8 @@ done
30
34
  exec ${cli.map(shellQuote).join(' ')} "$@"
31
35
  `;
32
36
  }
33
- export function agentBrowserShim(cli, ffmpegDir) {
37
+ export function agentBrowserShim(cli, ffmpegDir, executable) {
38
+ const executableLine = executable === null ? '' : `${EXECUTABLE_PATH_ENV}=${shellQuote(executable)}\nexport ${EXECUTABLE_PATH_ENV}\n`;
34
39
  const pathLine = ffmpegDir === null ? '' : `PATH=${shellQuote(ffmpegDir)}:"$PATH"\nexport PATH\n`;
35
40
  return `#!/bin/sh
36
41
  # ${TOOL}: agent-browser, the toolkit's pinned build, on the toolkit's Chrome for Testing.
@@ -39,19 +44,33 @@ case "$1" in
39
44
  echo "${TOOL}: agent-browser and its Chrome are pinned and installed by ${TOOL} (${CLI} browser install), so agent-browser $1 is refused here." >&2
40
45
  exit 2 ;;
41
46
  esac
42
- ${pathLine}exec ${cli.map(shellQuote).join(' ')} "$@"
47
+ ${executableLine}${pathLine}exec ${cli.map(shellQuote).join(' ')} "$@"
43
48
  `;
44
49
  }
45
- function writeExecutable(file, content) {
50
+ /** The browser executables an install gives a lane, one a line. */
51
+ function browsersRecord(install) {
52
+ return [install.chrome, install.headlessShell, install.ffmpeg]
53
+ .filter(file => file !== '')
54
+ .map(file => `${file}\n`)
55
+ .join('');
56
+ }
57
+ function writeFile(file, content, mode) {
46
58
  const temp = `${file}.${String(process.pid)}.tmp`;
47
- fs.writeFileSync(temp, content, { mode: 0o755 });
59
+ fs.writeFileSync(temp, content, { mode });
48
60
  fs.renameSync(temp, file);
49
61
  }
50
- /** Writes the lane's driver wrapper and the files its driver reads, and returns the folder that goes first on PATH. */
62
+ function writeExecutable(file, content) {
63
+ writeFile(file, content, 0o755);
64
+ }
65
+ /**
66
+ * Writes the lane's driver wrapper, the files its driver reads and the record of its browsers, and returns the folder
67
+ * that goes first on PATH.
68
+ */
51
69
  export function writeDriverFiles(setting, laneId) {
52
70
  const { paths, install, browser } = setting;
53
71
  for (const dir of [paths.dir, paths.bin, paths.output, paths.evidence])
54
72
  fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
73
+ writeFile(path.join(paths.bin, BROWSERS_RECORD), browsersRecord(install), 0o600);
55
74
  if (browser.driver === 'playwright') {
56
75
  fs.mkdirSync(paths.playwrightSessions, { recursive: true, mode: 0o700 });
57
76
  const config = path.join(paths.playwrightConfigHome, '.playwright');
@@ -69,7 +88,8 @@ export function writeDriverFiles(setting, laneId) {
69
88
  fs.rmSync(link, { force: true });
70
89
  fs.symlinkSync(install.ffmpeg, link);
71
90
  }
72
- writeExecutable(path.join(paths.bin, 'agent-browser'), agentBrowserShim(install.cli, ffmpegDir));
91
+ const executable = install.headlessShell === '' ? null : install.headlessShell;
92
+ writeExecutable(path.join(paths.bin, 'agent-browser'), agentBrowserShim(install.cli, ffmpegDir, executable));
73
93
  }
74
94
  return paths.bin;
75
95
  }
@@ -111,7 +131,7 @@ export function browserVariables(setting, basePath) {
111
131
  AGENT_BROWSER_SCREENSHOT_DIR: paths.output,
112
132
  });
113
133
  if (install.headlessShell !== '')
114
- vars.AGENT_BROWSER_EXECUTABLE_PATH = install.headlessShell;
134
+ vars[EXECUTABLE_PATH_ENV] = install.headlessShell;
115
135
  if (browser.streamPort !== null)
116
136
  vars.AGENT_BROWSER_STREAM_PORT = String(browser.streamPort);
117
137
  return vars;
@@ -6,18 +6,24 @@ import path from 'node:path';
6
6
  import { pipeline } from 'node:stream/promises';
7
7
  import { DriverError, messageOf } from "../driver/errors.js";
8
8
  import { runCommand, tail } from "../driver/exec.js";
9
- import { dataDir, sha256Of } from "../driver/install.js";
9
+ import { dataDir, namedDataDir, sha256Of } from "../driver/install.js";
10
10
  import { tryLock, unlock } from "../driver/lock.js";
11
+ import { compareVersions, laneToolsOf, realOrResolved, removeOlderVersions, systemCommandLines, } from "../driver/superseded.js";
11
12
  import { CLI, envName, TOOL } from "../name.js";
12
13
  import { identityOf, systemProcesses } from "../processes.js";
14
+ import { EXECUTABLE_PATH_ENV } from "./env.js";
13
15
  import { AGENT_BROWSER_RELEASE, hostKey, PLAYWRIGHT_RELEASE, } from "./release.js";
14
16
  /** A Playwright browsers folder to use instead of the pinned builds, for a host they aren't pinned for. */
15
17
  export const BROWSERS_OVERRIDE_ENV = envName('PLAYWRIGHT_BROWSERS');
16
18
  export const INSTALL_FIX = `${CLI} browser install`;
17
19
  const MARKER = '.agentlane-installed';
18
20
  const LOCK_WAIT_MS = 15 * 60_000;
21
+ /** The browsers folder the toolkit installs the pinned builds in and links its playwright-cores from. */
22
+ function sharedBrowsersPath(env) {
23
+ return path.join(dataDir(env), 'playwright', 'browsers');
24
+ }
19
25
  export function browsersPath(env) {
20
- return env[BROWSERS_OVERRIDE_ENV] ?? path.join(dataDir(env), 'playwright', 'browsers');
26
+ return env[BROWSERS_OVERRIDE_ENV] ?? sharedBrowsersPath(env);
21
27
  }
22
28
  function playwrightHome(env, release) {
23
29
  return path.join(dataDir(env), 'playwright', release.version);
@@ -282,13 +288,86 @@ function replace(target, fresh, work) {
282
288
  fs.renameSync(target, path.join(work, `previous-${path.basename(target)}-${String(Date.now())}`));
283
289
  fs.renameSync(fresh, target);
284
290
  }
291
+ function installedFolders(parent) {
292
+ try {
293
+ return fs.readdirSync(parent).filter(name => !name.startsWith('.') && readMarker(path.join(parent, name)) !== undefined);
294
+ }
295
+ catch {
296
+ return [];
297
+ }
298
+ }
299
+ function packagesBy(parent, version, order) {
300
+ return installedFolders(parent)
301
+ .filter(name => readMarker(path.join(parent, name))?.version === name && order(compareVersions(name, version)))
302
+ .map(name => path.join(parent, name));
303
+ }
304
+ /** The browser folders a playwright-core package runs, by its `browsers.json`; undefined when that can't be read. */
305
+ function browsersNamedBy(pkg, base) {
306
+ try {
307
+ const pins = JSON.parse(fs.readFileSync(path.join(pkg, 'browsers.json'), 'utf8')).browsers;
308
+ return pins.flatMap(pin => [pin.revision, ...Object.values(pin.revisionOverrides ?? {})].map(revision => path.join(base, browserFolder({ name: pin.name, revision }))));
309
+ }
310
+ catch {
311
+ return undefined;
312
+ }
313
+ }
314
+ function pinnedBrowsers(core, base) {
315
+ return browsersNamedBy(path.join(core, 'package'), base);
316
+ }
317
+ /**
318
+ * The entries of a browsers folder's `.links`, read as Playwright's own cleanup reads them: each holds the path of a
319
+ * playwright-core package, whose `browsers.json` names the browser folders that cleanup keeps.
320
+ */
321
+ function linksOf(base) {
322
+ let files;
323
+ try {
324
+ files = fs.readdirSync(path.join(base, '.links'));
325
+ }
326
+ catch {
327
+ return [];
328
+ }
329
+ return files.map(name => {
330
+ const file = path.join(base, '.links', name);
331
+ let target = '';
332
+ try {
333
+ target = fs.readFileSync(file, 'utf8');
334
+ }
335
+ catch {
336
+ return { file, target, real: undefined, names: undefined };
337
+ }
338
+ if (!path.isAbsolute(target))
339
+ return { file, target, real: undefined, names: undefined };
340
+ return { file, target, real: realOrResolved(target), names: browsersNamedBy(target, base) };
341
+ });
342
+ }
343
+ /** The browser folders the toolkit installed, each with the pinned build it is a revision of. */
344
+ function installedBuilds(base, builds) {
345
+ return installedFolders(base).flatMap(name => {
346
+ const m = /^(.+)-(\d+)$/.exec(name);
347
+ const pin = builds.find(b => browserFolder({ name: b.name, revision: '' }) === `${m?.[1] ?? ''}-`);
348
+ return m?.[2] !== undefined && pin !== undefined ? [{ dir: path.join(base, name), pin, revision: m[2] }] : [];
349
+ });
350
+ }
351
+ function olderBrowsers(base, builds) {
352
+ return installedBuilds(base, builds).filter(build => compareVersions(build.revision, build.pin.revision) < 0);
353
+ }
354
+ function linkOf(base, core) {
355
+ return path.join(base, '.links', crypto.createHash('sha1').update(core).digest('hex'));
356
+ }
285
357
  /** Links our playwright-core into the browsers folder the way Playwright's own install does, so its cleanup keeps them. */
286
358
  function linkPlaywright(base, core) {
287
- const links = path.join(base, '.links');
288
- fs.mkdirSync(links, { recursive: true });
289
- fs.writeFileSync(path.join(links, crypto.createHash('sha1').update(core).digest('hex')), core);
359
+ fs.mkdirSync(path.join(base, '.links'), { recursive: true });
360
+ fs.writeFileSync(linkOf(base, core), core);
290
361
  }
291
- /** Installs what the driver needs that is missing or does not match its pin, under a lock held across processes. */
362
+ /**
363
+ * Installs what the driver needs that is missing or does not match its pin, under a lock held across processes.
364
+ * Then removes the older driver packages and browsers it installed that no running process, no file of a recorded
365
+ * lane's `bin/` or `browser/ffmpeg/ffmpeg`, no kept version and no readable link in the browsers folder's `.links` uses.
366
+ * A running agent-browser keeps the browser its environment names and every older ffmpeg, and every older browser stays
367
+ * while a process or a lane's file runs agent-browser, of any version, without naming the browser it launches. An older
368
+ * playwright-core stays, with its link, while an older browser it pins does: Playwright's own install deletes every
369
+ * browser folder that no link names, unless PLAYWRIGHT_SKIP_BROWSER_GC is set.
370
+ */
292
371
  export async function ensureBrowserDriver(driver, options) {
293
372
  const releases = {
294
373
  playwright: options.playwright ?? PLAYWRIGHT_RELEASE,
@@ -352,6 +431,65 @@ export async function ensureBrowserDriver(driver, options) {
352
431
  const after = installState(options.env, driver, releases);
353
432
  if (after.kind !== 'installed')
354
433
  throw new DriverError(describeInstallState(driver, after), `retry ${INSTALL_FIX}`);
434
+ const log = options.log ?? (() => undefined);
435
+ const base = sharedBrowsersPath(options.env);
436
+ const cores = path.dirname(core);
437
+ const overridden = options.env[BROWSERS_OVERRIDE_ENV] !== undefined;
438
+ const olderInstalled = olderBrowsers(base, playwright.browsers);
439
+ const browsers = overridden ? [] : olderInstalled;
440
+ const all = browsers.map(b => b.dir);
441
+ const agentBrowsers = path.dirname(agentBrowserHome(options.env, agentBrowser));
442
+ const launches = overridden
443
+ ? []
444
+ : installedBuilds(base, playwright.browsers)
445
+ .filter(b => b.pin.name !== 'ffmpeg')
446
+ .map(b => b.dir);
447
+ const newer = packagesBy(cores, playwright.version, order => order > 0).map(dir => pinnedBrowsers(dir, base) ?? all);
448
+ const olderCores = packagesBy(cores, playwright.version, order => order < 0);
449
+ const links = linksOf(base);
450
+ const coreLinks = new Map(olderCores.map(dir => {
451
+ const real = realOrResolved(path.join(dir, 'package'));
452
+ return [dir, links.filter(link => link.real === real)];
453
+ }));
454
+ const ownLinks = new Set([...coreLinks.values()].flat());
455
+ const named = new Map(links.filter(link => !ownLinks.has(link)).flatMap(link => (link.names ?? []).map(dir => [dir, link.target])));
456
+ const candidates = browsers.filter(b => !newer.flat().includes(b.dir));
457
+ for (const b of candidates) {
458
+ const by = named.get(b.dir);
459
+ if (by !== undefined)
460
+ log(`kept ${b.dir}, which ${by} names`);
461
+ }
462
+ const olderFolders = olderInstalled.map(b => b.dir);
463
+ const older = [
464
+ ...olderCores.map(dir => {
465
+ const pins = pinnedBrowsers(dir, base);
466
+ return { dir, needs: pins ?? all, heldBy: olderFolders.filter(folder => pins?.includes(folder)) };
467
+ }),
468
+ ...packagesBy(agentBrowsers, agentBrowser.version, order => order < 0).map(dir => ({ dir, needs: all })),
469
+ ...candidates.filter(b => !named.has(b.dir)).map(b => ({ dir: b.dir })),
470
+ ];
471
+ const removed = removeOlderVersions(older, {
472
+ root: namedDataDir(options.env),
473
+ me,
474
+ processes,
475
+ commandLines: options.commandLines ?? systemCommandLines,
476
+ laneTools: options.laneTools ?? laneToolsOf(options.env),
477
+ launchers: [
478
+ {
479
+ dir: agentBrowsers,
480
+ launches,
481
+ variable: EXECUTABLE_PATH_ENV,
482
+ needs: all,
483
+ onPath: browsers.filter(b => b.pin.name === 'ffmpeg').map(b => b.dir),
484
+ },
485
+ ],
486
+ log,
487
+ });
488
+ for (const dir of removed.filter(d => path.dirname(d) === cores)) {
489
+ for (const file of [linkOf(base, path.join(dir, 'package')), ...(coreLinks.get(dir) ?? []).map(link => link.file)]) {
490
+ fs.rmSync(file, { force: true });
491
+ }
492
+ }
355
493
  return after.driver;
356
494
  }
357
495
  finally {
@@ -108,8 +108,11 @@ function forgetLogin(paths) {
108
108
  fs.rmSync(target, { recursive: true, force: true });
109
109
  }
110
110
  }
111
- /** Stops a lane's browser: the driver's own close first, then every process of it the process table still shows. */
112
- export async function stopLaneBrowser(tools, stateDir, lane, session, clock, control) {
111
+ /**
112
+ * Stops a lane's browser: the driver's own close first, then every process of it the process table still shows. Once
113
+ * nothing of it runs, its login goes too, unless `keepLogin`, for a lane that opens its browser again.
114
+ */
115
+ export async function stopLaneBrowser(tools, stateDir, lane, session, clock, control, keepLogin = false) {
113
116
  const result = { notes: [], problems: [] };
114
117
  if (control) {
115
118
  try {
@@ -130,7 +133,7 @@ export async function stopLaneBrowser(tools, stateDir, lane, session, clock, con
130
133
  result.problems.push(`the process table could not be read, so the browser of lane ${lane} may still run`);
131
134
  else if (stopped.left.length > 0)
132
135
  result.problems.push(`the browser of lane ${lane} survived SIGKILL: ${describeProcesses(stopped.left)}`);
133
- else
136
+ else if (!keepLogin)
134
137
  forgetLogin(marks.paths);
135
138
  return result;
136
139
  }
@@ -1,23 +1,8 @@
1
1
  import { spawnSync } from 'node:child_process';
2
2
  import fs from 'node:fs';
3
3
  import path from 'node:path';
4
+ import { environmentOf } from "../processes.js";
4
5
  const SEARCH_PATH = `${process.env.PATH ?? ''}:/usr/bin:/bin`;
5
- function readEnviron(pid) {
6
- if (process.platform === 'linux') {
7
- try {
8
- return fs.readFileSync(`/proc/${String(pid)}/environ`, 'utf8').replaceAll('\0', ' ');
9
- }
10
- catch {
11
- return undefined;
12
- }
13
- }
14
- const result = spawnSync('ps', ['eww', '-o', 'command=', '-p', String(pid)], {
15
- encoding: 'utf8',
16
- env: { PATH: SEARCH_PATH, LC_ALL: 'C' },
17
- timeout: 10_000,
18
- });
19
- return result.status === 0 ? result.stdout : undefined;
20
- }
21
6
  export const realBrowserProcesses = {
22
7
  list() {
23
8
  const result = spawnSync('ps', ['-A', '-ww', '-o', 'pid=,ppid=,pgid=,args='], {
@@ -36,7 +21,7 @@ export const realBrowserProcesses = {
36
21
  }
37
22
  return entries;
38
23
  },
39
- environment: readEnviron,
24
+ environment: environmentOf,
40
25
  signal(pid, signal) {
41
26
  try {
42
27
  process.kill(pid, signal);
package/dist/cache/key.js CHANGED
@@ -1,6 +1,5 @@
1
1
  import { execFile } from 'node:child_process';
2
2
  import { createHash } from 'node:crypto';
3
- import { createReadStream } from 'node:fs';
4
3
  import fs from 'node:fs/promises';
5
4
  import path from 'node:path';
6
5
  import { promisify } from 'node:util';
@@ -18,10 +17,23 @@ function sha256(data) {
18
17
  }
19
18
  async function sha256File(file) {
20
19
  const hash = createHash('sha256');
21
- for await (const chunk of createReadStream(file))
20
+ const handle = await fs.open(file);
21
+ for await (const chunk of handle.createReadStream())
22
22
  hash.update(chunk);
23
23
  return hash.digest('hex');
24
24
  }
25
+ function stampOf(stat) {
26
+ return [stat.dev, stat.ino, stat.size, stat.mtimeNs, stat.ctimeNs].join(':');
27
+ }
28
+ async function fileDigest(full, stat, digests) {
29
+ const stamp = stampOf(stat);
30
+ const known = digests?.get(full);
31
+ if (known?.stamp === stamp)
32
+ return known.digest;
33
+ const digest = await sha256File(full);
34
+ digests?.set(full, { stamp, digest });
35
+ return digest;
36
+ }
25
37
  function errorText(error) {
26
38
  if (!(error instanceof Error))
27
39
  return String(error);
@@ -42,11 +54,11 @@ async function listFiles(root, input) {
42
54
  const names = [...new Set(stdout.split('\0').filter(name => name.length > 0))];
43
55
  return names.sort((a, b) => Buffer.compare(Buffer.from(a), Buffer.from(b)));
44
56
  }
45
- async function describeFile(root, name) {
57
+ async function describeFile(root, name, digests) {
46
58
  const full = path.join(root, name);
47
59
  let stat;
48
60
  try {
49
- stat = await fs.lstat(full);
61
+ stat = await fs.lstat(full, { bigint: true });
50
62
  }
51
63
  catch (error) {
52
64
  if (error.code === 'ENOENT')
@@ -57,14 +69,14 @@ async function describeFile(root, name) {
57
69
  return `link ${sha256(await fs.readlink(full))} ${name}`;
58
70
  if (stat.isDirectory())
59
71
  return `dir ${name}`;
60
- const exec = (stat.mode & 0o111) !== 0 ? 'x' : '-';
61
- return `file${exec} ${await sha256File(full)} ${name}`;
72
+ const exec = (stat.mode & 73n) !== 0n ? 'x' : '-';
73
+ return `file${exec} ${await fileDigest(full, stat, digests)} ${name}`;
62
74
  }
63
- async function hashFiles(root, input) {
75
+ async function hashFiles(root, input, digests) {
64
76
  const names = await listFiles(root, input);
65
77
  const lines = [];
66
78
  for (let i = 0; i < names.length; i += HASH_CONCURRENCY) {
67
- lines.push(...(await Promise.all(names.slice(i, i + HASH_CONCURRENCY).map(name => describeFile(root, name)))));
79
+ lines.push(...(await Promise.all(names.slice(i, i + HASH_CONCURRENCY).map(name => describeFile(root, name, digests)))));
68
80
  }
69
81
  return sha256(lines.join('\n'));
70
82
  }
@@ -95,8 +107,8 @@ async function hashEnvFile(root, envFile) {
95
107
  throw new CacheKeyError(`reading the env file ${envFile} failed: ${errorText(error)}`);
96
108
  }
97
109
  }
98
- async function hashInput(root, input, env) {
99
- return input.kind === 'files' ? hashFiles(root, input) : hashCommand(root, input.run, env);
110
+ async function hashInput(root, input, options) {
111
+ return input.kind === 'files' ? hashFiles(root, input, options.digests) : hashCommand(root, input.run, options.env);
100
112
  }
101
113
  export async function computeKey(options) {
102
114
  const lines = [
@@ -107,7 +119,7 @@ export async function computeKey(options) {
107
119
  `env=${await hashEnvFile(options.root, options.envFile)}`,
108
120
  ];
109
121
  const inputs = inputsFor(options.inputs, options.platform);
110
- const digests = await Promise.all(inputs.map(input => hashInput(options.root, input, options.env)));
122
+ const digests = await Promise.all(inputs.map(input => hashInput(options.root, input, options)));
111
123
  inputs.forEach((input, i) => {
112
124
  lines.push(`input=${JSON.stringify(input)} ${digests[i] ?? ''}`);
113
125
  });
package/dist/cli.js CHANGED
@@ -19,6 +19,8 @@ import { REGISTRY_COMMANDS, REGISTRY_HELP, REGISTRY_USAGE, runRegistryCommand }
19
19
  import { realServiceSystem } from "./services/system.js";
20
20
  import { UP_COMMANDS, UP_HELP, UP_USAGE, runUpCommand } from "./up-cli.js";
21
21
  import { systemToolchain } from "./up/preflight.js";
22
+ import { passBy, runSweep, sweeper, SWEEP_COMMAND, sweepTick } from "./up/sweep.js";
23
+ import { upWaker } from "./up/waker.js";
22
24
  import { asksForHelp, helpOf } from "./usage.js";
23
25
  const USAGE = `Usage: ${TOOL} <command>
24
26
 
@@ -76,10 +78,58 @@ async function main(args) {
76
78
  process.stdout.write(help);
77
79
  return 0;
78
80
  }
81
+ const state = stateDir(process.env);
82
+ const startSweep = sweeper(process.argv[1] ?? '', process.env);
83
+ const passingBy = () => {
84
+ passBy({ dir: state }, startSweep);
85
+ };
86
+ if (command !== 'hook' && command !== SWEEP_COMMAND && command !== 'reap')
87
+ passingBy();
79
88
  const mac = process.platform === 'darwin';
80
- const android = createEmulatorBackend({ env: process.env, logDir: path.join(stateDir(process.env), 'emulators') });
89
+ const android = createEmulatorBackend({ env: process.env, logDir: path.join(state, 'emulators') });
81
90
  const devices = mac ? { ios: simctlBackend, android } : { android };
82
91
  const sim = mac ? createXcrunTools() : null;
92
+ const envDeps = () => ({
93
+ env: process.env,
94
+ cwd: process.cwd(),
95
+ log: line => process.stderr.write(`${line}\n`),
96
+ processes: systemProcesses,
97
+ ports: tcpProbe,
98
+ devices,
99
+ sim,
100
+ system: realServiceSystem,
101
+ toolchain: systemToolchain,
102
+ startDriver: async (lane, at, javaHome) => {
103
+ await prewarmEngine({
104
+ stateDir: at,
105
+ lane,
106
+ env: process.env,
107
+ ...(javaHome === undefined ? {} : { javaHome }),
108
+ progress: line => process.stderr.write(`${TOOL} up: driver: ${line}\n`),
109
+ });
110
+ },
111
+ clock: systemClock,
112
+ onCancel,
113
+ passBy: passingBy,
114
+ });
115
+ if (command === SWEEP_COMMAND) {
116
+ if (rest.length > 0) {
117
+ process.stderr.write(`${TOOL} ${SWEEP_COMMAND} takes no arguments\n`);
118
+ return 2;
119
+ }
120
+ await runSweep({
121
+ dir: state,
122
+ env: process.env,
123
+ processes: systemProcesses,
124
+ system: realServiceSystem,
125
+ clock: systemClock,
126
+ devices,
127
+ sim,
128
+ ports: tcpProbe,
129
+ log: line => process.stderr.write(`${new Date().toISOString()} ${line}\n`),
130
+ });
131
+ return 0;
132
+ }
83
133
  if (REGISTRY_COMMANDS.includes(command)) {
84
134
  return runRegistryCommand(command, rest, {
85
135
  env: process.env,
@@ -92,11 +142,12 @@ async function main(args) {
92
142
  onCancel,
93
143
  sim,
94
144
  system: realServiceSystem,
145
+ passBy: passingBy,
95
146
  });
96
147
  }
97
148
  if (HOOK_COMMANDS.includes(command)) {
98
149
  const worker = process.env[SESSION_END_OWNER_ENV] !== undefined;
99
- return runHookCommand(rest, {
150
+ const code = await runHookCommand(rest, {
100
151
  env: process.env,
101
152
  cwd: process.cwd(),
102
153
  stdout: text => process.stdout.write(text),
@@ -109,29 +160,12 @@ async function main(args) {
109
160
  stdin: worker || process.stdin.isTTY ? '' : fs.readFileSync(0, 'utf8'),
110
161
  startWorker: detachedWorker(process.argv[1] ?? ''),
111
162
  });
163
+ if (worker)
164
+ passingBy();
165
+ return code;
112
166
  }
113
167
  if (UP_COMMANDS.includes(command)) {
114
- return runUpCommand(command, rest, {
115
- env: process.env,
116
- cwd: process.cwd(),
117
- log: line => process.stderr.write(`${line}\n`),
118
- processes: systemProcesses,
119
- ports: tcpProbe,
120
- devices,
121
- sim,
122
- system: realServiceSystem,
123
- toolchain: systemToolchain,
124
- startDriver: async (lane, state) => {
125
- await prewarmEngine({
126
- stateDir: state,
127
- lane,
128
- env: process.env,
129
- progress: line => process.stderr.write(`${TOOL} up: driver: ${line}\n`),
130
- });
131
- },
132
- clock: systemClock,
133
- onCancel,
134
- }, text => process.stdout.write(text));
168
+ return runUpCommand(command, rest, envDeps(), text => process.stdout.write(text));
135
169
  }
136
170
  if (DRIVER_COMMANDS.includes(command)) {
137
171
  return runDriverCommand(command, rest, {
@@ -142,6 +176,12 @@ async function main(args) {
142
176
  stdin: process.stdin,
143
177
  output: process.stdout,
144
178
  processes: systemProcesses,
179
+ wake: upWaker(envDeps()),
180
+ sweep: {
181
+ run: () => {
182
+ sweepTick({ dir: state }, devices, startSweep);
183
+ },
184
+ },
145
185
  });
146
186
  }
147
187
  if (AGENT_COMMANDS.includes(command)) {
@@ -5,3 +5,9 @@ export class DeviceBackendError extends Error {
5
5
  this.name = 'DeviceBackendError';
6
6
  }
7
7
  }
8
+ export class DeviceRunningError extends DeviceBackendError {
9
+ constructor(id) {
10
+ super(`${id} is running, so it is not deleted`);
11
+ this.name = 'DeviceRunningError';
12
+ }
13
+ }