The best way to control Claude Code, Codex and more from any computer or phone

main
ts 2,615 lines 98.2 KB
Raw
1 import {
2 EnvironmentAuthorizationError,
3 EnvironmentId,
4 EventId,
5 MessageId,
6 NodeId,
7 ORCHESTRATION_V2_WS_METHODS,
8 RuntimeRequestId,
9 RunId,
10 ThreadId,
11 TurnItemId,
12 type OrchestrationV2ThreadDetailSnapshot,
13 type OrchestrationV2ThreadProjection,
14 type OrchestrationV2ThreadStreamItem,
15 type OrchestrationV2TurnItem,
16 } from "@t3tools/contracts";
17 import { describe, expect, it } from "@effect/vitest";
18 import * as DateTime from "effect/DateTime";
19 import * as Deferred from "effect/Deferred";
20 import * as Effect from "effect/Effect";
21 import * as Exit from "effect/Exit";
22 import * as Fiber from "effect/Fiber";
23 import * as Option from "effect/Option";
24 import * as Queue from "effect/Queue";
25 import * as Ref from "effect/Ref";
26 import * as Scope from "effect/Scope";
27 import * as Scheduler from "effect/Scheduler";
28 import * as Schema from "effect/Schema";
29 import * as Stream from "effect/Stream";
30 import * as SubscriptionRef from "effect/SubscriptionRef";
31 import * as TestClock from "effect/testing/TestClock";
32 import { HttpClient, HttpClientResponse } from "effect/http";
33 import { pendingBackgroundTurnItems } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
34
35 import type { WsRpcProtocolClient } from "../rpc/protocol.ts";
36 import {
37 AVAILABLE_CONNECTION_STATE,
38 PrimaryConnectionTarget,
39 type PreparedConnection,
40 type SupervisorConnectionState,
41 } from "../connection/model.ts";
42 import * as ConnectionWakeups from "../connection/wakeups.ts";
43 import * as EnvironmentSupervisor from "../connection/supervisor.ts";
44 import * as Persistence from "../platform/persistence.ts";
45 import {
46 ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION,
47 StoredOrchestrationThreadSnapshot,
48 decodeOrDiscardOrchestrationCache,
49 } from "../platform/orchestrationCache.ts";
50 import * as RpcSession from "../rpc/session.ts";
51 import { v2Now, v2Projection, v2ThreadId } from "./orchestrationV2TestFixtures.ts";
52 import { onThreadRequestsResolved, type ThreadRequestsResolved } from "./threadRequests.ts";
53 import { EMPTY_THREAD_HISTORY_META } from "./threadHistoryMerge.ts";
54 import * as ThreadHistoryController from "./threadHistoryController.ts";
55 import {
56 EMPTY_ENVIRONMENT_THREAD_STATE,
57 makeEnvironmentThreadState,
58 type EnvironmentThreadState,
59 type ThreadSnapshotLoadResult,
60 } from "./threads.ts";
61 import * as ThreadSnapshotLoader from "./threadSnapshotHttp.ts";
62
63 const TARGET = new PrimaryConnectionTarget({
64 environmentId: EnvironmentId.make("environment-1"),
65 label: "Test environment",
66 httpBaseUrl: "https://environment.example.test",
67 wsBaseUrl: "wss://environment.example.test",
68 });
69 const THREAD_ID = v2ThreadId;
70 const CACHED_SNAPSHOT_SEQUENCE = 7;
71 const PREPARED: PreparedConnection = {
72 environmentId: TARGET.environmentId,
73 label: TARGET.label,
74 httpBaseUrl: TARGET.httpBaseUrl,
75 socketUrl: TARGET.wsBaseUrl,
76 httpAuthorization: null,
77 target: TARGET,
78 };
79 const BASE_PROJECTION: OrchestrationV2ThreadProjection = {
80 ...v2Projection,
81 thread: { ...v2Projection.thread, title: "Cached thread" },
82 };
83
84 const TEST_DEFECT = { defect: "Malformed snapshot" } as const;
85
86 type TestThreadInput = OrchestrationV2ThreadStreamItem | Error | typeof TEST_DEFECT;
87
88 function testSession(
89 client: WsRpcProtocolClient,
90 initialConfig: Effect.Effect<object> = Effect.succeed({}),
91 ): RpcSession.RpcSession {
92 return {
93 client,
94 initialConfig: initialConfig as never,
95 subscribeServerConfig: (input = {}) => client.subscribeServerConfig(input),
96 ready: Effect.void,
97 probe: Effect.void,
98 inboundFrames: Effect.succeed(0),
99 closed: Effect.never,
100 };
101 }
102
103 function awaitThreadState(
104 observed: Queue.Queue<EnvironmentThreadState>,
105 predicate: (state: EnvironmentThreadState) => boolean,
106 ) {
107 return Queue.take(observed).pipe(
108 Effect.repeat({
109 until: predicate,
110 }),
111 );
112 }
113
114 const makeHarness = Effect.fn("TestEnvironmentThreads.makeHarness")(function* (options?: {
115 readonly cached?: OrchestrationV2ThreadProjection;
116 readonly cachedHistory?: {
117 readonly historyCursor: string | null;
118 readonly hasMoreHistory: boolean;
119 readonly latestLocalTurnOrdinal?: number | null;
120 };
121 readonly httpSnapshot?: ThreadSnapshotLoadResult;
122 /** Answers per load call; the last one repeats. Overrides `httpSnapshot`. */
123 readonly httpSnapshots?: readonly ThreadSnapshotLoadResult[];
124 /** The test sends the completion marker itself instead of the fake server sending it first. */
125 readonly completionMarker?: boolean;
126 readonly resumeCache?: NonNullable<Parameters<typeof makeEnvironmentThreadState>[1]>;
127 readonly loadCached?: ReturnType<Persistence.EnvironmentCacheStore["Service"]["loadThread"]>;
128 readonly saveThread?: Persistence.EnvironmentCacheStore["Service"]["saveThread"];
129 readonly historyPaging?: "enabled" | "no-http" | "no-controller";
130 readonly socketSnapshots?: true;
131 readonly initialConfig?: Effect.Effect<object>;
132 readonly historyHttpClient?: HttpClient.HttpClient;
133 readonly getTurnItem?: (input: {
134 readonly itemId: TurnItemId;
135 }) => Effect.Effect<{ readonly item: OrchestrationV2TurnItem | null }>;
136 }) {
137 const inputs = yield* Queue.unbounded<TestThreadInput>();
138 const observed = yield* Queue.unbounded<EnvironmentThreadState>();
139 const latest = yield* Ref.make<EnvironmentThreadState>(EMPTY_ENVIRONMENT_THREAD_STATE);
140 const retryCount = yield* Ref.make(0);
141 const subscriptionCount = yield* SubscriptionRef.make(0);
142 const loaderCalls = yield* Ref.make(0);
143 const lastSubscribeAfterSequence = yield* Ref.make<number | undefined>(undefined);
144 const lastRequestCompletionMarker = yield* Ref.make(false);
145 const lastAcceptBoundedSnapshot = yield* Ref.make<true | undefined>(undefined);
146 const lastAcceptCompactTurnItems = yield* Ref.make<true | undefined>(undefined);
147 const wakeups = yield* Queue.unbounded<ConnectionWakeups.ConnectionWakeup>();
148 const savedThreads = yield* Ref.make<ReadonlyArray<OrchestrationV2ThreadDetailSnapshot>>([]);
149 const removedThreads = yield* Ref.make<ReadonlyArray<ThreadId>>([]);
150 const supervisorState = yield* SubscriptionRef.make<SupervisorConnectionState>(
151 AVAILABLE_CONNECTION_STATE,
152 );
153 const streamFrom = (queue: Queue.Queue<TestThreadInput>) =>
154 Stream.fromQueue(queue).pipe(
155 Stream.mapEffect((input) =>
156 input === TEST_DEFECT
157 ? Effect.die(input)
158 : input instanceof Error
159 ? Effect.fail(input)
160 : Effect.succeed(input),
161 ),
162 );
163 const client = {
164 [ORCHESTRATION_V2_WS_METHODS.subscribeThread]: (input: {
165 readonly afterSequence?: number;
166 readonly requestCompletionMarker?: true;
167 readonly acceptBoundedSnapshot?: true;
168 readonly acceptCompactTurnItems?: true;
169 }) =>
170 Stream.unwrap(
171 Ref.set(lastSubscribeAfterSequence, input.afterSequence).pipe(
172 Effect.andThen(
173 Ref.set(lastRequestCompletionMarker, input.requestCompletionMarker === true),
174 ),
175 Effect.andThen(Ref.set(lastAcceptBoundedSnapshot, input.acceptBoundedSnapshot)),
176 Effect.andThen(Ref.set(lastAcceptCompactTurnItems, input.acceptCompactTurnItems)),
177 Effect.andThen(SubscriptionRef.update(subscriptionCount, (count) => count + 1)),
178 Effect.as(
179 options?.completionMarker === true
180 ? streamFrom(inputs)
181 : Stream.concat(Stream.make(synchronized()), streamFrom(inputs)),
182 ),
183 ),
184 ),
185 [ORCHESTRATION_V2_WS_METHODS.getTurnItem]: (input: { readonly itemId: TurnItemId }) =>
186 options?.getTurnItem?.(input) ?? Effect.die("Unexpected getTurnItem request"),
187 } as unknown as WsRpcProtocolClient;
188 const sessionConfig =
189 options?.initialConfig ??
190 Effect.succeed(options?.socketSnapshots ? { threadSnapshotPagination: true } : {});
191 const supervisorSession = yield* SubscriptionRef.make<Option.Option<RpcSession.RpcSession>>(
192 Option.some(testSession(client, sessionConfig)),
193 );
194 const prepared = yield* SubscriptionRef.make<Option.Option<PreparedConnection>>(
195 Option.some(PREPARED),
196 );
197 const snapshotLoader = ThreadSnapshotLoader.ThreadSnapshotLoader.of({
198 load: (_prepared, threadId) =>
199 Ref.updateAndGet(loaderCalls, (count) => count + 1).pipe(
200 Effect.map((count) =>
201 threadId === THREAD_ID
202 ? (options?.httpSnapshots?.[Math.min(count, options.httpSnapshots.length) - 1] ??
203 options?.httpSnapshot ??
204 ({ _tag: "unavailable" } satisfies ThreadSnapshotLoadResult))
205 : ({ _tag: "unavailable" } satisfies ThreadSnapshotLoadResult),
206 ),
207 ),
208 });
209 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
210 target: TARGET,
211 state: supervisorState,
212 session: supervisorSession,
213 prepared,
214 connect: Effect.void,
215 disconnect: Effect.void,
216 retryNow: Ref.update(retryCount, (count) => count + 1),
217 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
218 const cache = Persistence.EnvironmentCacheStore.of({
219 loadShell: () => Effect.succeedNone,
220 saveShell: () => Effect.void,
221 loadThread: (_environmentId, threadId) =>
222 options?.loadCached ??
223 Effect.succeed(
224 threadId === THREAD_ID && options?.cached !== undefined
225 ? Option.some({
226 snapshotSequence: CACHED_SNAPSHOT_SEQUENCE,
227 projection: options.cached,
228 ...(options.cachedHistory === undefined
229 ? {}
230 : {
231 historyCursor: options.cachedHistory.historyCursor,
232 hasMoreHistory: options.cachedHistory.hasMoreHistory,
233 ...(options.cachedHistory.latestLocalTurnOrdinal === undefined
234 ? {}
235 : {
236 latestLocalTurnOrdinal: options.cachedHistory.latestLocalTurnOrdinal,
237 }),
238 }),
239 })
240 : Option.none(),
241 ),
242 saveThread: (environmentId, thread) =>
243 Ref.update(savedThreads, (current) => [...current, thread]).pipe(
244 Effect.andThen(options?.saveThread?.(environmentId, thread) ?? Effect.void),
245 ),
246 removeThread: (_environmentId, threadId) =>
247 Ref.update(removedThreads, (current) => [...current, threadId]),
248 loadServerConfig: () => Effect.succeedNone,
249 saveServerConfig: () => Effect.void,
250 loadVcsRefs: () => Effect.succeedNone,
251 saveVcsRefs: () => Effect.void,
252 removeVcsRefs: () => Effect.void,
253 clearVcsRefs: () => Effect.void,
254 clear: () => Effect.void,
255 });
256 let makeThreadState = makeEnvironmentThreadState(THREAD_ID, options?.resumeCache).pipe(
257 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
258 Effect.provideService(Persistence.EnvironmentCacheStore, cache),
259 Effect.provideService(ThreadSnapshotLoader.ThreadSnapshotLoader, snapshotLoader),
260 Effect.provideService(
261 ConnectionWakeups.ConnectionWakeups,
262 ConnectionWakeups.ConnectionWakeups.of({ changes: Stream.fromQueue(wakeups) }),
263 ),
264 );
265 if (options?.historyPaging !== "no-http") {
266 makeThreadState = makeThreadState.pipe(
267 Effect.provideService(
268 HttpClient.HttpClient,
269 options?.historyHttpClient ??
270 HttpClient.make(() => Effect.die("Unexpected history HTTP request")),
271 ),
272 );
273 }
274 const historyController = yield* ThreadHistoryController.ThreadHistoryController.pipe(
275 Effect.provide(ThreadHistoryController.layer),
276 );
277 if (options?.historyPaging !== "no-controller") {
278 makeThreadState = makeThreadState.pipe(
279 Effect.provideService(ThreadHistoryController.ThreadHistoryController, historyController),
280 );
281 }
282 const threadState = yield* makeThreadState;
283 yield* SubscriptionRef.changes(threadState).pipe(
284 Stream.runForEach((state) =>
285 Ref.set(latest, state).pipe(Effect.andThen(Queue.offer(observed, state))),
286 ),
287 Effect.forkScoped,
288 );
289
290 return {
291 threadState,
292 loadWork: (anchorId: string) =>
293 historyController.loadEarlier(TARGET.environmentId, THREAD_ID, undefined, anchorId),
294 loadEarlier: (throughEntryId?: string) =>
295 historyController.loadEarlier(TARGET.environmentId, THREAD_ID, throughEntryId),
296 reconcilePendingWork: historyController.reconcilePendingWork(TARGET.environmentId, THREAD_ID),
297 inputs,
298 observed,
299 latest,
300 retryCount,
301 subscriptionCount,
302 loaderCalls,
303 lastSubscribeAfterSequence,
304 lastRequestCompletionMarker,
305 lastAcceptBoundedSnapshot,
306 lastAcceptCompactTurnItems,
307 supervisorState,
308 supervisorSession,
309 savedThreads,
310 removedThreads,
311 wakeups,
312 replaceSession: SubscriptionRef.set(
313 supervisorSession,
314 Option.some(testSession(client, sessionConfig)),
315 ),
316 };
317 });
318
319 const snapshot = (
320 projection: OrchestrationV2ThreadProjection,
321 snapshotSequence = 1,
322 ): Extract<OrchestrationV2ThreadStreamItem, { readonly kind: "snapshot" }> => ({
323 kind: "snapshot",
324 snapshotSequence,
325 projection,
326 });
327
328 const synchronized = (): OrchestrationV2ThreadStreamItem => ({ kind: "synchronized" });
329
330 const PENDING_QUESTION = {
331 id: RuntimeRequestId.make("question"),
332 nodeId: NodeId.make("question-node"),
333 providerTurnId: null,
334 nativeRequestRef: null,
335 kind: "user_input" as const,
336 status: "pending" as const,
337 responseCapability: { type: "message" as const },
338 createdAt: v2Now,
339 resolvedAt: null,
340 };
341
342 const QUESTION_CARD: OrchestrationV2TurnItem = {
343 id: TurnItemId.make("question-card"),
344 threadId: THREAD_ID,
345 runId: null,
346 nodeId: PENDING_QUESTION.nodeId,
347 providerThreadId: null,
348 providerTurnId: null,
349 nativeItemRef: null,
350 parentItemId: null,
351 ordinal: 1,
352 status: "running",
353 title: null,
354 startedAt: v2Now,
355 completedAt: null,
356 updatedAt: v2Now,
357 type: "user_input_request",
358 requestId: PENDING_QUESTION.id,
359 questions: [{ id: "scope", header: "Scope", question: "Which scope?", options: [] }],
360 };
361
362 const PROJECTION_WITH_QUESTION = {
363 ...BASE_PROJECTION,
364 runtimeRequests: [PENDING_QUESTION],
365 turnItems: [QUESTION_CARD],
366 };
367
368 const questionResolved = (sequence: number): OrchestrationV2ThreadStreamItem => ({
369 kind: "event",
370 sequence,
371 event: {
372 id: EventId.make("event-request"),
373 type: "runtime-request.updated",
374 threadId: THREAD_ID,
375 occurredAt: v2Now,
376 payload: { ...PENDING_QUESTION, status: "resolved", resolvedAt: v2Now },
377 },
378 });
379
380 const titleUpdated = (title: string, sequence = 2): OrchestrationV2ThreadStreamItem => {
381 const occurredAt = DateTime.makeUnsafe("2026-06-20T01:00:00.000Z");
382 return {
383 kind: "event",
384 sequence,
385 event: {
386 id: EventId.make("event-title"),
387 type: "thread.metadata-updated",
388 threadId: THREAD_ID,
389 occurredAt,
390 payload: { ...v2Projection.thread, title, updatedAt: occurredAt },
391 },
392 };
393 };
394
395 const deleted = (sequence = 3): OrchestrationV2ThreadStreamItem => {
396 const occurredAt = DateTime.makeUnsafe("2026-06-20T02:00:00.000Z");
397 return {
398 kind: "event",
399 sequence,
400 event: {
401 id: EventId.make("event-deleted"),
402 type: "thread.deleted",
403 threadId: THREAD_ID,
404 occurredAt,
405 payload: { ...v2Projection.thread, updatedAt: occurredAt, deletedAt: occurredAt },
406 },
407 };
408 };
409
410 describe("EnvironmentThreads", () => {
411 it.effect.each([
412 ["disk", false],
413 ["retained", false],
414 ["disk", true],
415 ["retained", true],
416 ] as const)(
417 "recovers a %s cache only when its pending question card is missing: %s",
418 ([cacheKind, hasCard]) =>
419 Effect.gen(function* () {
420 const projection = {
421 ...PROJECTION_WITH_QUESTION,
422 turnItems: hasCard ? [QUESTION_CARD] : [],
423 };
424 const schema = Schema.fromJsonString(StoredOrchestrationThreadSnapshot);
425 const encoded = yield* Schema.encodeEffect(schema)({
426 schemaVersion: ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION,
427 environmentId: TARGET.environmentId,
428 threadId: THREAD_ID,
429 snapshot: { snapshotSequence: CACHED_SNAPSHOT_SEQUENCE, projection },
430 });
431 const stored = yield* Schema.decodeEffect(schema)(encoded);
432 const h = yield* makeHarness({
433 loadCached: Effect.succeedSome(stored.snapshot),
434 ...(cacheKind === "retained"
435 ? {
436 resumeCache: {
437 owner: undefined,
438 snapshot: {
439 state: {
440 ...EMPTY_ENVIRONMENT_THREAD_STATE,
441 data: Option.some(projection),
442 status: "live",
443 },
444 sequence: CACHED_SNAPSHOT_SEQUENCE,
445 persisted: true,
446 },
447 },
448 }
449 : {}),
450 httpSnapshot: {
451 _tag: "present",
452 snapshot: {
453 snapshotSequence: CACHED_SNAPSHOT_SEQUENCE,
454 projection: PROJECTION_WITH_QUESTION,
455 },
456 },
457 completionMarker: true,
458 });
459 yield* Queue.offer(h.inputs, synchronized());
460 const state = yield* awaitThreadState(h.observed, (value) => value.status === "live");
461 expect(yield* Ref.get(h.loaderCalls)).toBe(hasCard ? 0 : 1);
462 expect(Option.getOrThrow(state.data)).toEqual(PROJECTION_WITH_QUESTION);
463 expect(yield* Ref.get(h.lastSubscribeAfterSequence)).toBe(CACHED_SNAPSHOT_SEQUENCE);
464 }),
465 );
466
467 it.effect("discards an oversized version-4 cache and requests a fresh bounded snapshot", () =>
468 Effect.gen(function* () {
469 const item: OrchestrationV2TurnItem = {
470 id: TurnItemId.make("old-unbounded-cache-item"),
471 threadId: THREAD_ID,
472 runId: null,
473 nodeId: null,
474 providerThreadId: null,
475 providerTurnId: null,
476 nativeItemRef: null,
477 parentItemId: null,
478 ordinal: 1,
479 status: "completed",
480 title: null,
481 startedAt: BASE_PROJECTION.updatedAt,
482 completedAt: BASE_PROJECTION.updatedAt,
483 updatedAt: BASE_PROJECTION.updatedAt,
484 type: "system_notice",
485 message: "x".repeat(1_100_000),
486 };
487 const schema = Schema.fromJsonString(StoredOrchestrationThreadSnapshot);
488 const encoded = yield* Schema.encodeEffect(schema)({
489 schemaVersion: ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION,
490 environmentId: TARGET.environmentId,
491 threadId: THREAD_ID,
492 snapshot: {
493 snapshotSequence: CACHED_SNAPSHOT_SEQUENCE,
494 projection: { ...BASE_PROJECTION, turnItems: [item] },
495 },
496 });
497 const stale = encoded.replace(
498 `"schemaVersion":${ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION}`,
499 '"schemaVersion":4',
500 );
501 let discarded = false;
502 const h = yield* makeHarness({
503 loadCached: decodeOrDiscardOrchestrationCache(
504 Schema.decodeEffect(schema)(stale).pipe(
505 Effect.map((stored) => Option.some(stored.snapshot)),
506 ),
507 Effect.sync(() => {
508 discarded = true;
509 }),
510 ),
511 httpSnapshot: {
512 _tag: "present",
513 snapshot: { snapshotSequence: CACHED_SNAPSHOT_SEQUENCE, projection: BASE_PROJECTION },
514 history: { historyCursor: "fresh-bounded-cursor", hasMoreHistory: true },
515 },
516 });
517 const state = yield* awaitThreadState(h.observed, (value) => value.status === "live");
518 expect(discarded).toBe(true);
519 expect(yield* Ref.get(h.loaderCalls)).toBe(1);
520 expect(Option.getOrThrow(state.data)).toEqual(BASE_PROJECTION);
521 expect(state.history.historyCursor).toBe("fresh-bounded-cursor");
522 expect(state.history.hasMoreHistory).toBe(true);
523 }),
524 );
525
526 it.effect.each(["read failure", "cache miss"] as const)(
527 "loads the server thread after a %s",
528 (scenario) =>
529 Effect.gen(function* () {
530 const h = yield* makeHarness({
531 loadCached:
532 scenario === "cache miss"
533 ? Effect.succeedNone
534 : Effect.fail(
535 new Persistence.ConnectionPersistenceError({
536 operation: "load-thread",
537 message: "The database connection is closing.",
538 }),
539 ),
540 httpSnapshot: {
541 _tag: "present",
542 snapshot: { snapshotSequence: 7, projection: BASE_PROJECTION },
543 },
544 });
545
546 const state = yield* awaitThreadState(h.observed, (value) => value.status === "live");
547 expect(Option.getOrThrow(state.data)).toEqual(BASE_PROJECTION);
548 expect(yield* Ref.get(h.loaderCalls)).toBe(1);
549 expect(yield* SubscriptionRef.get(h.subscriptionCount)).toBe(1);
550 }),
551 );
552
553 it.effect("flushes newer data after an older background write completes", () =>
554 Effect.gen(function* () {
555 const writing = yield* Deferred.make<void>();
556 const written = yield* Deferred.make<void>();
557 const release = yield* Deferred.make<void>();
558 const saved = yield* Effect.scoped(
559 Effect.gen(function* () {
560 const h = yield* makeHarness({
561 cached: BASE_PROJECTION,
562 saveThread: (_environmentId, snapshot) =>
563 snapshot.snapshotSequence === 8
564 ? Deferred.succeed(writing, undefined).pipe(
565 Effect.andThen(Deferred.await(release)),
566 Effect.andThen(Deferred.succeed(written, undefined)),
567 )
568 : Effect.void,
569 });
570 yield* Queue.offer(h.inputs, titleUpdated("First update", 8));
571 yield* awaitThreadState(
572 h.observed,
573 (value) => Option.getOrNull(value.data)?.thread.title === "First update",
574 );
575 yield* TestClock.adjust("500 millis");
576 yield* Deferred.await(writing);
577 yield* Queue.offer(h.inputs, titleUpdated("Newer update", 9));
578 yield* awaitThreadState(
579 h.observed,
580 (value) => Option.getOrNull(value.data)?.thread.title === "Newer update",
581 );
582 yield* Deferred.succeed(release, undefined);
583 yield* Deferred.await(written);
584 return h.savedThreads;
585 }),
586 );
587 expect(
588 (yield* Ref.get(saved)).map((snapshot) => [
589 snapshot.snapshotSequence,
590 snapshot.projection.thread.title,
591 ]),
592 ).toEqual([
593 [8, "First update"],
594 [9, "Newer update"],
595 ]);
596 }),
597 );
598
599 it.effect("does not resume past a canceled event whose data was not applied", () =>
600 Effect.gen(function* () {
601 const resumeCache: NonNullable<Parameters<typeof makeEnvironmentThreadState>[1]> = {
602 snapshot: undefined,
603 owner: undefined,
604 };
605 const scope = yield* Effect.acquireRelease(Scope.make(), (scope) =>
606 Scope.close(scope, Exit.void),
607 );
608 const first = yield* makeHarness({
609 httpSnapshot: {
610 _tag: "present",
611 snapshot: {
612 projection: BASE_PROJECTION,
613 snapshotSequence: CACHED_SNAPSHOT_SEQUENCE,
614 },
615 },
616 resumeCache,
617 }).pipe(Effect.provideService(Scope.Scope, scope));
618 yield* awaitThreadState(first.observed, (value) => value.status === "live");
619 const applying = yield* Deferred.make<void>();
620 const update = titleUpdated("Not applied", 8);
621 if (update.kind !== "event") return yield* Effect.die("Expected an event");
622 let sequenceReads = 0;
623 Object.defineProperty(update, "sequence", {
624 get: () => {
625 if (++sequenceReads === 2) Deferred.doneUnsafe(applying, Exit.void);
626 return 8;
627 },
628 });
629 yield* first.threadState.semaphore.take(1);
630 yield* Queue.offer(first.inputs, update);
631 yield* Deferred.await(applying);
632 yield* Scope.close(scope, Exit.void);
633 yield* first.threadState.semaphore.release(1);
634
635 const resumed = yield* makeHarness({ resumeCache });
636 const state = yield* awaitThreadState(resumed.observed, (value) => value.status === "live");
637 expect(Option.getOrThrow(state.data).thread.title).toBe(BASE_PROJECTION.thread.title);
638 expect(yield* Ref.get(resumed.lastSubscribeAfterSequence)).toBe(CACHED_SNAPSHOT_SEQUENCE);
639 expect(yield* Ref.get(first.savedThreads)).toEqual([
640 {
641 projection: BASE_PROJECTION,
642 snapshotSequence: CACHED_SNAPSHOT_SEQUENCE,
643 historyCursor: null,
644 hasMoreHistory: false,
645 latestLocalTurnOrdinal: null,
646 },
647 ]);
648 }),
649 );
650
651 it.effect("prevents an old scope from replacing its successor's cached data", () =>
652 Effect.gen(function* () {
653 const announced: ThreadRequestsResolved[] = [];
654 yield* Effect.acquireRelease(
655 Effect.sync(() => onThreadRequestsResolved((event) => announced.push(event))),
656 (unsubscribe) => Effect.sync(unsubscribe),
657 );
658 const resumeCache: NonNullable<Parameters<typeof makeEnvironmentThreadState>[1]> = {
659 snapshot: undefined,
660 owner: undefined,
661 };
662 const oldScope = yield* Effect.acquireRelease(Scope.make(), (scope) =>
663 Scope.close(scope, Exit.void),
664 );
665 const old = yield* makeHarness({
666 cached: PROJECTION_WITH_QUESTION,
667 resumeCache,
668 }).pipe(Effect.provideService(Scope.Scope, oldScope));
669 yield* awaitThreadState(old.observed, (value) => value.status === "live");
670 const successor = yield* makeHarness({ resumeCache });
671 yield* Queue.offer(successor.inputs, titleUpdated("Successor title", 10));
672 yield* awaitThreadState(
673 successor.observed,
674 (value) => Option.getOrNull(value.data)?.thread.title === "Successor title",
675 );
676 announced.length = 0;
677 yield* Queue.offerAll(old.inputs, [questionResolved(8), titleUpdated("Old scope title", 9)]);
678 yield* awaitThreadState(
679 old.observed,
680 (value) => Option.getOrNull(value.data)?.thread.title === "Old scope title",
681 );
682 yield* Scope.close(oldScope, Exit.void);
683
684 // Draft pruners would delete the answer the successor still waits on.
685 expect(announced).toEqual([]);
686 expect(resumeCache.snapshot?.sequence).toBe(10);
687 expect(Option.getOrThrow(resumeCache.snapshot!.state.data).thread.title).toBe(
688 "Successor title",
689 );
690 expect(yield* Ref.get(old.savedThreads)).toEqual([]);
691 }),
692 );
693
694 it.effect("does not let a delayed cache read replace an initialized successor", () =>
695 Effect.gen(function* () {
696 const resumeCache: NonNullable<Parameters<typeof makeEnvironmentThreadState>[1]> = {
697 snapshot: undefined,
698 owner: undefined,
699 };
700 const loading = yield* Deferred.make<void>();
701 const response = yield* Deferred.make<Option.Option<OrchestrationV2ThreadDetailSnapshot>>();
702 const oldFiber = yield* makeHarness({
703 resumeCache,
704 loadCached: Deferred.succeed(loading, undefined).pipe(
705 Effect.andThen(Deferred.await(response)),
706 ),
707 }).pipe(Effect.forkScoped);
708 yield* Deferred.await(loading);
709 const successor = yield* makeHarness({ cached: BASE_PROJECTION, resumeCache });
710 yield* awaitThreadState(successor.observed, (value) => value.status === "live");
711 yield* Deferred.succeed(
712 response,
713 Option.some({
714 snapshotSequence: 3,
715 projection: {
716 ...BASE_PROJECTION,
717 thread: { ...BASE_PROJECTION.thread, title: "Old disk data" },
718 },
719 }),
720 );
721 const old = yield* Fiber.join(oldFiber);
722 yield* awaitThreadState(old.observed, (value) => value.status === "live");
723
724 expect(resumeCache.snapshot?.sequence).toBe(CACHED_SNAPSHOT_SEQUENCE);
725 expect(Option.getOrThrow(resumeCache.snapshot!.state.data).thread.title).toBe(
726 BASE_PROJECTION.thread.title,
727 );
728 }),
729 );
730
731 it.effect("does not let an old deletion remove its successor's persisted cache", () =>
732 Effect.gen(function* () {
733 const resumeCache: NonNullable<Parameters<typeof makeEnvironmentThreadState>[1]> = {
734 snapshot: undefined,
735 owner: undefined,
736 };
737 const old = yield* makeHarness({ cached: BASE_PROJECTION, resumeCache });
738 yield* awaitThreadState(old.observed, (value) => value.status === "live");
739 const successor = yield* makeHarness({ resumeCache });
740 yield* Queue.offer(successor.inputs, titleUpdated("Successor title", 9));
741 yield* awaitThreadState(
742 successor.observed,
743 (value) => Option.getOrNull(value.data)?.thread.title === "Successor title",
744 );
745 const update = deleted();
746 if (update.kind !== "event") return yield* Effect.die("Expected an event");
747 yield* Queue.offer(old.inputs, { ...update, sequence: 8 });
748 yield* awaitThreadState(old.observed, (value) => value.status === "deleted");
749
750 expect(resumeCache.snapshot?.sequence).toBe(9);
751 expect(yield* Ref.get(old.removedThreads)).toEqual([]);
752 }),
753 );
754
755 it.effect("retains a deletion instead of restoring the old disk snapshot", () =>
756 Effect.gen(function* () {
757 const resumeCache: NonNullable<Parameters<typeof makeEnvironmentThreadState>[1]> = {
758 snapshot: undefined,
759 owner: undefined,
760 };
761 yield* Effect.scoped(
762 Effect.gen(function* () {
763 const first = yield* makeHarness({ cached: BASE_PROJECTION, resumeCache });
764 const update = deleted();
765 if (update.kind !== "event") return yield* Effect.die("Expected an event");
766 yield* Queue.offer(first.inputs, { ...update, sequence: 8 });
767 yield* awaitThreadState(first.observed, (value) => value.status === "deleted");
768 }),
769 );
770 const resumed = yield* makeHarness({ cached: BASE_PROJECTION, resumeCache });
771 const state = yield* awaitThreadState(
772 resumed.observed,
773 (value) => value.status === "deleted",
774 );
775 expect(Option.isNone(state.data)).toBe(true);
776 expect(yield* Ref.get(resumed.loaderCalls)).toBe(0);
777 }),
778 );
779
780 it.effect("asks the server again after an HTTP miss, since the thread may not exist yet", () =>
781 Effect.gen(function* () {
782 const resumeCache: NonNullable<Parameters<typeof makeEnvironmentThreadState>[1]> = {
783 snapshot: undefined,
784 owner: undefined,
785 };
786 yield* Effect.scoped(
787 Effect.gen(function* () {
788 const first = yield* makeHarness({ httpSnapshot: { _tag: "missing" }, resumeCache });
789 yield* awaitThreadState(first.observed, (value) => value.status === "deleted");
790 }),
791 );
792 const resumed = yield* makeHarness({
793 resumeCache,
794 httpSnapshot: {
795 _tag: "present",
796 snapshot: { snapshotSequence: CACHED_SNAPSHOT_SEQUENCE, projection: BASE_PROJECTION },
797 },
798 });
799 const state = yield* awaitThreadState(resumed.observed, (value) => value.status === "live");
800 expect(Option.getOrThrow(state.data)).toEqual(BASE_PROJECTION);
801 expect(yield* Ref.get(resumed.loaderCalls)).toBe(1);
802 }),
803 );
804
805 it.effect("keeps asking a mounted thread over HTTP after a miss until it exists", () =>
806 Effect.gen(function* () {
807 const harness = yield* makeHarness({
808 httpSnapshots: [
809 { _tag: "missing" },
810 { _tag: "missing" },
811 {
812 _tag: "present",
813 snapshot: { snapshotSequence: CACHED_SNAPSHOT_SEQUENCE, projection: BASE_PROJECTION },
814 },
815 ],
816 });
817 yield* awaitThreadState(harness.observed, (value) => value.status === "deleted");
818 yield* TestClock.adjust("250 millis");
819 yield* TestClock.adjust("500 millis");
820 const state = yield* awaitThreadState(harness.observed, (value) => value.status === "live");
821 expect(Option.getOrThrow(state.data)).toEqual(BASE_PROJECTION);
822 expect(yield* Ref.get(harness.loaderCalls)).toBe(3);
823 }),
824 );
825
826 it.effect.each([
827 // The HTTP retry decides, so a missing thread never flashes an error.
828 {
829 label: "a socket-only open asks HTTP",
830 socketSnapshots: true,
831 httpSnapshot: { _tag: "missing" },
832 failure: new Error("Thread not found"),
833 status: "deleted",
834 loads: [0, 1],
835 },
836 // A defect never retries on its own, so the socket-only open resubscribes once.
837 {
838 label: "a socket-only open that hits a defect asks HTTP",
839 socketSnapshots: true,
840 httpSnapshot: { _tag: "missing" },
841 failure: TEST_DEFECT,
842 status: "deleted",
843 loads: [0, 1],
844 },
845 // Authorization failures are never retried, so the error must show at once.
846 {
847 label: "a socket-only open shows an authorization error",
848 socketSnapshots: true,
849 httpSnapshot: { _tag: "missing" },
850 failure: new EnvironmentAuthorizationError({
851 message: "Not allowed",
852 requiredScope: "orchestration:read",
853 }),
854 status: "empty",
855 loads: [0, 0],
856 },
857 // HTTP already answered this attempt, so the error shows before the usual retry.
858 {
859 label: "an HTTP open shows the error",
860 socketSnapshots: undefined,
861 httpSnapshot: { _tag: "unavailable" },
862 failure: new Error("Socket closed"),
863 status: "empty",
864 loads: [1, 2],
865 },
866 ] as const)(
867 "after a cold open fails, $label",
868 ({ socketSnapshots, httpSnapshot, failure, status, loads: [before, after] }) =>
869 Effect.gen(function* () {
870 const harness = yield* makeHarness({
871 httpSnapshot,
872 ...(socketSnapshots ? { socketSnapshots } : {}),
873 });
874 yield* SubscriptionRef.changes(harness.subscriptionCount).pipe(
875 Stream.filter((count) => count === 1),
876 Stream.runHead,
877 );
878 expect(yield* Ref.get(harness.lastAcceptBoundedSnapshot)).toBe(true);
879 expect(yield* Ref.get(harness.loaderCalls)).toBe(before);
880
881 yield* Queue.offer(harness.inputs, failure);
882 yield* TestClock.adjust("250 millis");
883 const settled = yield* awaitThreadState(
884 harness.observed,
885 (value) => value.status === "deleted" || Option.isSome(value.error),
886 );
887 expect(settled.status).toBe(status);
888 expect(yield* Ref.get(harness.loaderCalls)).toBe(after);
889 }),
890 );
891
892 it.effect("retries a defective socket-only open over HTTP only once", () =>
893 Effect.gen(function* () {
894 let configReads = 0;
895 const harness = yield* makeHarness({
896 httpSnapshot: { _tag: "missing" },
897 // The retry's config read dies too, so a stale flag would resubscribe without backing off.
898 initialConfig: Effect.suspend(() =>
899 ++configReads === 1
900 ? Effect.succeed({ threadSnapshotPagination: true })
901 : Effect.die("Config lost"),
902 ),
903 });
904 yield* SubscriptionRef.changes(harness.subscriptionCount).pipe(
905 Stream.filter((count) => count === 1),
906 Stream.runHead,
907 );
908
909 yield* Queue.offer(harness.inputs, TEST_DEFECT);
910 const settled = yield* awaitThreadState(harness.observed, (value) =>
911 Option.isSome(value.error),
912 );
913 expect(settled.status).toBe("empty");
914 expect(configReads).toBe(2);
915 }),
916 );
917
918 it.effect("asks HTTP when a socket-only open loses its session before any data", () =>
919 Effect.gen(function* () {
920 const harness = yield* makeHarness({
921 httpSnapshot: { _tag: "missing" },
922 socketSnapshots: true,
923 });
924 yield* SubscriptionRef.changes(harness.subscriptionCount).pipe(
925 Stream.filter((count) => count === 1),
926 Stream.runHead,
927 );
928 expect(yield* Ref.get(harness.loaderCalls)).toBe(0);
929
930 // A timed-out session never reaches onExpectedFailure.
931 yield* harness.replaceSession;
932 const settled = yield* awaitThreadState(
933 harness.observed,
934 (value) => value.status === "deleted",
935 );
936 expect(settled.status).toBe("deleted");
937 expect(yield* Ref.get(harness.loaderCalls)).toBe(1);
938 }),
939 );
940
941 it.effect("resumes a warm cache via afterSequence without an HTTP fetch", () =>
942 Effect.gen(function* () {
943 const harness = yield* makeHarness({ cached: BASE_PROJECTION });
944
945 yield* Queue.offer(harness.inputs, titleUpdated("Live title", CACHED_SNAPSHOT_SEQUENCE + 1));
946 const live = yield* awaitThreadState(
947 harness.observed,
948 (value) =>
949 value.status === "live" &&
950 Option.isSome(value.data) &&
951 value.data.value.thread.title === "Live title",
952 );
953
954 // Readers compare this to a command's commit sequence; it moves with the data it describes.
955 expect(live.sequence).toBe(CACHED_SNAPSHOT_SEQUENCE + 1);
956 expect(yield* Ref.get(harness.lastSubscribeAfterSequence)).toBe(CACHED_SNAPSHOT_SEQUENCE);
957 expect(yield* Ref.get(harness.loaderCalls)).toBe(0);
958 }),
959 );
960
961 describe("cached work (#18188)", () => {
962 const at = DateTime.makeUnsafe("2026-06-20T00:00:00.000Z");
963 const parentThreadId = ThreadId.make("thread:parent");
964 const command = (
965 id: string,
966 status: "running" | "completed",
967 exitCode?: number,
968 ordinal = id === "older-command" ? 1 : 2,
969 ): Extract<OrchestrationV2TurnItem, { type: "command_execution" }> => ({
970 id: TurnItemId.make(id),
971 threadId: THREAD_ID,
972 runId: null,
973 nodeId: null,
974 providerThreadId: null,
975 providerTurnId: null,
976 nativeItemRef: null,
977 parentItemId: null,
978 ordinal,
979 status,
980 title: null,
981 startedAt: at,
982 completedAt: status === "completed" ? at : null,
983 updatedAt: at,
984 type: "command_execution",
985 input: `run ${id}`,
986 output: "",
987 ...(exitCode === undefined ? {} : { exitCode }),
988 });
989 const localRow = (item: OrchestrationV2TurnItem, position: number) => ({
990 position,
991 visibility: "local" as const,
992 sourceThreadId: THREAD_ID,
993 sourceItemId: item.id,
994 item,
995 });
996 const cachedItem = (item: OrchestrationV2TurnItem) => ({
997 ...BASE_PROJECTION,
998 turnItems: [item],
999 visibleTurnItems: [localRow(item, 0)],
1000 });
1001 const historyPage = (
1002 request: Parameters<typeof HttpClientResponse.fromWeb>[0],
1003 items: ReadonlyArray<ReturnType<typeof localRow>>,
1004 nextCursor: string | null,
1005 ) =>
1006 HttpClientResponse.fromWeb(
1007 request,
1008 Response.json({
1009 snapshotSequence: CACHED_SNAPSHOT_SEQUENCE,
1010 items,
1011 nextCursor,
1012 hasMoreHistory: nextCursor !== null,
1013 }),
1014 );
1015 it.effect("loads a long unwrapped turn through its user message and resumes failed pages", () =>
1016 Effect.gen(function* () {
1017 const prompt = {
1018 ...command("prompt", "completed", 0, 0),
1019 type: "user_message" as const,
1020 messageId: MessageId.make("prompt"),
1021 inputIntent: "turn_start" as const,
1022 createdBy: "user" as const,
1023 creationSource: "web" as const,
1024 text: "Do the work",
1025 attachments: [],
1026 };
1027 const work = Array.from({ length: 1_000 }, (_, index) =>
1028 command(`work-${index + 1}`, "completed", 0, index + 1),
1029 );
1030 const older = [prompt, ...work.slice(0, -1)];
1031 const tail = work.at(-1)!;
1032 const requests: string[] = [];
1033 let failed = false;
1034 const harness = yield* makeHarness({
1035 cached: cachedItem(tail),
1036 cachedHistory: { historyCursor: "1000", hasMoreHistory: true },
1037 historyHttpClient: HttpClient.make((request, url) => {
1038 const cursor = url.searchParams.get("cursor")!;
1039 requests.push(cursor);
1040 if (cursor === "before-user") return Effect.succeed(historyPage(request, [], cursor));
1041 if (cursor === "925" && !failed) {
1042 failed = true;
1043 return Effect.succeed(
1044 HttpClientResponse.fromWeb(request, new Response(null, { status: 503 })),
1045 );
1046 }
1047 const end = Number(cursor);
1048 expect(Number.isFinite(end)).toBe(true);
1049 const start = Math.max(0, end - 75);
1050 return Effect.succeed(
1051 historyPage(
1052 request,
1053 older.slice(start, end).map(localRow),
1054 start === 0 ? "before-user" : String(start),
1055 ),
1056 );
1057 }),
1058 });
1059 expect((yield* harness.loadEarlier())._tag).toBe("error");
1060 expect(requests).toEqual(["1000", "925"]);
1061 expect(yield* harness.loadEarlier()).toEqual({ _tag: "loaded" });
1062 const state = yield* SubscriptionRef.get(harness.threadState);
1063 expect(
1064 Option.getOrThrow(state.data).visibleTurnItems.map((row) => row.sourceItemId),
1065 ).toEqual([prompt, ...work].map((item) => item.id));
1066 expect(state.history).toMatchObject({
1067 historyCursor: "before-user",
1068 hasMoreHistory: true,
1069 loading: false,
1070 });
1071 expect(requests.slice(1, 3)).toEqual(["925", "925"]);
1072 expect(yield* harness.loadEarlier()).toEqual({
1073 _tag: "error",
1074 message: "History did not advance.",
1075 });
1076 expect((yield* SubscriptionRef.get(harness.threadState)).history.loading).toBe(false);
1077 }),
1078 );
1079 const boundary = (fields: object, id = "boundary", ordinal = 2) => ({
1080 ...command(id, "completed", 0, ordinal),
1081 ...fields,
1082 });
1083 const message = (
1084 inputIntent: "turn_start" | "steer",
1085 createdBy: "agent" | "user",
1086 id = "boundary",
1087 ordinal = 2,
1088 ) =>
1089 boundary(
1090 {
1091 type: "user_message",
1092 messageId: MessageId.make(id),
1093 inputIntent,
1094 createdBy,
1095 creationSource: "mcp",
1096 text: "Run every check",
1097 attachments: [],
1098 },
1099 id,
1100 ordinal,
1101 );
1102 it.effect.each([
1103 ["an agent-started turn", message("turn_start", "agent"), ["3"]],
1104 [
1105 "a notification",
1106 boundary({
1107 type: "notification",
1108 source: { kind: "delegated_task", taskIds: [] },
1109 outcome: "completed",
1110 summary: "Checks finished",
1111 }),
1112 ["3"],
1113 ],
1114 ["a steer, paging on to its turn", message("steer", "user"), ["3", "2", "1"]],
1115 ] as const)("stops paging earlier history at %s", ([, item, expected]) =>
1116 Effect.gen(function* () {
1117 const pages: Record<string, ReadonlyArray<unknown>> = {
1118 "3": [item],
1119 "2": [command("work-1", "completed", 0, 1)],
1120 "1": [message("turn_start", "user", "opener", 0)],
1121 };
1122 const requests: string[] = [];
1123 const harness = yield* makeHarness({
1124 cached: cachedItem(command("tail", "completed", 0, 3)),
1125 cachedHistory: { historyCursor: "3", hasMoreHistory: true },
1126 historyHttpClient: HttpClient.make((request, url) => {
1127 const cursor = url.searchParams.get("cursor")!;
1128 requests.push(cursor);
1129 const items = (pages[cursor] ?? []) as ReadonlyArray<OrchestrationV2TurnItem>;
1130 return Effect.succeed(
1131 historyPage(request, items.map(localRow), String(Number(cursor) - 1)),
1132 );
1133 }),
1134 });
1135 expect(yield* harness.loadEarlier()).toEqual({ _tag: "loaded" });
1136 expect(requests).toEqual(expected);
1137 }),
1138 );
1139 it.effect("loads deferred work on demand and resumes a failed page", () =>
1140 Effect.gen(function* () {
1141 const anchor = {
1142 ...command("anchor", "completed", 0, 1),
1143 deferredWork: { cursor: "first", itemCount: 3, lastOrdinal: 3 },
1144 };
1145 const requests: string[] = [];
1146 let failed = false;
1147 const harness = yield* makeHarness({
1148 cached: cachedItem(anchor),
1149 cachedHistory: { historyCursor: "older", hasMoreHistory: true },
1150 historyHttpClient: HttpClient.make((request, url) => {
1151 expect(url.searchParams.get("view")).toBe("work");
1152 const cursor = url.searchParams.get("cursor")!;
1153 requests.push(cursor);
1154 if (cursor === "second" && !failed) {
1155 failed = true;
1156 return Effect.succeed(
1157 HttpClientResponse.fromWeb(request, new Response("Unavailable", { status: 503 })),
1158 );
1159 }
1160 return Effect.succeed(
1161 historyPage(
1162 request,
1163 [
1164 localRow(
1165 command(`work-${cursor}`, "completed", 0, cursor === "first" ? 3 : 2),
1166 0,
1167 ),
1168 ],
1169 cursor === "first" ? "second" : null,
1170 ),
1171 );
1172 }),
1173 });
1174 expect(requests).toEqual([]);
1175 expect((yield* harness.loadWork(anchor.id))._tag).toBe("error");
1176 const partial = Option.getOrThrow((yield* SubscriptionRef.get(harness.threadState)).data);
1177 expect(partial.visibleTurnItems[0]?.item.deferredWork?.cursor).toBe("second");
1178 expect(yield* harness.loadWork(anchor.id)).toEqual({ _tag: "loaded" });
1179 const state = yield* SubscriptionRef.get(harness.threadState);
1180 expect(
1181 Option.getOrThrow(state.data).visibleTurnItems.map((row) => row.item.ordinal),
1182 ).toEqual([1, 2, 3]);
1183 expect(state.history.historyCursor).toBe("older");
1184 expect(requests).toEqual(["first", "second", "second"]);
1185 }),
1186 );
1187
1188 it.effect.each(["deferred", "complete"] as const)(
1189 "settles a replaced work request when the new snapshot is %s",
1190 (mode) =>
1191 Effect.gen(function* () {
1192 const anchor = {
1193 ...command("anchor", "completed"),
1194 deferredWork: { cursor: "before", itemCount: 80, lastOrdinal: 80 },
1195 };
1196 const asked = yield* Deferred.make<void>();
1197 const release = yield* Deferred.make<void>();
1198 const harness = yield* makeHarness({
1199 cached: cachedItem(anchor),
1200 historyHttpClient: HttpClient.make((request) =>
1201 Effect.gen(function* () {
1202 yield* Deferred.succeed(asked, undefined);
1203 yield* Deferred.await(release);
1204 return historyPage(request, [], null);
1205 }),
1206 ),
1207 });
1208 const loading = yield* harness.loadWork(anchor.id).pipe(Effect.forkChild);
1209 yield* Deferred.await(asked);
1210 const replacement =
1211 mode === "deferred"
1212 ? { ...anchor, deferredWork: { ...anchor.deferredWork, cursor: "after" } }
1213 : command("anchor", "completed");
1214 yield* Queue.offer(
1215 harness.inputs,
1216 snapshot(
1217 {
1218 ...BASE_PROJECTION,
1219 thread: { ...BASE_PROJECTION.thread, title: "Replacement" },
1220 turnItems: [replacement],
1221 visibleTurnItems: [localRow(replacement, 0)],
1222 },
1223 CACHED_SNAPSHOT_SEQUENCE + 1,
1224 ),
1225 );
1226 yield* awaitThreadState(
1227 harness.observed,
1228 (value) => Option.getOrNull(value.data)?.thread.title === "Replacement",
1229 );
1230 yield* Deferred.succeed(release, undefined);
1231 expect((yield* Fiber.join(loading))._tag).toBe(mode === "deferred" ? "error" : "noop");
1232 }),
1233 );
1234
1235 it.effect("does not mark replacement history loading when a queued request becomes stale", () =>
1236 Effect.gen(function* () {
1237 const harness = yield* makeHarness({
1238 cached: BASE_PROJECTION,
1239 cachedHistory: { historyCursor: "older", hasMoreHistory: true },
1240 historyHttpClient: HttpClient.make((request) =>
1241 Effect.succeed(historyPage(request, [], null)),
1242 ),
1243 });
1244 const held = yield* Deferred.make<void>();
1245 const release = yield* Deferred.make<void>();
1246 const replacement = yield* SubscriptionRef.updateEffect(harness.threadState, (latest) =>
1247 Deferred.succeed(held, undefined).pipe(
1248 Effect.andThen(Deferred.await(release)),
1249 Effect.as({ ...latest, history: { ...latest.history, historyCursor: "replacement" } }),
1250 ),
1251 ).pipe(Effect.forkChild({ startImmediately: true }));
1252 yield* Deferred.await(held);
1253 const dispatcher = new Scheduler.MixedScheduler("sync").makeDispatcher();
1254 const loading = yield* harness.loadEarlier().pipe(
1255 Effect.forkChild({ startImmediately: true }),
1256 Effect.provideService(Scheduler.Scheduler, {
1257 executionMode: "sync",
1258 shouldYield: () => false,
1259 makeDispatcher: () => dispatcher,
1260 }),
1261 );
1262 dispatcher.flush();
1263 yield* Deferred.succeed(release, undefined);
1264 yield* Fiber.join(replacement);
1265 dispatcher.flush();
1266 expect(yield* Fiber.join(loading)).toEqual({ _tag: "noop" });
1267 expect((yield* SubscriptionRef.get(harness.threadState)).history).toMatchObject({
1268 historyCursor: "replacement",
1269 loading: false,
1270 });
1271 }),
1272 );
1273
1274 it.effect("loads targeted messages without fetching unrelated deferred work", () =>
1275 Effect.gen(function* () {
1276 const anchor = {
1277 ...command("anchor", "completed"),
1278 deferredWork: { cursor: "work", itemCount: 80, lastOrdinal: 80 },
1279 };
1280 const message = {
1281 ...command("older-message", "completed"),
1282 type: "assistant_message" as const,
1283 messageId: MessageId.make("older-message"),
1284 text: "Older response",
1285 streaming: false,
1286 };
1287 const views: Array<string | null> = [];
1288 const harness = yield* makeHarness({
1289 cached: cachedItem(anchor),
1290 cachedHistory: { historyCursor: "older", hasMoreHistory: true },
1291 historyHttpClient: HttpClient.make((request, url) => {
1292 views.push(url.searchParams.get("view"));
1293 return Effect.succeed(
1294 url.searchParams.get("view") === "work"
1295 ? HttpClientResponse.fromWeb(request, new Response("Unavailable", { status: 503 }))
1296 : historyPage(request, [localRow(message, 0)], null),
1297 );
1298 }),
1299 });
1300 expect(yield* harness.loadEarlier(message.messageId)).toEqual({ _tag: "loaded" });
1301 const state = yield* SubscriptionRef.get(harness.threadState);
1302 expect(
1303 Option.getOrThrow(state.data).visibleTurnItems.some(
1304 (row) => row.sourceItemId === message.id,
1305 ),
1306 ).toBe(true);
1307 expect(views).toEqual(["conversation", null]);
1308 }),
1309 );
1310
1311 // A fork's parent row keeps its own source; the child's Stop must not touch it.
1312 const inheritedRow = {
1313 position: 0,
1314 visibility: "inherited" as const,
1315 sourceThreadId: parentThreadId,
1316 sourceItemId: TurnItemId.make("cached-command"),
1317 item: { ...command("cached-command", "running"), threadId: parentThreadId },
1318 };
1319 const cachedProjection = (items: ReadonlyArray<OrchestrationV2TurnItem>) => ({
1320 ...BASE_PROJECTION,
1321 turnItems: items,
1322 visibleTurnItems: [inheritedRow, ...items.map((item, index) => localRow(item, index + 1))],
1323 });
1324 // Loaded older history the bounded window would drop, next to the stale item.
1325 const cached: OrchestrationV2ThreadProjection = cachedProjection([
1326 command("older-command", "completed", 0),
1327 command("cached-command", "running"),
1328 ]);
1329 const cachedHistory = { historyCursor: "older", hasMoreHistory: true };
1330 const projectionStatuses = (projection: OrchestrationV2ThreadProjection) => ({
1331 items: projection.turnItems.map((item) => [String(item.id), item.status]),
1332 rows: projection.visibleTurnItems.map((row) => [
1333 row.visibility,
1334 String(row.sourceItemId),
1335 row.item.status,
1336 ]),
1337 pending: pendingBackgroundTurnItems(projection).map((item) => String(item.id)),
1338 });
1339 const statuses = (state: EnvironmentThreadState) =>
1340 projectionStatuses(Option.getOrThrow(state.data));
1341 const settledStatuses = {
1342 items: [
1343 ["older-command", "completed"],
1344 ["cached-command", "completed"],
1345 ],
1346 rows: [
1347 ["inherited", "cached-command", "running"],
1348 ["local", "older-command", "completed"],
1349 ["local", "cached-command", "completed"],
1350 ],
1351 pending: [],
1352 };
1353 const serverItems = (items: Record<string, OrchestrationV2TurnItem | null>) => ({
1354 getTurnItem: ({ itemId }: { readonly itemId: TurnItemId }) =>
1355 Effect.succeed({ item: items[itemId] ?? null }),
1356 });
1357
1358 it.effect("settles an item the server finished without any new event, keeping history", () =>
1359 Effect.gen(function* () {
1360 const written = yield* Deferred.make<OrchestrationV2ThreadDetailSnapshot>();
1361 const harness = yield* makeHarness({
1362 cached,
1363 cachedHistory,
1364 saveThread: (_environmentId, thread) => Deferred.succeed(written, thread),
1365 ...serverItems({ "cached-command": command("cached-command", "completed", 0) }),
1366 });
1367 const before = yield* SubscriptionRef.get(harness.threadState);
1368
1369 yield* harness.reconcilePendingWork;
1370
1371 const after = yield* SubscriptionRef.get(harness.threadState);
1372 expect(statuses(after)).toEqual(settledStatuses);
1373 const settled = Option.getOrThrow(after.data);
1374 expect(settled.visibleTurnItems[0]).toBe(inheritedRow);
1375 expect(settled.visibleTurnItems[2]!.item).toBe(settled.turnItems[1]);
1376 expect(after.history).toEqual(before.history);
1377 yield* TestClock.adjust("500 millis");
1378 const saved = yield* Deferred.await(written);
1379 expect(projectionStatuses(saved.projection)).toEqual(settledStatuses);
1380 expect(saved).toMatchObject({ historyCursor: "older", hasMoreHistory: true });
1381 }),
1382 );
1383
1384 it.effect("saves the repair after older history was loaded, without the expansion", () =>
1385 Effect.gen(function* () {
1386 const evenOlder = command("even-older-command", "completed", 0, 0);
1387 const scope = yield* Scope.make();
1388 const harness = yield* makeHarness({
1389 cached,
1390 cachedHistory,
1391 historyHttpClient: HttpClient.make((request) =>
1392 Effect.succeed(
1393 HttpClientResponse.fromWeb(
1394 request,
1395 Response.json({
1396 snapshotSequence: CACHED_SNAPSHOT_SEQUENCE,
1397 items: [localRow(evenOlder, 0)],
1398 nextCursor: null,
1399 hasMoreHistory: false,
1400 }),
1401 ),
1402 ),
1403 ),
1404 ...serverItems({ "cached-command": command("cached-command", "completed", 0) }),
1405 }).pipe(Scope.provide(scope));
1406 expect(yield* harness.loadEarlier()).toEqual({ _tag: "loaded" });
1407 expect((yield* SubscriptionRef.get(harness.threadState)).history.expanded).toBe(true);
1408
1409 yield* harness.reconcilePendingWork;
1410 const after = yield* SubscriptionRef.get(harness.threadState);
1411 expect(after.history).toMatchObject({ expanded: true, historyCursor: null });
1412 expect(statuses(after).pending).toEqual([]);
1413 yield* TestClock.adjust("500 millis");
1414 yield* Scope.close(scope, Exit.void);
1415
1416 const saved = (yield* Ref.get(harness.savedThreads)).at(-1);
1417 expect(saved).toMatchObject({
1418 snapshotSequence: CACHED_SNAPSHOT_SEQUENCE,
1419 historyCursor: "older",
1420 hasMoreHistory: true,
1421 });
1422 expect(projectionStatuses(saved!.projection)).toEqual(settledStatuses);
1423
1424 const reloaded = yield* makeHarness({
1425 cached: saved!.projection,
1426 cachedHistory: { historyCursor: saved!.historyCursor!, hasMoreHistory: true },
1427 });
1428 expect(statuses(yield* SubscriptionRef.get(reloaded.threadState))).toEqual(settledStatuses);
1429 }),
1430 );
1431
1432 const expandedHarness = Effect.fn("expandedHarness")(function* (
1433 scope: Scope.Closeable,
1434 options?: Pick<NonNullable<Parameters<typeof makeHarness>[0]>, "getTurnItem">,
1435 ) {
1436 const harness = yield* makeHarness({
1437 cached,
1438 cachedHistory,
1439 historyHttpClient: HttpClient.make((request) =>
1440 Effect.succeed(
1441 HttpClientResponse.fromWeb(
1442 request,
1443 Response.json({
1444 snapshotSequence: CACHED_SNAPSHOT_SEQUENCE,
1445 items: [localRow(command("even-older-command", "completed", 0, 0), 0)],
1446 nextCursor: null,
1447 hasMoreHistory: false,
1448 }),
1449 ),
1450 ),
1451 ),
1452 ...serverItems({ "cached-command": command("cached-command", "completed", 0) }),
1453 ...options,
1454 }).pipe(Scope.provide(scope));
1455 return harness;
1456 });
1457 const expand = (harness: Effect.Success<ReturnType<typeof expandedHarness>>) =>
1458 Effect.gen(function* () {
1459 expect(yield* harness.loadEarlier()).toEqual({ _tag: "loaded" });
1460 expect((yield* SubscriptionRef.get(harness.threadState)).history.expanded).toBe(true);
1461 });
1462 const lastSaved = (harness: Effect.Success<ReturnType<typeof expandedHarness>>) =>
1463 Ref.get(harness.savedThreads).pipe(Effect.map((saved) => saved.at(-1)));
1464
1465 it.effect("saves an expanded thread's repair when its scope closes before the save runs", () =>
1466 Effect.gen(function* () {
1467 const scope = yield* Scope.make();
1468 const harness = yield* expandedHarness(scope);
1469 yield* expand(harness);
1470
1471 yield* harness.reconcilePendingWork;
1472 yield* Scope.close(scope, Exit.void);
1473
1474 const saved = yield* lastSaved(harness);
1475 expect(saved).toMatchObject({ historyCursor: "older", hasMoreHistory: true });
1476 expect(projectionStatuses(saved!.projection)).toEqual(settledStatuses);
1477 }),
1478 );
1479
1480 it.effect(
1481 "saves an expanded thread's repair when its scope closes during the save cooldown",
1482 () =>
1483 Effect.gen(function* () {
1484 const scope = yield* Scope.make();
1485 const harness = yield* expandedHarness(scope);
1486 yield* Queue.offer(harness.inputs, titleUpdated("Renamed", CACHED_SNAPSHOT_SEQUENCE + 1));
1487 yield* awaitThreadState(
1488 harness.observed,
1489 (value) => Option.getOrThrow(value.data).thread.title === "Renamed",
1490 );
1491 yield* TestClock.adjust("500 millis");
1492 expect((yield* lastSaved(harness))?.projection.thread.title).toBe("Renamed");
1493 yield* expand(harness);
1494
1495 yield* harness.reconcilePendingWork;
1496 yield* Scope.close(scope, Exit.void);
1497
1498 const saved = yield* lastSaved(harness);
1499 expect(saved).toMatchObject({
1500 snapshotSequence: CACHED_SNAPSHOT_SEQUENCE + 1,
1501 historyCursor: "older",
1502 hasMoreHistory: true,
1503 });
1504 expect(saved!.projection.thread.title).toBe("Renamed");
1505 expect(projectionStatuses(saved!.projection)).toEqual(settledStatuses);
1506 }),
1507 );
1508
1509 it.effect("saves a completion event that lands while the server is asked", () =>
1510 Effect.gen(function* () {
1511 const asked = yield* Deferred.make<void>();
1512 const answer = yield* Deferred.make<OrchestrationV2TurnItem>();
1513 const scope = yield* Scope.make();
1514 const harness = yield* expandedHarness(scope, {
1515 getTurnItem: () =>
1516 Deferred.succeed(asked, undefined).pipe(
1517 Effect.andThen(Deferred.await(answer)),
1518 Effect.map((item) => ({ item })),
1519 ),
1520 });
1521 yield* expand(harness);
1522 const reconciling = yield* harness.reconcilePendingWork.pipe(Effect.forkChild);
1523 yield* Deferred.await(asked);
1524 const fromEvent = command("cached-command", "completed", 1);
1525 yield* Queue.offer(harness.inputs, {
1526 kind: "event",
1527 sequence: CACHED_SNAPSHOT_SEQUENCE + 1,
1528 event: {
1529 id: EventId.make("event-cached-command-completed"),
1530 type: "turn-item.updated",
1531 threadId: THREAD_ID,
1532 occurredAt: at,
1533 payload: fromEvent,
1534 },
1535 });
1536 yield* awaitThreadState(harness.observed, (value) =>
1537 Option.getOrThrow(value.data).turnItems.some(
1538 (item) => item.id === fromEvent.id && item.status === "completed",
1539 ),
1540 );
1541
1542 yield* Deferred.succeed(answer, command("cached-command", "completed", 0));
1543 yield* Fiber.join(reconciling);
1544 yield* Scope.close(scope, Exit.void);
1545
1546 const live = Option.getOrThrow((yield* SubscriptionRef.get(harness.threadState)).data);
1547 expect(live.turnItems.find((item) => item.id === fromEvent.id)).toEqual(fromEvent);
1548 const saved = yield* lastSaved(harness);
1549 expect(saved).toMatchObject({ historyCursor: "older", hasMoreHistory: true });
1550 expect(projectionStatuses(saved!.projection)).toEqual(settledStatuses);
1551 expect(saved!.projection.turnItems.find((item) => item.id === fromEvent.id)).toEqual(
1552 fromEvent,
1553 );
1554 }),
1555 );
1556
1557 // The event is newer than any reply, so the saved copy follows it however the lookup ends.
1558 it.effect.each(["running", "missing", "timed out"] as const)(
1559 "saves a completion event that overtakes a %s lookup reply",
1560 (reply) =>
1561 Effect.gen(function* () {
1562 const asked = yield* Deferred.make<void>();
1563 const answer = yield* Deferred.make<void>();
1564 const scope = yield* Scope.make();
1565 const harness = yield* expandedHarness(scope, {
1566 getTurnItem: () =>
1567 Deferred.succeed(asked, undefined).pipe(
1568 Effect.andThen(reply === "timed out" ? Effect.never : Deferred.await(answer)),
1569 Effect.as({
1570 item: reply === "running" ? command("cached-command", "running") : null,
1571 }),
1572 ),
1573 });
1574 yield* expand(harness);
1575 const reconciling = yield* harness.reconcilePendingWork.pipe(Effect.forkChild);
1576 yield* Deferred.await(asked);
1577 const fromEvent = command("cached-command", "completed", 1);
1578 yield* Queue.offer(harness.inputs, {
1579 kind: "event",
1580 sequence: CACHED_SNAPSHOT_SEQUENCE + 1,
1581 event: {
1582 id: EventId.make("event-cached-command-completed"),
1583 type: "turn-item.updated",
1584 threadId: THREAD_ID,
1585 occurredAt: at,
1586 payload: fromEvent,
1587 },
1588 });
1589 yield* awaitThreadState(harness.observed, (value) =>
1590 Option.getOrThrow(value.data).turnItems.some(
1591 (item) => item.id === fromEvent.id && item.status === "completed",
1592 ),
1593 );
1594
1595 if (reply === "timed out") yield* TestClock.adjust("10 seconds");
1596 else yield* Deferred.succeed(answer, undefined);
1597 yield* Fiber.join(reconciling);
1598 yield* Scope.close(scope, Exit.void);
1599
1600 const saved = yield* lastSaved(harness);
1601 expect(saved).toMatchObject({ historyCursor: "older", hasMoreHistory: true });
1602 expect(projectionStatuses(saved!.projection)).toEqual(settledStatuses);
1603 expect(saved!.projection.turnItems.find((item) => item.id === fromEvent.id)).toEqual(
1604 fromEvent,
1605 );
1606 }),
1607 );
1608
1609 it.effect("saves the repair for an expanded thread resumed in the same session", () =>
1610 Effect.gen(function* () {
1611 const resumeCache: NonNullable<Parameters<typeof makeEnvironmentThreadState>[1]> = {
1612 snapshot: {
1613 state: {
1614 data: Option.some(cached),
1615 status: "cached",
1616 error: Option.none(),
1617 history: { ...EMPTY_THREAD_HISTORY_META, historyCursor: null, expanded: true },
1618 sequence: CACHED_SNAPSHOT_SEQUENCE,
1619 },
1620 sequence: CACHED_SNAPSHOT_SEQUENCE,
1621 persisted: true,
1622 },
1623 owner: undefined,
1624 };
1625 const scope = yield* Scope.make();
1626 const harness = yield* makeHarness({
1627 cached,
1628 cachedHistory,
1629 resumeCache,
1630 ...serverItems({ "cached-command": command("cached-command", "completed", 0) }),
1631 }).pipe(Scope.provide(scope));
1632
1633 yield* harness.reconcilePendingWork;
1634 yield* TestClock.adjust("500 millis");
1635 yield* Scope.close(scope, Exit.void);
1636
1637 const saved = (yield* Ref.get(harness.savedThreads)).at(-1);
1638 expect(saved).toMatchObject({ historyCursor: "older", hasMoreHistory: true });
1639 expect(projectionStatuses(saved!.projection)).toEqual(settledStatuses);
1640 }),
1641 );
1642
1643 it.effect("gives up on a lookup the server never answers, settling the rest", () =>
1644 Effect.gen(function* () {
1645 const askedCached = yield* Deferred.make<void>();
1646 const askedSilent = yield* Deferred.make<void>();
1647 const harness = yield* makeHarness({
1648 cached: cachedProjection([...cached.turnItems, command("silent-command", "running")]),
1649 cachedHistory,
1650 getTurnItem: ({ itemId }) =>
1651 itemId === "silent-command"
1652 ? Deferred.succeed(askedSilent, undefined).pipe(Effect.andThen(Effect.never))
1653 : Deferred.succeed(askedCached, undefined).pipe(
1654 Effect.as({ item: command("cached-command", "completed", 0) }),
1655 ),
1656 });
1657 const reconciling = yield* harness.reconcilePendingWork.pipe(Effect.forkChild);
1658 yield* Deferred.await(askedCached);
1659 yield* Deferred.await(askedSilent);
1660
1661 yield* TestClock.adjust("10 seconds");
1662
1663 expect(reconciling.pollUnsafe()).toEqual(Exit.void);
1664 expect(statuses(yield* SubscriptionRef.get(harness.threadState)).items).toEqual([
1665 ["older-command", "completed"],
1666 ["cached-command", "completed"],
1667 ["silent-command", "running"],
1668 ]);
1669 }),
1670 );
1671
1672 it.effect("keeps items the server still runs or does not return", () =>
1673 Effect.gen(function* () {
1674 const harness = yield* makeHarness({
1675 cached: cachedProjection([...cached.turnItems, command("missing-command", "running")]),
1676 cachedHistory,
1677 ...serverItems({ "cached-command": command("cached-command", "running") }),
1678 });
1679
1680 yield* harness.reconcilePendingWork;
1681
1682 expect(statuses(yield* SubscriptionRef.get(harness.threadState)).items).toEqual([
1683 ["older-command", "completed"],
1684 ["cached-command", "running"],
1685 ["missing-command", "running"],
1686 ]);
1687 }),
1688 );
1689
1690 it.effect("keeps events that land while the server is asked", () =>
1691 Effect.gen(function* () {
1692 const asked = yield* Deferred.make<void>();
1693 const answer = yield* Deferred.make<OrchestrationV2TurnItem>();
1694 const harness = yield* makeHarness({
1695 cached,
1696 cachedHistory,
1697 getTurnItem: () =>
1698 Deferred.succeed(asked, undefined).pipe(
1699 Effect.andThen(Deferred.await(answer)),
1700 Effect.map((item) => ({ item })),
1701 ),
1702 });
1703 const reconciling = yield* harness.reconcilePendingWork.pipe(Effect.forkChild);
1704 yield* Deferred.await(asked);
1705 yield* Queue.offer(harness.inputs, titleUpdated("Renamed", CACHED_SNAPSHOT_SEQUENCE + 1));
1706 yield* awaitThreadState(
1707 harness.observed,
1708 (value) => Option.getOrThrow(value.data).thread.title === "Renamed",
1709 );
1710
1711 yield* Deferred.succeed(answer, command("cached-command", "completed", 0));
1712 yield* Fiber.join(reconciling);
1713
1714 const after = yield* SubscriptionRef.get(harness.threadState);
1715 expect(Option.getOrThrow(after.data).thread.title).toBe("Renamed");
1716 expect(statuses(after)).toEqual(settledStatuses);
1717 }),
1718 );
1719 });
1720
1721 it.effect("keeps progressive history metadata from a bounded socket fallback", () =>
1722 Effect.gen(function* () {
1723 const olderItem = {
1724 id: "item:older",
1725 threadId: THREAD_ID,
1726 runId: null,
1727 nodeId: null,
1728 providerThreadId: null,
1729 providerTurnId: null,
1730 nativeItemRef: null,
1731 parentItemId: null,
1732 ordinal: 1,
1733 status: "completed",
1734 title: null,
1735 startedAt: "2026-06-20T00:00:00.000Z",
1736 completedAt: "2026-06-20T00:00:00.000Z",
1737 updatedAt: "2026-06-20T00:00:00.000Z",
1738 type: "command_execution",
1739 input: "pwd",
1740 output: "older history",
1741 exitCode: 0,
1742 };
1743 const harness = yield* makeHarness({
1744 historyHttpClient: HttpClient.make((request, url) => {
1745 expect(url.searchParams.get("cursor")).toBe("socket-history-cursor");
1746 expect(url.searchParams.get("throughEntryId")).toBe(olderItem.id);
1747 return Effect.succeed(
1748 HttpClientResponse.fromWeb(
1749 request,
1750 Response.json({
1751 snapshotSequence: 14,
1752 items: [
1753 {
1754 position: 0,
1755 visibility: "local",
1756 sourceThreadId: THREAD_ID,
1757 sourceItemId: olderItem.id,
1758 item: olderItem,
1759 },
1760 ],
1761 nextCursor: null,
1762 hasMoreHistory: false,
1763 }),
1764 ),
1765 );
1766 }),
1767 });
1768 yield* Queue.offer(harness.inputs, {
1769 kind: "snapshot",
1770 snapshotSequence: 14,
1771 projection: BASE_PROJECTION,
1772 historyCursor: "socket-history-cursor",
1773 hasMoreHistory: true,
1774 latestLocalTurnOrdinal: 47,
1775 });
1776
1777 const state = yield* awaitThreadState(
1778 harness.observed,
1779 (value) =>
1780 value.status === "live" && value.history.historyCursor === "socket-history-cursor",
1781 );
1782 expect(state.history).toMatchObject({
1783 historyCursor: "socket-history-cursor",
1784 hasMoreHistory: true,
1785 loading: false,
1786 error: null,
1787 expanded: false,
1788 latestLocalTurnOrdinal: 47,
1789 });
1790 expect(yield* Ref.get(harness.lastSubscribeAfterSequence)).toBeUndefined();
1791 expect(yield* Ref.get(harness.lastAcceptBoundedSnapshot)).toBe(true);
1792
1793 yield* TestClock.adjust("500 millis");
1794 yield* Effect.yieldNow;
1795 expect((yield* Ref.get(harness.savedThreads)).at(-1)).toMatchObject({
1796 snapshotSequence: 14,
1797 historyCursor: "socket-history-cursor",
1798 hasMoreHistory: true,
1799 latestLocalTurnOrdinal: 47,
1800 });
1801 expect(yield* harness.loadEarlier(olderItem.id)).toEqual({ _tag: "loaded" });
1802 const expanded = yield* SubscriptionRef.get(harness.threadState);
1803 expect(
1804 Option.getOrThrow(expanded.data).turnItems.some((item) => item.id === olderItem.id),
1805 ).toBe(true);
1806 expect(expanded.history.hasMoreHistory).toBe(false);
1807 }),
1808 );
1809
1810 it.effect.each(["complete", "replacement snapshot", "retry"] as const)(
1811 "progressively reveals conversation history before activity, then handles %s",
1812 (mode) =>
1813 Effect.gen(function* () {
1814 const common = {
1815 threadId: THREAD_ID,
1816 runId: null,
1817 nodeId: null,
1818 providerThreadId: null,
1819 providerTurnId: null,
1820 nativeItemRef: null,
1821 parentItemId: null,
1822 status: "completed",
1823 title: null,
1824 startedAt: "2026-06-20T00:00:00.000Z",
1825 completedAt: "2026-06-20T00:00:00.000Z",
1826 updatedAt: "2026-06-20T00:00:00.000Z",
1827 };
1828 const message = {
1829 ...common,
1830 id: "item:older-message",
1831 ordinal: 1,
1832 type: "assistant_message",
1833 messageId: "message:older",
1834 text: "COD4 result",
1835 streaming: false,
1836 };
1837 const prompt = {
1838 ...message,
1839 id: "item:older-prompt",
1840 messageId: "message:older-prompt",
1841 ordinal: 0,
1842 type: "user_message",
1843 inputIntent: "turn_start",
1844 createdBy: "user",
1845 creationSource: "web",
1846 attachments: [],
1847 };
1848 const activity = {
1849 ...common,
1850 id: "item:older-command",
1851 ordinal: 2,
1852 type: "command_execution",
1853 input: "pwd",
1854 output: "",
1855 exitCode: 0,
1856 };
1857 const projected = (item: typeof message | typeof activity, position: number) => ({
1858 position,
1859 visibility: "local",
1860 sourceThreadId: THREAD_ID,
1861 sourceItemId: item.id,
1862 item,
1863 });
1864 const activityStarted = yield* Deferred.make<void>();
1865 const releaseActivity = yield* Deferred.make<void>();
1866 let activityRequests = 0;
1867 const harness = yield* makeHarness({
1868 historyHttpClient: HttpClient.make((request, url) =>
1869 Effect.gen(function* () {
1870 if (url.searchParams.get("cursor") === "next-cursor")
1871 return HttpClientResponse.fromWeb(
1872 request,
1873 Response.json({
1874 snapshotSequence: 14,
1875 items: [projected(prompt, 0)],
1876 nextCursor: "before-prompt",
1877 hasMoreHistory: true,
1878 }),
1879 );
1880 expect(url.searchParams.get("cursor")).toBe("progressive-cursor");
1881 const conversationOnly = url.searchParams.get("view") === "conversation";
1882 if (!conversationOnly && activityRequests++ === 0) {
1883 yield* Deferred.succeed(activityStarted, undefined);
1884 yield* Deferred.await(releaseActivity);
1885 if (mode === "retry")
1886 return HttpClientResponse.fromWeb(
1887 request,
1888 new Response("Activity unavailable", { status: 503 }),
1889 );
1890 }
1891 return HttpClientResponse.fromWeb(
1892 request,
1893 Response.json({
1894 snapshotSequence: 14,
1895 items: conversationOnly
1896 ? [projected(message, 0)]
1897 : [projected(message, 0), projected(activity, 1)],
1898 nextCursor: "next-cursor",
1899 hasMoreHistory: true,
1900 }),
1901 );
1902 }),
1903 ),
1904 });
1905 yield* Queue.offer(harness.inputs, {
1906 ...snapshot(BASE_PROJECTION, 14),
1907 historyCursor: "progressive-cursor",
1908 hasMoreHistory: true,
1909 latestLocalTurnOrdinal: 47,
1910 });
1911 yield* awaitThreadState(
1912 harness.observed,
1913 (state) => state.history.historyCursor === "progressive-cursor",
1914 );
1915 const loading = yield* harness.loadEarlier(message.messageId).pipe(Effect.forkScoped);
1916 yield* Deferred.await(activityStarted);
1917 const preview = yield* SubscriptionRef.get(harness.threadState);
1918 expect(preview.history).toMatchObject({
1919 loading: true,
1920 historyCursor: "progressive-cursor",
1921 expanded: true,
1922 });
1923 expect(
1924 Option.getOrThrow(preview.data).visibleTurnItems.map((row) => row.sourceItemId),
1925 ).toEqual([message.id]);
1926 if (mode === "replacement snapshot") {
1927 yield* Queue.offer(harness.inputs, snapshot(BASE_PROJECTION, 15));
1928 yield* awaitThreadState(
1929 harness.observed,
1930 (state) => state.history.historyCursor === null,
1931 );
1932 }
1933 yield* Deferred.succeed(releaseActivity, undefined);
1934 const result = yield* Fiber.join(loading);
1935 if (mode === "replacement snapshot") {
1936 expect(result).toEqual({ _tag: "noop" });
1937 const state = yield* SubscriptionRef.get(harness.threadState);
1938 expect(Option.getOrThrow(state.data).visibleTurnItems).toEqual(
1939 BASE_PROJECTION.visibleTurnItems,
1940 );
1941 expect(state.history.historyCursor).toBeNull();
1942 return;
1943 }
1944 if (mode === "retry") {
1945 expect(result._tag).toBe("error");
1946 expect(yield* harness.loadEarlier()).toEqual({ _tag: "loaded" });
1947 } else expect(result).toEqual({ _tag: "loaded" });
1948 const complete = yield* SubscriptionRef.get(harness.threadState);
1949 expect(complete.history).toMatchObject({
1950 loading: false,
1951 historyCursor: mode === "retry" ? "before-prompt" : "next-cursor",
1952 error: null,
1953 latestLocalTurnOrdinal: 47,
1954 });
1955 expect(
1956 Option.getOrThrow(complete.data).visibleTurnItems.map((row) => row.sourceItemId),
1957 ).toEqual(
1958 mode === "retry" ? [prompt.id, message.id, activity.id] : [message.id, activity.id],
1959 );
1960 expect(Option.getOrThrow(complete.data).turnItems).toHaveLength(mode === "retry" ? 3 : 2);
1961 }),
1962 );
1963
1964 it.effect("restores compact socket snapshot turnItems before reducing and caching", () =>
1965 Effect.gen(function* () {
1966 const at = DateTime.makeUnsafe("2026-06-20T00:00:00.000Z");
1967 const command = (id: string, ordinal: number, threadId = THREAD_ID) =>
1968 ({
1969 id: TurnItemId.make(id),
1970 threadId,
1971 runId: null,
1972 nodeId: null,
1973 providerThreadId: null,
1974 providerTurnId: null,
1975 nativeItemRef: null,
1976 parentItemId: null,
1977 ordinal,
1978 status: "completed" as const,
1979 title: null,
1980 startedAt: at,
1981 completedAt: at,
1982 updatedAt: at,
1983 type: "command_execution" as const,
1984 input: id,
1985 output: id,
1986 exitCode: 0,
1987 }) satisfies OrchestrationV2TurnItem;
1988 const parentThread = ThreadId.make("thread:compact-parent");
1989 const inherited = command("parent-item", 1, parentThread);
1990 const local = [command("local-1", 1), command("local-2", 2)];
1991 const retainedRequest = {
1992 ...command("retained-request", 0),
1993 type: "run_interrupt_request" as const,
1994 message: "Stop",
1995 } as unknown as OrchestrationV2TurnItem;
1996 const visibleTurnItems = [
1997 {
1998 position: 0,
1999 visibility: "inherited" as const,
2000 sourceThreadId: parentThread,
2001 sourceItemId: inherited.id,
2002 item: inherited,
2003 },
2004 ...local.map((item, index) => ({
2005 position: index + 1,
2006 visibility: "local" as const,
2007 sourceThreadId: THREAD_ID,
2008 sourceItemId: item.id,
2009 item,
2010 })),
2011 ];
2012 const harness = yield* makeHarness();
2013 yield* Queue.offer(harness.inputs, {
2014 kind: "snapshot",
2015 snapshotSequence: 5,
2016 projection: { ...BASE_PROJECTION, turnItems: [retainedRequest], visibleTurnItems },
2017 historyCursor: "compact-cursor",
2018 hasMoreHistory: true,
2019 latestLocalTurnOrdinal: 2,
2020 turnItemsOmitLocalVisible: true,
2021 });
2022
2023 const seeded = yield* awaitThreadState(
2024 harness.observed,
2025 (value) => value.history.historyCursor === "compact-cursor",
2026 );
2027 expect(Option.getOrThrow(seeded.data).turnItems).toEqual([...local, retainedRequest]);
2028 expect(yield* Ref.get(harness.lastAcceptBoundedSnapshot)).toBe(true);
2029 expect(yield* Ref.get(harness.lastAcceptCompactTurnItems)).toBe(true);
2030
2031 const updated = { ...local[1]!, output: "updated", ordinal: 2 };
2032 yield* Queue.offer(harness.inputs, {
2033 kind: "event",
2034 sequence: 6,
2035 event: {
2036 id: EventId.make("event-compact-update"),
2037 type: "turn-item.updated",
2038 threadId: THREAD_ID,
2039 occurredAt: at,
2040 payload: updated,
2041 },
2042 });
2043 const live = yield* awaitThreadState(harness.observed, (value) =>
2044 Option.exists(value.data, (data) =>
2045 data.turnItems.some(
2046 (item) => item.type === "command_execution" && item.output === "updated",
2047 ),
2048 ),
2049 );
2050 expect(Option.getOrThrow(live.data).turnItems.map((item) => String(item.id))).toEqual([
2051 "local-1",
2052 "local-2",
2053 "retained-request",
2054 ]);
2055
2056 yield* TestClock.adjust("500 millis");
2057 yield* Effect.yieldNow;
2058 const saved = (yield* Ref.get(harness.savedThreads)).at(-1);
2059 expect(saved?.projection.turnItems.map((item) => String(item.id))).toEqual([
2060 "local-1",
2061 "local-2",
2062 "retained-request",
2063 ]);
2064 expect(saved && "turnItemsOmitLocalVisible" in saved).toBe(false);
2065 }),
2066 );
2067
2068 it.effect("installs bounded snapshot history meta and resumes via afterSequence", () =>
2069 Effect.gen(function* () {
2070 const httpProjection: OrchestrationV2ThreadProjection = {
2071 ...BASE_PROJECTION,
2072 thread: { ...BASE_PROJECTION.thread, title: "Bounded title" },
2073 };
2074 const harness = yield* makeHarness({
2075 httpSnapshot: {
2076 _tag: "present",
2077 snapshot: { snapshotSequence: 11, projection: httpProjection },
2078 history: {
2079 historyCursor: "opaque-cursor",
2080 hasMoreHistory: true,
2081 },
2082 },
2083 });
2084 yield* Queue.offer(harness.inputs, titleUpdated("Live after bounded", 12));
2085
2086 const state = yield* awaitThreadState(
2087 harness.observed,
2088 (value) =>
2089 value.status === "live" &&
2090 Option.isSome(value.data) &&
2091 value.data.value.thread.title === "Live after bounded" &&
2092 value.history.hasMoreHistory,
2093 );
2094
2095 expect(state.history).toMatchObject({
2096 historyCursor: "opaque-cursor",
2097 hasMoreHistory: true,
2098 loading: false,
2099 error: null,
2100 expanded: false,
2101 });
2102 expect(yield* Ref.get(harness.lastSubscribeAfterSequence)).toBe(11);
2103 expect(yield* Ref.get(harness.lastAcceptBoundedSnapshot)).toBe(true);
2104 expect(Option.getOrThrow(state.data).thread.title).toBe("Live after bounded");
2105 }),
2106 );
2107
2108 it.effect("legacy warm cache without history meta stays complete (no false load-earlier)", () =>
2109 Effect.gen(function* () {
2110 const harness = yield* makeHarness({ cached: BASE_PROJECTION });
2111 const state = yield* awaitThreadState(harness.observed, (value) => Option.isSome(value.data));
2112 expect(state.history).toMatchObject({
2113 historyCursor: null,
2114 hasMoreHistory: false,
2115 expanded: false,
2116 });
2117 expect(yield* Ref.get(harness.loaderCalls)).toBe(0);
2118 expect(yield* Ref.get(harness.lastAcceptBoundedSnapshot)).toBeUndefined();
2119 }),
2120 );
2121
2122 it.effect.each(["disk", "retained"] as const)(
2123 "retains paging support through a complete bounded %s cache",
2124 (cacheKind) =>
2125 Effect.gen(function* () {
2126 const resumeCache: NonNullable<Parameters<typeof makeEnvironmentThreadState>[1]> = {
2127 snapshot: undefined,
2128 owner: undefined,
2129 };
2130 const cold = yield* Effect.scoped(
2131 Effect.gen(function* () {
2132 const harness = yield* makeHarness({
2133 resumeCache,
2134 httpSnapshot: {
2135 _tag: "present",
2136 snapshot: { snapshotSequence: 4, projection: BASE_PROJECTION },
2137 history: {
2138 historyCursor: null,
2139 hasMoreHistory: false,
2140 latestLocalTurnOrdinal: null,
2141 },
2142 },
2143 });
2144 yield* Queue.offer(harness.inputs, titleUpdated("Complete bounded", 5));
2145 yield* awaitThreadState(
2146 harness.observed,
2147 (value) =>
2148 value.status === "live" &&
2149 Option.isSome(value.data) &&
2150 value.data.value.thread.title === "Complete bounded",
2151 );
2152 return harness;
2153 }),
2154 );
2155 const saved = (yield* Ref.get(cold.savedThreads)).at(-1);
2156 expect(saved).toMatchObject({
2157 snapshotSequence: 5,
2158 historyCursor: null,
2159 hasMoreHistory: false,
2160 latestLocalTurnOrdinal: null,
2161 });
2162
2163 const warm = yield* makeHarness(
2164 cacheKind === "retained" ? { resumeCache } : { loadCached: Effect.succeedSome(saved!) },
2165 );
2166 yield* Queue.offer(warm.inputs, titleUpdated("Grown while closed", 200));
2167 yield* awaitThreadState(
2168 warm.observed,
2169 (value) =>
2170 value.status === "live" &&
2171 Option.isSome(value.data) &&
2172 value.data.value.thread.title === "Grown while closed",
2173 );
2174 expect(yield* Ref.get(warm.loaderCalls)).toBe(0);
2175 expect(yield* Ref.get(warm.lastSubscribeAfterSequence)).toBe(5);
2176 expect(yield* Ref.get(warm.lastAcceptBoundedSnapshot)).toBe(true);
2177 }),
2178 );
2179
2180 it.effect("socket snapshot clears progressive history meta left from a bounded window", () =>
2181 Effect.gen(function* () {
2182 const harness = yield* makeHarness({
2183 cached: {
2184 ...BASE_PROJECTION,
2185 thread: { ...BASE_PROJECTION.thread, title: "Warm progressive" },
2186 },
2187 cachedHistory: {
2188 historyCursor: "stale-cursor",
2189 hasMoreHistory: true,
2190 },
2191 });
2192
2193 yield* Queue.offer(
2194 harness.inputs,
2195 snapshot(
2196 {
2197 ...BASE_PROJECTION,
2198 thread: { ...BASE_PROJECTION.thread, title: "Full socket snapshot" },
2199 },
2200 CACHED_SNAPSHOT_SEQUENCE + 1,
2201 ),
2202 );
2203
2204 const state = yield* awaitThreadState(
2205 harness.observed,
2206 (value) =>
2207 value.status === "live" &&
2208 Option.isSome(value.data) &&
2209 value.data.value.thread.title === "Full socket snapshot" &&
2210 value.history.historyCursor === null &&
2211 value.history.hasMoreHistory === false,
2212 );
2213
2214 expect(state.history).toMatchObject({
2215 historyCursor: null,
2216 hasMoreHistory: false,
2217 expanded: false,
2218 loading: false,
2219 error: null,
2220 });
2221
2222 yield* TestClock.adjust("500 millis");
2223 yield* Effect.yieldNow;
2224 const saved = (yield* Ref.get(harness.savedThreads)).at(-1);
2225 expect(saved?.projection.thread.title).toBe("Full socket snapshot");
2226 expect(saved?.historyCursor).toBeNull();
2227 expect(saved?.hasMoreHistory).toBe(false);
2228 }),
2229 );
2230
2231 it.effect("installs and advances latestLocalTurnOrdinal for partial progressive windows", () =>
2232 Effect.gen(function* () {
2233 const harness = yield* makeHarness({
2234 httpSnapshot: {
2235 _tag: "present",
2236 snapshot: {
2237 snapshotSequence: 3,
2238 projection: {
2239 ...BASE_PROJECTION,
2240 thread: { ...BASE_PROJECTION.thread, title: "Watermark seed" },
2241 },
2242 latestLocalTurnOrdinal: 15,
2243 },
2244 history: {
2245 historyCursor: "wm-cursor",
2246 hasMoreHistory: true,
2247 latestLocalTurnOrdinal: 15,
2248 },
2249 },
2250 });
2251
2252 const seeded = yield* awaitThreadState(
2253 harness.observed,
2254 (value) =>
2255 Option.isSome(value.data) &&
2256 value.data.value.thread.title === "Watermark seed" &&
2257 value.history.latestLocalTurnOrdinal === 15,
2258 );
2259 expect(seeded.history.latestLocalTurnOrdinal).toBe(15);
2260
2261 yield* TestClock.adjust("500 millis");
2262 yield* Effect.yieldNow;
2263 const savedSeed = (yield* Ref.get(harness.savedThreads)).at(-1);
2264 expect(savedSeed?.latestLocalTurnOrdinal).toBe(15);
2265
2266 const newer = {
2267 id: TurnItemId.make("item-newer-live"),
2268 threadId: THREAD_ID,
2269 runId: null,
2270 nodeId: null,
2271 providerThreadId: null,
2272 providerTurnId: null,
2273 nativeItemRef: null,
2274 parentItemId: null,
2275 ordinal: 22,
2276 status: "completed" as const,
2277 title: null,
2278 startedAt: DateTime.makeUnsafe("2026-06-20T00:00:00.000Z"),
2279 completedAt: DateTime.makeUnsafe("2026-06-20T00:00:00.000Z"),
2280 updatedAt: DateTime.makeUnsafe("2026-06-20T00:00:00.000Z"),
2281 type: "command_execution" as const,
2282 input: "echo newer",
2283 output: "newer",
2284 exitCode: 0,
2285 } satisfies OrchestrationV2TurnItem;
2286
2287 yield* Queue.offer(harness.inputs, {
2288 kind: "event",
2289 sequence: 4,
2290 event: {
2291 id: EventId.make("event-newer-item"),
2292 type: "turn-item.updated",
2293 threadId: THREAD_ID,
2294 occurredAt: DateTime.makeUnsafe("2026-06-20T01:00:00.000Z"),
2295 payload: newer,
2296 },
2297 });
2298
2299 const advanced = yield* awaitThreadState(
2300 harness.observed,
2301 (value) =>
2302 value.status === "live" &&
2303 Option.isSome(value.data) &&
2304 value.history.latestLocalTurnOrdinal === 22 &&
2305 value.data.value.turnItems.some((item) => String(item.id) === String(newer.id)),
2306 );
2307 expect(advanced.history.latestLocalTurnOrdinal).toBe(22);
2308 expect(advanced.history.historyCursor).toBe("wm-cursor");
2309 }),
2310 );
2311
2312 it.effect("falls back to the socket when the HTTP snapshot is only unavailable", () =>
2313 Effect.gen(function* () {
2314 const harness = yield* makeHarness({
2315 httpSnapshot: { _tag: "unavailable" },
2316 });
2317
2318 yield* Queue.offer(
2319 harness.inputs,
2320 snapshot({
2321 ...BASE_PROJECTION,
2322 thread: { ...BASE_PROJECTION.thread, title: "Socket title" },
2323 }),
2324 );
2325
2326 const state = yield* awaitThreadState(
2327 harness.observed,
2328 (value) =>
2329 value.status === "live" &&
2330 Option.isSome(value.data) &&
2331 value.data.value.thread.title === "Socket title",
2332 );
2333
2334 expect(Option.getOrThrow(state.data).thread.title).toBe("Socket title");
2335 expect(yield* Ref.get(harness.loaderCalls)).toBeGreaterThanOrEqual(1);
2336 expect(yield* SubscriptionRef.get(harness.subscriptionCount)).toBe(1);
2337 expect(yield* Ref.get(harness.removedThreads)).toEqual([]);
2338 }),
2339 );
2340
2341 it.effect("does not recreate a deleted cache from a queued save or finalizer", () =>
2342 Effect.gen(function* () {
2343 const scope = yield* Scope.make();
2344 const harness = yield* makeHarness().pipe(Effect.provideService(Scope.Scope, scope));
2345 yield* Queue.offer(harness.inputs, snapshot(BASE_PROJECTION));
2346 yield* awaitThreadState(harness.observed, (value) => value.status === "live");
2347 yield* TestClock.adjust("500 millis");
2348 expect(yield* Ref.get(harness.savedThreads)).toHaveLength(1);
2349
2350 yield* Queue.offer(harness.inputs, titleUpdated("Queued before deletion"));
2351 yield* awaitThreadState(
2352 harness.observed,
2353 (value) =>
2354 Option.isSome(value.data) && value.data.value.thread.title === "Queued before deletion",
2355 );
2356 yield* Queue.offer(harness.inputs, deleted());
2357 const state = yield* awaitThreadState(
2358 harness.observed,
2359 (value) => value.status === "deleted",
2360 );
2361 expect(Option.isNone(state.data)).toBe(true);
2362 expect(yield* Ref.get(harness.removedThreads)).toEqual([THREAD_ID]);
2363
2364 yield* TestClock.adjust("10 seconds");
2365 expect(yield* Ref.get(harness.savedThreads)).toHaveLength(1);
2366 yield* Scope.close(scope, Exit.void);
2367 expect(yield* Ref.get(harness.savedThreads)).toHaveLength(1);
2368 }),
2369 );
2370
2371 it.effect("caches a running thread once when its view closes, not on each update", () =>
2372 Effect.gen(function* () {
2373 const scope = yield* Scope.make();
2374 const harness = yield* makeHarness().pipe(Effect.provideService(Scope.Scope, scope));
2375 const running: OrchestrationV2ThreadProjection = {
2376 ...BASE_PROJECTION,
2377 runs: [
2378 {
2379 id: RunId.make("run-running"),
2380 threadId: THREAD_ID,
2381 ordinal: 1,
2382 providerInstanceId: BASE_PROJECTION.thread.providerInstanceId,
2383 modelSelection: BASE_PROJECTION.thread.modelSelection,
2384 providerThreadId: null,
2385 userMessageId: MessageId.make("message-running"),
2386 rootNodeId: null,
2387 activeAttemptId: null,
2388 status: "running",
2389 requestedAt: v2Now,
2390 startedAt: v2Now,
2391 completedAt: null,
2392 checkpointId: null,
2393 contextHandoffId: null,
2394 },
2395 ],
2396 };
2397 yield* Queue.offer(harness.inputs, snapshot(running, 4));
2398 yield* Queue.offer(harness.inputs, titleUpdated("Streaming update", 5));
2399 yield* awaitThreadState(
2400 harness.observed,
2401 (value) =>
2402 Option.isSome(value.data) && value.data.value.thread.title === "Streaming update",
2403 );
2404 yield* TestClock.adjust("10 seconds");
2405 expect(yield* Ref.get(harness.savedThreads)).toHaveLength(0);
2406
2407 yield* Scope.close(scope, Exit.void);
2408 const saved = yield* Ref.get(harness.savedThreads);
2409 expect(saved).toHaveLength(1);
2410 expect(saved[0]?.snapshotSequence).toBe(5);
2411 expect(saved[0]?.projection.thread.title).toBe("Streaming update");
2412 expect(saved[0]?.projection.runs[0]?.status).toBe("running");
2413 }),
2414 );
2415
2416 it.effect("preserves data after a domain failure and resumes on a replacement session", () =>
2417 Effect.gen(function* () {
2418 const harness = yield* makeHarness({ cached: BASE_PROJECTION });
2419 yield* Queue.offer(harness.inputs, snapshot(BASE_PROJECTION));
2420 yield* Queue.offer(harness.inputs, new Error("stream failed"));
2421
2422 const state = yield* awaitThreadState(harness.observed, (value) =>
2423 Option.isSome(value.error),
2424 );
2425
2426 expect(Option.getOrThrow(state.data)).toEqual(BASE_PROJECTION);
2427 expect(Option.getOrThrow(state.error)).toBe("stream failed");
2428 expect(yield* Ref.get(harness.retryCount)).toBe(0);
2429
2430 yield* harness.replaceSession;
2431 yield* SubscriptionRef.changes(harness.subscriptionCount).pipe(
2432 Stream.filter((count) => count >= 2),
2433 Stream.runHead,
2434 );
2435 yield* Queue.offer(
2436 harness.inputs,
2437 snapshot({
2438 ...BASE_PROJECTION,
2439 thread: { ...BASE_PROJECTION.thread, title: "Recovered thread" },
2440 }),
2441 );
2442 const recovered = yield* awaitThreadState(
2443 harness.observed,
2444 (value) =>
2445 value.status === "live" &&
2446 Option.isSome(value.data) &&
2447 value.data.value.thread.title === "Recovered thread",
2448 );
2449
2450 expect(Option.isNone(recovered.error)).toBe(true);
2451 expect(yield* SubscriptionRef.get(harness.subscriptionCount)).toBe(2);
2452 }),
2453 );
2454
2455 it.effect(
2456 "resubscribes after a snapshot decode defect ends the thread subscription (#23041)",
2457 () =>
2458 Effect.gen(function* () {
2459 const harness = yield* makeHarness({ httpSnapshot: { _tag: "unavailable" } });
2460 yield* SubscriptionRef.changes(harness.subscriptionCount).pipe(
2461 Stream.filter((count) => count >= 1),
2462 Stream.runHead,
2463 );
2464 yield* Queue.offer(harness.inputs, TEST_DEFECT);
2465 const failed = yield* awaitThreadState(harness.observed, (value) =>
2466 Option.isSome(value.error),
2467 );
2468 expect(failed.status).toBe("empty");
2469 expect(Option.getOrThrow(failed.error)).toBe("Could not synchronize the thread.");
2470
2471 yield* TestClock.adjust("60 seconds");
2472 expect(yield* SubscriptionRef.get(harness.subscriptionCount)).toBe(2);
2473
2474 yield* Queue.offer(
2475 harness.inputs,
2476 snapshot({
2477 ...BASE_PROJECTION,
2478 thread: { ...BASE_PROJECTION.thread, title: "Recovered thread" },
2479 }),
2480 );
2481 const recovered = yield* awaitThreadState(
2482 harness.observed,
2483 (value) =>
2484 value.status === "live" &&
2485 Option.isSome(value.data) &&
2486 value.data.value.thread.title === "Recovered thread",
2487 );
2488 expect(Option.isNone(recovered.error)).toBe(true);
2489 }),
2490 );
2491
2492 it.effect("keeps replayed updates synchronizing until the completion marker arrives", () =>
2493 Effect.gen(function* () {
2494 const harness = yield* makeHarness({ cached: BASE_PROJECTION, completionMarker: true });
2495 yield* awaitThreadState(
2496 harness.observed,
2497 (value) => value.status === "synchronizing" && Option.isSome(value.data),
2498 );
2499 expect(yield* Ref.get(harness.lastRequestCompletionMarker)).toBe(true);
2500
2501 yield* Queue.offer(
2502 harness.inputs,
2503 titleUpdated("Caught-up title", CACHED_SNAPSHOT_SEQUENCE + 1),
2504 );
2505 const catchingUp = yield* awaitThreadState(
2506 harness.observed,
2507 (value) =>
2508 value.status === "synchronizing" &&
2509 Option.isSome(value.data) &&
2510 value.data.value.thread.title === "Caught-up title",
2511 );
2512 expect(catchingUp.status).toBe("synchronizing");
2513
2514 yield* Queue.offer(harness.inputs, synchronized());
2515 const live = yield* awaitThreadState(
2516 harness.observed,
2517 (value) => value.status === "live" && Option.isSome(value.data),
2518 );
2519 expect(Option.getOrThrow(live.data).thread.title).toBe("Caught-up title");
2520 }),
2521 );
2522
2523 it.effect("announces request resolution only from a synchronized projection", () =>
2524 Effect.gen(function* () {
2525 const announced = yield* Queue.unbounded<ThreadRequestsResolved>();
2526 yield* Effect.acquireRelease(
2527 Effect.sync(() => onThreadRequestsResolved((event) => Queue.offerUnsafe(announced, event))),
2528 (unsubscribe) => Effect.sync(unsubscribe),
2529 );
2530 const harness = yield* makeHarness({
2531 cached: PROJECTION_WITH_QUESTION,
2532 completionMarker: true,
2533 });
2534 yield* awaitThreadState(
2535 harness.observed,
2536 (value) => value.status === "synchronizing" && Option.isSome(value.data),
2537 );
2538
2539 // Answered on another device while this client was away.
2540 yield* Queue.offerAll(harness.inputs, [
2541 questionResolved(CACHED_SNAPSHOT_SEQUENCE + 1),
2542 synchronized(),
2543 ]);
2544
2545 expect(yield* Queue.take(announced)).toEqual({
2546 environmentId: TARGET.environmentId,
2547 threadId: THREAD_ID,
2548 pendingRequestIds: [],
2549 unansweredRequestIds: [],
2550 });
2551 expect(yield* Queue.size(announced)).toBe(0);
2552 }),
2553 );
2554
2555 it.effect("skips an unknown event type and resumes after it", () =>
2556 Effect.gen(function* () {
2557 const harness = yield* makeHarness({ cached: BASE_PROJECTION, completionMarker: true });
2558 const unknown = (sequence: number): OrchestrationV2ThreadStreamItem => ({
2559 kind: "unknown-event",
2560 sequence,
2561 eventType: "run.from-a-future-server",
2562 });
2563 const occurredAt = DateTime.makeUnsafe("2026-06-20T01:00:00.000Z");
2564 yield* Queue.offerAll(harness.inputs, [
2565 {
2566 kind: "event",
2567 sequence: CACHED_SNAPSHOT_SEQUENCE + 1,
2568 event: {
2569 id: EventId.make("event-message"),
2570 type: "message.updated",
2571 threadId: THREAD_ID,
2572 occurredAt,
2573 payload: {
2574 id: MessageId.make("message-before"),
2575 threadId: THREAD_ID,
2576 runId: null,
2577 nodeId: null,
2578 role: "assistant",
2579 text: "Before",
2580 streaming: false,
2581 attachments: [],
2582 createdBy: "agent",
2583 creationSource: "provider",
2584 createdAt: occurredAt,
2585 updatedAt: occurredAt,
2586 },
2587 },
2588 },
2589 unknown(CACHED_SNAPSHOT_SEQUENCE + 2),
2590 titleUpdated("After", CACHED_SNAPSHOT_SEQUENCE + 3),
2591 unknown(CACHED_SNAPSHOT_SEQUENCE + 4),
2592 synchronized(),
2593 ]);
2594 const live = yield* awaitThreadState(
2595 harness.observed,
2596 (value) =>
2597 value.status === "live" &&
2598 Option.isSome(value.data) &&
2599 value.data.value.thread.title === "After",
2600 );
2601 expect(Option.isNone(live.error)).toBe(true);
2602 expect(Option.getOrThrow(live.data).messages.map((message) => message.text)).toEqual([
2603 "Before",
2604 ]);
2605
2606 yield* harness.replaceSession;
2607 yield* SubscriptionRef.changes(harness.subscriptionCount).pipe(
2608 Stream.filter((count) => count >= 2),
2609 Stream.runHead,
2610 );
2611 expect(yield* SubscriptionRef.get(harness.subscriptionCount)).toBe(2);
2612 expect(yield* Ref.get(harness.lastSubscribeAfterSequence)).toBe(CACHED_SNAPSHOT_SEQUENCE + 4);
2613 }),
2614 );
2615 });