@operad-io/harness-claude 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 +4 -0
- package/package.json +4 -4
- package/src/access.ts +121 -39
- package/src/claude-install.ts +114 -15
- package/src/index.ts +184 -283
- package/src/protocol.ts +102 -98
- package/src/worker.ts +1161 -679
package/src/protocol.ts
CHANGED
|
@@ -3,14 +3,48 @@
|
|
|
3
3
|
* tables; `test/heavy/protocol.test.ts` checks the program against them.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import {
|
|
7
|
-
|
|
6
|
+
import {
|
|
7
|
+
mountedServersOf,
|
|
8
|
+
readAccess,
|
|
9
|
+
readBoolean,
|
|
10
|
+
readImages,
|
|
11
|
+
readKeep,
|
|
12
|
+
readOperadTools,
|
|
13
|
+
readOptionalString,
|
|
14
|
+
readPositiveInteger,
|
|
15
|
+
readPositiveNumber,
|
|
16
|
+
readRequiredStrings,
|
|
17
|
+
readSecrets,
|
|
18
|
+
readStringList,
|
|
19
|
+
readUnknownFields,
|
|
20
|
+
type ImageInput,
|
|
21
|
+
type SharedLine,
|
|
22
|
+
type WorkerKeep,
|
|
23
|
+
type WorkerResultName,
|
|
24
|
+
type WorkerUsage,
|
|
25
|
+
} from "@operad-io/harness-process";
|
|
8
26
|
import { isRecord } from "@operad-io/engine";
|
|
9
|
-
|
|
27
|
+
|
|
28
|
+
export type { AskAck, GrantAck, WorkerResultName, WorkerUsage } from "@operad-io/harness-process";
|
|
10
29
|
|
|
11
30
|
export const EFFORT_LEVELS = ["low", "medium", "high", "xhigh", "max"] as const;
|
|
12
31
|
export type WorkerEffort = (typeof EFFORT_LEVELS)[number];
|
|
13
32
|
|
|
33
|
+
/** The context window of each model the plugin lists, in tokens. */
|
|
34
|
+
export const CLAUDE_CONTEXT_WINDOWS: Readonly<Record<string, number>> = {
|
|
35
|
+
opus: 200_000,
|
|
36
|
+
sonnet: 200_000,
|
|
37
|
+
haiku: 200_000,
|
|
38
|
+
"opus[1m]": 1_000_000,
|
|
39
|
+
"sonnet[1m]": 1_000_000,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/** The context window a model name names, when claude's list knows it. */
|
|
43
|
+
export function claudeContextWindow(model: string | null | undefined): number | undefined {
|
|
44
|
+
if (model === null || model === undefined) return undefined;
|
|
45
|
+
return CLAUDE_CONTEXT_WINDOWS[model.toLowerCase()];
|
|
46
|
+
}
|
|
47
|
+
|
|
14
48
|
/** What the program reads from stdin. */
|
|
15
49
|
export interface WorkerInput {
|
|
16
50
|
readonly prompt: string;
|
|
@@ -34,20 +68,19 @@ export interface WorkerInput {
|
|
|
34
68
|
readonly secretValues?: readonly string[] | undefined;
|
|
35
69
|
/** Images sent with the prompt, read from these files. */
|
|
36
70
|
readonly images?: readonly ImageInput[] | undefined;
|
|
71
|
+
/** Skill directories to load; no other skills are discovered. */
|
|
72
|
+
readonly skills?: readonly string[] | undefined;
|
|
73
|
+
/** Load the owner's own agent settings (ADR 0024 D4); off unless true. */
|
|
74
|
+
readonly useOwnSettings?: boolean | undefined;
|
|
75
|
+
/** Set by a chat reply: the worker may serve the next reply and then end itself. */
|
|
76
|
+
readonly keep?: WorkerKeep | undefined;
|
|
37
77
|
}
|
|
38
78
|
|
|
79
|
+
export type { WorkerKeep } from "@operad-io/harness-process";
|
|
80
|
+
|
|
39
81
|
/** A gate such as a full test run must finish in the foreground; claude's own default is 120 s. */
|
|
40
82
|
export const DEFAULT_BASH_TIMEOUT_SECS = 1800;
|
|
41
83
|
|
|
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
84
|
export type FailureReason = "invalid_input" | "session_missing" | "model_error";
|
|
52
85
|
|
|
53
86
|
/** `estimated` for a subscription login (an API-price figure), `exact` for an API key. */
|
|
@@ -59,6 +92,10 @@ export interface WorkerModelError {
|
|
|
59
92
|
readonly message: string;
|
|
60
93
|
readonly rateLimitType: string | null;
|
|
61
94
|
readonly resetsAt: number | null;
|
|
95
|
+
/** How long until the reset, in seconds; null when claude gave no reset time. */
|
|
96
|
+
readonly retry_after_secs: number | null;
|
|
97
|
+
/** When the rate limit was reported, in epoch milliseconds; null without a reset time. */
|
|
98
|
+
readonly reported_at: number | null;
|
|
62
99
|
}
|
|
63
100
|
|
|
64
101
|
/** The last line of stdout. */
|
|
@@ -71,6 +108,8 @@ export interface WorkerResult {
|
|
|
71
108
|
readonly finalMessage: string | null;
|
|
72
109
|
/** The model that did the work: the first key of the result's `modelUsage`. */
|
|
73
110
|
readonly model: string | null;
|
|
111
|
+
/** The context after the last model call, when one ran. */
|
|
112
|
+
readonly context?: { readonly used: number; readonly window?: number } | undefined;
|
|
74
113
|
readonly reason?: FailureReason;
|
|
75
114
|
readonly message?: string;
|
|
76
115
|
readonly modelError?: WorkerModelError;
|
|
@@ -106,25 +145,6 @@ export type WorkerLine =
|
|
|
106
145
|
| SharedLine
|
|
107
146
|
| WorkerResult;
|
|
108
147
|
|
|
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
148
|
export type ParsedInput =
|
|
129
149
|
| { readonly ok: true; readonly input: WorkerInput }
|
|
130
150
|
| { readonly ok: false; readonly message: string };
|
|
@@ -145,6 +165,9 @@ export const INPUT_FIELDS = [
|
|
|
145
165
|
"mcpServers",
|
|
146
166
|
"secretValues",
|
|
147
167
|
"images",
|
|
168
|
+
"skills",
|
|
169
|
+
"useOwnSettings",
|
|
170
|
+
"keep",
|
|
148
171
|
] as const;
|
|
149
172
|
|
|
150
173
|
const SESSION_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
@@ -155,31 +178,20 @@ export function parseInput(value: unknown): ParsedInput {
|
|
|
155
178
|
|
|
156
179
|
// A field the program does not know is refused, never ignored: a flag that
|
|
157
180
|
// is silently dropped would hide a wrong model.
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
return { ok: false, message: `${key} is not an input field` };
|
|
161
|
-
}
|
|
162
|
-
}
|
|
181
|
+
const unknown = readUnknownFields(value, INPUT_FIELDS);
|
|
182
|
+
if (!unknown.ok) return unknown;
|
|
163
183
|
|
|
164
|
-
const
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
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)) {
|
|
184
|
+
const required = readRequiredStrings(value, ["prompt", "cwd"]);
|
|
185
|
+
if (!required.ok) return required;
|
|
186
|
+
const command = readOptionalString(value, "command");
|
|
187
|
+
if (!command.ok) return command;
|
|
188
|
+
const resumeSession = readOptionalString(value, "resumeSession");
|
|
189
|
+
if (!resumeSession.ok) return resumeSession;
|
|
190
|
+
if (resumeSession.value !== undefined && !SESSION_ID.test(resumeSession.value)) {
|
|
179
191
|
return { ok: false, message: "resumeSession must be a session id (a UUID)" };
|
|
180
192
|
}
|
|
181
|
-
const model =
|
|
182
|
-
if (model
|
|
193
|
+
const model = readOptionalString(value, "model");
|
|
194
|
+
if (!model.ok) return model;
|
|
183
195
|
|
|
184
196
|
const level = value["effort"];
|
|
185
197
|
let effort: WorkerEffort | undefined;
|
|
@@ -191,67 +203,59 @@ export function parseInput(value: unknown): ParsedInput {
|
|
|
191
203
|
effort = known;
|
|
192
204
|
}
|
|
193
205
|
|
|
194
|
-
const secretValues = value
|
|
195
|
-
if (secretValues
|
|
196
|
-
return { ok: false, message: "secretValues must be an array of strings" };
|
|
197
|
-
}
|
|
206
|
+
const secretValues = readSecrets(value);
|
|
207
|
+
if (!secretValues.ok) return secretValues;
|
|
198
208
|
|
|
199
|
-
const images =
|
|
200
|
-
if (
|
|
209
|
+
const images = readImages(value);
|
|
210
|
+
if (!images.ok) return images;
|
|
201
211
|
|
|
202
|
-
const
|
|
203
|
-
if (
|
|
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
|
-
}
|
|
212
|
+
const skills = readStringList(value, "skills");
|
|
213
|
+
if (!skills.ok) return skills;
|
|
210
214
|
|
|
211
|
-
const
|
|
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
|
-
}
|
|
215
|
+
const useOwnSettings = readBoolean(value, "useOwnSettings");
|
|
216
|
+
if (!useOwnSettings.ok) return useOwnSettings;
|
|
218
217
|
|
|
219
|
-
const
|
|
220
|
-
if (
|
|
221
|
-
|
|
222
|
-
|
|
218
|
+
const maxTurns = readPositiveInteger(value, "maxTurns");
|
|
219
|
+
if (!maxTurns.ok) return maxTurns;
|
|
220
|
+
const budgetUsd = readPositiveNumber(value, "budgetUsd");
|
|
221
|
+
if (!budgetUsd.ok) return budgetUsd;
|
|
222
|
+
const bashTimeoutSecs = readPositiveInteger(value, "bashTimeoutSecs");
|
|
223
|
+
if (!bashTimeoutSecs.ok) return bashTimeoutSecs;
|
|
224
|
+
|
|
225
|
+
const operadTools = readOperadTools(value);
|
|
226
|
+
if (!operadTools.ok) return operadTools;
|
|
223
227
|
|
|
224
228
|
const mcpServers = mountedServersOf(value["mcpServers"]);
|
|
225
229
|
if (value["mcpServers"] !== undefined && (!isRecord(value["mcpServers"]) || (Object.keys(value["mcpServers"]).length > 0 && mcpServers === undefined))) {
|
|
226
230
|
return { ok: false, message: "mcpServers must be an object of servers, each with a command" };
|
|
227
231
|
}
|
|
228
232
|
|
|
229
|
-
const access = value
|
|
230
|
-
if (access
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
if (
|
|
234
|
-
const profile = readProfile(access);
|
|
235
|
-
if (!profile.ok) return { ok: false, message: `access.${profile.message}` };
|
|
236
|
-
}
|
|
233
|
+
const access = readAccess(value);
|
|
234
|
+
if (!access.ok) return access;
|
|
235
|
+
|
|
236
|
+
const keep = readKeep(value);
|
|
237
|
+
if (!keep.ok) return keep;
|
|
237
238
|
|
|
238
239
|
return {
|
|
239
240
|
ok: true,
|
|
240
241
|
input: {
|
|
241
|
-
prompt: prompt ?? "",
|
|
242
|
-
cwd: cwd ?? "",
|
|
243
|
-
command,
|
|
244
|
-
resumeSession,
|
|
245
|
-
model,
|
|
242
|
+
prompt: required.value["prompt"] ?? "",
|
|
243
|
+
cwd: required.value["cwd"] ?? "",
|
|
244
|
+
command: command.value,
|
|
245
|
+
resumeSession: resumeSession.value,
|
|
246
|
+
model: model.value,
|
|
246
247
|
effort,
|
|
247
|
-
access,
|
|
248
|
-
maxTurns,
|
|
249
|
-
budgetUsd,
|
|
250
|
-
bashTimeoutSecs,
|
|
251
|
-
operadTools: operadTools
|
|
248
|
+
access: access.value,
|
|
249
|
+
maxTurns: maxTurns.value,
|
|
250
|
+
budgetUsd: budgetUsd.value,
|
|
251
|
+
bashTimeoutSecs: bashTimeoutSecs.value,
|
|
252
|
+
operadTools: operadTools.value,
|
|
252
253
|
mcpServers,
|
|
253
|
-
secretValues,
|
|
254
|
-
images: images.length === 0 ? undefined : images,
|
|
254
|
+
secretValues: secretValues.value,
|
|
255
|
+
images: images.value.length === 0 ? undefined : images.value,
|
|
256
|
+
skills: skills.value === undefined || skills.value.length === 0 ? undefined : skills.value,
|
|
257
|
+
useOwnSettings: useOwnSettings.value,
|
|
258
|
+
keep: keep.value,
|
|
255
259
|
},
|
|
256
260
|
};
|
|
257
261
|
}
|