Skip to content

Commit f11be7c

Browse files
committed
fix(client-runtime): merge older pages at batch watermarks
1 parent e0c1897 commit f11be7c

2 files changed

Lines changed: 157 additions & 3 deletions

File tree

‎packages/client-runtime/src/state/threads-pagination.test.ts‎

Lines changed: 148 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import * as Queue from "effect/Queue";
1919
import * as Ref from "effect/Ref";
2020
import * as Stream from "effect/Stream";
2121
import * as SubscriptionRef from "effect/SubscriptionRef";
22+
import * as TestClock from "effect/testing/TestClock";
2223

2324
import type { WsRpcProtocolClient } from "../rpc/protocol.ts";
2425
import {
@@ -148,9 +149,14 @@ const makeHarness = Effect.fn("TestThreadPagination.makeHarness")(function* (opt
148149
const supervisorState = yield* SubscriptionRef.make<SupervisorConnectionState>(
149150
AVAILABLE_CONNECTION_STATE,
150151
);
152+
// Preserve each queued backlog as a chunk through the subscription boundary.
151153
const client = {
152154
[ORCHESTRATION_WS_METHODS.subscribeThread]: (input: Record<string, unknown>) =>
153-
Stream.unwrap(Ref.set(lastSubscribeInput, input).pipe(Effect.as(Stream.fromQueue(inputs)))),
155+
Stream.unwrap(
156+
Ref.set(lastSubscribeInput, input).pipe(
157+
Effect.as(Stream.fromQueue(inputs).pipe(Stream.chunks, Stream.flatMap(Stream.fromArray))),
158+
),
159+
),
154160
} as unknown as WsRpcProtocolClient;
155161
const session: RpcSession.RpcSession = {
156162
client,
@@ -264,9 +270,39 @@ const titleEvent = (title: string, sequence: number): OrchestrationThreadStreamI
264270
},
265271
});
266272

273+
const olderMessageEvent = (
274+
sequence: number,
275+
text: string,
276+
streaming = true,
277+
): OrchestrationThreadStreamItem => ({
278+
kind: "event",
279+
event: {
280+
eventId: EventId.make(`event-message-${sequence}`),
281+
sequence,
282+
occurredAt: "2026-04-01T01:30:00.000Z",
283+
commandId: null,
284+
causationEventId: null,
285+
correlationId: null,
286+
metadata: {},
287+
aggregateKind: "thread",
288+
aggregateId: THREAD_ID,
289+
type: "thread.message-sent",
290+
payload: {
291+
threadId: THREAD_ID,
292+
messageId: OLDER_MESSAGE.id,
293+
role: "assistant",
294+
turnId: OLDER_MESSAGE.turnId,
295+
text,
296+
streaming,
297+
createdAt: OLDER_MESSAGE.createdAt,
298+
updatedAt: "2026-04-01T01:30:00.000Z",
299+
},
300+
},
301+
});
302+
267303
// Reverting to turnCount 1 retains only turns whose checkpoint count is <= 1:
268304
// turn-1 survives, turn-2 (the loaded window's newest turn) is discarded.
269-
const revertEvent = (sequence: number): OrchestrationThreadStreamItem => ({
305+
const revertEvent = (sequence: number, turnCount = 1): OrchestrationThreadStreamItem => ({
270306
kind: "event",
271307
event: {
272308
eventId: EventId.make(`event-revert-${sequence}`),
@@ -281,7 +317,7 @@ const revertEvent = (sequence: number): OrchestrationThreadStreamItem => ({
281317
type: "thread.reverted",
282318
payload: {
283319
threadId: THREAD_ID,
284-
turnCount: 1,
320+
turnCount,
285321
},
286322
},
287323
});
@@ -480,6 +516,115 @@ describe("thread pagination state", () => {
480516
}),
481517
);
482518

519+
for (const watermark of [11, 12, 138]) {
520+
it.effect(`merges a parked page before later deltas in a batch at watermark ${watermark}`, () =>
521+
Effect.gen(function* () {
522+
const harness = yield* makeHarness({ initialResponse: Option.some(WINDOWED_SNAPSHOT) });
523+
yield* harness.awaitState((value) => Option.isSome(value.page));
524+
requestOlderThreadTurns(TARGET.environmentId, THREAD_ID);
525+
yield* harness.awaitState((value) =>
526+
Option.match(value.page, { onNone: () => false, onSome: (page) => page.loadingOlder }),
527+
);
528+
yield* harness.resolveNextPage(
529+
Option.some({
530+
...OLDER_PAGE,
531+
snapshotSequence: watermark,
532+
page: {
533+
beforeCursor: null,
534+
hasMore: false,
535+
snapshotSequence: watermark,
536+
threadSequence: watermark,
537+
},
538+
}),
539+
);
540+
yield* Effect.yieldNow;
541+
expect(hasMessage(yield* SubscriptionRef.get(harness.threadState), "message-old")).toBe(
542+
false,
543+
);
544+
545+
// The first two cases cross the watermark inside a run; 138 places
546+
// it at the 128-item slice boundary. One queue offer delivers a backlog.
547+
yield* Queue.offerAll(harness.inputs, [
548+
...Array.from({ length: watermark - 10 }, (_, index) =>
549+
titleEvent(`Title ${index}`, 11 + index),
550+
),
551+
olderMessageEvent(watermark, " duplicate replay"),
552+
olderMessageEvent(watermark + 1, " + next delta"),
553+
olderMessageEvent(watermark + 2, "", false),
554+
titleEvent("Batch finished", watermark + 3),
555+
]);
556+
const state = yield* harness.awaitState((value) =>
557+
Option.match(value.data, {
558+
onNone: () => false,
559+
onSome: (thread) => thread.title === "Batch finished",
560+
}),
561+
);
562+
const thread = Option.getOrThrow(state.data);
563+
expect(thread.messages.find((entry) => entry.id === OLDER_MESSAGE.id)?.text).toBe(
564+
`${OLDER_MESSAGE.text} + next delta`,
565+
);
566+
expect(thread.messages.map((entry) => entry.id)).toEqual([
567+
OLDER_MESSAGE.id,
568+
RECENT_MESSAGE.id,
569+
]);
570+
expect(Option.getOrThrow(state.page)).toEqual({
571+
beforeCursor: null,
572+
hasMore: false,
573+
loadingOlder: false,
574+
});
575+
576+
yield* TestClock.adjust("500 millis");
577+
yield* Effect.yieldNow;
578+
const saved = (yield* Ref.get(harness.savedThreads)).at(-1);
579+
expect(saved?.snapshotSequence).toBe(watermark + 3);
580+
expect(saved?.thread.messages).toEqual(thread.messages);
581+
expect(saved?.page?.beforeCursor).toBe(null);
582+
}),
583+
);
584+
}
585+
586+
for (const revertSequence of [11, 12, 13]) {
587+
it.effect(`preserves page ordering when a batch reverts at sequence ${revertSequence}`, () =>
588+
Effect.gen(function* () {
589+
const harness = yield* makeHarness({ initialResponse: Option.some(WINDOWED_SNAPSHOT) });
590+
yield* harness.awaitState((value) => Option.isSome(value.page));
591+
requestOlderThreadTurns(TARGET.environmentId, THREAD_ID);
592+
yield* harness.awaitState((value) =>
593+
Option.match(value.page, { onNone: () => false, onSome: (page) => page.loadingOlder }),
594+
);
595+
yield* harness.resolveNextPage(
596+
Option.some({
597+
...OLDER_PAGE,
598+
snapshotSequence: 12,
599+
page: { beforeCursor: null, hasMore: false, snapshotSequence: 12, threadSequence: 12 },
600+
}),
601+
);
602+
yield* Effect.yieldNow;
603+
yield* Queue.offerAll(harness.inputs, [
604+
...[11, 12, 13].map((sequence) =>
605+
sequence === revertSequence
606+
? revertEvent(sequence, 0)
607+
: titleEvent(`Title ${sequence}`, sequence),
608+
),
609+
titleEvent("Batch finished", 14),
610+
]);
611+
const state = yield* harness.awaitState(
612+
(value) =>
613+
Option.match(value.data, {
614+
onNone: () => false,
615+
onSome: (thread) => thread.title === "Batch finished",
616+
}) &&
617+
Option.match(value.page, { onNone: () => false, onSome: (page) => !page.loadingOlder }),
618+
);
619+
expect(Option.getOrThrow(state.data).messages).toEqual([]);
620+
expect(Option.getOrThrow(state.page).loadingOlder).toBe(false);
621+
expect(Option.getOrThrow(state.page).beforeCursor).toBe(
622+
revertSequence <= 12 ? "cursor-1" : null,
623+
);
624+
}),
625+
);
626+
}
627+
483628
it.effect("a revert keeps the page cursor and triggers no refresh fetch", () =>
484629
Effect.gen(function* () {
485630
// Cursors are an (anchor, turnId) keyset derived from event content, so

‎packages/client-runtime/src/state/threads.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,9 +544,18 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
544544
items: ReadonlyArray<OrchestrationThreadStreamItem>,
545545
) {
546546
let events: Array<OrchestrationEvent> = [];
547+
let pendingWatermark = (yield* Ref.get(pendingOlderPage))?.snapshot.page?.threadSequence;
547548
for (const item of items) {
548549
if (item.kind === "event") {
549550
events.push(item.event);
551+
if (pendingWatermark !== undefined && item.event.sequence >= pendingWatermark) {
552+
// Publish and merge at the page's watermark, before a later delta
553+
// can create a partial row that would shadow the page's full row.
554+
// The lock prevents another page from parking during this slice.
555+
yield* applyEventRunLocked(events);
556+
events = [];
557+
pendingWatermark = undefined;
558+
}
550559
continue;
551560
}
552561
if (events.length > 0) {

0 commit comments

Comments
 (0)