import * as NodeServices from "@effect/platform-node/NodeServices"; import { assert, it } from "@effect/vitest"; import { AuthOrchestrationReadScope, AuthSessionId, EnvironmentId, ProjectId, ThreadId, ProviderDriverKind, ProviderInstanceId, WS_METHODS, WsServerRefreshProvidersRpc, WsRpcGroup, WsSubscribeServerConfigRpc, type OrchestrationV2StoredEvent, type OrchestrationV2ThreadShell, type OrchestrationProjectShell, type ServerConfig, type ServerConfigStreamEvent, type ServerProvider, } from "@t3tools/contracts"; import * as Deferred from "effect/Deferred"; import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; import * as Layer from "effect/Layer"; import * as PubSub from "effect/PubSub"; import * as Queue from "effect/Queue"; import * as Stream from "effect/Stream"; import * as TestClock from "effect/testing/TestClock"; import { Headers } from "effect/http"; import { RpcGroup, RpcMessage, RpcServer } from "effect/rpc"; import * as SqlClient from "effect/sql/SqlClient"; import * as PreviewAutomationBroker from "./mcp/PreviewAutomationBroker.ts"; import * as ServerBrowser from "./preview/ServerBrowser.ts"; import * as AcpRegistryRuntimeCoordinator from "@t3tools/provider-acp-registry/server/AcpRegistryRuntimeCoordinator"; import * as AcpRegistrySupport from "@t3tools/provider-acp-registry/server/AcpRegistrySupport"; import * as AgentSessionImporter from "./project/AgentSessionImporter.ts"; import * as AgentSessionScanner from "./project/AgentSessionScanner.ts"; import * as RpcAuthorization from "./auth/RpcAuthorization.ts"; import * as AnalyticsService from "./telemetry/AnalyticsService.ts"; import * as AntigravityInstallation from "./provider/AntigravityInstallation.ts"; import * as BackgroundPolicy from "./background/BackgroundPolicy.ts"; import * as CheckpointDiffQuery from "./checkpointing/CheckpointDiffQuery.ts"; import * as CommandOutputHub from "./orchestration-v2/CommandOutputHub.ts"; import * as CodexInstallation from "./provider/CodexInstallation.ts"; import * as DeviceService from "./device/DeviceService.ts"; import * as DocumentText from "./provider/DocumentText.ts"; import * as DirectEndpoints from "./environment/DirectEndpoints.ts"; import * as EnvironmentAuth from "./auth/EnvironmentAuth.ts"; import * as EnvironmentTheme from "./environmentTheme.ts"; import * as GitWorkflowService from "./git/GitWorkflowService.ts"; import * as HostResources from "./resourceTelemetry/HostResources.ts"; import * as ManagedProjectFolders from "./project/ManagedProjectFolders.ts"; import * as McpAppRequests from "./mcpApps/McpAppRequests.ts"; import * as ModelManifest from "./provider/ModelManifest.ts"; import * as Orchestrator from "./orchestration-v2/Orchestrator.ts"; import * as OtlpEndpointCheck from "./observability/OtlpEndpointCheck.ts"; import * as PairingGrantStore from "./auth/PairingGrantStore.ts"; import * as PeerLinkRequests from "./peer/PeerLinkRequests.ts"; import * as PeerLinks from "./peer/PeerLinks.ts"; import * as PortScanner from "./preview/PortScanner.ts"; import * as PreviewManager from "./preview/Manager.ts"; import * as ProcessDiagnostics from "./diagnostics/ProcessDiagnostics.ts"; import * as ProcessResourceMonitor from "./diagnostics/ProcessResourceMonitor.ts"; import * as ProjectCloneTracker from "./project/ProjectCloneTracker.ts"; import * as ProjectSetupScriptRunner from "./project/ProjectSetupScriptRunner.ts"; import * as ProviderAuthService from "./provider/ProviderAuthService.ts"; import * as ProviderInstanceRegistry from "./provider/ProviderInstanceRegistry.ts"; import * as ProviderLatestVersions from "@t3tools/provider-core/server/ProviderLatestVersions"; import * as ProviderMaintenanceRunner from "./provider/providerMaintenanceRunner.ts"; import * as ProviderRegistry from "./provider/ProviderRegistry.ts"; import * as ProviderSessionManager from "./orchestration-v2/ProviderSessionManager.ts"; import * as PullRequestReviewGuideService from "./pullRequest/PullRequestReviewGuideService.ts"; import * as PullRequestService from "./pullRequest/PullRequestService.ts"; import * as PullRequestSyncReactor from "./orchestration-v2/PullRequestSyncReactor.ts"; import * as RelayClient from "@t3tools/shared/relayClient"; import * as RemoteOpenTargets from "./environment/RemoteOpenTargets.ts"; import * as RepositoryIdentityResolver from "./project/RepositoryIdentityResolver.ts"; import * as ResourceTelemetry from "./resourceTelemetry/ResourceTelemetry.ts"; import * as ReviewService from "./review/ReviewService.ts"; import * as ScheduledTasks from "./scheduledTasks/ScheduledTaskService.ts"; import * as SecretRequests from "./secrets/SecretRequests.ts"; import * as GuestShareService from "./guest/GuestShareService.ts"; import * as ServerEnvironment from "./environment/ServerEnvironment.ts"; import * as ServerLifecycleEvents from "./serverLifecycleEvents.ts"; import * as ServerRuntimeStartup from "./serverRuntimeStartup.ts"; import * as ServerSelfUpdate from "./cloud/selfUpdate.ts"; import * as SessionStore from "./auth/SessionStore.ts"; import * as SourceControlDiscovery from "./sourceControl/SourceControlDiscovery.ts"; import * as SourceControlRepositoryService from "./sourceControl/SourceControlRepositoryService.ts"; import * as SourceControlSignIn from "./sourceControl/SourceControlSignIn.ts"; import * as StorageCleanup from "./storageCleanup.ts"; import * as DatabaseStorage from "./persistence/DatabaseStorage.ts"; import * as TerminalManager from "./terminal/Manager.ts"; import * as ThreadLaunchService from "./orchestration-v2/ThreadLaunchService.ts"; import * as ThreadSearch from "./orchestration-v2/ThreadSearch.ts"; import * as UsageLimitSources from "./usage/UsageLimitSources.ts"; import * as UsageService from "./usage/UsageService.ts"; import * as VcsProvisioningService from "./vcs/VcsProvisioningService.ts"; import * as VcsStatusBroadcaster from "./vcs/VcsStatusBroadcaster.ts"; import * as WorkspaceEntries from "./workspace/WorkspaceEntries.ts"; import * as WorkspaceFileSystem from "./workspace/WorkspaceFileSystem.ts"; import * as WorktreeSetupTracker from "./project/WorktreeSetupTracker.ts"; import * as ProjectFaviconResolver from "./project/ProjectFaviconResolver.ts"; import * as TraceDiagnostics from "./diagnostics/TraceDiagnostics.ts"; import * as TriageService from "./triage/TriageService.ts"; import * as WorkspacePaths from "./workspace/WorkspacePaths.ts"; import * as ServerSecretStore from "./auth/ServerSecretStore.ts"; import * as ServerConfigModule from "./config.ts"; import * as Keybindings from "./keybindings.ts"; import * as ServerSettings from "./serverSettings.ts"; import * as EventStore from "./orchestration-v2/EventStore.ts"; import * as ProjectionMaintenance from "./orchestration-v2/ProjectionMaintenance.ts"; import * as ProjectionStore from "./orchestration-v2/ProjectionStore.ts"; import * as ThreadManagement from "./orchestration-v2/ThreadManagementService.ts"; import * as ProjectStore from "./orchestration-v2/ProjectStore.ts"; import * as OrchestrationEventStore from "./persistence/OrchestrationEventStore.ts"; import * as SqlitePersistence from "./persistence/Sqlite.ts"; import * as ExternalLauncher from "./process/externalLauncher.ts"; import * as ProjectEnrichment from "./project/ProjectEnrichmentService.ts"; import * as ProjectService from "./project/ProjectService.ts"; import { layerWsRpc, subscribeOrchestrationV2Shell, subscribeOrchestrationV2Thread, WS_RPC_SERVER_OPTIONS, withLateEditorConfig, } from "./ws.ts"; const snapshotConfig = (fields: Partial) => ({ availableEditors: [], settings: {}, ...fields }) as unknown as ServerConfig; const settingsUpdated = (settings: object): ServerConfigStreamEvent => ({ version: 1, type: "settingsUpdated", payload: { settings: settings as ServerConfig["settings"] }, }); it.effect("resends late editors without rolling back updates already sent", () => Effect.gen(function* () { const settingsSent = yield* Deferred.make(); const events = yield* withLateEditorConfig( snapshotConfig({ settings: { enableProviderUpdateChecks: true } as never }), Stream.make(settingsUpdated({ enableProviderUpdateChecks: false })), { resolveAvailableEditors: () => Effect.succeed(["file-manager"]), resolveFileManagerRevealKind: () => Deferred.await(settingsSent).pipe(Effect.as("file-explorer" as const)), }, ).pipe( Stream.tap((event) => event.type === "settingsUpdated" ? Deferred.succeed(settingsSent, undefined) : Effect.void, ), Stream.runCollect, ); const [first, second] = Array.from(events); assert.equal(events.length, 2); assert.equal(first?.type, "settingsUpdated"); assert.equal(second?.type, "snapshot"); if (second?.type === "snapshot") { assert.deepEqual(second.config.availableEditors, ["file-manager"]); assert.equal(second.config.shellRevealInFileManagerKind, "file-explorer"); assert.deepEqual(second.config.settings, { enableProviderUpdateChecks: false } as never); } }), ); it.effect("resends a file manager reveal kind that missed the snapshot", () => Effect.gen(function* () { const events = yield* withLateEditorConfig( snapshotConfig({ availableEditors: ["file-manager"] }), Stream.empty, { resolveAvailableEditors: () => Effect.succeed(["file-manager"]), resolveFileManagerRevealKind: () => Effect.succeed("file-explorer"), }, ).pipe(Stream.runCollect); const [late] = Array.from(events); assert.equal(events.length, 1); assert.equal(late?.type, "snapshot"); if (late?.type === "snapshot") { assert.equal(late.config.shellRevealInFileManagerKind, "file-explorer"); } }), ); const ReplayTestLayer = OrchestrationEventStore.layer.pipe( Layer.provideMerge(SqlitePersistence.layerMemory), ); it.live.each(["thread", "shell"] as const)( "%s resume skips unknown types, synchronizes, and receives the live tail", (scope) => Effect.gen(function* () { const store = yield* OrchestrationEventStore.OrchestrationEventStore; const sql = yield* SqlClient.SqlClient; const now = "2026-01-05T00:00:00.000Z"; const threadId = ThreadId.make("thread:replay"); const projectId = ProjectId.make("project:replay"); const project = { id: projectId, title: "Replay", workspaceRoot: "/tmp/replay", defaultModelSelection: null, scripts: [], createdAt: now, updatedAt: now, } satisfies OrchestrationProjectShell; const insert = (version: number, unknown = false) => sql<{ sequence: number }>` INSERT INTO orchestration_events ( event_id, aggregate_kind, stream_id, stream_version, event_type, occurred_at, actor_kind, payload_json, metadata_json, application_event_version ) VALUES ( ${`replay:${version}`}, ${scope === "thread" ? "thread" : "project"}, ${scope === "thread" ? threadId : projectId}, ${version}, ${unknown ? "future.event" : scope === "thread" ? "provider-session.detached" : "project.meta-updated"}, ${now}, 'server', ${JSON.stringify( scope === "thread" ? { providerSessionId: "session:replay", detachedAt: now } : { projectId, updatedAt: now }, )}, '{}', 2 ) RETURNING sequence `; const [before] = yield* insert(0); yield* insert(1, true); const [after] = yield* insert(2); const [highWater] = yield* insert(3, true); const updates = yield* PubSub.unbounded(); let liveSequence = 0; const consume = ( stream: Stream.Stream, ) => stream.pipe( Stream.tap((item) => Effect.gen(function* () { if (item.kind !== "synchronized") return; const [live] = yield* insert(4); liveSequence = live!.sequence; const events = yield* store .readApplicationEvents({ afterSequence: highWater!.sequence, throughSequence: liveSequence, }) .pipe(Stream.runCollect); yield* store.publishCommitted(events); }), ), Stream.takeUntil((item) => item.sequence === liveSequence), Stream.map((item) => ({ kind: item.kind as string, sequence: item.sequence })), Stream.runCollect, ); const subscription = Effect.gen(function* () { return scope === "thread" ? yield* subscribeOrchestrationV2Thread({ threadId, afterSequence: before!.sequence, requestCompletionMarker: true, }).pipe(Effect.flatMap(consume)) : yield* subscribeOrchestrationV2Shell({ afterSequence: before!.sequence, requestCompletionMarker: true, }).pipe(Effect.flatMap(consume)); }); const items = yield* subscription.pipe( Effect.provideService(ThreadManagement.ThreadManagementService, { ensureLegacyTranscript: () => Effect.void, getShellSnapshot: () => Effect.die("shell resume must not load a full snapshot"), listActiveSubagentThreadIds: () => Effect.succeed([]), streamStoredEventsFrom: ( input: Parameters< ThreadManagement.ThreadManagementService["Service"]["streamStoredEventsFrom"] >[0], ) => { assert.equal(input?.afterSequence, highWater!.sequence); return store .streamApplicationEvents({ afterSequence: input?.afterSequence ?? 0 }) .pipe( Stream.filter((event): event is OrchestrationV2StoredEvent => "event" in event), ); }, } as unknown as ThreadManagement.ThreadManagementService["Service"]), Effect.provideService(ProjectStore.ProjectStoreV2, { listShells: () => Effect.succeed([]), } as unknown as ProjectStore.ProjectStoreV2["Service"]), Effect.provideService(ProjectService.ProjectService, { getShell: () => Effect.succeedSome(project), } as unknown as ProjectService.ProjectService["Service"]), Effect.provideService(ProjectEnrichment.ProjectEnrichmentService, { subscribeChanges: PubSub.subscribe(updates), } as ProjectEnrichment.ProjectEnrichmentService["Service"]), ); assert.deepEqual( items.map((item) => item.kind), scope === "thread" ? ["event", "synchronized", "event"] : ["project.updated", "synchronized", "project.updated"], ); assert.deepEqual( items.flatMap((item) => (item.sequence === undefined ? [] : [item.sequence])), [after!.sequence, liveSequence], ); }).pipe(Effect.scoped, Effect.provide(ReplayTestLayer)), ); it.effect("coalesces streaming replay before synchronization and the live tail", () => Effect.gen(function* () { const store = yield* OrchestrationEventStore.OrchestrationEventStore; const sql = yield* SqlClient.SqlClient; const threadId = ThreadId.make("thread:streaming-replay"); const now = "2026-10-27T00:00:00.000Z"; const insert = (version: number, text: string, streaming: boolean) => sql<{ sequence: number; }>` INSERT INTO orchestration_events ( event_id, aggregate_kind, stream_id, stream_version, event_type, occurred_at, actor_kind, payload_json, metadata_json, application_event_version ) VALUES ( ${`streaming-replay:${version}`}, 'thread', ${threadId}, ${version}, 'message.updated', ${now}, 'server', ${JSON.stringify({ createdBy: "agent", creationSource: "web", id: "message:streaming-replay", threadId, runId: null, nodeId: null, role: "assistant", text, attachments: [], streaming, createdAt: now, updatedAt: now, })}, '{}', 2 ) RETURNING sequence `; yield* insert(0, "H", true); yield* insert(1, "He", true); const [latest] = yield* insert(2, "Hello", true); const [completed] = yield* insert(3, "Hello!", false); const items = yield* subscribeOrchestrationV2Thread({ threadId, afterSequence: 0, requestCompletionMarker: true, }).pipe( Effect.flatMap(Stream.runCollect), Effect.provideService(ThreadManagement.ThreadManagementService, { ensureLegacyTranscript: () => Effect.void, streamStoredEventsFrom: () => Stream.unwrap( insert(4, "Next", false).pipe( Effect.map(() => store.readAgentEvents({ threadId, afterSequence: completed!.sequence }), ), ), ), } as unknown as ThreadManagement.ThreadManagementService["Service"]), ); assert.deepEqual( items.map((item) => item.kind === "event" ? [item.sequence, item.event.type === "message.updated" ? item.event.payload.text : null] : [item.kind], ), [ [latest!.sequence, "Hello"], [completed!.sequence, "Hello!"], ["synchronized"], [completed!.sequence + 1, "Next"], ], ); }).pipe(Effect.scoped, Effect.provide(ReplayTestLayer)), ); it.effect("shell snapshots omit idle children and older settled threads only on request", () => Effect.gen(function* () { const shell = ( id: string, parent: string | null, activityRunStatus: "running" | null, settledDaysAgo: number | null = null, ) => ({ id: ThreadId.make(id), projectId: "project", archivedAt: null, status: activityRunStatus ?? "completed", activityRunStatus, pendingRuntimeRequest: null, pendingBackgroundTasks: [], settledOverride: settledDaysAgo === null ? null : "settled", settledAt: settledDaysAgo === null ? null : DateTime.makeUnsafe(-settledDaysAgo * 86_400_000), lineage: { parentThreadId: parent === null ? null : ThreadId.make(parent), relationshipToParent: parent === null ? null : "subagent", rootThreadId: ThreadId.make(parent ?? id), }, }) as unknown as OrchestrationV2ThreadShell; const threads = [ shell("parent", null, null), shell("idle", "parent", null), shell("live", "parent", "running"), ...Array.from({ length: 25 }, (_, index) => shell(`recent-${index}`, null, null, 1)), shell("old", null, null, 30), ]; const firstSnapshot = (input: { readonly omitIdleSubagents?: boolean; readonly omitOlderSettled?: boolean; }) => subscribeOrchestrationV2Shell(input).pipe( Effect.flatMap((stream) => stream.pipe(Stream.take(1), Stream.runCollect)), Effect.map(([item]) => (item?.kind === "snapshot" ? item.snapshot : null)), Effect.provideService(ThreadManagement.ThreadManagementService, { readShellSnapshot: () => Effect.succeed( Effect.succeed({ schemaVersion: 1, snapshotSequence: 0, threads, archivedThreads: [], }), ), } as unknown as ThreadManagement.ThreadManagementService["Service"]), Effect.provideService(ProjectStore.ProjectStoreV2, { listShells: () => Effect.succeed([]), } as unknown as ProjectStore.ProjectStoreV2["Service"]), Effect.provideService(ProjectService.ProjectService, { getShell: () => Effect.succeedNone, } as unknown as ProjectService.ProjectService["Service"]), Effect.provideServiceEffect( ProjectEnrichment.ProjectEnrichmentService, PubSub.unbounded().pipe( Effect.map( (changes) => ({ subscribeChanges: PubSub.subscribe(changes), }) as unknown as ProjectEnrichment.ProjectEnrichmentService["Service"], ), ), ), ); const recent = threads.slice(3, 28).map((thread) => thread.id); const full = yield* firstSnapshot({}); assert.deepEqual( full?.threads.map((thread) => thread.id), threads.map((thread) => thread.id), ); assert.isUndefined(full?.olderSettled); const withoutChildren = yield* firstSnapshot({ omitIdleSubagents: true }); assert.deepEqual( withoutChildren?.threads.map((thread) => thread.id), ["parent", "live", ...recent, "old"], ); const withoutOld = yield* firstSnapshot({ omitIdleSubagents: true, omitOlderSettled: true }); assert.deepEqual( withoutOld?.threads.map((thread) => thread.id), ["parent", "live", ...recent], ); assert.deepEqual(withoutOld?.olderSettled?.counts, [ { projectId: ProjectId.make("project"), count: 1 }, ]); }).pipe(Effect.scoped, Effect.provide(ReplayTestLayer)), ); // Build the production RPC layer; unrelated capabilities fail if this test calls them. const layerUnusedConfigRpcServices = Layer.mergeAll( Layer.mock(DocumentText.DocumentText)({}), Layer.mock(ServerSecretStore.ServerSecretStore)({}), Layer.mock(WorkspacePaths.WorkspacePaths)({}), Layer.mock(TraceDiagnostics.TraceDiagnostics)({}), Layer.mock(TriageService.TriageService)({}), Layer.mock(ProjectFaviconResolver.ProjectFaviconResolver)({}), Layer.mock(ProjectEnrichment.ProjectEnrichmentService)({}), Layer.mock(AcpRegistryRuntimeCoordinator.AcpRegistryRuntimeCoordinator)({}), Layer.mock(AcpRegistrySupport.AcpRegistryCatalog)({}), Layer.mock(AgentSessionImporter.AgentSessionImporter)({}), Layer.mock(AgentSessionScanner.AgentSessionScanner)({}), Layer.mock(AnalyticsService.AnalyticsService)({}), Layer.mock(AntigravityInstallation.AntigravityInstallation)({ managedDirectory: "/unused" }), Layer.mock(BackgroundPolicy.BackgroundPolicy)({}), Layer.mock(CheckpointDiffQuery.CheckpointDiffQuery)({}), Layer.mock(CommandOutputHub.CommandOutputHub)({}), Layer.mock(CodexInstallation.CodexInstallation)({ managedDirectory: "/unused" }), Layer.mock(DeviceService.DeviceService)({}), Layer.mock(EnvironmentTheme.EnvironmentThemeService)({}), Layer.mock(GitWorkflowService.GitWorkflowService)({}), Layer.mock(HostResources.HostResources)({}), Layer.mock(McpAppRequests.McpAppRequests)({}), Layer.mock(ModelManifest.ModelManifest)({}), Layer.mock(OrchestrationEventStore.OrchestrationEventStore)({}), Layer.mock(Orchestrator.OrchestratorV2)({}), Layer.mock(OtlpEndpointCheck.OtlpEndpointCheck)({}), Layer.mock(PairingGrantStore.PairingGrantStore)({}), Layer.mock(PeerLinks.PeerLinks)({}), Layer.mock(PeerLinkRequests.PeerLinkRequests)({}), Layer.mock(PortScanner.PortDiscovery)({ trackAgent: () => () => undefined }), Layer.mock(PreviewManager.PreviewManager)({}), Layer.mock(ProcessDiagnostics.ProcessDiagnostics)({}), Layer.mock(ProcessResourceMonitor.ProcessResourceMonitor)({}), Layer.mock(ProjectCloneTracker.ProjectCloneTracker)({}), Layer.mock(ProjectService.ProjectService)({}), Layer.mock(ProjectSetupScriptRunner.ProjectSetupScriptRunner)({}), Layer.mock(ProjectStore.ProjectStoreV2)({}), Layer.mock(ProviderAuthService.ProviderAuthService)({}), Layer.mock(ProviderInstanceRegistry.ProviderInstanceRegistry)({}), Layer.mock(ProviderLatestVersions.ProviderLatestVersions)({}), Layer.mock(ProviderMaintenanceRunner.ProviderMaintenanceRunner)({}), Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({}), Layer.mock(PullRequestReviewGuideService.PullRequestReviewGuideService)({}), Layer.mock(PullRequestService.PullRequestService)({}), Layer.mock(PullRequestSyncReactor.PullRequestSyncReactor)({}), Layer.mock(RelayClient.RelayClient)({}), Layer.mock(RepositoryIdentityResolver.RepositoryIdentityResolver)({}), Layer.mock(ResourceTelemetry.ResourceTelemetry)({}), Layer.mock(ReviewService.ReviewService)({}), Layer.mock(ScheduledTasks.ScheduledTaskService)({}), Layer.mock(SecretRequests.SecretRequests)({}), Layer.mock(GuestShareService.GuestShareService)({}), Layer.mock(ServerLifecycleEvents.ServerLifecycleEvents)({}), Layer.mock(ServerRuntimeStartup.ServerRuntimeStartup)({}), Layer.mock(ServerSelfUpdate.ServerSelfUpdate)({}), Layer.mock(SessionStore.SessionStore)({ cookieName: "config-test", legacyCookieName: undefined }), Layer.mock(SourceControlDiscovery.SourceControlDiscovery)({}), Layer.mock(SourceControlRepositoryService.SourceControlRepositoryService)({}), Layer.mock(SourceControlSignIn.SourceControlSignIn)({}), Layer.mock(StorageCleanup.StorageCleanup)({}), Layer.mock(DatabaseStorage.DatabaseStorage)({}), Layer.mock(TerminalManager.TerminalManager)({}), Layer.mock(ThreadLaunchService.ThreadLaunchService)({}), Layer.mock(ThreadManagement.ThreadManagementService)({}), Layer.mock(ThreadSearch.ThreadSearch)({}), Layer.mock(UsageService.UsageService)({}), Layer.mock(VcsProvisioningService.VcsProvisioningService)({}), Layer.mock(VcsStatusBroadcaster.VcsStatusBroadcaster)({}), Layer.mock(WorkspaceEntries.WorkspaceEntries)({}), Layer.mock(WorkspaceFileSystem.WorkspaceFileSystem)({}), Layer.mock(WorktreeSetupTracker.WorktreeSetupTracker)({}), ); const layerConfigSources = Layer.mergeAll( Keybindings.layer, ServerSettings.layer.pipe(Layer.provide(ServerSecretStore.layer)), ).pipe( Layer.provideMerge(Layer.fresh(SqlitePersistence.layerMemory)), Layer.provideMerge( Layer.fresh( ServerConfigModule.layerTest(process.cwd(), { prefix: "t3-config-subscription-test-" }), ), ), Layer.provideMerge(NodeServices.layer), ); it.effect( "preserves settings during discovery and keybindings during snapshot ACK at the config RPC boundary", () => Effect.gen(function* () { const keybindings = yield* Keybindings.Keybindings; const settings = yield* ServerSettings.ServerSettingsService; const keybindingsSubscribed = yield* Deferred.make(); const settingsSubscribed = yield* Deferred.make(); const keybindingsChanges = keybindings.subscribeChanges.pipe( Effect.tap(() => Deferred.succeed(keybindingsSubscribed, undefined)), ); const settingsChanges = settings.subscribeChanges.pipe( Effect.tap(() => Deferred.succeed(settingsSubscribed, undefined)), ); const initialKeybindings = (yield* keybindings.getSnapshot).keybindings; const discoveryStarted = yield* Deferred.make(); const finishDiscovery = yield* Deferred.make(); const responses = yield* Queue.unbounded(); const configRpcServices = Layer.mergeAll( layerUnusedConfigRpcServices, Layer.mock(ExternalLauncher.ExternalLauncher)({ resolveAvailableEditors: () => Deferred.succeed(discoveryStarted, undefined).pipe( Effect.andThen(Deferred.await(finishDiscovery)), Effect.as([]), ), // @effect-diagnostics-next-line effectSucceedWithVoid:off - This optional result requires undefined, not the wider void type. resolveFileManagerRevealKind: () => Effect.succeed(undefined), }), Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]), streamChanges: Stream.never, }), Layer.mock(UsageLimitSources.UsageLimitSources)({ streamChanges: Stream.make([]) }), Layer.mock(RemoteOpenTargets.RemoteOpenTargets)({ resolveTargets: () => Effect.succeed([]), }), Layer.mock(DirectEndpoints.DirectEndpoints)({ resolve: () => Effect.succeed([]) }), Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({ scratchRoot: Effect.succeedNone, namedProjectsRoot: process.cwd(), }), Layer.mock(ServerEnvironment.ServerEnvironment)({ getDescriptor: Effect.succeed({ environmentId: EnvironmentId.make("config-test"), label: "Config test", platform: { os: "linux", arch: "x64" }, serverVersion: "0.0.0", capabilities: { repositoryIdentity: false }, }), }), Layer.mock(EnvironmentAuth.EnvironmentAuth)({ getDescriptor: () => Effect.succeed({ policy: "loopback-browser", bootstrapMethods: [], sessionMethods: ["browser-session-cookie"], sessionCookieName: "config-test-session", }), }), // Signal subscription on either accessor so the test also fails, rather // than hangs, against a handler that subscribes after the snapshot. Layer.succeed(Keybindings.Keybindings, { ...keybindings, streamChanges: Stream.unwrap(keybindingsChanges), subscribeChanges: keybindingsChanges, }), Layer.succeed(ServerSettings.ServerSettingsService, { ...settings, streamChanges: Stream.unwrap(settingsChanges), subscribeChanges: settingsChanges, }), ); const server = yield* RpcServer.makeNoSerialization( RpcGroup.make(WsSubscribeServerConfigRpc), { ...WS_RPC_SERVER_OPTIONS, onFromServer: (response) => response._tag === "Chunk" ? Effect.forEach(response.values, (event) => Queue.offer(responses, event), { discard: true, }) : Effect.void, }, ).pipe( Effect.provide( layerWsRpc( { sessionId: AuthSessionId.make("config-test"), subject: "config-test", method: "browser-session-cookie", scopes: [], }, {}, yield* ServerBrowser.ServerBrowser, ).pipe(Layer.provide(configRpcServices)), ), ); const requestId = RpcMessage.RequestId("config"); const acknowledge = server.write(0, { _tag: "Ack", requestId }); yield* server.write(0, { _tag: "Request", id: requestId, tag: WS_METHODS.subscribeServerConfig, payload: {}, headers: Headers.empty, }); yield* Deferred.await(discoveryStarted); const duringDiscovery = yield* settings.updateSettings({ defaultAutoPull: true }); yield* Deferred.succeed(finishDiscovery, undefined); const snapshot = yield* Queue.take(responses); assert.equal(snapshot.type, "snapshot"); if (snapshot.type !== "snapshot") return yield* Effect.die("Missing initial snapshot"); assert.isFalse(snapshot.config.settings.defaultAutoPull); assert.deepEqual(snapshot.config.keybindings, initialKeybindings); const duringAck = yield* keybindings.upsertKeybindingRule({ key: "mod+alt+y", command: "terminal.toggle", }); yield* acknowledge; yield* Deferred.await(keybindingsSubscribed); yield* Deferred.await(settingsSubscribed); // Later updates expose a dropped buffered event as a value mismatch, not a timeout. const latestSettings = yield* settings.updateSettings({ enableProviderUpdateChecks: false }); const latestKeybindings = yield* keybindings.upsertKeybindingRule({ key: "mod+alt+z", command: "terminal.split", }); const expectedSettings = [duringDiscovery, latestSettings].map( ServerSettings.redactServerSettingsForClient, ); const expectedKeybindings = [duringAck, latestKeybindings]; // The two sources can interleave, but each must retain its own publication order. for (let i = 0; i < 4; i++) { const event = yield* Queue.take(responses); if (event.type === "settingsUpdated") { assert.deepEqual(event.payload.settings, expectedSettings.shift()); } else { assert.equal(event.type, "keybindingsUpdated"); if (event.type !== "keybindingsUpdated") return yield* Effect.die("Unexpected config event"); assert.deepEqual(event.payload.keybindings, expectedKeybindings.shift()); assert.deepEqual(event.payload.issues, []); } yield* acknowledge; } assert.isEmpty(expectedSettings); assert.isEmpty(expectedKeybindings); yield* server.write(0, { _tag: "Interrupt", requestId, interruptors: [] }); }).pipe( Effect.provide( Layer.mergeAll( Layer.fresh(layerConfigSources), Layer.mock(PreviewAutomationBroker.PreviewAutomationBroker)({}), Layer.mock(ServerBrowser.ServerBrowser)({}), ), ), Effect.scoped, ), ); // The shipped SwiftUI iOS client reads settings through this tag, so the server keeps answering it. it.effect("server.getSettings returns the client-redacted server settings", () => Effect.gen(function* () { const settings = yield* ServerSettings.ServerSettingsService; const exits = yield* Queue.unbounded(); const server = yield* RpcServer.makeNoSerialization(WsRpcGroup, { ...WS_RPC_SERVER_OPTIONS, onFromServer: (response) => response._tag === "Exit" ? Queue.offer(exits, response.exit) : Effect.void, }).pipe( Effect.provide( layerWsRpc( { sessionId: AuthSessionId.make("settings-test"), subject: "settings-test", method: "browser-session-cookie", scopes: [AuthOrchestrationReadScope], }, {}, yield* ServerBrowser.ServerBrowser, ).pipe( Layer.provide( Layer.mergeAll( layerUnusedConfigRpcServices, Layer.mock(ExternalLauncher.ExternalLauncher)({}), Layer.mock(ProviderRegistry.ProviderRegistry)({}), Layer.mock(UsageLimitSources.UsageLimitSources)({}), Layer.mock(RemoteOpenTargets.RemoteOpenTargets)({}), Layer.mock(DirectEndpoints.DirectEndpoints)({}), Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({ namedProjectsRoot: process.cwd(), }), Layer.mock(ServerEnvironment.ServerEnvironment)({}), Layer.mock(EnvironmentAuth.EnvironmentAuth)({}), ), ), Layer.provideMerge(RpcAuthorization.layer([AuthOrchestrationReadScope])), ), ), ); const updated = yield* settings.updateSettings({ defaultAutoPull: true }); yield* server.write(0, { _tag: "Request", id: RpcMessage.RequestId("settings"), tag: "server.getSettings", payload: {}, headers: Headers.empty, }); assert.deepEqual( yield* Queue.take(exits), Exit.succeed(ServerSettings.redactServerSettingsForClient(updated)), ); }).pipe( Effect.provide( Layer.mergeAll( Layer.fresh(layerConfigSources), Layer.mock(PreviewAutomationBroker.PreviewAutomationBroker)({}), Layer.mock(ServerBrowser.ServerBrowser)({}), ), ), Effect.scoped, ), ); it.effect("on-demand clients get provider config without workspace snapshots", () => Effect.gen(function* () { const instanceId = ProviderInstanceId.make("claudeAgent"); const snapshotFor = (cwd: string, checkedAt: string) => ({ cwd, checkedAt, slashCommands: [{ name: `${cwd.slice(1)}-command` }], skills: [], }); const provider: ServerProvider = { instanceId, driver: ProviderDriverKind.make("claudeAgent"), enabled: true, installed: true, version: "1.0.0", status: "ready", auth: { status: "authenticated" }, checkedAt: "2026-10-25T00:00:00.000Z", models: [], slashCommands: [{ name: "machine-command" }], skills: [], workspaceSnapshots: [ snapshotFor("/a", "2026-10-25T00:00:00.000Z"), snapshotFor("/b", "2026-10-25T00:00:00.000Z"), ], }; const providerChanges = yield* Queue.unbounded>(); const pulled = yield* Queue.unbounded(); const responses = yield* Queue.unbounded(); const configRpcServices = Layer.mergeAll( layerUnusedConfigRpcServices, Layer.mock(ExternalLauncher.ExternalLauncher)({ resolveAvailableEditors: () => Effect.succeed([]), // @effect-diagnostics-next-line effectSucceedWithVoid:off - This optional result requires undefined, not the wider void type. resolveFileManagerRevealKind: () => Effect.succeed(undefined), }), Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([provider]), streamChanges: Stream.fromQueue(providerChanges).pipe( Stream.tap(() => Queue.offer(pulled, undefined)), ), refreshWorkspaceSnapshot: () => Effect.succeed([provider]), }), Layer.mock(UsageLimitSources.UsageLimitSources)({ current: Effect.succeed([]), streamChanges: Stream.make([]), }), Layer.mock(RemoteOpenTargets.RemoteOpenTargets)({ resolveTargets: () => Effect.succeed([]), }), Layer.mock(DirectEndpoints.DirectEndpoints)({ resolve: () => Effect.succeed([]) }), Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({ scratchRoot: Effect.succeedNone, namedProjectsRoot: process.cwd(), }), Layer.mock(ServerEnvironment.ServerEnvironment)({ getDescriptor: Effect.succeed({ environmentId: EnvironmentId.make("config-test"), label: "Config test", platform: { os: "linux", arch: "x64" }, serverVersion: "0.0.0", capabilities: { repositoryIdentity: false }, }), }), Layer.mock(EnvironmentAuth.EnvironmentAuth)({ getDescriptor: () => Effect.succeed({ policy: "loopback-browser", bootstrapMethods: [], sessionMethods: ["browser-session-cookie"], sessionCookieName: "config-test-session", }), }), ); const server = yield* RpcServer.makeNoSerialization( RpcGroup.make(WsSubscribeServerConfigRpc, WsServerRefreshProvidersRpc), { ...WS_RPC_SERVER_OPTIONS, onFromServer: (response) => response._tag === "Chunk" ? Effect.forEach(response.values, (value) => Queue.offer(responses, value), { discard: true, }) : response._tag === "Exit" && response.exit._tag === "Success" ? Queue.offer(responses, response.exit.value) : Effect.void, }, ).pipe( Effect.provide( layerWsRpc( { sessionId: AuthSessionId.make("config-test"), subject: "config-test", method: "browser-session-cookie", scopes: [], }, {}, yield* ServerBrowser.ServerBrowser, ).pipe(Layer.provide(configRpcServices)), ), ); const configRequest = RpcMessage.RequestId("config"); yield* server.write(0, { _tag: "Request", id: configRequest, tag: WS_METHODS.subscribeServerConfig, payload: { workspaceSnapshotsOnDemand: true }, headers: Headers.empty, }); const snapshot = (yield* Queue.take(responses)) as ServerConfigStreamEvent; if (snapshot.type !== "snapshot") return yield* Effect.die("Missing initial snapshot"); assert.isUndefined(snapshot.config.providers[0]?.workspaceSnapshots); yield* server.write(0, { _tag: "Ack", requestId: configRequest }); // A rescan that only touches a snapshot sends nothing; the next real change does. yield* Queue.offer(providerChanges, [ { ...provider, workspaceSnapshots: [snapshotFor("/a", "2026-10-25T00:05:00.000Z")] }, ]); yield* Queue.take(pulled); yield* TestClock.adjust("1 second"); yield* Queue.offer(providerChanges, [{ ...provider, status: "warning" }]); yield* Queue.take(pulled); yield* TestClock.adjust("1 second"); const update = (yield* Queue.take(responses)) as ServerConfigStreamEvent; if (update.type !== "providerStatuses") return yield* Effect.die("Expected provider update"); assert.equal(update.payload.providers[0]?.status, "warning"); assert.isUndefined(update.payload.providers[0]?.workspaceSnapshots); yield* server.write(0, { _tag: "Request", id: RpcMessage.RequestId("refresh"), tag: WS_METHODS.serverRefreshProviders, payload: { instanceId, cwd: "/b", workspaceSnapshotsOnDemand: true }, headers: Headers.empty, }); const refreshed = (yield* Queue.take(responses)) as { readonly providers: ReadonlyArray; }; assert.deepEqual( refreshed.providers.map((entry) => [entry.workspaceSnapshots, entry.slashCommands]), [[[snapshotFor("/b", "2026-10-25T00:00:00.000Z")], []]], ); yield* server.write(0, { _tag: "Interrupt", requestId: configRequest, interruptors: [] }); }).pipe( Effect.provide( Layer.mergeAll( Layer.fresh(layerConfigSources), Layer.mock(PreviewAutomationBroker.PreviewAutomationBroker)({}), Layer.mock(ServerBrowser.ServerBrowser)({}), ), ), Effect.scoped, ), ); // Compaction keeps the oldest and newest event per message, so a cursor older // than the deleted rows still replays each message where the client would // first have seen it (reducers append on first sight) and reaches the same // final state. No snapshot fallback is needed. it.effect("thread resume across compacted history keeps message order and final state", () => Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2; const now = "2026-10-13T00:00:00.000Z"; const threadId = ThreadId.make("thread:compacted"); const insertMessage = (version: number, id: string, text: string) => sql<{ sequence: number; }>` INSERT INTO orchestration_events ( event_id, aggregate_kind, stream_id, stream_version, event_type, occurred_at, actor_kind, payload_json, metadata_json, application_event_version ) VALUES ( ${`compacted:${version}`}, 'thread', ${threadId}, ${version}, 'message.updated', ${now}, 'server', ${JSON.stringify({ createdBy: "user", creationSource: "web", id, threadId, runId: null, nodeId: null, role: "user", text, attachments: [], streaming: false, createdAt: now, updatedAt: now, })}, '{}', 2 ) RETURNING sequence `; const [first] = yield* insertMessage(0, "message:a", "a created"); const [second] = yield* insertMessage(1, "message:b", "b created"); yield* insertMessage(2, "message:a", "a superseded"); const [head] = yield* insertMessage(3, "message:a", "a final"); // thread.deleted is never superseded: clients switch on the event type. for (const version of [0, 1]) { yield* sql` INSERT INTO orchestration_events ( event_id, aggregate_kind, stream_id, stream_version, event_type, occurred_at, actor_kind, payload_json, metadata_json, application_event_version ) VALUES ( ${`deleted:${version}`}, 'thread', 'thread:deleted-twice', ${version}, 'thread.deleted', ${now}, 'server', '{}', '{}', 2 ) `; } assert.equal((yield* maintenance.compactEventStore()).deletedEventCount, 1); const items = yield* subscribeOrchestrationV2Thread({ threadId, afterSequence: first!.sequence - 1, requestCompletionMarker: true, }).pipe( Effect.flatMap((stream) => stream.pipe( Stream.takeUntil((item) => item.kind === "synchronized"), Stream.runCollect, ), ), Effect.provideService(ThreadManagement.ThreadManagementService, { ensureLegacyTranscript: () => Effect.void, streamStoredEventsFrom: () => Stream.never, } as unknown as ThreadManagement.ThreadManagementService["Service"]), ); assert.deepEqual( Array.from(items, (item) => item.kind === "event" ? [item.sequence, item.event.type === "message.updated" ? item.event.payload.text : null] : [item.kind], ), [ [first!.sequence, "a created"], [second!.sequence, "b created"], [head!.sequence, "a final"], ["synchronized"], ], ); }).pipe( Effect.provide( ProjectionMaintenance.layer.pipe( Layer.provide(Layer.merge(EventStore.layer, ProjectionStore.layer)), Layer.provideMerge(ReplayTestLayer), ), ), ), );