Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

CodexAdapterV2.testkit.ts

212 lines7.0 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { type ProviderReplayTranscript } from "@t3tools/contracts";
3import * as CodexClient from "effect-codex-app-server/client";
4import * as CodexReplay from "effect-codex-app-server/replay";
5import * as Effect from "effect/Effect";
6import * as FileSystem from "effect/FileSystem";
7import * as Layer from "effect/Layer";
8import * as Path from "effect/Path";
9import * as PlatformError from "effect/PlatformError";
10import * as Schema from "effect/Schema";
11​
12import { ServerConfig } from "../../config.ts";
13import { layer as idAllocatorLayer } from "../IdAllocator.ts";
14import { ProviderAdapterOpenSessionError } from "../ProviderAdapter.ts";
15import { ProviderAdapterDriverCreateError } from "../ProviderAdapterDriver.ts";
16import { makeDriverLayer as makeProviderAdapterRegistryDriverLayer } from "../ProviderAdapterRegistry.ts";
17import type { OrchestratorV2ProviderReplayHarness } from "../testkit/ProviderReplayHarness.ts";
18import {
19 CODEX_DEFAULT_INSTANCE_ID,
20 CODEX_DRIVER_KIND,
21 CodexAdapterV2Driver,
22 CodexAppServerClientFactory,
23} from "./CodexAdapterV2.ts";
24​
25export class CodexReplayTranscriptDecodeError extends Schema.TaggedErrorClass<CodexReplayTranscriptDecodeError>()(
26 "CodexReplayTranscriptDecodeError",
27 {
28 driver: Schema.optional(Schema.String),
29 protocol: Schema.optional(Schema.String),
30 scenario: Schema.optional(Schema.String),
31 cause: Schema.Defect(),
32 },
33) {
34 override get message(): string {
35 return `Failed to decode Codex app-server replay transcript for scenario ${this.scenario ?? "<unknown>"}.`;
36 }
38​
39export const CodexOrchestratorReplayHarnessError = Schema.Union([
40 CodexReplayTranscriptDecodeError,
41 CodexReplay.CodexAppServerReplayError,
42 ProviderAdapterDriverCreateError,
43]);
44export type CodexOrchestratorReplayHarnessError = typeof CodexOrchestratorReplayHarnessError.Type;
45​
46function metadataFromTranscript(transcript: ProviderReplayTranscript): {
47 readonly provider?: string;
48 readonly protocol?: string;
49 readonly scenario?: string;
50} {
51 return {
52 provider: transcript.provider,
53 protocol: transcript.protocol,
54 scenario: transcript.scenario,
55 };
57​
58function makeReplayServerConfig(
59 scenario: string,
60): Effect.Effect<
61 ServerConfig["Service"],
62 PlatformError.PlatformError,
63 FileSystem.FileSystem | Path.Path
64> {
65 return Effect.gen(function* () {
66 const fs = yield* FileSystem.FileSystem;
67 const path = yield* Path.Path;
68 const baseDir = yield* fs.makeTempDirectory({
69 prefix: `t3-orchestration-v2-codex-${scenario}-`,
70 });
71 const stateDir = path.join(baseDir, "userdata");
72 const logsDir = path.join(stateDir, "logs");
73 const providerLogsDir = path.join(logsDir, "provider");
74 const terminalLogsDir = path.join(logsDir, "terminals");
75 const attachmentsDir = path.join(stateDir, "attachments");
76 const worktreesDir = path.join(baseDir, "worktrees");
77 const providerStatusCacheDir = path.join(baseDir, "caches");
78​
79 for (const directory of [
80 stateDir,
81 logsDir,
82 providerLogsDir,
83 terminalLogsDir,
84 attachmentsDir,
85 worktreesDir,
86 providerStatusCacheDir,
87 ]) {
88 yield* fs.makeDirectory(directory, { recursive: true });
89 }
90​
91 return {
92 logLevel: "Error",
93 traceMinLevel: "Info",
94 traceTimingEnabled: true,
95 traceBatchWindowMs: 200,
96 traceMaxBytes: 10 * 1024 * 1024,
97 traceMaxFiles: 10,
98 otlpTracesUrl: undefined,
99 otlpMetricsUrl: undefined,
100 otlpExportIntervalMs: 10_000,
101 otlpServiceName: "t3-server",
102 mode: "web",
103 port: 0,
104 host: undefined,
105 cwd: process.cwd(),
106 baseDir,
107 staticDir: undefined,
108 devUrl: undefined,
109 noBrowser: false,
110 startupPresentation: "browser",
111 tailscaleServeEnabled: false,
112 tailscaleServePort: 443,
113 desktopBootstrapToken: undefined,
114 autoBootstrapProjectFromCwd: false,
115 logWebSocketEvents: false,
116 stateDir,
117 dbPath: path.join(stateDir, "state.sqlite"),
118 keybindingsConfigPath: path.join(stateDir, "keybindings.json"),
119 settingsPath: path.join(stateDir, "settings.json"),
120 providerStatusCacheDir,
121 worktreesDir,
122 attachmentsDir,
123 logsDir,
124 serverLogPath: path.join(logsDir, "server.log"),
125 serverTracePath: path.join(logsDir, "server.trace.ndjson"),
126 providerLogsDir,
127 providerEventLogPath: path.join(providerLogsDir, "events.log"),
128 terminalLogsDir,
129 anonymousIdPath: path.join(stateDir, "anonymous-id"),
130 environmentIdPath: path.join(stateDir, "environment-id"),
131 serverRuntimeStatePath: path.join(stateDir, "server-runtime.json"),
132 secretsDir: path.join(stateDir, "secrets"),
133 };
134 });
136​
137export function makeCodexProviderAdapterRegistryReplayLayer(input: {
138 readonly transcript: CodexReplay.CodexAppServerReplayTranscript;
139 readonly driver?: CodexReplay.CodexAppServerReplayDriver;
140}) {
141 const replayLayer =
142 input.driver === undefined
143 ? CodexReplay.layerReplay(input.transcript)
144 : CodexReplay.layerReplayWithDriver(input.driver);
145 const replayClientFactoryLayer = Layer.succeed(CodexAppServerClientFactory, {
146 open: (openInput) =>
147 Effect.gen(function* () {
148 const context = yield* Layer.build(replayLayer).pipe(
149 Effect.mapError(
150 (cause) =>
151 new ProviderAdapterOpenSessionError({
152 driver: CODEX_DRIVER_KIND,
153 providerSessionId: openInput.providerSessionId,
154 cause,
155 }),
156 ),
157 );
158 return yield* Effect.service(CodexClient.CodexAppServerClient).pipe(
159 Effect.provide(context),
160 );
161 }),
162 });
163 const serverConfigLayer = Layer.effect(
164 ServerConfig,
165 makeReplayServerConfig(input.transcript.scenario).pipe(Effect.orDie),
166 ).pipe(Layer.provide(NodeServices.layer));
167 const registryLayer = makeProviderAdapterRegistryDriverLayer({
168 drivers: [CodexAdapterV2Driver],
169 configMap: {
170 [CODEX_DEFAULT_INSTANCE_ID]: {
171 driver: CODEX_DRIVER_KIND,
172 },
173 },
174 }).pipe(
175 Layer.provide(
176 Layer.mergeAll(
177 replayClientFactoryLayer,
178 serverConfigLayer,
179 NodeServices.layer,
180 idAllocatorLayer,
181 ),
182 ),
183 );
184​
185 return registryLayer;
187​
188export const CodexOrchestratorReplayHarness: OrchestratorV2ProviderReplayHarness<
189 CodexReplay.CodexAppServerReplayTranscript,
190 CodexOrchestratorReplayHarnessError
191> = {
192 driver: CODEX_DRIVER_KIND,
193 decodeTranscript: (transcript) =>
194 Schema.decodeUnknownEffect(CodexReplay.CodexAppServerReplayTranscript)(transcript).pipe(
195 Effect.mapError(
196 (cause) =>
197 new CodexReplayTranscriptDecodeError({
198 ...metadataFromTranscript(transcript),
199 cause,
200 }),
201 ),
202 ),
203 makeProviderAdapterRegistryLayer: (transcript) => {
204 return Layer.effectContext(
205 CodexReplay.makeReplayDriver(transcript).pipe(
206 Effect.flatMap((driver) =>
207 Layer.build(makeCodexProviderAdapterRegistryReplayLayer({ transcript, driver })),
208 ),
209 ),
210 );
211 },
212};