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

CodexAdapterV2.testkit.ts

278 lines9.9 KB

1import * as OtelEnvironment from "@t3tools/shared/otelEnvironment";
2import { DEFAULT_SIGNAL_EXPORT } from "@t3tools/shared/observability";
3import * as NodeServices from "@effect/platform-node/NodeServices";
4import { type ProviderReplayTranscript } from "@t3tools/contracts";
5import * as CodexClient from "effect-codex-app-server/client";
6import type * as CodexError from "effect-codex-app-server/errors";
7import * as CodexReplay from "effect-codex-app-server/replay";
8import * as Effect from "effect/Effect";
9import * as FileSystem from "effect/FileSystem";
10import * as Layer from "effect/Layer";
11import * as Path from "effect/Path";
12import * as PlatformError from "effect/PlatformError";
13import * as Predicate from "effect/Predicate";
14import * as Schema from "effect/Schema";
15​
16import * as ServerConfig from "../../config.ts";
17import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
18import { ProviderAdapterOpenSessionError } from "@t3tools/provider-core/server/ProviderAdapter";
19import { ProviderAdapterDriverCreateError } from "@t3tools/provider-core/server/adapterDriver";
20import * as ProviderAdapterRegistry from "../ProviderAdapterRegistry.ts";
21import type { OrchestratorV2ProviderReplayHarness } from "../testkit/ProviderReplayHarness.ts";
22import type { ProviderReplayGate } from "@t3tools/provider-testing/replayGate";
23import * as CodexAdapterV2 from "./CodexAdapterV2.ts";
24​
25export class CodexReplayTranscriptDecodeError extends Schema.TaggedError<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​
46export function withCodexReplayChildMetadata(
47 client: CodexClient.CodexAppServerClient["Service"],
48 transcript: CodexReplay.CodexAppServerReplayTranscript,
49 readMetadata: (
50 threadId: string,
51 method: "thread/read" | "thread/resume",
52 ) => Effect.Effect<unknown, CodexError.CodexAppServerError> = (threadId) =>
53 Effect.succeed({ thread: { id: threadId }, model: null }),
54): CodexClient.CodexAppServerClient["Service"] {
55 const childThreadIds = new Set(
56 transcript.entries.flatMap((entry) => {
57 if (entry.type !== "emit_inbound" || !Predicate.isObject(entry.frame)) return [];
58 const params = entry.frame.params;
59 if (!Predicate.isObject(params) || !Predicate.isObject(params.item)) return [];
60 const item = params.item;
61 if (item.type === "subAgentActivity" && typeof item.agentThreadId === "string") {
62 return [item.agentThreadId];
63 }
64 return item.type === "collabAgentToolCall" && Array.isArray(item.receiverThreadIds)
65 ? item.receiverThreadIds.filter(Predicate.isString)
66 : [];
67 }),
68 );
69 return {
70 ...client,
71 raw: {
72 ...client.raw,
73 request: (method, params) =>
74 (method === "thread/read" || method === "thread/resume") &&
75 Predicate.isObject(params) &&
76 (method === "thread/read" ? params.includeTurns === false : params.excludeTurns === true) &&
77 typeof params.threadId === "string" &&
78 childThreadIds.has(params.threadId)
79 ? readMetadata(params.threadId, method)
80 : client.raw.request(method, params),
81 },
82 };
84​
85function metadataFromTranscript(transcript: ProviderReplayTranscript): {
86 readonly provider?: string;
87 readonly protocol?: string;
88 readonly scenario?: string;
89} {
90 return {
91 provider: transcript.provider,
92 protocol: transcript.protocol,
93 scenario: transcript.scenario,
94 };
96​
97export function makeReplayServerConfig(
98 scenario: string,
99): Effect.Effect<
100 ServerConfig.ServerConfig["Service"],
101 PlatformError.PlatformError,
102 FileSystem.FileSystem | Path.Path
103> {
104 return Effect.gen(function* () {
105 const fs = yield* FileSystem.FileSystem;
106 const path = yield* Path.Path;
107 const baseDir = yield* fs.makeTempDirectory({
108 prefix: `t3-orchestration-v2-codex-${scenario}-`,
109 });
110 const stateDir = path.join(baseDir, "userdata");
111 const logsDir = path.join(stateDir, "logs");
112 const providerLogsDir = path.join(logsDir, "provider");
113 const terminalLogsDir = path.join(logsDir, "terminals");
114 const attachmentsDir = path.join(stateDir, "attachments");
115 const environmentThemesDir = path.join(stateDir, "themes");
116 const worktreesDir = path.join(baseDir, "worktrees");
117 const providerStatusCacheDir = path.join(baseDir, "caches");
118​
119 for (const directory of [
120 stateDir,
121 logsDir,
122 providerLogsDir,
123 terminalLogsDir,
124 attachmentsDir,
125 environmentThemesDir,
126 worktreesDir,
127 providerStatusCacheDir,
128 ]) {
129 yield* fs.makeDirectory(directory, { recursive: true });
130 }
131​
132 return {
133 logLevel: "Error",
134 traceMinLevel: "Info",
135 traceTimingEnabled: true,
136 traceBatchWindowMs: 200,
137 traceMaxBytes: 10 * 1024 * 1024,
138 traceMaxFiles: 10,
139 otelEnvironment: OtelEnvironment.none,
140 otlpTracesUrl: undefined,
141 otlpMetricsUrl: undefined,
142 otlpLogsUrl: undefined,
143 otlpTracesExport: DEFAULT_SIGNAL_EXPORT,
144 otlpMetricsExport: DEFAULT_SIGNAL_EXPORT,
145 otlpLogsExport: DEFAULT_SIGNAL_EXPORT,
146 mode: "web",
147 port: 0,
148 host: undefined,
149 cwd: process.cwd(),
150 baseDir,
151 staticDir: undefined,
152 devUrl: undefined,
153 devAllowedOrigins: [],
154 noBrowser: false,
155 startupPresentation: "browser",
156 tailscaleServeEnabled: false,
157 tailscaleServePort: 443,
158 desktopBootstrapToken: undefined,
159 autoBootstrapProjectFromCwd: false,
160 logWebSocketEvents: false,
161 stateDir,
162 dbPath: path.join(stateDir, "state.sqlite"),
163 keybindingsConfigPath: path.join(stateDir, "keybindings.json"),
164 settingsPath: path.join(stateDir, "settings.json"),
165 providerStatusCacheDir,
166 worktreesDir,
167 attachmentsDir,
168 browserArtifactsDir: path.join(stateDir, "browser-artifacts"),
169 environmentThemesDir,
170 logsDir,
171 serverLogPath: path.join(logsDir, "server.log"),
172 serverTracePath: path.join(logsDir, "server.trace.ndjson"),
173 providerLogsDir,
174 providerEventLogPath: path.join(providerLogsDir, "events.log"),
175 terminalLogsDir,
176 anonymousIdPath: path.join(stateDir, "anonymous-id"),
177 environmentIdPath: path.join(stateDir, "environment-id"),
178 serverRuntimeStatePath: path.join(stateDir, "server-runtime.json"),
179 secretsDir: path.join(stateDir, "secrets"),
180 };
181 });
183​
184export function layer(input: {
185 readonly transcript: CodexReplay.CodexAppServerReplayTranscript;
186 readonly driver?: CodexReplay.CodexAppServerReplayDriver;
187}) {
188 const layerReplay =
189 input.driver === undefined
190 ? CodexReplay.layerReplay(input.transcript)
191 : CodexReplay.layerReplayWithDriver(input.driver);
192 const layerReplayClientFactory = Layer.succeed(CodexAdapterV2.CodexAppServerClientFactory, {
193 open: (openInput) =>
194 Effect.gen(function* () {
195 const context = yield* Layer.build(layerReplay).pipe(
196 Effect.mapError(
197 (cause) =>
198 new ProviderAdapterOpenSessionError({
199 driver: CodexAdapterV2.CODEX_DRIVER_KIND,
200 providerSessionId: openInput.providerSessionId,
201 cause,
202 }),
203 ),
204 );
205 return yield* Effect.service(CodexClient.CodexAppServerClient).pipe(
206 Effect.map((client) => withCodexReplayChildMetadata(client, input.transcript)),
207 Effect.provide(context),
208 );
209 }),
210 });
211 const layerServerConfig = Layer.effect(
212 ServerConfig.ServerConfig,
213 makeReplayServerConfig(input.transcript.scenario).pipe(Effect.orDie),
214 ).pipe(Layer.provide(NodeServices.layer));
215 const layerRegistry = ProviderAdapterRegistry.layerFromDrivers({
216 drivers: [CodexAdapterV2.CodexAdapterV2Driver],
217 configMap: {
218 [CodexAdapterV2.CODEX_DEFAULT_INSTANCE_ID]: {
219 driver: CodexAdapterV2.CODEX_DRIVER_KIND,
220 },
221 },
222 }).pipe(
223 Layer.provide(
224 Layer.mergeAll(
225 layerReplayClientFactory,
226 layerServerConfig,
227 NodeServices.layer,
228 IdAllocator.layer,
229 ),
230 ),
231 );
232​
233 return layerRegistry;
235​
236const decodeCodexAppServerReplayTranscript = Schema.decodeUnknownEffect(
237 CodexReplay.CodexAppServerReplayTranscript,
238);
239​
240export const CodexOrchestratorReplayHarness: OrchestratorV2ProviderReplayHarness<
241 CodexReplay.CodexAppServerReplayTranscript,
242 CodexOrchestratorReplayHarnessError
243> = {
244 driver: CodexAdapterV2.CODEX_DRIVER_KIND,
245 decodeTranscript: (transcript) =>
246 decodeCodexAppServerReplayTranscript(transcript).pipe(
247 Effect.mapError(
248 (cause) =>
249 new CodexReplayTranscriptDecodeError({
250 ...metadataFromTranscript(transcript),
251 cause,
252 }),
253 ),
254 ),
255 makeProviderAdapterRegistryLayer: (
256 transcript,
257 options: { readonly replayGate?: ProviderReplayGate } = {},
258 ) => {
259 return Layer.effectContext(
260 Effect.gen(function* () {
261 const replayGate = options.replayGate;
262 if (replayGate !== undefined) {
263 yield* Effect.addFinalizer(() => Effect.sync(() => replayGate.releaseAll()));
264 }
265 const driver = yield* CodexReplay.makeReplayDriver(
266 transcript,
267 replayGate === undefined
268 ? {}
269 : {
270 beforeEmitInbound: (entry) =>
271 Effect.promise((signal) => replayGate.beforeEmit(entry.label, signal)),
272 },
273 );
274 return yield* Layer.build(layer({ transcript, driver }));
275 }),
276 );
277 },
278};