instar 1.3.569 → 1.3.571

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.
Files changed (36) hide show
  1. package/dist/commands/server.d.ts.map +1 -1
  2. package/dist/commands/server.js +127 -53
  3. package/dist/commands/server.js.map +1 -1
  4. package/dist/core/CoherenceJournal.d.ts +46 -0
  5. package/dist/core/CoherenceJournal.d.ts.map +1 -1
  6. package/dist/core/CoherenceJournal.js +94 -2
  7. package/dist/core/CoherenceJournal.js.map +1 -1
  8. package/dist/core/JournalSyncApplier.d.ts +22 -0
  9. package/dist/core/JournalSyncApplier.d.ts.map +1 -1
  10. package/dist/core/JournalSyncApplier.js +39 -2
  11. package/dist/core/JournalSyncApplier.js.map +1 -1
  12. package/dist/core/MachinePoolRegistry.d.ts.map +1 -1
  13. package/dist/core/MachinePoolRegistry.js +26 -4
  14. package/dist/core/MachinePoolRegistry.js.map +1 -1
  15. package/dist/core/PeerPresencePuller.d.ts +39 -0
  16. package/dist/core/PeerPresencePuller.d.ts.map +1 -1
  17. package/dist/core/PeerPresencePuller.js +46 -1
  18. package/dist/core/PeerPresencePuller.js.map +1 -1
  19. package/dist/core/ReplicatedPeerStreamReader.d.ts +97 -0
  20. package/dist/core/ReplicatedPeerStreamReader.d.ts.map +1 -0
  21. package/dist/core/ReplicatedPeerStreamReader.js +274 -0
  22. package/dist/core/ReplicatedPeerStreamReader.js.map +1 -0
  23. package/dist/core/ReplicatedRecordEmitter.d.ts +102 -0
  24. package/dist/core/ReplicatedRecordEmitter.d.ts.map +1 -0
  25. package/dist/core/ReplicatedRecordEmitter.js +122 -0
  26. package/dist/core/ReplicatedRecordEmitter.js.map +1 -0
  27. package/dist/core/ws2SendWiring.d.ts +52 -0
  28. package/dist/core/ws2SendWiring.d.ts.map +1 -0
  29. package/dist/core/ws2SendWiring.js +71 -0
  30. package/dist/core/ws2SendWiring.js.map +1 -0
  31. package/package.json +1 -1
  32. package/src/data/builtin-manifest.json +2 -2
  33. package/upgrades/1.3.570.md +69 -0
  34. package/upgrades/1.3.571.md +64 -0
  35. package/upgrades/side-effects/statesync-peer-advert-propagation-fix.md +125 -0
  36. package/upgrades/side-effects/ws2-send-side-emission.md +40 -0
@@ -0,0 +1,274 @@
1
+ /**
2
+ * ReplicatedPeerStreamReader — materializes a replicated store's per-origin records
3
+ * from the coherence-journal streams on disk (WS2 send-side, the union-read half).
4
+ *
5
+ * Spec: docs/specs/WS2-SEND-SIDE-EMISSION-SPEC.md §3.3 + §3.4; the substrate is
6
+ * docs/specs/multi-machine-replicated-store-foundation.md §7.1 (namespaced per-origin
7
+ * storage), §7.2 (the union the reader merges).
8
+ *
9
+ * THE GAP THIS CLOSES: `ReplicatedStoreReader.loadOriginRecords` returned only the OWN
10
+ * origin, so even a correctly-received peer record (durably written by
11
+ * JournalSyncApplier to `peers/<M>.<kind>.jsonl`) was invisible to a read. This reader
12
+ * is the seam that makes the union real: it reads the OWN stream
13
+ * (`<self>.<kind>.jsonl` + archives) AND every peer replica stream
14
+ * (`peers/<M>.<kind>.jsonl` + archives; quarantine + meta excluded), validates each
15
+ * line through the SAME `validateReplicatedEnvelope` + store schema the writer used,
16
+ * and folds to the LATEST record per `(origin, recordKey)` by HLC-max. A delete is a
17
+ * tombstone (kept) so the union's delete-resolution + resurrection guard work.
18
+ *
19
+ * It supplies three seams to the rest of the system:
20
+ * - `loadOriginRecords(store, recordKey)` + `listRecordKeys(store)` — the
21
+ * ReplicatedStoreReader seams (the no-clobber union's per-origin input).
22
+ * - `loadWitness(store, recordKey)` — the emitter's `observed` source (the MAX HLC
23
+ * over every origin record held for the key — own prior + applied peers).
24
+ * - `loadOwnEntries(store, origin)` — the snapshot-serve seam (raw OWN entries by
25
+ * kind), replacing the `loadOwnEntries: () => ({})` stub.
26
+ *
27
+ * PURE-ish: all I/O is the injected `fsImpl` (defaults to node:fs); no Date, no
28
+ * network. Bounded — replicated memory stores are small (per-kind retention caps), and
29
+ * the read ceiling mirrors the applier's SERVE_READ_BYTE_CEILING.
30
+ */
31
+ import fs from 'node:fs';
32
+ import path from 'node:path';
33
+ import { sanitizeMachineId, readTailTolerant, } from './CoherenceJournal.js';
34
+ import { HybridLogicalClock } from './HybridLogicalClock.js';
35
+ import { validateReplicatedEnvelope, } from './ReplicatedRecordEnvelope.js';
36
+ /** The byte ceiling for one stream read (mirrors JournalSyncApplier.SERVE_READ_BYTE_CEILING). */
37
+ const READ_BYTE_CEILING = 64 * 1024 * 1024;
38
+ /** A counters bag that ignores every bump — the reader surfaces nothing per-field
39
+ * (a malformed replica line is simply dropped from the union, the safe direction). */
40
+ const NOOP_COUNTERS = {
41
+ bumpSchemaReject: () => { },
42
+ bumpDroppedField: () => { },
43
+ bumpJailReject: () => { },
44
+ };
45
+ function realFs() {
46
+ return {
47
+ openSync: fs.openSync,
48
+ writeSync: fs.writeSync,
49
+ fdatasyncSync: fs.fdatasyncSync,
50
+ closeSync: fs.closeSync,
51
+ existsSync: fs.existsSync,
52
+ statSync: fs.statSync,
53
+ renameSync: fs.renameSync,
54
+ writeFileSync: fs.writeFileSync,
55
+ readFileSync: fs.readFileSync,
56
+ readdirSync: fs.readdirSync,
57
+ truncateSync: fs.truncateSync,
58
+ mkdirSync: fs.mkdirSync,
59
+ readSync: fs.readSync,
60
+ };
61
+ }
62
+ function escapeRegExp(s) {
63
+ return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
64
+ }
65
+ // RULE 3: EXEMPT — this is NOT a state-detector. ReplicatedPeerStreamReader reads
66
+ // the agent's OWN coherence-journal stream files (a fixed, self-authored JSONL format)
67
+ // and validates each line through validateReplicatedEnvelope. It does not parse
68
+ // provider/CLI output, does not detect external/environment state, and has no
69
+ // signature-matching that could drift across providers — it deterministically folds
70
+ // in-house journal entries by HLC. The "*Reader" name matches the Rule-3 pattern
71
+ // heuristic but the substance is an internal-format reader (mirrors the same exemption
72
+ // on ReplicatedStoreReader).
73
+ export class ReplicatedPeerStreamReader {
74
+ stateDir;
75
+ registry;
76
+ selfMachineId;
77
+ io;
78
+ constructor(config) {
79
+ if (!config)
80
+ throw new Error('ReplicatedPeerStreamReader: config required');
81
+ if (!config.registry)
82
+ throw new Error('ReplicatedPeerStreamReader: registry required (not null)');
83
+ if (typeof config.selfMachineId !== 'string' || config.selfMachineId.length === 0) {
84
+ throw new Error('ReplicatedPeerStreamReader: selfMachineId must be a non-empty string');
85
+ }
86
+ this.stateDir = config.stateDir;
87
+ this.registry = config.registry;
88
+ this.selfMachineId = config.selfMachineId;
89
+ this.io = config.fsImpl ?? realFs();
90
+ }
91
+ // ── The ReplicatedStoreReader seams ────────────────────────────────────────
92
+ /**
93
+ * Every origin's CURRENT record for (store, recordKey) — the own stream + each peer
94
+ * replica namespace, ONE record per origin (the latest by HLC, a delete kept as a
95
+ * tombstone). The union reader merges these via the no-clobber rule. A single-machine
96
+ * install returns only the own origin (so the union is a strict no-op = that record).
97
+ */
98
+ loadOriginRecords(store, recordKey) {
99
+ const byOrigin = this.materialize(store).get(recordKey);
100
+ return byOrigin ? [...byOrigin.values()] : [];
101
+ }
102
+ /** Every recordKey the store currently holds across ALL origins (for readAll). */
103
+ listRecordKeys(store) {
104
+ return [...this.materialize(store).keys()];
105
+ }
106
+ // ── The emitter `observed`-witness seam (§7.2) ─────────────────────────────
107
+ /**
108
+ * The MAX HLC over every origin record this machine currently holds for
109
+ * (store, recordKey) — own prior + applied peers. The author stamps this as the
110
+ * record's `observed` witness. Returns undefined when none is held (first write of
111
+ * the key) ⇒ the author omits `observed` ⇒ flag-on-conflict (the safe direction).
112
+ * Sound: it only witnesses a version provably on disk, so a not-yet-pulled peer
113
+ * version is absent here and the merge flags concurrent rather than silently
114
+ * resolving (§7.2 err-toward-flag).
115
+ */
116
+ loadWitness(store, recordKey) {
117
+ const records = this.loadOriginRecords(store, recordKey);
118
+ let max;
119
+ for (const r of records) {
120
+ if (max === undefined || HybridLogicalClock.compare(r.envelope.hlc, max) > 0) {
121
+ max = r.envelope.hlc;
122
+ }
123
+ }
124
+ return max;
125
+ }
126
+ // ── The snapshot-serve seam (replaces loadOwnEntries: () => ({})) ──────────
127
+ /**
128
+ * The OWN-stream raw entries for `store`, keyed by contributing journal kind — the
129
+ * single-origin snapshot input (§6). `origin` MUST be this machine (single-origin,
130
+ * §6.1); a request for any other origin returns `{}` (we only serve what we
131
+ * authored). Reads the own current + archive streams; returns ALL entries (the
132
+ * materializer folds to the latest per recordKey and computes the seq watermark).
133
+ */
134
+ loadOwnEntries(store, origin) {
135
+ const reg = this.registry.getByStore(store);
136
+ if (!reg)
137
+ return {};
138
+ // Single-origin: only serve OUR OWN authored stream.
139
+ if (origin !== this.selfMachineId)
140
+ return {};
141
+ const kind = reg.kind;
142
+ const entries = this.readKindEntries(this.ownStreamFiles(origin, kind));
143
+ if (entries.length === 0)
144
+ return {};
145
+ const raw = entries.map((e) => ({
146
+ seq: e.seq,
147
+ ts: e.ts,
148
+ machine: e.machine,
149
+ kind: e.kind,
150
+ data: e.data,
151
+ }));
152
+ return { [kind]: raw };
153
+ }
154
+ // ── Materialization (own + peer streams → latest per (origin, recordKey)) ──
155
+ /**
156
+ * Build `recordKey → (origin → latest OriginRecord)` for a store by reading the own
157
+ * stream + every peer replica stream and folding by HLC-max per (origin, recordKey).
158
+ * Read FRESH each call (no cache) so a write-then-read is always consistent — the
159
+ * stores are small + bounded, so the scan is cheap.
160
+ */
161
+ materialize(store) {
162
+ const out = new Map();
163
+ const reg = this.registry.getByStore(store);
164
+ if (!reg)
165
+ return out;
166
+ const kind = reg.kind;
167
+ const schema = reg.schema;
168
+ const files = [
169
+ ...this.ownStreamFiles(this.selfMachineId, kind),
170
+ ...this.peerStreamFiles(kind),
171
+ ];
172
+ for (const file of files) {
173
+ const entries = this.readKindEntries([file]);
174
+ for (const entry of entries) {
175
+ if (entry.kind !== kind)
176
+ continue;
177
+ const data = entry.data;
178
+ if (!data || typeof data !== 'object')
179
+ continue;
180
+ const result = validateReplicatedEnvelope(data, schema, NOOP_COUNTERS);
181
+ if (!result.ok)
182
+ continue;
183
+ const origin = result.envelope.origin;
184
+ const recordKey = result.envelope.recordKey;
185
+ const rec = { origin, envelope: result.envelope, data: result.storeFields };
186
+ let byOrigin = out.get(recordKey);
187
+ if (!byOrigin) {
188
+ byOrigin = new Map();
189
+ out.set(recordKey, byOrigin);
190
+ }
191
+ const prior = byOrigin.get(origin);
192
+ // Keep the LATEST record per (origin, recordKey) by HLC-max (a delete is a
193
+ // tombstone — kept, so the union resolves delete↔put deterministically).
194
+ if (!prior || HybridLogicalClock.compare(rec.envelope.hlc, prior.envelope.hlc) > 0) {
195
+ byOrigin.set(origin, rec);
196
+ }
197
+ }
198
+ }
199
+ return out;
200
+ }
201
+ /** Read + tolerant-parse every JournalEntry from the given stream files. */
202
+ readKindEntries(files) {
203
+ const out = [];
204
+ for (const f of files) {
205
+ const read = readTailTolerant(this.io, f, Number.MAX_SAFE_INTEGER, READ_BYTE_CEILING);
206
+ for (const e of read.entries)
207
+ out.push(e);
208
+ }
209
+ return out;
210
+ }
211
+ // ── Path enumeration ───────────────────────────────────────────────────────
212
+ journalDir() {
213
+ return path.join(this.stateDir, 'state', 'coherence-journal');
214
+ }
215
+ peersDir() {
216
+ return path.join(this.journalDir(), 'peers');
217
+ }
218
+ /** Own current + archive stream files for (origin, kind), newest-first by stamp. */
219
+ ownStreamFiles(origin, kind) {
220
+ const safe = sanitizeMachineId(origin);
221
+ const dir = this.journalDir();
222
+ const current = path.join(dir, `${safe}.${kind}.jsonl`);
223
+ const out = [];
224
+ if (this.io.existsSync(current))
225
+ out.push(current);
226
+ out.push(...this.archivesFor(dir, safe, kind));
227
+ return out;
228
+ }
229
+ /** Every peer replica current + archive stream file for `kind` (quarantine excluded). */
230
+ peerStreamFiles(kind) {
231
+ const dir = this.peersDir();
232
+ let names;
233
+ try {
234
+ names = this.io.readdirSync(dir);
235
+ }
236
+ catch { /* @silent-fallback-ok: peers dir absent = no peers yet (single-machine / fresh) — an empty union, never an error. */
237
+ return [];
238
+ }
239
+ const k = escapeRegExp(kind);
240
+ // `<id>.<kind>.jsonl` (current) OR `<id>.<kind>.<digits>.jsonl` (archive). The
241
+ // `<id>` is greedy (`.+`) and may itself contain dots. Quarantine files
242
+ // (`<id>.<kind>.quarantine.<digits>.jsonl`) are EXCLUDED — they are a fenced
243
+ // old incarnation, never part of the live union.
244
+ const re = new RegExp(`^(.+)\\.${k}(?:\\.\\d+)?\\.jsonl$`);
245
+ const out = [];
246
+ for (const n of names) {
247
+ if (n.includes('.quarantine.'))
248
+ continue;
249
+ if (re.test(n))
250
+ out.push(path.join(dir, n));
251
+ }
252
+ return out;
253
+ }
254
+ /** Archive files `<safe>.<kind>.<stamp>.jsonl` in `dir`, newest-first. */
255
+ archivesFor(dir, safe, kind) {
256
+ let names;
257
+ try {
258
+ names = this.io.readdirSync(dir);
259
+ }
260
+ catch { /* @silent-fallback-ok: dir absent = nothing to read — empty, never an error. */
261
+ return [];
262
+ }
263
+ const re = new RegExp(`^${escapeRegExp(`${safe}.${kind}.`)}(\\d+)\\.jsonl$`);
264
+ const archives = [];
265
+ for (const n of names) {
266
+ const m = re.exec(n);
267
+ if (m)
268
+ archives.push({ file: path.join(dir, n), stamp: Number(m[1]) });
269
+ }
270
+ archives.sort((a, b) => b.stamp - a.stamp);
271
+ return archives.map((a) => a.file);
272
+ }
273
+ }
274
+ //# sourceMappingURL=ReplicatedPeerStreamReader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ReplicatedPeerStreamReader.js","sourceRoot":"","sources":["../../src/core/ReplicatedPeerStreamReader.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,EAIL,iBAAiB,EACjB,gBAAgB,GACjB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,kBAAkB,EAAqB,MAAM,yBAAyB,CAAC;AAChF,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b,CAAC;IAED,0EAA0E;IAClE,WAAW,CAAC,GAAW,EAAE,IAAY,EAAE,IAAiB;QAC9D,IAAI,KAAe,CAAC;QACpB,IAAI,CAAC;YACH,KAAK,GAAG,IAAI,CAAC,EAAE,CAAC,WAAW,CAAC,GAAG,CAAa,CAAC;QAC/C,CAAC;QAAC,MAAM,CAAC,CAAC,gFAAgF;YACxF,OAAO,EAAE,CAAC;QACZ,CAAC;QACD,MAAM,EAAE,GAAG,IAAI,MAAM,CAAC,IAAI,YAAY,CAAC,GAAG,IAAI,IAAI,IAAI,GAAG,CAAC,iBAAiB,CAAC,CAAC;QAC7E,MAAM,QAAQ,GAAsC,EAAE,CAAC;QACvD,KAAK,MAAM,CAAC,IAAI,KAAK,EAAE,CAAC;YACtB,MAAM,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;YACrB,IAAI,CAAC;gBAAE,QAAQ,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;QACzE,CAAC;QACD,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC;QAC3C,OAAO,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;IACrC,CAAC;CACF"}
@@ -0,0 +1,102 @@
1
+ /**
2
+ * ReplicatedRecordEmitter — the GENERIC journal-backed SEND emitter (WS2 send-side).
3
+ *
4
+ * Spec: docs/specs/WS2-SEND-SIDE-EMISSION-SPEC.md §4; the substrate it rides is
5
+ * docs/specs/multi-machine-replicated-store-foundation.md §4 (the envelope +
6
+ * flag-gated emission) and §7.2 (the `observed` last-writer-witness).
7
+ *
8
+ * THE GAP THIS CLOSES: every memory manager already calls an internal
9
+ * `emitter.emitPut(record)` / `emitter.emitDelete(...)` hook on each write, behind a
10
+ * `*ReplicationEmitter | null` seam — but server.ts never constructed the concrete
11
+ * emitter those hooks call (it was deferred to "a later rollout stage" that never
12
+ * came), so the hooks fired into a no-op and nothing reached the journal own-streams.
13
+ * This class IS that concrete emitter; server.ts builds ONE and adapts it to each
14
+ * manager's emit seam (a tiny per-store adapter that names the store's
15
+ * `build*RecordData`).
16
+ *
17
+ * It is store-AGNOSTIC: `emit(store, recordKey, build)` resolves the store's journal
18
+ * kind from the injected `ReplicatedKindRegistry`, gates on
19
+ * `multiMachine.stateSync.<store>.enabled` (dark by default), stamps the HLC, derives
20
+ * the `observed` witness, asks the store's builder for the disclosure-minimized
21
+ * envelope `data`, and appends it via `CoherenceJournal.emitReplicatedRecord`. ONE
22
+ * generic path serves all 7 kinds; the only per-store thing is the builder closure.
23
+ *
24
+ * SAFETY: NEVER throws into the caller (the manager hooks are best-effort). A disabled
25
+ * store, an unregistered store, a degenerate (null) recordKey, a builder that returns
26
+ * null or throws (e.g. a record over its per-entry byte cap) — every one is a counted
27
+ * no-op, never an exception that would break the local write the agent is performing.
28
+ * Emission is SINGLE-ORIGIN by construction: it stamps `origin = this machine` and the
29
+ * journal writes only this machine's own stream (§6.1 anti-forgery holds end-to-end).
30
+ */
31
+ import type { HlcTimestamp } from './HybridLogicalClock.js';
32
+ import { type ReplicatedKindRegistry, type StateSyncStores } from './ReplicatedRecordEnvelope.js';
33
+ /** The minimal journal seam the emitter needs (so it is unit-testable with a fake). */
34
+ export interface ReplicatedRecordEmitterJournal {
35
+ /** Append a built replicated-record envelope `data` for `kind` (no-op when the
36
+ * kind is not a registered replicated kind; never throws). */
37
+ emitReplicatedRecord(kind: string, data: Record<string, unknown>): void;
38
+ }
39
+ /** The minimal HLC seam (only `tick()` is needed on the author side). */
40
+ export interface ReplicatedRecordEmitterClock {
41
+ /** Local-event advance — strictly greater than every prior tick/receive (§3.2.1). */
42
+ tick(): HlcTimestamp;
43
+ }
44
+ /** A builder closure: produce the disclosure-minimized envelope `data` for this write
45
+ * given the freshly-ticked `hlc`, this machine's `origin`, and the `observed` witness
46
+ * (absent ⇒ no prior witness). Returns null to SKIP (a degenerate record with no
47
+ * stable identity surface). May THROW on an over-cap record — the emitter catches it. */
48
+ export type BuildRecordData = (hlc: HlcTimestamp, origin: string, observed: HlcTimestamp | undefined) => Record<string, unknown> | null;
49
+ /** The DI'd seams (asserted real, never null/no-op, by the wiring-integrity test). */
50
+ export interface ReplicatedRecordEmitterSeams {
51
+ /** The journal append sink. */
52
+ journal: ReplicatedRecordEmitterJournal;
53
+ /** The author-side HLC clock (persisted; one per machine). */
54
+ clock: ReplicatedRecordEmitterClock;
55
+ /** The replicated-kind registry — resolves store → journal kind; the emitter only
56
+ * emits a registered store. */
57
+ registry: ReplicatedKindRegistry;
58
+ /** This machine's origin id (stamped on every emitted record — single-origin). */
59
+ origin: string;
60
+ /** The per-store stateSync flags (the dark-by-default gate). A getter so a live
61
+ * config flip is honored without reconstructing the emitter. */
62
+ stores: () => StateSyncStores | undefined;
63
+ /**
64
+ * The last-writer-witness source (§7.2): the MAX HLC over every origin record this
65
+ * machine CURRENTLY holds on disk for (store, recordKey) — own prior + applied
66
+ * peers. Sound by construction: it can only witness a version provably on disk, so a
67
+ * not-yet-pulled peer version is simply absent ⇒ the merge flags concurrent
68
+ * (err-toward-flag), never a silent clobber. Returns undefined when none is held
69
+ * (the first write of a key) ⇒ `observed` omitted. Best-effort; throwing is caught.
70
+ */
71
+ loadWitness: (store: string, recordKey: string) => HlcTimestamp | undefined;
72
+ /** Optional structured logger (default no-op). */
73
+ log?: (event: string, detail: Record<string, unknown>) => void;
74
+ }
75
+ /** Lifetime counters for observability (mirrors the journal's degradation style). */
76
+ export interface ReplicatedRecordEmitterStats {
77
+ /** Records appended to the journal. */
78
+ emitted: number;
79
+ /** No-ops because the store was disabled (dark). */
80
+ storeDisabled: number;
81
+ /** No-ops because the recordKey was null/degenerate or the builder returned null. */
82
+ skipped: number;
83
+ /** Builder/journal throws caught (never propagated to the manager). */
84
+ errors: number;
85
+ }
86
+ export declare class ReplicatedRecordEmitter {
87
+ private readonly seams;
88
+ private readonly stats;
89
+ constructor(seams: ReplicatedRecordEmitterSeams);
90
+ /** Read-only stats (for observability + the wiring-integrity assertions). */
91
+ getStats(): Readonly<ReplicatedRecordEmitterStats>;
92
+ /**
93
+ * Emit one replicated record for (store, recordKey). The single generic path for a
94
+ * put OR a delete — the only difference is the `build` closure (a put builder vs a
95
+ * tombstone builder). Never throws into the caller.
96
+ *
97
+ * Order (§4): dark gate → degenerate guard → witness → tick → build → append.
98
+ */
99
+ emit(store: string, recordKey: string | null | undefined, build: BuildRecordData): void;
100
+ private log;
101
+ }
102
+ //# sourceMappingURL=ReplicatedRecordEmitter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ReplicatedRecordEmitter.d.ts","sourceRoot":"","sources":["../../src/core/ReplicatedRecordEmitter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,EAEL,KAAK,sBAAsB,EAC3B,KAAK,eAAe,EACrB,MAAM,+BAA+B,CAAC;AAEvC,uFAAuF;AACvF,MAAM,WAAW,8BAA8B;IAC7C;mEAC+D;IAC/D,oBAAoB,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;CACzE;AAED,yEAAyE;AACzE,MAAM,WAAW,4BAA4B;IAC3C,qFAAqF;IACrF,IAAI,IAAI,YAAY,CAAC;CACtB;AAED;;;0FAG0F;AAC1F,MAAM,MAAM,eAAe,GAAG,CAC5B,GAAG,EAAE,YAAY,EACjB,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,YAAY,GAA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,122 @@
1
+ /**
2
+ * ReplicatedRecordEmitter — the GENERIC journal-backed SEND emitter (WS2 send-side).
3
+ *
4
+ * Spec: docs/specs/WS2-SEND-SIDE-EMISSION-SPEC.md §4; the substrate it rides is
5
+ * docs/specs/multi-machine-replicated-store-foundation.md §4 (the envelope +
6
+ * flag-gated emission) and §7.2 (the `observed` last-writer-witness).
7
+ *
8
+ * THE GAP THIS CLOSES: every memory manager already calls an internal
9
+ * `emitter.emitPut(record)` / `emitter.emitDelete(...)` hook on each write, behind a
10
+ * `*ReplicationEmitter | null` seam — but server.ts never constructed the concrete
11
+ * emitter those hooks call (it was deferred to "a later rollout stage" that never
12
+ * came), so the hooks fired into a no-op and nothing reached the journal own-streams.
13
+ * This class IS that concrete emitter; server.ts builds ONE and adapts it to each
14
+ * manager's emit seam (a tiny per-store adapter that names the store's
15
+ * `build*RecordData`).
16
+ *
17
+ * It is store-AGNOSTIC: `emit(store, recordKey, build)` resolves the store's journal
18
+ * kind from the injected `ReplicatedKindRegistry`, gates on
19
+ * `multiMachine.stateSync.<store>.enabled` (dark by default), stamps the HLC, derives
20
+ * the `observed` witness, asks the store's builder for the disclosure-minimized
21
+ * envelope `data`, and appends it via `CoherenceJournal.emitReplicatedRecord`. ONE
22
+ * generic path serves all 7 kinds; the only per-store thing is the builder closure.
23
+ *
24
+ * SAFETY: NEVER throws into the caller (the manager hooks are best-effort). A disabled
25
+ * store, an unregistered store, a degenerate (null) recordKey, a builder that returns
26
+ * null or throws (e.g. a record over its per-entry byte cap) — every one is a counted
27
+ * no-op, never an exception that would break the local write the agent is performing.
28
+ * Emission is SINGLE-ORIGIN by construction: it stamps `origin = this machine` and the
29
+ * journal writes only this machine's own stream (§6.1 anti-forgery holds end-to-end).
30
+ */
31
+ import { isStoreEmissionEnabled, } from './ReplicatedRecordEnvelope.js';
32
+ export class ReplicatedRecordEmitter {
33
+ seams;
34
+ stats = {
35
+ emitted: 0,
36
+ storeDisabled: 0,
37
+ skipped: 0,
38
+ errors: 0,
39
+ };
40
+ constructor(seams) {
41
+ // Wiring-integrity preconditions: the seams MUST be real, not null/no-op.
42
+ if (!seams)
43
+ throw new Error('ReplicatedRecordEmitter: seams are required');
44
+ if (!seams.journal || typeof seams.journal.emitReplicatedRecord !== 'function') {
45
+ throw new Error('ReplicatedRecordEmitter: journal.emitReplicatedRecord seam must be a function (not a no-op)');
46
+ }
47
+ if (!seams.clock || typeof seams.clock.tick !== 'function') {
48
+ throw new Error('ReplicatedRecordEmitter: clock.tick seam must be a function');
49
+ }
50
+ if (!seams.registry)
51
+ throw new Error('ReplicatedRecordEmitter: registry seam is required (not null)');
52
+ if (typeof seams.origin !== 'string' || seams.origin.length === 0) {
53
+ throw new Error('ReplicatedRecordEmitter: origin must be a non-empty string');
54
+ }
55
+ if (typeof seams.stores !== 'function')
56
+ throw new Error('ReplicatedRecordEmitter: stores seam must be a function');
57
+ if (typeof seams.loadWitness !== 'function')
58
+ throw new Error('ReplicatedRecordEmitter: loadWitness seam must be a function (not a no-op)');
59
+ this.seams = seams;
60
+ }
61
+ /** Read-only stats (for observability + the wiring-integrity assertions). */
62
+ getStats() {
63
+ return { ...this.stats };
64
+ }
65
+ /**
66
+ * Emit one replicated record for (store, recordKey). The single generic path for a
67
+ * put OR a delete — the only difference is the `build` closure (a put builder vs a
68
+ * tombstone builder). Never throws into the caller.
69
+ *
70
+ * Order (§4): dark gate → degenerate guard → witness → tick → build → append.
71
+ */
72
+ emit(store, recordKey, build) {
73
+ try {
74
+ // 1. Dark gate (the default). A disabled store emits NOTHING — a strict no-op.
75
+ if (!isStoreEmissionEnabled(this.seams.stores(), store)) {
76
+ this.stats.storeDisabled++;
77
+ return;
78
+ }
79
+ // 2. Only a registered store has a journal kind to ride.
80
+ const reg = this.seams.registry.getByStore(store);
81
+ if (!reg) {
82
+ this.stats.skipped++;
83
+ return;
84
+ }
85
+ // 3. Degenerate guard — a null/empty recordKey has no stable identity surface.
86
+ if (typeof recordKey !== 'string' || recordKey.length === 0) {
87
+ this.stats.skipped++;
88
+ return;
89
+ }
90
+ // 4. Witness BEFORE the tick — the HLC this machine had already merged for the
91
+ // key (own prior + applied peers). Best-effort; a witness read fault degrades
92
+ // to "no witness" (flag-on-conflict, the safe direction), never a throw.
93
+ let observed;
94
+ try {
95
+ observed = this.seams.loadWitness(store, recordKey);
96
+ }
97
+ catch (e) { /* @silent-fallback-ok: a witness read fault degrades to no-witness ⇒ flag-on-conflict (the safe merge direction, §7.2); never blocks the emit. */
98
+ observed = undefined;
99
+ this.log('witness-read-failed', { store, error: e?.message });
100
+ }
101
+ // 5. Tick AFTER the witness so hlc > observed (a clean sequential position).
102
+ const hlc = this.seams.clock.tick();
103
+ // 6. Build the disclosure-minimized envelope data (store-specific).
104
+ const data = build(hlc, this.seams.origin, observed);
105
+ if (data === null || data === undefined) {
106
+ this.stats.skipped++;
107
+ return;
108
+ }
109
+ // 7. Append. The journal validates + op-key-dedupes + enqueues (non-blocking).
110
+ this.seams.journal.emitReplicatedRecord(reg.kind, data);
111
+ this.stats.emitted++;
112
+ }
113
+ catch (e) { /* @silent-fallback-ok: the manager's write must NEVER fail because replication did — a builder throw (e.g. over-cap) / journal fault is a counted no-op, surfaced via stats + the log, never propagated (Structure > Willpower: the safety is structural, not per-caller). */
114
+ this.stats.errors++;
115
+ this.log('emit-failed', { store, error: e?.message });
116
+ }
117
+ }
118
+ log(event, detail) {
119
+ this.seams.log?.(event, detail);
120
+ }
121
+ }
122
+ //# sourceMappingURL=ReplicatedRecordEmitter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ReplicatedRecordEmitter.js","sourceRoot":"","sources":["../../src/core/ReplicatedRecordEmitter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAGH,OAAO,EACL,sBAAsB,GAGvB,MAAM,+BAA+B,CAAC;AAgEvC,MAAM,OAAO,uBAAuB;IACjB,KAAK,CAA+B;IACpC,KAAK,GAAiC;QACrD,OAAO,EAAE,CA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xC,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC;gBACrB,OAAO;YACT,CAAC;YACD,+EAA+E;YAC/E,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,oBAAoB,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;YACxD,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC;QACvB,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC,CAAC,8QAA8Q;YAC1R,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC;YACpB,IAAI,CAAC,GAAG,CAAC,aAAa,EAAE,EAAE,KAAK,EAAE,KAAK,EAAG,CAAW,EAAE,OAAO,EAAE,CAAC,CAAC;QACnE,CAAC;IACH,CAAC;IAEO,GAAG,CAAC,KAAa,EAAE,MAA+B;QACxD,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IAClC,CAAC;CACF"}
@@ -0,0 +1,52 @@
1
+ /**
2
+ * ws2SendWiring — the SEND-side wiring manifest + the wiring-integrity ratchet
3
+ * (WS2 send-side, docs/specs/WS2-SEND-SIDE-EMISSION-SPEC.md §6/§7).
4
+ *
5
+ * THE INVARIANT THIS ENFORCES: every replicated store registered in the
6
+ * `ReplicatedKindRegistry` (the RECEIVE/advert half) must be CONSCIOUSLY classified
7
+ * as either SEND-WIRED (its manager's emit hooks are attached to the journal-backed
8
+ * emitter) or SEND-PENDING (a known, enumerated follow-up). A new replicated kind
9
+ * added to the registry WITHOUT placing it in one of these sets fails the ratchet —
10
+ * which is the EXACT gap this workstream fixes: a kind shipped receive-only (advert +
11
+ * apply machinery) with the SEND half silently a no-op. The ratchet makes that a CI
12
+ * failure, not a memory item (Structure > Willpower).
13
+ *
14
+ * Pure data + a pure check — no I/O, no deps. The server's wiring + the
15
+ * wiring-integrity test BOTH read these sets, so they cannot drift.
16
+ */
17
+ /** Stores whose manager emit hooks ARE attached to the journal-backed emitter (their
18
+ * records actually cross). The learnings slice ships first; the other seamed stores
19
+ * follow on the SAME emitter (WS2-SEND-2). */
20
+ export declare const WS2_SEND_WIRED_STORES: ReadonlyArray<string>;
21
+ /**
22
+ * Stores registered for RECEIVE but whose SEND wiring is a KNOWN, enumerated
23
+ * follow-up — NOT a silent omission. Each is here for a stated reason:
24
+ * - relationships / knowledge / evolutionActions / userRegistry: fully seamed
25
+ * managers (emitPut + emitDelete); table-row wiring onto the same emitter (WS2-SEND-2).
26
+ * - topicOperator: seamed put-only (no emitDelete in its manager interface yet) (WS2-SEND-3).
27
+ * - preferences: NO manager emit seam yet — it rode the deprecated `preferences-sync`
28
+ * verb; needs a manager emit hook before it can be wired (WS2-SEND-3).
29
+ */
30
+ export declare const WS2_SEND_PENDING_STORES: ReadonlyArray<string>;
31
+ /** A registered store's send-wiring classification. */
32
+ export type Ws2SendStatus = 'wired' | 'pending' | 'unclassified';
33
+ /** Classify a registered store's send-wiring status. */
34
+ export declare function ws2SendStatus(store: string): Ws2SendStatus;
35
+ /** The ratchet result: any registered store that is neither wired nor pending. */
36
+ export interface Ws2SendWiringAudit {
37
+ wired: string[];
38
+ pending: string[];
39
+ /** Registered stores in NEITHER set — the failure condition (a silent receive-only kind). */
40
+ unclassified: string[];
41
+ ok: boolean;
42
+ }
43
+ /**
44
+ * Audit a set of registered store keys against the wiring manifest. `ok` is false iff
45
+ * any registered store is unclassified (neither wired nor pending) — the exact
46
+ * receive-only gap this workstream closes. Also flags a store in BOTH sets (a manifest
47
+ * authoring error) as unclassified-style failure via a thrown precondition is avoided;
48
+ * instead WIRED takes precedence and the overlap is surfaced by the caller's own
49
+ * disjointness assertion in the test.
50
+ */
51
+ export declare function auditWs2SendWiring(registeredStores: ReadonlyArray<string>): Ws2SendWiringAudit;
52
+ //# sourceMappingURL=ws2SendWiring.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ws2SendWiring.d.ts","sourceRoot":"","sources":["../../src/core/ws2SendWiring.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH;;+CAE+C;AAC/C,eAAO,MAAM,qBAAqB,EAAE,aAAa,CAAC,MAAM,CAEtD,CAAC;AAEH;;;;;;;;GAQG;AACH,eAAO,MAAM,uBAAuB,EAAE,aAAa,CAAC,MAAM,CAOxD,CAAC;AAEH,uDAAuD;AACvD,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG,SAAS,GAAG,cAAc,CAAC;AAEjE,wDAAwD;AACxD,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,aAAa,CAI1D;AAED,kFAAkF;AAClF,MAAM,WAAW,kBAAkB;IACjC,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,6FAA6F;IAC7F,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,EAAE,EAAE,OAAO,CAAC;CACb;AAED;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAAC,gBAAgB,EAAE,aAAa,CAAC,MAAM,CAAC,GAAG,kBAAkB,CAW9F"}
@@ -0,0 +1,71 @@
1
+ /**
2
+ * ws2SendWiring — the SEND-side wiring manifest + the wiring-integrity ratchet
3
+ * (WS2 send-side, docs/specs/WS2-SEND-SIDE-EMISSION-SPEC.md §6/§7).
4
+ *
5
+ * THE INVARIANT THIS ENFORCES: every replicated store registered in the
6
+ * `ReplicatedKindRegistry` (the RECEIVE/advert half) must be CONSCIOUSLY classified
7
+ * as either SEND-WIRED (its manager's emit hooks are attached to the journal-backed
8
+ * emitter) or SEND-PENDING (a known, enumerated follow-up). A new replicated kind
9
+ * added to the registry WITHOUT placing it in one of these sets fails the ratchet —
10
+ * which is the EXACT gap this workstream fixes: a kind shipped receive-only (advert +
11
+ * apply machinery) with the SEND half silently a no-op. The ratchet makes that a CI
12
+ * failure, not a memory item (Structure > Willpower).
13
+ *
14
+ * Pure data + a pure check — no I/O, no deps. The server's wiring + the
15
+ * wiring-integrity test BOTH read these sets, so they cannot drift.
16
+ */
17
+ /** Stores whose manager emit hooks ARE attached to the journal-backed emitter (their
18
+ * records actually cross). The learnings slice ships first; the other seamed stores
19
+ * follow on the SAME emitter (WS2-SEND-2). */
20
+ export const WS2_SEND_WIRED_STORES = Object.freeze([
21
+ 'learnings',
22
+ ]);
23
+ /**
24
+ * Stores registered for RECEIVE but whose SEND wiring is a KNOWN, enumerated
25
+ * follow-up — NOT a silent omission. Each is here for a stated reason:
26
+ * - relationships / knowledge / evolutionActions / userRegistry: fully seamed
27
+ * managers (emitPut + emitDelete); table-row wiring onto the same emitter (WS2-SEND-2).
28
+ * - topicOperator: seamed put-only (no emitDelete in its manager interface yet) (WS2-SEND-3).
29
+ * - preferences: NO manager emit seam yet — it rode the deprecated `preferences-sync`
30
+ * verb; needs a manager emit hook before it can be wired (WS2-SEND-3).
31
+ */
32
+ export const WS2_SEND_PENDING_STORES = Object.freeze([
33
+ 'relationships',
34
+ 'knowledge',
35
+ 'evolutionActions',
36
+ 'userRegistry',
37
+ 'topicOperator',
38
+ 'preferences',
39
+ ]);
40
+ /** Classify a registered store's send-wiring status. */
41
+ export function ws2SendStatus(store) {
42
+ if (WS2_SEND_WIRED_STORES.includes(store))
43
+ return 'wired';
44
+ if (WS2_SEND_PENDING_STORES.includes(store))
45
+ return 'pending';
46
+ return 'unclassified';
47
+ }
48
+ /**
49
+ * Audit a set of registered store keys against the wiring manifest. `ok` is false iff
50
+ * any registered store is unclassified (neither wired nor pending) — the exact
51
+ * receive-only gap this workstream closes. Also flags a store in BOTH sets (a manifest
52
+ * authoring error) as unclassified-style failure via a thrown precondition is avoided;
53
+ * instead WIRED takes precedence and the overlap is surfaced by the caller's own
54
+ * disjointness assertion in the test.
55
+ */
56
+ export function auditWs2SendWiring(registeredStores) {
57
+ const wired = [];
58
+ const pending = [];
59
+ const unclassified = [];
60
+ for (const store of registeredStores) {
61
+ const status = ws2SendStatus(store);
62
+ if (status === 'wired')
63
+ wired.push(store);
64
+ else if (status === 'pending')
65
+ pending.push(store);
66
+ else
67
+ unclassified.push(store);
68
+ }
69
+ return { wired, pending, unclassified, ok: unclassified.length === 0 };
70
+ }
71
+ //# sourceMappingURL=ws2SendWiring.js.map