@operad-io/harness-codex 0.0.0-stage → 0.0.1
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 +106 -2
- package/package.json +23 -4
- package/src/access.ts +192 -0
- package/src/index.ts +634 -0
- package/src/install.ts +125 -0
- package/src/log.ts +299 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Yinghai Zhao
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,107 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @operad-io/harness-codex
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The `codex` plugin: one `agent` step that starts the user's `codex-acp` and
|
|
4
|
+
talks ACP to it, and the `codex:prompt` flow that runs one prompt. Decisions:
|
|
5
|
+
`docs/backlog/operad-harness.md` 2, 6, 8, 9 and 10.
|
|
6
|
+
|
|
7
|
+
The ACP client is `AcpClient` in `@operad-io/harness-process` (`src/acp.ts`), so
|
|
8
|
+
another harness that speaks ACP reuses it. `runAgent` owns the process, the
|
|
9
|
+
wall time and the stall limit; `AcpClient` is only the protocol.
|
|
10
|
+
|
|
11
|
+
## The conversation
|
|
12
|
+
|
|
13
|
+
`initialize`, then `session/new` with `cwd` set to the task's worktree (or
|
|
14
|
+
`session/load` for `resumeSession`; an agent without `loadSession` fails the
|
|
15
|
+
step with `session_missing`), then `session/set_config_option` for the session
|
|
16
|
+
mode, the model and the reasoning effort, then `session/prompt`. Each
|
|
17
|
+
`session/update` is turned into log lines. `session/cancel` is sent when the
|
|
18
|
+
step is cancelled; codex-acp answers the prompt with the stop reason
|
|
19
|
+
`cancelled`, and an agent that does not answer within `CANCEL_GRACE_MS` (10 s)
|
|
20
|
+
is ended. What `session/load` replays of the earlier conversation is neither
|
|
21
|
+
logged nor counted.
|
|
22
|
+
|
|
23
|
+
## Credentials
|
|
24
|
+
|
|
25
|
+
None from operad. codex-acp uses the user's local Codex (ChatGPT) login as it
|
|
26
|
+
is. A session that needs a login fails with `not_logged_in`; run `codex login`.
|
|
27
|
+
|
|
28
|
+
## Settings
|
|
29
|
+
|
|
30
|
+
The `codex` section of `config.json`.
|
|
31
|
+
|
|
32
|
+
| setting | type | default | meaning |
|
|
33
|
+
|---|---|---|---|
|
|
34
|
+
| `model` | string | Codex's own | the model; the step's `--model` wins |
|
|
35
|
+
| `effort` | `minimal`, `low`, `medium`, `high`, `xhigh` | Codex's own | the reasoning effort; the step's `--effort` wins |
|
|
36
|
+
| `command` | array of strings | the `bun` of PATH on the real path of the `codex-acp` of PATH | the program and arguments that start codex-acp |
|
|
37
|
+
| `wallTimeSecs` | number | `3600` | the longest one run may take (at most 28500) |
|
|
38
|
+
| `stallSecs` | number | `600` | how long a run may be silent while no tool call is open |
|
|
39
|
+
| `concurrency` | object, model to positive integer | `{}` | how many runs of one model may go at once; a model not listed has no limit |
|
|
40
|
+
|
|
41
|
+
`command` is a list because the default is two words. codex-acp's shebang is
|
|
42
|
+
`#!/usr/bin/env node`, which finds a node only in a shell that set one up (fnm),
|
|
43
|
+
so the default runs the real path of `codex-acp` with `bun`. On the owner's
|
|
44
|
+
machine that is `["/opt/homebrew/bin/bun", "/Users/hai/.bun/bin/codex-acp"]`.
|
|
45
|
+
|
|
46
|
+
`operad plugin check codex` runs `codex-acp --version`, `codex --version` and
|
|
47
|
+
`codex login status`, and reports the two versions and the login kind. It
|
|
48
|
+
refuses, saying what to do, when codex-acp cannot start or Codex is not logged
|
|
49
|
+
in; a Codex it cannot find on PATH is reported as unknown, not refused.
|
|
50
|
+
|
|
51
|
+
## Step args
|
|
52
|
+
|
|
53
|
+
`--model <name>` and `--effort <level>` only; any other entry fails the step
|
|
54
|
+
with `invalid_args`, as for `pi` and `claude`. A model codex-acp does not offer
|
|
55
|
+
is a `model_error` of kind `unavailable`; an effort the model does not take is
|
|
56
|
+
`invalid_args`.
|
|
57
|
+
|
|
58
|
+
## The sandbox
|
|
59
|
+
|
|
60
|
+
Codex's own sandbox stays on. The session mode is `workspace-write` (`SESSION_MODE`):
|
|
61
|
+
write access to the worktree and nothing outside it. The sandbox cannot write
|
|
62
|
+
`.git`, so a flow that needs a commit uses the work stage's `commit`
|
|
63
|
+
(`operad-dev-flows.md` decision 9). The sandbox has no network; a command that
|
|
64
|
+
needs it comes to the client as a permission request, and the access profile
|
|
65
|
+
answers it (a network request is allowed once, since the task needs it).
|
|
66
|
+
|
|
67
|
+
## Permissions
|
|
68
|
+
|
|
69
|
+
`session/request_permission` is answered by `decideAccess` (`src/access.ts`),
|
|
70
|
+
the same rules the other harnesses apply through `access-match`: a deny wins; a
|
|
71
|
+
one-time permission covers one exact refused call and is used through the host
|
|
72
|
+
(`ctx.useGrant`, the same `grant_use` and `grant_ack` the worker protocol
|
|
73
|
+
carries) before the call is let through; a call the profile says must ask goes
|
|
74
|
+
to a person through `ctx.ask`. An allowed call gets the `allow_once` option,
|
|
75
|
+
never `allow_always`. The request is read by its ACP tool `kind`: `execute`
|
|
76
|
+
with a command is a `bash` call; `edit`, `delete` and `move` judge each path in
|
|
77
|
+
`locations` as an `edit`; `read` and `search` judge their path; `fetch`, `think`
|
|
78
|
+
and a request of operad's own MCP server are inert; any other call is allowed
|
|
79
|
+
only when the profile has no `denyPaths` and `denyCommands`.
|
|
80
|
+
|
|
81
|
+
Codex asks only about what leaves its sandbox. A command that runs inside the
|
|
82
|
+
sandbox is not asked about, so a profile's deny rules do not see it: Codex's
|
|
83
|
+
sandbox is the bound there, not the profile.
|
|
84
|
+
|
|
85
|
+
## Usage, images, time
|
|
86
|
+
|
|
87
|
+
- Tokens come from the prompt's `usage`. The cost is unknown for a subscription
|
|
88
|
+
login: `costMicros` and `costQuality` are `null`, never `0`, and nothing in
|
|
89
|
+
`dev` or `pool` stops or blocks on it.
|
|
90
|
+
- Each image generation (a tool call titled `Image generation`) is counted per
|
|
91
|
+
attempt in `imageGenCalls` of the step result and of the closing `result`
|
|
92
|
+
line of the log.
|
|
93
|
+
- Every tool call is in the step log as `tool_execution_start`, `tool_output`
|
|
94
|
+
(the head, cut at 64 KB) and `tool_execution_end`; an assistant message is
|
|
95
|
+
`text_delta` lines and a closing `message_end`; a refused call is a `blocked`
|
|
96
|
+
line. A rework reads these.
|
|
97
|
+
- There is no stall detection of its own. The stall limit pauses while a tool
|
|
98
|
+
call or a permission question is open, so an image generation that prints
|
|
99
|
+
nothing for minutes is never a stall; the wall limit still applies.
|
|
100
|
+
- `usageLimitExceeded` and `rateLimitExceeded` of Codex are a `model_error` of
|
|
101
|
+
kind `usage_limit`; `serverOverloaded` is `unavailable`. With no fallback
|
|
102
|
+
configured the task waits (`operad-worker-pool.md` decision 10).
|
|
103
|
+
|
|
104
|
+
## Not done
|
|
105
|
+
|
|
106
|
+
Messages sent to a run while it works (the inbox) are not passed on. The chat
|
|
107
|
+
does not offer `codex`: it declares no models and no efforts.
|
package/package.json
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@operad-io/harness-codex",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": "./src/index.ts"
|
|
7
|
+
},
|
|
8
|
+
"dependencies": {
|
|
9
|
+
"@operad-io/access-match": "0.0.1",
|
|
10
|
+
"@operad-io/engine": "0.0.1",
|
|
11
|
+
"@operad-io/harness-process": "0.0.1"
|
|
12
|
+
},
|
|
13
|
+
"description": "operad: the codex harness, a client of the codex-acp adapter (in development)",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/operad-io/operad.git",
|
|
17
|
+
"directory": "packages/harness/codex"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"files": [
|
|
21
|
+
"src",
|
|
22
|
+
"README.md",
|
|
23
|
+
"LICENSE"
|
|
24
|
+
]
|
|
25
|
+
}
|
package/src/access.ts
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The access profile applied to the calls codex asks permission for
|
|
3
|
+
* (docs/backlog/operad-harness.md decision 6).
|
|
4
|
+
*
|
|
5
|
+
* codex-acp sends `session/request_permission` for a command that leaves the
|
|
6
|
+
* sandbox, a file change and a network request. The request describes the call
|
|
7
|
+
* by an ACP tool `kind`, `rawInput` and `locations`; `@operad-io/access-match`
|
|
8
|
+
* judges calls by lower-case tool names and the input fields `command` and
|
|
9
|
+
* `path`. `translate` is the one place that maps the two.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { findGrant, match, type AccessProfile } from "@operad-io/access-match";
|
|
13
|
+
import { OPERAD_SERVER, type CallAnswer, type CallQuestion } from "@operad-io/harness-process";
|
|
14
|
+
import { isRecord } from "@operad-io/engine";
|
|
15
|
+
|
|
16
|
+
/** One call in the words `access-match` takes. */
|
|
17
|
+
export interface JudgedCall {
|
|
18
|
+
readonly tool: "bash" | "read" | "edit" | "grep";
|
|
19
|
+
readonly input: Record<string, unknown>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type Translated =
|
|
23
|
+
| { readonly kind: "calls"; readonly calls: readonly JudgedCall[] }
|
|
24
|
+
| { readonly kind: "missing"; readonly field: string }
|
|
25
|
+
| { readonly kind: "inert" }
|
|
26
|
+
| { readonly kind: "unmapped" };
|
|
27
|
+
|
|
28
|
+
function locations(toolCall: Readonly<Record<string, unknown>>): readonly string[] {
|
|
29
|
+
const list = toolCall["locations"];
|
|
30
|
+
if (!Array.isArray(list)) return [];
|
|
31
|
+
const paths: string[] = [];
|
|
32
|
+
for (const item of list) {
|
|
33
|
+
if (isRecord(item) && typeof item["path"] === "string" && item["path"].length > 0) paths.push(item["path"]);
|
|
34
|
+
}
|
|
35
|
+
return paths;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The command of an execute call: a string, or the words of an argv. */
|
|
39
|
+
export function commandOf(rawInput: unknown): string | undefined {
|
|
40
|
+
if (!isRecord(rawInput)) return undefined;
|
|
41
|
+
const command = rawInput["command"];
|
|
42
|
+
if (typeof command === "string" && command.trim().length > 0) return command;
|
|
43
|
+
if (Array.isArray(command) && command.length > 0 && command.every((word) => typeof word === "string")) {
|
|
44
|
+
return command.join(" ");
|
|
45
|
+
}
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The calls the profile must judge for one permission request:
|
|
51
|
+
*
|
|
52
|
+
* - `execute` with a command is a `bash` call, even when it needs the network;
|
|
53
|
+
* with a `url` and no command it is a network request, which no rule names,
|
|
54
|
+
* so it is inert (the task needs the network and Codex's own sandbox asks
|
|
55
|
+
* before it leaves it);
|
|
56
|
+
* - `edit`, `delete` and `move` judge each path they touch as an `edit`;
|
|
57
|
+
* - `read` and `search` judge the path they name, if any;
|
|
58
|
+
* - `fetch` and `think`, and a request of operad's own MCP server, are inert.
|
|
59
|
+
*
|
|
60
|
+
* Anything else (an MCP tool of another server, a mode switch, extra sandbox
|
|
61
|
+
* permissions) is unmapped: it is allowed only when the profile has no
|
|
62
|
+
* `denyPaths` and no `denyCommands`, since a rule that could match it cannot
|
|
63
|
+
* be checked.
|
|
64
|
+
*/
|
|
65
|
+
export function translate(toolCall: Readonly<Record<string, unknown>>): Translated {
|
|
66
|
+
const kind = toolCall["kind"];
|
|
67
|
+
const rawInput = toolCall["rawInput"];
|
|
68
|
+
if (isRecord(rawInput) && rawInput["serverName"] === OPERAD_SERVER) return { kind: "inert" };
|
|
69
|
+
switch (kind) {
|
|
70
|
+
case "execute": {
|
|
71
|
+
if (isRecord(rawInput) && rawInput["serverName"] !== undefined) return { kind: "unmapped" };
|
|
72
|
+
const command = commandOf(rawInput);
|
|
73
|
+
// A command that needs the network is judged as the command it is.
|
|
74
|
+
if (command !== undefined) return { kind: "calls", calls: [{ tool: "bash", input: { command } }] };
|
|
75
|
+
if (isRecord(rawInput) && typeof rawInput["url"] === "string") return { kind: "inert" };
|
|
76
|
+
return { kind: "missing", field: "command" };
|
|
77
|
+
}
|
|
78
|
+
case "edit":
|
|
79
|
+
case "delete":
|
|
80
|
+
case "move": {
|
|
81
|
+
const paths = locations(toolCall);
|
|
82
|
+
return paths.length === 0
|
|
83
|
+
? { kind: "missing", field: "path" }
|
|
84
|
+
: { kind: "calls", calls: paths.map((path) => ({ tool: "edit" as const, input: { path } })) };
|
|
85
|
+
}
|
|
86
|
+
case "read":
|
|
87
|
+
case "search": {
|
|
88
|
+
const paths = locations(toolCall);
|
|
89
|
+
const tool = kind === "read" ? ("read" as const) : ("grep" as const);
|
|
90
|
+
return paths.length === 0
|
|
91
|
+
? kind === "read"
|
|
92
|
+
? { kind: "missing", field: "path" }
|
|
93
|
+
: { kind: "calls", calls: [{ tool, input: {} }] }
|
|
94
|
+
: { kind: "calls", calls: paths.map((path) => ({ tool, input: { path } })) };
|
|
95
|
+
}
|
|
96
|
+
case "fetch":
|
|
97
|
+
case "think":
|
|
98
|
+
return { kind: "inert" };
|
|
99
|
+
default:
|
|
100
|
+
return { kind: "unmapped" };
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Whether the profile has a rule that an unchecked call could break. */
|
|
105
|
+
export function hasRules(profile: AccessProfile): boolean {
|
|
106
|
+
return (profile.denyPaths ?? []).length > 0 || (profile.denyCommands ?? []).length > 0;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export type AccessDecision =
|
|
110
|
+
| { readonly allow: true }
|
|
111
|
+
| {
|
|
112
|
+
readonly allow: false;
|
|
113
|
+
readonly reason: string;
|
|
114
|
+
/** The wait for a person ended; the run is about to be ended and holds the call. */
|
|
115
|
+
readonly stop?: true;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/** What the step gives the decision: the host's one-time permission use and a question to a person. */
|
|
119
|
+
export interface Deciders {
|
|
120
|
+
/** Asks the host to use up a one-time permission; without it no grant applies. */
|
|
121
|
+
readonly useGrant?: ((waitId: string) => Promise<boolean>) | undefined;
|
|
122
|
+
/** Asks a person about a call the profile says must ask; without it such a call is refused. */
|
|
123
|
+
readonly ask?: ((question: CallQuestion & { readonly shown: string }) => Promise<CallAnswer>) | undefined;
|
|
124
|
+
/** What a person reads for one call. */
|
|
125
|
+
readonly shown: (call: JudgedCall) => string;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Whether the call a permission request describes may run, by the same rules
|
|
130
|
+
* the other harnesses apply: a deny wins; a one-time permission covers one
|
|
131
|
+
* exact refused call and is used up through the host before it lets the call
|
|
132
|
+
* through; a call that must ask goes to a person.
|
|
133
|
+
*/
|
|
134
|
+
export async function decideAccess(
|
|
135
|
+
profile: AccessProfile,
|
|
136
|
+
toolCall: Readonly<Record<string, unknown>>,
|
|
137
|
+
cwd: string,
|
|
138
|
+
deciders: Deciders,
|
|
139
|
+
): Promise<AccessDecision> {
|
|
140
|
+
const translated = translate(toolCall);
|
|
141
|
+
if (translated.kind === "inert") return { allow: true };
|
|
142
|
+
if (translated.kind === "unmapped") {
|
|
143
|
+
if (!hasRules(profile)) return { allow: true };
|
|
144
|
+
return {
|
|
145
|
+
allow: false,
|
|
146
|
+
reason: "this call is not one the access profile can check; it is refused while denyPaths or denyCommands is set",
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
if (translated.kind === "missing") {
|
|
150
|
+
if (!hasRules(profile)) return { allow: true };
|
|
151
|
+
return {
|
|
152
|
+
allow: false,
|
|
153
|
+
reason: `this call has no ${translated.field}; it is refused while denyPaths or denyCommands is set`,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
for (const judged of translated.calls) {
|
|
157
|
+
const call = { tool: judged.tool, input: judged.input, cwd };
|
|
158
|
+
const verdict = match(profile, call);
|
|
159
|
+
if (verdict.allow) continue;
|
|
160
|
+
// A one-time permission covers one exact call that would be refused. The
|
|
161
|
+
// host writes the fact before it answers, so a crash after it cannot give
|
|
162
|
+
// the permission twice.
|
|
163
|
+
const grant = findGrant(profile, call);
|
|
164
|
+
if (grant !== undefined && deciders.useGrant !== undefined) {
|
|
165
|
+
let used = false;
|
|
166
|
+
try {
|
|
167
|
+
used = await deciders.useGrant(grant.waitId);
|
|
168
|
+
} catch {
|
|
169
|
+
used = false;
|
|
170
|
+
}
|
|
171
|
+
if (used) continue;
|
|
172
|
+
}
|
|
173
|
+
if (verdict.ask === true && deciders.ask !== undefined) {
|
|
174
|
+
let answer: CallAnswer;
|
|
175
|
+
try {
|
|
176
|
+
answer = await deciders.ask({
|
|
177
|
+
kind: verdict.kind,
|
|
178
|
+
value: verdict.value,
|
|
179
|
+
...(verdict.tool === undefined ? {} : { tool: verdict.tool }),
|
|
180
|
+
shown: deciders.shown(judged),
|
|
181
|
+
});
|
|
182
|
+
} catch {
|
|
183
|
+
answer = { decision: "deny", reason: "the question could not be asked" };
|
|
184
|
+
}
|
|
185
|
+
if (answer.decision === "allow") continue;
|
|
186
|
+
if (answer.decision === "stop") return { allow: false, reason: "waiting for an answer", stop: true };
|
|
187
|
+
return { allow: false, reason: `${verdict.reason}; ${answer.reason ?? "a person refused it"}` };
|
|
188
|
+
}
|
|
189
|
+
return { allow: false, reason: verdict.reason };
|
|
190
|
+
}
|
|
191
|
+
return { allow: true };
|
|
192
|
+
}
|