@pgxsinkit/client 0.2.2 → 0.2.3
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 +254 -391
- package/dist/index.js.map +13 -14
- 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/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/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/dist/adoption.d.ts
DELETED
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
import { type JournalStatusCounts } from "./store-lifecycle";
|
|
2
|
-
import type { StoreBootVerdict, StoreMetaPhase } from "./store-meta";
|
|
3
|
-
/**
|
|
4
|
-
* The consumer's explicit reconstructibility declaration (ADR-0049 D7). `"server-reconstructible"` authorizes
|
|
5
|
-
* AUTOMATIC adoption: the store is server-reconstructible and its local-only data is disposable. DEFAULT OFF
|
|
6
|
-
* (`undefined`) — hook absence is NEVER authority: `rawExec` writes documented local-only state on any store,
|
|
7
|
-
* so only this explicit declaration (or the manual adoption API) authorizes deleting the idb predecessor.
|
|
8
|
-
*/
|
|
9
|
-
export type AdoptionDeclaration = "server-reconstructible" | undefined;
|
|
10
|
-
/**
|
|
11
|
-
* The effects the adoption transition drives, in the one order it always runs them (step 11b supplies the real
|
|
12
|
-
* implementations). The transition is a strict data-before-authority sequence: prove the idb store owes the
|
|
13
|
-
* server nothing, reconstruct an opfs successor through the Adoption-bootstrap gate, commit it via the shared
|
|
14
|
-
* barrier, and only THEN delete the predecessor.
|
|
15
|
-
*/
|
|
16
|
-
export interface AdoptionEffects {
|
|
17
|
-
/**
|
|
18
|
-
* Boot the idb engine PRE-EXPOSE (never surfaced to tabs — invariant 3) and return the journal status
|
|
19
|
-
* counts the drain predicate reads. The engine is booted solely to read the journal; the caller never
|
|
20
|
-
* exposes THIS booted instance until adoption defers (journal owed) or fails.
|
|
21
|
-
*/
|
|
22
|
-
bootIdbPreExpose(): Promise<JournalStatusCounts>;
|
|
23
|
-
/** Strict-sync the never-exposed idb engine, then close it (all handles released) before building the successor. */
|
|
24
|
-
strictSyncAndCloseIdb(): Promise<void>;
|
|
25
|
-
/** Advance the store meta record phase. The transition calls it with `"adopting"` and, on failure, `"idb-authoritative"`. */
|
|
26
|
-
setPhase(phase: StoreMetaPhase): Promise<void>;
|
|
27
|
-
/**
|
|
28
|
-
* Build the opfs candidate via the normal server bootstrap path AND await the Adoption-bootstrap gate:
|
|
29
|
-
* authorized online reconstruction — the initial catch-up of the eager Consistency groups a valid initial
|
|
30
|
-
* store requires ("authorized" includes legitimately anonymous/public shapes). Resolves ONLY when the gate
|
|
31
|
-
* passed; THROWS on gate failure (offline, unauthorized, `syncEnabled: false`, or a bootstrap error).
|
|
32
|
-
*/
|
|
33
|
-
buildCandidateThroughGate(): Promise<void>;
|
|
34
|
-
/**
|
|
35
|
-
* Run the shared commitment barrier on the CANDIDATE engine: `strictSync()` returns with VFS health good →
|
|
36
|
-
* sentinel → `opfs-committed`. Step 11b wires `runCommitmentBarrier` (store-lifecycle.ts) with real effects;
|
|
37
|
-
* here it is one injected step. It MUST be all-or-nothing: a barrier failure publishes NOTHING (invariant 3),
|
|
38
|
-
* so a throw leaves the idb store authoritative and the candidate uncommitted.
|
|
39
|
-
*/
|
|
40
|
-
commitCandidate(): Promise<void>;
|
|
41
|
-
/** Delete the idb predecessor now the opfs successor is committed. Idempotent: absent counts as deleted. */
|
|
42
|
-
deleteIdbPredecessor(): Promise<void>;
|
|
43
|
-
/**
|
|
44
|
-
* Tear down a failed candidate: delete the stale sentinel (if any) AND the candidate directory,
|
|
45
|
-
* delete-if-present (a barrier-gap crash's sentinel must never survive). Recreate-only; idempotent.
|
|
46
|
-
*/
|
|
47
|
-
teardownCandidate(): Promise<void>;
|
|
48
|
-
}
|
|
49
|
-
/** The narrow candidate surface the real boot wiring hands this module. */
|
|
50
|
-
export interface AdoptionCandidateHandle {
|
|
51
|
-
ready: Promise<void>;
|
|
52
|
-
strictSync(): Promise<void>;
|
|
53
|
-
close(): Promise<void>;
|
|
54
|
-
}
|
|
55
|
-
/** IO callbacks needed to construct the real stateful adoption effects without importing the client entrypoint. */
|
|
56
|
-
export interface AdoptionEffectBuilderOptions {
|
|
57
|
-
readIdbJournal(): Promise<JournalStatusCounts>;
|
|
58
|
-
setPhase(phase: StoreMetaPhase): Promise<void>;
|
|
59
|
-
buildCandidate(): Promise<AdoptionCandidateHandle>;
|
|
60
|
-
publishSentinel(): Promise<void>;
|
|
61
|
-
deleteSentinel(): Promise<void>;
|
|
62
|
-
deleteStoreDirectory(): Promise<void>;
|
|
63
|
-
deleteIdbPredecessor(): Promise<void>;
|
|
64
|
-
gateDeadlineMs?: number;
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Construct the stateful adoption effects around narrow IO callbacks. Candidate ownership, gate timing,
|
|
68
|
-
* data-before-authority publication, and failed-candidate cleanup live here rather than in the package entrypoint.
|
|
69
|
-
*/
|
|
70
|
-
export declare function buildAdoptionEffects(options: AdoptionEffectBuilderOptions): AdoptionEffects;
|
|
71
|
-
/**
|
|
72
|
-
* The outcome of an adoption transition. `adopted: true` means the opfs successor is committed and the idb
|
|
73
|
-
* predecessor is gone (or scheduled for cleanup — see `predecessorCleanupPending`). Every `adopted: false`
|
|
74
|
-
* outcome leaves the idb store AUTHORITATIVE and untouched:
|
|
75
|
-
* - `not-declared` — automatic adoption without the declaration (default off); nothing ran (invariant 4).
|
|
76
|
-
* - `journal-owed` — the drain predicate failed; adoption is DEFERRED (the caller exposes the idb engine
|
|
77
|
-
* normally). The orchestrator boots the idb engine to READ the journal but exposes nothing itself.
|
|
78
|
-
* - `gate-failed` — the Adoption-bootstrap gate rejected (offline/unauthorized/`syncEnabled: false`/bootstrap
|
|
79
|
-
* error). The candidate was torn down; idb stays authoritative. `error` carries the gate failure text.
|
|
80
|
-
* - `barrier-failed` — the commitment barrier threw (strict sync did not return / VFS health bad). Nothing
|
|
81
|
-
* published; candidate torn down; idb stays authoritative. `error` carries the barrier failure text.
|
|
82
|
-
*/
|
|
83
|
-
export type AdoptionOutcome = {
|
|
84
|
-
adopted: true;
|
|
85
|
-
/**
|
|
86
|
-
* Set only when the predecessor deletion FAILED after a successful commit. Adoption still SUCCEEDED
|
|
87
|
-
* (the phase is `opfs-committed`); the boot classifier's lingering-idb row (adoption crash-row 3 →
|
|
88
|
-
* `opfs-committed` with an idb store present → `open-committed`, then `completeAdoption` finishes the
|
|
89
|
-
* deletion) completes the cleanup on the next boot. Carries the deletion failure text for diagnostics.
|
|
90
|
-
*/
|
|
91
|
-
predecessorCleanupPending?: string;
|
|
92
|
-
} | {
|
|
93
|
-
adopted: false;
|
|
94
|
-
reason: "not-declared" | "journal-owed" | "gate-failed" | "barrier-failed";
|
|
95
|
-
error?: string;
|
|
96
|
-
};
|
|
97
|
-
/**
|
|
98
|
-
* The AUTOMATIC adoption transition, run at boot BEFORE any engine is exposed (the caller guarantees pre-expose
|
|
99
|
-
* exclusivity — invariant 3's no-race-by-construction). Consults the {@link AdoptionDeclaration} FIRST: without
|
|
100
|
-
* `"server-reconstructible"` it refuses with `not-declared` and runs ZERO effects (invariant 4 — default off,
|
|
101
|
-
* hook absence is never authority). With the declaration it runs the shared {@link runTransition}.
|
|
102
|
-
*/
|
|
103
|
-
export declare function runAdoptionTransition(declaration: AdoptionDeclaration, effects: AdoptionEffects): Promise<AdoptionOutcome>;
|
|
104
|
-
/**
|
|
105
|
-
* The MANUAL adoption API's core: the identical transition, but WITHOUT the declaration check — the consumer's
|
|
106
|
-
* explicit call IS the authorization (they were told to export/migrate any local-only data first). The drain
|
|
107
|
-
* predicate still gates it (a store that owes the server work is not adoptable), and every failure recovery is
|
|
108
|
-
* identical to the automatic path.
|
|
109
|
-
*/
|
|
110
|
-
export declare function runManualAdoption(effects: AdoptionEffects): Promise<AdoptionOutcome>;
|
|
111
|
-
/**
|
|
112
|
-
* The boot-wiring predicate (plan step 11b): is a boot eligible to START an automatic adoption? True IFF the
|
|
113
|
-
* consumer declared `"server-reconstructible"` AND opfs sync access is available in this scope AND the boot
|
|
114
|
-
* classifier landed on `boot-idb-authoritative` — adoption only ever starts from an idb-authoritative store
|
|
115
|
-
* with opfs available (there is nowhere to build the successor otherwise, and no other verdict is a candidate).
|
|
116
|
-
*/
|
|
117
|
-
export declare function adoptionEligible(declaration: AdoptionDeclaration, verdict: StoreBootVerdict, hasOpfsSyncAccess: boolean): boolean;
|
|
118
|
-
//# sourceMappingURL=adoption.d.ts.map
|
package/dist/adoption.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"adoption.d.ts","sourceRoot":"","sources":["../src/adoption.ts"],"names":[],"mappings":"AAoBA,OAAO,EAA4C,KAAK,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AACvG,OAAO,KAAK,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAErE;;;;;GAKG;AACH,MAAM,MAAM,mBAAmB,GAAG,wBAAwB,GAAG,SAAS,CAAC;AAEvE;;;;;GAKG;AACH,MAAM,WAAW,eAAe;IAC9B;;;;OAIG;IACH,gBAAgB,IAAI,OAAO,CAAC,mBAAmB,CAAC,CAAC;IACjD,oHAAoH;IACpH,qBAAqB,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACvC,6HAA6H;IAC7H,QAAQ,CAAC,KAAK,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC/C;;;;;OAKG;IACH,yBAAyB,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAC3C;;;;;OAKG;IACH,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACjC,4GAA4G;IAC5G,oBAAoB,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACtC;;;OAGG;IACH,iBAAiB,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACpC;AAED,2EAA2E;AAC3E,MAAM,WAAW,uBAAuB;IACtC,KAAK,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IACrB,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5B,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB;AAED,mHAAmH;AACnH,MAAM,WAAW,4BAA4B;IAC3C,cAAc,IAAI,OAAO,CAAC,mBAAmB,CAAC,CAAC;IAC/C,QAAQ,CAAC,KAAK,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC/C,cAAc,IAAI,OAAO,CAAC,uBAAuB,CAAC,CAAC;IACnD,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACjC,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAChC,oBAAoB,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACtC,oBAAoB,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACtC,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AA0BD;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,4BAA4B,GAAG,eAAe,CAiC3F;AAED;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,eAAe,GACvB;IACE,OAAO,EAAE,IAAI,CAAC;IACd;;;;;OAKG;IACH,yBAAyB,CAAC,EAAE,MAAM,CAAC;CACpC,GACD;IAAE,OAAO,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,cAAc,GAAG,cAAc,GAAG,aAAa,GAAG,gBAAgB,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AA4DnH;;;;;GAKG;AACH,wBAAgB,qBAAqB,CACnC,WAAW,EAAE,mBAAmB,EAChC,OAAO,EAAE,eAAe,GACvB,OAAO,CAAC,eAAe,CAAC,CAK1B;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,eAAe,CAAC,CAEpF;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAC9B,WAAW,EAAE,mBAAmB,EAChC,OAAO,EAAE,gBAAgB,EACzB,iBAAiB,EAAE,OAAO,GACzB,OAAO,CAET"}
|
package/src/adoption.ts
DELETED
|
@@ -1,275 +0,0 @@
|
|
|
1
|
-
// Adoption orchestrator + declaration surface — ADR-0049 (capability-driven engine placement) decision 7,
|
|
2
|
-
// plan step 11a. The automatic and manual adoption transitions that turn an existing idb-authoritative store
|
|
3
|
-
// into a committed opfs successor, expressed as ONE pure, effect-injected sequence over the plan's
|
|
4
|
-
// "Adoption sequence (exclusive, quiescent — D7)" and its adoption crash rows. CONTEXT § "Language — engine
|
|
5
|
-
// placement" terms are used exactly: "Adoption gate", "Adoption-bootstrap gate", "Drain predicate".
|
|
6
|
-
//
|
|
7
|
-
// EFFECT-INJECTED BY CONSTRUCTION. Like `store-lifecycle.ts`, this module touches NO IndexedDB, OPFS, or
|
|
8
|
-
// PGlite: every side effect is a method on an injected {@link AdoptionEffects}. Plan step 11b wires the REAL
|
|
9
|
-
// effects into the boot path (`createSyncClient` / `defineSyncWorker`) — the pre-expose idb boot, the drain
|
|
10
|
-
// read, the opfs candidate build via the normal server bootstrap through the Adoption-bootstrap gate, the
|
|
11
|
-
// shared commitment barrier (`runCommitmentBarrier` from `store-lifecycle.ts`), and the predecessor deletion.
|
|
12
|
-
// Here the orchestrator owns only the DECISION SEQUENCE and its failure recovery; wiring is NOT this step.
|
|
13
|
-
//
|
|
14
|
-
// INVARIANTS (ADR-0049): the transition runs at boot BEFORE any engine is exposed — the caller guarantees
|
|
15
|
-
// pre-expose exclusivity (invariant 3, no-race-by-construction), so the drain predicate cannot be raced by a
|
|
16
|
-
// concurrent mutation. Automatic adoption is default OFF and consults the declaration FIRST (invariant 4 — no
|
|
17
|
-
// silent local-only data loss; hook absence is never authority because `rawExec` writes documented local-only
|
|
18
|
-
// state on any store). The idb predecessor is deleted ONLY after the commitment barrier publishes
|
|
19
|
-
// (invariant 3/4 — commitment precedes exposure; nothing strands, nothing is silently lost).
|
|
20
|
-
|
|
21
|
-
import { journalOwesNothing, runCommitmentBarrier, type JournalStatusCounts } from "./store-lifecycle";
|
|
22
|
-
import type { StoreBootVerdict, StoreMetaPhase } from "./store-meta";
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* The consumer's explicit reconstructibility declaration (ADR-0049 D7). `"server-reconstructible"` authorizes
|
|
26
|
-
* AUTOMATIC adoption: the store is server-reconstructible and its local-only data is disposable. DEFAULT OFF
|
|
27
|
-
* (`undefined`) — hook absence is NEVER authority: `rawExec` writes documented local-only state on any store,
|
|
28
|
-
* so only this explicit declaration (or the manual adoption API) authorizes deleting the idb predecessor.
|
|
29
|
-
*/
|
|
30
|
-
export type AdoptionDeclaration = "server-reconstructible" | undefined;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* The effects the adoption transition drives, in the one order it always runs them (step 11b supplies the real
|
|
34
|
-
* implementations). The transition is a strict data-before-authority sequence: prove the idb store owes the
|
|
35
|
-
* server nothing, reconstruct an opfs successor through the Adoption-bootstrap gate, commit it via the shared
|
|
36
|
-
* barrier, and only THEN delete the predecessor.
|
|
37
|
-
*/
|
|
38
|
-
export interface AdoptionEffects {
|
|
39
|
-
/**
|
|
40
|
-
* Boot the idb engine PRE-EXPOSE (never surfaced to tabs — invariant 3) and return the journal status
|
|
41
|
-
* counts the drain predicate reads. The engine is booted solely to read the journal; the caller never
|
|
42
|
-
* exposes THIS booted instance until adoption defers (journal owed) or fails.
|
|
43
|
-
*/
|
|
44
|
-
bootIdbPreExpose(): Promise<JournalStatusCounts>;
|
|
45
|
-
/** Strict-sync the never-exposed idb engine, then close it (all handles released) before building the successor. */
|
|
46
|
-
strictSyncAndCloseIdb(): Promise<void>;
|
|
47
|
-
/** Advance the store meta record phase. The transition calls it with `"adopting"` and, on failure, `"idb-authoritative"`. */
|
|
48
|
-
setPhase(phase: StoreMetaPhase): Promise<void>;
|
|
49
|
-
/**
|
|
50
|
-
* Build the opfs candidate via the normal server bootstrap path AND await the Adoption-bootstrap gate:
|
|
51
|
-
* authorized online reconstruction — the initial catch-up of the eager Consistency groups a valid initial
|
|
52
|
-
* store requires ("authorized" includes legitimately anonymous/public shapes). Resolves ONLY when the gate
|
|
53
|
-
* passed; THROWS on gate failure (offline, unauthorized, `syncEnabled: false`, or a bootstrap error).
|
|
54
|
-
*/
|
|
55
|
-
buildCandidateThroughGate(): Promise<void>;
|
|
56
|
-
/**
|
|
57
|
-
* Run the shared commitment barrier on the CANDIDATE engine: `strictSync()` returns with VFS health good →
|
|
58
|
-
* sentinel → `opfs-committed`. Step 11b wires `runCommitmentBarrier` (store-lifecycle.ts) with real effects;
|
|
59
|
-
* here it is one injected step. It MUST be all-or-nothing: a barrier failure publishes NOTHING (invariant 3),
|
|
60
|
-
* so a throw leaves the idb store authoritative and the candidate uncommitted.
|
|
61
|
-
*/
|
|
62
|
-
commitCandidate(): Promise<void>;
|
|
63
|
-
/** Delete the idb predecessor now the opfs successor is committed. Idempotent: absent counts as deleted. */
|
|
64
|
-
deleteIdbPredecessor(): Promise<void>;
|
|
65
|
-
/**
|
|
66
|
-
* Tear down a failed candidate: delete the stale sentinel (if any) AND the candidate directory,
|
|
67
|
-
* delete-if-present (a barrier-gap crash's sentinel must never survive). Recreate-only; idempotent.
|
|
68
|
-
*/
|
|
69
|
-
teardownCandidate(): Promise<void>;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/** The narrow candidate surface the real boot wiring hands this module. */
|
|
73
|
-
export interface AdoptionCandidateHandle {
|
|
74
|
-
ready: Promise<void>;
|
|
75
|
-
strictSync(): Promise<void>;
|
|
76
|
-
close(): Promise<void>;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/** IO callbacks needed to construct the real stateful adoption effects without importing the client entrypoint. */
|
|
80
|
-
export interface AdoptionEffectBuilderOptions {
|
|
81
|
-
readIdbJournal(): Promise<JournalStatusCounts>;
|
|
82
|
-
setPhase(phase: StoreMetaPhase): Promise<void>;
|
|
83
|
-
buildCandidate(): Promise<AdoptionCandidateHandle>;
|
|
84
|
-
publishSentinel(): Promise<void>;
|
|
85
|
-
deleteSentinel(): Promise<void>;
|
|
86
|
-
deleteStoreDirectory(): Promise<void>;
|
|
87
|
-
deleteIdbPredecessor(): Promise<void>;
|
|
88
|
-
gateDeadlineMs?: number;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
const DEFAULT_ADOPTION_GATE_DEADLINE_MS = 30_000;
|
|
92
|
-
|
|
93
|
-
async function awaitAdoptionGate(ready: Promise<void>, deadlineMs: number): Promise<void> {
|
|
94
|
-
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
95
|
-
const deadline = new Promise<never>((_, reject) => {
|
|
96
|
-
timer = setTimeout(
|
|
97
|
-
() =>
|
|
98
|
-
reject(
|
|
99
|
-
new Error(
|
|
100
|
-
"[pgxsinkit] adoption gate unmet: the eager Consistency groups did not complete their initial " +
|
|
101
|
-
`catch-up within ${deadlineMs}ms (offline, unauthorized, or the server was unreachable). The idb ` +
|
|
102
|
-
"store stays authoritative.",
|
|
103
|
-
),
|
|
104
|
-
),
|
|
105
|
-
deadlineMs,
|
|
106
|
-
);
|
|
107
|
-
});
|
|
108
|
-
try {
|
|
109
|
-
await Promise.race([ready, deadline]);
|
|
110
|
-
} finally {
|
|
111
|
-
if (timer !== undefined) clearTimeout(timer);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Construct the stateful adoption effects around narrow IO callbacks. Candidate ownership, gate timing,
|
|
117
|
-
* data-before-authority publication, and failed-candidate cleanup live here rather than in the package entrypoint.
|
|
118
|
-
*/
|
|
119
|
-
export function buildAdoptionEffects(options: AdoptionEffectBuilderOptions): AdoptionEffects {
|
|
120
|
-
let candidate: AdoptionCandidateHandle | undefined;
|
|
121
|
-
return {
|
|
122
|
-
bootIdbPreExpose: () => options.readIdbJournal(),
|
|
123
|
-
async strictSyncAndCloseIdb() {
|
|
124
|
-
// The callback's drain read closes its read-only strict IDB engine before returning.
|
|
125
|
-
},
|
|
126
|
-
setPhase: (phase) => options.setPhase(phase),
|
|
127
|
-
async buildCandidateThroughGate() {
|
|
128
|
-
candidate = await options.buildCandidate();
|
|
129
|
-
await awaitAdoptionGate(candidate.ready, options.gateDeadlineMs ?? DEFAULT_ADOPTION_GATE_DEADLINE_MS);
|
|
130
|
-
},
|
|
131
|
-
async commitCandidate() {
|
|
132
|
-
const active = candidate;
|
|
133
|
-
if (active === undefined) throw new Error("[pgxsinkit] adoption: commit called before the candidate was built");
|
|
134
|
-
await runCommitmentBarrier({
|
|
135
|
-
strictSyncReturns: () => active.strictSync(),
|
|
136
|
-
publishSentinel: () => options.publishSentinel(),
|
|
137
|
-
setPhase: (phase) => options.setPhase(phase),
|
|
138
|
-
});
|
|
139
|
-
await active.close().catch(() => undefined);
|
|
140
|
-
candidate = undefined;
|
|
141
|
-
},
|
|
142
|
-
deleteIdbPredecessor: () => options.deleteIdbPredecessor(),
|
|
143
|
-
async teardownCandidate() {
|
|
144
|
-
if (candidate !== undefined) {
|
|
145
|
-
await candidate.close().catch(() => undefined);
|
|
146
|
-
candidate = undefined;
|
|
147
|
-
}
|
|
148
|
-
await options.deleteSentinel();
|
|
149
|
-
await options.deleteStoreDirectory();
|
|
150
|
-
},
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* The outcome of an adoption transition. `adopted: true` means the opfs successor is committed and the idb
|
|
156
|
-
* predecessor is gone (or scheduled for cleanup — see `predecessorCleanupPending`). Every `adopted: false`
|
|
157
|
-
* outcome leaves the idb store AUTHORITATIVE and untouched:
|
|
158
|
-
* - `not-declared` — automatic adoption without the declaration (default off); nothing ran (invariant 4).
|
|
159
|
-
* - `journal-owed` — the drain predicate failed; adoption is DEFERRED (the caller exposes the idb engine
|
|
160
|
-
* normally). The orchestrator boots the idb engine to READ the journal but exposes nothing itself.
|
|
161
|
-
* - `gate-failed` — the Adoption-bootstrap gate rejected (offline/unauthorized/`syncEnabled: false`/bootstrap
|
|
162
|
-
* error). The candidate was torn down; idb stays authoritative. `error` carries the gate failure text.
|
|
163
|
-
* - `barrier-failed` — the commitment barrier threw (strict sync did not return / VFS health bad). Nothing
|
|
164
|
-
* published; candidate torn down; idb stays authoritative. `error` carries the barrier failure text.
|
|
165
|
-
*/
|
|
166
|
-
export type AdoptionOutcome =
|
|
167
|
-
| {
|
|
168
|
-
adopted: true;
|
|
169
|
-
/**
|
|
170
|
-
* Set only when the predecessor deletion FAILED after a successful commit. Adoption still SUCCEEDED
|
|
171
|
-
* (the phase is `opfs-committed`); the boot classifier's lingering-idb row (adoption crash-row 3 →
|
|
172
|
-
* `opfs-committed` with an idb store present → `open-committed`, then `completeAdoption` finishes the
|
|
173
|
-
* deletion) completes the cleanup on the next boot. Carries the deletion failure text for diagnostics.
|
|
174
|
-
*/
|
|
175
|
-
predecessorCleanupPending?: string;
|
|
176
|
-
}
|
|
177
|
-
| { adopted: false; reason: "not-declared" | "journal-owed" | "gate-failed" | "barrier-failed"; error?: string };
|
|
178
|
-
|
|
179
|
-
/** Extract a stable message from a thrown value (an `Error`'s message, else its string form). */
|
|
180
|
-
function errorText(error: unknown): string {
|
|
181
|
-
return error instanceof Error ? error.message : String(error);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* The shared transition body — identical for the automatic and manual entry points (they differ ONLY in the
|
|
186
|
-
* declaration check, applied before this runs). Steps, in order (plan "Adoption sequence"):
|
|
187
|
-
*
|
|
188
|
-
* 1. Boot the idb engine pre-expose and read the journal. NOT drained → `journal-owed` (the orchestrator boots
|
|
189
|
-
* NOTHING further and exposes NOTHING itself — the caller then exposes the idb engine normally; adoption
|
|
190
|
-
* deferred). Drained → continue.
|
|
191
|
-
* 2. Strict-sync + close the never-exposed idb engine.
|
|
192
|
-
* 3. Phase → `adopting`.
|
|
193
|
-
* 4. Build the opfs candidate through the Adoption-bootstrap gate. On throw → tear the candidate down + set
|
|
194
|
-
* `idb-authoritative` (idb stays authoritative, plan gate-unmet row) → `gate-failed`.
|
|
195
|
-
* 5. Commit the candidate via the shared barrier. On throw → tear the candidate down + set `idb-authoritative`
|
|
196
|
-
* → `barrier-failed` (the barrier is all-or-nothing; nothing published).
|
|
197
|
-
* 6. Delete the idb predecessor → adopted. A deletion throw here does NOT unadopt: the phase is already
|
|
198
|
-
* `opfs-committed`, so the outcome is `adopted: true` with `predecessorCleanupPending` recorded and the
|
|
199
|
-
* classifier's lingering-idb row finishes the deletion next boot.
|
|
200
|
-
*/
|
|
201
|
-
async function runTransition(effects: AdoptionEffects): Promise<AdoptionOutcome> {
|
|
202
|
-
const counts = await effects.bootIdbPreExpose();
|
|
203
|
-
if (!journalOwesNothing(counts)) return { adopted: false, reason: "journal-owed" };
|
|
204
|
-
|
|
205
|
-
await effects.strictSyncAndCloseIdb();
|
|
206
|
-
await effects.setPhase("adopting");
|
|
207
|
-
|
|
208
|
-
try {
|
|
209
|
-
await effects.buildCandidateThroughGate();
|
|
210
|
-
} catch (error) {
|
|
211
|
-
// Adoption-bootstrap gate unmet (offline/unauthorized/syncEnabled: false/bootstrap error): nothing
|
|
212
|
-
// published, so idb stays authoritative (plan "Adoption gate unmet" row).
|
|
213
|
-
await effects.teardownCandidate();
|
|
214
|
-
await effects.setPhase("idb-authoritative");
|
|
215
|
-
return { adopted: false, reason: "gate-failed", error: errorText(error) };
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
try {
|
|
219
|
-
await effects.commitCandidate();
|
|
220
|
-
} catch (error) {
|
|
221
|
-
// The barrier is all-or-nothing — a failure publishes NOTHING (invariant 3). Recover to idb-authoritative.
|
|
222
|
-
await effects.teardownCandidate();
|
|
223
|
-
await effects.setPhase("idb-authoritative");
|
|
224
|
-
return { adopted: false, reason: "barrier-failed", error: errorText(error) };
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
try {
|
|
228
|
-
await effects.deleteIdbPredecessor();
|
|
229
|
-
} catch (error) {
|
|
230
|
-
// Adoption already SUCCEEDED (phase `opfs-committed`); a failed predecessor deletion never unadopts. The
|
|
231
|
-
// boot classifier's lingering-idb row completes the deletion next boot (plan adoption crash-row 3).
|
|
232
|
-
return { adopted: true, predecessorCleanupPending: errorText(error) };
|
|
233
|
-
}
|
|
234
|
-
return { adopted: true };
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
/**
|
|
238
|
-
* The AUTOMATIC adoption transition, run at boot BEFORE any engine is exposed (the caller guarantees pre-expose
|
|
239
|
-
* exclusivity — invariant 3's no-race-by-construction). Consults the {@link AdoptionDeclaration} FIRST: without
|
|
240
|
-
* `"server-reconstructible"` it refuses with `not-declared` and runs ZERO effects (invariant 4 — default off,
|
|
241
|
-
* hook absence is never authority). With the declaration it runs the shared {@link runTransition}.
|
|
242
|
-
*/
|
|
243
|
-
export function runAdoptionTransition(
|
|
244
|
-
declaration: AdoptionDeclaration,
|
|
245
|
-
effects: AdoptionEffects,
|
|
246
|
-
): Promise<AdoptionOutcome> {
|
|
247
|
-
if (declaration !== "server-reconstructible") {
|
|
248
|
-
return Promise.resolve({ adopted: false, reason: "not-declared" });
|
|
249
|
-
}
|
|
250
|
-
return runTransition(effects);
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
/**
|
|
254
|
-
* The MANUAL adoption API's core: the identical transition, but WITHOUT the declaration check — the consumer's
|
|
255
|
-
* explicit call IS the authorization (they were told to export/migrate any local-only data first). The drain
|
|
256
|
-
* predicate still gates it (a store that owes the server work is not adoptable), and every failure recovery is
|
|
257
|
-
* identical to the automatic path.
|
|
258
|
-
*/
|
|
259
|
-
export function runManualAdoption(effects: AdoptionEffects): Promise<AdoptionOutcome> {
|
|
260
|
-
return runTransition(effects);
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
/**
|
|
264
|
-
* The boot-wiring predicate (plan step 11b): is a boot eligible to START an automatic adoption? True IFF the
|
|
265
|
-
* consumer declared `"server-reconstructible"` AND opfs sync access is available in this scope AND the boot
|
|
266
|
-
* classifier landed on `boot-idb-authoritative` — adoption only ever starts from an idb-authoritative store
|
|
267
|
-
* with opfs available (there is nowhere to build the successor otherwise, and no other verdict is a candidate).
|
|
268
|
-
*/
|
|
269
|
-
export function adoptionEligible(
|
|
270
|
-
declaration: AdoptionDeclaration,
|
|
271
|
-
verdict: StoreBootVerdict,
|
|
272
|
-
hasOpfsSyncAccess: boolean,
|
|
273
|
-
): boolean {
|
|
274
|
-
return declaration === "server-reconstructible" && hasOpfsSyncAccess && verdict.action === "boot-idb-authoritative";
|
|
275
|
-
}
|