@operad-io/harness-process 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 +1 -1
- package/package.json +4 -3
- package/src/acp.ts +4 -0
- package/src/agent-settings.ts +35 -0
- package/src/agent-step.ts +259 -0
- package/src/always-allow.ts +9 -56
- package/src/ask-call.ts +285 -10
- package/src/ask-note.ts +45 -1
- package/src/flows.ts +44 -0
- package/src/fold-lines.ts +85 -15
- package/src/inbox.ts +5 -3
- package/src/index.ts +14 -0
- package/src/jsonl-log.ts +48 -6
- package/src/keep.ts +646 -0
- package/src/limit.ts +1 -0
- package/src/lines.ts +225 -0
- package/src/probe-cache.ts +43 -0
- package/src/process.ts +689 -106
- package/src/question-form.ts +133 -0
- package/src/refusal.ts +47 -0
- package/src/skills.ts +232 -0
- package/src/step.ts +109 -27
- package/src/timings.ts +83 -0
- package/src/tool-events.ts +22 -0
- package/src/version.ts +49 -0
- package/src/wire.ts +205 -0
- package/src/worker-entry.ts +84 -0
- package/src/worker-step.ts +122 -0
package/src/lines.ts
CHANGED
|
@@ -9,6 +9,16 @@
|
|
|
9
9
|
export const TOOL_OUTPUT_CAP_BYTES = 64 * 1024;
|
|
10
10
|
|
|
11
11
|
export type SharedLine =
|
|
12
|
+
/**
|
|
13
|
+
* Written once when the harness session is ready, before the prompt goes to
|
|
14
|
+
* the model. Every harness writes this line type, so a reader can time the
|
|
15
|
+
* wait for the worker apart from the wait for the model.
|
|
16
|
+
*/
|
|
17
|
+
| {
|
|
18
|
+
readonly type: "session";
|
|
19
|
+
readonly sessionId?: string | null;
|
|
20
|
+
readonly model?: string | null;
|
|
21
|
+
}
|
|
12
22
|
| {
|
|
13
23
|
readonly type: "tool_execution_start";
|
|
14
24
|
readonly toolCallId: string;
|
|
@@ -29,6 +39,21 @@ export type SharedLine =
|
|
|
29
39
|
readonly error?: string;
|
|
30
40
|
}
|
|
31
41
|
| { readonly type: "blocked"; readonly toolName: string; readonly reason: string }
|
|
42
|
+
/**
|
|
43
|
+
* The harness is waiting before it asks the model again. `delayMs` is how
|
|
44
|
+
* long it waits, and `reason` is the provider's message that caused it.
|
|
45
|
+
*/
|
|
46
|
+
| {
|
|
47
|
+
readonly type: "retry_start";
|
|
48
|
+
readonly attempt: number;
|
|
49
|
+
readonly maxAttempts: number;
|
|
50
|
+
readonly delayMs: number;
|
|
51
|
+
readonly reason: string;
|
|
52
|
+
}
|
|
53
|
+
/** The retry is over: it succeeded, or it ran out and the failure follows. */
|
|
54
|
+
| { readonly type: "retry_end"; readonly success: boolean }
|
|
55
|
+
/** A line for the step log that neither the model nor the fold reads. */
|
|
56
|
+
| { readonly type: "log"; readonly text: string }
|
|
32
57
|
/**
|
|
33
58
|
* The program asks to use up a one-time permission before it lets a call
|
|
34
59
|
* through. The host writes the fact and answers on stdin with a
|
|
@@ -51,6 +76,17 @@ export type SharedLine =
|
|
|
51
76
|
readonly session: string | null;
|
|
52
77
|
}
|
|
53
78
|
| { readonly type: "ask_resolved"; readonly id: number }
|
|
79
|
+
/**
|
|
80
|
+
* The program asks the person the questions its agent asked, as one form,
|
|
81
|
+
* and holds the call until the host answers with an `AskFormAck` line.
|
|
82
|
+
* `session` is what a late answer continues.
|
|
83
|
+
*/
|
|
84
|
+
| {
|
|
85
|
+
readonly type: "ask_form";
|
|
86
|
+
readonly id: number;
|
|
87
|
+
readonly questions: readonly unknown[];
|
|
88
|
+
readonly session: string | null;
|
|
89
|
+
}
|
|
54
90
|
/** New assistant text only, not the text so far. */
|
|
55
91
|
| { readonly type: "text_delta"; readonly text: string }
|
|
56
92
|
/** New thinking only, not the thinking so far. */
|
|
@@ -119,6 +155,195 @@ export function maskSecrets(line: string, secrets: readonly string[]): string {
|
|
|
119
155
|
return masked;
|
|
120
156
|
}
|
|
121
157
|
|
|
158
|
+
/** The shortest time between two increment lines of one stream, in milliseconds. */
|
|
159
|
+
export const STREAM_GROUP_MS = 100;
|
|
160
|
+
|
|
161
|
+
/** The spellings `maskSecrets` masks for one value: the value and, if it differs, its JSON text. */
|
|
162
|
+
function spellingsOf(secret: string): readonly string[] {
|
|
163
|
+
if (secret.length === 0) return [];
|
|
164
|
+
const escaped = JSON.stringify(secret).slice(1, -1);
|
|
165
|
+
return escaped === secret ? [secret] : [secret, escaped];
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Splits an increment so that no line ends inside a secret: the longest suffix
|
|
170
|
+
* of `text` that is a proper prefix of a secret value is held back and joined
|
|
171
|
+
* to the next increment, where the completed secret is masked. A secret that
|
|
172
|
+
* is wholly in the emitted part is masked there. Both the value as written and
|
|
173
|
+
* its JSON-escaped spelling count, as `maskSecrets` masks both.
|
|
174
|
+
*/
|
|
175
|
+
export function splitSecretPrefix(
|
|
176
|
+
text: string,
|
|
177
|
+
secrets: readonly string[],
|
|
178
|
+
): { readonly emit: string; readonly held: string } {
|
|
179
|
+
let held = 0;
|
|
180
|
+
for (const secret of secrets.flatMap(spellingsOf)) {
|
|
181
|
+
const longest = Math.min(secret.length - 1, text.length);
|
|
182
|
+
for (let length = longest; length > held; length -= 1) {
|
|
183
|
+
if (text.endsWith(secret.slice(0, length))) {
|
|
184
|
+
held = length;
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
if (held === 0) return { emit: text, held: "" };
|
|
190
|
+
return { emit: text.slice(0, text.length - held), held: text.slice(text.length - held) };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export type IncrementKind = "text" | "thinking";
|
|
194
|
+
|
|
195
|
+
/** A line an increment writes: new text or thinking, or the full text that ends it. */
|
|
196
|
+
export interface IncrementLine {
|
|
197
|
+
readonly type: "text_delta" | "thinking_delta" | "text_end" | "thinking_end";
|
|
198
|
+
readonly text: string;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** Where increments are written. */
|
|
202
|
+
export interface IncrementSink {
|
|
203
|
+
line(line: IncrementLine): void;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export interface IncrementsOptions {
|
|
207
|
+
/**
|
|
208
|
+
* Write the first part of a stream as soon as it arrives. A harness that
|
|
209
|
+
* groups from the first part instead (claude) passes false: it writes when
|
|
210
|
+
* the next part comes after `STREAM_GROUP_MS`, or when the block ends.
|
|
211
|
+
*/
|
|
212
|
+
readonly firstImmediately?: boolean;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Assistant text and thinking as they stream. A line carries the new text
|
|
217
|
+
* only, and each stream writes at most one per `STREAM_GROUP_MS`: a message
|
|
218
|
+
* that arrives faster is grouped. Secret values are masked in every line, and
|
|
219
|
+
* a suffix that could start one is held back until the next part completes it
|
|
220
|
+
* or the stream ends.
|
|
221
|
+
*/
|
|
222
|
+
export class Increments {
|
|
223
|
+
#text = "";
|
|
224
|
+
#thinking = "";
|
|
225
|
+
#textAt: number | undefined;
|
|
226
|
+
#thinkingAt: number | undefined;
|
|
227
|
+
readonly #firstImmediately: boolean;
|
|
228
|
+
|
|
229
|
+
constructor(
|
|
230
|
+
private readonly io: IncrementSink,
|
|
231
|
+
private readonly now: () => number,
|
|
232
|
+
private readonly secrets: readonly string[],
|
|
233
|
+
options: IncrementsOptions = {},
|
|
234
|
+
) {
|
|
235
|
+
this.#firstImmediately = options.firstImmediately ?? true;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
text(delta: string): void {
|
|
239
|
+
if (delta.length === 0) return;
|
|
240
|
+
const at = this.now();
|
|
241
|
+
if (this.#firstImmediately) {
|
|
242
|
+
this.#text += delta;
|
|
243
|
+
if (this.#textAt === undefined || at - this.#textAt >= STREAM_GROUP_MS) this.#writeText(at, false);
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
if (this.#textAt !== undefined && at - this.#textAt >= STREAM_GROUP_MS) this.#writeText(at, false);
|
|
247
|
+
if (this.#textAt === undefined) this.#textAt = at;
|
|
248
|
+
this.#text += delta;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
thinking(delta: string): void {
|
|
252
|
+
if (delta.length === 0) return;
|
|
253
|
+
const at = this.now();
|
|
254
|
+
if (this.#firstImmediately) {
|
|
255
|
+
this.#thinking += delta;
|
|
256
|
+
if (this.#thinkingAt === undefined || at - this.#thinkingAt >= STREAM_GROUP_MS) {
|
|
257
|
+
this.#writeThinking(at, false);
|
|
258
|
+
}
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
if (this.#thinkingAt !== undefined && at - this.#thinkingAt >= STREAM_GROUP_MS) {
|
|
262
|
+
this.#writeThinking(at, false);
|
|
263
|
+
}
|
|
264
|
+
if (this.#thinkingAt === undefined) this.#thinkingAt = at;
|
|
265
|
+
this.#thinking += delta;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Writes what a stream has buffered. A complete flush writes a held suffix
|
|
270
|
+
* too, when the block ends and no later part can complete it; an incomplete
|
|
271
|
+
* one keeps the suffix that could start a secret.
|
|
272
|
+
*/
|
|
273
|
+
flush(kind: IncrementKind, complete: boolean): void {
|
|
274
|
+
if (kind === "text") {
|
|
275
|
+
if (this.#text.length === 0) {
|
|
276
|
+
if (complete) this.#textAt = undefined;
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
this.#writeText(undefined, complete);
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
if (this.#thinking.length === 0) {
|
|
283
|
+
if (complete) this.#thinkingAt = undefined;
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
this.#writeThinking(undefined, complete);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** A thinking block finished: its full text follows the increments. */
|
|
290
|
+
endThinking(text: string): void {
|
|
291
|
+
this.#thinking = "";
|
|
292
|
+
this.#thinkingAt = undefined;
|
|
293
|
+
if (text.length > 0) this.io.line({ type: "thinking_end", text: maskSecrets(text, this.secrets) });
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/** An assistant message finished: its full text follows the increments. */
|
|
297
|
+
endMessage(text: string): void {
|
|
298
|
+
if (text.length > 0) {
|
|
299
|
+
this.#text = "";
|
|
300
|
+
this.#textAt = undefined;
|
|
301
|
+
this.io.line({ type: "text_end", text: maskSecrets(text, this.secrets) });
|
|
302
|
+
} else if (this.#text.length > 0) {
|
|
303
|
+
this.#writeText(this.now(), false);
|
|
304
|
+
}
|
|
305
|
+
// A thinking block that ended wrote its own full text; anything left is
|
|
306
|
+
// a block the run stopped in, and the increments are all it has.
|
|
307
|
+
if (this.#thinking.length > 0) this.#writeThinking(this.now(), false);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* The stream is ending and no later text can complete a held prefix: write
|
|
312
|
+
* it now, masked when it holds a whole secret. Every end path calls this
|
|
313
|
+
* before its Result, so a stopped reply keeps what it had reached.
|
|
314
|
+
*/
|
|
315
|
+
release(): void {
|
|
316
|
+
if (this.#text.length > 0) {
|
|
317
|
+
this.io.line({ type: "text_delta", text: maskSecrets(this.#text, this.secrets) });
|
|
318
|
+
this.#text = "";
|
|
319
|
+
this.#textAt = undefined;
|
|
320
|
+
}
|
|
321
|
+
if (this.#thinking.length > 0) {
|
|
322
|
+
this.io.line({ type: "thinking_delta", text: maskSecrets(this.#thinking, this.secrets) });
|
|
323
|
+
this.#thinking = "";
|
|
324
|
+
this.#thinkingAt = undefined;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
#writeText(at: number | undefined, complete: boolean): void {
|
|
329
|
+
const { emit, held } = complete
|
|
330
|
+
? { emit: this.#text, held: "" }
|
|
331
|
+
: splitSecretPrefix(this.#text, this.secrets);
|
|
332
|
+
if (emit.length > 0) this.io.line({ type: "text_delta", text: maskSecrets(emit, this.secrets) });
|
|
333
|
+
this.#text = held;
|
|
334
|
+
this.#textAt = complete ? undefined : at ?? this.#textAt;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
#writeThinking(at: number | undefined, complete: boolean): void {
|
|
338
|
+
const { emit, held } = complete
|
|
339
|
+
? { emit: this.#thinking, held: "" }
|
|
340
|
+
: splitSecretPrefix(this.#thinking, this.secrets);
|
|
341
|
+
if (emit.length > 0) this.io.line({ type: "thinking_delta", text: maskSecrets(emit, this.secrets) });
|
|
342
|
+
this.#thinking = held;
|
|
343
|
+
this.#thinkingAt = complete ? undefined : at ?? this.#thinkingAt;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
122
347
|
/**
|
|
123
348
|
* Hands a line that was written to the log to the caller's receiver, with the
|
|
124
349
|
* secret values masked. A receiver that throws does not stop the step: the log
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A short-lived answer from a probe, shared by every caller that asks for the
|
|
3
|
+
* same key at the same time. Harnesses use it around a child process that is
|
|
4
|
+
* slow enough for two clients to ask at once, such as a login check.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export interface CachedProbeOptions {
|
|
8
|
+
/** The time now in milliseconds. */
|
|
9
|
+
readonly now: () => number;
|
|
10
|
+
/** How long one answer is kept. */
|
|
11
|
+
readonly ttlMs: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Creates a cache for one kind of probe. A fresh answer is returned without
|
|
16
|
+
* running the probe; a miss starts one probe per key, which concurrent callers
|
|
17
|
+
* share, and keeps its answer, including an absent one.
|
|
18
|
+
*/
|
|
19
|
+
export function createCachedProbe<T>(
|
|
20
|
+
options: CachedProbeOptions,
|
|
21
|
+
): (key: string, probe: () => Promise<T>) => Promise<T> {
|
|
22
|
+
const cache = new Map<string, { readonly at: number; readonly value: T }>();
|
|
23
|
+
const pending = new Map<string, Promise<T>>();
|
|
24
|
+
return (key, probe) => {
|
|
25
|
+
const kept = cache.get(key);
|
|
26
|
+
if (kept !== undefined && options.now() - kept.at < options.ttlMs) {
|
|
27
|
+
return Promise.resolve(kept.value);
|
|
28
|
+
}
|
|
29
|
+
const running = pending.get(key);
|
|
30
|
+
if (running !== undefined) return running;
|
|
31
|
+
const loading = Promise.resolve()
|
|
32
|
+
.then(probe)
|
|
33
|
+
.then((value) => {
|
|
34
|
+
cache.set(key, { at: options.now(), value });
|
|
35
|
+
return value;
|
|
36
|
+
})
|
|
37
|
+
.finally(() => {
|
|
38
|
+
pending.delete(key);
|
|
39
|
+
});
|
|
40
|
+
pending.set(key, loading);
|
|
41
|
+
return loading;
|
|
42
|
+
};
|
|
43
|
+
}
|