@operad-io/harness-claude 0.1.0 → 0.2.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/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 { readProfile } from "@operad-io/access-match";
7
- import { parseImageInputs, type ImageInput, type SharedLine } from "@operad-io/harness-process";
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
- import { mountedServersOf } from "@operad-io/harness-process";
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
- 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
- }
181
+ const unknown = readUnknownFields(value, INPUT_FIELDS);
182
+ if (!unknown.ok) return unknown;
163
183
 
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)) {
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 = text("model", false);
182
- if (model instanceof Error) return { ok: false, message: model.message };
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["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
- }
206
+ const secretValues = readSecrets(value);
207
+ if (!secretValues.ok) return secretValues;
198
208
 
199
- const images = parseImageInputs(value["images"]);
200
- if (typeof images === "string") return { ok: false, message: images };
209
+ const images = readImages(value);
210
+ if (!images.ok) return images;
201
211
 
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
- }
212
+ const skills = readStringList(value, "skills");
213
+ if (!skills.ok) return skills;
210
214
 
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
- }
215
+ const useOwnSettings = readBoolean(value, "useOwnSettings");
216
+ if (!useOwnSettings.ok) return useOwnSettings;
218
217
 
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
- }
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["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
- }
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 === undefined ? undefined : { home: String(operadTools["home"]) },
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
  }