@parcha/agentrun-kernel 0.0.0-stage → 0.1.0-beta.30

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.
Files changed (84) hide show
  1. package/CHANGELOG.md +181 -0
  2. package/LICENSE +202 -0
  3. package/NOTICE +6 -0
  4. package/README.md +63 -2
  5. package/dist/booking.d.ts +50 -0
  6. package/dist/booking.js +52 -0
  7. package/dist/delegate.d.ts +87 -0
  8. package/dist/delegate.js +143 -0
  9. package/dist/errors.d.ts +36 -0
  10. package/dist/errors.js +37 -0
  11. package/dist/host/alive.d.ts +2 -0
  12. package/dist/host/alive.js +28 -0
  13. package/dist/host/box-slot.d.ts +26 -0
  14. package/dist/host/box-slot.js +112 -0
  15. package/dist/host/box.d.ts +104 -0
  16. package/dist/host/box.js +16 -0
  17. package/dist/host/egress-proxy.d.ts +45 -0
  18. package/dist/host/egress-proxy.js +416 -0
  19. package/dist/host/egress-table.d.ts +24 -0
  20. package/dist/host/egress-table.js +165 -0
  21. package/dist/host/index.d.ts +16 -0
  22. package/dist/host/index.js +12 -0
  23. package/dist/host/overhead.d.ts +53 -0
  24. package/dist/host/overhead.js +75 -0
  25. package/dist/host/parent-death.d.ts +15 -0
  26. package/dist/host/parent-death.js +42 -0
  27. package/dist/host/parent-watch.d.ts +1 -0
  28. package/dist/host/parent-watch.js +38 -0
  29. package/dist/host/pinned-binary.d.ts +6 -0
  30. package/dist/host/pinned-binary.js +48 -0
  31. package/dist/host/proxy.d.ts +212 -0
  32. package/dist/host/proxy.js +808 -0
  33. package/dist/host/serial.d.ts +3 -0
  34. package/dist/host/serial.js +10 -0
  35. package/dist/index.d.ts +13 -0
  36. package/dist/index.js +7 -0
  37. package/dist/kernel/blobs.d.ts +16 -0
  38. package/dist/kernel/blobs.js +59 -0
  39. package/dist/kernel/drive.d.ts +40 -0
  40. package/dist/kernel/drive.js +239 -0
  41. package/dist/kernel/errors.d.ts +27 -0
  42. package/dist/kernel/errors.js +42 -0
  43. package/dist/kernel/follow-up.d.ts +67 -0
  44. package/dist/kernel/follow-up.js +146 -0
  45. package/dist/kernel/halt.d.ts +11 -0
  46. package/dist/kernel/halt.js +17 -0
  47. package/dist/kernel/index.d.ts +8 -0
  48. package/dist/kernel/index.js +5 -0
  49. package/dist/kernel/types.d.ts +129 -0
  50. package/dist/kernel/types.js +1 -0
  51. package/dist/process/builtins.d.ts +142 -0
  52. package/dist/process/builtins.js +207 -0
  53. package/dist/process/inbox.d.ts +29 -0
  54. package/dist/process/inbox.js +52 -0
  55. package/dist/process/index.d.ts +9 -0
  56. package/dist/process/index.js +11 -0
  57. package/dist/process/journal.d.ts +111 -0
  58. package/dist/process/journal.js +232 -0
  59. package/dist/process/records.d.ts +143 -0
  60. package/dist/process/records.js +142 -0
  61. package/dist/process/recovery.d.ts +76 -0
  62. package/dist/process/recovery.js +139 -0
  63. package/dist/process/settle.d.ts +22 -0
  64. package/dist/process/settle.js +38 -0
  65. package/dist/process/tail.d.ts +20 -0
  66. package/dist/process/tail.js +53 -0
  67. package/dist/process/tools.d.ts +105 -0
  68. package/dist/process/tools.js +120 -0
  69. package/dist/process/zod.d.ts +32 -0
  70. package/dist/process/zod.js +109 -0
  71. package/dist/runner.d.ts +154 -0
  72. package/dist/runner.js +559 -0
  73. package/dist/schema-check-cases.d.ts +7 -0
  74. package/dist/schema-check-cases.js +73 -0
  75. package/dist/schema-check.d.ts +3 -0
  76. package/dist/schema-check.js +88 -0
  77. package/dist/skill-files.d.ts +23 -0
  78. package/dist/skill-files.js +136 -0
  79. package/dist/testing.d.ts +91 -0
  80. package/dist/testing.js +196 -0
  81. package/dist/types.d.ts +256 -0
  82. package/dist/types.js +17 -0
  83. package/package.json +74 -4
  84. package/vendor/durable-harness/UPSTREAM.json +67 -0
@@ -0,0 +1,146 @@
1
+ import { foldSession, openDrive } from "./drive.js";
2
+ import { EffectFailure, EffectInterrupted } from "./errors.js";
3
+ /** The effect's key in the follow-up's own session. */
4
+ export const FOLLOW_UP_EFFECT = "follow-up";
5
+ /** What a request the reserve refuses says, first. */
6
+ export const FOLLOW_UP_BUDGET_REACHED = "follow_up_budget_reached";
7
+ /** What a request asked for after the follow-up was cancelled says, first. */
8
+ export const FOLLOW_UP_CANCELLED = "follow_up_cancelled";
9
+ /** The effect that records the follow-up's own cut turn settled: no later run settles another turn. */
10
+ const SETTLED_EFFECT = "follow-up.settled";
11
+ /** The run of each follow-up key in this process: a second run waits for it, never fences it. */
12
+ const runs = new WeakMap();
13
+ /** The follow-up's own session key, beside the session it follows up. */
14
+ export const followUpKey = (session, name) => `${session}:follow-up:${name}`;
15
+ /** The follow-up's state in its records, as written: `running` is one a process that died left without an outcome. A
16
+ * host reads it before it resumes an open turn: a turn of a `running` or `interrupted` follow-up is settled instead. */
17
+ export function followUpState(records) {
18
+ const effect = foldSession("", records).effects.get(FOLLOW_UP_EFFECT);
19
+ if (!effect)
20
+ return "none";
21
+ if (effect.status === "completed")
22
+ return "done";
23
+ if (effect.status === "failed")
24
+ return effect.error?.interrupted ? "interrupted" : "failed";
25
+ return "running";
26
+ }
27
+ export function followUpOnce(options) {
28
+ const { store, prompt, reserveUsd } = options;
29
+ const { worstUsd } = options;
30
+ if (typeof reserveUsd !== "number" || !Number.isFinite(reserveUsd) || reserveUsd <= 0)
31
+ throw new Error("reserveUsd must be a number of dollars above 0");
32
+ if (typeof worstUsd !== "number" || !Number.isFinite(worstUsd) || worstUsd < 0)
33
+ throw new Error("worstUsd must be given, a number of dollars, 0 or more");
34
+ const key = followUpKey(options.session, options.name);
35
+ // The id of the prompt's submission in the session: the follow-up's own, so a later run names it to the settle and a
36
+ // settle never withdraws input that is not the follow-up's.
37
+ const submission = `follow-up:${key}`;
38
+ let active = false;
39
+ let signalNow;
40
+ // The session's spentUsd when the follow-up's first request asks: the reserve counts from there. Whatever the session
41
+ // books after that counts against the reserve too: a task request whose cost was booked late (REQUEST_COST_LATE), and
42
+ // a harness's own background requests during the follow-up's turn (a CLI's small-model calls). Both refuse the
43
+ // follow-up sooner, never let it spend more.
44
+ let start;
45
+ const refuse = (why) => ({ refuse: `${FOLLOW_UP_BUDGET_REACHED}: ${why}` });
46
+ return {
47
+ key,
48
+ beforeRequest({ spentUsd, inFlight }) {
49
+ if (!active)
50
+ return undefined;
51
+ if (signalNow?.aborted)
52
+ return { refuse: `${FOLLOW_UP_CANCELLED}: the follow-up was cancelled; the request was not sent` };
53
+ if (spentUsd === null)
54
+ return refuse("what the session has spent is unknown, so the follow-up's reserve cannot be kept; the request was not sent");
55
+ start ??= spentUsd;
56
+ const used = spentUsd - start;
57
+ if (used + worstUsd * (inFlight.length + 1) > reserveUsd)
58
+ return refuse(`$${used.toFixed(6)} spent of a $${reserveUsd} reserve, and the next request could cost up to $${worstUsd}; the request was not sent`);
59
+ return undefined;
60
+ },
61
+ run(target, signal) {
62
+ const byKey = runs.get(store) ?? new Map();
63
+ runs.set(store, byKey);
64
+ const prior = byKey.get(key) ?? Promise.resolve();
65
+ // Waiting its turn, a run still hears its own signal: cancelled, it stops waiting and leaves the run before it alone.
66
+ const turn = new Promise((resolve, reject) => {
67
+ if (signal.aborted)
68
+ return reject(signal.reason);
69
+ const onAbort = () => reject(signal.reason);
70
+ signal.addEventListener("abort", onAbort, { once: true });
71
+ void prior.then(() => {
72
+ signal.removeEventListener("abort", onAbort);
73
+ resolve();
74
+ });
75
+ });
76
+ const run = turn.then(() => runOnce(target, signal));
77
+ // The next run of the key waits for the one before this, whether this one ran or gave up waiting.
78
+ const settled = prior.then(() => run.catch(() => undefined));
79
+ byKey.set(key, settled);
80
+ void settled.then(() => byKey.get(key) === settled && byKey.delete(key));
81
+ return run;
82
+ },
83
+ };
84
+ async function runOnce(target, signal) {
85
+ const drive = await openDrive({ writer: await store.open(key), signal });
86
+ const known = drive.state().effects.get(FOLLOW_UP_EFFECT);
87
+ if (known?.status === "completed")
88
+ return { status: "done" };
89
+ if (known?.status === "failed") {
90
+ if (!known.error?.interrupted)
91
+ return { status: "failed", error: known.error?.message ?? "failed" };
92
+ // Its own turn may still be open in the session: closed unsent, never resumed, and once. An open turn after that
93
+ // is another's, and is left alone.
94
+ if (drive.state().effects.get(SETTLED_EFFECT)?.status !== "completed") {
95
+ if (target.state === "open_turn")
96
+ await target.settleOpenTurn({ opening: submission });
97
+ await drive.effect(SETTLED_EFFECT, { name: SETTLED_EFFECT, replay: "safe", input: null }, async () => null);
98
+ }
99
+ return { status: "interrupted" };
100
+ }
101
+ if (target.state === "open_turn")
102
+ throw new Error("the session has an open turn that is not this follow-up's: recover it before the follow-up");
103
+ try {
104
+ await drive.effect(FOLLOW_UP_EFFECT, { name: options.name, replay: "never", input: { prompt } }, async () => {
105
+ if (signal.aborted)
106
+ throw signal.reason;
107
+ active = true;
108
+ signalNow = signal;
109
+ // A cancel stops the turn and ends the run at once: no outcome is written, so the next process reads the
110
+ // follow-up interrupted, and a turn that ends after it never answers done.
111
+ let onAbort = () => { };
112
+ const aborted = new Promise((_, reject) => {
113
+ onAbort = () => {
114
+ void target.interrupt().catch(() => undefined);
115
+ reject(signal.reason);
116
+ };
117
+ signal.addEventListener("abort", onAbort, { once: true });
118
+ });
119
+ aborted.catch(() => undefined);
120
+ try {
121
+ // A send the session refuses because it holds the id already cannot strand the follow-up: the effect is
122
+ // `never`, so a crash after the submission landed is recovered interrupted, and the prompt is never sent again.
123
+ await Promise.race([target.send(prompt, { id: submission }), aborted]);
124
+ const end = await Promise.race([target.turnEnd(), aborted]);
125
+ if (signal.aborted)
126
+ throw signal.reason;
127
+ if (end.status === "interrupted")
128
+ throw new EffectInterrupted(end.error ?? "the follow-up's turn was interrupted");
129
+ if (end.status !== "completed")
130
+ throw new Error(end.error ?? "the follow-up's turn failed");
131
+ return null;
132
+ }
133
+ finally {
134
+ active = false;
135
+ signal.removeEventListener("abort", onAbort);
136
+ }
137
+ });
138
+ return { status: "done" };
139
+ }
140
+ catch (error) {
141
+ if (error instanceof EffectFailure)
142
+ return error.error.interrupted ? { status: "interrupted" } : { status: "failed", error: error.error.message };
143
+ throw error;
144
+ }
145
+ }
146
+ }
@@ -0,0 +1,11 @@
1
+ import type { TurnStatus } from "../types.js";
2
+ /** The session's own stop of its drive: told apart from every other halt by identity, never by its text. */
3
+ export declare class SessionHalt extends Error {
4
+ readonly kind: "interrupted" | "closed";
5
+ constructor(kind: "interrupted" | "closed");
6
+ }
7
+ /** The outcome of a turn whose drive `signal` was aborted, the session's host being `host`. */
8
+ export declare function abortedOutcome(signal: AbortSignal, host?: AbortSignal): {
9
+ status: TurnStatus;
10
+ error?: string;
11
+ };
@@ -0,0 +1,17 @@
1
+ /** The session's own stop of its drive: told apart from every other halt by identity, never by its text. */
2
+ export class SessionHalt extends Error {
3
+ kind;
4
+ constructor(kind) {
5
+ super(kind);
6
+ this.kind = kind;
7
+ this.name = "SessionHalt";
8
+ }
9
+ }
10
+ /** The outcome of a turn whose drive `signal` was aborted, the session's host being `host`. */
11
+ export function abortedOutcome(signal, host) {
12
+ const reason = signal.reason;
13
+ if (reason instanceof SessionHalt || host?.aborted)
14
+ return { status: "interrupted" };
15
+ const message = reason instanceof Error ? reason.message : reason === undefined ? "the turn was halted, with no reason given" : String(reason);
16
+ return { status: "failed", error: message.slice(0, 500) };
17
+ }
@@ -0,0 +1,8 @@
1
+ export type { DriveContext, EffectError, EffectIO, EffectOptions, EffectRecord, EffectStatus, HostTool, HostToolIO, ToolOutput, KernelWorld, ReplayPolicy, SessionState, Submission, SubmissionKind } from "./types.js";
2
+ export { EffectFailure, EffectInterrupted, Fatal, Restart, Unavailable } from "./errors.js";
3
+ export { abortedOutcome, SessionHalt } from "./halt.js";
4
+ export { BLOB_PIECE_CHARS, BLOB_RECORD, blobStreamName, readBlobs, sha256Hex, writeBlob } from "./blobs.js";
5
+ export { foldSession, hostToolIO, INTERRUPTED_TEXT, KERNEL_RECORDS, openDrive, pendingSubmissions } from "./drive.js";
6
+ export type { Drive, OpenDriveOptions } from "./drive.js";
7
+ export { FOLLOW_UP_BUDGET_REACHED, FOLLOW_UP_CANCELLED, FOLLOW_UP_EFFECT, followUpKey, followUpOnce, followUpState } from "./follow-up.js";
8
+ export type { FollowUp, FollowUpOptions, FollowUpResult, FollowUpState, FollowUpTarget } from "./follow-up.js";
@@ -0,0 +1,5 @@
1
+ export { EffectFailure, EffectInterrupted, Fatal, Restart, Unavailable } from "./errors.js";
2
+ export { abortedOutcome, SessionHalt } from "./halt.js";
3
+ export { BLOB_PIECE_CHARS, BLOB_RECORD, blobStreamName, readBlobs, sha256Hex, writeBlob } from "./blobs.js";
4
+ export { foldSession, hostToolIO, INTERRUPTED_TEXT, KERNEL_RECORDS, openDrive, pendingSubmissions } from "./drive.js";
5
+ export { FOLLOW_UP_BUDGET_REACHED, FOLLOW_UP_CANCELLED, FOLLOW_UP_EFFECT, followUpKey, followUpOnce, followUpState } from "./follow-up.js";
@@ -0,0 +1,129 @@
1
+ import type { JsonValue, SessionWriter, ToolImage } from "../types.js";
2
+ export type ReplayPolicy = "safe" | "never";
3
+ export type SubmissionKind = "prompt" | "steer" | "followUp" | "withdraw";
4
+ /** One input a person sent the session, as the host recorded it. */
5
+ export interface Submission {
6
+ id: string;
7
+ kind: SubmissionKind;
8
+ /** What the person sent. In the session's state (foldSession), a prompt or follow-up after `carry` withdraws holds
9
+ * their words first (drive.ts `carrying`); the record keeps the person's own. */
10
+ text?: string;
11
+ /** For a withdraw: the submission it withdraws. */
12
+ target?: string;
13
+ /** For a withdraw of a prompt or follow-up: its text stays in the conversation, carried into the next prompt or
14
+ * follow-up, whose `text` in the session's state (foldSession) holds it first, pending or placed. A person's stop
15
+ * withdraws this way (settleTurn): what they asked is kept, never run on its own. */
16
+ carry?: true;
17
+ /** For a `carry` withdraw of a placed prompt or follow-up its CLI never recorded (process/settle.ts `unsent`):
18
+ * its text is carried as an unplaced one's is, and it stays placed, so nothing sends it on its own. */
19
+ unsent?: true;
20
+ receivedAt: Date;
21
+ }
22
+ export type EffectStatus = "pending" | "running" | "completed" | "failed";
23
+ export interface EffectError {
24
+ message: string;
25
+ /** The effect's drive went away before it reported an outcome: it may or may not have taken effect. */
26
+ interrupted?: boolean;
27
+ /** What it reported before it was interrupted. */
28
+ partial?: unknown[];
29
+ }
30
+ /** One effect as the session's records hold it: its intent, then at most one outcome. */
31
+ export interface EffectRecord {
32
+ key: string;
33
+ stepId: string;
34
+ name: string;
35
+ replay: ReplayPolicy;
36
+ input: unknown;
37
+ status: EffectStatus;
38
+ output?: unknown;
39
+ error?: EffectError;
40
+ /** 0 for the first run; one more each time a `safe` effect a crash cut off is run again. */
41
+ attempt: number;
42
+ startedAt?: Date;
43
+ /** Submissions this effect placed: taken out of `pending()` at its intent. */
44
+ placed: string[];
45
+ }
46
+ /** The session as its records fold. */
47
+ export interface SessionState {
48
+ runId: string;
49
+ status: "running";
50
+ submissions: Submission[];
51
+ placed: Set<string>;
52
+ withdrawn: Set<string>;
53
+ /** By key: the first intent for a key wins. */
54
+ effects: Map<string, EffectRecord>;
55
+ /** In the order their intents were written. */
56
+ effectOrder: EffectRecord[];
57
+ }
58
+ export interface EffectOptions {
59
+ name: string;
60
+ replay: ReplayPolicy;
61
+ input: unknown;
62
+ placed?: string[];
63
+ /** Where the effect must run and what it needs; recorded with the intent, not acted on by this kernel. */
64
+ requires?: string;
65
+ needs?: string[];
66
+ }
67
+ export interface EffectIO {
68
+ signal: AbortSignal;
69
+ progress(chunk: unknown): void;
70
+ stepId: string;
71
+ }
72
+ /** A blob store for records too large to keep inline: pieces of text, content-addressed, kept in the session's own
73
+ * records, written through its writer. */
74
+ export interface KernelWorld {
75
+ readonly store: SessionWriter;
76
+ }
77
+ /** What a copied harness drive gets: its session's state, effects that run at most once (or again, when `safe`, after a
78
+ * crash), its pending input, and its signal. One drive holds a session at a time: its writer is fenced once a newer
79
+ * drive opens, and the stale drive's next write is a `Restart`. */
80
+ export interface DriveContext {
81
+ readonly runId: string;
82
+ readonly world: KernelWorld;
83
+ readonly config: unknown;
84
+ state(): SessionState;
85
+ effect<T>(key: string, options: EffectOptions, fn: (io: EffectIO) => Promise<T>): Promise<T>;
86
+ isMemoized(key: string): boolean;
87
+ pending(): Submission[];
88
+ /** Re-folds this drive's records; it reads nothing. This drive is the session's only writer (a newer `open` fences
89
+ * it), and door input is already folded by `submit`, which also wakes `onHint`. A fenced drive learns it on its
90
+ * next append, not here. */
91
+ refresh(): Promise<void>;
92
+ live(event: unknown): void;
93
+ /** Called when new input may be waiting (a submission was recorded through this drive); returns the unsubscribe. */
94
+ onHint?(listener: (hint: {
95
+ eventType: "hook_received" | "reconnect";
96
+ }) => void): () => void;
97
+ readonly signal: AbortSignal;
98
+ halt?(reason: unknown): void;
99
+ /** How many times this session's drive has been rebuilt in this process (0 first). */
100
+ readonly attempt: number;
101
+ /** When this drive must have let go, on Date.now()'s clock, if it has a deadline. */
102
+ readonly deadline?: number;
103
+ }
104
+ /** A tool the host runs for the harness, as the copied tool bridge calls it. */
105
+ export interface HostTool<Input = any> {
106
+ name: string;
107
+ label?: string;
108
+ description: string;
109
+ inputSchema: object;
110
+ replay: ReplayPolicy;
111
+ /** Where the tool must run, and what it needs: passed to the effect's intent. */
112
+ requires?: string;
113
+ needs?: string[];
114
+ /** Its answer: the text, or the text with the images it shows. Recorded as the call's outcome, so a call answered
115
+ * from the record answers with the same images. */
116
+ execute(input: Input, io: HostToolIO): Promise<string | ToolOutput>;
117
+ }
118
+ /** A tool's answer with images. */
119
+ export interface ToolOutput {
120
+ text: string;
121
+ images?: readonly ToolImage[];
122
+ }
123
+ export interface HostToolIO {
124
+ signal: AbortSignal;
125
+ progress(chunk: unknown): void;
126
+ ctx: DriveContext;
127
+ stepId?: string;
128
+ }
129
+ export type { JsonValue };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,142 @@
1
+ import { type DriveContext, type HostToolIO, type ReplayPolicy } from '../kernel/index.js';
2
+ import { type ToolResult } from './tools.js';
3
+ /**
4
+ * A harness's own (built-in) tools, in the log.
5
+ *
6
+ * With its built-in tools on, a harness runs tools the drive never sees run:
7
+ * Codex's shell and `apply_patch` run inside app-server. The drive sees
8
+ * them through the harness's own hooks: one before a call runs (a Codex
9
+ * approval request), one after it (`item/completed`). `BuiltinCalls` makes that pair the kernel's effect
10
+ * sandwich for the call, keyed by the harness's own call id
11
+ * (`tool:<callId>`, step name `tool.<name>`, like a bridged tool):
12
+ *
13
+ * - `begin` commits the call's intent and resolves once it is committed
14
+ * (for a `never` call: durable, after the harness's own record of the call
15
+ * is in the log, `beforeNever`). Only then does the hook let the call run.
16
+ * - `end` commits its outcome: the result the harness reports (clipped: the
17
+ * full result is in the harness's own transcript, which the log carries).
18
+ *
19
+ * What that buys, and what it does not:
20
+ *
21
+ * - Every built-in call that ran has an intent in the log first. After a
22
+ * crash, the recovery note lists each call of the turn with what the log
23
+ * knows: completed (its result), failed, or interrupted, "may or may not
24
+ * have taken effect" (`never`) / "safe to repeat" (`safe`).
25
+ * - Nothing reruns a built-in call by itself. The harness resumes its own
26
+ * session, which answers a call that was in flight "interrupted, outcome
27
+ * unknown" (Codex gets it injected).
28
+ * A harness that dispatches the same call id again gets the log's answer
29
+ * (`begin` returns it) and must not run it. For a built-in, `safe` only
30
+ * changes the note's wording: the model decides what to repeat.
31
+ * - What a call did is not in the log. The working directory is a disk on
32
+ * the machine that drives the session (see `LocalWorkspace`), and a
33
+ * process a tool started does not outlive the drive (`ProcessHost`'s
34
+ * group registry kills it).
35
+ */
36
+ export interface BuiltinCall {
37
+ /** The harness's own id for the call (stable across a resume of its session: it is the memo key). */
38
+ callId: string;
39
+ /** The harness's tool name (`Bash`, `shell`, `edit`, ...). */
40
+ name: string;
41
+ input: unknown;
42
+ /** Default: `BuiltinCallsOptions.replay(name)`. */
43
+ replay?: ReplayPolicy;
44
+ }
45
+ export interface BuiltinCallsOptions {
46
+ /** A built-in's replay policy, by name. Default `never`. */
47
+ replay?: (name: string) => ReplayPolicy;
48
+ /**
49
+ * Runs before the intent of a `never` call: the native journal's barrier,
50
+ * so the harness's own record of the call is in the log before its intent.
51
+ */
52
+ beforeNever?: (call: BuiltinCall) => Promise<void>;
53
+ /** Publish `tool_start` / `tool_end` on `ctx.live`. Default true. */
54
+ live?: boolean;
55
+ /** How much of a result the step records. Default 8000 characters. */
56
+ outputChars?: number;
57
+ /**
58
+ * Run each call's effect body inside this wrapper: `run` lets the call go
59
+ * (`begin` resolves once the wrapper calls it) and resolves with the
60
+ * result the harness reports (`end`), or rejects with its failure. What
61
+ * the wrapper returns is the call's recorded outcome, and what it throws
62
+ * its failure. For a harness whose built-ins work on files the drive
63
+ * keeps: sync the files in before `run`, and commit what the call changed
64
+ * with its outcome. A wrapper that throws before
65
+ * calling `run` fails the call: `begin` answers with that failure, and the
66
+ * call never runs. One that returns without calling `run` decides the
67
+ * outcome itself: `begin` answers with it, and the call never runs either
68
+ * (nothing on the live stream). One that throws after `run` fails the call
69
+ * although the harness ran it (the step records the failure, `end`
70
+ * resolves). Calling `run` again returns the first call's promise. Default:
71
+ * none (the call runs as soon as its intent is committed).
72
+ */
73
+ around?: (call: BuiltinCall, run: () => Promise<string>, io: HostToolIO) => Promise<string>;
74
+ }
75
+ /** What `begin` says the hook should do. */
76
+ export type BuiltinStart =
77
+ /** The intent is committed: let the call run. */
78
+ {
79
+ run: true;
80
+ }
81
+ /** The log already has an outcome for this call id: do not run it, answer with this. */
82
+ | {
83
+ run: false;
84
+ result: ToolResult;
85
+ };
86
+ /**
87
+ * The built-in calls of one drive. Build one per `drive(ctx)`.
88
+ *
89
+ * A call the drive leaves open (the harness's process died mid-call, the
90
+ * drive stopped) is dealt with by `close`: interrupted if the drive is
91
+ * still live, else left to the kernel (a stopped drive's intent is released
92
+ * or recovered as interrupted by the next delivery).
93
+ */
94
+ export declare class BuiltinCalls {
95
+ private readonly ctx;
96
+ private readonly options;
97
+ private readonly open;
98
+ /** Each call id's first `begin`, reserved before it awaits anything. */
99
+ private readonly begun;
100
+ private readonly replay;
101
+ private readonly live;
102
+ private readonly outputChars;
103
+ constructor(ctx: DriveContext, options?: BuiltinCallsOptions);
104
+ /** Calls begun and not ended. */
105
+ get size(): number;
106
+ /** Whether this call is open in this drive. */
107
+ has(callId: string): boolean;
108
+ /**
109
+ * The harness is about to run `call`: commit its intent. Resolves once the
110
+ * intent is committed (`{ run: true }`), or at once with the log's answer
111
+ * when the call id already has an outcome (`{ run: false }`). Rejects if
112
+ * the intent cannot be committed (the drive lost the session, a write
113
+ * failed): the drive is halted, and the hook must refuse the call.
114
+ */
115
+ begin(call: BuiltinCall): Promise<BuiltinStart>;
116
+ /** The same call begun again: never run it twice. Wait for the first begin, then for its outcome, from the log. */
117
+ private again;
118
+ private beginOnce;
119
+ /**
120
+ * The call ended: commit its outcome. `isError` (the tool reported a
121
+ * failure) commits a failed step with `text` as its error. Resolves once
122
+ * the outcome is committed (or the drive stopped first). A call this
123
+ * drive never began (a hook that fired without its `begin`) is begun and
124
+ * ended here: recorded after the fact.
125
+ */
126
+ end(callId: string, result: {
127
+ name?: string;
128
+ input?: unknown;
129
+ text: string;
130
+ isError?: boolean;
131
+ }): Promise<void>;
132
+ /**
133
+ * The harness can no longer report these calls (its process exited mid-call):
134
+ * commit each open call as interrupted, "may or may not have taken effect".
135
+ * A no-op once the drive stopped (the kernel resolves its intents).
136
+ */
137
+ interruptAll(reason?: string): Promise<void>;
138
+ /** Wait for every open call to settle, or `ms` to pass. */
139
+ settled(ms: number): Promise<void>;
140
+ }
141
+ /** A tool name list as a replay classifier: these are `safe`, the rest `never`. */
142
+ export declare function replayBy(safe: Iterable<string>): (name: string) => ReplayPolicy;