@awebai/oats 0.61.0 → 0.63.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/README.md +1 -1
- package/bin/oats.mjs +27 -12
- package/docs/capabilities.md +1 -1
- package/docs/desktop-cli-api.md +30 -2
- package/docs/execution-targets.md +89 -4
- package/docs/implementation.md +2 -1
- package/docs/named-test-runs.md +382 -0
- package/docs/official-catalog.md +1 -1
- package/docs/packages.md +5 -5
- package/docs/release-lane.md +13 -0
- package/docs/release-notes/v0.62.0.md +223 -0
- package/docs/release-notes/v0.63.0.md +135 -0
- package/docs/souls-and-instances.md +6 -1
- package/docs/upgrading.md +135 -11
- package/docs/workspaces.md +1 -1
- package/lib/core.mjs +73 -23
- package/lib/doctor-capture.mjs +35 -0
- package/lib/session-viewer.mjs +86 -7
- package/lib/tree-copy.mjs +188 -7
- package/package-catalog.json +1 -1
- package/package.json +1 -1
- package/packages/client/cli-locator.mjs +2 -2
- package/packages/record/README.md +87 -13
- package/packages/record/bin/capture-hook.mjs +8 -0
- package/packages/record/bin/capture.mjs +200 -139
- package/packages/record/bin/recall.mjs +8 -3
- package/packages/record/bin/setup.mjs +116 -225
- package/packages/record/bin/turn-record.mjs +2 -2
- package/packages/record/docs/capture-service.md +1527 -28
- package/packages/record/docs/capture-status.schema.json +429 -12
- package/packages/record/lib/capture-admission.mjs +82 -0
- package/packages/record/lib/capture-aw-unit.mjs +137 -0
- package/packages/record/lib/capture-aw.mjs +31 -12
- package/packages/record/lib/capture-budget.mjs +31 -0
- package/packages/record/lib/capture-cc.mjs +39 -6
- package/packages/record/lib/capture-cleanup-queue.mjs +135 -0
- package/packages/record/lib/capture-cleanup-work.mjs +77 -0
- package/packages/record/lib/capture-command-operation.mjs +37 -0
- package/packages/record/lib/capture-command-output.mjs +62 -0
- package/packages/record/lib/capture-command-worker.mjs +6 -0
- package/packages/record/lib/capture-command.mjs +65 -0
- package/packages/record/lib/capture-completion.mjs +152 -0
- package/packages/record/lib/capture-context.mjs +50 -0
- package/packages/record/lib/capture-default-roots.mjs +30 -0
- package/packages/record/lib/capture-discovery.mjs +114 -0
- package/packages/record/lib/capture-failures.mjs +45 -0
- package/packages/record/lib/capture-health-doctor.mjs +13 -0
- package/packages/record/lib/capture-health-installation.mjs +117 -0
- package/packages/record/lib/capture-health.mjs +116 -0
- package/packages/record/lib/capture-hook-ownership.mjs +58 -0
- package/packages/record/lib/capture-hook-runner.mjs +17 -0
- package/packages/record/lib/capture-hook.mjs +26 -0
- package/packages/record/lib/capture-index-inventory.mjs +205 -0
- package/packages/record/lib/capture-index-outcomes.mjs +113 -0
- package/packages/record/lib/capture-index-reset.mjs +110 -0
- package/packages/record/lib/capture-index-work.mjs +77 -0
- package/packages/record/lib/capture-issue-preparation.mjs +73 -0
- package/packages/record/lib/capture-issues.mjs +15 -2
- package/packages/record/lib/capture-lock.mjs +137 -17
- package/packages/record/lib/capture-native-unit.mjs +322 -0
- package/packages/record/lib/capture-pass.mjs +116 -0
- package/packages/record/lib/capture-recovery-snapshot.mjs +182 -0
- package/packages/record/lib/capture-retained-scratch.mjs +213 -0
- package/packages/record/lib/capture-retry.mjs +102 -0
- package/packages/record/lib/capture-run.mjs +125 -0
- package/packages/record/lib/capture-scratch.mjs +12 -3
- package/packages/record/lib/capture-service-definition.mjs +104 -0
- package/packages/record/lib/capture-service-install.mjs +362 -0
- package/packages/record/lib/capture-service-ownership.mjs +137 -0
- package/packages/record/lib/capture-service-runner.mjs +18 -0
- package/packages/record/lib/capture-small-snapshot.mjs +85 -0
- package/packages/record/lib/capture-source-discovery.mjs +319 -0
- package/packages/record/lib/capture-source-queue.mjs +199 -0
- package/packages/record/lib/capture-source-work.mjs +85 -0
- package/packages/record/lib/capture-state.mjs +16 -71
- package/packages/record/lib/capture-status.mjs +171 -0
- package/packages/record/lib/capture-supervisor.mjs +136 -0
- package/packages/record/lib/capture-watch.mjs +32 -0
- package/packages/record/lib/capture-work-session.mjs +241 -0
- package/packages/record/lib/capture-worker.mjs +74 -0
- package/packages/record/lib/derived-state.mjs +58 -17
- package/packages/record/lib/file-lines.mjs +22 -6
- package/packages/record/lib/formats.mjs +4 -3
- package/packages/record/lib/index-capture.mjs +97 -0
- package/packages/record/lib/index-db.mjs +121 -66
- package/packages/record/lib/index-reset.mjs +19 -0
- package/packages/record/lib/store.mjs +46 -14
- package/packages/record/lib/transcript-prefix.mjs +125 -0
- package/skills/oats-getting-started/SKILL.md +1 -1
- package/docs/macos-test-runs.md +0 -246
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
// Private command allocations survive worker lifetimes deliberately. A durable
|
|
2
|
+
// job reference plus allocation/file identities is required; names alone never
|
|
3
|
+
// authorize adoption, proof or deletion. Public per-call scratch stays separate.
|
|
4
|
+
import { closeSync, constants, fstatSync, fsyncSync, lstatSync, mkdtempSync, openSync,
|
|
5
|
+
renameSync, rmSync, unlinkSync, writeFileSync } from "node:fs";
|
|
6
|
+
import { randomBytes, createHash } from "node:crypto";
|
|
7
|
+
import { hostname } from "node:os";
|
|
8
|
+
import { join } from "node:path";
|
|
9
|
+
import { captureContext } from "./capture-context.mjs";
|
|
10
|
+
import { holderLiveness, readCaptureLockOwner } from "./capture-lock.mjs";
|
|
11
|
+
import { captureScratchRoot, captureScratchDirectory, readCaptureScratchMarker,
|
|
12
|
+
sameCaptureScratchMarker } from "./capture-scratch.mjs";
|
|
13
|
+
|
|
14
|
+
const hash = value => createHash("sha256").update(value).digest("hex");
|
|
15
|
+
const identity = stat => ({ dev: String(stat.dev), ino: String(stat.ino) });
|
|
16
|
+
const equal = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
17
|
+
const stamp = marker => ({ identity: identity(marker.stat), hash: hash(marker.raw) });
|
|
18
|
+
const validIdentity = value => value && ["dev", "ino"].every(k => typeof value[k] === "string" && /^\d+$/.test(value[k]));
|
|
19
|
+
const validStamp = value => value && validIdentity(value.identity) && /^[a-f0-9]{64}$/.test(value.hash);
|
|
20
|
+
|
|
21
|
+
function worker(root, start) {
|
|
22
|
+
const owner = readCaptureLockOwner(root);
|
|
23
|
+
if (!process.connected || start?.root !== root || start.parent?.pid !== process.ppid ||
|
|
24
|
+
owner?.pid !== process.ppid || owner.host !== hostname() || owner.nonce !== start.parent.nonce ||
|
|
25
|
+
owner.worker?.pid !== process.pid || owner.worker.host !== hostname() || owner.worker.nonce !== start.workerNonce) {
|
|
26
|
+
throw new Error("retained capture scratch requires the registered root-lock worker");
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function context(store, stream, start) {
|
|
30
|
+
worker(store.root, start);
|
|
31
|
+
const current = captureContext(store);
|
|
32
|
+
if (!current?.state.depth || !current.held.has(stream)) throw new Error("retained capture scratch requires its stream work group");
|
|
33
|
+
return current.state;
|
|
34
|
+
}
|
|
35
|
+
function claimFor(state, key) {
|
|
36
|
+
const claim = state.get("recovery-allocation", key);
|
|
37
|
+
if (claim === null) return null;
|
|
38
|
+
if (claim.v !== 1 || typeof claim.stream !== "string" || !claim.stream ||
|
|
39
|
+
!/^recovery-[A-Za-z0-9]{6}$/.test(claim.name) || !/^[a-f0-9]{32}$/.test(claim.id) ||
|
|
40
|
+
![claim.base, claim.directory, claim.payload].every(validIdentity) || !validStamp(claim.owner) ||
|
|
41
|
+
!["active", "deleting"].includes(claim.phase)) throw new Error("invalid retained capture job reference");
|
|
42
|
+
return claim;
|
|
43
|
+
}
|
|
44
|
+
function location(root, claim) {
|
|
45
|
+
const base = captureScratchRoot(root, false), path = join(base.path, claim.name);
|
|
46
|
+
if (!equal(identity(base.stat), claim.base) || !equal(identity(captureScratchDirectory(path)), claim.directory)) {
|
|
47
|
+
throw new Error("retained capture allocation identity changed");
|
|
48
|
+
}
|
|
49
|
+
return { base, path };
|
|
50
|
+
}
|
|
51
|
+
function ownerAt(path, key, claim) {
|
|
52
|
+
const marker = readCaptureScratchMarker(path), value = marker?.value;
|
|
53
|
+
if (!marker || value.v !== 2 || value.kind !== "recovery" || value.allocation !== claim.id || value.job !== hash(key) ||
|
|
54
|
+
value.host !== hostname() || !Number.isInteger(value.pid) || value.pid <= 0 || value.pid > 2147483647 ||
|
|
55
|
+
!/^[a-f0-9]{32}$/.test(value.nonce) || !validIdentity(value.publication) ||
|
|
56
|
+
!equal(value.publication, identity(marker.stat)) ||
|
|
57
|
+
!(equal(stamp(marker), claim.owner) || validStamp(value.previous) && equal(value.previous, claim.owner))) {
|
|
58
|
+
throw new Error("retained capture owner differs from its durable reference");
|
|
59
|
+
}
|
|
60
|
+
return marker;
|
|
61
|
+
}
|
|
62
|
+
function payloadAt(path, claim, { allowMissing = false } = {}) {
|
|
63
|
+
try {
|
|
64
|
+
const stat = lstatSync(join(path, "payload"), { bigint: true });
|
|
65
|
+
if (!stat.isFile() || stat.isSymbolicLink() || !equal(identity(stat), claim.payload) ||
|
|
66
|
+
(stat.mode & 0o077n) !== 0n || (process.getuid && stat.uid !== BigInt(process.getuid()))) {
|
|
67
|
+
throw new Error("retained capture payload identity or privacy changed");
|
|
68
|
+
}
|
|
69
|
+
return stat;
|
|
70
|
+
} catch (error) { if (allowMissing && error.code === "ENOENT") return null; throw error; }
|
|
71
|
+
}
|
|
72
|
+
function assertReference(root, state, key, claim, path, marker, start) {
|
|
73
|
+
worker(root, start);
|
|
74
|
+
if (!equal(claimFor(state, key), claim) || location(root, claim).path !== path ||
|
|
75
|
+
!sameCaptureScratchMarker(marker, readCaptureScratchMarker(path))) throw new Error("retained capture reference changed");
|
|
76
|
+
}
|
|
77
|
+
function publish(path, data, check) {
|
|
78
|
+
const temporary = join(path, `.owner-${randomBytes(16).toString("hex")}.tmp`);
|
|
79
|
+
let fd, dir, made, renamed = false;
|
|
80
|
+
try {
|
|
81
|
+
dir = openSync(path, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
82
|
+
fd = openSync(temporary, "wx", 0o600); made = identity(fstatSync(fd));
|
|
83
|
+
// The self identity distinguishes this publication from copied/replaced
|
|
84
|
+
// bytes, even in the crash window before its stamp reaches the DB.
|
|
85
|
+
writeFileSync(fd, JSON.stringify({ ...data, publication: made })); fsyncSync(fd);
|
|
86
|
+
check();
|
|
87
|
+
if (!equal(identity(lstatSync(temporary)), made)) throw new Error("retained capture owner publication changed");
|
|
88
|
+
renameSync(temporary, join(path, "owner.json")); renamed = true; fsyncSync(dir);
|
|
89
|
+
const marker = readCaptureScratchMarker(path);
|
|
90
|
+
if (!marker || !equal(identity(marker.stat), made)) throw new Error("retained capture owner publication failed");
|
|
91
|
+
return marker;
|
|
92
|
+
} finally {
|
|
93
|
+
if (!renamed && made) {
|
|
94
|
+
try { if (equal(identity(lstatSync(temporary)), made)) unlinkSync(temporary); } catch { /* uncertain files remain */ }
|
|
95
|
+
}
|
|
96
|
+
if (fd !== undefined) closeSync(fd); if (dir !== undefined) closeSync(dir);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function ownerValue(key, id, start, previous) {
|
|
100
|
+
return { v: 2, kind: "recovery", allocation: id, job: hash(key), host: hostname(), pid: process.pid,
|
|
101
|
+
nonce: start.workerNonce, ...(previous ? { previous } : {}) };
|
|
102
|
+
}
|
|
103
|
+
function handle(root, state, key, claim, path, marker, start, fd) {
|
|
104
|
+
let closed = false;
|
|
105
|
+
return { path, claim, fd,
|
|
106
|
+
verify() {
|
|
107
|
+
if (closed) throw new Error("retained capture allocation is closed");
|
|
108
|
+
assertReference(root, state, key, claim, path, marker, start);
|
|
109
|
+
const named = payloadAt(path, claim, { allowMissing: claim.phase === "deleting" });
|
|
110
|
+
if (fd !== null && (!named || !equal(identity(fstatSync(fd)), claim.payload))) throw new Error("retained capture descriptor changed");
|
|
111
|
+
},
|
|
112
|
+
close() { if (!closed && fd !== null) closeSync(fd); closed = true; },
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function createRetainedCaptureScratch(store, stream, key, start) {
|
|
117
|
+
const state = context(store, stream, start);
|
|
118
|
+
if (typeof key !== "string" || !key || claimFor(state, key)) throw new Error("retained capture job already exists or is invalid");
|
|
119
|
+
const base = captureScratchRoot(store.root, true), path = mkdtempSync(join(base.path, "recovery-"));
|
|
120
|
+
const directory = identity(captureScratchDirectory(path)), id = randomBytes(16).toString("hex");
|
|
121
|
+
let fd, parent, rootFd;
|
|
122
|
+
try {
|
|
123
|
+
fd = openSync(join(path, "payload"), "wx+", 0o600); fsyncSync(fd);
|
|
124
|
+
const marker = publish(path, ownerValue(key, id, start), () => {
|
|
125
|
+
worker(store.root, start);
|
|
126
|
+
if (!equal(identity(captureScratchDirectory(path)), directory) ||
|
|
127
|
+
!equal(identity(captureScratchRoot(store.root, false).stat), identity(base.stat))) throw new Error("retained capture allocation changed before publication");
|
|
128
|
+
});
|
|
129
|
+
parent = openSync(base.path, constants.O_RDONLY | constants.O_NOFOLLOW); fsyncSync(parent);
|
|
130
|
+
rootFd = openSync(store.root, "r"); fsyncSync(rootFd);
|
|
131
|
+
const claim = { v: 1, stream, id, name: path.slice(base.path.length + 1), base: identity(base.stat),
|
|
132
|
+
directory, payload: identity(fstatSync(fd)), owner: stamp(marker), phase: "active" };
|
|
133
|
+
payloadAt(path, claim);
|
|
134
|
+
state.set("recovery-allocation", key, claim);
|
|
135
|
+
return handle(store.root, state, key, claim, path, marker, start, fd);
|
|
136
|
+
} catch (error) {
|
|
137
|
+
if (fd !== undefined) closeSync(fd);
|
|
138
|
+
// A killed/uncommitted creation has no durable reference. It is not safe
|
|
139
|
+
// to infer cleanup authority later merely from its pathname or age.
|
|
140
|
+
throw error;
|
|
141
|
+
} finally { if (parent !== undefined) closeSync(parent); if (rootFd !== undefined) closeSync(rootFd); }
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function openRetainedCaptureScratch(store, stream, key, start, { cleanup = false, liveness = holderLiveness } = {}) {
|
|
145
|
+
const state = context(store, stream, start); let claim = claimFor(state, key);
|
|
146
|
+
if (!claim || claim.stream !== stream) throw new Error("retained capture job reference is missing or belongs to another stream");
|
|
147
|
+
const { path } = location(store.root, claim); let marker = ownerAt(path, key, claim), fd = null;
|
|
148
|
+
const payload = payloadAt(path, claim, { allowMissing: cleanup || claim.phase === "deleting" });
|
|
149
|
+
if (payload) {
|
|
150
|
+
fd = openSync(join(path, "payload"), constants.O_RDWR | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
151
|
+
if (!equal(identity(fstatSync(fd)), claim.payload)) { closeSync(fd); throw new Error("retained capture payload changed during open"); }
|
|
152
|
+
}
|
|
153
|
+
try {
|
|
154
|
+
const mine = marker.value.pid === process.pid && marker.value.nonce === start.workerNonce;
|
|
155
|
+
if (!mine) {
|
|
156
|
+
if (liveness(marker.value.pid) !== "dead") throw new Error("retained capture owner is not proven dead");
|
|
157
|
+
marker = publish(path, ownerValue(key, claim.id, start, claim.owner), () => {
|
|
158
|
+
assertReference(store.root, state, key, claim, path, marker, start);
|
|
159
|
+
payloadAt(path, claim, { allowMissing: cleanup || claim.phase === "deleting" });
|
|
160
|
+
if (liveness(marker.value.pid) !== "dead") throw new Error("retained capture owner death became uncertain");
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
// Publication may precede this transaction's commit. A successor accepts
|
|
164
|
+
// the self-identified publication's previous stamp, never an arbitrary
|
|
165
|
+
// replacement marker, then publishes its own lease after death proof.
|
|
166
|
+
claim = { ...claim, owner: stamp(marker), ...(cleanup ? { phase: "deleting" } : {}) };
|
|
167
|
+
state.set("recovery-allocation", key, claim);
|
|
168
|
+
return handle(store.root, state, key, claim, path, marker, start, fd);
|
|
169
|
+
} catch (error) { if (fd !== null) closeSync(fd); throw error; }
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// Resume deletion after either side of its filesystem/DB boundary. Once the
|
|
173
|
+
// exact allocation is gone, there is no owner marker to adopt. Only an already
|
|
174
|
+
// committed deleting claim in the unchanged scratch base qualifies absence;
|
|
175
|
+
// a missing owner/payload inside an extant directory is still uncertain.
|
|
176
|
+
export function prepareRetainedCaptureCleanup(store, stream, key, start) {
|
|
177
|
+
const state = context(store, stream, start), claim = claimFor(state, key);
|
|
178
|
+
if (!claim || claim.stream !== stream) throw new Error("retained cleanup reference is missing or differs");
|
|
179
|
+
try { location(store.root, claim); }
|
|
180
|
+
catch (error) {
|
|
181
|
+
if (error.code !== "ENOENT" || claim.phase !== "deleting" ||
|
|
182
|
+
!equal(identity(captureScratchRoot(store.root, false).stat), claim.base)) throw error;
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
openRetainedCaptureScratch(store, stream, key, start, { cleanup: true }).close();
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Mark deletion through open(..., {cleanup:true}), COMMIT that group, then
|
|
189
|
+
// remove outside a DB transaction. The caller can finalize many removed job
|
|
190
|
+
// references in one later transaction. A crash at either boundary is repeatable.
|
|
191
|
+
export function removeRetainedCaptureScratch(root, state, key, start) {
|
|
192
|
+
worker(root, start);
|
|
193
|
+
if (state.depth) throw new Error("retained capture cleanup needs a committed deletion mark");
|
|
194
|
+
const claim = claimFor(state, key);
|
|
195
|
+
if (!claim || claim.phase !== "deleting") throw new Error("retained capture cleanup lacks a durable deletion mark");
|
|
196
|
+
let where;
|
|
197
|
+
try { where = location(root, claim); }
|
|
198
|
+
catch (error) {
|
|
199
|
+
if (error.code === "ENOENT") {
|
|
200
|
+
// Absence is completion only for the exact referenced allocation. A
|
|
201
|
+
// replaced scratch base must still refuse even if this child is absent.
|
|
202
|
+
if (!equal(identity(captureScratchRoot(root, false).stat), claim.base)) throw error;
|
|
203
|
+
return true;
|
|
204
|
+
}
|
|
205
|
+
throw error;
|
|
206
|
+
}
|
|
207
|
+
const { path } = where, marker = ownerAt(path, key, claim);
|
|
208
|
+
if (marker.value.pid !== process.pid || marker.value.nonce !== start.workerNonce) throw new Error("retained capture cleanup requires adoption by this worker");
|
|
209
|
+
payloadAt(path, claim, { allowMissing: true });
|
|
210
|
+
assertReference(root, state, key, claim, path, marker, start);
|
|
211
|
+
rmSync(path, { recursive: true });
|
|
212
|
+
return true;
|
|
213
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// Private retry queue for the root-lock-owning worker. A predecessor must have
|
|
2
|
+
// exited before reconciliation: a durable group start proves no member failed.
|
|
3
|
+
// There is no diagnostic execution mode; due members use normal single-stream
|
|
4
|
+
// admissions after healthy work. Public synchronous writers do not enter here.
|
|
5
|
+
import { readCaptureAdmission, readCaptureRetry } from "./capture-admission.mjs";
|
|
6
|
+
import { failureAfterAttempt, retryAfterAttempt, FAILURE_REASONS } from "./capture-failures.mjs";
|
|
7
|
+
import { fileVersion, sameVersion } from "./derived-state.mjs";
|
|
8
|
+
import { keysetRows } from "./sqlite-rows.mjs";
|
|
9
|
+
|
|
10
|
+
export function reconcileCaptureInterruption(state, { reason = "interrupted", now = Date.now() } = {}) {
|
|
11
|
+
if (state.depth) throw new Error("interruption reconciliation must precede processing");
|
|
12
|
+
if (!FAILURE_REASONS.includes(reason)) throw new Error("invalid capture interruption reason");
|
|
13
|
+
const active = readCaptureAdmission(state);
|
|
14
|
+
if (!active) return null;
|
|
15
|
+
return state.transaction(() => {
|
|
16
|
+
if (readCaptureAdmission(state)?.id !== active.id) throw new Error("capture admission changed before reconciliation");
|
|
17
|
+
const single = active.members.length === 1;
|
|
18
|
+
const failure = { id: active.id, runId: active.runId, reason: single ? reason : "interrupted",
|
|
19
|
+
memberCount: active.members.length };
|
|
20
|
+
for (const member of active.members) {
|
|
21
|
+
let nextRetryAt = retryAfterAttempt(1, now);
|
|
22
|
+
if (single) {
|
|
23
|
+
const row = failureAfterAttempt(state.get("capture-failed", member.stream), {
|
|
24
|
+
stream: member.stream, offset: member.offset, reason,
|
|
25
|
+
}, now);
|
|
26
|
+
nextRetryAt = row.nextRetryAt;
|
|
27
|
+
state.set("capture-failed", member.stream, { ...row, unitId: active.id,
|
|
28
|
+
stage: active.stage, generation: member.generation });
|
|
29
|
+
}
|
|
30
|
+
state.set("capture-retry", member.stream, { ...member, stage: active.stage,
|
|
31
|
+
unitId: active.id, attemptedRun: active.runId, nextRetryAt });
|
|
32
|
+
}
|
|
33
|
+
// This fact is consumed by the run/status writer. Unknown group membership
|
|
34
|
+
// is pending work, never a collection of invented public failed rows.
|
|
35
|
+
state.set("capture-run-failure", "last", failure);
|
|
36
|
+
state.delete("capture-admission", "active");
|
|
37
|
+
return failure;
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// candidates is the scheduler's finite, fairly ordered sweep, not a history
|
|
42
|
+
// inventory retained here. Scanning it entails metadata work; journal content
|
|
43
|
+
// is never read. The caller supplies the next sweep position with each item.
|
|
44
|
+
export function selectCaptureWork(store, state, candidates, { runId, now = Date.now(), limit = 128,
|
|
45
|
+
allowRetry = true, retryFilter = () => true, stopped = () => false } = {}) {
|
|
46
|
+
if (typeof runId !== "string" || !runId || !Number.isSafeInteger(now) || now < 0 ||
|
|
47
|
+
!Number.isSafeInteger(limit) || limit < 1 || limit > 128 || typeof allowRetry !== "boolean" ||
|
|
48
|
+
typeof retryFilter !== "function" || typeof stopped !== "function") throw new Error("invalid capture queue selection");
|
|
49
|
+
const members = [], seen = new Set(); let stage, next;
|
|
50
|
+
for (const item of candidates) {
|
|
51
|
+
if (stopped()) break;
|
|
52
|
+
if (readCaptureRetry(state, item.stream) || seen.has(item.stream)) continue;
|
|
53
|
+
if (stage !== undefined && stage !== item.stage) break;
|
|
54
|
+
stage = item.stage; next = item.next; seen.add(item.stream);
|
|
55
|
+
members.push({ stream: item.stream, offset: item.offset, generation: item.generation });
|
|
56
|
+
if (members.length === limit) break;
|
|
57
|
+
}
|
|
58
|
+
if (stopped()) return null;
|
|
59
|
+
if (members.length) return { runId, stage, next, members };
|
|
60
|
+
if (!allowRetry) return null;
|
|
61
|
+
|
|
62
|
+
// Keyset iteration retains only a bounded page. Dates are not used to erase
|
|
63
|
+
// rows, and an incomplete member cannot be admitted repeatedly in one pass.
|
|
64
|
+
const after = state.get("capture-schedule", "retry-after") ?? "";
|
|
65
|
+
if (typeof after !== "string") throw new Error("invalid capture retry position");
|
|
66
|
+
function* deferred() {
|
|
67
|
+
for (const comparison of [">", "<="]) {
|
|
68
|
+
yield* keysetRows(state.db, `SELECT key FROM state WHERE kind = 'capture-retry' AND key ${comparison} ?`, "key", [after]);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
for (const { key } of deferred()) {
|
|
72
|
+
if (stopped()) return null;
|
|
73
|
+
const retry = readCaptureRetry(state, key);
|
|
74
|
+
if (retry.attemptedRun === runId || now < Date.parse(retry.nextRetryAt) || !retryFilter(retry)) continue;
|
|
75
|
+
const generation = fileVersion(store.journalPath(retry.stream));
|
|
76
|
+
return { runId, stage: retry.stage, retry: true,
|
|
77
|
+
next: { stage: retry.stage, after: retry.stream },
|
|
78
|
+
members: [{ stream: retry.stream, generation,
|
|
79
|
+
offset: sameVersion(generation, retry.generation) ? retry.offset : 0 }] };
|
|
80
|
+
}
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Call inside the processing transaction only AFTER committing the stage's
|
|
85
|
+
// work through the failed snapshot, or validating that it no longer needs work.
|
|
86
|
+
// A checkpoint inside that snapshot cannot erase a multi-frame unit's failure.
|
|
87
|
+
// Index commits precede this capture-state transaction's commit; a crash between
|
|
88
|
+
// them conservatively leaves the retry visible for reconciliation.
|
|
89
|
+
export function resolveCaptureRetry(state, { id, stream, through, reconciled = false }) {
|
|
90
|
+
const active = readCaptureAdmission(state), retry = readCaptureRetry(state, stream);
|
|
91
|
+
if (!state.depth || active?.id !== id || !active.retry || active.members.length !== 1 ||
|
|
92
|
+
active.members[0].stream !== stream || !retry || retry.attemptedRun !== active.runId) {
|
|
93
|
+
throw new Error("capture retry resolution requires its active processing transaction");
|
|
94
|
+
}
|
|
95
|
+
if (!reconciled && (!sameVersion(active.members[0].generation, retry.generation) ||
|
|
96
|
+
!Number.isSafeInteger(through) || through < (retry.generation?.size ?? 1))) return false;
|
|
97
|
+
const failed = state.get("capture-failed", stream);
|
|
98
|
+
if (failed && failed.unitId !== retry.unitId) throw new Error("capture failure changed before resolution");
|
|
99
|
+
state.delete("capture-retry", stream);
|
|
100
|
+
if (failed) state.delete("capture-failed", stream);
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// Worker-only run accounting. The status FILE is published history; this small
|
|
2
|
+
// derived row holds counters joined to committed work while a pass is running.
|
|
3
|
+
// A stale cache row cannot replace a finished file receipt or a different run.
|
|
4
|
+
import { CAPTURE_POLICY } from "./capture-budget.mjs";
|
|
5
|
+
import { readIssueBacklog } from "./capture-issues.mjs";
|
|
6
|
+
import { CAPTURE_COUNT_FIELDS, captureReclaimedLock, emptyCaptureCounts, emptyCaptureStatus, readCaptureStatus, persistCaptureStatus } from "./capture-status.mjs";
|
|
7
|
+
|
|
8
|
+
const reasons = new Set(["deadline", "rss", "heap", "interrupted", "input", "io"]);
|
|
9
|
+
const messages = Object.freeze({ deadline: "Capture worker exceeded its admitted time allowance.",
|
|
10
|
+
rss: "Capture worker exceeded its sampled memory allowance.", heap: "Capture worker aborted at its heap limit.",
|
|
11
|
+
interrupted: "Capture execution was interrupted.",
|
|
12
|
+
input: "Capture inputs remain pending, set aside or failed.", io: "Capture could not complete an operational step." });
|
|
13
|
+
const summarize = run => Object.fromEntries(["id", "trigger", "pid", "startedAt", "finishedAt", "counts", "peakRssBytes"].map(key => [key, structuredClone(run[key])]));
|
|
14
|
+
const failureSummary = (run, reason, at) => ({ ...summarize({ ...run, finishedAt: at }), reason,
|
|
15
|
+
message: messages[reason] + " Counters report only durably confirmed unit accounting, not all physical journal writes; zero turnsAppended does not prove that no append reached disk." });
|
|
16
|
+
const maximumPeak = (a, b) => a === null && b === null ? null : Math.max(a ?? 0, b ?? 0);
|
|
17
|
+
|
|
18
|
+
export class CaptureRun {
|
|
19
|
+
constructor(root, state, { id, trigger = "manual", pid = process.pid, reclaimedLock = null,
|
|
20
|
+
previousFailure = null, currentFailure = null, clock = () => new Date().toISOString() } = {}) {
|
|
21
|
+
if (state.depth) throw new Error("capture run starts outside processing transactions");
|
|
22
|
+
if (previousFailure !== null && !reasons.has(previousFailure)) throw new Error("invalid prior capture failure");
|
|
23
|
+
if (currentFailure !== null && (currentFailure?.id !== id || !reasons.has(currentFailure.reason)))
|
|
24
|
+
throw new Error("invalid current capture failure");
|
|
25
|
+
this.root = root; this.state = state; this.clock = clock;
|
|
26
|
+
const at = clock(), backlog = readIssueBacklog(state.db, { pending: true });
|
|
27
|
+
const prior = readCaptureStatus(root) ?? emptyCaptureStatus(backlog), cached = state.get("capture-run", "current");
|
|
28
|
+
if (prior.lastRun?.id === id && prior.lastRun.outcome !== "running") throw new Error("capture run identity already finished");
|
|
29
|
+
this.history = { policy: { ...CAPTURE_POLICY }, lastSuccess: prior.lastSuccess,
|
|
30
|
+
lastFailure: prior.lastFailure, lastProgressAt: prior.lastProgressAt };
|
|
31
|
+
let run;
|
|
32
|
+
if (prior.lastRun?.id === id && prior.lastRun.outcome === "running") {
|
|
33
|
+
// Only the same unfinished, published run may reuse derived counters.
|
|
34
|
+
// Losing its cache cannot promote that run to complete: remember the
|
|
35
|
+
// interruption, then allow independent/rebuild work to progress.
|
|
36
|
+
run = cached?.run?.id === id ? cached.run : prior.lastRun;
|
|
37
|
+
run = { ...run, peakRssBytes: maximumPeak(run.peakRssBytes, prior.lastRun.peakRssBytes) };
|
|
38
|
+
this.history.lastProgressAt = cached?.run?.id === id ? cached.progressAt : prior.lastProgressAt;
|
|
39
|
+
if (!cached || cached.run?.id !== id) previousFailure ??= "interrupted";
|
|
40
|
+
if (previousFailure) this.history.lastFailure = failureSummary(run, previousFailure, at);
|
|
41
|
+
run = { ...run, pid };
|
|
42
|
+
} else {
|
|
43
|
+
if (prior.lastRun?.outcome === "running") {
|
|
44
|
+
const abandoned = cached?.run?.id === prior.lastRun.id ? cached.run : prior.lastRun;
|
|
45
|
+
abandoned.peakRssBytes = maximumPeak(abandoned.peakRssBytes, prior.lastRun.peakRssBytes);
|
|
46
|
+
this.history.lastFailure = failureSummary(abandoned, previousFailure ?? "interrupted", at);
|
|
47
|
+
if (cached?.run?.id === prior.lastRun.id) this.history.lastProgressAt = cached.progressAt;
|
|
48
|
+
}
|
|
49
|
+
run = { id, trigger, pid, startedAt: at, finishedAt: null, outcome: "running", reason: null,
|
|
50
|
+
counts: emptyCaptureCounts(), peakRssBytes: null, reclaimedLock: captureReclaimedLock(reclaimedLock) };
|
|
51
|
+
}
|
|
52
|
+
// Supervisor evidence is explicitly bound to this pass, even if its first
|
|
53
|
+
// child died before publishing a receipt. It must not be attributed to an
|
|
54
|
+
// older abandoned receipt (whose failure alone cannot poison a new pass).
|
|
55
|
+
if (currentFailure) this.history.lastFailure = failureSummary(run, currentFailure.reason, at);
|
|
56
|
+
this.id = id; this.peakRssBytes = run.peakRssBytes; this.finished = false;
|
|
57
|
+
// Publish initial identity BEFORE any processing or new counter-cache
|
|
58
|
+
// initialization. A death here leaves an honest unfinished receipt; a
|
|
59
|
+
// cache leftover from an older pass is never mistaken for this new run.
|
|
60
|
+
persistCaptureStatus(root, { schemaVersion: 1, ...this.history, lastRun: run, backlog });
|
|
61
|
+
state.transaction(() => state.set("capture-run", "current", { run, progressAt: this.history.lastProgressAt }));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
current() {
|
|
65
|
+
if (this.finished) throw new Error("capture run already finalized");
|
|
66
|
+
const row = this.state.get("capture-run", "current");
|
|
67
|
+
if (row?.run?.id !== this.id || row.run.outcome !== "running") throw new Error("capture run accounting changed");
|
|
68
|
+
return row;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Invoke in the processing commit, AFTER journal fsync / index commit. These
|
|
72
|
+
// are confirmed committed-unit counts, not reconstructed guesses about an
|
|
73
|
+
// append whose worker died before its derived checkpoint/count commit.
|
|
74
|
+
record(delta = {}, { progress = false, peakRssBytes = null } = {}) {
|
|
75
|
+
if (!this.state.depth || typeof progress !== "boolean" || !delta || typeof delta !== "object" || Array.isArray(delta) ||
|
|
76
|
+
Object.keys(delta).some(key => !CAPTURE_COUNT_FIELDS.includes(key) || !Number.isSafeInteger(delta[key]) || delta[key] < 0) ||
|
|
77
|
+
peakRssBytes !== null && (!Number.isSafeInteger(peakRssBytes) || peakRssBytes < 0)) throw new Error("invalid committed capture accounting");
|
|
78
|
+
const row = this.current();
|
|
79
|
+
for (const [key, value] of Object.entries(delta)) {
|
|
80
|
+
if (!Number.isSafeInteger(row.run.counts[key] + value)) throw new Error("capture count overflow");
|
|
81
|
+
row.run.counts[key] += value;
|
|
82
|
+
}
|
|
83
|
+
if (peakRssBytes !== null) row.run.peakRssBytes = Math.max(row.run.peakRssBytes ?? 0, peakRssBytes);
|
|
84
|
+
if (progress) row.progressAt = this.clock();
|
|
85
|
+
this.state.set("capture-run", "current", row);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
snapshot({ pending = true, peakRssBytes = null } = {}) {
|
|
89
|
+
if (this.state.depth) throw new Error("capture status cannot publish uncommitted progress");
|
|
90
|
+
const row = this.current();
|
|
91
|
+
if (peakRssBytes !== null) {
|
|
92
|
+
if (!Number.isSafeInteger(peakRssBytes) || peakRssBytes < 0) throw new Error("invalid capture RSS sample");
|
|
93
|
+
this.peakRssBytes = maximumPeak(this.peakRssBytes, peakRssBytes);
|
|
94
|
+
}
|
|
95
|
+
this.peakRssBytes = maximumPeak(this.peakRssBytes, row.run.peakRssBytes);
|
|
96
|
+
row.run.peakRssBytes = this.peakRssBytes;
|
|
97
|
+
this.history.lastProgressAt = row.progressAt;
|
|
98
|
+
return { schemaVersion: 1, ...this.history, lastRun: row.run, backlog: readIssueBacklog(this.state.db, { pending }) };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
publish(options) {
|
|
102
|
+
return persistCaptureStatus(this.root, this.snapshot(options));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
finish({ outcome, reason = null, pending = false, peakRssBytes = null } = {}) {
|
|
106
|
+
if (!["complete", "partial", "failed"].includes(outcome) || reason !== null && !reasons.has(reason) ||
|
|
107
|
+
outcome === "complete" && reason !== null || outcome === "failed" && reason === null) throw new Error("invalid capture run outcome");
|
|
108
|
+
const snapshot = this.snapshot({ pending, peakRssBytes }), at = this.clock();
|
|
109
|
+
// A failure earlier in THIS pass survives later healthy work. Older
|
|
110
|
+
// historical failures are retained but do not forbid a new full success.
|
|
111
|
+
if (snapshot.lastFailure?.id === this.id) { outcome = "failed"; reason = snapshot.lastFailure.reason; }
|
|
112
|
+
if (outcome === "complete" && (snapshot.backlog.pending || snapshot.lastRun.counts.inputsPending ||
|
|
113
|
+
snapshot.lastRun.counts.inputsSetAside || snapshot.lastRun.counts.inputsFailed)) {
|
|
114
|
+
outcome = "partial"; reason = "input";
|
|
115
|
+
}
|
|
116
|
+
snapshot.lastRun = { ...snapshot.lastRun, finishedAt: at, outcome, reason };
|
|
117
|
+
if (outcome === "complete") snapshot.lastSuccess = summarize(snapshot.lastRun);
|
|
118
|
+
if (outcome === "failed") snapshot.lastFailure = failureSummary(snapshot.lastRun, reason, at);
|
|
119
|
+
// No second DB commit is needed for completion. The atomic file is the
|
|
120
|
+
// published receipt; the next run ignores this run's stale counter cache.
|
|
121
|
+
const result = persistCaptureStatus(this.root, snapshot);
|
|
122
|
+
this.finished = true;
|
|
123
|
+
return result;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
// Private same-volume scratch for synchronous capture/receipt work.
|
|
2
|
-
// are never transferred to subprocesses.
|
|
1
|
+
// Private same-volume scratch for synchronous capture/receipt work. These
|
|
2
|
+
// per-call allocations are never transferred to subprocesses. Cleanup reclaims only proven
|
|
3
3
|
// dead local owners; an incomplete marker or reused PID remains conservative.
|
|
4
4
|
import { closeSync, constants, fstatSync, lstatSync, mkdirSync, mkdtempSync, openSync, opendirSync, readSync, rmSync, rmdirSync, statSync, writeFileSync } from "node:fs";
|
|
5
5
|
import { randomBytes } from "node:crypto";
|
|
@@ -88,8 +88,12 @@ export function cleanupCaptureScratch(root, lock, { liveness = localLiveness } =
|
|
|
88
88
|
const entries = opendirSync(base.path); let removed = 0;
|
|
89
89
|
try {
|
|
90
90
|
for (let entry; (entry = entries.readSync()) !== null;) {
|
|
91
|
-
const match = /^(snapshot|receipt)-[A-Za-z0-9]{6}$/.exec(entry.name);
|
|
91
|
+
const match = /^(snapshot|receipt|recovery)-[A-Za-z0-9]{6}$/.exec(entry.name);
|
|
92
92
|
if (!match || !entry.isDirectory() || entry.isSymbolicLink()) continue;
|
|
93
|
+
// Retained command jobs cross worker lifetimes intentionally. This
|
|
94
|
+
// ordinary scavenger has no durable job reference and therefore cannot
|
|
95
|
+
// authorize removal, even when the previous allocator is proven dead.
|
|
96
|
+
if (match[1] === "recovery") continue;
|
|
93
97
|
const path = join(base.path, entry.name); let stat;
|
|
94
98
|
try { stat = directory(path); } catch { continue; }
|
|
95
99
|
const candidate = marker(path), value = candidate?.value;
|
|
@@ -109,3 +113,8 @@ export function cleanupCaptureScratch(root, lock, { liveness = localLiveness } =
|
|
|
109
113
|
} finally { entries.closeSync(); }
|
|
110
114
|
return removed;
|
|
111
115
|
}
|
|
116
|
+
|
|
117
|
+
// Package-private filesystem checks shared with retained command allocations.
|
|
118
|
+
// They do not grant adoption or cleanup authority on their own.
|
|
119
|
+
export { privateRoot as captureScratchRoot, directory as captureScratchDirectory,
|
|
120
|
+
marker as readCaptureScratchMarker, sameMarker as sameCaptureScratchMarker };
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// Private finite-service request and pure manager definitions. Rendering grants
|
|
2
|
+
// no ownership, replacement or activation authority; setup must preflight those
|
|
3
|
+
// independently before it writes any definition or invokes a manager.
|
|
4
|
+
import { isAbsolute, join, resolve } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { captureCommandOptions } from "./capture-command.mjs";
|
|
7
|
+
import { CAPTURE_POLICY } from "./capture-budget.mjs";
|
|
8
|
+
|
|
9
|
+
export const captureServiceLabel = "ai.aweb.turn-record-capture";
|
|
10
|
+
export const captureServiceUnit = "turn-record-capture.service";
|
|
11
|
+
export const captureServiceTimer = "turn-record-capture.timer";
|
|
12
|
+
// Setup's private record, under the record root, that it started activating
|
|
13
|
+
// the service and has not verified it: a rerun tells its own unfinished step
|
|
14
|
+
// from an operator's disable, and health reports the unverified schedule.
|
|
15
|
+
export const capturePendingActivationName = "capture-service-pending.json";
|
|
16
|
+
const marker = "oats-turn-record-service:1";
|
|
17
|
+
const entry = fileURLToPath(new URL("./capture-service-runner.mjs", import.meta.url));
|
|
18
|
+
|
|
19
|
+
export function captureServiceRequest(value) {
|
|
20
|
+
if (!value || typeof value !== "object" || Array.isArray(value) ||
|
|
21
|
+
Object.keys(value).some(key => !["root", "owner", "plan"].includes(key)) ||
|
|
22
|
+
typeof value.root !== "string" || !value.root || value.root.includes("\0"))
|
|
23
|
+
throw new Error("invalid private capture service request");
|
|
24
|
+
const options = captureCommandOptions({ owner: value.owner, plan: value.plan,
|
|
25
|
+
trigger: "service", indexEnabled: true, retrySetAside: false });
|
|
26
|
+
if (options.plan.sources.length !== 4) throw new Error("capture service requires all sources");
|
|
27
|
+
return { root: resolve(options.plan.cwd, value.root), owner: options.owner, plan: options.plan };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function xml(value) {
|
|
31
|
+
// XML 1.0 forbids these characters even as numeric references. Refuse rather
|
|
32
|
+
// than persist a definition whose parsed arguments differ from the request.
|
|
33
|
+
if (/[\u0000-\u0008\u000b\u000c\u000e-\u001f\ufffe\uffff]/u.test(value) ||
|
|
34
|
+
/[\ud800-\udfff]/u.test(value)) throw new Error("unrepresentable capture service XML value");
|
|
35
|
+
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">")
|
|
36
|
+
.replace(/\r/g, " ");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function systemd(value, executable = false) {
|
|
40
|
+
if (/[\u0000-\u001f\u007f]/u.test(value) || (executable && /['"\\]/u.test(value)))
|
|
41
|
+
throw new Error("unrepresentable capture service systemd value");
|
|
42
|
+
// Specifiers apply to every word; environment substitution applies to argv,
|
|
43
|
+
// not the executable. Always quote: JSON is one argument, never shell input.
|
|
44
|
+
let escaped = value.replace(/%/g, "%%").replace(/\\/g, "\\\\").replace(/"/g, '\\"');
|
|
45
|
+
if (!executable) escaped = escaped.replace(/\$/g, "$$$$");
|
|
46
|
+
return `"${escaped}"`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// The schedule is the fixed policy interval. intervalSeconds is an internal
|
|
50
|
+
// module seam for guarded real-manager tests only: no CLI, setting or caller
|
|
51
|
+
// passes another value, and its default is the policy's 900 seconds.
|
|
52
|
+
export function captureServiceIntervalText(intervalSeconds) {
|
|
53
|
+
return intervalSeconds % 60 ? `${intervalSeconds}s` : `${intervalSeconds / 60}min`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function captureServiceDefinition({ platform, node, request, intervalSeconds = CAPTURE_POLICY.intervalSeconds } = {}) {
|
|
57
|
+
if (!["darwin", "linux"].includes(platform) || typeof node !== "string" ||
|
|
58
|
+
!isAbsolute(node) || /[\u0000-\u001f\u007f\ud800-\udfff]/u.test(node) ||
|
|
59
|
+
!Number.isSafeInteger(intervalSeconds) || intervalSeconds < 1)
|
|
60
|
+
throw new Error("invalid capture service definition selection");
|
|
61
|
+
const selected = captureServiceRequest(request);
|
|
62
|
+
const argv = [node, entry, JSON.stringify(selected)];
|
|
63
|
+
if (platform === "darwin") {
|
|
64
|
+
const log = join(selected.plan.home, "Library", "Logs", "turn-record-capture.log");
|
|
65
|
+
const contents = `<?xml version="1.0" encoding="UTF-8"?>
|
|
66
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
67
|
+
<!-- ${marker} -->
|
|
68
|
+
<plist version="1.0"><dict>
|
|
69
|
+
<key>Label</key><string>${captureServiceLabel}</string>
|
|
70
|
+
<key>ProgramArguments</key><array>${argv.map(arg => `<string>${xml(arg)}</string>`).join("")}</array>
|
|
71
|
+
<key>RunAtLoad</key><true/>
|
|
72
|
+
<key>StartInterval</key><integer>${intervalSeconds}</integer>
|
|
73
|
+
<key>StandardOutPath</key><string>${xml(log)}</string>
|
|
74
|
+
<key>StandardErrorPath</key><string>${xml(log)}</string>
|
|
75
|
+
</dict></plist>
|
|
76
|
+
`;
|
|
77
|
+
return { argv, files: [{ path: join(selected.plan.home, "Library", "LaunchAgents", `${captureServiceLabel}.plist`), contents }] };
|
|
78
|
+
}
|
|
79
|
+
const directory = join(selected.plan.home, ".config", "systemd", "user");
|
|
80
|
+
const service = `# ${marker}
|
|
81
|
+
[Unit]
|
|
82
|
+
Description=turn-record bounded capture pass
|
|
83
|
+
|
|
84
|
+
[Service]
|
|
85
|
+
Type=exec
|
|
86
|
+
ExecStart=${argv.map((arg, index) => systemd(arg, index === 0)).join(" ")}
|
|
87
|
+
MemoryMax=2G
|
|
88
|
+
RuntimeMaxSec=300
|
|
89
|
+
`;
|
|
90
|
+
const timer = `# ${marker}
|
|
91
|
+
[Unit]
|
|
92
|
+
Description=turn-record periodic capture
|
|
93
|
+
|
|
94
|
+
[Timer]
|
|
95
|
+
OnBootSec=1min
|
|
96
|
+
OnUnitActiveSec=${captureServiceIntervalText(intervalSeconds)}
|
|
97
|
+
Unit=${captureServiceUnit}
|
|
98
|
+
|
|
99
|
+
[Install]
|
|
100
|
+
WantedBy=timers.target
|
|
101
|
+
`;
|
|
102
|
+
return { argv, files: [{ path: join(directory, captureServiceUnit), contents: service },
|
|
103
|
+
{ path: join(directory, captureServiceTimer), contents: timer }] };
|
|
104
|
+
}
|