jackline 0.0.0-stage → 0.1.0-alpha.2
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/dist/approvals.d.ts +62 -0
- package/dist/approvals.js +60 -0
- package/dist/approvals.js.map +1 -0
- package/dist/errors.d.ts +125 -0
- package/dist/errors.js +172 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/journal.d.ts +44 -0
- package/dist/journal.js +47 -0
- package/dist/journal.js.map +1 -0
- package/dist/mcp.d.ts +56 -0
- package/dist/mcp.js +66 -0
- package/dist/mcp.js.map +1 -0
- package/dist/mock.d.ts +38 -0
- package/dist/mock.js +69 -0
- package/dist/mock.js.map +1 -0
- package/dist/models.d.ts +76 -0
- package/dist/models.js +163 -0
- package/dist/models.js.map +1 -0
- package/dist/native.d.ts +17 -0
- package/dist/native.js +61 -0
- package/dist/native.js.map +1 -0
- package/dist/policy.d.ts +144 -0
- package/dist/policy.js +129 -0
- package/dist/policy.js.map +1 -0
- package/dist/run.d.ts +594 -0
- package/dist/run.js +1168 -0
- package/dist/run.js.map +1 -0
- package/dist/schema.d.ts +18 -0
- package/dist/schema.js +104 -0
- package/dist/schema.js.map +1 -0
- package/native/index.d.ts +140 -0
- package/native/index.js +783 -0
- package/package.json +78 -4
- package/README.md +0 -3
package/dist/run.d.ts
ADDED
|
@@ -0,0 +1,594 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runs, handles, tools, capabilities, budgets and the effect protocol.
|
|
3
|
+
*
|
|
4
|
+
* Every interaction with the outside world goes through `Run`. Each one is
|
|
5
|
+
* proposed to the kernel, which checks it (capability, policy, budget) and
|
|
6
|
+
* records it. It runs only if allowed, and its outcome is recorded with
|
|
7
|
+
* labels. In replay, nothing is executed: recorded outcomes (and refusals)
|
|
8
|
+
* are returned.
|
|
9
|
+
*
|
|
10
|
+
* This is a port of `python/jackline/_run.py`. Where it deliberately differs,
|
|
11
|
+
* the comment says so; docs/typescript/api.md lists every difference.
|
|
12
|
+
*/
|
|
13
|
+
import { inspect } from "node:util";
|
|
14
|
+
import { type ApprovalRequest, type Approver, type Decision, type Receipt } from "./approvals.js";
|
|
15
|
+
import { Divergence } from "./errors.js";
|
|
16
|
+
import { type JournalLike } from "./journal.js";
|
|
17
|
+
import { type NativeKernel } from "./native.js";
|
|
18
|
+
import { type Constraint, type Label, type LabelSpec, type Policy, type Readers } from "./policy.js";
|
|
19
|
+
import { type JsonSchema } from "./schema.js";
|
|
20
|
+
export type EffectClass = "read_only" | "idempotent" | "irreversible";
|
|
21
|
+
type Json = Record<string, unknown>;
|
|
22
|
+
/** Unwinds the agent when a human's decision is needed and the approver chose to pause. */
|
|
23
|
+
declare class Paused extends Error {
|
|
24
|
+
readonly request: ApprovalRequest;
|
|
25
|
+
constructor(request: ApprovalRequest);
|
|
26
|
+
}
|
|
27
|
+
/** Unwinds the agent when an irreversible call's outcome is unknown after a crash. */
|
|
28
|
+
declare class InDoubtSignal extends Error {
|
|
29
|
+
readonly info: InDoubtCall;
|
|
30
|
+
constructor(info: InDoubtCall);
|
|
31
|
+
}
|
|
32
|
+
/** An irreversible call a crash left unsettled. Settle it with `resolve`. */
|
|
33
|
+
export interface InDoubtCall {
|
|
34
|
+
identity: string;
|
|
35
|
+
target: string;
|
|
36
|
+
args: Record<string, unknown>;
|
|
37
|
+
idempotencyKey: string;
|
|
38
|
+
}
|
|
39
|
+
/** Everything a run and its sub-runs share. */
|
|
40
|
+
declare class RunState {
|
|
41
|
+
readonly kernel: NativeKernel;
|
|
42
|
+
readonly runId: string;
|
|
43
|
+
readonly key: string;
|
|
44
|
+
readonly mode: "live" | "replay" | "resume";
|
|
45
|
+
readonly approver: Approver | null;
|
|
46
|
+
/** A pause or in-doubt signal. Sticky: agent code that catches it can't continue. */
|
|
47
|
+
interrupt: Paused | InDoubtSignal | null;
|
|
48
|
+
/** Sticky as well: a replay that diverged stays diverged. */
|
|
49
|
+
divergence: Divergence | null;
|
|
50
|
+
private readonly counters;
|
|
51
|
+
private readonly tails;
|
|
52
|
+
constructor(kernel: NativeKernel, runId: string, key: string, mode: "live" | "replay" | "resume", approver: Approver | null);
|
|
53
|
+
next(kind: string, parent: string): number;
|
|
54
|
+
/**
|
|
55
|
+
* Take a place in line on a task path. The kernel numbers calls on a path in
|
|
56
|
+
* the order they are proposed, so proposals are made in the order calls
|
|
57
|
+
* were *invoked*: a call's identity is fixed synchronously, when
|
|
58
|
+
* `run.call(...)` is called, whatever order earlier calls' asynchronous
|
|
59
|
+
* preparation (such as counting tokens) finishes in.
|
|
60
|
+
*/
|
|
61
|
+
enter(path: string): {
|
|
62
|
+
wait: Promise<void> | null;
|
|
63
|
+
release: () => void;
|
|
64
|
+
};
|
|
65
|
+
/** Throw a pending signal again, so agent code can't carry on past it. */
|
|
66
|
+
check(): void;
|
|
67
|
+
/** Call the kernel, remembering a divergence. */
|
|
68
|
+
guard<T>(fn: () => T): T;
|
|
69
|
+
}
|
|
70
|
+
declare const HANDLE_TYPE: unique symbol;
|
|
71
|
+
/** True if `value` is a Jackline handle. */
|
|
72
|
+
export declare function isHandle(value: unknown): value is Handle;
|
|
73
|
+
/** What a handle offers besides its fields. */
|
|
74
|
+
export interface HandleCore<T> {
|
|
75
|
+
/** A handle to part of this value: a field of an object or an index of an array. */
|
|
76
|
+
field(key: string | number): Handle;
|
|
77
|
+
/** This value's label: `integrity`, `readers`, `tags`, `sources`. */
|
|
78
|
+
readonly label: Label;
|
|
79
|
+
/** Type-only: the type of the value it holds. */
|
|
80
|
+
readonly [HANDLE_TYPE]?: T;
|
|
81
|
+
}
|
|
82
|
+
type HandleFields<T> = 0 extends 1 & T ? {
|
|
83
|
+
readonly [key: string]: Handle<any>;
|
|
84
|
+
} : unknown extends T ? {
|
|
85
|
+
readonly [key: string]: Handle;
|
|
86
|
+
} : T extends readonly (infer E)[] ? {
|
|
87
|
+
readonly [index: number]: Handle<E>;
|
|
88
|
+
} : T extends object ? {
|
|
89
|
+
readonly [K in keyof T]-?: Handle<T[K]>;
|
|
90
|
+
} : object;
|
|
91
|
+
/**
|
|
92
|
+
* A value from the outside world, held by the kernel with its labels.
|
|
93
|
+
*
|
|
94
|
+
* - Pass it into prompts and tool arguments **without revealing it**: its
|
|
95
|
+
* label travels with it and your run's context stays clean.
|
|
96
|
+
* - Take parts of it with `handle.x`, `handle["x"]`, `handle[0]` or
|
|
97
|
+
* `handle.field("x")`.
|
|
98
|
+
* - Read it with `run.reveal(handle)`. From then on, everything the run does
|
|
99
|
+
* carries the handle's label, because your code may have been steered by
|
|
100
|
+
* what it read (spec G4).
|
|
101
|
+
*
|
|
102
|
+
* Using a handle as a string or number (`${handle}`, `handle + 1`,
|
|
103
|
+
* `JSON.stringify(handle)`, iterating it) throws `HandleMisuse`.
|
|
104
|
+
*/
|
|
105
|
+
export type Handle<T = unknown> = HandleCore<T> & HandleFields<T>;
|
|
106
|
+
/** A value, or a handle to one: what tool arguments and prompts accept. */
|
|
107
|
+
export type MaybeHandle<T> = T | HandleCore<unknown>;
|
|
108
|
+
/** Passed to every tool as its second argument. */
|
|
109
|
+
export interface EffectContext {
|
|
110
|
+
readonly identity: string;
|
|
111
|
+
readonly idempotencyKey: string;
|
|
112
|
+
readonly effectId: number;
|
|
113
|
+
/**
|
|
114
|
+
* Aborted when the run's deadline passes while the call is running. JS
|
|
115
|
+
* can't cancel a promise, so long-running tools should honour it.
|
|
116
|
+
*/
|
|
117
|
+
readonly signal: AbortSignal;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Hard limits for a run or sub-run (spec G2). Every field is optional.
|
|
121
|
+
*
|
|
122
|
+
* Every effect reserves its worst-case cost before it runs, and is refused if
|
|
123
|
+
* that doesn't fit, so a run can never spend more than its budget.
|
|
124
|
+
*/
|
|
125
|
+
export interface Budget {
|
|
126
|
+
/** Money, in dollars. */
|
|
127
|
+
usd?: number;
|
|
128
|
+
/** Totals across all LLM calls. */
|
|
129
|
+
inputTokens?: number;
|
|
130
|
+
outputTokens?: number;
|
|
131
|
+
/** Number of LLM and tool calls. */
|
|
132
|
+
steps?: number;
|
|
133
|
+
/** Wall-clock deadline from the start (`minutes` and `seconds` add up). */
|
|
134
|
+
minutes?: number;
|
|
135
|
+
seconds?: number;
|
|
136
|
+
/** The most input any single LLM call may have. */
|
|
137
|
+
contextTokens?: number;
|
|
138
|
+
/** Larger tool results are kept out of model context. */
|
|
139
|
+
maxToolResultTokens?: number;
|
|
140
|
+
/** How many times this run may ask a human (spec G5). */
|
|
141
|
+
approvals?: number;
|
|
142
|
+
/** How long an approval request stays valid, in seconds. Default: 24 hours. */
|
|
143
|
+
approvalTtlSeconds?: number;
|
|
144
|
+
}
|
|
145
|
+
/** Spend or what's left. `null` means unlimited. */
|
|
146
|
+
export interface Spend {
|
|
147
|
+
usd: number | null;
|
|
148
|
+
inputTokens: number | null;
|
|
149
|
+
outputTokens: number | null;
|
|
150
|
+
steps: number | null;
|
|
151
|
+
}
|
|
152
|
+
/** After a crash, says whether an irreversible call went through (spec G8). */
|
|
153
|
+
export type Reconcile = (args: Record<string, unknown>, idempotencyKey: string) => Receipt | Promise<Receipt>;
|
|
154
|
+
export interface ToolOptions {
|
|
155
|
+
/** The tool's name, as policies, grants and models see it. */
|
|
156
|
+
name: string;
|
|
157
|
+
/** What the model sees in `run.agent()`. */
|
|
158
|
+
description?: string;
|
|
159
|
+
/**
|
|
160
|
+
* What happens if the call has to be repeated (spec G8): `read_only` (safe
|
|
161
|
+
* to repeat), `idempotent` (safe with the same key) or `irreversible` (the
|
|
162
|
+
* default, and the safe assumption).
|
|
163
|
+
*/
|
|
164
|
+
effect?: EffectClass;
|
|
165
|
+
/** Dollars per call (for example a paid API), counted against the budget. */
|
|
166
|
+
cost?: number;
|
|
167
|
+
/** The label of what it returns. Default: `untrusted()`. */
|
|
168
|
+
output?: LabelSpec;
|
|
169
|
+
/**
|
|
170
|
+
* For irreversible tools. If the process crashed mid-call, `resume` calls
|
|
171
|
+
* `reconcile(args, idempotencyKey)` to ask whether it went through. Return
|
|
172
|
+
* `committed(result)`, `notCommitted()` or `unknown()`. Without it, the run
|
|
173
|
+
* stops for a person to decide.
|
|
174
|
+
*/
|
|
175
|
+
reconcile?: Reconcile;
|
|
176
|
+
/** JSON Schema for the arguments, shown to models in `run.agent()`. */
|
|
177
|
+
parameters?: JsonSchema;
|
|
178
|
+
}
|
|
179
|
+
export type ToolFn<A, R> = (args: A, ctx: EffectContext) => R | Promise<R>;
|
|
180
|
+
export type AnyArgs = Record<string, any>;
|
|
181
|
+
type AnyTool = Tool<any, any>;
|
|
182
|
+
type AnyCapability = Capability<any, any>;
|
|
183
|
+
/** A function declared as a tool with `tool(...)`. */
|
|
184
|
+
export declare class Tool<A extends AnyArgs = AnyArgs, R = unknown> {
|
|
185
|
+
readonly name: string;
|
|
186
|
+
readonly description: string;
|
|
187
|
+
readonly effect: EffectClass;
|
|
188
|
+
readonly costUsd: number;
|
|
189
|
+
readonly output: LabelSpec | null;
|
|
190
|
+
readonly reconcile: Reconcile | null;
|
|
191
|
+
readonly parameters: JsonSchema | null;
|
|
192
|
+
/** For tools with a manifest (MCP): returns the current manifest, for pinning. */
|
|
193
|
+
readonly manifest: (() => Json) | null;
|
|
194
|
+
readonly fn: ToolFn<A, R>;
|
|
195
|
+
constructor(options: ToolOptions & {
|
|
196
|
+
manifest?: () => Json;
|
|
197
|
+
}, fn: ToolFn<A, R>);
|
|
198
|
+
/** @internal */
|
|
199
|
+
manifestDigest(): string | null;
|
|
200
|
+
/** JSON Schema for the tool's arguments, shown to models in `run.agent()`. */
|
|
201
|
+
inputSchema(): JsonSchema;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Declare a function as a tool. It is called with one arguments object and an
|
|
205
|
+
* `EffectContext`.
|
|
206
|
+
*
|
|
207
|
+
* ```ts
|
|
208
|
+
* const getOrder = jl.tool(
|
|
209
|
+
* { name: "get_order", effect: "read_only", output: jl.trusted() },
|
|
210
|
+
* async ({ orderId }: { orderId: string }) => orders[orderId],
|
|
211
|
+
* );
|
|
212
|
+
* ```
|
|
213
|
+
*/
|
|
214
|
+
export declare function tool<A extends AnyArgs = AnyArgs, R = unknown>(options: ToolOptions, fn: ToolFn<A, R>): Tool<A, Awaited<R>>;
|
|
215
|
+
/**
|
|
216
|
+
* Tool arguments: each may be a plain value or a handle. Handles are opaque
|
|
217
|
+
* until revealed, so any handle is accepted; the kernel checks the call.
|
|
218
|
+
*/
|
|
219
|
+
export type CallArgs<A> = {
|
|
220
|
+
[K in keyof A]: A[K] | HandleCore<unknown>;
|
|
221
|
+
};
|
|
222
|
+
export interface NarrowOptions {
|
|
223
|
+
maxCalls?: number;
|
|
224
|
+
/** Constraints per argument, e.g. `{ amount: atMost(200) }`. */
|
|
225
|
+
args?: Record<string, Constraint | Constraint[]>;
|
|
226
|
+
/** The capability stops working after this many seconds. */
|
|
227
|
+
expiresInSeconds?: number;
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Permission to call one tool, possibly narrowed (spec G3). Get one with
|
|
231
|
+
* `run.grant(tool)`; narrow it with `.narrow(...)`. Narrowing can only remove
|
|
232
|
+
* permissions: every ancestor's limits still apply.
|
|
233
|
+
*/
|
|
234
|
+
export declare class Capability<A extends AnyArgs = AnyArgs, R = unknown> {
|
|
235
|
+
private readonly run;
|
|
236
|
+
readonly id: number;
|
|
237
|
+
readonly tool: Tool<A, R>;
|
|
238
|
+
readonly hostApproval: boolean;
|
|
239
|
+
/** @internal */
|
|
240
|
+
constructor(run: Run, id: number, tool: Tool<A, R>, hostApproval?: boolean);
|
|
241
|
+
/** The same capability, but every call asks a human first (spec G5). */
|
|
242
|
+
requiringApproval(): Capability<A, R>;
|
|
243
|
+
/** A narrower capability. */
|
|
244
|
+
narrow(options?: NarrowOptions): Capability<A, R>;
|
|
245
|
+
[inspect.custom](): string;
|
|
246
|
+
}
|
|
247
|
+
/** A chat message. */
|
|
248
|
+
export interface Message {
|
|
249
|
+
role: string;
|
|
250
|
+
content: unknown;
|
|
251
|
+
}
|
|
252
|
+
export declare function user(content: unknown): Message;
|
|
253
|
+
export declare function assistant(content: unknown): Message;
|
|
254
|
+
export declare function system(content: unknown): Message;
|
|
255
|
+
/** What a model call may cost at most. The kernel prices it. */
|
|
256
|
+
export interface CostBound {
|
|
257
|
+
/** Price-table key, e.g. `claude-opus-5-5`. `null` means no money cost. */
|
|
258
|
+
model?: string | null;
|
|
259
|
+
inputTokens: number;
|
|
260
|
+
outputTokens: number;
|
|
261
|
+
/** Server-side fallbacks may route the call to another model. */
|
|
262
|
+
fallback?: boolean;
|
|
263
|
+
}
|
|
264
|
+
/** What a model call actually used. */
|
|
265
|
+
export interface Usage {
|
|
266
|
+
inputTokens?: number;
|
|
267
|
+
outputTokens?: number;
|
|
268
|
+
cacheWriteTokens?: number;
|
|
269
|
+
cacheReadTokens?: number;
|
|
270
|
+
/** The model that answered, when a fallback may have served the call. */
|
|
271
|
+
servedModel?: string | null;
|
|
272
|
+
}
|
|
273
|
+
/** A tool call a model asked for. */
|
|
274
|
+
export interface ToolCall {
|
|
275
|
+
id: string;
|
|
276
|
+
name: string;
|
|
277
|
+
input?: Record<string, unknown>;
|
|
278
|
+
}
|
|
279
|
+
/** What a model call returns (recorded as is). */
|
|
280
|
+
export interface ModelResult {
|
|
281
|
+
text: string;
|
|
282
|
+
stop_reason?: string | null;
|
|
283
|
+
model?: string | null;
|
|
284
|
+
usage?: Record<string, number> | null;
|
|
285
|
+
content?: unknown[];
|
|
286
|
+
tool_calls?: ToolCall[];
|
|
287
|
+
raw?: unknown;
|
|
288
|
+
parsed?: unknown;
|
|
289
|
+
[key: string]: unknown;
|
|
290
|
+
}
|
|
291
|
+
/** Usage as model APIs report it (`input_tokens`, ...) to `Usage`. */
|
|
292
|
+
export declare function usageFromApi(u: unknown, servedModel?: string | null): Usage | null;
|
|
293
|
+
/**
|
|
294
|
+
* A safe upper bound on a request's input tokens: one token per byte of its
|
|
295
|
+
* JSON, plus overhead for message framing. Real tokens are several bytes
|
|
296
|
+
* each, so this overestimates; adapters with exact counting can do better.
|
|
297
|
+
*/
|
|
298
|
+
export declare function estimateInputTokens(request: Json): number;
|
|
299
|
+
/**
|
|
300
|
+
* Base class for model adapters.
|
|
301
|
+
*
|
|
302
|
+
* `request` turns messages and options into the plain data that gets
|
|
303
|
+
* recorded and hashed. `costRequest` says what the call may cost at most,
|
|
304
|
+
* `execute` sends it, and `usage` reads what it actually cost.
|
|
305
|
+
*/
|
|
306
|
+
export declare abstract class Model {
|
|
307
|
+
target: string;
|
|
308
|
+
mocked: boolean;
|
|
309
|
+
/** Price-table key, e.g. `claude-opus-5-5`. `null` means no money cost. */
|
|
310
|
+
priceKey: string | null;
|
|
311
|
+
defaultMaxTokens: number;
|
|
312
|
+
request(messages: unknown[], options: Json): Json;
|
|
313
|
+
costRequest(request: Json): CostBound | Promise<CostBound>;
|
|
314
|
+
usage(result: ModelResult): Usage | null;
|
|
315
|
+
abstract execute(request: Json): Promise<ModelResult>;
|
|
316
|
+
}
|
|
317
|
+
export interface LlmOptions {
|
|
318
|
+
/**
|
|
319
|
+
* A JSON Schema the model's answer must match (structured output). The
|
|
320
|
+
* handle then points at the parsed, validated value.
|
|
321
|
+
*/
|
|
322
|
+
output?: JsonSchema;
|
|
323
|
+
/** Extra validation or parsing of structured output, e.g. a zod schema's `parse`. */
|
|
324
|
+
validate?: (value: unknown) => unknown;
|
|
325
|
+
/** Anything else is passed to the model adapter, e.g. `max_tokens`. */
|
|
326
|
+
[option: string]: unknown;
|
|
327
|
+
}
|
|
328
|
+
export interface AgentOptions {
|
|
329
|
+
tools?: (AnyTool | AnyCapability)[];
|
|
330
|
+
/** Model calls before `StepLimit` (default 10). */
|
|
331
|
+
maxSteps?: number;
|
|
332
|
+
system?: string;
|
|
333
|
+
/** Anything else is passed to the model adapter. */
|
|
334
|
+
[option: string]: unknown;
|
|
335
|
+
}
|
|
336
|
+
/** One execution of an agent function. The only door to the outside world. */
|
|
337
|
+
export declare class Run {
|
|
338
|
+
/** @internal */
|
|
339
|
+
readonly _state: RunState;
|
|
340
|
+
/** @internal */
|
|
341
|
+
readonly _account: number;
|
|
342
|
+
private readonly rootPath;
|
|
343
|
+
private readonly implicit;
|
|
344
|
+
/** @internal */
|
|
345
|
+
constructor(state: RunState, account?: number, rootPath?: string | null);
|
|
346
|
+
get runId(): string;
|
|
347
|
+
/** `live`, `replay` or `resume`. */
|
|
348
|
+
get mode(): "live" | "replay" | "resume";
|
|
349
|
+
/** The current task path. A sub-run's effects stay under its own root. */
|
|
350
|
+
private path;
|
|
351
|
+
private finish;
|
|
352
|
+
private fail;
|
|
353
|
+
/** Throw for refusals and replayed failures. True if the result was replayed. */
|
|
354
|
+
private static checkProposal;
|
|
355
|
+
/**
|
|
356
|
+
* Propose an effect. Everything up to taking a place in line runs
|
|
357
|
+
* synchronously, so the call's identity is fixed when it is invoked.
|
|
358
|
+
*/
|
|
359
|
+
private propose;
|
|
360
|
+
private effect;
|
|
361
|
+
/** Resolve approvals and in-doubt calls until the kernel's answer is final. */
|
|
362
|
+
private settle;
|
|
363
|
+
private hostEffect;
|
|
364
|
+
/**
|
|
365
|
+
* Call a model. Returns a handle to `{ text, stop_reason, model, usage, content, tool_calls, raw }`.
|
|
366
|
+
*
|
|
367
|
+
* With `output` (a JSON Schema), the model must return that shape
|
|
368
|
+
* (structured output), and the handle points at the parsed, validated
|
|
369
|
+
* value. The result carries the labels of everything the model read (spec G4).
|
|
370
|
+
*/
|
|
371
|
+
llm<T = ModelResult>(model: Model, messages: unknown[], options?: LlmOptions): Promise<Handle<T>>;
|
|
372
|
+
/**
|
|
373
|
+
* Permission to call a tool (spec G3). Only tools allowed by
|
|
374
|
+
* `start(..., { grants })` can be granted, and only by the top-level run;
|
|
375
|
+
* sub-runs use the capabilities passed to them.
|
|
376
|
+
*/
|
|
377
|
+
grant<A extends AnyArgs, R>(t: Tool<A, R>): Capability<A, R>;
|
|
378
|
+
/**
|
|
379
|
+
* Call a tool through a capability. Passing a tool grants it first. Each
|
|
380
|
+
* argument that is an unmodified handle keeps its own label; anything else
|
|
381
|
+
* carries the run's context label.
|
|
382
|
+
*/
|
|
383
|
+
call<A extends AnyArgs, R>(t: Tool<A, R> | Capability<A, R>, args?: CallArgs<A>): Promise<Handle<R>>;
|
|
384
|
+
/**
|
|
385
|
+
* A tool-using loop: the model calls tools until it answers.
|
|
386
|
+
*
|
|
387
|
+
* Every call the model proposes goes through the kernel like any other,
|
|
388
|
+
* but it carries the label of everything the model has read (spec G4): a
|
|
389
|
+
* model that read an attacker's email can't trigger a tool your policy
|
|
390
|
+
* reserves for trusted decisions. When a call is refused, the model is told
|
|
391
|
+
* so in fixed words that never repeat the arguments. Returns a handle to
|
|
392
|
+
* the final reply. Throws `StepLimit` after `maxSteps` model calls.
|
|
393
|
+
*/
|
|
394
|
+
agent(model: Model, prompt: string | Handle | Message[], options?: AgentOptions): Promise<Handle<ModelResult>>;
|
|
395
|
+
private capability;
|
|
396
|
+
/** @internal */
|
|
397
|
+
_setImplicit(cap: AnyCapability): void;
|
|
398
|
+
private callTool;
|
|
399
|
+
/**
|
|
400
|
+
* Read a handle's value as plain data.
|
|
401
|
+
*
|
|
402
|
+
* Revealing untrusted data makes every later call of this run carry that
|
|
403
|
+
* label, because your code may now be steered by it (spec G4). Prefer
|
|
404
|
+
* passing handles along unrevealed where you can.
|
|
405
|
+
*/
|
|
406
|
+
reveal<T>(handle: Handle<T>): T;
|
|
407
|
+
/**
|
|
408
|
+
* Mark a value as trusted, after checking it (spec G4).
|
|
409
|
+
*
|
|
410
|
+
* With `check`, the tool is called with `args` (which may include the
|
|
411
|
+
* handle) and must return `true` with a trusted label; declare it
|
|
412
|
+
* `output: trusted({ independentOfArgs: true })`. Without `check`, your
|
|
413
|
+
* code vouches for the value. Either way, it's journaled.
|
|
414
|
+
*/
|
|
415
|
+
endorse<T>(handle: Handle<T>, options: {
|
|
416
|
+
reason: string;
|
|
417
|
+
check?: AnyTool | AnyCapability;
|
|
418
|
+
args?: Json;
|
|
419
|
+
}): Promise<Handle<T>>;
|
|
420
|
+
/** Lower a value's sensitivity: set who may read it and drop its tags. Journaled. */
|
|
421
|
+
declassify<T>(handle: Handle<T>, options: {
|
|
422
|
+
to?: Readers;
|
|
423
|
+
reason: string;
|
|
424
|
+
}): Handle<T>;
|
|
425
|
+
/** Everything this run's code has revealed, combined. Every tool call carries it. */
|
|
426
|
+
get contextLabel(): Label;
|
|
427
|
+
/** The current time in milliseconds since the epoch. Recorded, so replays see the same clock. */
|
|
428
|
+
now(): Promise<number>;
|
|
429
|
+
/** A random number in [0, 1). Recorded, so replays see the same number. */
|
|
430
|
+
random(): Promise<number>;
|
|
431
|
+
/** A random UUID string. Recorded, so replays see the same value. */
|
|
432
|
+
uuid(): Promise<string>;
|
|
433
|
+
/**
|
|
434
|
+
* Wait for several effects running concurrently, with stable call identities.
|
|
435
|
+
*
|
|
436
|
+
* - A promise from `run.call(...)`, `run.llm(...)` or `run.spawn(...)` already
|
|
437
|
+
* has its identity: it was fixed when the call was made.
|
|
438
|
+
* - A function `() => Promise` runs as its own branch with its own task path
|
|
439
|
+
* (`parent/n/i`), so every call inside it, however many and whatever order
|
|
440
|
+
* branches finish in, keeps the same identity in a replay. Use this form
|
|
441
|
+
* for branches that make more than one call.
|
|
442
|
+
*/
|
|
443
|
+
gather<const T extends readonly unknown[]>(items: T): Promise<{
|
|
444
|
+
-readonly [K in keyof T]: T[K] extends () => infer R ? Awaited<R> : Awaited<T[K]>;
|
|
445
|
+
}>;
|
|
446
|
+
/**
|
|
447
|
+
* What's left: `usd`, `inputTokens`, `outputTokens`, `steps` (`null` =
|
|
448
|
+
* unlimited) and `seconds` until the deadline.
|
|
449
|
+
*/
|
|
450
|
+
get remaining(): Spend & {
|
|
451
|
+
seconds: number | null;
|
|
452
|
+
};
|
|
453
|
+
/** What this run (or sub-run) has spent so far. */
|
|
454
|
+
get spent(): Spend;
|
|
455
|
+
/**
|
|
456
|
+
* Run `fn` as a sub-run with its own budget slice and only the capabilities
|
|
457
|
+
* in `grants` (spec G2, G3).
|
|
458
|
+
*
|
|
459
|
+
* The sub-run can never spend more than its slice; whatever it doesn't use
|
|
460
|
+
* returns when `fn` settles. It starts with this run's context label.
|
|
461
|
+
* Returns what `fn` returns.
|
|
462
|
+
*/
|
|
463
|
+
spawn<R>(budget: Budget, fn: (child: SubRun) => Promise<R>): Promise<R>;
|
|
464
|
+
spawn<R>(budget: Budget, options: {
|
|
465
|
+
grants?: AnyCapability[];
|
|
466
|
+
}, fn: (child: SubRun) => Promise<R>): Promise<R>;
|
|
467
|
+
}
|
|
468
|
+
/** A run inside a run, with its own budget slice and capabilities. */
|
|
469
|
+
export declare class SubRun extends Run {
|
|
470
|
+
private ended;
|
|
471
|
+
/** @internal */
|
|
472
|
+
constructor(parent: Run, account: number, rootPath: string);
|
|
473
|
+
/** End the sub-run early. Unused budget returns to the parent. Returns what it spent. */
|
|
474
|
+
end(): Spend;
|
|
475
|
+
}
|
|
476
|
+
export type RunStatus = "completed" | "failed" | "paused" | "in_doubt";
|
|
477
|
+
export interface RunResult<R = unknown> {
|
|
478
|
+
value: R | null;
|
|
479
|
+
/**
|
|
480
|
+
* `completed`, `failed`, `paused` (waiting for a human) or `in_doubt` (a
|
|
481
|
+
* crash left an irreversible call's outcome unknown).
|
|
482
|
+
*/
|
|
483
|
+
status: RunStatus;
|
|
484
|
+
runId: string;
|
|
485
|
+
error: unknown;
|
|
486
|
+
/** Total spent. */
|
|
487
|
+
spent: Spend;
|
|
488
|
+
/** For `paused`: the approval requests waiting for a human. */
|
|
489
|
+
pending: ApprovalRequest[];
|
|
490
|
+
/** For `in_doubt`: the calls a person must settle with `resolve`. */
|
|
491
|
+
inDoubt: InDoubtCall[];
|
|
492
|
+
/** For resumes with `allowCodeChange`: calls that no longer matched. */
|
|
493
|
+
mismatches: string[];
|
|
494
|
+
}
|
|
495
|
+
/** An agent function: it gets the run first, then its own arguments. */
|
|
496
|
+
export type Agent<A extends unknown[] = any[], R = unknown> = (run: Run, ...args: A) => Promise<R>;
|
|
497
|
+
export interface StartOptions {
|
|
498
|
+
/** Default: `run-` and 12 random hex characters. */
|
|
499
|
+
runId?: string;
|
|
500
|
+
/** A SQLite file path or an open journal. Default: `jackline.db`. */
|
|
501
|
+
journal?: JournalLike;
|
|
502
|
+
/** Hard limits. */
|
|
503
|
+
budget?: Budget;
|
|
504
|
+
/**
|
|
505
|
+
* Extra model prices in dollars per million tokens, e.g.
|
|
506
|
+
* `{ "my-model": { input: 1.0, output: 4.0 } }`.
|
|
507
|
+
*/
|
|
508
|
+
prices?: Record<string, {
|
|
509
|
+
input: number;
|
|
510
|
+
output: number;
|
|
511
|
+
cacheWrite?: number;
|
|
512
|
+
cacheRead?: number;
|
|
513
|
+
}>;
|
|
514
|
+
/** Rules checked on every tool call. */
|
|
515
|
+
policy?: Policy;
|
|
516
|
+
/**
|
|
517
|
+
* The tools this run may use (tools or name patterns like `"search_*"`).
|
|
518
|
+
* Omit it to allow any tool declared with `tool()`.
|
|
519
|
+
*/
|
|
520
|
+
grants?: (AnyTool | string)[];
|
|
521
|
+
/**
|
|
522
|
+
* Asked whenever a call needs a human (spec G5). Return `approve()`,
|
|
523
|
+
* `deny()` or `pause()`. Without one, calls that need approval are refused
|
|
524
|
+
* with `ApprovalRequired`.
|
|
525
|
+
*/
|
|
526
|
+
approver?: Approver;
|
|
527
|
+
/** Recorded, and checked on resume (`"git"` reads the current commit). */
|
|
528
|
+
codeVersion?: string;
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
* Run an agent function, recording everything it does.
|
|
532
|
+
*
|
|
533
|
+
* The agent is `async (run, ...args) => ...`. Its arguments are given as an
|
|
534
|
+
* array and must be plain data, so a replay can call it again with the same
|
|
535
|
+
* input. Options come last:
|
|
536
|
+
*
|
|
537
|
+
* ```ts
|
|
538
|
+
* await jl.start(agent, ["How do I rotate a key?"], { budget: { usd: 0.5 } });
|
|
539
|
+
* await jl.start(agentWithoutArgs, { journal: "runs.db" });
|
|
540
|
+
* ```
|
|
541
|
+
*/
|
|
542
|
+
export declare function start<R>(agent: (run: Run) => Promise<R>, options?: StartOptions): Promise<RunResult<R>>;
|
|
543
|
+
export declare function start<A extends unknown[], R>(agent: (run: Run, ...args: A) => Promise<R>, args: A, options?: StartOptions): Promise<RunResult<R>>;
|
|
544
|
+
/**
|
|
545
|
+
* Re-run a recorded run offline: zero model or tool calls.
|
|
546
|
+
*
|
|
547
|
+
* Budget and policy decisions are replayed from the recording, including
|
|
548
|
+
* refusals. Throws `Divergence` at the first call that doesn't match the
|
|
549
|
+
* recording, or at the end if recorded calls were never made or the result differs.
|
|
550
|
+
*/
|
|
551
|
+
export declare function replay<R>(agent: Agent<any[], R>, // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
552
|
+
runId: string, options?: {
|
|
553
|
+
journal?: JournalLike;
|
|
554
|
+
}): Promise<RunResult<R>>;
|
|
555
|
+
export interface ResumeOptions {
|
|
556
|
+
journal?: JournalLike;
|
|
557
|
+
approver?: Approver;
|
|
558
|
+
/** Decisions for paused approvals, by approval ID (from `result.pending`). */
|
|
559
|
+
approvals?: Record<string, Decision>;
|
|
560
|
+
codeVersion?: string;
|
|
561
|
+
allowCodeChange?: boolean;
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* Continue a run that crashed, paused for approval, or stopped in doubt (spec G8).
|
|
565
|
+
*
|
|
566
|
+
* The agent runs again from the start: completed calls return their recorded
|
|
567
|
+
* results instantly and budgets continue where they were. Then:
|
|
568
|
+
*
|
|
569
|
+
* - read-only and idempotent calls cut off by the crash run again (same idempotency key);
|
|
570
|
+
* - an irreversible call cut off by the crash is never re-run automatically.
|
|
571
|
+
* Its tool's `reconcile` is asked whether it went through; without an
|
|
572
|
+
* answer, the run stops with status `"in_doubt"` (settle it with `resolve`);
|
|
573
|
+
* - `approvals` maps approval IDs (from `result.pending`) to decisions. Each
|
|
574
|
+
* applies only if the call is exactly what the human saw.
|
|
575
|
+
*
|
|
576
|
+
* If `codeVersion` differs from the recorded one, resuming is refused unless
|
|
577
|
+
* `allowCodeChange` is set; calls that no longer match are then run live and
|
|
578
|
+
* listed in `result.mismatches` (spec G9).
|
|
579
|
+
*/
|
|
580
|
+
export declare function resume<R>(agent: Agent<any[], R>, // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
581
|
+
runId: string, options?: ResumeOptions): Promise<RunResult<R>>;
|
|
582
|
+
/**
|
|
583
|
+
* Settle an in-doubt call by hand, for example after checking with the
|
|
584
|
+
* payment provider (spec G8). `identity` comes from `result.inDoubt`. The next
|
|
585
|
+
* `resume` uses the answer: `committed: true` reuses `value` as the call's
|
|
586
|
+
* result; `committed: false` lets it run again.
|
|
587
|
+
*/
|
|
588
|
+
export declare function resolve(runId: string, identity: string, options: {
|
|
589
|
+
committed: boolean;
|
|
590
|
+
value?: unknown;
|
|
591
|
+
by?: string;
|
|
592
|
+
journal?: JournalLike;
|
|
593
|
+
}): void;
|
|
594
|
+
export {};
|