agentlane 0.5.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +44 -0
- package/README.md +70 -74
- package/dist/agents/launcher.js +3 -1
- package/dist/cli.js +3 -1
- package/dist/hooks/run.js +39 -14
- package/dist/plugin/aims.js +304 -6
- package/dist/plugin/guard.js +28 -2
- package/dist/plugin/hook-cli.js +11 -3
- package/dist/processes.js +13 -0
- package/dist/project/config.js +42 -4
- package/dist/project/schema.js +44 -0
- package/dist/registry/parks.js +8 -2
- package/dist/registry/registry.js +183 -35
- package/dist/registry/resources.js +46 -7
- package/dist/registry/shared.js +384 -0
- package/dist/registry/status.js +10 -0
- package/dist/registry/store.js +98 -7
- package/dist/registry-cli.js +45 -1
- package/dist/up/doctor.js +42 -1
- package/dist/up/exec.js +87 -7
- package/dist/up/lane-files.js +13 -0
- package/dist/up/lane-services.js +10 -0
- package/dist/up/maestro-env.js +6 -2
- package/dist/up/park.js +5 -3
- package/dist/up/preflight.js +2 -1
- package/dist/up/resources.js +104 -21
- package/dist/up/restart.js +21 -6
- package/dist/up/shared.js +321 -0
- package/dist/up/sweep.js +31 -6
- package/dist/up/up.js +76 -14
- package/dist/up/waker.js +9 -5
- package/dist/up-cli.js +34 -11
- package/package.json +8 -2
- package/schema/agentlane.schema.json +65 -0
- package/skills/create-verification-skill/SKILL.md +9 -4
- package/skills/dev-loop/SKILL.md +9 -3
- package/skills/setup/SKILL.md +3 -3
package/dist/registry/parks.js
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import { DEVICE_NOUN } from "../devices/backend.js";
|
|
2
2
|
import { names } from "./rows.js";
|
|
3
3
|
import { int, optInt, optText, text } from "./store.js";
|
|
4
|
-
export const NOTHING_TO_RESTART = { device: null, services: [], browser: null, app: null };
|
|
4
|
+
export const NOTHING_TO_RESTART = { device: null, services: [], shared: [], browser: null, app: null };
|
|
5
|
+
function strings(value) {
|
|
6
|
+
return Array.isArray(value) ? value.filter((s) => typeof s === 'string') : [];
|
|
7
|
+
}
|
|
5
8
|
function restartsOf(raw) {
|
|
6
9
|
const parsed = JSON.parse(raw);
|
|
7
10
|
return {
|
|
8
11
|
device: parsed.device ?? null,
|
|
9
|
-
services:
|
|
12
|
+
services: strings(parsed.services),
|
|
13
|
+
shared: strings(parsed.shared),
|
|
10
14
|
browser: parsed.browser ?? null,
|
|
11
15
|
app: typeof parsed.app === 'string' ? parsed.app : null,
|
|
12
16
|
};
|
|
@@ -43,6 +47,7 @@ export function mergeRestarts(earlier, later) {
|
|
|
43
47
|
return {
|
|
44
48
|
device: later.device ?? earlier.device,
|
|
45
49
|
services: [...new Set([...earlier.services, ...later.services])],
|
|
50
|
+
shared: [...new Set([...earlier.shared, ...later.shared])],
|
|
46
51
|
browser: later.browser ?? earlier.browser,
|
|
47
52
|
app: later.app ?? earlier.app,
|
|
48
53
|
};
|
|
@@ -52,6 +57,7 @@ export function describeRestarts(restarts) {
|
|
|
52
57
|
const { device } = restarts;
|
|
53
58
|
const parts = [
|
|
54
59
|
...(device ? [`${DEVICE_NOUN[device.platform]} ${device.name} (${device.deviceTypeName}, ${device.runtimeName})`] : []),
|
|
60
|
+
...(restarts.shared.length > 0 ? [`shared services ${restarts.shared.join(', ')}`] : []),
|
|
55
61
|
...(restarts.services.length > 0 ? [`services ${restarts.services.join(', ')}`] : []),
|
|
56
62
|
...(restarts.app ? [`the app ${restarts.app}`] : []),
|
|
57
63
|
...(restarts.browser ? [`the ${restarts.browser} browser`] : []),
|
|
@@ -13,9 +13,10 @@ import { dropPark, NOTHING_TO_RESTART, readPark, writePark } from "./parks.js";
|
|
|
13
13
|
import { configPath, diskNames } from "./paths.js";
|
|
14
14
|
import { deletePoolDevice, listPrunable } from "./prune.js";
|
|
15
15
|
import { NotClaimed, Refusal, UsageError } from "./refusal.js";
|
|
16
|
-
import { endTeardown, failResource, failTeardown, finishResource, holdersBeforeLock, pendingTeardowns, startResource, takeTeardowns, } from "./resources.js";
|
|
16
|
+
import { endTeardown, failResource, failTeardown, finishResource, followResourceNeeds, holdersBeforeLock, pendingTeardowns, startResource, takeTeardowns, } from "./resources.js";
|
|
17
17
|
import { loadLane, ownerFromRow, processesOf } from "./rows.js";
|
|
18
|
-
import {
|
|
18
|
+
import { beginStart, beginStop, endCommand, failStart, failStop, finishStart, finishStop, laneActive, laneNeeds, markNeedsUnneeded, noteCommand, readShared, recordLaneNeeds, SHARED_IDLE_MS, SHARED_STOP_RETRY_MS, sharedHoldersBeforeLock, stopCandidates, } from "./shared.js";
|
|
19
|
+
import { readsAsIs, readStatus } from "./status.js";
|
|
19
20
|
import { int, optInt, optText, Store, text, UNIDENTIFIED } from "./store.js";
|
|
20
21
|
export const STEPS = [
|
|
21
22
|
'claim:inspected',
|
|
@@ -1169,6 +1170,7 @@ export class Registry {
|
|
|
1169
1170
|
const id = text(row, 'id');
|
|
1170
1171
|
const device = db.prepare('SELECT name FROM devices WHERE claim_id = ?').get(id);
|
|
1171
1172
|
const processes = processesOf(db, id);
|
|
1173
|
+
const needed = laneActive(readPark(db, id)) ? laneNeeds(db, id) : [];
|
|
1172
1174
|
const ended = {
|
|
1173
1175
|
id,
|
|
1174
1176
|
owner: describeOwner(ownerFromRow(row)),
|
|
@@ -1186,8 +1188,17 @@ export class Registry {
|
|
|
1186
1188
|
by_owner_key, by_label, by_worktree, ended_at)
|
|
1187
1189
|
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);
|
|
1188
1190
|
db.prepare('DELETE FROM claims WHERE id = ?').run(id);
|
|
1191
|
+
markNeedsUnneeded(db, needed, now);
|
|
1189
1192
|
return ended;
|
|
1190
1193
|
}
|
|
1194
|
+
/** Writes a change to a lane's park, marking the shared services it needed as unneeded when it stops being active. */
|
|
1195
|
+
changePark(laneId, change) {
|
|
1196
|
+
const db = this.store.db;
|
|
1197
|
+
const needed = laneActive(readPark(db, laneId)) ? laneNeeds(db, laneId) : [];
|
|
1198
|
+
change();
|
|
1199
|
+
if (needed.length > 0 && !laneActive(readPark(db, laneId)))
|
|
1200
|
+
markNeedsUnneeded(db, needed, this.clock.now());
|
|
1201
|
+
}
|
|
1191
1202
|
endAndLease(rows, reason, by, me, keeper) {
|
|
1192
1203
|
const db = this.store.db;
|
|
1193
1204
|
const now = this.clock.now();
|
|
@@ -1721,14 +1732,16 @@ export class Registry {
|
|
|
1721
1732
|
const park = readPark(db, ref.id);
|
|
1722
1733
|
if (park && park.state !== 'parked' && !this.heldByDead(park, dead))
|
|
1723
1734
|
return null;
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1735
|
+
this.changePark(ref.id, () => {
|
|
1736
|
+
writePark(db, ref.id, {
|
|
1737
|
+
state: 'parking',
|
|
1738
|
+
since: this.clock.now(),
|
|
1739
|
+
parkedAt: park?.parkedAt ?? null,
|
|
1740
|
+
holder: me,
|
|
1741
|
+
attempt,
|
|
1742
|
+
restarts: park?.restarts ?? NOTHING_TO_RESTART,
|
|
1743
|
+
problems: park?.problems ?? [],
|
|
1744
|
+
});
|
|
1732
1745
|
});
|
|
1733
1746
|
return loadLane(db, ref.id);
|
|
1734
1747
|
});
|
|
@@ -1740,10 +1753,12 @@ export class Registry {
|
|
|
1740
1753
|
const park = readPark(db, ref.id);
|
|
1741
1754
|
if (park?.state !== 'parking' || park.attempt !== attempt)
|
|
1742
1755
|
return;
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1756
|
+
this.changePark(ref.id, () => {
|
|
1757
|
+
if (park.parkedAt === null)
|
|
1758
|
+
dropPark(db, ref.id);
|
|
1759
|
+
else
|
|
1760
|
+
writePark(db, ref.id, { ...park, state: 'parked', since: this.clock.now(), holder: null, attempt: null });
|
|
1761
|
+
});
|
|
1747
1762
|
});
|
|
1748
1763
|
}
|
|
1749
1764
|
/**
|
|
@@ -1791,14 +1806,16 @@ export class Registry {
|
|
|
1791
1806
|
kept.push(`kept ${NOUN[platform]} ${name}: this process has no ${NOUN[platform]} tool to shut it down`);
|
|
1792
1807
|
}
|
|
1793
1808
|
}
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1809
|
+
this.changePark(ref.id, () => {
|
|
1810
|
+
writePark(db, ref.id, {
|
|
1811
|
+
state: 'parked',
|
|
1812
|
+
since: now,
|
|
1813
|
+
parkedAt: now,
|
|
1814
|
+
holder: null,
|
|
1815
|
+
attempt: null,
|
|
1816
|
+
restarts,
|
|
1817
|
+
problems: kept,
|
|
1818
|
+
});
|
|
1802
1819
|
});
|
|
1803
1820
|
return { parked: true, work: this.inWrite({ own, others }, me, keeper, notes, 'park:stopped-device') };
|
|
1804
1821
|
});
|
|
@@ -1863,14 +1880,16 @@ export class Registry {
|
|
|
1863
1880
|
if (park?.attempt !== attempt)
|
|
1864
1881
|
return;
|
|
1865
1882
|
const now = this.clock.now();
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1883
|
+
this.changePark(ref.id, () => {
|
|
1884
|
+
writePark(db, ref.id, {
|
|
1885
|
+
...park,
|
|
1886
|
+
state: 'parked',
|
|
1887
|
+
since: now,
|
|
1888
|
+
parkedAt: park.parkedAt ?? now,
|
|
1889
|
+
holder: null,
|
|
1890
|
+
attempt: null,
|
|
1891
|
+
problems,
|
|
1892
|
+
});
|
|
1874
1893
|
});
|
|
1875
1894
|
});
|
|
1876
1895
|
}
|
|
@@ -1927,25 +1946,35 @@ export class Registry {
|
|
|
1927
1946
|
this.store.db.prepare('DELETE FROM process_alive WHERE claim_id = ? AND service = ?').run(ref.id, service);
|
|
1928
1947
|
});
|
|
1929
1948
|
}
|
|
1949
|
+
/**
|
|
1950
|
+
* Records on each of the lane's resources the shared services it needs now, besides those it recorded before, and the
|
|
1951
|
+
* lane's repository, for its teardown to start.
|
|
1952
|
+
*/
|
|
1953
|
+
followResourceNeeds(ref, resources) {
|
|
1954
|
+
this.store.write(() => {
|
|
1955
|
+
const row = this.requireLane(ref);
|
|
1956
|
+
followResourceNeeds(this.store.db, { id: ref.id, repository: text(row, 'project_root') }, resources);
|
|
1957
|
+
});
|
|
1958
|
+
}
|
|
1930
1959
|
/** Records what the resource's teardown needs before its create runs; `kept` when this lane already has it. */
|
|
1931
1960
|
startResource(ref, spec) {
|
|
1932
1961
|
const me = this.self();
|
|
1933
1962
|
const holders = holdersBeforeLock(this.store.db, this.processes);
|
|
1934
1963
|
return this.store.write(() => {
|
|
1935
1964
|
const row = this.requireLane(ref);
|
|
1936
|
-
return startResource(this.store.db, { id: ref.id, worktree: text(row, 'worktree') }, spec, me, holders, this.clock.now());
|
|
1965
|
+
return startResource(this.store.db, { id: ref.id, worktree: text(row, 'worktree'), repository: text(row, 'project_root') }, spec, me, holders, this.clock.now());
|
|
1937
1966
|
});
|
|
1938
1967
|
}
|
|
1939
1968
|
finishResource(lane, name, exports) {
|
|
1940
1969
|
const me = this.self();
|
|
1941
1970
|
this.store.write(() => {
|
|
1942
|
-
finishResource(this.store.db, lane, name, exports, me);
|
|
1971
|
+
finishResource(this.store.db, lane, name, exports, me, this.clock.now());
|
|
1943
1972
|
});
|
|
1944
1973
|
}
|
|
1945
1974
|
failResource(lane, name, problem) {
|
|
1946
1975
|
const me = this.self();
|
|
1947
1976
|
this.store.write(() => {
|
|
1948
|
-
failResource(this.store.db, lane, name, problem, me);
|
|
1977
|
+
failResource(this.store.db, lane, name, problem, me, this.clock.now());
|
|
1949
1978
|
});
|
|
1950
1979
|
}
|
|
1951
1980
|
/** Leases the resources of ended lanes to this process for teardown, last created first. */
|
|
@@ -1957,7 +1986,7 @@ export class Registry {
|
|
|
1957
1986
|
finishTeardown(lane, name) {
|
|
1958
1987
|
const me = this.self();
|
|
1959
1988
|
this.store.write(() => {
|
|
1960
|
-
endTeardown(this.store.db, lane, name, me);
|
|
1989
|
+
endTeardown(this.store.db, lane, name, me, this.clock.now());
|
|
1961
1990
|
});
|
|
1962
1991
|
}
|
|
1963
1992
|
failTeardown(lane, name, problem) {
|
|
@@ -1966,6 +1995,108 @@ export class Registry {
|
|
|
1966
1995
|
failTeardown(this.store.db, lane, name, problem, me, this.clock.now());
|
|
1967
1996
|
});
|
|
1968
1997
|
}
|
|
1998
|
+
/**
|
|
1999
|
+
* Records the shared services the lane needs, by name: exactly `names`, from its resources' and services' `needs`.
|
|
2000
|
+
* While the lane is active, one it no longer needs counts as unneeded from now if no other active lane needs it.
|
|
2001
|
+
*/
|
|
2002
|
+
recordNeeds(ref, names) {
|
|
2003
|
+
this.store.write(() => {
|
|
2004
|
+
const row = this.requireLane(ref);
|
|
2005
|
+
recordLaneNeeds(this.store.db, ref.id, text(row, 'project_root'), names, this.clock.now());
|
|
2006
|
+
});
|
|
2007
|
+
}
|
|
2008
|
+
/** The shared services the lane's record says it needs, by name. */
|
|
2009
|
+
needsOf(laneId) {
|
|
2010
|
+
return laneNeeds(this.store.db, laneId).map(n => n.name);
|
|
2011
|
+
}
|
|
2012
|
+
/**
|
|
2013
|
+
* Takes the start of shared service `name` for a lane, or for a teardown this process holds in `repository`, or says
|
|
2014
|
+
* who has it; see `beginStart`. A lane's start also records that the lane needs it.
|
|
2015
|
+
*/
|
|
2016
|
+
beginSharedStart(need, name, spec, foundDown) {
|
|
2017
|
+
const me = this.self();
|
|
2018
|
+
const holders = sharedHoldersBeforeLock(this.store.db, this.processes);
|
|
2019
|
+
return this.store.write(() => {
|
|
2020
|
+
let whom;
|
|
2021
|
+
if ('lane' in need) {
|
|
2022
|
+
const row = this.requireLane(need.lane);
|
|
2023
|
+
whom = { kind: 'lane', repository: text(row, 'project_root'), lane: need.lane.id };
|
|
2024
|
+
}
|
|
2025
|
+
else
|
|
2026
|
+
whom = { kind: 'teardown', repository: need.repository };
|
|
2027
|
+
return beginStart(this.store.db, whom, name, spec, foundDown, me, holders, this.clock.now());
|
|
2028
|
+
});
|
|
2029
|
+
}
|
|
2030
|
+
/** Records the start this process holds as done; false when it no longer holds it. */
|
|
2031
|
+
finishSharedStart(key) {
|
|
2032
|
+
const me = this.self();
|
|
2033
|
+
return this.store.write(() => finishStart(this.store.db, key, me, this.clock.now()));
|
|
2034
|
+
}
|
|
2035
|
+
/** Records the start this process holds as failed; it stays recorded as running, with `problem`, for the sweep to stop. */
|
|
2036
|
+
failSharedStart(key, problem) {
|
|
2037
|
+
const me = this.self();
|
|
2038
|
+
this.store.write(() => {
|
|
2039
|
+
failStart(this.store.db, key, problem, me, this.clock.now());
|
|
2040
|
+
});
|
|
2041
|
+
}
|
|
2042
|
+
/**
|
|
2043
|
+
* Records the shell of the start or stop command this process runs for a shared service it holds, so a process taking
|
|
2044
|
+
* over once this one has died waits for that command. Nothing is recorded when the shell has already exited. Throws
|
|
2045
|
+
* when `ps` can't be read, or when this process no longer holds the shared service.
|
|
2046
|
+
*/
|
|
2047
|
+
noteSharedCommand(key, pid) {
|
|
2048
|
+
const me = this.self();
|
|
2049
|
+
const table = this.processes.startTimes([pid]);
|
|
2050
|
+
if (!table)
|
|
2051
|
+
throw new Error(`could not read the start time of its shell, pid ${String(pid)}`);
|
|
2052
|
+
const start = table.get(pid);
|
|
2053
|
+
if (start === undefined)
|
|
2054
|
+
return;
|
|
2055
|
+
const noted = this.store.write(() => noteCommand(this.store.db, key, { pid, start }, me));
|
|
2056
|
+
if (!noted)
|
|
2057
|
+
throw new Error(`this process no longer holds shared service ${key.name}`);
|
|
2058
|
+
}
|
|
2059
|
+
/** Forgets the command this process recorded for a shared service, once it has ended. */
|
|
2060
|
+
endSharedCommand(key) {
|
|
2061
|
+
const me = this.self();
|
|
2062
|
+
this.store.write(() => {
|
|
2063
|
+
endCommand(this.store.db, key, me);
|
|
2064
|
+
});
|
|
2065
|
+
}
|
|
2066
|
+
/** The shared services a sweep may have to stop or mark, by one read of the registry; no process starts. */
|
|
2067
|
+
sharedStopCandidates() {
|
|
2068
|
+
const now = this.clock.now();
|
|
2069
|
+
return stopCandidates(this.store.db, now, { idleSince: now - SHARED_IDLE_MS, retrySince: now - SHARED_STOP_RETRY_MS });
|
|
2070
|
+
}
|
|
2071
|
+
/** Takes the stop of a shared service nothing has needed for SHARED_IDLE_MS, or says why it is left; see `beginStop`. */
|
|
2072
|
+
beginSharedStop(key) {
|
|
2073
|
+
const me = this.self();
|
|
2074
|
+
const holders = sharedHoldersBeforeLock(this.store.db, this.processes);
|
|
2075
|
+
return this.store.write(() => {
|
|
2076
|
+
this.store.actor = { who: `${TOOL} stopping shared service ${key.name}`, folder: key.repository };
|
|
2077
|
+
const now = this.clock.now();
|
|
2078
|
+
return beginStop(this.store.db, key, me, holders, now, {
|
|
2079
|
+
idleSince: now - SHARED_IDLE_MS,
|
|
2080
|
+
retrySince: now - SHARED_STOP_RETRY_MS,
|
|
2081
|
+
});
|
|
2082
|
+
});
|
|
2083
|
+
}
|
|
2084
|
+
finishSharedStop(key) {
|
|
2085
|
+
const me = this.self();
|
|
2086
|
+
this.store.write(() => {
|
|
2087
|
+
finishStop(this.store.db, key, me, this.clock.now());
|
|
2088
|
+
});
|
|
2089
|
+
}
|
|
2090
|
+
failSharedStop(key, problem) {
|
|
2091
|
+
const me = this.self();
|
|
2092
|
+
this.store.write(() => {
|
|
2093
|
+
failStop(this.store.db, key, problem, me, this.clock.now());
|
|
2094
|
+
});
|
|
2095
|
+
}
|
|
2096
|
+
/** Every shared service, with the lanes that need it, when it stops if none does, and its last problem. */
|
|
2097
|
+
sharedServices() {
|
|
2098
|
+
return readShared(this.store.db, this.clock.now());
|
|
2099
|
+
}
|
|
1969
2100
|
/** The browsers of lanes that have ended, however they ended: their processes may still run. */
|
|
1970
2101
|
endedBrowsers() {
|
|
1971
2102
|
return this.store.db
|
|
@@ -2034,9 +2165,26 @@ function browserServices(request) {
|
|
|
2034
2165
|
? [debug, { name: BROWSER_STREAM_SERVICE, type: BROWSER_PORT_TYPE }]
|
|
2035
2166
|
: [debug];
|
|
2036
2167
|
}
|
|
2168
|
+
/** The shared services lane `laneId` needs, by name, as its record says; read without upgrading the registry. */
|
|
2169
|
+
export function readLaneNeeds(dir, laneId, lockWaitMs = DEFAULT_LOCK_WAIT_MS) {
|
|
2170
|
+
const store = Store.openToRead(dir, lockWaitMs);
|
|
2171
|
+
if (!store)
|
|
2172
|
+
return [];
|
|
2173
|
+
try {
|
|
2174
|
+
return store.lacking().includes('shared_needs') ? [] : laneNeeds(store.db, laneId).map(n => n.name);
|
|
2175
|
+
}
|
|
2176
|
+
finally {
|
|
2177
|
+
store.close();
|
|
2178
|
+
}
|
|
2179
|
+
}
|
|
2037
2180
|
export function readRegistryStatus(options) {
|
|
2038
2181
|
const config = options.config ?? loadConfig(options.dir);
|
|
2039
|
-
const
|
|
2182
|
+
const lockWaitMs = options.lockWaitMs ?? DEFAULT_LOCK_WAIT_MS;
|
|
2183
|
+
let store = Store.openToRead(options.dir, lockWaitMs);
|
|
2184
|
+
if (store && !readsAsIs(store.lacking())) {
|
|
2185
|
+
store.close();
|
|
2186
|
+
store = Store.openExisting(options.dir, lockWaitMs);
|
|
2187
|
+
}
|
|
2040
2188
|
try {
|
|
2041
2189
|
return readStatus(store, {
|
|
2042
2190
|
dir: options.dir,
|
|
@@ -3,6 +3,7 @@ import { formatTime } from "./format.js";
|
|
|
3
3
|
import { isProven, judgeProcess } from "./owner.js";
|
|
4
4
|
import { Refusal } from "./refusal.js";
|
|
5
5
|
import { names, optValues, values } from "./rows.js";
|
|
6
|
+
import { markUnneeded } from "./shared.js";
|
|
6
7
|
import { int, optInt, optText, text } from "./store.js";
|
|
7
8
|
export const TEARDOWN_RETRY_MS = 5 * 60_000;
|
|
8
9
|
const ORPHANED = 'NOT EXISTS (SELECT 1 FROM claims c WHERE c.id = r.claim_id)';
|
|
@@ -42,6 +43,21 @@ function busy(row, spec) {
|
|
|
42
43
|
},
|
|
43
44
|
], [`${CLI} up (again, once pid ${pid} has finished)`, `kill ${pid} (only if that process is hung)`]);
|
|
44
45
|
}
|
|
46
|
+
function addNeeds(db, row, repository, needs) {
|
|
47
|
+
const kept = [...new Set([...needs, ...names(row, 'needs')])];
|
|
48
|
+
db.prepare('UPDATE resources SET repository = ?, needs = ? WHERE seq = ?').run(repository, JSON.stringify(kept), int(row, 'seq'));
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Records on each of the lane's resources the shared services it needs now, besides those it recorded before, and the
|
|
52
|
+
* lane's repository, for its teardown to start.
|
|
53
|
+
*/
|
|
54
|
+
export function followResourceNeeds(db, lane, resources) {
|
|
55
|
+
for (const resource of resources) {
|
|
56
|
+
const row = db.prepare('SELECT seq, needs FROM resources WHERE claim_id = ? AND name = ?').get(lane.id, resource.name);
|
|
57
|
+
if (row)
|
|
58
|
+
addNeeds(db, row, lane.repository, resource.needs);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
45
61
|
export function startResource(db, lane, spec, me, holders, now) {
|
|
46
62
|
const row = db.prepare('SELECT * FROM resources WHERE claim_id = ? AND name = ?').get(lane.id, spec.name);
|
|
47
63
|
if (row) {
|
|
@@ -49,22 +65,39 @@ export function startResource(db, lane, spec, me, holders, now) {
|
|
|
49
65
|
if (held === 'held')
|
|
50
66
|
throw busy(row, spec);
|
|
51
67
|
const exports = optValues(row, 'exports');
|
|
52
|
-
if (exports !== null && held === 'free')
|
|
68
|
+
if (exports !== null && held === 'free') {
|
|
69
|
+
addNeeds(db, row, lane.repository, spec.needs);
|
|
53
70
|
return { kind: 'kept', exports, createdAt: int(row, 'created_at') };
|
|
54
|
-
|
|
71
|
+
}
|
|
72
|
+
db.prepare(`UPDATE resources SET cwd = ?, teardown = ?, timeout_ms = ?, env = ?, cleared = ?, repository = ?, needs = ?,
|
|
73
|
+
exports = NULL, problem = NULL WHERE seq = ?`).run(spec.cwd, spec.teardown, spec.timeoutMs, JSON.stringify(spec.env), JSON.stringify(spec.cleared), lane.repository, JSON.stringify(spec.needs), int(row, 'seq'));
|
|
55
74
|
hold(db, int(row, 'seq'), me, now);
|
|
56
75
|
return { kind: 'create', retry: optText(row, 'problem') ?? 'its last create did not finish' };
|
|
57
76
|
}
|
|
58
|
-
db.prepare(`INSERT INTO resources (claim_id, name, worktree, cwd, teardown, timeout_ms, env, cleared, created_at,
|
|
77
|
+
db.prepare(`INSERT INTO resources (claim_id, name, worktree, cwd, teardown, timeout_ms, env, cleared, repository, needs, created_at,
|
|
59
78
|
holder_pid, holder_start, holder_since)
|
|
60
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(lane.id, spec.name, lane.worktree, spec.cwd, spec.teardown, spec.timeoutMs, JSON.stringify(spec.env), JSON.stringify(spec.cleared), now, me.pid, me.start, now);
|
|
79
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(lane.id, spec.name, lane.worktree, spec.cwd, spec.teardown, spec.timeoutMs, JSON.stringify(spec.env), JSON.stringify(spec.cleared), lane.repository, JSON.stringify(spec.needs), now, me.pid, me.start, now);
|
|
61
80
|
return { kind: 'create', retry: null };
|
|
62
81
|
}
|
|
63
|
-
|
|
82
|
+
/** The shared services a resource row `me` holds needs, read before the row is let go, so they can be marked unneeded after. */
|
|
83
|
+
function heldNeeds(db, lane, name, me) {
|
|
84
|
+
const row = db.prepare(`SELECT repository, needs FROM resources WHERE ${MINE}`).get(lane, name, me.pid, me.start);
|
|
85
|
+
const repository = row ? optText(row, 'repository') : null;
|
|
86
|
+
return row && repository !== null ? { repository, needs: names(row, 'needs') } : null;
|
|
87
|
+
}
|
|
88
|
+
function letGo(db, held, now) {
|
|
89
|
+
if (held)
|
|
90
|
+
markUnneeded(db, held.repository, held.needs, now);
|
|
91
|
+
}
|
|
92
|
+
export function finishResource(db, lane, name, exports, me, now) {
|
|
93
|
+
const held = heldNeeds(db, lane, name, me);
|
|
64
94
|
db.prepare(`UPDATE resources SET exports = ?, problem = NULL, holder_pid = NULL, holder_start = NULL, holder_since = NULL WHERE ${MINE}`).run(JSON.stringify(exports), lane, name, me.pid, me.start);
|
|
95
|
+
letGo(db, held, now);
|
|
65
96
|
}
|
|
66
|
-
export function failResource(db, lane, name, problem, me) {
|
|
97
|
+
export function failResource(db, lane, name, problem, me, now) {
|
|
98
|
+
const held = heldNeeds(db, lane, name, me);
|
|
67
99
|
db.prepare(`UPDATE resources SET problem = ?, holder_pid = NULL, holder_start = NULL, holder_since = NULL WHERE ${MINE}`).run(problem, lane, name, me.pid, me.start);
|
|
100
|
+
letGo(db, held, now);
|
|
68
101
|
}
|
|
69
102
|
export function takeTeardowns(db, me, holders, now, choice) {
|
|
70
103
|
const rows = choice.lane === undefined
|
|
@@ -82,11 +115,13 @@ export function takeTeardowns(db, me, holders, now, choice) {
|
|
|
82
115
|
lane: text(row, 'claim_id'),
|
|
83
116
|
name: text(row, 'name'),
|
|
84
117
|
worktree: text(row, 'worktree'),
|
|
118
|
+
repository: optText(row, 'repository'),
|
|
85
119
|
cwd: text(row, 'cwd'),
|
|
86
120
|
teardown: text(row, 'teardown'),
|
|
87
121
|
timeoutMs: int(row, 'timeout_ms'),
|
|
88
122
|
env: values(row, 'env'),
|
|
89
123
|
cleared: names(row, 'cleared'),
|
|
124
|
+
needs: names(row, 'needs'),
|
|
90
125
|
exports: optValues(row, 'exports'),
|
|
91
126
|
createdAt: int(row, 'created_at'),
|
|
92
127
|
attempts: int(row, 'attempts'),
|
|
@@ -94,12 +129,16 @@ export function takeTeardowns(db, me, holders, now, choice) {
|
|
|
94
129
|
}
|
|
95
130
|
return taken;
|
|
96
131
|
}
|
|
97
|
-
export function endTeardown(db, lane, name, me) {
|
|
132
|
+
export function endTeardown(db, lane, name, me, now) {
|
|
133
|
+
const held = heldNeeds(db, lane, name, me);
|
|
98
134
|
db.prepare(`DELETE FROM resources WHERE ${MINE}`).run(lane, name, me.pid, me.start);
|
|
135
|
+
letGo(db, held, now);
|
|
99
136
|
}
|
|
100
137
|
export function failTeardown(db, lane, name, problem, me, now) {
|
|
138
|
+
const held = heldNeeds(db, lane, name, me);
|
|
101
139
|
db.prepare(`UPDATE resources SET attempts = attempts + 1, last_attempt_at = ?, problem = ?,
|
|
102
140
|
holder_pid = NULL, holder_start = NULL, holder_since = NULL WHERE ${MINE}`).run(now, problem, lane, name, me.pid, me.start);
|
|
141
|
+
letGo(db, held, now);
|
|
103
142
|
}
|
|
104
143
|
export function pendingTeardowns(db) {
|
|
105
144
|
return db
|