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

CodexSessionRuntime.test.ts

277 lines7.4 KB

1import assert from "node:assert/strict";
2​
3import * as Effect from "effect/Effect";
4import * as Schema from "effect/Schema";
5import { describe, it } from "vitest";
6import { ThreadId } from "@t3tools/contracts";
7import * as CodexErrors from "effect-codex-app-server/errors";
8import * as CodexRpc from "effect-codex-app-server/rpc";
9​
10import {
11 CODEX_DEFAULT_MODE_DEVELOPER_INSTRUCTIONS,
12 CODEX_PLAN_MODE_DEVELOPER_INSTRUCTIONS,
13} from "../CodexDeveloperInstructions.ts";
14import {
15 buildTurnStartParams,
16 isRecoverableThreadResumeError,
17 openCodexThread,
18} from "./CodexSessionRuntime.ts";
19const isCodexAppServerRequestError = Schema.is(CodexErrors.CodexAppServerRequestError);
20​
21function makeThreadOpenResponse(
22 threadId: string,
23): CodexRpc.ClientRequestResponsesByMethod["thread/start"] {
24 return {
25 cwd: "/tmp/project",
26 model: "gpt-5.3-codex",
27 modelProvider: "openai",
28 approvalPolicy: "never",
29 approvalsReviewer: "user",
30 sandbox: { type: "danger-full-access" },
31 thread: {
32 id: threadId,
33 createdAt: "2026-04-18T00:00:00.000Z",
34 source: { session: "cli" },
35 turns: [],
36 status: {
37 state: "idle",
38 activeFlags: [],
39 },
40 },
41 } as unknown as CodexRpc.ClientRequestResponsesByMethod["thread/start"];
43​
44describe("buildTurnStartParams", () => {
45 it("includes plan collaboration mode when requested", () => {
46 const params = Effect.runSync(
47 buildTurnStartParams({
48 threadId: "provider-thread-1",
49 runtimeMode: "full-access",
50 prompt: "Make a plan",
51 model: "gpt-5.3-codex",
52 effort: "medium",
53 interactionMode: "plan",
54 }),
55 );
56​
57 assert.deepStrictEqual(params, {
58 threadId: "provider-thread-1",
59 approvalPolicy: "never",
60 sandboxPolicy: {
61 type: "dangerFullAccess",
62 },
63 input: [
64 {
65 type: "text",
66 text: "Make a plan",
67 },
68 ],
69 model: "gpt-5.3-codex",
70 effort: "medium",
71 collaborationMode: {
72 mode: "plan",
73 settings: {
74 model: "gpt-5.3-codex",
75 reasoning_effort: "medium",
76 developer_instructions: CODEX_PLAN_MODE_DEVELOPER_INSTRUCTIONS,
77 },
78 },
79 });
80 });
81​
82 it("includes default collaboration mode and image attachments", () => {
83 const params = Effect.runSync(
84 buildTurnStartParams({
85 threadId: "provider-thread-1",
86 runtimeMode: "auto-accept-edits",
87 prompt: "Implement it",
88 model: "gpt-5.3-codex",
89 interactionMode: "default",
90 attachments: [
91 {
92 type: "image",
93 url: "data:image/png;base64,abc",
94 },
95 ],
96 }),
97 );
98​
99 assert.deepStrictEqual(params, {
100 threadId: "provider-thread-1",
101 approvalPolicy: "on-request",
102 sandboxPolicy: {
103 type: "workspaceWrite",
104 },
105 input: [
106 {
107 type: "text",
108 text: "Implement it",
109 },
110 {
111 type: "image",
112 url: "data:image/png;base64,abc",
113 },
114 ],
115 model: "gpt-5.3-codex",
116 collaborationMode: {
117 mode: "default",
118 settings: {
119 model: "gpt-5.3-codex",
120 reasoning_effort: "medium",
121 developer_instructions: CODEX_DEFAULT_MODE_DEVELOPER_INSTRUCTIONS,
122 },
123 },
124 });
125 });
126​
127 it("omits collaboration mode when interaction mode is absent", () => {
128 const params = Effect.runSync(
129 buildTurnStartParams({
130 threadId: "provider-thread-1",
131 runtimeMode: "approval-required",
132 prompt: "Review",
133 }),
134 );
135​
136 assert.deepStrictEqual(params, {
137 threadId: "provider-thread-1",
138 approvalPolicy: "untrusted",
139 sandboxPolicy: {
140 type: "readOnly",
141 },
142 input: [
143 {
144 type: "text",
145 text: "Review",
146 },
147 ],
148 });
149 });
150});
151​
152describe("isRecoverableThreadResumeError", () => {
153 it("matches missing thread errors", () => {
154 assert.equal(
155 isRecoverableThreadResumeError(
156 new CodexErrors.CodexAppServerRequestError({
157 code: -32603,
158 errorMessage: "Thread does not exist",
159 }),
160 ),
161 true,
162 );
163 });
164​
165 it("ignores non-recoverable resume errors", () => {
166 assert.equal(
167 isRecoverableThreadResumeError(
168 new CodexErrors.CodexAppServerRequestError({
169 code: -32603,
170 errorMessage: "Permission denied",
171 }),
172 ),
173 false,
174 );
175 });
176​
177 it("ignores unrelated missing-resource errors that do not mention threads", () => {
178 assert.equal(
179 isRecoverableThreadResumeError(
180 new CodexErrors.CodexAppServerRequestError({
181 code: -32603,
182 errorMessage: "Config file not found",
183 }),
184 ),
185 false,
186 );
187 assert.equal(
188 isRecoverableThreadResumeError(
189 new CodexErrors.CodexAppServerRequestError({
190 code: -32603,
191 errorMessage: "Model does not exist",
192 }),
193 ),
194 false,
195 );
196 });
197});
198​
199describe("openCodexThread", () => {
200 it("falls back to thread/start when resume fails recoverably", async () => {
201 const calls: Array<{ method: "thread/start" | "thread/resume"; payload: unknown }> = [];
202 const started = makeThreadOpenResponse("fresh-thread");
203 const client = {
204 request: <M extends "thread/start" | "thread/resume">(
205 method: M,
206 payload: CodexRpc.ClientRequestParamsByMethod[M],
207 ) => {
208 calls.push({ method, payload });
209 if (method === "thread/resume") {
210 return Effect.fail(
211 new CodexErrors.CodexAppServerRequestError({
212 code: -32603,
213 errorMessage: "thread not found",
214 }),
215 );
216 }
217 return Effect.succeed(started as CodexRpc.ClientRequestResponsesByMethod[M]);
218 },
219 };
220​
221 const opened = await Effect.runPromise(
222 openCodexThread({
223 client,
224 threadId: ThreadId.make("thread-1"),
225 runtimeMode: "full-access",
226 cwd: "/tmp/project",
227 requestedModel: "gpt-5.3-codex",
228 serviceTier: undefined,
229 resumeThreadId: "stale-thread",
230 }),
231 );
232​
233 assert.equal(opened.thread.id, "fresh-thread");
234 assert.deepStrictEqual(
235 calls.map((call) => call.method),
236 ["thread/resume", "thread/start"],
237 );
238 });
239​
240 it("propagates non-recoverable resume failures", async () => {
241 const client = {
242 request: <M extends "thread/start" | "thread/resume">(
243 method: M,
244 _payload: CodexRpc.ClientRequestParamsByMethod[M],
245 ) => {
246 if (method === "thread/resume") {
247 return Effect.fail(
248 new CodexErrors.CodexAppServerRequestError({
249 code: -32603,
250 errorMessage: "timed out waiting for server",
251 }),
252 );
253 }
254 return Effect.succeed(
255 makeThreadOpenResponse("fresh-thread") as CodexRpc.ClientRequestResponsesByMethod[M],
256 );
257 },
258 };
259​
260 await assert.rejects(
261 Effect.runPromise(
262 openCodexThread({
263 client,
264 threadId: ThreadId.make("thread-1"),
265 runtimeMode: "full-access",
266 cwd: "/tmp/project",
267 requestedModel: "gpt-5.3-codex",
268 serviceTier: undefined,
269 resumeThreadId: "stale-thread",
270 }),
271 ),
272 (error: unknown) =>
273 isCodexAppServerRequestError(error) &&
274 error.errorMessage === "timed out waiting for server",
275 );
276 });
277});