agentlane 0.0.0 → 0.4.0-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +118 -0
- package/LICENSE +27 -0
- package/README.md +167 -2
- package/dist/agents/claude-plugin.js +79 -0
- package/dist/agents/cli.js +258 -0
- package/dist/agents/detect.js +88 -0
- package/dist/agents/init.js +251 -0
- package/dist/agents/launcher.js +32 -0
- package/dist/agents/package.js +119 -0
- package/dist/agents/skills.js +59 -0
- package/dist/agents/toml.js +325 -0
- package/dist/agents/wiring-check.js +117 -0
- package/dist/agents/wiring.js +374 -0
- package/dist/bench/host.js +104 -0
- package/dist/bench/live.js +511 -0
- package/dist/bench/mcp-client.js +91 -0
- package/dist/bench/model.js +15 -0
- package/dist/bench/report.js +162 -0
- package/dist/bench/run.js +121 -0
- package/dist/bench/schedule.js +140 -0
- package/dist/bench/stats.js +59 -0
- package/dist/bench-cli.js +167 -0
- package/dist/browser/aria.js +184 -0
- package/dist/browser/control.js +203 -0
- package/dist/browser/env.js +118 -0
- package/dist/browser/evidence.js +44 -0
- package/dist/browser/install.js +361 -0
- package/dist/browser/kinds.js +10 -0
- package/dist/browser/lane-browser.js +224 -0
- package/dist/browser/libraries.js +56 -0
- package/dist/browser/paths.js +29 -0
- package/dist/browser/release.js +121 -0
- package/dist/browser/screen.js +19 -0
- package/dist/browser/signin.js +154 -0
- package/dist/browser/stop.js +163 -0
- package/dist/browser/tools.js +25 -0
- package/dist/cache/build.js +154 -0
- package/dist/cache/config.js +186 -0
- package/dist/cache/index.js +4 -0
- package/dist/cache/key.js +196 -0
- package/dist/cache/store.js +303 -0
- package/dist/cli.js +178 -0
- package/dist/clock.js +4 -0
- package/dist/devices/adb.js +160 -0
- package/dist/devices/android.js +214 -0
- package/dist/devices/backend.js +7 -0
- package/dist/devices/bundler.js +46 -0
- package/dist/devices/common.js +51 -0
- package/dist/devices/controls.js +65 -0
- package/dist/devices/dial.js +56 -0
- package/dist/devices/emulator.js +396 -0
- package/dist/devices/hygiene.js +202 -0
- package/dist/devices/index.js +5 -0
- package/dist/devices/launch.js +76 -0
- package/dist/devices/simctl.js +119 -0
- package/dist/devices/tools.js +1 -0
- package/dist/devices/variant.js +99 -0
- package/dist/devices/xcrun.js +265 -0
- package/dist/driver/absence.js +54 -0
- package/dist/driver/caller.js +51 -0
- package/dist/driver/client.js +312 -0
- package/dist/driver/diff.js +180 -0
- package/dist/driver/elements.js +70 -0
- package/dist/driver/engine-main.js +13 -0
- package/dist/driver/engine.js +499 -0
- package/dist/driver/errors.js +11 -0
- package/dist/driver/exec.js +66 -0
- package/dist/driver/failure.js +156 -0
- package/dist/driver/flow.js +94 -0
- package/dist/driver/idle.js +100 -0
- package/dist/driver/images.js +68 -0
- package/dist/driver/install.js +272 -0
- package/dist/driver/lock.js +101 -0
- package/dist/driver/look.js +223 -0
- package/dist/driver/maestro-child.js +194 -0
- package/dist/driver/maestro-runs.js +46 -0
- package/dist/driver/paths.js +105 -0
- package/dist/driver/pixels.js +144 -0
- package/dist/driver/plan.js +108 -0
- package/dist/driver/release.js +7 -0
- package/dist/driver/runner.js +42 -0
- package/dist/driver/selector.js +91 -0
- package/dist/driver/session.js +474 -0
- package/dist/driver/suggest.js +86 -0
- package/dist/driver/supervisor.js +251 -0
- package/dist/driver/tree.js +426 -0
- package/dist/driver-cli.js +865 -0
- package/dist/evidence/device.js +177 -0
- package/dist/evidence/evidence.js +304 -0
- package/dist/evidence/files.js +92 -0
- package/dist/hooks/env.js +81 -0
- package/dist/hooks/run.js +101 -0
- package/dist/mcp/server.js +69 -0
- package/dist/mcp/tools.js +246 -0
- package/dist/name.js +22 -0
- package/dist/plugin/aims.js +465 -0
- package/dist/plugin/conventions.js +73 -0
- package/dist/plugin/guard.js +163 -0
- package/dist/plugin/hook-cli.js +216 -0
- package/dist/plugin/session-name.js +28 -0
- package/dist/plugin/shell.js +254 -0
- package/dist/processes.js +77 -0
- package/dist/project/config.js +316 -0
- package/dist/project/schema.js +335 -0
- package/dist/project/validate.js +224 -0
- package/dist/registry/config.js +142 -0
- package/dist/registry/format.js +22 -0
- package/dist/registry/owner.js +265 -0
- package/dist/registry/paths.js +50 -0
- package/dist/registry/ports.js +26 -0
- package/dist/registry/prune.js +160 -0
- package/dist/registry/refusal.js +53 -0
- package/dist/registry/registry.js +1027 -0
- package/dist/registry/resources.js +117 -0
- package/dist/registry/rows.js +115 -0
- package/dist/registry/status.js +85 -0
- package/dist/registry/store.js +274 -0
- package/dist/registry/types.js +1 -0
- package/dist/registry-cli.js +544 -0
- package/dist/services/services.js +268 -0
- package/dist/services/system.js +189 -0
- package/dist/up/deps.js +1 -0
- package/dist/up/doctor.js +424 -0
- package/dist/up/exec.js +133 -0
- package/dist/up/lane-files.js +75 -0
- package/dist/up/lane-services.js +38 -0
- package/dist/up/maestro-env.js +172 -0
- package/dist/up/preflight.js +232 -0
- package/dist/up/release.js +148 -0
- package/dist/up/resources.js +178 -0
- package/dist/up/restart.js +87 -0
- package/dist/up/setup.js +28 -0
- package/dist/up/up.js +610 -0
- package/dist/up-cli.js +258 -0
- package/dist/usage.js +16 -0
- package/package.json +58 -3
- package/schema/agentlane.schema.json +675 -0
- package/skills/create-verification-skill/LICENSE +21 -0
- package/skills/create-verification-skill/SKILL.md +88 -0
- package/skills/dev-loop/LICENSE +21 -0
- package/skills/dev-loop/SKILL.md +61 -0
- package/skills/device/SKILL.md +8 -0
- package/skills/drive/SKILL.md +64 -0
- package/skills/maintain-verification-skill/LICENSE +21 -0
- package/skills/maintain-verification-skill/SKILL.md +42 -0
- package/skills/setup/SKILL.md +80 -0
|
@@ -0,0 +1,1027 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import { BROWSER_DEBUG_SERVICE, BROWSER_PORT_TYPE, BROWSER_SERVICES, BROWSER_STREAM_SERVICE } from "../browser/kinds.js";
|
|
3
|
+
import { systemClock } from "../clock.js";
|
|
4
|
+
import { CLI, TOOL } from "../name.js";
|
|
5
|
+
import { identityOf } from "../processes.js";
|
|
6
|
+
import { DEVICE_MEMORY_MB, EMULATOR_PORTS, EMULATOR_SERVICE, emulatorSerial, loadConfig, poolCap } from "./config.js";
|
|
7
|
+
import { formatAge, formatTime } from "./format.js";
|
|
8
|
+
import { describeOwner, describeState, isProven, judgeProcess, ownerKey, ownerState, worktreeState } from "./owner.js";
|
|
9
|
+
import { configPath, diskNames } from "./paths.js";
|
|
10
|
+
import { deletePoolDevice, listPrunable } from "./prune.js";
|
|
11
|
+
import { NotClaimed, Refusal, UsageError } from "./refusal.js";
|
|
12
|
+
import { endTeardown, failResource, failTeardown, finishResource, holdersBeforeLock, pendingTeardowns, startResource, takeTeardowns, } from "./resources.js";
|
|
13
|
+
import { loadLane, ownerFromRow } from "./rows.js";
|
|
14
|
+
import { readStatus } from "./status.js";
|
|
15
|
+
import { int, optInt, optText, Store, text } from "./store.js";
|
|
16
|
+
export const STEPS = [
|
|
17
|
+
'claim:inspected',
|
|
18
|
+
'claim:locked',
|
|
19
|
+
'claim:swept',
|
|
20
|
+
'claim:ticketed',
|
|
21
|
+
'claim:reserved',
|
|
22
|
+
'claim:reserve-committed',
|
|
23
|
+
'claim:retired-device',
|
|
24
|
+
'claim:created-device',
|
|
25
|
+
'claim:took-over',
|
|
26
|
+
'claim:inserted',
|
|
27
|
+
'claim:committed',
|
|
28
|
+
'claim:waiting',
|
|
29
|
+
'claim:booted',
|
|
30
|
+
'release:locked',
|
|
31
|
+
'release:ended',
|
|
32
|
+
'release:committed',
|
|
33
|
+
'touch:locked',
|
|
34
|
+
];
|
|
35
|
+
export const HISTORY_KEEP_MS = 7 * 24 * 3_600_000;
|
|
36
|
+
const NOUN = { ios: 'simulator', android: 'emulator' };
|
|
37
|
+
function emulatorConsolePorts() {
|
|
38
|
+
const ports = [];
|
|
39
|
+
for (let p = EMULATOR_PORTS.last - 1; p >= EMULATOR_PORTS.first; p -= 2)
|
|
40
|
+
ports.push(p);
|
|
41
|
+
return ports;
|
|
42
|
+
}
|
|
43
|
+
function gigabytes(mb) {
|
|
44
|
+
return `${String(Math.round(mb / 100) / 10)} GB`;
|
|
45
|
+
}
|
|
46
|
+
function listOrNothing(backend) {
|
|
47
|
+
try {
|
|
48
|
+
return backend?.list() ?? [];
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return [];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const DEFAULT_LOCK_WAIT_MS = 120_000;
|
|
55
|
+
export class Registry {
|
|
56
|
+
dir;
|
|
57
|
+
config;
|
|
58
|
+
store;
|
|
59
|
+
processes;
|
|
60
|
+
probe;
|
|
61
|
+
backends;
|
|
62
|
+
clock;
|
|
63
|
+
pollMs;
|
|
64
|
+
onStep;
|
|
65
|
+
selfIdentity;
|
|
66
|
+
claiming = false;
|
|
67
|
+
constructor(options, config, store) {
|
|
68
|
+
this.dir = options.dir;
|
|
69
|
+
this.config = config;
|
|
70
|
+
this.store = store;
|
|
71
|
+
this.processes = options.processes;
|
|
72
|
+
this.probe = options.ports;
|
|
73
|
+
this.backends = options.devices ?? {};
|
|
74
|
+
this.clock = options.clock ?? systemClock;
|
|
75
|
+
this.pollMs = options.pollMs ?? 500;
|
|
76
|
+
this.onStep = options.onStep ?? (() => undefined);
|
|
77
|
+
this.selfIdentity = options.self;
|
|
78
|
+
}
|
|
79
|
+
static open(options) {
|
|
80
|
+
const config = options.config ?? loadConfig(options.dir);
|
|
81
|
+
return new Registry(options, config, Store.open(options.dir, options.lockWaitMs ?? DEFAULT_LOCK_WAIT_MS));
|
|
82
|
+
}
|
|
83
|
+
close() {
|
|
84
|
+
this.store.close();
|
|
85
|
+
}
|
|
86
|
+
step(step) {
|
|
87
|
+
this.onStep(step);
|
|
88
|
+
}
|
|
89
|
+
self() {
|
|
90
|
+
this.selfIdentity ??= identityOf(this.processes, process.pid);
|
|
91
|
+
if (!this.selfIdentity)
|
|
92
|
+
throw new Error(`ps could not read this process's start time (pid ${String(process.pid)})`);
|
|
93
|
+
return this.selfIdentity;
|
|
94
|
+
}
|
|
95
|
+
backend(platform) {
|
|
96
|
+
const backend = this.backends[platform];
|
|
97
|
+
if (backend)
|
|
98
|
+
return backend;
|
|
99
|
+
throw new UsageError(platform === 'ios' ? 'iOS simulators are unavailable on this machine' : 'Android emulators are unavailable on this machine', 'claim without a device (--no-device)');
|
|
100
|
+
}
|
|
101
|
+
caller(owner, worktree) {
|
|
102
|
+
const caller = { key: ownerKey(owner), label: describeOwner(owner), worktree };
|
|
103
|
+
this.store.actor = { who: caller.label, folder: worktree };
|
|
104
|
+
return caller;
|
|
105
|
+
}
|
|
106
|
+
snapshotBeforeLock() {
|
|
107
|
+
const db = this.store.db;
|
|
108
|
+
const pids = [];
|
|
109
|
+
const lanes = new Set();
|
|
110
|
+
for (const row of db.prepare('SELECT id, owner_pid FROM claims').all()) {
|
|
111
|
+
lanes.add(text(row, 'id'));
|
|
112
|
+
const pid = optInt(row, 'owner_pid');
|
|
113
|
+
if (pid !== null)
|
|
114
|
+
pids.push(pid);
|
|
115
|
+
}
|
|
116
|
+
const reservations = new Set();
|
|
117
|
+
for (const row of db.prepare('SELECT name, creator_pid FROM devices WHERE device_id IS NULL').all()) {
|
|
118
|
+
reservations.add(text(row, 'name'));
|
|
119
|
+
pids.push(int(row, 'creator_pid'));
|
|
120
|
+
}
|
|
121
|
+
const tickets = new Set();
|
|
122
|
+
for (const row of db.prepare('SELECT ticket, waiter_pid FROM queue').all()) {
|
|
123
|
+
tickets.add(int(row, 'ticket'));
|
|
124
|
+
pids.push(int(row, 'waiter_pid'));
|
|
125
|
+
}
|
|
126
|
+
return { table: pids.length === 0 ? new Map() : this.processes.startTimes(pids), lanes, reservations, tickets };
|
|
127
|
+
}
|
|
128
|
+
verdictFor(row, verdicts) {
|
|
129
|
+
return verdicts.lanes.has(text(row, 'id')) ? judgeProcess(ownerFromRow(row).process, verdicts.table) : 'alive';
|
|
130
|
+
}
|
|
131
|
+
candidatePorts(services, platform) {
|
|
132
|
+
const ports = new Set();
|
|
133
|
+
if (platform === 'android')
|
|
134
|
+
for (let p = EMULATOR_PORTS.first; p <= EMULATOR_PORTS.last; p++)
|
|
135
|
+
ports.add(p);
|
|
136
|
+
for (const type of new Set(services.map(s => s.type))) {
|
|
137
|
+
const block = this.config.blocks[type];
|
|
138
|
+
if (!block)
|
|
139
|
+
continue;
|
|
140
|
+
for (let p = block.first; p <= block.last; p++)
|
|
141
|
+
ports.add(p);
|
|
142
|
+
}
|
|
143
|
+
return [...ports];
|
|
144
|
+
}
|
|
145
|
+
validate(request) {
|
|
146
|
+
const names = new Set();
|
|
147
|
+
for (const s of request.services) {
|
|
148
|
+
if (names.has(s.name))
|
|
149
|
+
throw new UsageError(`service '${s.name}' is listed twice`, 'list each service once');
|
|
150
|
+
if (s.name === EMULATOR_SERVICE) {
|
|
151
|
+
throw new UsageError(`the service name '${EMULATOR_SERVICE}' is taken by the Android emulator's console port`, 'give the service another name');
|
|
152
|
+
}
|
|
153
|
+
if (BROWSER_SERVICES.includes(s.name)) {
|
|
154
|
+
throw new UsageError(`the service name '${s.name}' is taken by the lane's browser ports`, 'give the service another name');
|
|
155
|
+
}
|
|
156
|
+
names.add(s.name);
|
|
157
|
+
if (!this.config.blocks[s.type]) {
|
|
158
|
+
throw new UsageError(`there is no port block for service type '${s.type}'`, `use one of ${Object.keys(this.config.blocks).join(', ')}, or add "ports": {"${s.type}": [first, last]} to ${configPath(this.dir)}`);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
async claim(request) {
|
|
163
|
+
if (this.claiming)
|
|
164
|
+
throw new Error('a Registry claims one lane at a time');
|
|
165
|
+
this.claiming = true;
|
|
166
|
+
try {
|
|
167
|
+
return await this.claimOnce(request);
|
|
168
|
+
}
|
|
169
|
+
finally {
|
|
170
|
+
this.claiming = false;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
async claimOnce(request) {
|
|
174
|
+
this.validate(request);
|
|
175
|
+
const backend = request.device ? this.backend(request.device.platform) : null;
|
|
176
|
+
const spec = backend && request.device ? backend.resolveSpec(request.device) : null;
|
|
177
|
+
const caller = this.caller(request.owner, request.worktree);
|
|
178
|
+
const me = this.self();
|
|
179
|
+
const deadline = this.clock.now() + request.waitMs;
|
|
180
|
+
const progress = { tookOver: [], freed: [], notes: [], replacedDriver: null };
|
|
181
|
+
try {
|
|
182
|
+
for (;;) {
|
|
183
|
+
const verdicts = this.snapshotBeforeLock();
|
|
184
|
+
const listening = await this.probe.listening(this.candidatePorts([...request.services, ...browserServices(request)], request.device?.platform ?? null));
|
|
185
|
+
this.step('claim:inspected');
|
|
186
|
+
const result = this.store.write(() => this.attempt(request, caller, spec, backend, me, verdicts, listening, progress));
|
|
187
|
+
if (result.kind === 'refused')
|
|
188
|
+
throw result.refusal;
|
|
189
|
+
if (result.kind === 'reserved') {
|
|
190
|
+
this.step('claim:reserve-committed');
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
if (result.kind === 'done') {
|
|
194
|
+
this.step('claim:committed');
|
|
195
|
+
const device = result.lane.device;
|
|
196
|
+
const held = device ? this.backends[device.platform] : undefined;
|
|
197
|
+
if (request.device && held && (request.boot || device?.platform === 'android')) {
|
|
198
|
+
await this.boot(held, result.lane, result.created, caller, progress);
|
|
199
|
+
}
|
|
200
|
+
return { lane: result.lane, created: result.created, ...progress };
|
|
201
|
+
}
|
|
202
|
+
this.step('claim:waiting');
|
|
203
|
+
const left = deadline - this.clock.now();
|
|
204
|
+
if (left <= 0)
|
|
205
|
+
throw this.poolBusy(result, request);
|
|
206
|
+
await this.clock.sleep(Math.min(this.pollMs, left));
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
catch (error) {
|
|
210
|
+
this.abandon(me, backend);
|
|
211
|
+
throw error;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
async boot(backend, lane, created, caller, progress) {
|
|
215
|
+
if (!lane.device)
|
|
216
|
+
return;
|
|
217
|
+
try {
|
|
218
|
+
await backend.boot(lane.device.id, lane.ports.find(p => p.service === EMULATOR_SERVICE)?.port);
|
|
219
|
+
this.step('claim:booted');
|
|
220
|
+
}
|
|
221
|
+
catch (error) {
|
|
222
|
+
if (created) {
|
|
223
|
+
this.store.write(() => {
|
|
224
|
+
const row = this.store.db.prepare('SELECT * FROM claims WHERE id = ? AND token = ?').get(lane.id, lane.token);
|
|
225
|
+
if (row)
|
|
226
|
+
this.endAndShutDown([row], 'released', caller, progress.notes);
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
throw error;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
cancelPending() {
|
|
233
|
+
return this.abandon(this.self(), null);
|
|
234
|
+
}
|
|
235
|
+
abandon(me, backend) {
|
|
236
|
+
try {
|
|
237
|
+
this.store.write(() => {
|
|
238
|
+
const db = this.store.db;
|
|
239
|
+
db.prepare('DELETE FROM queue WHERE waiter_pid = ? AND waiter_start = ?').run(me.pid, me.start);
|
|
240
|
+
const rows = db
|
|
241
|
+
.prepare('SELECT * FROM devices WHERE device_id IS NULL AND creator_pid = ? AND creator_start = ?')
|
|
242
|
+
.all(me.pid, me.start);
|
|
243
|
+
if (rows.length === 0)
|
|
244
|
+
return;
|
|
245
|
+
for (const row of rows) {
|
|
246
|
+
this.settleReservation(row, listOrNothing(backend ?? this.backends[text(row, 'platform')]));
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
return null;
|
|
250
|
+
}
|
|
251
|
+
catch (cleanupError) {
|
|
252
|
+
return cleanupError instanceof Error ? cleanupError : new Error(String(cleanupError));
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
attempt(request, caller, spec, backend, me, verdicts, listening, progress) {
|
|
256
|
+
this.step('claim:locked');
|
|
257
|
+
const db = this.store.db;
|
|
258
|
+
const now = this.clock.now();
|
|
259
|
+
progress.freed.push(...this.sweep(verdicts, now, caller));
|
|
260
|
+
const listed = this.reconcileAll(verdicts, backend, progress.notes);
|
|
261
|
+
this.step('claim:swept');
|
|
262
|
+
const existing = db.prepare('SELECT * FROM claims WHERE owner_key = ? AND worktree = ?').get(caller.key, caller.worktree);
|
|
263
|
+
const existingId = existing ? text(existing, 'id') : null;
|
|
264
|
+
let needDevice = request.device !== null;
|
|
265
|
+
let held;
|
|
266
|
+
if (existingId !== null) {
|
|
267
|
+
held = db.prepare('SELECT * FROM devices WHERE claim_id = ?').get(existingId);
|
|
268
|
+
const platform = held ? text(held, 'platform') : null;
|
|
269
|
+
if (held && platform === request.device?.platform && listed && !listed.has(text(held, 'device_id'))) {
|
|
270
|
+
db.prepare('DELETE FROM devices WHERE name = ?').run(text(held, 'name'));
|
|
271
|
+
progress.notes.push(`your ${NOUN[platform]} ${text(held, 'name')} no longer exists; claiming another`);
|
|
272
|
+
held = undefined;
|
|
273
|
+
}
|
|
274
|
+
else if (held) {
|
|
275
|
+
needDevice = false;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
const heldBrowser = existingId === null ? undefined : db.prepare('SELECT * FROM browsers WHERE claim_id = ?').get(existingId);
|
|
279
|
+
const browserChange = request.browser
|
|
280
|
+
? existingId !== null && heldBrowser !== undefined && text(heldBrowser, 'driver') !== request.browser.driver
|
|
281
|
+
? `this lane's browser is driven by ${request.browser.driver} now, not ${text(heldBrowser, 'driver')}`
|
|
282
|
+
: null
|
|
283
|
+
: null;
|
|
284
|
+
const wanted = [...request.services, ...browserServices(request)].filter(s => existingId === null || !db.prepare('SELECT 1 FROM ports WHERE claim_id = ? AND service = ?').get(existingId, s.name));
|
|
285
|
+
const refuse = (refusal) => {
|
|
286
|
+
this.dropTicket(me);
|
|
287
|
+
this.dropReservation(me);
|
|
288
|
+
return { kind: 'refused', refusal };
|
|
289
|
+
};
|
|
290
|
+
const portPlan = this.planPorts(wanted, listening, now, caller, verdicts);
|
|
291
|
+
if (portPlan instanceof Refusal)
|
|
292
|
+
return refuse(portPlan);
|
|
293
|
+
const emulator = this.planEmulatorPort(request, existingId, held, needDevice, listed, listening, now);
|
|
294
|
+
if (emulator instanceof Refusal)
|
|
295
|
+
return refuse(emulator);
|
|
296
|
+
let deviceName = null;
|
|
297
|
+
if (needDevice && spec && backend && listed) {
|
|
298
|
+
const strict = request.device?.strict === true;
|
|
299
|
+
const plan = this.planDevice(spec, strict, backend, listed, me, verdicts, now, caller, progress, 0);
|
|
300
|
+
if (plan.kind !== 'assign')
|
|
301
|
+
return plan;
|
|
302
|
+
deviceName = plan.name;
|
|
303
|
+
}
|
|
304
|
+
let tookOverPorts = false;
|
|
305
|
+
for (const id of portPlan.takeovers) {
|
|
306
|
+
const row = db.prepare('SELECT * FROM claims WHERE id = ?').get(id);
|
|
307
|
+
if (!row)
|
|
308
|
+
continue;
|
|
309
|
+
progress.tookOver.push(this.end(row, 'taken-over', caller, now));
|
|
310
|
+
tookOverPorts = true;
|
|
311
|
+
}
|
|
312
|
+
if (tookOverPorts)
|
|
313
|
+
this.step('claim:took-over');
|
|
314
|
+
const action = request.action ?? 'claim';
|
|
315
|
+
let laneId;
|
|
316
|
+
if (existingId !== null) {
|
|
317
|
+
laneId = existingId;
|
|
318
|
+
db.prepare('UPDATE claims SET last_used_at = ?, last_action = ?, owner_pid = ?, owner_start = ?, owner_session = ? WHERE id = ?').run(now, action, request.owner.process?.pid ?? null, request.owner.process?.start ?? null, request.owner.sessionId, laneId);
|
|
319
|
+
}
|
|
320
|
+
else {
|
|
321
|
+
do
|
|
322
|
+
laneId = randomBytes(4).toString('hex');
|
|
323
|
+
while (db.prepare('SELECT 1 FROM resources WHERE claim_id = ?').get(laneId) ||
|
|
324
|
+
db.prepare('SELECT 1 FROM browsers WHERE claim_id = ?').get(laneId));
|
|
325
|
+
db.prepare(`INSERT INTO claims (id, token, owner_key, owner_kind, owner_pid, owner_start, owner_session, owner_thread,
|
|
326
|
+
project_root, worktree, created_at, last_used_at, last_action)
|
|
327
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(laneId, randomBytes(24).toString('base64url'), caller.key, request.owner.kind, request.owner.process?.pid ?? null, request.owner.process?.start ?? null, request.owner.sessionId, request.owner.threadId, request.projectRoot, request.worktree, now, now, action);
|
|
328
|
+
}
|
|
329
|
+
for (const pick of portPlan.picks) {
|
|
330
|
+
db.prepare('INSERT INTO ports (port, type, service, claim_id) VALUES (?, ?, ?, ?)').run(pick.port, pick.type, pick.service, laneId);
|
|
331
|
+
}
|
|
332
|
+
if (request.browser) {
|
|
333
|
+
if (heldBrowser === undefined) {
|
|
334
|
+
db.prepare('INSERT INTO browsers (claim_id, driver, session, worktree, created_at) VALUES (?, ?, ?, ?, ?)').run(laneId, request.browser.driver, browserSession(laneId), request.worktree, now);
|
|
335
|
+
}
|
|
336
|
+
else if (browserChange !== null) {
|
|
337
|
+
db.prepare('UPDATE browsers SET driver = ? WHERE claim_id = ?').run(request.browser.driver, laneId);
|
|
338
|
+
progress.notes.push(browserChange);
|
|
339
|
+
progress.replacedDriver = text(heldBrowser, 'driver');
|
|
340
|
+
}
|
|
341
|
+
if (!browserServices(request).some(s => s.name === BROWSER_STREAM_SERVICE)) {
|
|
342
|
+
db.prepare('DELETE FROM ports WHERE claim_id = ? AND service = ?').run(laneId, BROWSER_STREAM_SERVICE);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
if (deviceName !== null)
|
|
346
|
+
db.prepare('UPDATE devices SET claim_id = ? WHERE name = ?').run(laneId, deviceName);
|
|
347
|
+
if (emulator.drop !== null)
|
|
348
|
+
db.prepare('DELETE FROM ports WHERE port = ?').run(emulator.drop);
|
|
349
|
+
if (emulator.add !== null && (deviceName !== null || !needDevice)) {
|
|
350
|
+
db.prepare('INSERT INTO ports (port, type, service, claim_id) VALUES (?, ?, ?, ?)').run(emulator.add, EMULATOR_SERVICE, EMULATOR_SERVICE, laneId);
|
|
351
|
+
}
|
|
352
|
+
this.dropTicket(me);
|
|
353
|
+
this.step('claim:inserted');
|
|
354
|
+
const lane = loadLane(db, laneId);
|
|
355
|
+
if (!lane)
|
|
356
|
+
throw new Error(`lane ${laneId} vanished inside its own transaction`);
|
|
357
|
+
return { kind: 'done', lane, created: existingId === null };
|
|
358
|
+
}
|
|
359
|
+
ticketOf(me) {
|
|
360
|
+
const row = this.store.db
|
|
361
|
+
.prepare('SELECT MIN(ticket) AS ticket FROM queue WHERE waiter_pid = ? AND waiter_start = ?')
|
|
362
|
+
.get(me.pid, me.start);
|
|
363
|
+
return row ? optInt(row, 'ticket') : null;
|
|
364
|
+
}
|
|
365
|
+
dropTicket(me) {
|
|
366
|
+
this.store.db.prepare('DELETE FROM queue WHERE waiter_pid = ? AND waiter_start = ?').run(me.pid, me.start);
|
|
367
|
+
}
|
|
368
|
+
dropReservation(me) {
|
|
369
|
+
this.store.db
|
|
370
|
+
.prepare('DELETE FROM devices WHERE device_id IS NULL AND creator_pid = ? AND creator_start = ?')
|
|
371
|
+
.run(me.pid, me.start);
|
|
372
|
+
}
|
|
373
|
+
sweep(verdicts, now, caller) {
|
|
374
|
+
const db = this.store.db;
|
|
375
|
+
const freed = [];
|
|
376
|
+
for (const row of db.prepare('SELECT * FROM claims').all()) {
|
|
377
|
+
const state = ownerState(this.verdictFor(row, verdicts), worktreeState(text(row, 'worktree')));
|
|
378
|
+
if (isProven(state))
|
|
379
|
+
freed.push(this.end(row, state, caller, now));
|
|
380
|
+
}
|
|
381
|
+
for (const row of db.prepare('SELECT * FROM queue').all()) {
|
|
382
|
+
const ticket = int(row, 'ticket');
|
|
383
|
+
if (!verdicts.tickets.has(ticket))
|
|
384
|
+
continue;
|
|
385
|
+
const verdict = judgeProcess({ pid: int(row, 'waiter_pid'), start: text(row, 'waiter_start') }, verdicts.table);
|
|
386
|
+
if (isProven(verdict))
|
|
387
|
+
db.prepare('DELETE FROM queue WHERE ticket = ?').run(ticket);
|
|
388
|
+
}
|
|
389
|
+
db.prepare('DELETE FROM history WHERE ended_at < ?').run(now - HISTORY_KEEP_MS);
|
|
390
|
+
return freed;
|
|
391
|
+
}
|
|
392
|
+
reconcileAll(verdicts, wanted, notes) {
|
|
393
|
+
let wantedList = null;
|
|
394
|
+
for (const backend of Object.values(this.backends)) {
|
|
395
|
+
if (backend === wanted) {
|
|
396
|
+
wantedList = this.reconcile(backend, verdicts, notes);
|
|
397
|
+
continue;
|
|
398
|
+
}
|
|
399
|
+
if (!this.store.db.prepare('SELECT 1 FROM devices WHERE platform = ? LIMIT 1').get(backend.platform))
|
|
400
|
+
continue;
|
|
401
|
+
try {
|
|
402
|
+
this.reconcile(backend, verdicts, notes);
|
|
403
|
+
}
|
|
404
|
+
catch (error) {
|
|
405
|
+
notes.push(`could not check the ${backend.platform} pool: ${error.message}`);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
return wantedList;
|
|
409
|
+
}
|
|
410
|
+
reconcile(backend, verdicts, notes) {
|
|
411
|
+
const db = this.store.db;
|
|
412
|
+
const listed = backend.list();
|
|
413
|
+
const byId = new Map(listed.map(d => [d.id, d]));
|
|
414
|
+
for (const row of db.prepare('SELECT * FROM devices WHERE platform = ?').all(backend.platform)) {
|
|
415
|
+
const name = text(row, 'name');
|
|
416
|
+
const id = optText(row, 'device_id');
|
|
417
|
+
if (id === null) {
|
|
418
|
+
if (!verdicts.reservations.has(name))
|
|
419
|
+
continue;
|
|
420
|
+
const verdict = judgeProcess({ pid: int(row, 'creator_pid'), start: text(row, 'creator_start') }, verdicts.table);
|
|
421
|
+
if (isProven(verdict))
|
|
422
|
+
notes.push(this.settleReservation(row, listed));
|
|
423
|
+
continue;
|
|
424
|
+
}
|
|
425
|
+
const device = byId.get(id);
|
|
426
|
+
const claimed = optText(row, 'claim_id') !== null;
|
|
427
|
+
if (!device) {
|
|
428
|
+
if (!claimed) {
|
|
429
|
+
db.prepare('DELETE FROM devices WHERE name = ?').run(name);
|
|
430
|
+
notes.push(`forgot pool ${NOUN[backend.platform]} ${name}: it no longer exists`);
|
|
431
|
+
}
|
|
432
|
+
continue;
|
|
433
|
+
}
|
|
434
|
+
if (!claimed && device.booted) {
|
|
435
|
+
try {
|
|
436
|
+
backend.shutdown(id);
|
|
437
|
+
device.booted = false;
|
|
438
|
+
notes.push(`shut down unclaimed pool ${NOUN[backend.platform]} ${name}`);
|
|
439
|
+
}
|
|
440
|
+
catch (error) {
|
|
441
|
+
notes.push(`could not shut down unclaimed pool ${NOUN[backend.platform]} ${name}: ${error.message}`);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
return byId;
|
|
446
|
+
}
|
|
447
|
+
settleReservation(row, listed) {
|
|
448
|
+
const db = this.store.db;
|
|
449
|
+
const name = text(row, 'name');
|
|
450
|
+
const noun = NOUN[text(row, 'platform')];
|
|
451
|
+
const named = listed.filter(d => d.name === name);
|
|
452
|
+
const only = named.length === 1 ? named[0] : undefined;
|
|
453
|
+
if (only && !db.prepare('SELECT 1 FROM devices WHERE device_id = ?').get(only.id)) {
|
|
454
|
+
db.prepare('UPDATE devices SET device_id = ?, creator_pid = NULL, creator_start = NULL WHERE name = ?').run(only.id, name);
|
|
455
|
+
return `adopted pool ${noun} ${name} (${only.id}), left when claiming was interrupted`;
|
|
456
|
+
}
|
|
457
|
+
db.prepare('DELETE FROM devices WHERE name = ?').run(name);
|
|
458
|
+
return `dropped the reservation for ${name}, left when claiming was interrupted`;
|
|
459
|
+
}
|
|
460
|
+
idleLanes(now, caller, verdicts) {
|
|
461
|
+
return this.store.db
|
|
462
|
+
.prepare('SELECT * FROM claims WHERE last_used_at < ? AND owner_key != ? ORDER BY last_used_at ASC')
|
|
463
|
+
.all(now - this.config.idleMs, caller.key)
|
|
464
|
+
.filter(row => !isProven(this.verdictFor(row, verdicts)));
|
|
465
|
+
}
|
|
466
|
+
planPorts(wanted, listening, now, caller, verdicts) {
|
|
467
|
+
const held = new Map();
|
|
468
|
+
for (const row of this.store.db.prepare('SELECT port, claim_id FROM ports').all())
|
|
469
|
+
held.set(int(row, 'port'), text(row, 'claim_id'));
|
|
470
|
+
const idle = wanted.length > 0 ? this.idleLanes(now, caller, verdicts) : [];
|
|
471
|
+
const picks = [];
|
|
472
|
+
const takeovers = [];
|
|
473
|
+
for (const service of wanted) {
|
|
474
|
+
const block = this.config.blocks[service.type];
|
|
475
|
+
if (!block)
|
|
476
|
+
continue;
|
|
477
|
+
const usable = (p) => !listening.has(p) && !picks.some(x => x.port === p);
|
|
478
|
+
let port;
|
|
479
|
+
for (let p = block.first; p <= block.last && port === undefined; p++) {
|
|
480
|
+
const holder = held.get(p);
|
|
481
|
+
if (usable(p) && (holder === undefined || takeovers.includes(holder)))
|
|
482
|
+
port = p;
|
|
483
|
+
}
|
|
484
|
+
for (const row of idle) {
|
|
485
|
+
if (port !== undefined)
|
|
486
|
+
break;
|
|
487
|
+
const id = text(row, 'id');
|
|
488
|
+
if (takeovers.includes(id))
|
|
489
|
+
continue;
|
|
490
|
+
const theirs = [...held].find(([p, c]) => c === id && p >= block.first && p <= block.last && usable(p));
|
|
491
|
+
if (theirs) {
|
|
492
|
+
takeovers.push(id);
|
|
493
|
+
port = theirs[0];
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
if (port === undefined)
|
|
497
|
+
return this.portsExhausted(service.type, held, listening, now);
|
|
498
|
+
picks.push({ service: service.name, type: service.type, port });
|
|
499
|
+
}
|
|
500
|
+
return { picks, takeovers };
|
|
501
|
+
}
|
|
502
|
+
planEmulatorPort(request, laneId, held, needDevice, listed, listening, now) {
|
|
503
|
+
const none = { add: null, drop: null };
|
|
504
|
+
if (request.device?.platform !== 'android' || !listed)
|
|
505
|
+
return none;
|
|
506
|
+
if (!needDevice && (!held || text(held, 'platform') !== 'android'))
|
|
507
|
+
return none;
|
|
508
|
+
const row = laneId === null
|
|
509
|
+
? undefined
|
|
510
|
+
: this.store.db.prepare('SELECT port FROM ports WHERE claim_id = ? AND service = ?').get(laneId, EMULATOR_SERVICE);
|
|
511
|
+
const current = row ? int(row, 'port') : null;
|
|
512
|
+
if (current !== null) {
|
|
513
|
+
const running = held ? listed.get(text(held, 'device_id'))?.serial : undefined;
|
|
514
|
+
const occupied = running !== emulatorSerial(current) && (listening.has(current) || listening.has(current + 1));
|
|
515
|
+
if (!occupied)
|
|
516
|
+
return none;
|
|
517
|
+
}
|
|
518
|
+
const taken = new Set(this.store.db
|
|
519
|
+
.prepare('SELECT port FROM ports WHERE port BETWEEN ? AND ?')
|
|
520
|
+
.all(EMULATOR_PORTS.first, EMULATOR_PORTS.last)
|
|
521
|
+
.map(r => int(r, 'port')));
|
|
522
|
+
const running = new Set([...listed.values()].flatMap(d => (d.serial ? [d.serial] : [])));
|
|
523
|
+
const free = (p) => !taken.has(p) && !taken.has(p + 1) && !listening.has(p) && !listening.has(p + 1) && !running.has(emulatorSerial(p));
|
|
524
|
+
const pick = emulatorConsolePorts().find(free);
|
|
525
|
+
if (pick === undefined)
|
|
526
|
+
return this.emulatorPortsExhausted(listening, now);
|
|
527
|
+
return { add: pick, drop: current };
|
|
528
|
+
}
|
|
529
|
+
emulatorPortsExhausted(listening, now) {
|
|
530
|
+
const db = this.store.db;
|
|
531
|
+
const rows = db
|
|
532
|
+
.prepare('SELECT DISTINCT c.* FROM claims c JOIN ports p ON p.claim_id = c.id WHERE p.port BETWEEN ? AND ? ORDER BY c.last_used_at')
|
|
533
|
+
.all(EMULATOR_PORTS.first, EMULATOR_PORTS.last);
|
|
534
|
+
const holders = rows.map(row => this.holderOf(row, now));
|
|
535
|
+
const foreign = emulatorConsolePorts().filter(p => listening.has(p) || listening.has(p + 1));
|
|
536
|
+
if (foreign.length > 0) {
|
|
537
|
+
holders.push({
|
|
538
|
+
who: `emulators or other processes the registry did not start, on console ports ${foreign.sort((a, b) => a - b).join(', ')}`,
|
|
539
|
+
folder: '(unknown)',
|
|
540
|
+
since: '(unknown)',
|
|
541
|
+
detail: 'adb devices lists the emulators among them',
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
const range = `${String(EMULATOR_PORTS.first)}-${String(EMULATOR_PORTS.last)}`;
|
|
545
|
+
const mostIdle = rows[0];
|
|
546
|
+
return new Refusal('ports-exhausted', `no free Android emulator console and adb port pair in ${range}, the only ports a running adb server scans`, holders.length > 0 ? holders : [{ who: 'no lane the registry can see', folder: this.dir, since: formatTime(now) }], [
|
|
547
|
+
mostIdle
|
|
548
|
+
? `${CLI} release (run by the session holding lane ${text(mostIdle, 'id')}, in ${text(mostIdle, 'worktree')})`
|
|
549
|
+
: `${CLI} status`,
|
|
550
|
+
`lsof -nP -iTCP:${range} -sTCP:LISTEN (names the processes on those ports)`,
|
|
551
|
+
]);
|
|
552
|
+
}
|
|
553
|
+
memoryInUse(pool, isMine) {
|
|
554
|
+
let used = 0;
|
|
555
|
+
for (const row of pool) {
|
|
556
|
+
const busy = optText(row, 'claim_id') !== null || optText(row, 'device_id') === null;
|
|
557
|
+
if (busy && !isMine(row))
|
|
558
|
+
used += DEVICE_MEMORY_MB[text(row, 'platform')];
|
|
559
|
+
}
|
|
560
|
+
return used;
|
|
561
|
+
}
|
|
562
|
+
poolSummary(pool) {
|
|
563
|
+
const counts = (platform) => {
|
|
564
|
+
const rows = pool.filter(r => text(r, 'platform') === platform);
|
|
565
|
+
const claimed = rows.filter(r => optText(r, 'claim_id') !== null).length;
|
|
566
|
+
return `${String(claimed)} of ${String(rows.length)} ${NOUN[platform]}s claimed (at most ${String(poolCap(this.config, platform))})`;
|
|
567
|
+
};
|
|
568
|
+
const used = this.memoryInUse(pool, () => false);
|
|
569
|
+
return `${counts('ios')}, ${counts('android')}; ${gigabytes(used)} of the ${gigabytes(this.config.memoryBudgetMb)} memory budget in use`;
|
|
570
|
+
}
|
|
571
|
+
planDevice(spec, strict, backend, listed, me, verdicts, now, caller, progress, depth) {
|
|
572
|
+
const db = this.store.db;
|
|
573
|
+
const platform = backend.platform;
|
|
574
|
+
const noun = NOUN[platform];
|
|
575
|
+
let ticket = this.ticketOf(me);
|
|
576
|
+
if (ticket === null) {
|
|
577
|
+
const inserted = db
|
|
578
|
+
.prepare('INSERT INTO queue (waiter_pid, waiter_start, owner_key, owner_label, worktree, enqueued_at) VALUES (?, ?, ?, ?, ?, ?)')
|
|
579
|
+
.run(me.pid, me.start, caller.key, caller.label, caller.worktree, now);
|
|
580
|
+
ticket = Number(inserted.lastInsertRowid);
|
|
581
|
+
this.step('claim:ticketed');
|
|
582
|
+
}
|
|
583
|
+
const aheadRow = db
|
|
584
|
+
.prepare(`SELECT COUNT(*) AS ahead FROM queue q WHERE q.ticket < ? AND NOT EXISTS (
|
|
585
|
+
SELECT 1 FROM devices d WHERE d.device_id IS NULL AND d.creator_pid = q.waiter_pid AND d.creator_start = q.waiter_start)`)
|
|
586
|
+
.get(ticket);
|
|
587
|
+
const ahead = aheadRow ? int(aheadRow, 'ahead') : 0;
|
|
588
|
+
const pool = db.prepare('SELECT * FROM devices ORDER BY created_at, name').all();
|
|
589
|
+
const isMine = (r) => optText(r, 'device_id') === null && optInt(r, 'creator_pid') === me.pid && optText(r, 'creator_start') === me.start;
|
|
590
|
+
const rows = pool.filter(r => text(r, 'platform') === platform);
|
|
591
|
+
const mine = rows.find(isMine);
|
|
592
|
+
const free = rows.filter(r => {
|
|
593
|
+
const id = optText(r, 'device_id');
|
|
594
|
+
return id !== null && optText(r, 'claim_id') === null && listed.has(id);
|
|
595
|
+
});
|
|
596
|
+
const others = rows.length - (mine ? 1 : 0);
|
|
597
|
+
const room = Math.max(0, poolCap(this.config, platform) - others);
|
|
598
|
+
const spareMemory = this.config.memoryBudgetMb - this.memoryInUse(pool, isMine);
|
|
599
|
+
const fits = Math.floor(spareMemory / DEVICE_MEMORY_MB[platform]);
|
|
600
|
+
const wait = () => {
|
|
601
|
+
this.dropReservation(me);
|
|
602
|
+
return { kind: 'wait', platform, holders: this.poolHolders(now, ticket), pool: this.poolSummary(pool) };
|
|
603
|
+
};
|
|
604
|
+
if (ahead > 0 && Math.min(free.length + room, fits) <= ahead)
|
|
605
|
+
return wait();
|
|
606
|
+
if (fits > 0) {
|
|
607
|
+
const match = free.find(r => text(r, 'device_type') === spec.deviceType && text(r, 'runtime') === spec.runtime);
|
|
608
|
+
if (match) {
|
|
609
|
+
this.dropReservation(me);
|
|
610
|
+
return { kind: 'assign', name: text(match, 'name') };
|
|
611
|
+
}
|
|
612
|
+
const score = (r) => (text(r, 'device_type') === spec.deviceType ? 2 : 0) + (text(r, 'runtime') === spec.runtime ? 1 : 0);
|
|
613
|
+
const spare = [...free].sort((a, b) => score(b) - score(a))[0];
|
|
614
|
+
if (spare && room === 0 && !strict) {
|
|
615
|
+
this.dropReservation(me);
|
|
616
|
+
progress.notes.push(`took free pool ${noun} ${text(spare, 'name')} (${text(spare, 'device_type_name')}, ${text(spare, 'runtime_name')}) though ${spec.deviceTypeName}, ${spec.runtimeName} was asked for: the pool is full, and a device type is a preference unless the lane is strict`);
|
|
617
|
+
return { kind: 'assign', name: text(spare, 'name') };
|
|
618
|
+
}
|
|
619
|
+
if (room > 0 || spare) {
|
|
620
|
+
if (!mine)
|
|
621
|
+
return this.reserve(spec, backend, listed, me, now);
|
|
622
|
+
if (depth > 0)
|
|
623
|
+
return { kind: 'reserved' };
|
|
624
|
+
if (room === 0 && spare) {
|
|
625
|
+
const retired = text(spare, 'device_id');
|
|
626
|
+
backend.delete(retired);
|
|
627
|
+
listed.delete(retired);
|
|
628
|
+
db.prepare('DELETE FROM devices WHERE name = ?').run(text(spare, 'name'));
|
|
629
|
+
progress.notes.push(`replaced unclaimed pool ${noun} ${text(spare, 'name')} (${text(spare, 'device_type_name')}, ${text(spare, 'runtime_name')}): the lane is strict and needs ${spec.deviceTypeName}, ${spec.runtimeName}`);
|
|
630
|
+
this.step('claim:retired-device');
|
|
631
|
+
}
|
|
632
|
+
const name = text(mine, 'name');
|
|
633
|
+
const id = backend.create(name, spec);
|
|
634
|
+
this.step('claim:created-device');
|
|
635
|
+
db.prepare('UPDATE devices SET device_id = ?, creator_pid = NULL, creator_start = NULL WHERE name = ?').run(id, name);
|
|
636
|
+
listed.set(id, { id, name, deviceType: spec.deviceType, runtime: spec.runtime, booted: false });
|
|
637
|
+
progress.notes.push(`created pool ${noun} ${name} (${spec.deviceTypeName}, ${spec.runtimeName}, ${id})`);
|
|
638
|
+
return { kind: 'assign', name };
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
if (depth === 0) {
|
|
642
|
+
const deviceSlot = free.length + room > 0;
|
|
643
|
+
const idle = this.idleLanes(now, caller, verdicts).find(row => {
|
|
644
|
+
const device = db.prepare('SELECT platform FROM devices WHERE claim_id = ?').get(text(row, 'id'));
|
|
645
|
+
if (!device)
|
|
646
|
+
return false;
|
|
647
|
+
const theirs = text(device, 'platform');
|
|
648
|
+
if (theirs === platform)
|
|
649
|
+
return true;
|
|
650
|
+
return deviceSlot && spareMemory + DEVICE_MEMORY_MB[theirs] >= DEVICE_MEMORY_MB[platform];
|
|
651
|
+
});
|
|
652
|
+
if (idle) {
|
|
653
|
+
progress.tookOver.push(this.end(idle, 'taken-over', caller, now));
|
|
654
|
+
this.step('claim:took-over');
|
|
655
|
+
return this.planDevice(spec, strict, backend, listed, me, verdicts, now, caller, progress, depth + 1);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
return wait();
|
|
659
|
+
}
|
|
660
|
+
reserve(spec, backend, listed, me, now) {
|
|
661
|
+
const db = this.store.db;
|
|
662
|
+
const taken = new Set([...listed.values()].map(d => d.name));
|
|
663
|
+
let name;
|
|
664
|
+
do {
|
|
665
|
+
name = `${diskNames(this.dir).pool[backend.platform]}${randomBytes(3).toString('hex')}`;
|
|
666
|
+
} while (taken.has(name) || db.prepare('SELECT 1 FROM devices WHERE name = ?').get(name));
|
|
667
|
+
db.prepare(`INSERT INTO devices (name, platform, device_id, device_type, device_type_name, runtime, runtime_name,
|
|
668
|
+
creator_pid, creator_start, claim_id, created_at)
|
|
669
|
+
VALUES (?, ?, NULL, ?, ?, ?, ?, ?, ?, NULL, ?)`).run(name, backend.platform, spec.deviceType, spec.deviceTypeName, spec.runtime, spec.runtimeName, me.pid, me.start, now);
|
|
670
|
+
this.step('claim:reserved');
|
|
671
|
+
return { kind: 'reserved' };
|
|
672
|
+
}
|
|
673
|
+
end(row, reason, by, now) {
|
|
674
|
+
const db = this.store.db;
|
|
675
|
+
const id = text(row, 'id');
|
|
676
|
+
const device = db.prepare('SELECT name FROM devices WHERE claim_id = ?').get(id);
|
|
677
|
+
const processes = db
|
|
678
|
+
.prepare('SELECT * FROM processes WHERE claim_id = ? ORDER BY service')
|
|
679
|
+
.all(id)
|
|
680
|
+
.map(p => ({ service: text(p, 'service'), pid: int(p, 'pid'), start: text(p, 'start'), cwd: optText(p, 'cwd') }));
|
|
681
|
+
const ended = {
|
|
682
|
+
id,
|
|
683
|
+
owner: describeOwner(ownerFromRow(row)),
|
|
684
|
+
worktree: text(row, 'worktree'),
|
|
685
|
+
device: device ? text(device, 'name') : null,
|
|
686
|
+
reason,
|
|
687
|
+
createdAt: int(row, 'created_at'),
|
|
688
|
+
lastUsedAt: int(row, 'last_used_at'),
|
|
689
|
+
lastAction: text(row, 'last_action'),
|
|
690
|
+
endedAt: now,
|
|
691
|
+
by: by.label,
|
|
692
|
+
...(processes.length > 0 ? { processes } : {}),
|
|
693
|
+
};
|
|
694
|
+
db.prepare(`INSERT INTO history (claim_id, owner_key, owner_label, worktree, device, reason, created_at, last_used_at, last_action,
|
|
695
|
+
by_owner_key, by_label, by_worktree, ended_at)
|
|
696
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(id, text(row, 'owner_key'), ended.owner, ended.worktree, ended.device, reason, ended.createdAt, ended.lastUsedAt, ended.lastAction, by.key, by.label, by.worktree, now);
|
|
697
|
+
db.prepare('DELETE FROM claims WHERE id = ?').run(id);
|
|
698
|
+
return ended;
|
|
699
|
+
}
|
|
700
|
+
endAndShutDown(rows, reason, by, notes) {
|
|
701
|
+
const db = this.store.db;
|
|
702
|
+
const now = this.clock.now();
|
|
703
|
+
return rows.map(row => {
|
|
704
|
+
const device = db.prepare('SELECT * FROM devices WHERE claim_id = ?').get(text(row, 'id'));
|
|
705
|
+
const ended = this.end(row, reason, by, now);
|
|
706
|
+
this.step('release:ended');
|
|
707
|
+
const backend = device ? this.backends[text(device, 'platform')] : undefined;
|
|
708
|
+
if (device && backend) {
|
|
709
|
+
try {
|
|
710
|
+
backend.shutdown(text(device, 'device_id'));
|
|
711
|
+
}
|
|
712
|
+
catch (error) {
|
|
713
|
+
notes.push(`could not shut down ${text(device, 'name')}: ${error.message}`);
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
return ended;
|
|
717
|
+
});
|
|
718
|
+
}
|
|
719
|
+
holderOf(row, now) {
|
|
720
|
+
return {
|
|
721
|
+
who: describeOwner(ownerFromRow(row)),
|
|
722
|
+
folder: text(row, 'worktree'),
|
|
723
|
+
since: formatTime(int(row, 'created_at')),
|
|
724
|
+
detail: `lane ${text(row, 'id')}, last used ${formatAge(now - int(row, 'last_used_at'))} ago (${text(row, 'last_action')})`,
|
|
725
|
+
};
|
|
726
|
+
}
|
|
727
|
+
poolHolders(now, ticket) {
|
|
728
|
+
const db = this.store.db;
|
|
729
|
+
const holders = [];
|
|
730
|
+
const claimed = db
|
|
731
|
+
.prepare('SELECT c.*, d.name AS device_name, d.platform AS device_platform FROM claims c JOIN devices d ON d.claim_id = c.id ORDER BY c.created_at')
|
|
732
|
+
.all();
|
|
733
|
+
for (const row of claimed) {
|
|
734
|
+
const holder = this.holderOf(row, now);
|
|
735
|
+
holders.push({ ...holder, detail: `${text(row, 'device_name')}, ${holder.detail ?? ''}` });
|
|
736
|
+
}
|
|
737
|
+
for (const row of db.prepare('SELECT * FROM devices WHERE device_id IS NULL').all()) {
|
|
738
|
+
holders.push({
|
|
739
|
+
who: `a process claiming a lane (pid ${String(int(row, 'creator_pid'))})`,
|
|
740
|
+
folder: `(creating ${text(row, 'platform') === 'android' ? 'an emulator' : 'a simulator'})`,
|
|
741
|
+
since: formatTime(int(row, 'created_at')),
|
|
742
|
+
detail: text(row, 'name'),
|
|
743
|
+
});
|
|
744
|
+
}
|
|
745
|
+
for (const row of db.prepare('SELECT * FROM queue WHERE ticket < ? ORDER BY ticket').all(ticket)) {
|
|
746
|
+
holders.push({
|
|
747
|
+
who: `${text(row, 'owner_label')}, queued ahead of you`,
|
|
748
|
+
folder: text(row, 'worktree'),
|
|
749
|
+
since: formatTime(int(row, 'enqueued_at')),
|
|
750
|
+
});
|
|
751
|
+
}
|
|
752
|
+
return holders;
|
|
753
|
+
}
|
|
754
|
+
poolBusy(wait, request) {
|
|
755
|
+
const waited = request.waitMs > 0 ? `waited ${formatAge(request.waitMs)} in the queue` : 'did not wait (--timeout 0)';
|
|
756
|
+
const named = wait.holders.length > 0
|
|
757
|
+
? wait.holders
|
|
758
|
+
: [{ who: 'no lane the registry can see', folder: this.dir, since: formatTime(this.clock.now()) }];
|
|
759
|
+
const cap = wait.platform === 'ios' ? 'poolSize' : 'emulatorPoolSize';
|
|
760
|
+
return new Refusal('pool-busy', `no pool ${NOUN[wait.platform]} is free and none can be reclaimed (${wait.pool}); ${waited}`, named, [
|
|
761
|
+
`${CLI} claim --timeout 1800 (wait up to 30 minutes for a slot)`,
|
|
762
|
+
`${CLI} release (run by a holder's session, in its folder)`,
|
|
763
|
+
`set "${cap}": ${String(poolCap(this.config, wait.platform) + 1)} or "memoryBudgetGB": ${String(Math.ceil((this.config.memoryBudgetMb + DEVICE_MEMORY_MB[wait.platform]) / 1000))} in ${configPath(this.dir)} (allow one more ${NOUN[wait.platform]})`,
|
|
764
|
+
]);
|
|
765
|
+
}
|
|
766
|
+
portsExhausted(type, held, listening, now) {
|
|
767
|
+
const db = this.store.db;
|
|
768
|
+
const block = this.config.blocks[type];
|
|
769
|
+
const holders = [];
|
|
770
|
+
const rows = [];
|
|
771
|
+
const foreign = [];
|
|
772
|
+
for (let p = block?.first ?? 0; block && p <= block.last; p++) {
|
|
773
|
+
const id = held.get(p);
|
|
774
|
+
if (id === undefined) {
|
|
775
|
+
if (listening.has(p))
|
|
776
|
+
foreign.push(p);
|
|
777
|
+
continue;
|
|
778
|
+
}
|
|
779
|
+
if (rows.some(r => text(r, 'id') === id))
|
|
780
|
+
continue;
|
|
781
|
+
const row = db.prepare('SELECT * FROM claims WHERE id = ?').get(id);
|
|
782
|
+
if (row)
|
|
783
|
+
rows.push(row);
|
|
784
|
+
}
|
|
785
|
+
rows.sort((a, b) => int(a, 'last_used_at') - int(b, 'last_used_at'));
|
|
786
|
+
for (const row of rows)
|
|
787
|
+
holders.push(this.holderOf(row, now));
|
|
788
|
+
if (foreign.length > 0) {
|
|
789
|
+
holders.push({
|
|
790
|
+
who: `processes the registry did not start, listening on ${foreign.join(', ')}`,
|
|
791
|
+
folder: '(unknown)',
|
|
792
|
+
since: '(unknown)',
|
|
793
|
+
detail: `lsof -nP -iTCP:${String(foreign[0])} -sTCP:LISTEN names them`,
|
|
794
|
+
});
|
|
795
|
+
}
|
|
796
|
+
const range = block ? `${String(block.first)}-${String(block.last)}` : '';
|
|
797
|
+
const mostIdle = rows[0];
|
|
798
|
+
return new Refusal('ports-exhausted', `no free ${type} port in ${range}`, holders.length > 0 ? holders : [{ who: 'no lane the registry can see', folder: this.dir, since: formatTime(now) }], [
|
|
799
|
+
mostIdle
|
|
800
|
+
? `${CLI} release (run by the session holding lane ${text(mostIdle, 'id')}, in ${text(mostIdle, 'worktree')})`
|
|
801
|
+
: `${CLI} status`,
|
|
802
|
+
`set "ports": {"${type}": [first, last]} in ${configPath(this.dir)} (a wider block)`,
|
|
803
|
+
]);
|
|
804
|
+
}
|
|
805
|
+
laneGone(id) {
|
|
806
|
+
const row = this.store.db.prepare('SELECT * FROM history WHERE claim_id = ? ORDER BY seq DESC LIMIT 1').get(id);
|
|
807
|
+
const recovery = [`${CLI} claim (a new lane for this session and folder)`];
|
|
808
|
+
if (!row) {
|
|
809
|
+
return new Refusal('lane-gone', `lane ${id} does not exist`, [{ who: 'nobody', folder: '(no record)', since: '(no record)', detail: `the registry has no record of lane ${id}` }], recovery);
|
|
810
|
+
}
|
|
811
|
+
const reason = text(row, 'reason');
|
|
812
|
+
const when = formatTime(int(row, 'ended_at'));
|
|
813
|
+
let detail;
|
|
814
|
+
if (reason === 'taken-over')
|
|
815
|
+
detail = `took it over after it sat idle since ${formatTime(int(row, 'last_used_at'))}`;
|
|
816
|
+
else if (reason === 'released' || reason === 'forced')
|
|
817
|
+
detail = `${reason} it`;
|
|
818
|
+
else
|
|
819
|
+
detail = `freed it: ${describeState(reason)}`;
|
|
820
|
+
return new Refusal('lane-gone', `lane ${id} has ended`, [{ who: text(row, 'by_label'), folder: text(row, 'by_worktree'), since: when, detail }], recovery);
|
|
821
|
+
}
|
|
822
|
+
requireLane(ref) {
|
|
823
|
+
const row = this.store.db.prepare('SELECT * FROM claims WHERE id = ?').get(ref.id);
|
|
824
|
+
if (!row)
|
|
825
|
+
throw this.laneGone(ref.id);
|
|
826
|
+
if (text(row, 'token') !== ref.token) {
|
|
827
|
+
throw new Refusal('token-mismatch', `the token given for lane ${ref.id} is not its token`, [this.holderOf(row, this.clock.now())], [`${CLI} claim (run by the owning session in ${text(row, 'worktree')}; it prints the lane's token)`]);
|
|
828
|
+
}
|
|
829
|
+
return row;
|
|
830
|
+
}
|
|
831
|
+
find(owner, worktree) {
|
|
832
|
+
const row = this.store.db
|
|
833
|
+
.prepare('SELECT id FROM claims WHERE owner_key = ? AND worktree = ?')
|
|
834
|
+
.get(ownerKey(owner), worktree);
|
|
835
|
+
return row ? loadLane(this.store.db, text(row, 'id')) : null;
|
|
836
|
+
}
|
|
837
|
+
release(ref, by, notes = []) {
|
|
838
|
+
const caller = this.caller(by, '');
|
|
839
|
+
const [ended] = this.store.write(() => {
|
|
840
|
+
this.step('release:locked');
|
|
841
|
+
const row = this.requireLane(ref);
|
|
842
|
+
caller.worktree = text(row, 'worktree');
|
|
843
|
+
return this.endAndShutDown([row], 'released', caller, notes);
|
|
844
|
+
});
|
|
845
|
+
this.step('release:committed');
|
|
846
|
+
if (!ended)
|
|
847
|
+
throw new Error(`releasing lane ${ref.id} ended nothing`);
|
|
848
|
+
return ended;
|
|
849
|
+
}
|
|
850
|
+
releaseOwn(owner, worktree, notes = []) {
|
|
851
|
+
const caller = this.caller(owner, worktree ?? '');
|
|
852
|
+
const ended = this.store.write(() => {
|
|
853
|
+
this.step('release:locked');
|
|
854
|
+
const db = this.store.db;
|
|
855
|
+
const rows = worktree === null
|
|
856
|
+
? db.prepare('SELECT * FROM claims WHERE owner_key = ? ORDER BY created_at').all(caller.key)
|
|
857
|
+
: db.prepare('SELECT * FROM claims WHERE owner_key = ? AND worktree = ?').all(caller.key, worktree);
|
|
858
|
+
return this.endAndShutDown(rows, 'released', caller, notes);
|
|
859
|
+
});
|
|
860
|
+
this.step('release:committed');
|
|
861
|
+
return ended;
|
|
862
|
+
}
|
|
863
|
+
releaseById(id, by, force, notes = []) {
|
|
864
|
+
const caller = this.caller(by, '');
|
|
865
|
+
const verdicts = this.snapshotBeforeLock();
|
|
866
|
+
const [ended] = this.store.write(() => {
|
|
867
|
+
this.step('release:locked');
|
|
868
|
+
const row = this.store.db.prepare('SELECT * FROM claims WHERE id = ?').get(id);
|
|
869
|
+
if (!row)
|
|
870
|
+
throw this.laneGone(id);
|
|
871
|
+
caller.worktree = text(row, 'worktree');
|
|
872
|
+
if (text(row, 'owner_key') === caller.key)
|
|
873
|
+
return this.endAndShutDown([row], 'released', caller, notes);
|
|
874
|
+
const state = ownerState(this.verdictFor(row, verdicts), worktreeState(text(row, 'worktree')));
|
|
875
|
+
if (isProven(state))
|
|
876
|
+
return this.endAndShutDown([row], state, caller, notes);
|
|
877
|
+
if (force)
|
|
878
|
+
return this.endAndShutDown([row], 'forced', caller, notes);
|
|
879
|
+
const now = this.clock.now();
|
|
880
|
+
const forced = `${CLI} release --lane ${id} --force`;
|
|
881
|
+
if (state === 'cant-tell') {
|
|
882
|
+
throw new Refusal('owner-unknown', `lane ${id} belongs to an owner the registry can't tell is alive or not, so it won't free it on its own`, [this.holderOf(row, now)], [`${forced} (if you know that session is finished)`, `${CLI} status (every lane with its owner)`]);
|
|
883
|
+
}
|
|
884
|
+
throw new Refusal('held-by-live-owner', `lane ${id} belongs to a session that is still running`, [this.holderOf(row, now)], [
|
|
885
|
+
`${CLI} release (run by that session, in ${text(row, 'worktree')})`,
|
|
886
|
+
`${forced} (only if you know that session no longer needs it)`,
|
|
887
|
+
]);
|
|
888
|
+
});
|
|
889
|
+
this.step('release:committed');
|
|
890
|
+
if (!ended)
|
|
891
|
+
throw new Error(`releasing lane ${id} ended nothing`);
|
|
892
|
+
return ended;
|
|
893
|
+
}
|
|
894
|
+
touch(ref, action) {
|
|
895
|
+
return this.store.write(() => {
|
|
896
|
+
this.step('touch:locked');
|
|
897
|
+
this.requireLane(ref);
|
|
898
|
+
this.store.db
|
|
899
|
+
.prepare('UPDATE claims SET last_used_at = ?, last_action = ? WHERE id = ?')
|
|
900
|
+
.run(this.clock.now(), action, ref.id);
|
|
901
|
+
const lane = loadLane(this.store.db, ref.id);
|
|
902
|
+
if (!lane)
|
|
903
|
+
throw new Error(`lane ${ref.id} vanished inside its own transaction`);
|
|
904
|
+
return lane;
|
|
905
|
+
});
|
|
906
|
+
}
|
|
907
|
+
touchOwn(owner, worktree, action) {
|
|
908
|
+
this.caller(owner, worktree);
|
|
909
|
+
const lane = this.find(owner, worktree);
|
|
910
|
+
if (!lane)
|
|
911
|
+
throw new NotClaimed(`${describeOwner(owner)} holds no lane in ${worktree}`, `${CLI} claim (run in ${worktree})`);
|
|
912
|
+
return this.touch({ id: lane.id, token: lane.token }, action);
|
|
913
|
+
}
|
|
914
|
+
recordProcess(ref, proc) {
|
|
915
|
+
this.store.write(() => {
|
|
916
|
+
this.requireLane(ref);
|
|
917
|
+
this.store.db
|
|
918
|
+
.prepare('INSERT OR REPLACE INTO processes (claim_id, service, pid, start, cwd) VALUES (?, ?, ?, ?, ?)')
|
|
919
|
+
.run(ref.id, proc.service, proc.pid, proc.start, proc.cwd);
|
|
920
|
+
});
|
|
921
|
+
}
|
|
922
|
+
forgetProcess(ref, service) {
|
|
923
|
+
this.store.write(() => {
|
|
924
|
+
this.requireLane(ref);
|
|
925
|
+
this.store.db.prepare('DELETE FROM processes WHERE claim_id = ? AND service = ?').run(ref.id, service);
|
|
926
|
+
});
|
|
927
|
+
}
|
|
928
|
+
/** Records what the resource's teardown needs before its create runs; `kept` when this lane already has it. */
|
|
929
|
+
startResource(ref, spec) {
|
|
930
|
+
const holders = holdersBeforeLock(this.store.db, this.processes);
|
|
931
|
+
return this.store.write(() => {
|
|
932
|
+
const row = this.requireLane(ref);
|
|
933
|
+
return startResource(this.store.db, { id: ref.id, worktree: text(row, 'worktree') }, spec, this.self(), holders, this.clock.now());
|
|
934
|
+
});
|
|
935
|
+
}
|
|
936
|
+
finishResource(lane, name, exports) {
|
|
937
|
+
this.store.write(() => {
|
|
938
|
+
finishResource(this.store.db, lane, name, exports, this.self());
|
|
939
|
+
});
|
|
940
|
+
}
|
|
941
|
+
failResource(lane, name, problem) {
|
|
942
|
+
this.store.write(() => {
|
|
943
|
+
failResource(this.store.db, lane, name, problem, this.self());
|
|
944
|
+
});
|
|
945
|
+
}
|
|
946
|
+
/** Leases the resources of ended lanes to this process for teardown, last created first. */
|
|
947
|
+
takeTeardowns(choice = {}) {
|
|
948
|
+
const holders = holdersBeforeLock(this.store.db, this.processes);
|
|
949
|
+
return this.store.write(() => takeTeardowns(this.store.db, this.self(), holders, this.clock.now(), choice));
|
|
950
|
+
}
|
|
951
|
+
finishTeardown(lane, name) {
|
|
952
|
+
this.store.write(() => {
|
|
953
|
+
endTeardown(this.store.db, lane, name, this.self());
|
|
954
|
+
});
|
|
955
|
+
}
|
|
956
|
+
failTeardown(lane, name, problem) {
|
|
957
|
+
this.store.write(() => {
|
|
958
|
+
failTeardown(this.store.db, lane, name, problem, this.self(), this.clock.now());
|
|
959
|
+
});
|
|
960
|
+
}
|
|
961
|
+
/** The browsers of lanes that have ended, however they ended: their processes may still run. */
|
|
962
|
+
endedBrowsers() {
|
|
963
|
+
return this.store.db
|
|
964
|
+
.prepare('SELECT * FROM browsers b WHERE NOT EXISTS (SELECT 1 FROM claims c WHERE c.id = b.claim_id) ORDER BY created_at')
|
|
965
|
+
.all()
|
|
966
|
+
.map(row => ({
|
|
967
|
+
lane: text(row, 'claim_id'),
|
|
968
|
+
driver: text(row, 'driver'),
|
|
969
|
+
session: text(row, 'session'),
|
|
970
|
+
worktree: text(row, 'worktree'),
|
|
971
|
+
createdAt: int(row, 'created_at'),
|
|
972
|
+
}));
|
|
973
|
+
}
|
|
974
|
+
/** Forgets an ended lane's browser once nothing of it runs; a live lane's browser is never forgotten. */
|
|
975
|
+
forgetBrowser(lane) {
|
|
976
|
+
this.store.write(() => {
|
|
977
|
+
this.store.db
|
|
978
|
+
.prepare('DELETE FROM browsers WHERE claim_id = ? AND NOT EXISTS (SELECT 1 FROM claims c WHERE c.id = ?)')
|
|
979
|
+
.run(lane, lane);
|
|
980
|
+
});
|
|
981
|
+
}
|
|
982
|
+
pendingTeardowns() {
|
|
983
|
+
return pendingTeardowns(this.store.db);
|
|
984
|
+
}
|
|
985
|
+
prunable() {
|
|
986
|
+
return listPrunable(this.store, this.dir, this.backends);
|
|
987
|
+
}
|
|
988
|
+
pruneDevice(device) {
|
|
989
|
+
return this.store.write(() => deletePoolDevice(this.store.db, this.backends[device.platform], device));
|
|
990
|
+
}
|
|
991
|
+
status() {
|
|
992
|
+
return readStatus(this.store, {
|
|
993
|
+
dir: this.dir,
|
|
994
|
+
config: this.config,
|
|
995
|
+
processes: this.processes,
|
|
996
|
+
devices: this.backends,
|
|
997
|
+
clock: this.clock,
|
|
998
|
+
});
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
export function browserSession(laneId) {
|
|
1002
|
+
return `${TOOL}-${laneId}`;
|
|
1003
|
+
}
|
|
1004
|
+
function browserServices(request) {
|
|
1005
|
+
if (!request.browser)
|
|
1006
|
+
return [];
|
|
1007
|
+
const debug = { name: BROWSER_DEBUG_SERVICE, type: BROWSER_PORT_TYPE };
|
|
1008
|
+
return request.browser.driver === 'agent-browser'
|
|
1009
|
+
? [debug, { name: BROWSER_STREAM_SERVICE, type: BROWSER_PORT_TYPE }]
|
|
1010
|
+
: [debug];
|
|
1011
|
+
}
|
|
1012
|
+
export function readRegistryStatus(options) {
|
|
1013
|
+
const config = options.config ?? loadConfig(options.dir);
|
|
1014
|
+
const store = Store.openExisting(options.dir, options.lockWaitMs ?? DEFAULT_LOCK_WAIT_MS);
|
|
1015
|
+
try {
|
|
1016
|
+
return readStatus(store, {
|
|
1017
|
+
dir: options.dir,
|
|
1018
|
+
config,
|
|
1019
|
+
processes: options.processes,
|
|
1020
|
+
devices: options.devices ?? {},
|
|
1021
|
+
clock: options.clock ?? systemClock,
|
|
1022
|
+
});
|
|
1023
|
+
}
|
|
1024
|
+
finally {
|
|
1025
|
+
store?.close();
|
|
1026
|
+
}
|
|
1027
|
+
}
|