diff --git a/apps/server/scripts/acp-replay-agent.ts b/apps/server/scripts/acp-replay-agent.ts index f0cd0da1da..705d25681c 100644 --- a/apps/server/scripts/acp-replay-agent.ts +++ b/apps/server/scripts/acp-replay-agent.ts @@ -1,7 +1,7 @@ #!/usr/bin/env node // @effect-diagnostics nodeBuiltinImport:off -import { writeFileSync } from "node:fs"; -import { createInterface } from "node:readline"; +import * as NodeFS from "node:fs"; +import * as NodeReadline from "node:readline"; interface ReplayEntry { readonly type: "emit_inbound" | "expect_outbound" | "runtime_exit"; @@ -54,7 +54,7 @@ const pendingClientRequestIds = new Map(); const pendingAgentRequestMethods = new Map(); function writeStatus(failure?: unknown): void { - writeFileSync( + NodeFS.writeFileSync( replayStatusPath, JSON.stringify({ scenario: transcript.scenario, @@ -268,7 +268,7 @@ function handleMessage(message: JsonRpcMessage): void { writeStatus(); flushInbound(); -const input = createInterface({ input: process.stdin, crlfDelay: Infinity }); +const input = NodeReadline.createInterface({ input: process.stdin, crlfDelay: Infinity }); input.on("line", (line) => { if (stopped || line.trim().length === 0) return; try { diff --git a/apps/server/scripts/record-codex-app-server-replay-fixture.ts b/apps/server/scripts/record-codex-app-server-replay-fixture.ts index f7f227d7d1..026703d111 100644 --- a/apps/server/scripts/record-codex-app-server-replay-fixture.ts +++ b/apps/server/scripts/record-codex-app-server-replay-fixture.ts @@ -1067,7 +1067,7 @@ function installReplayHandlers({ startCommandExecution, beforeApprovalResponse, }: { - readonly client: CodexClient.CodexAppServerClientShape; + readonly client: CodexClient.CodexAppServerClient["Service"]; readonly startTurn: (turnId: string) => Effect.Effect; readonly completeTurn: (turnId: string) => Effect.Effect; readonly startCommandExecution: (turnId: string) => Effect.Effect; @@ -1222,7 +1222,7 @@ function runReplaySession({ }); const runTurnStep = ( - client: CodexClient.CodexAppServerClientShape, + client: CodexClient.CodexAppServerClient["Service"], threadId: string, step: TurnReplayStep, ) => diff --git a/apps/server/src/orchestration-v2/AcpRegistryOrchestratorV2.live.test.ts b/apps/server/src/orchestration-v2/AcpRegistryOrchestratorV2.live.test.ts index cfa967823f..159b2069fa 100644 --- a/apps/server/src/orchestration-v2/AcpRegistryOrchestratorV2.live.test.ts +++ b/apps/server/src/orchestration-v2/AcpRegistryOrchestratorV2.live.test.ts @@ -15,7 +15,7 @@ import * as Layer from "effect/Layer"; import { FetchHttpClient } from "effect/unstable/http"; import { describe } from "vite-plus/test"; -import { CheckpointStoreLive } from "../checkpointing/Layers/CheckpointStore.ts"; +import * as CheckpointStore from "../checkpointing/CheckpointStore.ts"; import { ServerConfig } from "../config.ts"; import { SqlitePersistenceMemory } from "../persistence/Layers/Sqlite.ts"; import { ProviderInstanceRegistryHydrationLive } from "../provider/Layers/ProviderInstanceRegistryHydration.ts"; @@ -48,7 +48,7 @@ const vcsDriverRegistryLayer = VcsDriverRegistry.layer.pipe( Layer.provide(NodeServices.layer), ); -const checkpointStoreLayer = CheckpointStoreLive.pipe(Layer.provide(vcsDriverRegistryLayer)); +const checkpointStoreLayer = CheckpointStore.layer.pipe(Layer.provide(vcsDriverRegistryLayer)); const serverSettingsLayer = ServerSettingsService.layerTest({ providerInstances: { diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts index c343542336..f4fd97a677 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts @@ -24,7 +24,7 @@ import * as TestClock from "effect/testing/TestClock"; import { ChildProcessSpawner } from "effect/unstable/process"; import { ServerConfig } from "../../config.ts"; -import { AcpSessionRuntime } from "../../provider/acp/AcpSessionRuntime.ts"; +import * as AcpSessionRuntime from "../../provider/acp/AcpSessionRuntime.ts"; import { layer as idAllocatorLayer, IdAllocatorV2 } from "../IdAllocator.ts"; import { ProviderAdapterV2RuntimePolicy, @@ -66,7 +66,9 @@ function makeMockRuntime(input: { ), ), ); - return yield* Effect.service(AcpSessionRuntime).pipe(Effect.provide(context)); + return yield* Effect.service(AcpSessionRuntime.AcpSessionRuntime).pipe( + Effect.provide(context), + ); }); } diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.testkit.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.testkit.ts index c51b099308..5b3c51f787 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.testkit.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.testkit.ts @@ -11,10 +11,7 @@ import type * as Scope from "effect/Scope"; import { ChildProcessSpawner } from "effect/unstable/process"; import * as EffectAcpErrors from "effect-acp/errors"; -import { - AcpSessionRuntime, - type AcpSessionRuntimeShape, -} from "../../provider/acp/AcpSessionRuntime.ts"; +import * as AcpSessionRuntime from "../../provider/acp/AcpSessionRuntime.ts"; import { ACP_PROTOCOL, type AcpAdapterV2RuntimeInput } from "./AcpAdapterV2.ts"; export const AcpReplayTranscript = Schema.Struct({ @@ -155,7 +152,11 @@ export function makeAcpReplayRuntime(input: { readonly childProcessSpawner: ChildProcessSpawner.ChildProcessSpawner["Service"]; }): ( runtimeInput: AcpAdapterV2RuntimeInput, -) => Effect.Effect { +) => Effect.Effect< + AcpSessionRuntime.AcpSessionRuntime["Service"], + EffectAcpErrors.AcpError, + Scope.Scope +> { const encodedTranscript = Buffer.from(JSON.stringify(input.transcript), "utf8").toString( "base64", ); @@ -182,6 +183,8 @@ export function makeAcpReplayRuntime(input: { ), ), ); - return yield* Effect.service(AcpSessionRuntime).pipe(Effect.provide(context)); + return yield* Effect.service(AcpSessionRuntime.AcpSessionRuntime).pipe( + Effect.provide(context), + ); }); } diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.ts index 55d2e873ce..1a21d33d74 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.ts @@ -35,7 +35,7 @@ import type * as EffectAcpProtocol from "effect-acp/protocol"; import type * as EffectAcpSchema from "effect-acp/schema"; import { resolveAttachmentPath } from "../../attachmentStore.ts"; -import { type ServerConfigShape } from "../../config.ts"; +import { ServerConfig } from "../../config.ts"; import * as McpProviderSession from "../../mcp/McpProviderSession.ts"; import { mergeToolCallState, @@ -46,9 +46,9 @@ import { } from "../../provider/acp/AcpRuntimeModel.ts"; import type { AcpSessionRuntimeOptions, - AcpSessionRuntimeShape, AcpSessionRuntimeStartResult, } from "../../provider/acp/AcpSessionRuntime.ts"; +import * as AcpSessionRuntime from "../../provider/acp/AcpSessionRuntime.ts"; import { IdAllocatorV2, type IdAllocatorV2Shape } from "../IdAllocator.ts"; import { ProviderAdapterEnsureThreadError, @@ -90,7 +90,7 @@ export interface AcpAdapterV2UserInputRequest { } export interface AcpAdapterV2ExtensionContext { - readonly runtime: AcpSessionRuntimeShape; + readonly runtime: AcpSessionRuntime.AcpSessionRuntime["Service"]; readonly requestUserInput: ( input: AcpAdapterV2UserInputRequest, ) => Effect.Effect; @@ -101,7 +101,11 @@ export interface AcpAdapterV2Flavor { readonly capabilities: OrchestrationV2ProviderCapabilities; readonly makeRuntime: ( input: AcpAdapterV2RuntimeInput, - ) => Effect.Effect; + ) => Effect.Effect< + AcpSessionRuntime.AcpSessionRuntime["Service"], + EffectAcpErrors.AcpError, + Scope.Scope + >; readonly resolveModelId?: (selection: ModelSelection) => string | undefined; readonly registerExtensions?: ( context: AcpAdapterV2ExtensionContext, @@ -114,7 +118,7 @@ export interface AcpAdapterV2Options { readonly flavor: AcpAdapterV2Flavor; readonly fileSystem: FileSystem.FileSystem; readonly idAllocator: IdAllocatorV2Shape; - readonly serverConfig: ServerConfigShape; + readonly serverConfig: ServerConfig["Service"]; } export const AcpProviderCapabilitiesV2 = { diff --git a/apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.ts index 13a8c40ea8..ecc59dc162 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.ts @@ -19,10 +19,7 @@ import { makeAcpRegistryResolver, type AcpRegistryResolverShape, } from "../../provider/acp/AcpRegistrySupport.ts"; -import { - AcpSessionRuntime, - type AcpSessionRuntimeShape, -} from "../../provider/acp/AcpSessionRuntime.ts"; +import * as AcpSessionRuntime from "../../provider/acp/AcpSessionRuntime.ts"; import { mergeProviderInstanceEnvironment } from "../../provider/ProviderInstanceEnvironment.ts"; import { IdAllocatorV2 } from "../IdAllocator.ts"; import { @@ -55,14 +52,22 @@ export interface AcpRegistryAdapterV2Options { readonly serverConfig: ServerConfig["Service"]; readonly makeRuntime?: ( input: AcpAdapterV2RuntimeInput, - ) => Effect.Effect; + ) => Effect.Effect< + AcpSessionRuntime.AcpSessionRuntime["Service"], + EffectAcpErrors.AcpError, + Scope.Scope + >; readonly assertComplete?: Effect.Effect; } function makeAcpRegistryRuntime(options: AcpRegistryAdapterV2Options) { return ( input: AcpAdapterV2RuntimeInput, - ): Effect.Effect => + ): Effect.Effect< + AcpSessionRuntime.AcpSessionRuntime["Service"], + EffectAcpErrors.AcpError, + Scope.Scope + > => Effect.gen(function* () { const resolved = yield* options.resolver .resolve(options.settings, input.cwd, options.environment) @@ -86,7 +91,9 @@ function makeAcpRegistryRuntime(options: AcpRegistryAdapterV2Options) { ), ), ); - return yield* Effect.service(AcpSessionRuntime).pipe(Effect.provide(context)); + return yield* Effect.service(AcpSessionRuntime.AcpSessionRuntime).pipe( + Effect.provide(context), + ); }); } diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.testkit.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.testkit.ts index c11dab1a79..2f025feb82 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.testkit.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.testkit.ts @@ -9,7 +9,7 @@ import * as Path from "effect/Path"; import * as PlatformError from "effect/PlatformError"; import * as Schema from "effect/Schema"; -import { ServerConfig, type ServerConfigShape } from "../../config.ts"; +import { ServerConfig } from "../../config.ts"; import { layer as idAllocatorLayer } from "../IdAllocator.ts"; import { ProviderAdapterOpenSessionError } from "../ProviderAdapter.ts"; import { layerFromProviderAdapter } from "../ProviderAdapterRegistry.ts"; @@ -51,7 +51,7 @@ function metadataFromTranscript(transcript: ProviderReplayTranscript): { function makeReplayServerConfig( scenario: string, ): Effect.Effect< - ServerConfigShape, + ServerConfig["Service"], PlatformError.PlatformError, FileSystem.FileSystem | Path.Path > { diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts index 30791b36f8..09a978c41e 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts @@ -41,7 +41,11 @@ import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"; import { resolveAttachmentPath } from "../../attachmentStore.ts"; import { ServerConfig } from "../../config.ts"; -import { CODEX_PLAN_MODE_DEVELOPER_INSTRUCTIONS } from "../../provider/codex/CodexDeveloperInstructions.ts"; +import { CODEX_PLAN_MODE_DEVELOPER_INSTRUCTIONS } from "../../provider/CodexDeveloperInstructions.ts"; +import { + materializeCodexShadowHome, + resolveCodexHomeLayout, +} from "../../provider/Drivers/CodexHomeLayout.ts"; import { type EventNdjsonLogger, makeEventNdjsonLogger, @@ -758,7 +762,7 @@ export interface CodexAppServerClientFactoryShape { readonly providerSessionId: OrchestrationV2ProviderSession["id"]; readonly runtimePolicy: ProviderAdapterV2RuntimePolicy; }) => Effect.Effect< - CodexClient.CodexAppServerClientShape, + CodexClient.CodexAppServerClient["Service"], ProviderAdapterOpenSessionError, Scope.Scope >; @@ -1056,7 +1060,7 @@ export interface CodexAdapterV2Options { readonly clientFactory: CodexAppServerClientFactoryShape; readonly fileSystem: FileSystem.FileSystem; readonly idAllocator: IdAllocatorV2Shape; - readonly serverConfig: ServerConfigShape; + readonly serverConfig: ServerConfig["Service"]; } export function makeCodexAdapterV2(adapterOptions: CodexAdapterV2Options): ProviderAdapterV2Shape { diff --git a/apps/server/src/orchestration-v2/Adapters/CursorAdapterV2.testkit.ts b/apps/server/src/orchestration-v2/Adapters/CursorAdapterV2.testkit.ts index c5f0ab5cfa..0e46276c79 100644 --- a/apps/server/src/orchestration-v2/Adapters/CursorAdapterV2.testkit.ts +++ b/apps/server/src/orchestration-v2/Adapters/CursorAdapterV2.testkit.ts @@ -13,7 +13,7 @@ import * as Path from "effect/Path"; import * as PlatformError from "effect/PlatformError"; import * as Schema from "effect/Schema"; -import { ServerConfig, type ServerConfigShape } from "../../config.ts"; +import { ServerConfig } from "../../config.ts"; import { layer as idAllocatorLayer } from "../IdAllocator.ts"; import { ProviderAdapterDriverCreateError } from "../ProviderAdapterDriver.ts"; import { makeDriverLayer as makeProviderAdapterRegistryDriverLayer } from "../ProviderAdapterRegistry.ts"; @@ -508,7 +508,7 @@ export function makeCursorAgentSdkReplayLayer( function makeReplayServerConfig( scenario: string, ): Effect.Effect< - ServerConfigShape, + ServerConfig["Service"], PlatformError.PlatformError, FileSystem.FileSystem | Path.Path > { diff --git a/apps/server/src/orchestration-v2/Adapters/CursorAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CursorAdapterV2.ts index 83372035de..eff2a11c4d 100644 --- a/apps/server/src/orchestration-v2/Adapters/CursorAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/CursorAdapterV2.ts @@ -39,7 +39,7 @@ import * as Schema from "effect/Schema"; import * as Stream from "effect/Stream"; import { resolveAttachmentPath } from "../../attachmentStore.ts"; -import { ServerConfig, type ServerConfigShape } from "../../config.ts"; +import { ServerConfig } from "../../config.ts"; import * as McpProviderSession from "../../mcp/McpProviderSession.ts"; import { cursorSdkParameterId } from "../../provider/cursorSdkModel.ts"; import { mergeProviderInstanceEnvironment } from "../../provider/ProviderInstanceEnvironment.ts"; @@ -818,7 +818,7 @@ export interface CursorAdapterV2Options { readonly fileSystem: FileSystem.FileSystem; readonly idAllocator: IdAllocatorV2Shape; readonly runner: CursorAgentSdkRunnerShape; - readonly serverConfig: ServerConfigShape; + readonly serverConfig: ServerConfig["Service"]; } export function makeCursorAdapterV2( diff --git a/apps/server/src/orchestration-v2/Adapters/GrokAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/GrokAdapterV2.ts index bd5caa0859..7757d603e3 100644 --- a/apps/server/src/orchestration-v2/Adapters/GrokAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/GrokAdapterV2.ts @@ -26,7 +26,7 @@ import { XAiAskUserQuestionRequest, } from "../../provider/acp/XAiAcpExtension.ts"; import { mergeProviderInstanceEnvironment } from "../../provider/ProviderInstanceEnvironment.ts"; -import type { AcpSessionRuntimeShape } from "../../provider/acp/AcpSessionRuntime.ts"; +import * as AcpSessionRuntime from "../../provider/acp/AcpSessionRuntime.ts"; import { IdAllocatorV2 } from "../IdAllocator.ts"; import { ProviderAdapterV2 } from "../ProviderAdapter.ts"; import { @@ -83,7 +83,11 @@ export interface GrokAdapterV2Options { readonly serverConfig: ServerConfig["Service"]; readonly makeRuntime?: ( input: AcpAdapterV2RuntimeInput, - ) => Effect.Effect; + ) => Effect.Effect< + AcpSessionRuntime.AcpSessionRuntime["Service"], + EffectAcpErrors.AcpError, + Scope.Scope + >; readonly assertComplete?: Effect.Effect; } diff --git a/apps/server/src/orchestration-v2/Adapters/OpenCodeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/OpenCodeAdapterV2.ts index 91303741e6..1255e9330e 100644 --- a/apps/server/src/orchestration-v2/Adapters/OpenCodeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/OpenCodeAdapterV2.ts @@ -49,7 +49,7 @@ import * as Scope from "effect/Scope"; import * as Stream from "effect/Stream"; import { resolveAttachmentPath } from "../../attachmentStore.ts"; -import { ServerConfig, type ServerConfigShape } from "../../config.ts"; +import { ServerConfig } from "../../config.ts"; import * as McpProviderSession from "../../mcp/McpProviderSession.ts"; import { mergeProviderInstanceEnvironment } from "../../provider/ProviderInstanceEnvironment.ts"; import { @@ -271,7 +271,7 @@ export interface OpenCodeAdapterV2Options { readonly environment: NodeJS.ProcessEnv; readonly runtime: OpenCodeRuntimeShape; readonly idAllocator: IdAllocatorV2Shape; - readonly serverConfig: ServerConfigShape; + readonly serverConfig: ServerConfig["Service"]; } function protocolError(detail: string, payload?: unknown): ProviderAdapterProtocolError { diff --git a/apps/server/src/orchestration-v2/CheckpointService.ts b/apps/server/src/orchestration-v2/CheckpointService.ts index f6d3db7306..cd2f55c053 100644 --- a/apps/server/src/orchestration-v2/CheckpointService.ts +++ b/apps/server/src/orchestration-v2/CheckpointService.ts @@ -9,6 +9,7 @@ import { RunId, ThreadId, } from "@t3tools/contracts"; +import * as NodeCrypto from "node:crypto"; import * as Context from "effect/Context"; import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; @@ -19,7 +20,7 @@ import * as Schema from "effect/Schema"; import * as Semaphore from "effect/Semaphore"; import { parseTurnDiffFilesFromUnifiedDiff } from "../checkpointing/Diffs.ts"; -import { CheckpointStore } from "../checkpointing/Services/CheckpointStore.ts"; +import * as CheckpointStore from "../checkpointing/CheckpointStore.ts"; import { IdAllocatorV2, type IdAllocatorV2Shape } from "./IdAllocator.ts"; const CHECKPOINT_REFS_PREFIX = "refs/t3/orchestration-v2/checkpoints"; @@ -30,7 +31,7 @@ export class CheckpointRootScopePrepareError extends Schema.TaggedErrorClass = - Layer.effect( - CheckpointServiceV2, - Effect.gen(function* () { - const checkpointStore = yield* CheckpointStore; - const idAllocator = yield* IdAllocatorV2; - const workspaceSemaphores = yield* Ref.make(new Map()); +export const layer: Layer.Layer< + CheckpointServiceV2, + never, + CheckpointStore.CheckpointStore | IdAllocatorV2 +> = Layer.effect( + CheckpointServiceV2, + Effect.gen(function* () { + const checkpointStore = yield* CheckpointStore.CheckpointStore; + const idAllocator = yield* IdAllocatorV2; + const workspaceSemaphores = yield* Ref.make(new Map()); - const getWorkspaceSemaphore = (cwd: string) => + const getWorkspaceSemaphore = (cwd: string) => + Effect.gen(function* () { + const existing = (yield* Ref.get(workspaceSemaphores)).get(cwd); + if (existing !== undefined) { + return existing; + } + + const created = yield* Semaphore.make(1); + return yield* Ref.modify(workspaceSemaphores, (current) => { + const concurrent = current.get(cwd); + if (concurrent !== undefined) { + return [concurrent, current]; + } + const updated = new Map(current); + updated.set(cwd, created); + return [created, updated]; + }); + }); + + const withWorkspaceLock = (cwd: string, effect: Effect.Effect) => + Effect.flatMap(getWorkspaceSemaphore(cwd), (semaphore) => semaphore.withPermits(1)(effect)); + + const isGitCheckpointable = (cwd: string) => + checkpointStore.isGitRepository(cwd).pipe(Effect.orElseSucceed(() => false)); + + const ensureScope: CheckpointServiceV2Shape["ensureScope"] = (scope) => Effect.succeed(scope); + + const captureBaseline: CheckpointServiceV2Shape["captureBaseline"] = (input) => + withWorkspaceLock( + input.scope.cwd, Effect.gen(function* () { - const existing = (yield* Ref.get(workspaceSemaphores)).get(cwd); - if (existing !== undefined) { - return existing; + if (!(yield* isGitCheckpointable(input.scope.cwd))) { + return; } - const created = yield* Semaphore.make(1); - return yield* Ref.modify(workspaceSemaphores, (current) => { - const concurrent = current.get(cwd); - if (concurrent !== undefined) { - return [concurrent, current]; - } - const updated = new Map(current); - updated.set(cwd, created); - return [created, updated]; + const checkpointRef = checkpointRefForScopeOrdinal({ + scopeId: input.scope.id, + ordinalWithinScope: input.ordinalWithinScope, }); - }); + const exists = yield* checkpointStore.hasCheckpointRef({ + cwd: input.scope.cwd, + checkpointRef, + }); + if (exists) { + return; + } - const withWorkspaceLock = (cwd: string, effect: Effect.Effect) => - Effect.flatMap(getWorkspaceSemaphore(cwd), (semaphore) => semaphore.withPermits(1)(effect)); - - const isGitCheckpointable = (cwd: string) => - checkpointStore.isGitRepository(cwd).pipe(Effect.orElseSucceed(() => false)); - - const ensureScope: CheckpointServiceV2Shape["ensureScope"] = (scope) => Effect.succeed(scope); - - const captureBaseline: CheckpointServiceV2Shape["captureBaseline"] = (input) => - withWorkspaceLock( - input.scope.cwd, - Effect.gen(function* () { - if (!(yield* isGitCheckpointable(input.scope.cwd))) { - return; - } - - const checkpointRef = checkpointRefForScopeOrdinal({ + yield* checkpointStore.captureCheckpoint({ + cwd: input.scope.cwd, + checkpointRef, + }); + }), + ).pipe( + Effect.mapError( + (cause) => + new CheckpointBaselineCaptureError({ scopeId: input.scope.id, ordinalWithinScope: input.ordinalWithinScope, - }); - const exists = yield* checkpointStore.hasCheckpointRef({ - cwd: input.scope.cwd, - checkpointRef, - }); - if (exists) { - return; - } + cause, + }), + ), + ); - yield* checkpointStore.captureCheckpoint({ - cwd: input.scope.cwd, - checkpointRef, - }); - }), - ).pipe( - Effect.mapError( - (cause) => - new CheckpointBaselineCaptureError({ - scopeId: input.scope.id, - ordinalWithinScope: input.ordinalWithinScope, - cause, - }), - ), - ); - - const capture: CheckpointServiceV2Shape["capture"] = (input) => - withWorkspaceLock( - input.scope.cwd, - Effect.gen(function* () { - const checkpointId = yield* checkpointIdForScopeOrdinal(idAllocator, { - scopeId: input.scope.id, - ordinalWithinScope: input.ordinalWithinScope, - }); - const parentCheckpointId = - input.ordinalWithinScope > 1 - ? yield* checkpointIdForScopeOrdinal(idAllocator, { - scopeId: input.scope.id, - ordinalWithinScope: input.ordinalWithinScope - 1, - }) - : null; - const checkpointRef = checkpointRefForScopeOrdinal({ - scopeId: input.scope.id, - ordinalWithinScope: input.ordinalWithinScope, - }); - const previousCheckpointRef = checkpointRefForScopeOrdinal({ - scopeId: input.scope.id, - ordinalWithinScope: Math.max(0, input.ordinalWithinScope - 1), - }); - - if (!(yield* isGitCheckpointable(input.scope.cwd))) { - return makeCheckpoint({ - id: checkpointId, - scope: input.scope, - runId: input.runId, - nodeId: input.nodeId, - parentCheckpointId, - ordinalWithinScope: input.ordinalWithinScope, - appRunOrdinal: input.appRunOrdinal, - ref: checkpointRef, - status: "missing", - files: [], - capturedAt: input.capturedAt, - }); - } - - const captured = yield* checkpointStore - .captureCheckpoint({ - cwd: input.scope.cwd, - checkpointRef, - }) - .pipe( - Effect.as(true), - Effect.catch((cause) => - Effect.logWarning("orchestration V2 checkpoint capture failed", { - scopeId: input.scope.id, - checkpointRef, - cause: String(cause), - }).pipe(Effect.as(false)), - ), - ); - - if (!captured) { - return makeCheckpoint({ - id: checkpointId, - scope: input.scope, - runId: input.runId, - nodeId: input.nodeId, - parentCheckpointId, - ordinalWithinScope: input.ordinalWithinScope, - appRunOrdinal: input.appRunOrdinal, - ref: checkpointRef, - status: "error", - files: [], - capturedAt: input.capturedAt, - }); - } - - const previousExists = yield* checkpointStore.hasCheckpointRef({ - cwd: input.scope.cwd, - checkpointRef: previousCheckpointRef, - }); - const files = previousExists - ? yield* checkpointStore - .diffCheckpoints({ - cwd: input.scope.cwd, - fromCheckpointRef: previousCheckpointRef, - toCheckpointRef: checkpointRef, - fallbackFromToHead: false, - }) - .pipe( - Effect.map((diff) => - parseTurnDiffFilesFromUnifiedDiff(diff).map((file) => ({ - path: file.path, - kind: "modified", - additions: file.additions, - deletions: file.deletions, - })), - ), - Effect.catch((cause) => - Effect.logWarning("orchestration V2 checkpoint diff summary failed", { - scopeId: input.scope.id, - checkpointRef, - cause: String(cause), - }).pipe(Effect.as([])), - ), - ) - : []; + const capture: CheckpointServiceV2Shape["capture"] = (input) => + withWorkspaceLock( + input.scope.cwd, + Effect.gen(function* () { + const checkpointId = yield* checkpointIdForScopeOrdinal(idAllocator, { + scopeId: input.scope.id, + ordinalWithinScope: input.ordinalWithinScope, + }); + const parentCheckpointId = + input.ordinalWithinScope > 1 + ? yield* checkpointIdForScopeOrdinal(idAllocator, { + scopeId: input.scope.id, + ordinalWithinScope: input.ordinalWithinScope - 1, + }) + : null; + const checkpointRef = checkpointRefForScopeOrdinal({ + scopeId: input.scope.id, + ordinalWithinScope: input.ordinalWithinScope, + }); + const previousCheckpointRef = checkpointRefForScopeOrdinal({ + scopeId: input.scope.id, + ordinalWithinScope: Math.max(0, input.ordinalWithinScope - 1), + }); + if (!(yield* isGitCheckpointable(input.scope.cwd))) { return makeCheckpoint({ id: checkpointId, scope: input.scope, @@ -416,94 +344,172 @@ export const layer: Layer.Layer - new CheckpointCaptureError({ - scopeId: input.scope.id, - cause, - }), - ), - ); + } - const restore: CheckpointServiceV2Shape["restore"] = (input) => - withWorkspaceLock( - input.scope.cwd, - Effect.gen(function* () { - if (input.checkpoint.status !== "ready") { - return yield* new CheckpointRestoreError({ - scopeId: input.scope.id, - checkpointId: input.checkpoint.id, - cause: `Checkpoint status is ${input.checkpoint.status}.`, - }); - } - - const restored = yield* checkpointStore.restoreCheckpoint({ + const captured = yield* checkpointStore + .captureCheckpoint({ cwd: input.scope.cwd, - checkpointRef: input.checkpoint.ref, - fallbackToHead: false, + checkpointRef, + }) + .pipe( + Effect.as(true), + Effect.catch((cause) => + Effect.logWarning("orchestration V2 checkpoint capture failed", { + scopeId: input.scope.id, + checkpointRef, + cause: String(cause), + }).pipe(Effect.as(false)), + ), + ); + + if (!captured) { + return makeCheckpoint({ + id: checkpointId, + scope: input.scope, + runId: input.runId, + nodeId: input.nodeId, + parentCheckpointId, + ordinalWithinScope: input.ordinalWithinScope, + appRunOrdinal: input.appRunOrdinal, + ref: checkpointRef, + status: "error", + files: [], + capturedAt: input.capturedAt, }); - if (!restored) { - return yield* new CheckpointRestoreError({ + } + + const previousExists = yield* checkpointStore.hasCheckpointRef({ + cwd: input.scope.cwd, + checkpointRef: previousCheckpointRef, + }); + const files = previousExists + ? yield* checkpointStore + .diffCheckpoints({ + cwd: input.scope.cwd, + fromCheckpointRef: previousCheckpointRef, + toCheckpointRef: checkpointRef, + fallbackFromToHead: false, + ignoreWhitespace: false, + }) + .pipe( + Effect.map((diff) => + parseTurnDiffFilesFromUnifiedDiff(diff).map((file) => ({ + path: file.path, + kind: "modified", + additions: file.additions, + deletions: file.deletions, + })), + ), + Effect.catch((cause) => + Effect.logWarning("orchestration V2 checkpoint diff summary failed", { + scopeId: input.scope.id, + checkpointRef, + cause: String(cause), + }).pipe(Effect.as([])), + ), + ) + : []; + + return makeCheckpoint({ + id: checkpointId, + scope: input.scope, + runId: input.runId, + nodeId: input.nodeId, + parentCheckpointId, + ordinalWithinScope: input.ordinalWithinScope, + appRunOrdinal: input.appRunOrdinal, + ref: checkpointRef, + status: "ready", + files, + capturedAt: input.capturedAt, + }); + }), + ).pipe( + Effect.mapError( + (cause) => + new CheckpointCaptureError({ + scopeId: input.scope.id, + cause, + }), + ), + ); + + const restore: CheckpointServiceV2Shape["restore"] = (input) => + withWorkspaceLock( + input.scope.cwd, + Effect.gen(function* () { + if (input.checkpoint.status !== "ready") { + return yield* new CheckpointRestoreError({ + scopeId: input.scope.id, + checkpointId: input.checkpoint.id, + cause: `Checkpoint status is ${input.checkpoint.status}.`, + }); + } + + const restored = yield* checkpointStore.restoreCheckpoint({ + cwd: input.scope.cwd, + checkpointRef: input.checkpoint.ref, + fallbackToHead: false, + }); + if (!restored) { + return yield* new CheckpointRestoreError({ + scopeId: input.scope.id, + checkpointId: input.checkpoint.id, + cause: "Checkpoint ref is unavailable.", + }); + } + }), + ).pipe( + Effect.mapError((cause) => + isCheckpointRestoreError(cause) + ? cause + : new CheckpointRestoreError({ scopeId: input.scope.id, checkpointId: input.checkpoint.id, - cause: "Checkpoint ref is unavailable.", - }); - } - }), - ).pipe( - Effect.mapError((cause) => - Schema.is(CheckpointRestoreError)(cause) - ? cause - : new CheckpointRestoreError({ - scopeId: input.scope.id, - checkpointId: input.checkpoint.id, - cause, - }), - ), - ); + cause, + }), + ), + ); - const deleteStaleRefs: CheckpointServiceV2Shape["deleteStaleRefs"] = (input) => - withWorkspaceLock( - input.scope.cwd, - checkpointStore.deleteCheckpointRefs({ - cwd: input.scope.cwd, - checkpointRefs: input.checkpoints.map((checkpoint) => checkpoint.ref), - }), - ).pipe( + const deleteStaleRefs: CheckpointServiceV2Shape["deleteStaleRefs"] = (input) => + withWorkspaceLock( + input.scope.cwd, + checkpointStore.deleteCheckpointRefs({ + cwd: input.scope.cwd, + checkpointRefs: input.checkpoints.map((checkpoint) => checkpoint.ref), + }), + ).pipe( + Effect.mapError( + (cause) => + new CheckpointDeleteStaleRefsError({ + scopeId: input.scope.id, + checkpointIds: input.checkpoints.map((checkpoint) => checkpoint.id), + cause, + }), + ), + ); + + return CheckpointServiceV2.of({ + prepareRootRunScope: (input) => + makeRootRunScope({ ...input, idAllocator }).pipe( Effect.mapError( (cause) => - new CheckpointDeleteStaleRefsError({ - scopeId: input.scope.id, - checkpointIds: input.checkpoints.map((checkpoint) => checkpoint.id), + new CheckpointRootScopePrepareError({ + threadId: input.threadId, + runId: input.runId, cause, }), ), - ); - - return CheckpointServiceV2.of({ - prepareRootRunScope: (input) => - makeRootRunScope({ ...input, idAllocator }).pipe( - Effect.mapError( - (cause) => - new CheckpointRootScopePrepareError({ - threadId: input.threadId, - runId: input.runId, - cause, - }), - ), - ), - ensureScope, - captureBaseline, - capture, - restore, - deleteStaleRefs, - } satisfies CheckpointServiceV2Shape); - }), - ); -import { createHash } from "node:crypto"; + ), + ensureScope, + captureBaseline, + capture, + restore, + deleteStaleRefs, + } satisfies CheckpointServiceV2Shape); + }), +); diff --git a/apps/server/src/orchestration-v2/CursorOrchestratorV2.live.test.ts b/apps/server/src/orchestration-v2/CursorOrchestratorV2.live.test.ts index 72327d272c..59ff981f89 100644 --- a/apps/server/src/orchestration-v2/CursorOrchestratorV2.live.test.ts +++ b/apps/server/src/orchestration-v2/CursorOrchestratorV2.live.test.ts @@ -13,7 +13,7 @@ import * as Layer from "effect/Layer"; import { FetchHttpClient } from "effect/unstable/http"; import { describe } from "vite-plus/test"; -import { CheckpointStoreLive } from "../checkpointing/Layers/CheckpointStore.ts"; +import * as CheckpointStore from "../checkpointing/CheckpointStore.ts"; import { ServerConfig } from "../config.ts"; import { SqlitePersistenceMemory } from "../persistence/Layers/Sqlite.ts"; import { ProviderInstanceRegistryHydrationLive } from "../provider/Layers/ProviderInstanceRegistryHydration.ts"; @@ -39,7 +39,7 @@ const vcsDriverRegistryLayer = VcsDriverRegistry.layer.pipe( Layer.provide(NodeServices.layer), ); -const checkpointStoreLayer = CheckpointStoreLive.pipe(Layer.provide(vcsDriverRegistryLayer)); +const checkpointStoreLayer = CheckpointStore.layer.pipe(Layer.provide(vcsDriverRegistryLayer)); const serverSettingsLayer = ServerSettingsService.layerTest({ providers: { diff --git a/apps/server/src/orchestration-v2/GrokOrchestratorV2.live.test.ts b/apps/server/src/orchestration-v2/GrokOrchestratorV2.live.test.ts index 3d4762bf04..5e5c0aa33e 100644 --- a/apps/server/src/orchestration-v2/GrokOrchestratorV2.live.test.ts +++ b/apps/server/src/orchestration-v2/GrokOrchestratorV2.live.test.ts @@ -13,7 +13,7 @@ import * as Layer from "effect/Layer"; import { FetchHttpClient } from "effect/unstable/http"; import { describe } from "vite-plus/test"; -import { CheckpointStoreLive } from "../checkpointing/Layers/CheckpointStore.ts"; +import * as CheckpointStore from "../checkpointing/CheckpointStore.ts"; import { ServerConfig } from "../config.ts"; import { SqlitePersistenceMemory } from "../persistence/Layers/Sqlite.ts"; import { ProviderInstanceRegistryHydrationLive } from "../provider/Layers/ProviderInstanceRegistryHydration.ts"; @@ -39,7 +39,7 @@ const vcsDriverRegistryLayer = VcsDriverRegistry.layer.pipe( Layer.provide(NodeServices.layer), ); -const checkpointStoreLayer = CheckpointStoreLive.pipe(Layer.provide(vcsDriverRegistryLayer)); +const checkpointStoreLayer = CheckpointStore.layer.pipe(Layer.provide(vcsDriverRegistryLayer)); const serverSettingsLayer = ServerSettingsService.layerTest({ providers: { diff --git a/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts b/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts index 0882a0dbcb..d5dd871efb 100644 --- a/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts +++ b/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts @@ -23,7 +23,7 @@ import * as Stream from "effect/Stream"; import { TestClock } from "effect/testing"; import { HttpServer } from "effect/unstable/http"; -import { ServerEnvironment } from "../environment/Services/ServerEnvironment.ts"; +import { ServerEnvironment } from "../environment/ServerEnvironment.ts"; import * as McpProviderSession from "../mcp/McpProviderSession.ts"; import * as McpSessionRegistry from "../mcp/McpSessionRegistry.ts"; import { SqlitePersistenceMemory } from "../persistence/Layers/Sqlite.ts"; diff --git a/apps/server/src/orchestration-v2/runtimeLayer.test.ts b/apps/server/src/orchestration-v2/runtimeLayer.test.ts index ea514e2582..266dcdf6bf 100644 --- a/apps/server/src/orchestration-v2/runtimeLayer.test.ts +++ b/apps/server/src/orchestration-v2/runtimeLayer.test.ts @@ -12,7 +12,7 @@ import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Stream from "effect/Stream"; -import { CheckpointStoreLive } from "../checkpointing/Layers/CheckpointStore.ts"; +import * as CheckpointStore from "../checkpointing/CheckpointStore.ts"; import { ServerConfig } from "../config.ts"; import { GitCoreLive } from "../git/Layers/GitCore.ts"; import { SqlitePersistenceMemory } from "../persistence/Layers/Sqlite.ts"; @@ -35,9 +35,8 @@ const GitCoreTestLayer = GitCoreLive.pipe( Layer.provide(NodeServices.layer), ); -const CheckpointStoreTestLayer = CheckpointStoreLive.pipe( - Layer.provide(GitCoreTestLayer), - Layer.provide(NodeServices.layer), +const CheckpointStoreTestLayer = CheckpointStore.layer.pipe( + Layer.provide(VcsDriverRegistryTestLayer), ); const driver = ProviderDriverKind.make("codex"); diff --git a/apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts b/apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts index ff6a4b43a8..399f7fb0b6 100644 --- a/apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts +++ b/apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts @@ -9,9 +9,8 @@ import type * as SqlClient from "effect/unstable/sql/SqlClient"; import type { MigrationError } from "effect/unstable/sql/Migrator"; import type { SqlError } from "effect/unstable/sql/SqlError"; -import { CheckpointStoreLive } from "../../checkpointing/Layers/CheckpointStore.ts"; -import { ServerConfig, type ServerConfigShape } from "../../config.ts"; -import { GitCoreLive } from "../../git/Layers/GitCore.ts"; +import * as CheckpointStore from "../../checkpointing/CheckpointStore.ts"; +import { ServerConfig } from "../../config.ts"; import { SqlitePersistenceMemory } from "../../persistence/Layers/Sqlite.ts"; import { ServerSettingsService } from "../../serverSettings.ts"; import * as VcsDriverRegistry from "../../vcs/VcsDriverRegistry.ts"; @@ -59,7 +58,7 @@ import { export function makeReplayServerConfig( scenario: string, ): Effect.Effect< - ServerConfigShape, + ServerConfig["Service"], PlatformError.PlatformError, FileSystem.FileSystem | Path.Path > { @@ -245,8 +244,8 @@ export function makeOrchestratorV2ReplayLayerWithRegistry( Layer.provide(serverConfigLayer), Layer.provide(NodeServices.layer), ); - const checkpointStoreLayer = CheckpointStoreLive.pipe( - Layer.provide(gitCoreLayer), + const checkpointStoreLayer = CheckpointStore.layer.pipe( + Layer.provide(vcsDriverRegistryLayer), Layer.provide(NodeServices.layer), ); const checkpointServiceProvided = checkpointServiceLayer.pipe( diff --git a/apps/server/src/server.test.ts b/apps/server/src/server.test.ts index 533c4a4e4f..4db8ffdd9b 100644 --- a/apps/server/src/server.test.ts +++ b/apps/server/src/server.test.ts @@ -567,13 +567,10 @@ const buildAppUnderTest = (options?: { ), ); - const servedRoutesLayer = HttpRouter.serve( - makeRoutesLayer.pipe(Layer.provide(ServiceLauncherClient.layer)), - { - disableListenLog: true, - disableLogger: true, - }, - ).pipe( + const servedRoutesBaseLayer = HttpRouter.serve(makeRoutesLayer, { + disableListenLog: true, + disableLogger: true, + }).pipe( Layer.provide( Layer.mock(Keybindings.Keybindings)({ loadConfigState: Effect.succeed({ @@ -780,6 +777,9 @@ const buildAppUnderTest = (options?: { ...options?.layers?.checkpointDiffQuery, }), ), + ); + + const servedRoutesLayer = servedRoutesBaseLayer.pipe( Layer.provide(OrchestrationV2UnavailableTestLayer), ); diff --git a/apps/web/src/routes/debug.orchestration-v2.tsx b/apps/web/src/routes/debug.orchestration-v2.tsx index 53ff46134f..4d9d48906c 100644 --- a/apps/web/src/routes/debug.orchestration-v2.tsx +++ b/apps/web/src/routes/debug.orchestration-v2.tsx @@ -28,8 +28,7 @@ import type { CSSProperties, DragEvent, ReactNode } from "react"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { ProviderModelPicker } from "../components/chat/ProviderModelPicker"; -import { getPrimaryEnvironmentConnection } from "../environments/runtime"; -import { useSettings } from "../hooks/useSettings"; +import { usePrimarySettings } from "../hooks/useSettings"; import { removeAndRenumberTimelineItem, upsertTimelineItemAtStablePosition, @@ -1627,9 +1626,28 @@ function OrchestrationV2DebugRoute() { const unsubscribeRef = useRef<(() => void) | null>(null); const shellUnsubscribeRef = useRef<(() => void) | null>(null); - const serverProviders = useServerProviders(); - const settings = useSettings(); - const keybindings = useServerKeybindings(); + const environmentId = usePrimaryEnvironmentId(); + const serverProviders = useAtomValue(primaryServerProvidersAtom); + const settings = usePrimarySettings(); + const keybindings = useAtomValue(primaryServerKeybindingsAtom); + const dispatchCommandMutation = useAtomCommand(orchestrationEnvironment.v2.dispatchCommand, { + reportFailure: false, + }); + const getThreadProjectionQuery = useAtomQueryRunner( + orchestrationEnvironment.v2.threadProjection, + { reportFailure: false }, + ); + const shellSubscription = useEnvironmentQuery( + environmentId === null ? null : orchestrationEnvironment.v2.shell({ environmentId, input: {} }), + ); + const threadSubscription = useEnvironmentQuery( + environmentId === null || threadId === null + ? null + : orchestrationEnvironment.v2.thread({ + environmentId, + input: { threadId }, + }), + ); const providerSnapshots = useMemo( () => deriveOrchestrationV2DebugProviderSnapshots({ diff --git a/packages/client-runtime/src/rpc/client.ts b/packages/client-runtime/src/rpc/client.ts index bfe57a6c0d..a221122fa6 100644 --- a/packages/client-runtime/src/rpc/client.ts +++ b/packages/client-runtime/src/rpc/client.ts @@ -1,4 +1,8 @@ -import { ORCHESTRATION_WS_METHODS, WS_METHODS } from "@t3tools/contracts"; +import { + ORCHESTRATION_V2_WS_METHODS, + ORCHESTRATION_WS_METHODS, + WS_METHODS, +} from "@t3tools/contracts"; import * as Cause from "effect/Cause"; import * as Context from "effect/Context"; import type * as Duration from "effect/Duration"; diff --git a/packages/effect-codex-app-server/src/replay.ts b/packages/effect-codex-app-server/src/replay.ts index 1fb2d8115b..a07a487b6d 100644 --- a/packages/effect-codex-app-server/src/replay.ts +++ b/packages/effect-codex-app-server/src/replay.ts @@ -319,7 +319,6 @@ function encodeInboundFrame(frame: unknown): Uint8Array { } function replayTransportError( - transcript: CodexAppServerReplayTranscript, error: CodexAppServerReplayError, ): CodexError.CodexAppServerTransportError { return new CodexError.CodexAppServerTransportError({ @@ -505,7 +504,7 @@ const makeReplayClientWithState = Effect.fn( const terminationError: Effect.Effect = Ref.get(state).pipe( Effect.map((current) => current.failure - ? replayTransportError(transcript, current.failure) + ? replayTransportError(current.failure) : new CodexError.CodexAppServerProcessExitedError({ code: 0 }), ), );