@ultimat3/realtime 31.24.0 → 31.25.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/CLAUDE.md CHANGED
@@ -97,7 +97,9 @@ A rule marked `†` has its full text in [`docs/history/realtime-rules.md`](../.
97
97
  outside the lane, against the live window.
98
98
  - **`desynced` is a mark with a reader**: the next delivery re-snapshots out of the shared window,
99
99
  then clears it; `result.refill` is checked BEFORE the mark. A quiet query's three other readers
100
- (drain, a landed read, `repairMarked`): `docs/history/realtime.md` (2026-11-09).
100
+ (drain, a landed read, `repairMarked`): `docs/history/realtime.md` (2026-11-09). A bus
101
+ reconnect or a replicator resync runs `repairMarked` AT ONCE: no change follows either
102
+ (`sync-bus-repair.test.ts`).
101
103
  - **A change the window already holds is refused** (`change.lsn <= entry.lsn`, `staleChanges`).
102
104
  **A gap is detected** (`producer` + `seq`; `SeqGapDetector.#chain`, `replicator-handover.ts`). †
103
105
  - **`stop()` reads the walsender's goodbye before `Terminate`** (`pg-replication-goodbye.ts`). †
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultimat3/realtime",
3
- "version": "31.24.0",
3
+ "version": "31.25.0",
4
4
  "description": "Three-tier realtime: channels, live queries, local-first sync — one protocol, one mutator shape",
5
5
  "keywords": [
6
6
  "ultimate",
@@ -52,10 +52,10 @@
52
52
  "test": "bun test"
53
53
  },
54
54
  "dependencies": {
55
- "@ultimat3/core": "31.24.0",
56
- "@ultimat3/entity": "31.24.0",
57
- "@ultimat3/policy": "31.24.0",
58
- "@ultimat3/query": "31.24.0",
55
+ "@ultimat3/core": "31.25.0",
56
+ "@ultimat3/entity": "31.25.0",
57
+ "@ultimat3/policy": "31.25.0",
58
+ "@ultimat3/query": "31.25.0",
59
59
  "nats": "2.29.3"
60
60
  }
61
61
  }
package/src/live-query.ts CHANGED
@@ -92,8 +92,9 @@ export class LiveQueryRegistry {
92
92
  * The `sync` node calls this when the change stream skips a sequence: over core NATS a fanout is
93
93
  * at-most-once, and a node that missed eleven changes during a reconnect otherwise holds a window
94
94
  * whose lsn never moved, subscribers whose cursors never moved, and therefore nothing that would
95
- * ever ask for a re-snapshot. The repair lands on the next change to each query — which is the
96
- * event that proves the query is moving at all.
95
+ * ever ask for a re-snapshot. The repair lands on the next change the node hears (every stale
96
+ * window is its target), or at once when the stream said so with no change behind it — a bus
97
+ * reconnect or a replicator resync calls `repairMarked()` (`sync-bus-handlers.ts`).
97
98
  *
98
99
  * `entity` — the RELATION, as on `ChangeEvent.table` — narrows it to the windows that read it:
99
100
  * a filtered write (`updateWhere`/`deleteWhere`) names its table but no row, and staling every
@@ -3,6 +3,7 @@
3
3
  // mark. Split from `live-query.ts` at its size ceiling: the registry owns the tables, this owns
4
4
  // what a repair does with them.
5
5
 
6
+ import { mapBounded } from '@ultimat3/core';
6
7
  import type { LiveEntries } from './live-entries';
7
8
  import { type FanoutDeps, repairSubscriber, repairWindow } from './live-fanout';
8
9
  import type { SyncSocket } from './socket';
@@ -31,14 +32,23 @@ export async function repairSocket(
31
32
  return (await Promise.all(repairs)).filter(Boolean).length;
32
33
  }
33
34
 
35
+ /**
36
+ * How many windows one sweep re-reads at once. A bus reconnect reaches every node together, and
37
+ * each sweeps every window: unbounded, N windows × M nodes queued on the pool past the read deadline
38
+ * and re-staled the windows they were repairing.
39
+ */
40
+ export const REPAIR_WIDTH = 8;
41
+
34
42
  /** The node's sweep: every window owing a mark, each in its lane, one read a window at most. */
35
43
  export async function repairEveryWindow(fanout: FanoutDeps, entries: LiveEntries): Promise<number> {
36
- const lanes = [...entries.values()].map((entry) =>
37
- entry.lock.run(() => repairWindow(fanout, entry)),
44
+ const lanes = await mapBounded(
45
+ [...entries.values()],
46
+ (entry) => entry.lock.run(() => repairWindow(fanout, entry)),
47
+ { width: REPAIR_WIDTH, settle: 'all' },
38
48
  );
39
49
  let sent = 0;
40
50
  let failure: { readonly error: unknown } | null = null;
41
- for (const lane of await Promise.allSettled(lanes)) {
51
+ for (const lane of lanes) {
42
52
  if (lane.status === 'fulfilled') sent += lane.value;
43
53
  else failure ??= { error: lane.reason };
44
54
  }
@@ -0,0 +1,27 @@
1
+ // Single responsibility: the replicator's change handler as a PASS on a heartbeat the host hands
2
+ // in. The host's liveness (`packages/cli/src/runtime-liveness.ts`) judges a pass held open too long
3
+ // — a publish that never returns — as a wedged replicator. Structural, so this package names no
4
+ // jobs type: `@ultimat3/jobs`' `loopHeartbeat()` is what the cli passes.
5
+
6
+ import type { ChangeEvent } from './changefeed';
7
+
8
+ /** A pass begins; the answer ends it, idempotently. */
9
+ export interface PassHeartbeat {
10
+ enter(): () => void;
11
+ }
12
+
13
+ /** `handler`, each call held open on `heartbeat` until it settles. No heartbeat: `handler` itself. */
14
+ export function heldBy(
15
+ heartbeat: PassHeartbeat | undefined,
16
+ handler: (change: ChangeEvent) => Promise<void>,
17
+ ): (change: ChangeEvent) => Promise<void> {
18
+ if (heartbeat === undefined) return handler;
19
+ return async (change) => {
20
+ const leave = heartbeat.enter();
21
+ try {
22
+ await handler(change);
23
+ } finally {
24
+ leave();
25
+ }
26
+ };
27
+ }
package/src/replicator.ts CHANGED
@@ -20,6 +20,7 @@ import { encodeResync, RESYNC_SUBJECT } from './replication-resync';
20
20
  import { type DeadlineOutcome, withinStopDeadline } from './replicator-deadline';
21
21
  import { encodeEnvelope } from './replicator-envelope';
22
22
  import { handOver } from './replicator-handover';
23
+ import { heldBy, type PassHeartbeat } from './replicator-heartbeat';
23
24
  import { busAway, fencedOut, PUBLISH_RETRY_DELAYS_MS } from './replicator-publish';
24
25
  import { connectionLost, RedialCurve } from './replicator-reconnect';
25
26
  import { ResyncDebt } from './replicator-resync';
@@ -52,6 +53,8 @@ export interface ReplicatorOptions {
52
53
  readonly rng?: Rng;
53
54
  /** The timer a restart waits on. Injected so a test proves the takeover loop without sleeping. */
54
55
  readonly schedule?: Scheduler;
56
+ /** Each change handled is a pass held open on it: the role's liveness sees a wedged publish. */
57
+ readonly heartbeat?: PassHeartbeat;
55
58
  }
56
59
 
57
60
  export interface ReplicatorStats {
@@ -295,7 +298,7 @@ export function changeFeedReplicator(options: ReplicatorOptions): Replicator {
295
298
  // A restart resumes where THIS process got to. `options.from` is where the first run began;
296
299
  // handing it to the second would have the feed re-deliver everything published since.
297
300
  const from = lastLsn ?? resumeFrom;
298
- const onChange = changeHandler(run);
301
+ const onChange = heldBy(options.heartbeat, changeHandler(run));
299
302
  // A recreated slot is a NEW timeline: no old position may drop its changes or resume it.
300
303
  const onResync = (reason: string): void => {
301
304
  resync.owe(reason);
package/src/server.ts CHANGED
@@ -239,6 +239,7 @@ export {
239
239
  parseEnvelope,
240
240
  SeqGapDetector,
241
241
  } from './replicator-envelope';
242
+ export type { PassHeartbeat } from './replicator-heartbeat';
242
243
  export {
243
244
  actorIdOf,
244
245
  CLOSE,
@@ -148,10 +148,17 @@ function desync(bus: SyncBus, table: string, replicated = true): void {
148
148
  logger.warn('live.change_gap', { table, desynced: marked, channelGaps });
149
149
  }
150
150
 
151
- /** Every window and every records topic re-read, the producer history forgotten, said once. */
151
+ /**
152
+ * Every window and every records topic re-read, the producer history forgotten, said once. The
153
+ * windows are repaired NOW: a reconnect and a resync arrive with no change behind them, and a quiet
154
+ * query that moved during the outage has no later change to be repaired by — its page showed the
155
+ * rows from before the outage until the idle sweep (`multi-node.live.test.ts`). A gap seen on a
156
+ * message needs no kick: that message's delivery reaches every stale window (`targets`).
157
+ */
152
158
  function resyncAll(bus: SyncBus, event: string, fields: Record<string, string>): void {
153
159
  bus.gaps.forget();
154
160
  const marked = bus.registry.invalidate();
155
161
  const channelGaps = bus.hub.invalidate();
156
162
  logger.warn(event, { ...fields, desynced: marked, channelGaps });
163
+ if (marked > 0) detach(bus.registry.repairMarked(), 'live.repair', event);
157
164
  }