@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 CHANGED
@@ -1,9 +1,17 @@
1
1
  # @operad-io/harness-codex
2
2
 
3
- **The codex harness.** Starts your own `codex-acp` and talks ACP to it, to run a prompt as one agent step.
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.1.1",
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.1.1",
10
- "@operad-io/engine": "0.1.1",
11
- "@operad-io/harness-process": "0.1.1"
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 the codex-acp adapter",
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 codex asks permission for
3
- * (ADR 0022 D4).
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`
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`. `translate` is the one place that maps the two.
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 { OPERAD_SERVER, type CallAnswer, type CallQuestion } from "@operad-io/harness-process";
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
- 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"]);
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 paths;
33
+ return undefined;
36
34
  }
37
35
 
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(" ");
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
- return undefined;
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 permission request:
55
+ * The calls the profile must judge for one approval request:
51
56
  *
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.
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 (an MCP tool of another server, a mode switch, extra sandbox
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 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" };
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
- toolCall: Readonly<Record<string, unknown>>,
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 = translate(toolCall);
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 {