diff --git a/src/adapters/cross-context-invalidation/browser-cross-context-invalidation.ts b/src/adapters/cross-context-invalidation/browser-cross-context-invalidation.ts index 7bea739c..a6743c3a 100644 --- a/src/adapters/cross-context-invalidation/browser-cross-context-invalidation.ts +++ b/src/adapters/cross-context-invalidation/browser-cross-context-invalidation.ts @@ -145,10 +145,9 @@ export type BrowserCrossContextInvalidation = Readonly<{ }>; type SeenEvent = Readonly<{ expiresAt: number }>; -type SourceHighWatermark = Readonly<{ - sequence: number; - expiresAt: number; -}>; +// No TTL: a watermark must outlive its last event, or the next event after a +// quiet period reads as a GAP. `sourceCapacity` LRU bounds the memory. +type SourceHighWatermark = Readonly<{ sequence: number }>; const DEFAULT_EVENT_TTL_MS = 60_000; const DEFAULT_DEDUPE_CAPACITY = 1_024; @@ -497,10 +496,7 @@ export function createBrowserCrossContextInvalidation( event.sequence > previous.sequence + 1) ? "GAP" : "NEXT"; - touchSource(sourceKey, { - sequence: event.sequence, - expiresAt: event.expiresAt, - }); + touchSource(sourceKey, { sequence: event.sequence }); const delivery = Object.freeze({ event, ordering, transport }); for (const listener of [...listeners]) { @@ -546,11 +542,6 @@ export function createBrowserCrossContextInvalidation( for (const [eventId, entry] of seenEvents) { if (entry.expiresAt <= currentTime) seenEvents.delete(eventId); } - for (const [sourceKey, entry] of sourceHighWatermarks) { - if (entry.expiresAt <= currentTime) { - sourceHighWatermarks.delete(sourceKey); - } - } } function subscribe( diff --git a/tests/unit/cross-tab-invalidation.test.ts b/tests/unit/cross-tab-invalidation.test.ts index 49df90b2..dc0c944b 100644 --- a/tests/unit/cross-tab-invalidation.test.ts +++ b/tests/unit/cross-tab-invalidation.test.ts @@ -460,6 +460,42 @@ describe("browser cross-context invalidation transport", () => { runtime.close(); }); + it("keeps the per-source sequence watermark past the event TTL", () => { + const network = new FakeBroadcastNetwork(); + let currentTime = NOW; + const runtime = createBrowserCrossContextInvalidation( + dependencies("tab-b", { + createBroadcastChannel: network.createChannel, + nowEpochMilliseconds: () => currentTime, + }), + ); + const delivered: CrossContextInvalidationDelivery[] = []; + runtime.subscribe((delivery) => delivered.push(delivery)); + + network.emit(wireEvent({ eventId: "remote-event-00000001", sequence: 1 })); + // The source stays quiet longer than the 60 s event TTL. + currentTime += 61_000; + network.emit( + wireEvent({ + eventId: "remote-event-00000002", + sequence: 2, + emittedAt: currentTime, + expiresAt: currentTime + 60_000, + }), + ); + + expect( + delivered.map(({ event, ordering }) => [ + event.sequence, + ordering, + ]), + ).toEqual([ + [1, "NEXT"], + [2, "NEXT"], + ]); + runtime.close(); + }); + it("falls back when BroadcastChannel open or publish fails", () => { const storageBus = new FakeStorageBus(); const firstStorage = storageBus.createEndpoint(); @@ -618,12 +654,14 @@ describe("browser cross-context invalidation transport", () => { it("bounds TTL and LRU tracking instead of growing with remote sources", () => { const network = new FakeBroadcastNetwork(); let currentTime = NOW; + const observations: CrossContextInvalidationObservation[] = []; const runtime = createBrowserCrossContextInvalidation( dependencies("tab-b", { createBroadcastChannel: network.createChannel, nowEpochMilliseconds: () => currentTime, dedupeCapacity: 2, sourceCapacity: 2, + observe: (observation) => observations.push(observation), }), ); const received = vi.fn(); @@ -651,7 +689,13 @@ describe("browser cross-context invalidation transport", () => { network.emit( distinctSource(3, "bounded-event-3", currentTime), ); - expect(received).toHaveBeenCalledTimes(5); + // The dedupe entry expired with the event TTL, but the sequence watermark + // does not expire, so the replay is dropped as stale, not redelivered. + expect(received).toHaveBeenCalledTimes(4); + expect(observations.at(-1)).toMatchObject({ + outcome: "DROPPED", + reason: "STALE", + }); runtime.close(); });