Open History to see the last commit
1import * as NodeAssert from "node:assert/strict";
2
3import { it } from "@effect/vitest";
4import * as Effect from "effect/Effect";
5import * as Schema from "effect/Schema";
6import { describe } from "vite-plus/test";
7import { DEFAULT_MODEL, ThreadId } from "@t3tools/contracts";
8import * as CodexErrors from "effect-codex-app-server/errors";
9import * as CodexRpc from "effect-codex-app-server/rpc";
10import * as EffectCodexSchema from "effect-codex-app-server/schema";
11
12import {
13 buildCodexDeveloperInstructions,
14 codexDefaultModeDeveloperInstructions,
15 codexPlanModeDeveloperInstructions,
16} from "../CodexDeveloperInstructions.ts";
17import { codexSessionAppServerArgs } from "./codexLaunchArgs.ts";
18import {
19 buildTurnStartParams,
20 describeMcpElicitation,
21 hasConfiguredMcpServer,
22 isRecoverableThreadResumeError,
23 makeMemoryConsolidationNotificationFilter,
24 openCodexThread,
25 toMcpElicitationResponse,
26} from "./CodexSessionRuntime.ts";
27const isCodexAppServerRequestError = Schema.is(CodexErrors.CodexAppServerRequestError);
28
29describe("CodexSessionRuntimeIdentifierGenerationError", () => {
30 it("retains identifier purpose and the random source failure", () => {
31 const cause = new Error("random source unavailable");
32 const error = new CodexErrors.CodexAppServerIdentifierGenerationError({
33 purpose: "provider-event",
34 cause,
35 });
36
37 NodeAssert.equal(error.purpose, "provider-event");
38 NodeAssert.strictEqual(error.cause, cause);
39 NodeAssert.equal(
40 error.message,
41 "Failed to generate Codex App Server identifier for provider-event.",
42 );
43 });
44});
45
46function makeThreadOpenResponse(
47 threadId: string,
48): CodexRpc.ClientRequestResponsesByMethod["thread/start"] {
49 return {
50 cwd: "/tmp/project",
51 model: "gpt-5.3-codex",
52 modelProvider: "openai",
53 approvalPolicy: "never",
54 approvalsReviewer: "user",
55 sandbox: { type: "danger-full-access" },
56 thread: {
57 id: threadId,
58 createdAt: "2026-04-18T00:00:00.000Z",
59 source: { session: "cli" },
60 turns: [],
61 status: {
62 state: "idle",
63 activeFlags: [],
64 },
65 },
66 } as unknown as CodexRpc.ClientRequestResponsesByMethod["thread/start"];
67}
68
69describe("buildTurnStartParams", () => {
70 it("keeps invalid turn values only in the schema cause", () => {
71 const secret = "codex-turn-input-secret-sentinel";
72 const error = Effect.runSync(
73 buildTurnStartParams({
74 threadId: "provider-thread-1",
75 runtimeMode: "full-access",
76 attachments: [
77 {
78 type: "image",
79 url: { secret } as unknown as string,
80 },
81 ],
82 }).pipe(Effect.flip),
83 );
84 const { cause, ...directDiagnostics } = error;
85
86 NodeAssert.equal(error.operation, "decode-request-payload");
87 NodeAssert.equal(error.method, "turn/start");
88 NodeAssert.ok((error.issueCount ?? 0) > 0);
89 NodeAssert.ok(error.issueKinds?.includes("Pointer"));
90 NodeAssert.ok((error.maximumPathDepth ?? 0) > 0);
91 NodeAssert.ok(Schema.isSchemaError(cause));
92 NodeAssert.doesNotMatch(error.message, new RegExp(secret));
93 NodeAssert.doesNotMatch(JSON.stringify(directDiagnostics), new RegExp(secret));
94 });
95
96 it("includes plan collaboration mode when requested", () => {
97 const params = Effect.runSync(
98 buildTurnStartParams({
99 threadId: "provider-thread-1",
100 runtimeMode: "full-access",
101 prompt: "Make a plan",
102 model: "gpt-5.3-codex",
103 effort: "medium",
104 interactionMode: "plan",
105 }),
106 );
107
108 NodeAssert.deepStrictEqual(params, {
109 threadId: "provider-thread-1",
110 approvalPolicy: "never",
111 approvalsReviewer: "user",
112 sandboxPolicy: {
113 type: "dangerFullAccess",
114 },
115 input: [
116 {
117 type: "text",
118 text: "Make a plan",
119 },
120 ],
121 model: "gpt-5.3-codex",
122 effort: "medium",
123 collaborationMode: {
124 mode: "plan",
125 settings: {
126 model: "gpt-5.3-codex",
127 reasoning_effort: "medium",
128 developer_instructions: buildCodexDeveloperInstructions("plan", {
129 model: "gpt-5.3-codex",
130 reasoningEffort: "medium",
131 }),
132 },
133 },
134 });
135 });
136
137 it("includes default collaboration mode and image attachments", () => {
138 const params = Effect.runSync(
139 buildTurnStartParams({
140 threadId: "provider-thread-1",
141 runtimeMode: "auto-accept-edits",
142 prompt: "Implement it",
143 model: "gpt-5.3-codex",
144 interactionMode: "default",
145 attachments: [
146 {
147 type: "image",
148 url: "data:image/png;base64,abc",
149 },
150 ],
151 }),
152 );
153
154 NodeAssert.deepStrictEqual(params, {
155 threadId: "provider-thread-1",
156 approvalPolicy: "on-request",
157 approvalsReviewer: "user",
158 sandboxPolicy: {
159 type: "workspaceWrite",
160 },
161 input: [
162 {
163 type: "text",
164 text: "Implement it",
165 },
166 {
167 type: "image",
168 url: "data:image/png;base64,abc",
169 },
170 ],
171 model: "gpt-5.3-codex",
172 collaborationMode: {
173 mode: "default",
174 settings: {
175 model: "gpt-5.3-codex",
176 reasoning_effort: "medium",
177 developer_instructions: buildCodexDeveloperInstructions("default", {
178 model: "gpt-5.3-codex",
179 reasoningEffort: "medium",
180 }),
181 },
182 },
183 });
184 });
185
186 it("reports the same fallback model and effort in settings and instructions", () => {
187 const params = Effect.runSync(
188 buildTurnStartParams({
189 threadId: "provider-thread-1",
190 runtimeMode: "full-access",
191 prompt: "Go",
192 interactionMode: "default",
193 }),
194 );
195
196 const settings = params.collaborationMode?.settings;
197 NodeAssert.equal(settings?.model, DEFAULT_MODEL);
198 NodeAssert.equal(settings?.reasoning_effort, "medium");
199 NodeAssert.ok(settings?.developer_instructions?.includes(`as ${DEFAULT_MODEL} with medium`));
200 });
201
202 it.effect("routes approvals to the auto reviewer in auto mode", () =>
203 Effect.gen(function* () {
204 const params = yield* buildTurnStartParams({
205 threadId: "provider-thread-1",
206 runtimeMode: "auto",
207 prompt: "Ship it",
208 });
209
210 NodeAssert.deepStrictEqual(params, {
211 threadId: "provider-thread-1",
212 approvalPolicy: "on-request",
213 approvalsReviewer: "auto_review",
214 sandboxPolicy: {
215 type: "workspaceWrite",
216 },
217 input: [
218 {
219 type: "text",
220 text: "Ship it",
221 },
222 ],
223 });
224 }),
225 );
226
227 it("omits collaboration mode when interaction mode is absent", () => {
228 const params = Effect.runSync(
229 buildTurnStartParams({
230 threadId: "provider-thread-1",
231 runtimeMode: "approval-required",
232 prompt: "Review",
233 }),
234 );
235
236 NodeAssert.deepStrictEqual(params, {
237 threadId: "provider-thread-1",
238 approvalPolicy: "untrusted",
239 approvalsReviewer: "user",
240 sandboxPolicy: {
241 type: "readOnly",
242 },
243 input: [
244 {
245 type: "text",
246 text: "Review",
247 },
248 ],
249 });
250 });
251});
252
253describe("Codex MCP elicitation approvals", () => {
254 const request = {
255 mode: "form",
256 message: "Allow ChatGPT to use Safari?",
257 serverName: "computer-use",
258 threadId: "provider-thread-1",
259 turnId: "turn-1",
260 _meta: {
261 app_name: "Safari",
262 persist: ["session", "always"],
263 },
264 requestedSchema: {
265 type: "object",
266 properties: {
267 approval: {
268 type: "string",
269 oneOf: [
270 { const: "once", title: "Allow once" },
271 { const: "session", title: "Allow for this session" },
272 { const: "always", title: "Always allow Safari" },
273 ],
274 },
275 },
276 required: ["approval"],
277 },
278 } satisfies EffectCodexSchema.McpServerElicitationRequestParams;
279
280 it("preserves the app name and advertised persistence choices", () => {
281 NodeAssert.deepStrictEqual(describeMcpElicitation(request), {
282 appName: "Safari",
283 options: [
284 { decision: "cancel", label: "Cancel" },
285 { decision: "decline", label: "Decline" },
286 { decision: "acceptForSession", label: "Allow for this session" },
287 { decision: "acceptAlways", label: "Always allow Safari" },
288 { decision: "accept", label: "Approve" },
289 ],
290 });
291 });
292
293 it("extracts the app name from a Computer Use request without metadata", () => {
294 const { _meta, ...requestWithoutMetadata } = request;
295
296 NodeAssert.equal(describeMcpElicitation(requestWithoutMetadata).appName, "Safari");
297 });
298
299 it("returns the accepted form option to Codex", () => {
300 NodeAssert.deepStrictEqual(toMcpElicitationResponse(request, "accept"), {
301 action: "accept",
302 content: { approval: "once" },
303 });
304 });
305
306 it("returns session-scoped approval in the MCP response", () => {
307 NodeAssert.deepStrictEqual(toMcpElicitationResponse(request, "acceptForSession"), {
308 action: "accept",
309 _meta: { persist: "session" },
310 content: { approval: "session" },
311 });
312 });
313
314 it("returns persistent approval in the MCP response", () => {
315 NodeAssert.deepStrictEqual(toMcpElicitationResponse(request, "acceptAlways"), {
316 action: "accept",
317 _meta: { persist: "always" },
318 content: { approval: "always" },
319 });
320 });
321
322 it("returns rejection without form content", () => {
323 NodeAssert.deepStrictEqual(toMcpElicitationResponse(request, "decline"), {
324 action: "decline",
325 });
326 });
327
328 it("returns cancellation without form content", () => {
329 NodeAssert.deepStrictEqual(toMcpElicitationResponse(request, "cancel"), {
330 action: "cancel",
331 });
332 });
333
334 it("supports boolean permanent-approval fields", () => {
335 const booleanRequest = {
336 ...request,
337 _meta: { app_name: "Safari" },
338 requestedSchema: {
339 type: "object",
340 properties: {
341 always: { type: "boolean", title: "Always allow Safari" },
342 },
343 },
344 } satisfies EffectCodexSchema.McpServerElicitationRequestParams;
345
346 NodeAssert.ok(
347 describeMcpElicitation(booleanRequest).options.some(
348 (option) => option.decision === "acceptAlways",
349 ),
350 );
351 NodeAssert.deepStrictEqual(toMcpElicitationResponse(booleanRequest, "acceptAlways"), {
352 action: "accept",
353 _meta: { persist: "always" },
354 content: { always: true },
355 });
356 });
357
358 it("preserves valid nullable MCP form fields and persistence choices", () => {
359 const nullableRequest = {
360 ...request,
361 _meta: {
362 app_name: null,
363 appName: "Safari",
364 connector_name: null,
365 persist: null,
366 target: null,
367 tool_params: null,
368 },
369 requestedSchema: {
370 type: "object",
371 properties: {
372 approval: {
373 type: "string",
374 title: null,
375 description: null,
376 default: null,
377 enum: ["once", "always"],
378 enumNames: null,
379 },
380 },
381 required: ["approval"],
382 },
383 } satisfies EffectCodexSchema.McpServerElicitationRequestParams;
384
385 NodeAssert.equal(describeMcpElicitation(nullableRequest).appName, "Safari");
386 NodeAssert.ok(
387 describeMcpElicitation(nullableRequest).options.some(
388 (option) => option.decision === "acceptAlways",
389 ),
390 );
391 NodeAssert.deepStrictEqual(toMcpElicitationResponse(nullableRequest, "acceptAlways"), {
392 action: "accept",
393 _meta: { persist: "always" },
394 content: { approval: "always" },
395 });
396 });
397
398 it("declines required form fields that an approval prompt cannot collect", () => {
399 const inputRequest = {
400 ...request,
401 requestedSchema: {
402 type: "object",
403 properties: {
404 email: { type: "string", format: "email" },
405 },
406 required: ["email"],
407 },
408 } satisfies EffectCodexSchema.McpServerElicitationRequestParams;
409
410 NodeAssert.deepStrictEqual(toMcpElicitationResponse(inputRequest, "accept"), {
411 action: "decline",
412 });
413 });
414
415 it("does not approve URL elicitations without opening their requested URL", () => {
416 const urlRequest = {
417 mode: "url",
418 message: "Finish signing in to continue.",
419 serverName: "computer-use",
420 threadId: "provider-thread-1",
421 turnId: "turn-1",
422 elicitationId: "sign-in-1",
423 url: "https://example.com/authorize",
424 } satisfies EffectCodexSchema.McpServerElicitationRequestParams;
425
426 NodeAssert.deepStrictEqual(toMcpElicitationResponse(urlRequest, "accept"), {
427 action: "decline",
428 });
429 });
430
431 it("omits persistence choices that cannot satisfy required form fields", () => {
432 const onceOnlyRequest = {
433 ...request,
434 _meta: { app_name: "Safari", persist: ["session", "always"] },
435 requestedSchema: {
436 type: "object",
437 properties: {
438 approval: {
439 type: "string",
440 enum: ["once"],
441 },
442 },
443 required: ["approval"],
444 },
445 } satisfies EffectCodexSchema.McpServerElicitationRequestParams;
446
447 NodeAssert.deepStrictEqual(describeMcpElicitation(onceOnlyRequest).options, [
448 { decision: "cancel", label: "Cancel" },
449 { decision: "decline", label: "Decline" },
450 { decision: "accept", label: "Approve" },
451 ]);
452 });
453});
454
455describe("buildCodexDeveloperInstructions", () => {
456 it("appends runtime info after the mode instructions", () => {
457 const instructions = buildCodexDeveloperInstructions("default", {
458 model: "gpt-5.3-codex",
459 reasoningEffort: "high",
460 });
461
462 NodeAssert.ok(instructions.startsWith(codexDefaultModeDeveloperInstructions(true)));
463 NodeAssert.match(instructions, /T3 Code/);
464 NodeAssert.match(instructions, /Codex harness/);
465 NodeAssert.match(instructions, /as gpt-5\.3-codex with high reasoning effort/);
466 });
467
468 it("describes Markdown media support in the runtime context in both modes", () => {
469 for (const mode of ["default", "plan"] as const) {
470 const instructions = buildCodexDeveloperInstructions(mode, {
471 model: "gpt-5.3-codex",
472 reasoningEffort: "high",
473 });
474 NodeAssert.match(
475 instructions,
476 /<runtime_info>.*embed images and videos.*Markdown.*<\/runtime_info>/,
477 );
478 }
479 });
480
481 it("includes runtime info alongside plan mode instructions", () => {
482 const instructions = buildCodexDeveloperInstructions("plan", {
483 model: "gpt-5.3-codex",
484 reasoningEffort: "medium",
485 });
486
487 NodeAssert.ok(instructions.startsWith(codexPlanModeDeveloperInstructions(true)));
488 NodeAssert.match(instructions, /as gpt-5\.3-codex with medium reasoning effort/);
489 });
490
491 it("varies with the model and effort of each turn", () => {
492 const first = buildCodexDeveloperInstructions("default", {
493 model: "gpt-5.3-codex",
494 reasoningEffort: "medium",
495 });
496 const second = buildCodexDeveloperInstructions("default", {
497 model: "gpt-5.4",
498 reasoningEffort: "high",
499 });
500
501 NodeAssert.notEqual(first, second);
502 });
503
504 it("flattens multiline metadata into single-line runtime info", () => {
505 const instructions = buildCodexDeveloperInstructions("default", {
506 model: "gpt\n5.3\ncodex",
507 reasoningEffort: " high\neffort ",
508 });
509
510 NodeAssert.match(instructions, /as gpt 5\.3 codex with high effort reasoning effort/);
511 NodeAssert.doesNotMatch(instructions, /<runtime_info>[^<]*\n/);
512 });
513});
514
515describe("T3 browser developer instructions", () => {
516 it("prefers the product-native preview tools in both collaboration modes", () => {
517 for (const instructions of [
518 codexDefaultModeDeveloperInstructions(true),
519 codexPlanModeDeveloperInstructions(true),
520 ]) {
521 NodeAssert.match(instructions, /t3-code/);
522 NodeAssert.match(instructions, /preview_status/);
523 NodeAssert.match(instructions, /preview_open/);
524 NodeAssert.match(instructions, /Do not switch to global browser skills/);
525 }
526 });
527
528 it("omits the browser block entirely when the preview tools are not attached", () => {
529 for (const instructions of [
530 codexDefaultModeDeveloperInstructions(false),
531 codexPlanModeDeveloperInstructions(false),
532 ]) {
533 NodeAssert.doesNotMatch(instructions, /preview_status/);
534 NodeAssert.doesNotMatch(instructions, /preview_open/);
535 NodeAssert.doesNotMatch(instructions, /T3 Code collaborative browser/);
536 // Steering away from other browser automation must go with the tools;
537 // keeping it would leave the model talked out of its only option.
538 NodeAssert.doesNotMatch(instructions, /Do not switch to global browser skills/);
539 // The rest of the collaboration mode is untouched.
540 NodeAssert.match(instructions, /<collaboration_mode>/);
541 NodeAssert.match(instructions, /<\/collaboration_mode>/);
542 }
543 });
544
545 it("tracks the turn's MCP configuration rather than defaulting to on", () => {
546 const runtime = { model: "gpt-5.3-codex", reasoningEffort: "high" };
547 NodeAssert.match(buildCodexDeveloperInstructions("default", runtime, true), /preview_open/);
548 NodeAssert.doesNotMatch(
549 buildCodexDeveloperInstructions("default", runtime, false),
550 /preview_open/,
551 );
552 });
553});
554
555describe("hasConfiguredMcpServer", () => {
556 it("detects inline Codex MCP configuration arguments", () => {
557 NodeAssert.equal(hasConfiguredMcpServer(undefined), false);
558 NodeAssert.equal(hasConfiguredMcpServer(["--model", "gpt-5.4"]), false);
559 NodeAssert.equal(
560 hasConfiguredMcpServer(["-c", 'mcp_servers.t3-code.url="http://127.0.0.1/mcp"']),
561 true,
562 );
563 });
564});
565
566function makeThreadStartedNotification(
567 threadId: string,
568 source: EffectCodexSchema.V2ThreadStartedNotification["thread"]["source"],
569 threadSource?: string,
570) {
571 return {
572 method: "thread/started" as const,
573 params: {
574 thread: {
575 cliVersion: "0.0.0",
576 createdAt: 0,
577 cwd: "/tmp/project",
578 ephemeral: true,
579 id: threadId,
580 modelProvider: "openai",
581 preview: "",
582 sessionId: threadId,
583 source,
584 status: { type: "idle" as const },
585 ...(threadSource ? { threadSource } : {}),
586 turns: [],
587 updatedAt: 0,
588 },
589 },
590 };
591}
592
593describe("makeMemoryConsolidationNotificationFilter", () => {
594 it("suppresses memory consolidation without hiding other Codex subagents", () => {
595 const shouldSuppress = makeMemoryConsolidationNotificationFilter();
596
597 NodeAssert.equal(
598 shouldSuppress(
599 makeThreadStartedNotification("memory-thread", "unknown", "memory_consolidation"),
600 ),
601 true,
602 );
603 NodeAssert.equal(
604 shouldSuppress({
605 method: "item/agentMessage/delta",
606 params: {
607 delta: "internal memory update",
608 itemId: "memory-message",
609 threadId: "memory-thread",
610 turnId: "memory-turn",
611 },
612 }),
613 true,
614 );
615 NodeAssert.equal(
616 shouldSuppress({
617 method: "serverRequest/resolved",
618 params: {
619 requestId: "memory-approval",
620 threadId: "memory-thread",
621 },
622 }),
623 false,
624 );
625 NodeAssert.equal(
626 shouldSuppress({
627 method: "warning",
628 params: {
629 message: "internal warning",
630 threadId: "memory-thread",
631 },
632 }),
633 true,
634 );
635 NodeAssert.equal(
636 shouldSuppress({
637 method: "item/agentMessage/delta",
638 params: {
639 delta: "normal reply",
640 itemId: "root-message",
641 threadId: "root-thread",
642 turnId: "root-turn",
643 },
644 }),
645 false,
646 );
647
648 NodeAssert.equal(
649 shouldSuppress(
650 makeThreadStartedNotification("legacy-memory-thread", {
651 subAgent: "memory_consolidation",
652 }),
653 ),
654 true,
655 );
656
657 for (const source of [
658 { subAgent: "review" as const },
659 { subAgent: "compact" as const },
660 {
661 subAgent: {
662 thread_spawn: {
663 depth: 1,
664 parent_thread_id: "root-thread",
665 },
666 },
667 },
668 ]) {
669 NodeAssert.equal(
670 shouldSuppress(makeThreadStartedNotification("visible-subagent", source)),
671 false,
672 );
673 }
674 });
675
676 it("forgets memory consolidation threads after they close", () => {
677 const shouldSuppress = makeMemoryConsolidationNotificationFilter();
678 shouldSuppress(
679 makeThreadStartedNotification("memory-thread", "unknown", "memory_consolidation"),
680 );
681
682 NodeAssert.equal(
683 shouldSuppress({
684 method: "thread/closed",
685 params: { threadId: "memory-thread" },
686 }),
687 true,
688 );
689 NodeAssert.equal(
690 shouldSuppress({
691 method: "item/agentMessage/delta",
692 params: {
693 delta: "later message",
694 itemId: "later-message",
695 threadId: "memory-thread",
696 turnId: "later-turn",
697 },
698 }),
699 false,
700 );
701 });
702});
703
704describe("codexSessionAppServerArgs", () => {
705 it("keeps the app-server subcommand when explicit args are provided", () => {
706 NodeAssert.deepStrictEqual(codexSessionAppServerArgs(["-c", "model=gpt-5"], undefined), [
707 "app-server",
708 "-c",
709 "model=gpt-5",
710 ]);
711 });
712
713 it("keeps launch args when explicit app-server args are provided", () => {
714 NodeAssert.deepStrictEqual(
715 codexSessionAppServerArgs(
716 ["-c", "mcp_servers.t3-code.url=http://127.0.0.1/mcp"],
717 "--strict-config --enable foo",
718 ),
719 [
720 "app-server",
721 "--strict-config",
722 "--enable",
723 "foo",
724 "-c",
725 "mcp_servers.t3-code.url=http://127.0.0.1/mcp",
726 ],
727 );
728 });
729});
730
731describe("isRecoverableThreadResumeError", () => {
732 it("matches missing thread errors", () => {
733 NodeAssert.equal(
734 isRecoverableThreadResumeError(
735 new CodexErrors.CodexAppServerRequestError({
736 code: -32603,
737 errorMessage: "Thread does not exist",
738 }),
739 ),
740 true,
741 );
742 });
743
744 it("matches a missing rollout for a known thread id", () => {
745 NodeAssert.equal(
746 isRecoverableThreadResumeError(
747 new CodexErrors.CodexAppServerRequestError({
748 code: -32603,
749 errorMessage: "no rollout found for thread id 019fdf74-aaa9-7950-b252-7cc7a8650470",
750 }),
751 ),
752 true,
753 );
754 });
755
756 it("ignores non-recoverable resume errors", () => {
757 NodeAssert.equal(
758 isRecoverableThreadResumeError(
759 new CodexErrors.CodexAppServerRequestError({
760 code: -32603,
761 errorMessage: "Permission denied",
762 }),
763 ),
764 false,
765 );
766 });
767
768 it("ignores unrelated missing-resource errors that do not mention threads", () => {
769 NodeAssert.equal(
770 isRecoverableThreadResumeError(
771 new CodexErrors.CodexAppServerRequestError({
772 code: -32603,
773 errorMessage: "Config file not found",
774 }),
775 ),
776 false,
777 );
778 NodeAssert.equal(
779 isRecoverableThreadResumeError(
780 new CodexErrors.CodexAppServerRequestError({
781 code: -32603,
782 errorMessage: "Model does not exist",
783 }),
784 ),
785 false,
786 );
787 });
788});
789
790describe("openCodexThread", () => {
791 it.effect("falls back to thread/start when resume fails recoverably", () =>
792 Effect.gen(function* () {
793 const calls: Array<{ method: "thread/start" | "thread/resume"; payload: unknown }> = [];
794 const started = makeThreadOpenResponse("fresh-thread");
795 const client = {
796 request: <M extends "thread/start" | "thread/resume">(
797 method: M,
798 payload: CodexRpc.ClientRequestParamsByMethod[M],
799 ) => {
800 calls.push({ method, payload });
801 if (method === "thread/resume") {
802 return Effect.fail(
803 new CodexErrors.CodexAppServerRequestError({
804 code: -32603,
805 errorMessage: "thread not found",
806 }),
807 );
808 }
809 return Effect.succeed(started as CodexRpc.ClientRequestResponsesByMethod[M]);
810 },
811 };
812
813 const opened = yield* openCodexThread({
814 client,
815 threadId: ThreadId.make("thread-1"),
816 runtimeMode: "full-access",
817 cwd: "/tmp/project",
818 requestedModel: "gpt-5.3-codex",
819 serviceTier: undefined,
820 resumeThreadId: "stale-thread",
821 });
822
823 NodeAssert.equal(opened.thread.id, "fresh-thread");
824 NodeAssert.deepStrictEqual(
825 calls.map((call) => call.method),
826 ["thread/resume", "thread/start"],
827 );
828 }),
829 );
830
831 it.effect("propagates non-recoverable resume failures", () =>
832 Effect.gen(function* () {
833 const client = {
834 request: <M extends "thread/start" | "thread/resume">(
835 method: M,
836 _payload: CodexRpc.ClientRequestParamsByMethod[M],
837 ) => {
838 if (method === "thread/resume") {
839 return Effect.fail(
840 new CodexErrors.CodexAppServerRequestError({
841 code: -32603,
842 errorMessage: "timed out waiting for server",
843 }),
844 );
845 }
846 return Effect.succeed(
847 makeThreadOpenResponse("fresh-thread") as CodexRpc.ClientRequestResponsesByMethod[M],
848 );
849 },
850 };
851
852 const error = yield* openCodexThread({
853 client,
854 threadId: ThreadId.make("thread-1"),
855 runtimeMode: "full-access",
856 cwd: "/tmp/project",
857 requestedModel: "gpt-5.3-codex",
858 serviceTier: undefined,
859 resumeThreadId: "stale-thread",
860 }).pipe(Effect.flip);
861
862 NodeAssert.ok(isCodexAppServerRequestError(error));
863 NodeAssert.equal(error.errorMessage, "timed out waiting for server");
864 }),
865 );
866});