@operad-io/harness-claude 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 +286 -2
- package/package.json +24 -4
- package/src/access.ts +168 -0
- package/src/claude-install.ts +175 -0
- package/src/index.ts +449 -0
- package/src/protocol.ts +257 -0
- package/src/worker.ts +960 -0
package/src/protocol.ts
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The wire shapes of the claude child program. `README.md` holds the same
|
|
3
|
+
* tables; `test/heavy/protocol.test.ts` checks the program against them.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { readProfile } from "@operad-io/access-match";
|
|
7
|
+
import { parseImageInputs, type ImageInput, type SharedLine } from "@operad-io/harness-process";
|
|
8
|
+
import { isRecord } from "@operad-io/engine";
|
|
9
|
+
import { mountedServersOf } from "@operad-io/harness-process";
|
|
10
|
+
|
|
11
|
+
export const EFFORT_LEVELS = ["low", "medium", "high", "xhigh", "max"] as const;
|
|
12
|
+
export type WorkerEffort = (typeof EFFORT_LEVELS)[number];
|
|
13
|
+
|
|
14
|
+
/** What the program reads from stdin. */
|
|
15
|
+
export interface WorkerInput {
|
|
16
|
+
readonly prompt: string;
|
|
17
|
+
readonly cwd: string;
|
|
18
|
+
/** The claude command (a name on PATH or a path); unset means `claude`. */
|
|
19
|
+
readonly command?: string | undefined;
|
|
20
|
+
/** A session id from an earlier `Result.sessionId`. */
|
|
21
|
+
readonly resumeSession?: string | undefined;
|
|
22
|
+
readonly model?: string | undefined;
|
|
23
|
+
readonly effort?: WorkerEffort | undefined;
|
|
24
|
+
readonly access?: Readonly<Record<string, unknown>> | undefined;
|
|
25
|
+
readonly maxTurns?: number | undefined;
|
|
26
|
+
readonly budgetUsd?: number | undefined;
|
|
27
|
+
/** The longest a Bash call may run in the foreground, in seconds; unset means `DEFAULT_BASH_TIMEOUT_SECS`. */
|
|
28
|
+
readonly bashTimeoutSecs?: number | undefined;
|
|
29
|
+
/** Operad's own tools, served to claude as an MCP server of the named home. */
|
|
30
|
+
readonly operadTools?: { readonly home: string } | undefined;
|
|
31
|
+
/** Other MCP servers to start for the agent, by name (a bot's servers, behind its guard). */
|
|
32
|
+
readonly mcpServers?: Readonly<Record<string, { readonly command: string; readonly args: readonly string[] }>> | undefined;
|
|
33
|
+
/** Secret values of the operad secret store; a log line shows `***` where one appears. */
|
|
34
|
+
readonly secretValues?: readonly string[] | undefined;
|
|
35
|
+
/** Images sent with the prompt, read from these files. */
|
|
36
|
+
readonly images?: readonly ImageInput[] | undefined;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** A gate such as a full test run must finish in the foreground; claude's own default is 120 s. */
|
|
40
|
+
export const DEFAULT_BASH_TIMEOUT_SECS = 1800;
|
|
41
|
+
|
|
42
|
+
export interface WorkerUsage {
|
|
43
|
+
input?: number;
|
|
44
|
+
output?: number;
|
|
45
|
+
cache_read?: number;
|
|
46
|
+
cache_write?: number;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export type WorkerResultName = "done" | "failed" | "cancelled";
|
|
50
|
+
|
|
51
|
+
export type FailureReason = "invalid_input" | "session_missing" | "model_error";
|
|
52
|
+
|
|
53
|
+
/** `estimated` for a subscription login (an API-price figure), `exact` for an API key. */
|
|
54
|
+
export type CostQuality = "estimated" | "exact";
|
|
55
|
+
|
|
56
|
+
/** Set when the run stopped at a rate limit. */
|
|
57
|
+
export interface WorkerModelError {
|
|
58
|
+
readonly kind: "usage_limit";
|
|
59
|
+
readonly message: string;
|
|
60
|
+
readonly rateLimitType: string | null;
|
|
61
|
+
readonly resetsAt: number | null;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The last line of stdout. */
|
|
65
|
+
export interface WorkerResult {
|
|
66
|
+
readonly type: "result";
|
|
67
|
+
readonly result: WorkerResultName;
|
|
68
|
+
readonly sessionId: string | null;
|
|
69
|
+
readonly usage: WorkerUsage;
|
|
70
|
+
readonly cost: { readonly micros: number; readonly quality: CostQuality } | null;
|
|
71
|
+
readonly finalMessage: string | null;
|
|
72
|
+
/** The model that did the work: the first key of the result's `modelUsage`. */
|
|
73
|
+
readonly model: string | null;
|
|
74
|
+
readonly reason?: FailureReason;
|
|
75
|
+
readonly message?: string;
|
|
76
|
+
readonly modelError?: WorkerModelError;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export type WorkerLine =
|
|
80
|
+
/**
|
|
81
|
+
* Written once, when claude starts a session. `apiKeySource` says which
|
|
82
|
+
* credential claude uses: `none` for a subscription login, `ANTHROPIC_API_KEY`
|
|
83
|
+
* when that environment variable is set.
|
|
84
|
+
*/
|
|
85
|
+
| {
|
|
86
|
+
readonly type: "session";
|
|
87
|
+
readonly claudeVersion: string;
|
|
88
|
+
readonly apiKeySource: string;
|
|
89
|
+
readonly model: string;
|
|
90
|
+
}
|
|
91
|
+
| {
|
|
92
|
+
readonly type: "message_end";
|
|
93
|
+
readonly role: "assistant";
|
|
94
|
+
/** The first 300 characters of the message's text, one paragraph; absent when it has none. */
|
|
95
|
+
readonly text?: string;
|
|
96
|
+
readonly stopReason: string | null;
|
|
97
|
+
readonly usage: Required<WorkerUsage>;
|
|
98
|
+
readonly cost: null;
|
|
99
|
+
}
|
|
100
|
+
| {
|
|
101
|
+
readonly type: "rate_limit";
|
|
102
|
+
readonly status: string;
|
|
103
|
+
readonly rateLimitType: string | null;
|
|
104
|
+
readonly resetsAt: number | null;
|
|
105
|
+
}
|
|
106
|
+
| SharedLine
|
|
107
|
+
| WorkerResult;
|
|
108
|
+
|
|
109
|
+
/** A stdin line after the input: the host's answer to a `grant_use` line. */
|
|
110
|
+
export interface GrantAck {
|
|
111
|
+
readonly type: "grant_ack";
|
|
112
|
+
readonly id: number;
|
|
113
|
+
readonly used: boolean;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* A stdin line after the input: the host's answer to an `ask_call` line.
|
|
118
|
+
* `allow` lets this one call run; `deny` refuses it; `stop` means the wait
|
|
119
|
+
* ended and the program is about to be ended: it holds the call.
|
|
120
|
+
*/
|
|
121
|
+
export interface AskAck {
|
|
122
|
+
readonly type: "ask_ack";
|
|
123
|
+
readonly id: number;
|
|
124
|
+
readonly decision: "allow" | "deny" | "stop";
|
|
125
|
+
readonly reason?: string;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export type ParsedInput =
|
|
129
|
+
| { readonly ok: true; readonly input: WorkerInput }
|
|
130
|
+
| { readonly ok: false; readonly message: string };
|
|
131
|
+
|
|
132
|
+
/** The stdin fields, in the order the README lists them. */
|
|
133
|
+
export const INPUT_FIELDS = [
|
|
134
|
+
"prompt",
|
|
135
|
+
"cwd",
|
|
136
|
+
"command",
|
|
137
|
+
"resumeSession",
|
|
138
|
+
"model",
|
|
139
|
+
"effort",
|
|
140
|
+
"access",
|
|
141
|
+
"maxTurns",
|
|
142
|
+
"budgetUsd",
|
|
143
|
+
"bashTimeoutSecs",
|
|
144
|
+
"operadTools",
|
|
145
|
+
"mcpServers",
|
|
146
|
+
"secretValues",
|
|
147
|
+
"images",
|
|
148
|
+
] as const;
|
|
149
|
+
|
|
150
|
+
const SESSION_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
151
|
+
|
|
152
|
+
/** Validates the decoded stdin object; the message names the field at fault. */
|
|
153
|
+
export function parseInput(value: unknown): ParsedInput {
|
|
154
|
+
if (!isRecord(value)) return { ok: false, message: "the input must be a JSON object" };
|
|
155
|
+
|
|
156
|
+
// A field the program does not know is refused, never ignored: a flag that
|
|
157
|
+
// is silently dropped would hide a wrong model.
|
|
158
|
+
for (const key of Object.keys(value)) {
|
|
159
|
+
if (!(INPUT_FIELDS as readonly string[]).includes(key)) {
|
|
160
|
+
return { ok: false, message: `${key} is not an input field` };
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const text = (field: "prompt" | "cwd" | "command" | "resumeSession" | "model", required: boolean): string | undefined | Error => {
|
|
165
|
+
const got = value[field];
|
|
166
|
+
if (got === undefined) return required ? new Error(`${field} is required`) : undefined;
|
|
167
|
+
if (typeof got !== "string" || got.length === 0) return new Error(`${field} must be a non-empty string`);
|
|
168
|
+
return got;
|
|
169
|
+
};
|
|
170
|
+
const prompt = text("prompt", true);
|
|
171
|
+
if (prompt instanceof Error) return { ok: false, message: prompt.message };
|
|
172
|
+
const cwd = text("cwd", true);
|
|
173
|
+
if (cwd instanceof Error) return { ok: false, message: cwd.message };
|
|
174
|
+
const command = text("command", false);
|
|
175
|
+
if (command instanceof Error) return { ok: false, message: command.message };
|
|
176
|
+
const resumeSession = text("resumeSession", false);
|
|
177
|
+
if (resumeSession instanceof Error) return { ok: false, message: resumeSession.message };
|
|
178
|
+
if (resumeSession !== undefined && !SESSION_ID.test(resumeSession)) {
|
|
179
|
+
return { ok: false, message: "resumeSession must be a session id (a UUID)" };
|
|
180
|
+
}
|
|
181
|
+
const model = text("model", false);
|
|
182
|
+
if (model instanceof Error) return { ok: false, message: model.message };
|
|
183
|
+
|
|
184
|
+
const level = value["effort"];
|
|
185
|
+
let effort: WorkerEffort | undefined;
|
|
186
|
+
if (level !== undefined) {
|
|
187
|
+
const known = EFFORT_LEVELS.find((candidate) => candidate === level);
|
|
188
|
+
if (known === undefined) {
|
|
189
|
+
return { ok: false, message: `effort must be one of ${EFFORT_LEVELS.join(", ")}` };
|
|
190
|
+
}
|
|
191
|
+
effort = known;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const secretValues = value["secretValues"];
|
|
195
|
+
if (secretValues !== undefined && (!Array.isArray(secretValues) || !secretValues.every((v): v is string => typeof v === "string"))) {
|
|
196
|
+
return { ok: false, message: "secretValues must be an array of strings" };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const images = parseImageInputs(value["images"]);
|
|
200
|
+
if (typeof images === "string") return { ok: false, message: images };
|
|
201
|
+
|
|
202
|
+
const maxTurns = value["maxTurns"];
|
|
203
|
+
if (maxTurns !== undefined && (typeof maxTurns !== "number" || !Number.isInteger(maxTurns) || maxTurns < 1)) {
|
|
204
|
+
return { ok: false, message: "maxTurns must be a positive integer" };
|
|
205
|
+
}
|
|
206
|
+
const budgetUsd = value["budgetUsd"];
|
|
207
|
+
if (budgetUsd !== undefined && (typeof budgetUsd !== "number" || !Number.isFinite(budgetUsd) || budgetUsd <= 0)) {
|
|
208
|
+
return { ok: false, message: "budgetUsd must be a positive number" };
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const bashTimeoutSecs = value["bashTimeoutSecs"];
|
|
212
|
+
if (
|
|
213
|
+
bashTimeoutSecs !== undefined &&
|
|
214
|
+
(typeof bashTimeoutSecs !== "number" || !Number.isInteger(bashTimeoutSecs) || bashTimeoutSecs < 1)
|
|
215
|
+
) {
|
|
216
|
+
return { ok: false, message: "bashTimeoutSecs must be a positive integer" };
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const operadTools = value["operadTools"];
|
|
220
|
+
if (operadTools !== undefined && (!isRecord(operadTools) || typeof operadTools["home"] !== "string" || operadTools["home"].length === 0)) {
|
|
221
|
+
return { ok: false, message: "operadTools must be an object with a home" };
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const mcpServers = mountedServersOf(value["mcpServers"]);
|
|
225
|
+
if (value["mcpServers"] !== undefined && (!isRecord(value["mcpServers"]) || (Object.keys(value["mcpServers"]).length > 0 && mcpServers === undefined))) {
|
|
226
|
+
return { ok: false, message: "mcpServers must be an object of servers, each with a command" };
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const access = value["access"];
|
|
230
|
+
if (access !== undefined && !isRecord(access)) {
|
|
231
|
+
return { ok: false, message: "access must be an object" };
|
|
232
|
+
}
|
|
233
|
+
if (access !== undefined) {
|
|
234
|
+
const profile = readProfile(access);
|
|
235
|
+
if (!profile.ok) return { ok: false, message: `access.${profile.message}` };
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return {
|
|
239
|
+
ok: true,
|
|
240
|
+
input: {
|
|
241
|
+
prompt: prompt ?? "",
|
|
242
|
+
cwd: cwd ?? "",
|
|
243
|
+
command,
|
|
244
|
+
resumeSession,
|
|
245
|
+
model,
|
|
246
|
+
effort,
|
|
247
|
+
access,
|
|
248
|
+
maxTurns,
|
|
249
|
+
budgetUsd,
|
|
250
|
+
bashTimeoutSecs,
|
|
251
|
+
operadTools: operadTools === undefined ? undefined : { home: String(operadTools["home"]) },
|
|
252
|
+
mcpServers,
|
|
253
|
+
secretValues,
|
|
254
|
+
images: images.length === 0 ? undefined : images,
|
|
255
|
+
},
|
|
256
|
+
};
|
|
257
|
+
}
|