@operad-io/harness-codex 0.1.1 → 0.3.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/README.md +9 -1
- package/package.json +5 -5
- package/src/access.ts +59 -71
- package/src/app-server.ts +585 -0
- package/src/index.ts +387 -282
- package/src/install.ts +353 -32
- package/src/log.ts +274 -139
- package/src/models-probe.ts +132 -0
package/README.md
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
# @operad-io/harness-codex
|
|
2
2
|
|
|
3
|
-
**The codex harness.** Starts your own `codex-
|
|
3
|
+
**The codex harness.** Starts your own `codex app-server` and speaks Codex's JSON-RPC to it, to run a prompt as one agent step.
|
|
4
4
|
|
|
5
5
|
This package is part of [operad](https://github.com/operad-io/operad). Most people install only [`@operad-io/cli`](https://www.npmjs.com/package/@operad-io/cli).
|
|
6
6
|
|
|
7
|
+
## Signed in
|
|
8
|
+
|
|
9
|
+
The plugin's `signedIn` answer, which `GET /v1/plugins` carries for clients, comes from `codex login status`: `true` on a zero exit, `false` on a non-zero one. The answer is kept for a short time and the probe runs without blocking `serve`. It is absent when `codex` is not found or cannot be asked. Only the yes/no is exposed, never an account or a key.
|
|
10
|
+
|
|
11
|
+
## Models
|
|
12
|
+
|
|
13
|
+
The plugin's `model` choices come from the app-server's own `model/list`, read in the background and kept for a short time so a chat can offer the models Codex offers without blocking. The list is read once when the plugins load, so the first client that asks after `serve` starts already has it. A step's `--model` still wins.
|
|
14
|
+
|
|
7
15
|
## Documentation
|
|
8
16
|
|
|
9
17
|
- User guide: [docs/user/dev.md](https://github.com/operad-io/operad/blob/main/docs/user/dev.md)
|
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@operad-io/harness-codex",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./src/index.ts"
|
|
7
7
|
},
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@operad-io/access-match": "0.
|
|
10
|
-
"@operad-io/engine": "0.
|
|
11
|
-
"@operad-io/harness-process": "0.
|
|
9
|
+
"@operad-io/access-match": "0.3.0",
|
|
10
|
+
"@operad-io/engine": "0.3.0",
|
|
11
|
+
"@operad-io/harness-process": "0.3.0"
|
|
12
12
|
},
|
|
13
|
-
"description": "operad: the codex harness, a client of
|
|
13
|
+
"description": "operad: the codex harness, a client of Codex's own app-server",
|
|
14
14
|
"repository": {
|
|
15
15
|
"type": "git",
|
|
16
16
|
"url": "git+https://github.com/operad-io/operad.git",
|
package/src/access.ts
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The access profile applied to the calls
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* by an ACP tool `kind`, `rawInput` and `locations`; `@operad-io/access-match`
|
|
2
|
+
* The access profile applied to the calls Codex asks operad about (ADR 0022
|
|
3
|
+
* D4). `codex app-server` sends `item/commandExecution/requestApproval` for a
|
|
4
|
+
* command that leaves its sandbox and `item/fileChange/requestApproval` for a
|
|
5
|
+
* file change it may not make; the older `execCommandApproval` and
|
|
6
|
+
* `applyPatchApproval` names carry the same facts. `@operad-io/access-match`
|
|
8
7
|
* judges calls by lower-case tool names and the input fields `command` and
|
|
9
|
-
* `path
|
|
8
|
+
* `path`; `translateApproval` is the one place that maps the two.
|
|
10
9
|
*/
|
|
11
10
|
|
|
12
11
|
import { findGrant, match, type AccessProfile } from "@operad-io/access-match";
|
|
13
|
-
import {
|
|
12
|
+
import type { CallAnswer, CallQuestion } from "@operad-io/harness-process";
|
|
14
13
|
import { isRecord } from "@operad-io/engine";
|
|
15
14
|
|
|
16
15
|
/** One call in the words `access-match` takes. */
|
|
@@ -25,80 +24,66 @@ export type Translated =
|
|
|
25
24
|
| { readonly kind: "inert" }
|
|
26
25
|
| { readonly kind: "unmapped" };
|
|
27
26
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if (
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
if (isRecord(item) && typeof item["path"] === "string" && item["path"].length > 0) paths.push(item["path"]);
|
|
27
|
+
/** The command of a request: a string, or the words of an argv. */
|
|
28
|
+
export function commandOf(value: unknown): string | undefined {
|
|
29
|
+
if (typeof value === "string" && value.trim().length > 0) return value;
|
|
30
|
+
if (Array.isArray(value) && value.length > 0 && value.every((word) => typeof word === "string")) {
|
|
31
|
+
return value.join(" ");
|
|
34
32
|
}
|
|
35
|
-
return
|
|
33
|
+
return undefined;
|
|
36
34
|
}
|
|
37
35
|
|
|
38
|
-
/** The
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
36
|
+
/** The paths of a file change item or of the legacy `fileChanges` map. */
|
|
37
|
+
function pathsOf(params: Readonly<Record<string, unknown>>, item: Readonly<Record<string, unknown>> | undefined): readonly string[] {
|
|
38
|
+
const paths: string[] = [];
|
|
39
|
+
if (Array.isArray(item?.["changes"])) {
|
|
40
|
+
for (const change of item["changes"]) {
|
|
41
|
+
if (isRecord(change) && typeof change["path"] === "string" && change["path"].length > 0) paths.push(change["path"]);
|
|
42
|
+
}
|
|
45
43
|
}
|
|
46
|
-
|
|
44
|
+
if (isRecord(params["fileChanges"])) {
|
|
45
|
+
for (const path of Object.keys(params["fileChanges"])) {
|
|
46
|
+
if (path.length > 0 && !paths.includes(path)) paths.push(path);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const root = params["grantRoot"];
|
|
50
|
+
if (typeof root === "string" && root.length > 0 && !paths.includes(root)) paths.push(root);
|
|
51
|
+
return paths;
|
|
47
52
|
}
|
|
48
53
|
|
|
49
54
|
/**
|
|
50
|
-
* The calls the profile must judge for one
|
|
55
|
+
* The calls the profile must judge for one approval request:
|
|
51
56
|
*
|
|
52
|
-
* -
|
|
53
|
-
* with a
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
* -
|
|
57
|
-
*
|
|
58
|
-
* - `fetch` and `think`, and a request of operad's own MCP server, are inert.
|
|
57
|
+
* - a command approval is a `bash` call, even when it needs the network; a
|
|
58
|
+
* request with a network context and no command is inert (the task needs the
|
|
59
|
+
* network and Codex asks before it leaves its sandbox);
|
|
60
|
+
* - a file-change approval judges each path it touches as an `edit`;
|
|
61
|
+
* - a terminal-input approval (`writeStdin`) is not a command this profile can
|
|
62
|
+
* see, so it is unmapped.
|
|
59
63
|
*
|
|
60
|
-
* Anything else
|
|
61
|
-
* permissions) is unmapped: it is allowed only when the profile has no
|
|
64
|
+
* Anything else is unmapped: it is allowed only when the profile has no
|
|
62
65
|
* `denyPaths` and no `denyCommands`, since a rule that could match it cannot
|
|
63
66
|
* be checked.
|
|
64
67
|
*/
|
|
65
|
-
export function
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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" };
|
|
68
|
+
export function translateApproval(
|
|
69
|
+
method: string,
|
|
70
|
+
params: Readonly<Record<string, unknown>>,
|
|
71
|
+
item: Readonly<Record<string, unknown>> | undefined,
|
|
72
|
+
): Translated {
|
|
73
|
+
if (method === "item/commandExecution/requestApproval" || method === "execCommandApproval") {
|
|
74
|
+
if (params["kind"] === "writeStdin") return { kind: "unmapped" };
|
|
75
|
+
const command = commandOf(params["command"]);
|
|
76
|
+
if (command !== undefined) return { kind: "calls", calls: [{ tool: "bash", input: { command } }] };
|
|
77
|
+
if (isRecord(params["networkApprovalContext"])) return { kind: "inert" };
|
|
78
|
+
return { kind: "missing", field: "command" };
|
|
79
|
+
}
|
|
80
|
+
if (method === "item/fileChange/requestApproval" || method === "applyPatchApproval") {
|
|
81
|
+
const paths = pathsOf(params, item);
|
|
82
|
+
return paths.length === 0
|
|
83
|
+
? { kind: "missing", field: "path" }
|
|
84
|
+
: { kind: "calls", calls: paths.map((path) => ({ tool: "edit" as const, input: { path } })) };
|
|
101
85
|
}
|
|
86
|
+
return { kind: "unmapped" };
|
|
102
87
|
}
|
|
103
88
|
|
|
104
89
|
/** Whether the profile has a rule that an unchecked call could break. */
|
|
@@ -133,11 +118,13 @@ export interface Deciders {
|
|
|
133
118
|
*/
|
|
134
119
|
export async function decideAccess(
|
|
135
120
|
profile: AccessProfile,
|
|
136
|
-
|
|
121
|
+
method: string,
|
|
122
|
+
params: Readonly<Record<string, unknown>>,
|
|
123
|
+
item: Readonly<Record<string, unknown>> | undefined,
|
|
137
124
|
cwd: string,
|
|
138
125
|
deciders: Deciders,
|
|
139
126
|
): Promise<AccessDecision> {
|
|
140
|
-
const translated =
|
|
127
|
+
const translated = translateApproval(method, params, item);
|
|
141
128
|
if (translated.kind === "inert") return { allow: true };
|
|
142
129
|
if (translated.kind === "unmapped") {
|
|
143
130
|
if (!hasRules(profile)) return { allow: true };
|
|
@@ -177,6 +164,7 @@ export async function decideAccess(
|
|
|
177
164
|
kind: verdict.kind,
|
|
178
165
|
value: verdict.value,
|
|
179
166
|
...(verdict.tool === undefined ? {} : { tool: verdict.tool }),
|
|
167
|
+
...(verdict.outward === undefined ? {} : { outward: verdict.outward }),
|
|
180
168
|
shown: deciders.shown(judged),
|
|
181
169
|
});
|
|
182
170
|
} catch {
|