From 3596d266592e215739e9cdf189e2d035b3914737 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 14 Jun 2026 16:55:44 -0700 Subject: [PATCH] refactor(orchestration-v2): adopt host process spawn policy Co-authored-by: codex --- .../record-codex-app-server-replay-fixture.ts | 35 ++++++-- .../Adapters/ClaudeAdapterV2.ts | 7 +- .../Adapters/CodexAdapterV2.test.ts | 60 ++++++++++++++ .../Adapters/CodexAdapterV2.ts | 79 +++++++++++-------- apps/server/src/server.test.ts | 8 +- 5 files changed, 142 insertions(+), 47 deletions(-) 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 a87774825c..f7f227d7d1 100644 --- a/apps/server/scripts/record-codex-app-server-replay-fixture.ts +++ b/apps/server/scripts/record-codex-app-server-replay-fixture.ts @@ -1,11 +1,15 @@ import * as NodeRuntime from "@effect/platform-node/NodeRuntime"; import * as NodeServices from "@effect/platform-node/NodeServices"; +import { HostProcessEnvironment } from "@t3tools/shared/hostProcess"; +import { resolveSpawnCommand } from "@t3tools/shared/shell"; +import * as Console from "effect/Console"; import * as Deferred from "effect/Deferred"; import * as FileSystem from "effect/FileSystem"; +import * as Layer from "effect/Layer"; import * as Path from "effect/Path"; import * as PlatformError from "effect/PlatformError"; -import * as Console from "effect/Console"; import * as Effect from "effect/Effect"; +import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"; import * as CodexClient from "effect-codex-app-server/client"; import type * as CodexSchema from "effect-codex-app-server/schema"; @@ -977,11 +981,7 @@ function sessionizeForkReplayRecords(input: { function makeCodexLayer({ recorder }: { readonly recorder: Recorder }) { const clientRequestMethodById = new Map(); const serverRequestMethodById = new Map(); - - return CodexClient.layerCommand({ - command: process.env.T3_CODEX_BIN ?? process.env.CODEX_BIN ?? "codex", - args: ["app-server"], - cwd: process.cwd(), + const clientOptions: CodexClient.CodexAppServerClientOptions = { logIncoming: true, logOutgoing: true, logger: (event) => { @@ -1036,7 +1036,28 @@ function makeCodexLayer({ recorder }: { readonly recorder: Recorder }) { yield* recorder.writeRecord(record); }).pipe(Effect.ignore); }, - }); + }; + + return Layer.effect( + CodexClient.CodexAppServerClient, + Effect.gen(function* () { + const environment = yield* HostProcessEnvironment; + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; + const commandName = environment.T3_CODEX_BIN ?? environment.CODEX_BIN ?? "codex"; + const spawnCommand = yield* resolveSpawnCommand(commandName, ["app-server"], { + env: environment, + }); + const handle = yield* spawner.spawn( + ChildProcess.make(spawnCommand.command, spawnCommand.args, { + cwd: process.cwd(), + env: environment, + shell: spawnCommand.shell, + }), + ); + const context = yield* Layer.build(CodexClient.layerChildProcess(handle, clientOptions)); + return yield* Effect.service(CodexClient.CodexAppServerClient).pipe(Effect.provide(context)); + }), + ); } function installReplayHandlers({ diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 7f2204514b..89c02637da 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -16,6 +16,7 @@ import { } from "@anthropic-ai/claude-agent-sdk"; import type { WebSearchOutput } from "@anthropic-ai/claude-agent-sdk/sdk-tools"; import { parseCliArgs } from "@t3tools/shared/cliArgs"; +import { HostProcessEnvironment } from "@t3tools/shared/hostProcess"; import { type ModelSelection, type OrchestrationV2ConversationMessage, @@ -3237,9 +3238,10 @@ export const ClaudeAdapterV2Driver: ProviderAdapterDriver< create: Effect.fn("ClaudeAdapterV2Driver.create")( function* (input: ProviderAdapterDriverCreateInput) { const { instanceId, environment, enabled, config } = input; + const hostEnvironment = yield* HostProcessEnvironment; const idAllocator = yield* IdAllocatorV2; const queryRunner = yield* ClaudeAgentSdkQueryRunner; - const baseEnvironment = mergeProviderInstanceEnvironment(environment); + const baseEnvironment = mergeProviderInstanceEnvironment(environment, hostEnvironment); const claudeEnvironment = yield* makeClaudeEnvironment(config, baseEnvironment); return makeClaudeAdapterV2({ instanceId, @@ -3265,13 +3267,14 @@ export const ClaudeAdapterV2Driver: ProviderAdapterDriver< }; const makeDefaultClaudeAdapterV2 = Effect.fn("ClaudeAdapterV2.layer")(function* () { + const hostEnvironment = yield* HostProcessEnvironment; const idAllocator = yield* IdAllocatorV2; const queryRunner = yield* ClaudeAgentSdkQueryRunner; return makeClaudeAdapterV2({ instanceId: CLAUDE_DEFAULT_INSTANCE_ID, settings: DEFAULT_CLAUDE_SETTINGS, - environment: process.env, + environment: hostEnvironment, idAllocator, queryRunner, }); diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts index 3e7bce5f0b..f105e89799 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts @@ -10,15 +10,75 @@ import { ThreadId, } from "@t3tools/contracts"; import { assert, describe, it } from "@effect/vitest"; +import { HostProcessEnvironment, HostProcessPlatform } from "@t3tools/shared/hostProcess"; +import { SpawnExecutableResolution } from "@t3tools/shared/shell"; import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; +import { ChildProcess } from "effect/unstable/process"; import type { EventNdjsonLogger } from "../../provider/Layers/EventNdjsonLogger.ts"; import { makeCodexAppServerProtocolLogger, + makeCodexAppServerSpawnCommand, resolveCodexRollbackTurnCount, } from "./CodexAdapterV2.ts"; +describe("CodexAdapterV2 process spawning", () => { + it.effect("resolves Windows command shims through the shared spawn policy", () => + Effect.gen(function* () { + const command = yield* makeCodexAppServerSpawnCommand({ + command: "codex", + args: ["app-server", "argument with spaces"], + cwd: "C:\\workspace", + env: { CUSTOM: "1" }, + extendEnv: true, + }); + + assert.isTrue(ChildProcess.isStandardCommand(command)); + if (!ChildProcess.isStandardCommand(command)) { + return; + } + assert.equal(command.command, '^"C:\\npm\\codex.cmd^"'); + assert.deepEqual(command.args, ['^"app-server^"', '^"argument^ with^ spaces^"']); + assert.equal(command.options.shell, true); + assert.equal(command.options.cwd, "C:\\workspace"); + assert.deepEqual(command.options.env, { CUSTOM: "1" }); + assert.equal(command.options.extendEnv, true); + }).pipe( + Effect.provideService(HostProcessPlatform, "win32"), + Effect.provideService(HostProcessEnvironment, { + PATH: "C:\\Windows\\System32", + HOST_ONLY: "1", + }), + Effect.provideService(SpawnExecutableResolution, (_command, _platform, environment) => { + assert.equal(environment.HOST_ONLY, "1"); + assert.equal(environment.CUSTOM, "1"); + return "C:\\npm\\codex.cmd"; + }), + ), + ); + + it.effect("uses direct execution for native executables", () => + Effect.gen(function* () { + const command = yield* makeCodexAppServerSpawnCommand({ + command: "codex.exe", + args: ["app-server"], + }); + + assert.isTrue(ChildProcess.isStandardCommand(command)); + if (!ChildProcess.isStandardCommand(command)) { + return; + } + assert.equal(command.command, "C:\\bin\\codex.exe"); + assert.deepEqual(command.args, ["app-server"]); + assert.equal(command.options.shell, false); + }).pipe( + Effect.provideService(HostProcessPlatform, "win32"), + Effect.provideService(SpawnExecutableResolution, () => "C:\\bin\\codex.exe"), + ), + ); +}); + describe("CodexAdapterV2 native protocol logging", () => { it.effect("writes app-server protocol frames to the native provider log", () => Effect.gen(function* () { diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts index a963851925..a19763ce60 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts @@ -1,3 +1,6 @@ +import { CodexSettings, defaultInstanceIdForDriver, ProviderDriverKind } from "@t3tools/contracts"; +import { HostProcessEnvironment } from "@t3tools/shared/hostProcess"; +import { resolveSpawnCommand } from "@t3tools/shared/shell"; import type { ChatAttachment, OrchestrationV2ConversationMessage, @@ -666,8 +669,34 @@ export class CodexAppServerClientFactory extends Context.Service< CodexAppServerClientFactoryShape >()("t3/orchestration-v2/Adapters/CodexAdapterV2/CodexAppServerClientFactory") {} +export const makeCodexAppServerSpawnCommand = Effect.fn( + "CodexAdapterV2.makeCodexAppServerSpawnCommand", +)(function* (input: { + readonly command: string; + readonly args: ReadonlyArray; + readonly cwd?: string | undefined; + readonly env?: NodeJS.ProcessEnv | undefined; + readonly extendEnv?: boolean | undefined; +}) { + const spawnCommand = yield* resolveSpawnCommand(input.command, input.args, { + ...(input.env === undefined ? {} : { env: input.env }), + ...(input.extendEnv === undefined ? {} : { extendEnv: input.extendEnv }), + }); + return ChildProcess.make(spawnCommand.command, spawnCommand.args, { + ...(input.cwd === undefined ? {} : { cwd: input.cwd }), + ...(input.env === undefined ? {} : { env: input.env }), + ...(input.extendEnv === undefined ? {} : { extendEnv: input.extendEnv }), + shell: spawnCommand.shell, + }); +}); + export const makeCodexAppServerClientFactoryCommandLayer = ( - options: CodexClient.CodexAppServerCommandLayerOptions, + options: CodexClient.CodexAppServerClientOptions & { + readonly command: string; + readonly args?: ReadonlyArray; + readonly cwd?: string; + readonly env?: NodeJS.ProcessEnv; + }, ): Layer.Layer => Layer.effect( CodexAppServerClientFactory, @@ -677,12 +706,13 @@ export const makeCodexAppServerClientFactoryCommandLayer = ( open: (input) => Effect.gen(function* () { const scope = yield* Scope.Scope; - const command = ChildProcess.make(options.command, [...(options.args ?? [])], { + const command = yield* makeCodexAppServerSpawnCommand({ + command: options.command, + args: options.args ?? [], ...((input.runtimePolicy.cwd ?? options.cwd) === undefined ? {} : { cwd: input.runtimePolicy.cwd ?? options.cwd }), - ...(options.env === undefined ? {} : { env: { ...process.env, ...options.env } }), - shell: process.platform === "win32", + ...(options.env === undefined ? {} : { env: options.env, extendEnv: true }), }); const handle = yield* spawner.spawn(command).pipe( Effect.provideService(Scope.Scope, scope), @@ -747,34 +777,15 @@ export const codexAppServerClientFactoryFromSettingsLayer: Layer.Layer< open: (input) => Effect.gen(function* () { const scope = yield* Scope.Scope; - yield* settingsService.ready.pipe( - Effect.mapError( - (cause) => - new ProviderAdapterOpenSessionError({ - provider: CODEX_PROVIDER, - providerSessionId: input.providerSessionId, - cause, - }), - ), - ); - const codexSettings = yield* settingsService.getSettings.pipe( - Effect.map((settings) => settings.providers.codex), - Effect.mapError( - (cause) => - new ProviderAdapterOpenSessionError({ - provider: CODEX_PROVIDER, - providerSessionId: input.providerSessionId, - cause, - }), - ), - ); - const command = ChildProcess.make(codexSettings.binaryPath || "codex", ["app-server"], { + const environment = { + ...input.environment, + ...(input.settings.homePath ? { CODEX_HOME: input.settings.homePath } : {}), + }; + const command = yield* makeCodexAppServerSpawnCommand({ + command: input.settings.binaryPath || "codex", + args: ["app-server"], ...(input.runtimePolicy.cwd === null ? {} : { cwd: input.runtimePolicy.cwd }), - env: { - ...process.env, - ...(codexSettings.homePath ? { CODEX_HOME: codexSettings.homePath } : {}), - }, - shell: process.platform === "win32", + env: environment, }); const handle = yield* spawner.spawn(command).pipe( Effect.provideService(Scope.Scope, scope), @@ -824,6 +835,7 @@ export const CodexAdapterV2Driver: ProviderAdapterDriver Effect.die("PreviewManager not stubbed in this test"), navigate: () => Effect.die("PreviewManager not stubbed in this test"),