fix(realtime): bound the live probe buffer by what it still holds (Q-6.4-10)

bufferProbeValue compared the ceilings against acceptedEvents /
acceptedBytes, counters that only grew. Activation drains the buffer
while the probe keeps receiving, so once a probe had accepted
maxProbeBufferedEvents values in total, the next write during activation
failed closed and left the coordinator CLOSED even with an almost empty
buffer. The check now uses buffer.length and bufferedBytes, which the
drain already decrements; the two cumulative counters are removed.

RED: new test (limit 2, write two, activate, write a third while the
first drains) got QUEUE_OVERFLOW before the fix; it now buffers, and a
fourth write that really exceeds the current buffer still fails closed.
Gates: architecture, types app/test, lint, adapter-inventory, test:unit,
test:component pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-24 13:54:42 +09:00
co-authored by Claude Opus 5.5
parent c7b921f82f
commit 13274f7f3a
2 changed files with 39 additions and 10 deletions
@@ -133,8 +133,6 @@ type InternalProbe<Value> = {
facade: LiveProbeLease<Value>;
readonly buffer: BufferedValue<Value>[];
bufferedBytes: number;
acceptedEvents: number;
acceptedBytes: number;
};
type QuiescenceOutcome = "QUIESCED" | "TIMER_FAILED" | "TIMED_OUT";
@@ -225,8 +223,6 @@ export function createLivePollHandoffCoordinator<Value>(
lease,
buffer: [],
bufferedBytes: 0,
acceptedEvents: 0,
acceptedBytes: 0,
facade: null as unknown as LiveProbeLease<Value>,
};
selected.facade = Object.freeze({
@@ -264,11 +260,11 @@ export function createLivePollHandoffCoordinator<Value>(
value: Value,
wireBytes: number,
): RealtimeResult<LivePollWriteReceipt> {
// Bounded by what is still buffered: activation drains the buffer while
// the probe keeps receiving.
if (
selected.acceptedEvents + 1 >
limits.maxProbeBufferedEvents ||
selected.acceptedBytes + wireBytes >
limits.maxProbeBufferedBytes
selected.buffer.length + 1 > limits.maxProbeBufferedEvents ||
selected.bufferedBytes + wireBytes > limits.maxProbeBufferedBytes
) {
if (transitioning) {
failClosed();
@@ -283,8 +279,6 @@ export function createLivePollHandoffCoordinator<Value>(
}
selected.buffer.push(Object.freeze({ value, wireBytes }));
selected.bufferedBytes += wireBytes;
selected.acceptedEvents += 1;
selected.acceptedBytes += wireBytes;
return realtimeSuccess(
Object.freeze({
kind: "BUFFERED" as const,
@@ -426,6 +426,41 @@ describe("live/poll authoritative writer handoff", () => {
});
});
it("bounds the probe by what is still buffered, not by all it ever accepted", async () => {
const firstLive = deferred<RealtimeResult<void>>();
const order: string[] = [];
const harness = createHarness({
initial: "POLL",
limits: { ...limits, maxProbeBufferedEvents: 2 },
apply: vi.fn(async ({ value }) => {
order.push(value);
if (value === "live-1") await firstLive.promise;
return realtimeSuccess(undefined);
}),
});
const live = expectProbe(harness.coordinator.beginLiveProbe());
await live.write("live-1", 3);
await live.write("live-2", 3);
const activation = live.activate();
// live-1 left the buffer for the drain, so one slot is free again.
await vi.waitFor(() => expect(order).toEqual(["live-1"]));
await expect(live.write("live-3", 3)).resolves.toMatchObject({
ok: true,
value: { kind: "BUFFERED" },
});
await expect(live.write("live-4", 3)).resolves.toMatchObject({
ok: false,
error: { kind: "QUEUE_OVERFLOW" },
});
// Overflow during activation still fails closed.
firstLive.resolve(realtimeSuccess(undefined));
await expect(activation).resolves.toMatchObject({ ok: false });
expect(harness.coordinator.inspect().state).toBe("CLOSED");
expect(order).toEqual(["live-1"]);
});
it("drops an overflowing probe without displacing poll and never reuses its generation", async () => {
const harness = createHarness({
initial: "POLL",