fix(realtime): refuse recovery once its quiescence ends in DRAINING (R-4.1)

runRecovery() checked lifecycle === "DRAINING" only on entry. After
`await quiescence` it re-checked closed/abort/scope but not DRAINING, so an
effect that timed out while the recovery waited left the stream DRAINING and
the recovery still called the authority, committed a new resume state and
cleared recoveryRequired while the abandoned effect kept running (the
RT-RR-01/02 case).

The post-quiescence check now also fails with CLOSED/RECOVER when the stream
is DRAINING, without calling the authority.

Test: "fails a recovery whose effect quiescence ends in DRAINING" in
stream-coordinator.test.ts. RED before the fix: the recovery resolved
{ ok: true } (authority called and committed). After: CLOSED, authority not
called, no resume state; once the late effect settles the stream is OPEN and
STALE.

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:
DongHyeonka
2026-09-24 12:19:48 +09:00
co-authored by Claude Opus 5.5
parent 87a45893d6
commit a6553e67bd
2 changed files with 55 additions and 1 deletions
+3 -1
View File
@@ -707,7 +707,9 @@ export function createRealtimeStreamCoordinator(
if (!calledFromCurrentJob) {
await quiescence;
}
if (closed || state.closed) {
// RT-RR-01. The effect this recovery waited on can time out and keep
// running; recovering beside it would let its late write win.
if (closed || state.closed || state.lifecycle === "DRAINING") {
state.freshness = "UNKNOWN";
return realtimeFailure("CLOSED", "RECOVER");
}
@@ -1053,6 +1053,58 @@ describe("realtime physical task ownership", () => {
firstApply.resolve(realtimeSuccess(undefined));
});
it("fails a recovery whose effect quiescence ends in DRAINING", async () => {
const wedged = deferred<RealtimeResult<void>>();
let recoveries = 0;
const harness = createHarness({
apply: async () => wedged.promise,
recover: async () => {
recoveries += 1;
return realtimeSuccess(snapshotCommit("0"));
},
taskLimits: { effectTimeoutMs: 5, drainTimeoutMs: 5 },
});
await harness.initialize();
recoveries = 0;
const applying = harness.coordinator.accept(
harness.event({
eventId: "event-00000001",
sequence: "1",
resumeCursor: "cursor-00000001",
}),
);
for (let flush = 0; flush < 10; flush += 1) await Promise.resolve();
expect(harness.effects).toHaveBeenCalledOnce();
expect(harness.coordinator.lifecycle(STREAM_ID)).toBe("OPEN");
// The effect ignores abort, so it outlives its deadline and the stream
// enters DRAINING while this recovery waits for quiescence.
const recovering = harness.coordinator.recover(
STREAM_ID,
"SEQUENCE_GAP",
);
await expect(applying).resolves.toMatchObject({
ok: false,
error: { kind: "IDLE_TIMEOUT", operation: "APPLY" },
});
await expect(recovering).resolves.toMatchObject({
ok: false,
error: { kind: "CLOSED", operation: "RECOVER" },
});
expect(recoveries).toBe(0);
expect(harness.coordinator.inspect(STREAM_ID).hasResumeState).toBe(
false,
);
// The late effect settles; the stream still needs a real recovery.
wedged.resolve(realtimeSuccess(undefined));
await vi.waitFor(() =>
expect(harness.coordinator.lifecycle(STREAM_ID)).toBe("OPEN"),
);
expect(harness.coordinator.inspect(STREAM_ID).freshness).toBe("STALE");
});
});
type HarnessOptions = Readonly<{