@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 1,689 lines 66.7 KB
Raw
1 import {
2 EnvironmentId,
3 type ExecutionEnvironmentDescriptor,
4 ORCHESTRATION_V2_WS_METHODS,
5 type OrchestrationV2ShellSnapshot,
6 type OrchestrationV2ShellStreamItem,
7 ThreadId,
8 type ThreadPullRequestLink,
9 } from "@t3tools/contracts";
10 import { describe, expect, it } from "@effect/vitest";
11 import * as Cause from "effect/Cause";
12 import * as Deferred from "effect/Deferred";
13 import * as Effect from "effect/Effect";
14 import * as Exit from "effect/Exit";
15 import * as Fiber from "effect/Fiber";
16 import * as Option from "effect/Option";
17 import * as Queue from "effect/Queue";
18 import * as Ref from "effect/Ref";
19 import * as Scope from "effect/Scope";
20 import * as Stream from "effect/Stream";
21 import * as SubscriptionRef from "effect/SubscriptionRef";
22 import * as TestClock from "effect/testing/TestClock";
23
24 import {
25 AVAILABLE_CONNECTION_STATE,
26 PrimaryConnectionTarget,
27 type PreparedConnection,
28 } from "../connection/model.ts";
29 import * as EnvironmentSupervisor from "../connection/supervisor.ts";
30 import * as Persistence from "../platform/persistence.ts";
31 import * as RpcSession from "../rpc/session.ts";
32 import type { WsRpcProtocolClient } from "../rpc/protocol.ts";
33 import { type EnvironmentShellState, makeEnvironmentShellState } from "./shell.ts";
34 import * as ShellSnapshotLoader from "./shellSnapshotHttp.ts";
35 import { v2Project, v2ShellSnapshot, v2ThreadShell } from "./orchestrationV2TestFixtures.ts";
36
37 const TARGET = new PrimaryConnectionTarget({
38 environmentId: EnvironmentId.make("environment-1"),
39 label: "Test environment",
40 httpBaseUrl: "https://environment.example.test",
41 wsBaseUrl: "wss://environment.example.test",
42 });
43
44 const PREPARED: PreparedConnection = {
45 environmentId: TARGET.environmentId,
46 label: TARGET.label,
47 httpBaseUrl: TARGET.httpBaseUrl,
48 socketUrl: TARGET.wsBaseUrl,
49 httpAuthorization: null,
50 target: TARGET,
51 };
52
53 const LIVE_SHELL_SNAPSHOT: OrchestrationV2ShellSnapshot = {
54 ...v2ShellSnapshot,
55 snapshotSequence: 1,
56 };
57
58 function session(client: WsRpcProtocolClient): RpcSession.RpcSession {
59 return {
60 client,
61 initialConfig: Effect.succeed({
62 shellResumeCompletionMarker: true,
63 environment: { serverVersion: "1.0.0" },
64 } as never),
65 subscribeServerConfig: (input = {}) => client.subscribeServerConfig(input),
66 ready: Effect.void,
67 probe: Effect.void,
68 inboundFrames: Effect.succeed(0),
69 closed: Effect.never,
70 };
71 }
72
73 const makePersistenceHarness = Effect.fn("test.makeShellPersistenceHarness")(function* (
74 saveShell: Persistence.EnvironmentCacheStore["Service"]["saveShell"],
75 ) {
76 const events = yield* Queue.unbounded<OrchestrationV2ShellStreamItem>();
77 const client = {
78 [ORCHESTRATION_V2_WS_METHODS.subscribeShell]: () => Stream.fromQueue(events),
79 } as unknown as WsRpcProtocolClient;
80 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
81 target: TARGET,
82 state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE),
83 session: yield* SubscriptionRef.make(Option.some(session(client))),
84 prepared: yield* SubscriptionRef.make(Option.some(PREPARED)),
85 connect: Effect.void,
86 disconnect: Effect.void,
87 retryNow: Effect.void,
88 });
89 const cache = Persistence.EnvironmentCacheStore.of({
90 loadShell: () => Effect.succeedNone,
91 saveShell,
92 loadThread: () => Effect.succeedNone,
93 saveThread: () => Effect.void,
94 removeThread: () => Effect.void,
95 loadServerConfig: () => Effect.succeedNone,
96 saveServerConfig: () => Effect.void,
97 loadVcsRefs: () => Effect.succeedNone,
98 saveVcsRefs: () => Effect.void,
99 removeVcsRefs: () => Effect.void,
100 clearVcsRefs: () => Effect.void,
101 clear: () => Effect.void,
102 });
103 const state = yield* makeEnvironmentShellState().pipe(
104 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
105 Effect.provideService(Persistence.EnvironmentCacheStore, cache),
106 Effect.provideService(ShellSnapshotLoader.ShellSnapshotLoader, {
107 load: () => Effect.succeedNone,
108 }),
109 );
110 const send = Effect.fn("test.sendShellItem")(function* (
111 item: OrchestrationV2ShellStreamItem,
112 ready: (snapshot: OrchestrationV2ShellSnapshot) => boolean,
113 ) {
114 const previous = yield* SubscriptionRef.get(state);
115 yield* Queue.offer(events, item);
116 return yield* SubscriptionRef.changes(state).pipe(
117 Stream.filter((value) => value !== previous),
118 Stream.map((value) => value.snapshot),
119 Stream.filter(Option.isSome),
120 Stream.map((value) => value.value),
121 Stream.filter(ready),
122 Stream.runHead,
123 Effect.map(Option.getOrThrow),
124 );
125 });
126 yield* send({ kind: "snapshot", snapshot: LIVE_SHELL_SNAPSHOT }, () => true);
127 return { state, send };
128 });
129
130 describe("environment shell synchronization", () => {
131 it.effect.each([
132 { bufferSize: Infinity, expectedSequences: [51], existingRows: false },
133 { bufferSize: Infinity, expectedSequences: [51], existingRows: true },
134 // RpcClient defaults to a 16-event buffer, which splits larger server chunks.
135 { bufferSize: 16, expectedSequences: [17, 33, 49, 51], existingRows: false },
136 { bufferSize: 16, expectedSequences: [17, 33, 49, 51], existingRows: true },
137 ])("batches $bufferSize events (existing: $existingRows)", (input) =>
138 Effect.gen(function* () {
139 const { bufferSize, expectedSequences, existingRows } = input;
140 const events = yield* Queue.bounded<OrchestrationV2ShellStreamItem>(bufferSize);
141 let rowVisits = 0;
142 const rows = Array.from({ length: 50 }, (_, index) => ({
143 ...v2ThreadShell,
144 get id() {
145 rowVisits += 1;
146 return ThreadId.make(`thread-${index}`);
147 },
148 }));
149 const batchSnapshot = { ...LIVE_SHELL_SNAPSHOT, threads: existingRows ? rows : [] };
150 const client = {
151 [ORCHESTRATION_V2_WS_METHODS.subscribeShell]: () => Stream.fromQueue(events),
152 } as unknown as WsRpcProtocolClient;
153 const supervisorState = yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE);
154 const activeSession = yield* SubscriptionRef.make<Option.Option<RpcSession.RpcSession>>(
155 Option.some(session(client)),
156 );
157 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
158 target: TARGET,
159 state: supervisorState,
160 session: activeSession,
161 prepared: yield* SubscriptionRef.make(Option.some(PREPARED)),
162 connect: Effect.void,
163 disconnect: Effect.void,
164 retryNow: Effect.void,
165 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
166 const cache = Persistence.EnvironmentCacheStore.of({
167 loadShell: () => Effect.succeedNone,
168 saveShell: () => Effect.void,
169 loadThread: () => Effect.succeedNone,
170 saveThread: () => Effect.void,
171 removeThread: () => Effect.void,
172 loadServerConfig: () => Effect.succeedNone,
173 saveServerConfig: () => Effect.void,
174 loadVcsRefs: () => Effect.succeedNone,
175 saveVcsRefs: () => Effect.void,
176 removeVcsRefs: () => Effect.void,
177 clearVcsRefs: () => Effect.void,
178 clear: () => Effect.void,
179 });
180 const shellState = yield* makeEnvironmentShellState().pipe(
181 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
182 Effect.provideService(Persistence.EnvironmentCacheStore, cache),
183 Effect.provideService(
184 ShellSnapshotLoader.ShellSnapshotLoader,
185 ShellSnapshotLoader.ShellSnapshotLoader.of({
186 load: () => Effect.succeedNone,
187 }),
188 ),
189 );
190 yield* SubscriptionRef.set(supervisorState, {
191 desired: true,
192 network: "online",
193 phase: "connected",
194 stage: null,
195 attempt: 1,
196 generation: 1,
197 lastFailure: null,
198 retryAt: null,
199 refusedRouteIds: [],
200 });
201 yield* Queue.offer(events, { kind: "snapshot", snapshot: batchSnapshot });
202 yield* Queue.offer(events, { kind: "synchronized" });
203 yield* SubscriptionRef.changes(shellState).pipe(
204 Stream.filter((state) => state.status === "live"),
205 Stream.runHead,
206 );
207
208 // Observe before publishing so no batch can arrive before the subscription.
209 const observed = yield* SubscriptionRef.changes(shellState).pipe(
210 Stream.drop(1),
211 Stream.takeUntil(
212 (state) => Option.isSome(state.snapshot) && state.snapshot.value.snapshotSequence === 51,
213 ),
214 Stream.runCollect,
215 Effect.forkScoped({ startImmediately: true }),
216 );
217 rowVisits = 0;
218 yield* Queue.offerAll(
219 events,
220 Array.from({ length: 50 }, (_, index) => ({
221 kind: "thread.updated" as const,
222 sequence: 2 + index,
223 location: "active" as const,
224 thread: {
225 ...v2ThreadShell,
226 get id() {
227 rowVisits += 1;
228 return ThreadId.make(`thread-${index}`);
229 },
230 title: "Updated",
231 },
232 })),
233 );
234 const states = yield* Fiber.join(observed);
235 if (existingRows) {
236 // Index each received batch once; distinct updates must not rescan every row.
237 expect(rowVisits).toBeLessThanOrEqual(50 * (expectedSequences.length + 10));
238 }
239 const snapshots = states.map((state) => Option.getOrThrow(state.snapshot));
240 expect(snapshots.map((snapshot) => snapshot.snapshotSequence)).toEqual(expectedSequences);
241 expect(snapshots.at(-1)!.threads.map((thread) => thread.id)).toEqual(
242 Array.from({ length: 50 }, (_, index) => `thread-${index}`),
243 );
244
245 const removedObserved = yield* SubscriptionRef.changes(shellState).pipe(
246 Stream.filter(
247 (state) => Option.isSome(state.snapshot) && state.snapshot.value.snapshotSequence === 52,
248 ),
249 Stream.runHead,
250 Effect.forkScoped({ startImmediately: true }),
251 );
252 yield* Queue.offerAll(events, [
253 { kind: "future.event", sequence: 99 } as never,
254 {
255 kind: "thread.removed",
256 sequence: 52,
257 location: "active",
258 threadId: ThreadId.make("thread-0"),
259 },
260 {
261 kind: "thread.removed",
262 sequence: 51,
263 location: "active",
264 threadId: ThreadId.make("thread-1"),
265 },
266 ]);
267 const removedState = Option.getOrThrow(yield* Fiber.join(removedObserved));
268 expect(removedState.generation).toBe(states.at(-1)!.generation);
269 expect([...removedState.removedAt]).toEqual([[ThreadId.make("thread-0"), 52]]);
270
271 // An authoritative snapshot resets even a higher live cursor in the same batch.
272 const resetRow = { ...v2ThreadShell, id: ThreadId.make("reset-row") };
273 const resetObserved = yield* SubscriptionRef.changes(shellState).pipe(
274 Stream.filter(
275 (state) => Option.isSome(state.snapshot) && state.snapshot.value.snapshotSequence === 4,
276 ),
277 Stream.runHead,
278 Effect.forkScoped({ startImmediately: true }),
279 );
280 yield* Queue.offerAll(events, [
281 {
282 kind: "thread.removed",
283 sequence: 52,
284 location: "active",
285 threadId: ThreadId.make("thread-0"),
286 },
287 {
288 kind: "snapshot",
289 snapshot: { ...LIVE_SHELL_SNAPSHOT, snapshotSequence: 2, threads: [resetRow] },
290 },
291 { kind: "thread.removed", sequence: 2, location: "active", threadId: resetRow.id },
292 {
293 kind: "thread.updated",
294 sequence: 3,
295 location: "active",
296 thread: { ...resetRow, title: "After reset" },
297 },
298 { kind: "synchronized" },
299 {
300 kind: "thread.updated",
301 sequence: 4,
302 location: "active",
303 thread: { ...resetRow, title: "After completion" },
304 },
305 ]);
306 const resetState = Option.getOrThrow(yield* Fiber.join(resetObserved));
307 expect(resetState.status).toBe("live");
308 expect(resetState.generation).toBeGreaterThan(removedState.generation);
309 expect(resetState.removedAt.size).toBe(0);
310 expect(Option.getOrThrow(resetState.snapshot).threads).toEqual([
311 { ...resetRow, title: "After completion" },
312 ]);
313 }),
314 );
315
316 it.effect("skips unchanged shell writes, persists resets and flushes changed rows", () =>
317 Effect.gen(function* () {
318 const saved = yield* SubscriptionRef.make<ReadonlyArray<OrchestrationV2ShellSnapshot>>([]);
319 yield* Effect.scoped(
320 Effect.gen(function* () {
321 const h = yield* makePersistenceHarness((_id, snapshot) =>
322 SubscriptionRef.update(saved, (values) => [...values, snapshot]),
323 );
324 yield* TestClock.adjust("500 millis");
325 yield* h.send(
326 { kind: "thread.updated", sequence: 10, location: "active", thread: v2ThreadShell },
327 (snapshot) => snapshot.snapshotSequence === 10,
328 );
329 yield* TestClock.adjust("10 seconds");
330 expect(
331 (yield* SubscriptionRef.get(saved)).map((snapshot) => snapshot.snapshotSequence),
332 ).toEqual([1]);
333 const live = Option.getOrThrow((yield* SubscriptionRef.get(h.state)).snapshot);
334 expect(live.snapshotSequence).toBe(10);
335 const enriched = yield* h.send(
336 {
337 kind: "snapshot",
338 snapshot: { ...live, projects: live.projects.map((project) => ({ ...project })) },
339 resolvedRepositoryIdentityRoots: live.projects.map(
340 (project) => project.workspaceRoot,
341 ),
342 },
343 () => true,
344 );
345 expect(enriched.projects).toEqual(live.projects);
346 expect(enriched.snapshotSequence).toBe(10);
347 yield* TestClock.adjust("10 seconds");
348 expect(
349 (yield* SubscriptionRef.get(saved)).map((snapshot) => snapshot.snapshotSequence),
350 ).toEqual([1]);
351 yield* h.send(
352 { kind: "snapshot", snapshot: { ...LIVE_SHELL_SNAPSHOT, snapshotSequence: 4 } },
353 (snapshot) => snapshot.snapshotSequence === 4,
354 );
355 yield* TestClock.adjust("10 seconds");
356 yield* h.send(
357 {
358 kind: "snapshot",
359 snapshot: {
360 ...LIVE_SHELL_SNAPSHOT,
361 snapshotSequence: 4,
362 projects: [{ ...v2Project, title: "Changed at same sequence" }],
363 },
364 },
365 (snapshot) => snapshot.projects[0]?.title === "Changed at same sequence",
366 );
367 }),
368 );
369 expect(
370 (yield* SubscriptionRef.get(saved)).map((snapshot) => [
371 snapshot.snapshotSequence,
372 snapshot.projects[0]?.title,
373 ]),
374 ).toEqual([
375 [1, v2Project.title],
376 [4, v2Project.title],
377 [4, "Changed at same sequence"],
378 ]);
379 }),
380 );
381
382 it.effect("retries a failed shell save after a cursor-only update", () =>
383 Effect.gen(function* () {
384 const attempts = yield* Ref.make<ReadonlyArray<number>>([]);
385 yield* Effect.scoped(
386 Effect.gen(function* () {
387 const h = yield* makePersistenceHarness((_id, snapshot) =>
388 Effect.gen(function* () {
389 const values = yield* Ref.updateAndGet(attempts, (values) => [
390 ...values,
391 snapshot.snapshotSequence,
392 ]);
393 if (values.length === 1)
394 return yield* new Persistence.ConnectionPersistenceError({
395 operation: "save-shell",
396 message: "test failure",
397 });
398 }),
399 );
400 yield* TestClock.adjust("500 millis");
401 yield* h.send(
402 { kind: "thread.updated", sequence: 2, location: "active", thread: v2ThreadShell },
403 (snapshot) => snapshot.snapshotSequence === 2,
404 );
405 yield* TestClock.adjust("10 seconds");
406 }),
407 );
408 expect(yield* Ref.get(attempts)).toEqual([1, 2]);
409 }),
410 );
411
412 it.effect("flushes the latest live snapshot without blocking connection state updates", () =>
413 Effect.gen(function* () {
414 const events = yield* Queue.unbounded<OrchestrationV2ShellStreamItem>();
415 const saved = yield* SubscriptionRef.make<ReadonlyArray<OrchestrationV2ShellSnapshot>>([]);
416 const saveStarted = yield* Deferred.make<void>();
417 const releaseSave = yield* Deferred.make<void>();
418 const client = {
419 [ORCHESTRATION_V2_WS_METHODS.subscribeShell]: () => Stream.fromQueue(events),
420 } as unknown as WsRpcProtocolClient;
421 const supervisorState = yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE);
422 const activeSession = yield* SubscriptionRef.make<Option.Option<RpcSession.RpcSession>>(
423 Option.some(session(client)),
424 );
425 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
426 target: TARGET,
427 state: supervisorState,
428 session: activeSession,
429 prepared: yield* SubscriptionRef.make(Option.some(PREPARED)),
430 connect: Effect.void,
431 disconnect: Effect.void,
432 retryNow: Effect.void,
433 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
434 const cache = Persistence.EnvironmentCacheStore.of({
435 loadShell: () => Effect.succeedNone,
436 saveShell: (_environmentId, snapshot) =>
437 Deferred.succeed(saveStarted, undefined).pipe(
438 Effect.andThen(Deferred.await(releaseSave)),
439 Effect.andThen(SubscriptionRef.update(saved, (values) => [...values, snapshot])),
440 ),
441 loadThread: () => Effect.succeedNone,
442 saveThread: () => Effect.void,
443 removeThread: () => Effect.void,
444 loadServerConfig: () => Effect.succeedNone,
445 saveServerConfig: () => Effect.void,
446 loadVcsRefs: () => Effect.succeedNone,
447 saveVcsRefs: () => Effect.void,
448 removeVcsRefs: () => Effect.void,
449 clearVcsRefs: () => Effect.void,
450 clear: () => Effect.void,
451 });
452 const snapshotLoader = ShellSnapshotLoader.ShellSnapshotLoader.of({
453 load: () => Effect.succeedNone,
454 });
455
456 const shellState = yield* makeEnvironmentShellState().pipe(
457 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
458 Effect.provideService(Persistence.EnvironmentCacheStore, cache),
459 Effect.provideService(ShellSnapshotLoader.ShellSnapshotLoader, snapshotLoader),
460 );
461
462 yield* SubscriptionRef.set(supervisorState, {
463 desired: true,
464 network: "online",
465 phase: "connecting",
466 stage: "synchronizing",
467 attempt: 1,
468 generation: 0,
469 lastFailure: null,
470 retryAt: null,
471 refusedRouteIds: [],
472 });
473 yield* Queue.offer(events, {
474 kind: "snapshot",
475 snapshot: LIVE_SHELL_SNAPSHOT,
476 });
477 yield* Queue.offer(events, { kind: "synchronized" });
478 yield* SubscriptionRef.changes(shellState).pipe(
479 Stream.filter((state) => state.status === "live"),
480 Stream.runHead,
481 );
482
483 yield* SubscriptionRef.set(supervisorState, {
484 desired: true,
485 network: "online",
486 phase: "connecting",
487 stage: "synchronizing",
488 attempt: 2,
489 generation: 1,
490 lastFailure: null,
491 retryAt: null,
492 refusedRouteIds: [],
493 });
494 yield* SubscriptionRef.set(supervisorState, {
495 desired: false,
496 network: "online",
497 phase: "available",
498 stage: null,
499 attempt: 2,
500 generation: 2,
501 lastFailure: null,
502 retryAt: null,
503 refusedRouteIds: [],
504 });
505 yield* Deferred.await(saveStarted);
506
507 yield* SubscriptionRef.set(supervisorState, {
508 desired: true,
509 network: "online",
510 phase: "connecting",
511 stage: "synchronizing",
512 attempt: 3,
513 generation: 3,
514 lastFailure: null,
515 retryAt: null,
516 refusedRouteIds: [],
517 });
518 yield* SubscriptionRef.changes(shellState).pipe(
519 Stream.filter((state) => state.status === "synchronizing"),
520 Stream.runHead,
521 );
522
523 expect((yield* SubscriptionRef.get(shellState)).status).toBe("synchronizing");
524 expect(yield* SubscriptionRef.get(saved)).toEqual([]);
525
526 yield* Deferred.succeed(releaseSave, undefined);
527 yield* SubscriptionRef.changes(saved).pipe(
528 Stream.filter((values) => values.length > 0),
529 Stream.runHead,
530 );
531
532 expect(yield* SubscriptionRef.get(saved)).toEqual([LIVE_SHELL_SNAPSHOT]);
533 }),
534 );
535
536 it.effect("re-persists same-sequence enrichment when an older in-flight save completes", () =>
537 Effect.gen(function* () {
538 const repositoryIdentity = {
539 canonicalKey: "github.com/example/repo",
540 locator: {
541 source: "git-remote" as const,
542 remoteName: "origin",
543 remoteUrl: "https://github.com/example/repo.git",
544 },
545 };
546 const unenrichedSnapshot: OrchestrationV2ShellSnapshot = {
547 ...LIVE_SHELL_SNAPSHOT,
548 snapshotSequence: 1,
549 projects: [{ ...v2Project, repositoryIdentity: null }],
550 };
551 const enrichedSnapshot: OrchestrationV2ShellSnapshot = {
552 ...LIVE_SHELL_SNAPSHOT,
553 snapshotSequence: 1,
554 projects: [{ ...v2Project, repositoryIdentity }],
555 };
556
557 const events = yield* Queue.unbounded<OrchestrationV2ShellStreamItem>();
558 const saved = yield* SubscriptionRef.make<ReadonlyArray<OrchestrationV2ShellSnapshot>>([]);
559 const unenrichedSaveStarted = yield* Deferred.make<void>();
560 const releaseUnenrichedSave = yield* Deferred.make<void>();
561 const client = {
562 [ORCHESTRATION_V2_WS_METHODS.subscribeShell]: () => Stream.fromQueue(events),
563 } as unknown as WsRpcProtocolClient;
564 const supervisorState = yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE);
565 const activeSession = yield* SubscriptionRef.make<Option.Option<RpcSession.RpcSession>>(
566 Option.some(session(client)),
567 );
568 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
569 target: TARGET,
570 state: supervisorState,
571 session: activeSession,
572 prepared: yield* SubscriptionRef.make(Option.some(PREPARED)),
573 connect: Effect.void,
574 disconnect: Effect.void,
575 retryNow: Effect.void,
576 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
577 const cache = Persistence.EnvironmentCacheStore.of({
578 loadShell: () => Effect.succeedNone,
579 saveShell: (_environmentId, snapshot) =>
580 Effect.gen(function* () {
581 if (snapshot.projects[0]?.repositoryIdentity == null) {
582 yield* Deferred.succeed(unenrichedSaveStarted, undefined);
583 yield* Deferred.await(releaseUnenrichedSave);
584 }
585 yield* SubscriptionRef.update(saved, (values) => [...values, snapshot]);
586 }),
587 loadThread: () => Effect.succeedNone,
588 saveThread: () => Effect.void,
589 removeThread: () => Effect.void,
590 loadServerConfig: () => Effect.succeedNone,
591 saveServerConfig: () => Effect.void,
592 loadVcsRefs: () => Effect.succeedNone,
593 saveVcsRefs: () => Effect.void,
594 removeVcsRefs: () => Effect.void,
595 clearVcsRefs: () => Effect.void,
596 clear: () => Effect.void,
597 });
598 const snapshotLoader = ShellSnapshotLoader.ShellSnapshotLoader.of({
599 load: () => Effect.succeedNone,
600 });
601
602 const shellState = yield* makeEnvironmentShellState().pipe(
603 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
604 Effect.provideService(Persistence.EnvironmentCacheStore, cache),
605 Effect.provideService(ShellSnapshotLoader.ShellSnapshotLoader, snapshotLoader),
606 );
607
608 yield* SubscriptionRef.set(supervisorState, {
609 desired: true,
610 network: "online",
611 phase: "connecting",
612 stage: "synchronizing",
613 attempt: 1,
614 generation: 0,
615 lastFailure: null,
616 retryAt: null,
617 refusedRouteIds: [],
618 });
619 yield* Queue.offer(events, {
620 kind: "snapshot",
621 snapshot: unenrichedSnapshot,
622 });
623 yield* Queue.offer(events, { kind: "synchronized" });
624 yield* SubscriptionRef.changes(shellState).pipe(
625 Stream.filter((state) => state.status === "live"),
626 Stream.runHead,
627 );
628
629 // Disconnect flush starts an immediate persist of the unenriched snapshot
630 // (debounced path needs TestClock advancement; flush does not).
631 yield* SubscriptionRef.set(supervisorState, {
632 desired: false,
633 network: "online",
634 phase: "available",
635 stage: null,
636 attempt: 1,
637 generation: 1,
638 lastFailure: null,
639 retryAt: null,
640 refusedRouteIds: [],
641 });
642 yield* Deferred.await(unenrichedSaveStarted);
643
644 yield* Queue.offer(events, {
645 kind: "snapshot",
646 snapshot: enrichedSnapshot,
647 });
648 yield* SubscriptionRef.changes(shellState).pipe(
649 Stream.filter(
650 (state) =>
651 Option.isSome(state.snapshot) &&
652 state.snapshot.value.projects[0]?.repositoryIdentity != null,
653 ),
654 Stream.runHead,
655 );
656
657 yield* Deferred.succeed(releaseUnenrichedSave, undefined);
658 yield* SubscriptionRef.changes(saved).pipe(
659 Stream.filter((values) => values.at(-1)?.projects[0]?.repositoryIdentity != null),
660 Stream.runHead,
661 );
662
663 const finalSaved = (yield* SubscriptionRef.get(saved)).at(-1);
664 expect(finalSaved?.snapshotSequence).toBe(1);
665 expect(finalSaved?.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
666 }),
667 );
668
669 it.effect("scope teardown flush stays stable while a save is blocked", () =>
670 Effect.gen(function* () {
671 const lateSnapshot: OrchestrationV2ShellSnapshot = {
672 ...LIVE_SHELL_SNAPSHOT,
673 snapshotSequence: 2,
674 projects: [{ ...v2Project, title: "Late stream event" }],
675 };
676 const events = yield* Queue.unbounded<OrchestrationV2ShellStreamItem>();
677 const saved = yield* SubscriptionRef.make<ReadonlyArray<OrchestrationV2ShellSnapshot>>([]);
678 const saveStarted = yield* Deferred.make<void>();
679 const releaseSave = yield* Deferred.make<void>();
680 const client = {
681 [ORCHESTRATION_V2_WS_METHODS.subscribeShell]: () => Stream.fromQueue(events),
682 } as unknown as WsRpcProtocolClient;
683 const supervisorState = yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE);
684 const activeSession = yield* SubscriptionRef.make<Option.Option<RpcSession.RpcSession>>(
685 Option.some(session(client)),
686 );
687 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
688 target: TARGET,
689 state: supervisorState,
690 session: activeSession,
691 prepared: yield* SubscriptionRef.make(Option.some(PREPARED)),
692 connect: Effect.void,
693 disconnect: Effect.void,
694 retryNow: Effect.void,
695 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
696 const cache = Persistence.EnvironmentCacheStore.of({
697 loadShell: () => Effect.succeedNone,
698 saveShell: (_environmentId, snapshot) =>
699 Deferred.succeed(saveStarted, undefined).pipe(
700 Effect.andThen(Deferred.await(releaseSave)),
701 Effect.andThen(SubscriptionRef.update(saved, (values) => [...values, snapshot])),
702 ),
703 loadThread: () => Effect.succeedNone,
704 saveThread: () => Effect.void,
705 removeThread: () => Effect.void,
706 loadServerConfig: () => Effect.succeedNone,
707 saveServerConfig: () => Effect.void,
708 loadVcsRefs: () => Effect.succeedNone,
709 saveVcsRefs: () => Effect.void,
710 removeVcsRefs: () => Effect.void,
711 clearVcsRefs: () => Effect.void,
712 clear: () => Effect.void,
713 });
714 const snapshotLoader = ShellSnapshotLoader.ShellSnapshotLoader.of({
715 load: () => Effect.succeedNone,
716 });
717
718 const scope = yield* Scope.make();
719 const shellState = yield* makeEnvironmentShellState().pipe(
720 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
721 Effect.provideService(Persistence.EnvironmentCacheStore, cache),
722 Effect.provideService(ShellSnapshotLoader.ShellSnapshotLoader, snapshotLoader),
723 Scope.provide(scope),
724 );
725
726 yield* SubscriptionRef.set(supervisorState, {
727 desired: true,
728 network: "online",
729 phase: "connecting",
730 stage: "synchronizing",
731 attempt: 1,
732 generation: 0,
733 lastFailure: null,
734 retryAt: null,
735 refusedRouteIds: [],
736 });
737 yield* Queue.offer(events, {
738 kind: "snapshot",
739 snapshot: LIVE_SHELL_SNAPSHOT,
740 });
741 yield* Queue.offer(events, { kind: "synchronized" });
742 yield* SubscriptionRef.changes(shellState).pipe(
743 Stream.filter((state) => state.status === "live"),
744 Stream.runHead,
745 );
746
747 const closeFiber = yield* Effect.forkChild(Scope.close(scope, Exit.void));
748 yield* Deferred.await(saveStarted);
749 yield* Queue.offer(events, {
750 kind: "snapshot",
751 snapshot: lateSnapshot,
752 });
753 for (let index = 0; index < 50; index += 1) {
754 yield* Effect.yieldNow;
755 }
756 yield* Deferred.succeed(releaseSave, undefined);
757 yield* Fiber.join(closeFiber);
758
759 const values = yield* SubscriptionRef.get(saved);
760 expect(values.length).toBeGreaterThan(0);
761 expect(values.every((snapshot) => snapshot.snapshotSequence === 1)).toBe(true);
762 expect(values.every((snapshot) => snapshot.projects[0]?.title !== "Late stream event")).toBe(
763 true,
764 );
765 }),
766 );
767
768 it.effect("applies authoritative lower-sequence HTTP resets over client-ahead cache", () =>
769 Effect.gen(function* () {
770 const cachedSnapshot: OrchestrationV2ShellSnapshot = {
771 ...v2ShellSnapshot,
772 snapshotSequence: 20,
773 projects: [{ ...v2Project, title: "Client ahead" }],
774 };
775 const httpSnapshot: OrchestrationV2ShellSnapshot = {
776 ...v2ShellSnapshot,
777 snapshotSequence: 4,
778 projects: [{ ...v2Project, title: "Server reset" }],
779 };
780 const events = yield* Queue.unbounded<OrchestrationV2ShellStreamItem>();
781 const client = {
782 [ORCHESTRATION_V2_WS_METHODS.subscribeShell]: () => Stream.fromQueue(events),
783 } as unknown as WsRpcProtocolClient;
784 const supervisorState = yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE);
785 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
786 target: TARGET,
787 state: supervisorState,
788 session: yield* SubscriptionRef.make(Option.some(session(client))),
789 prepared: yield* SubscriptionRef.make(Option.some(PREPARED)),
790 connect: Effect.void,
791 disconnect: Effect.void,
792 retryNow: Effect.void,
793 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
794 const cache = Persistence.EnvironmentCacheStore.of({
795 loadShell: () => Effect.succeedSome(cachedSnapshot),
796 saveShell: () => Effect.void,
797 loadThread: () => Effect.succeedNone,
798 saveThread: () => Effect.void,
799 removeThread: () => Effect.void,
800 loadServerConfig: () => Effect.succeedNone,
801 saveServerConfig: () => Effect.void,
802 loadVcsRefs: () => Effect.succeedNone,
803 saveVcsRefs: () => Effect.void,
804 removeVcsRefs: () => Effect.void,
805 clearVcsRefs: () => Effect.void,
806 clear: () => Effect.void,
807 });
808 const snapshotLoader = ShellSnapshotLoader.ShellSnapshotLoader.of({
809 load: () => Effect.succeedSome(httpSnapshot),
810 });
811 const shellState = yield* makeEnvironmentShellState().pipe(
812 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
813 Effect.provideService(Persistence.EnvironmentCacheStore, cache),
814 Effect.provideService(ShellSnapshotLoader.ShellSnapshotLoader, snapshotLoader),
815 );
816
817 yield* SubscriptionRef.changes(shellState).pipe(
818 Stream.filter(
819 (state) => Option.isSome(state.snapshot) && state.snapshot.value.snapshotSequence === 4,
820 ),
821 Stream.runHead,
822 );
823
824 const state = yield* SubscriptionRef.get(shellState);
825 expect(Option.getOrThrow(state.snapshot).snapshotSequence).toBe(4);
826 expect(Option.getOrThrow(state.snapshot).projects[0]?.title).toBe("Server reset");
827 }),
828 );
829
830 it.effect("opts in to shell omissions and counts authoritative snapshots and removals", () =>
831 Effect.gen(function* () {
832 const events = yield* Queue.unbounded<OrchestrationV2ShellStreamItem>();
833 const subscribeInputs = yield* Queue.unbounded<unknown>();
834 const loadOptions = yield* Ref.make<ReadonlyArray<unknown>>([]);
835 const client = {
836 [ORCHESTRATION_V2_WS_METHODS.subscribeShell]: (input: unknown) =>
837 Stream.unwrap(
838 Queue.offer(subscribeInputs, input).pipe(Effect.as(Stream.fromQueue(events))),
839 ),
840 } as unknown as WsRpcProtocolClient;
841 const familySession: RpcSession.RpcSession = {
842 ...session(client),
843 initialConfig: Effect.succeed({
844 shellResumeCompletionMarker: true,
845 shellThreadFamilies: true,
846 shellOlderSettled: true,
847 environment: { serverVersion: "1.0.0", databaseMigration: 70 },
848 } as never),
849 };
850 const activeSession = yield* SubscriptionRef.make(Option.some(familySession));
851 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
852 target: TARGET,
853 state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE),
854 session: activeSession,
855 prepared: yield* SubscriptionRef.make(Option.some(PREPARED)),
856 connect: Effect.void,
857 disconnect: Effect.void,
858 retryNow: Effect.void,
859 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
860 const cache = Persistence.EnvironmentCacheStore.of({
861 loadShell: () => Effect.succeedNone,
862 saveShell: () => Effect.void,
863 loadThread: () => Effect.succeedNone,
864 saveThread: () => Effect.void,
865 removeThread: () => Effect.void,
866 loadServerConfig: () => Effect.succeedNone,
867 saveServerConfig: () => Effect.void,
868 loadVcsRefs: () => Effect.succeedNone,
869 saveVcsRefs: () => Effect.void,
870 removeVcsRefs: () => Effect.void,
871 clearVcsRefs: () => Effect.void,
872 clear: () => Effect.void,
873 });
874 const snapshotLoader = ShellSnapshotLoader.ShellSnapshotLoader.of({
875 load: (_prepared, options) =>
876 Ref.update(loadOptions, (all) => [...all, options]).pipe(
877 Effect.as(Option.some(LIVE_SHELL_SNAPSHOT)),
878 ),
879 });
880 const shellState = yield* makeEnvironmentShellState().pipe(
881 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
882 Effect.provideService(Persistence.EnvironmentCacheStore, cache),
883 Effect.provideService(ShellSnapshotLoader.ShellSnapshotLoader, snapshotLoader),
884 );
885 const stateWhere = (predicate: (state: EnvironmentShellState) => boolean) =>
886 SubscriptionRef.changes(shellState).pipe(Stream.filter(predicate), Stream.runHead);
887
888 // Both transports must omit, or omitted children would come back through the socket.
889 expect(yield* Queue.take(subscribeInputs)).toMatchObject({
890 afterSequence: 1,
891 omitIdleSubagents: true,
892 omitOlderSettled: true,
893 });
894 expect(yield* Ref.get(loadOptions)).toEqual([
895 { omitIdleSubagents: true, omitOlderSettled: true },
896 ]);
897 const loaded = Option.getOrThrow(yield* stateWhere((state) => state.generation > 0));
898 expect(loaded.removedAt.size).toBe(0);
899
900 const gone = ThreadId.make("thread-gone");
901 yield* Queue.offer(events, {
902 kind: "snapshot",
903 snapshot: { ...LIVE_SHELL_SNAPSHOT, threads: [] },
904 resolvedRepositoryIdentityRoots: [],
905 });
906 yield* Queue.offer(events, {
907 kind: "thread.removed",
908 sequence: 2,
909 location: "active",
910 threadId: gone,
911 });
912 const removed = Option.getOrThrow(yield* stateWhere((state) => state.removedAt.size > 0));
913 // An enrichment refresh is not authoritative.
914 expect(removed.generation).toBe(loaded.generation);
915 expect(removed.removedAt.get(gone)).toBe(2);
916
917 yield* Queue.offer(events, {
918 kind: "snapshot",
919 snapshot: { ...LIVE_SHELL_SNAPSHOT, snapshotSequence: 5 },
920 });
921 const reset = Option.getOrThrow(
922 yield* stateWhere((state) => state.generation > loaded.generation),
923 );
924 expect(reset.removedAt.size).toBe(0);
925
926 // A reconnect resumes the server-loaded shell from its cursor, with no second HTTP load.
927 yield* SubscriptionRef.set(activeSession, Option.some({ ...familySession }));
928 expect(yield* Queue.take(subscribeInputs)).toMatchObject({
929 afterSequence: 5,
930 omitIdleSubagents: true,
931 omitOlderSettled: true,
932 });
933 expect(yield* Ref.get(loadOptions)).toHaveLength(1);
934
935 // A migrated server may have rewritten rows without events, so it reloads.
936 yield* SubscriptionRef.set(
937 activeSession,
938 Option.some({
939 ...familySession,
940 initialConfig: familySession.initialConfig.pipe(
941 Effect.map((config) => ({
942 ...config,
943 environment: { ...config.environment, databaseMigration: 71 },
944 })),
945 ),
946 }),
947 );
948 yield* Queue.take(subscribeInputs);
949 expect(yield* Ref.get(loadOptions)).toHaveLength(2);
950 }),
951 );
952
953 it.effect("fills deferred cached pull request links without overwriting newer rows", () =>
954 Effect.gen(function* () {
955 const link = (number: number): ThreadPullRequestLink => ({
956 host: "github.com",
957 repository: "pingdotgg/t3code",
958 number,
959 url: `https://github.com/pingdotgg/t3code/pull/${number}`,
960 source: "agent",
961 linkedAt: "2026-06-20T00:00:00.000Z",
962 snapshot: null,
963 stack: null,
964 });
965 const otherThreadId = ThreadId.make("thread-other");
966 const cachedSnapshot: OrchestrationV2ShellSnapshot = {
967 ...v2ShellSnapshot,
968 snapshotSequence: 5,
969 threads: [v2ThreadShell, { ...v2ThreadShell, id: otherThreadId }],
970 };
971 const linksReady = yield* Deferred.make<void>();
972 const events = yield* Queue.unbounded<OrchestrationV2ShellStreamItem>();
973 const client = {
974 [ORCHESTRATION_V2_WS_METHODS.subscribeShell]: () => Stream.fromQueue(events),
975 } as unknown as WsRpcProtocolClient;
976 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
977 target: TARGET,
978 state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE),
979 session: yield* SubscriptionRef.make(Option.some(session(client))),
980 prepared: yield* SubscriptionRef.make(Option.some(PREPARED)),
981 connect: Effect.void,
982 disconnect: Effect.void,
983 retryNow: Effect.void,
984 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
985 const cache = Persistence.EnvironmentCacheStore.of({
986 loadShell: () =>
987 Effect.succeedSome({
988 ...cachedSnapshot,
989 loadPullRequests: Deferred.await(linksReady).pipe(
990 Effect.as(
991 new Map([
992 [v2ThreadShell.id, [link(1)]],
993 [otherThreadId, [link(2)]],
994 ]),
995 ),
996 ),
997 }),
998 saveShell: () => Effect.void,
999 loadThread: () => Effect.succeedNone,
1000 saveThread: () => Effect.void,
1001 removeThread: () => Effect.void,
1002 loadServerConfig: () => Effect.succeedNone,
1003 saveServerConfig: () => Effect.void,
1004 loadVcsRefs: () => Effect.succeedNone,
1005 saveVcsRefs: () => Effect.void,
1006 removeVcsRefs: () => Effect.void,
1007 clearVcsRefs: () => Effect.void,
1008 clear: () => Effect.void,
1009 });
1010 const snapshotLoader = ShellSnapshotLoader.ShellSnapshotLoader.of({
1011 load: () => Effect.succeedNone,
1012 });
1013 const shellState = yield* makeEnvironmentShellState().pipe(
1014 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
1015 Effect.provideService(Persistence.EnvironmentCacheStore, cache),
1016 Effect.provideService(ShellSnapshotLoader.ShellSnapshotLoader, snapshotLoader),
1017 );
1018
1019 const threadsOf = (state: {
1020 readonly snapshot: Option.Option<OrchestrationV2ShellSnapshot>;
1021 }) => Option.getOrThrow(state.snapshot).threads;
1022 expect(threadsOf(yield* SubscriptionRef.get(shellState))).toEqual(cachedSnapshot.threads);
1023
1024 const serverRow = { ...v2ThreadShell, title: "From server", pullRequests: [link(9)] };
1025 yield* Queue.offer(events, {
1026 kind: "thread.updated",
1027 sequence: 6,
1028 location: "active",
1029 thread: serverRow,
1030 });
1031 yield* SubscriptionRef.changes(shellState).pipe(
1032 Stream.filter((state) => threadsOf(state)[0]?.title === "From server"),
1033 Stream.runHead,
1034 );
1035
1036 yield* Deferred.succeed(linksReady, undefined);
1037 const filled = yield* SubscriptionRef.changes(shellState).pipe(
1038 Stream.map(threadsOf),
1039 Stream.filter((threads) => threads[1]?.pullRequests !== undefined),
1040 Stream.runHead,
1041 Effect.map(Option.getOrThrow),
1042 );
1043 expect(filled[0]).toBe(serverRow);
1044 expect(filled[1]?.pullRequests).toEqual([link(2)]);
1045 }),
1046 );
1047
1048 it.effect("drops an older snapshot's links once a newer snapshot reuses its rows", () =>
1049 Effect.gen(function* () {
1050 const link = (number: number): ThreadPullRequestLink => ({
1051 host: "github.com",
1052 repository: "pingdotgg/t3code",
1053 number,
1054 url: `https://github.com/pingdotgg/t3code/pull/${number}`,
1055 source: "agent",
1056 linkedAt: "2026-06-20T00:00:00.000Z",
1057 snapshot: null,
1058 stack: null,
1059 });
1060 const firstLinks = yield* Deferred.make<void>();
1061 const secondLinks = yield* Deferred.make<void>();
1062 const firstFillStarted = yield* Deferred.make<void>();
1063 const loads = yield* Ref.make(0);
1064 const subscriptions = yield* Ref.make(0);
1065 const client = {
1066 [ORCHESTRATION_V2_WS_METHODS.subscribeShell]: () =>
1067 Stream.unwrap(Ref.update(subscriptions, (n) => n + 1).pipe(Effect.as(Stream.never))),
1068 } as unknown as WsRpcProtocolClient;
1069 const activeSession = yield* SubscriptionRef.make<Option.Option<RpcSession.RpcSession>>(
1070 Option.some(session(client)),
1071 );
1072 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
1073 target: TARGET,
1074 state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE),
1075 session: activeSession,
1076 prepared: yield* SubscriptionRef.make(Option.some(PREPARED)),
1077 connect: Effect.void,
1078 disconnect: Effect.void,
1079 retryNow: Effect.void,
1080 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
1081 const cache = Persistence.EnvironmentCacheStore.of({
1082 loadShell: () => Effect.succeedNone,
1083 saveShell: () => Effect.void,
1084 loadThread: () => Effect.succeedNone,
1085 saveThread: () => Effect.void,
1086 removeThread: () => Effect.void,
1087 loadServerConfig: () => Effect.succeedNone,
1088 saveServerConfig: () => Effect.void,
1089 loadVcsRefs: () => Effect.succeedNone,
1090 saveVcsRefs: () => Effect.void,
1091 removeVcsRefs: () => Effect.void,
1092 clearVcsRefs: () => Effect.void,
1093 clear: () => Effect.void,
1094 });
1095 const snapshotLoader = ShellSnapshotLoader.ShellSnapshotLoader.of({
1096 load: () =>
1097 Ref.updateAndGet(loads, (n) => n + 1).pipe(
1098 Effect.map((n) =>
1099 Option.some({
1100 ...v2ShellSnapshot,
1101 snapshotSequence: n,
1102 loadPullRequests: (n === 1
1103 ? Deferred.succeed(firstFillStarted, undefined).pipe(
1104 Effect.andThen(Deferred.await(firstLinks)),
1105 )
1106 : Deferred.await(secondLinks)
1107 ).pipe(Effect.as(new Map([[v2ThreadShell.id, [link(n)]]]))),
1108 }),
1109 ),
1110 ),
1111 });
1112 const shellState = yield* makeEnvironmentShellState().pipe(
1113 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
1114 Effect.provideService(Persistence.EnvironmentCacheStore, cache),
1115 Effect.provideService(ShellSnapshotLoader.ShellSnapshotLoader, snapshotLoader),
1116 );
1117 yield* Deferred.await(firstFillStarted);
1118
1119 // New omissions make the next session reload instead of resuming.
1120 yield* SubscriptionRef.set(
1121 activeSession,
1122 Option.some({
1123 ...session(client),
1124 initialConfig: Effect.succeed({
1125 shellThreadFamilies: true,
1126 environment: { serverVersion: "1.0.0" },
1127 } as never),
1128 }),
1129 );
1130 yield* SubscriptionRef.changes(shellState).pipe(
1131 Stream.filter(
1132 (state) => Option.isSome(state.snapshot) && state.snapshot.value.snapshotSequence === 2,
1133 ),
1134 Stream.runHead,
1135 );
1136
1137 yield* Deferred.succeed(firstLinks, undefined);
1138 yield* Deferred.succeed(secondLinks, undefined);
1139 const filled = yield* SubscriptionRef.changes(shellState).pipe(
1140 Stream.map((state) => Option.getOrThrow(state.snapshot).threads[0]?.pullRequests),
1141 Stream.filter((links) => links !== undefined),
1142 Stream.runHead,
1143 Effect.map(Option.getOrThrow),
1144 );
1145 expect(filled).toEqual([link(2)]);
1146 }),
1147 );
1148 });
1149
1150 describe("environment shell cold start", () => {
1151 const NOW = 1_800_000_000_000;
1152 const HOUR = 60 * 60 * 1000;
1153 const CACHED_SEQUENCE = 7;
1154 const HTTP_SEQUENCE = 9;
1155 // What `session()` advertises: server 1.0.0, no omissions, no migration.
1156 const CURRENT_ORIGIN = {
1157 serverVersion: "1.0.0",
1158 databaseMigration: null,
1159 omitIdleSubagents: false,
1160 omitOlderSettled: false,
1161 };
1162 const RECENT_ORIGIN = { ...CURRENT_ORIGIN, snapshotAt: NOW - HOUR };
1163
1164 // A server that names its shell flags in the descriptor, and the matching live config.
1165 const FAMILY_DESCRIPTOR = {
1166 serverVersion: "1.0.0",
1167 capabilities: { shellThreadFamilies: true, shellOlderSettled: true },
1168 } as ExecutionEnvironmentDescriptor;
1169 const FAMILY_CONFIG = {
1170 shellThreadFamilies: true,
1171 shellOlderSettled: true,
1172 environment: { serverVersion: "1.0.0" },
1173 };
1174 const FAMILY_FLAGS = { omitIdleSubagents: true, omitOlderSettled: true };
1175
1176 const makeColdStart = Effect.fn("test.makeColdStartShell")(function* (
1177 origin: Persistence.ShellCacheOrigin | undefined,
1178 // Set for a connection that is prepared but has no session yet.
1179 // Each held load waits for one `releaseLoads`. An "uninterruptible" hold stands for a load
1180 // that applies its answer, which an interrupt has to wait for.
1181 early?: {
1182 readonly serverResumes: boolean;
1183 readonly holdLoads?: boolean | "uninterruptible";
1184 },
1185 ) {
1186 yield* TestClock.setTime(NOW);
1187 const subscriptions = yield* Queue.unbounded<{
1188 readonly input: unknown;
1189 readonly events: Queue.Queue<OrchestrationV2ShellStreamItem>;
1190 }>();
1191 const saved = yield* Queue.unbounded<{
1192 readonly snapshot: OrchestrationV2ShellSnapshot;
1193 readonly origin: Persistence.ShellCacheOrigin | undefined;
1194 }>();
1195 const httpLoads = yield* Ref.make(0);
1196 const loadOptions = yield* Queue.unbounded<ShellSnapshotLoader.ShellSnapshotOptions>();
1197 const releases = yield* Queue.unbounded<void>();
1198 const scope = yield* Effect.scope;
1199 const holdLoad =
1200 early?.holdLoads === "uninterruptible"
1201 ? // Registered after the load's fiber, so the end of a failed test does not wait for it.
1202 Scope.addFinalizer(scope, Queue.shutdown(releases)).pipe(
1203 Effect.andThen(Effect.uninterruptible(Queue.take(releases))),
1204 )
1205 : early?.holdLoads === true
1206 ? Queue.take(releases)
1207 : Effect.void;
1208 const client = {
1209 [ORCHESTRATION_V2_WS_METHODS.subscribeShell]: (input: unknown) =>
1210 Stream.unwrap(
1211 Effect.gen(function* () {
1212 const events = yield* Queue.unbounded<OrchestrationV2ShellStreamItem>();
1213 yield* Queue.offer(subscriptions, { input, events });
1214 return Stream.fromQueue(events);
1215 }),
1216 ),
1217 } as unknown as WsRpcProtocolClient;
1218 const activeSession = yield* SubscriptionRef.make(
1219 early === undefined ? Option.some(session(client)) : Option.none<RpcSession.RpcSession>(),
1220 );
1221 const prepared = yield* SubscriptionRef.make(
1222 Option.some(early === undefined ? PREPARED : { ...PREPARED, descriptor: FAMILY_DESCRIPTOR }),
1223 );
1224 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
1225 target: TARGET,
1226 state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE),
1227 session: activeSession,
1228 prepared,
1229 connect: Effect.void,
1230 disconnect: Effect.void,
1231 retryNow: Effect.void,
1232 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
1233 const cache = Persistence.EnvironmentCacheStore.of({
1234 loadShell: () =>
1235 Effect.succeedSome({
1236 ...v2ShellSnapshot,
1237 snapshotSequence: CACHED_SEQUENCE,
1238 ...(origin === undefined ? {} : { origin }),
1239 }),
1240 saveShell: (_environmentId, snapshot, origin?: Persistence.ShellCacheOrigin) =>
1241 Queue.offer(saved, { snapshot, origin }).pipe(Effect.asVoid),
1242 loadThread: () => Effect.succeedNone,
1243 saveThread: () => Effect.void,
1244 removeThread: () => Effect.void,
1245 loadServerConfig: () => Effect.succeedNone,
1246 saveServerConfig: () => Effect.void,
1247 loadVcsRefs: () => Effect.succeedNone,
1248 saveVcsRefs: () => Effect.void,
1249 removeVcsRefs: () => Effect.void,
1250 clearVcsRefs: () => Effect.void,
1251 clear: () => Effect.void,
1252 });
1253 const snapshotLoader = ShellSnapshotLoader.ShellSnapshotLoader.of({
1254 load: (_prepared, options = {}) =>
1255 Queue.offer(loadOptions, options).pipe(
1256 Effect.andThen(holdLoad),
1257 Effect.andThen(Ref.updateAndGet(httpLoads, (count) => count + 1)),
1258 Effect.map((count) =>
1259 Option.some<ShellSnapshotLoader.ShellHttpAnswer>(
1260 early?.serverResumes === true && options.resumeFrom !== undefined
1261 ? { resumable: true }
1262 : {
1263 ...v2ShellSnapshot,
1264 snapshotSequence: HTTP_SEQUENCE + count - 1,
1265 projectIdentities: `identities-${count}`,
1266 },
1267 ),
1268 ),
1269 ),
1270 });
1271 const shellState = yield* makeEnvironmentShellState().pipe(
1272 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
1273 Effect.provideService(Persistence.EnvironmentCacheStore, cache),
1274 Effect.provideService(ShellSnapshotLoader.ShellSnapshotLoader, snapshotLoader),
1275 );
1276 const nextConnection = Effect.suspend(() =>
1277 SubscriptionRef.set(
1278 prepared,
1279 Option.some({ ...PREPARED, descriptor: { ...FAMILY_DESCRIPTOR } }),
1280 ),
1281 );
1282 return {
1283 shellState,
1284 subscriptions,
1285 saved,
1286 httpLoads,
1287 loadOptions,
1288 releaseLoads: Queue.offer(releases, undefined),
1289 nextConnection,
1290 // A connection attempt that ended, then the next one.
1291 prepareAgain: SubscriptionRef.set(prepared, Option.none()).pipe(
1292 Effect.andThen(nextConnection),
1293 ),
1294 // Resolves `configRead` when the subscription reads the config, just before it claims the load.
1295 connect: (config: object, configRead?: Deferred.Deferred<void>) =>
1296 SubscriptionRef.set(
1297 activeSession,
1298 Option.some({
1299 ...session(client),
1300 initialConfig: (configRead === undefined
1301 ? Effect.void
1302 : Deferred.succeed(configRead, undefined)
1303 ).pipe(Effect.as(config as never)),
1304 }),
1305 ),
1306 reconnect: SubscriptionRef.set(activeSession, Option.some(session(client))),
1307 // The registry swaps a rotated bearer into the prepared connection of a live session.
1308 rotateCredential: SubscriptionRef.update(
1309 prepared,
1310 Option.map((current) => ({
1311 ...current,
1312 httpAuthorization: { _tag: "Bearer" as const, token: "rotated" },
1313 })),
1314 ),
1315 liveAt: (sequence: number) =>
1316 SubscriptionRef.changes(shellState).pipe(
1317 Stream.filter(
1318 (state) =>
1319 state.status === "live" &&
1320 Option.exists(state.snapshot, (snapshot) => snapshot.snapshotSequence === sequence),
1321 ),
1322 Stream.runHead,
1323 Effect.map(Option.getOrThrow),
1324 ),
1325 };
1326 });
1327
1328 it.effect.each([
1329 { label: "a recent matching origin", origin: RECENT_ORIGIN, resumes: true },
1330 { label: "no origin", origin: undefined, resumes: false },
1331 {
1332 label: "other omissions",
1333 origin: { ...RECENT_ORIGIN, omitIdleSubagents: true },
1334 resumes: false,
1335 },
1336 {
1337 label: "another migration",
1338 origin: { ...RECENT_ORIGIN, databaseMigration: 70 },
1339 resumes: false,
1340 },
1341 {
1342 label: "another server version",
1343 origin: { ...RECENT_ORIGIN, serverVersion: "0.9.0" },
1344 resumes: false,
1345 },
1346 {
1347 label: "a 12 hour old origin",
1348 origin: { ...RECENT_ORIGIN, snapshotAt: NOW - 12 * HOUR },
1349 resumes: false,
1350 },
1351 ])("starts from a cache with $label (resumes: $resumes)", ({ origin, resumes }) =>
1352 Effect.gen(function* () {
1353 const h = yield* makeColdStart(origin);
1354 const first = yield* Queue.take(h.subscriptions);
1355 const sequence = resumes ? CACHED_SEQUENCE : HTTP_SEQUENCE;
1356 expect(first.input).toMatchObject({ afterSequence: sequence });
1357 // Only held rows may be refused; a fresh snapshot's cursor never loops back to HTTP.
1358 if (resumes) expect(first.input).toMatchObject({ httpSnapshotFallback: true });
1359 else expect(first.input).not.toHaveProperty("httpSnapshotFallback");
1360 expect(yield* Ref.get(h.httpLoads)).toBe(resumes ? 0 : 1);
1361
1362 // Thread families, older threads, and search wait for a nonzero generation.
1363 yield* Queue.offer(first.events, { kind: "synchronized" });
1364 expect((yield* h.liveAt(sequence)).generation).toBeGreaterThan(0);
1365
1366 if (!resumes) {
1367 // The next cold start may resume from this snapshot.
1368 yield* TestClock.adjust("500 millis");
1369 expect(yield* Queue.take(h.saved)).toMatchObject({
1370 snapshot: { snapshotSequence: HTTP_SEQUENCE },
1371 origin: { ...CURRENT_ORIGIN, snapshotAt: NOW },
1372 });
1373 }
1374 }),
1375 );
1376
1377 it.effect.each([
1378 {
1379 label: "no rows to resume",
1380 origin: undefined,
1381 serverResumes: false,
1382 config: FAMILY_CONFIG,
1383 loads: [FAMILY_FLAGS],
1384 subscribes: { afterSequence: HTTP_SEQUENCE, knownProjectIdentities: "identities-1" },
1385 },
1386 {
1387 label: "rows the server can replay from",
1388 origin: { ...RECENT_ORIGIN, ...FAMILY_FLAGS },
1389 serverResumes: true,
1390 config: FAMILY_CONFIG,
1391 loads: [{ ...FAMILY_FLAGS, resumeFrom: CACHED_SEQUENCE }],
1392 subscribes: { afterSequence: CACHED_SEQUENCE, httpSnapshotFallback: true },
1393 },
1394 {
1395 label: "rows the server cannot replay from",
1396 origin: { ...RECENT_ORIGIN, ...FAMILY_FLAGS },
1397 serverResumes: false,
1398 config: FAMILY_CONFIG,
1399 loads: [{ ...FAMILY_FLAGS, resumeFrom: CACHED_SEQUENCE }],
1400 subscribes: { afterSequence: HTTP_SEQUENCE, knownProjectIdentities: "identities-1" },
1401 },
1402 {
1403 // The server answered that it can replay, then the rows passed their age limit.
1404 label: "rows that expire before the socket is ready",
1405 origin: { ...RECENT_ORIGIN, ...FAMILY_FLAGS, snapshotAt: NOW - 12 * HOUR + 1 },
1406 serverResumes: true,
1407 config: FAMILY_CONFIG,
1408 loads: [{ ...FAMILY_FLAGS, resumeFrom: CACHED_SEQUENCE }],
1409 subscribes: {},
1410 expires: true,
1411 },
1412 {
1413 // The rows loaded with the descriptor's omissions are replaced, never kept.
1414 label: "a live config with other omissions",
1415 origin: undefined,
1416 serverResumes: false,
1417 config: { environment: { serverVersion: "1.0.0" } },
1418 loads: [FAMILY_FLAGS, { omitIdleSubagents: false, omitOlderSettled: false }],
1419 subscribes: { afterSequence: HTTP_SEQUENCE + 1, knownProjectIdentities: "identities-2" },
1420 },
1421 ])(
1422 "asks for the shell once the connection is prepared, with $label",
1423 ({ origin, serverResumes, config, loads, subscribes, expires }) =>
1424 Effect.gen(function* () {
1425 const h = yield* makeColdStart(origin, { serverResumes });
1426 expect(yield* Queue.take(h.loadOptions)).toEqual(loads[0]);
1427 if (!serverResumes) {
1428 // The rows are on screen before a socket exists, and are not live yet.
1429 const early = yield* SubscriptionRef.changes(h.shellState).pipe(
1430 Stream.filter((state) =>
1431 Option.exists(
1432 state.snapshot,
1433 (snapshot) => snapshot.snapshotSequence === HTTP_SEQUENCE,
1434 ),
1435 ),
1436 Stream.runHead,
1437 Effect.map(Option.getOrThrow),
1438 );
1439 expect(early.status).not.toBe("live");
1440 expect(early.generation).toBeGreaterThan(0);
1441 }
1442 expect(yield* Queue.size(h.subscriptions)).toBe(0);
1443
1444 if (expires) yield* TestClock.adjust("1 millis");
1445 yield* h.connect(config);
1446 const first = yield* Queue.take(h.subscriptions);
1447 expect(first.input).toMatchObject(subscribes);
1448 for (const key of [
1449 "afterSequence",
1450 "httpSnapshotFallback",
1451 "knownProjectIdentities",
1452 ] as const) {
1453 if (!(key in subscribes)) expect(first.input).not.toHaveProperty(key);
1454 }
1455 expect(yield* Queue.clear(h.loadOptions)).toEqual(loads.slice(1));
1456 expect(yield* Ref.get(h.httpLoads)).toBe(loads.length);
1457 // The socket sends the full snapshot these rows asked for; the rest needs a cursor.
1458 if (!("afterSequence" in subscribes)) return;
1459
1460 yield* Queue.offer(first.events, { kind: "synchronized" });
1461 yield* h.liveAt(subscribes.afterSequence);
1462 if (loads.length > 1) {
1463 yield* TestClock.adjust("500 millis");
1464 const saves = yield* Queue.takeAll(h.saved);
1465 expect(saves.at(-1)).toMatchObject({
1466 snapshot: { snapshotSequence: subscribes.afterSequence },
1467 origin: { ...CURRENT_ORIGIN, snapshotAt: NOW },
1468 });
1469 }
1470
1471 // A rotated credential keeps the session, so its events still apply and nothing reloads.
1472 yield* h.rotateCredential;
1473 yield* Queue.offer(first.events, {
1474 kind: "thread.removed",
1475 sequence: subscribes.afterSequence + 1,
1476 location: "active",
1477 threadId: ThreadId.make("no-such-thread"),
1478 });
1479 yield* h.liveAt(subscribes.afterSequence + 1);
1480 expect(yield* Ref.get(h.httpLoads)).toBe(loads.length);
1481 }),
1482 );
1483
1484 it.effect("a replaced session waits for the load its connection already started", () =>
1485 Effect.gen(function* () {
1486 const h = yield* makeColdStart(undefined, {
1487 serverResumes: false,
1488 holdLoads: "uninterruptible",
1489 });
1490 expect(yield* Queue.take(h.loadOptions)).toEqual(FAMILY_FLAGS);
1491 const claimed = yield* Deferred.make<void>();
1492 yield* h.connect(FAMILY_CONFIG, claimed);
1493 yield* Deferred.await(claimed);
1494 // Lets the first subscription run on from its config read to its wait for the load.
1495 yield* TestClock.adjust("1 millis");
1496 // The first subscription waits for the load. A second request could answer first, and
1497 // the older answer would then replace its rows under a cursor the socket has passed.
1498 yield* h.connect(FAMILY_CONFIG);
1499 yield* TestClock.adjust("1 millis");
1500 expect(yield* Queue.size(h.loadOptions)).toBe(0);
1501
1502 // The connection changes twice while the first load ends. The next session reads the
1503 // last connection before its load exists, so it replaces the load of the one between.
1504 yield* h.nextConnection;
1505 yield* TestClock.adjust("1 millis");
1506 yield* h.nextConnection;
1507 yield* h.connect(FAMILY_CONFIG);
1508 yield* TestClock.adjust("1 millis");
1509 yield* h.releaseLoads;
1510 expect(yield* Queue.take(h.loadOptions)).toEqual(FAMILY_FLAGS);
1511 yield* TestClock.adjust("1 millis");
1512 // That session is replaced while it waits for the load between to end. The load it then
1513 // starts must stay the one load of the last connection.
1514 yield* h.connect(FAMILY_CONFIG);
1515 yield* TestClock.adjust("1 millis");
1516 yield* h.releaseLoads;
1517 expect(yield* Queue.take(h.loadOptions)).toEqual(FAMILY_FLAGS);
1518 yield* TestClock.adjust("1 millis");
1519 expect(yield* Queue.size(h.loadOptions)).toBe(0);
1520
1521 yield* h.releaseLoads;
1522 const subscribed = yield* Queue.take(h.subscriptions);
1523 expect(subscribed.input).toMatchObject({ afterSequence: HTTP_SEQUENCE });
1524 expect(yield* Queue.size(h.loadOptions)).toBe(0);
1525 expect(yield* Ref.get(h.httpLoads)).toBe(1);
1526 }),
1527 );
1528
1529 it.effect("resumes held rows on the socket when the HTTP answer does not come", () =>
1530 Effect.gen(function* () {
1531 const h = yield* makeColdStart(
1532 { ...RECENT_ORIGIN, ...FAMILY_FLAGS },
1533 { serverResumes: false, holdLoads: true },
1534 );
1535 yield* Queue.take(h.loadOptions);
1536 const claimed = yield* Deferred.make<void>();
1537 yield* h.connect(FAMILY_CONFIG, claimed);
1538 yield* Deferred.await(claimed);
1539 // Lets the subscription run on from its config read to its wait for the load.
1540 yield* TestClock.adjust("1 millis");
1541 expect(yield* Queue.size(h.subscriptions)).toBe(0);
1542
1543 yield* TestClock.adjust("3 seconds");
1544 const first = yield* Queue.take(h.subscriptions);
1545 expect(first.input).toMatchObject({
1546 afterSequence: CACHED_SEQUENCE,
1547 httpSnapshotFallback: true,
1548 });
1549 yield* Queue.offer(first.events, {
1550 kind: "thread.removed",
1551 sequence: HTTP_SEQUENCE + 1,
1552 location: "active",
1553 threadId: ThreadId.make("no-such-thread"),
1554 });
1555 yield* Queue.offer(first.events, { kind: "synchronized" });
1556 yield* h.liveAt(HTTP_SEQUENCE + 1);
1557
1558 // The late answer is an older snapshot. It must not replace the rows the socket moved on.
1559 yield* h.releaseLoads;
1560 yield* TestClock.adjust("1 millis");
1561 expect(yield* Ref.get(h.httpLoads)).toBe(0);
1562 expect(yield* SubscriptionRef.get(h.shellState)).toMatchObject({
1563 status: "live",
1564 snapshot: Option.some({ snapshotSequence: HTTP_SEQUENCE + 1 }),
1565 });
1566 }),
1567 );
1568
1569 it.effect("drops the load of a connection that ended and waits for the next one", () =>
1570 Effect.gen(function* () {
1571 const h = yield* makeColdStart(undefined, { serverResumes: false, holdLoads: true });
1572 yield* Queue.take(h.loadOptions);
1573 yield* h.prepareAgain;
1574 yield* Queue.take(h.loadOptions);
1575
1576 // The socket is ready while the second load is in flight.
1577 yield* h.connect(FAMILY_CONFIG);
1578 yield* h.releaseLoads;
1579 const first = yield* Queue.take(h.subscriptions);
1580 expect(first.input).toMatchObject({
1581 afterSequence: HTTP_SEQUENCE,
1582 knownProjectIdentities: "identities-1",
1583 });
1584 // The first load ended with its connection and never answered.
1585 expect(yield* Ref.get(h.httpLoads)).toBe(1);
1586
1587 yield* Queue.offer(first.events, { kind: "synchronized" });
1588 yield* h.liveAt(HTTP_SEQUENCE);
1589 }),
1590 );
1591
1592 it.effect("keeps syncing row status after a shell stream defect", () =>
1593 Effect.gen(function* () {
1594 const h = yield* makeColdStart(RECENT_ORIGIN);
1595 const rowUpdate = (sequence: number, status: "completed" | "running") => ({
1596 kind: "thread.updated" as const,
1597 sequence,
1598 location: "active" as const,
1599 thread: { ...v2ThreadShell, status },
1600 });
1601 const liveRow = (sequence: number, status: "completed" | "running") =>
1602 SubscriptionRef.changes(h.shellState).pipe(
1603 Stream.filter(
1604 (state) =>
1605 state.status === "live" &&
1606 Option.exists(
1607 state.snapshot,
1608 (snapshot) =>
1609 snapshot.snapshotSequence === sequence &&
1610 snapshot.threads.some(
1611 (thread) => thread.id === v2ThreadShell.id && thread.status === status,
1612 ),
1613 ),
1614 ),
1615 Stream.runHead,
1616 Effect.map(Option.getOrThrow),
1617 );
1618
1619 const first = yield* Queue.take(h.subscriptions);
1620 yield* Queue.offerAll(first.events, [
1621 rowUpdate(CACHED_SEQUENCE + 1, "completed"),
1622 { kind: "synchronized" },
1623 ]);
1624 yield* liveRow(CACHED_SEQUENCE + 1, "completed");
1625
1626 const failWithDefect = (events: typeof first.events) =>
1627 Queue.failCause(events, Cause.die(new Error("shell invariant failed"))).pipe(
1628 Effect.andThen(
1629 SubscriptionRef.changes(h.shellState).pipe(
1630 Stream.filter((state) =>
1631 Option.exists(
1632 state.error,
1633 (error) => error === "Could not synchronize environment data.",
1634 ),
1635 ),
1636 Stream.runHead,
1637 ),
1638 ),
1639 );
1640
1641 yield* failWithDefect(first.events);
1642 yield* h.reconnect;
1643 const resumed = yield* Queue.take(h.subscriptions);
1644 expect(resumed.input).toMatchObject({ afterSequence: CACHED_SEQUENCE + 1 });
1645 yield* Queue.offerAll(resumed.events, [
1646 rowUpdate(CACHED_SEQUENCE + 2, "running"),
1647 { kind: "synchronized" },
1648 ]);
1649 expect((yield* liveRow(CACHED_SEQUENCE + 2, "running")).error).toEqual(Option.none());
1650
1651 // Without a reconnect or wakeup, the stream resubscribes once the backoff passes.
1652 yield* failWithDefect(resumed.events);
1653 yield* TestClock.adjust("250 millis");
1654 const retried = yield* Queue.take(h.subscriptions);
1655 expect(retried.input).toMatchObject({ afterSequence: CACHED_SEQUENCE + 2 });
1656 }),
1657 );
1658
1659 it.effect("reloads over HTTP when the server answers a resume with snapshot-required", () =>
1660 Effect.gen(function* () {
1661 const h = yield* makeColdStart(RECENT_ORIGIN);
1662
1663 const coldResume = yield* Queue.take(h.subscriptions);
1664 expect(coldResume.input).toMatchObject({ afterSequence: CACHED_SEQUENCE });
1665 yield* Queue.offer(coldResume.events, { kind: "snapshot-required" });
1666 const coldReload = yield* Queue.take(h.subscriptions);
1667 expect(coldReload.input).toMatchObject({ afterSequence: HTTP_SEQUENCE });
1668 expect(coldReload.input).not.toHaveProperty("httpSnapshotFallback");
1669 expect(yield* Ref.get(h.httpLoads)).toBe(1);
1670 yield* Queue.offer(coldReload.events, { kind: "synchronized" });
1671 expect((yield* h.liveAt(HTTP_SEQUENCE)).generation).toBeGreaterThan(0);
1672
1673 // A warm reconnect resumes the in-memory shell and falls back the same way.
1674 yield* h.reconnect;
1675 const warmResume = yield* Queue.take(h.subscriptions);
1676 expect(warmResume.input).toMatchObject({
1677 afterSequence: HTTP_SEQUENCE,
1678 httpSnapshotFallback: true,
1679 });
1680 expect(yield* Ref.get(h.httpLoads)).toBe(1);
1681 yield* Queue.offer(warmResume.events, { kind: "snapshot-required" });
1682 const warmReload = yield* Queue.take(h.subscriptions);
1683 expect(warmReload.input).toMatchObject({ afterSequence: HTTP_SEQUENCE + 1 });
1684 expect(yield* Ref.get(h.httpLoads)).toBe(2);
1685 yield* Queue.offer(warmReload.events, { kind: "synchronized" });
1686 yield* h.liveAt(HTTP_SEQUENCE + 1);
1687 }),
1688 );
1689 });