@serfab/quereus-plugin-sereus 0.9.0 → 0.11.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/dist/types.d.ts CHANGED
@@ -1,6 +1,21 @@
1
1
  import type { Libp2p } from '@libp2p/interface';
2
2
  import type { IRepo } from '@optimystic/db-core';
3
3
  import type { IRawStorage } from '@optimystic/db-p2p';
4
+ /**
5
+ * Which Optimystic transactor a strand connection commits through — a choice of
6
+ * storage engine, nothing more. See {@link StrandConnectionOptions.transactor}.
7
+ *
8
+ * Deliberately a closed union, and narrower than what Optimystic's
9
+ * `collection-factory.ts` accepts: that factory also switches on `'mesh-test'`
10
+ * (its production transactor stack over a one-node mock mesh) and falls through
11
+ * to a host-registered custom transactor for any other name. Neither is offered
12
+ * here — `composeStrand`'s node gate special-cases only `'test'`, so
13
+ * `'mesh-test'` would get a real libp2p node it does not want, and nothing in
14
+ * this repo registers a custom transactor. A closed union lets `parseConfig`
15
+ * reject a typo (`'locl'`) instead of handing it downstream; widen it (and the
16
+ * node gate with it) when one of those values is actually needed.
17
+ */
18
+ export type StrandTransactor = 'local' | 'network' | 'test';
4
19
  export interface StrandConnectionOptions {
5
20
  /** UUID of the strand to connect to */
6
21
  strandId: string;
@@ -18,29 +33,43 @@ export interface StrandConnectionOptions {
18
33
  enableCache?: boolean;
19
34
  /** FRET profile (default: 'edge') */
20
35
  fretProfile?: 'edge' | 'core';
36
+ /**
37
+ * Number of nodes Optimystic is told this strand's replication cluster should
38
+ * have. Ignored when {@link libp2pNode} is injected (the injected node was
39
+ * built with its own value). Every peer on the strand should use the same
40
+ * number — see `resolveStrandClusterSize`. Defaults to
41
+ * `DEFAULT_STRAND_CLUSTER_SIZE` (4 — the smallest breadth whose 0.75
42
+ * super-majority still commits with one holder offline).
43
+ */
44
+ clusterSize?: number;
21
45
  /** Inject an existing libp2p node instead of creating one */
22
46
  libp2pNode?: Libp2p;
23
47
  /** Required when libp2pNode is provided */
24
48
  coordinatedRepo?: IRepo;
25
- /**
26
- * Lifecycle mode. `'networked'` (default) uses the network transactor and
27
- * is appropriate for multi-peer participation. `'bootstrap'` switches to a
28
- * local transactor so a solo node can apply schema and accept DML with no
29
- * peer round trips; pair it with a persistent `storage` to survive restart.
30
- */
31
- mode?: 'bootstrap' | 'networked';
32
49
  /**
33
50
  * Persistent raw storage. When provided:
34
51
  * - it is passed to `createLibp2pNode` as `storage` so the libp2p data path uses it,
35
- * - in `bootstrap` mode it is also handed to the optimystic plugin as
52
+ * - with `transactor: 'local'` it is also handed to the optimystic plugin as
36
53
  * `rawStorageFactory: () => storage` so the local transactor persists DML
37
54
  * on the same instance (avoids cache divergence between the two consumers).
38
55
  *
39
56
  * The plugin treats `storage` as borrowed — it is NOT closed on `shutdown()`.
40
57
  */
41
58
  storage?: IRawStorage;
42
- /** @internal Override transactor type. Used by unit tests with `'test'`. When set, takes precedence only if `mode` is not specified. */
43
- transactor?: string;
59
+ /**
60
+ * Which Optimystic transactor writes and reads go through (default
61
+ * `'network'`):
62
+ *
63
+ * - `'network'` — transactions go through the strand's libp2p cohort. A node
64
+ * that is alone coordinates for itself, so this works with one peer or
65
+ * many; it is the only value an application should use.
66
+ * - `'local'` — transactions go straight to this process's raw storage, no
67
+ * peers consulted. For in-process tests and tooling; pair it with a
68
+ * persistent {@link storage} to survive restart.
69
+ * - `'test'` — Optimystic's in-memory fake. No libp2p node is created unless
70
+ * one is injected. Unit tests only.
71
+ */
72
+ transactor?: StrandTransactor;
44
73
  }
45
74
  export interface SereusPluginResult {
46
75
  vtables: [];
@@ -57,6 +86,13 @@ export interface SereusPluginResult {
57
86
  tables: number;
58
87
  indexes: number;
59
88
  };
89
+ /**
90
+ * The transactor this connection actually resolved to — the RESOLVED value,
91
+ * not the requested one, so a spec whose whole point is the arm it runs on can
92
+ * assert it rather than assume it. Equals `options.transactor` when one was
93
+ * given, `'network'` otherwise.
94
+ */
95
+ transactor: StrandTransactor;
60
96
  /** Shuts down the libp2p node and collection factory. Call when done. */
61
97
  shutdown: () => Promise<void>;
62
98
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@serfab/quereus-plugin-sereus",
3
- "version": "0.9.0",
3
+ "version": "0.11.0",
4
4
  "type": "module",
5
5
  "description": "Quereus plugin that connects to a Sereus strand for SQL access",
6
6
  "main": "dist/index.js",
@@ -67,13 +67,13 @@
67
67
  "@libp2p/circuit-relay-v2": "^4.1.3",
68
68
  "@libp2p/interface": "^3.1.0",
69
69
  "@libp2p/websockets": "^10.1.3",
70
- "@optimystic/db-core": "^0.14.1",
71
- "@optimystic/db-p2p": "^0.14.1",
72
- "@optimystic/db-p2p-storage-fs": "^0.14.1",
73
- "@optimystic/db-p2p-storage-web": "^0.14.1",
74
- "@optimystic/quereus-plugin-crypto": "^0.14.1",
75
- "@optimystic/quereus-plugin-optimystic": "^0.14.1",
76
- "@quereus/quereus": "^4.4.0",
70
+ "@optimystic/db-core": "^0.24.0",
71
+ "@optimystic/db-p2p": "^0.24.0",
72
+ "@optimystic/db-p2p-storage-fs": "^0.24.0",
73
+ "@optimystic/db-p2p-storage-web": "^0.24.0",
74
+ "@optimystic/quereus-plugin-crypto": "^0.24.0",
75
+ "@optimystic/quereus-plugin-optimystic": "^0.24.0",
76
+ "@quereus/quereus": "^4.14.0",
77
77
  "debug": "^4.4.3"
78
78
  },
79
79
  "quereus": {
@@ -0,0 +1,107 @@
1
+ /**
2
+ * Wrap an embedder-supplied `IRawStorage` in `@optimystic/db-p2p`'s write-through
3
+ * raw-storage cache (`CachedRawStorage`) before handing it to a node or transactor.
4
+ *
5
+ * Why: bringing a control database or strand up is raw-storage-operation bound —
6
+ * uncached, a cold start re-reads the same handful of blocks ~2000 times (see the
7
+ * budget history in cadre-core's `test/control-start-storage-op-budget.spec.ts` and
8
+ * the sereus tickets `complete/optimystic-block-read-amplification-on-control-start`
9
+ * + `complete/optimystic-schema-catalog-reread-per-write-blows-storage-budgets`).
10
+ * Upstream ships the remedy as an opt-in wrapper — adoption is deliberately the
11
+ * consumer's choice — and this module is where Sereus opts in. It lives in this
12
+ * package (not cadre-core) for the same reason `cluster-size.ts` does: both the SQL
13
+ * plugin's own connectors (`composeStrand`, including the browser/IndexedDB path,
14
+ * where per-operation latency is highest) and cadre-core's control/strand seams
15
+ * need the identical wrap, and two copies could disagree.
16
+ *
17
+ * Soundness rests on the cache's single-process-owner invariant (upstream
18
+ * docs/storage.md §6): every write to the backend must funnel through the wrapped
19
+ * instance. Two rules uphold that:
20
+ *
21
+ * - **One wrap per inner instance** (WeakMap memo): the same `IRawStorage` reaches
22
+ * this function from several seams at once — cadre-core's node wiring and the
23
+ * strand backfill share the instance the node was built with, and the SQL
24
+ * plugin's `composeStrand` re-wraps whatever it is handed. Two live caches over
25
+ * one backend would each miss the other's writes; the memo makes the wrap
26
+ * idempotent instead — a caller wrapping an already-wrapped or already-seen
27
+ * instance gets the same object back.
28
+ * - **A Sereus process owns its data stores** — no second process writes the same
29
+ * backend, which is the invariant's other half.
30
+ *
31
+ * `MemoryRawStorage` is returned unwrapped on upstream's own guidance: the backend
32
+ * is already in-memory, so the cache adds bookkeeping with nothing to save.
33
+ *
34
+ * The memo dedupes *concurrent* seams; it is not what keeps a cache alive across a
35
+ * runtime rebuild. That is cadre-core's job, and it does it by OWNERSHIP: one
36
+ * resolved store per scope per runtime lifetime — a strand's store for the life of
37
+ * its instance (a hibernation quiesce → resume reuses the store already resolved,
38
+ * so the wake lands on a warm cache), the control store for the life of one
39
+ * `CadreNode.start()`. Because the store outlives the rebuild, so does its wrap,
40
+ * whether or not the embedder's provider returns a stable instance.
41
+ *
42
+ * When a scope's runtime stops for good, cadre-core calls {@link disposeStorageCache}
43
+ * — the release half of that ownership. Without it the retired wrapper's
44
+ * `CacheStoreHandle` would stay in the shared pool's registry for the process
45
+ * lifetime (`unregisterStore` is the only removal), one orphan per stop.
46
+ */
47
+
48
+ // Imported from the `/rn` entry — the platform-neutral surface (no Node-only
49
+ // transports) — because `composeStrand` reaches this module on the browser path
50
+ // too. Both db-p2p entrypoints re-export the same storage modules, so class
51
+ // identity (the `instanceof` checks below) is unaffected on Node.
52
+ import { CachedRawStorage, MemoryRawStorage, type IRawStorage } from '@optimystic/db-p2p/rn';
53
+
54
+ const wraps = new WeakMap<IRawStorage, CachedRawStorage>();
55
+
56
+ /**
57
+ * Reverse of `wraps`, so {@link disposeStorageCache} can evict the memo entry it
58
+ * cannot otherwise find — `wraps` is keyed by the inner instance, and dispose is
59
+ * handed the wrapper.
60
+ */
61
+ const inners = new WeakMap<CachedRawStorage, IRawStorage>();
62
+
63
+ /**
64
+ * The cached view of `storage`, memoized per instance. `label` names the store in
65
+ * the shared pool's `stats()` (e.g. `control`, or the strand id).
66
+ */
67
+ export function wrapStorageWithCache(storage: IRawStorage, label: string): IRawStorage {
68
+ if (storage instanceof MemoryRawStorage || storage instanceof CachedRawStorage) {
69
+ return storage;
70
+ }
71
+ let wrapped = wraps.get(storage);
72
+ if (!wrapped) {
73
+ wrapped = new CachedRawStorage(storage, undefined, label);
74
+ wraps.set(storage, wrapped);
75
+ inners.set(wrapped, storage);
76
+ }
77
+ return wrapped;
78
+ }
79
+
80
+ /**
81
+ * Release the cache wrapper {@link wrapStorageWithCache} returned: unregister it from
82
+ * the shared pool and forget the memo, so a later re-wrap of the same inner instance
83
+ * gets a live cache rather than this retired one. A disposed wrapper must never be
84
+ * handed out again — its pool store id is retired and never reused — and providers
85
+ * that return a stable instance per scope (the web reference app's `strand-storage.ts`,
86
+ * the integration harness's `captureRawStorage`) would get exactly that back on a
87
+ * relaunch if the memo were left in place.
88
+ *
89
+ * No-op for anything this module returned unwrapped (e.g. `MemoryRawStorage`), so
90
+ * callers need no `instanceof` test. Only the wrapper is released: the inner store the
91
+ * embedder returned is never closed — that handle stays the embedder's to manage.
92
+ */
93
+ export async function disposeStorageCache(storage: IRawStorage): Promise<void> {
94
+ if (!(storage instanceof CachedRawStorage)) {
95
+ return;
96
+ }
97
+ const inner = inners.get(storage);
98
+ if (inner) {
99
+ inners.delete(storage);
100
+ // Only evict the memo if it still points at THIS wrapper: a re-wrap after an
101
+ // earlier dispose has already installed a live successor we must not drop.
102
+ if (wraps.get(inner) === storage) {
103
+ wraps.delete(inner);
104
+ }
105
+ }
106
+ await storage.dispose();
107
+ }
@@ -0,0 +1,248 @@
1
+ import type { NodeOptions } from '@optimystic/db-p2p';
2
+
3
+ /**
4
+ * Smallest cluster size Optimystic will honour — its own `minAbsoluteClusterSize`,
5
+ * and the smallest value that reaches the cluster path at all (a lone node writes to
6
+ * local storage without forming a cluster). A validation floor, not a default.
7
+ */
8
+ export const MIN_CLUSTER_SIZE = 2;
9
+
10
+ /**
11
+ * How many nodes each block of the **control** (cadre membership) database is replicated
12
+ * to. Chosen to exceed any party's node count, so in practice every control block lands
13
+ * on every member of the party.
14
+ *
15
+ * **Why full replication here.** Every control node reads the whole control database —
16
+ * membership, peer addresses, the strand list — so a member left out of a block's cohort is
17
+ * a member that may never learn the fact, and at a cohort of two the *read repair* that is
18
+ * supposed to make that safe cannot converge (it can ask exactly one peer, and that peer may
19
+ * be the member that also missed the write). Measured: 4 failures in 10 runs of the
20
+ * control-DB replication scenario at size 2, 0 in 20 at size 3 and 0 in 10 at size 8.
21
+ *
22
+ * **Why this number, and why a constant.** Optimystic caps a cohort at the peers that
23
+ * actually serve the network and downsizes a cohort it cannot fill (`allowDownsize: true`,
24
+ * which Cadre passes), so any value at or above the party's node count yields the same
25
+ * behaviour: cohort = whole party. 16 is roughly twice the largest deployment the product
26
+ * documents (`docs/architecture.md` → "Enterprise (Multi-Node Mixed)", 7 nodes) — headroom
27
+ * without pretending to support arbitrarily large parties; a party genuinely running more
28
+ * than 16 nodes is back to partial replication for its control database and should raise
29
+ * this. It is a constant rather than the live member count because Optimystic freezes the
30
+ * value at libp2p-node construction, before the `ControlDatabase` holding the `CadrePeer`
31
+ * rows exists, and because a per-node derivation lets two members disagree.
32
+ *
33
+ * A cadre is one party by construction, so the owner-blind cohort-selection question that
34
+ * applies to strand networks (see {@link DEFAULT_STRAND_CLUSTER_SIZE} below) is not a question
35
+ * here: "replicate to the whole party" already is the whole intent, and there is no party
36
+ * diversity for this breadth to buy or fail to buy.
37
+ *
38
+ * Canonical explanation of all of the above — the read-repair mechanics, what whole-party
39
+ * breadth does and does not remove, and what the wider cohort costs in write availability —
40
+ * is `docs/architecture.md` → "Replication cluster size". Keep it there, not here.
41
+ *
42
+ * **Deliberately not `clusterPolicy.assumedClusterSize`.** That option means "the smallest
43
+ * cohort this deployment can genuinely field", and it feeds both the membership admission
44
+ * gate's low-confidence path *and* the read-repair/reconcile corroboration floor. A Cadre
45
+ * party legitimately runs one or two nodes, so Cadre leaves it at Optimystic's default of 2;
46
+ * asserting 16 there would make the admission gate demand `ceil(0.75 x 16) = 12` declared
47
+ * peers and refuse every real party's writes.
48
+ *
49
+ * NOTE: cohort selection overfetches proportionally to this number —
50
+ * `Libp2pKeyPeerNetwork.membershipOverfetch` asks FRET for
51
+ * `max(clusterSize * 4, clusterSize + 16)` candidates and does a peerStore protocol lookup
52
+ * per candidate, so 16 requests a 64-peer proximity band. Free today, because the result is
53
+ * bounded by the peers FRET actually knows (2 in a 3-node party). If control-network cohort
54
+ * selection ever shows up as slow on a large mesh, look here first.
55
+ */
56
+ export const CONTROL_REPLICATION_BREADTH = 16;
57
+
58
+ /**
59
+ * Cluster consensus policy every CONTROL-network libp2p node runs — production
60
+ * (`CadreNode.buildControlNodeOptions`) and the integration harness alike. One definition so
61
+ * the two cannot drift: the object literal used to be hand-copied at both sites, and the copy
62
+ * at the harness had picked up a `superMajorityThreshold` override production never had.
63
+ *
64
+ * `superMajorityThreshold` is deliberately ABSENT. Omitting it makes both the cluster member
65
+ * and the coordinator fall back to Optimystic's `DEFAULT_SUPER_MAJORITY_THRESHOLD` (0.75) —
66
+ * the bar a real party commits control writes against. Setting it here, or at any one
67
+ * consumer, reintroduces exactly the divergence this constant exists to prevent. The absence
68
+ * is a researched decision, not an inherited default: no threshold both relaxes the
69
+ * three-node unanimity bar and satisfies Optimystic's partition-safety condition at the
70
+ * shipped admission fraction — see `docs/architecture.md` → "Replication cluster size".
71
+ *
72
+ * `sizeTolerance` only makes sense alongside `allowDownsize`: a fixed
73
+ * {@link CONTROL_REPLICATION_BREADTH}-wide target is unsatisfiable by any real (2-7 node)
74
+ * party, so the cohort must be allowed to shrink to the party that actually exists.
75
+ *
76
+ * NOT for strand networks: {@link STRAND_CLUSTER_POLICY} is a structurally identical object
77
+ * for a different network with different reasoning. The shape match is a coincidence; keep
78
+ * them separate.
79
+ *
80
+ * `assumedClusterSize` is REQUIRED here, and 2 is not a guess — it is the smallest party we
81
+ * support. The field feeds two Optimystic consumers with opposite failure modes, and only one
82
+ * of them defaults to something workable for us:
83
+ *
84
+ * - The membership admission gate already defaults to 2 (`minAbsoluteClusterSize`), so naming
85
+ * it changes nothing there.
86
+ * - The read-repair / reconcile corroboration floor does NOT. Absent this field it falls back
87
+ * to `clusterSize` — which for the control network is {@link CONTROL_REPLICATION_BREADTH},
88
+ * 16. `corroboratorCapacity(peers, 16)` is then `max(peers, 15)`, so the floor of two
89
+ * distinct non-self corroborators binds. A two-node party fields exactly one, can never
90
+ * reach two, and so can never repair a block: `cluster-fetch:no-quorum` forever.
91
+ *
92
+ * Declaring 2 does NOT lower the replication factor — that stays at
93
+ * {@link CONTROL_REPLICATION_BREADTH}. It states the cohort size we can genuinely field, which
94
+ * is what the corroboration floor is asking. See Optimystic's `cluster/cluster-policy.ts`
95
+ * ("Why two size yardsticks, not one") and `corroboratorCapacity` in `cluster/quorum-restore.ts`.
96
+ *
97
+ * The `satisfies` is load-bearing: without it a mistyped or obsolete key would compile at both
98
+ * this definition and every consumer, because TypeScript only excess-property-checks fresh
99
+ * object literals, not a shared constant handed to `clusterPolicy`.
100
+ */
101
+ export const CONTROL_CLUSTER_POLICY = Object.freeze({
102
+ allowDownsize: true,
103
+ sizeTolerance: 0.5,
104
+ assumedClusterSize: 2,
105
+ } satisfies NonNullable<NodeOptions['clusterPolicy']>);
106
+
107
+ /**
108
+ * Default number of nodes a **strand** network is told its replication cluster should have.
109
+ *
110
+ * Strand data is application data, where partial replication is a legitimate choice: only
111
+ * the control database's every-member-reads-all-of-it character forces full replication
112
+ * ({@link CONTROL_REPLICATION_BREADTH}). So the question here is not "everyone" but "how
113
+ * few is too few", and the answer is four.
114
+ *
115
+ * **Why four.** A write commits when a super-majority of the cohort approves, and that bar is
116
+ * Optimystic's `DEFAULT_SUPER_MAJORITY_THRESHOLD` of 0.75 — which Cadre selects by naming no
117
+ * threshold at all (see {@link CONTROL_CLUSTER_POLICY}). Approvals needed is
118
+ * `ceil(cohort x 0.75)`, so:
119
+ *
120
+ * | copies | approvals needed | holders that may be offline |
121
+ * |--------|------------------|-----------------------------|
122
+ * | 2 | 2 | 0 |
123
+ * | 3 | 3 | 0 |
124
+ * | 4 | 3 | 1 |
125
+ * | 5 | 4 | 1 |
126
+ * | 6 | 5 | 1 |
127
+ *
128
+ * Four is the smallest breadth that lets a write commit while one holder is away. At two or
129
+ * three every holder must be awake for every write, which for a workspace shared between
130
+ * phones and laptops is the ordinary case, not the rare one. Six buys no more fault tolerance
131
+ * than four and costs more overfetch (see the NOTE below).
132
+ *
133
+ * **It is also a correctness floor, not only a durability one — but the floor is measured in
134
+ * machines, not in this number.** A node that has fallen behind and can ask exactly one peer
135
+ * whether it is current has Optimystic accept that single answer as the cluster's truth
136
+ * (`corroboratorCapacity` in `db-p2p/src/cluster/quorum-restore.ts` lowers the corroboration
137
+ * floor to one when the cohort cannot hold a second voter). If that peer is also behind it
138
+ * honestly reports the stale revision, the reader concludes it is current, and re-arms its
139
+ * repair window forever. Measured on the control-DB replication scenario: 4 failures in 10 runs
140
+ * at breadth 2, 0 in 20 at breadth 3, 0 in 10 at breadth 8. What lifts the floor off a single
141
+ * voter is a *third machine*, not a wider target: `corroboratorCapacity` takes the cohort peers
142
+ * actually present against {@link STRAND_CLUSTER_POLICY}'s declared `assumedClusterSize`, so a
143
+ * three-machine strand needs two corroborators whatever this number says, and a two-machine one
144
+ * cannot supply them however wide the target is. The underlying Optimystic behaviour is unfixed
145
+ * — `backlog/debt-read-repair-single-voter-corroboration` — so a two-machine strand still takes
146
+ * the exposure. Raising this number does not buy the way out; adding a machine does.
147
+ *
148
+ * **Why not derived from the party or member count.** The strand's `Member` rows live *in* the
149
+ * strand database, which runs on the strand libp2p node, whose cluster size is frozen at
150
+ * construction — reading the list to configure the node needs the node to exist. Open strands
151
+ * have no member list at all. The count also changes in the unsafe direction: a node that
152
+ * restarted after someone joined derives a wider expected cohort than one that did not, and the
153
+ * membership admission gate's confident path is what rejects the *wider* view. And a `Member` is
154
+ * a party, not a machine; cohort width consumes machines. Full reasoning in
155
+ * `docs/architecture.md` → "Replication cluster size".
156
+ *
157
+ * **Four machines, not four parties.** This number counts machines, and today that is not the
158
+ * same as counting independent holders. Cohort selection upstream is owner-blind — it
159
+ * picks nodes by hash proximity with no concept of which peer belongs to which party
160
+ * (`optimystic/tickets/backlog/feat-cohort-selection-owner-aware-placement`) — and on the
161
+ * cadre-driven path, the one that resolves a seed for itself, a strand's libp2p mesh is seeded
162
+ * from exactly one party's own control-database peer rows (`CadreNode.resolveCohortSeed`), so
163
+ * the cohort actually formed is that one party's machines. Four copies are, on that path, four
164
+ * machines belonging to one party — one failure domain, one witness. A cross-party mesh is not
165
+ * forbidden, only undiscoverable: it takes hand-supplied addresses
166
+ * (`StrandConnectionOptions.bootstrapNodes`) or a hand-written dial (see the doc). Tracked:
167
+ * `backlog/feat-strand-party-identity`. Full reasoning: `docs/architecture.md` →
168
+ * "Replication cluster size".
169
+ *
170
+ * Raising or lowering it per strand is still the embedder's call
171
+ * ({@link resolveStrandClusterSize}, `CadreNodeConfig.strandClusterSize`,
172
+ * `StrandConnectionOptions.clusterSize`) — but every node on one strand must agree, so a change
173
+ * means restarting all of them. There is no smaller-than-{@link MIN_CLUSTER_SIZE} option.
174
+ *
175
+ * NOTE: cohort selection overfetches proportionally — `Libp2pKeyPeerNetwork.membershipOverfetch`
176
+ * asks FRET for `max(clusterSize * 4, clusterSize + 16)` candidates and does one peerStore
177
+ * protocol lookup per candidate, so 4 requests a 20-peer band (was 18 at breadth 2). Negligible,
178
+ * and bounded by the peers FRET actually knows. If strand cohort selection ever shows up as slow,
179
+ * look here first.
180
+ */
181
+ export const DEFAULT_STRAND_CLUSTER_SIZE = 4;
182
+
183
+ /**
184
+ * Cluster consensus policy every STRAND-network libp2p node runs — production
185
+ * (`StrandInstanceManager.buildStrandRuntime`) and the plugin's own networked e2e mesh alike.
186
+ * One definition so the two cannot drift; the literal used to be hand-copied at both sites.
187
+ *
188
+ * Structurally identical to {@link CONTROL_CLUSTER_POLICY} and deliberately separate from it:
189
+ * that one is the control database's, whose breadth is fixed at
190
+ * {@link CONTROL_REPLICATION_BREADTH} for reasons that do not apply to application data.
191
+ *
192
+ * `superMajorityThreshold` is ABSENT for the same reason it is absent there: omitting it is how
193
+ * both the coordinator and the cluster member select Optimystic's
194
+ * `DEFAULT_SUPER_MAJORITY_THRESHOLD` (0.75), the bar {@link DEFAULT_STRAND_CLUSTER_SIZE}'s
195
+ * copies/approvals table is computed against.
196
+ *
197
+ * `sizeTolerance` only makes sense alongside `allowDownsize`: a {@link
198
+ * DEFAULT_STRAND_CLUSTER_SIZE}-wide target is unsatisfiable by a strand of one, two or three
199
+ * machines, so the cohort must be allowed to shrink to the mesh that actually exists.
200
+ *
201
+ * `assumedClusterSize` is REQUIRED, and {@link MIN_CLUSTER_SIZE} is not a guess — it is the
202
+ * smallest strand that reaches the cluster path at all. Absent this field the read-repair /
203
+ * reconcile corroboration floor falls back to `clusterSize`, so
204
+ * `corroboratorCapacity(peers, 4)` is `max(peers, 3)` and two distinct non-self corroborators
205
+ * become mandatory. A two-machine strand fields exactly one peer, can never reach two, and so
206
+ * can never repair a block — `cluster-fetch:no-quorum` forever, which is a read that never
207
+ * catches up and a `create table` that dies with `Missing block` on the joining node. Declaring
208
+ * the honest floor does NOT lower the replication factor (that stays at
209
+ * {@link DEFAULT_STRAND_CLUSTER_SIZE}) and does not relax anything a strand with three or more
210
+ * machines does: `corroboratorCapacity` takes the MAX of the cohort actually present and this
211
+ * number, so the relaxed branch is reachable only by a cohort that is genuinely that small. See
212
+ * Optimystic's `cluster/cluster-policy.ts` ("Why two size yardsticks, not one").
213
+ *
214
+ * The `satisfies` is load-bearing for the same reason as {@link CONTROL_CLUSTER_POLICY}'s.
215
+ */
216
+ export const STRAND_CLUSTER_POLICY = Object.freeze({
217
+ allowDownsize: true,
218
+ sizeTolerance: 0.5,
219
+ assumedClusterSize: MIN_CLUSTER_SIZE,
220
+ } satisfies NonNullable<NodeOptions['clusterPolicy']>);
221
+
222
+ /**
223
+ * Resolve the cluster size to hand `createLibp2pNode` for a **strand** network, applying
224
+ * {@link DEFAULT_STRAND_CLUSTER_SIZE} and rejecting values Optimystic cannot honour. The
225
+ * control network does not route through here — it uses the fixed
226
+ * {@link CONTROL_REPLICATION_BREADTH}.
227
+ *
228
+ * Leaving the value unset is NOT the same as passing the default: Optimystic's own fallback
229
+ * is 10, so every strand node-creating path must route through here.
230
+ *
231
+ * Every node on the same strand should resolve to the same value: the membership admission
232
+ * gate's *confident* path compares a coordinator's declared set against the member's own
233
+ * cohort view, and that view is bounded by this number, so a member configured much higher
234
+ * can reject a smaller declared set as a downsize. Divergence is a live hazard whenever FRET
235
+ * has a confident network-size estimate — not the unconditional refusal earlier revisions of
236
+ * this comment described. See `docs/architecture.md` → "Replication cluster size".
237
+ */
238
+ export function resolveStrandClusterSize(configured?: number): number {
239
+ if (configured === undefined) {
240
+ return DEFAULT_STRAND_CLUSTER_SIZE;
241
+ }
242
+ if (!Number.isInteger(configured) || configured < MIN_CLUSTER_SIZE) {
243
+ throw new Error(
244
+ `clusterSize must be an integer >= ${MIN_CLUSTER_SIZE} (Optimystic's minimum cluster size); got ${configured}`
245
+ );
246
+ }
247
+ return configured;
248
+ }
@@ -4,8 +4,10 @@ import optimysticPlugin from '@optimystic/quereus-plugin-optimystic/plugin';
4
4
  import type { Libp2p } from '@libp2p/interface';
5
5
  import type { IRepo } from '@optimystic/db-core';
6
6
  import type { IRawStorage } from '@optimystic/db-p2p';
7
- import type { StrandConnectionOptions, SereusPluginResult, Libp2pNodeWithRepo } from './types.js';
7
+ import type { StrandConnectionOptions, SereusPluginResult, StrandTransactor, Libp2pNodeWithRepo } from './types.js';
8
8
  import { STRAND_SCHEMA } from './strand-schema.js';
9
+ import { resolveStrandClusterSize } from './cluster-size.js';
10
+ import { wrapStorageWithCache } from './cached-storage.js';
9
11
 
10
12
  const log = debug('sereus:plugin:strand');
11
13
  const timing = debug('sereus:plugin:strand:timing');
@@ -74,6 +76,12 @@ export interface CreateNodeContext {
74
76
  fretProfile: 'edge' | 'core';
75
77
  /** libp2p listening port (Node TCP). Browser transports ignore it. */
76
78
  port: number;
79
+ /**
80
+ * Resolved replication cluster size. Already defaulted — pass it to
81
+ * `createLibp2pNode` verbatim; omitting it falls back to Optimystic's own
82
+ * default of 10, which gates writes on any smaller party.
83
+ */
84
+ clusterSize: number;
77
85
  /** Resolved persistent storage to back the node, if any. */
78
86
  storage?: IRawStorage;
79
87
  }
@@ -81,7 +89,7 @@ export interface CreateNodeContext {
81
89
  /** Context passed to the platform's storage-resolution strategy. */
82
90
  export interface ResolveStorageContext {
83
91
  strandId: string;
84
- resolvedTransactor: string;
92
+ resolvedTransactor: StrandTransactor;
85
93
  /** The storage the caller passed in `options.storage`, if any. */
86
94
  requestedStorage?: IRawStorage;
87
95
  }
@@ -136,40 +144,40 @@ export async function composeStrand(
136
144
  port = 0,
137
145
  enableCache = true,
138
146
  fretProfile = 'edge',
139
- mode,
147
+ transactor = 'network',
140
148
  } = options;
141
149
 
142
- // Resolve the transactor. `mode` is the public knob: bootstrap -> local,
143
- // networked -> network. The legacy `transactor` override (used by unit
144
- // tests with `'test'`) only applies when `mode` is unspecified.
145
- let resolvedTransactor: string;
146
- if (mode !== undefined) {
147
- resolvedTransactor = mode === 'bootstrap' ? 'local' : 'network';
148
- } else if (options.transactor !== undefined) {
149
- resolvedTransactor = options.transactor;
150
- } else {
151
- resolvedTransactor = 'network';
152
- }
150
+ // Resolve (and validate) up front so a nonsense value fails before any plugin
151
+ // registration or node creation has happened.
152
+ const clusterSize = resolveStrandClusterSize(options.clusterSize);
153
+
154
+ // The storage engine every write and read below goes through — see
155
+ // `StrandConnectionOptions.transactor`. Named separately from the option so
156
+ // the default is applied exactly once and reported on the result.
157
+ const resolvedTransactor: StrandTransactor = transactor;
153
158
 
154
159
  const networkName = `strand-${strandId}`;
155
- log('Connecting to strand %s (network: %s, mode=%s, transactor=%s)',
156
- strandId, networkName, mode ?? '(default)', resolvedTransactor);
160
+ log('Connecting to strand %s (network: %s, transactor=%s)',
161
+ strandId, networkName, resolvedTransactor);
157
162
 
158
163
  // Resolve storage. Node passes `options.storage` through; the browser
159
- // defaults to IndexedDB. The resolved instance feeds BOTH the plugin's
160
- // bootstrap `rawStorageFactory` and node creation, so it must be decided up
161
- // front (before pluginConfig is built).
162
- const storage = platform.resolveStorage
164
+ // defaults to IndexedDB. The resolved instance feeds BOTH the local
165
+ // transactor's `rawStorageFactory` and node creation, so it must be decided
166
+ // up front (before pluginConfig is built). Wrapped in the write-through
167
+ // raw-storage cache (cached-storage.ts) — idempotent, so a caller that
168
+ // already wrapped (cadre-core's seams) passes through unchanged.
169
+ const resolvedStorage = platform.resolveStorage
163
170
  ? await platform.resolveStorage({ strandId, resolvedTransactor, requestedStorage: options.storage })
164
171
  : options.storage;
172
+ const storage = resolvedStorage ? wrapStorageWithCache(resolvedStorage, strandId) : resolvedStorage;
165
173
 
166
174
  // 1. Register crypto plugin (digest, sign, verify, etc.)
167
175
  await platform.registerCrypto(db);
168
176
  log('Registered crypto plugin');
169
177
 
170
- // 2. Register optimystic plugin with transactor defaults. In local mode with
171
- // persistent storage, hand the same instance to the plugin so the local
172
- // transactor persists DML on the host backend (not in-memory).
178
+ // 2. Register optimystic plugin with transactor defaults. On the local
179
+ // transactor with persistent storage, hand the same instance to the plugin so
180
+ // DML persists on the host backend (not in-memory).
173
181
  const pluginConfig: Record<string, unknown> = {
174
182
  default_transactor: resolvedTransactor,
175
183
  default_key_network: 'libp2p',
@@ -209,7 +217,7 @@ export async function composeStrand(
209
217
  coordinatedRepo = options.coordinatedRepo;
210
218
  log('Using injected libp2p node');
211
219
  } else {
212
- const created = await platform.createNode({ networkName, bootstrapNodes, fretProfile, port, storage });
220
+ const created = await platform.createNode({ networkName, bootstrapNodes, fretProfile, port, clusterSize, storage });
213
221
  createdNode = created;
214
222
  node = created;
215
223
  const repo = (created as Libp2pNodeWithRepo).coordinatedRepo;
@@ -302,6 +310,7 @@ export async function composeStrand(
302
310
  functions: [],
303
311
  collations: [],
304
312
  hydrated,
313
+ transactor: resolvedTransactor,
305
314
  async shutdown() {
306
315
  log('Shutting down strand connection %s', strandId);
307
316
  await collectionFactory.shutdown();