import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer";import * as NodeSocket from "@effect/platform-node/NodeSocket";import * as NodeServices from "@effect/platform-node/NodeServices";import * as NodeCrypto from "node:crypto";import { HostProcessPlatform } from "@t3tools/shared/hostProcess"; import { AuthAccessTokenType, AuthEnvironmentBootstrapTokenType, AuthTokenExchangeGrantType, CommandId, DEFAULT_SERVER_SETTINGS, type DpopFailureReason, EnvironmentId, EventId, GitCommandError, KeybindingRule, MessageId, OpenError, ORCHESTRATION_V2_WS_METHODS, type OrchestrationThreadShell, TerminalNotRunningError, type OrchestrationCommand, type OrchestrationEvent, type OrchestrationV2ShellStreamItem, type OrchestrationV2ThreadStreamItem, ORCHESTRATION_WS_METHODS, type ProviderKind, ProjectId, ProviderDriverKind, ProviderInstanceId, ResolvedKeybindingRule, ThreadId, TurnId, WS_METHODS, WsRpcGroup, EditorId,} from "@t3tools/contracts";import { computeDpopAccessTokenHash, computeDpopJwkThumbprint, type DpopPublicJwk,} from "@t3tools/shared/dpop";import { RELAY_HEALTH_REQUEST_TYP, RELAY_MINT_REQUEST_TYP } from "@t3tools/shared/relayJwt";import * as RelayClient from "@t3tools/shared/relayClient";import { assert, it } from "@effect/vitest";import { assertFailure, assertInclude, assertTrue } from "@effect/vitest/utils";import { Deferred, Duration, Effect, FileSystem, Fiber, Layer, ManagedRuntime, Option, Path, Queue, Schema, Stream,} from "effect";import { FetchHttpClient, HttpBody, HttpClient, HttpClientRequest, HttpClientResponse, HttpRouter, HttpServer,} from "effect/unstable/http";import { OtlpSerialization, OtlpTracer } from "effect/unstable/observability";import { RpcClient, RpcSerialization } from "effect/unstable/rpc";import * as Socket from "effect/unstable/socket/Socket";import { vi } from "vitest";import { readFile } from "node:fs/promises"; const TEST_EPOCH = DateTime.makeUnsafe("1970-01-01T00:00:00.000Z"); import * as BackgroundPolicy from "./background/BackgroundPolicy.ts";import * as ServerConfig from "./config.ts";import { makeRoutesLayer } from "./server.ts";import { resolveAttachmentRelativePath } from "./attachmentPaths.ts";import { CheckpointDiffQuery, type CheckpointDiffQueryShape,} from "./checkpointing/Services/CheckpointDiffQuery.ts";import { GitCore, type GitCoreShape } from "./git/Services/GitCore.ts";import { GitManager, type GitManagerShape } from "./git/Services/GitManager.ts";import { GitStatusBroadcasterLive } from "./git/Layers/GitStatusBroadcaster.ts";import { GitStatusBroadcaster, type GitStatusBroadcasterShape,} from "./git/Services/GitStatusBroadcaster.ts";import { Keybindings, type KeybindingsShape } from "./keybindings.ts";import { Open, type OpenShape } from "./open.ts";import { OrchestrationEngineService, type OrchestrationEngineShape,} from "./orchestration/Services/OrchestrationEngine.ts";import { layerUnavailable as OrchestratorV2UnavailableLayer } from "./orchestration-v2/Orchestrator.ts";import { layer as ThreadManagementServiceLayer } from "./orchestration-v2/ThreadManagementService.ts";import { OrchestrationListenerCallbackError } from "./orchestration/Errors.ts";import { OrchestratorV2, layerUnavailable as OrchestratorV2Unavailable,} from "./orchestration-v2/Orchestrator.ts";import { CodexOrchestratorReplayHarness } from "./orchestration-v2/Adapters/CodexAdapterV2.testkit.ts";import { layer as idAllocatorV2Layer } from "./orchestration-v2/IdAllocator.ts";import { provideDeterministicTestRuntime } from "./orchestration-v2/testkit/DeterministicRuntime.ts";import { ORCHESTRATOR_REPLAY_FIXTURES } from "./orchestration-v2/testkit/fixtures/index.ts";import { materializeFixtureInput } from "./orchestration-v2/testkit/fixtures/shared.ts";import { makeOrchestratorV2ProviderReplayLayer } from "./orchestration-v2/testkit/ProviderReplayHarness.ts";import { decodeProviderReplayNdjson } from "./orchestration-v2/testkit/ReplayTranscriptNdjson.ts";import { OrchestrationListenerCallbackError } from "./orchestration/Errors.ts";import * as ProjectionSnapshotQuery from "./orchestration/Services/ProjectionSnapshotQuery.ts";import { ThreadDeletionReactor } from "./orchestration/Services/ThreadDeletionReactor.ts";import { SqlitePersistenceMemory } from "./persistence/Layers/Sqlite.ts";import { OrchestrationEventStoreLive } from "./persistence/Layers/OrchestrationEventStore.ts";import { OrchestrationEventStore } from "./persistence/Services/OrchestrationEventStore.ts";import { PersistenceSqlError } from "./persistence/Errors.ts";import * as ProviderRegistry from "./provider/Services/ProviderRegistry.ts";import { makeManualOnlyProviderMaintenanceCapabilities } from "./provider/providerMaintenance.ts";import * as ServerLifecycleEvents from "./serverLifecycleEvents.ts";import * as ServerRuntimeStartup from "./serverRuntimeStartup.ts";import * as ServiceLauncherClient from "./cloud/serviceLauncherClient.ts";import * as ServerSettings from "./serverSettings.ts";import * as TerminalManager from "./terminal/Manager.ts";import * as PreviewManager from "./preview/Manager.ts";import * as PortScanner from "./preview/PortScanner.ts";import * as BrowserTraceCollector from "./observability/BrowserTraceCollector.ts";import * as ProjectFaviconResolver from "./project/ProjectFaviconResolver.ts";import * as T3ProjectFileLoader from "./project/T3ProjectFileLoader.ts";import * as ProjectSetupScriptRunner from "./project/ProjectSetupScriptRunner.ts";import * as RepositoryIdentityResolver from "./project/RepositoryIdentityResolver.ts";import * as ServerEnvironment from "./environment/ServerEnvironment.ts";import { layer as mcpSessionRegistryTestLayer } from "./mcp/McpSessionRegistry.testkit.ts";import * as WorkspaceEntries from "./workspace/WorkspaceEntries.ts";import * as WorkspaceFileSystem from "./workspace/WorkspaceFileSystem.ts";import * as WorkspacePaths from "./workspace/WorkspacePaths.ts";import * as GitVcsDriver from "./vcs/GitVcsDriver.ts";import * as VcsDriver from "./vcs/VcsDriver.ts";import * as VcsStatusBroadcaster from "./vcs/VcsStatusBroadcaster.ts";import * as VcsDriverRegistry from "./vcs/VcsDriverRegistry.ts";import * as VcsProvisioningService from "./vcs/VcsProvisioningService.ts";import * as GitWorkflowService from "./git/GitWorkflowService.ts";import * as ReviewService from "./review/ReviewService.ts";import * as SourceControlRepositoryService from "./sourceControl/SourceControlRepositoryService.ts";import * as ServerSecretStore from "./auth/ServerSecretStore.ts";import * as EnvironmentAuth from "./auth/EnvironmentAuth.ts";import * as CloudManagedEndpointRuntime from "./cloud/ManagedEndpointRuntime.ts";import * as CloudCliTokenManager from "./cloud/CliTokenManager.ts";import * as ProcessDiagnostics from "./diagnostics/ProcessDiagnostics.ts";import * as ProcessResourceMonitor from "./diagnostics/ProcessResourceMonitor.ts";import * as TraceDiagnostics from "./diagnostics/TraceDiagnostics.ts";import * as DesktopTelemetryReceiver from "./resourceTelemetry/DesktopTelemetryReceiver.ts";import * as NativeTelemetryClient from "./resourceTelemetry/NativeTelemetryClient.ts";import * as ResourceAttribution from "./resourceTelemetry/ResourceAttribution.ts";import * as ResourceTelemetry from "./resourceTelemetry/ResourceTelemetry.ts";import * as UsageService from "./usage/UsageService.ts";import * as Data from "effect/Data"; const OrchestrationV2UnavailableTestLayer = Layer.merge( OrchestratorV2UnavailableLayer, ThreadManagementServiceLayer.pipe(Layer.provide(OrchestratorV2UnavailableLayer)),); const defaultProjectId = ProjectId.make("project-default");const defaultThreadId = ThreadId.make("thread-default");const defaultDesktopBootstrapToken = "test-desktop-bootstrap-token";const defaultModelSelection = { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5-codex",} as const; const makeLiveToolActivityEvent = ( sequence: number, kind: "tool.updated" | "tool.completed" = "tool.updated", options: { readonly toolCallId?: string; readonly title?: string; readonly path?: string; } = {},): Extract<OrchestrationEvent, { type: "thread.activity-appended" }> => { const { toolCallId = "call-edit", title = "Editing app.ts", path = "src/app.ts" } = options; const activity: OrchestrationThreadActivity = { id: EventId.make(`activity-${sequence}`), tone: "tool", kind, summary: title, payload: { itemType: "file_change", title, data: { toolCallId, path }, }, turnId: TurnId.make("turn-edit"), createdAt: "2026-01-01T00:00:01.000Z", }; return { sequence, eventId: EventId.make(`event-tool-${sequence}`), aggregateKind: "thread", aggregateId: defaultThreadId, occurredAt: "2026-01-01T00:00:01.000Z", commandId: null, causationEventId: null, correlationId: null, metadata: {}, type: "thread.activity-appended", payload: { threadId: defaultThreadId, activity }, };};const testEnvironmentDescriptor = { environmentId: EnvironmentId.make("environment-test"), label: "Test environment", platform: { os: "darwin" as const, arch: "arm64" as const, }, serverVersion: "0.0.0-test", capabilities: { repositoryIdentity: true, },};const makeDefaultOrchestrationReadModel = () => { const now = "2026-01-01T00:00:00.000Z"; return { snapshotSequence: 0, updatedAt: now, projects: [ { id: defaultProjectId, title: "Default Project", workspaceRoot: "/tmp/default-project", defaultModelSelection, scripts: [], createdAt: now, updatedAt: now, deletedAt: null, }, ], threads: [ { id: defaultThreadId, projectId: defaultProjectId, title: "Default Thread", modelSelection: defaultModelSelection, interactionMode: "default" as const, runtimeMode: "full-access" as const, branch: null, worktreePath: null, createdAt: now, updatedAt: now, archivedAt: null, settledOverride: null, settledAt: null, latestTurn: null, messages: [], session: null, activities: [], proposedPlans: [], checkpoints: [], deletedAt: null, }, ], };}; const makeDefaultOrchestrationThreadShell = ( overrides: Partial<OrchestrationThreadShell> = {},): OrchestrationThreadShell => { const now = "2026-01-01T00:00:00.000Z"; return { id: defaultThreadId, projectId: defaultProjectId, title: "Default Thread", modelSelection: defaultModelSelection, runtimeMode: "full-access", interactionMode: "default", branch: null, worktreePath: null, latestTurn: null, createdAt: now, updatedAt: now, archivedAt: null, settledOverride: null, settledAt: null, session: null, latestUserMessageAt: null, hasPendingApprovals: false, hasPendingUserInput: false, hasActionableProposedPlan: false, ...overrides, };}; const browserOtlpTracingLayer = Layer.mergeAll( FetchHttpClient.layer, OtlpSerialization.layerJson, Layer.succeed(HttpClient.TracerDisabledWhen, () => true),); const makeAuthTestLayer = () => EnvironmentAuth.layer.pipe( Layer.provide(SqlitePersistenceMemory), Layer.provide(ServerSecretStore.layer), Layer.provide( Layer.mock(ServerEnvironment.ServerEnvironmentIdentity)({ getEnvironmentId: Effect.succeed(testEnvironmentDescriptor.environmentId), }), ), ); const makeBrowserOtlpPayload = (spanName: string) => Effect.gen(function* () { const collector = yield* Effect.acquireRelease( Effect.promise(async () => { const NodeHttp = await import("node:http"); return await new Promise<{ readonly close: () => Promise<void>; readonly firstRequest: Promise<{ readonly body: string; readonly contentType: string | null; }>; readonly url: string; }>((resolve, reject) => { let resolveFirstRequest: | ((request: { readonly body: string; readonly contentType: string | null }) => void) | undefined; const firstRequest = new Promise<{ readonly body: string; readonly contentType: string | null; }>((resolveRequest) => { resolveFirstRequest = resolveRequest; }); const server = NodeHttp.createServer((request, response) => { const chunks: Buffer[] = []; request.on("data", (chunk) => { chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)); }); request.on("end", () => { resolveFirstRequest?.({ body: Buffer.concat(chunks).toString("utf8"), contentType: request.headers["content-type"] ?? null, }); resolveFirstRequest = undefined; response.statusCode = 204; response.end(); }); }); server.on("error", reject); server.listen(0, "127.0.0.1", () => { const address = server.address(); if (!address || typeof address === "string") { reject(new Error("Expected TCP collector address")); return; } resolve({ url: `http://127.0.0.1:${address.port}/v1/traces`, firstRequest, close: () => new Promise<void>((resolveClose, rejectClose) => { server.close((error) => { if (error) { rejectClose(error); return; } resolveClose(); }); }), }); }); }); }), ({ close }) => Effect.promise(close), ); const runtime = ManagedRuntime.make( OtlpTracer.layer({ url: collector.url, exportInterval: "10 millis", resource: { serviceName: "t3-web", attributes: { "service.runtime": "t3-web", "service.mode": "browser", "service.version": "test", }, }, }).pipe(Layer.provide(browserOtlpTracingLayer)), ); try { yield* Effect.promise(() => runtime.runPromise(Effect.void.pipe(Effect.withSpan(spanName)))); } finally { yield* Effect.promise(() => runtime.dispose()); } const request = yield* Effect.raceFirst( Effect.promise(() => collector.firstRequest).pipe(Effect.orDie), Effect.sleep(Duration.seconds(1)).pipe( Effect.andThen(Effect.die(new Error("Timed out waiting for OTLP trace export"))), ), ); // @effect-diagnostics-next-line preferSchemaOverJson:off return JSON.parse(request.body) as OtlpTracer.TraceData; }); const buildAppUnderTest = (options?: { config?: Partial<ServerConfig.ServerConfig["Service"]>; layers?: { keybindings?: Partial<KeybindingsShape>; providerRegistry?: Partial<ProviderRegistryShape>; serverSettings?: Partial<ServerSettingsShape>; open?: Partial<OpenShape>; gitCore?: Partial<GitCoreShape>; gitManager?: Partial<GitManagerShape>; gitStatusBroadcaster?: Partial<GitStatusBroadcasterShape>; projectSetupScriptRunner?: Partial<ProjectSetupScriptRunnerShape>; terminalManager?: Partial<TerminalManagerShape>; orchestrationEngine?: Partial<OrchestrationEngineShape>; projectionSnapshotQuery?: Partial<ProjectionSnapshotQueryShape>; checkpointDiffQuery?: Partial<CheckpointDiffQueryShape>; browserTraceCollector?: Partial<BrowserTraceCollectorShape>; serverLifecycleEvents?: Partial<ServerLifecycleEventsShape>; serverRuntimeStartup?: Partial<ServerRuntimeStartupShape>; serverEnvironment?: Partial<ServerEnvironmentShape>; repositoryIdentityResolver?: Partial<RepositoryIdentityResolverShape>; orchestratorV2?: Layer.Layer<OrchestratorV2, never, never>; };}) => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; const tempBaseDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-router-test-" }); const baseDir = options?.config?.baseDir ?? tempBaseDir; const devUrl = options?.config?.devUrl; const derivedPaths = yield* ServerConfig.deriveServerPaths(baseDir, devUrl); const config: ServerConfig.ServerConfig["Service"] = { logLevel: "Info", traceMinLevel: "Info", traceTimingEnabled: true, traceBatchWindowMs: 200, traceMaxBytes: 10 * 1024 * 1024, traceMaxFiles: 10, otlpTracesUrl: undefined, otlpMetricsUrl: undefined, otlpExportIntervalMs: 10_000, otlpServiceName: "t3-server", mode: "desktop", port: 0, host: "127.0.0.1", cwd: process.cwd(), baseDir, ...derivedPaths, staticDir: undefined, devUrl, devAllowedOrigins: [], noBrowser: true, startupPresentation: "browser", desktopBootstrapToken: defaultDesktopBootstrapToken, autoBootstrapProjectFromCwd: false, logWebSocketEvents: false, tailscaleServeEnabled: false, tailscaleServePort: 443, ...options?.config, }; const layerConfig = ServerConfig.layer(config); const defaultVcsDriver: VcsDriver.VcsDriver["Service"] = { capabilities: { kind: "git", supportsWorktrees: true, supportsBookmarks: false, supportsAtomicSnapshot: false, supportsPushDefaultRemote: true, ignoreClassifier: "native", }, execute: () => Effect.succeed({ exitCode: ChildProcessSpawner.ExitCode(0), stdout: "", stderr: "", stdoutTruncated: false, stderrTruncated: false, }), detectRepository: () => Effect.succeed(null), isInsideWorkTree: () => Effect.succeed(false), listWorkspaceFiles: () => Effect.succeed({ paths: [], truncated: false, freshness: { source: "live-local", observedAt: TEST_EPOCH, expiresAt: Option.none(), }, }), listRemotes: () =>Lines 1–500 of 9650. Load more
import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer"; import * as NodeSocket from "@effect/platform-node/NodeSocket"; import * as NodeServices from "@effect/platform-node/NodeServices"; import * as NodeCrypto from "node:crypto"; import { HostProcessPlatform } from "@t3tools/shared/hostProcess"; import { AuthAccessTokenType, AuthEnvironmentBootstrapTokenType, AuthTokenExchangeGrantType, CommandId, DEFAULT_SERVER_SETTINGS, type DpopFailureReason, EnvironmentId, EventId, GitCommandError, KeybindingRule, MessageId, OpenError, ORCHESTRATION_V2_WS_METHODS, type OrchestrationThreadShell, TerminalNotRunningError, type OrchestrationCommand, type OrchestrationEvent, type OrchestrationV2ShellStreamItem, type OrchestrationV2ThreadStreamItem, ORCHESTRATION_WS_METHODS, type ProviderKind, ProjectId, ProviderDriverKind, ProviderInstanceId, ResolvedKeybindingRule, ThreadId, TurnId, WS_METHODS, WsRpcGroup, EditorId, } from "@t3tools/contracts"; import { computeDpopAccessTokenHash, computeDpopJwkThumbprint, type DpopPublicJwk, } from "@t3tools/shared/dpop"; import { RELAY_HEALTH_REQUEST_TYP, RELAY_MINT_REQUEST_TYP } from "@t3tools/shared/relayJwt"; import * as RelayClient from "@t3tools/shared/relayClient"; import { assert, it } from "@effect/vitest"; import { assertFailure, assertInclude, assertTrue } from "@effect/vitest/utils"; import { Deferred, Duration, Effect, FileSystem, Fiber, Layer, ManagedRuntime, Option, Path, Queue, Schema, Stream, } from "effect"; import { FetchHttpClient, HttpBody, HttpClient, HttpClientRequest, HttpClientResponse, HttpRouter, HttpServer, } from "effect/unstable/http"; import { OtlpSerialization, OtlpTracer } from "effect/unstable/observability"; import { RpcClient, RpcSerialization } from "effect/unstable/rpc"; import * as Socket from "effect/unstable/socket/Socket"; import { vi } from "vitest"; import { readFile } from "node:fs/promises"; const TEST_EPOCH = DateTime.makeUnsafe("1970-01-01T00:00:00.000Z"); import * as BackgroundPolicy from "./background/BackgroundPolicy.ts"; import * as ServerConfig from "./config.ts"; import { makeRoutesLayer } from "./server.ts"; import { resolveAttachmentRelativePath } from "./attachmentPaths.ts"; import { CheckpointDiffQuery, type CheckpointDiffQueryShape, } from "./checkpointing/Services/CheckpointDiffQuery.ts"; import { GitCore, type GitCoreShape } from "./git/Services/GitCore.ts"; import { GitManager, type GitManagerShape } from "./git/Services/GitManager.ts"; import { GitStatusBroadcasterLive } from "./git/Layers/GitStatusBroadcaster.ts"; import { GitStatusBroadcaster, type GitStatusBroadcasterShape, } from "./git/Services/GitStatusBroadcaster.ts"; import { Keybindings, type KeybindingsShape } from "./keybindings.ts"; import { Open, type OpenShape } from "./open.ts"; import { OrchestrationEngineService, type OrchestrationEngineShape, } from "./orchestration/Services/OrchestrationEngine.ts"; import { layerUnavailable as OrchestratorV2UnavailableLayer } from "./orchestration-v2/Orchestrator.ts"; import { layer as ThreadManagementServiceLayer } from "./orchestration-v2/ThreadManagementService.ts"; import { OrchestrationListenerCallbackError } from "./orchestration/Errors.ts"; import { OrchestratorV2, layerUnavailable as OrchestratorV2Unavailable, } from "./orchestration-v2/Orchestrator.ts"; import { CodexOrchestratorReplayHarness } from "./orchestration-v2/Adapters/CodexAdapterV2.testkit.ts"; import { layer as idAllocatorV2Layer } from "./orchestration-v2/IdAllocator.ts"; import { provideDeterministicTestRuntime } from "./orchestration-v2/testkit/DeterministicRuntime.ts"; import { ORCHESTRATOR_REPLAY_FIXTURES } from "./orchestration-v2/testkit/fixtures/index.ts"; import { materializeFixtureInput } from "./orchestration-v2/testkit/fixtures/shared.ts"; import { makeOrchestratorV2ProviderReplayLayer } from "./orchestration-v2/testkit/ProviderReplayHarness.ts"; import { decodeProviderReplayNdjson } from "./orchestration-v2/testkit/ReplayTranscriptNdjson.ts"; import { OrchestrationListenerCallbackError } from "./orchestration/Errors.ts"; import * as ProjectionSnapshotQuery from "./orchestration/Services/ProjectionSnapshotQuery.ts"; import { ThreadDeletionReactor } from "./orchestration/Services/ThreadDeletionReactor.ts"; import { SqlitePersistenceMemory } from "./persistence/Layers/Sqlite.ts"; import { OrchestrationEventStoreLive } from "./persistence/Layers/OrchestrationEventStore.ts"; import { OrchestrationEventStore } from "./persistence/Services/OrchestrationEventStore.ts"; import { PersistenceSqlError } from "./persistence/Errors.ts"; import * as ProviderRegistry from "./provider/Services/ProviderRegistry.ts"; import { makeManualOnlyProviderMaintenanceCapabilities } from "./provider/providerMaintenance.ts"; import * as ServerLifecycleEvents from "./serverLifecycleEvents.ts"; import * as ServerRuntimeStartup from "./serverRuntimeStartup.ts"; import * as ServiceLauncherClient from "./cloud/serviceLauncherClient.ts"; import * as ServerSettings from "./serverSettings.ts"; import * as TerminalManager from "./terminal/Manager.ts"; import * as PreviewManager from "./preview/Manager.ts"; import * as PortScanner from "./preview/PortScanner.ts"; import * as BrowserTraceCollector from "./observability/BrowserTraceCollector.ts"; import * as ProjectFaviconResolver from "./project/ProjectFaviconResolver.ts"; import * as T3ProjectFileLoader from "./project/T3ProjectFileLoader.ts"; import * as ProjectSetupScriptRunner from "./project/ProjectSetupScriptRunner.ts"; import * as RepositoryIdentityResolver from "./project/RepositoryIdentityResolver.ts"; import * as ServerEnvironment from "./environment/ServerEnvironment.ts"; import { layer as mcpSessionRegistryTestLayer } from "./mcp/McpSessionRegistry.testkit.ts"; import * as WorkspaceEntries from "./workspace/WorkspaceEntries.ts"; import * as WorkspaceFileSystem from "./workspace/WorkspaceFileSystem.ts"; import * as WorkspacePaths from "./workspace/WorkspacePaths.ts"; import * as GitVcsDriver from "./vcs/GitVcsDriver.ts"; import * as VcsDriver from "./vcs/VcsDriver.ts"; import * as VcsStatusBroadcaster from "./vcs/VcsStatusBroadcaster.ts"; import * as VcsDriverRegistry from "./vcs/VcsDriverRegistry.ts"; import * as VcsProvisioningService from "./vcs/VcsProvisioningService.ts"; import * as GitWorkflowService from "./git/GitWorkflowService.ts"; import * as ReviewService from "./review/ReviewService.ts"; import * as SourceControlRepositoryService from "./sourceControl/SourceControlRepositoryService.ts"; import * as ServerSecretStore from "./auth/ServerSecretStore.ts"; import * as EnvironmentAuth from "./auth/EnvironmentAuth.ts"; import * as CloudManagedEndpointRuntime from "./cloud/ManagedEndpointRuntime.ts"; import * as CloudCliTokenManager from "./cloud/CliTokenManager.ts"; import * as ProcessDiagnostics from "./diagnostics/ProcessDiagnostics.ts"; import * as ProcessResourceMonitor from "./diagnostics/ProcessResourceMonitor.ts"; import * as TraceDiagnostics from "./diagnostics/TraceDiagnostics.ts"; import * as DesktopTelemetryReceiver from "./resourceTelemetry/DesktopTelemetryReceiver.ts"; import * as NativeTelemetryClient from "./resourceTelemetry/NativeTelemetryClient.ts"; import * as ResourceAttribution from "./resourceTelemetry/ResourceAttribution.ts"; import * as ResourceTelemetry from "./resourceTelemetry/ResourceTelemetry.ts"; import * as UsageService from "./usage/UsageService.ts"; import * as Data from "effect/Data"; const OrchestrationV2UnavailableTestLayer = Layer.merge( OrchestratorV2UnavailableLayer, ThreadManagementServiceLayer.pipe(Layer.provide(OrchestratorV2UnavailableLayer)), ); const defaultProjectId = ProjectId.make("project-default"); const defaultThreadId = ThreadId.make("thread-default"); const defaultDesktopBootstrapToken = "test-desktop-bootstrap-token"; const defaultModelSelection = { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5-codex", } as const; const makeLiveToolActivityEvent = ( sequence: number, kind: "tool.updated" | "tool.completed" = "tool.updated", options: { readonly toolCallId?: string; readonly title?: string; readonly path?: string; } = {}, ): Extract<OrchestrationEvent, { type: "thread.activity-appended" }> => { const { toolCallId = "call-edit", title = "Editing app.ts", path = "src/app.ts" } = options; const activity: OrchestrationThreadActivity = { id: EventId.make(`activity-${sequence}`), tone: "tool", kind, summary: title, payload: { itemType: "file_change", title, data: { toolCallId, path }, }, turnId: TurnId.make("turn-edit"), createdAt: "2026-01-01T00:00:01.000Z", }; return { sequence, eventId: EventId.make(`event-tool-${sequence}`), aggregateKind: "thread", aggregateId: defaultThreadId, occurredAt: "2026-01-01T00:00:01.000Z", commandId: null, causationEventId: null, correlationId: null, metadata: {}, type: "thread.activity-appended", payload: { threadId: defaultThreadId, activity }, }; }; const testEnvironmentDescriptor = { environmentId: EnvironmentId.make("environment-test"), label: "Test environment", platform: { os: "darwin" as const, arch: "arm64" as const, }, serverVersion: "0.0.0-test", capabilities: { repositoryIdentity: true, }, }; const makeDefaultOrchestrationReadModel = () => { const now = "2026-01-01T00:00:00.000Z"; return { snapshotSequence: 0, updatedAt: now, projects: [ { id: defaultProjectId, title: "Default Project", workspaceRoot: "/tmp/default-project", defaultModelSelection, scripts: [], createdAt: now, updatedAt: now, deletedAt: null, }, ], threads: [ { id: defaultThreadId, projectId: defaultProjectId, title: "Default Thread", modelSelection: defaultModelSelection, interactionMode: "default" as const, runtimeMode: "full-access" as const, branch: null, worktreePath: null, createdAt: now, updatedAt: now, archivedAt: null, settledOverride: null, settledAt: null, latestTurn: null, messages: [], session: null, activities: [], proposedPlans: [], checkpoints: [], deletedAt: null, }, ], }; }; const makeDefaultOrchestrationThreadShell = ( overrides: Partial<OrchestrationThreadShell> = {}, ): OrchestrationThreadShell => { const now = "2026-01-01T00:00:00.000Z"; return { id: defaultThreadId, projectId: defaultProjectId, title: "Default Thread", modelSelection: defaultModelSelection, runtimeMode: "full-access", interactionMode: "default", branch: null, worktreePath: null, latestTurn: null, createdAt: now, updatedAt: now, archivedAt: null, settledOverride: null, settledAt: null, session: null, latestUserMessageAt: null, hasPendingApprovals: false, hasPendingUserInput: false, hasActionableProposedPlan: false, ...overrides, }; }; const browserOtlpTracingLayer = Layer.mergeAll( FetchHttpClient.layer, OtlpSerialization.layerJson, Layer.succeed(HttpClient.TracerDisabledWhen, () => true), ); const makeAuthTestLayer = () => EnvironmentAuth.layer.pipe( Layer.provide(SqlitePersistenceMemory), Layer.provide(ServerSecretStore.layer), Layer.provide( Layer.mock(ServerEnvironment.ServerEnvironmentIdentity)({ getEnvironmentId: Effect.succeed(testEnvironmentDescriptor.environmentId), }), ), ); const makeBrowserOtlpPayload = (spanName: string) => Effect.gen(function* () { const collector = yield* Effect.acquireRelease( Effect.promise(async () => { const NodeHttp = await import("node:http"); return await new Promise<{ readonly close: () => Promise<void>; readonly firstRequest: Promise<{ readonly body: string; readonly contentType: string | null; }>; readonly url: string; }>((resolve, reject) => { let resolveFirstRequest: | ((request: { readonly body: string; readonly contentType: string | null }) => void) | undefined; const firstRequest = new Promise<{ readonly body: string; readonly contentType: string | null; }>((resolveRequest) => { resolveFirstRequest = resolveRequest; }); const server = NodeHttp.createServer((request, response) => { const chunks: Buffer[] = []; request.on("data", (chunk) => { chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)); }); request.on("end", () => { resolveFirstRequest?.({ body: Buffer.concat(chunks).toString("utf8"), contentType: request.headers["content-type"] ?? null, }); resolveFirstRequest = undefined; response.statusCode = 204; response.end(); }); }); server.on("error", reject); server.listen(0, "127.0.0.1", () => { const address = server.address(); if (!address || typeof address === "string") { reject(new Error("Expected TCP collector address")); return; } resolve({ url: `http://127.0.0.1:${address.port}/v1/traces`, firstRequest, close: () => new Promise<void>((resolveClose, rejectClose) => { server.close((error) => { if (error) { rejectClose(error); return; } resolveClose(); }); }), }); }); }); }), ({ close }) => Effect.promise(close), ); const runtime = ManagedRuntime.make( OtlpTracer.layer({ url: collector.url, exportInterval: "10 millis", resource: { serviceName: "t3-web", attributes: { "service.runtime": "t3-web", "service.mode": "browser", "service.version": "test", }, }, }).pipe(Layer.provide(browserOtlpTracingLayer)), ); try { yield* Effect.promise(() => runtime.runPromise(Effect.void.pipe(Effect.withSpan(spanName)))); } finally { yield* Effect.promise(() => runtime.dispose()); } const request = yield* Effect.raceFirst( Effect.promise(() => collector.firstRequest).pipe(Effect.orDie), Effect.sleep(Duration.seconds(1)).pipe( Effect.andThen(Effect.die(new Error("Timed out waiting for OTLP trace export"))), ), ); // @effect-diagnostics-next-line preferSchemaOverJson:off return JSON.parse(request.body) as OtlpTracer.TraceData; }); const buildAppUnderTest = (options?: { config?: Partial<ServerConfig.ServerConfig["Service"]>; layers?: { keybindings?: Partial<KeybindingsShape>; providerRegistry?: Partial<ProviderRegistryShape>; serverSettings?: Partial<ServerSettingsShape>; open?: Partial<OpenShape>; gitCore?: Partial<GitCoreShape>; gitManager?: Partial<GitManagerShape>; gitStatusBroadcaster?: Partial<GitStatusBroadcasterShape>; projectSetupScriptRunner?: Partial<ProjectSetupScriptRunnerShape>; terminalManager?: Partial<TerminalManagerShape>; orchestrationEngine?: Partial<OrchestrationEngineShape>; projectionSnapshotQuery?: Partial<ProjectionSnapshotQueryShape>; checkpointDiffQuery?: Partial<CheckpointDiffQueryShape>; browserTraceCollector?: Partial<BrowserTraceCollectorShape>; serverLifecycleEvents?: Partial<ServerLifecycleEventsShape>; serverRuntimeStartup?: Partial<ServerRuntimeStartupShape>; serverEnvironment?: Partial<ServerEnvironmentShape>; repositoryIdentityResolver?: Partial<RepositoryIdentityResolverShape>; orchestratorV2?: Layer.Layer<OrchestratorV2, never, never>; }; }) => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; const tempBaseDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-router-test-" }); const baseDir = options?.config?.baseDir ?? tempBaseDir; const devUrl = options?.config?.devUrl; const derivedPaths = yield* ServerConfig.deriveServerPaths(baseDir, devUrl); const config: ServerConfig.ServerConfig["Service"] = { logLevel: "Info", traceMinLevel: "Info", traceTimingEnabled: true, traceBatchWindowMs: 200, traceMaxBytes: 10 * 1024 * 1024, traceMaxFiles: 10, otlpTracesUrl: undefined, otlpMetricsUrl: undefined, otlpExportIntervalMs: 10_000, otlpServiceName: "t3-server", mode: "desktop", port: 0, host: "127.0.0.1", cwd: process.cwd(), baseDir, ...derivedPaths, staticDir: undefined, devUrl, devAllowedOrigins: [], noBrowser: true, startupPresentation: "browser", desktopBootstrapToken: defaultDesktopBootstrapToken, autoBootstrapProjectFromCwd: false, logWebSocketEvents: false, tailscaleServeEnabled: false, tailscaleServePort: 443, ...options?.config, }; const layerConfig = ServerConfig.layer(config); const defaultVcsDriver: VcsDriver.VcsDriver["Service"] = { capabilities: { kind: "git", supportsWorktrees: true, supportsBookmarks: false, supportsAtomicSnapshot: false, supportsPushDefaultRemote: true, ignoreClassifier: "native", }, execute: () => Effect.succeed({ exitCode: ChildProcessSpawner.ExitCode(0), stdout: "", stderr: "", stdoutTruncated: false, stderrTruncated: false, }), detectRepository: () => Effect.succeed(null), isInsideWorkTree: () => Effect.succeed(false), listWorkspaceFiles: () => Effect.succeed({ paths: [], truncated: false, freshness: { source: "live-local", observedAt: TEST_EPOCH, expiresAt: Option.none(), }, }), listRemotes: () =>