Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import * as Schema from "effect/Schema";
3
4import * as CodexSchema from "./schema.ts";
5
6const isGetAccountResponse = Schema.is(CodexSchema.V2GetAccountResponse);
7const isThreadReadResponse = Schema.is(CodexSchema.V2ThreadReadResponse);
8const isThreadResumeResponse = Schema.is(CodexSchema.V2ThreadResumeResponse);
9const isThreadForkResponse = Schema.is(CodexSchema.V2ThreadForkResponse);
10const isTurnCompletedNotification = Schema.is(CodexSchema.V2TurnCompletedNotification);
11const decodeThreadResumeResponse = Schema.decodeUnknownSync(CodexSchema.V2ThreadResumeResponse);
12
13it("keeps async questions in live notifications and thread history", () => {
14 const item = {
15 type: "agentMessage",
16 id: "question-1",
17 text: "Which package?\n- pnpm\n- npm\n\nWhat should it be named?",
18 phase: "final_answer",
19 delivery: "async",
20 questions: [
21 { title: "Which package manager?", options: ["pnpm", "npm"] },
22 { title: "What should it be named?" },
23 ],
24 } as const;
25 for (const schema of [
26 CodexSchema.ServerNotification__ThreadItem,
27 CodexSchema.V2ItemStartedNotification__ThreadItem,
28 CodexSchema.V2ItemCompletedNotification__ThreadItem,
29 CodexSchema.V2ThreadReadResponse__ThreadItem,
30 CodexSchema.V2ThreadResumeResponse__ThreadItem,
31 ]) {
32 assert.deepEqual(Schema.decodeSync(schema)(item), item);
33 }
34});
35
36it("keeps partial answers in notifications and persisted thread histories", () => {
37 const item = {
38 type: "agentMessage",
39 id: "partial-1",
40 text: "A stable answer before more tool work.",
41 phase: "partial_answer",
42 } as const;
43 for (const schema of [
44 CodexSchema.ServerNotification__ThreadItem,
45 CodexSchema.V2ItemStartedNotification__ThreadItem,
46 CodexSchema.V2ItemCompletedNotification__ThreadItem,
47 CodexSchema.V2ThreadReadResponse__ThreadItem,
48 CodexSchema.V2ThreadResumeResponse__ThreadItem,
49 CodexSchema.V2ThreadForkResponse__ThreadItem,
50 CodexSchema.V2TurnCompletedNotification__ThreadItem,
51 ]) {
52 assert.deepEqual(Schema.decodeSync(schema)(item), item);
53 }
54 const response = {
55 approvalPolicy: "never",
56 approvalsReviewer: "user",
57 cwd: "/tmp/project",
58 model: "gpt-5.6-sol",
59 modelProvider: "openai",
60 sandbox: { type: "dangerFullAccess" },
61 thread: {
62 cliVersion: "0.162.0",
63 createdAt: 0,
64 updatedAt: 0,
65 cwd: "/tmp/project",
66 ephemeral: false,
67 id: "thread-1",
68 modelProvider: "openai",
69 preview: "",
70 projectId: null,
71 sessionId: "session-1",
72 source: "cli",
73 status: { type: "idle" },
74 turns: [{ id: "turn-1", status: "completed", items: [item] }],
75 },
76 };
77 assert.equal(isThreadForkResponse(response), true);
78 assert.equal(isThreadResumeResponse(response), true);
79 assert.equal(isThreadReadResponse({ thread: response.thread }), true);
80});
81
82it("accepts Codex 0.150 multi-agent values", () => {
83 const schemas = [
84 CodexSchema.ServerNotification__SubAgentActivityKind,
85 CodexSchema.V2ItemStartedNotification__SubAgentActivityKind,
86 CodexSchema.V2ItemCompletedNotification__SubAgentActivityKind,
87 CodexSchema.V2ThreadReadResponse__SubAgentActivityKind,
88 CodexSchema.V2ThreadResumeResponse__SubAgentActivityKind,
89 ];
90
91 for (const schema of schemas) {
92 assert.equal(Schema.is(schema)("completed"), true);
93 }
94 const isServerNotificationCollabAgentTool = Schema.is(
95 CodexSchema.ServerNotification__CollabAgentTool,
96 );
97
98 const isV2ThreadResumeResponseCollabAgentTool = Schema.is(
99 CodexSchema.V2ThreadResumeResponse__CollabAgentTool,
100 );
101
102 for (const tool of ["sendMessage", "followupTask", "interruptAgent", "listAgents"]) {
103 assert.equal(isServerNotificationCollabAgentTool(tool), true);
104 assert.equal(isV2ThreadResumeResponseCollabAgentTool(tool), true);
105 }
106 const isServerNotificationCollabAgentToolCallStatus = Schema.is(
107 CodexSchema.ServerNotification__CollabAgentToolCallStatus,
108 );
109
110 assert.equal(isServerNotificationCollabAgentToolCallStatus("interrupted"), true);
111 const isV2ThreadResumeResponseCollabAgentToolCallStatus = Schema.is(
112 CodexSchema.V2ThreadResumeResponse__CollabAgentToolCallStatus,
113 );
114
115 assert.equal(isV2ThreadResumeResponseCollabAgentToolCallStatus("interrupted"), true);
116
117 const resumeResponse = {
118 approvalPolicy: "never",
119 approvalsReviewer: "user",
120 cwd: "/tmp/project",
121 model: "gpt-5.6-sol",
122 modelProvider: "openai",
123 sandbox: { type: "dangerFullAccess" },
124 thread: {
125 cliVersion: "0.150.0",
126 createdAt: 0,
127 cwd: "/tmp/project",
128 ephemeral: false,
129 id: "root-thread",
130 modelProvider: "openai",
131 preview: "",
132 projectId: null,
133 sessionId: "session-1",
134 source: "cli",
135 status: { type: "idle" },
136 turns: [
137 {
138 id: "turn-1",
139 status: "completed",
140 items: [
141 {
142 agentsStates: {},
143 id: "item-1",
144 receiverThreadIds: ["child-thread"],
145 senderThreadId: "root-thread",
146 status: "interrupted",
147 tool: "followupTask",
148 type: "collabAgentToolCall",
149 },
150 ],
151 },
152 ],
153 updatedAt: 0,
154 },
155 };
156 const isV2ThreadResumeResponse = Schema.is(CodexSchema.V2ThreadResumeResponse);
157
158 assert.equal(isV2ThreadResumeResponse(resumeResponse), true);
159});
160
161it("accepts Codex misalignment policy errors for thread responses", () => {
162 const failedThread = {
163 cliVersion: "0.150.0",
164 createdAt: 0,
165 cwd: "/tmp/project",
166 ephemeral: false,
167 id: "thread-1",
168 modelProvider: "openai",
169 preview: "",
170 projectId: null,
171 sessionId: "session-1",
172 source: "cli",
173 status: { type: "idle" },
174 turns: [
175 {
176 error: {
177 codexErrorInfo: "misalignmentPolicyViolation",
178 message: "Misalignment policy violation",
179 },
180 id: "turn-1",
181 items: [],
182 status: "failed",
183 },
184 ],
185 updatedAt: 0,
186 };
187 const resumeLikeResponse = {
188 approvalPolicy: "never",
189 approvalsReviewer: "user",
190 cwd: "/tmp/project",
191 model: "gpt-5.6-sol",
192 modelProvider: "openai",
193 sandbox: { type: "dangerFullAccess" },
194 thread: failedThread,
195 };
196 assert.equal(isThreadReadResponse({ thread: failedThread }), true);
197 assert.equal(isThreadResumeResponse(resumeLikeResponse), true);
198 assert.equal(isThreadForkResponse(resumeLikeResponse), true);
199 const decodedResume = decodeThreadResumeResponse(resumeLikeResponse);
200 assert.equal(decodedResume.thread.turns[0]?.error?.codexErrorInfo, "misalignmentPolicyViolation");
201 assert.equal(
202 isTurnCompletedNotification({
203 threadId: "thread-1",
204 turn: {
205 error: {
206 codexErrorInfo: "misalignmentPolicyViolation",
207 message: "Misalignment policy violation",
208 },
209 id: "turn-1",
210 items: [],
211 status: "failed",
212 },
213 }),
214 true,
215 );
216});
217
218it("accepts account plan slugs newer than the pinned protocol", () => {
219 const planTypes = [
220 "self_serve_business_prolite",
221 "ent26",
222 "enterprise_cbp_automation",
223 "edu_plus",
224 "edu_pro",
225 "promax",
226 "some_future_plan",
227 ];
228
229 for (const planType of planTypes) {
230 const accountResponse = {
231 account: {
232 email: "user@example.com",
233 planType,
234 type: "chatgpt",
235 },
236 requiresOpenaiAuth: true,
237 };
238
239 assert.equal(isGetAccountResponse(accountResponse), true);
240 }
241});