refactor(orchestration-v2): adopt host process spawn policy
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
de198dd1f8
commit
c15de61875
5 files changed
+142
-47
No files matched your search
@@ -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<string, string>();
|
||||
const serverRequestMethodById = new Map<string, string>();
|
||||
|
||||
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({
|
||||
|
||||
@@ -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<ClaudeSettings>) {
|
||||
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,
|
||||
});
|
||||
|
||||
@@ -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* () {
|
||||
|
||||
@@ -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<string>;
|
||||
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<string>;
|
||||
readonly cwd?: string;
|
||||
readonly env?: NodeJS.ProcessEnv;
|
||||
},
|
||||
): Layer.Layer<CodexAppServerClientFactory, never, ChildProcessSpawner.ChildProcessSpawner> =>
|
||||
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<CodexSettings, CodexAda
|
||||
Effect.gen(function* () {
|
||||
const clientFactory = yield* CodexAppServerClientFactory;
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const hostEnvironment = yield* HostProcessEnvironment;
|
||||
const idAllocator = yield* IdAllocatorV2;
|
||||
const serverConfig = yield* ServerConfig;
|
||||
const homeLayout = yield* resolveCodexHomeLayout(config);
|
||||
@@ -849,7 +861,7 @@ export const CodexAdapterV2Driver: ProviderAdapterDriver<CodexSettings, CodexAda
|
||||
return makeCodexAdapterV2({
|
||||
instanceId,
|
||||
settings,
|
||||
environment: mergeProviderInstanceEnvironment(environment),
|
||||
environment: mergeProviderInstanceEnvironment(environment, hostEnvironment),
|
||||
clientFactory,
|
||||
fileSystem,
|
||||
idAllocator,
|
||||
@@ -867,13 +879,14 @@ export const layer: Layer.Layer<
|
||||
Effect.gen(function* () {
|
||||
const clientFactory = yield* CodexAppServerClientFactory;
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const hostEnvironment = yield* HostProcessEnvironment;
|
||||
const idAllocator = yield* IdAllocatorV2;
|
||||
const serverConfig = yield* ServerConfig;
|
||||
|
||||
return makeCodexAdapterV2({
|
||||
instanceId: CODEX_DEFAULT_INSTANCE_ID,
|
||||
settings: DEFAULT_CODEX_SETTINGS,
|
||||
environment: process.env,
|
||||
environment: hostEnvironment,
|
||||
clientFactory,
|
||||
fileSystem,
|
||||
idAllocator,
|
||||
|
||||
@@ -684,13 +684,11 @@ const buildAppUnderTest = (options?: {
|
||||
...options?.layers?.projectSetupScriptRunner,
|
||||
}),
|
||||
),
|
||||
Layer.provide(
|
||||
Layer.mock(TerminalManager.TerminalManager)({
|
||||
...options?.layers?.terminalManager,
|
||||
}),
|
||||
),
|
||||
Layer.provide(
|
||||
Layer.mergeAll(
|
||||
Layer.mock(TerminalManager)({
|
||||
...options?.layers?.terminalManager,
|
||||
}),
|
||||
Layer.mock(PreviewManager.PreviewManager)({
|
||||
open: () => Effect.die("PreviewManager not stubbed in this test"),
|
||||
navigate: () => Effect.die("PreviewManager not stubbed in this test"),
|
||||
|
||||
Reference in new issue
Block a user