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.
package/dist/up/doctor.js CHANGED
@@ -12,12 +12,15 @@ import { readDialDefault } from "../devices/dial.js";
12
12
  import { checkHygiene } from "../devices/hygiene.js";
13
13
  import { MAESTRO_OVERRIDE_ENV, maestroBuild } from "../driver/install.js";
14
14
  import { metroUrl } from "../devices/launch.js";
15
- import { LAUNCH_ARGS_ENV, webUrl } from "../hooks/env.js";
15
+ import { hookEnvironment, LAUNCH_ARGS_ENV, webUrl } from "../hooks/env.js";
16
+ import { runCheck } from "../hooks/run.js";
16
17
  import { judgeAndroidVariant, judgeIosVariant, readIosBundle, } from "../devices/variant.js";
17
18
  import { CLI } from "../name.js";
18
19
  import { asMobile } from "../project/config.js";
19
20
  import { AGENTS, describeOwner, describeState } from "../registry/owner.js";
20
21
  import { formatTime } from "../registry/format.js";
22
+ import { readLaneNeeds } from "../registry/registry.js";
23
+ import { SHARED_READY_RUN_MAX_MS } from "../registry/shared.js";
21
24
  import { describeRestarts } from "../registry/parks.js";
22
25
  import { explainInspection, inspectService, listenersElsewhere } from "../services/services.js";
23
26
  import { toolchainFindings } from "./preflight.js";
@@ -270,6 +273,9 @@ export async function diagnose(deps, input) {
270
273
  if (!config || !lane || mismatch)
271
274
  return finish(lane, checks);
272
275
  checks.push(...resourceChecks(config, lane));
276
+ checks.push(...(await sharedChecks(deps, config, lane)));
277
+ if (config.platform === null)
278
+ return finish(lane, checks);
273
279
  const tools = deps.browser ?? realBrowserTools;
274
280
  if (hasBrowser(lane))
275
281
  checks.push(...(await browserChecks(tools, stateDir(deps.env), lane, deps.env)));
@@ -328,9 +334,10 @@ export async function diagnose(deps, input) {
328
334
  name: 'hygiene',
329
335
  ok: hygiene.ok,
330
336
  detail: hygiene.ok
331
- ? hygiene.steps.map(s => `${s.step} ${s.got}`).join('; ')
337
+ ? hygiene.steps.map(s => (s.unconfirmed ? `${s.step} unconfirmed: ${s.unconfirmed}` : `${s.step} ${s.got}`)).join('; ')
332
338
  : off.map(s => `${s.step} reads ${s.got}, not ${s.wanted}`).join('; '),
333
339
  fix: UP,
340
+ ...(hygiene.ok && hygiene.steps.some(s => s.unconfirmed) ? { warning: true } : {}),
334
341
  });
335
342
  }
336
343
  catch (error) {
@@ -367,6 +374,41 @@ async function serviceChecks(deps, config, lane) {
367
374
  }
368
375
  return checks;
369
376
  }
377
+ async function sharedReady(deps, spec) {
378
+ const name = `shared ${spec.name}`;
379
+ const fix = `${UP} (it starts ${spec.name})`;
380
+ const timeoutMs = Math.min(spec.timeoutMs, SHARED_READY_RUN_MAX_MS);
381
+ try {
382
+ const run = await runCheck(spec.ready, { cwd: spec.cwd, env: hookEnvironment(deps.env, {}), timeoutMs, ownGroup: true });
383
+ if (run.code === 0)
384
+ return { name, ok: true, detail: `running: its ready passes (${spec.ready})`, fix: '' };
385
+ const how = run.code !== null
386
+ ? `exited ${String(run.code)}`
387
+ : run.ms >= timeoutMs
388
+ ? `did not answer within ${String(timeoutMs / 1000)} s`
389
+ : `was killed by ${run.signal ?? 'a signal'}`;
390
+ const last = run.output.split('\n').at(-1) ?? '';
391
+ return { name, ok: false, detail: `its ready ${how} (${spec.ready}): ${last === '' ? '(no output)' : last}`, fix };
392
+ }
393
+ catch (error) {
394
+ return { name, ok: false, detail: `its ready could not run in ${spec.cwd}: ${message(error)}`, fix };
395
+ }
396
+ }
397
+ async function sharedChecks(deps, config, lane) {
398
+ const checks = [];
399
+ for (const name of readLaneNeeds(stateDir(deps.env), lane.id)) {
400
+ const spec = config.shared.find(s => s.name === name);
401
+ checks.push(spec
402
+ ? await sharedReady(deps, spec)
403
+ : {
404
+ name: `shared ${name}`,
405
+ ok: false,
406
+ detail: `lane ${lane.id} needs it, and ${config.file} declares no shared service ${name}`,
407
+ fix: `${UP} (it records what the lane needs now)`,
408
+ });
409
+ }
410
+ return checks;
411
+ }
370
412
  function resourceChecks(config, lane) {
371
413
  const declared = config.resources.map(r => r.name);
372
414
  const names = [...new Set([...declared, ...lane.resources.map(r => r.name)])];
package/dist/up/exec.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { spawn } from 'node:child_process';
2
2
  import os from 'node:os';
3
3
  import path from 'node:path';
4
+ import { setTimeout as sleep } from 'node:timers/promises';
4
5
  import { laneBrowser, hasBrowser } from "../browser/lane-browser.js";
5
6
  import { keepFailureEvidence } from "../browser/evidence.js";
6
7
  import { realBrowserTools } from "../browser/tools.js";
@@ -15,7 +16,7 @@ import { NotClaimed, Refusal, UsageError } from "../registry/refusal.js";
15
16
  import { Registry } from "../registry/registry.js";
16
17
  import { keepTouching, recordCall, touchLane } from "../registry/touch.js";
17
18
  import { resolveFolder } from "../registry-cli.js";
18
- import { laneDir, readUpRecord } from "./lane-files.js";
19
+ import { configPlatform, laneDir, readUpRecord } from "./lane-files.js";
19
20
  import { reversePorts } from "./doctor.js";
20
21
  import { driverVariables } from "./maestro-env.js";
21
22
  import { enterLane } from "./wake.js";
@@ -52,10 +53,24 @@ async function restoreDeviceNetwork(deps, config, lane, dir) {
52
53
  deps.log(`${TOOL} exec: could not restore adb reverse on ${serial} (${error.message.split('\n')[0] ?? ''}); ${CLI} doctor names the fix`);
53
54
  }
54
55
  }
56
+ export const CALLER_POLL_MS = 1000;
57
+ export const STOP_GRACE_MS = 5000;
58
+ const STOP_CHECK_MS = 100;
59
+ const CALLER_GONE_EXIT = 128 + os.constants.signals.SIGHUP;
60
+ function callerOf(deps) {
61
+ const parentPid = deps.parentPid ?? (() => process.ppid);
62
+ const pid = parentPid();
63
+ return { gone: () => parentPid() !== pid };
64
+ }
65
+ /**
66
+ * Runs a command with the lane's environment and returns its exit code. When the process that started exec goes away
67
+ * while the command runs, exec ends the command and every process below it, and returns 129 as a hung-up command would.
68
+ */
55
69
  export async function execInLane(deps, argv) {
56
70
  const [command, ...args] = argv;
57
71
  if (command === undefined)
58
72
  throw new UsageError('exec needs a command', `${CLI} exec -- <command> [args...]`);
73
+ const caller = callerOf(deps);
59
74
  const folder = resolveFolder(deps.cwd);
60
75
  const owner = resolveOwner(deps.env, deps.processes, deps.selfPid);
61
76
  const state = stateDir(deps.env);
@@ -87,7 +102,7 @@ export async function execInLane(deps, argv) {
87
102
  touchLane(site, ref, `exec: ${command}`);
88
103
  const dir = laneDir(state, lane.id);
89
104
  const record = readUpRecord(dir);
90
- const { config, problem } = lenientConfig(record?.config ?? findProjectConfig(deps.cwd), record?.platform ?? lane.device?.platform, deps.log);
105
+ const { config, problem } = lenientConfig(record?.config ?? findProjectConfig(deps.cwd), configPlatform(record, lane), deps.log);
91
106
  let env = laneEnvironment(deps.env, lane, config, dir, await driverVariables(deps, config, lane, dir, problem));
92
107
  await restoreDeviceNetwork(deps, config && asMobile(config), lane, dir);
93
108
  const browser = hasBrowser(lane)
@@ -108,13 +123,16 @@ export async function execInLane(deps, argv) {
108
123
  if (browser)
109
124
  env = { ...env, ...browser.vars };
110
125
  const stopTouching = keepTouching(site, ref, `exec: ${command}`, deps.heartbeatMs);
111
- let code;
126
+ let ending;
112
127
  try {
113
- code = await run(deps, command, args, env);
128
+ ending = await run(deps, caller, command, args, env);
114
129
  }
115
130
  finally {
116
131
  stopTouching();
117
132
  }
133
+ if (ending.kind === 'caller-gone')
134
+ return CALLER_GONE_EXIT;
135
+ const { code } = ending;
118
136
  try {
119
137
  touchLane(site, ref, `exec: ${command}`);
120
138
  }
@@ -128,7 +146,9 @@ export async function execInLane(deps, argv) {
128
146
  }
129
147
  return code;
130
148
  }
131
- function run(deps, command, args, env) {
149
+ function run(deps, caller, command, args, env) {
150
+ if (caller.gone())
151
+ return Promise.resolve({ kind: 'caller-gone' });
132
152
  return new Promise((resolve, reject) => {
133
153
  const child = spawn(command, args, { cwd: deps.cwd, env, stdio: 'inherit' });
134
154
  const forward = (signal) => {
@@ -137,7 +157,15 @@ function run(deps, command, args, env) {
137
157
  const ignore = () => undefined;
138
158
  process.on('SIGINT', ignore);
139
159
  process.on('SIGTERM', forward);
160
+ let stopping = null;
161
+ const watch = setInterval(() => {
162
+ if (!caller.gone())
163
+ return;
164
+ clearInterval(watch);
165
+ stopping = stopTree(deps.system, child, deps.stopGraceMs ?? STOP_GRACE_MS);
166
+ }, deps.callerPollMs ?? CALLER_POLL_MS);
140
167
  const done = () => {
168
+ clearInterval(watch);
141
169
  process.off('SIGINT', ignore);
142
170
  process.off('SIGTERM', forward);
143
171
  };
@@ -145,14 +173,66 @@ function run(deps, command, args, env) {
145
173
  done();
146
174
  if (error.code === 'ENOENT') {
147
175
  deps.log(`${TOOL} exec: ${command}: command not found`);
148
- resolve(127);
176
+ resolve({ kind: 'exited', code: 127 });
149
177
  }
150
178
  else
151
179
  reject(error);
152
180
  });
153
181
  child.once('close', (code, signal) => {
154
182
  done();
155
- resolve(code ?? 128 + (signal ? os.constants.signals[signal] : 0));
183
+ if (stopping) {
184
+ stopping.then(() => {
185
+ resolve({ kind: 'caller-gone' });
186
+ }, reject);
187
+ return;
188
+ }
189
+ resolve({ kind: 'exited', code: code ?? 128 + (signal ? os.constants.signals[signal] : 0) });
156
190
  });
157
191
  });
158
192
  }
193
+ function track(system, pids) {
194
+ const starts = system.running(pids);
195
+ if (!starts)
196
+ return pids.map(pid => ({ pid, start: null }));
197
+ return pids.flatMap(pid => {
198
+ const start = starts.get(pid);
199
+ return start === undefined ? [] : [{ pid, start }];
200
+ });
201
+ }
202
+ function stillRunning(system, tracked) {
203
+ if (tracked.length === 0)
204
+ return [];
205
+ const starts = system.running(tracked.map(p => p.pid));
206
+ if (!starts)
207
+ return [...tracked];
208
+ return tracked.filter(p => {
209
+ const start = starts.get(p.pid);
210
+ return start !== undefined && (p.start === null || start === p.start);
211
+ });
212
+ }
213
+ async function stopTree(system, child, graceMs) {
214
+ const root = child.pid;
215
+ if (root === undefined)
216
+ return;
217
+ const exited = () => child.exitCode !== null || child.signalCode !== null;
218
+ const below = track(system, (system.descendants(root) ?? []).map(d => d.pid));
219
+ child.kill('SIGTERM');
220
+ for (const p of below)
221
+ system.signal(p.pid, 'SIGTERM');
222
+ const deadline = Date.now() + graceMs;
223
+ let left = stillRunning(system, below);
224
+ while ((left.length > 0 || !exited()) && Date.now() < deadline) {
225
+ await sleep(Math.min(STOP_CHECK_MS, Math.max(0, deadline - Date.now())));
226
+ left = stillRunning(system, left);
227
+ }
228
+ if (left.length === 0 && exited())
229
+ return;
230
+ const doomed = new Set(left.map(p => p.pid));
231
+ for (const pid of exited() ? [...doomed] : [root, ...doomed]) {
232
+ for (const d of system.descendants(pid) ?? [])
233
+ doomed.add(d.pid);
234
+ }
235
+ child.kill('SIGKILL');
236
+ for (const pid of doomed)
237
+ system.signal(pid, 'SIGKILL');
238
+ }
@@ -34,6 +34,19 @@ export function readUpRecord(dir) {
34
34
  return null;
35
35
  }
36
36
  }
37
+ /** Every platform `up` brought the lane up for; none for a lane with the project's data only, or one `up` never ran for. */
38
+ export function upPlatforms(record) {
39
+ if (!record)
40
+ return [];
41
+ return record.platforms ?? (record.platform === null ? [] : [record.platform]);
42
+ }
43
+ /**
44
+ * The platform to read a lane's config for: the one `up` ran for last, null for a lane with the project's data only,
45
+ * and the lane's device's platform when `up` never ran for it.
46
+ */
47
+ export function configPlatform(record, lane) {
48
+ return record ? record.platform : lane?.device?.platform;
49
+ }
37
50
  /**
38
51
  * The folder a wake runs `up` from: the one holding the agentlane.json `up` last read for the lane, which can sit below
39
52
  * the git root the lane is keyed by; the lane's worktree when no record names one, or that folder is gone.
@@ -1,6 +1,16 @@
1
1
  import { CLI } from "../name.js";
2
+ import { PROJECT_PLATFORMS } from "../project/schema.js";
2
3
  import { describeListeners, describeStop, listenersOn, ServiceFailed, startService, stopService, waitForService, } from "../services/services.js";
3
4
  import { serviceLog } from "./lane-files.js";
5
+ /** The services a lane brought up for `platforms` runs: none for a lane brought up for no platform (`up --no-app`). */
6
+ export function servicesOfPlatforms(config, platforms) {
7
+ return config.allServices.filter(s => (s.platforms ?? PROJECT_PLATFORMS).some(p => platforms.includes(p)));
8
+ }
9
+ /** The shared services a lane brought up for `platforms` needs: those its resources and those services name. */
10
+ export function sharedNeedsOfPlatforms(config, platforms) {
11
+ const named = new Set([...config.resources, ...servicesOfPlatforms(config, platforms)].flatMap(user => user.needs));
12
+ return config.shared.filter(s => named.has(s.name));
13
+ }
4
14
  export function claimedPort(lane, service) {
5
15
  return lane.ports.find(p => p.service === service)?.port;
6
16
  }
@@ -121,8 +121,12 @@ function maestroDevice(config, configProblem, lane) {
121
121
  if (!config.driver)
122
122
  return { refusal: `${config.file} has no driver block\nto fix: add "driver": {} to ${config.file}` };
123
123
  const held = lane.device;
124
- if (!held)
125
- return { refusal: `the lane holds no simulator or emulator\nto fix: ${CLI} release, then ${CLI} up` };
124
+ if (!held) {
125
+ const fix = config.platform === null
126
+ ? `${CLI} up (it brings the app up in this lane, which has the project's data only)`
127
+ : `${CLI} release, then ${CLI} up`;
128
+ return { refusal: `the lane holds no simulator or emulator\nto fix: ${fix}` };
129
+ }
126
130
  if (held.platform !== 'android')
127
131
  return { device: held.id };
128
132
  if (held.serial === null) {
package/dist/up/park.js CHANGED
@@ -34,7 +34,7 @@ function bootedNow(backend, id) {
34
34
  return null;
35
35
  }
36
36
  }
37
- function restartsOf(deps, dir, lane) {
37
+ function restartsOf(deps, registry, lane) {
38
38
  const device = lane.device;
39
39
  const parked = device
40
40
  ? {
@@ -51,8 +51,9 @@ function restartsOf(deps, dir, lane) {
51
51
  return {
52
52
  device: parked,
53
53
  services: lane.processes.map(p => p.service),
54
+ shared: registry.needsOf(lane.id),
54
55
  browser: lane.browser?.driver ?? null,
55
- app: readUpRecord(laneDir(dir, lane.id))?.bundleId ?? null,
56
+ app: readUpRecord(laneDir(registry.dir, lane.id))?.bundleId ?? null,
56
57
  };
57
58
  }
58
59
  async function stopEverything(deps, registry, dir, lane) {
@@ -122,7 +123,8 @@ export async function parkLane(deps, registry, candidate, idleSince) {
122
123
  registry.abortPark(ref, attempt);
123
124
  return { kind: 'left', lane: ref.id, reason: 'it was used' };
124
125
  }
125
- const restarts = mergeRestarts(candidate.park?.restarts ?? restartsOf(deps, dir, lane), restartsOf(deps, dir, lane));
126
+ const current = restartsOf(deps, registry, lane);
127
+ const restarts = mergeRestarts(candidate.park?.restarts ?? current, current);
126
128
  const notes = [];
127
129
  try {
128
130
  const problems = await stopEverything(deps, registry, dir, lane);
@@ -213,7 +213,8 @@ export async function preconditionFinding(p, cwd, env) {
213
213
  }
214
214
  export async function projectFindings(config, stage, env, stopAtFirst) {
215
215
  const findings = [];
216
- const applies = (p) => p.before === stage && (p.platforms === null || p.platforms.includes(config.platform));
216
+ const { platform } = config;
217
+ const applies = (p) => p.before === stage && (p.platforms === null || (platform !== null && p.platforms.includes(platform)));
217
218
  for (const p of config.preconditions.filter(applies)) {
218
219
  const finding = await preconditionFinding(p, config.dir, env);
219
220
  findings.push(finding);
@@ -5,6 +5,7 @@ import { describeExit, HookFailed, runHook, tail } from "../hooks/run.js";
5
5
  import { CLI, TOOL } from "../name.js";
6
6
  import { EXPORT_NAME_PATTERN, EXPORTS_FILE_ENV } from "../project/schema.js";
7
7
  import { laneDir } from "./lane-files.js";
8
+ import { SharedServiceFailed, startSharedForTeardown } from "./shared.js";
8
9
  export class ResourceFailed extends Error {
9
10
  resource;
10
11
  constructor(resource, problem) {
@@ -108,6 +109,7 @@ export async function createResources(r, lane) {
108
109
  timeoutMs: resource.teardownTimeoutMs,
109
110
  env: recorded(laneVariables(result.lane, r.config, r.dir)),
110
111
  cleared: resourceVariables(r.config),
112
+ needs: resource.needs,
111
113
  });
112
114
  if (start.kind === 'kept') {
113
115
  result.kept.push(resource.name);
@@ -143,35 +145,116 @@ async function tearDown(deps, stateDir, t) {
143
145
  });
144
146
  return { ms: run.ms, moved };
145
147
  }
148
+ function sharedFailure(error) {
149
+ const didNotRun = 'teardown did not run, since';
150
+ if (!(error instanceof SharedServiceFailed)) {
151
+ const problem = message(error);
152
+ return {
153
+ headline: `${didNotRun} the shared services it needs could not be started`,
154
+ movedNote: '',
155
+ recordedAfterHeadline: problem,
156
+ detailAfterHeadline: problem,
157
+ };
158
+ }
159
+ const prefix = `shared service ${error.service}: `;
160
+ const problem = error.message.startsWith(prefix) ? error.message.slice(prefix.length) : error.message;
161
+ return {
162
+ headline: `${didNotRun} shared service ${error.service} did not start`,
163
+ movedNote: '',
164
+ recordedAfterHeadline: problem.split('\n')[0] ?? problem,
165
+ detailAfterHeadline: problem,
166
+ };
167
+ }
168
+ function commandFailure(error, t, log) {
169
+ const moved = isDirectory(t.cwd) ? '' : ` (${movedNote(t)})`;
170
+ if (error instanceof HookFailed) {
171
+ return {
172
+ headline: `teardown ${describeExit(error.run)}`,
173
+ movedNote: moved,
174
+ recordedAfterHeadline: t.teardown,
175
+ detailAfterHeadline: `${t.teardown}\n${tail(log)}\nfull log: ${log}`,
176
+ };
177
+ }
178
+ return {
179
+ headline: `teardown could not run (${message(error)})`,
180
+ movedNote: moved,
181
+ recordedAfterHeadline: t.teardown,
182
+ detailAfterHeadline: `full log: ${log}`,
183
+ };
184
+ }
185
+ function movedNote(t) {
186
+ return `${t.cwd} is gone, so it ran in the lane's log folder`;
187
+ }
188
+ /**
189
+ * The shared services whose start failed in one pass of teardowns, by repository and name, so no later teardown of
190
+ * that pass tries again. A pass is one `tearDownEndedResources` call, or every call one sweep makes for its reaps.
191
+ */
192
+ export class FailedStarts {
193
+ failed = new Map();
194
+ static key(repository, name) {
195
+ return JSON.stringify([repository, name]);
196
+ }
197
+ of(t) {
198
+ const { repository } = t;
199
+ if (repository === null)
200
+ return undefined;
201
+ return t.needs.map(name => this.failed.get(FailedStarts.key(repository, name))).find(failed => failed !== undefined);
202
+ }
203
+ remember(t, error) {
204
+ if (t.repository !== null && error instanceof SharedServiceFailed) {
205
+ this.failed.set(FailedStarts.key(t.repository, error.service), error);
206
+ }
207
+ }
208
+ }
209
+ async function tearDownOne(deps, registry, stateDir, t, notes, failedStarts) {
210
+ const failedBefore = failedStarts.of(t);
211
+ if (failedBefore)
212
+ return sharedFailure(failedBefore);
213
+ try {
214
+ await startSharedForTeardown({
215
+ env: hookEnvironment(deps.env, {}, t.cleared),
216
+ clock: deps.clock,
217
+ log: line => notes.push(`resource ${t.name}: ${line}`),
218
+ }, registry, t);
219
+ }
220
+ catch (error) {
221
+ failedStarts.remember(t, error);
222
+ return sharedFailure(error);
223
+ }
224
+ try {
225
+ const done = await tearDown(deps, stateDir, t);
226
+ registry.finishTeardown(t.lane, t.name);
227
+ notes.push(`tore down resource ${t.name} (${seconds(done.ms)}${done.moved ? `; ${movedNote(t)}` : ''})`);
228
+ return null;
229
+ }
230
+ catch (error) {
231
+ return commandFailure(error, t, resourceLog(laneDir(stateDir, t.lane), t.name));
232
+ }
233
+ }
146
234
  /**
147
- * Tears down the resources of lanes that have ended, by release or by a reap, with what their create recorded. A failed
148
- * teardown keeps its record, so a later pass tries again; a pass skips one that failed in the last few minutes, unless
149
- * it names that lane.
235
+ * Tears down the resources of lanes that have ended, by release or by a reap, with what their create recorded, each
236
+ * once the shared services it needs run. A failed teardown keeps its record, so a later pass tries again; a pass skips
237
+ * one that failed in the last few minutes, unless it names that lane. A shared service that does not start fails the
238
+ * teardown that needs it, which does not run, and every later teardown of the pass that needs it, without another start.
239
+ * The pass is this call unless the caller passes the `failedStarts` of a pass it spreads over several calls.
150
240
  */
151
- export async function tearDownEndedResources(deps, registry, stateDir, choice = {}) {
241
+ export async function tearDownEndedResources(deps, registry, stateDir, choice = {}, failedStarts = new FailedStarts()) {
152
242
  const byLane = new Map();
153
243
  for (const t of registry.takeTeardowns(choice.lane === undefined ? {} : { lane: choice.lane })) {
154
244
  const into = byLane.get(t.lane) ?? { lane: t.lane, notes: [], problems: [] };
155
245
  byLane.set(t.lane, into);
156
- const away = `${t.cwd} is gone, so it ran in the lane's log folder`;
157
- try {
158
- const done = await tearDown(deps, stateDir, t);
246
+ const failure = await tearDownOne(deps, registry, stateDir, t, into.notes, failedStarts);
247
+ if (failure === null)
248
+ continue;
249
+ const how = `${failure.headline}${failure.movedNote}`;
250
+ const detail = failure.detailAfterHeadline;
251
+ if (choice.force) {
159
252
  registry.finishTeardown(t.lane, t.name);
160
- into.notes.push(`tore down resource ${t.name} (${seconds(done.ms)}${done.moved ? `; ${away}` : ''})`);
253
+ into.problems.push(`forgot resource ${t.name}: ${how}, and --force drops its record, leaving what it made: ${detail}`);
161
254
  }
162
- catch (error) {
163
- const log = resourceLog(laneDir(stateDir, t.lane), t.name);
164
- const how = error instanceof HookFailed ? `teardown ${describeExit(error.run)}` : `teardown could not run (${message(error)})`;
165
- const where = isDirectory(t.cwd) ? '' : ` (${away})`;
166
- const detail = error instanceof HookFailed ? `${t.teardown}\n${tail(log)}\nfull log: ${log}` : `full log: ${log}`;
167
- if (choice.force) {
168
- registry.finishTeardown(t.lane, t.name);
169
- into.problems.push(`forgot resource ${t.name}: ${how}${where}, and --force drops its record, leaving what it made: ${detail}`);
170
- }
171
- else {
172
- registry.failTeardown(t.lane, t.name, `${how}: ${t.teardown}`);
173
- into.problems.push(`resource ${t.name}: ${how}${where} (attempt ${String(t.attempts + 1)}): ${detail}\nits record is kept: the next release or reap tries again, and ${CLI} release ${t.lane} runs it now`);
174
- }
255
+ else {
256
+ registry.failTeardown(t.lane, t.name, `${failure.headline}: ${failure.recordedAfterHeadline}`);
257
+ into.problems.push(`resource ${t.name}: ${how} (attempt ${String(t.attempts + 1)}): ${detail}\nits record is kept: the next release or reap tries again, and ${CLI} release ${t.lane} runs it now`);
175
258
  }
176
259
  }
177
260
  return [...byLane.values()];
@@ -1,4 +1,4 @@
1
- import { laneEnvironment } from "../hooks/env.js";
1
+ import { hookEnvironment, laneEnvironment, resourceVariables } from "../hooks/env.js";
2
2
  import { CLI, CONFIG_FILE, TOOL } from "../name.js";
3
3
  import { findProjectConfig, loadLaneConfig } from "../project/config.js";
4
4
  import { describeOwner, resolveOwner } from "../registry/owner.js";
@@ -8,8 +8,9 @@ import { Registry } from "../registry/registry.js";
8
8
  import { recordCall } from "../registry/touch.js";
9
9
  import { resolveFolder } from "../registry-cli.js";
10
10
  import { ServiceFailed } from "../services/services.js";
11
- import { laneDir, readUpRecord } from "./lane-files.js";
12
- import { claimedPort, recordedProcess, startClaimedService, stopClaimedService } from "./lane-services.js";
11
+ import { configPlatform, laneDir, readUpRecord, upPlatforms } from "./lane-files.js";
12
+ import { claimedPort, recordedProcess, servicesOfPlatforms, sharedNeedsOfPlatforms, startClaimedService, stopClaimedService, } from "./lane-services.js";
13
+ import { startSharedForLane } from "./shared.js";
13
14
  import { enterLane } from "./wake.js";
14
15
  import { upWaker } from "./waker.js";
15
16
  export async function restart(deps, names, worktree) {
@@ -42,9 +43,12 @@ export async function restart(deps, names, worktree) {
42
43
  if (!file) {
43
44
  throw new UsageError(`no ${CONFIG_FILE} in ${folder.worktree} or above it, so its services are unknown`, `write one at the project's root; '${CLI} schema' prints its JSON Schema`);
44
45
  }
45
- const config = loadLaneConfig(file, record?.platform ?? lane.device?.platform);
46
- const platforms = record?.platforms ?? [config.platform];
47
- const own = config.allServices.filter(s => s.platforms === null || s.platforms.some(p => platforms.includes(p)));
46
+ const config = loadLaneConfig(file, configPlatform(record, lane));
47
+ const platforms = record ? upPlatforms(record) : config.platform === null ? [] : [config.platform];
48
+ if (platforms.length === 0) {
49
+ throw new UsageError(`lane ${lane.id} has the project's data only, so it runs no service to restart`, `${CLI} up (it brings the app and its services up in this lane, and keeps its resources)`);
50
+ }
51
+ const own = servicesOfPlatforms(config, platforms);
48
52
  const targets = [...new Set(names)].map(name => {
49
53
  const service = own.find(s => s.name === name);
50
54
  if (!service) {
@@ -64,6 +68,17 @@ export async function restart(deps, names, worktree) {
64
68
  return { service, port, recorded };
65
69
  });
66
70
  const ref = { id: lane.id, token: lane.token };
71
+ const needed = sharedNeedsOfPlatforms(config, platforms);
72
+ if (needed.length > 0)
73
+ registry.touch(ref, 'restart: shared');
74
+ registry.followResourceNeeds(ref, config.resources);
75
+ await startSharedForLane({
76
+ env: hookEnvironment(deps.env, {}, resourceVariables(config)),
77
+ clock: deps.clock,
78
+ log: line => {
79
+ deps.log(`${TOOL} restart: shared: ${line}`);
80
+ },
81
+ }, registry, ref, needed);
67
82
  const services = {
68
83
  deps,
69
84
  registry,