@operad-io/harness-process 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/access-user.schema.json +52 -0
- package/package.json +22 -4
- package/src/acp.ts +370 -0
- package/src/always-allow.ts +203 -0
- package/src/ask-call.ts +251 -0
- package/src/ask-note.ts +47 -0
- package/src/event-note.ts +23 -0
- package/src/fold-lines.ts +238 -0
- package/src/grants.ts +53 -0
- package/src/images.ts +43 -0
- package/src/inbox.ts +45 -0
- package/src/index.ts +23 -0
- package/src/jsonl-log.ts +71 -0
- package/src/limit.ts +89 -0
- package/src/lines.ts +138 -0
- package/src/operad-tools.ts +57 -0
- package/src/process.ts +568 -0
- package/src/step.ts +293 -0
- package/src/usage.ts +62 -0
- package/README.md +0 -3
package/src/step.ts
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the hub side of every process harness does the same way: read the
|
|
3
|
+
* step's params, check the directory, read the worker's `result` line and turn
|
|
4
|
+
* the run's outcome into a step result.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { stat } from "node:fs/promises";
|
|
8
|
+
import type { AgentResultData, PluginStepResult } from "@operad-io/engine";
|
|
9
|
+
import { MAX_LINE_CHARS, type AgentRunOutcome } from "./process.ts";
|
|
10
|
+
import { standardUsage, type RawCounts, type UsageSource } from "./usage.ts";
|
|
11
|
+
import { isRecord } from "@operad-io/engine";
|
|
12
|
+
|
|
13
|
+
/** How many of the worker's last stderr lines a failure reason carries. */
|
|
14
|
+
const REASON_STDERR_LINES = 5;
|
|
15
|
+
|
|
16
|
+
/** The `result` line every worker program writes last; a harness adds its own fields. */
|
|
17
|
+
export interface AgentWorkerResult {
|
|
18
|
+
readonly type: "result";
|
|
19
|
+
readonly result: "done" | "failed" | "cancelled";
|
|
20
|
+
readonly sessionId: string | null;
|
|
21
|
+
readonly usage: RawCounts;
|
|
22
|
+
/** `quality` is `estimated` for an API-price figure; `incomplete` when some message had no price. */
|
|
23
|
+
readonly cost: {
|
|
24
|
+
readonly micros: number;
|
|
25
|
+
readonly quality?: "estimated" | "exact";
|
|
26
|
+
readonly incomplete?: true;
|
|
27
|
+
} | null;
|
|
28
|
+
readonly finalMessage: string | null;
|
|
29
|
+
/** The model that did the work, when the worker reports it. */
|
|
30
|
+
readonly model?: string | null;
|
|
31
|
+
readonly reason?: string;
|
|
32
|
+
readonly message?: string;
|
|
33
|
+
/** Set when the worker saw a rate limit: it is a usage limit whatever the text says. */
|
|
34
|
+
readonly modelError?: { readonly kind: string };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function text(value: unknown, at: string): string {
|
|
38
|
+
if (typeof value !== "string" || value.trim().length === 0) {
|
|
39
|
+
throw new Error(`${at}: must be a non-empty string`);
|
|
40
|
+
}
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function optionalText(value: unknown, at: string): string | undefined {
|
|
45
|
+
return value === undefined ? undefined : text(value, at);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function secs(value: unknown, at: string): number {
|
|
49
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) {
|
|
50
|
+
throw new Error(`${at}: must be a positive number of seconds`);
|
|
51
|
+
}
|
|
52
|
+
return value;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function stringListParam(value: unknown, at: string): readonly string[] {
|
|
56
|
+
if (value === undefined) return [];
|
|
57
|
+
if (!Array.isArray(value) || !value.every((item) => typeof item === "string")) {
|
|
58
|
+
throw new Error(`${at}: must be an array of strings`);
|
|
59
|
+
}
|
|
60
|
+
return value.filter((item): item is string => typeof item === "string");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** The spelling of one harness's step `args`. */
|
|
64
|
+
export interface StepFlagSpec<Level extends string> {
|
|
65
|
+
/** The flag that sets the reasoning level, such as `--effort`. */
|
|
66
|
+
readonly levelFlag: string;
|
|
67
|
+
readonly levels: readonly Level[];
|
|
68
|
+
/** The model value as the usage line shows it, such as `<name>`. */
|
|
69
|
+
readonly modelHint: string;
|
|
70
|
+
/** An error for a model value this harness does not take; undefined when it takes it. */
|
|
71
|
+
readonly modelProblem?: (value: string) => string | undefined;
|
|
72
|
+
/** True when a value may start with `-`, so the model check names it; otherwise a flag after a flag is "no value". */
|
|
73
|
+
readonly dashValues?: boolean;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Reads `--model <name>` and the level flag from the step's `args`. Anything
|
|
78
|
+
* else is an error message naming the entry: a flag silently ignored would hide
|
|
79
|
+
* a wrong model.
|
|
80
|
+
*/
|
|
81
|
+
export function parseStepFlags<Level extends string>(
|
|
82
|
+
args: readonly string[],
|
|
83
|
+
spec: StepFlagSpec<Level>,
|
|
84
|
+
): { readonly model?: string; readonly level?: Level } | string {
|
|
85
|
+
let model: string | undefined;
|
|
86
|
+
let level: Level | undefined;
|
|
87
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
88
|
+
const entry = args[index] ?? "";
|
|
89
|
+
if (entry !== "--model" && entry !== spec.levelFlag) {
|
|
90
|
+
return `args: unknown entry "${entry}"; expected --model ${spec.modelHint} or ${spec.levelFlag} <level>`;
|
|
91
|
+
}
|
|
92
|
+
const value = args[index + 1];
|
|
93
|
+
// A flag that follows is not a value: `--model --effort high` has none.
|
|
94
|
+
if (value === undefined || (spec.dashValues !== true && value.startsWith("-"))) return `args: "${entry}" has no value`;
|
|
95
|
+
index += 1;
|
|
96
|
+
if (entry === "--model") {
|
|
97
|
+
if (model !== undefined) return `args: "--model" is given twice`;
|
|
98
|
+
if (value.trim().length === 0) return `args: "--model" has no value`;
|
|
99
|
+
const problem = spec.modelProblem?.(value);
|
|
100
|
+
if (problem !== undefined) return problem;
|
|
101
|
+
model = value;
|
|
102
|
+
} else {
|
|
103
|
+
if (level !== undefined) return `args: "${spec.levelFlag}" is given twice`;
|
|
104
|
+
const known = spec.levels.find((candidate) => candidate === value);
|
|
105
|
+
if (known === undefined) {
|
|
106
|
+
return `args: "${spec.levelFlag} ${value}": the value must be one of ${spec.levels.join(", ")}`;
|
|
107
|
+
}
|
|
108
|
+
level = known;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return {
|
|
112
|
+
...(model === undefined ? {} : { model }),
|
|
113
|
+
...(level === undefined ? {} : { level }),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export async function directoryError(cwd: string): Promise<string | undefined> {
|
|
118
|
+
try {
|
|
119
|
+
const info = await stat(cwd);
|
|
120
|
+
return info.isDirectory() ? undefined : `cwd is not a directory: ${cwd}`;
|
|
121
|
+
} catch {
|
|
122
|
+
return `cwd does not exist: ${cwd}`;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Reads a `result` line of the worker, or undefined for any other line. */
|
|
127
|
+
export function parseResult<Result extends AgentWorkerResult>(line: string): Result | undefined {
|
|
128
|
+
let value: unknown;
|
|
129
|
+
try {
|
|
130
|
+
value = JSON.parse(line);
|
|
131
|
+
} catch {
|
|
132
|
+
return undefined;
|
|
133
|
+
}
|
|
134
|
+
if (!isRecord(value) || value.type !== "result") return undefined;
|
|
135
|
+
const name = value.result;
|
|
136
|
+
if (name !== "done" && name !== "failed" && name !== "cancelled") return undefined;
|
|
137
|
+
return value as unknown as Result;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const UNAVAILABLE = /\b429\b|\b(?:503|529)\b|overloaded|rate[ _-]?limit|too many requests/i;
|
|
141
|
+
const USAGE_LIMIT = /usage[ _-]?limit|quota|exceeded your (?:current )?(?:usage|plan|quota)|insufficient[ _-]credit|out of credits/i;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* The `modelError` of a worker failure with reason `model_error`, in the shape
|
|
145
|
+
* `radian` gives. `kind` comes from the message: a usage limit is
|
|
146
|
+
* `usage_limit`, a 429 or an overloaded provider is `unavailable`, anything else
|
|
147
|
+
* is `protocol`. A usage-limit message wins over a 429 in the same text.
|
|
148
|
+
*/
|
|
149
|
+
export function modelErrorOf(message: string): Record<string, unknown> {
|
|
150
|
+
const status = /\b(429|503|529)\b/.exec(message);
|
|
151
|
+
let kind: "unavailable" | "usage_limit" | "protocol" = "protocol";
|
|
152
|
+
if (USAGE_LIMIT.test(message)) kind = "usage_limit";
|
|
153
|
+
else if (UNAVAILABLE.test(message)) kind = "unavailable";
|
|
154
|
+
return {
|
|
155
|
+
kind,
|
|
156
|
+
status: status === null ? null : Number(status[1]),
|
|
157
|
+
retry_after_secs: null,
|
|
158
|
+
message,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export function agentData(
|
|
163
|
+
who: UsageSource,
|
|
164
|
+
outcome: AgentRunOutcome,
|
|
165
|
+
result: AgentWorkerResult | undefined,
|
|
166
|
+
log: string,
|
|
167
|
+
durationMs: number,
|
|
168
|
+
): AgentResultData {
|
|
169
|
+
const cost =
|
|
170
|
+
result?.cost === null || result?.cost === undefined
|
|
171
|
+
? null
|
|
172
|
+
: {
|
|
173
|
+
micros: result.cost.micros,
|
|
174
|
+
incomplete: result.cost.incomplete === true,
|
|
175
|
+
estimated: result.cost.quality === "estimated",
|
|
176
|
+
};
|
|
177
|
+
return {
|
|
178
|
+
exitCode: outcome.exitCode,
|
|
179
|
+
signal: outcome.signal,
|
|
180
|
+
sessionId: result?.sessionId ?? null,
|
|
181
|
+
finalMessage: result?.finalMessage ?? null,
|
|
182
|
+
usage: standardUsage(who, result?.usage, cost),
|
|
183
|
+
log,
|
|
184
|
+
durationMs,
|
|
185
|
+
stderr: outcome.stderrTail,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function failure(
|
|
190
|
+
who: UsageSource,
|
|
191
|
+
kind: "failed" | "interrupted",
|
|
192
|
+
outcome: AgentRunOutcome,
|
|
193
|
+
result: AgentWorkerResult | undefined,
|
|
194
|
+
log: string,
|
|
195
|
+
durationMs: number,
|
|
196
|
+
extra: Readonly<Record<string, unknown>>,
|
|
197
|
+
): PluginStepResult {
|
|
198
|
+
return {
|
|
199
|
+
result: kind,
|
|
200
|
+
data: { ...agentData(who, outcome, result, log, durationMs), ...extra },
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function resultFor(
|
|
205
|
+
who: UsageSource,
|
|
206
|
+
outcome: AgentRunOutcome,
|
|
207
|
+
result: AgentWorkerResult | undefined,
|
|
208
|
+
log: string,
|
|
209
|
+
durationMs: number,
|
|
210
|
+
limits: { readonly wallTimeSecs: number; readonly stallSecs: number },
|
|
211
|
+
command: string,
|
|
212
|
+
): PluginStepResult {
|
|
213
|
+
const fail = (extra: Readonly<Record<string, unknown>>): PluginStepResult =>
|
|
214
|
+
failure(who, "failed", outcome, result, log, durationMs, extra);
|
|
215
|
+
|
|
216
|
+
if (outcome.cancelled) {
|
|
217
|
+
return failure(who, "interrupted", outcome, result, log, durationMs, {
|
|
218
|
+
reason: "cancelled",
|
|
219
|
+
message: "the harness run was cancelled",
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
if (outcome.missing) {
|
|
223
|
+
return fail({ reason: "missing", message: `${command} was not found on PATH` });
|
|
224
|
+
}
|
|
225
|
+
if (outcome.spawnError !== undefined) {
|
|
226
|
+
return fail({ reason: "spawn", message: outcome.spawnError });
|
|
227
|
+
}
|
|
228
|
+
if (outcome.wall) {
|
|
229
|
+
return fail({
|
|
230
|
+
reason: "wall",
|
|
231
|
+
message: `the harness ran longer than ${limits.wallTimeSecs}s`,
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
if (outcome.stall) {
|
|
235
|
+
return fail({
|
|
236
|
+
reason: "stall",
|
|
237
|
+
message: `the harness printed no new line for ${limits.stallSecs}s`,
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
if (outcome.lineTooLong) {
|
|
241
|
+
return fail({
|
|
242
|
+
reason: "line_too_long",
|
|
243
|
+
message: `the harness printed a line longer than ${MAX_LINE_CHARS} characters`,
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (result === undefined) {
|
|
248
|
+
const tail = outcome.stderrTail.slice(-REASON_STDERR_LINES).join(" | ");
|
|
249
|
+
const detail = tail.length === 0 ? "" : `: ${tail}`;
|
|
250
|
+
// A signal nobody here sent (kill -9, an out-of-memory kill) is an
|
|
251
|
+
// interruption of the run, not a verdict of the agent.
|
|
252
|
+
if (outcome.signal !== null) {
|
|
253
|
+
return failure(who, "interrupted", outcome, result, log, durationMs, {
|
|
254
|
+
reason: "signal",
|
|
255
|
+
message: `the harness was killed by ${outcome.signal} without a Result line${detail}`,
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
return fail({
|
|
259
|
+
reason: "no_result",
|
|
260
|
+
message: `the harness exited ${String(outcome.exitCode)} without a Result line${detail}`,
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (result.result === "cancelled") {
|
|
265
|
+
return failure(who, "interrupted", outcome, result, log, durationMs, {
|
|
266
|
+
reason: "cancelled",
|
|
267
|
+
message: "the harness run was cancelled",
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
if (result.result === "failed") {
|
|
271
|
+
const message = result.message ?? "the harness failed";
|
|
272
|
+
// A rate limit the worker saw is a usage limit whatever the text says.
|
|
273
|
+
const modelError =
|
|
274
|
+
result.modelError !== undefined
|
|
275
|
+
? { kind: result.modelError.kind, status: null, retry_after_secs: null, message }
|
|
276
|
+
: modelErrorOf(message);
|
|
277
|
+
return fail({
|
|
278
|
+
reason: result.reason ?? "exit",
|
|
279
|
+
message,
|
|
280
|
+
...(result.reason === "model_error" ? { modelError } : {}),
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
if (outcome.exitCode !== 0) {
|
|
284
|
+
return fail({
|
|
285
|
+
reason: "exit",
|
|
286
|
+
message: `the harness exited ${String(outcome.exitCode)} after a done Result`,
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
return {
|
|
290
|
+
result: "done",
|
|
291
|
+
data: agentData(who, outcome, result, log, durationMs) as unknown as Record<string, unknown>,
|
|
292
|
+
};
|
|
293
|
+
}
|
package/src/usage.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/** Builds the engine's standard `Usage` from the counts a harness reads. */
|
|
2
|
+
|
|
3
|
+
import type { CostQuality, Usage } from "@operad-io/engine";
|
|
4
|
+
|
|
5
|
+
/** Who ran: the harness plugin and the model its step named, if it named one. */
|
|
6
|
+
export interface UsageSource {
|
|
7
|
+
readonly harness: string;
|
|
8
|
+
readonly model: string | null;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** The counts as a harness reads them from its agent; an absent count is 0. */
|
|
12
|
+
export interface RawCounts {
|
|
13
|
+
readonly input?: number | undefined;
|
|
14
|
+
readonly output?: number | undefined;
|
|
15
|
+
readonly cache_read?: number | undefined;
|
|
16
|
+
readonly cache_write?: number | undefined;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** The cost as a harness reads it; `micros` is the part of the run it priced. */
|
|
20
|
+
export interface RawCost {
|
|
21
|
+
readonly micros: number;
|
|
22
|
+
readonly incomplete?: boolean | undefined;
|
|
23
|
+
readonly estimated?: boolean | undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function count(value: number | undefined): number {
|
|
27
|
+
return typeof value === "number" && Number.isFinite(value) && value >= 0
|
|
28
|
+
? Math.round(value)
|
|
29
|
+
: 0;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** A null `cost` is an unknown cost; a `micros` of 0 is a run known to be free. */
|
|
33
|
+
export function standardUsage(
|
|
34
|
+
source: UsageSource,
|
|
35
|
+
counts: RawCounts | undefined,
|
|
36
|
+
cost: RawCost | null | undefined,
|
|
37
|
+
): Usage {
|
|
38
|
+
const known =
|
|
39
|
+
cost !== null &&
|
|
40
|
+
cost !== undefined &&
|
|
41
|
+
Number.isFinite(cost.micros) &&
|
|
42
|
+
cost.micros >= 0;
|
|
43
|
+
const quality: CostQuality | null = !known
|
|
44
|
+
? null
|
|
45
|
+
: cost.incomplete === true
|
|
46
|
+
? "incomplete"
|
|
47
|
+
: cost.estimated === true
|
|
48
|
+
? "estimated"
|
|
49
|
+
: "exact";
|
|
50
|
+
return {
|
|
51
|
+
harness: source.harness,
|
|
52
|
+
model: source.model,
|
|
53
|
+
tokens: {
|
|
54
|
+
input: count(counts?.input),
|
|
55
|
+
output: count(counts?.output),
|
|
56
|
+
cacheread: count(counts?.cache_read),
|
|
57
|
+
cachewrite: count(counts?.cache_write),
|
|
58
|
+
},
|
|
59
|
+
costMicros: known ? Math.round(cost.micros) : null,
|
|
60
|
+
costQuality: quality,
|
|
61
|
+
};
|
|
62
|
+
}
|
package/README.md
DELETED