@kairos-es/core 1.0.0-alpha.5 → 1.0.0-alpha.6

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.
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.subscriptionTuningFields = exports.subscribeStream = exports.DEFAULT_POLL_INTERVAL_MILLIS = exports.DEFAULT_CATCH_UP_PAGE_SIZE = void 0;
6
+ exports.subscriptionTuningFields = exports.subscribeStream = exports.subscribeItems = exports.DEFAULT_POLL_INTERVAL_MILLIS = exports.DEFAULT_CATCH_UP_PAGE_SIZE = void 0;
7
7
  var _effect = require("effect");
8
8
  var _Event = require("./Event.js");
9
9
  /**
@@ -13,6 +13,15 @@ var _Event = require("./Event.js");
13
13
  * the cursor's exclusive lower bound) and the poll loop that together turn a paged
14
14
  * read into a live subscription.
15
15
  *
16
+ * The algorithm is `subscribeItems`, generic over the item `A` it delivers and the
17
+ * cursor `C` it resumes from, because nothing in it is about an event: any source
18
+ * that can page-read strictly after a cursor and name the cursor of what it
19
+ * returned is subscribable by the same loop. `subscribeStream` is the event-store
20
+ * shape — `SequencedEvent` ordered by `Position` — over the same loop, and it is
21
+ * what every engine calls, unchanged by the generalisation. A second kind of
22
+ * source reuses the loop rather than growing a second one, for the reason the next
23
+ * paragraph gives.
24
+ *
16
25
  * It lives in core because delivery semantics are contract semantics. An engine
17
26
  * supplies TRANSPORT through two STUBBABLE seams — `fetch`, a paged read strictly
18
27
  * after a cursor, and `listen`, an optional wake source — and inherits the delivery
@@ -23,48 +32,52 @@ var _Event = require("./Event.js");
23
32
  * Both seams take plain in-memory values in a test, so the wake/poll/de-dup logic
24
33
  * is exercisable with no live backend at all.
25
34
  *
26
- * All THREE are the callers, and the count matters to a reader here: it means there
27
- * is exactly ONE delivery algorithm in this repo, and it is the code below. The
35
+ * All THREE engines are callers, and the count matters to a reader here: it means
36
+ * there is exactly ONE delivery algorithm in this repo, and it is the code below. The
28
37
  * in-memory store is a caller like the rest — it hands in a `fetch` over its log
29
38
  * `Ref` and its append `PubSub` as a wake source and keeps no delivery logic of its
30
39
  * own — which is what makes the shared contract suite's subscribe cases a comparison
31
40
  * of two BACKENDS rather than of two delivery algorithms.
32
41
  *
33
- * Correctness rests on the periodic position-based reconcile — a re-read from the
42
+ * Correctness rests on the periodic cursor-based reconcile — a re-read from the
34
43
  * cursor every poll interval — and NEVER on the wake source, which only lowers
35
44
  * delivery latency below that interval. A subscription with no wake source at all
36
45
  * is therefore complete and correct, merely slower, which is why `listen` is
37
46
  * optional in the TYPE rather than by the convention of passing `Stream.never`:
38
47
  * poll-only delivery is a supported shape, not a degraded one.
39
48
  *
40
- * ## The precondition every engine inherits: ASCENDING COMMIT ORDER
49
+ * ## The precondition every caller inherits: ASCENDING COMMIT ORDER
41
50
  *
42
- * A position that is committed and VISIBLE must never be below one already
43
- * delivered. Gaps are fine: the cursor is an ascending exclusive lower bound, so a
44
- * number that is allocated and never committed, or never allocated at all, is
45
- * simply a number the machine never sees. Out-of-order commits are not fine. If one
51
+ * It binds EVERY caller, over whatever cursor it chose — an engine's `Position`, or
52
+ * any other `C` handed to `subscribeItems` — and the example below is written in
53
+ * positions only because the engines are the callers it was first written for. A
54
+ * cursor that is committed and VISIBLE must never be below one already delivered.
55
+ * Gaps are fine: the cursor is an ascending exclusive lower bound, so a number
56
+ * that is allocated and never committed, or never allocated at all, is simply a
57
+ * number the machine never sees. Out-of-order commits are not fine. If one
46
58
  * writer allocates position 5 and another allocates 6, and 6 commits first, a poll
47
59
  * landing between the two commits delivers 6 and advances the cursor past it — and
48
60
  * 5 is then lost forever, because no later read looks below the cursor and nothing
49
- * anywhere notices the hole. An engine whose concurrent writers can commit out of
61
+ * anywhere notices the hole. A caller whose concurrent writers can commit out of
50
62
  * order MUST serialise them, or this machine will silently skip. An engine that
51
63
  * takes an exclusive lock on both of its append paths discharges the obligation
52
64
  * that way; an engine whose append is a single statement discharges it
53
65
  * structurally.
54
66
  *
55
67
  * The obligation is written down HERE, in the algorithm that depends on it, rather
56
- * than in whichever engine happens to satisfy it. It is a property of this delivery
57
- * loop and of nothing else, so an engine author reading only their own source has
68
+ * than in whichever caller happens to satisfy it. It is a property of this delivery
69
+ * loop and of nothing else, so an author reading only their own source has
58
70
  * no way to discover that anything downstream needs it — and the failure mode is
59
71
  * the worst kind there is: no error, no defect, no failing test, just an event that
60
72
  * a subscriber never receives. A single engine owning both its append path and its
61
73
  * own wake loop can leave the assumption implicit; a shared machine cannot.
62
74
  *
63
- * ## `catchUpPageSize` bounds the READ, not the emission
75
+ * ## `catchUpPageSize` bounds the READ, not the emission — unless `emit: 'page'`
64
76
  *
65
- * `drainFrom` below PAGES the catch-up read — `catchUpPageSize` bounds each
66
- * `fetch` — but it COLLECTS every page into one array, which is emitted as a SINGLE
67
- * chunk. So peak memory at subscription start is O(backlog) rather than O(page): a
77
+ * Under the default `emit: 'backlog'`, which is what `subscribeStream` and so every
78
+ * engine uses, `drainFrom` below PAGES the catch-up read — `catchUpPageSize` bounds
79
+ * each `fetch` — but it COLLECTS every page into one array, which is emitted as a
80
+ * SINGLE chunk. So peak memory at subscription start is O(backlog) rather than O(page): a
68
81
  * subscriber joining behind a large log materialises the whole backlog before its
69
82
  * consumer sees the first element.
70
83
  *
@@ -75,6 +88,19 @@ var _Event = require("./Event.js");
75
88
  * stay small even against a one-chunk catch-up. The cost therefore lands on backlog
76
89
  * memory alone, and it lands hardest on the durable-log-with-in-memory-view mode,
77
90
  * where an ephemeral view re-catches-up the entire log on every boot.
91
+ *
92
+ * `emit: 'page'` changes exactly that and nothing else. Each fetched page is
93
+ * emitted as its own chunk and the cursor advances past it before the next read, so
94
+ * peak memory is O(page) and the consumer sees the first item after ONE read rather
95
+ * than after the whole backlog. Delivery is unchanged: the same items in the same
96
+ * order, the same de-dup, the same wake and poll. What a caller gives up is the
97
+ * one-chunk catch-up, so it must not read meaning into chunk boundaries under
98
+ * either mode. It is for a consumer that does NOT re-chunk downstream and whose
99
+ * catch-up can be the whole of a large source — a full resync of a table, say —
100
+ * where holding the backlog would be the cost the paging was meant to avoid. The
101
+ * engines keep `'backlog'` because the read side's re-chunking already bounds what
102
+ * they commit, and changing their emission would move a behaviour nobody asked to
103
+ * move.
78
104
  */
79
105
 
80
106
  /**
@@ -102,6 +128,10 @@ const DEFAULT_CATCH_UP_PAGE_SIZE = exports.DEFAULT_CATCH_UP_PAGE_SIZE = 512;
102
128
  * one-second fallback poll (`conn.wait(..., interval=1)`).
103
129
  */
104
130
  const DEFAULT_POLL_INTERVAL_MILLIS = exports.DEFAULT_POLL_INTERVAL_MILLIS = 1000;
131
+ /** A usable `pollInterval`: see `subscriptionTuningFields` for the bounds' argument. */
132
+ const PollIntervalMillis = /*#__PURE__*/_effect.Schema.Number.check(/*#__PURE__*/_effect.Schema.isGreaterThan(0), /*#__PURE__*/_effect.Schema.isFinite());
133
+ /** A usable `catchUpPageSize`: see `subscriptionTuningFields` for the bounds' argument. */
134
+ const CatchUpPageSize = /*#__PURE__*/_effect.Schema.Int.check(/*#__PURE__*/_effect.Schema.isGreaterThan(0));
105
135
  /**
106
136
  * The two subscribe-tuning config FIELDS, for an engine whose config is a `Schema`
107
137
  * to spread into its own struct.
@@ -132,8 +162,24 @@ const DEFAULT_POLL_INTERVAL_MILLIS = exports.DEFAULT_POLL_INTERVAL_MILLIS = 1000
132
162
  * absent key, never an explicit `undefined`.
133
163
  */
134
164
  const subscriptionTuningFields = exports.subscriptionTuningFields = {
135
- pollInterval: /*#__PURE__*/_effect.Schema.optionalKey(/*#__PURE__*/_effect.Schema.Number.check(/*#__PURE__*/_effect.Schema.isGreaterThan(0), /*#__PURE__*/_effect.Schema.isFinite())),
136
- catchUpPageSize: /*#__PURE__*/_effect.Schema.optionalKey(/*#__PURE__*/_effect.Schema.Int.check(/*#__PURE__*/_effect.Schema.isGreaterThan(0)))
165
+ pollInterval: /*#__PURE__*/_effect.Schema.optionalKey(PollIntervalMillis),
166
+ catchUpPageSize: /*#__PURE__*/_effect.Schema.optionalKey(CatchUpPageSize)
167
+ };
168
+ /**
169
+ * Why a caller-supplied tuning knob is unusable, naming the field and the value, or
170
+ * `undefined` when both are usable or absent. It judges with the SAME two schemas
171
+ * the config fields are built from, so `subscribeItems`, whose caller may have no
172
+ * config to decode, accepts exactly what an engine's config accepts and the bounds
173
+ * have one home.
174
+ */
175
+ const tuningFault = deps => {
176
+ if (deps.pollInterval !== undefined && !_effect.Schema.is(PollIntervalMillis)(deps.pollInterval)) {
177
+ return `subscribeItems: pollInterval must be a finite number of milliseconds greater than 0, got ${deps.pollInterval}`;
178
+ }
179
+ if (deps.catchUpPageSize !== undefined && !_effect.Schema.is(CatchUpPageSize)(deps.catchUpPageSize)) {
180
+ return `subscribeItems: catchUpPageSize must be an integer greater than 0, got ${deps.catchUpPageSize}`;
181
+ }
182
+ return undefined;
137
183
  };
138
184
  /**
139
185
  * Retry a TRANSIENT poll fault (an `E` from one reconcile read) on a capped
@@ -144,14 +190,40 @@ const subscriptionTuningFields = exports.subscriptionTuningFields = {
144
190
  */
145
191
  const POLL_RETRY_SCHEDULE = /*#__PURE__*/_effect.Schedule.max([/*#__PURE__*/_effect.Schedule.min([/*#__PURE__*/_effect.Schedule.exponential('100 millis', 2), /*#__PURE__*/_effect.Schedule.spaced('2 seconds')]), /*#__PURE__*/_effect.Schedule.recurs(20)]);
146
192
  /**
147
- * Build the subscription `Stream`: catch up from `after`, then track the live tail.
148
- * The cursor threads through the `Stream.paginate` state, starting at `after` and
149
- * advancing to the last delivered position each batch; the stream never completes
150
- * on its own (the next state is always `Option.some`), so the enclosing scope
151
- * interrupts it on close. An `E` mid-stream stays on the channel; `makeContractStore` dies it
152
- * (subscribe is `E = never` on the contract).
193
+ * Build a subscription `Stream` over any ascending-cursor source: catch up from
194
+ * `initial`, then track the live tail. The cursor threads through the
195
+ * `Stream.paginate` state, starting at `initial` and advancing to the cursor of the
196
+ * last delivered item each batch; the stream never completes on its own (the next
197
+ * state is always `Option.some`), so the enclosing scope interrupts it on close. An
198
+ * `E` mid-stream stays on the channel, for the caller to die or to supervise.
199
+ *
200
+ * This is THE delivery algorithm, and `subscribeStream` runs the same machine. Under
201
+ * `emit: 'backlog'` a batch is everything a drain to the tail returned; under
202
+ * `'page'` it is one fetched page, so the paginate step and the page coincide and a
203
+ * full page is followed at once by the next read rather than by a wait.
204
+ *
205
+ * A `pollInterval` or `catchUpPageSize` the config fields would refuse is a DEFECT
206
+ * here, raised by the stream before it reads or forks anything, with a message
207
+ * naming the field. This caller may have no config whose decode would have caught
208
+ * it, and unchecked the two failures are silent or late: a page size of 0 gives a
209
+ * stream that never emits and never fails, and 1.5 dies only at the first read,
210
+ * inside `ReadLimit`, in words that name neither knob.
153
211
  */
154
- const subscribeStream = (deps, after) => _effect.Stream.unwrap(_effect.Effect.gen(function* () {
212
+ const subscribeItems = deps => {
213
+ const fault = tuningFault(deps);
214
+ return fault === undefined ? machine(deps) : _effect.Stream.die(new Error(fault));
215
+ };
216
+ /**
217
+ * The machine itself, with no check of its tuning. `subscribeStream` runs it
218
+ * directly, and deliberately: the event path stays exactly as it was, with no
219
+ * second check of figures each SQL engine has already decoded from its config
220
+ * through the same bounds, and the in-memory store's are a test's own options.
221
+ */
222
+ exports.subscribeItems = subscribeItems;
223
+ const machine = deps => _effect.Stream.unwrap(_effect.Effect.gen(function* () {
224
+ const pageSize = deps.catchUpPageSize ?? DEFAULT_CATCH_UP_PAGE_SIZE;
225
+ const pollInterval = deps.pollInterval ?? DEFAULT_POLL_INTERVAL_MILLIS;
226
+ const limit = _Event.ReadLimit.make(pageSize);
155
227
  // The wake primitive: a sliding(1) queue holding at most one pending
156
228
  // signal. The wake source `offer`s an opaque wake on every notification;
157
229
  // the poll loop `take`s (with a timeout). The lost-wakeup guard is
@@ -172,62 +244,83 @@ const subscribeStream = (deps, after) => _effect.Stream.unwrap(_effect.Effect.ge
172
244
  // scope releases it. We never read its elements directly — every
173
245
  // notification is an opaque wake signal.
174
246
  yield* listener.pipe(_effect.Stream.runDrain, _effect.Effect.forkScoped);
247
+ // One page strictly after `from`: the step `drainFrom` loops, and the whole
248
+ // of a `'page'` read.
249
+ const fetchPage = from => deps.fetch(from, limit);
175
250
  // Reconcile: page-read from `from` until a short page, advancing to the last
176
- // delivered position. Retries a TRANSIENT poll fault on a capped schedule so
251
+ // delivered cursor. Retries a TRANSIENT poll fault on a capped schedule so
177
252
  // a single connection blip does not kill a days-long subscription; a
178
- // persistent fault propagates and dies.
253
+ // persistent fault propagates. The retry wraps the WHOLE drain, so a fault on
254
+ // page three re-reads pages one and two: nothing has been emitted yet, so
255
+ // that costs reads and never a duplicate.
179
256
  const drainFrom = from => _effect.Effect.gen(function* () {
180
257
  const collected = [];
181
258
  let cursor = from;
182
259
  // Loop pages until a short page signals the tail is drained.
183
260
  while (true) {
184
- const events = yield* deps.fetch(cursor, _Event.ReadLimit.make(deps.catchUpPageSize));
185
- for (const event of events) {
186
- collected.push(event);
261
+ const items = yield* fetchPage(cursor);
262
+ for (const item of items) {
263
+ collected.push(item);
187
264
  }
188
- const lastEvent = events.at(-1);
189
- if (lastEvent === undefined || events.length < deps.catchUpPageSize) {
265
+ const lastItem = items.at(-1);
266
+ if (lastItem === undefined || items.length < pageSize) {
190
267
  break;
191
268
  }
192
- cursor = lastEvent.position;
269
+ cursor = deps.cursorOf(lastItem);
193
270
  }
194
- const last = collected.at(-1)?.position ?? from;
195
- return {
196
- events: collected,
197
- last
198
- };
271
+ return collected;
199
272
  }).pipe(_effect.Effect.retry(POLL_RETRY_SCHEDULE));
273
+ // The read one paginate step performs, by emission mode. Under `'page'` the
274
+ // retry wraps the single page, which is the whole of what is unemitted.
275
+ const read = (deps.emit ?? 'backlog') === 'page' ? from => fetchPage(from).pipe(_effect.Effect.retry(POLL_RETRY_SCHEDULE)) : drainFrom;
200
276
  /**
201
- * Produce the NEXT non-empty batch from a cursor position, together with
202
- * the advanced cursor — a pure `Position → Effect<[events, next]>`. The
203
- * `> boundary` de-dup is enforced by `fetch`'s exclusive lower bound, so a
204
- * live re-read after a wake never re-emits an already-delivered position.
277
+ * Produce the NEXT non-empty batch from a cursor, together with the advanced
278
+ * cursor — a pure `C → Effect<[items, next]>`. The `> boundary` de-dup is
279
+ * enforced by `fetch`'s exclusive lower bound, so a live re-read after a wake
280
+ * never re-emits an already-delivered item.
205
281
  *
206
- * Drain the wake queue BEFORE reading (lost-wakeup guard), then drain from
207
- * the cursor. If a drain yields nothing, wait for a wake but only up to the
208
- * poll interval, then loop and re-read ANYWAY — the periodic position-based
209
- * poll that makes delivery correct independently of any wake. This effect
210
- * only completes by returning a non-empty batch; the scope interrupts it on
211
- * close.
282
+ * Drain the wake queue BEFORE reading (lost-wakeup guard), then read from the
283
+ * cursor. If a read yields nothing, wait for a wake but only up to the poll
284
+ * interval, then loop and re-read ANYWAY — the periodic cursor-based poll that
285
+ * makes delivery correct independently of any wake. This effect only
286
+ * completes by returning a non-empty batch; the scope interrupts it on close.
212
287
  */
213
288
  const nextBatch = from => _effect.Effect.gen(function* () {
214
289
  while (true) {
215
290
  // `Queue.clear` and not `Queue.takeAll`: the drain must NOT wait, and
216
291
  // `takeAll` suspends until at least one signal is present.
217
292
  yield* _effect.Queue.clear(wake);
218
- const {
219
- events,
220
- last
221
- } = yield* drainFrom(from);
222
- if (events.length > 0) {
223
- return [events, last];
293
+ const items = yield* read(from);
294
+ const last = items.at(-1);
295
+ if (last !== undefined) {
296
+ return [items, deps.cursorOf(last)];
224
297
  }
225
298
  // Nothing new: block on a wake, but only up to the poll interval — on
226
299
  // timeout we fall through and the loop re-reads regardless.
227
- yield* _effect.Effect.ignore(_effect.Effect.timeout(_effect.Queue.take(wake), deps.pollInterval));
300
+ yield* _effect.Effect.ignore(_effect.Effect.timeout(_effect.Queue.take(wake), pollInterval));
228
301
  }
229
302
  });
230
- return _effect.Stream.paginate(after ?? _Event.ORIGIN, from => _effect.Effect.map(nextBatch(from), ([events, last]) => [events, _effect.Option.some(last)]));
303
+ return _effect.Stream.paginate(deps.initial, from => _effect.Effect.map(nextBatch(from), ([items, next]) => [items, _effect.Option.some(next)]));
231
304
  }));
305
+ /**
306
+ * Build the event subscription `Stream`: catch up from `after`, then track the live
307
+ * tail. A THIN wrapper over the machine `subscribeItems` runs, fixing the item to
308
+ * `SequencedEvent`, the cursor to its `position`, the start to `ORIGIN` when `after`
309
+ * is absent, and the emission to the default `'backlog'` — so every engine's call
310
+ * site, and every figure it delivers, is what it was before the machine became
311
+ * generic. It skips `subscribeItems`' tuning check, for the reason on `machine`. An `E`
312
+ * mid-stream stays on the channel; `makeContractStore` dies it (subscribe is
313
+ * `E = never` on the contract).
314
+ */
315
+ const subscribeStream = (deps, after) => machine({
316
+ fetch: deps.fetch,
317
+ cursorOf: event => event.position,
318
+ initial: after ?? _Event.ORIGIN,
319
+ ...(deps.listen === undefined ? {} : {
320
+ listen: deps.listen
321
+ }),
322
+ pollInterval: deps.pollInterval,
323
+ catchUpPageSize: deps.catchUpPageSize
324
+ });
232
325
  exports.subscribeStream = subscribeStream;
233
326
  //# sourceMappingURL=SubscribeMachine.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"SubscribeMachine.js","names":["_effect","require","_Event","DEFAULT_CATCH_UP_PAGE_SIZE","exports","DEFAULT_POLL_INTERVAL_MILLIS","subscriptionTuningFields","pollInterval","Schema","optionalKey","Number","check","isGreaterThan","isFinite","catchUpPageSize","Int","POLL_RETRY_SCHEDULE","Schedule","max","min","exponential","spaced","recurs","subscribeStream","deps","after","Stream","unwrap","Effect","gen","wake","Queue","sliding","listener","listen","never","pipe","tap","offer","undefined","retry","runDrain","forkScoped","drainFrom","from","collected","cursor","events","fetch","ReadLimit","make","event","push","lastEvent","at","length","position","last","nextBatch","clear","ignore","timeout","take","paginate","ORIGIN","map","Option","some"],"sources":["../../src/SubscribeMachine.ts"],"sourcesContent":[null],"mappings":";;;;;;AAsEA,IAAAA,OAAA,GAAAC,OAAA;AAUA,IAAAC,MAAA,GAAAD,OAAA;A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,CAACR,cAAM,CAACY,KAAK,CAACxB,mBAAmB,CAAC,CAAC;EAE5C;;;;;;;;;;;;;EAaA,MAAM0C,SAAS,GACbd,IAAc,IAEdhB,cAAM,CAACC,GAAG,CAAC,aAAS;IAClB,OAAO,IAAI,EAAE;MACX;MACA;MACA,OAAOE,aAAK,CAAC4B,KAAK,CAAC7B,IAAI,CAAC;MACxB,MAAM;QAAEiB,MAAM;QAAEU;MAAI,CAAE,GAAG,OAAOd,SAAS,CAACC,IAAI,CAAC;MAC/C,IAAIG,MAAM,CAACQ,MAAM,GAAG,CAAC,EAAE;QACrB,OAAO,CAACR,MAAM,EAAEU,IAAI,CAAU;MAChC;MACA;MACA;MACA,OAAO7B,cAAM,CAACgC,MAAM,CAClBhC,cAAM,CAACiC,OAAO,CAAC9B,aAAK,CAAC+B,IAAI,CAAChC,IAAI,CAAC,EAAEN,IAAI,CAACjB,YAAY,CAAC,CACpD;IACH;EACF,CAAC,CAAC;EAEJ,OAAOmB,cAAM,CAACqC,QAAQ,CAACtC,KAAK,IAAIuC,aAAM,EAAGpB,IAAI,IAC3ChB,cAAM,CAACqC,GAAG,CACRP,SAAS,CAACd,IAAI,CAAC,EACf,CAAC,CAACG,MAAM,EAAEU,IAAI,CAAC,KAAK,CAACV,MAAM,EAAEmB,cAAM,CAACC,IAAI,CAACV,IAAI,CAAC,CAAU,CACzD,CACF;AACH,CAAC,CAAC,CACH;AAAArD,OAAA,CAAAmB,eAAA,GAAAA,eAAA","ignoreList":[]}
1
+ {"version":3,"file":"SubscribeMachine.js","names":["_effect","require","_Event","DEFAULT_CATCH_UP_PAGE_SIZE","exports","DEFAULT_POLL_INTERVAL_MILLIS","PollIntervalMillis","Schema","Number","check","isGreaterThan","isFinite","CatchUpPageSize","Int","subscriptionTuningFields","pollInterval","optionalKey","catchUpPageSize","tuningFault","deps","undefined","is","POLL_RETRY_SCHEDULE","Schedule","max","min","exponential","spaced","recurs","subscribeItems","fault","machine","Stream","die","Error","unwrap","Effect","gen","pageSize","limit","ReadLimit","make","wake","Queue","sliding","listener","listen","never","pipe","tap","offer","retry","runDrain","forkScoped","fetchPage","from","fetch","drainFrom","collected","cursor","items","item","push","lastItem","at","length","cursorOf","read","emit","nextBatch","clear","last","ignore","timeout","take","paginate","initial","map","next","Option","some","subscribeStream","after","event","position","ORIGIN"],"sources":["../../src/SubscribeMachine.ts"],"sourcesContent":[null],"mappings":";;;;;;AAgGA,IAAAA,OAAA,GAAAC,OAAA;AAUA,IAAAC,MAAA,GAAAD,OAAA;AA1GA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4GA;;;;;;;AAOO,MAAME,0BAA0B,GAAAC,OAAA,CAAAD,0BAAA,GAAG,GAAG;AAE7C;;;;;;;;;;;;;;;;AAgBO,MAAME,4BAA4B,GAAAD,OAAA,CAAAC,4BAAA,GAAG,IAAI;AAEhD;AACA,MAAMC,kBAAkB,gBAAGC,cAAM,CAACC,MAAM,CAACC,KAAK,cAC5CF,cAAM,CAACG,aAAa,CAAC,CAAC,CAAC,eACvBH,cAAM,CAACI,QAAQ,EAAE,CAClB;AAED;AACA,MAAMC,eAAe,gBAAGL,cAAM,CAACM,GAAG,CAACJ,KAAK,cAACF,cAAM,CAACG,aAAa,CAAC,CAAC,CAAC,CAAC;AAEjE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BO,MAAMI,wBAAwB,GAAAV,OAAA,CAAAU,wBAAA,GAAG;EACtCC,YAAY,eAAER,cAAM,CAACS,WAAW,CAACV,kBAAkB,CAAC;EACpDW,eAAe,eAAEV,cAAM,CAACS,WAAW,CAACJ,eAAe;CAC3C;AAEV;;;;;;;AAOA,MAAMM,WAAW,GAAIC,IAGpB,IAAwB;EACvB,IACEA,IAAI,CAACJ,YAAY,KAAKK,SAAS,IAC/B,CAACb,cAAM,CAACc,EAAE,CAACf,kBAAkB,CAAC,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,GAAGA,CAC7B9D,IAAyB,EACzB+D,KAA2B,KAE3BnD,OAAO,CAA8B;EACnCyB,KAAK,EAAErC,IAAI,CAACqC,KAAK;EACjBU,QAAQ,EAAGiB,KAAK,IAAKA,KAAK,CAACC,QAAQ;EACnCR,OAAO,EAAEM,KAAK,IAAIG,aAAM;EACxB,IAAIlE,IAAI,CAAC2B,MAAM,KAAK1B,SAAS,GAAG,EAAE,GAAG;IAAE0B,MAAM,EAAE3B,IAAI,CAAC2B;EAAM,CAAE,CAAC;EAC7D/B,YAAY,EAAEI,IAAI,CAACJ,YAAY;EAC/BE,eAAe,EAAEE,IAAI,CAACF;CACvB,CAAC;AAAAb,OAAA,CAAA6E,eAAA,GAAAA,eAAA","ignoreList":[]}
@@ -5,6 +5,15 @@
5
5
  * the cursor's exclusive lower bound) and the poll loop that together turn a paged
6
6
  * read into a live subscription.
7
7
  *
8
+ * The algorithm is `subscribeItems`, generic over the item `A` it delivers and the
9
+ * cursor `C` it resumes from, because nothing in it is about an event: any source
10
+ * that can page-read strictly after a cursor and name the cursor of what it
11
+ * returned is subscribable by the same loop. `subscribeStream` is the event-store
12
+ * shape — `SequencedEvent` ordered by `Position` — over the same loop, and it is
13
+ * what every engine calls, unchanged by the generalisation. A second kind of
14
+ * source reuses the loop rather than growing a second one, for the reason the next
15
+ * paragraph gives.
16
+ *
8
17
  * It lives in core because delivery semantics are contract semantics. An engine
9
18
  * supplies TRANSPORT through two STUBBABLE seams — `fetch`, a paged read strictly
10
19
  * after a cursor, and `listen`, an optional wake source — and inherits the delivery
@@ -15,48 +24,52 @@
15
24
  * Both seams take plain in-memory values in a test, so the wake/poll/de-dup logic
16
25
  * is exercisable with no live backend at all.
17
26
  *
18
- * All THREE are the callers, and the count matters to a reader here: it means there
19
- * is exactly ONE delivery algorithm in this repo, and it is the code below. The
27
+ * All THREE engines are callers, and the count matters to a reader here: it means
28
+ * there is exactly ONE delivery algorithm in this repo, and it is the code below. The
20
29
  * in-memory store is a caller like the rest — it hands in a `fetch` over its log
21
30
  * `Ref` and its append `PubSub` as a wake source and keeps no delivery logic of its
22
31
  * own — which is what makes the shared contract suite's subscribe cases a comparison
23
32
  * of two BACKENDS rather than of two delivery algorithms.
24
33
  *
25
- * Correctness rests on the periodic position-based reconcile — a re-read from the
34
+ * Correctness rests on the periodic cursor-based reconcile — a re-read from the
26
35
  * cursor every poll interval — and NEVER on the wake source, which only lowers
27
36
  * delivery latency below that interval. A subscription with no wake source at all
28
37
  * is therefore complete and correct, merely slower, which is why `listen` is
29
38
  * optional in the TYPE rather than by the convention of passing `Stream.never`:
30
39
  * poll-only delivery is a supported shape, not a degraded one.
31
40
  *
32
- * ## The precondition every engine inherits: ASCENDING COMMIT ORDER
41
+ * ## The precondition every caller inherits: ASCENDING COMMIT ORDER
33
42
  *
34
- * A position that is committed and VISIBLE must never be below one already
35
- * delivered. Gaps are fine: the cursor is an ascending exclusive lower bound, so a
36
- * number that is allocated and never committed, or never allocated at all, is
37
- * simply a number the machine never sees. Out-of-order commits are not fine. If one
43
+ * It binds EVERY caller, over whatever cursor it chose — an engine's `Position`, or
44
+ * any other `C` handed to `subscribeItems` — and the example below is written in
45
+ * positions only because the engines are the callers it was first written for. A
46
+ * cursor that is committed and VISIBLE must never be below one already delivered.
47
+ * Gaps are fine: the cursor is an ascending exclusive lower bound, so a number
48
+ * that is allocated and never committed, or never allocated at all, is simply a
49
+ * number the machine never sees. Out-of-order commits are not fine. If one
38
50
  * writer allocates position 5 and another allocates 6, and 6 commits first, a poll
39
51
  * landing between the two commits delivers 6 and advances the cursor past it — and
40
52
  * 5 is then lost forever, because no later read looks below the cursor and nothing
41
- * anywhere notices the hole. An engine whose concurrent writers can commit out of
53
+ * anywhere notices the hole. A caller whose concurrent writers can commit out of
42
54
  * order MUST serialise them, or this machine will silently skip. An engine that
43
55
  * takes an exclusive lock on both of its append paths discharges the obligation
44
56
  * that way; an engine whose append is a single statement discharges it
45
57
  * structurally.
46
58
  *
47
59
  * The obligation is written down HERE, in the algorithm that depends on it, rather
48
- * than in whichever engine happens to satisfy it. It is a property of this delivery
49
- * loop and of nothing else, so an engine author reading only their own source has
60
+ * than in whichever caller happens to satisfy it. It is a property of this delivery
61
+ * loop and of nothing else, so an author reading only their own source has
50
62
  * no way to discover that anything downstream needs it — and the failure mode is
51
63
  * the worst kind there is: no error, no defect, no failing test, just an event that
52
64
  * a subscriber never receives. A single engine owning both its append path and its
53
65
  * own wake loop can leave the assumption implicit; a shared machine cannot.
54
66
  *
55
- * ## `catchUpPageSize` bounds the READ, not the emission
67
+ * ## `catchUpPageSize` bounds the READ, not the emission — unless `emit: 'page'`
56
68
  *
57
- * `drainFrom` below PAGES the catch-up read — `catchUpPageSize` bounds each
58
- * `fetch` — but it COLLECTS every page into one array, which is emitted as a SINGLE
59
- * chunk. So peak memory at subscription start is O(backlog) rather than O(page): a
69
+ * Under the default `emit: 'backlog'`, which is what `subscribeStream` and so every
70
+ * engine uses, `drainFrom` below PAGES the catch-up read — `catchUpPageSize` bounds
71
+ * each `fetch` — but it COLLECTS every page into one array, which is emitted as a
72
+ * SINGLE chunk. So peak memory at subscription start is O(backlog) rather than O(page): a
60
73
  * subscriber joining behind a large log materialises the whole backlog before its
61
74
  * consumer sees the first element.
62
75
  *
@@ -67,6 +80,19 @@
67
80
  * stay small even against a one-chunk catch-up. The cost therefore lands on backlog
68
81
  * memory alone, and it lands hardest on the durable-log-with-in-memory-view mode,
69
82
  * where an ephemeral view re-catches-up the entire log on every boot.
83
+ *
84
+ * `emit: 'page'` changes exactly that and nothing else. Each fetched page is
85
+ * emitted as its own chunk and the cursor advances past it before the next read, so
86
+ * peak memory is O(page) and the consumer sees the first item after ONE read rather
87
+ * than after the whole backlog. Delivery is unchanged: the same items in the same
88
+ * order, the same de-dup, the same wake and poll. What a caller gives up is the
89
+ * one-chunk catch-up, so it must not read meaning into chunk boundaries under
90
+ * either mode. It is for a consumer that does NOT re-chunk downstream and whose
91
+ * catch-up can be the whole of a large source — a full resync of a table, say —
92
+ * where holding the backlog would be the cost the paging was meant to avoid. The
93
+ * engines keep `'backlog'` because the read side's re-chunking already bounds what
94
+ * they commit, and changing their emission would move a behaviour nobody asked to
95
+ * move.
70
96
  */
71
97
  import { Effect, Schema, type Scope, Stream } from 'effect';
72
98
  import type { Position, SequencedEvent } from './Event.js';
@@ -130,9 +156,74 @@ export declare const subscriptionTuningFields: {
130
156
  readonly catchUpPageSize: Schema.optionalKey<Schema.Int>;
131
157
  };
132
158
  /**
133
- * The two seams the subscribe machine runs over, plus the two tuning knobs. `E` is
134
- * the engine's own infrastructure error — whatever its reads fail with — and it
135
- * stays on the channel through to `makeContractStore`, which dies it.
159
+ * How a subscription hands its catch-up to the consumer: the whole backlog as ONE
160
+ * chunk (`'backlog'`), or each fetched page as a chunk of its own (`'page'`). The
161
+ * module doc's "`catchUpPageSize` bounds the READ" section owns what each costs.
162
+ *
163
+ * A closed union of two words rather than a boolean, so a call site says WHICH
164
+ * behaviour it chose instead of what it turned off, and a third shape, should one
165
+ * ever be argued for, is added beside these rather than encoded in a second flag.
166
+ */
167
+ export type SubscriptionEmission = 'backlog' | 'page';
168
+ /**
169
+ * The machine's inputs over ANY item `A` with an ascending cursor `C`: the two seams,
170
+ * where the cursor comes from and starts, and the tuning.
171
+ *
172
+ * Generic in the item and the cursor because nothing in the algorithm is about an
173
+ * EVENT. The wake queue, the poll, the paged reconcile and the exclusive-lower-bound
174
+ * de-dup need only three things of what they deliver: a page-read strictly after a
175
+ * cursor, a way to read the cursor OFF a delivered item, and a starting cursor. A
176
+ * log of `SequencedEvent`s ordered by `Position` is one such source; a feed of rows
177
+ * ordered by a feed-local sequence is another, and a second copy of this loop for it
178
+ * would be exactly the divergence the module doc argues against.
179
+ *
180
+ * The machine never COMPARES two cursors. `fetch` is the only thing that interprets
181
+ * one — its exclusive lower bound is what does the de-dup — so `C` needs no ordering
182
+ * instance here, and the ascending-commit-order obligation in the module doc is the
183
+ * caller's, stated over whatever `C` it chose.
184
+ *
185
+ * `initial` is REQUIRED, unlike `subscribeStream`'s `after`. Only a cursor type the
186
+ * machine knows can have a default — `ORIGIN` for `Position` — and for any other the
187
+ * caller is the only party that knows what "before everything" is.
188
+ *
189
+ * `pollInterval` and `catchUpPageSize` are optional here and fall back to
190
+ * `DEFAULT_POLL_INTERVAL_MILLIS` and `DEFAULT_CATCH_UP_PAGE_SIZE`: a caller with no
191
+ * config of its own takes the same figures every engine does, from the same place.
192
+ * The event-store shape, `SubscriptionDeps`, keeps them required, because every
193
+ * engine already resolves them from its config and that type is unchanged.
194
+ */
195
+ export interface ItemSubscriptionDeps<A, C, E> {
196
+ /**
197
+ * Page-read the items strictly after `after`, up to `limit`, ascending by cursor.
198
+ * Returning FEWER than `limit` signals the tail is drained. The exclusive lower
199
+ * bound IS the de-dup: a re-read from the cursor never returns what was delivered.
200
+ */
201
+ readonly fetch: (after: C, limit: ReadLimit) => Effect.Effect<ReadonlyArray<A>, E>;
202
+ /** The cursor of a delivered item: the next `fetch` starts strictly after it. */
203
+ readonly cursorOf: (item: A) => C;
204
+ /** The cursor to start from, exclusive: the first `fetch` reads strictly after it. */
205
+ readonly initial: C;
206
+ /** The raw wake source — see `SubscriptionDeps.listen`, whose rules it shares. */
207
+ readonly listen?: Stream.Stream<unknown, E>;
208
+ /**
209
+ * The poll interval in milliseconds (default `DEFAULT_POLL_INTERVAL_MILLIS`):
210
+ * finite and greater than 0, or `subscribeItems` dies.
211
+ */
212
+ readonly pollInterval?: number;
213
+ /**
214
+ * The catch-up page size (default `DEFAULT_CATCH_UP_PAGE_SIZE`): an integer
215
+ * greater than 0, or `subscribeItems` dies.
216
+ */
217
+ readonly catchUpPageSize?: number;
218
+ /** The catch-up's chunking (default `'backlog'`); see `SubscriptionEmission`. */
219
+ readonly emit?: SubscriptionEmission;
220
+ }
221
+ /**
222
+ * The two seams the event-store subscribe machine runs over, plus the two tuning
223
+ * knobs: `ItemSubscriptionDeps` fixed to `SequencedEvent` and `Position`, the shape
224
+ * every engine's `subscribe` already hands to `subscribeStream`. `E` is the engine's
225
+ * own infrastructure error — whatever its reads fail with — and it stays on the
226
+ * channel through to `makeContractStore`, which dies it.
136
227
  *
137
228
  * ONE type parameter and not two, deliberately: a wake source with an error type of
138
229
  * its own is `Stream.orDie`d by the caller, yielding a `Stream<unknown, never>`
@@ -164,12 +255,35 @@ export interface SubscriptionDeps<E> {
164
255
  readonly catchUpPageSize: number;
165
256
  }
166
257
  /**
167
- * Build the subscription `Stream`: catch up from `after`, then track the live tail.
168
- * The cursor threads through the `Stream.paginate` state, starting at `after` and
169
- * advancing to the last delivered position each batch; the stream never completes
170
- * on its own (the next state is always `Option.some`), so the enclosing scope
171
- * interrupts it on close. An `E` mid-stream stays on the channel; `makeContractStore` dies it
172
- * (subscribe is `E = never` on the contract).
258
+ * Build a subscription `Stream` over any ascending-cursor source: catch up from
259
+ * `initial`, then track the live tail. The cursor threads through the
260
+ * `Stream.paginate` state, starting at `initial` and advancing to the cursor of the
261
+ * last delivered item each batch; the stream never completes on its own (the next
262
+ * state is always `Option.some`), so the enclosing scope interrupts it on close. An
263
+ * `E` mid-stream stays on the channel, for the caller to die or to supervise.
264
+ *
265
+ * This is THE delivery algorithm, and `subscribeStream` runs the same machine. Under
266
+ * `emit: 'backlog'` a batch is everything a drain to the tail returned; under
267
+ * `'page'` it is one fetched page, so the paginate step and the page coincide and a
268
+ * full page is followed at once by the next read rather than by a wait.
269
+ *
270
+ * A `pollInterval` or `catchUpPageSize` the config fields would refuse is a DEFECT
271
+ * here, raised by the stream before it reads or forks anything, with a message
272
+ * naming the field. This caller may have no config whose decode would have caught
273
+ * it, and unchecked the two failures are silent or late: a page size of 0 gives a
274
+ * stream that never emits and never fails, and 1.5 dies only at the first read,
275
+ * inside `ReadLimit`, in words that name neither knob.
276
+ */
277
+ export declare const subscribeItems: <A, C, E>(deps: ItemSubscriptionDeps<A, C, E>) => Stream.Stream<A, E, Scope.Scope>;
278
+ /**
279
+ * Build the event subscription `Stream`: catch up from `after`, then track the live
280
+ * tail. A THIN wrapper over the machine `subscribeItems` runs, fixing the item to
281
+ * `SequencedEvent`, the cursor to its `position`, the start to `ORIGIN` when `after`
282
+ * is absent, and the emission to the default `'backlog'` — so every engine's call
283
+ * site, and every figure it delivers, is what it was before the machine became
284
+ * generic. It skips `subscribeItems`' tuning check, for the reason on `machine`. An `E`
285
+ * mid-stream stays on the channel; `makeContractStore` dies it (subscribe is
286
+ * `E = never` on the contract).
173
287
  */
174
288
  export declare const subscribeStream: <E>(deps: SubscriptionDeps<E>, after: Position | undefined) => Stream.Stream<SequencedEvent, E, Scope.Scope>;
175
289
  //# sourceMappingURL=SubscribeMachine.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"SubscribeMachine.d.ts","sourceRoot":"","sources":["../../src/SubscribeMachine.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqEG;AACH,OAAO,EACL,MAAM,EAIN,MAAM,EACN,KAAK,KAAK,EACV,MAAM,EACP,MAAM,QAAQ,CAAA;AACf,OAAO,KAAK,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AACvD,OAAO,EAAU,SAAS,EAAE,MAAM,SAAS,CAAA;AAE3C;;;;;;GAMG;AACH,eAAO,MAAM,0BAA0B,MAAM,CAAA;AAE7C;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,4BAA4B,OAAO,CAAA;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,eAAO,MAAM,wBAAwB;;;CAO3B,CAAA;AAiBV;;;;;;;;;;;GAWG;AACH,MAAM,WAAW,gBAAgB,CAAC,CAAC;IACjC;;;OAGG;IACH,QAAQ,CAAC,KAAK,EAAE,CACd,IAAI,EAAE,QAAQ,EACd,KAAK,EAAE,SAAS,KACb,MAAM,CAAC,MAAM,CAAC,aAAa,CAAC,cAAc,CAAC,EAAE,CAAC,CAA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