agentlane 0.5.0 → 0.6.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.
@@ -0,0 +1,321 @@
1
+ import { createHash } from 'node:crypto';
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
4
+ import { rotateLog } from "../disk-limits.js";
5
+ import { hookEnvironment } from "../hooks/env.js";
6
+ import { describeExit, HookFailed, runCheck, runHook, tail } from "../hooks/run.js";
7
+ import { CLI } from "../name.js";
8
+ import { killGroup } from "../processes.js";
9
+ import { formatTime } from "../registry/format.js";
10
+ import { SHARED_READY_RUN_MAX_MS, sharedHoldMs } from "../registry/shared.js";
11
+ /** How often a start polls `ready` while it waits for the server to answer. */
12
+ export const SHARED_READY_POLL_MS = 500;
13
+ /** How often a start looks again while another process starts or stops the shared service. */
14
+ export const SHARED_WAIT_POLL_MS = 250;
15
+ const SHARED_LOG_MAX_BYTES = 1024 * 1024;
16
+ /**
17
+ * The variables of a start's environment recorded with the shared service's commands: PATH, which finds the programs,
18
+ * and those that choose the daemon, context and project a container tool talks to. Every later command of the shared
19
+ * service runs with the start's values of them, and without those the start lacked.
20
+ */
21
+ export const SHARED_START_VARIABLES = [
22
+ 'PATH',
23
+ 'DOCKER_HOST',
24
+ 'DOCKER_CONTEXT',
25
+ 'DOCKER_CONFIG',
26
+ 'DOCKER_CERT_PATH',
27
+ 'DOCKER_TLS_VERIFY',
28
+ 'COMPOSE_FILE',
29
+ 'COMPOSE_PROJECT_NAME',
30
+ 'COMPOSE_PROFILES',
31
+ 'CONTAINER_HOST',
32
+ 'CONTAINER_CONNECTION',
33
+ ];
34
+ export class SharedServiceFailed extends Error {
35
+ service;
36
+ constructor(service, problem) {
37
+ super(`shared service ${service}: ${problem}`);
38
+ this.name = 'SharedServiceFailed';
39
+ this.service = service;
40
+ }
41
+ }
42
+ /** The folder of a repository's shared service logs, which is also where a command whose folder is gone runs. */
43
+ export function sharedDir(stateDir, repository) {
44
+ const hash = createHash('sha256').update(repository).digest('hex').slice(0, 10);
45
+ return path.join(stateDir, 'shared', `${path.basename(repository) || 'root'}-${hash}`);
46
+ }
47
+ export function sharedLog(stateDir, key) {
48
+ return path.join(sharedDir(stateDir, key.repository), `${key.name}.log`);
49
+ }
50
+ /** What a lane's config says to run, with this process's values of SHARED_START_VARIABLES, so a later stop reaches the same server. */
51
+ export function sharedCommands(spec, env) {
52
+ const recorded = {};
53
+ for (const name of SHARED_START_VARIABLES) {
54
+ const value = env[name];
55
+ if (value !== undefined)
56
+ recorded[name] = value;
57
+ }
58
+ return { start: spec.start, stop: spec.stop, ready: spec.ready, cwd: spec.cwd, timeoutMs: spec.timeoutMs, env: recorded };
59
+ }
60
+ function message(error) {
61
+ return error instanceof Error ? error.message : String(error);
62
+ }
63
+ function seconds(ms) {
64
+ return `${String(Math.round(ms / 100) / 10)} s`;
65
+ }
66
+ function isDirectory(dir) {
67
+ try {
68
+ return fs.statSync(dir).isDirectory();
69
+ }
70
+ catch {
71
+ return false;
72
+ }
73
+ }
74
+ function quietly(action) {
75
+ try {
76
+ action();
77
+ }
78
+ catch {
79
+ return;
80
+ }
81
+ }
82
+ function placeOf(deps, stateDir, key, commands) {
83
+ const dir = sharedDir(stateDir, key.repository);
84
+ fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
85
+ const log = sharedLog(stateDir, key);
86
+ rotateLog(log, SHARED_LOG_MAX_BYTES);
87
+ const moved = !isDirectory(commands.cwd);
88
+ const lacked = SHARED_START_VARIABLES.filter(name => commands.env[name] === undefined);
89
+ return { cwd: moved ? dir : commands.cwd, env: hookEnvironment(deps.env, commands.env, lacked), log, moved };
90
+ }
91
+ /** Runs ready once, for at most `budgetMs` and at most SHARED_READY_RUN_MAX_MS. */
92
+ async function answers(commands, place, budgetMs) {
93
+ try {
94
+ const run = await runCheck(commands.ready, {
95
+ cwd: place.cwd,
96
+ env: place.env,
97
+ timeoutMs: Math.min(budgetMs, SHARED_READY_RUN_MAX_MS),
98
+ ownGroup: true,
99
+ });
100
+ return { ok: run.code === 0, output: run.output };
101
+ }
102
+ catch (error) {
103
+ return { ok: false, output: message(error) };
104
+ }
105
+ }
106
+ /**
107
+ * Runs ready from now until it passes or `commands.timeoutMs` runs out. Each run gets what is left of that window, and
108
+ * one that answers after the window ends doesn't count.
109
+ */
110
+ async function readyWithin(deps, commands, place) {
111
+ const deadline = deps.clock.now() + commands.timeoutMs;
112
+ let output = '';
113
+ for (;;) {
114
+ const left = deadline - deps.clock.now();
115
+ if (left <= 0)
116
+ return { ok: false, output };
117
+ const ready = await answers(commands, place, left);
118
+ if (deps.clock.now() > deadline)
119
+ return { ok: false, output: ready.output };
120
+ if (ready.ok)
121
+ return ready;
122
+ output = ready.output;
123
+ const rest = deadline - deps.clock.now();
124
+ if (rest <= 0)
125
+ return { ok: false, output };
126
+ await deps.clock.sleep(Math.min(SHARED_READY_POLL_MS, rest));
127
+ }
128
+ }
129
+ function quoted(output) {
130
+ const lines = output.trimEnd().split('\n').slice(-30);
131
+ return output.trim() === '' ? ' | (no output)' : lines.map(line => ` | ${line}`).join('\n');
132
+ }
133
+ /**
134
+ * Runs the start or stop command of a shared service this process holds, once its shell is recorded on the row, so a
135
+ * process taking over after this one dies waits for it. One that cannot be recorded never runs.
136
+ */
137
+ function runFenced(registry, key, what, command, place, timeoutMs) {
138
+ return runHook(`shared ${key.name} ${what}`, command, {
139
+ ...place,
140
+ timeoutMs,
141
+ beforeRun: pid => {
142
+ registry.noteSharedCommand(key, pid);
143
+ },
144
+ });
145
+ }
146
+ /**
147
+ * Runs the start this process holds: ready first, which passing means the server already runs, else start, then ready
148
+ * until it passes or the timeout runs out. A failed start records the problem, leaving the service recorded as running
149
+ * for the sweep to stop once nothing needs it, and throws. Null when this process lost the start to another meanwhile.
150
+ */
151
+ async function attemptStart(deps, registry, key, commands, place) {
152
+ if ((await answers(commands, place, commands.timeoutMs)).ok)
153
+ return registry.finishSharedStart(key) ? 'found-running' : null;
154
+ deps.log?.(`starting shared service ${key.name}: ${commands.start} (log: ${place.log})`);
155
+ const where = place.moved ? `\nit ran in ${place.cwd}, since its folder ${commands.cwd} is gone` : '';
156
+ const failed = (problem, detail) => {
157
+ registry.failSharedStart(key, problem);
158
+ return new SharedServiceFailed(key.name, `${problem}\n${detail}\nfull log: ${place.log}${where}`);
159
+ };
160
+ try {
161
+ await runFenced(registry, key, 'start', commands.start, place, commands.timeoutMs);
162
+ }
163
+ catch (error) {
164
+ const how = error instanceof HookFailed
165
+ ? `start ${describeExit(error.run)}: ${commands.start}`
166
+ : `start could not run (${message(error)})`;
167
+ throw failed(how, tail(place.log));
168
+ }
169
+ quietly(() => {
170
+ registry.endSharedCommand(key);
171
+ });
172
+ const ready = await readyWithin(deps, commands, place);
173
+ if (!ready.ok) {
174
+ throw failed(`ready did not pass within ${seconds(commands.timeoutMs)} of its start: ${commands.ready}`, quoted(ready.output));
175
+ }
176
+ return registry.finishSharedStart(key) ? 'started' : null;
177
+ }
178
+ async function runStart(deps, registry, key, commands) {
179
+ try {
180
+ return await attemptStart(deps, registry, key, commands, placeOf(deps, registry.dir, key, commands));
181
+ }
182
+ catch (error) {
183
+ if (error instanceof SharedServiceFailed)
184
+ throw error;
185
+ const problem = `its start stopped partway: ${message(error)}`;
186
+ quietly(() => {
187
+ registry.failSharedStart(key, problem);
188
+ });
189
+ throw new SharedServiceFailed(key.name, problem);
190
+ }
191
+ }
192
+ async function bringUp(deps, registry, need, name, spec) {
193
+ const say = deps.log ?? (() => undefined);
194
+ let foundDown = false;
195
+ let waitingOn = null;
196
+ let ending = null;
197
+ for (;;) {
198
+ const claim = registry.beginSharedStart(need, name, spec, foundDown);
199
+ if (claim.kind === 'unknown') {
200
+ throw new SharedServiceFailed(name, `no start of it is recorded for ${claim.key.repository}, so its commands are unknown; ${CLI} up in a worktree of that repository starts it`);
201
+ }
202
+ if (claim.kind === 'running') {
203
+ const place = placeOf(deps, registry.dir, claim.key, claim.commands);
204
+ if ((await answers(claim.commands, place, claim.commands.timeoutMs)).ok)
205
+ return { name, outcome: 'kept', took: null };
206
+ say(`shared service ${name} is recorded as running, but its ready check fails: ${claim.commands.ready}`);
207
+ foundDown = true;
208
+ continue;
209
+ }
210
+ if (claim.kind === 'busy') {
211
+ const { holder, orphan } = claim;
212
+ const doing = claim.state === 'starting' ? 'starting' : 'stopping';
213
+ const what = claim.state === 'starting' ? 'start' : 'stop';
214
+ const left = `the ${what} of shared service ${name} that pid ${String(holder.pid)} left running when it died`;
215
+ const pid = orphan ? orphan.shell.pid : holder.pid;
216
+ const pastHold = deps.clock.now() > claim.since + sharedHoldMs(claim.timeoutMs);
217
+ if (orphan?.overdue) {
218
+ if (ending === pid && pastHold) {
219
+ throw new SharedServiceFailed(name, `the ${what} that pid ${String(holder.pid)} left running when it died still runs as pid ${String(pid)}, past its ${seconds(claim.timeoutMs)} timeout, and could not be ended; kill ${String(pid)} as the user that owns that process, then try again`);
220
+ }
221
+ if (ending !== pid) {
222
+ ending = pid;
223
+ say(`ending ${left}, pid ${String(pid)}: it ran past its ${seconds(claim.timeoutMs)}`);
224
+ }
225
+ killGroup(pid, deps.signal);
226
+ }
227
+ else {
228
+ if (pastHold) {
229
+ throw new SharedServiceFailed(name, `pid ${String(pid)} has been ${doing} it since ${formatTime(claim.since)}, longer than its commands may take; kill ${String(pid)} if that process is hung, then try again`);
230
+ }
231
+ if (waitingOn !== pid) {
232
+ waitingOn = pid;
233
+ say(orphan
234
+ ? `waiting for ${left}, pid ${String(pid)}`
235
+ : `waiting for pid ${String(pid)}, which is ${doing} shared service ${name}`);
236
+ }
237
+ }
238
+ await deps.clock.sleep(SHARED_WAIT_POLL_MS);
239
+ continue;
240
+ }
241
+ if (claim.took) {
242
+ say(`taking shared service ${name} over from pid ${String(claim.took.pid)}, which died while ${claim.took.state} it`);
243
+ }
244
+ const outcome = await runStart(deps, registry, claim.key, claim.commands);
245
+ if (outcome)
246
+ return { name, outcome, took: claim.took };
247
+ }
248
+ }
249
+ /**
250
+ * Brings up the shared services a lane needs, as `up` and a wake do: records `specs`' names as exactly what the lane
251
+ * needs, then, in order, keeps each one that runs and answers, waits for a start or stop another process is running,
252
+ * and starts each one that is stopped, unless its ready already passes. Throws SharedServiceFailed, naming the service
253
+ * and the end of the failed command's output, for the first that does not come up; a failed start runs no stop, and
254
+ * leaves the service for the sweep to stop once nothing needs it.
255
+ */
256
+ export async function startSharedForLane(deps, registry, ref, specs) {
257
+ registry.recordNeeds(ref, specs.map(s => s.name));
258
+ const started = [];
259
+ for (const spec of specs)
260
+ started.push(await bringUp(deps, registry, { lane: ref }, spec.name, sharedCommands(spec, deps.env)));
261
+ return started;
262
+ }
263
+ /**
264
+ * Brings up the shared services a resource's teardown needs, with the commands recorded at their last start, for a
265
+ * teardown this process holds (from `takeTeardowns`), so none stops while the teardown runs. Throws SharedServiceFailed
266
+ * as `startSharedForLane` does.
267
+ */
268
+ export async function startSharedForTeardown(deps, registry, teardown) {
269
+ const { repository } = teardown;
270
+ if (repository === null)
271
+ return [];
272
+ const started = [];
273
+ for (const name of teardown.needs)
274
+ started.push(await bringUp(deps, registry, { repository }, name, null));
275
+ return started;
276
+ }
277
+ /**
278
+ * The sweep's part: stops each shared service no active lane and no running teardown has needed for SHARED_IDLE_MS,
279
+ * with the commands recorded at its last start. A stop whose folder is gone runs in the shared service's log folder. A
280
+ * failed stop leaves it recorded as running with the problem, for a later sweep to try again. The command of a start or
281
+ * stop whose process died is waited for while its shell runs, and its process group ended once that shell runs past its
282
+ * timeout. What the shell leaves running once it exits is the server, which only a stop ends.
283
+ */
284
+ export async function stopUnneededShared(deps, registry) {
285
+ const stopped = [];
286
+ for (const key of registry.sharedStopCandidates()) {
287
+ let claim = registry.beginSharedStop(key);
288
+ if (claim.kind === 'left' && claim.orphan?.overdue) {
289
+ deps.log?.(`shared service ${key.name}: ending pid ${String(claim.orphan.shell.pid)}: ${claim.reason}, past its timeout`);
290
+ killGroup(claim.orphan.shell.pid, deps.signal);
291
+ claim = registry.beginSharedStop(key);
292
+ }
293
+ if (claim.kind === 'left')
294
+ continue;
295
+ const { commands } = claim;
296
+ let place = null;
297
+ let how = null;
298
+ try {
299
+ place = placeOf(deps, registry.dir, key, commands);
300
+ await runFenced(registry, key, 'stop', commands.stop, place, commands.timeoutMs);
301
+ }
302
+ catch (error) {
303
+ how = error instanceof HookFailed ? `stop ${describeExit(error.run)}` : `stop could not run (${message(error)})`;
304
+ }
305
+ const done = { ...key, unneededSince: claim.unneededSince, moved: place?.moved ?? false, took: claim.took };
306
+ if (how === null) {
307
+ registry.finishSharedStop(key);
308
+ stopped.push({ ...done, outcome: 'stopped', problem: null });
309
+ continue;
310
+ }
311
+ registry.failSharedStop(key, `${how}: ${commands.stop}`);
312
+ const where = place?.moved ? ` (it ran in ${place.cwd}, since its folder ${commands.cwd} is gone)` : '';
313
+ const log = place?.log ?? sharedLog(registry.dir, key);
314
+ stopped.push({
315
+ ...done,
316
+ outcome: 'failed',
317
+ problem: `${how}${where}: ${commands.stop}\n${tail(log)}\nfull log: ${log}`,
318
+ });
319
+ }
320
+ return stopped;
321
+ }
package/dist/up/sweep.js CHANGED
@@ -22,7 +22,8 @@ import { leadersAliveAt } from "../services/services.js";
22
22
  import { LANE_FILES_KEEP_MS, laneFolderIds, pruneEndedLaneFiles } from "./lane-files.js";
23
23
  import { attemptId, deadHolder, PARK_RETRY_MS, parkLane } from "./park.js";
24
24
  import { tearDownLane } from "./release.js";
25
- import { tearDownEndedResources } from "./resources.js";
25
+ import { FailedStarts, tearDownEndedResources } from "./resources.js";
26
+ import { stopUnneededShared } from "./shared.js";
26
27
  export const SWEEP_COMMAND = 'sweep';
27
28
  /** How long after starting a sweeper the commands passing by leave the sweep to it. */
28
29
  export const SWEEP_RESPAWN_MS = 2 * 60_000;
@@ -109,7 +110,8 @@ function reapDue(lane, now) {
109
110
  /**
110
111
  * Whether the sweep has work: an owner no check or call has shown alive lately, a lane whose owner is gone to park or
111
112
  * end, a lane no hold keeps up to park, or one a park or wake left partway; a service leader not seen running lately;
112
- * a device recorder an ended lane left marked as running. One read of the registry, plus the lane-folder call record
113
+ * a device recorder an ended lane left marked as running; a shared service nothing has needed for SHARED_IDLE_MS, or
114
+ * whose failed stop is due again. One read of the registry, plus the lane-folder call record
113
115
  * of each lane the registry has as unused and the lane folders' list; no process starts. With `runningSince`, the start
114
116
  * of a sweep still running, only an owner check that fell due after that start counts, since that sweep covers the rest.
115
117
  */
@@ -139,6 +141,8 @@ export function sweepDue(site, runningSince) {
139
141
  return true;
140
142
  if (endedFolders(folders, lanes).some(id => strayDue(laneEvidence(site.dir, id), now)))
141
143
  return true;
144
+ if (registry.sharedStopCandidates().length > 0)
145
+ return true;
142
146
  return registry
143
147
  .parkCandidates(idleSince, now - PARK_RETRY_MS)
144
148
  .some(c => c.hold === null && !gone.has(c.ref.id) && unused(site.dir, c, idleSince));
@@ -233,7 +237,7 @@ function untilEnd(lane, foundAt) {
233
237
  const ends = `ends at ${formatTime(foundAt + RETURN_WINDOW_MS)}`;
234
238
  return session ? `${ends} unless ${session} comes back` : `${ends}; nothing can take it back`;
235
239
  }
236
- async function reapLane(deps, registry, lane, dead) {
240
+ async function reapLane(deps, registry, lane, dead, failedStarts) {
237
241
  const { ref } = lane;
238
242
  const attempt = attemptId();
239
243
  const start = registry.beginReap(ref, attempt, dead);
@@ -247,7 +251,7 @@ async function reapLane(deps, registry, lane, dead) {
247
251
  return { kind: 'left', lane: ref.id, reason: 'another call took it while it was being ended' };
248
252
  const after = [
249
253
  ...(await stopEndedBrowsers(deps.browser ?? realBrowserTools, registry, deps.dir, deps.clock)).filter(t => t.lane === ref.id),
250
- ...(await tearDownEndedResources(deps, registry, deps.dir, { lane: ref.id })),
254
+ ...(await tearDownEndedResources(deps, registry, deps.dir, { lane: ref.id }, failedStarts)),
251
255
  ];
252
256
  return {
253
257
  kind: 'ended',
@@ -267,7 +271,9 @@ async function reapLane(deps, registry, lane, dead) {
267
271
  * The sweep's work, in the process that does it, for every lane on the Mac. It asks the process table once about the
268
272
  * owners no check or call has shown alive lately. A lane whose owner it finds gone is parked at once, and ended with
269
273
  * what a release does RETURN_WINDOW_MS after it was found gone, unless a process of its owner's session takes it back
270
- * first. It parks each lane whose owner left it unused for the park window and holds it no more.
274
+ * first. Its reaps tear down resources as one pass, so a shared service whose start fails is tried once however many
275
+ * reaped lanes need it. It parks each lane whose owner left it unused for the park window and holds it no more. It stops
276
+ * each shared service no active lane and no running teardown has needed for SHARED_IDLE_MS.
271
277
  */
272
278
  export async function runSweep(deps) {
273
279
  const say = deps.log ?? (() => undefined);
@@ -279,6 +285,7 @@ export async function runSweep(deps) {
279
285
  reaps: [],
280
286
  alive: [],
281
287
  recorders: [],
288
+ shared: [],
282
289
  pruned: [],
283
290
  problems: [],
284
291
  };
@@ -378,6 +385,7 @@ export async function runSweep(deps) {
378
385
  problem(candidate.ref.id, `parking stopped partway: ${messageOf(error)}`);
379
386
  }
380
387
  }
388
+ const failedStarts = new FailedStarts();
381
389
  for (const lane of goneIds.size > 0 ? registry.sweepList() : []) {
382
390
  const reaper = lane.gone?.reaper ?? null;
383
391
  if (!reapDue(lane, now))
@@ -387,7 +395,7 @@ export async function runSweep(deps) {
387
395
  ...[deadHolder(lane.park, deps.processes)].filter((p) => p !== null),
388
396
  ];
389
397
  try {
390
- const outcome = await reapLane(deps, registry, lane, dead);
398
+ const outcome = await reapLane(deps, registry, lane, dead, failedStarts);
391
399
  report.reaps.push(outcome);
392
400
  if (outcome.kind === 'ended') {
393
401
  say(`ended lane ${outcome.lane}: its owner was found gone (${describeState(outcome.state)}) at ${formatTime(outcome.foundAt)}, ${formatAge(now - outcome.foundAt)} ago, and did not come back`);
@@ -403,6 +411,23 @@ export async function runSweep(deps) {
403
411
  problem(lane.ref.id, `ending it stopped partway: ${messageOf(error)}`);
404
412
  }
405
413
  }
414
+ try {
415
+ report.shared = await stopUnneededShared({ env: deps.env, clock: deps.clock }, registry);
416
+ }
417
+ catch (error) {
418
+ report.problems.push(`shared services: stopping them stopped partway: ${messageOf(error)}`);
419
+ say(`shared services: PROBLEM: stopping them stopped partway: ${messageOf(error)}`);
420
+ }
421
+ for (const stop of report.shared) {
422
+ const which = `shared service ${stop.name} of ${stop.repository}`;
423
+ if (stop.outcome === 'stopped') {
424
+ say(`stopped ${which}: nothing has needed it since ${formatTime(stop.unneededSince)}`);
425
+ }
426
+ else {
427
+ report.problems.push(`${which}: ${stop.problem ?? 'its stop failed'}`);
428
+ say(`${which}: PROBLEM: ${stop.problem ?? 'its stop failed'}`);
429
+ }
430
+ }
406
431
  const folders = laneFolderIds(deps.dir);
407
432
  for (const id of endedFolders(folders, registry.sweepList())) {
408
433
  const evidence = laneEvidence(deps.dir, id);
package/dist/up/up.js CHANGED
@@ -19,7 +19,7 @@ import { laneEnvironment, laneVariables, hookEnvironment, LAUNCH_ARGS_ENV, resou
19
19
  import { runHook } from "../hooks/run.js";
20
20
  import { CLI, CONFIG_FILE, TOOL } from "../name.js";
21
21
  import { identityOf } from "../processes.js";
22
- import { asMobile, findProjectConfig, forPlatform, loadProjectConfig } from "../project/config.js";
22
+ import { asMobile, findProjectConfig, forPlatform, loadProjectConfig, withoutApp } from "../project/config.js";
23
23
  import { WEB_PLATFORM } from "../project/schema.js";
24
24
  import { loadConfig, withProjectBlocks } from "../registry/config.js";
25
25
  import { formatAge, shellQuote } from "../registry/format.js";
@@ -30,13 +30,14 @@ import { Registry } from "../registry/registry.js";
30
30
  import { keepTouching, recordCall } from "../registry/touch.js";
31
31
  import { resolveFolder } from "../registry-cli.js";
32
32
  import { explainInspection, inspectService } from "../services/services.js";
33
- import { LANE_FILES_KEEP_MS, laneDir, hookLog, pruneEndedLaneFiles, readUpRecord, writeUpRecord } from "./lane-files.js";
34
- import { claimedPort, recordedProcess, startClaimedService, stopClaimedService } from "./lane-services.js";
33
+ import { LANE_FILES_KEEP_MS, laneDir, hookLog, pruneEndedLaneFiles, readUpRecord, upPlatforms, writeUpRecord, } from "./lane-files.js";
34
+ import { claimedPort, recordedProcess, sharedNeedsOfPlatforms, startClaimedService, stopClaimedService, } from "./lane-services.js";
35
35
  import { rememberAnswers } from "./answers.js";
36
36
  import { bundlerPort, diagnose, dialArguments, reversePorts } from "./doctor.js";
37
37
  import { driverVariables } from "./maestro-env.js";
38
38
  import { androidBuildEnvironment, projectFindings, toolchainFindings, Unmet } from "./preflight.js";
39
39
  import { createResources, tearDownEndedResources } from "./resources.js";
40
+ import { startSharedForLane } from "./shared.js";
40
41
  import { settleWake, takeLane } from "./wake.js";
41
42
  function seconds(ms) {
42
43
  return `${(ms / 1000).toFixed(1)} s`;
@@ -209,8 +210,10 @@ async function ensureService(ctx, service) {
209
210
  const recorded = recordedProcess(lane, service.name);
210
211
  if (recorded) {
211
212
  const seen = await inspectService(deps.system, service, port, recorded);
212
- if (seen.healthy)
213
- return `already running on ${String(port)} (pid ${String(recorded.pid)}); ${seen.readyDetail}`;
213
+ if (seen.healthy) {
214
+ const unread = seen.listeners === null ? `; the listener on ${String(port)} could not be read` : '';
215
+ return `already running on ${String(port)} (pid ${String(recorded.pid)}); ${seen.readyDetail}${unread}`;
216
+ }
214
217
  const stopped = await stopClaimedService(ctx, recorded);
215
218
  const started = await startClaimedService(ctx, service, port);
216
219
  return `${started}, replacing pid ${String(recorded.pid)}, which failed its check (${explainInspection(seen)}): ${stopped}`;
@@ -345,7 +348,7 @@ async function deviceSteps(ctx, run, options, booting, approval, setup) {
345
348
  }, report => report.changed
346
349
  ? `applied ${report.steps
347
350
  .filter(s => s.changed)
348
- .map(s => s.step)
351
+ .map(s => (s.unconfirmed ? `${s.step} (unconfirmed: ${s.unconfirmed})` : s.step))
349
352
  .join(', ')}`
350
353
  : 'already in place');
351
354
  await run.step('launch', () => launch(ctx), detail => detail);
@@ -419,6 +422,33 @@ async function browserSteps(ctx, run, tools, driverChanged) {
419
422
  return `the browser starts with the saved login, ${describeSavedLogin(login)}${traced ? TRACE_NOTE : ''}`;
420
423
  }, detail => detail);
421
424
  }
425
+ const SHARED_OUTCOMES = {
426
+ started: 'started',
427
+ kept: 'running',
428
+ 'found-running': 'already up, so its start did not run',
429
+ };
430
+ function describeShared(started) {
431
+ if (started.length === 0)
432
+ return 'none needed';
433
+ return started.map(s => `${s.name} ${SHARED_OUTCOMES[s.outcome]}`).join(', ');
434
+ }
435
+ async function sharedStep(ctx, run, platforms) {
436
+ const { deps, registry, config } = ctx;
437
+ const needed = sharedNeedsOfPlatforms(config, platforms);
438
+ const bringUp = () => {
439
+ if (needed.length > 0)
440
+ registry.touch(ctx.ref, 'up: shared');
441
+ const log = (line) => {
442
+ deps.log(`${TOOL} up: shared: ${line}`);
443
+ };
444
+ const env = hookEnvironment(deps.env, {}, resourceVariables(config));
445
+ return startSharedForLane({ env, clock: deps.clock, log }, registry, ctx.ref, needed);
446
+ };
447
+ if (config.shared.length > 0)
448
+ await run.step('shared', bringUp, describeShared);
449
+ else
450
+ await bringUp();
451
+ }
422
452
  function describeResources(made) {
423
453
  const parts = [
424
454
  made.created.length > 0 ? `created ${made.created.join(', ')}` : '',
@@ -426,13 +456,42 @@ function describeResources(made) {
426
456
  ];
427
457
  return parts.filter(p => p !== '').join('; ');
428
458
  }
459
+ function describeClaim(o) {
460
+ const { lane } = o;
461
+ const held = [
462
+ ...(lane.device ? [`${lane.device.name} (${lane.device.id})`] : []),
463
+ ...(lane.browser ? [`a ${lane.browser.driver} browser`] : []),
464
+ ...lane.ports.map(p => `${p.service} ${String(p.port)}`),
465
+ ];
466
+ const what = held.length === 0 ? 'no device, browser or port' : [...(lane.device ? [] : ['no device']), ...held].join(', ');
467
+ return `${o.created ? 'claimed lane' : 'reusing lane'} ${lane.id}: ${what}`;
468
+ }
469
+ function refuseAppLane(lane, record) {
470
+ const platforms = upPlatforms(record);
471
+ const held = [
472
+ ...(lane.device ? [lane.device.name] : []),
473
+ ...(lane.browser ? [`a ${lane.browser.driver} browser`] : []),
474
+ ...(lane.ports.length > 0 ? [`ports for ${lane.ports.map(p => p.service).join(', ')}`] : []),
475
+ ];
476
+ if (platforms.length === 0 && held.length === 0)
477
+ return;
478
+ const what = [
479
+ ...(platforms.length > 0 ? [`was brought up for ${platforms.join(', ')}`] : []),
480
+ ...(held.length > 0 ? [`holds ${held.join(', ')}`] : []),
481
+ ];
482
+ throw new UsageError(`this folder's lane ${lane.id} ${what.join(' and ')}, and up --no-app brings up a lane with the project's data alone`, `run the command under ${CLI} exec in this lane, which gives it the resources' values (${CLI} up creates any it lacks); or ${CLI} release, then ${CLI} up --no-app`);
483
+ }
429
484
  export async function up(given, options) {
430
485
  const { deps, forgetDeviceStates } = rememberAnswers(given);
431
486
  const started = deps.clock.now();
432
487
  const run = new Run(deps);
433
488
  const start = options.worktree ?? deps.cwd;
434
489
  const located = locateConfig(start);
435
- const config = forPlatform(located, options.platform ?? located.platform);
490
+ const withApp = options.app !== false;
491
+ if (!withApp && !options.waking && located.resources.length === 0) {
492
+ throw new UsageError(`${located.file} declares no resources, so up --no-app has no data to bring up`, `declare the data under resources in ${CONFIG_FILE}, and the server it lives on under shared; or run ${CLI} up for the app`);
493
+ }
494
+ const config = withApp ? forPlatform(located, options.platform ?? located.platform) : withoutApp(located);
436
495
  const mobile = asMobile(config);
437
496
  const folder = resolveFolder(start);
438
497
  const backend = mobile ? deps.devices[mobile.platform] : undefined;
@@ -470,6 +529,8 @@ export async function up(given, options) {
470
529
  const owner = resolveOwner(deps.env, deps.processes, deps.selfPid);
471
530
  const before = options.waking ? null : registry.takeBack(owner, folder.worktree);
472
531
  if (before) {
532
+ if (!withApp)
533
+ refuseAppLane(before, readUpRecord(laneDir(state, before.id)));
473
534
  const ref = { id: before.id, token: before.token };
474
535
  recordCall(state, ref, deps.clock.now());
475
536
  if (registry.parkOf(before.id))
@@ -499,7 +560,7 @@ export async function up(given, options) {
499
560
  finally {
500
561
  stop?.();
501
562
  }
502
- }, o => `${o.created ? 'claimed lane' : 'reusing lane'} ${o.lane.id}: ${o.lane.device?.name ?? 'no device'}${o.lane.device ? ` (${o.lane.device.id})` : ''}${o.lane.browser ? `, a ${o.lane.browser.driver} browser` : ''}, ${o.lane.ports.map(p => `${p.service} ${String(p.port)}`).join(', ')}`);
563
+ }, describeClaim);
503
564
  for (const note of outcome.notes)
504
565
  deps.log(`${TOOL} up: ${note}`);
505
566
  const browsers = deps.browser ?? realBrowserTools;
@@ -515,12 +576,13 @@ export async function up(given, options) {
515
576
  const target = mobile ? deviceTarget(deps, mobile, lane, options) : null;
516
577
  const dir = laneDir(state, lane.id);
517
578
  const previous = readUpRecord(dir);
579
+ const platforms = [...new Set([...upPlatforms(previous), ...(config.platform === null ? [] : [config.platform])])];
518
580
  writeUpRecord(dir, {
519
581
  version: 1,
520
582
  lane: lane.id,
521
583
  config: config.file,
522
- platform: config.platform,
523
- platforms: [...new Set([...(previous?.platforms ?? (previous ? [previous.platform] : [])), config.platform])],
584
+ platform: config.platform ?? previous?.platform ?? null,
585
+ platforms,
524
586
  bundleId: mobile?.app.bundleId ?? previous?.bundleId ?? null,
525
587
  launch: mobile?.app.launch ?? previous?.launch ?? null,
526
588
  pinned: previous?.pinned ?? false,
@@ -572,8 +634,10 @@ export async function up(given, options) {
572
634
  throw error;
573
635
  }
574
636
  const booting = bootingWithSetup ?? (target ? startBoot(target) : null);
575
- if (config.resources.length > 0 && !options.waking) {
576
- try {
637
+ try {
638
+ registry.followResourceNeeds(ctx.ref, config.resources);
639
+ await sharedStep(ctx, run, platforms);
640
+ if (config.resources.length > 0 && !options.waking) {
577
641
  const made = await run.step('resources', () => createResources(ctx, ctx.lane), describeResources);
578
642
  ctx.lane = made.lane;
579
643
  ctx.env = laneEnvironment(deps.env, made.lane, config, dir);
@@ -582,11 +646,11 @@ export async function up(given, options) {
582
646
  device.env = ctx.env;
583
647
  }
584
648
  }
585
- catch (error) {
586
- if (booting)
587
- await booting;
588
- throw error;
589
- }
649
+ }
650
+ catch (error) {
651
+ if (booting)
652
+ await booting;
653
+ throw error;
590
654
  }
591
655
  let build = null;
592
656
  if (device && booting)
package/dist/up/waker.js CHANGED
@@ -1,12 +1,13 @@
1
1
  import { stateDir } from "../registry/paths.js";
2
2
  import { Registry } from "../registry/registry.js";
3
- import { laneDir, readUpRecord, wakeFolder } from "./lane-files.js";
3
+ import { laneDir, readUpRecord, upPlatforms, wakeFolder } from "./lane-files.js";
4
4
  import { up } from "./up.js";
5
5
  /** How long waking a lane waits for a pool device, as `up` does by default. */
6
6
  export const WAKE_WAIT_MS = 600_000;
7
7
  /**
8
8
  * Wakes a lane through the steps `up` takes, minus setup and resource creation, for each platform `up` brought it up
9
- * for; a lane `up` never ran for gets its device back, booted if it was.
9
+ * for; a lane with the project's data only gets the shared services its resources need back, and nothing else; a lane
10
+ * `up` never ran for gets its device back, booted if it was.
10
11
  */
11
12
  export function upWaker(deps) {
12
13
  return async (lane, park) => {
@@ -14,9 +15,12 @@ export function upWaker(deps) {
14
15
  const record = readUpRecord(laneDir(state, lane.id));
15
16
  if (record) {
16
17
  const folder = wakeFolder(state, lane);
17
- for (const platform of record.platforms ?? [record.platform]) {
18
- await up({ ...deps, cwd: folder }, { setup: false, cacheMode: 'use', waitMs: WAKE_WAIT_MS, worktree: folder, platform, waking: true });
19
- }
18
+ const wake = { setup: false, cacheMode: 'use', waitMs: WAKE_WAIT_MS, worktree: folder, waking: true };
19
+ const platforms = upPlatforms(record);
20
+ if (platforms.length === 0)
21
+ await up({ ...deps, cwd: folder }, { ...wake, app: false });
22
+ for (const platform of platforms)
23
+ await up({ ...deps, cwd: folder }, { ...wake, platform });
20
24
  return;
21
25
  }
22
26
  const device = park.restarts.device;