@pgxsinkit/client 0.2.2 → 0.2.4
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/dist/boot-report.d.ts +3 -3
- package/dist/index.d.ts +35 -119
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +259 -392
- package/dist/index.js.map +14 -15
- package/dist/opfs-effects.d.ts +1 -1
- package/dist/store-boot.d.ts +82 -18
- package/dist/store-boot.d.ts.map +1 -1
- package/dist/store-lifecycle.d.ts +5 -66
- package/dist/store-lifecycle.d.ts.map +1 -1
- package/dist/store-meta.d.ts +20 -21
- package/dist/store-meta.d.ts.map +1 -1
- package/dist/worker/attach-sync-client.d.ts +51 -5
- package/dist/worker/attach-sync-client.d.ts.map +1 -1
- package/dist/worker/define-sync-worker.d.ts +1 -12
- package/dist/worker/define-sync-worker.d.ts.map +1 -1
- package/dist/worker/election-coordinator.d.ts.map +1 -1
- package/dist/worker/engine-control.d.ts +13 -0
- package/dist/worker/engine-control.d.ts.map +1 -1
- package/dist/worker/engine-router.d.ts +21 -5
- package/dist/worker/engine-router.d.ts.map +1 -1
- package/package.json +3 -3
- package/skills/core/SKILL.md +3 -2
- package/skills/operating/SKILL.md +40 -8
- package/src/boot-report.ts +3 -3
- package/src/index.ts +159 -486
- package/src/opfs-effects.ts +2 -2
- package/src/store-boot.ts +183 -71
- package/src/store-lifecycle.ts +12 -96
- package/src/store-meta.ts +33 -33
- package/src/worker/attach-sync-client.ts +194 -21
- package/src/worker/define-sync-worker.ts +94 -26
- package/src/worker/election-coordinator.ts +6 -1
- package/src/worker/engine-control.ts +12 -0
- package/src/worker/engine-router.ts +176 -21
- package/dist/adoption.d.ts +0 -118
- package/dist/adoption.d.ts.map +0 -1
- package/src/adoption.ts +0 -275
package/src/opfs-effects.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// The REAL OPFS effects for the store-boot wiring (ADR-0049 capability-driven engine placement, plan
|
|
2
|
-
// step 10a). `store-lifecycle.ts` owns the PURE, effect-injected destruction/
|
|
2
|
+
// step 10a). `store-lifecycle.ts` owns the PURE, effect-injected destruction/fresh-candidate
|
|
3
3
|
// machines; `store-boot.ts` assembles observations and executes their verdicts. This module is the concrete
|
|
4
4
|
// OPFS side those verdicts drive: create/delete the commitment sentinel, delete the store directory, observe
|
|
5
5
|
// the commitment namespace without creating anything, and hand the store directory handle to the
|
|
@@ -70,7 +70,7 @@ function resolveDefaultGetRoot(): (() => Promise<DirLike>) | null {
|
|
|
70
70
|
/**
|
|
71
71
|
* Construct the real OPFS effects for a store, all under `store-path.ts`'s disjoint namespaces. The returned
|
|
72
72
|
* object's methods satisfy the idempotency contracts the `store-lifecycle.ts` machines require, so a boot that
|
|
73
|
-
* resumes an interrupted destruction/candidate
|
|
73
|
+
* resumes an interrupted destruction/candidate re-runs them safely.
|
|
74
74
|
*/
|
|
75
75
|
export function createOpfsEffects(storePath: string, deps?: OpfsEffectsDeps): OpfsEffects {
|
|
76
76
|
const getRoot = deps?.getRoot ?? resolveDefaultGetRoot();
|
package/src/store-boot.ts
CHANGED
|
@@ -1,25 +1,33 @@
|
|
|
1
1
|
// Store boot resolution — assemble the boot observations and EXECUTE the classifier's verdict with real
|
|
2
2
|
// effects (ADR-0049 capability-driven engine placement, plan step 10a). `store-meta.ts` owns the PURE
|
|
3
|
-
// classifier ({@link classifyStoreBoot}, boot classification 1–
|
|
4
|
-
// owns the PURE, effect-injected destruction/
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
3
|
+
// classifier ({@link classifyStoreBoot}, boot classification 1–6) and the meta-record IO; `store-lifecycle.ts`
|
|
4
|
+
// owns the PURE, effect-injected destruction/fresh-candidate machines; `opfs-effects.ts` owns the concrete
|
|
5
|
+
// OPFS side. This module is the WIRING that reads the meta record, observes the commitment namespace and the
|
|
6
|
+
// recordless idb fact, classifies, and then runs the destructive/candidate effects BEFORE returning the
|
|
7
|
+
// resolved `dataDir` + `storageBackend` a caller opens the store at.
|
|
8
|
+
//
|
|
9
|
+
// It also owns the OTHER browser boot arm: {@link resolveDeniedBootAuthority}, the one bounded meta read a
|
|
10
|
+
// probe-DENIED (no OPFS sync access) boot performs before it may mint an IDB store. That arm never runs the
|
|
11
|
+
// phase machine — it is record-blind by design — but it still owes the record three things: finish an
|
|
12
|
+
// authorized `deleting` handoff, RETIRE an unexposed `opfs-candidate` (nothing may be exposed beneath a record
|
|
13
|
+
// that still claims one), and REFUSE an `opfs-committed` store outright
|
|
14
|
+
// ({@link CommittedStoreUnreachableError}), because a home without handles cannot open it and would
|
|
15
|
+
// otherwise mint an empty idb sibling at the same path.
|
|
8
16
|
//
|
|
9
17
|
// It never assembles a storage URL itself — every `dataDir` comes from `store-path.ts`'s
|
|
10
18
|
// {@link resolveStoreDataDir}, which stays the toolkit's only URL assembler. It carries no DOM lib dependency
|
|
11
19
|
// and takes injectable deps (meta IO, OPFS root, the recordless idb existence check) so Bun unit tests fake the
|
|
12
20
|
// whole surface with no real IndexedDB / OPFS / WASM.
|
|
13
21
|
//
|
|
14
|
-
// THE COMMITMENT BARRIER IS NOT HERE. When the verdict stands up a fresh opfs CANDIDATE (`virgin-create`
|
|
15
|
-
//
|
|
16
|
-
//
|
|
17
|
-
//
|
|
18
|
-
//
|
|
19
|
-
//
|
|
22
|
+
// THE COMMITMENT BARRIER IS NOT HERE. When the verdict stands up a fresh opfs CANDIDATE (`virgin-create` with
|
|
23
|
+
// opfs access), this function returns with the candidate's record at `opfs-candidate` and its directory created
|
|
24
|
+
// but UNCOMMITTED. The strict-sync → sentinel → `opfs-committed` barrier that promotes it (invariant 3 — an
|
|
25
|
+
// uncommitted candidate is never exposed to writes) is the mint seam's post-open work, wired in plan step
|
|
26
|
+
// 10b/11. The returned {@link StoreBootResolution.verdict} tells the caller a candidate is uncommitted and
|
|
27
|
+
// needs that barrier.
|
|
20
28
|
|
|
21
29
|
import { createOpfsEffects, type OpfsEffectsDeps } from "./opfs-effects";
|
|
22
|
-
import { beginFreshCandidate,
|
|
30
|
+
import { beginFreshCandidate, resumeDeletion } from "./store-lifecycle";
|
|
23
31
|
import {
|
|
24
32
|
classifyStoreBoot,
|
|
25
33
|
deleteStoreMetaRecord,
|
|
@@ -48,10 +56,11 @@ export interface StoreBootResolution {
|
|
|
48
56
|
/** The resolved backend, for diagnostics. */
|
|
49
57
|
storageBackend: ResolvedStorageBackend;
|
|
50
58
|
/**
|
|
51
|
-
* The executed boot verdict
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
* run the commitment barrier before exposing that store
|
|
59
|
+
* The executed boot verdict — always a TERMINAL one (the record-clearing verdicts re-classify rather than
|
|
60
|
+
* return). Absent on the passthrough backends (`memory` / `filesystem`), which have NO meta machinery.
|
|
61
|
+
* Present on every browser classification — in particular it is the signal that an opfs CANDIDATE was stood
|
|
62
|
+
* up UNCOMMITTED (`virgin-create`): the mint seam must run the commitment barrier before exposing that store
|
|
63
|
+
* to writes (plan step 10b/11).
|
|
55
64
|
*/
|
|
56
65
|
verdict?: StoreBootVerdict;
|
|
57
66
|
}
|
|
@@ -74,10 +83,12 @@ export interface ResolveStoreBootOptions {
|
|
|
74
83
|
}
|
|
75
84
|
|
|
76
85
|
/**
|
|
77
|
-
* Bounded re-classification budget.
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
86
|
+
* Bounded re-classification budget. Two verdicts re-run the loop, and both end by DELETING the meta record:
|
|
87
|
+
* `resume-deletion` (the destructive lifecycle — sentinel → backend store → record) and
|
|
88
|
+
* `delete-candidate-and-rebuild` (retiring an unexposed candidate — sentinel → directory → record). Each
|
|
89
|
+
* leaves a RECORDLESS state whose next classification is necessarily terminal (`boot-idb-authoritative` when
|
|
90
|
+
* an idb store exists at the path, otherwise `virgin-create`). One re-run is enough; the small budget is a
|
|
91
|
+
* guard against an unexpected non-terminating cycle.
|
|
81
92
|
*/
|
|
82
93
|
const MAX_DELETION_RECLASSIFY = 4;
|
|
83
94
|
|
|
@@ -141,39 +152,137 @@ function resolveHasIndexedDb(meta?: StoreMetaDeps): boolean {
|
|
|
141
152
|
return typeof (globalThis as { indexedDB?: unknown }).indexedDB !== "undefined";
|
|
142
153
|
}
|
|
143
154
|
|
|
155
|
+
/** The `detail` tag {@link CommittedStoreUnreachableError} travels under across the worker bridge. */
|
|
156
|
+
export const COMMITTED_STORE_UNREACHABLE_CODE = "committed-store-unreachable";
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* The clone-safe wire form of a {@link CommittedStoreUnreachableError}, carried in the EXISTING bridge error
|
|
160
|
+
* `detail` field (the `{ message, name, detail }` shape `serializeError` produces) — no new protocol field.
|
|
161
|
+
* The attach side calls {@link committedStoreUnreachableFromWire} on every bridge error `detail`, so a refusal
|
|
162
|
+
* raised in an engine home reaches the tab as the CLASS, not a name-tagged plain `Error`.
|
|
163
|
+
*/
|
|
164
|
+
export interface CommittedStoreUnreachableWire {
|
|
165
|
+
code: typeof COMMITTED_STORE_UNREACHABLE_CODE;
|
|
166
|
+
storePath: string;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Thrown when a boot whose engine home holds NO OPFS sync-access grant meets a store whose meta record says
|
|
171
|
+
* `opfs-committed` — the user's real store lives in the OPFS backend, which this home cannot open. Left
|
|
172
|
+
* unguarded that boot opens `idb://<storePath>` instead and MINTS AN EMPTY SIBLING at the same path: the app
|
|
173
|
+
* looks wiped, and any offline writes fork into a store no OPFS-capable boot ever opens. So the boot fails
|
|
174
|
+
* CLOSED, matching the ADR-0050 posture (never a silently different storage mode) and the
|
|
175
|
+
* {@link NonPersistentStoreError} / `StorageDeclarationRefusedError` precedents. There is deliberately no
|
|
176
|
+
* override: a committed store is only reachable from a home that can hold handles.
|
|
177
|
+
*
|
|
178
|
+
* A distinct type — not a bare `Error` — so a caller can `instanceof`-branch it from a genuine boot failure,
|
|
179
|
+
* and it survives the worker bridge AS THAT TYPE: the instance carries the clone-safe {@link
|
|
180
|
+
* CommittedStoreUnreachableWire} `detail` the bridge forwards, and the tab side reconstructs it through
|
|
181
|
+
* {@link committedStoreUnreachableFromWire} (the same tagged-detail pair the execution-limit and relocation
|
|
182
|
+
* errors use). `storePath` is on the instance too, because the remedy is path-addressed. The message names
|
|
183
|
+
* BOTH exits, because a consumer meeting this has to choose between them.
|
|
184
|
+
*/
|
|
185
|
+
export class CommittedStoreUnreachableError extends Error {
|
|
186
|
+
readonly code = COMMITTED_STORE_UNREACHABLE_CODE;
|
|
187
|
+
readonly storePath: string;
|
|
188
|
+
readonly detail: CommittedStoreUnreachableWire;
|
|
189
|
+
|
|
190
|
+
constructor(storePath: string) {
|
|
191
|
+
super(
|
|
192
|
+
`[pgxsinkit] refusing to boot ${JSON.stringify(storePath)} here: its store meta record says the store is ` +
|
|
193
|
+
"COMMITTED to the OPFS backend, and this boot's engine home holds no OPFS sync-access grant — so it " +
|
|
194
|
+
"cannot open that store. Booting the IndexedDB store at the same path would mint an EMPTY sibling: the " +
|
|
195
|
+
"app would look wiped and offline writes would fork into a store no OPFS-capable boot ever opens. " +
|
|
196
|
+
"Either boot from an engine home that HOLDS a grant (worker mode's elected engine worker, or the " +
|
|
197
|
+
"SharedWorker-direct home), or destroy the store first — call " +
|
|
198
|
+
`\`destroyStoreArtifacts(${JSON.stringify(storePath)})\` (exported from \`@pgxsinkit/client\`), which is ` +
|
|
199
|
+
"path-addressed and needs no grant: it deletes BOTH backends plus the commitment sentinel and the meta " +
|
|
200
|
+
"record (quiesce any live worker for the path first, `quiesceStoreWorker`) — and let the next boot " +
|
|
201
|
+
"rebuild it (ADR-0049 invariant 3; ADR-0050 never a silently different storage mode).",
|
|
202
|
+
);
|
|
203
|
+
this.name = "CommittedStoreUnreachableError";
|
|
204
|
+
this.storePath = storePath;
|
|
205
|
+
this.detail = { code: COMMITTED_STORE_UNREACHABLE_CODE, storePath };
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
144
209
|
/**
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
*
|
|
210
|
+
* Reconstruct a typed {@link CommittedStoreUnreachableError} from a bridge error's `detail`. STRICT shape
|
|
211
|
+
* check: returns `undefined` for anything that is not exactly the `{ code: "committed-store-unreachable",
|
|
212
|
+
* storePath }` wire form (a foreign/absent detail, a wrong code, a missing/non-string path, a non-object), so a
|
|
213
|
+
* different failure is never misclassified as this refusal. The single source of truth for the decoding —
|
|
214
|
+
* every bridge seam calls THIS, never its own shape sniff.
|
|
149
215
|
*/
|
|
150
|
-
export
|
|
216
|
+
export function committedStoreUnreachableFromWire(detail: unknown): CommittedStoreUnreachableError | undefined {
|
|
217
|
+
if (typeof detail !== "object" || detail === null) return undefined;
|
|
218
|
+
const candidate = detail as { code?: unknown; storePath?: unknown };
|
|
219
|
+
if (candidate.code !== COMMITTED_STORE_UNREACHABLE_CODE) return undefined;
|
|
220
|
+
if (typeof candidate.storePath !== "string") return undefined;
|
|
221
|
+
return new CommittedStoreUnreachableError(candidate.storePath);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* The PRE-MINT meta gate every probe-denied (no OPFS sync access) browser boot passes through — one bounded
|
|
226
|
+
* meta read serving both of the record's claims such a boot must honour, before any replacement IDB store is
|
|
227
|
+
* created:
|
|
228
|
+
*
|
|
229
|
+
* - **`opfs-committed` → REFUSE** ({@link CommittedStoreUnreachableError}). This home cannot open the
|
|
230
|
+
* committed store, and minting the idb sibling at the same path would publish an empty store over a
|
|
231
|
+
* populated one. Fail closed; the remedy is a deliberate `destroy()` or a granted engine home.
|
|
232
|
+
* - **`deleting` → complete the handoff.** The old cache is already explicitly destructible; the authority
|
|
233
|
+
* handoff required is to remove the OPFS commitment sentinel and the store directory, then publish
|
|
234
|
+
* `idb-authoritative` before the replacement is exposed. The directory goes NOW, with the rest: the
|
|
235
|
+
* replacement is an idb store for life (the backend is fixed at first mint), so no later OPFS candidate
|
|
236
|
+
* would ever sweep it. Returns `true`.
|
|
237
|
+
* - **`opfs-candidate` → retire the candidate.** An unexposed candidate has no authority (the classifier's own
|
|
238
|
+
* rule), and this home cannot open OPFS at all — so it is retired rather than left standing over the idb
|
|
239
|
+
* store about to be minted: delete the commitment sentinel (a barrier-gap crash can have published one) and
|
|
240
|
+
* the store directory — async OPFS deletion needs no sync-access grant — then publish `idb-authoritative`.
|
|
241
|
+
* Returns `true`.
|
|
242
|
+
* - **Everything else** (no record, `idb-authoritative`, or no meta store at all) → proceed untouched. A
|
|
243
|
+
* no-grant boot is otherwise record-blind by design: the opfs commitment phase machine belongs to the
|
|
244
|
+
* granted lane, and {@link META_STORE_UNAVAILABLE} (no IndexedDB — the Bun/Node filesystem lane) cannot hold
|
|
245
|
+
* a record at all, so there is nothing there to be blind to.
|
|
246
|
+
*
|
|
247
|
+
* An UNREADABLE record propagates ({@link StoreMetaUnreadableError}) — a failed meta read is an error, never
|
|
248
|
+
* "no record" (invariant 12).
|
|
249
|
+
*/
|
|
250
|
+
export async function resolveDeniedBootAuthority(
|
|
151
251
|
storePath: string,
|
|
152
252
|
deps?: ResolveStoreBootOptions["deps"],
|
|
153
253
|
): Promise<boolean> {
|
|
154
254
|
const meta = deps?.meta;
|
|
155
255
|
const record = await readStoreMetaRecord(storePath, meta);
|
|
156
|
-
if (record === META_STORE_UNAVAILABLE
|
|
256
|
+
if (record === META_STORE_UNAVAILABLE) return false;
|
|
257
|
+
// The committed refusal rides THIS read — a no-grant boot never gets a second look at the record, and the
|
|
258
|
+
// hazard (an empty idb sibling minted over a committed store) is decided by exactly this phase.
|
|
259
|
+
const phase = record?.phase;
|
|
260
|
+
if (phase === "opfs-committed") throw new CommittedStoreUnreachableError(storePath);
|
|
261
|
+
if (phase !== "deleting" && phase !== "opfs-candidate") return false;
|
|
157
262
|
|
|
158
263
|
const effects = createOpfsEffects(storePath, deps?.opfs);
|
|
159
|
-
//
|
|
160
|
-
//
|
|
264
|
+
// Both arms end by publishing idb authority over the commitment namespace, so both need it OBSERVABLE: if it
|
|
265
|
+
// cannot be observed, sentinel deletion cannot be confirmed. Keep the recorded phase authoritative and fail
|
|
266
|
+
// closed rather than publish a conflicting replacement.
|
|
161
267
|
if ((await effects.observeCommitmentNamespace()) === "unobservable") {
|
|
162
268
|
throw new Error(
|
|
163
|
-
`[pgxsinkit] cannot
|
|
164
|
-
|
|
269
|
+
`[pgxsinkit] cannot settle ${JSON.stringify(storePath)} for an IDB boot (record phase ` +
|
|
270
|
+
`${JSON.stringify(phase)}): the OPFS commitment namespace is unobservable in this scope, so sentinel ` +
|
|
271
|
+
"removal cannot be confirmed.",
|
|
165
272
|
);
|
|
166
273
|
}
|
|
167
|
-
|
|
274
|
+
// The idb database is deleted only on the `deleting` arm — that is the destruction this boot inherited and
|
|
275
|
+
// completes. A candidate's retirement destroys the CANDIDATE, never a store at the path it did not mint.
|
|
276
|
+
if (phase === "deleting") await deleteIdbDatabase(storePath, meta);
|
|
168
277
|
await effects.deleteSentinel();
|
|
278
|
+
await effects.deleteStoreDirectory();
|
|
169
279
|
await writeStoreMetaRecord(storePath, { phase: "idb-authoritative", updatedAt: Date.now() }, meta);
|
|
170
280
|
return true;
|
|
171
281
|
}
|
|
172
282
|
|
|
173
283
|
/**
|
|
174
|
-
* Resolve where a store boots and finish any destructive/candidate
|
|
175
|
-
*
|
|
176
|
-
* EXECUTED:
|
|
284
|
+
* Resolve where a store boots and finish any destructive/candidate work the verdict demands, then return the
|
|
285
|
+
* `dataDir` + `storageBackend` the mint seam opens at. The full plan boot classification 1–6, EXECUTED:
|
|
177
286
|
*
|
|
178
287
|
* - **memory override** → `memory://` passthrough, no classification (the sanctioned test/ephemeral lane has
|
|
179
288
|
* no meta machinery).
|
|
@@ -186,15 +295,17 @@ export async function recoverDeniedBootDeletion(
|
|
|
186
295
|
* - `resume-deletion` → complete the destructive lifecycle, then RE-CLASSIFY from the now-clean state
|
|
187
296
|
* (bounded by {@link MAX_DELETION_RECLASSIFY}).
|
|
188
297
|
* - `delete-candidate-and-rebuild` → delete the stale sentinel AND the candidate directory (a barrier-gap
|
|
189
|
-
* crash's sentinel must never survive), then
|
|
298
|
+
* crash's sentinel must never survive) AND the record, then RE-CLASSIFY from the now-recordless state — so
|
|
299
|
+
* an idb store at this path is opened in place rather than shadowed by the rebuild.
|
|
190
300
|
* - `repair-record-then-open-committed` → write `opfs-committed`, then open committed.
|
|
191
301
|
* - `open-committed` → open the committed opfs store (open failures are HARD at mint time; the bounded
|
|
192
|
-
* retries for transient UnknownError-class failures live in the mint seam's factory-call wrapper).
|
|
302
|
+
* retries for transient UnknownError-class failures live in the mint seam's factory-call wrapper). A record
|
|
303
|
+
* already at `opfs-committed` takes the WARM FAST PATH: it is classified straight off the record, so neither
|
|
304
|
+
* the commitment-namespace observation nor the recordless-idb probe runs at all (both are irrelevant to
|
|
305
|
+
* classification 2).
|
|
193
306
|
* - `boot-idb-authoritative` → write `idb-authoritative` FIRST when there is no record yet (recordless idb), then
|
|
194
|
-
* `idb://`.
|
|
195
|
-
*
|
|
196
|
-
* (`opfs-committed` + delete the idb predecessor) → `opfs://`; `teardown-and-restart` tears the candidate
|
|
197
|
-
* down, sets `idb-authoritative`, and boots `idb://` (adoption re-runs later, plan step 11).
|
|
307
|
+
* `idb://`. TERMINAL: a store's backend is fixed at first mint, so an idb store stays idb whatever this
|
|
308
|
+
* boot's capabilities are — the only route to another backend is a deliberate destroy + a fresh boot.
|
|
198
309
|
* - `virgin-create` → with opfs access, {@link beginFreshCandidate} (record `opfs-candidate` BEFORE the
|
|
199
310
|
* directory) → `opfs://` UNCOMMITTED (barrier is step 10b/11); without opfs access, `idb-authoritative` →
|
|
200
311
|
* `idb://`.
|
|
@@ -229,7 +340,8 @@ export async function resolveStoreBoot(storePath: string, opts: ResolveStoreBoot
|
|
|
229
340
|
const writePhase = (phase: StoreMetaPhase): Promise<void> =>
|
|
230
341
|
writeStoreMetaRecord(storePath, { phase, updatedAt: Date.now() }, meta);
|
|
231
342
|
|
|
232
|
-
// The virgin creation path —
|
|
343
|
+
// The virgin creation path — the one place this module mints a store (a retired candidate's rebuild reaches
|
|
344
|
+
// it through re-classification, like any other virgin state).
|
|
233
345
|
const finishVirginCreate = async (verdict: StoreBootVerdict): Promise<StoreBootResolution> => {
|
|
234
346
|
if (opts.hasOpfsSyncAccess) {
|
|
235
347
|
// Record-first authority (invariant 12): the `opfs-candidate` record is written BEFORE the directory, so
|
|
@@ -248,7 +360,7 @@ export async function resolveStoreBoot(storePath: string, opts: ResolveStoreBoot
|
|
|
248
360
|
return { dataDir: idbDataDir, storageBackend: "idbfs", verdict };
|
|
249
361
|
};
|
|
250
362
|
|
|
251
|
-
//
|
|
363
|
+
// The two record-clearing verdicts loop; every other verdict returns. Cleared record → a terminal verdict.
|
|
252
364
|
for (let iteration = 0; iteration < MAX_DELETION_RECLASSIFY; iteration += 1) {
|
|
253
365
|
// A failed meta read is an ERROR, never "no record" (invariant 12): StoreMetaUnreadableError propagates
|
|
254
366
|
// here and fails the boot closed.
|
|
@@ -258,6 +370,24 @@ export async function resolveStoreBoot(storePath: string, opts: ResolveStoreBoot
|
|
|
258
370
|
// false). Faithful to store-meta's documented mapping.
|
|
259
371
|
const metaUnavailable = metaResult === META_STORE_UNAVAILABLE;
|
|
260
372
|
const record = metaUnavailable ? undefined : metaResult;
|
|
373
|
+
|
|
374
|
+
// WARM COMMITTED FAST PATH. A record at `opfs-committed` DETERMINES the verdict: classification 2 says the
|
|
375
|
+
// OPFS observations are IRRELEVANT on that arm (a committed store never re-derives its verdict from OPFS),
|
|
376
|
+
// and the classifier consults `idbStoreExists` only on the recordless arm (classification 6). So on the
|
|
377
|
+
// warmest and by far most frequent boot, NEITHER of the two remaining probes is a boot dependency: the
|
|
378
|
+
// ~8-handle commitment-namespace observation and the IndexedDB open→onupgradeneeded→abort existence cycle
|
|
379
|
+
// both come OFF the critical path. The verdict is still derived by the classifier — store-meta owns
|
|
380
|
+
// classification, this module never hardcodes one — with the observations it documents as irrelevant here;
|
|
381
|
+
// if it ever answered anything but `open-committed` for this phase, the guard below falls through to the
|
|
382
|
+
// full observe-then-classify path unchanged.
|
|
383
|
+
const committedFastVerdict =
|
|
384
|
+
record?.phase === "opfs-committed"
|
|
385
|
+
? classifyStoreBoot({ record, opfs: "unobservable", idbStoreExists: false })
|
|
386
|
+
: undefined;
|
|
387
|
+
if (committedFastVerdict?.action === "open-committed") {
|
|
388
|
+
return { dataDir: opfsDataDir, storageBackend: "opfs-repacked", verdict: committedFastVerdict };
|
|
389
|
+
}
|
|
390
|
+
|
|
261
391
|
const opfsObservation = await effects.observeCommitmentNamespace();
|
|
262
392
|
const idbPresent = metaUnavailable ? false : await idbExists(storePath);
|
|
263
393
|
const verdict = classifyStoreBoot({ record, opfs: opfsObservation, idbStoreExists: idbPresent });
|
|
@@ -279,52 +409,34 @@ export async function resolveStoreBoot(storePath: string, opts: ResolveStoreBoot
|
|
|
279
409
|
continue;
|
|
280
410
|
|
|
281
411
|
case "delete-candidate-and-rebuild":
|
|
282
|
-
// An unexposed candidate has no authority
|
|
283
|
-
// barrier-gap crash's published sentinel must never survive into the rebuilt candidate's
|
|
284
|
-
// (plan fresh/restore crash table). Then
|
|
412
|
+
// An unexposed candidate has no authority, so RETIRE it: the stale sentinel MUST go alongside the
|
|
413
|
+
// directory — a barrier-gap crash's published sentinel must never survive into the rebuilt candidate's
|
|
414
|
+
// lifetime (plan fresh/restore crash table) — and the record goes with them. Then re-classify from the
|
|
415
|
+
// now-recordless state, exactly as `resume-deletion` does. That re-read is what keeps the rebuild
|
|
416
|
+
// honest: a store's backend is fixed at first mint, so an existing idb store at this path lands on
|
|
417
|
+
// classification 6 and is opened IN PLACE, never shadowed by a fresh opfs mint. A truly clean state
|
|
418
|
+
// reaches `virgin-create` and rebuilds.
|
|
285
419
|
await effects.deleteSentinel();
|
|
286
420
|
await effects.deleteStoreDirectory();
|
|
287
|
-
|
|
421
|
+
await deleteStoreMetaRecord(storePath, meta);
|
|
422
|
+
continue;
|
|
288
423
|
|
|
289
424
|
case "repair-record-then-open-committed":
|
|
290
425
|
// Sentinel present without a record (record loss): the sentinel is committed authority. Repair the
|
|
291
426
|
// record, then open committed.
|
|
292
427
|
await writePhase("opfs-committed");
|
|
293
|
-
if (idbPresent) await deleteIdbDatabase(storePath, meta);
|
|
294
428
|
return { dataDir: opfsDataDir, storageBackend: "opfs-repacked", verdict };
|
|
295
429
|
|
|
296
430
|
case "open-committed":
|
|
297
|
-
//
|
|
298
|
-
// idempotently before publishing the committed store to this boot.
|
|
299
|
-
if (idbPresent) await deleteIdbDatabase(storePath, meta);
|
|
431
|
+
// The record is already the authority for a committed store: open it, write nothing, touch nothing.
|
|
300
432
|
return { dataDir: opfsDataDir, storageBackend: "opfs-repacked", verdict };
|
|
301
433
|
|
|
302
434
|
case "boot-idb-authoritative":
|
|
303
435
|
// Recordless idb store (no record yet, an existing idb store): write the record FIRST (invariant 14). A
|
|
304
|
-
// present `idb-authoritative` record is left as-is.
|
|
436
|
+
// present `idb-authoritative` record is left as-is. TERMINAL — the backend is fixed at first mint.
|
|
305
437
|
if (record == null) await writePhase("idb-authoritative");
|
|
306
438
|
return { dataDir: idbDataDir, storageBackend: "idbfs", verdict };
|
|
307
439
|
|
|
308
|
-
case "adoption-recovery": {
|
|
309
|
-
// Disambiguate purely from the sentinel (plan adoption crash rows). "unobservable" ⇒ no sentinel.
|
|
310
|
-
const sentinelPresent = opfsObservation !== "unobservable" && opfsObservation.sentinelPresent;
|
|
311
|
-
const recovery = classifyAdoptionRecovery({ sentinelPresent });
|
|
312
|
-
if (recovery.action === "complete-adoption") {
|
|
313
|
-
// The barrier had published before the crash: set `opfs-committed`, delete the idb predecessor.
|
|
314
|
-
await completeAdoption({
|
|
315
|
-
setPhase: writePhase,
|
|
316
|
-
deleteIdbPredecessor: () => deleteIdbDatabase(storePath, meta),
|
|
317
|
-
});
|
|
318
|
-
return { dataDir: opfsDataDir, storageBackend: "opfs-repacked", verdict };
|
|
319
|
-
}
|
|
320
|
-
// Nothing committed: tear the candidate down and fall back to the still-authoritative idb store.
|
|
321
|
-
// Adoption re-runs later from step 1 (plan step 11).
|
|
322
|
-
await effects.deleteSentinel();
|
|
323
|
-
await effects.deleteStoreDirectory();
|
|
324
|
-
await writePhase("idb-authoritative");
|
|
325
|
-
return { dataDir: idbDataDir, storageBackend: "idbfs", verdict };
|
|
326
|
-
}
|
|
327
|
-
|
|
328
440
|
case "virgin-create":
|
|
329
441
|
return finishVirginCreate(verdict);
|
|
330
442
|
|
package/src/store-lifecycle.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
// Store lifecycle machines — ADR-0049 (capability-driven engine placement) step 3. The fresh/restore
|
|
2
|
-
// COMMITMENT BARRIER (decision 7)
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
// "Adoption gate", "Adoption-bootstrap gate", "Drain predicate", "Destructive lifecycle".
|
|
2
|
+
// COMMITMENT BARRIER (decision 7) and the DESTRUCTIVE lifecycle (decision 8) as PURE, EFFECT-DRIVEN modules
|
|
3
|
+
// over the plan's crash tables. CONTEXT § "Language — engine placement" terms used exactly: "Commitment
|
|
4
|
+
// marker", "Destructive lifecycle".
|
|
6
5
|
//
|
|
7
6
|
// EFFECT-INJECTED BY CONSTRUCTION. None of these machines touches IndexedDB, OPFS, or PGlite: every side
|
|
8
7
|
// effect is a method on an injected effects object. Plan step 10 wires the REAL effects (the meta record IO
|
|
@@ -11,11 +10,10 @@
|
|
|
11
10
|
// the observed state. Because of that, EVERY step must be IDEMPOTENT under resume — each effect's contract
|
|
12
11
|
// below states the idempotency the wirer must provide (delete-if-present, create-if-absent, set-phase-again).
|
|
13
12
|
//
|
|
14
|
-
// The
|
|
15
|
-
//
|
|
16
|
-
//
|
|
17
|
-
//
|
|
18
|
-
// SEQUENCE, not the gates that authorize entering it.
|
|
13
|
+
// The PROVENANCE GATES themselves (fresh = successful local initialization/recovery; restore = successful
|
|
14
|
+
// backup load + restore recovery) are CALLER facts, not modelled here: the caller invokes the barrier only
|
|
15
|
+
// AFTER its provenance gate has passed. This module owns the shared data-before-authority SEQUENCE, not the
|
|
16
|
+
// gates that authorize entering it.
|
|
19
17
|
|
|
20
18
|
import type { StoreMetaPhase } from "./store-meta";
|
|
21
19
|
|
|
@@ -25,8 +23,8 @@ import type { StoreMetaPhase } from "./store-meta";
|
|
|
25
23
|
|
|
26
24
|
/**
|
|
27
25
|
* The effects the shared commitment barrier drives, in the one order it always runs them. The barrier is the
|
|
28
|
-
* SINGLE data-before-authority sequence every provenance shares (fresh, restore
|
|
29
|
-
*
|
|
26
|
+
* SINGLE data-before-authority sequence every provenance shares (fresh, restore): the caller has already
|
|
27
|
+
* passed its provenance gate; the barrier makes the data durable, THEN publishes authority.
|
|
30
28
|
*/
|
|
31
29
|
export interface CommitmentBarrierEffects {
|
|
32
30
|
/**
|
|
@@ -53,10 +51,9 @@ export interface CommitmentBarrierEffects {
|
|
|
53
51
|
/**
|
|
54
52
|
* Run the shared commitment barrier (ADR-0049 decision 7): `strictSync()` returns with VFS health good →
|
|
55
53
|
* publish the sentinel → set the `opfs-committed` phase. The caller invokes this ONLY after its provenance
|
|
56
|
-
* gate has passed (fresh: local init/recovery; restore: backup load + restore recovery
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
* exposes the store (fresh/restore) or deletes the predecessor (adoption).
|
|
54
|
+
* gate has passed (fresh: local init/recovery; restore: backup load + restore recovery). Any throw propagates
|
|
55
|
+
* and later steps NEVER run — so a failed barrier publishes NOTHING and an uncommitted candidate is never
|
|
56
|
+
* granted authority (invariant 3). After it resolves the caller exposes the store.
|
|
60
57
|
*/
|
|
61
58
|
export async function runCommitmentBarrier(effects: CommitmentBarrierEffects): Promise<void> {
|
|
62
59
|
await effects.strictSyncReturns();
|
|
@@ -144,84 +141,3 @@ export async function runDestruction(effects: DestructionEffects): Promise<void>
|
|
|
144
141
|
await effects.setPhase("deleting");
|
|
145
142
|
await resumeDeletion(effects);
|
|
146
143
|
}
|
|
147
|
-
|
|
148
|
-
// =========================================================================================================
|
|
149
|
-
// C. Adoption recovery (pure verdict over the plan's adoption crash rows) + completion executor
|
|
150
|
-
// =========================================================================================================
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* The recovery verdict for a store found mid-adoption (phase `adopting`) at boot. Disambiguated purely by
|
|
154
|
-
* whether the commitment sentinel was published before the crash (plan's adoption crash table).
|
|
155
|
-
*/
|
|
156
|
-
export type AdoptionRecoveryVerdict =
|
|
157
|
-
// Sentinel present: the barrier committed before the crash — set `opfs-committed`, delete the idb
|
|
158
|
-
// predecessor, done ({@link completeAdoption}).
|
|
159
|
-
| { action: "complete-adoption" }
|
|
160
|
-
// No sentinel: nothing committed — tear the candidate down and re-run adoption from step 1 (the idb store
|
|
161
|
-
// is still authoritative, so no write is stranded).
|
|
162
|
-
| { action: "teardown-and-restart" };
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Classify an interrupted adoption purely from the observed sentinel presence (ADR-0049 D7 adoption sequence;
|
|
166
|
-
* plan adoption crash rows). Sentinel present → `complete-adoption` (the commitment barrier had published,
|
|
167
|
-
* so the opfs store is committed); sentinel absent → `teardown-and-restart` (idb remains authoritative — the
|
|
168
|
-
* predecessor is deleted ONLY after commitment, so nothing is lost). This owns RECOVERY only; the full
|
|
169
|
-
* forward adoption transition (pre-expose idb boot, the drain predicate, strict-sync close of the
|
|
170
|
-
* never-exposed engine, phase `adopting`, server bootstrap through the Adoption-bootstrap gate) is plan
|
|
171
|
-
* step 11's wiring.
|
|
172
|
-
*/
|
|
173
|
-
export function classifyAdoptionRecovery(obs: { sentinelPresent: boolean }): AdoptionRecoveryVerdict {
|
|
174
|
-
return obs.sentinelPresent ? { action: "complete-adoption" } : { action: "teardown-and-restart" };
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
/** The effects the adoption completion executor drives (the `complete-adoption` verdict). */
|
|
178
|
-
export interface AdoptionCompletionEffects {
|
|
179
|
-
/** Advance the meta record phase; the executor only ever calls it with `"opfs-committed"`. Idempotency: set-again. */
|
|
180
|
-
setPhase(phase: StoreMetaPhase): Promise<void>;
|
|
181
|
-
/** Delete the idb predecessor now the opfs store is committed. Idempotency required: absent counts as deleted. */
|
|
182
|
-
deleteIdbPredecessor(): Promise<void>;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* Complete an adoption whose commitment barrier already published the sentinel (the `complete-adoption`
|
|
187
|
-
* verdict): set `opfs-committed`, then delete the idb predecessor. Ordered committed-flag-BEFORE-deletion so
|
|
188
|
-
* a crash between them re-enters as `opfs-committed` with the idb store lingering — the committed-boot path
|
|
189
|
-
* idempotently finishes the deletion (plan adoption crash row 3). The predecessor is deleted ONLY after
|
|
190
|
-
* commitment, never before (invariant 4 — no silent local-only data loss).
|
|
191
|
-
*/
|
|
192
|
-
export async function completeAdoption(effects: AdoptionCompletionEffects): Promise<void> {
|
|
193
|
-
await effects.setPhase("opfs-committed");
|
|
194
|
-
await effects.deleteIdbPredecessor();
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
// =========================================================================================================
|
|
198
|
-
// D. Drain predicate (canonical journal status terms — CONTEXT "Drain predicate")
|
|
199
|
-
// =========================================================================================================
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* The per-status row counts of the Mutation journal the drain predicate reads. Names + semantics mirror the
|
|
203
|
-
* canonical journal status machine in `packages/contracts/src/mutation.ts` (`mutationStatusSchema`), but this
|
|
204
|
-
* takes COUNTS as plain input rather than coupling to the journal schema — the predicate is pure and
|
|
205
|
-
* unit-testable without a journal.
|
|
206
|
-
*/
|
|
207
|
-
export interface JournalStatusCounts {
|
|
208
|
-
pending: number;
|
|
209
|
-
sending: number;
|
|
210
|
-
acked: number;
|
|
211
|
-
failed: number;
|
|
212
|
-
quarantined: number;
|
|
213
|
-
conflicted: number;
|
|
214
|
-
rejected: number;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* The DRAIN PREDICATE (CONTEXT § "Drain predicate"; ADR-0049 D7): the idb store owes nothing to the server
|
|
219
|
-
* iff no row is `pending`, `sending`, `failed`, `quarantined`, or `conflicted`. `acked` and `rejected` rows
|
|
220
|
-
* are PERMITTED — both are settled (rejection is terminal, ADR-0022). Evaluated PRE-EXPOSE in the adoption
|
|
221
|
-
* transition so no mutation can race it. Echo-landing is deliberately NOT required (the opfs successor is
|
|
222
|
-
* rebuilt from the server, which already holds every acked write) — this is weaker than Data export's
|
|
223
|
-
* "drained", which reads local tables.
|
|
224
|
-
*/
|
|
225
|
-
export function journalOwesNothing(counts: JournalStatusCounts): boolean {
|
|
226
|
-
return counts.pending + counts.sending + counts.failed + counts.quarantined + counts.conflicted === 0;
|
|
227
|
-
}
|