fix(cross-context): keep the sequence watermark past the event TTL (R-2.2)

The per-source high watermark was stored with the event's expiresAt and
pruned with it. After a source was quiet for longer than the 60 s event
TTL, its next event had no previous watermark and `sequence > 1`, so it
was delivered as GAP, and the query-cache coordinator then invalidated
every topic.

The watermark now carries only the sequence. It is removed only by the
existing sourceCapacity LRU. Event dedupe entries still expire with the
event TTL.

The existing test "bounds TTL and LRU tracking instead of growing with
remote sources" expected a same-source replay of seq 1 to be delivered
again after the TTL, which only worked because the watermark expired.
It now expects that replay to be dropped as STALE.

Verified: "keeps the per-source sequence watermark past the event TTL"
failed before the fix (seq 2 after 61 s was GAP) and passes now.
check:architecture, check:types:app, check:types:test, lint,
test:component pass. test:unit passes except removal-fixture, which needs
generated artifacts/release files that a fresh worktree lacks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-24 12:15:58 +09:00
co-authored by Claude Opus 5.5
parent 54abaec12a
commit d4f679374b
2 changed files with 49 additions and 14 deletions
@@ -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(
+45 -1
View File
@@ -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();
});