Skip to content

Commit 783fd02

Browse files
committed
fix(sync): retain settled snapshots across event batches
1 parent b623dc2 commit 783fd02

3 files changed

Lines changed: 86 additions & 30 deletions

File tree

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

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
TurnId,
99
type OrchestrationThread,
1010
type OrchestrationThreadDetailSnapshot,
11+
type OrchestrationSession,
1112
type OrchestrationThreadStreamItem,
1213
} from "@t3tools/contracts";
1314
import { describe, expect, it } from "@effect/vitest";
@@ -281,6 +282,44 @@ const snapshot = (thread: OrchestrationThread): OrchestrationThreadStreamItem =>
281282

282283
const synchronized = (): OrchestrationThreadStreamItem => ({ kind: "synchronized" });
283284

285+
const sessionUpdated = (
286+
status: OrchestrationSession["status"],
287+
sequence: number,
288+
activeTurnId: TurnId | null,
289+
): OrchestrationThreadStreamItem => ({
290+
kind: "event",
291+
event: {
292+
eventId: EventId.make(`event-session-${sequence}`),
293+
sequence,
294+
occurredAt:
295+
sequence === CACHED_SNAPSHOT_SEQUENCE + 1
296+
? "2026-04-01T08:00:00.000Z"
297+
: "2026-04-01T09:00:00.000Z",
298+
commandId: null,
299+
causationEventId: null,
300+
correlationId: null,
301+
metadata: {},
302+
aggregateKind: "thread",
303+
aggregateId: THREAD_ID,
304+
type: "thread.session-set",
305+
payload: {
306+
threadId: THREAD_ID,
307+
session: {
308+
threadId: THREAD_ID,
309+
status,
310+
providerName: "codex",
311+
runtimeMode: "full-access",
312+
activeTurnId,
313+
lastError: null,
314+
updatedAt:
315+
sequence === CACHED_SNAPSHOT_SEQUENCE + 1
316+
? "2026-04-01T08:00:00.000Z"
317+
: "2026-04-01T09:00:00.000Z",
318+
},
319+
},
320+
},
321+
});
322+
284323
const titleUpdated = (title: string, sequence = 2): OrchestrationThreadStreamItem => ({
285324
kind: "event",
286325
event: {
@@ -397,6 +436,42 @@ describe("EnvironmentThreads", () => {
397436
}),
398437
);
399438

439+
it.effect("persists a settled snapshot before a batched turn starts", () =>
440+
Effect.gen(function* () {
441+
const harness = yield* makeHarness({
442+
cached: ACTIVE_THREAD,
443+
eventBatchSize: 2,
444+
});
445+
446+
yield* Queue.offer(
447+
harness.inputs,
448+
sessionUpdated("ready", CACHED_SNAPSHOT_SEQUENCE + 1, null),
449+
);
450+
yield* Queue.offer(
451+
harness.inputs,
452+
sessionUpdated("running", CACHED_SNAPSHOT_SEQUENCE + 2, TurnId.make("turn-2")),
453+
);
454+
yield* Queue.offer(harness.inputs, synchronized());
455+
456+
const state = yield* awaitThreadState(
457+
harness.observed,
458+
(value) =>
459+
value.status === "live" &&
460+
Option.isSome(value.data) &&
461+
value.data.value.session?.status === "running" &&
462+
value.data.value.session.activeTurnId === TurnId.make("turn-2"),
463+
);
464+
465+
expect(Option.getOrThrow(state.data).session?.status).toBe("running");
466+
yield* TestClock.adjust("500 millis");
467+
yield* Effect.yieldNow;
468+
469+
const saved = (yield* Ref.get(harness.savedThreads)).at(-1);
470+
expect(saved?.snapshotSequence).toBe(CACHED_SNAPSHOT_SEQUENCE + 1);
471+
expect(saved?.thread.session?.status).toBe("ready");
472+
}),
473+
);
474+
400475
it.effect("reduces live events and persists the latest thread", () =>
401476
Effect.gen(function* () {
402477
const harness = yield* makeHarness({ cached: BASE_THREAD });

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

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,15 @@ import {
3333
type EnvironmentThreadStatus,
3434
} from "./threadState.ts";
3535

36-
// Coalesce one render-sized burst so web and mobile derive their large thread
37-
// views once instead of once per streamed event. The size cap bounds catch-up
38-
// memory and prevents a quiet connection from waiting indefinitely.
3936
const THREAD_EVENT_BATCH_WINDOW = Duration.millis(16);
4037
const THREAD_EVENT_BATCH_MAX_SIZE = 64;
4138

4239
interface ThreadStreamBatchReduction {
4340
readonly state: EnvironmentThreadState;
4441
readonly lastSequence: number;
4542
readonly awaitingCompletion: boolean;
46-
readonly threadChanged: boolean;
4743
readonly threadDeleted: boolean;
44+
readonly persistableSnapshot: OrchestrationThreadDetailSnapshot | null;
4845
}
4946

5047
export interface EnvironmentThreadStateOptions {
@@ -61,8 +58,8 @@ function reduceThreadStreamItems(
6158
let lastSequence = currentSequence;
6259
let awaitingCompletion = currentAwaitingCompletion;
6360
let thread = Option.getOrNull(currentState.data);
64-
let threadChanged = false;
6561
let threadDeleted = false;
62+
let persistableSnapshot: OrchestrationThreadDetailSnapshot | null = null;
6663

6764
for (const item of items) {
6865
if (item.kind === "synchronized") {
@@ -80,8 +77,8 @@ function reduceThreadStreamItems(
8077
if (item.kind === "snapshot") {
8178
lastSequence = item.snapshot.snapshotSequence;
8279
thread = item.snapshot.thread;
83-
threadChanged = true;
8480
threadDeleted = false;
81+
persistableSnapshot = shouldPersistThread(thread) ? item.snapshot : null;
8582
state = {
8683
data: Option.some(thread),
8784
status: awaitingCompletion ? "synchronizing" : "live",
@@ -99,6 +96,7 @@ function reduceThreadStreamItems(
9996
if (item.event.type === "thread.deleted") {
10097
awaitingCompletion = false;
10198
threadDeleted = true;
99+
persistableSnapshot = null;
102100
state = {
103101
data: Option.none(),
104102
status: "deleted",
@@ -111,7 +109,9 @@ function reduceThreadStreamItems(
111109
const result = applyThreadDetailEvent(thread, item.event);
112110
if (result.kind === "updated") {
113111
thread = result.thread;
114-
threadChanged = true;
112+
if (shouldPersistThread(thread)) {
113+
persistableSnapshot = { snapshotSequence: lastSequence, thread };
114+
}
115115
state = {
116116
data: Option.some(thread),
117117
status: awaitingCompletion ? "synchronizing" : "live",
@@ -121,6 +121,7 @@ function reduceThreadStreamItems(
121121
awaitingCompletion = false;
122122
thread = null;
123123
threadDeleted = true;
124+
persistableSnapshot = null;
124125
state = {
125126
data: Option.none(),
126127
status: "deleted",
@@ -133,8 +134,8 @@ function reduceThreadStreamItems(
133134
state,
134135
lastSequence,
135136
awaitingCompletion,
136-
threadChanged,
137137
threadDeleted,
138+
persistableSnapshot,
138139
};
139140
}
140141

@@ -281,15 +282,8 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
281282
return;
282283
}
283284

284-
const thread = Option.getOrNull(reduction.state.data);
285-
// Active threads can update many times per second and retain large tool
286-
// payloads. The server remains the source of truth while a turn is active;
287-
// persist once it settles so cache encoding stays off the streaming path.
288-
if (reduction.threadChanged && thread !== null && shouldPersistThread(thread)) {
289-
yield* Queue.offer(persistence, {
290-
snapshotSequence: reduction.lastSequence,
291-
thread,
292-
});
285+
if (reduction.persistableSnapshot !== null) {
286+
yield* Queue.offer(persistence, reduction.persistableSnapshot);
293287
}
294288
});
295289

‎tasks/todo.md‎

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)