agentlane 0.4.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +76 -0
- package/dist/agents/claude-plugin.js +54 -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/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 +1258 -235
- package/dist/registry/rows.js +18 -4
- package/dist/registry/status.js +51 -8
- package/dist/registry/store.js +63 -1
- package/dist/registry/touch.js +180 -0
- package/dist/registry-cli.js +197 -36
- package/dist/services/services.js +223 -54
- package/dist/services/system.js +2 -1
- package/dist/up/answers.js +124 -0
- package/dist/up/doctor.js +27 -3
- 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 +33 -12
- 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/registry/prune.js
CHANGED
|
@@ -1,17 +1,47 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import { randomBytes } from 'node:crypto';
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { DeviceRunningError } from "../devices/backend.js";
|
|
6
|
+
import { dataDir } from "../driver/install.js";
|
|
7
|
+
import { CLI, FORMER, TOOL } from "../name.js";
|
|
8
|
+
import { allLeases } from "./leases.js";
|
|
9
|
+
import { databasePath, isFormerRegistryDir, isPoolName, registryDirs } from "./paths.js";
|
|
10
|
+
import { UsageError } from "./refusal.js";
|
|
11
|
+
import { optInt, optText, Store, text } from "./store.js";
|
|
4
12
|
const NOUN = { ios: 'simulator', android: 'emulator' };
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
13
|
+
const DELETING = '.deleting-';
|
|
14
|
+
const REMOVING = '.removing-';
|
|
15
|
+
const NOT_SHUT_DOWN = 'it is no longer shut down';
|
|
16
|
+
export function requireHome(env) {
|
|
17
|
+
if (env.HOME && path.isAbsolute(env.HOME))
|
|
18
|
+
return;
|
|
19
|
+
throw new UsageError('prune needs HOME to find every registry and download folder on this machine', 'set HOME to your home folder');
|
|
20
|
+
}
|
|
9
21
|
function messageOf(error) {
|
|
10
22
|
return error instanceof Error ? error.message : String(error);
|
|
11
23
|
}
|
|
12
24
|
function noTool(platform) {
|
|
13
25
|
return `this machine has no ${NOUN[platform]} tool to check it with`;
|
|
14
26
|
}
|
|
27
|
+
const DOING = new Map([
|
|
28
|
+
['shutdown', 'shutting it down'],
|
|
29
|
+
['delete', 'deleting it'],
|
|
30
|
+
]);
|
|
31
|
+
function leaseHolder(work, pid) {
|
|
32
|
+
return `a process ${DOING.get(work) ?? 'working on it'} (pid ${String(pid)})`;
|
|
33
|
+
}
|
|
34
|
+
function readTogether(db, read) {
|
|
35
|
+
if (db.isTransaction)
|
|
36
|
+
return read();
|
|
37
|
+
db.exec('BEGIN');
|
|
38
|
+
try {
|
|
39
|
+
return read();
|
|
40
|
+
}
|
|
41
|
+
finally {
|
|
42
|
+
db.exec('COMMIT');
|
|
43
|
+
}
|
|
44
|
+
}
|
|
15
45
|
export function listPrunable(store, dir, backends) {
|
|
16
46
|
const listing = { stateDir: dir, free: [], kept: [], unrecorded: [] };
|
|
17
47
|
const listed = new Map();
|
|
@@ -24,7 +54,13 @@ export function listPrunable(store, dir, backends) {
|
|
|
24
54
|
}
|
|
25
55
|
}
|
|
26
56
|
const recorded = new Set();
|
|
27
|
-
const rows
|
|
57
|
+
const { rows, leases } = store
|
|
58
|
+
? readTogether(store.db, () => ({
|
|
59
|
+
rows: store.db.prepare('SELECT * FROM devices ORDER BY created_at, name').all(),
|
|
60
|
+
leases: allLeases(store.db),
|
|
61
|
+
}))
|
|
62
|
+
: { rows: [], leases: [] };
|
|
63
|
+
const standIns = new Set(leases.map(lease => lease.reservation));
|
|
28
64
|
for (const row of rows) {
|
|
29
65
|
const name = text(row, 'name');
|
|
30
66
|
const platform = text(row, 'platform');
|
|
@@ -33,7 +69,9 @@ export function listPrunable(store, dir, backends) {
|
|
|
33
69
|
recorded.add(`${platform} ${name}`);
|
|
34
70
|
if (id !== null)
|
|
35
71
|
recorded.add(`${platform} ${id}`);
|
|
36
|
-
|
|
72
|
+
if (standIns.has(name))
|
|
73
|
+
continue;
|
|
74
|
+
const keep = (reason) => listing.kept.push({ name, platform, id, laneId, registry: dir, reason });
|
|
37
75
|
const devices = listed.get(platform);
|
|
38
76
|
if (laneId !== null)
|
|
39
77
|
keep(`held by lane ${laneId}`);
|
|
@@ -56,19 +94,25 @@ export function listPrunable(store, dir, backends) {
|
|
|
56
94
|
id,
|
|
57
95
|
deviceTypeName: text(row, 'device_type_name'),
|
|
58
96
|
runtimeName: text(row, 'runtime_name'),
|
|
59
|
-
state: device === undefined ? 'missing' : device.booted ? 'booted' : 'shutdown',
|
|
97
|
+
state: device === undefined ? 'missing' : device.booted ? 'booted' : device.stopped === true ? 'shutdown' : 'changing',
|
|
60
98
|
});
|
|
61
99
|
}
|
|
62
100
|
}
|
|
63
101
|
}
|
|
102
|
+
for (const { name, platform, id, work, holder } of leases) {
|
|
103
|
+
recorded.add(`${platform} ${name}`);
|
|
104
|
+
recorded.add(`${platform} ${id}`);
|
|
105
|
+
listing.kept.push({ name, platform, id, laneId: null, registry: dir, reason: `held by ${leaseHolder(work, holder.pid)}` });
|
|
106
|
+
}
|
|
64
107
|
for (const [platform, devices] of listed) {
|
|
65
108
|
if (devices instanceof Error)
|
|
66
109
|
continue;
|
|
67
110
|
for (const d of devices.values()) {
|
|
68
111
|
if (recorded.has(`${platform} ${d.id}`) || recorded.has(`${platform} ${d.name}`))
|
|
69
112
|
continue;
|
|
70
|
-
if (isPoolName(platform, d.name))
|
|
71
|
-
listing.unrecorded.push({ name: d.name, platform, id: d.id });
|
|
113
|
+
if (isPoolName(platform, d.name)) {
|
|
114
|
+
listing.unrecorded.push({ name: d.name, platform, id: d.id, stopped: d.stopped === true, disk: d.disk ?? null });
|
|
115
|
+
}
|
|
72
116
|
}
|
|
73
117
|
}
|
|
74
118
|
listing.unrecorded.sort((a, b) => a.platform.localeCompare(b.platform) || a.name.localeCompare(b.name));
|
|
@@ -98,7 +142,7 @@ export function deletePoolDevice(db, backend, device) {
|
|
|
98
142
|
return result('kept', `${device.id} is named ${present.name} now`);
|
|
99
143
|
if (present) {
|
|
100
144
|
try {
|
|
101
|
-
if (present.
|
|
145
|
+
if (present.stopped !== true)
|
|
102
146
|
backend.shutdown(device.id);
|
|
103
147
|
backend.delete(device.id);
|
|
104
148
|
}
|
|
@@ -109,17 +153,562 @@ export function deletePoolDevice(db, backend, device) {
|
|
|
109
153
|
db.prepare('DELETE FROM devices WHERE name = ?').run(device.name);
|
|
110
154
|
return present ? result('deleted') : result('forgotten', 'it no longer exists');
|
|
111
155
|
}
|
|
156
|
+
export function diskBytes(paths) {
|
|
157
|
+
let total = 0;
|
|
158
|
+
const pending = [...paths];
|
|
159
|
+
for (let next = pending.pop(); next !== undefined; next = pending.pop()) {
|
|
160
|
+
let stat;
|
|
161
|
+
try {
|
|
162
|
+
stat = fs.lstatSync(next);
|
|
163
|
+
}
|
|
164
|
+
catch {
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
total += stat.blocks * 512;
|
|
168
|
+
if (!stat.isDirectory())
|
|
169
|
+
continue;
|
|
170
|
+
try {
|
|
171
|
+
for (const entry of fs.readdirSync(next))
|
|
172
|
+
pending.push(path.join(next, entry));
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return total;
|
|
179
|
+
}
|
|
180
|
+
function fileIdentity(file) {
|
|
181
|
+
try {
|
|
182
|
+
const stat = fs.statSync(file);
|
|
183
|
+
return `${String(stat.dev)}:${String(stat.ino)}`;
|
|
184
|
+
}
|
|
185
|
+
catch {
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
function distinctRegistries(env, dirs) {
|
|
190
|
+
const byIdentity = new Map();
|
|
191
|
+
for (const dir of dirs) {
|
|
192
|
+
const former = isFormerRegistryDir(env, dir);
|
|
193
|
+
const identity = fileIdentity(databasePath(dir)) ?? `absent ${dir}`;
|
|
194
|
+
const seen = byIdentity.get(identity);
|
|
195
|
+
if (seen)
|
|
196
|
+
seen.former ||= former;
|
|
197
|
+
else
|
|
198
|
+
byIdentity.set(identity, { dir, former, identity });
|
|
199
|
+
}
|
|
200
|
+
return [...byIdentity.values()];
|
|
201
|
+
}
|
|
202
|
+
function openRegistries(env, dirs, lockWaitMs) {
|
|
203
|
+
const opened = [];
|
|
204
|
+
try {
|
|
205
|
+
const folders = distinctRegistries(env, dirs).sort((a, b) => a.identity.localeCompare(b.identity));
|
|
206
|
+
for (const { dir, former } of folders) {
|
|
207
|
+
const store = Store.openExisting(dir, lockWaitMs);
|
|
208
|
+
if (store)
|
|
209
|
+
opened.push({ store, former });
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
catch (error) {
|
|
213
|
+
closeAll(opened);
|
|
214
|
+
throw error;
|
|
215
|
+
}
|
|
216
|
+
return opened;
|
|
217
|
+
}
|
|
218
|
+
function closeAll(opened) {
|
|
219
|
+
for (const { store } of opened)
|
|
220
|
+
store.close();
|
|
221
|
+
}
|
|
222
|
+
function underLocks(opened, body) {
|
|
223
|
+
const [first, ...rest] = opened;
|
|
224
|
+
return first ? first.store.write(() => underLocks(rest, body)) : body();
|
|
225
|
+
}
|
|
226
|
+
const RECORDS_OF = `
|
|
227
|
+
SELECT claim_id AS lane, NULL AS work, NULL AS pid, CASE WHEN claim_id IS NULL THEN 2 ELSE 0 END AS rank
|
|
228
|
+
FROM devices WHERE name IN (?, ?) OR device_id IN (?, ?)
|
|
229
|
+
UNION ALL
|
|
230
|
+
SELECT NULL, work, holder_pid, 1 FROM device_leases WHERE name IN (?, ?) OR device_id IN (?, ?)
|
|
231
|
+
ORDER BY rank LIMIT 1`;
|
|
232
|
+
function recordOf(store, name, id) {
|
|
233
|
+
const row = store.db.prepare(RECORDS_OF).get(name, id, name, id, name, id, name, id);
|
|
234
|
+
if (!row)
|
|
235
|
+
return null;
|
|
236
|
+
const work = optText(row, 'work');
|
|
237
|
+
const pid = optInt(row, 'pid');
|
|
238
|
+
return { laneId: optText(row, 'lane'), holder: work === null || pid === null ? null : leaseHolder(work, pid) };
|
|
239
|
+
}
|
|
240
|
+
const FOLDER_COLUMNS = [
|
|
241
|
+
'SELECT project_root AS folder FROM claims',
|
|
242
|
+
'SELECT worktree AS folder FROM claims',
|
|
243
|
+
'SELECT worktree AS folder FROM history',
|
|
244
|
+
'SELECT worktree AS folder FROM queue',
|
|
245
|
+
'SELECT worktree AS folder FROM resources',
|
|
246
|
+
'SELECT worktree AS folder FROM browsers',
|
|
247
|
+
].join(' UNION ');
|
|
248
|
+
function foldersOf(store) {
|
|
249
|
+
return store.db
|
|
250
|
+
.prepare(FOLDER_COLUMNS)
|
|
251
|
+
.all()
|
|
252
|
+
.map(row => text(row, 'folder'));
|
|
253
|
+
}
|
|
254
|
+
function lanesIn(store) {
|
|
255
|
+
const lanes = store.db.prepare('SELECT id FROM claims ORDER BY created_at').all();
|
|
256
|
+
const queued = store.db.prepare('SELECT ticket FROM queue ORDER BY ticket').all();
|
|
257
|
+
return [...lanes.map(row => `lane ${text(row, 'id')}`), ...queued.map(() => 'a claim waiting in the queue')];
|
|
258
|
+
}
|
|
259
|
+
function gitCommonDir(env, folder) {
|
|
260
|
+
if (!fs.existsSync(folder))
|
|
261
|
+
return null;
|
|
262
|
+
const result = spawnSync('git', ['-C', folder, 'rev-parse', '--path-format=absolute', '--git-common-dir'], {
|
|
263
|
+
encoding: 'utf8',
|
|
264
|
+
env,
|
|
265
|
+
});
|
|
266
|
+
if (result.status !== 0)
|
|
267
|
+
return null;
|
|
268
|
+
try {
|
|
269
|
+
return fs.realpathSync(result.stdout.trim());
|
|
270
|
+
}
|
|
271
|
+
catch {
|
|
272
|
+
return null;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
function withAsideCopies(target) {
|
|
276
|
+
const parent = path.dirname(target);
|
|
277
|
+
const base = path.basename(target);
|
|
278
|
+
let names;
|
|
279
|
+
try {
|
|
280
|
+
names = fs.readdirSync(parent);
|
|
281
|
+
}
|
|
282
|
+
catch {
|
|
283
|
+
return [];
|
|
284
|
+
}
|
|
285
|
+
return names
|
|
286
|
+
.filter(name => name === base || name.startsWith(`${base}${DELETING}`) || name.startsWith(`${base}${REMOVING}`))
|
|
287
|
+
.sort()
|
|
288
|
+
.map(name => path.join(parent, name));
|
|
289
|
+
}
|
|
290
|
+
function repositoryOf(commonDir) {
|
|
291
|
+
return path.basename(commonDir) === '.git' ? path.dirname(commonDir) : commonDir;
|
|
292
|
+
}
|
|
293
|
+
export function formerLeftovers(env, commonDirs) {
|
|
294
|
+
const found = [];
|
|
295
|
+
for (const target of withAsideCopies(downloadsDir(env)))
|
|
296
|
+
found.push({ kind: 'downloads', path: target, repository: null });
|
|
297
|
+
for (const common of [...new Set(commonDirs)].sort()) {
|
|
298
|
+
for (const target of withAsideCopies(path.join(common, FORMER.tool, 'build-cache'))) {
|
|
299
|
+
found.push({ kind: 'build-cache', path: target, repository: repositoryOf(common) });
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
return found
|
|
303
|
+
.filter(leftover => isLeftoverPath(env, leftover))
|
|
304
|
+
.map(leftover => ({ ...leftover, bytes: diskBytes([leftover.path]) }));
|
|
305
|
+
}
|
|
306
|
+
function formerLanesReason(dir, lanes) {
|
|
307
|
+
return `the registry in ${dir}, which 0.2.x may use, has ${lanes.join(', ')}, which may be 0.2.x's; prune deletes what 0.2.x left once no such registry has lanes`;
|
|
308
|
+
}
|
|
309
|
+
function formerLanesIn(opened) {
|
|
310
|
+
for (const { store, former } of opened) {
|
|
311
|
+
const lanes = former ? lanesIn(store) : [];
|
|
312
|
+
if (lanes.length > 0)
|
|
313
|
+
return formerLanesReason(store.dir, lanes);
|
|
314
|
+
}
|
|
315
|
+
return null;
|
|
316
|
+
}
|
|
317
|
+
export function reportPrunable(listing, ctx) {
|
|
318
|
+
requireHome(ctx.env);
|
|
319
|
+
const dirs = registryDirs(ctx.env);
|
|
320
|
+
const report = {
|
|
321
|
+
stateDir: listing.stateDir,
|
|
322
|
+
registries: distinctRegistries(ctx.env, dirs).map(folder => folder.dir),
|
|
323
|
+
free: listing.free,
|
|
324
|
+
kept: [...listing.kept],
|
|
325
|
+
orphans: [],
|
|
326
|
+
leftovers: [],
|
|
327
|
+
leftoversKept: null,
|
|
328
|
+
};
|
|
329
|
+
const folders = new Set([ctx.cwd]);
|
|
330
|
+
let opened = [];
|
|
331
|
+
let unreadable = null;
|
|
332
|
+
try {
|
|
333
|
+
opened = openRegistries(ctx.env, dirs, ctx.lockWaitMs);
|
|
334
|
+
}
|
|
335
|
+
catch (error) {
|
|
336
|
+
unreadable = messageOf(error);
|
|
337
|
+
}
|
|
338
|
+
try {
|
|
339
|
+
const inUse = fileIdentity(databasePath(listing.stateDir));
|
|
340
|
+
const others = opened.filter(({ store }) => inUse === null || fileIdentity(databasePath(store.dir)) !== inUse);
|
|
341
|
+
for (const d of listing.unrecorded) {
|
|
342
|
+
const keep = (reason, laneId = null, registry = null) => report.kept.push({ name: d.name, platform: d.platform, id: d.id, laneId, registry, reason });
|
|
343
|
+
if (unreadable !== null) {
|
|
344
|
+
keep(`could not read every registry on this Mac: ${unreadable}`);
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
const elsewhere = others
|
|
348
|
+
.map(({ store }) => ({ dir: store.dir, record: recordOf(store, d.name, d.id) }))
|
|
349
|
+
.find(found => found.record !== null);
|
|
350
|
+
if (elsewhere?.record) {
|
|
351
|
+
const { laneId, holder } = elsewhere.record;
|
|
352
|
+
const by = laneId !== null ? `lane ${laneId}` : holder;
|
|
353
|
+
keep(by === null ? `the registry in ${elsewhere.dir} records it` : `held by ${by} in the registry in ${elsewhere.dir}`, laneId, elsewhere.dir);
|
|
354
|
+
}
|
|
355
|
+
else if (!d.stopped)
|
|
356
|
+
keep('no registry records it, but it is not shut down, so something may be using it');
|
|
357
|
+
else {
|
|
358
|
+
const paths = d.disk?.paths ?? [];
|
|
359
|
+
report.orphans.push({
|
|
360
|
+
name: d.name,
|
|
361
|
+
platform: d.platform,
|
|
362
|
+
id: d.id,
|
|
363
|
+
paths,
|
|
364
|
+
bytes: d.disk?.bytes ?? (paths.length > 0 ? diskBytes(paths) : null),
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
for (const { store } of opened)
|
|
369
|
+
for (const folder of foldersOf(store))
|
|
370
|
+
folders.add(folder);
|
|
371
|
+
report.leftoversKept =
|
|
372
|
+
unreadable === null ? formerLanesIn(opened) : `could not read every registry on this Mac: ${unreadable}`;
|
|
373
|
+
}
|
|
374
|
+
finally {
|
|
375
|
+
closeAll(opened);
|
|
376
|
+
}
|
|
377
|
+
const commonDirs = [...folders].map(folder => gitCommonDir(ctx.env, folder)).filter((dir) => dir !== null);
|
|
378
|
+
report.leftovers = formerLeftovers(ctx.env, commonDirs);
|
|
379
|
+
return report;
|
|
380
|
+
}
|
|
381
|
+
export function deleteOrphan(ctx, backend, device) {
|
|
382
|
+
requireHome(ctx.env);
|
|
383
|
+
const result = (outcome, detail = null) => ({ device, outcome, detail });
|
|
384
|
+
if (!backend)
|
|
385
|
+
return result('kept', noTool(device.platform));
|
|
386
|
+
let checked;
|
|
387
|
+
let opened = [];
|
|
388
|
+
try {
|
|
389
|
+
opened = openRegistries(ctx.env, registryDirs(ctx.env), ctx.lockWaitMs);
|
|
390
|
+
checked = underLocks(opened, () => {
|
|
391
|
+
for (const { store } of opened) {
|
|
392
|
+
const record = recordOf(store, device.name, device.id);
|
|
393
|
+
if (!record)
|
|
394
|
+
continue;
|
|
395
|
+
const now = record.holder === null ? 'records it now' : `now records it as held by ${record.holder}`;
|
|
396
|
+
return result('kept', `the registry in ${store.dir} ${now}`);
|
|
397
|
+
}
|
|
398
|
+
let present;
|
|
399
|
+
try {
|
|
400
|
+
present = backend.list().find(d => d.id === device.id);
|
|
401
|
+
}
|
|
402
|
+
catch (error) {
|
|
403
|
+
return result('failed', `could not list the ${NOUN[device.platform]}s: ${messageOf(error)}`);
|
|
404
|
+
}
|
|
405
|
+
if (!present)
|
|
406
|
+
return result('gone', 'it no longer exists');
|
|
407
|
+
if (present.name !== device.name)
|
|
408
|
+
return result('kept', `${device.id} is named ${present.name} now`);
|
|
409
|
+
if (present.stopped !== true)
|
|
410
|
+
return result('kept', NOT_SHUT_DOWN);
|
|
411
|
+
return null;
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
catch (error) {
|
|
415
|
+
return result('failed', messageOf(error));
|
|
416
|
+
}
|
|
417
|
+
finally {
|
|
418
|
+
closeAll(opened);
|
|
419
|
+
}
|
|
420
|
+
if (checked)
|
|
421
|
+
return checked;
|
|
422
|
+
try {
|
|
423
|
+
backend.delete(device.id, 'refuse');
|
|
424
|
+
}
|
|
425
|
+
catch (error) {
|
|
426
|
+
if (error instanceof DeviceRunningError)
|
|
427
|
+
return result('kept', NOT_SHUT_DOWN);
|
|
428
|
+
return result('failed', messageOf(error));
|
|
429
|
+
}
|
|
430
|
+
return result('deleted');
|
|
431
|
+
}
|
|
432
|
+
function downloadsDir(env) {
|
|
433
|
+
return path.join(path.dirname(dataDir(env)), FORMER.tool);
|
|
434
|
+
}
|
|
435
|
+
function markIn(name) {
|
|
436
|
+
let first = null;
|
|
437
|
+
for (const mark of [DELETING, REMOVING]) {
|
|
438
|
+
const at = name.indexOf(mark);
|
|
439
|
+
if (at >= 0 && (first === null || at < first.at))
|
|
440
|
+
first = { at, mark };
|
|
441
|
+
}
|
|
442
|
+
return first;
|
|
443
|
+
}
|
|
444
|
+
function withoutAside(name) {
|
|
445
|
+
const found = markIn(name);
|
|
446
|
+
return found === null ? name : name.slice(0, found.at);
|
|
447
|
+
}
|
|
448
|
+
function isAsideName(target) {
|
|
449
|
+
return markIn(path.basename(target)) !== null;
|
|
450
|
+
}
|
|
451
|
+
function isBeingRemoved(target) {
|
|
452
|
+
return markIn(path.basename(target))?.mark === REMOVING;
|
|
453
|
+
}
|
|
454
|
+
function originalOf(aside) {
|
|
455
|
+
return path.join(path.dirname(aside), withoutAside(path.basename(aside)));
|
|
456
|
+
}
|
|
457
|
+
function asideName(target, mark, now) {
|
|
458
|
+
return `${target}${mark}${String(now)}-${randomBytes(3).toString('hex')}`;
|
|
459
|
+
}
|
|
460
|
+
const STAMPED = /^(\d+)-[0-9a-f]{6}$/;
|
|
461
|
+
function movedAsideAt(aside) {
|
|
462
|
+
const name = path.basename(aside);
|
|
463
|
+
const found = markIn(name);
|
|
464
|
+
const stamp = found === null ? null : STAMPED.exec(name.slice(found.at + found.mark.length));
|
|
465
|
+
const ms = stamp === null ? NaN : Number(stamp[1]);
|
|
466
|
+
return Number.isSafeInteger(ms) ? ms : -1;
|
|
467
|
+
}
|
|
468
|
+
function isMissing(target) {
|
|
469
|
+
try {
|
|
470
|
+
fs.lstatSync(target);
|
|
471
|
+
}
|
|
472
|
+
catch (error) {
|
|
473
|
+
return error.code === 'ENOENT';
|
|
474
|
+
}
|
|
475
|
+
return false;
|
|
476
|
+
}
|
|
477
|
+
function resolvesLiterally(target, anchor) {
|
|
478
|
+
try {
|
|
479
|
+
fs.lstatSync(target);
|
|
480
|
+
}
|
|
481
|
+
catch {
|
|
482
|
+
return true;
|
|
483
|
+
}
|
|
484
|
+
try {
|
|
485
|
+
return fs.realpathSync(target) === path.join(fs.realpathSync(anchor), path.relative(anchor, target));
|
|
486
|
+
}
|
|
487
|
+
catch {
|
|
488
|
+
return false;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
function isLeftoverPath(env, leftover) {
|
|
492
|
+
const { kind, path: target } = leftover;
|
|
493
|
+
if (!path.isAbsolute(target) || path.normalize(target) !== target)
|
|
494
|
+
return false;
|
|
495
|
+
const parent = path.dirname(target);
|
|
496
|
+
if (kind === 'downloads') {
|
|
497
|
+
return (parent === path.dirname(downloadsDir(env)) &&
|
|
498
|
+
withoutAside(path.basename(target)) === FORMER.tool &&
|
|
499
|
+
resolvesLiterally(target, parent));
|
|
500
|
+
}
|
|
501
|
+
const common = path.dirname(parent);
|
|
502
|
+
return (withoutAside(path.basename(target)) === 'build-cache' &&
|
|
503
|
+
path.basename(parent) === FORMER.tool &&
|
|
504
|
+
gitCommonDir(env, common) === common &&
|
|
505
|
+
resolvesLiterally(target, common));
|
|
506
|
+
}
|
|
507
|
+
function removeIfEmpty(dir) {
|
|
508
|
+
try {
|
|
509
|
+
fs.rmdirSync(dir);
|
|
510
|
+
}
|
|
511
|
+
catch {
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
function openFormerRegistries(ctx) {
|
|
516
|
+
const dirs = registryDirs(ctx.env).filter(dir => isFormerRegistryDir(ctx.env, dir));
|
|
517
|
+
return openRegistries(ctx.env, dirs, ctx.lockWaitMs);
|
|
518
|
+
}
|
|
519
|
+
function underFormerLocks(ctx, body, failed) {
|
|
520
|
+
let opened = [];
|
|
521
|
+
const ran = {};
|
|
522
|
+
try {
|
|
523
|
+
opened = openFormerRegistries(ctx);
|
|
524
|
+
return underLocks(opened, () => (ran.result = body(opened)));
|
|
525
|
+
}
|
|
526
|
+
catch (error) {
|
|
527
|
+
return ran.result ?? failed(error);
|
|
528
|
+
}
|
|
529
|
+
finally {
|
|
530
|
+
closeAll(opened);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
const REFUSED = 'refused: it is not a folder 0.2.x leaves';
|
|
534
|
+
function moveAside(env, leftover) {
|
|
535
|
+
const done = (outcome, detail) => ({ leftover, outcome, detail });
|
|
536
|
+
if (!isLeftoverPath(env, leftover))
|
|
537
|
+
return done('failed', REFUSED);
|
|
538
|
+
if (!fs.existsSync(leftover.path))
|
|
539
|
+
return done('gone', 'it no longer exists');
|
|
540
|
+
if (isAsideName(leftover.path))
|
|
541
|
+
return { leftover, aside: leftover.path };
|
|
542
|
+
const aside = asideName(leftover.path, DELETING, Date.now());
|
|
543
|
+
try {
|
|
544
|
+
fs.renameSync(leftover.path, aside);
|
|
545
|
+
}
|
|
546
|
+
catch (error) {
|
|
547
|
+
return done('failed', messageOf(error));
|
|
548
|
+
}
|
|
549
|
+
return { leftover, aside };
|
|
550
|
+
}
|
|
551
|
+
function asFound(leftover, why) {
|
|
552
|
+
return isAsideName(leftover.path) ? { leftover, aside: leftover.path } : { leftover, outcome: 'kept', detail: why };
|
|
553
|
+
}
|
|
554
|
+
export function moveLeftoversAside(ctx, leftovers) {
|
|
555
|
+
requireHome(ctx.env);
|
|
556
|
+
if (leftovers.length === 0)
|
|
557
|
+
return [];
|
|
558
|
+
return underFormerLocks(ctx, (opened) => {
|
|
559
|
+
const blocked = formerLanesIn(opened);
|
|
560
|
+
return blocked === null
|
|
561
|
+
? leftovers.map(leftover => moveAside(ctx.env, leftover))
|
|
562
|
+
: putBack(ctx.env, leftovers.map(leftover => asFound(leftover, blocked)), blocked);
|
|
563
|
+
}, error => leftovers.map(leftover => ({ leftover, outcome: 'failed', detail: messageOf(error) })));
|
|
564
|
+
}
|
|
565
|
+
function renameBack(env, { leftover, aside }) {
|
|
566
|
+
const original = originalOf(aside);
|
|
567
|
+
if (!isLeftoverPath(env, { kind: leftover.kind, path: aside }) ||
|
|
568
|
+
!isLeftoverPath(env, { kind: leftover.kind, path: original })) {
|
|
569
|
+
return REFUSED;
|
|
570
|
+
}
|
|
571
|
+
try {
|
|
572
|
+
fs.renameSync(aside, original);
|
|
573
|
+
}
|
|
574
|
+
catch (error) {
|
|
575
|
+
return messageOf(error);
|
|
576
|
+
}
|
|
577
|
+
return null;
|
|
578
|
+
}
|
|
579
|
+
function movedByThisRun({ leftover, aside }) {
|
|
580
|
+
return leftover.path !== aside;
|
|
581
|
+
}
|
|
582
|
+
function restoreFirst(a, b) {
|
|
583
|
+
return Number(movedByThisRun(b)) - Number(movedByThisRun(a)) || movedAsideAt(b.aside) - movedAsideAt(a.aside);
|
|
584
|
+
}
|
|
585
|
+
function vanished(copy) {
|
|
586
|
+
const { leftover, aside } = copy;
|
|
587
|
+
return { leftover, outcome: 'gone', detail: movedByThisRun(copy) ? `${aside} no longer exists` : 'it no longer exists' };
|
|
588
|
+
}
|
|
589
|
+
function putBack(env, steps, why) {
|
|
590
|
+
const tried = new Map();
|
|
591
|
+
const restorable = steps.filter(isAside).filter(copy => !isBeingRemoved(copy.aside));
|
|
592
|
+
for (const copy of restorable.sort(restoreFirst)) {
|
|
593
|
+
if (isMissing(copy.aside) || !isMissing(originalOf(copy.aside)))
|
|
594
|
+
continue;
|
|
595
|
+
tried.set(copy, renameBack(env, copy));
|
|
596
|
+
}
|
|
597
|
+
return steps.map(step => {
|
|
598
|
+
if (!isAside(step))
|
|
599
|
+
return step;
|
|
600
|
+
const { leftover, aside } = step;
|
|
601
|
+
const original = originalOf(aside);
|
|
602
|
+
const kept = (detail) => ({ leftover, outcome: 'kept', detail });
|
|
603
|
+
const error = tried.get(step);
|
|
604
|
+
if (error === null)
|
|
605
|
+
return kept(movedByThisRun(step) ? why : `${why}; put back at ${original}`);
|
|
606
|
+
if (isMissing(aside))
|
|
607
|
+
return vanished(step);
|
|
608
|
+
if (error !== undefined)
|
|
609
|
+
return kept(`${why}; it stays at ${aside}: ${error}`);
|
|
610
|
+
if (isBeingRemoved(aside))
|
|
611
|
+
return kept(why);
|
|
612
|
+
return kept(`${why}; ${original} exists again, so the old copy stays at ${aside}`);
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
function takeForRemoval(env, copy) {
|
|
616
|
+
const { leftover, aside } = copy;
|
|
617
|
+
if (isBeingRemoved(aside))
|
|
618
|
+
return { leftover, removing: aside };
|
|
619
|
+
if (!isLeftoverPath(env, { kind: leftover.kind, path: aside }))
|
|
620
|
+
return { leftover, outcome: 'failed', detail: REFUSED };
|
|
621
|
+
const removing = asideName(originalOf(aside), REMOVING, Date.now());
|
|
622
|
+
try {
|
|
623
|
+
fs.renameSync(aside, removing);
|
|
624
|
+
}
|
|
625
|
+
catch (error) {
|
|
626
|
+
return isMissing(aside) ? vanished(copy) : { leftover, outcome: 'failed', detail: `${aside}: ${messageOf(error)}` };
|
|
627
|
+
}
|
|
628
|
+
return { leftover, removing };
|
|
629
|
+
}
|
|
630
|
+
function remove(env, { leftover, removing }) {
|
|
631
|
+
if (!isLeftoverPath(env, { kind: leftover.kind, path: removing })) {
|
|
632
|
+
return { leftover, outcome: 'failed', detail: `${removing}: ${REFUSED}` };
|
|
633
|
+
}
|
|
634
|
+
try {
|
|
635
|
+
fs.rmSync(removing, { recursive: true, force: true });
|
|
636
|
+
}
|
|
637
|
+
catch (error) {
|
|
638
|
+
return { leftover, outcome: 'failed', detail: `${removing}: ${messageOf(error)}` };
|
|
639
|
+
}
|
|
640
|
+
if (leftover.kind === 'build-cache')
|
|
641
|
+
removeIfEmpty(path.dirname(leftover.path));
|
|
642
|
+
return { leftover, outcome: 'deleted', detail: null };
|
|
643
|
+
}
|
|
644
|
+
function isAside(step) {
|
|
645
|
+
return 'aside' in step;
|
|
646
|
+
}
|
|
647
|
+
function isRemoval(step) {
|
|
648
|
+
return 'removing' in step;
|
|
649
|
+
}
|
|
650
|
+
export function finishLeftovers(ctx, steps) {
|
|
651
|
+
requireHome(ctx.env);
|
|
652
|
+
if (!steps.some(isAside))
|
|
653
|
+
return steps.filter((step) => !isAside(step));
|
|
654
|
+
const restore = (why) => putBack(ctx.env, steps, why);
|
|
655
|
+
const checked = underFormerLocks(ctx, opened => {
|
|
656
|
+
const blocked = formerLanesIn(opened);
|
|
657
|
+
return blocked === null
|
|
658
|
+
? steps.map(step => (isAside(step) ? takeForRemoval(ctx.env, step) : step))
|
|
659
|
+
: restore(`${blocked}, found after it was moved aside`);
|
|
660
|
+
}, error => restore(`could not check every registry 0.2.x may use: ${messageOf(error)}`));
|
|
661
|
+
return checked.map(step => (isRemoval(step) ? remove(ctx.env, step) : step));
|
|
662
|
+
}
|
|
663
|
+
export function deleteLeftovers(ctx, leftovers) {
|
|
664
|
+
return finishLeftovers(ctx, moveLeftoversAside(ctx, leftovers));
|
|
665
|
+
}
|
|
666
|
+
export function putBackAsideCopies(ctx, leftovers) {
|
|
667
|
+
requireHome(ctx.env);
|
|
668
|
+
const copies = leftovers
|
|
669
|
+
.filter(leftover => isAsideName(leftover.path) && !isBeingRemoved(leftover.path))
|
|
670
|
+
.map(leftover => ({ leftover, aside: leftover.path }));
|
|
671
|
+
if (copies.length === 0)
|
|
672
|
+
return [];
|
|
673
|
+
return underFormerLocks(ctx, opened => {
|
|
674
|
+
const blocked = formerLanesIn(opened);
|
|
675
|
+
return blocked === null ? [] : putBack(ctx.env, copies, blocked);
|
|
676
|
+
}, error => putBack(ctx.env, copies, `could not check every registry 0.2.x may use: ${messageOf(error)}`));
|
|
677
|
+
}
|
|
678
|
+
export function formatBytes(bytes) {
|
|
679
|
+
if (bytes === null)
|
|
680
|
+
return 'size unknown';
|
|
681
|
+
if (bytes >= 1e9)
|
|
682
|
+
return `${(bytes / 1e9).toFixed(1)} GB`;
|
|
683
|
+
if (bytes >= 1e6)
|
|
684
|
+
return `${(bytes / 1e6).toFixed(1)} MB`;
|
|
685
|
+
return `${String(Math.ceil(bytes / 1e3))} kB`;
|
|
686
|
+
}
|
|
687
|
+
function totalBytes(items) {
|
|
688
|
+
return formatBytes(items.reduce((sum, item) => sum + (item.bytes ?? 0), 0));
|
|
689
|
+
}
|
|
112
690
|
function shownId(d) {
|
|
113
691
|
return d.id === null || d.id === d.name ? '' : ` ${d.id}`;
|
|
114
692
|
}
|
|
115
693
|
const STATE = {
|
|
116
694
|
shutdown: 'shutdown',
|
|
117
695
|
booted: 'booted, so it is shut down first',
|
|
696
|
+
changing: 'starting or stopping, so it is shut down first',
|
|
118
697
|
missing: 'already deleted, so only its record goes',
|
|
119
698
|
};
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
699
|
+
function describeLeftover(l) {
|
|
700
|
+
return l.kind === 'downloads' ? "0.2.x's Maestro and browser downloads" : `0.2.x's build cache for ${l.repository ?? ''}`;
|
|
701
|
+
}
|
|
702
|
+
function plural(count, noun) {
|
|
703
|
+
return `${String(count)} ${noun}${count === 1 ? '' : 's'}`;
|
|
704
|
+
}
|
|
705
|
+
export function renderPruneReport(report, deleting) {
|
|
706
|
+
const lines = [`${TOOL} registry: ${report.stateDir}`];
|
|
707
|
+
const others = report.registries.filter(dir => dir !== report.stateDir);
|
|
708
|
+
if (others.length > 0)
|
|
709
|
+
lines.push(`also checked: ${others.join(', ')}`);
|
|
710
|
+
lines.push('');
|
|
711
|
+
const { free, kept, orphans, leftovers } = report;
|
|
123
712
|
if (free.length === 0)
|
|
124
713
|
lines.push('no pool device is free to delete');
|
|
125
714
|
else {
|
|
@@ -133,15 +722,26 @@ export function renderPruneListing(listing, deleting) {
|
|
|
133
722
|
for (const d of kept)
|
|
134
723
|
lines.push(` ${d.name} ${NOUN[d.platform]}${shownId(d)} ${d.reason}`);
|
|
135
724
|
}
|
|
136
|
-
if (
|
|
137
|
-
lines.push('', `named like pool devices but
|
|
138
|
-
for (const d of
|
|
139
|
-
lines.push(` ${d.name} ${NOUN[d.platform]}${shownId(d)}`);
|
|
140
|
-
|
|
141
|
-
|
|
725
|
+
if (orphans.length > 0) {
|
|
726
|
+
lines.push('', `named like pool devices but in no registry on this Mac (${String(orphans.length)}, ${totalBytes(orphans)}):`);
|
|
727
|
+
for (const d of orphans)
|
|
728
|
+
lines.push(` ${d.name} ${NOUN[d.platform]}${shownId(d)} ${formatBytes(d.bytes)}`);
|
|
729
|
+
}
|
|
730
|
+
if (leftovers.length > 0) {
|
|
731
|
+
lines.push('', `what 0.2.x left (${String(leftovers.length)}, ${totalBytes(leftovers)}):`);
|
|
732
|
+
for (const l of leftovers)
|
|
733
|
+
lines.push(` ${l.path} ${describeLeftover(l)} ${formatBytes(l.bytes)}`);
|
|
734
|
+
if (report.leftoversKept !== null)
|
|
735
|
+
lines.push(` kept: ${report.leftoversKept}`);
|
|
142
736
|
}
|
|
143
|
-
|
|
144
|
-
|
|
737
|
+
const offered = [
|
|
738
|
+
free.length > 0 ? `the ${plural(free.length, 'pool device')} no lane holds` : null,
|
|
739
|
+
orphans.length > 0 ? `the ${plural(orphans.length, 'device')} no registry records` : null,
|
|
740
|
+
leftovers.length > 0 && report.leftoversKept === null ? 'what 0.2.x left' : null,
|
|
741
|
+
].filter((part) => part !== null);
|
|
742
|
+
if (!deleting && offered.length > 0) {
|
|
743
|
+
const last = offered.pop() ?? '';
|
|
744
|
+
lines.push('', `${CLI} prune --yes deletes ${offered.length > 0 ? `${offered.join(', ')} and ${last}` : last}`);
|
|
145
745
|
}
|
|
146
746
|
return `${lines.join('\n')}\n`;
|
|
147
747
|
}
|
|
@@ -158,3 +758,22 @@ export function describePruneResult(r) {
|
|
|
158
758
|
return `could not delete ${name}: ${r.detail ?? ''}`;
|
|
159
759
|
}
|
|
160
760
|
}
|
|
761
|
+
function describeSweep(what, size, outcome, detail) {
|
|
762
|
+
switch (outcome) {
|
|
763
|
+
case 'deleted':
|
|
764
|
+
return `deleted ${what} (${formatBytes(size)})`;
|
|
765
|
+
case 'gone':
|
|
766
|
+
return `skipped ${what}: ${detail ?? ''}`;
|
|
767
|
+
case 'kept':
|
|
768
|
+
return `kept ${what}: ${detail ?? ''}`;
|
|
769
|
+
case 'failed':
|
|
770
|
+
return `could not delete ${what}: ${detail ?? ''}`;
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
export function describeOrphanResult(r) {
|
|
774
|
+
const { name, platform, id } = r.device;
|
|
775
|
+
return describeSweep(`${name} (${NOUN[platform]}${id === name ? '' : ` ${id}`})`, r.device.bytes, r.outcome, r.detail);
|
|
776
|
+
}
|
|
777
|
+
export function describeLeftoverResult(r) {
|
|
778
|
+
return describeSweep(r.leftover.path, r.leftover.bytes, r.outcome, r.detail);
|
|
779
|
+
}
|