agentlane 0.4.1 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +87 -0
- package/dist/agents/claude-plugin.js +69 -8
- package/dist/agents/cli.js +11 -4
- package/dist/agents/hook-check.js +290 -0
- package/dist/agents/launcher.js +34 -6
- package/dist/agents/wiring-check.js +10 -4
- package/dist/agents/wiring.js +6 -33
- package/dist/browser/env.js +27 -7
- package/dist/browser/install.js +144 -6
- package/dist/browser/lane-browser.js +6 -3
- package/dist/browser/stop.js +2 -17
- package/dist/cache/key.js +23 -11
- package/dist/cli.js +63 -23
- package/dist/devices/backend.js +6 -0
- package/dist/devices/emulator.js +15 -5
- package/dist/devices/hygiene.js +20 -4
- package/dist/devices/simctl.js +15 -3
- package/dist/disk-limits.js +134 -0
- package/dist/driver/caller.js +21 -6
- package/dist/driver/client.js +9 -5
- package/dist/driver/engine.js +29 -14
- package/dist/driver/install.js +88 -36
- package/dist/driver/paths.js +15 -11
- package/dist/driver/session.js +4 -1
- package/dist/driver/superseded.js +378 -0
- package/dist/driver-cli.js +32 -23
- package/dist/evidence/device.js +4 -0
- package/dist/evidence/evidence.js +4 -2
- package/dist/evidence/files.js +68 -0
- package/dist/evidence/strays.js +107 -0
- package/dist/hooks/env.js +2 -1
- package/dist/mcp/server.js +15 -0
- package/dist/mcp/tools.js +9 -1
- package/dist/plugin/guard.js +30 -0
- package/dist/plugin/hook-cli.js +101 -6
- package/dist/plugin/hook-mark.js +73 -0
- package/dist/plugin/hook-once.js +78 -0
- package/dist/plugin/shell.js +109 -8
- package/dist/processes.js +52 -2
- package/dist/registry/config.js +12 -8
- package/dist/registry/holds.js +37 -0
- package/dist/registry/leases.js +109 -0
- package/dist/registry/owner-checks.js +55 -0
- package/dist/registry/owner.js +18 -0
- package/dist/registry/parks.js +60 -0
- package/dist/registry/paths.js +28 -4
- package/dist/registry/ports.js +5 -1
- package/dist/registry/prune.js +643 -24
- package/dist/registry/refusal.js +1 -0
- package/dist/registry/registry.js +1261 -236
- package/dist/registry/rows.js +18 -4
- package/dist/registry/status.js +51 -8
- package/dist/registry/store.js +65 -2
- package/dist/registry/touch.js +180 -0
- package/dist/registry-cli.js +197 -36
- package/dist/services/services.js +227 -56
- package/dist/services/system.js +2 -1
- package/dist/up/answers.js +124 -0
- package/dist/up/doctor.js +29 -4
- package/dist/up/exec.js +29 -4
- package/dist/up/lane-files.js +26 -8
- package/dist/up/lane-services.js +8 -3
- package/dist/up/park.js +146 -0
- package/dist/up/preflight.js +2 -10
- package/dist/up/release.js +20 -24
- package/dist/up/restart.js +9 -2
- package/dist/up/sweep.js +437 -0
- package/dist/up/up.js +38 -15
- package/dist/up/wake.js +124 -0
- package/dist/up/waker.js +49 -0
- package/dist/up-cli.js +6 -1
- package/package.json +2 -2
- package/skills/dev-loop/SKILL.md +7 -5
- package/skills/drive/SKILL.md +4 -0
- package/skills/setup/SKILL.md +2 -2
package/dist/registry/rows.js
CHANGED
|
@@ -58,6 +58,23 @@ export function loadLane(db, id) {
|
|
|
58
58
|
const row = db.prepare('SELECT token FROM claims WHERE id = ?').get(id);
|
|
59
59
|
return info && row ? { ...info, token: text(row, 'token') } : null;
|
|
60
60
|
}
|
|
61
|
+
export function processesOf(db, id) {
|
|
62
|
+
return db
|
|
63
|
+
.prepare(`SELECT p.service, p.pid, p.start, p.cwd, a.alive_at FROM processes p
|
|
64
|
+
LEFT JOIN process_alive a ON a.claim_id = p.claim_id AND a.service = p.service AND a.pid = p.pid AND a.start = p.start
|
|
65
|
+
WHERE p.claim_id = ? ORDER BY p.service`)
|
|
66
|
+
.all(id)
|
|
67
|
+
.map(p => {
|
|
68
|
+
const aliveAt = optInt(p, 'alive_at');
|
|
69
|
+
return {
|
|
70
|
+
service: text(p, 'service'),
|
|
71
|
+
pid: int(p, 'pid'),
|
|
72
|
+
start: text(p, 'start'),
|
|
73
|
+
cwd: optText(p, 'cwd'),
|
|
74
|
+
...(aliveAt === null ? {} : { aliveAt }),
|
|
75
|
+
};
|
|
76
|
+
});
|
|
77
|
+
}
|
|
61
78
|
export function loadLaneInfo(db, id) {
|
|
62
79
|
const row = db.prepare('SELECT * FROM claims WHERE id = ?').get(id);
|
|
63
80
|
if (!row)
|
|
@@ -80,10 +97,7 @@ export function loadLaneInfo(db, id) {
|
|
|
80
97
|
device: device ? deviceFromRow(device, ports.find(p => p.service === EMULATOR_SERVICE)?.port ?? null) : null,
|
|
81
98
|
browser: browser ? browserFromRow(browser, ports) : null,
|
|
82
99
|
ports,
|
|
83
|
-
processes: db
|
|
84
|
-
.prepare('SELECT * FROM processes WHERE claim_id = ? ORDER BY service')
|
|
85
|
-
.all(id)
|
|
86
|
-
.map(p => ({ service: text(p, 'service'), pid: int(p, 'pid'), start: text(p, 'start'), cwd: optText(p, 'cwd') })),
|
|
100
|
+
processes: processesOf(db, id),
|
|
87
101
|
resources: db
|
|
88
102
|
.prepare('SELECT name, exports FROM resources WHERE claim_id = ? ORDER BY seq')
|
|
89
103
|
.all(id)
|
package/dist/registry/status.js
CHANGED
|
@@ -1,8 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { readHold } from "./holds.js";
|
|
2
|
+
import { hostSession, isProven, judgeProcess, ownerState, worktreeState } from "./owner.js";
|
|
3
|
+
import { readCheck, RETURN_WINDOW_MS } from "./owner-checks.js";
|
|
4
|
+
import { readPark } from "./parks.js";
|
|
2
5
|
import { pendingTeardowns } from "./resources.js";
|
|
3
6
|
import { endedFromHistory, loadLaneInfo } from "./rows.js";
|
|
4
7
|
import { int, optText, text } from "./store.js";
|
|
5
8
|
const RECENT_MS = 24 * 3_600_000;
|
|
9
|
+
function processState(verdict) {
|
|
10
|
+
if (verdict === 'alive')
|
|
11
|
+
return 'running';
|
|
12
|
+
if (verdict === 'cant-tell')
|
|
13
|
+
return 'unknown';
|
|
14
|
+
return 'gone';
|
|
15
|
+
}
|
|
6
16
|
export function readStatus(store, sources) {
|
|
7
17
|
const now = sources.clock.now();
|
|
8
18
|
const report = {
|
|
@@ -23,7 +33,7 @@ export function readStatus(store, sources) {
|
|
|
23
33
|
.all()
|
|
24
34
|
.map(row => loadLaneInfo(db, text(row, 'id')))
|
|
25
35
|
.filter((c) => c !== null);
|
|
26
|
-
const table = sources.processes.startTimes(lanes.flatMap(c => (c.owner.process ? [c.owner.process.pid] : [])));
|
|
36
|
+
const table = sources.processes.startTimes(lanes.flatMap(c => [...(c.owner.process ? [c.owner.process.pid] : []), ...c.processes.map(p => p.pid)]));
|
|
27
37
|
const listed = new Map();
|
|
28
38
|
for (const backend of Object.values(sources.devices)) {
|
|
29
39
|
if (db.prepare('SELECT 1 FROM devices WHERE platform = ? LIMIT 1').get(backend.platform) === undefined)
|
|
@@ -44,12 +54,45 @@ export function readStatus(store, sources) {
|
|
|
44
54
|
return 'missing';
|
|
45
55
|
return device.booted ? 'booted' : 'shutdown';
|
|
46
56
|
};
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
57
|
+
const parkStatus = (id) => {
|
|
58
|
+
const park = readPark(db, id);
|
|
59
|
+
if (!park)
|
|
60
|
+
return null;
|
|
61
|
+
return {
|
|
62
|
+
state: park.state,
|
|
63
|
+
since: park.since,
|
|
64
|
+
parkedAt: park.parkedAt,
|
|
65
|
+
by: park.holder?.pid ?? null,
|
|
66
|
+
restarts: park.restarts,
|
|
67
|
+
problems: park.problems,
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
const goneOf = (lane) => {
|
|
71
|
+
const gone = readCheck(db, lane.id, lane.ownerKey)?.gone ?? null;
|
|
72
|
+
if (!gone)
|
|
73
|
+
return null;
|
|
74
|
+
return {
|
|
75
|
+
state: gone.state,
|
|
76
|
+
foundAt: gone.foundAt,
|
|
77
|
+
endsAt: gone.foundAt + RETURN_WINDOW_MS,
|
|
78
|
+
session: hostSession(lane.owner),
|
|
79
|
+
by: gone.reaper?.pid ?? null,
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
report.lanes = lanes.map((lane) => {
|
|
83
|
+
const owner = ownerState(judgeProcess(lane.owner.process, table), worktreeState(lane.worktree));
|
|
84
|
+
const ownerGone = goneOf(lane);
|
|
85
|
+
return {
|
|
86
|
+
...lane,
|
|
87
|
+
processes: lane.processes.map(p => ({ ...p, state: processState(judgeProcess(p, table)) })),
|
|
88
|
+
ownerState: owner,
|
|
89
|
+
idleMs: now - lane.lastUsedAt,
|
|
90
|
+
deviceState: lane.device ? stateOf(lane.device.platform, lane.device.id) : null,
|
|
91
|
+
park: parkStatus(lane.id),
|
|
92
|
+
hold: isProven(owner) || ownerGone ? null : readHold(db, lane.id, lane.ownerKey, now),
|
|
93
|
+
ownerGone,
|
|
94
|
+
};
|
|
95
|
+
});
|
|
53
96
|
report.pool = db
|
|
54
97
|
.prepare('SELECT * FROM devices ORDER BY created_at, name')
|
|
55
98
|
.all()
|
package/dist/registry/store.js
CHANGED
|
@@ -34,8 +34,70 @@ CREATE TABLE IF NOT EXISTS browsers (
|
|
|
34
34
|
worktree TEXT NOT NULL,
|
|
35
35
|
created_at INTEGER NOT NULL
|
|
36
36
|
) STRICT;
|
|
37
|
+
CREATE TABLE IF NOT EXISTS process_alive (
|
|
38
|
+
claim_id TEXT NOT NULL REFERENCES claims(id) ON DELETE CASCADE,
|
|
39
|
+
service TEXT NOT NULL,
|
|
40
|
+
pid INTEGER NOT NULL,
|
|
41
|
+
start TEXT NOT NULL,
|
|
42
|
+
alive_at INTEGER NOT NULL,
|
|
43
|
+
PRIMARY KEY (claim_id, service)
|
|
44
|
+
) STRICT;
|
|
45
|
+
CREATE TABLE IF NOT EXISTS device_leases (
|
|
46
|
+
reservation TEXT PRIMARY KEY,
|
|
47
|
+
name TEXT NOT NULL,
|
|
48
|
+
platform TEXT NOT NULL,
|
|
49
|
+
device_id TEXT NOT NULL,
|
|
50
|
+
device_type TEXT NOT NULL,
|
|
51
|
+
device_type_name TEXT NOT NULL,
|
|
52
|
+
runtime TEXT NOT NULL,
|
|
53
|
+
runtime_name TEXT NOT NULL,
|
|
54
|
+
created_at INTEGER NOT NULL,
|
|
55
|
+
work TEXT NOT NULL,
|
|
56
|
+
holder_pid INTEGER NOT NULL,
|
|
57
|
+
holder_start TEXT NOT NULL,
|
|
58
|
+
since INTEGER NOT NULL,
|
|
59
|
+
console_port INTEGER
|
|
60
|
+
) STRICT;
|
|
61
|
+
CREATE TABLE IF NOT EXISTS parks (
|
|
62
|
+
claim_id TEXT PRIMARY KEY REFERENCES claims(id) ON DELETE CASCADE,
|
|
63
|
+
state TEXT NOT NULL,
|
|
64
|
+
since INTEGER NOT NULL,
|
|
65
|
+
parked_at INTEGER,
|
|
66
|
+
holder_pid INTEGER,
|
|
67
|
+
holder_start TEXT,
|
|
68
|
+
attempt TEXT,
|
|
69
|
+
restarts TEXT NOT NULL,
|
|
70
|
+
problems TEXT NOT NULL,
|
|
71
|
+
CHECK (state IN ('parking', 'parked', 'waking')),
|
|
72
|
+
CHECK ((holder_pid IS NULL) = (state = 'parked') AND (holder_pid IS NULL) = (holder_start IS NULL)
|
|
73
|
+
AND (holder_pid IS NULL) = (attempt IS NULL))
|
|
74
|
+
) STRICT;
|
|
75
|
+
CREATE TABLE IF NOT EXISTS holds (
|
|
76
|
+
claim_id TEXT PRIMARY KEY REFERENCES claims(id) ON DELETE CASCADE,
|
|
77
|
+
owner_key TEXT NOT NULL,
|
|
78
|
+
reason TEXT NOT NULL,
|
|
79
|
+
since INTEGER NOT NULL,
|
|
80
|
+
until INTEGER NOT NULL,
|
|
81
|
+
CHECK (until > since)
|
|
82
|
+
) STRICT;
|
|
83
|
+
CREATE TABLE IF NOT EXISTS owner_checks (
|
|
84
|
+
claim_id TEXT PRIMARY KEY REFERENCES claims(id) ON DELETE CASCADE,
|
|
85
|
+
owner_key TEXT NOT NULL,
|
|
86
|
+
checked_at INTEGER,
|
|
87
|
+
gone TEXT,
|
|
88
|
+
gone_at INTEGER,
|
|
89
|
+
reaper_pid INTEGER,
|
|
90
|
+
reaper_start TEXT,
|
|
91
|
+
reaping_since INTEGER,
|
|
92
|
+
attempt TEXT,
|
|
93
|
+
CHECK (gone IS NULL OR gone IN ('dead', 'pid-reused', 'worktree-gone')),
|
|
94
|
+
CHECK ((gone IS NULL) = (gone_at IS NULL)),
|
|
95
|
+
CHECK ((reaper_pid IS NULL) = (reaper_start IS NULL) AND (reaper_pid IS NULL) = (reaping_since IS NULL)
|
|
96
|
+
AND (reaper_pid IS NULL) = (attempt IS NULL)),
|
|
97
|
+
CHECK (reaper_pid IS NULL OR gone IS NOT NULL)
|
|
98
|
+
) STRICT;
|
|
37
99
|
`;
|
|
38
|
-
const ADDED_TABLES = ['resources', 'browsers'];
|
|
100
|
+
const ADDED_TABLES = ['resources', 'browsers', 'process_alive', 'device_leases', 'parks', 'holds', 'owner_checks'];
|
|
39
101
|
const SCHEMA = `
|
|
40
102
|
CREATE TABLE claims (
|
|
41
103
|
id TEXT PRIMARY KEY,
|
|
@@ -151,11 +213,12 @@ function untilFree(deadline, action, onTimeout) {
|
|
|
151
213
|
}
|
|
152
214
|
}
|
|
153
215
|
}
|
|
216
|
+
export const UNIDENTIFIED = 'an unidentified caller';
|
|
154
217
|
export class Store {
|
|
155
218
|
db;
|
|
156
219
|
dir;
|
|
157
220
|
lockWaitMs;
|
|
158
|
-
actor = { who:
|
|
221
|
+
actor = { who: UNIDENTIFIED, folder: process.cwd() };
|
|
159
222
|
constructor(dir, db, lockWaitMs) {
|
|
160
223
|
this.dir = dir;
|
|
161
224
|
this.db = db;
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import { systemClock } from "../clock.js";
|
|
4
|
+
import { laneDir, lastCallFile } from "../up/lane-files.js";
|
|
5
|
+
import { tcpProbe } from "./ports.js";
|
|
6
|
+
import { Refusal } from "./refusal.js";
|
|
7
|
+
import { Registry } from "./registry.js";
|
|
8
|
+
/** How long a one-shot lane call waits on a busy registry to record itself as the lane's last use before it goes on without. */
|
|
9
|
+
export const TOUCH_WAIT_MS = 250;
|
|
10
|
+
export const TOUCH_RETRY_MS = 500;
|
|
11
|
+
/** How often a call that runs for long, such as `exec`'s command or an `up` hook, counts again while it runs. */
|
|
12
|
+
export const HEARTBEAT_MS = 60_000;
|
|
13
|
+
function tagOf(token) {
|
|
14
|
+
return createHash('sha256').update(token).digest('hex').slice(0, 16);
|
|
15
|
+
}
|
|
16
|
+
function readCall(file) {
|
|
17
|
+
try {
|
|
18
|
+
const raw = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
19
|
+
return typeof raw.lane === 'string' && typeof raw.at === 'number' && Number.isFinite(raw.at)
|
|
20
|
+
? { lane: raw.lane, at: raw.at }
|
|
21
|
+
: null;
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Records `at` as the time of the lane's latest call in a file in the lane's folder, without the registry's lock, so a
|
|
29
|
+
* call counts even while the registry is busy. The record names the lane's token, so a later lane given the same id
|
|
30
|
+
* never reads it as its own.
|
|
31
|
+
*/
|
|
32
|
+
export function recordCall(stateDir, ref, at) {
|
|
33
|
+
try {
|
|
34
|
+
const dir = laneDir(stateDir, ref.id);
|
|
35
|
+
const file = lastCallFile(dir);
|
|
36
|
+
const lane = tagOf(ref.token);
|
|
37
|
+
const known = readCall(file);
|
|
38
|
+
if (known?.lane === lane && known.at >= at)
|
|
39
|
+
return;
|
|
40
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
41
|
+
const tmp = `${file}.${String(process.pid)}.tmp`;
|
|
42
|
+
fs.writeFileSync(tmp, JSON.stringify({ lane, at }));
|
|
43
|
+
fs.renameSync(tmp, file);
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/** The time of a lane's last call: the later of the registry's `last_used_at` and the last call recorded in the lane's folder. */
|
|
50
|
+
export function laneLastCall(stateDir, lane) {
|
|
51
|
+
let recorded;
|
|
52
|
+
try {
|
|
53
|
+
recorded = readCall(lastCallFile(laneDir(stateDir, lane.id)));
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return lane.lastUsedAt;
|
|
57
|
+
}
|
|
58
|
+
return recorded?.lane === tagOf(lane.token) ? Math.max(lane.lastUsedAt, recorded.at) : lane.lastUsedAt;
|
|
59
|
+
}
|
|
60
|
+
function isBusy(error) {
|
|
61
|
+
return error instanceof Refusal && error.code === 'registry-busy';
|
|
62
|
+
}
|
|
63
|
+
/** Whether a touch failed because its lane has ended: the lane is gone, or its id now names another lane. */
|
|
64
|
+
export function laneHasEnded(error) {
|
|
65
|
+
return error instanceof Refusal && (error.code === 'lane-gone' || error.code === 'token-mismatch');
|
|
66
|
+
}
|
|
67
|
+
function whenFree(site, body) {
|
|
68
|
+
let registry;
|
|
69
|
+
try {
|
|
70
|
+
registry = Registry.open({
|
|
71
|
+
dir: site.dir,
|
|
72
|
+
processes: site.processes,
|
|
73
|
+
ports: tcpProbe,
|
|
74
|
+
devices: {},
|
|
75
|
+
lockWaitMs: site.waitMs ?? TOUCH_WAIT_MS,
|
|
76
|
+
...(site.clock ? { clock: site.clock } : {}),
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
if (isBusy(error))
|
|
81
|
+
return false;
|
|
82
|
+
throw error;
|
|
83
|
+
}
|
|
84
|
+
try {
|
|
85
|
+
body(registry);
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
if (isBusy(error))
|
|
90
|
+
return false;
|
|
91
|
+
throw error;
|
|
92
|
+
}
|
|
93
|
+
finally {
|
|
94
|
+
registry.close();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Writes a touch to the registry alone, for a caller that has already recorded the call. False when the registry
|
|
99
|
+
* stayed busy; a lane that has ended throws.
|
|
100
|
+
*/
|
|
101
|
+
export function landTouch(site, ref, action) {
|
|
102
|
+
return whenFree(site, registry => {
|
|
103
|
+
registry.touch(ref, action);
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Records a call as its lane's last use. False when the registry stayed busy for the site's wait, so only the lane's
|
|
108
|
+
* folder holds the call; a lane that has ended throws.
|
|
109
|
+
*/
|
|
110
|
+
export function touchLane(site, ref, action) {
|
|
111
|
+
recordCall(site.dir, ref, (site.clock ?? systemClock).now());
|
|
112
|
+
return landTouch(site, ref, action);
|
|
113
|
+
}
|
|
114
|
+
/** As touchLane, for the lane the owner holds in a folder; a folder with no lane of the owner's is left alone. */
|
|
115
|
+
export function touchOwnLane(site, owner, worktree, action) {
|
|
116
|
+
return whenFree(site, registry => {
|
|
117
|
+
const lane = registry.find(owner, worktree);
|
|
118
|
+
if (!lane)
|
|
119
|
+
return;
|
|
120
|
+
const ref = { id: lane.id, token: lane.token };
|
|
121
|
+
recordCall(site.dir, ref, (site.clock ?? systemClock).now());
|
|
122
|
+
registry.touch(ref, action);
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Touches the lane every `everyMs` until the returned function is called, so a call that runs for long keeps counting
|
|
127
|
+
* while it runs. It never waits on a busy registry, records the call in the lane's folder even when the registry can't
|
|
128
|
+
* be opened, and stops early only once the lane has ended.
|
|
129
|
+
*/
|
|
130
|
+
export function keepTouching(site, ref, action, everyMs = HEARTBEAT_MS) {
|
|
131
|
+
const timer = setInterval(() => {
|
|
132
|
+
try {
|
|
133
|
+
touchLane({ ...site, waitMs: 0 }, ref, action);
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
if (laneHasEnded(error))
|
|
137
|
+
clearInterval(timer);
|
|
138
|
+
}
|
|
139
|
+
}, everyMs);
|
|
140
|
+
timer.unref();
|
|
141
|
+
return () => {
|
|
142
|
+
clearInterval(timer);
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Touches for a long-lived caller, such as the MCP server: the call is recorded at once, and a touch the registry
|
|
147
|
+
* refused or couldn't take is tried again every `retryMs`, never waiting on the lock, until it lands, the lane ends, or
|
|
148
|
+
* a later touch takes its place.
|
|
149
|
+
*/
|
|
150
|
+
export class LaneToucher {
|
|
151
|
+
site;
|
|
152
|
+
retryMs;
|
|
153
|
+
retry;
|
|
154
|
+
constructor(site, retryMs = TOUCH_RETRY_MS) {
|
|
155
|
+
this.site = { ...site, waitMs: 0 };
|
|
156
|
+
this.retryMs = retryMs;
|
|
157
|
+
}
|
|
158
|
+
touch(ref, action) {
|
|
159
|
+
clearTimeout(this.retry);
|
|
160
|
+
this.retry = undefined;
|
|
161
|
+
if (!touchLane(this.site, ref, action))
|
|
162
|
+
this.later(ref, action);
|
|
163
|
+
}
|
|
164
|
+
later(ref, action) {
|
|
165
|
+
this.retry = setTimeout(() => {
|
|
166
|
+
this.retry = undefined;
|
|
167
|
+
let landed = false;
|
|
168
|
+
try {
|
|
169
|
+
landed = landTouch(this.site, ref, action);
|
|
170
|
+
}
|
|
171
|
+
catch (error) {
|
|
172
|
+
if (laneHasEnded(error))
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
if (!landed)
|
|
176
|
+
this.later(ref, action);
|
|
177
|
+
}, this.retryMs);
|
|
178
|
+
this.retry.unref();
|
|
179
|
+
}
|
|
180
|
+
}
|