@awebai/oats 0.60.0 → 0.62.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 +15 -19
- package/bin/oats.mjs +68 -21
- package/docs/capabilities.md +1 -1
- package/docs/configuration.md +9 -0
- package/docs/desktop-cli-api.md +28 -5
- package/docs/desktop.md +7 -0
- package/docs/execution-targets.md +49 -3
- package/docs/first-team.md +8 -0
- 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-notes/v0.61.0.md +155 -0
- package/docs/release-notes/v0.62.0.md +223 -0
- package/docs/tui.md +10 -4
- package/docs/upgrading.md +133 -11
- package/docs/workspaces.md +1 -1
- package/lib/core.mjs +2 -2
- package/lib/doctor-capture.mjs +35 -0
- package/lib/session-viewer.mjs +86 -7
- package/package-catalog.json +1 -1
- package/package.json +1 -1
- package/packages/client/cli-locator.mjs +2 -2
- package/packages/client/instance-presentation.mjs +10 -0
- package/packages/client/instance-tree.mjs +48 -6
- package/packages/record/README.md +87 -13
- package/packages/record/bin/capture-hook.mjs +8 -0
- package/packages/record/bin/capture.mjs +143 -124
- package/packages/record/bin/recall.mjs +8 -3
- package/packages/record/bin/setup.mjs +115 -225
- package/packages/record/bin/turn-record.mjs +2 -2
- package/packages/record/docs/capture-service.md +1443 -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 +61 -0
- package/packages/record/lib/capture-command-worker.mjs +6 -0
- package/packages/record/lib/capture-command.mjs +48 -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 +109 -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 +102 -16
- package/packages/record/lib/capture-native-unit.mjs +322 -0
- package/packages/record/lib/capture-pass.mjs +102 -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 +117 -0
- package/packages/record/lib/capture-watch.mjs +32 -0
- package/packages/record/lib/capture-work-session.mjs +238 -0
- package/packages/record/lib/capture-worker.mjs +58 -0
- package/packages/record/lib/derived-state.mjs +28 -15
- 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 +3 -17
- package/packages/record/lib/index-reset.mjs +19 -0
- package/packages/record/lib/store.mjs +36 -11
- package/packages/record/lib/transcript-prefix.mjs +125 -0
- package/packages/tui/lib/client.mjs +2 -2
- package/packages/tui/lib/model.mjs +15 -5
- package/packages/tui/lib/views/palette.mjs +7 -3
- package/skills/oats-getting-started/SKILL.md +1 -1
- package/docs/macos-test-runs.md +0 -246
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
// Private command inventory. The caller owns the root capture lock; public
|
|
2
|
+
// synchronous capture never opens this queue. Pages and discovery checkpoints
|
|
3
|
+
// commit together. Source content/cursors remain the owning capture helpers'
|
|
4
|
+
// responsibility, and losing this derived queue only restarts enumeration.
|
|
5
|
+
import { randomUUID } from "node:crypto";
|
|
6
|
+
import { isAbsolute } from "node:path";
|
|
7
|
+
|
|
8
|
+
const sources = new Set(["aw", "cc", "pi", "codex"]);
|
|
9
|
+
const outcomes = new Set(["ready", "complete", "pending", "set-aside", "ignored", "failed"]);
|
|
10
|
+
const pageSize = 128;
|
|
11
|
+
const visitFlags = Object.freeze({ filesVisited: 1, inputsPending: 2, inputsSetAside: 4, inputsFailed: 8, inputsIgnored: 16 });
|
|
12
|
+
const validInput = input => input && sources.has(input.source) && typeof input.path === "string" &&
|
|
13
|
+
isAbsolute(input.path) && !input.path.includes("\0") && typeof input.stream === "string" && input.stream &&
|
|
14
|
+
(input.details === undefined || input.details && typeof input.details === "object" && !Array.isArray(input.details));
|
|
15
|
+
|
|
16
|
+
export class CaptureSourceQueue {
|
|
17
|
+
constructor(state, { owner, selection }) {
|
|
18
|
+
if (state.depth || typeof owner !== "string" || !owner || typeof selection !== "string" || !selection)
|
|
19
|
+
throw new Error("invalid capture source queue selection");
|
|
20
|
+
this.state = state; this.owner = owner; this.selection = selection;
|
|
21
|
+
const present = state.db.prepare("SELECT 1 AS found FROM sqlite_master WHERE type='table' AND name='capture_sources'").get()?.found;
|
|
22
|
+
if (!present) state.transaction(() => { state.db.exec(`
|
|
23
|
+
CREATE TABLE IF NOT EXISTS capture_sources (
|
|
24
|
+
scope TEXT NOT NULL, position INTEGER NOT NULL, source TEXT NOT NULL,
|
|
25
|
+
path TEXT NOT NULL, stream TEXT NOT NULL, details TEXT NOT NULL,
|
|
26
|
+
outcome TEXT NOT NULL DEFAULT 'ready', PRIMARY KEY(scope,position), UNIQUE(scope,source,path)
|
|
27
|
+
);
|
|
28
|
+
CREATE INDEX IF NOT EXISTS capture_sources_ready ON capture_sources(scope,outcome,position);
|
|
29
|
+
CREATE INDEX IF NOT EXISTS capture_sources_stream ON capture_sources(scope,stream,outcome,position);
|
|
30
|
+
`);
|
|
31
|
+
// A missing inventory table cannot inherit a completed discovery cursor.
|
|
32
|
+
// Re-enumeration is safe: journal IDs/source proofs still own dedupe.
|
|
33
|
+
state.db.exec("DELETE FROM state WHERE kind IN ('source-sweep','source-queue-garbage')");
|
|
34
|
+
});
|
|
35
|
+
// Private inventory metadata, never an outgoing-release source cursor.
|
|
36
|
+
// Additive columns also accept queues created by an earlier service build.
|
|
37
|
+
const columns = new Set(state.db.prepare("PRAGMA table_info(capture_sources)").all().map(column => column.name));
|
|
38
|
+
if (!columns.has("visited_run") || !columns.has("visited_flags")) state.transaction(() => {
|
|
39
|
+
if (!columns.has("visited_run")) state.db.exec("ALTER TABLE capture_sources ADD COLUMN visited_run TEXT");
|
|
40
|
+
if (!columns.has("visited_flags")) state.db.exec("ALTER TABLE capture_sources ADD COLUMN visited_flags INTEGER NOT NULL DEFAULT 0");
|
|
41
|
+
});
|
|
42
|
+
const prior = state.get("source-sweep", owner);
|
|
43
|
+
if (prior && (!prior.id || prior.owner !== owner || typeof prior.selection !== "string" ||
|
|
44
|
+
!Number.isSafeInteger(prior.next) || prior.next < 1 || typeof prior.discovered !== "boolean" ||
|
|
45
|
+
typeof prior.finished !== "boolean")) throw new Error("invalid capture source sweep");
|
|
46
|
+
if (prior && prior.selection === selection && !prior.finished) this.sweep = prior;
|
|
47
|
+
else {
|
|
48
|
+
this.sweep = { id: randomUUID(), owner, selection, next: 1, discovered: false, finished: false, discovery: null };
|
|
49
|
+
state.transaction(() => {
|
|
50
|
+
if (prior) state.set("source-queue-garbage", prior.id, true);
|
|
51
|
+
state.set("source-sweep", owner, this.sweep);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
current() {
|
|
57
|
+
const row = this.state.get("source-sweep", this.owner);
|
|
58
|
+
if (row?.id !== this.sweep.id) throw new Error("capture source sweep changed");
|
|
59
|
+
return row;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// The walker may conservatively replay a basename after a removed anchor.
|
|
63
|
+
// Duplicate discovery never renumbers an input or erases completed work.
|
|
64
|
+
add(inputs, checkpoint, { complete = false } = {}) {
|
|
65
|
+
if (!Array.isArray(inputs) || inputs.length > pageSize || inputs.some(input => !validInput(input)) || typeof complete !== "boolean")
|
|
66
|
+
throw new Error("invalid capture discovery page");
|
|
67
|
+
this.state.transaction(() => {
|
|
68
|
+
const row = this.current();
|
|
69
|
+
if (row.discovered || row.finished) throw new Error("capture source discovery already completed");
|
|
70
|
+
const insert = this.state.db.prepare(`INSERT INTO capture_sources(scope,position,source,path,stream,details)
|
|
71
|
+
VALUES (?,?,?,?,?,?) ON CONFLICT(scope,source,path) DO NOTHING`);
|
|
72
|
+
for (const input of inputs) {
|
|
73
|
+
const existing = this.state.db.prepare("SELECT stream,details FROM capture_sources WHERE scope=? AND source=? AND path=?")
|
|
74
|
+
.get(row.id, input.source, input.path);
|
|
75
|
+
const details = JSON.stringify(input.details ?? {});
|
|
76
|
+
// Early node:sqlite can return an object of nulls for no match.
|
|
77
|
+
// stream is NOT NULL for every real inventory row.
|
|
78
|
+
if (existing?.stream != null && (existing.stream !== input.stream || existing.details !== details)) throw new Error("capture input changed during discovery");
|
|
79
|
+
if (existing?.stream == null) {
|
|
80
|
+
if (!Number.isSafeInteger(row.next + 1)) throw new Error("capture source inventory position overflow");
|
|
81
|
+
insert.run(row.id, row.next++, input.source, input.path, input.stream, details);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
row.discovery = checkpoint; row.discovered = complete;
|
|
85
|
+
this.state.set("source-sweep", this.owner, row);
|
|
86
|
+
this.sweep = row;
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Admission writes this cursor BEFORE processing; a rolled-back append must
|
|
91
|
+
// not force every new worker back to the first growing input. Each stage has
|
|
92
|
+
// its own position, so index work cannot erase source-stage fairness.
|
|
93
|
+
cursor(position) { return `${this.sweep.id}:${position}`; }
|
|
94
|
+
position(cursor) {
|
|
95
|
+
if (typeof cursor !== "string" || !cursor.startsWith(`${this.sweep.id}:`)) return 0;
|
|
96
|
+
const n = Number(cursor.slice(this.sweep.id.length + 1));
|
|
97
|
+
if (!Number.isSafeInteger(n) || n < 0) throw new Error("invalid capture source queue cursor");
|
|
98
|
+
return n;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
*ready({ stopped = () => false, work } = {}) {
|
|
102
|
+
const row = this.current();
|
|
103
|
+
const after = this.position(this.state.get("capture-schedule", "position:capture")?.after);
|
|
104
|
+
// Native collision precedence is unchanged: an earlier source sharing its
|
|
105
|
+
// stream must finish its fixed snapshot (or reach pending/refusal) before
|
|
106
|
+
// a later one can run. AW alone may interleave across its shared stream.
|
|
107
|
+
for (const later of [true, false]) {
|
|
108
|
+
let last = later ? after : 0;
|
|
109
|
+
while (!stopped()) {
|
|
110
|
+
const page = this.state.db.prepare(`SELECT q.position,q.source,q.path,q.stream,q.details,q.outcome
|
|
111
|
+
FROM capture_sources q WHERE q.scope=? AND q.outcome='ready' AND q.position>?
|
|
112
|
+
${later ? "" : "AND q.position<=?"}
|
|
113
|
+
AND (q.source='aw' OR NOT EXISTS (
|
|
114
|
+
SELECT 1 FROM capture_sources prior WHERE prior.scope=q.scope AND prior.stream=q.stream
|
|
115
|
+
AND prior.outcome='ready' AND prior.position<q.position))
|
|
116
|
+
ORDER BY q.position LIMIT ${pageSize}`).all(...(later ? [row.id, last] : [row.id, last, after]));
|
|
117
|
+
if (work) work.inventoryRows = (work.inventoryRows ?? 0) + page.length;
|
|
118
|
+
if (!page.length) break;
|
|
119
|
+
for (const input of page) {
|
|
120
|
+
if (stopped()) return;
|
|
121
|
+
last = input.position;
|
|
122
|
+
yield { ...input, details: JSON.parse(input.details), next: { stage: "capture", after: this.cursor(input.position) } };
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Use inside the SAME processing transaction as the input's committed
|
|
129
|
+
// source checkpoint. This is scheduling state, never append/proof authority.
|
|
130
|
+
mark(position, outcome) {
|
|
131
|
+
if (!this.state.depth || !Number.isSafeInteger(position) || position < 1 || !outcomes.has(outcome))
|
|
132
|
+
throw new Error("capture source result requires its processing transaction");
|
|
133
|
+
this.current();
|
|
134
|
+
const changed = this.state.db.prepare("UPDATE capture_sources SET outcome=? WHERE scope=? AND position=? AND outcome!=?")
|
|
135
|
+
.run(outcome, this.sweep.id, position, outcome);
|
|
136
|
+
if (!changed.changes && !this.state.db.prepare("SELECT 1 AS found FROM capture_sources WHERE scope=? AND position=?").get(this.sweep.id, position)?.found)
|
|
137
|
+
throw new Error("capture source queue input is missing");
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Count each input/category at most once in a run even when its recovery or
|
|
141
|
+
// append spans many workers. These facts commit with the source checkpoint.
|
|
142
|
+
visit(position, runId, categories = {}) {
|
|
143
|
+
if (!this.state.depth || typeof runId !== "string" || !runId ||
|
|
144
|
+
!Number.isSafeInteger(position) || position < 1 ||
|
|
145
|
+
Object.keys(categories).some(key => !Object.hasOwn(visitFlags, key) || typeof categories[key] !== "boolean"))
|
|
146
|
+
throw new Error("capture input visits require valid committed accounting");
|
|
147
|
+
this.current();
|
|
148
|
+
const row = this.state.db.prepare("SELECT visited_run,visited_flags FROM capture_sources WHERE scope=? AND position=?")
|
|
149
|
+
.get(this.sweep.id, position);
|
|
150
|
+
if (!row || !Number.isSafeInteger(row.visited_flags) || row.visited_flags < 0 || row.visited_flags > 31)
|
|
151
|
+
throw new Error("capture input visit state is missing or invalid");
|
|
152
|
+
const before = row.visited_run === runId ? row.visited_flags : 0;
|
|
153
|
+
let flags = before; const delta = {};
|
|
154
|
+
for (const [name, flag] of Object.entries(visitFlags)) if (name === "filesVisited" || categories[name]) {
|
|
155
|
+
if (!(flags & flag)) { flags |= flag; delta[name] = 1; }
|
|
156
|
+
}
|
|
157
|
+
if (row.visited_run !== runId || flags !== before) this.state.db
|
|
158
|
+
.prepare("UPDATE capture_sources SET visited_run=?,visited_flags=? WHERE scope=? AND position=?")
|
|
159
|
+
.run(runId, flags, this.sweep.id, position);
|
|
160
|
+
return delta;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Used only for a due single-stream retry. Native precedence remains the
|
|
164
|
+
// earliest unfinished source; AW resumes after the durable account position.
|
|
165
|
+
forStream(stream) {
|
|
166
|
+
const after = this.position(this.state.get("capture-schedule", "position:capture")?.after);
|
|
167
|
+
const row = this.state.db.prepare(`SELECT position,source,path,stream,details,outcome FROM capture_sources
|
|
168
|
+
WHERE scope=? AND stream=? AND outcome='ready'
|
|
169
|
+
ORDER BY CASE WHEN source='aw' AND position<=? THEN 1 ELSE 0 END,position LIMIT 1`)
|
|
170
|
+
.get(this.sweep.id, stream, after);
|
|
171
|
+
return row?.position != null ? { ...row, details: JSON.parse(row.details), next: { stage: "capture", after: this.cursor(row.position) } } : null;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
finish() {
|
|
175
|
+
if (!this.state.depth) throw new Error("capture source sweep finish requires a transaction");
|
|
176
|
+
const row = this.current();
|
|
177
|
+
if (!row.discovered || this.state.db.prepare("SELECT 1 AS found FROM capture_sources WHERE scope=? AND outcome='ready' LIMIT 1").get(row.id)?.found)
|
|
178
|
+
return false;
|
|
179
|
+
row.finished = true; this.state.set("source-sweep", this.owner, row); this.sweep = row;
|
|
180
|
+
// Finished enumeration/visits do NOT imply a successful pass: pending,
|
|
181
|
+
// set-aside and failed outcomes still need the worker's truthful receipt.
|
|
182
|
+
return true;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// No table-wide reset or scan of every current row. Replacing a sweep
|
|
186
|
+
// atomically publishes its exact obsolete scope; cleanup seeks that scope
|
|
187
|
+
// and removes at most one page. Other owners' active scopes are untouched.
|
|
188
|
+
cleanup() {
|
|
189
|
+
if (!this.state.depth) throw new Error("capture source cleanup requires a transaction");
|
|
190
|
+
const obsolete = this.state.db.prepare("SELECT key FROM state WHERE kind='source-queue-garbage' ORDER BY key LIMIT 1").get()?.key;
|
|
191
|
+
if (obsolete == null) return 0;
|
|
192
|
+
if (obsolete === this.current().id) throw new Error("active capture source inventory cannot be deleted");
|
|
193
|
+
const count = Number(this.state.db.prepare(`DELETE FROM capture_sources WHERE rowid IN (
|
|
194
|
+
SELECT rowid FROM capture_sources WHERE scope=? ORDER BY position LIMIT ${pageSize})`).run(obsolete).changes);
|
|
195
|
+
if (!this.state.db.prepare("SELECT 1 AS found FROM capture_sources WHERE scope=? LIMIT 1").get(obsolete)?.found)
|
|
196
|
+
this.state.delete("source-queue-garbage", obsolete);
|
|
197
|
+
return count;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// One admitted source group in the command worker. This joins queue progress
|
|
2
|
+
// and run counters to durable capture checkpoints; discovery/index completion
|
|
3
|
+
// and final receipts remain the owning scheduler's responsibility.
|
|
4
|
+
import { captureAwUnit } from "./capture-aw-unit.mjs";
|
|
5
|
+
import { captureNativeUnit } from "./capture-native-unit.mjs";
|
|
6
|
+
import { withCaptureGroup, captureContext } from "./capture-context.mjs";
|
|
7
|
+
import { selectCaptureWork, resolveCaptureRetry } from "./capture-retry.mjs";
|
|
8
|
+
import { readCaptureRetry } from "./capture-admission.mjs";
|
|
9
|
+
import { fileVersion } from "./derived-state.mjs";
|
|
10
|
+
import { loadIgnore } from "./ignore.mjs";
|
|
11
|
+
|
|
12
|
+
export function captureSourceWork(store, state, queue, run, start, {
|
|
13
|
+
limit = 128, maxRecords = 2000, maxBytes = 8388608, ignore = loadIgnore(store.root),
|
|
14
|
+
now = Date.now(), work, indexInventory = null, allowRetry = true, retryFilter, inputReady = () => true,
|
|
15
|
+
} = {}) {
|
|
16
|
+
if (!Number.isSafeInteger(limit) || limit < 1 || limit > 128 ||
|
|
17
|
+
!Number.isSafeInteger(maxRecords) || maxRecords < limit || maxRecords > 2000 ||
|
|
18
|
+
!Number.isSafeInteger(maxBytes) || maxBytes < limit || maxBytes > 8388608 || typeof inputReady !== "function")
|
|
19
|
+
throw new Error("invalid capture source work allowance");
|
|
20
|
+
if (indexInventory !== null && (indexInventory.state !== state || typeof indexInventory.include !== "function"))
|
|
21
|
+
throw new Error("capture source index inventory must belong to the current state");
|
|
22
|
+
const budget = start.budget, stopped = () => budget.sample() !== null;
|
|
23
|
+
if (stopped()) return { stopped: true, selected: 0, cleanup: [] };
|
|
24
|
+
const inputs = new Map();
|
|
25
|
+
function* candidates() {
|
|
26
|
+
for (const input of queue.ready({ stopped, work })) {
|
|
27
|
+
if (readCaptureRetry(state, input.stream) || inputs.has(input.stream) || !inputReady(input)) continue;
|
|
28
|
+
// At most one file from a shared stream belongs to an admission. The
|
|
29
|
+
// durable queue position rotates AW accounts between such admissions.
|
|
30
|
+
inputs.set(input.stream, input);
|
|
31
|
+
const generation = fileVersion(store.journalPath(input.stream));
|
|
32
|
+
yield { stream: input.stream, stage: "capture", offset: generation?.size ?? 0, generation, next: input.next };
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const selection = selectCaptureWork(store, state, candidates(), { runId: run.id, now, limit, allowRetry, retryFilter, stopped });
|
|
36
|
+
if (!selection) return { selected: 0, cleanup: [] };
|
|
37
|
+
// Retry selection spans stages. Return other-stage work to the scheduler;
|
|
38
|
+
// never execute it as capture or clear its failure through the wrong path.
|
|
39
|
+
if (selection.stage !== "capture") return { selected: 0, deferred: selection, cleanup: [] };
|
|
40
|
+
if (selection.retry) {
|
|
41
|
+
const input = queue.forStream(selection.members[0].stream);
|
|
42
|
+
if (!input || !inputReady(input)) return { selected: 0, deferred: selection, cleanup: [] };
|
|
43
|
+
inputs.set(input.stream, input);
|
|
44
|
+
selection.next = input.next;
|
|
45
|
+
}
|
|
46
|
+
const recordsEach = Math.floor(maxRecords / selection.members.length);
|
|
47
|
+
const bytesEach = Math.floor(maxBytes / selection.members.length);
|
|
48
|
+
return withCaptureGroup(store, selection.members.map(member => member.stream), () => {
|
|
49
|
+
const results = [], cleanup = [];
|
|
50
|
+
for (const member of selection.members) {
|
|
51
|
+
if (stopped()) break;
|
|
52
|
+
const input = inputs.get(member.stream), before = state.db.prepare("SELECT total_changes() AS n").get().n;
|
|
53
|
+
const options = { owner: queue.owner, path: input.path, ignore, maxRecords: recordsEach,
|
|
54
|
+
maxBytes: bytesEach, maxRecordBytes: budget.policy.maxRecordBytes, stopped, work: work?.content };
|
|
55
|
+
const result = input.source === "aw"
|
|
56
|
+
? captureAwUnit(store, { ...options, kind: input.details.kind,
|
|
57
|
+
selfName: input.details.selfName, workspace: input.details.workspace })
|
|
58
|
+
: captureNativeUnit(store, { ...options, format: input.source, capturedPi: input.details.capturedPi, start });
|
|
59
|
+
// Inspect mutation before visits/counters: repeatedly visiting an
|
|
60
|
+
// unchanged or deferred input is not processing progress.
|
|
61
|
+
const progressed = state.db.prepare("SELECT total_changes() AS n").get().n !== before;
|
|
62
|
+
const outcome = result.ignored ? "ignored" : result.setAside ? "set-aside" :
|
|
63
|
+
result.pending ? "pending" : result.complete ? (result.failed ? "failed" : "complete") : "ready";
|
|
64
|
+
queue.mark(input.position, outcome);
|
|
65
|
+
const delta = queue.visit(input.position, run.id, { inputsIgnored: !!result.ignored,
|
|
66
|
+
inputsSetAside: !!result.setAside, inputsPending: !!result.pending, inputsFailed: !!result.failed });
|
|
67
|
+
run.record({ ...delta, bytesCommitted: result.bytesCommitted, turnsAppended: result.appended },
|
|
68
|
+
{ progress: progressed, peakRssBytes: budget.peakRssBytes });
|
|
69
|
+
if (selection.retry && result.complete) {
|
|
70
|
+
// Completing the fixed input validates that this source admission no
|
|
71
|
+
// longer needs work. A partial prefix proof cannot clear the failure.
|
|
72
|
+
resolveCaptureRetry(state, { id: captureContext(store).admission.id, stream: member.stream, reconciled: true });
|
|
73
|
+
}
|
|
74
|
+
if (result.cleanup) cleanup.push({ stream: member.stream, key: result.key });
|
|
75
|
+
results.push({ position: input.position, ...result });
|
|
76
|
+
}
|
|
77
|
+
// Include every committed input, including replay with no new append.
|
|
78
|
+
// Journal bytes may survive an earlier rolled-back source transaction.
|
|
79
|
+
if (indexInventory) indexInventory.include(results.map(result => result.stream));
|
|
80
|
+
// An input that took the unchanged path touched neither its journal nor its
|
|
81
|
+
// checkpoint; every other one may have.
|
|
82
|
+
if (indexInventory) indexInventory.changed(results.filter(result => !result.unchanged).map(result => result.stream));
|
|
83
|
+
return { selected: selection.members.length, results, cleanup };
|
|
84
|
+
}, { state, admission: selection, admissionNow: now, batchBytes: maxBytes, check: () => budget.check() });
|
|
85
|
+
}
|
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
// Shared capture status and operator recovery.
|
|
2
|
-
//
|
|
1
|
+
// Shared capture status and operator recovery. Null policy/run fields retain
|
|
2
|
+
// their algorithm-phase meaning: no such evidence has been supplied.
|
|
3
3
|
import { randomUUID } from "node:crypto";
|
|
4
|
-
import { closeSync, constants, fstatSync, fsyncSync, openSync, readSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
|
|
5
|
-
import { join } from "node:path";
|
|
6
4
|
import { DerivedState, invalidateLegacyMaps } from "./derived-state.mjs";
|
|
7
|
-
import {
|
|
5
|
+
import { readIssueBacklog } from "./capture-issues.mjs";
|
|
6
|
+
import { emptyCaptureStatus, readCaptureStatus, persistCaptureStatus } from "./capture-status.mjs";
|
|
8
7
|
|
|
9
8
|
export const CAPTURE_SOURCES = ["aw", "cc", "pi", "codex"];
|
|
10
|
-
export
|
|
9
|
+
export { captureHealth, SET_ASIDE_REMEDY } from "./capture-health.mjs";
|
|
11
10
|
|
|
12
11
|
// Caller owns the root capture lock. One transaction persists every selected
|
|
13
12
|
// generation; a missing source cursor cannot erase this rescan request.
|
|
@@ -26,7 +25,13 @@ export function retrySetAside(root) {
|
|
|
26
25
|
const state = new DerivedState(root);
|
|
27
26
|
try {
|
|
28
27
|
state.transaction(() => {
|
|
29
|
-
for (const { key, value } of state.entries("set-aside"))
|
|
28
|
+
for (const { key, value } of state.entries("set-aside")) {
|
|
29
|
+
// A command's explicit retry invalidates an older staged proof once.
|
|
30
|
+
// Keep this private generation after the issue resolves, so subsequent
|
|
31
|
+
// workers do not restart merely because the active row was removed.
|
|
32
|
+
state.set("source-retry", key, randomUUID());
|
|
33
|
+
state.set("set-aside", key, { ...value, retry: true });
|
|
34
|
+
}
|
|
30
35
|
});
|
|
31
36
|
} finally { state.close(); }
|
|
32
37
|
}
|
|
@@ -37,69 +42,9 @@ export function writeCaptureStatus(root, { pending = false } = {}) {
|
|
|
37
42
|
try {
|
|
38
43
|
backlog = readIssueBacklog(state.db, { pending });
|
|
39
44
|
} finally { state.close(); }
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
fd = openSync(temp, "wx", 0o600);
|
|
45
|
-
writeFileSync(fd, JSON.stringify(status) + "\n"); fsyncSync(fd); closeSync(fd); fd = undefined;
|
|
46
|
-
renameSync(temp, path);
|
|
47
|
-
} finally {
|
|
48
|
-
if (fd !== undefined) closeSync(fd);
|
|
49
|
-
try { unlinkSync(temp); } catch (err) { if (err.code !== "ENOENT") throw err; }
|
|
50
|
-
}
|
|
51
|
-
return status;
|
|
45
|
+
// Exact receipt callers update issue evidence, never erase prior worker run
|
|
46
|
+
// facts or turn their unbounded operation into a bounded-service success.
|
|
47
|
+
const prior = readCaptureStatus(root) ?? emptyCaptureStatus(backlog);
|
|
48
|
+
return persistCaptureStatus(root, { ...prior, backlog });
|
|
52
49
|
}
|
|
53
50
|
|
|
54
|
-
function validStatus(status) {
|
|
55
|
-
const b = status?.backlog;
|
|
56
|
-
return status?.schemaVersion === 1 && status.policy === null && status.lastRun === null && status.lastSuccess === null &&
|
|
57
|
-
status.lastFailure === null && status.lastProgressAt === null && typeof b?.pending === "boolean" &&
|
|
58
|
-
Number.isSafeInteger(b.setAsideCount) && b.setAsideCount >= 0 && Array.isArray(b.setAside) && b.setAside.length <= 100 &&
|
|
59
|
-
b.setAside.length === Math.min(b.setAsideCount, 100) && b.truncated === (b.setAsideCount > 100) &&
|
|
60
|
-
b.setAside.every(e => CAPTURE_SOURCES.includes(e.source) && typeof e.path === "string" && typeof e.reason === "string" &&
|
|
61
|
-
Number.isSafeInteger(e.offset) && e.offset >= 0 && (e.line === undefined || Number.isSafeInteger(e.line) && e.line > 0));
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// Read-only, including a never-installed/absent root: no writable DB construction,
|
|
65
|
-
// directory creation, manager query, index rebuild or capture.
|
|
66
|
-
export function captureHealth(root) {
|
|
67
|
-
const path = join(root, "capture-status.json");
|
|
68
|
-
let status = { lastRun: null, lastSuccess: null, lastFailure: null,
|
|
69
|
-
backlog: { pending: false, setAsideCount: 0, setAside: [], truncated: false } };
|
|
70
|
-
const rows = [];
|
|
71
|
-
try {
|
|
72
|
-
const fd = openSync(path, constants.O_RDONLY | constants.O_NONBLOCK);
|
|
73
|
-
let text;
|
|
74
|
-
try {
|
|
75
|
-
const stat = fstatSync(fd);
|
|
76
|
-
if (!stat.isFile()) throw new Error("status is not a regular file");
|
|
77
|
-
const limit = 1024 * 1024;
|
|
78
|
-
if (stat.size > limit) throw new Error("status exceeds 1 MiB");
|
|
79
|
-
const bytes = Buffer.alloc(limit + 1);
|
|
80
|
-
let size = 0;
|
|
81
|
-
while (size < bytes.length) {
|
|
82
|
-
const count = readSync(fd, bytes, size, bytes.length - size, size);
|
|
83
|
-
if (!count) break;
|
|
84
|
-
size += count;
|
|
85
|
-
}
|
|
86
|
-
if (size > limit) throw new Error("status exceeds 1 MiB");
|
|
87
|
-
text = bytes.toString("utf8", 0, size);
|
|
88
|
-
} finally { closeSync(fd); }
|
|
89
|
-
const parsed = JSON.parse(text);
|
|
90
|
-
if (!validStatus(parsed)) throw new Error("unsupported or malformed status");
|
|
91
|
-
status = parsed;
|
|
92
|
-
} catch (err) {
|
|
93
|
-
if (err.code !== "ENOENT") rows.push({ code: "capture-status-invalid", message: `Cannot read capture status at ${path}: ${err.message}` });
|
|
94
|
-
}
|
|
95
|
-
let backlog;
|
|
96
|
-
try {
|
|
97
|
-
backlog = readCurrentIssueBacklog(root);
|
|
98
|
-
if (!backlog && status.backlog.setAsideCount) throw new Error("capture.db is missing but the last command reported active set-aside inputs");
|
|
99
|
-
} catch (err) { throw new Error(`Cannot read current capture issues at ${join(root, "index", "capture.db")}: ${err.message}`, { cause: err }); }
|
|
100
|
-
backlog ??= { pending: false, setAsideCount: 0, setAside: [], truncated: false };
|
|
101
|
-
if (backlog.setAsideCount) rows.push({ code: "capture-input-set-aside", message:
|
|
102
|
-
`${backlog.setAsideCount} capture inputs set aside. ${SET_ASIDE_REMEDY.replace("<root>", root)}` });
|
|
103
|
-
return { schemaVersion: 1, status: rows.length ? "warning" : "ok", rows,
|
|
104
|
-
lastRun: status.lastRun, lastSuccess: status.lastSuccess, lastFailure: status.lastFailure, backlog };
|
|
105
|
-
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// Shared status format and atomic file I/O. This module neither creates a run
|
|
2
|
+
// nor infers success from counters. Only admitted command workers publish run
|
|
3
|
+
// facts; exact receipt commands may refresh backlog while retaining history.
|
|
4
|
+
import { randomUUID } from "node:crypto";
|
|
5
|
+
import { closeSync, constants, fstatSync, fsyncSync, openSync, readdirSync, readSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import { CAPTURE_POLICY } from "./capture-budget.mjs";
|
|
8
|
+
import { validFailureBacklog } from "./capture-failures.mjs";
|
|
9
|
+
|
|
10
|
+
export const CAPTURE_COUNT_FIELDS = Object.freeze(["filesVisited", "bytesCommitted", "turnsAppended", "turnsIndexed",
|
|
11
|
+
"inputsPending", "inputsSetAside", "inputsFailed", "inputsIgnored"]);
|
|
12
|
+
export const emptyCaptureCounts = () => Object.fromEntries(CAPTURE_COUNT_FIELDS.map(key => [key, 0]));
|
|
13
|
+
const triggers = new Set(["service", "hook", "setup", "manual", "watch"]);
|
|
14
|
+
const reasons = new Set(["deadline", "rss", "heap", "interrupted", "input", "io"]);
|
|
15
|
+
const object = value => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
16
|
+
const integer = value => Number.isSafeInteger(value) && value >= 0;
|
|
17
|
+
const exact = (value, required, optional = []) => object(value) && required.every(key => Object.hasOwn(value, key)) &&
|
|
18
|
+
Object.keys(value).every(key => required.includes(key) || optional.includes(key));
|
|
19
|
+
const stamp = (value, now) => typeof value === "string" && Number.isFinite(Date.parse(value)) &&
|
|
20
|
+
new Date(value).toISOString() === value && Date.parse(value) <= now;
|
|
21
|
+
const peak = value => value === null || integer(value);
|
|
22
|
+
const counts = value => exact(value, CAPTURE_COUNT_FIELDS) && CAPTURE_COUNT_FIELDS.every(key => integer(value[key]));
|
|
23
|
+
const reclaimed = (value, now) => exact(value, ["pid", "startedAt"]) && Number.isSafeInteger(value.pid) && value.pid > 0 &&
|
|
24
|
+
stamp(value.startedAt, now);
|
|
25
|
+
const identityKeys = ["id", "trigger", "pid", "startedAt"];
|
|
26
|
+
const summaryKeys = [...identityKeys, "finishedAt", "counts", "peakRssBytes"];
|
|
27
|
+
function identity(value, now) {
|
|
28
|
+
return object(value) && typeof value.id === "string" && value.id.length > 0 && triggers.has(value.trigger) &&
|
|
29
|
+
Number.isSafeInteger(value.pid) && value.pid > 0 && stamp(value.startedAt, now);
|
|
30
|
+
}
|
|
31
|
+
function summary(value, now, failure = false) {
|
|
32
|
+
return exact(value, [...summaryKeys, ...(failure ? ["reason", "message"] : [])], failure ? ["path", "offset"] : []) &&
|
|
33
|
+
identity(value, now) && stamp(value.finishedAt, now) && value.startedAt <= value.finishedAt && counts(value.counts) && peak(value.peakRssBytes) &&
|
|
34
|
+
(!failure || reasons.has(value.reason) && typeof value.message === "string" && value.message.length > 0 && value.message.length <= 2048 &&
|
|
35
|
+
(value.path === undefined || typeof value.path === "string") && (value.offset === undefined || integer(value.offset)));
|
|
36
|
+
}
|
|
37
|
+
function run(value, now) {
|
|
38
|
+
if (!exact(value, [...summaryKeys, "outcome", "reason", "reclaimedLock"]) || !identity(value, now) ||
|
|
39
|
+
!counts(value.counts) || !peak(value.peakRssBytes) ||
|
|
40
|
+
!["running", "complete", "partial", "failed"].includes(value.outcome) || value.reason !== null && !reasons.has(value.reason)) return false;
|
|
41
|
+
if (value.reclaimedLock !== null && !reclaimed(value.reclaimedLock, now)) return false;
|
|
42
|
+
if (value.outcome === "running") return value.finishedAt === null && value.reason === null;
|
|
43
|
+
return stamp(value.finishedAt, now) && value.startedAt <= value.finishedAt &&
|
|
44
|
+
(value.outcome !== "complete" || value.reason === null) && (value.outcome !== "failed" || value.reason !== null);
|
|
45
|
+
}
|
|
46
|
+
function backlog(value) {
|
|
47
|
+
return exact(value, ["pending", "setAsideCount", "setAside", "truncated"], ["failedCount", "failed", "failedTruncated"]) &&
|
|
48
|
+
typeof value.pending === "boolean" && integer(value.setAsideCount) && Array.isArray(value.setAside) &&
|
|
49
|
+
value.setAside.length === Math.min(value.setAsideCount, 100) && value.truncated === (value.setAsideCount > 100) &&
|
|
50
|
+
validFailureBacklog(value, { legacy: true }) &&
|
|
51
|
+
(value.failed === undefined || value.failed.every(row => exact(row, ["stream", "offset", "reason", "attempts", "nextRetryAt"]))) &&
|
|
52
|
+
value.setAside.every(row =>
|
|
53
|
+
exact(row, ["source", "path", "offset", "reason"], ["line"]) && ["aw", "cc", "pi", "codex"].includes(row.source) &&
|
|
54
|
+
typeof row.path === "string" && typeof row.reason === "string" && integer(row.offset) &&
|
|
55
|
+
(row.line === undefined || Number.isSafeInteger(row.line) && row.line > 0));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function validCaptureStatus(status, { now = Date.now() } = {}) {
|
|
59
|
+
if (!exact(status, ["schemaVersion", "policy", "lastRun", "lastSuccess", "lastFailure", "lastProgressAt", "backlog"]) ||
|
|
60
|
+
status.schemaVersion !== 1 || !backlog(status.backlog)) return false;
|
|
61
|
+
if (status.policy !== null && !(exact(status.policy, Object.keys(CAPTURE_POLICY)) &&
|
|
62
|
+
Object.keys(CAPTURE_POLICY).every(key => status.policy[key] === CAPTURE_POLICY[key]))) return false;
|
|
63
|
+
if (status.lastRun !== null && !run(status.lastRun, now) || status.lastSuccess !== null && !summary(status.lastSuccess, now) ||
|
|
64
|
+
status.lastFailure !== null && !summary(status.lastFailure, now, true) ||
|
|
65
|
+
status.lastProgressAt !== null && !stamp(status.lastProgressAt, now)) return false;
|
|
66
|
+
// Algorithm-phase nulls are valid absence of evidence; service facts cannot
|
|
67
|
+
// be published as an unqualified null-policy algorithm snapshot.
|
|
68
|
+
return status.policy !== null || [status.lastRun, status.lastSuccess, status.lastFailure, status.lastProgressAt].every(value => value === null);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Reclaim evidence is optional. A dead owner record with a missing,
|
|
72
|
+
// nonstandard or future start (clock step, skewed shared home) is published
|
|
73
|
+
// as null rather than refusing this run's receipt.
|
|
74
|
+
export function captureReclaimedLock(value, { now = Date.now() } = {}) {
|
|
75
|
+
const evidence = value ? { pid: value.pid, startedAt: value.startedAt } : null;
|
|
76
|
+
return evidence && reclaimed(evidence, now) ? evidence : null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function emptyCaptureStatus(backlog) {
|
|
80
|
+
return { schemaVersion: 1, policy: null, lastRun: null, lastSuccess: null, lastFailure: null, lastProgressAt: null, backlog };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// The health transport carries the same nullable receipt/backlog components,
|
|
84
|
+
// without a policy/progress snapshot. Reuse their validators here; consumers
|
|
85
|
+
// validate this envelope, never read status or reinterpret diagnosis rules.
|
|
86
|
+
export function validCaptureHealthEnvelope(value, { now = Date.now() } = {}) {
|
|
87
|
+
return exact(value, ["schemaVersion", "status", "rows", "lastRun", "lastSuccess", "lastFailure", "backlog"]) &&
|
|
88
|
+
value.schemaVersion === 1 && Array.isArray(value.rows) &&
|
|
89
|
+
value.status === (value.rows.length ? "warning" : "ok") &&
|
|
90
|
+
value.rows.every(row => exact(row, ["code", "message"]) &&
|
|
91
|
+
typeof row.code === "string" && /^capture-[a-z]+(?:-[a-z]+)*$/.test(row.code) &&
|
|
92
|
+
typeof row.message === "string" && row.message.length > 0) &&
|
|
93
|
+
(value.lastRun === null || run(value.lastRun, now)) &&
|
|
94
|
+
(value.lastSuccess === null || summary(value.lastSuccess, now)) &&
|
|
95
|
+
(value.lastFailure === null || summary(value.lastFailure, now, true)) && backlog(value.backlog);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function readCaptureStatus(root, options) {
|
|
99
|
+
const path = join(root, "capture-status.json");
|
|
100
|
+
let fd;
|
|
101
|
+
try { fd = openSync(path, constants.O_RDONLY | constants.O_NONBLOCK); }
|
|
102
|
+
catch (error) { if (error.code === "ENOENT") return null; throw error; }
|
|
103
|
+
let text;
|
|
104
|
+
try {
|
|
105
|
+
const stat = fstatSync(fd), limit = 1024 * 1024;
|
|
106
|
+
if (!stat.isFile() || stat.size > limit) throw new Error("capture status is not a regular file within 1 MiB");
|
|
107
|
+
const bytes = Buffer.alloc(limit + 1); let size = 0;
|
|
108
|
+
while (size < bytes.length) {
|
|
109
|
+
const count = readSync(fd, bytes, size, bytes.length - size, size);
|
|
110
|
+
if (!count) break; size += count;
|
|
111
|
+
}
|
|
112
|
+
if (size > limit) throw new Error("capture status exceeds 1 MiB");
|
|
113
|
+
text = bytes.toString("utf8", 0, size);
|
|
114
|
+
} finally { closeSync(fd); }
|
|
115
|
+
let parsed;
|
|
116
|
+
try { parsed = JSON.parse(text); } catch { throw new Error("malformed capture status JSON"); }
|
|
117
|
+
if (!validCaptureStatus(parsed, options)) throw new Error("unsupported, malformed or future-dated capture status");
|
|
118
|
+
return parsed;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const INVALID_PREFIX = "capture-status.json.invalid-";
|
|
122
|
+
function syncDirectory(path) {
|
|
123
|
+
const fd = openSync(path, constants.O_RDONLY);
|
|
124
|
+
try { fsyncSync(fd); } finally { closeSync(fd); }
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** A malformed, oversized, non-regular or future-dated status is no evidence
|
|
128
|
+
* and would stop every pass that reads it. It is moved aside, never
|
|
129
|
+
* overwritten, to `capture-status.json.invalid-<UTC stamp>` (the root
|
|
130
|
+
* directory synced after the rename), so the next publication starts from an
|
|
131
|
+
* empty status with no history; one moved-aside copy is kept. A system error
|
|
132
|
+
* reading it (permission, I/O, descriptors) says nothing about its content
|
|
133
|
+
* and is thrown, as is a failed rename: the original stays in place.
|
|
134
|
+
* → { path, reason, stale? } of the moved-aside copy (`stale` lists older
|
|
135
|
+
* copies that could not be removed), or null when the status is absent or
|
|
136
|
+
* readable. */
|
|
137
|
+
export function setAsideInvalidCaptureStatus(root, { now = Date.now() } = {}) {
|
|
138
|
+
let reason;
|
|
139
|
+
try { readCaptureStatus(root, { now }); return null; }
|
|
140
|
+
catch (error) { if (error.code) throw error; reason = error.message; }
|
|
141
|
+
const path = join(root, "capture-status.json");
|
|
142
|
+
const aside = join(root, INVALID_PREFIX + new Date(now).toISOString().replace(/[-:]/g, "").replace(/\.\d+Z$/, "Z"));
|
|
143
|
+
const older = readdirSync(root).filter(name => name.startsWith(INVALID_PREFIX)).map(name => join(root, name)).filter(name => name !== aside);
|
|
144
|
+
try { renameSync(path, aside); }
|
|
145
|
+
catch (error) { throw new Error(`capture status ${path} is unreadable (${reason}) and could not be moved aside: ${error.message}; it was left in place`); }
|
|
146
|
+
// Moved but not confirmed durable: the error says where the status now is.
|
|
147
|
+
try { syncDirectory(root); }
|
|
148
|
+
catch (error) {
|
|
149
|
+
throw Object.assign(new Error(`capture status ${path} is unreadable (${reason}); it was moved aside to ${aside}, but syncing ${root} failed (${error.message}), so the move may not be durable`),
|
|
150
|
+
{ invalidStatus: { path: aside, reason } });
|
|
151
|
+
}
|
|
152
|
+
// The new copy is already safe: an older one that cannot go stays, reported.
|
|
153
|
+
const stale = older.filter(name => { try { unlinkSync(name); return false; } catch (error) { return error.code !== "ENOENT"; } });
|
|
154
|
+
if (older.length > stale.length) try { syncDirectory(root); } catch { /* the removals are diagnostics housekeeping */ }
|
|
155
|
+
return { path: aside, reason, ...(stale.length ? { stale } : {}) };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export function persistCaptureStatus(root, status, options) {
|
|
159
|
+
if (!validCaptureStatus(status, options)) throw new Error("refusing invalid capture status publication");
|
|
160
|
+
const path = join(root, "capture-status.json"), temp = `${path}.${randomUUID()}.tmp`;
|
|
161
|
+
let fd;
|
|
162
|
+
try {
|
|
163
|
+
fd = openSync(temp, "wx", 0o600);
|
|
164
|
+
writeFileSync(fd, JSON.stringify(status) + "\n"); fsyncSync(fd); closeSync(fd); fd = undefined;
|
|
165
|
+
renameSync(temp, path);
|
|
166
|
+
} finally {
|
|
167
|
+
if (fd !== undefined) closeSync(fd);
|
|
168
|
+
try { unlinkSync(temp); } catch (error) { if (error.code !== "ENOENT") throw error; }
|
|
169
|
+
}
|
|
170
|
+
return status;
|
|
171
|
+
}
|