pi-better-server 0.2.2 → 0.3.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/package.json +3 -3
- package/scripts/measure-shared-reads/daemon.mjs +7 -0
- package/scripts/measure-shared-reads/probe.mjs +10 -0
- package/scripts/measure-shared-reads.mjs +145 -0
- package/scripts/test-on-pi.sh +19 -2
- package/src/cli.ts +39 -2
- package/src/client.ts +9 -4
- package/src/daemon-owner.ts +47 -6
- package/src/handshake.ts +8 -7
- package/src/lifecycle-config.ts +4 -0
- package/src/paths.ts +1 -11
- package/src/protocol.ts +5 -0
- package/src/registry.ts +2 -1
- package/src/runtime/background-history.ts +241 -0
- package/src/runtime/background.ts +173 -0
- package/src/runtime/cancellation.ts +95 -0
- package/src/runtime/client.ts +111 -0
- package/src/runtime/domain/assignment-observation.d.mts +1 -0
- package/src/runtime/domain/assignment-observation.mjs +103 -0
- package/src/runtime/domain/assignment-projection.d.mts +1 -0
- package/src/runtime/domain/assignment-projection.mjs +245 -0
- package/src/runtime/domain/background/conditions.mjs +93 -0
- package/src/runtime/domain/background/process-identity.mjs +110 -0
- package/src/runtime/domain/background/process.mjs +511 -0
- package/src/runtime/domain/background/registry.mjs +374 -0
- package/src/runtime/domain/background/snapshot.mjs +73 -0
- package/src/runtime/domain/background/supervisor/identity.mjs +92 -0
- package/src/runtime/domain/background/supervisor/invocation.mjs +29 -0
- package/src/runtime/domain/background/supervisor/store.mjs +604 -0
- package/src/runtime/domain/background/supervisor/supervisor.mjs +720 -0
- package/src/runtime/domain/background/supervisor/types.mjs +47 -0
- package/src/runtime/domain/background/terminal-publication.mjs +58 -0
- package/src/runtime/domain/background/types.mjs +3 -0
- package/src/runtime/domain/coordinator/index.mjs +50 -0
- package/src/runtime/domain/coordinator/src/assignment-outcomes.mjs +160 -0
- package/src/runtime/domain/coordinator/src/discovery.mjs +85 -0
- package/src/runtime/domain/coordinator/src/errors.mjs +12 -0
- package/src/runtime/domain/coordinator/src/operations.mjs +299 -0
- package/src/runtime/domain/coordinator/src/outbox.mjs +282 -0
- package/src/runtime/domain/coordinator/src/recovery.mjs +51 -0
- package/src/runtime/domain/coordinator/src/rows.mjs +277 -0
- package/src/runtime/domain/coordinator/src/schema.mjs +351 -0
- package/src/runtime/domain/coordinator/src/sqlite.mjs +193 -0
- package/src/runtime/domain/coordinator/src/store.mjs +235 -0
- package/src/runtime/domain/coordinator/src/task-cancellation.mjs +187 -0
- package/src/runtime/domain/coordinator/src/types.mjs +24 -0
- package/src/runtime/domain/coordinator/src/validate.mjs +233 -0
- package/src/runtime/domain/coordinator/src/waits.mjs +850 -0
- package/src/runtime/domain/health-observation.d.mts +3 -0
- package/src/runtime/domain/health-observation.mjs +613 -0
- package/src/runtime/domain/health-surface.mjs +279 -0
- package/src/runtime/domain/log-cursor.mjs +148 -0
- package/src/runtime/domain/log-paths.mjs +13 -0
- package/src/runtime/domain/parse.d.mts +2 -0
- package/src/runtime/domain/parse.mjs +1173 -0
- package/src/runtime/domain/process-status.d.mts +1 -0
- package/src/runtime/domain/process-status.mjs +25 -0
- package/src/runtime/domain/session-store.d.mts +2 -0
- package/src/runtime/domain/session-store.mjs +489 -0
- package/src/runtime/domain/shared-harness-settings.d.mts +1 -0
- package/src/runtime/domain/shared-harness-settings.mjs +126 -0
- package/src/runtime/domain/shared-log-utils.mjs +1223 -0
- package/src/runtime/domain/shared-stall-detector.mjs +37 -0
- package/src/runtime/domain/widget.d.mts +1 -0
- package/src/runtime/domain/widget.mjs +250 -0
- package/src/runtime/guardian.ts +189 -0
- package/src/runtime/health-config.ts +27 -0
- package/src/runtime/local-client.ts +34 -0
- package/src/runtime/queries.ts +76 -0
- package/src/runtime/query-executor.ts +228 -0
- package/src/runtime/query-worker.mjs +118 -0
- package/src/runtime/read-model.ts +357 -0
- package/src/runtime/root.ts +29 -0
- package/src/runtime/socket.ts +10 -0
- package/src/runtime/status-cli.ts +11 -0
- package/src/runtime/work-client.d.mts +13 -0
- package/src/runtime/work-client.mjs +88 -0
- package/src/server.ts +136 -12
- package/src/session-activity.ts +22 -2
- package/src/shared-ssh-core/index.ts +30 -1
- package/src/startup/activation.ts +32 -5
- package/src/startup/ensure.ts +13 -1
- package/src/startup/install.ts +2 -0
- package/src/version.ts +4 -0
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
// Generated canonical coordinator. Do not edit directly.
|
|
2
|
+
/**
|
|
3
|
+
* Row mapping for the coordinator store.
|
|
4
|
+
*
|
|
5
|
+
* Reads are strict: a column that does not match its declared shape raises
|
|
6
|
+
* `store-corruption` instead of being coerced, so a damaged journal is reported
|
|
7
|
+
* rather than silently reinterpreted.
|
|
8
|
+
*/
|
|
9
|
+
import { columnBoolean, columnJson, columnNullableNumber, columnNullableText, columnNumber, columnText, getRow, getRows, } from "./sqlite.mjs";
|
|
10
|
+
import { StoreError } from "./errors.mjs";
|
|
11
|
+
// ---- owner identity -----------------------------------------------------
|
|
12
|
+
export function ownerFromRow(row, prefix = "owner") {
|
|
13
|
+
const kind = columnText(row, `${prefix}_kind`);
|
|
14
|
+
return {
|
|
15
|
+
kind,
|
|
16
|
+
sessionId: columnText(row, `${prefix}_session_id`),
|
|
17
|
+
workspace: columnText(row, `${prefix}_workspace`),
|
|
18
|
+
branchId: columnNullableText(row, `${prefix}_branch_id`),
|
|
19
|
+
goalId: columnNullableText(row, `${prefix}_goal_id`),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
// ---- operations ---------------------------------------------------------
|
|
23
|
+
export function getOperationRow(ctx, operationId) {
|
|
24
|
+
return getRow(ctx, "SELECT * FROM operations WHERE operation_id = ?", operationId);
|
|
25
|
+
}
|
|
26
|
+
export function getOperationRowByTask(ctx, taskId) {
|
|
27
|
+
return getRow(ctx, "SELECT * FROM operations WHERE task_id = ?", taskId);
|
|
28
|
+
}
|
|
29
|
+
export function mapOperation(row) {
|
|
30
|
+
return {
|
|
31
|
+
operationId: columnText(row, "operation_id"),
|
|
32
|
+
taskId: columnText(row, "task_id"),
|
|
33
|
+
launchToken: columnText(row, "launch_token"),
|
|
34
|
+
owner: ownerFromRow(row),
|
|
35
|
+
observer: {
|
|
36
|
+
kind: columnText(row, "observer_kind"),
|
|
37
|
+
version: columnText(row, "observer_version"),
|
|
38
|
+
},
|
|
39
|
+
supervision: columnJson(row, "supervision_json"),
|
|
40
|
+
authorityRef: columnNullableText(row, "authority_ref"),
|
|
41
|
+
intentRef: columnNullableText(row, "intent_ref"),
|
|
42
|
+
phase: columnText(row, "phase"),
|
|
43
|
+
health: columnText(row, "health"),
|
|
44
|
+
healthReason: columnNullableText(row, "health_reason"),
|
|
45
|
+
terminalRevision: columnNumber(row, "terminal_revision"),
|
|
46
|
+
terminalOutcome: columnNullableText(row, "terminal_outcome"),
|
|
47
|
+
terminalEventId: columnNullableText(row, "terminal_event_id"),
|
|
48
|
+
terminalDetailLost: columnBoolean(row, "terminal_detail_lost"),
|
|
49
|
+
terminalReason: columnNullableText(row, "terminal_reason"),
|
|
50
|
+
createdAt: columnNumber(row, "created_at"),
|
|
51
|
+
updatedAt: columnNumber(row, "updated_at"),
|
|
52
|
+
terminalAt: columnNullableNumber(row, "terminal_at"),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
export function readOperation(ctx, operationId) {
|
|
56
|
+
const row = getOperationRow(ctx, operationId);
|
|
57
|
+
return row ? mapOperation(row) : null;
|
|
58
|
+
}
|
|
59
|
+
export function requireOperation(ctx, operationId) {
|
|
60
|
+
const row = getOperationRow(ctx, operationId);
|
|
61
|
+
if (!row) {
|
|
62
|
+
throw new StoreError("unknown-operation", `No durable operation is registered as ${operationId}. Register the launch before reporting a terminal outcome or waiting on it.`);
|
|
63
|
+
}
|
|
64
|
+
return mapOperation(row);
|
|
65
|
+
}
|
|
66
|
+
export function mapOperationEvidence(row) {
|
|
67
|
+
const summary = columnNullableText(row, "summary");
|
|
68
|
+
const bytes = columnNullableNumber(row, "bytes");
|
|
69
|
+
const record = {
|
|
70
|
+
operationId: columnText(row, "operation_id"),
|
|
71
|
+
terminalRevision: columnNumber(row, "terminal_revision"),
|
|
72
|
+
ordinal: columnNumber(row, "ordinal"),
|
|
73
|
+
kind: columnText(row, "kind"),
|
|
74
|
+
ref: columnText(row, "ref"),
|
|
75
|
+
detailLost: columnBoolean(row, "detail_lost"),
|
|
76
|
+
capturedAt: columnNumber(row, "captured_at"),
|
|
77
|
+
};
|
|
78
|
+
if (summary !== null)
|
|
79
|
+
record.summary = summary;
|
|
80
|
+
if (bytes !== null)
|
|
81
|
+
record.bytes = bytes;
|
|
82
|
+
return record;
|
|
83
|
+
}
|
|
84
|
+
export function readOperationEvidence(ctx, operationId, terminalRevision) {
|
|
85
|
+
const rows = terminalRevision === undefined
|
|
86
|
+
? getRows(ctx, "SELECT * FROM operation_evidence WHERE operation_id = ? ORDER BY terminal_revision, ordinal", operationId)
|
|
87
|
+
: getRows(ctx, "SELECT * FROM operation_evidence WHERE operation_id = ? AND terminal_revision = ? ORDER BY ordinal", operationId, terminalRevision);
|
|
88
|
+
return rows.map(mapOperationEvidence);
|
|
89
|
+
}
|
|
90
|
+
// ---- waits --------------------------------------------------------------
|
|
91
|
+
export function getWaitRow(ctx, waitId, generation) {
|
|
92
|
+
return getRow(ctx, "SELECT * FROM waits WHERE wait_id = ? AND generation = ?", waitId, generation);
|
|
93
|
+
}
|
|
94
|
+
export function latestWaitGeneration(ctx, waitId) {
|
|
95
|
+
const row = getRow(ctx, "SELECT MAX(generation) AS generation FROM waits WHERE wait_id = ?", waitId);
|
|
96
|
+
if (!row)
|
|
97
|
+
return null;
|
|
98
|
+
const value = columnNullableNumber(row, "generation");
|
|
99
|
+
return value ?? null;
|
|
100
|
+
}
|
|
101
|
+
export function readWaitRow(ctx, waitId, generation) {
|
|
102
|
+
if (generation !== undefined)
|
|
103
|
+
return getWaitRow(ctx, waitId, generation) ?? null;
|
|
104
|
+
const latest = latestWaitGeneration(ctx, waitId);
|
|
105
|
+
if (latest === null)
|
|
106
|
+
return null;
|
|
107
|
+
return getWaitRow(ctx, waitId, latest) ?? null;
|
|
108
|
+
}
|
|
109
|
+
export function requireWaitRow(ctx, waitId, generation) {
|
|
110
|
+
const row = getWaitRow(ctx, waitId, generation);
|
|
111
|
+
if (!row) {
|
|
112
|
+
throw new StoreError("unknown-wait", `No wait generation ${generation} is registered for ${waitId}. Read the current generation before acting on it.`);
|
|
113
|
+
}
|
|
114
|
+
return row;
|
|
115
|
+
}
|
|
116
|
+
export function readDependencies(ctx, waitId, generation) {
|
|
117
|
+
return getRows(ctx, "SELECT * FROM wait_dependencies WHERE wait_id = ? AND generation = ? ORDER BY ordinal", waitId, generation).map((row) => ({
|
|
118
|
+
operationId: columnText(row, "operation_id"),
|
|
119
|
+
link: columnText(row, "link"),
|
|
120
|
+
terminalRevision: columnNumber(row, "terminal_revision"),
|
|
121
|
+
terminalOutcome: columnNullableText(row, "terminal_outcome"),
|
|
122
|
+
settledAt: columnNullableNumber(row, "settled_at"),
|
|
123
|
+
}));
|
|
124
|
+
}
|
|
125
|
+
export function dependencyState(ctx, waitId, generation) {
|
|
126
|
+
const dependencies = readDependencies(ctx, waitId, generation);
|
|
127
|
+
const settled = dependencies.length > 0 && dependencies.every((dep) => dep.terminalRevision > 0);
|
|
128
|
+
const settledTimes = dependencies
|
|
129
|
+
.map((dep) => dep.settledAt)
|
|
130
|
+
.filter((value) => value !== null);
|
|
131
|
+
return {
|
|
132
|
+
dependencies,
|
|
133
|
+
settled,
|
|
134
|
+
settledAt: settled && settledTimes.length > 0 ? Math.max(...settledTimes) : null,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
export function mapWait(ctx, row) {
|
|
138
|
+
const waitId = columnText(row, "wait_id");
|
|
139
|
+
const generation = columnNumber(row, "generation");
|
|
140
|
+
const state = dependencyState(ctx, waitId, generation);
|
|
141
|
+
const acknowledgement = columnJson(row, "acknowledgement_json");
|
|
142
|
+
return {
|
|
143
|
+
waitId,
|
|
144
|
+
generation,
|
|
145
|
+
owner: ownerFromRow(row),
|
|
146
|
+
phase: columnText(row, "phase"),
|
|
147
|
+
phaseReason: columnNullableText(row, "phase_reason"),
|
|
148
|
+
cancelEpoch: columnNumber(row, "cancel_epoch"),
|
|
149
|
+
continuationNote: columnText(row, "continuation_note"),
|
|
150
|
+
park: columnJson(row, "park_json"),
|
|
151
|
+
origin: columnJson(row, "origin_json"),
|
|
152
|
+
dependencies: state.dependencies,
|
|
153
|
+
settled: state.settled,
|
|
154
|
+
settledAt: state.settledAt,
|
|
155
|
+
acknowledgement: acknowledgement
|
|
156
|
+
? { ...acknowledgement, disposition: acknowledgement.disposition }
|
|
157
|
+
: null,
|
|
158
|
+
createdAt: columnNumber(row, "created_at"),
|
|
159
|
+
updatedAt: columnNumber(row, "updated_at"),
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
export function readWait(ctx, waitId, generation) {
|
|
163
|
+
const row = readWaitRow(ctx, waitId, generation);
|
|
164
|
+
return row ? mapWait(ctx, row) : null;
|
|
165
|
+
}
|
|
166
|
+
export function requireWait(ctx, waitId, generation) {
|
|
167
|
+
return mapWait(ctx, requireWaitRow(ctx, waitId, generation));
|
|
168
|
+
}
|
|
169
|
+
// ---- resume claims ------------------------------------------------------
|
|
170
|
+
export function mapClaim(row) {
|
|
171
|
+
return {
|
|
172
|
+
claimId: columnText(row, "claim_id"),
|
|
173
|
+
claimToken: columnText(row, "claim_token"),
|
|
174
|
+
waitId: columnText(row, "wait_id"),
|
|
175
|
+
generation: columnNumber(row, "generation"),
|
|
176
|
+
cancelEpoch: columnNumber(row, "cancel_epoch"),
|
|
177
|
+
state: columnText(row, "state"),
|
|
178
|
+
attemptId: columnNullableText(row, "attempt_id"),
|
|
179
|
+
launchToken: columnNullableText(row, "launch_token"),
|
|
180
|
+
lease: columnJson(row, "lease_json") ?? { ownerId: "unknown" },
|
|
181
|
+
reservedCheckpointId: columnText(row, "reserved_checkpoint_id"),
|
|
182
|
+
claimedAt: columnNumber(row, "claimed_at"),
|
|
183
|
+
publishedAt: columnNullableNumber(row, "published_at"),
|
|
184
|
+
releasedAt: columnNullableNumber(row, "released_at"),
|
|
185
|
+
invalidatedAt: columnNullableNumber(row, "invalidated_at"),
|
|
186
|
+
reason: columnNullableText(row, "reason"),
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
export function readClaim(ctx, claimId) {
|
|
190
|
+
const row = getRow(ctx, "SELECT * FROM resume_claims WHERE claim_id = ?", claimId);
|
|
191
|
+
return row ? mapClaim(row) : null;
|
|
192
|
+
}
|
|
193
|
+
export function readActiveClaim(ctx, waitId, generation) {
|
|
194
|
+
const row = getRow(ctx, "SELECT * FROM resume_claims WHERE wait_id = ? AND generation = ? AND state IN ('claimed', 'published')", waitId, generation);
|
|
195
|
+
return row ? mapClaim(row) : null;
|
|
196
|
+
}
|
|
197
|
+
export function requireClaim(ctx, claimId) {
|
|
198
|
+
const claim = readClaim(ctx, claimId);
|
|
199
|
+
if (!claim) {
|
|
200
|
+
throw new StoreError("claim-state", `No resume claim ${claimId} is recorded. Claims are created by claimResume and are not caller-supplied.`);
|
|
201
|
+
}
|
|
202
|
+
return claim;
|
|
203
|
+
}
|
|
204
|
+
// ---- evidence reads and pins -------------------------------------------
|
|
205
|
+
export function mapEvidenceRead(row) {
|
|
206
|
+
return {
|
|
207
|
+
waitId: columnText(row, "wait_id"),
|
|
208
|
+
generation: columnNumber(row, "generation"),
|
|
209
|
+
operationId: columnText(row, "operation_id"),
|
|
210
|
+
terminalRevision: columnNumber(row, "terminal_revision"),
|
|
211
|
+
reader: {
|
|
212
|
+
kind: columnText(row, "reader_kind"),
|
|
213
|
+
sessionId: columnText(row, "reader_session_id"),
|
|
214
|
+
workspace: columnText(row, "reader_workspace"),
|
|
215
|
+
branchId: columnNullableText(row, "reader_branch_id"),
|
|
216
|
+
goalId: columnNullableText(row, "reader_goal_id"),
|
|
217
|
+
},
|
|
218
|
+
readAt: columnNumber(row, "read_at"),
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
export function mapEvidencePin(row) {
|
|
222
|
+
return {
|
|
223
|
+
pinId: columnText(row, "pin_id"),
|
|
224
|
+
waitId: columnText(row, "wait_id"),
|
|
225
|
+
generation: columnNumber(row, "generation"),
|
|
226
|
+
operationId: columnText(row, "operation_id"),
|
|
227
|
+
terminalRevision: columnNumber(row, "terminal_revision"),
|
|
228
|
+
kind: columnText(row, "kind"),
|
|
229
|
+
ref: columnText(row, "ref"),
|
|
230
|
+
summary: columnNullableText(row, "summary"),
|
|
231
|
+
detailLost: columnBoolean(row, "detail_lost"),
|
|
232
|
+
bytes: columnNullableNumber(row, "bytes"),
|
|
233
|
+
pinnedAt: columnNumber(row, "pinned_at"),
|
|
234
|
+
releasedAt: columnNullableNumber(row, "released_at"),
|
|
235
|
+
releaseReason: columnNullableText(row, "release_reason"),
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
export function readPins(ctx, waitId, generation, includeReleased) {
|
|
239
|
+
const rows = includeReleased
|
|
240
|
+
? getRows(ctx, "SELECT * FROM evidence_pins WHERE wait_id = ? AND generation = ? ORDER BY pinned_at, pin_id", waitId, generation)
|
|
241
|
+
: getRows(ctx, "SELECT * FROM evidence_pins WHERE wait_id = ? AND generation = ? AND released_at IS NULL ORDER BY pinned_at, pin_id", waitId, generation);
|
|
242
|
+
return rows.map(mapEvidencePin);
|
|
243
|
+
}
|
|
244
|
+
export function readPinsForRevision(ctx, waitId, generation, operationId, terminalRevision) {
|
|
245
|
+
return getRows(ctx, `SELECT * FROM evidence_pins
|
|
246
|
+
WHERE wait_id = ? AND generation = ? AND operation_id = ? AND terminal_revision = ?
|
|
247
|
+
ORDER BY pinned_at, pin_id`, waitId, generation, operationId, terminalRevision).map(mapEvidencePin);
|
|
248
|
+
}
|
|
249
|
+
// ---- outbox -------------------------------------------------------------
|
|
250
|
+
export function mapOutbox(ctx, row) {
|
|
251
|
+
const waitId = columnText(row, "wait_id");
|
|
252
|
+
const generation = columnNumber(row, "generation");
|
|
253
|
+
const operationId = columnText(row, "operation_id");
|
|
254
|
+
const terminalRevision = columnNumber(row, "terminal_revision");
|
|
255
|
+
return {
|
|
256
|
+
eventId: columnText(row, "event_id"),
|
|
257
|
+
type: columnText(row, "event_type"),
|
|
258
|
+
operationId,
|
|
259
|
+
terminalRevision,
|
|
260
|
+
waitId,
|
|
261
|
+
generation,
|
|
262
|
+
recipient: ownerFromRow(row, "recipient"),
|
|
263
|
+
evidence: row.event_type === "assignment-outcome" ? readPins(ctx, waitId, generation, false) : readPinsForRevision(ctx, waitId, generation, operationId, terminalRevision),
|
|
264
|
+
state: columnText(row, "state"),
|
|
265
|
+
attempts: columnNumber(row, "attempts"),
|
|
266
|
+
dispatchClaim: columnJson(row, "dispatch_claim_json"),
|
|
267
|
+
createdAt: columnNumber(row, "created_at"),
|
|
268
|
+
claimedAt: columnNullableNumber(row, "claimed_at"),
|
|
269
|
+
deliveredAt: columnNullableNumber(row, "delivered_at"),
|
|
270
|
+
acknowledgedAt: columnNullableNumber(row, "acknowledged_at"),
|
|
271
|
+
voidedAt: columnNullableNumber(row, "voided_at"),
|
|
272
|
+
reason: columnNullableText(row, "reason"),
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
export function readOutboxRow(ctx, eventId) {
|
|
276
|
+
return getRow(ctx, "SELECT * FROM outbox_events WHERE event_id = ?", eventId) ?? null;
|
|
277
|
+
}
|
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
// Generated canonical coordinator. Do not edit directly.
|
|
2
|
+
/**
|
|
3
|
+
* Schema v4 for the durable operation coordinator.
|
|
4
|
+
*
|
|
5
|
+
* The store refuses to open a database whose recorded version it does not
|
|
6
|
+
* understand, and leaves the file untouched. An unreadable or newer database
|
|
7
|
+
* disables the feature; it never deletes records to recover.
|
|
8
|
+
*
|
|
9
|
+
* Uniqueness constraints carry lifecycle invariants into the database itself:
|
|
10
|
+
*
|
|
11
|
+
* - one durable operation per background task id;
|
|
12
|
+
* - one pending wait generation per owner identity (partial unique index);
|
|
13
|
+
* - one active resume claim per wait generation (partial unique index);
|
|
14
|
+
* - one outbox event per terminal revision per wait generation;
|
|
15
|
+
* - one evidence pin per reference and one evidence read per revision.
|
|
16
|
+
*/
|
|
17
|
+
import { SCHEMA_VERSION } from "./types.mjs";
|
|
18
|
+
import { StoreError } from "./errors.mjs";
|
|
19
|
+
import { columnText, getRow, getRows, run } from "./sqlite.mjs";
|
|
20
|
+
const DDL = [
|
|
21
|
+
`CREATE TABLE task_cancellation_authorities (
|
|
22
|
+
credential_hash TEXT PRIMARY KEY,
|
|
23
|
+
request_json TEXT NOT NULL,
|
|
24
|
+
directory TEXT NOT NULL
|
|
25
|
+
)`,
|
|
26
|
+
`CREATE TABLE task_cancellation_requests (
|
|
27
|
+
owner_key TEXT NOT NULL,
|
|
28
|
+
request_key TEXT NOT NULL,
|
|
29
|
+
operation_id TEXT NOT NULL REFERENCES operations (operation_id),
|
|
30
|
+
intent_json TEXT NOT NULL,
|
|
31
|
+
result_json TEXT,
|
|
32
|
+
state TEXT NOT NULL CHECK (state IN ('accepted', 'stop-requested')),
|
|
33
|
+
PRIMARY KEY (owner_key, request_key)
|
|
34
|
+
)`,
|
|
35
|
+
`CREATE TABLE assignment_repairs (
|
|
36
|
+
fault_key TEXT PRIMARY KEY,
|
|
37
|
+
assignment_id TEXT NOT NULL,
|
|
38
|
+
wait_id TEXT NOT NULL,
|
|
39
|
+
generation INTEGER NOT NULL,
|
|
40
|
+
fault_json TEXT NOT NULL,
|
|
41
|
+
claim_id TEXT NOT NULL
|
|
42
|
+
)`,
|
|
43
|
+
`CREATE TABLE schema_meta (
|
|
44
|
+
key TEXT PRIMARY KEY,
|
|
45
|
+
value TEXT NOT NULL
|
|
46
|
+
)`,
|
|
47
|
+
`CREATE TABLE operations (
|
|
48
|
+
operation_id TEXT PRIMARY KEY,
|
|
49
|
+
task_id TEXT NOT NULL UNIQUE,
|
|
50
|
+
launch_token TEXT NOT NULL,
|
|
51
|
+
owner_kind TEXT NOT NULL,
|
|
52
|
+
owner_session_id TEXT NOT NULL,
|
|
53
|
+
owner_workspace TEXT NOT NULL,
|
|
54
|
+
owner_branch_id TEXT,
|
|
55
|
+
owner_goal_id TEXT,
|
|
56
|
+
observer_kind TEXT NOT NULL,
|
|
57
|
+
observer_version TEXT NOT NULL,
|
|
58
|
+
supervision_json TEXT,
|
|
59
|
+
supervision_host_id TEXT,
|
|
60
|
+
authority_ref TEXT,
|
|
61
|
+
intent_ref TEXT,
|
|
62
|
+
phase TEXT NOT NULL,
|
|
63
|
+
health TEXT NOT NULL,
|
|
64
|
+
health_reason TEXT,
|
|
65
|
+
terminal_revision INTEGER NOT NULL DEFAULT 0,
|
|
66
|
+
terminal_outcome TEXT,
|
|
67
|
+
terminal_event_id TEXT,
|
|
68
|
+
terminal_detail_lost INTEGER NOT NULL DEFAULT 0,
|
|
69
|
+
terminal_reason TEXT,
|
|
70
|
+
created_at INTEGER NOT NULL,
|
|
71
|
+
updated_at INTEGER NOT NULL,
|
|
72
|
+
terminal_at INTEGER
|
|
73
|
+
)`,
|
|
74
|
+
`CREATE INDEX operations_owner ON operations (owner_kind, owner_session_id)`,
|
|
75
|
+
`CREATE INDEX operations_phase ON operations (phase)`,
|
|
76
|
+
`CREATE INDEX operations_running_supervisor ON operations (phase, supervision_host_id)`,
|
|
77
|
+
// Discovery leadership is separate from per-operation supervision. The token
|
|
78
|
+
// fences stale acquisition and release; the host id is checked with process
|
|
79
|
+
// identity before a replacement.
|
|
80
|
+
`CREATE TABLE discovery_claims (
|
|
81
|
+
scope TEXT PRIMARY KEY,
|
|
82
|
+
host_id TEXT NOT NULL,
|
|
83
|
+
token TEXT NOT NULL,
|
|
84
|
+
claimed_at INTEGER NOT NULL,
|
|
85
|
+
updated_at INTEGER NOT NULL
|
|
86
|
+
)`,
|
|
87
|
+
// Small shared state for change notification. The table is additive so an
|
|
88
|
+
// older reader that understands schema version 1 can safely ignore it.
|
|
89
|
+
`CREATE TABLE coordinator_state (
|
|
90
|
+
key TEXT PRIMARY KEY,
|
|
91
|
+
value TEXT NOT NULL
|
|
92
|
+
)`,
|
|
93
|
+
`CREATE TABLE operation_evidence (
|
|
94
|
+
operation_id TEXT NOT NULL REFERENCES operations (operation_id),
|
|
95
|
+
terminal_revision INTEGER NOT NULL,
|
|
96
|
+
ordinal INTEGER NOT NULL,
|
|
97
|
+
kind TEXT NOT NULL,
|
|
98
|
+
ref TEXT NOT NULL,
|
|
99
|
+
summary TEXT,
|
|
100
|
+
detail_lost INTEGER NOT NULL DEFAULT 0,
|
|
101
|
+
bytes INTEGER,
|
|
102
|
+
captured_at INTEGER NOT NULL,
|
|
103
|
+
PRIMARY KEY (operation_id, terminal_revision, ordinal)
|
|
104
|
+
)`,
|
|
105
|
+
`CREATE TABLE waits (
|
|
106
|
+
dispatch_disarmed INTEGER NOT NULL DEFAULT 0,
|
|
107
|
+
wait_id TEXT NOT NULL,
|
|
108
|
+
generation INTEGER NOT NULL,
|
|
109
|
+
owner_kind TEXT NOT NULL,
|
|
110
|
+
owner_session_id TEXT NOT NULL,
|
|
111
|
+
owner_workspace TEXT NOT NULL,
|
|
112
|
+
owner_branch_id TEXT,
|
|
113
|
+
owner_goal_id TEXT,
|
|
114
|
+
phase TEXT NOT NULL,
|
|
115
|
+
phase_reason TEXT,
|
|
116
|
+
cancel_epoch INTEGER NOT NULL DEFAULT 0,
|
|
117
|
+
continuation_note TEXT NOT NULL,
|
|
118
|
+
park_json TEXT,
|
|
119
|
+
origin_json TEXT,
|
|
120
|
+
settled_at INTEGER,
|
|
121
|
+
acknowledged_at INTEGER,
|
|
122
|
+
acknowledgement_json TEXT,
|
|
123
|
+
created_at INTEGER NOT NULL,
|
|
124
|
+
updated_at INTEGER NOT NULL,
|
|
125
|
+
PRIMARY KEY (wait_id, generation)
|
|
126
|
+
)`,
|
|
127
|
+
// At most one generation per owner identity (session + branch) may be pending.
|
|
128
|
+
`CREATE UNIQUE INDEX waits_one_pending_per_owner
|
|
129
|
+
ON waits (owner_kind, owner_session_id, coalesce(owner_branch_id, ''))
|
|
130
|
+
WHERE phase NOT IN ('consumed', 'cancelled') AND dispatch_disarmed = 0`,
|
|
131
|
+
`CREATE INDEX waits_owner ON waits (owner_kind, owner_session_id)`,
|
|
132
|
+
`CREATE TABLE wait_dependencies (
|
|
133
|
+
wait_id TEXT NOT NULL,
|
|
134
|
+
generation INTEGER NOT NULL,
|
|
135
|
+
ordinal INTEGER NOT NULL,
|
|
136
|
+
operation_id TEXT NOT NULL REFERENCES operations (operation_id),
|
|
137
|
+
link TEXT NOT NULL,
|
|
138
|
+
terminal_revision INTEGER NOT NULL DEFAULT 0,
|
|
139
|
+
terminal_outcome TEXT,
|
|
140
|
+
settled_at INTEGER,
|
|
141
|
+
PRIMARY KEY (wait_id, generation, operation_id)
|
|
142
|
+
)`,
|
|
143
|
+
`CREATE INDEX wait_dependencies_operation ON wait_dependencies (operation_id, terminal_revision)`,
|
|
144
|
+
`CREATE TABLE resume_claims (
|
|
145
|
+
claim_id TEXT PRIMARY KEY,
|
|
146
|
+
wait_id TEXT NOT NULL,
|
|
147
|
+
generation INTEGER NOT NULL,
|
|
148
|
+
cancel_epoch INTEGER NOT NULL,
|
|
149
|
+
claim_token TEXT NOT NULL UNIQUE,
|
|
150
|
+
state TEXT NOT NULL,
|
|
151
|
+
attempt_id TEXT UNIQUE,
|
|
152
|
+
launch_token TEXT,
|
|
153
|
+
lease_json TEXT NOT NULL,
|
|
154
|
+
reserved_checkpoint_id TEXT NOT NULL,
|
|
155
|
+
claimed_at INTEGER NOT NULL,
|
|
156
|
+
published_at INTEGER,
|
|
157
|
+
released_at INTEGER,
|
|
158
|
+
invalidated_at INTEGER,
|
|
159
|
+
reason TEXT
|
|
160
|
+
)`,
|
|
161
|
+
`CREATE UNIQUE INDEX resume_claims_one_active
|
|
162
|
+
ON resume_claims (wait_id, generation)
|
|
163
|
+
WHERE state IN ('claimed', 'published')`,
|
|
164
|
+
`CREATE TABLE evidence_reads (
|
|
165
|
+
wait_id TEXT NOT NULL,
|
|
166
|
+
generation INTEGER NOT NULL,
|
|
167
|
+
operation_id TEXT NOT NULL,
|
|
168
|
+
terminal_revision INTEGER NOT NULL,
|
|
169
|
+
reader_kind TEXT NOT NULL,
|
|
170
|
+
reader_session_id TEXT NOT NULL,
|
|
171
|
+
reader_workspace TEXT NOT NULL,
|
|
172
|
+
reader_branch_id TEXT,
|
|
173
|
+
reader_goal_id TEXT,
|
|
174
|
+
read_at INTEGER NOT NULL,
|
|
175
|
+
PRIMARY KEY (wait_id, generation, operation_id, terminal_revision)
|
|
176
|
+
)`,
|
|
177
|
+
`CREATE TABLE evidence_pins (
|
|
178
|
+
pin_id TEXT PRIMARY KEY,
|
|
179
|
+
wait_id TEXT NOT NULL,
|
|
180
|
+
generation INTEGER NOT NULL,
|
|
181
|
+
operation_id TEXT NOT NULL,
|
|
182
|
+
terminal_revision INTEGER NOT NULL,
|
|
183
|
+
kind TEXT NOT NULL,
|
|
184
|
+
ref TEXT NOT NULL,
|
|
185
|
+
summary TEXT,
|
|
186
|
+
detail_lost INTEGER NOT NULL DEFAULT 0,
|
|
187
|
+
bytes INTEGER,
|
|
188
|
+
pinned_at INTEGER NOT NULL,
|
|
189
|
+
released_at INTEGER,
|
|
190
|
+
release_reason TEXT
|
|
191
|
+
)`,
|
|
192
|
+
`CREATE UNIQUE INDEX evidence_pins_reference
|
|
193
|
+
ON evidence_pins (wait_id, generation, operation_id, terminal_revision, kind, ref)`,
|
|
194
|
+
`CREATE INDEX evidence_pins_operation ON evidence_pins (operation_id, terminal_revision, released_at)`,
|
|
195
|
+
`CREATE TABLE outbox_events (
|
|
196
|
+
event_type TEXT NOT NULL DEFAULT 'dependency-ready',
|
|
197
|
+
event_id TEXT PRIMARY KEY,
|
|
198
|
+
operation_id TEXT NOT NULL,
|
|
199
|
+
terminal_revision INTEGER NOT NULL,
|
|
200
|
+
wait_id TEXT NOT NULL,
|
|
201
|
+
generation INTEGER NOT NULL,
|
|
202
|
+
recipient_kind TEXT NOT NULL,
|
|
203
|
+
recipient_session_id TEXT NOT NULL,
|
|
204
|
+
recipient_workspace TEXT NOT NULL,
|
|
205
|
+
recipient_branch_id TEXT,
|
|
206
|
+
recipient_goal_id TEXT,
|
|
207
|
+
state TEXT NOT NULL,
|
|
208
|
+
attempts INTEGER NOT NULL DEFAULT 0,
|
|
209
|
+
dispatch_claim_json TEXT,
|
|
210
|
+
created_at INTEGER NOT NULL,
|
|
211
|
+
claimed_at INTEGER,
|
|
212
|
+
delivered_at INTEGER,
|
|
213
|
+
acknowledged_at INTEGER,
|
|
214
|
+
voided_at INTEGER,
|
|
215
|
+
reason TEXT,
|
|
216
|
+
UNIQUE (operation_id, terminal_revision, wait_id, generation)
|
|
217
|
+
)`,
|
|
218
|
+
`CREATE INDEX outbox_recipient ON outbox_events (recipient_kind, recipient_session_id, state)`,
|
|
219
|
+
`CREATE INDEX outbox_wait ON outbox_events (wait_id, generation)`,
|
|
220
|
+
`CREATE TABLE assignment_outcomes (
|
|
221
|
+
assignment_id TEXT PRIMARY KEY,
|
|
222
|
+
event_id TEXT NOT NULL UNIQUE,
|
|
223
|
+
record_json TEXT NOT NULL
|
|
224
|
+
)`,
|
|
225
|
+
`CREATE TABLE assignment_bindings (
|
|
226
|
+
assignment_id TEXT PRIMARY KEY,
|
|
227
|
+
owner_json TEXT NOT NULL,
|
|
228
|
+
recipient_json TEXT NOT NULL
|
|
229
|
+
)`,
|
|
230
|
+
`CREATE TABLE assignment_attempts (
|
|
231
|
+
attempt_id TEXT PRIMARY KEY,
|
|
232
|
+
assignment_id TEXT NOT NULL REFERENCES assignment_bindings (assignment_id)
|
|
233
|
+
)`,
|
|
234
|
+
`CREATE TABLE assignment_outcome_waits (
|
|
235
|
+
assignment_id TEXT NOT NULL REFERENCES assignment_outcomes (assignment_id),
|
|
236
|
+
wait_id TEXT NOT NULL,
|
|
237
|
+
generation INTEGER NOT NULL,
|
|
238
|
+
PRIMARY KEY (wait_id, generation)
|
|
239
|
+
)`,
|
|
240
|
+
];
|
|
241
|
+
const SCHEMA_VERSION_KEY = "schema_version";
|
|
242
|
+
function existingTables(ctx) {
|
|
243
|
+
return getRows(ctx, `SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%' ORDER BY name`).map((row) => columnText(row, "name"));
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Create the schema on an empty database, or verify the recorded version.
|
|
247
|
+
* Any mismatch refuses without modifying the file.
|
|
248
|
+
*/
|
|
249
|
+
export function ensureSchema(ctx) {
|
|
250
|
+
const tables = existingTables(ctx);
|
|
251
|
+
const hasMeta = tables.includes("schema_meta");
|
|
252
|
+
if (!hasMeta) {
|
|
253
|
+
if (tables.length > 0) {
|
|
254
|
+
throw new StoreError("schema-version", `The database at this path holds tables (${tables.join(", ")}) but no ${SCHEMA_VERSION_KEY}. It was not written by the operation coordinator; refusing to reuse or clear it.`);
|
|
255
|
+
}
|
|
256
|
+
ctx.transaction(() => {
|
|
257
|
+
for (const statement of DDL)
|
|
258
|
+
ctx.db.exec(statement);
|
|
259
|
+
run(ctx, "INSERT INTO schema_meta (key, value) VALUES (?, ?)", SCHEMA_VERSION_KEY, String(SCHEMA_VERSION));
|
|
260
|
+
});
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
const row = getRow(ctx, "SELECT value FROM schema_meta WHERE key = ?", SCHEMA_VERSION_KEY);
|
|
264
|
+
if (!row) {
|
|
265
|
+
throw new StoreError("schema-version", `The database at this path has no ${SCHEMA_VERSION_KEY} record. Refusing to read or migrate unknown records.`);
|
|
266
|
+
}
|
|
267
|
+
const raw = columnText(row, "value");
|
|
268
|
+
const recorded = Number(raw);
|
|
269
|
+
if (!Number.isInteger(recorded) || recorded < 1) {
|
|
270
|
+
throw new StoreError("schema-version", `The database records schema version ${JSON.stringify(raw)}, which is not a usable version. Records are left in place.`);
|
|
271
|
+
}
|
|
272
|
+
if (recorded > SCHEMA_VERSION) {
|
|
273
|
+
throw new StoreError("schema-version", `The database was written by schema version ${recorded}; this build understands version ${SCHEMA_VERSION}. Upgrade the harness instead of downgrading the records.`);
|
|
274
|
+
}
|
|
275
|
+
if (recorded < SCHEMA_VERSION) {
|
|
276
|
+
if (recorded === 3) {
|
|
277
|
+
ctx.transaction(() => {
|
|
278
|
+
ctx.db.exec(DDL.find((sql) => sql.startsWith("CREATE TABLE assignment_repairs")));
|
|
279
|
+
run(ctx, "UPDATE schema_meta SET value = ? WHERE key = ?", String(SCHEMA_VERSION), SCHEMA_VERSION_KEY);
|
|
280
|
+
});
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
if (recorded === 1 || recorded === 2) {
|
|
284
|
+
ensureAdditiveSchema(ctx);
|
|
285
|
+
ctx.transaction(() => {
|
|
286
|
+
if (recorded === 1)
|
|
287
|
+
ctx.db.exec("ALTER TABLE outbox_events ADD COLUMN event_type TEXT NOT NULL DEFAULT 'dependency-ready'");
|
|
288
|
+
for (const statement of DDL.filter((sql) => sql.startsWith("CREATE TABLE assignment_")))
|
|
289
|
+
ctx.db.exec(statement.replace("CREATE TABLE", "CREATE TABLE IF NOT EXISTS"));
|
|
290
|
+
ctx.db.exec("ALTER TABLE waits ADD COLUMN dispatch_disarmed INTEGER NOT NULL DEFAULT 0");
|
|
291
|
+
ctx.db.exec("UPDATE waits SET dispatch_disarmed = 1 WHERE EXISTS (SELECT 1 FROM assignment_outcome_waits aw WHERE aw.wait_id = waits.wait_id AND aw.generation = waits.generation)");
|
|
292
|
+
ctx.db.exec("DROP INDEX waits_one_pending_per_owner");
|
|
293
|
+
ctx.db.exec(DDL.find((sql) => sql.startsWith("CREATE UNIQUE INDEX waits_one_pending_per_owner")));
|
|
294
|
+
run(ctx, "UPDATE schema_meta SET value = ? WHERE key = ?", String(SCHEMA_VERSION), SCHEMA_VERSION_KEY);
|
|
295
|
+
});
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
throw new StoreError("schema-version", `The database is at schema version ${recorded}; this build needs version ${SCHEMA_VERSION}. No migration is provided for this pair; records are left in place.`);
|
|
299
|
+
}
|
|
300
|
+
ensureAdditiveSchema(ctx);
|
|
301
|
+
ctx.db.exec(DDL.find((sql) => sql.startsWith("CREATE TABLE assignment_attempts")).replace("CREATE TABLE", "CREATE TABLE IF NOT EXISTS"));
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Schema version 1 is extended additively: older readers ignore the new column,
|
|
305
|
+
* index and tables, so a live session on the previous build does not lose its
|
|
306
|
+
* records during a rolling upgrade. Missing additions are created once on open.
|
|
307
|
+
*/
|
|
308
|
+
function ensureAdditiveSchema(ctx) {
|
|
309
|
+
const hasColumn = () => getRows(ctx, "PRAGMA table_info(operations)").some((row) => columnText(row, "name") === "supervision_host_id");
|
|
310
|
+
const hasIndex = () => getRows(ctx, "SELECT name FROM sqlite_master WHERE type = 'index' AND name = ?", "operations_running_supervisor").length > 0;
|
|
311
|
+
const hasTable = (name) => existingTables(ctx).includes(name);
|
|
312
|
+
if (hasColumn()
|
|
313
|
+
&& hasIndex()
|
|
314
|
+
&& hasTable("discovery_claims")
|
|
315
|
+
&& hasTable("coordinator_state")
|
|
316
|
+
&& hasTable("task_cancellation_requests")
|
|
317
|
+
&& hasTable("task_cancellation_authorities")
|
|
318
|
+
&& getRows(ctx, "PRAGMA table_info(task_cancellation_requests)").some(row => row.name === "result_json"))
|
|
319
|
+
return;
|
|
320
|
+
ctx.transaction(() => {
|
|
321
|
+
ctx.db.exec(DDL.find((sql) => sql.startsWith("CREATE TABLE task_cancellation_requests")).replace("CREATE TABLE", "CREATE TABLE IF NOT EXISTS"));
|
|
322
|
+
if (!getRows(ctx, "PRAGMA table_info(task_cancellation_requests)").some(row => row.name === "result_json"))
|
|
323
|
+
ctx.db.exec("ALTER TABLE task_cancellation_requests ADD COLUMN result_json TEXT");
|
|
324
|
+
ctx.db.exec(DDL.find((sql) => sql.startsWith("CREATE TABLE task_cancellation_authorities")).replace("CREATE TABLE", "CREATE TABLE IF NOT EXISTS"));
|
|
325
|
+
if (!hasColumn()) {
|
|
326
|
+
ctx.db.exec("ALTER TABLE operations ADD COLUMN supervision_host_id TEXT");
|
|
327
|
+
ctx.db.exec(`UPDATE operations
|
|
328
|
+
SET supervision_host_id = CASE
|
|
329
|
+
WHEN json_valid(supervision_json) THEN json_extract(supervision_json, '$.hostId')
|
|
330
|
+
ELSE NULL
|
|
331
|
+
END`);
|
|
332
|
+
}
|
|
333
|
+
ctx.db.exec("CREATE INDEX IF NOT EXISTS operations_running_supervisor ON operations (phase, supervision_host_id)");
|
|
334
|
+
ctx.db.exec(`CREATE TABLE IF NOT EXISTS discovery_claims (
|
|
335
|
+
scope TEXT PRIMARY KEY,
|
|
336
|
+
host_id TEXT NOT NULL,
|
|
337
|
+
token TEXT NOT NULL,
|
|
338
|
+
claimed_at INTEGER NOT NULL,
|
|
339
|
+
updated_at INTEGER NOT NULL
|
|
340
|
+
)`);
|
|
341
|
+
ctx.db.exec(`CREATE TABLE IF NOT EXISTS coordinator_state (
|
|
342
|
+
key TEXT PRIMARY KEY,
|
|
343
|
+
value TEXT NOT NULL
|
|
344
|
+
)`);
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
/** Test and diagnostic seam: read the recorded schema version without opening a store. */
|
|
348
|
+
export function readSchemaVersion(ctx) {
|
|
349
|
+
const row = getRow(ctx, "SELECT value FROM schema_meta WHERE key = ?", SCHEMA_VERSION_KEY);
|
|
350
|
+
return row ? Number(columnText(row, "value")) : null;
|
|
351
|
+
}
|