agentlane 0.4.1 → 0.5.1
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/CHANGELOG.md +87 -0
- package/dist/agents/claude-plugin.js +69 -8
- package/dist/agents/cli.js +11 -4
- package/dist/agents/hook-check.js +290 -0
- package/dist/agents/launcher.js +34 -6
- package/dist/agents/wiring-check.js +10 -4
- package/dist/agents/wiring.js +6 -33
- package/dist/browser/env.js +27 -7
- package/dist/browser/install.js +144 -6
- package/dist/browser/lane-browser.js +6 -3
- package/dist/browser/stop.js +2 -17
- package/dist/cache/key.js +23 -11
- package/dist/cli.js +63 -23
- package/dist/devices/backend.js +6 -0
- package/dist/devices/emulator.js +15 -5
- package/dist/devices/hygiene.js +20 -4
- package/dist/devices/simctl.js +15 -3
- package/dist/disk-limits.js +134 -0
- package/dist/driver/caller.js +21 -6
- package/dist/driver/client.js +9 -5
- package/dist/driver/engine.js +29 -14
- package/dist/driver/install.js +88 -36
- package/dist/driver/paths.js +15 -11
- package/dist/driver/session.js +4 -1
- package/dist/driver/superseded.js +378 -0
- package/dist/driver-cli.js +32 -23
- package/dist/evidence/device.js +4 -0
- package/dist/evidence/evidence.js +4 -2
- package/dist/evidence/files.js +68 -0
- package/dist/evidence/strays.js +107 -0
- package/dist/hooks/env.js +2 -1
- package/dist/mcp/server.js +15 -0
- package/dist/mcp/tools.js +9 -1
- package/dist/plugin/guard.js +30 -0
- package/dist/plugin/hook-cli.js +101 -6
- package/dist/plugin/hook-mark.js +73 -0
- package/dist/plugin/hook-once.js +78 -0
- package/dist/plugin/shell.js +109 -8
- package/dist/processes.js +52 -2
- package/dist/registry/config.js +12 -8
- package/dist/registry/holds.js +37 -0
- package/dist/registry/leases.js +109 -0
- package/dist/registry/owner-checks.js +55 -0
- package/dist/registry/owner.js +18 -0
- package/dist/registry/parks.js +60 -0
- package/dist/registry/paths.js +28 -4
- package/dist/registry/ports.js +5 -1
- package/dist/registry/prune.js +643 -24
- package/dist/registry/refusal.js +1 -0
- package/dist/registry/registry.js +1261 -236
- package/dist/registry/rows.js +18 -4
- package/dist/registry/status.js +51 -8
- package/dist/registry/store.js +65 -2
- package/dist/registry/touch.js +180 -0
- package/dist/registry-cli.js +197 -36
- package/dist/services/services.js +227 -56
- package/dist/services/system.js +2 -1
- package/dist/up/answers.js +124 -0
- package/dist/up/doctor.js +29 -4
- package/dist/up/exec.js +29 -4
- package/dist/up/lane-files.js +26 -8
- package/dist/up/lane-services.js +8 -3
- package/dist/up/park.js +146 -0
- package/dist/up/preflight.js +2 -10
- package/dist/up/release.js +20 -24
- package/dist/up/restart.js +9 -2
- package/dist/up/sweep.js +437 -0
- package/dist/up/up.js +38 -15
- package/dist/up/wake.js +124 -0
- package/dist/up/waker.js +49 -0
- package/dist/up-cli.js +6 -1
- package/package.json +2 -2
- package/skills/dev-loop/SKILL.md +7 -5
- package/skills/drive/SKILL.md +4 -0
- package/skills/setup/SKILL.md +2 -2
package/dist/up/sweep.js
ADDED
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { stopEndedBrowsers } from "../browser/lane-browser.js";
|
|
5
|
+
import { realBrowserTools } from "../browser/tools.js";
|
|
6
|
+
import { systemClock } from "../clock.js";
|
|
7
|
+
import { rotateLog } from "../disk-limits.js";
|
|
8
|
+
import { messageOf } from "../driver/errors.js";
|
|
9
|
+
import { realEvidenceSystem } from "../evidence/device.js";
|
|
10
|
+
import { laneEvidence } from "../evidence/evidence.js";
|
|
11
|
+
import { describeStray, stopStrayRecorder, strayDue } from "../evidence/strays.js";
|
|
12
|
+
import { systemProcesses } from "../processes.js";
|
|
13
|
+
import { formatAge, formatTime } from "../registry/format.js";
|
|
14
|
+
import { describeHostSession, describeState, isProven, judgeProcess, ownerState, worktreeState } from "../registry/owner.js";
|
|
15
|
+
import { OWNER_CHECK_MS, REAP_RETRY_MS, RETURN_WINDOW_MS } from "../registry/owner-checks.js";
|
|
16
|
+
import { describeRestarts } from "../registry/parks.js";
|
|
17
|
+
import { databasePath } from "../registry/paths.js";
|
|
18
|
+
import { tcpProbe } from "../registry/ports.js";
|
|
19
|
+
import { Registry } from "../registry/registry.js";
|
|
20
|
+
import { laneLastCall, TOUCH_WAIT_MS } from "../registry/touch.js";
|
|
21
|
+
import { leadersAliveAt } from "../services/services.js";
|
|
22
|
+
import { LANE_FILES_KEEP_MS, laneFolderIds, pruneEndedLaneFiles } from "./lane-files.js";
|
|
23
|
+
import { attemptId, deadHolder, PARK_RETRY_MS, parkLane } from "./park.js";
|
|
24
|
+
import { tearDownLane } from "./release.js";
|
|
25
|
+
import { tearDownEndedResources } from "./resources.js";
|
|
26
|
+
export const SWEEP_COMMAND = 'sweep';
|
|
27
|
+
/** How long after starting a sweeper the commands passing by leave the sweep to it. */
|
|
28
|
+
export const SWEEP_RESPAWN_MS = 2 * 60_000;
|
|
29
|
+
/** How often each running MCP server passes by, for every lane on the Mac. */
|
|
30
|
+
export const SWEEP_INTERVAL_MS = 60_000;
|
|
31
|
+
export const SWEEP_LOG_MAX_BYTES = 1024 * 1024;
|
|
32
|
+
/**
|
|
33
|
+
* How old the moment a lane's service leader was last seen running may grow before a sweep is due to look again. A stop
|
|
34
|
+
* whose leader has exited proves ours only the processes of its group that started before that moment, so a longer
|
|
35
|
+
* wait leaves more of them unproven; a shorter one starts a sweeper more often while the lanes are in use.
|
|
36
|
+
*/
|
|
37
|
+
export const ALIVE_REFRESH_MS = 10 * 60_000;
|
|
38
|
+
function markFile(dir) {
|
|
39
|
+
return path.join(dir, 'sweep.json');
|
|
40
|
+
}
|
|
41
|
+
export function sweepLog(dir) {
|
|
42
|
+
return path.join(dir, 'sweep.log');
|
|
43
|
+
}
|
|
44
|
+
function readMark(dir) {
|
|
45
|
+
try {
|
|
46
|
+
const raw = JSON.parse(fs.readFileSync(markFile(dir), 'utf8'));
|
|
47
|
+
if (typeof raw.startedAt !== 'number')
|
|
48
|
+
return null;
|
|
49
|
+
return { startedAt: raw.startedAt, finishedAt: typeof raw.finishedAt === 'number' ? raw.finishedAt : null };
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function writeMark(dir, mark) {
|
|
56
|
+
const file = markFile(dir);
|
|
57
|
+
const tmp = `${file}.${String(process.pid)}.tmp`;
|
|
58
|
+
try {
|
|
59
|
+
fs.writeFileSync(tmp, JSON.stringify(mark));
|
|
60
|
+
fs.renameSync(tmp, file);
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
fs.rmSync(tmp, { force: true });
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function unused(dir, candidate, idleSince) {
|
|
67
|
+
return laneLastCall(dir, { ...candidate.ref, lastUsedAt: candidate.lastUsedAt }) <= idleSince;
|
|
68
|
+
}
|
|
69
|
+
/** Whether no check or call has shown the lane's owner alive for OWNER_CHECK_MS, so the sweep asks the process table. */
|
|
70
|
+
function unseen(dir, lane, now) {
|
|
71
|
+
if (lane.gone || !lane.owner.process)
|
|
72
|
+
return false;
|
|
73
|
+
const since = now - OWNER_CHECK_MS;
|
|
74
|
+
if (Math.max(lane.checkedAt ?? Number.NEGATIVE_INFINITY, lane.lastUsedAt) > since)
|
|
75
|
+
return false;
|
|
76
|
+
return laneLastCall(dir, { ...lane.ref, lastUsedAt: lane.lastUsedAt }) <= since;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Whether a lane records a service whose leader no check has seen running for ALIVE_REFRESH_MS, and no sweep has
|
|
80
|
+
* finished since then: one that did found that leader gone, or could not tell, and waits as long for the next look.
|
|
81
|
+
*/
|
|
82
|
+
function aliveRefreshDue(registry, mark, now) {
|
|
83
|
+
const since = now - ALIVE_REFRESH_MS;
|
|
84
|
+
if ((mark?.finishedAt ?? Number.NEGATIVE_INFINITY) > since)
|
|
85
|
+
return false;
|
|
86
|
+
return registry.serviceProcesses().some(p => (p.process.aliveAt ?? Number.NEGATIVE_INFINITY) <= since);
|
|
87
|
+
}
|
|
88
|
+
/** The lane folders, listed before `lanes` was read, of lanes that have ended. */
|
|
89
|
+
function endedFolders(folders, lanes) {
|
|
90
|
+
const live = new Set(lanes.map(l => l.ref.id));
|
|
91
|
+
return folders.filter(id => !live.has(id));
|
|
92
|
+
}
|
|
93
|
+
/** Whether a lane whose owner is gone is still to be parked: never yet, or a park left it running or stopped partway. */
|
|
94
|
+
function goneParkDue(lane, now) {
|
|
95
|
+
const park = lane.park;
|
|
96
|
+
if (!park)
|
|
97
|
+
return true;
|
|
98
|
+
if (park.since > now - PARK_RETRY_MS)
|
|
99
|
+
return false;
|
|
100
|
+
return park.state !== 'parked' || lane.running;
|
|
101
|
+
}
|
|
102
|
+
/** Whether a lane whose owner is gone has stayed gone through its window, and no process is ending it lately. */
|
|
103
|
+
function reapDue(lane, now) {
|
|
104
|
+
const gone = lane.gone;
|
|
105
|
+
if (!gone || now < gone.foundAt + RETURN_WINDOW_MS || lane.running)
|
|
106
|
+
return false;
|
|
107
|
+
return gone.reapingSince === null || gone.reapingSince <= now - REAP_RETRY_MS;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* 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
|
+
* 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
|
+
* of each lane the registry has as unused and the lane folders' list; no process starts. With `runningSince`, the start
|
|
114
|
+
* of a sweep still running, only an owner check that fell due after that start counts, since that sweep covers the rest.
|
|
115
|
+
*/
|
|
116
|
+
export function sweepDue(site, runningSince) {
|
|
117
|
+
if (!fs.existsSync(databasePath(site.dir)))
|
|
118
|
+
return false;
|
|
119
|
+
const now = (site.clock ?? systemClock).now();
|
|
120
|
+
const registry = Registry.open({
|
|
121
|
+
dir: site.dir,
|
|
122
|
+
processes: site.processes ?? systemProcesses,
|
|
123
|
+
ports: tcpProbe,
|
|
124
|
+
devices: {},
|
|
125
|
+
lockWaitMs: TOUCH_WAIT_MS,
|
|
126
|
+
...(site.clock ? { clock: site.clock } : {}),
|
|
127
|
+
});
|
|
128
|
+
try {
|
|
129
|
+
const idleSince = now - registry.config.parkMs;
|
|
130
|
+
const folders = laneFolderIds(site.dir);
|
|
131
|
+
const lanes = registry.sweepList();
|
|
132
|
+
if (runningSince !== undefined) {
|
|
133
|
+
return lanes.some(l => unseen(site.dir, l, now) && !unseen(site.dir, l, runningSince));
|
|
134
|
+
}
|
|
135
|
+
const gone = new Set(lanes.filter(l => l.gone).map(l => l.ref.id));
|
|
136
|
+
if (lanes.some(l => (l.gone ? goneParkDue(l, now) || reapDue(l, now) : unseen(site.dir, l, now))))
|
|
137
|
+
return true;
|
|
138
|
+
if (aliveRefreshDue(registry, readMark(site.dir), now))
|
|
139
|
+
return true;
|
|
140
|
+
if (endedFolders(folders, lanes).some(id => strayDue(laneEvidence(site.dir, id), now)))
|
|
141
|
+
return true;
|
|
142
|
+
return registry
|
|
143
|
+
.parkCandidates(idleSince, now - PARK_RETRY_MS)
|
|
144
|
+
.some(c => c.hold === null && !gone.has(c.ref.id) && unused(site.dir, c, idleSince));
|
|
145
|
+
}
|
|
146
|
+
finally {
|
|
147
|
+
registry.close();
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* The sweep every command runs as it passes by: when there is work no sweeper started lately covers, it starts one
|
|
152
|
+
* with `start`, which does the work in a process of its own, so the command never waits on it. It never fails the
|
|
153
|
+
* command.
|
|
154
|
+
*/
|
|
155
|
+
export function passBy(site, start) {
|
|
156
|
+
try {
|
|
157
|
+
const now = (site.clock ?? systemClock).now();
|
|
158
|
+
const mark = readMark(site.dir);
|
|
159
|
+
const running = mark?.finishedAt === null && now - mark.startedAt < SWEEP_RESPAWN_MS ? mark.startedAt : undefined;
|
|
160
|
+
if (!sweepDue(site, running))
|
|
161
|
+
return false;
|
|
162
|
+
writeMark(site.dir, { startedAt: now, finishedAt: null });
|
|
163
|
+
start(site.dir);
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
catch {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* What a running MCP server does every SWEEP_INTERVAL_MS: it lands the device work its own calls could not record as
|
|
172
|
+
* done, then passes by. Neither waits on a teardown, so the server's tool calls never wait on its sweep.
|
|
173
|
+
*/
|
|
174
|
+
export function sweepTick(site, devices, start) {
|
|
175
|
+
settleLeftovers(site, devices);
|
|
176
|
+
return passBy(site, start);
|
|
177
|
+
}
|
|
178
|
+
function settleLeftovers(site, devices) {
|
|
179
|
+
if (!Registry.holdsLeftovers(site.dir))
|
|
180
|
+
return;
|
|
181
|
+
try {
|
|
182
|
+
const registry = Registry.open({
|
|
183
|
+
dir: site.dir,
|
|
184
|
+
processes: site.processes ?? systemProcesses,
|
|
185
|
+
ports: tcpProbe,
|
|
186
|
+
devices,
|
|
187
|
+
lockWaitMs: TOUCH_WAIT_MS,
|
|
188
|
+
...(site.clock ? { clock: site.clock } : {}),
|
|
189
|
+
});
|
|
190
|
+
try {
|
|
191
|
+
registry.settleOwn();
|
|
192
|
+
}
|
|
193
|
+
finally {
|
|
194
|
+
registry.close();
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
catch {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
/** Starts `<cli> sweep` detached, appending to the state folder's sweep log. */
|
|
202
|
+
export function sweeper(cli, env) {
|
|
203
|
+
return dir => {
|
|
204
|
+
const log = sweepLog(dir);
|
|
205
|
+
rotateLog(log, SWEEP_LOG_MAX_BYTES);
|
|
206
|
+
const fd = fs.openSync(log, 'a');
|
|
207
|
+
try {
|
|
208
|
+
const child = spawn(process.execPath, [cli, SWEEP_COMMAND], { env, detached: true, stdio: ['ignore', fd, fd] });
|
|
209
|
+
child.unref();
|
|
210
|
+
}
|
|
211
|
+
finally {
|
|
212
|
+
fs.closeSync(fd);
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
/** Records the moment each service leader the lanes record was seen running, as `waitForService` does at its start. */
|
|
217
|
+
function refreshLeaders(deps, registry, say) {
|
|
218
|
+
const leaders = registry.serviceProcesses();
|
|
219
|
+
const seen = leadersAliveAt(deps.system, leaders, deps.clock);
|
|
220
|
+
if (!seen) {
|
|
221
|
+
say(`could not read whether the services of lanes ${[...new Set(leaders.map(l => l.lane))].join(', ')} run`);
|
|
222
|
+
return [];
|
|
223
|
+
}
|
|
224
|
+
return registry
|
|
225
|
+
.recordLeadersAlive(seen.alive, seen.at)
|
|
226
|
+
.map(l => ({ lane: l.lane, service: l.process.service, aliveAt: seen.at }));
|
|
227
|
+
}
|
|
228
|
+
function judged(lane) {
|
|
229
|
+
return { id: lane.ref.id, ownerKey: lane.ownerKey, process: lane.owner.process };
|
|
230
|
+
}
|
|
231
|
+
function untilEnd(lane, foundAt) {
|
|
232
|
+
const session = describeHostSession(lane.owner);
|
|
233
|
+
const ends = `ends at ${formatTime(foundAt + RETURN_WINDOW_MS)}`;
|
|
234
|
+
return session ? `${ends} unless ${session} comes back` : `${ends}; nothing can take it back`;
|
|
235
|
+
}
|
|
236
|
+
async function reapLane(deps, registry, lane, dead) {
|
|
237
|
+
const { ref } = lane;
|
|
238
|
+
const attempt = attemptId();
|
|
239
|
+
const start = registry.beginReap(ref, attempt, dead);
|
|
240
|
+
if (start.kind === 'left')
|
|
241
|
+
return { kind: 'left', lane: ref.id, reason: start.reason };
|
|
242
|
+
try {
|
|
243
|
+
const teardown = await tearDownLane({ ...deps, sim: deps.sim ?? null, browser: deps.browser ?? realBrowserTools }, deps.dir, start.lane);
|
|
244
|
+
const notes = [...teardown.notes];
|
|
245
|
+
const ended = registry.endReaped(ref, attempt, notes);
|
|
246
|
+
if (!ended)
|
|
247
|
+
return { kind: 'left', lane: ref.id, reason: 'another call took it while it was being ended' };
|
|
248
|
+
const after = [
|
|
249
|
+
...(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 })),
|
|
251
|
+
];
|
|
252
|
+
return {
|
|
253
|
+
kind: 'ended',
|
|
254
|
+
lane: ref.id,
|
|
255
|
+
state: start.gone.state,
|
|
256
|
+
foundAt: start.gone.foundAt,
|
|
257
|
+
notes: [...notes, ...after.flatMap(t => t.notes)],
|
|
258
|
+
problems: [...teardown.problems, ...after.flatMap(t => t.problems)],
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
catch (error) {
|
|
262
|
+
registry.abortReap(ref, attempt);
|
|
263
|
+
throw error;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* 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
|
+
* owners no check or call has shown alive lately. A lane whose owner it finds gone is parked at once, and ended with
|
|
269
|
+
* 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.
|
|
271
|
+
*/
|
|
272
|
+
export async function runSweep(deps) {
|
|
273
|
+
const say = deps.log ?? (() => undefined);
|
|
274
|
+
const report = {
|
|
275
|
+
checked: [],
|
|
276
|
+
found: [],
|
|
277
|
+
outcomes: [],
|
|
278
|
+
held: [],
|
|
279
|
+
reaps: [],
|
|
280
|
+
alive: [],
|
|
281
|
+
recorders: [],
|
|
282
|
+
pruned: [],
|
|
283
|
+
problems: [],
|
|
284
|
+
};
|
|
285
|
+
const problem = (lane, line) => {
|
|
286
|
+
report.problems.push(`lane ${lane}: ${line}`);
|
|
287
|
+
say(`lane ${lane}: PROBLEM: ${line}`);
|
|
288
|
+
};
|
|
289
|
+
const registry = Registry.open({
|
|
290
|
+
dir: deps.dir,
|
|
291
|
+
processes: deps.processes,
|
|
292
|
+
ports: deps.ports,
|
|
293
|
+
devices: deps.devices,
|
|
294
|
+
clock: deps.clock,
|
|
295
|
+
...(deps.config ? { config: deps.config } : {}),
|
|
296
|
+
...(deps.self ? { self: deps.self } : {}),
|
|
297
|
+
});
|
|
298
|
+
try {
|
|
299
|
+
const now = deps.clock.now();
|
|
300
|
+
const lanes = registry.sweepList();
|
|
301
|
+
report.alive = refreshLeaders(deps, registry, say);
|
|
302
|
+
const unseenLanes = new Set(lanes.filter(l => unseen(deps.dir, l, now)));
|
|
303
|
+
const stuckReapers = lanes.flatMap(l => (l.gone?.reaper && reapDue(l, now) ? [l.gone.reaper] : []));
|
|
304
|
+
const pids = [
|
|
305
|
+
...[...unseenLanes].flatMap(l => (l.owner.process ? [l.owner.process.pid] : [])),
|
|
306
|
+
...stuckReapers.map(r => r.pid),
|
|
307
|
+
];
|
|
308
|
+
const table = pids.length > 0 ? deps.processes.startTimes(pids) : new Map();
|
|
309
|
+
report.checked = [...unseenLanes].map(l => l.ref.id);
|
|
310
|
+
const seen = [];
|
|
311
|
+
const gone = [];
|
|
312
|
+
for (const lane of lanes) {
|
|
313
|
+
if (lane.gone)
|
|
314
|
+
continue;
|
|
315
|
+
const verdict = unseenLanes.has(lane)
|
|
316
|
+
? judgeProcess(lane.owner.process, table)
|
|
317
|
+
: lane.owner.process
|
|
318
|
+
? 'alive'
|
|
319
|
+
: 'cant-tell';
|
|
320
|
+
const state = ownerState(verdict, worktreeState(lane.worktree));
|
|
321
|
+
if (isProven(state))
|
|
322
|
+
gone.push({ ...judged(lane), state });
|
|
323
|
+
else if (unseenLanes.has(lane))
|
|
324
|
+
seen.push(judged(lane));
|
|
325
|
+
}
|
|
326
|
+
const found = new Set(registry.noteOwners(seen, gone));
|
|
327
|
+
for (const g of gone) {
|
|
328
|
+
const lane = lanes.find(l => l.ref.id === g.id);
|
|
329
|
+
if (!found.has(g.id) || !lane)
|
|
330
|
+
continue;
|
|
331
|
+
report.found.push({ lane: g.id, state: g.state });
|
|
332
|
+
say(`found the owner of lane ${g.id} gone (${describeState(g.state)}): the lane is parked now, and it ${untilEnd(lane, now)}`);
|
|
333
|
+
if (lane.hold)
|
|
334
|
+
say(`ended the hold on lane ${g.id}, "${lane.hold.reason}": ${describeState(g.state)}`);
|
|
335
|
+
}
|
|
336
|
+
const goneLanes = (found.size > 0 ? registry.sweepList() : lanes).filter(l => l.gone !== null);
|
|
337
|
+
for (const lane of goneLanes) {
|
|
338
|
+
if (!lane.gone || !goneParkDue(lane, now))
|
|
339
|
+
continue;
|
|
340
|
+
try {
|
|
341
|
+
const outcome = await parkLane(deps, registry, lane, 'owner-gone');
|
|
342
|
+
report.outcomes.push(outcome);
|
|
343
|
+
if (outcome.kind === 'parked') {
|
|
344
|
+
say(`parked lane ${outcome.lane}, whose owner is gone; it ${untilEnd(lane, lane.gone.foundAt)}`);
|
|
345
|
+
for (const line of outcome.problems)
|
|
346
|
+
problem(outcome.lane, line);
|
|
347
|
+
}
|
|
348
|
+
else
|
|
349
|
+
say(`left lane ${outcome.lane} as it was: ${outcome.reason}`);
|
|
350
|
+
}
|
|
351
|
+
catch (error) {
|
|
352
|
+
problem(lane.ref.id, `parking stopped partway: ${messageOf(error)}`);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
const idleSince = now - registry.config.parkMs;
|
|
356
|
+
const goneIds = new Set(goneLanes.map(l => l.ref.id));
|
|
357
|
+
for (const candidate of registry.parkCandidates(idleSince, now - PARK_RETRY_MS)) {
|
|
358
|
+
if (goneIds.has(candidate.ref.id) || !unused(deps.dir, candidate, idleSince))
|
|
359
|
+
continue;
|
|
360
|
+
if (candidate.hold) {
|
|
361
|
+
report.held.push(candidate.ref.id);
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
try {
|
|
365
|
+
const outcome = await parkLane(deps, registry, candidate, idleSince);
|
|
366
|
+
report.outcomes.push(outcome);
|
|
367
|
+
if (outcome.kind === 'parked') {
|
|
368
|
+
say(`parked lane ${outcome.lane}; its next call brings back ${describeRestarts(outcome.restarts)}`);
|
|
369
|
+
for (const line of outcome.notes)
|
|
370
|
+
say(`lane ${outcome.lane}: ${line}`);
|
|
371
|
+
for (const line of outcome.problems)
|
|
372
|
+
problem(outcome.lane, line);
|
|
373
|
+
}
|
|
374
|
+
else
|
|
375
|
+
say(`left lane ${outcome.lane} awake: ${outcome.reason}`);
|
|
376
|
+
}
|
|
377
|
+
catch (error) {
|
|
378
|
+
problem(candidate.ref.id, `parking stopped partway: ${messageOf(error)}`);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
for (const lane of goneIds.size > 0 ? registry.sweepList() : []) {
|
|
382
|
+
const reaper = lane.gone?.reaper ?? null;
|
|
383
|
+
if (!reapDue(lane, now))
|
|
384
|
+
continue;
|
|
385
|
+
const dead = [
|
|
386
|
+
...(reaper && isProven(judgeProcess(reaper, table)) ? [reaper] : []),
|
|
387
|
+
...[deadHolder(lane.park, deps.processes)].filter((p) => p !== null),
|
|
388
|
+
];
|
|
389
|
+
try {
|
|
390
|
+
const outcome = await reapLane(deps, registry, lane, dead);
|
|
391
|
+
report.reaps.push(outcome);
|
|
392
|
+
if (outcome.kind === 'ended') {
|
|
393
|
+
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`);
|
|
394
|
+
for (const line of outcome.notes)
|
|
395
|
+
say(`lane ${outcome.lane}: ${line}`);
|
|
396
|
+
for (const line of outcome.problems)
|
|
397
|
+
problem(outcome.lane, line);
|
|
398
|
+
}
|
|
399
|
+
else
|
|
400
|
+
say(`did not end lane ${outcome.lane} yet: ${outcome.reason}`);
|
|
401
|
+
}
|
|
402
|
+
catch (error) {
|
|
403
|
+
problem(lane.ref.id, `ending it stopped partway: ${messageOf(error)}`);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
const folders = laneFolderIds(deps.dir);
|
|
407
|
+
for (const id of endedFolders(folders, registry.sweepList())) {
|
|
408
|
+
const evidence = laneEvidence(deps.dir, id);
|
|
409
|
+
if (!strayDue(evidence, now))
|
|
410
|
+
continue;
|
|
411
|
+
try {
|
|
412
|
+
const stray = await stopStrayRecorder(deps.evidence ?? realEvidenceSystem, evidence, id, deps.clock);
|
|
413
|
+
if (!stray)
|
|
414
|
+
continue;
|
|
415
|
+
report.recorders.push(stray);
|
|
416
|
+
if (stray.kind === 'cant-tell' || stray.kind === 'survived')
|
|
417
|
+
problem(id, describeStray(stray));
|
|
418
|
+
else
|
|
419
|
+
say(`lane ${id}: ${describeStray(stray)}`);
|
|
420
|
+
if (stray.kind === 'stopped' || stray.kind === 'survived')
|
|
421
|
+
for (const line of stray.problems)
|
|
422
|
+
problem(id, line);
|
|
423
|
+
}
|
|
424
|
+
catch (error) {
|
|
425
|
+
problem(id, `stopping the recorder it left stopped partway: ${messageOf(error)}`);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
report.pruned = pruneEndedLaneFiles(deps.dir, registry.keptLaneIds(now - LANE_FILES_KEEP_MS), now, deps.env);
|
|
429
|
+
if (report.pruned.length > 0)
|
|
430
|
+
say(`removed the files of ended lanes ${report.pruned.join(', ')}`);
|
|
431
|
+
return report;
|
|
432
|
+
}
|
|
433
|
+
finally {
|
|
434
|
+
registry.close();
|
|
435
|
+
writeMark(deps.dir, { startedAt: readMark(deps.dir)?.startedAt ?? deps.clock.now(), finishedAt: deps.clock.now() });
|
|
436
|
+
}
|
|
437
|
+
}
|
package/dist/up/up.js
CHANGED
|
@@ -27,15 +27,17 @@ import { resolveOwner } from "../registry/owner.js";
|
|
|
27
27
|
import { stateDir } from "../registry/paths.js";
|
|
28
28
|
import { UsageError } from "../registry/refusal.js";
|
|
29
29
|
import { Registry } from "../registry/registry.js";
|
|
30
|
+
import { keepTouching, recordCall } from "../registry/touch.js";
|
|
30
31
|
import { resolveFolder } from "../registry-cli.js";
|
|
31
32
|
import { explainInspection, inspectService } from "../services/services.js";
|
|
32
|
-
import { laneDir, hookLog, pruneEndedLaneFiles, readUpRecord, writeUpRecord } from "./lane-files.js";
|
|
33
|
+
import { LANE_FILES_KEEP_MS, laneDir, hookLog, pruneEndedLaneFiles, readUpRecord, writeUpRecord } from "./lane-files.js";
|
|
33
34
|
import { claimedPort, recordedProcess, startClaimedService, stopClaimedService } from "./lane-services.js";
|
|
35
|
+
import { rememberAnswers } from "./answers.js";
|
|
34
36
|
import { bundlerPort, diagnose, dialArguments, reversePorts } from "./doctor.js";
|
|
35
37
|
import { driverVariables } from "./maestro-env.js";
|
|
36
38
|
import { androidBuildEnvironment, projectFindings, toolchainFindings, Unmet } from "./preflight.js";
|
|
37
|
-
import { stopEndedLanes } from "./release.js";
|
|
38
39
|
import { createResources, tearDownEndedResources } from "./resources.js";
|
|
40
|
+
import { settleWake, takeLane } from "./wake.js";
|
|
39
41
|
function seconds(ms) {
|
|
40
42
|
return `${(ms / 1000).toFixed(1)} s`;
|
|
41
43
|
}
|
|
@@ -76,7 +78,13 @@ async function hook(ctx, name, extraEnv = {}) {
|
|
|
76
78
|
const log = hookLog(ctx.dir, name);
|
|
77
79
|
ctx.deps.log(`${TOOL} up: ${name}: ${command} (log: ${log})`);
|
|
78
80
|
ctx.registry.touch(ctx.ref, `up: ${name}`);
|
|
79
|
-
|
|
81
|
+
const stopTouching = keepTouching({ dir: ctx.registry.dir, processes: ctx.deps.processes, clock: ctx.deps.clock }, ctx.ref, `up: ${name}`, ctx.deps.heartbeatMs);
|
|
82
|
+
try {
|
|
83
|
+
return await runHook(name, command, { cwd: ctx.config.dir, env: { ...ctx.env, ...extraEnv }, log });
|
|
84
|
+
}
|
|
85
|
+
finally {
|
|
86
|
+
stopTouching();
|
|
87
|
+
}
|
|
80
88
|
}
|
|
81
89
|
async function bootDevice(sim, udid, reboot) {
|
|
82
90
|
const device = await sim.device(udid);
|
|
@@ -144,6 +152,7 @@ async function buildApp(ctx, mode) {
|
|
|
144
152
|
}
|
|
145
153
|
const cache = new BuildCache({ dir: await cacheDirFor(config.dir), config: cacheConfig });
|
|
146
154
|
const builder = identityOf(ctx.deps.processes, process.pid);
|
|
155
|
+
const digests = new Map();
|
|
147
156
|
return cachedBuild({
|
|
148
157
|
cache,
|
|
149
158
|
variant: config.app.variant,
|
|
@@ -159,6 +168,7 @@ async function buildApp(ctx, mode) {
|
|
|
159
168
|
: await ctx.deps.toolchain.androidToolchain(config.dir, env),
|
|
160
169
|
...(cacheConfig.envFile !== undefined ? { envFile: cacheConfig.envFile } : {}),
|
|
161
170
|
env: ctx.env,
|
|
171
|
+
digests,
|
|
162
172
|
}),
|
|
163
173
|
install,
|
|
164
174
|
build,
|
|
@@ -199,8 +209,10 @@ async function ensureService(ctx, service) {
|
|
|
199
209
|
const recorded = recordedProcess(lane, service.name);
|
|
200
210
|
if (recorded) {
|
|
201
211
|
const seen = await inspectService(deps.system, service, port, recorded);
|
|
202
|
-
if (seen.healthy)
|
|
203
|
-
|
|
212
|
+
if (seen.healthy) {
|
|
213
|
+
const unread = seen.listeners === null ? `; the listener on ${String(port)} could not be read` : '';
|
|
214
|
+
return `already running on ${String(port)} (pid ${String(recorded.pid)}); ${seen.readyDetail}${unread}`;
|
|
215
|
+
}
|
|
204
216
|
const stopped = await stopClaimedService(ctx, recorded);
|
|
205
217
|
const started = await startClaimedService(ctx, service, port);
|
|
206
218
|
return `${started}, replacing pid ${String(recorded.pid)}, which failed its check (${explainInspection(seen)}): ${stopped}`;
|
|
@@ -335,7 +347,7 @@ async function deviceSteps(ctx, run, options, booting, approval, setup) {
|
|
|
335
347
|
}, report => report.changed
|
|
336
348
|
? `applied ${report.steps
|
|
337
349
|
.filter(s => s.changed)
|
|
338
|
-
.map(s => s.step)
|
|
350
|
+
.map(s => (s.unconfirmed ? `${s.step} (unconfirmed: ${s.unconfirmed})` : s.step))
|
|
339
351
|
.join(', ')}`
|
|
340
352
|
: 'already in place');
|
|
341
353
|
await run.step('launch', () => launch(ctx), detail => detail);
|
|
@@ -356,7 +368,7 @@ async function deviceSteps(ctx, run, options, booting, approval, setup) {
|
|
|
356
368
|
await run.step('driver', async () => {
|
|
357
369
|
registry.touch(ctx.ref, 'up: driver');
|
|
358
370
|
try {
|
|
359
|
-
await deps.startDriver(lane, stateDir(deps.env));
|
|
371
|
+
await deps.startDriver(lane, stateDir(deps.env), (await deps.toolchain.findJava17(deps.env))?.home);
|
|
360
372
|
return 'started; the first look is warm';
|
|
361
373
|
}
|
|
362
374
|
catch (error) {
|
|
@@ -416,7 +428,8 @@ function describeResources(made) {
|
|
|
416
428
|
];
|
|
417
429
|
return parts.filter(p => p !== '').join('; ');
|
|
418
430
|
}
|
|
419
|
-
export async function up(
|
|
431
|
+
export async function up(given, options) {
|
|
432
|
+
const { deps, forgetDeviceStates } = rememberAnswers(given);
|
|
420
433
|
const started = deps.clock.now();
|
|
421
434
|
const run = new Run(deps);
|
|
422
435
|
const start = options.worktree ?? deps.cwd;
|
|
@@ -442,17 +455,28 @@ export async function up(deps, options) {
|
|
|
442
455
|
: new UsageError('this machine has no iOS simulators', 'run up on a Mac with Xcode installed');
|
|
443
456
|
}
|
|
444
457
|
const state = stateDir(deps.env);
|
|
458
|
+
const registryConfig = withProjectBlocks(loadConfig(state), projectBlocks(config), config.file);
|
|
445
459
|
const registry = Registry.open({
|
|
446
460
|
dir: state,
|
|
447
461
|
processes: deps.processes,
|
|
448
462
|
ports: deps.ports,
|
|
449
463
|
devices: deps.devices,
|
|
450
|
-
config:
|
|
464
|
+
config: registryConfig,
|
|
451
465
|
clock: deps.clock,
|
|
452
466
|
...(deps.pollMs !== undefined ? { pollMs: deps.pollMs } : {}),
|
|
467
|
+
...(deps.passBy ? { sweepSoon: deps.passBy } : {}),
|
|
453
468
|
});
|
|
469
|
+
let woken = null;
|
|
470
|
+
const entry = { stateDir: state, processes: deps.processes, clock: deps.clock };
|
|
454
471
|
try {
|
|
455
472
|
const owner = resolveOwner(deps.env, deps.processes, deps.selfPid);
|
|
473
|
+
const before = options.waking ? null : registry.takeBack(owner, folder.worktree);
|
|
474
|
+
if (before) {
|
|
475
|
+
const ref = { id: before.id, token: before.token };
|
|
476
|
+
recordCall(state, ref, deps.clock.now());
|
|
477
|
+
if (registry.parkOf(before.id))
|
|
478
|
+
woken = await takeLane(entry, ref);
|
|
479
|
+
}
|
|
456
480
|
const outcome = await run.step('claim', async () => {
|
|
457
481
|
const stop = deps.onCancel?.(() => registry.cancelPending());
|
|
458
482
|
try {
|
|
@@ -480,12 +504,8 @@ export async function up(deps, options) {
|
|
|
480
504
|
}, 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(', ')}`);
|
|
481
505
|
for (const note of outcome.notes)
|
|
482
506
|
deps.log(`${TOOL} up: ${note}`);
|
|
483
|
-
for (const e of [...outcome.freed, ...outcome.tookOver]) {
|
|
484
|
-
deps.log(`${TOOL} up: ended lane ${e.id} of ${e.owner} in ${e.worktree} (${e.reason}, idle ${formatAge(e.endedAt - e.lastUsedAt)})`);
|
|
485
|
-
}
|
|
486
507
|
const browsers = deps.browser ?? realBrowserTools;
|
|
487
508
|
const ended = [
|
|
488
|
-
...(await stopEndedLanes(deps, state, [...outcome.freed, ...outcome.tookOver])),
|
|
489
509
|
...(await stopEndedBrowsers(browsers, registry, state, deps.clock)),
|
|
490
510
|
...(await tearDownEndedResources(deps, registry, state)),
|
|
491
511
|
];
|
|
@@ -508,7 +528,7 @@ export async function up(deps, options) {
|
|
|
508
528
|
pinned: previous?.pinned ?? false,
|
|
509
529
|
updatedAt: deps.clock.now(),
|
|
510
530
|
});
|
|
511
|
-
pruneEndedLaneFiles(state,
|
|
531
|
+
pruneEndedLaneFiles(state, registry.keptLaneIds(deps.clock.now() - LANE_FILES_KEEP_MS), deps.clock.now(), deps.env);
|
|
512
532
|
const ctx = {
|
|
513
533
|
deps,
|
|
514
534
|
registry,
|
|
@@ -554,7 +574,7 @@ export async function up(deps, options) {
|
|
|
554
574
|
throw error;
|
|
555
575
|
}
|
|
556
576
|
const booting = bootingWithSetup ?? (target ? startBoot(target) : null);
|
|
557
|
-
if (config.resources.length > 0) {
|
|
577
|
+
if (config.resources.length > 0 && !options.waking) {
|
|
558
578
|
try {
|
|
559
579
|
const made = await run.step('resources', () => createResources(ctx, ctx.lane), describeResources);
|
|
560
580
|
ctx.lane = made.lane;
|
|
@@ -582,6 +602,7 @@ export async function up(deps, options) {
|
|
|
582
602
|
if (!fresh)
|
|
583
603
|
throw new Error(`lane ${lane.id} ended while up was running`);
|
|
584
604
|
registry.touch(ctx.ref, 'up: doctor');
|
|
605
|
+
forgetDeviceStates();
|
|
585
606
|
const doctor = await run.step('doctor', () => diagnose(deps, {
|
|
586
607
|
config,
|
|
587
608
|
configProblem: null,
|
|
@@ -605,6 +626,8 @@ export async function up(deps, options) {
|
|
|
605
626
|
};
|
|
606
627
|
}
|
|
607
628
|
finally {
|
|
629
|
+
if (woken)
|
|
630
|
+
settleWake(entry, woken, null);
|
|
608
631
|
registry.close();
|
|
609
632
|
}
|
|
610
633
|
}
|