import { EnvironmentId, ORCHESTRATION_V2_WS_METHODS, type OrchestrationV2ShellSnapshot, type OrchestrationV2ShellStreamItem, ThreadId, type ThreadPullRequestLink, } from "@t3tools/contracts"; import { describe, expect, it } from "@effect/vitest"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; import * as Fiber from "effect/Fiber"; import * as Option from "effect/Option"; import * as Queue from "effect/Queue"; import * as Ref from "effect/Ref"; import * as Scope from "effect/Scope"; import * as Stream from "effect/Stream"; import * as SubscriptionRef from "effect/SubscriptionRef"; import * as TestClock from "effect/testing/TestClock"; import { AVAILABLE_CONNECTION_STATE, PrimaryConnectionTarget, type PreparedConnection, } from "../connection/model.ts"; import * as EnvironmentSupervisor from "../connection/supervisor.ts"; import * as ConnectionWakeups from "../connection/wakeups.ts"; import * as Persistence from "../platform/persistence.ts"; import * as RpcSession from "../rpc/session.ts"; import type { WsRpcProtocolClient } from "../rpc/protocol.ts"; import { makeEnvironmentShellState } from "./shell.ts"; import * as ShellSnapshotLoader from "./shellSnapshotHttp.ts"; import { v2Project, v2ShellSnapshot, v2ThreadShell } from "./orchestrationV2TestFixtures.ts"; const TARGET = new PrimaryConnectionTarget({ environmentId: EnvironmentId.make("environment-1"), label: "Test environment", httpBaseUrl: "https://environment.example.test", wsBaseUrl: "wss://environment.example.test", }); const PREPARED: PreparedConnection = { environmentId: TARGET.environmentId, label: TARGET.label, httpBaseUrl: TARGET.httpBaseUrl, socketUrl: TARGET.wsBaseUrl, httpAuthorization: null, target: TARGET, }; const LIVE_SHELL_SNAPSHOT: OrchestrationV2ShellSnapshot = { ...v2ShellSnapshot, snapshotSequence: 1, }; function session(client: WsRpcProtocolClient): RpcSession.RpcSession { return { client, initialConfig: Effect.succeed({ shellResumeCompletionMarker: true } as never), subscribeServerConfig: (input) => client.subscribeServerConfig(input), ready: Effect.void, probe: Effect.void, closed: Effect.never, }; } describe("environment shell synchronization", () => { it.effect("publishes live state before persistence and preserves it when ready", () => Effect.gen(function* () { const events = yield* Queue.unbounded(); const client = { [ORCHESTRATION_V2_WS_METHODS.subscribeShell]: () => Stream.fromQueue(events), } as unknown as WsRpcProtocolClient; const supervisorState = yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE); const activeSession = yield* SubscriptionRef.make>( Option.some(session(client)), ); const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ target: TARGET, state: supervisorState, session: activeSession, prepared: yield* SubscriptionRef.make(Option.some(PREPARED)), connect: Effect.void, disconnect: Effect.void, retryNow: Effect.void, } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]); const cache = Persistence.EnvironmentCacheStore.of({ loadShell: () => Effect.succeed(Option.none()), saveShell: () => Effect.void, loadThread: () => Effect.succeed(Option.none()), saveThread: () => Effect.void, removeThread: () => Effect.void, loadServerConfig: () => Effect.succeedNone, saveServerConfig: () => Effect.void, loadVcsRefs: () => Effect.succeedNone, saveVcsRefs: () => Effect.void, removeVcsRefs: () => Effect.void, clearVcsRefs: () => Effect.void, clear: () => Effect.void, }); // Cold cache with no HTTP snapshot available → falls back to the // socket-embedded snapshot. const snapshotLoader = ShellSnapshotLoader.ShellSnapshotLoader.of({ load: () => Effect.succeedNone, }); const shellState = yield* makeEnvironmentShellState().pipe( Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), Effect.provideService(Persistence.EnvironmentCacheStore, cache), Effect.provideService(ShellSnapshotLoader.ShellSnapshotLoader, snapshotLoader), ); yield* SubscriptionRef.set(supervisorState, { desired: true, network: "online", phase: "connecting", stage: "synchronizing", attempt: 1, generation: 0, lastFailure: null, retryAt: null, }); yield* Queue.offer(events, { kind: "snapshot", snapshot: LIVE_SHELL_SNAPSHOT, }); const synchronizing = yield* SubscriptionRef.changes(shellState).pipe( Stream.filter((state) => state.status === "synchronizing" && Option.isSome(state.snapshot)), Stream.runHead, ); expect(Option.getOrThrow(Option.getOrThrow(synchronizing).snapshot)).toEqual( LIVE_SHELL_SNAPSHOT, ); yield* Queue.offer(events, { kind: "synchronized" }); yield* SubscriptionRef.changes(shellState).pipe( Stream.filter((state) => state.status === "live"), Stream.runHead, ); yield* SubscriptionRef.set(supervisorState, { desired: true, network: "online", phase: "connected", stage: null, attempt: 1, generation: 1, lastFailure: null, retryAt: null, }); for (let index = 0; index < 10; index += 1) { yield* Effect.yieldNow; } const state = yield* SubscriptionRef.get(shellState); expect(state.status).toBe("live"); expect(Option.getOrThrow(state.snapshot)).toEqual(LIVE_SHELL_SNAPSHOT); }), ); it.live.each([ { bufferSize: Infinity, expectedSequences: [51] }, // RpcClient defaults to a 16-event buffer, which splits larger server chunks. { bufferSize: 16, expectedSequences: [17, 33, 49, 51] }, ])("batches live events with a $bufferSize event buffer", ({ bufferSize, expectedSequences }) => Effect.gen(function* () { const events = yield* Queue.bounded(bufferSize); const batchSnapshot = { ...LIVE_SHELL_SNAPSHOT, threads: [] }; const client = { [ORCHESTRATION_V2_WS_METHODS.subscribeShell]: () => Stream.fromQueue(events), } as unknown as WsRpcProtocolClient; const supervisorState = yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE); const activeSession = yield* SubscriptionRef.make>( Option.some(session(client)), ); const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ target: TARGET, state: supervisorState, session: activeSession, prepared: yield* SubscriptionRef.make(Option.some(PREPARED)), connect: Effect.void, disconnect: Effect.void, retryNow: Effect.void, } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]); const cache = Persistence.EnvironmentCacheStore.of({ loadShell: () => Effect.succeed(Option.none()), saveShell: () => Effect.void, loadThread: () => Effect.succeed(Option.none()), saveThread: () => Effect.void, removeThread: () => Effect.void, loadServerConfig: () => Effect.succeed(Option.none()), saveServerConfig: () => Effect.void, loadVcsRefs: () => Effect.succeed(Option.none()), saveVcsRefs: () => Effect.void, removeVcsRefs: () => Effect.void, clearVcsRefs: () => Effect.void, clear: () => Effect.void, }); const shellState = yield* makeEnvironmentShellState().pipe( Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), Effect.provideService(Persistence.EnvironmentCacheStore, cache), Effect.provideService( ShellSnapshotLoader.ShellSnapshotLoader, ShellSnapshotLoader.ShellSnapshotLoader.of({ load: () => Effect.succeed(Option.none()) }), ), ); yield* SubscriptionRef.set(supervisorState, { desired: true, network: "online", phase: "connected", stage: null, attempt: 1, generation: 1, lastFailure: null, retryAt: null, }); yield* Queue.offer(events, { kind: "snapshot", snapshot: batchSnapshot }); yield* Queue.offer(events, { kind: "synchronized" }); yield* SubscriptionRef.changes(shellState).pipe( Stream.filter((state) => state.status === "live"), Stream.runHead, ); // Observe before publishing so no batch can arrive before the subscription. const observed = yield* SubscriptionRef.changes(shellState).pipe( Stream.drop(1), Stream.takeUntil( (state) => Option.isSome(state.snapshot) && state.snapshot.value.threads.length === 50, ), Stream.runCollect, Effect.forkScoped({ startImmediately: true }), ); yield* Queue.offerAll( events, Array.from({ length: 50 }, (_, index) => ({ kind: "thread.updated" as const, sequence: 2 + index, location: "active" as const, thread: { ...v2ShellSnapshot.threads[0]!, id: `thread-${index}` } as never, })), ); const states = yield* Fiber.join(observed); const snapshots = states.map((state) => Option.getOrThrow(state.snapshot)); expect(snapshots.map((snapshot) => snapshot.snapshotSequence)).toEqual(expectedSequences); expect(snapshots.at(-1)!.threads.map((thread) => thread.id)).toEqual( Array.from({ length: 50 }, (_, index) => `thread-${index}`), ); }).pipe(Effect.scoped), ); it.effect("refreshes a warm shell cache from HTTP before resuming", () => Effect.gen(function* () { const cachedSnapshot: OrchestrationV2ShellSnapshot = { ...v2ShellSnapshot, snapshotSequence: 5, }; const events = yield* Queue.unbounded(); const capturedAfterSequence = yield* SubscriptionRef.make(undefined); const capturedCompletionMarker = yield* Ref.make(false); const loaderCalls = yield* SubscriptionRef.make(0); const httpSnapshot: OrchestrationV2ShellSnapshot = { ...v2ShellSnapshot, snapshotSequence: 9, }; const client = { [ORCHESTRATION_V2_WS_METHODS.subscribeShell]: (input: { readonly afterSequence?: number; readonly requestCompletionMarker?: true; }) => Stream.unwrap( Effect.all([ Ref.set(capturedCompletionMarker, input.requestCompletionMarker === true), SubscriptionRef.set(capturedAfterSequence, input.afterSequence), ]).pipe(Effect.as(Stream.fromQueue(events))), ), } as unknown as WsRpcProtocolClient; const supervisorState = yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE); const activeSession = yield* SubscriptionRef.make>( Option.some(session(client)), ); const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ target: TARGET, state: supervisorState, session: activeSession, prepared: yield* SubscriptionRef.make(Option.some(PREPARED)), connect: Effect.void, disconnect: Effect.void, retryNow: Effect.void, } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]); const cache = Persistence.EnvironmentCacheStore.of({ loadShell: () => Effect.succeedSome(cachedSnapshot), saveShell: () => Effect.void, loadThread: () => Effect.succeedNone, saveThread: () => Effect.void, removeThread: () => Effect.void, loadServerConfig: () => Effect.succeedNone, saveServerConfig: () => Effect.void, loadVcsRefs: () => Effect.succeedNone, saveVcsRefs: () => Effect.void, removeVcsRefs: () => Effect.void, clearVcsRefs: () => Effect.void, clear: () => Effect.void, }); const snapshotLoader = ShellSnapshotLoader.ShellSnapshotLoader.of({ load: () => SubscriptionRef.update(loaderCalls, (count) => count + 1).pipe( Effect.as(Option.some(httpSnapshot)), ), }); const shellState = yield* makeEnvironmentShellState().pipe( Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), Effect.provideService(Persistence.EnvironmentCacheStore, cache), Effect.provideService(ShellSnapshotLoader.ShellSnapshotLoader, snapshotLoader), ); // Wait until the subscription is established from the warm cache. yield* SubscriptionRef.changes(capturedAfterSequence).pipe( Stream.filter((value) => value !== undefined), Stream.runHead, ); expect(yield* SubscriptionRef.get(capturedAfterSequence)).toBe(9); expect(yield* Ref.get(capturedCompletionMarker)).toBe(true); expect(yield* SubscriptionRef.get(loaderCalls)).toBe(1); const synchronizing = yield* SubscriptionRef.get(shellState); expect(synchronizing.status).toBe("synchronizing"); expect(Option.getOrThrow(synchronizing.snapshot)).toEqual(httpSnapshot); yield* Queue.offer(events, { kind: "synchronized" }); yield* SubscriptionRef.changes(shellState).pipe( Stream.filter((value) => value.status === "live"), Stream.runHead, ); }), ); it.effect("resubscribes from the in-memory shell cursor when the app becomes active", () => Effect.gen(function* () { const events = yield* Queue.unbounded(); const wakeups = yield* Queue.unbounded(); const loaderCalls = yield* Ref.make(0); const capturedAfterSequences = yield* Ref.make>([]); const client = { [ORCHESTRATION_V2_WS_METHODS.subscribeShell]: (input: { readonly afterSequence?: number; }) => Stream.unwrap( Ref.update(capturedAfterSequences, (captured) => [ ...captured, input.afterSequence, ]).pipe(Effect.as(Stream.fromQueue(events))), ), } as unknown as WsRpcProtocolClient; const supervisorState = yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE); const activeSession = yield* SubscriptionRef.make>( Option.some(session(client)), ); const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ target: TARGET, state: supervisorState, session: activeSession, prepared: yield* SubscriptionRef.make(Option.some(PREPARED)), connect: Effect.void, disconnect: Effect.void, retryNow: Effect.void, } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]); const cache = Persistence.EnvironmentCacheStore.of({ loadShell: () => Effect.succeedSome(LIVE_SHELL_SNAPSHOT), saveShell: () => Effect.void, loadThread: () => Effect.succeedNone, saveThread: () => Effect.void, removeThread: () => Effect.void, loadServerConfig: () => Effect.succeedNone, saveServerConfig: () => Effect.void, loadVcsRefs: () => Effect.succeedNone, saveVcsRefs: () => Effect.void, removeVcsRefs: () => Effect.void, clearVcsRefs: () => Effect.void, clear: () => Effect.void, }); const snapshotLoader = ShellSnapshotLoader.ShellSnapshotLoader.of({ load: () => Ref.updateAndGet(loaderCalls, (count) => count + 1).pipe( Effect.map((count) => Option.some({ ...LIVE_SHELL_SNAPSHOT, snapshotSequence: count * 10 }), ), ), }); const shellState = yield* makeEnvironmentShellState().pipe( Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), Effect.provideService(Persistence.EnvironmentCacheStore, cache), Effect.provideService(ShellSnapshotLoader.ShellSnapshotLoader, snapshotLoader), Effect.provideService( ConnectionWakeups.ConnectionWakeups, ConnectionWakeups.ConnectionWakeups.of({ changes: Stream.fromQueue(wakeups) }), ), ); // A new session starts from an authoritative HTTP snapshot. for (let attempt = 0; attempt < 100; attempt += 1) { if ((yield* Ref.get(capturedAfterSequences)).length >= 1) break; yield* Effect.yieldNow; } expect(yield* Ref.get(capturedAfterSequences)).toEqual([10]); yield* Queue.offer(events, { kind: "synchronized" }); yield* SubscriptionRef.changes(shellState).pipe( Stream.filter((value) => value.status === "live"), Stream.runHead, ); // A newer snapshot arrives on the stream and advances the cursor. yield* Queue.offer(events, { kind: "snapshot", snapshot: { ...LIVE_SHELL_SNAPSHOT, snapshotSequence: 40 }, }); yield* SubscriptionRef.changes(shellState).pipe( Stream.filter( (value) => Option.isSome(value.snapshot) && value.snapshot.value.snapshotSequence === 40, ), Stream.runHead, ); yield* Queue.offer(wakeups, "application-active"); for (let attempt = 0; attempt < 100; attempt += 1) { if ((yield* Ref.get(capturedAfterSequences)).length >= 2) break; yield* Effect.yieldNow; } expect(yield* Ref.get(capturedAfterSequences)).toEqual([10, 40]); yield* Queue.offer(events, { kind: "synchronized" }); yield* Queue.offer(wakeups, "application-active-probe"); for (let attempt = 0; attempt < 100; attempt += 1) { if ((yield* Ref.get(capturedAfterSequences)).length >= 3) break; yield* Effect.yieldNow; } expect(yield* Ref.get(capturedAfterSequences)).toEqual([10, 40, 40]); yield* Queue.offer(wakeups, "application-active-reconnect"); for (let attempt = 0; attempt < 10; attempt += 1) { yield* Effect.yieldNow; } expect((yield* Ref.get(capturedAfterSequences)).length).toBe(3); expect(yield* Ref.get(loaderCalls)).toBe(1); // Replacing the session performs another authoritative refresh. yield* SubscriptionRef.set(activeSession, Option.some(session(client))); for (let attempt = 0; attempt < 100; attempt += 1) { if ((yield* Ref.get(capturedAfterSequences)).length >= 4) break; yield* Effect.yieldNow; } expect(yield* Ref.get(capturedAfterSequences)).toEqual([10, 40, 40, 20]); expect(yield* Ref.get(loaderCalls)).toBe(2); }), ); it.effect("throttles shell writes and flushes the latest snapshot on disconnect", () => Effect.gen(function* () { const events = yield* Queue.unbounded(); const saved = yield* Ref.make>([]); const client = { [ORCHESTRATION_V2_WS_METHODS.subscribeShell]: () => Stream.fromQueue(events), } as unknown as WsRpcProtocolClient; const supervisorState = yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE); const activeSession = yield* SubscriptionRef.make>( Option.some(session(client)), ); const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ target: TARGET, state: supervisorState, session: activeSession, prepared: yield* SubscriptionRef.make(Option.some(PREPARED)), connect: Effect.void, disconnect: Effect.void, retryNow: Effect.void, } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]); const cache = Persistence.EnvironmentCacheStore.of({ loadShell: () => Effect.succeed(Option.none()), saveShell: (_environmentId, snapshot) => Ref.update(saved, (values) => [...values, snapshot]), loadThread: () => Effect.succeed(Option.none()), saveThread: () => Effect.void, removeThread: () => Effect.void, loadServerConfig: () => Effect.succeed(Option.none()), saveServerConfig: () => Effect.void, loadVcsRefs: () => Effect.succeed(Option.none()), saveVcsRefs: () => Effect.void, removeVcsRefs: () => Effect.void, clearVcsRefs: () => Effect.void, clear: () => Effect.void, }); const snapshotLoader = ShellSnapshotLoader.ShellSnapshotLoader.of({ load: () => Effect.succeed(Option.none()), }); const shellState = yield* makeEnvironmentShellState().pipe( Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), Effect.provideService(Persistence.EnvironmentCacheStore, cache), Effect.provideService(ShellSnapshotLoader.ShellSnapshotLoader, snapshotLoader), ); const sendSnapshot = Effect.fn(function* (sequence: number) { yield* Queue.offer(events, { kind: "snapshot", snapshot: { ...LIVE_SHELL_SNAPSHOT, snapshotSequence: sequence }, }); yield* SubscriptionRef.changes(shellState).pipe( Stream.filter( (state) => Option.isSome(state.snapshot) && state.snapshot.value.snapshotSequence === sequence, ), Stream.runHead, ); }); yield* sendSnapshot(1); yield* TestClock.adjust("500 millis"); for (let sequence = 2; sequence <= 10; sequence++) { yield* sendSnapshot(sequence); yield* TestClock.adjust("1 second"); } expect((yield* Ref.get(saved)).map((snapshot) => snapshot.snapshotSequence)).toEqual([1]); yield* TestClock.adjust("1 second"); expect((yield* Ref.get(saved)).map((snapshot) => snapshot.snapshotSequence)).toEqual([1, 10]); yield* sendSnapshot(11); yield* SubscriptionRef.set(supervisorState, { ...AVAILABLE_CONNECTION_STATE, desired: false, }); yield* SubscriptionRef.changes(shellState).pipe( Stream.filter((state) => state.status === "cached"), Stream.runHead, ); // The disconnect flush runs independently from the connection-state update. yield* TestClock.adjust("0 millis"); expect((yield* Ref.get(saved)).map((snapshot) => snapshot.snapshotSequence)).toEqual([ 1, 10, 11, ]); }), ); it.effect("flushes the latest live snapshot without blocking connection state updates", () => Effect.gen(function* () { const events = yield* Queue.unbounded(); const saved = yield* Ref.make>([]); const saveStarted = yield* Deferred.make(); const releaseSave = yield* Deferred.make(); const client = { [ORCHESTRATION_V2_WS_METHODS.subscribeShell]: () => Stream.fromQueue(events), } as unknown as WsRpcProtocolClient; const supervisorState = yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE); const activeSession = yield* SubscriptionRef.make>( Option.some(session(client)), ); const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ target: TARGET, state: supervisorState, session: activeSession, prepared: yield* SubscriptionRef.make(Option.some(PREPARED)), connect: Effect.void, disconnect: Effect.void, retryNow: Effect.void, } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]); const cache = Persistence.EnvironmentCacheStore.of({ loadShell: () => Effect.succeed(Option.none()), saveShell: (_environmentId, snapshot) => Deferred.succeed(saveStarted, undefined).pipe( Effect.andThen(Deferred.await(releaseSave)), Effect.andThen(Ref.update(saved, (values) => [...values, snapshot])), ), loadThread: () => Effect.succeed(Option.none()), saveThread: () => Effect.void, removeThread: () => Effect.void, loadServerConfig: () => Effect.succeed(Option.none()), saveServerConfig: () => Effect.void, loadVcsRefs: () => Effect.succeed(Option.none()), saveVcsRefs: () => Effect.void, removeVcsRefs: () => Effect.void, clearVcsRefs: () => Effect.void, clear: () => Effect.void, }); const snapshotLoader = ShellSnapshotLoader.ShellSnapshotLoader.of({ load: () => Effect.succeed(Option.none()), }); const shellState = yield* makeEnvironmentShellState().pipe( Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), Effect.provideService(Persistence.EnvironmentCacheStore, cache), Effect.provideService(ShellSnapshotLoader.ShellSnapshotLoader, snapshotLoader), ); yield* SubscriptionRef.set(supervisorState, { desired: true, network: "online", phase: "connecting", stage: "synchronizing", attempt: 1, generation: 0, lastFailure: null, retryAt: null, }); yield* Queue.offer(events, { kind: "snapshot", snapshot: LIVE_SHELL_SNAPSHOT, }); yield* Queue.offer(events, { kind: "synchronized" }); yield* SubscriptionRef.changes(shellState).pipe( Stream.filter((state) => state.status === "live"), Stream.runHead, ); yield* SubscriptionRef.set(supervisorState, { desired: true, network: "online", phase: "connecting", stage: "synchronizing", attempt: 2, generation: 1, lastFailure: null, retryAt: null, }); yield* SubscriptionRef.set(supervisorState, { desired: false, network: "online", phase: "available", stage: null, attempt: 2, generation: 2, lastFailure: null, retryAt: null, }); yield* Deferred.await(saveStarted); yield* SubscriptionRef.set(supervisorState, { desired: true, network: "online", phase: "connecting", stage: "synchronizing", attempt: 3, generation: 3, lastFailure: null, retryAt: null, }); for (let index = 0; index < 100; index += 1) { if ((yield* SubscriptionRef.get(shellState)).status === "synchronizing") break; yield* Effect.yieldNow; } expect((yield* SubscriptionRef.get(shellState)).status).toBe("synchronizing"); expect(yield* Ref.get(saved)).toEqual([]); yield* Deferred.succeed(releaseSave, undefined); for (let index = 0; index < 100; index += 1) { if ((yield* Ref.get(saved)).length > 0) break; yield* Effect.yieldNow; } expect(yield* Ref.get(saved)).toEqual([LIVE_SHELL_SNAPSHOT]); }), ); it.effect("re-persists same-sequence enrichment when an older in-flight save completes", () => Effect.gen(function* () { const repositoryIdentity = { canonicalKey: "github.com/example/repo", locator: { source: "git-remote" as const, remoteName: "origin", remoteUrl: "https://github.com/example/repo.git", }, }; const unenrichedSnapshot: OrchestrationV2ShellSnapshot = { ...LIVE_SHELL_SNAPSHOT, snapshotSequence: 1, projects: [{ ...v2Project, repositoryIdentity: null }], }; const enrichedSnapshot: OrchestrationV2ShellSnapshot = { ...LIVE_SHELL_SNAPSHOT, snapshotSequence: 1, projects: [{ ...v2Project, repositoryIdentity }], }; const events = yield* Queue.unbounded(); const saved = yield* Ref.make>([]); const unenrichedSaveStarted = yield* Deferred.make(); const releaseUnenrichedSave = yield* Deferred.make(); const client = { [ORCHESTRATION_V2_WS_METHODS.subscribeShell]: () => Stream.fromQueue(events), } as unknown as WsRpcProtocolClient; const supervisorState = yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE); const activeSession = yield* SubscriptionRef.make>( Option.some(session(client)), ); const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ target: TARGET, state: supervisorState, session: activeSession, prepared: yield* SubscriptionRef.make(Option.some(PREPARED)), connect: Effect.void, disconnect: Effect.void, retryNow: Effect.void, } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]); const cache = Persistence.EnvironmentCacheStore.of({ loadShell: () => Effect.succeed(Option.none()), saveShell: (_environmentId, snapshot) => Effect.gen(function* () { if (snapshot.projects[0]?.repositoryIdentity == null) { yield* Deferred.succeed(unenrichedSaveStarted, undefined); yield* Deferred.await(releaseUnenrichedSave); } yield* Ref.update(saved, (values) => [...values, snapshot]); }), loadThread: () => Effect.succeed(Option.none()), saveThread: () => Effect.void, removeThread: () => Effect.void, loadServerConfig: () => Effect.succeed(Option.none()), saveServerConfig: () => Effect.void, loadVcsRefs: () => Effect.succeed(Option.none()), saveVcsRefs: () => Effect.void, removeVcsRefs: () => Effect.void, clearVcsRefs: () => Effect.void, clear: () => Effect.void, }); const snapshotLoader = ShellSnapshotLoader.ShellSnapshotLoader.of({ load: () => Effect.succeed(Option.none()), }); const shellState = yield* makeEnvironmentShellState().pipe( Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), Effect.provideService(Persistence.EnvironmentCacheStore, cache), Effect.provideService(ShellSnapshotLoader.ShellSnapshotLoader, snapshotLoader), ); yield* SubscriptionRef.set(supervisorState, { desired: true, network: "online", phase: "connecting", stage: "synchronizing", attempt: 1, generation: 0, lastFailure: null, retryAt: null, }); yield* Queue.offer(events, { kind: "snapshot", snapshot: unenrichedSnapshot, }); yield* Queue.offer(events, { kind: "synchronized" }); yield* SubscriptionRef.changes(shellState).pipe( Stream.filter((state) => state.status === "live"), Stream.runHead, ); // Disconnect flush starts an immediate persist of the unenriched snapshot // (debounced path needs TestClock advancement; flush does not). yield* SubscriptionRef.set(supervisorState, { desired: false, network: "online", phase: "available", stage: null, attempt: 1, generation: 1, lastFailure: null, retryAt: null, }); yield* Deferred.await(unenrichedSaveStarted); // Same sequence, different content. Session stays open so the stream can // still apply enrichment while the older save is in flight. yield* Queue.offer(events, { kind: "snapshot", snapshot: enrichedSnapshot, }); yield* SubscriptionRef.changes(shellState).pipe( Stream.filter( (state) => Option.isSome(state.snapshot) && state.snapshot.value.projects[0]?.repositoryIdentity != null, ), Stream.runHead, ); // Completing the older same-sequence save must re-persist the latest object. yield* Deferred.succeed(releaseUnenrichedSave, undefined); for (let index = 0; index < 100; index += 1) { const values = yield* Ref.get(saved); if (values.length > 0 && values.at(-1)?.projects[0]?.repositoryIdentity != null) { break; } yield* Effect.yieldNow; } const finalSaved = (yield* Ref.get(saved)).at(-1); expect(finalSaved?.snapshotSequence).toBe(1); expect(finalSaved?.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity); }), ); it.effect("scope teardown flush stays stable while a save is blocked", () => Effect.gen(function* () { const lateSnapshot: OrchestrationV2ShellSnapshot = { ...LIVE_SHELL_SNAPSHOT, snapshotSequence: 2, projects: [{ ...v2Project, title: "Late stream event" }], }; const events = yield* Queue.unbounded(); const saved = yield* Ref.make>([]); const saveStarted = yield* Deferred.make(); const releaseSave = yield* Deferred.make(); const client = { [ORCHESTRATION_V2_WS_METHODS.subscribeShell]: () => Stream.fromQueue(events), } as unknown as WsRpcProtocolClient; const supervisorState = yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE); const activeSession = yield* SubscriptionRef.make>( Option.some(session(client)), ); const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ target: TARGET, state: supervisorState, session: activeSession, prepared: yield* SubscriptionRef.make(Option.some(PREPARED)), connect: Effect.void, disconnect: Effect.void, retryNow: Effect.void, } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]); const cache = Persistence.EnvironmentCacheStore.of({ loadShell: () => Effect.succeed(Option.none()), saveShell: (_environmentId, snapshot) => Deferred.succeed(saveStarted, undefined).pipe( Effect.andThen(Deferred.await(releaseSave)), Effect.andThen(Ref.update(saved, (values) => [...values, snapshot])), ), loadThread: () => Effect.succeed(Option.none()), saveThread: () => Effect.void, removeThread: () => Effect.void, loadServerConfig: () => Effect.succeed(Option.none()), saveServerConfig: () => Effect.void, loadVcsRefs: () => Effect.succeed(Option.none()), saveVcsRefs: () => Effect.void, removeVcsRefs: () => Effect.void, clearVcsRefs: () => Effect.void, clear: () => Effect.void, }); const snapshotLoader = ShellSnapshotLoader.ShellSnapshotLoader.of({ load: () => Effect.succeed(Option.none()), }); const scope = yield* Scope.make(); const shellState = yield* makeEnvironmentShellState().pipe( Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), Effect.provideService(Persistence.EnvironmentCacheStore, cache), Effect.provideService(ShellSnapshotLoader.ShellSnapshotLoader, snapshotLoader), Scope.provide(scope), ); yield* SubscriptionRef.set(supervisorState, { desired: true, network: "online", phase: "connecting", stage: "synchronizing", attempt: 1, generation: 0, lastFailure: null, retryAt: null, }); yield* Queue.offer(events, { kind: "snapshot", snapshot: LIVE_SHELL_SNAPSHOT, }); yield* Queue.offer(events, { kind: "synchronized" }); yield* SubscriptionRef.changes(shellState).pipe( Stream.filter((state) => state.status === "live"), Stream.runHead, ); // Close scope so the finalizer flush blocks on save. Workers must be // interrupted before that flush; late stream events must not extend it. const closeFiber = yield* Effect.forkChild(Scope.close(scope, Exit.void)); yield* Deferred.await(saveStarted); yield* Queue.offer(events, { kind: "snapshot", snapshot: lateSnapshot, }); for (let index = 0; index < 50; index += 1) { yield* Effect.yieldNow; } yield* Deferred.succeed(releaseSave, undefined); yield* Fiber.join(closeFiber); const values = yield* Ref.get(saved); expect(values.length).toBeGreaterThan(0); expect(values.every((snapshot) => snapshot.snapshotSequence === 1)).toBe(true); expect(values.every((snapshot) => snapshot.projects[0]?.title !== "Late stream event")).toBe( true, ); }), ); it.effect("applies authoritative lower-sequence HTTP resets over client-ahead cache", () => Effect.gen(function* () { const cachedSnapshot: OrchestrationV2ShellSnapshot = { ...v2ShellSnapshot, snapshotSequence: 20, projects: [{ ...v2Project, title: "Client ahead" }], }; const httpSnapshot: OrchestrationV2ShellSnapshot = { ...v2ShellSnapshot, snapshotSequence: 4, projects: [{ ...v2Project, title: "Server reset" }], }; const events = yield* Queue.unbounded(); const client = { [ORCHESTRATION_V2_WS_METHODS.subscribeShell]: () => Stream.fromQueue(events), } as unknown as WsRpcProtocolClient; const supervisorState = yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE); const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ target: TARGET, state: supervisorState, session: yield* SubscriptionRef.make(Option.some(session(client))), prepared: yield* SubscriptionRef.make(Option.some(PREPARED)), connect: Effect.void, disconnect: Effect.void, retryNow: Effect.void, } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]); const cache = Persistence.EnvironmentCacheStore.of({ loadShell: () => Effect.succeed(Option.some(cachedSnapshot)), saveShell: () => Effect.void, loadThread: () => Effect.succeed(Option.none()), saveThread: () => Effect.void, removeThread: () => Effect.void, loadServerConfig: () => Effect.succeed(Option.none()), saveServerConfig: () => Effect.void, loadVcsRefs: () => Effect.succeed(Option.none()), saveVcsRefs: () => Effect.void, removeVcsRefs: () => Effect.void, clearVcsRefs: () => Effect.void, clear: () => Effect.void, }); const snapshotLoader = ShellSnapshotLoader.ShellSnapshotLoader.of({ load: () => Effect.succeed(Option.some(httpSnapshot)), }); const shellState = yield* makeEnvironmentShellState().pipe( Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), Effect.provideService(Persistence.EnvironmentCacheStore, cache), Effect.provideService(ShellSnapshotLoader.ShellSnapshotLoader, snapshotLoader), ); yield* SubscriptionRef.changes(shellState).pipe( Stream.filter( (state) => Option.isSome(state.snapshot) && state.snapshot.value.snapshotSequence === 4, ), Stream.runHead, ); const state = yield* SubscriptionRef.get(shellState); expect(Option.getOrThrow(state.snapshot).snapshotSequence).toBe(4); expect(Option.getOrThrow(state.snapshot).projects[0]?.title).toBe("Server reset"); }), ); it.effect("fills deferred cached pull request links without overwriting newer rows", () => Effect.gen(function* () { const link = (number: number): ThreadPullRequestLink => ({ host: "github.com", repository: "pingdotgg/t3code", number, url: `https://github.com/pingdotgg/t3code/pull/${number}`, source: "agent", linkedAt: "2026-06-20T00:00:00.000Z", snapshot: null, stack: null, }); const otherThreadId = ThreadId.make("thread-other"); const cachedSnapshot: OrchestrationV2ShellSnapshot = { ...v2ShellSnapshot, snapshotSequence: 5, threads: [v2ThreadShell, { ...v2ThreadShell, id: otherThreadId }], }; const linksReady = yield* Deferred.make(); const events = yield* Queue.unbounded(); const client = { [ORCHESTRATION_V2_WS_METHODS.subscribeShell]: () => Stream.fromQueue(events), } as unknown as WsRpcProtocolClient; const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ target: TARGET, state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE), session: yield* SubscriptionRef.make(Option.some(session(client))), prepared: yield* SubscriptionRef.make(Option.some(PREPARED)), connect: Effect.void, disconnect: Effect.void, retryNow: Effect.void, } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]); const cache = Persistence.EnvironmentCacheStore.of({ loadShell: () => Effect.succeedSome({ ...cachedSnapshot, loadPullRequests: Deferred.await(linksReady).pipe( Effect.as( new Map([ [v2ThreadShell.id, [link(1)]], [otherThreadId, [link(2)]], ]), ), ), }), saveShell: () => Effect.void, loadThread: () => Effect.succeedNone, saveThread: () => Effect.void, removeThread: () => Effect.void, loadServerConfig: () => Effect.succeedNone, saveServerConfig: () => Effect.void, loadVcsRefs: () => Effect.succeedNone, saveVcsRefs: () => Effect.void, removeVcsRefs: () => Effect.void, clearVcsRefs: () => Effect.void, clear: () => Effect.void, }); // No HTTP snapshot, so the cached rows stay until the socket sends deltas. const snapshotLoader = ShellSnapshotLoader.ShellSnapshotLoader.of({ load: () => Effect.succeedNone, }); const shellState = yield* makeEnvironmentShellState().pipe( Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), Effect.provideService(Persistence.EnvironmentCacheStore, cache), Effect.provideService(ShellSnapshotLoader.ShellSnapshotLoader, snapshotLoader), ); const threadsOf = (state: { readonly snapshot: Option.Option; }) => Option.getOrThrow(state.snapshot).threads; expect(threadsOf(yield* SubscriptionRef.get(shellState))).toEqual(cachedSnapshot.threads); // The server updates one row before the cached links finish decoding. const serverRow = { ...v2ThreadShell, title: "From server", pullRequests: [link(9)] }; yield* Queue.offer(events, { kind: "thread.updated", sequence: 6, location: "active", thread: serverRow, }); yield* SubscriptionRef.changes(shellState).pipe( Stream.filter((state) => threadsOf(state)[0]?.title === "From server"), Stream.runHead, ); yield* Deferred.succeed(linksReady, undefined); const filled = yield* SubscriptionRef.changes(shellState).pipe( Stream.map(threadsOf), Stream.filter((threads) => threads[1]?.pullRequests !== undefined), Stream.runHead, Effect.map(Option.getOrThrow), ); expect(filled[0]).toBe(serverRow); expect(filled[1]?.pullRequests).toEqual([link(2)]); }), ); it.effect("shows HTTP rows before their pull request links and saves them once filled", () => Effect.gen(function* () { const link: ThreadPullRequestLink = { host: "github.com", repository: "pingdotgg/t3code", number: 7, url: "https://github.com/pingdotgg/t3code/pull/7", source: "agent", linkedAt: "2026-06-20T00:00:00.000Z", snapshot: null, stack: null, }; const httpSnapshot: OrchestrationV2ShellSnapshot = { ...v2ShellSnapshot, snapshotSequence: 3, }; const linksReady = yield* Deferred.make(); const saved = yield* Queue.unbounded(); const client = { [ORCHESTRATION_V2_WS_METHODS.subscribeShell]: () => Stream.never, } as unknown as WsRpcProtocolClient; const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ target: TARGET, state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE), session: yield* SubscriptionRef.make(Option.some(session(client))), prepared: yield* SubscriptionRef.make(Option.some(PREPARED)), connect: Effect.void, disconnect: Effect.void, retryNow: Effect.void, } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]); const cache = Persistence.EnvironmentCacheStore.of({ loadShell: () => Effect.succeedNone, saveShell: (_environmentId, snapshot) => Queue.offer(saved, snapshot), loadThread: () => Effect.succeedNone, saveThread: () => Effect.void, removeThread: () => Effect.void, loadServerConfig: () => Effect.succeedNone, saveServerConfig: () => Effect.void, loadVcsRefs: () => Effect.succeedNone, saveVcsRefs: () => Effect.void, removeVcsRefs: () => Effect.void, clearVcsRefs: () => Effect.void, clear: () => Effect.void, }); const snapshotLoader = ShellSnapshotLoader.ShellSnapshotLoader.of({ load: () => Effect.succeedSome({ ...httpSnapshot, loadPullRequests: Deferred.await(linksReady).pipe( Effect.as(new Map([[v2ThreadShell.id, [link]]])), ), }), }); const shellState = yield* makeEnvironmentShellState().pipe( Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), Effect.provideService(Persistence.EnvironmentCacheStore, cache), Effect.provideService(ShellSnapshotLoader.ShellSnapshotLoader, snapshotLoader), ); const rows = yield* SubscriptionRef.changes(shellState).pipe( Stream.filter((state) => Option.isSome(state.snapshot)), Stream.runHead, Effect.map((state) => Option.getOrThrow(Option.getOrThrow(state).snapshot)), ); expect(rows.threads[0]?.pullRequests).toBeUndefined(); expect(rows).not.toHaveProperty("loadPullRequests"); yield* Deferred.succeed(linksReady, undefined); const filled = yield* SubscriptionRef.changes(shellState).pipe( Stream.map((state) => Option.getOrThrow(state.snapshot).threads[0]), Stream.filter((thread) => thread?.pullRequests !== undefined), Stream.runHead, Effect.map(Option.getOrThrow), ); expect(filled?.pullRequests).toEqual([link]); // The cache must end up with the links: the last save carries them. yield* TestClock.adjust("30 seconds"); const saves = yield* Queue.takeAll(saved); expect(saves.at(-1)?.threads[0]?.pullRequests).toEqual([link]); }), ); it.effect("does not save rows whose deferred links are still pending", () => Effect.gen(function* () { const link: ThreadPullRequestLink = { host: "github.com", repository: "pingdotgg/t3code", number: 8, url: "https://github.com/pingdotgg/t3code/pull/8", source: "agent", linkedAt: "2026-06-20T00:00:00.000Z", snapshot: null, stack: null, }; const saved = yield* Ref.make>([]); const client = { [ORCHESTRATION_V2_WS_METHODS.subscribeShell]: () => Stream.never, } as unknown as WsRpcProtocolClient; const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ target: TARGET, state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE), session: yield* SubscriptionRef.make(Option.some(session(client))), prepared: yield* SubscriptionRef.make(Option.some(PREPARED)), connect: Effect.void, disconnect: Effect.void, retryNow: Effect.void, } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]); const cache = Persistence.EnvironmentCacheStore.of({ loadShell: () => Effect.succeedNone, saveShell: (_environmentId, snapshot) => Ref.update(saved, (all) => [...all, snapshot]), loadThread: () => Effect.succeedNone, saveThread: () => Effect.void, removeThread: () => Effect.void, loadServerConfig: () => Effect.succeedNone, saveServerConfig: () => Effect.void, loadVcsRefs: () => Effect.succeedNone, saveVcsRefs: () => Effect.void, removeVcsRefs: () => Effect.void, clearVcsRefs: () => Effect.void, clear: () => Effect.void, }); const snapshotLoader = ShellSnapshotLoader.ShellSnapshotLoader.of({ load: () => Effect.succeedSome({ ...v2ShellSnapshot, snapshotSequence: 3, // Links never finish decoding before the shell closes. loadPullRequests: Effect.never.pipe(Effect.as(new Map([[v2ThreadShell.id, [link]]]))), }), }); const scope = yield* Scope.make(); const shellState = yield* makeEnvironmentShellState().pipe( Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), Effect.provideService(Persistence.EnvironmentCacheStore, cache), Effect.provideService(ShellSnapshotLoader.ShellSnapshotLoader, snapshotLoader), Scope.provide(scope), ); yield* SubscriptionRef.changes(shellState).pipe( Stream.filter((state) => Option.isSome(state.snapshot)), Stream.runHead, ); // Neither the throttled worker nor the closing flush may save the rows without links. yield* TestClock.adjust("30 seconds"); yield* Scope.close(scope, Exit.void); expect(yield* Ref.get(saved)).toEqual([]); }), ); it.effect("drops an older snapshot's links once a newer snapshot reuses its rows", () => Effect.gen(function* () { const link = (number: number): ThreadPullRequestLink => ({ host: "github.com", repository: "pingdotgg/t3code", number, url: `https://github.com/pingdotgg/t3code/pull/${number}`, source: "agent", linkedAt: "2026-06-20T00:00:00.000Z", snapshot: null, stack: null, }); const firstLinks = yield* Deferred.make(); const secondLinks = yield* Deferred.make(); // Set once each snapshot's links start decoding, i.e. once its rows are applied. const firstFillStarted = yield* Deferred.make(); const loads = yield* Ref.make(0); const subscriptions = yield* Ref.make(0); const client = { [ORCHESTRATION_V2_WS_METHODS.subscribeShell]: () => Stream.unwrap(Ref.update(subscriptions, (n) => n + 1).pipe(Effect.as(Stream.never))), } as unknown as WsRpcProtocolClient; const activeSession = yield* SubscriptionRef.make>( Option.some(session(client)), ); const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ target: TARGET, state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE), session: activeSession, prepared: yield* SubscriptionRef.make(Option.some(PREPARED)), connect: Effect.void, disconnect: Effect.void, retryNow: Effect.void, } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]); const cache = Persistence.EnvironmentCacheStore.of({ loadShell: () => Effect.succeedNone, saveShell: () => Effect.void, loadThread: () => Effect.succeedNone, saveThread: () => Effect.void, removeThread: () => Effect.void, loadServerConfig: () => Effect.succeedNone, saveServerConfig: () => Effect.void, loadVcsRefs: () => Effect.succeedNone, saveVcsRefs: () => Effect.void, removeVcsRefs: () => Effect.void, clearVcsRefs: () => Effect.void, clear: () => Effect.void, }); // Both snapshots carry the same row; only its pull request links differ. const snapshotLoader = ShellSnapshotLoader.ShellSnapshotLoader.of({ load: () => Ref.updateAndGet(loads, (n) => n + 1).pipe( Effect.map((n) => Option.some({ ...v2ShellSnapshot, snapshotSequence: n, loadPullRequests: (n === 1 ? Deferred.succeed(firstFillStarted, undefined).pipe( Effect.andThen(Deferred.await(firstLinks)), ) : Deferred.await(secondLinks) ).pipe(Effect.as(new Map([[v2ThreadShell.id, [link(n)]]]))), }), ), ), }); const shellState = yield* makeEnvironmentShellState().pipe( Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), Effect.provideService(Persistence.EnvironmentCacheStore, cache), Effect.provideService(ShellSnapshotLoader.ShellSnapshotLoader, snapshotLoader), ); yield* Deferred.await(firstFillStarted); // A new session loads a second snapshot that reuses the bare row. yield* SubscriptionRef.set(activeSession, Option.some(session(client))); yield* SubscriptionRef.changes(shellState).pipe( Stream.filter( (state) => Option.isSome(state.snapshot) && state.snapshot.value.snapshotSequence === 2, ), Stream.runHead, ); // The superseded first fill finishes first, then the second. yield* Deferred.succeed(firstLinks, undefined); yield* Deferred.succeed(secondLinks, undefined); const filled = yield* SubscriptionRef.changes(shellState).pipe( Stream.map((state) => Option.getOrThrow(state.snapshot).threads[0]?.pullRequests), Stream.filter((links) => links !== undefined), Stream.runHead, Effect.map(Option.getOrThrow), ); expect(filled).toEqual([link(2)]); }), ); });