@niwa-labs/harness-codex 0.0.0-stage → 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +16 -2
- package/package.json +20 -4
- package/scripts/gen-codex.ts +352 -0
- package/src/control.ts +1596 -0
- package/src/fixtures.ts +103 -0
- package/src/format.ts +196 -0
- package/src/incoming.ts +273 -0
- package/src/index.ts +128 -0
- package/src/paths.ts +42 -0
- package/src/protocol/generated/app-server.ts +2818 -0
- package/src/protocol/index.ts +106 -0
- package/src/reading.ts +940 -0
- package/src/refusals.ts +72 -0
- package/src/reverse.ts +243 -0
- package/src/skills.ts +6 -0
- package/src/tools.ts +69 -0
- package/test/codex-conformance.test.ts +29 -0
- package/test/codex-control-process.test.ts +1239 -0
- package/test/codex-control.test.ts +217 -0
- package/test/codex-elicitation.test.ts +71 -0
- package/test/codex-protocol.test.ts +245 -0
- package/test/codex-reading.test.ts +513 -0
- package/test/fixtures/codex-app-server/captured.jsonl +21 -0
- package/test/fixtures/codex-frames.ts +88 -0
- package/test/fixtures/zod-instance.ts +35 -0
- package/test/profile.test.ts +48 -0
- package/tsconfig.json +6 -0
package/src/refusals.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How the control side refuses a request of `codex app-server` that nobody can be asked about.
|
|
3
|
+
*
|
|
4
|
+
* Codex is always answered: unanswered, its turn waits forever. A session that has nobody to ask
|
|
5
|
+
* gets each request refused in the words Codex takes for a refusal, and so does a request whose
|
|
6
|
+
* parameters this build does not read. A request that has no answer niwa can give is answered with
|
|
7
|
+
* a JSON-RPC error. A session in `full` mode is never asked, so only `ask` and `auto` sessions get
|
|
8
|
+
* here, and only to leave their sandbox.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type {
|
|
12
|
+
ApplyPatchApprovalResponse,
|
|
13
|
+
CommandExecutionRequestApprovalResponse,
|
|
14
|
+
DynamicToolCallResponse,
|
|
15
|
+
ExecCommandApprovalResponse,
|
|
16
|
+
FileChangeRequestApprovalResponse,
|
|
17
|
+
JSONRPCErrorError,
|
|
18
|
+
McpServerElicitationRequestResponse,
|
|
19
|
+
PermissionsRequestApprovalResponse,
|
|
20
|
+
ServerRequest,
|
|
21
|
+
ToolRequestUserInputResponse,
|
|
22
|
+
} from "./protocol/generated/app-server.ts";
|
|
23
|
+
|
|
24
|
+
/** The method not found: what JSON-RPC says of a request that is not served. */
|
|
25
|
+
const METHOD_NOT_FOUND = -32601;
|
|
26
|
+
|
|
27
|
+
/** What goes back to Codex: the result it takes, or an error. */
|
|
28
|
+
export type Reply =
|
|
29
|
+
| {
|
|
30
|
+
readonly result:
|
|
31
|
+
| CommandExecutionRequestApprovalResponse
|
|
32
|
+
| FileChangeRequestApprovalResponse
|
|
33
|
+
| PermissionsRequestApprovalResponse
|
|
34
|
+
| ToolRequestUserInputResponse
|
|
35
|
+
| McpServerElicitationRequestResponse
|
|
36
|
+
| DynamicToolCallResponse
|
|
37
|
+
| ApplyPatchApprovalResponse
|
|
38
|
+
| ExecCommandApprovalResponse;
|
|
39
|
+
}
|
|
40
|
+
| { readonly error: JSONRPCErrorError };
|
|
41
|
+
|
|
42
|
+
function cannotServe(method: string): Reply {
|
|
43
|
+
return { error: { code: METHOD_NOT_FOUND, message: `this control side cannot serve ${method}` } };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The refusal of each request Codex makes: one for every method, or it does not compile. */
|
|
47
|
+
const REFUSALS: { readonly [Method in ServerRequest["method"]]: Reply } = {
|
|
48
|
+
"item/commandExecution/requestApproval": { result: { decision: "decline" } },
|
|
49
|
+
"item/fileChange/requestApproval": { result: { decision: "decline" } },
|
|
50
|
+
// Nothing is granted, and what was not granted is not granted for the turn.
|
|
51
|
+
"item/permissions/requestApproval": { result: { permissions: {}, scope: "turn" } },
|
|
52
|
+
"item/tool/requestUserInput": { result: { answers: {} } },
|
|
53
|
+
// niwa gives a session its tools as MCP servers; it serves no dynamic tool of its own.
|
|
54
|
+
"item/tool/call": { result: { contentItems: [], success: false } },
|
|
55
|
+
// An elicitation niwa cannot ask a person (a link to open, a form of OpenAI's own kind), and one of a session with nobody to ask: declined.
|
|
56
|
+
"mcpServer/elicitation/request": { result: { action: "decline", content: null, _meta: null } },
|
|
57
|
+
// The requests of the older protocol, for the clients that speak it: the same refusal in its words.
|
|
58
|
+
applyPatchApproval: { result: { decision: { denied: { rejection: "niwa asks nobody to approve a patch here" } } } },
|
|
59
|
+
execCommandApproval: { result: { decision: { denied: { rejection: "niwa asks nobody to approve a command here" } } } },
|
|
60
|
+
// niwa holds no sign-in of Codex's and attests nothing: there is no answer to give.
|
|
61
|
+
"account/chatgptAuthTokens/refresh": cannotServe("account/chatgptAuthTokens/refresh"),
|
|
62
|
+
"attestation/generate": cannotServe("attestation/generate"),
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
function isKnown(method: string): method is ServerRequest["method"] {
|
|
66
|
+
return Object.hasOwn(REFUSALS, method);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** The reply that refuses a request, whether or not the method is one the schemas know. */
|
|
70
|
+
export function refusalOf(method: string): Reply {
|
|
71
|
+
return isKnown(method) ? REFUSALS[method] : cannotServe(method);
|
|
72
|
+
}
|
package/src/reverse.ts
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What Codex asks its client in the middle of a turn, as niwa's common questions.
|
|
3
|
+
*
|
|
4
|
+
* `codex app-server` makes five requests that wait for a person: the approval of a command, of a
|
|
5
|
+
* file change and of extra permissions, `item/tool/requestUserInput`, and `mcpServer/elicitation/request`,
|
|
6
|
+
* which is where Codex asks leave to call a tool of an MCP server and where a server asks for a form to be filled.
|
|
7
|
+
* This module turns each into a `Question` and turns the answer back into the reply Codex takes. It never talks to
|
|
8
|
+
* the process: the control side sends and receives.
|
|
9
|
+
*
|
|
10
|
+
* Codex has more words for a decision than niwa has: "accept for the session", an exec-policy or a
|
|
11
|
+
* network-policy amendment, a permission granted for the session. None of them is ever sent: an
|
|
12
|
+
* approval is `accept`, `decline` or `cancel`, and a permission is granted for the turn. "Always" is
|
|
13
|
+
* niwa's own memory.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { assertNever, JsonSchemaValue, OptionIdSchema, pickedOptions, stableJson } from "@niwa-labs/core";
|
|
17
|
+
import type { Answer, Question, QuestionOption } from "@niwa-labs/core";
|
|
18
|
+
|
|
19
|
+
import type {
|
|
20
|
+
CommandExecutionRequestApprovalResponse,
|
|
21
|
+
FileChangeRequestApprovalResponse,
|
|
22
|
+
McpServerElicitationRequestParams,
|
|
23
|
+
McpServerElicitationRequestResponse,
|
|
24
|
+
PermissionsRequestApprovalResponse,
|
|
25
|
+
ServerRequest,
|
|
26
|
+
ToolRequestUserInputParams,
|
|
27
|
+
} from "./protocol/generated/app-server.ts";
|
|
28
|
+
|
|
29
|
+
/** The approvals of Codex: of a command, of a file change, of extra permissions. */
|
|
30
|
+
export type ApprovalRequest = Extract<
|
|
31
|
+
ServerRequest,
|
|
32
|
+
{
|
|
33
|
+
method:
|
|
34
|
+
| "item/commandExecution/requestApproval"
|
|
35
|
+
| "item/fileChange/requestApproval"
|
|
36
|
+
| "item/permissions/requestApproval";
|
|
37
|
+
}
|
|
38
|
+
>;
|
|
39
|
+
|
|
40
|
+
/** The elicitation of an MCP server: the approval of one of its tools, or a form it wants filled. */
|
|
41
|
+
export type ElicitationRequest = Extract<ServerRequest, { method: "mcpServer/elicitation/request" }>;
|
|
42
|
+
|
|
43
|
+
/** The requests niwa routes to the one who started the session: the approvals and the questions. */
|
|
44
|
+
export type ReversedRequest =
|
|
45
|
+
| ApprovalRequest
|
|
46
|
+
| ElicitationRequest
|
|
47
|
+
| Extract<ServerRequest, { method: "item/tool/requestUserInput" }>;
|
|
48
|
+
|
|
49
|
+
/** How much of the request is carried in a question's detail; the whole of it is kept in `raw`. */
|
|
50
|
+
const DETAIL_CHARS = 500;
|
|
51
|
+
|
|
52
|
+
/** What Codex is told of an approval: `cancel` also interrupts the turn. */
|
|
53
|
+
type Decision = "accept" | "decline" | "cancel";
|
|
54
|
+
|
|
55
|
+
/** The two options of an approval: the once-only ones Codex has, under its own words for them. */
|
|
56
|
+
const OPTIONS: readonly QuestionOption[] = [
|
|
57
|
+
{ id: OptionIdSchema.parse("accept"), label: "Allow once", flavor: "allow_once" },
|
|
58
|
+
{ id: OptionIdSchema.parse("decline"), label: "Decline", flavor: "reject_once" },
|
|
59
|
+
];
|
|
60
|
+
|
|
61
|
+
/** An approval of Codex: the question for the starter, and the reply Codex takes for each answer. */
|
|
62
|
+
export interface Approval {
|
|
63
|
+
readonly question: Question;
|
|
64
|
+
reply(answer: Answer): CommandExecutionRequestApprovalResponse | FileChangeRequestApprovalResponse | PermissionsRequestApprovalResponse;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function decisionOf(question: Question, answer: Answer): Decision {
|
|
68
|
+
const picked = pickedOptions(question, answer)[0];
|
|
69
|
+
if (picked?.flavor === "allow_once") return "accept";
|
|
70
|
+
if (picked?.flavor === "reject_once") return "decline";
|
|
71
|
+
return "cancel";
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function lines(...parts: ReadonlyArray<string | null | undefined>): string | undefined {
|
|
75
|
+
const given = parts.filter((part): part is string => part !== null && part !== undefined && part !== "");
|
|
76
|
+
return given.length === 0 ? undefined : given.join("\n");
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The approval a request of Codex is. `changed` are the paths of the file change the request is
|
|
81
|
+
* about, as Codex announced the item: the request itself names none.
|
|
82
|
+
*/
|
|
83
|
+
export function approvalOf(request: ApprovalRequest, changed: readonly string[] | undefined): Approval {
|
|
84
|
+
switch (request.method) {
|
|
85
|
+
case "item/commandExecution/requestApproval": {
|
|
86
|
+
const { command, cwd, reason, networkApprovalContext: network, approvalId, itemId } = request.params;
|
|
87
|
+
const host = network == null ? undefined : `${network.protocol}://${network.host}`;
|
|
88
|
+
const target = command == null ? undefined : (lines(cwd, command) ?? command);
|
|
89
|
+
const title = command ?? (host === undefined ? "Run a command" : `Network access: ${host}`);
|
|
90
|
+
const detail = lines(reason, cwd == null ? undefined : `in ${cwd}`);
|
|
91
|
+
const question: Question = {
|
|
92
|
+
kind: "permission",
|
|
93
|
+
title,
|
|
94
|
+
...(detail === undefined ? {} : { detail }),
|
|
95
|
+
options: [...OPTIONS],
|
|
96
|
+
subject: {
|
|
97
|
+
tool: "commandExecution",
|
|
98
|
+
kind: command == null && host !== undefined ? "network" : "execute",
|
|
99
|
+
target: target ?? host ?? approvalId ?? itemId,
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
return { question, reply: (answer) => ({ decision: decisionOf(question, answer) }) };
|
|
103
|
+
}
|
|
104
|
+
case "item/fileChange/requestApproval": {
|
|
105
|
+
const { reason, grantRoot, itemId } = request.params;
|
|
106
|
+
const paths = [...(changed ?? []), ...(grantRoot === null || grantRoot === undefined ? [] : [`root ${grantRoot}`])];
|
|
107
|
+
const question: Question = {
|
|
108
|
+
kind: "permission",
|
|
109
|
+
title: paths.length === 0 ? "Change files" : `Change files: ${paths.join(", ")}`,
|
|
110
|
+
...(reason == null || reason === "" ? {} : { detail: reason }),
|
|
111
|
+
options: [...OPTIONS],
|
|
112
|
+
subject: { tool: "fileChange", kind: "edit", target: paths.length === 0 ? itemId : paths.join("\n") },
|
|
113
|
+
};
|
|
114
|
+
return { question, reply: (answer) => ({ decision: decisionOf(question, answer) }) };
|
|
115
|
+
}
|
|
116
|
+
case "item/permissions/requestApproval": {
|
|
117
|
+
const { network, fileSystem } = request.params.permissions;
|
|
118
|
+
const granted = {
|
|
119
|
+
...(network == null ? {} : { network }),
|
|
120
|
+
...(fileSystem == null ? {} : { fileSystem }),
|
|
121
|
+
};
|
|
122
|
+
const asked = stableJson(granted);
|
|
123
|
+
const question: Question = {
|
|
124
|
+
kind: "permission",
|
|
125
|
+
title: "Extra permissions",
|
|
126
|
+
detail: lines(request.params.reason, asked.slice(0, DETAIL_CHARS)) ?? asked,
|
|
127
|
+
options: [...OPTIONS],
|
|
128
|
+
subject: { tool: "permissions", kind: "permissions", target: asked },
|
|
129
|
+
};
|
|
130
|
+
// Granted for the turn: the "session" scope is Codex's own "always".
|
|
131
|
+
return {
|
|
132
|
+
question,
|
|
133
|
+
reply: (answer) => ({ permissions: decisionOf(question, answer) === "accept" ? granted : {}, scope: "turn" }),
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
default:
|
|
137
|
+
return assertNever(request, "codex approval request");
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** One question of a `requestUserInput` call, as Codex names it and as niwa asks it. */
|
|
142
|
+
export interface UserQuestion {
|
|
143
|
+
readonly id: string;
|
|
144
|
+
readonly question: Question;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** The questions of a `requestUserInput` call. */
|
|
148
|
+
export function userQuestionsOf(params: ToolRequestUserInputParams): readonly UserQuestion[] {
|
|
149
|
+
return params.questions.map((asked) => {
|
|
150
|
+
const title = asked.question !== "" ? asked.question : asked.header !== "" ? asked.header : "A question";
|
|
151
|
+
const detail = asked.header !== "" && asked.header !== title ? { detail: asked.header } : {};
|
|
152
|
+
const offered = asked.options ?? [];
|
|
153
|
+
if (offered.length === 0) return { id: asked.id, question: { kind: "text", title, ...detail } };
|
|
154
|
+
const options: QuestionOption[] = offered.map((option, index) => ({
|
|
155
|
+
id: OptionIdSchema.parse(`option-${index + 1}`),
|
|
156
|
+
label: option.label,
|
|
157
|
+
...(option.description === "" ? {} : { description: option.description }),
|
|
158
|
+
flavor: "plain",
|
|
159
|
+
}));
|
|
160
|
+
return { id: asked.id, question: { kind: "choice", title, ...detail, options, allowOther: asked.isOther } };
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** What Codex takes as the answer to one question: the labels picked or the words given; none when it went unanswered. */
|
|
165
|
+
export function userAnswerOf(question: Question, answer: Answer): string[] | undefined {
|
|
166
|
+
if ("text" in answer) return [answer.text];
|
|
167
|
+
if ("ids" in answer) return pickedOptions(question, answer).map((option) => option.label);
|
|
168
|
+
return undefined;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** What Codex puts in the `_meta` of the elicitation that asks leave to call an MCP tool. */
|
|
172
|
+
const TOOL_APPROVAL_KIND = "mcp_tool_call";
|
|
173
|
+
|
|
174
|
+
/** A member of the `_meta` of an elicitation, when it is there. */
|
|
175
|
+
function metaOf(params: McpServerElicitationRequestParams, key: string): unknown {
|
|
176
|
+
const meta = params._meta;
|
|
177
|
+
return typeof meta === "object" && meta !== null && !Array.isArray(meta) ? meta[key] : undefined;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* The tool an approval asks leave for: Codex names it in `_meta.tool_name`
|
|
182
|
+
* (codex-rs/protocol/src/mcp_approval_meta.rs); the server is the request's `serverName`.
|
|
183
|
+
*/
|
|
184
|
+
function toolOfApproval(params: McpServerElicitationRequestParams): string | undefined {
|
|
185
|
+
const name = metaOf(params, "tool_name");
|
|
186
|
+
return typeof name === "string" && name !== "" ? name : undefined;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** A JSON value without its nulls: Codex writes an absent member of a schema as `null`, which no schema reads. */
|
|
190
|
+
function withoutNulls(value: unknown): unknown {
|
|
191
|
+
if (Array.isArray(value)) return value.map(withoutNulls);
|
|
192
|
+
if (typeof value !== "object" || value === null) return value;
|
|
193
|
+
return Object.fromEntries(
|
|
194
|
+
Object.entries(value)
|
|
195
|
+
.filter(([, member]) => member !== null)
|
|
196
|
+
.map(([key, member]) => [key, withoutNulls(member)]),
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** An elicitation of an MCP server: the question for the starter, and the reply Codex takes for each answer. */
|
|
201
|
+
export interface Elicitation {
|
|
202
|
+
readonly question: Question;
|
|
203
|
+
reply(answer: Answer): McpServerElicitationRequestResponse;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function elicitationReply(action: McpServerElicitationRequestResponse["action"], content: McpServerElicitationRequestResponse["content"]): McpServerElicitationRequestResponse {
|
|
207
|
+
return { action, content: content ?? null, _meta: null };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* The elicitation an MCP server's request is, or `undefined` for one niwa cannot ask a person (a link to open, a form of
|
|
212
|
+
* OpenAI's own kind): those stay declined.
|
|
213
|
+
*
|
|
214
|
+
* Leave to call a tool is a `permission` question under the tool's name as an MCP tool is named everywhere
|
|
215
|
+
* (`mcp__<server>__<tool>`), with the once-only options; "always" (Codex's `persist`) is never sent. A form is a `form`
|
|
216
|
+
* question, answered by its value; there is no declining a form, so a cancelled answer is Codex's `cancel`.
|
|
217
|
+
*/
|
|
218
|
+
export function elicitationOf(request: ElicitationRequest): Elicitation | undefined {
|
|
219
|
+
const { params } = request;
|
|
220
|
+
if (params.mode !== "form") return undefined;
|
|
221
|
+
if (metaOf(params, "codex_approval_kind") === TOOL_APPROVAL_KIND) {
|
|
222
|
+
const name = toolOfApproval(params);
|
|
223
|
+
if (name === undefined) return undefined;
|
|
224
|
+
const tool = `mcp__${params.serverName}__${name}`;
|
|
225
|
+
const given = metaOf(params, "tool_params");
|
|
226
|
+
const asked = given === undefined ? undefined : stableJson(given);
|
|
227
|
+
const question: Question = {
|
|
228
|
+
kind: "permission",
|
|
229
|
+
title: params.message,
|
|
230
|
+
...(asked === undefined ? {} : { detail: asked.slice(0, DETAIL_CHARS) }),
|
|
231
|
+
options: [...OPTIONS],
|
|
232
|
+
subject: { tool, kind: "mcp", target: asked === undefined ? tool : `${tool}\n${asked}` },
|
|
233
|
+
};
|
|
234
|
+
return { question, reply: (answer) => elicitationReply(decisionOf(question, answer), null) };
|
|
235
|
+
}
|
|
236
|
+
const schema = JsonSchemaValue.safeParse(withoutNulls(params.requestedSchema));
|
|
237
|
+
if (!schema.success) return undefined;
|
|
238
|
+
const question: Question = { kind: "form", title: params.message, schema: schema.data };
|
|
239
|
+
return {
|
|
240
|
+
question,
|
|
241
|
+
reply: (answer) => ("form" in answer ? elicitationReply("accept", answer.form) : elicitationReply("cancel", null)),
|
|
242
|
+
};
|
|
243
|
+
}
|
package/src/skills.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Where Codex reads skills from: the root shared by the agents that read `.agents/skills`. */
|
|
2
|
+
|
|
3
|
+
import { SHARED_AGENTS_SKILLS } from "@niwa-labs/harness";
|
|
4
|
+
import type { KindSkills } from "@niwa-labs/harness";
|
|
5
|
+
|
|
6
|
+
export const codexSkills: KindSkills = { kind: "codex", roots: SHARED_AGENTS_SKILLS };
|
package/src/tools.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/** Codex's own tools, as the common model describes them. */
|
|
2
|
+
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
|
|
5
|
+
import { shapeOf } from "@niwa-labs/harness";
|
|
6
|
+
import type { Checklist, ToolShape, ToolTable } from "@niwa-labs/harness";
|
|
7
|
+
|
|
8
|
+
/** The tools Codex gives its model. */
|
|
9
|
+
export const CODEX_TOOLS: ToolTable = {
|
|
10
|
+
shell: { category: "shell", target: ["command", "cmd"] },
|
|
11
|
+
local_shell: { category: "shell", target: ["command", "cmd"] },
|
|
12
|
+
"container.exec": { category: "shell", target: ["command", "cmd"] },
|
|
13
|
+
shell_command: { category: "shell", target: ["command", "cmd"] },
|
|
14
|
+
exec_command: { category: "shell", target: ["cmd", "command"] },
|
|
15
|
+
write_stdin: { category: "shell", target: ["session_id"] },
|
|
16
|
+
apply_patch: { category: "edit" },
|
|
17
|
+
view_image: { category: "read", target: ["path"] },
|
|
18
|
+
web_search: { category: "web", target: ["query"] },
|
|
19
|
+
update_plan: { category: "plan" },
|
|
20
|
+
spawn_agent: { category: "subagent", target: ["message", "prompt"] },
|
|
21
|
+
send_input: { category: "subagent", target: ["message"] },
|
|
22
|
+
wait: { category: "subagent" },
|
|
23
|
+
close_agent: { category: "subagent" },
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* An MCP tool, as Codex names it to the model: the server and the tool joined by `__`, with or
|
|
28
|
+
* without the `mcp__` prefix of newer builds.
|
|
29
|
+
*/
|
|
30
|
+
function isMcp(name: string): boolean {
|
|
31
|
+
return name.includes("__");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The files an `apply_patch` touches, in the order its patch names them; a file it moves is named by where it goes. */
|
|
35
|
+
export function patchedFiles(patch: string): string[] {
|
|
36
|
+
const files: string[] = [];
|
|
37
|
+
for (const match of patch.matchAll(/^\*\*\* (?:(?:Add|Update|Delete) File|Move to): (.+)$/gm)) {
|
|
38
|
+
const path = match[1]?.trim();
|
|
39
|
+
if (path !== undefined && path.length > 0 && !files.includes(path)) files.push(path);
|
|
40
|
+
}
|
|
41
|
+
return files;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** A call of a Codex tool, in the common model's words. A patch is about the first file it names, and lists all of them when it names several. */
|
|
45
|
+
export function codexToolShape(name: string, input: unknown): ToolShape {
|
|
46
|
+
const shape = shapeOf(CODEX_TOOLS, name, input, isMcp);
|
|
47
|
+
if (name !== "apply_patch") return shape;
|
|
48
|
+
const patch = typeof input === "string" ? input : typeof input === "object" && input !== null ? Object.getOwnPropertyDescriptor(input, "input")?.value : undefined;
|
|
49
|
+
const files = typeof patch === "string" ? patchedFiles(patch) : [];
|
|
50
|
+
const first = files[0];
|
|
51
|
+
if (first === undefined) return shape;
|
|
52
|
+
return files.length > 1 ? { ...shape, target: first, paths: files } : { ...shape, target: first };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The arguments of Codex's `update_plan`: the whole plan, each step with its state. */
|
|
56
|
+
const UpdatePlanArgs = z.object({
|
|
57
|
+
explanation: z.string().nullish(),
|
|
58
|
+
plan: z.array(z.object({ step: z.string(), status: z.enum(["pending", "in_progress", "completed"]) })),
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const PLAN_STATES = { pending: "pending", in_progress: "active", completed: "done" } as const;
|
|
62
|
+
|
|
63
|
+
/** The checklist a call leaves, when it is an `update_plan` whose arguments Codex would accept. */
|
|
64
|
+
export function codexChecklist(name: string, input: unknown): Checklist | undefined {
|
|
65
|
+
if (name !== "update_plan") return undefined;
|
|
66
|
+
const parsed = UpdatePlanArgs.safeParse(input);
|
|
67
|
+
if (!parsed.success) return undefined;
|
|
68
|
+
return { items: parsed.data.plan.map((step) => ({ text: step.step, state: PLAN_STATES[step.status] })) };
|
|
69
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// The Codex reading side passes the conformance suite on its own fixtures.
|
|
2
|
+
|
|
3
|
+
import { expect, test } from "bun:test";
|
|
4
|
+
|
|
5
|
+
import { readingConformance } from "@niwa-labs/harness";
|
|
6
|
+
import type { Harness } from "@niwa-labs/harness";
|
|
7
|
+
|
|
8
|
+
import { FIXTURE_HARNESS, writeCodexFixtures } from "../src/fixtures.ts";
|
|
9
|
+
import { CodexReading } from "../src/reading.ts";
|
|
10
|
+
|
|
11
|
+
test("H1 H3 H8 H10 codex passes the reading conformance suite", async () => {
|
|
12
|
+
const report = await readingConformance(
|
|
13
|
+
"codex",
|
|
14
|
+
(home) => {
|
|
15
|
+
const instance: Harness = {
|
|
16
|
+
id: FIXTURE_HARNESS,
|
|
17
|
+
kind: "codex" as never,
|
|
18
|
+
machine: "machine-1" as never,
|
|
19
|
+
home,
|
|
20
|
+
sides: { reading: { list: true, read: true, watch: false } },
|
|
21
|
+
};
|
|
22
|
+
return { instance, reading: new CodexReading({ harness: FIXTURE_HARNESS, home }) };
|
|
23
|
+
},
|
|
24
|
+
writeCodexFixtures,
|
|
25
|
+
)();
|
|
26
|
+
const failed = report.results.filter((result) => !result.passed);
|
|
27
|
+
expect(failed.map((result) => `${result.name}: ${result.detail ?? ""}`)).toEqual([]);
|
|
28
|
+
expect(report.results.length).toBeGreaterThan(40);
|
|
29
|
+
});
|