@operad-io/harness-claude 0.0.1 → 0.1.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/README.md CHANGED
@@ -1,287 +1,13 @@
1
1
  # @operad-io/harness-claude
2
2
 
3
- The claude harness. `src/worker.ts` is the child program: it runs one prompt
4
- through the Agent SDK, which starts your own `claude` command. It reads one
5
- JSON object from stdin and writes one JSON object per line to stdout. The last
6
- line is the `Result`. Decisions: `docs/backlog/operad-harness-claude.md` 2 to 8.
3
+ **The claude harness.** Runs a prompt through the Claude Agent SDK, which starts your own `claude` command. operad bundles no claude.
7
4
 
8
- Run it as `bun packages/harness/claude/src/worker.ts`. stderr is free text for
9
- humans and is never parsed.
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).
10
6
 
11
- operad bundles no claude. The root `package.json` replaces the eight
12
- platform packages of the SDK with empty ones
13
- (`docs/backlog/operad-harness-claude.md` decision 2), and the worker finds
14
- `claude` on `PATH` (or at the `command` it is given) and resolves its real path.
15
- The login is the one `claude` already has; the worker passes no credential. A
16
- `CLAUDE_CODE_OAUTH_TOKEN` in the environment (from `claude setup-token`) reaches
17
- `claude` unchanged, and nothing is logged from the environment.
7
+ ## Documentation
18
8
 
19
- `claude` runs with `settingSources: []`, `permissionMode: 'default'`,
20
- `permissionPrompts: 'host'`, the `claude_code` system prompt preset with nothing
21
- appended and the SDK's default tools. `canUseTool` is the only permission
22
- surface. These names are set in its environment: `CLAUDE_CODE_DISABLE_AUTO_MEMORY`,
23
- `CLAUDE_CODE_DISABLE_CLAUDE_MDS`, `DISABLE_TELEMETRY`, `DISABLE_ERROR_REPORTING`
24
- (all `1`), and `CLAUDE_CODE_DISABLE_BACKGROUND_TASKS=1`, so no command moves to the
25
- background. A run is one foreground turn: the tools that only schedule or wait for
26
- work outside the turn (`Monitor`, `ScheduleWakeup`, `CronCreate`, `CronDelete`,
27
- `CronList`, `TaskStop`) are in `disallowedTools`, and `BASH_DEFAULT_TIMEOUT_MS` and
28
- `BASH_MAX_TIMEOUT_MS` are set from `bashTimeoutSecs` so a gate (a full test run)
29
- finishes in the foreground instead of after claude's default 120 s. claude's Bash tool sources a snapshot of the user's `~/.zshrc` or `~/.bashrc`, so the user's aliases apply; claude has no supported switch to turn that off, and operad does not work around it (see "The agent's shell and the user's aliases" in the manual). If claude
30
- starts a second session after its result (a second `system/init`), the worker
31
- stops, logs it and ends `failed` with reason `model_error` and a message that says
32
- so; the first result's usage and session id are kept. Auto compaction and tool search are left on: a long run needs
33
- compaction.
34
-
35
- ## stdin
36
-
37
- One JSON object, then EOF (the step may keep stdin open for `grant_ack` lines).
38
- `test/heavy/protocol.test.ts` checks the program against this table. A field not in
39
- the table is refused, not ignored.
40
-
41
- | field | type | required | meaning |
42
- |---|---|---|---|
43
- | `prompt` | string, non-empty | yes | the text sent to the model |
44
- | `cwd` | string, non-empty | yes | working directory of the agent |
45
- | `command` | string, non-empty | no | the claude command, a name on PATH or a path; absent means `claude` |
46
- | `resumeSession` | string, a UUID | no | a `sessionId` from an earlier `Result`; the prompt is sent into that session |
47
- | `model` | string, non-empty | no | model name for claude (`--model`); absent means claude's own default |
48
- | `effort` | `low`, `medium`, `high`, `xhigh` or `max` | no | reasoning effort; absent means claude's default |
49
- | `bashTimeoutSecs` | positive integer | no | the longest one Bash call may run in the foreground, in seconds; absent means 1800. Sets claude's `BASH_DEFAULT_TIMEOUT_MS` and `BASH_MAX_TIMEOUT_MS` |
50
- | `operadTools` | object `{ home }` | no | operad's own tools: claude is given an MCP server (`@operad-io/mcp`'s `stdio.ts`, run by Bun with `--home`) named `operad`; its tools are not judged by the access profile, they run under the owner's MCP allow lists. Absent: claude gets no MCP server |
51
- | `mcpServers` | object, name to `{ command, args }` | no | other MCP servers claude starts, by name (a bot's servers, each behind the bot plugin's guard, which holds their secrets). Their tools are judged by the profile's `allowMcp`: a tool not listed asks first. Absent: none |
52
- | `secretValues` | array of strings | no | the values of the plugin's operad secrets; a log line shows `***` where one appears. Never written anywhere |
53
- | `images` | array of `{ path, mediaType }` | no | image files (`image/png`, `image/jpeg`, `image/gif`, `image/webp`) sent with the prompt as image blocks of the one user message. Absent: the prompt goes as text |
54
- | `access` | object | no | the access profile; `denyPaths` and `denyCommands` are enforced in `canUseTool` by `@operad-io/access-match`, `grants` are one-time permissions, other fields are ignored; absent means nothing is denied |
55
- | `maxTurns` | positive integer | no | SDK `maxTurns`; absent means no limit |
56
- | `budgetUsd` | positive number | no | SDK `maxBudgetUsd`; absent means none |
57
-
58
- An invalid object (not JSON, not an object, a required field missing or of the
59
- wrong type, an unknown field) writes one `Result` line with `result: "failed"`,
60
- `reason: "invalid_input"` and `message` naming the field, and exits 2.
61
-
62
- ## stdout
63
-
64
- One JSON object per line, flushed per line, each with a `type`:
65
-
66
- | type | other fields | meaning |
67
- |---|---|---|
68
- | `session` | `claudeVersion`, `apiKeySource`, `model` | claude started a session; written once, first. `apiKeySource` says which credential it uses: `none` for a subscription login, `ANTHROPIC_API_KEY` when that variable is set in the environment |
69
- | `message_end` | `role` (`"assistant"`), `text` (optional: the first 300 characters of the message's text as one paragraph; thinking and tool calls left out), `stopReason` (string or null), `usage` (`{input, output, cache_read, cache_write}`), `cost` (always `null`: claude prices a run, not a message) | an assistant message finished; written once per message |
70
- | `text_delta` | `text` | new assistant text only, not the text so far; increments are grouped into at most one line per 100 ms and the last part is written when the block or message ends |
71
- | `thinking_delta` | `text` | new thinking only, not the thinking so far; grouped like `text_delta`. Claude gives only what its thinking display gives; when there is no thinking text, no line is written |
72
- | `text_end` | `text` | the full text of a finished assistant message, after its `text_delta` lines |
73
- | `thinking_end` | `text` | the full text of a finished thinking block, after its `thinking_delta` lines |
74
- | `tool_execution_start` | `toolCallId`, `toolName`, `at`, `input` (optional) | a tool call began (the `PreToolUse` hook). `at` is the start time in epoch milliseconds. `input` says what ran: for `Bash` the command (one line, first 500 characters), for `Read`, `Edit` and `Write` the path, for another tool its main argument (path, pattern, query, URL or description) when it has one; absent otherwise. Never file contents or tool output. A command shows the values of leading environment assignments (`NAME=value`) as `NAME=***`, masks any value of the operad secret store or of a secret-named environment variable (`KEY`, `TOKEN`, `SECRET`, `PASS`, `PASSWORD`, `AUTH`, `CREDENTIAL`; 6 characters or more) as `***`, and shows `<command not shown: could not read it safely>` for a command it cannot read (an open quote, or a command or process substitution, heredoc or arithmetic in a leading assignment). A call the access profile denies shows `<tool> denied by the access profile`, never its arguments |
75
- | `tool_execution_end` | `toolCallId`, `toolName`, `isError`, `durationMs`, `error` (optional) | a tool call ended (`PostToolUse`, `PostToolUseFailure`, or a refusal). `durationMs` is how long the call took, measured from its start line. `error` is the first line of the error (at most 300 characters) when `isError` is true; the rest of the error stays out of the log |
76
- | `tool_output` | `toolCallId`, `text` | what a successful tool call printed, written before its `tool_execution_end`: Bash's stdout and stderr, a Read file's content, or another tool's text result. A `text` is at most 64 KiB of UTF-8; a longer output is written as consecutive lines with the same `toolCallId`, so joining them gives the whole output. A failed call writes no `tool_output`; its `error` is on the end line |
77
- | `blocked` | `toolName`, `reason` | `canUseTool` refused a call; its `tool_execution_end` has `isError: true`; the run continues |
78
- | `rate_limit` | `status`, `rateLimitType` (or null), `resetsAt` (or null) | claude reported its rate limit state |
79
- | `grant_use` | `id`, `waitId` | asks the host to use up a one-time permission; answered on stdin by `{"type":"grant_ack","id":n,"used":true\|false}` |
80
- | `ask_call` | `id`, `kind`, `value`, `tool` (path questions), `shown`, `session` | asks the host to ask a person whether one call may run (`askCommands`); answered on stdin by `{"type":"ask_ack","id":n,"decision":"allow"\|"deny"\|"stop"}` |
81
- | `ask_resolved` | `id` | the question closed; the call goes on or is refused |
82
- | `result` | see below | always the last line |
83
-
84
- ### Result
85
-
86
- | field | type | meaning |
87
- |---|---|---|
88
- | `type` | `"result"` | |
89
- | `result` | `"done"`, `"failed"` or `"cancelled"` | how the run ended |
90
- | `sessionId` | string or null | claude's `session_id`; its file is in `~/.claude/projects/`, and `claude --resume <id>` continues it |
91
- | `usage` | `{input, output, cache_read, cache_write}` | the result's four counters (`cache_creation_input_tokens` is `cache_write`); `{}` when there was no result |
92
- | `cost` | `{micros, quality}` or null | `round(total_cost_usd × 1e6)`; `quality` is `estimated` with a subscription login (an API-price figure, not a charge) and `exact` otherwise |
93
- | `finalMessage` | string or null | the result's `result` text |
94
- | `model` | string or null | the first key of the result's `modelUsage` |
95
- | `reason` | string | on `failed`: `invalid_input`, `session_missing` (claude has no such session) or `model_error` (also: claude is not installed, or it ended with no result) |
96
- | `message` | string | on `failed`: human text |
97
- | `modelError` | `{kind: "usage_limit", message, rateLimitType, resetsAt}` | on `failed`, when claude reported a rejected rate limit |
98
-
99
- A result with `is_error`, or any subtype but `success` (`error_max_turns`,
100
- `error_max_budget_usd`, ...), is `failed` with reason `model_error`.
101
-
102
- Every text of a new line (`text_delta`, `thinking_delta`, `text_end`,
103
- `thinking_end`, `tool_output`) has each value of `secretValues` replaced with
104
- `***`, as the existing command and error lines do.
105
-
106
- ## Exit codes
107
-
108
- | code | when |
109
- |---|---|
110
- | 0 | `done` |
111
- | 1 | `failed` after the input was valid |
112
- | 2 | invalid input |
113
- | 143 | SIGTERM: the program aborts the SDK query, writes a `cancelled` Result and exits |
114
-
115
- ## Tests
116
-
117
- `test/fake-claude.ts` is a fake `claude` process handed to the SDK through
118
- `spawnClaudeCodeProcess`: it answers the SDK's control requests and replays fixed
119
- stream-json from a script, so no test runs the real `claude` or sends a model
120
- request. `test/fake-worker.ts` runs the same `main` with that fake; the script
121
- comes from `FAKE_SCRIPT` and what the fake saw goes to `FAKE_RECORD`.
122
-
123
- ## The plugin
124
-
125
- `src/index.ts` is the hub side: the plugin `claude`, its step
126
- `claude/agent` and its flow `claude:prompt`. It never imports the
127
- SDK; it starts `bun src/worker.ts` through `runAgent` from
128
- `@operad-io/harness-process`, writes the stdin object above, appends every stdout
129
- line to the step's log with `JsonlLog`, and turns the last `result` line into the
130
- facts. The params, the result names (`done`, `failed`, `interrupted`) and the
131
- facts (`exitCode`, `signal`, `sessionId`, `finalMessage`, `usage`, `log`,
132
- `durationMs`, `stderr`) are `pi`'s. `usage` is the standard one in
133
- [the manual](../../../docs/user/dev.md#usage-and-end-results): `model` is the step's
134
- `--model` or the `model` setting, else the model claude reports; `costMicros` is
135
- the worker's `cost.micros`, and `costQuality` is `estimated` for a subscription
136
- login and `exact` for an API key. `sessionId` is claude's session id; pass it back
137
- as `resumeSession` to continue it (`claude --resume <id>` continues the same
138
- session).
139
-
140
- The plugin is not installed by `operad init`. Turn it on with:
141
-
142
- ```sh
143
- operad plugin enable claude
144
- ```
145
-
146
- A flow switches harness by changing `use` to `claude/agent`; `dev` does
147
- it with `worker.use` in `.operad/dev.json`.
148
-
149
- ### Params: `args`
150
-
151
- | flag | value | meaning |
152
- |---|---|---|
153
- | `--model` | a model name claude takes, such as `opus` | the model for this step |
154
- | `--effort` | `low`, `medium`, `high`, `xhigh` or `max` | the reasoning effort for this step |
155
-
156
- A flag in `args` wins over the setting of the same name. A chat passes its
157
- effort as the step param `effort`, which also wins over the setting. Any other
158
- entry fails the step at once, before the worker starts, with result `failed`,
159
- reason `invalid_args` and a message that names the entry: an unknown flag, a
160
- flag without its value, an effort outside the set, a flag given twice, the `=`
161
- spelling.
162
-
163
- ### Settings
164
-
165
- The `claude` section of `config.json`. A step param (`wallTimeSecs`,
166
- `stallSecs`) overrides the setting of the same name.
167
-
168
- | setting | type | default | meaning |
169
- |---|---|---|---|
170
- | `model` | string | unset | the model; unset uses claude's own default |
171
- | `effort` | `low`, `medium`, `high`, `xhigh` or `max` | unset | the reasoning effort; unset uses claude's default |
172
- | `command` | string | `claude` | the claude command: a name on PATH or a path |
173
- | `maxTurns` | integer, 1 or more | unset | the most turns one run may take |
174
- | `budgetUsd` | number, 0.01 or more | unset | the most one run may spend, for API-key logins |
175
- | `bashTimeoutSecs` | integer, 1 or more | `1800` | the longest one Bash call may run, in seconds; nothing runs in the background, so a long gate runs in the foreground within this time |
176
- | `wallTimeSecs` | number | `3600` | the longest one run may take; at most 28500 |
177
- | `stallSecs` | number | `600` | how long a run may print nothing while no tool call is open |
178
- | `concurrency` | object, model to positive integer | `{}` | how many runs of one model may go at once; key `claude:<model>` |
179
-
180
- ### How the step ends
181
-
182
- As `pi`'s table. A worker `failed` with reason `model_error` also sets
183
- `data.modelError`: a rate limit the worker saw is `usage_limit`; otherwise the
184
- kind is derived from the message text as in `pi` (`usage_limit`,
185
- `unavailable` or `protocol`). `retry_after_secs` is `null`.
186
-
187
- ### `operad plugin check`
188
-
189
- Runs `claude --version` and `claude auth status` on the claude the `command`
190
- setting names, and reports the version, the Agent SDK version and the login
191
- kind (for example `login claude.ai (max)`). It fails, saying what to do, when:
192
-
193
- - there is no claude command;
194
- - the version is below the floor, `2.1.319`, or `--version` prints nothing that
195
- starts with a version;
196
- - claude is not logged in, or `auth status` prints nothing it can read.
197
-
198
- The SDK version is only reported, never refused. Nothing from `auth status`
199
- but the login kind is read or printed: no e-mail, organisation or token.
200
-
201
- ### Access profile
202
-
203
- The step declares `enforces: ["denyPaths", "denyCommands", "askCommands", "askPaths"]`
204
- and passes the run's profile to the worker. A profile that sets `askCommands` is
205
- accepted: a call it matches asks a person first (see "Asking about a call").
206
-
207
- The worker judges every call in `canUseTool` (`src/access.ts`). `access-match`
208
- knows lower-case tool names and the fields `command` and `path`, so one table
209
- maps claude's tools to them:
210
-
211
- | claude tool | judged as | field read | field required |
212
- |---|---|---|---|
213
- | `Bash` | `bash` | `command` | yes |
214
- | `Read` | `read` | `file_path` | yes |
215
- | `Write` | `write` | `file_path` | yes |
216
- | `Edit` | `edit` | `file_path` | yes |
217
- | `MultiEdit` | `edit` | `file_path` | yes |
218
- | `NotebookEdit` | `edit` | `notebook_path` | yes |
219
- | `Grep` | `grep` | `path` | no (the search starts in `cwd`) |
220
- | `Glob` | `find` | `path` | no (the search starts in `cwd`) |
221
-
222
- A call whose required field is missing or not a string is refused while a deny
223
- rule is set: a call without its target cannot be judged.
224
-
225
- A tool that is not in the table:
226
-
227
- - `TodoWrite`, `WebFetch`, `WebSearch`, `ExitPlanMode`, `BashOutput`,
228
- `KillShell`, `Task` and `Agent` run no command and name no file, so they are
229
- allowed. A subagent's own tool calls come to `canUseTool` too and are judged
230
- like any other.
231
- - Any other name (an MCP tool, a tool a later claude adds) is allowed only when
232
- the profile has no `denyPaths` and no `denyCommands`. With a rule set it is
233
- refused, because a rule that could match it cannot be checked.
234
-
235
- A refused call is answered `{ behavior: "deny", message }`; the message is the
236
- reason `access-match` gave, the model receives it as the tool's result and the
237
- run goes on. The worker writes a `blocked` line and closes the call with a
238
- `tool_execution_end` that has `isError: true`.
239
-
240
- A one-time permission arrives in the profile as `grants: [{ waitId, kind, value,
241
- tool? }]`. When it is present the step keeps the worker's stdin open after the
242
- input line. A call the profile refuses and that equals a grant exactly (`Bash`
243
- command text, or the path for the one tool the grant names) makes the worker
244
- write `{"type":"grant_use","id":n,"waitId":"..."}` and wait for one stdin line
245
- `{"type":"grant_ack","id":n,"used":true|false}`. The host records `grant_used`
246
- before it answers `used: true`; only then does the call run. `used: false`, a
247
- host that is gone (stdin closed) and a host that fails all leave the call
248
- refused. A second identical call finds the grant used and is judged as usual.
249
- The host's half (`hasGrants`, `grantUse`, `answerGrant`) is in
250
- `@operad-io/harness-process`, shared with `pi`.
251
-
252
- ### Asking about a call
253
-
254
- A call the profile's `askCommands` matches (and that no deny rule matched: deny
255
- wins over ask) asks a person instead of running. The step keeps the worker's
256
- stdin open. The worker writes `{"type":"ask_call","id":n,"kind":"command","value":"<the whole command>","shown":"...","session":"<session>"|null}`
257
- and holds the call; the host asks with `ctx.ask` (choices `allow_once`,
258
- `allow_always` and `reject_once`, the grace period of the kernel: 10 minutes by
259
- default, set by `ask.graceSecs`, `askGraceSecs` of the step or the question) and
260
- answers on
261
- stdin `{"type":"ask_ack","id":n,"decision":"allow"|"deny"|"stop","reason"?}`,
262
- after which the worker writes `{"type":"ask_resolved","id":n}`. Questions go
263
- one at a time. A command that holds a value of the operad secret store is
264
- refused and never asked about, so no fact holds a secret.
265
-
266
- - Answered within the grace period (`allow`): the same step goes on and this one
267
- call runs. `allow_once` keeps nothing for another call; `allow_always` also
268
- appends the exact command or tool, path and working directory to
269
- `$OPERAD_HOME/access-user.json`, so later runs read it from the profile's
270
- `alwaysAllow` entries. `reject_once` is `deny`.
271
- - Not answered in time (`stop`): the worker holds the call, the host ends the
272
- program, and the step ends `waiting` (the result is declared). The one pending
273
- item stays, with the run's object. The flow's `waiting` route goes to a wait
274
- for a person and back to the step.
275
- - Answered later: the step runs again with `resumeSession`, `askAnswer` and
276
- `grants: [{ waitId, kind, value }]` for `allow_once` or `allow_always`. The
277
- worker uses the grant for the one call it covers (`grant_use`, above); an
278
- `allow_always` answer also writes its entry to the user layer first. Any other
279
- call asks again. After `reject_once` no grant arrives and the model is told so
280
- in the prompt.
281
- - A run no person's message started (a schedule, a webhook or another event)
282
- neither reads the user layer nor offers `allow_always` (`operad-sandbox.md`
283
- decision 8).
284
-
285
- The host's half (`createCallAsker`, `askCall`, `askRequest`) is in
286
- `@operad-io/harness-process`. `askCommands` is in the step's `enforces`.
9
+ - User guide: [docs/user/dev.md](https://github.com/operad-io/operad/blob/main/docs/user/dev.md)
10
+ - Website: <https://operad.io>
11
+ - Developer reference: `REFERENCE.md` in this directory
287
12
 
13
+ MIT licensed.
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "@operad-io/harness-claude",
3
- "version": "0.0.1",
3
+ "version": "0.1.1",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./src/index.ts"
7
7
  },
8
8
  "dependencies": {
9
9
  "@anthropic-ai/claude-agent-sdk": "^0.3.319",
10
- "@operad-io/access-match": "0.0.1",
11
- "@operad-io/engine": "0.0.1",
12
- "@operad-io/harness-process": "0.0.1"
10
+ "@operad-io/access-match": "0.1.1",
11
+ "@operad-io/engine": "0.1.1",
12
+ "@operad-io/harness-process": "0.1.1"
13
13
  },
14
- "description": "operad: the claude harness, a client of the Claude Agent SDK (in development)",
14
+ "description": "operad: the claude harness, a client of the Claude Agent SDK",
15
15
  "repository": {
16
16
  "type": "git",
17
17
  "url": "git+https://github.com/operad-io/operad.git",
package/src/access.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * The access profile applied to claude's tool calls
3
- * (docs/backlog/operad-harness-claude.md decision 6).
3
+ * (ADR 0024 D6).
4
4
  *
5
5
  * `@operad-io/access-match` judges calls by lower-case tool names and the
6
6
  * input fields `command` and `path`; claude names its tools in Title case and
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * The user's own `claude`: found, never bundled
3
- * (docs/backlog/operad-harness-claude.md decisions 2 and 10). `inspectClaude`
3
+ * (ADR 0024 D2, D10). `inspectClaude`
4
4
  * is what `operad plugin check` runs: `claude --version` and
5
5
  * `claude auth status`.
6
6
  */
package/src/index.ts CHANGED
@@ -7,6 +7,7 @@
7
7
  * child program, never by the hub.
8
8
  */
9
9
 
10
+ import pkg from "../package.json" with { type: "json" };
10
11
  import { secretValuesOf } from "@operad-io/access-match";
11
12
  import { existsSync } from "node:fs";
12
13
  import { fileURLToPath } from "node:url";
@@ -229,7 +230,7 @@ export function createClaudeHarnessPlugin(options: ClaudeHarnessOptions = {}) {
229
230
  return definePlugin({
230
231
  name: HARNESS,
231
232
  kind: "harness",
232
- version: "0.0.0",
233
+ version: pkg.version,
233
234
  models: CLAUDE_MODELS,
234
235
  efforts: CLAUDE_EFFORTS,
235
236
  settings: PLUGIN_SETTINGS,
package/src/worker.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  /**
2
- * The claude child program (docs/backlog/operad-harness-claude.md decisions
3
- * 2 to 8).
2
+ * The claude child program (ADR 0024 D2 to D8).
4
3
  *
5
4
  * Reads one JSON object from stdin, runs one prompt through the Agent SDK
6
5
  * (which starts the user's own `claude`) and writes one JSON object per line