agentlane 0.0.0 → 0.4.0-rc.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 (146) hide show
  1. package/CHANGELOG.md +118 -0
  2. package/LICENSE +27 -0
  3. package/README.md +167 -2
  4. package/dist/agents/claude-plugin.js +79 -0
  5. package/dist/agents/cli.js +258 -0
  6. package/dist/agents/detect.js +88 -0
  7. package/dist/agents/init.js +251 -0
  8. package/dist/agents/launcher.js +32 -0
  9. package/dist/agents/package.js +119 -0
  10. package/dist/agents/skills.js +59 -0
  11. package/dist/agents/toml.js +325 -0
  12. package/dist/agents/wiring-check.js +117 -0
  13. package/dist/agents/wiring.js +374 -0
  14. package/dist/bench/host.js +104 -0
  15. package/dist/bench/live.js +511 -0
  16. package/dist/bench/mcp-client.js +91 -0
  17. package/dist/bench/model.js +15 -0
  18. package/dist/bench/report.js +162 -0
  19. package/dist/bench/run.js +121 -0
  20. package/dist/bench/schedule.js +140 -0
  21. package/dist/bench/stats.js +59 -0
  22. package/dist/bench-cli.js +167 -0
  23. package/dist/browser/aria.js +184 -0
  24. package/dist/browser/control.js +203 -0
  25. package/dist/browser/env.js +118 -0
  26. package/dist/browser/evidence.js +44 -0
  27. package/dist/browser/install.js +361 -0
  28. package/dist/browser/kinds.js +10 -0
  29. package/dist/browser/lane-browser.js +224 -0
  30. package/dist/browser/libraries.js +56 -0
  31. package/dist/browser/paths.js +29 -0
  32. package/dist/browser/release.js +121 -0
  33. package/dist/browser/screen.js +19 -0
  34. package/dist/browser/signin.js +154 -0
  35. package/dist/browser/stop.js +163 -0
  36. package/dist/browser/tools.js +25 -0
  37. package/dist/cache/build.js +154 -0
  38. package/dist/cache/config.js +186 -0
  39. package/dist/cache/index.js +4 -0
  40. package/dist/cache/key.js +196 -0
  41. package/dist/cache/store.js +303 -0
  42. package/dist/cli.js +178 -0
  43. package/dist/clock.js +4 -0
  44. package/dist/devices/adb.js +160 -0
  45. package/dist/devices/android.js +214 -0
  46. package/dist/devices/backend.js +7 -0
  47. package/dist/devices/bundler.js +46 -0
  48. package/dist/devices/common.js +51 -0
  49. package/dist/devices/controls.js +65 -0
  50. package/dist/devices/dial.js +56 -0
  51. package/dist/devices/emulator.js +396 -0
  52. package/dist/devices/hygiene.js +202 -0
  53. package/dist/devices/index.js +5 -0
  54. package/dist/devices/launch.js +76 -0
  55. package/dist/devices/simctl.js +119 -0
  56. package/dist/devices/tools.js +1 -0
  57. package/dist/devices/variant.js +99 -0
  58. package/dist/devices/xcrun.js +265 -0
  59. package/dist/driver/absence.js +54 -0
  60. package/dist/driver/caller.js +51 -0
  61. package/dist/driver/client.js +312 -0
  62. package/dist/driver/diff.js +180 -0
  63. package/dist/driver/elements.js +70 -0
  64. package/dist/driver/engine-main.js +13 -0
  65. package/dist/driver/engine.js +499 -0
  66. package/dist/driver/errors.js +11 -0
  67. package/dist/driver/exec.js +66 -0
  68. package/dist/driver/failure.js +156 -0
  69. package/dist/driver/flow.js +94 -0
  70. package/dist/driver/idle.js +100 -0
  71. package/dist/driver/images.js +68 -0
  72. package/dist/driver/install.js +272 -0
  73. package/dist/driver/lock.js +101 -0
  74. package/dist/driver/look.js +223 -0
  75. package/dist/driver/maestro-child.js +194 -0
  76. package/dist/driver/maestro-runs.js +46 -0
  77. package/dist/driver/paths.js +105 -0
  78. package/dist/driver/pixels.js +144 -0
  79. package/dist/driver/plan.js +108 -0
  80. package/dist/driver/release.js +7 -0
  81. package/dist/driver/runner.js +42 -0
  82. package/dist/driver/selector.js +91 -0
  83. package/dist/driver/session.js +474 -0
  84. package/dist/driver/suggest.js +86 -0
  85. package/dist/driver/supervisor.js +251 -0
  86. package/dist/driver/tree.js +426 -0
  87. package/dist/driver-cli.js +865 -0
  88. package/dist/evidence/device.js +177 -0
  89. package/dist/evidence/evidence.js +304 -0
  90. package/dist/evidence/files.js +92 -0
  91. package/dist/hooks/env.js +81 -0
  92. package/dist/hooks/run.js +101 -0
  93. package/dist/mcp/server.js +69 -0
  94. package/dist/mcp/tools.js +246 -0
  95. package/dist/name.js +22 -0
  96. package/dist/plugin/aims.js +465 -0
  97. package/dist/plugin/conventions.js +73 -0
  98. package/dist/plugin/guard.js +163 -0
  99. package/dist/plugin/hook-cli.js +216 -0
  100. package/dist/plugin/session-name.js +28 -0
  101. package/dist/plugin/shell.js +254 -0
  102. package/dist/processes.js +77 -0
  103. package/dist/project/config.js +316 -0
  104. package/dist/project/schema.js +335 -0
  105. package/dist/project/validate.js +224 -0
  106. package/dist/registry/config.js +142 -0
  107. package/dist/registry/format.js +22 -0
  108. package/dist/registry/owner.js +265 -0
  109. package/dist/registry/paths.js +50 -0
  110. package/dist/registry/ports.js +26 -0
  111. package/dist/registry/prune.js +160 -0
  112. package/dist/registry/refusal.js +53 -0
  113. package/dist/registry/registry.js +1027 -0
  114. package/dist/registry/resources.js +117 -0
  115. package/dist/registry/rows.js +115 -0
  116. package/dist/registry/status.js +85 -0
  117. package/dist/registry/store.js +274 -0
  118. package/dist/registry/types.js +1 -0
  119. package/dist/registry-cli.js +544 -0
  120. package/dist/services/services.js +268 -0
  121. package/dist/services/system.js +189 -0
  122. package/dist/up/deps.js +1 -0
  123. package/dist/up/doctor.js +424 -0
  124. package/dist/up/exec.js +133 -0
  125. package/dist/up/lane-files.js +75 -0
  126. package/dist/up/lane-services.js +38 -0
  127. package/dist/up/maestro-env.js +172 -0
  128. package/dist/up/preflight.js +232 -0
  129. package/dist/up/release.js +148 -0
  130. package/dist/up/resources.js +178 -0
  131. package/dist/up/restart.js +87 -0
  132. package/dist/up/setup.js +28 -0
  133. package/dist/up/up.js +610 -0
  134. package/dist/up-cli.js +258 -0
  135. package/dist/usage.js +16 -0
  136. package/package.json +58 -3
  137. package/schema/agentlane.schema.json +675 -0
  138. package/skills/create-verification-skill/LICENSE +21 -0
  139. package/skills/create-verification-skill/SKILL.md +88 -0
  140. package/skills/dev-loop/LICENSE +21 -0
  141. package/skills/dev-loop/SKILL.md +61 -0
  142. package/skills/device/SKILL.md +8 -0
  143. package/skills/drive/SKILL.md +64 -0
  144. package/skills/maintain-verification-skill/LICENSE +21 -0
  145. package/skills/maintain-verification-skill/SKILL.md +42 -0
  146. package/skills/setup/SKILL.md +80 -0
@@ -0,0 +1,424 @@
1
+ import { setupFix } from "../agents/skills.js";
2
+ import { agentWiringChecks } from "../agents/wiring-check.js";
3
+ import { browserChecks, hasBrowser } from "../browser/lane-browser.js";
4
+ import { realBrowserTools } from "../browser/tools.js";
5
+ import { stateDir } from "../registry/paths.js";
6
+ import { adbTools } from "../devices/adb.js";
7
+ import { checkAndroidHygiene, describeReverse, REACT_NATIVE_DEVICE_PORT } from "../devices/android.js";
8
+ import { DEVICE_NOUN } from "../devices/backend.js";
9
+ import { bundlerLocation, readBundlerPin } from "../devices/bundler.js";
10
+ import { readDialDefault } from "../devices/dial.js";
11
+ import { checkHygiene } from "../devices/hygiene.js";
12
+ import { MAESTRO_OVERRIDE_ENV, maestroBuild } from "../driver/install.js";
13
+ import { metroUrl } from "../devices/launch.js";
14
+ import { LAUNCH_ARGS_ENV, webUrl } from "../hooks/env.js";
15
+ import { judgeAndroidVariant, judgeIosVariant, readIosBundle, } from "../devices/variant.js";
16
+ import { CLI } from "../name.js";
17
+ import { asMobile } from "../project/config.js";
18
+ import { AGENTS, describeOwner, describeState } from "../registry/owner.js";
19
+ import { explainInspection, inspectService, listenersElsewhere } from "../services/services.js";
20
+ import { toolchainFindings } from "./preflight.js";
21
+ function variantCheck(verdict, expected) {
22
+ return { name: 'app variant', ok: verdict.ok, detail: verdict.detail, fix: `${UP} installs the ${expected} build again` };
23
+ }
24
+ const UP = `${CLI} up`;
25
+ function message(error) {
26
+ return error instanceof Error ? error.message : String(error);
27
+ }
28
+ export function dialArguments(config, lane) {
29
+ return config.services.flatMap(service => {
30
+ const port = lane.ports.find(p => p.service === service.name)?.port;
31
+ return service.dial && port !== undefined
32
+ ? [service.dial.argument, service.dial.value.replaceAll('{port}', String(port))]
33
+ : [];
34
+ });
35
+ }
36
+ export function bundlerPort(config, lane) {
37
+ return config.app.bundler === null ? undefined : lane.ports.find(p => p.service === config.app.bundler)?.port;
38
+ }
39
+ /** The emulator ports `up` forwards to the Mac: the bundler, and every service the app dials, each on its own port. */
40
+ export function reversePorts(config, lane) {
41
+ const wanted = new Map();
42
+ const bundler = bundlerPort(config, lane);
43
+ if (bundler !== undefined)
44
+ wanted.set(config.app.launch === 'bundler-pin' ? REACT_NATIVE_DEVICE_PORT : bundler, bundler);
45
+ for (const service of config.services) {
46
+ const port = lane.ports.find(p => p.service === service.name)?.port;
47
+ if (service.dial && port !== undefined)
48
+ wanted.set(port, port);
49
+ }
50
+ return wanted;
51
+ }
52
+ async function androidAppChecks(adb, config, lane) {
53
+ const { bundleId } = config.app;
54
+ const checks = [];
55
+ const installed = await adb.appInstalled(bundleId);
56
+ checks.push({ name: 'app installed', ok: installed, detail: `${bundleId} ${installed ? 'is' : 'is not'} installed`, fix: UP });
57
+ if (!installed)
58
+ return checks;
59
+ checks.push(variantCheck(judgeAndroidVariant(bundleId, await adb.debuggable(bundleId), config.app.variant), config.app.variant));
60
+ const running = await adb.appRunning(bundleId);
61
+ checks.push({ name: 'app running', ok: running, detail: `${bundleId} ${running ? 'is' : 'is not'} running`, fix: UP });
62
+ const wanted = reversePorts(config, lane);
63
+ if (wanted.size > 0) {
64
+ const rules = await adb.reverses();
65
+ const missing = [...wanted].filter(([devicePort, hostPort]) => rules.get(devicePort) !== hostPort);
66
+ checks.push({
67
+ name: 'device network',
68
+ ok: missing.length === 0,
69
+ detail: missing.length === 0
70
+ ? `adb reverse forwards ${describeReverse(wanted)}`
71
+ : `adb reverse lacks ${describeReverse(new Map(missing))}, so the app can't reach those servers`,
72
+ fix: UP,
73
+ });
74
+ }
75
+ return checks;
76
+ }
77
+ async function androidDeviceChecks(deps, config, lane) {
78
+ const device = lane.device;
79
+ if (!device?.serial) {
80
+ return [
81
+ {
82
+ name: 'device',
83
+ ok: false,
84
+ detail: `the lane's emulator ${device?.name ?? ''} is not running`,
85
+ fix: `${CLI} release, then ${UP}`,
86
+ },
87
+ ];
88
+ }
89
+ const adb = adbTools(deps, device.serial);
90
+ const booted = await adb.bootCompleted();
91
+ const checks = [
92
+ {
93
+ name: 'device',
94
+ ok: booted,
95
+ detail: `${device.name} (${device.deviceTypeName}, ${device.runtimeName}, ${device.serial}) is ${booted ? 'booted' : 'not booted'}`,
96
+ fix: UP,
97
+ },
98
+ ];
99
+ if (!booted)
100
+ return checks;
101
+ try {
102
+ checks.push(...(await androidAppChecks(adb, config, lane)));
103
+ }
104
+ catch (error) {
105
+ checks.push({ name: 'app', ok: false, detail: message(error), fix: UP });
106
+ }
107
+ return checks;
108
+ }
109
+ async function androidHygieneCheck(deps, config, serial) {
110
+ try {
111
+ const hygiene = await checkAndroidHygiene(adbTools(deps, serial), config.hygiene);
112
+ const off = hygiene.steps.filter(s => !s.ok);
113
+ return {
114
+ name: 'hygiene',
115
+ ok: hygiene.ok,
116
+ detail: hygiene.ok
117
+ ? hygiene.steps.map(s => `${s.step} ${s.got}`).join('; ')
118
+ : off.map(s => `${s.step} reads ${s.got}, not ${s.wanted}`).join('; '),
119
+ fix: UP,
120
+ };
121
+ }
122
+ catch (error) {
123
+ return { name: 'hygiene', ok: false, detail: message(error), fix: UP };
124
+ }
125
+ }
126
+ async function appChecks(sim, config, lane, udid, built) {
127
+ const { bundleId } = config.app;
128
+ const checks = [];
129
+ const installed = await sim.appInstalled(udid, bundleId);
130
+ checks.push({ name: 'app installed', ok: installed, detail: `${bundleId} ${installed ? 'is' : 'is not'} installed`, fix: UP });
131
+ if (!installed)
132
+ return checks;
133
+ const container = await sim.appContainer(udid, bundleId);
134
+ if (container !== undefined) {
135
+ checks.push(variantCheck(judgeIosVariant(readIosBundle(container), config.app.variant, built), config.app.variant));
136
+ }
137
+ const running = await sim.appRunning(udid, bundleId);
138
+ checks.push({ name: 'app running', ok: running, detail: `${bundleId} ${running ? 'is' : 'is not'} running`, fix: UP });
139
+ if (!running)
140
+ return checks;
141
+ const commandLine = await sim.appCommandLine(udid, bundleId);
142
+ const args = commandLine ?? '';
143
+ const has = (words) => commandLine !== undefined && ` ${args} `.includes(` ${words.join(' ')} `);
144
+ const port = bundlerPort(config, lane);
145
+ if (config.app.launch === 'dev-client' && port !== undefined) {
146
+ const wanted = ['--initialUrl', metroUrl(port)];
147
+ checks.push({
148
+ name: 'launch',
149
+ ok: has(wanted),
150
+ detail: has(wanted)
151
+ ? `the dev client was launched with ${wanted.join(' ')}`
152
+ : `the app's arguments are '${args}', without ${wanted.join(' ')}`,
153
+ fix: UP,
154
+ });
155
+ }
156
+ if (config.app.launch === 'bundler-pin' && port !== undefined) {
157
+ const wanted = bundlerLocation(port);
158
+ const pin = await readBundlerPin(sim, udid, bundleId);
159
+ checks.push({
160
+ name: 'bundler pin',
161
+ ok: pin === wanted,
162
+ detail: pin === wanted ? `RCT_jsLocation reads back ${pin}` : `RCT_jsLocation reads '${pin ?? 'nothing'}', not ${wanted}`,
163
+ fix: UP,
164
+ });
165
+ }
166
+ const dial = dialArguments(config, lane);
167
+ for (let i = 0; i < dial.length; i += 2) {
168
+ const pair = [dial[i] ?? '', dial[i + 1] ?? ''];
169
+ const key = pair[0]?.replace(/^-+/, '') ?? '';
170
+ const launched = has(pair);
171
+ const kept = !launched && config.app.launch === 'bundler-pin' && (await readDialDefault(sim, udid, bundleId, key)) === pair[1];
172
+ checks.push({
173
+ name: `dials ${key}`,
174
+ ok: launched || kept,
175
+ detail: launched
176
+ ? `the app was launched with ${pair.join(' ')}`
177
+ : kept
178
+ ? `the app was relaunched without arguments, and its defaults hold ${key} = ${pair[1] ?? ''}`
179
+ : `the app's arguments are '${args}', without ${pair.join(' ')}`,
180
+ fix: config.app.launch === 'hook' ? `pass $${LAUNCH_ARGS_ENV} to the app in the launch hook, then ${UP}` : UP,
181
+ });
182
+ }
183
+ return checks;
184
+ }
185
+ export function describeMaestroBuild(build) {
186
+ switch (build.kind) {
187
+ case 'release':
188
+ return `the pinned release ${build.version} (sha256 ${build.sha256.slice(0, 12)}…), in ${build.home}`;
189
+ case 'not-installed':
190
+ return `the pinned release ${build.version}, not installed yet: the first look downloads it, or run ${CLI} driver install`;
191
+ case 'override': {
192
+ const from = `${MAESTRO_OVERRIDE_ENV}=${build.binary}`;
193
+ if (build.version === undefined)
194
+ return `NOT the release: ${from}, whose version can't be read`;
195
+ if (build.local)
196
+ return `NOT the release: a local build, ${build.version}, from ${from} (the release is ${build.release})`;
197
+ if (build.version === build.release)
198
+ return `NOT the pinned install: ${from} says ${build.version}, but nothing checked it against the release's sha256`;
199
+ return `NOT the release: ${from} is ${build.version} (the release is ${build.release})`;
200
+ }
201
+ }
202
+ }
203
+ function wiringChecks(config, folder, env) {
204
+ const wired = agentWiringChecks(config?.dir ?? folder, env);
205
+ if (wired.length > 0 || !config)
206
+ return wired;
207
+ return [
208
+ {
209
+ name: 'wiring',
210
+ ok: true,
211
+ warning: true,
212
+ detail: "no agent's skills, MCP server or hooks are set up in this project",
213
+ fix: setupFix(`run ${CLI} init --agents with the ones you use (${AGENTS.join(',')})`),
214
+ },
215
+ ];
216
+ }
217
+ export async function diagnose(deps, input) {
218
+ const checks = [];
219
+ const { config, lane } = input;
220
+ checks.push(config
221
+ ? { name: 'config', ok: true, detail: config.file, fix: '' }
222
+ : {
223
+ name: 'config',
224
+ ok: false,
225
+ detail: input.configProblem ?? 'no config',
226
+ fix: setupFix(`fix it by hand ('${CLI} schema' prints its JSON Schema)`),
227
+ });
228
+ checks.push(...wiringChecks(config, input.folder, deps.env));
229
+ const asked = input.platform;
230
+ const held = lane?.device;
231
+ const mismatch = asked !== undefined && asked !== 'web' && held && held.platform !== asked ? { asked, held } : undefined;
232
+ if (!lane) {
233
+ checks.push({ name: 'lane', ok: false, detail: `this session holds no lane in ${input.folder}`, fix: UP });
234
+ }
235
+ else if (mismatch) {
236
+ checks.push({
237
+ name: 'lane',
238
+ ok: false,
239
+ detail: `this folder's lane ${lane.id} holds ${mismatch.held.name}, ${DEVICE_NOUN[mismatch.held.platform]}, and doctor is checking ${mismatch.asked}`,
240
+ fix: `${CLI} release here, then ${UP} --platform ${mismatch.asked}; or run ${CLI} doctor --platform ${mismatch.held.platform}`,
241
+ });
242
+ }
243
+ else {
244
+ const alive = input.ownerState === 'alive';
245
+ checks.push({
246
+ name: 'lane',
247
+ ok: alive,
248
+ detail: `lane ${lane.id} held by ${describeOwner(lane.owner)} in ${lane.worktree}${alive ? '' : `: ${describeState(input.ownerState ?? 'cant-tell')}`}`,
249
+ fix: UP,
250
+ });
251
+ }
252
+ const mobile = config && asMobile(config);
253
+ if (config) {
254
+ const preflight = await toolchainFindings(config, { env: deps.env, toolchain: deps.toolchain, backend: mobile ? deps.devices[mobile.platform] : undefined }, false);
255
+ checks.push(...preflight.findings);
256
+ if (config.driver && mobile) {
257
+ checks.push({ name: 'Maestro', ok: true, detail: describeMaestroBuild(await maestroBuild(deps.env)), fix: '' });
258
+ }
259
+ }
260
+ if (!config || !lane || mismatch)
261
+ return finish(lane, checks);
262
+ checks.push(...resourceChecks(config, lane));
263
+ const tools = deps.browser ?? realBrowserTools;
264
+ if (hasBrowser(lane))
265
+ checks.push(...(await browserChecks(tools, stateDir(deps.env), lane, deps.env)));
266
+ else if (!mobile && config.web) {
267
+ checks.push({
268
+ name: 'browser',
269
+ ok: false,
270
+ detail: `lane ${lane.id} holds no browser`,
271
+ fix: `${UP} (it claims one and opens the web app in it)`,
272
+ });
273
+ }
274
+ if (!mobile) {
275
+ checks.push(...(await serviceChecks(deps, config, lane)));
276
+ checks.push(await webAppCheck(tools, config, lane, checks));
277
+ return finish(lane, checks);
278
+ }
279
+ if (lane.device?.platform === 'android') {
280
+ const deviceChecks = await androidDeviceChecks(deps, mobile, lane);
281
+ checks.push(...deviceChecks);
282
+ checks.push(...(await serviceChecks(deps, config, lane)));
283
+ const serial = lane.device.serial;
284
+ if (serial && deviceChecks[0]?.ok)
285
+ checks.push(await androidHygieneCheck(deps, mobile, serial));
286
+ return finish(lane, checks);
287
+ }
288
+ const udid = lane.device?.id;
289
+ const sim = deps.sim;
290
+ if (!udid || !sim) {
291
+ checks.push({ name: 'device', ok: false, detail: 'the lane holds no simulator', fix: `${CLI} release, then ${UP}` });
292
+ return finish(lane, checks);
293
+ }
294
+ const device = await sim.device(udid);
295
+ const booted = device?.state === 'Booted';
296
+ checks.push({
297
+ name: 'device',
298
+ ok: booted,
299
+ detail: device
300
+ ? `${device.name} (${lane.device?.deviceTypeName ?? ''}, ${lane.device?.runtimeName ?? ''}, ${udid}) is ${device.state}`
301
+ : `simulator ${udid} no longer exists`,
302
+ fix: UP,
303
+ });
304
+ if (booted) {
305
+ try {
306
+ checks.push(...(await appChecks(sim, mobile, lane, udid, input.installed)));
307
+ }
308
+ catch (error) {
309
+ checks.push({ name: 'app', ok: false, detail: message(error), fix: UP });
310
+ }
311
+ }
312
+ checks.push(...(await serviceChecks(deps, config, lane)));
313
+ if (booted) {
314
+ try {
315
+ const hygiene = await checkHygiene(sim, udid, config.hygiene);
316
+ const off = hygiene.steps.filter(s => !s.ok);
317
+ checks.push({
318
+ name: 'hygiene',
319
+ ok: hygiene.ok,
320
+ detail: hygiene.ok
321
+ ? hygiene.steps.map(s => `${s.step} ${s.got}`).join('; ')
322
+ : off.map(s => `${s.step} reads ${s.got}, not ${s.wanted}`).join('; '),
323
+ fix: UP,
324
+ });
325
+ }
326
+ catch (error) {
327
+ checks.push({ name: 'hygiene', ok: false, detail: message(error), fix: UP });
328
+ }
329
+ }
330
+ return finish(lane, checks);
331
+ }
332
+ async function serviceChecks(deps, config, lane) {
333
+ const checks = [];
334
+ for (const service of config.services) {
335
+ const port = lane.ports.find(p => p.service === service.name)?.port;
336
+ if (port === undefined) {
337
+ checks.push({ name: `service ${service.name}`, ok: false, detail: 'the lane has no port for it', fix: UP });
338
+ continue;
339
+ }
340
+ const recorded = lane.processes.find(p => p.service === service.name) ?? null;
341
+ const seen = await inspectService(deps.system, service, port, recorded);
342
+ const foreign = seen.listeners?.filter(l => !l.ours) ?? [];
343
+ const elsewhere = listenersElsewhere(deps.system, seen.listeners ?? [], lane.worktree);
344
+ const code = elsewhere.length === 0
345
+ ? ''
346
+ : `; ${elsewhere.map(l => `the listener pid ${String(l.pid)} runs in ${l.cwd ?? '(unknown)'}`).join(', ')}, outside this worktree ${lane.worktree}, so ${String(port)} serves that folder's code`;
347
+ checks.push({
348
+ name: `service ${service.name}`,
349
+ ok: seen.healthy && elsewhere.length === 0,
350
+ detail: `${explainInspection(seen)}${code}`,
351
+ fix: foreign.length > 0
352
+ ? `stop ${foreign.map(l => `pid ${String(l.pid)}`).join(', ')}, then ${UP}`
353
+ : elsewhere.length > 0
354
+ ? `make services.${service.name}.start run this worktree's code (its cwd and the paths it starts), then ${CLI} restart ${service.name}`
355
+ : `${UP} (it restarts the service)`,
356
+ });
357
+ }
358
+ return checks;
359
+ }
360
+ function resourceChecks(config, lane) {
361
+ const declared = config.resources.map(r => r.name);
362
+ const names = [...new Set([...declared, ...lane.resources.map(r => r.name)])];
363
+ return names.map(name => {
364
+ const held = lane.resources.find(r => r.name === name);
365
+ const exported = held?.exports ? Object.keys(held.exports) : [];
366
+ if (!held) {
367
+ return {
368
+ name: `resource ${name}`,
369
+ ok: false,
370
+ detail: `lane ${lane.id} has no ${name}: ${config.file} declared it after up last ran`,
371
+ fix: `${UP} (it creates it)`,
372
+ };
373
+ }
374
+ if (!held.exports) {
375
+ return {
376
+ name: `resource ${name}`,
377
+ ok: false,
378
+ detail: "its create failed or did not finish, so the lane's commands lack its values",
379
+ fix: `${UP} (it runs create again; the create log is in the lane's resources folder)`,
380
+ };
381
+ }
382
+ const kept = declared.includes(name)
383
+ ? ''
384
+ : `; ${config.file} no longer declares it, and its teardown runs when the lane ends`;
385
+ return {
386
+ name: `resource ${name}`,
387
+ ok: true,
388
+ detail: `created for lane ${lane.id}${exported.length > 0 ? `, exports ${exported.join(', ')}` : ''}${kept}`,
389
+ fix: '',
390
+ };
391
+ });
392
+ }
393
+ async function webAppCheck(tools, config, lane, checks) {
394
+ const url = webUrl(config, lane);
395
+ const service = config.web?.service ?? '';
396
+ const served = checks.find(c => c.name === `service ${service}`)?.ok === true;
397
+ if (url === null)
398
+ return { name: 'web app', ok: false, detail: `the lane has no port for ${service}`, fix: UP };
399
+ if (!served)
400
+ return { name: 'web app', ok: false, detail: `${url}, but ${service} is not healthy`, fix: UP };
401
+ const answer = await tools.probe(url);
402
+ return {
403
+ name: 'web app',
404
+ ok: answer !== null && answer.status < 500,
405
+ detail: answer === null
406
+ ? `${url} does not answer`
407
+ : answer.status === 200
408
+ ? url
409
+ : `${url} answers HTTP ${String(answer.status)}${answer.status < 500 ? '' : `, a server error`}`,
410
+ fix: answer === null ? UP : `read the log of ${service} in the lane's log folder`,
411
+ };
412
+ }
413
+ function finish(lane, checks) {
414
+ return { ok: checks.every(c => c.ok), lane: lane?.id ?? null, checks };
415
+ }
416
+ export function renderDoctor(report) {
417
+ const lines = report.checks.map(c => {
418
+ const head = `${c.ok ? (c.warning ? 'warn' : 'ok ') : 'FAIL'} ${c.name}: ${c.detail}`;
419
+ return c.ok && !c.warning ? head : `${head}\n fix: ${c.fix}`;
420
+ });
421
+ const failed = report.checks.filter(c => !c.ok).length;
422
+ lines.push(failed === 0 ? 'ready' : `not ready: ${String(failed)} check${failed === 1 ? '' : 's'} failed`);
423
+ return `${lines.join('\n')}\n`;
424
+ }
@@ -0,0 +1,133 @@
1
+ import { spawn } from 'node:child_process';
2
+ import os from 'node:os';
3
+ import path from 'node:path';
4
+ import { laneBrowser, hasBrowser } from "../browser/lane-browser.js";
5
+ import { keepFailureEvidence } from "../browser/evidence.js";
6
+ import { realBrowserTools } from "../browser/tools.js";
7
+ import { adbTools } from "../devices/adb.js";
8
+ import { describeRestored, restoreReverse } from "../devices/android.js";
9
+ import { laneEnvironment, webUrl } from "../hooks/env.js";
10
+ import { CLI, TOOL } from "../name.js";
11
+ import { asMobile, findProjectConfig, loadLaneConfig } from "../project/config.js";
12
+ import { describeOwner, resolveOwner } from "../registry/owner.js";
13
+ import { stateDir } from "../registry/paths.js";
14
+ import { NotClaimed, UsageError } from "../registry/refusal.js";
15
+ import { Registry } from "../registry/registry.js";
16
+ import { resolveFolder } from "../registry-cli.js";
17
+ import { laneDir, readUpRecord } from "./lane-files.js";
18
+ import { reversePorts } from "./doctor.js";
19
+ import { driverVariables } from "./maestro-env.js";
20
+ function lenientConfig(file, platform, log) {
21
+ if (!file)
22
+ return { config: null, problem: null };
23
+ try {
24
+ return { config: loadLaneConfig(file, platform), problem: null };
25
+ }
26
+ catch (error) {
27
+ const problem = error.message;
28
+ log(`${TOOL} exec: only the lane's own variables are set, and maestro refuses to run, because ${problem}`);
29
+ return { config: null, problem };
30
+ }
31
+ }
32
+ /**
33
+ * Puts back the adb reverse rules up set on the lane's emulator. A Maestro session's close can reset the emulator's adb
34
+ * transport, so the next command would run against an app that can't reach its bundler or API.
35
+ */
36
+ async function restoreDeviceNetwork(deps, config, lane, dir) {
37
+ const serial = lane.device?.platform === 'android' ? lane.device.serial : null;
38
+ if (!config || !serial || (readUpRecord(dir)?.reversed ?? []).length === 0)
39
+ return;
40
+ const wanted = reversePorts(config, lane);
41
+ if (wanted.size === 0)
42
+ return;
43
+ try {
44
+ const restored = await restoreReverse(adbTools(deps, serial), wanted, 'since the last command');
45
+ if (restored.size > 0)
46
+ deps.log(`${TOOL} exec: ${describeRestored(restored, serial, 'before the command')}`);
47
+ }
48
+ catch (error) {
49
+ deps.log(`${TOOL} exec: could not restore adb reverse on ${serial} (${error.message.split('\n')[0] ?? ''}); ${CLI} doctor names the fix`);
50
+ }
51
+ }
52
+ export async function execInLane(deps, argv) {
53
+ const [command, ...args] = argv;
54
+ if (command === undefined)
55
+ throw new UsageError('exec needs a command', `${CLI} exec -- <command> [args...]`);
56
+ const folder = resolveFolder(deps.cwd);
57
+ const owner = resolveOwner(deps.env, deps.processes, deps.selfPid);
58
+ const state = stateDir(deps.env);
59
+ const registry = Registry.open({
60
+ dir: state,
61
+ processes: deps.processes,
62
+ ports: deps.ports,
63
+ devices: deps.devices,
64
+ clock: deps.clock,
65
+ });
66
+ let lane;
67
+ try {
68
+ lane = registry.find(owner, folder.worktree);
69
+ if (!lane) {
70
+ throw new NotClaimed(`${describeOwner(owner)} holds no lane in ${folder.worktree}`, `${CLI} up (or ${CLI} claim)`);
71
+ }
72
+ registry.touch({ id: lane.id, token: lane.token }, `exec: ${command}`);
73
+ }
74
+ finally {
75
+ registry.close();
76
+ }
77
+ const dir = laneDir(state, lane.id);
78
+ const record = readUpRecord(dir);
79
+ const { config, problem } = lenientConfig(record?.config ?? findProjectConfig(deps.cwd), record?.platform ?? lane.device?.platform, deps.log);
80
+ let env = laneEnvironment(deps.env, lane, config, dir, await driverVariables(deps, config, lane, dir, problem));
81
+ await restoreDeviceNetwork(deps, config && asMobile(config), lane, dir);
82
+ const browser = hasBrowser(lane)
83
+ ? await laneBrowser({
84
+ tools: deps.browser ?? realBrowserTools,
85
+ stateDir: state,
86
+ lane,
87
+ config,
88
+ url: webUrl(config, lane),
89
+ env: deps.env,
90
+ laneEnv: env,
91
+ cwd: config?.dir ?? deps.cwd,
92
+ log: line => {
93
+ deps.log(`${TOOL} exec: ${line}`);
94
+ },
95
+ })
96
+ : null;
97
+ if (browser)
98
+ env = { ...env, ...browser.vars };
99
+ const code = await run(deps, command, args, env);
100
+ if (code !== 0 && browser && (await browser.control.isOpen().catch(() => false))) {
101
+ const kept = await keepFailureEvidence(browser.control, browser.setting.paths.evidence, [command, ...args].join(' '));
102
+ deps.log(`${TOOL} exec: ${command} exited ${String(code)}; kept the lane's browser as it was in ${kept.dir}: ${kept.files.map(f => path.basename(f)).join(', ') || 'nothing could be read'}${kept.problems.length > 0 ? ` (${kept.problems.join('; ')})` : ''}`);
103
+ }
104
+ return code;
105
+ }
106
+ function run(deps, command, args, env) {
107
+ return new Promise((resolve, reject) => {
108
+ const child = spawn(command, args, { cwd: deps.cwd, env, stdio: 'inherit' });
109
+ const forward = (signal) => {
110
+ child.kill(signal);
111
+ };
112
+ const ignore = () => undefined;
113
+ process.on('SIGINT', ignore);
114
+ process.on('SIGTERM', forward);
115
+ const done = () => {
116
+ process.off('SIGINT', ignore);
117
+ process.off('SIGTERM', forward);
118
+ };
119
+ child.once('error', error => {
120
+ done();
121
+ if (error.code === 'ENOENT') {
122
+ deps.log(`${TOOL} exec: ${command}: command not found`);
123
+ resolve(127);
124
+ }
125
+ else
126
+ reject(error);
127
+ });
128
+ child.once('close', (code, signal) => {
129
+ done();
130
+ resolve(code ?? 128 + (signal ? os.constants.signals[signal] : 0));
131
+ });
132
+ });
133
+ }
@@ -0,0 +1,75 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { sweepMaestroRunLinks } from "../driver/maestro-runs.js";
4
+ import { pruneDriverFiles } from "../driver/paths.js";
5
+ import { lanesRoot } from "../registry/paths.js";
6
+ import { HISTORY_KEEP_MS } from "../registry/registry.js";
7
+ export const LANE_FILES_KEEP_MS = HISTORY_KEEP_MS;
8
+ export function laneDir(stateDir, laneId) {
9
+ if (!/^[0-9a-f]{8}$/.test(laneId))
10
+ throw new Error(`'${laneId}' is not a lane id`);
11
+ return path.join(lanesRoot(stateDir), laneId);
12
+ }
13
+ export function serviceLog(dir, service) {
14
+ return path.join(dir, `${service}.log`);
15
+ }
16
+ export function hookLog(dir, hook) {
17
+ return path.join(dir, 'hooks', `${hook}.log`);
18
+ }
19
+ function recordFile(dir) {
20
+ return path.join(dir, 'up.json');
21
+ }
22
+ export function readUpRecord(dir) {
23
+ try {
24
+ const raw = JSON.parse(fs.readFileSync(recordFile(dir), 'utf8'));
25
+ return raw.version === 1 ? raw : null;
26
+ }
27
+ catch {
28
+ return null;
29
+ }
30
+ }
31
+ export function writeUpRecord(dir, record) {
32
+ fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
33
+ const file = recordFile(dir);
34
+ const tmp = `${file}.${String(process.pid)}.tmp`;
35
+ fs.writeFileSync(tmp, `${JSON.stringify(record, null, 2)}\n`);
36
+ fs.renameSync(tmp, file);
37
+ }
38
+ export function pruneLaneDirs(stateDir, liveIds, now) {
39
+ const root = lanesRoot(stateDir);
40
+ let names;
41
+ try {
42
+ names = fs.readdirSync(root);
43
+ }
44
+ catch {
45
+ return [];
46
+ }
47
+ const removed = [];
48
+ for (const name of names) {
49
+ if (!/^[0-9a-f]{8}$/.test(name) || liveIds.has(name))
50
+ continue;
51
+ const dir = path.join(root, name);
52
+ try {
53
+ if (now - fs.statSync(dir).mtimeMs < LANE_FILES_KEEP_MS)
54
+ continue;
55
+ fs.rmSync(dir, { recursive: true, force: true });
56
+ removed.push(name);
57
+ }
58
+ catch {
59
+ continue;
60
+ }
61
+ }
62
+ return removed;
63
+ }
64
+ /**
65
+ * Prunes everything kept per ended lane (its lane folder and its driver files) after the registry's 7 days, and the
66
+ * links to its Maestro runs that the lane's maestro made where Maestro writes them.
67
+ */
68
+ export function pruneEndedLaneFiles(stateDir, liveIds, now, env) {
69
+ const removed = new Set([
70
+ ...pruneLaneDirs(stateDir, liveIds, now),
71
+ ...pruneDriverFiles(stateDir, liveIds, now, LANE_FILES_KEEP_MS),
72
+ ]);
73
+ sweepMaestroRunLinks(env, lanesRoot(stateDir));
74
+ return [...removed];
75
+ }
@@ -0,0 +1,38 @@
1
+ import { CLI } from "../name.js";
2
+ import { describeListeners, describeStop, listenersOn, ServiceFailed, startService, stopService, waitForService, } from "../services/services.js";
3
+ import { serviceLog } from "./lane-files.js";
4
+ export function claimedPort(lane, service) {
5
+ return lane.ports.find(p => p.service === service)?.port;
6
+ }
7
+ export function recordedProcess(lane, service) {
8
+ return lane.processes.find(p => p.service === service) ?? null;
9
+ }
10
+ export async function stopClaimedService(c, recorded) {
11
+ const outcome = await stopService(c.deps.system, recorded, c.deps.clock);
12
+ const stopped = describeStop(recorded, outcome);
13
+ if (outcome.kind !== 'stopped' && outcome.kind !== 'already-gone') {
14
+ throw new ServiceFailed(recorded.service, `can't be restarted: ${stopped}`, null);
15
+ }
16
+ c.registry.forgetProcess(c.ref, recorded.service);
17
+ return stopped;
18
+ }
19
+ export async function startClaimedService(c, service, port) {
20
+ const { deps } = c;
21
+ const log = serviceLog(c.dir, service.name);
22
+ const foreign = listenersOn(deps.system, port, null);
23
+ if (foreign && foreign.length > 0) {
24
+ throw new ServiceFailed(service.name, `can't start: ${String(port)} is held by a process the toolkit did not start: ${describeListeners(foreign)}. Stop it, or run ${CLI} release and ${CLI} up for a fresh port`, null);
25
+ }
26
+ const proc = startService(deps.system, service, c.env, log);
27
+ c.registry.recordProcess(c.ref, proc);
28
+ try {
29
+ const ready = await waitForService(deps.system, service, port, proc, deps.clock, log);
30
+ c.registry.recordProcess(c.ref, ready);
31
+ return `started on ${String(port)} (pid ${String(proc.pid)}, log ${log})`;
32
+ }
33
+ catch (error) {
34
+ await stopService(deps.system, proc, deps.clock);
35
+ c.registry.forgetProcess(c.ref, service.name);
36
+ throw error;
37
+ }
38
+ }