fix(realtime): keep the transport open after a barrier-free recovery (R-4.3)
The event consumer projected every RECOVERED disposition to a RECOVERY_COMMITTED transport outcome. For a SNAPSHOT_ONLY + barrier NONE stream the coordinator never sets awaitingTransportBarrier, so the transport closed, the reconnect path called confirmTransportBarrier, and the coordinator answered PROTOCOL_MISMATCH: one sequence gap ended the stream. The RECOVERED disposition now carries transportBarrierRequired (optional, additive field on the RealtimeAcceptDisposition port type; absent keeps the old "barrier required" behavior). The coordinator fills it from the same recoveryRequiresTransportBarrier() rule that decides awaitingTransportBarrier, and the consumer returns CONTINUE when it is false. Test: "keeps reading after a SNAPSHOT_ONLY recovery that needs no transport barrier" in event-consumer.test.ts wires the real fetch SSE connection, consumer, codec and stream coordinator (only the app authority is faked). RED before the fix: read() ended with RECOVERY_COMMITTED at the gap instead of EOF. After: the gap recovers to snapshot 3, event 4 still applies, the stream reads to EOF and no barrier is pending. Gates: check:architecture, check:types:app, check:types:test, lint, test:component pass. test:unit passes except removal-fixture.test.ts, which needs generated artifacts/release evidence absent from this fresh worktree (unrelated). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
a6553e67bd
commit
a0e84eb897
@@ -178,6 +178,14 @@ function projectTransportOutcome(
|
||||
streamId: StreamRegistrationId | null,
|
||||
): RealtimeResult<RealtimeTransportEventOutcome> {
|
||||
if (!accepted.ok) return accepted;
|
||||
// A recovery with no barrier to confirm keeps the connection; handing it to
|
||||
// the reconnect path would ask for a barrier the coordinator never set.
|
||||
if (
|
||||
accepted.value.outcome === "RECOVERED" &&
|
||||
accepted.value.transportBarrierRequired === false
|
||||
) {
|
||||
return realtimeSuccess(REALTIME_TRANSPORT_CONTINUE);
|
||||
}
|
||||
if (
|
||||
accepted.value.outcome === "RECOVERED" ||
|
||||
accepted.value.outcome === "RECOVERY_BARRIER_REQUIRED"
|
||||
|
||||
@@ -909,6 +909,9 @@ export function createRealtimeStreamCoordinator(
|
||||
outcome: "RECOVERED" as const,
|
||||
reason,
|
||||
resumeState: result.value,
|
||||
transportBarrierRequired: recoveryRequiresTransportBarrier(
|
||||
state.registration,
|
||||
),
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -952,6 +955,9 @@ export function createRealtimeStreamCoordinator(
|
||||
outcome: "RECOVERED" as const,
|
||||
reason,
|
||||
resumeState: result.value,
|
||||
transportBarrierRequired: recoveryRequiresTransportBarrier(
|
||||
state.registration,
|
||||
),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -130,6 +130,12 @@ export type RealtimeAcceptDisposition =
|
||||
outcome: "RECOVERED";
|
||||
reason: RealtimeRecoveryReason;
|
||||
resumeState: RealtimeRecoveryCheckpoint;
|
||||
/**
|
||||
* `false` when the stream's recovery has no transport barrier to confirm
|
||||
* (SNAPSHOT_ONLY with barrier NONE), so the transport keeps its
|
||||
* connection. Absent means a barrier is required.
|
||||
*/
|
||||
transportBarrierRequired?: boolean;
|
||||
}>
|
||||
| Readonly<{
|
||||
outcome: "RECOVERY_BARRIER_REQUIRED";
|
||||
|
||||
@@ -18,9 +18,20 @@ import {
|
||||
realtimeFailure,
|
||||
realtimeSuccess,
|
||||
} from "../../../src/adapters/realtime/result.ts";
|
||||
import type {
|
||||
RealtimeStreamCoordinator,
|
||||
import {
|
||||
createRealtimeStreamCoordinator,
|
||||
type RealtimeStreamCoordinator,
|
||||
} from "../../../src/adapters/realtime/stream-coordinator.ts";
|
||||
import {
|
||||
createFetchSseConnection,
|
||||
} from "../../../src/adapters/realtime/sse/fetch-sse-connection.ts";
|
||||
import {
|
||||
STREAM_ID,
|
||||
TEST_MAPPERS,
|
||||
createTestRealtimeCodec,
|
||||
createTestRealtimeRegistry,
|
||||
realtimeEventJson,
|
||||
} from "./fixture.ts";
|
||||
|
||||
function setup(
|
||||
recoveryMode:
|
||||
@@ -209,3 +220,98 @@ describe("realtime transport event consumer", () => {
|
||||
expect(runtime.accept).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("SSE transport, consumer and stream coordinator together", () => {
|
||||
it("keeps reading after a SNAPSHOT_ONLY recovery that needs no transport barrier", async () => {
|
||||
const registry = createTestRealtimeRegistry({
|
||||
recovery: {
|
||||
mode: "SNAPSHOT_ONLY",
|
||||
snapshotOperationId: "GET_REFERENCE_REALTIME_SNAPSHOT",
|
||||
checkpointCodecId: "ReferenceRealtimeCheckpoint",
|
||||
barrier: "NONE",
|
||||
},
|
||||
delivery: "INVALIDATION_HINT",
|
||||
stateBearing: false,
|
||||
});
|
||||
const snapshots = ["0", "3"];
|
||||
const recoveries: string[] = [];
|
||||
const apply = vi.fn(async () => realtimeSuccess(undefined));
|
||||
const coordinator = createRealtimeStreamCoordinator({
|
||||
registry,
|
||||
mappers: TEST_MAPPERS,
|
||||
authority: {
|
||||
effects: { apply },
|
||||
recovery: {
|
||||
async recover(request) {
|
||||
recoveries.push(request.reason);
|
||||
const sequence = snapshots.shift() ?? "0";
|
||||
return realtimeSuccess({
|
||||
kind: "SNAPSHOT_RESET",
|
||||
checkpoint: {
|
||||
recoveryMode: "SNAPSHOT_ONLY",
|
||||
streamEpoch: "stream-epoch-0001",
|
||||
lastAppliedSequence: sequence,
|
||||
resumeCursor: null,
|
||||
snapshotRevision: `snapshot-revision-${sequence}`,
|
||||
},
|
||||
});
|
||||
},
|
||||
},
|
||||
},
|
||||
scope: {
|
||||
generation: 7,
|
||||
scopeBinding: "scope-binding-0001",
|
||||
isCurrent: () => true,
|
||||
},
|
||||
now: () => 10_000,
|
||||
});
|
||||
const consumer = createRealtimeEventConsumer({
|
||||
codec: createTestRealtimeCodec(registry),
|
||||
coordinator,
|
||||
});
|
||||
await expect(
|
||||
coordinator.recover(STREAM_ID, "INITIALIZE"),
|
||||
).resolves.toMatchObject({ ok: true });
|
||||
|
||||
const frame = (sequence: string) =>
|
||||
`data: ${realtimeEventJson({
|
||||
eventId: `event-0000000${sequence}`,
|
||||
sequence,
|
||||
recoveryMode: "SNAPSHOT_ONLY",
|
||||
resumeCursor: null,
|
||||
})}\n\n`;
|
||||
const body = [frame("1"), frame("3"), frame("4")].join("");
|
||||
const connection = createFetchSseConnection({
|
||||
endpoint: "https://app.example.test/events",
|
||||
applicationOrigin: "https://app.example.test",
|
||||
recoveryMode: "SNAPSHOT_ONLY",
|
||||
fetcher: async () =>
|
||||
new Response(body, {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "text/event-stream" },
|
||||
}),
|
||||
});
|
||||
|
||||
// Sequence 3 is a gap: the coordinator recovers to snapshot 3, and the
|
||||
// stream must keep reading so sequence 4 still applies.
|
||||
const closed = await connection.read({
|
||||
resumeCursor: null,
|
||||
onEvent: (event, signal) =>
|
||||
consumer.consumeForTransport(
|
||||
event.data,
|
||||
{ kind: "SSE_NO_CURSOR" },
|
||||
signal,
|
||||
),
|
||||
});
|
||||
|
||||
expect(closed).toMatchObject({ ok: true, value: { kind: "EOF" } });
|
||||
expect(recoveries).toEqual(["INITIALIZE", "SEQUENCE_GAP"]);
|
||||
expect(apply).toHaveBeenCalledTimes(2);
|
||||
expect(coordinator.getResumeState(STREAM_ID)).toMatchObject({
|
||||
lastAppliedSequence: "4",
|
||||
});
|
||||
expect(coordinator.inspect(STREAM_ID).awaitingTransportBarrier).toBe(
|
||||
false,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user