Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions apps/web/src/orchestrationRecovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,24 @@ describe("createOrchestrationRecoveryCoordinator", () => {
coordinator.classifyDomainEvent(7);
coordinator.markEventBatchApplied([{ sequence: 4 }, { sequence: 5 }, { sequence: 6 }]);

expect(coordinator.completeReplayRecovery()).toBe(true);
expect(coordinator.completeReplayRecovery()).toEqual({
replayMadeProgress: true,
shouldReplay: true,
});
});

it("does not immediately replay again when replay returns no new events", () => {
it("retries replay when no progress was made but higher live sequences were observed", () => {
const coordinator = createOrchestrationRecoveryCoordinator();

coordinator.beginSnapshotRecovery("bootstrap");
coordinator.completeSnapshotRecovery(3);
coordinator.classifyDomainEvent(5);
coordinator.beginReplayRecovery("sequence-gap");

expect(coordinator.completeReplayRecovery()).toBe(false);
expect(coordinator.completeReplayRecovery()).toEqual({
replayMadeProgress: false,
shouldReplay: true,
});
expect(coordinator.getState()).toMatchObject({
latestSequence: 3,
highestObservedSequence: 5,
Expand All @@ -79,6 +85,19 @@ describe("createOrchestrationRecoveryCoordinator", () => {
});
});

it("does not request another replay when a replay made no progress and nothing newer was observed", () => {
const coordinator = createOrchestrationRecoveryCoordinator();

coordinator.beginSnapshotRecovery("bootstrap");
coordinator.completeSnapshotRecovery(3);
coordinator.beginReplayRecovery("sequence-gap");

expect(coordinator.completeReplayRecovery()).toEqual({
replayMadeProgress: false,
shouldReplay: false,
});
});

it("marks replay failure as unbootstrapped so snapshot fallback is recovery-only", () => {
const coordinator = createOrchestrationRecoveryCoordinator();

Expand Down
17 changes: 11 additions & 6 deletions apps/web/src/orchestrationRecovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ export interface OrchestrationRecoveryState {
inFlight: OrchestrationRecoveryPhase | null;
}

export interface ReplayRecoveryCompletion {
replayMadeProgress: boolean;
shouldReplay: boolean;
}

type SequencedEvent = Readonly<{ sequence: number }>;

export function createOrchestrationRecoveryCoordinator() {
Expand Down Expand Up @@ -120,16 +125,16 @@ export function createOrchestrationRecoveryCoordinator() {
return true;
},

completeReplayRecovery(): boolean {
completeReplayRecovery(): ReplayRecoveryCompletion {
const replayMadeProgress =
replayStartSequence !== null && state.latestSequence > replayStartSequence;
replayStartSequence = null;
state.inFlight = null;
if (!replayMadeProgress) {
state.pendingReplay = false;
return false;
}
return resolveReplayNeedAfterRecovery().shouldReplay;
const replayResolution = resolveReplayNeedAfterRecovery();
return {
replayMadeProgress,
shouldReplay: replayResolution.shouldReplay,
};
Comment thread
cursor[bot] marked this conversation as resolved.
},

failReplayRecovery(): void {
Expand Down
17 changes: 15 additions & 2 deletions apps/web/src/routes/__root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ function coalesceOrchestrationUiEvents(
return coalesced;
}

const REPLAY_RECOVERY_RETRY_DELAY_MS = 100;

function ServerStateBootstrap() {
useEffect(() => startServerStateSync(getWsRpcClient().server), []);

Expand Down Expand Up @@ -440,8 +442,19 @@ function EventRouter() {
return;
}

if (!disposed && recovery.completeReplayRecovery()) {
void recoverFromSequenceGap();
if (!disposed) {
const replayCompletion = recovery.completeReplayRecovery();
if (replayCompletion.shouldReplay) {
if (!replayCompletion.replayMadeProgress) {
await new Promise<void>((resolve) => {
setTimeout(resolve, REPLAY_RECOVERY_RETRY_DELAY_MS);
});
if (disposed) {
return;
}
}
void recoverFromSequenceGap();
}
Comment thread
cursor[bot] marked this conversation as resolved.
}
};

Expand Down
Loading