@parcha/agentrun-kernel 0.1.0-beta.38 → 0.1.0-beta.40
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/CHANGELOG.md +83 -0
- package/NOTICE +2 -0
- package/dist/booking.d.ts +25 -6
- package/dist/booking.js +15 -4
- package/dist/host/index.d.ts +4 -3
- package/dist/host/index.js +3 -2
- package/dist/host/o200k.d.ts +11 -0
- package/dist/host/o200k.js +185 -0
- package/dist/host/overhead.d.ts +72 -12
- package/dist/host/overhead.js +220 -38
- package/dist/host/proxy.d.ts +85 -7
- package/dist/host/proxy.js +374 -87
- package/dist/index.d.ts +6 -6
- package/dist/index.js +4 -4
- package/dist/kernel/cap/admit.d.ts +5 -8
- package/dist/kernel/cap/admit.js +13 -19
- package/dist/kernel/cap/free.d.ts +43 -0
- package/dist/kernel/cap/free.js +71 -0
- package/dist/kernel/cap/index.d.ts +6 -5
- package/dist/kernel/cap/index.js +5 -4
- package/dist/kernel/cap/marks.d.ts +2 -3
- package/dist/kernel/cap/park.d.ts +3 -4
- package/dist/kernel/cap/park.js +4 -5
- package/dist/kernel/cap/port.d.ts +8 -13
- package/dist/kernel/cap/port.js +10 -18
- package/dist/kernel/cap/price.d.ts +10 -49
- package/dist/kernel/cap/price.js +15 -68
- package/dist/kernel/cap/recover.d.ts +1 -2
- package/dist/kernel/cap/recover.js +2 -4
- package/dist/kernel/cap/rule.d.ts +8 -16
- package/dist/kernel/cap/rule.js +35 -35
- package/dist/kernel/cap/run-cap.d.ts +21 -13
- package/dist/kernel/cap/run-cap.js +74 -36
- package/dist/kernel/cap/types.d.ts +11 -31
- package/dist/kernel/cap/words.d.ts +13 -8
- package/dist/kernel/cap/words.js +23 -14
- package/dist/kernel/cap/wrap-up.d.ts +4 -0
- package/dist/kernel/cap/wrap-up.js +5 -1
- package/dist/kernel/registry/computer-surface.generated.d.ts +1 -1
- package/dist/kernel/registry/computer-surface.generated.js +9 -25
- package/dist/kernel/registry/computer.d.ts +14 -7
- package/dist/kernel/registry/computer.js +52 -12
- package/dist/kernel/registry/gate.d.ts +2 -2
- package/dist/kernel/registry/gate.js +4 -7
- package/dist/kernel/registry/hands.d.ts +3 -4
- package/dist/kernel/registry/index.d.ts +1 -1
- package/dist/kernel/registry/index.js +1 -1
- package/dist/kernel/registry/registry.js +6 -9
- package/dist/process/recovery.d.ts +4 -1
- package/dist/process/recovery.js +15 -3
- package/dist/process/resume.d.ts +5 -2
- package/dist/process/resume.js +15 -3
- package/dist/process/tools.d.ts +26 -1
- package/dist/process/tools.js +47 -4
- package/dist/run-booking.d.ts +21 -2
- package/dist/run-booking.js +215 -18
- package/dist/runner.d.ts +82 -21
- package/dist/runner.js +176 -59
- package/dist/testing.d.ts +4 -2
- package/dist/testing.js +2 -2
- package/dist/tool-deferral.d.ts +3 -3
- package/dist/tool-deferral.js +4 -4
- package/dist/types.d.ts +70 -14
- package/dist/types.js +8 -0
- package/package.json +5 -3
- package/vendor/o200k/LICENSE +61 -0
- package/vendor/o200k/o200k_base.tiktoken +199998 -0
|
@@ -1,25 +1,43 @@
|
|
|
1
|
-
import { ACT_STEPS, COMPUTER_TOOL_SCHEMAS
|
|
1
|
+
import { ACT_STEPS, COMPUTER_TOOL_SCHEMAS } from "./computer-surface.generated.js";
|
|
2
2
|
import { answerOf, needsKindWords } from "./hands.js";
|
|
3
3
|
import { HANDOFF_TOOL, handoffDef, handoffExecutor } from "./handoff.js";
|
|
4
|
+
/** The tool a script's `computer` runs every op through that no step tool runs. */
|
|
5
|
+
export const COMPUTER_OP_TOOL = "computer_op";
|
|
4
6
|
/** The computer's tools, in the order a run lists them (the hand-over last). */
|
|
5
|
-
export const COMPUTER_TOOLS = ["computer_observe", "computer_act", "computer_screenshot",
|
|
6
|
-
/** The
|
|
7
|
-
|
|
7
|
+
export const COMPUTER_TOOLS = ["computer_observe", "computer_act", "computer_screenshot", COMPUTER_OP_TOOL, HANDOFF_TOOL];
|
|
8
|
+
/** The ops computer_op runs, each with the groups that offer it (one of them): the surface's ops (SURFACE_OPS) but the
|
|
9
|
+
* three the step tools run. */
|
|
10
|
+
export const COMPUTER_OPS = Object.freeze({
|
|
11
|
+
"input.act": ["input.pointer", "input.touch", "input.keys"],
|
|
12
|
+
"apps.list": ["apps"],
|
|
13
|
+
"apps.open": ["apps"],
|
|
14
|
+
"shell.run": ["exec"],
|
|
15
|
+
"processes.start": ["processes"],
|
|
16
|
+
"files.read": ["files"],
|
|
17
|
+
"files.write": ["files"],
|
|
18
|
+
"files.list": ["files"],
|
|
19
|
+
"files.stat": ["files"],
|
|
20
|
+
});
|
|
21
|
+
/** The op of each step tool, as a script's `computer` names it. */
|
|
22
|
+
export const STEP_TOOL_OPS = Object.freeze({ observe: "computer_observe", act: "computer_act", screenshot: "computer_screenshot" });
|
|
8
23
|
// The words are short on purpose: every request of a run with a computer carries them. The kind skill (desktop-computer,
|
|
9
24
|
// phone-computer) teaches how to work; a description says only what its tool does.
|
|
10
25
|
const OBSERVE = `An app's window as its elements, one line each (@e12 button "Save"), under a head line naming the state (s7). With no ui group: a screenshot.`;
|
|
11
26
|
const ACT = `Run steps on an app, each on an element's ref or at a point in the window (refused as stale once the window changed under it). The answer says how each step ran and whether the window changed. The first refused step stops the rest. steps: UiStep[]\n\`\`\`ts\n${ACT_STEPS}\n\`\`\``;
|
|
12
27
|
const SCREENSHOT = "The whole screen as an image, or a region of it.";
|
|
13
|
-
|
|
14
|
-
|
|
28
|
+
const OP = `One of the computer's ops no other tool runs, as a script's \`computer\` calls it: input.act {actions}, apps.list, apps.open {id, url?, background?}, shell.run {command, cwd?, env?, timeoutMs?}, processes.start {command, cwd?, env?}, files.read {path, encoding?}, files.write {path, data, encoding?}, files.list {path}, files.stat {path}. An op this computer does not offer is refused.`;
|
|
29
|
+
/** The ops computer_op runs on a computer with `groups`. */
|
|
30
|
+
export function computerOpsFor(groups) {
|
|
31
|
+
return Object.keys(COMPUTER_OPS).filter((op) => COMPUTER_OPS[op].some((group) => groups.includes(group)));
|
|
32
|
+
}
|
|
15
33
|
/** The tools a computer with `groups` offers: observe and act with the ui group, or a screen (observe) and input (act)
|
|
16
|
-
* without it; the screenshot with a screen;
|
|
34
|
+
* without it; the screenshot with a screen; computer_op with any op past those (computerOpsFor). */
|
|
17
35
|
export function computerToolsFor(groups) {
|
|
18
36
|
const has = (group) => groups.includes(group);
|
|
19
37
|
const input = has("input.pointer") || has("input.touch") || has("input.keys");
|
|
20
38
|
const observe = has("ui") || has("screen");
|
|
21
39
|
const act = has("ui") || input;
|
|
22
|
-
return [...(observe ? ["computer_observe"] : []), ...(act ? ["computer_act"] : []), ...(has("screen") ? ["computer_screenshot"] : []), ...(
|
|
40
|
+
return [...(observe ? ["computer_observe"] : []), ...(act ? ["computer_act"] : []), ...(has("screen") ? ["computer_screenshot"] : []), ...(computerOpsFor(groups).length > 0 ? [COMPUTER_OP_TOOL] : []), HANDOFF_TOOL];
|
|
23
41
|
}
|
|
24
42
|
/** computer_act's parameters on a computer with no ui group: no expect, which only the ui group checks, so neither the
|
|
25
43
|
* expect nor the wait on one (an empty act with an expect) is offered there. */
|
|
@@ -37,15 +55,25 @@ const def = (kind, name, description, replaySafe, parameters = COMPUTER_TOOL_SCH
|
|
|
37
55
|
parameters,
|
|
38
56
|
replaySafe,
|
|
39
57
|
});
|
|
40
|
-
/**
|
|
41
|
-
|
|
58
|
+
/** computer_op's parameters: the op, and its arguments as a script's `computer` gives them. */
|
|
59
|
+
const OP_PARAMETERS = {
|
|
60
|
+
type: "object",
|
|
61
|
+
properties: {
|
|
62
|
+
op: { type: "string", enum: Object.keys(COMPUTER_OPS) },
|
|
63
|
+
args: { type: "object", description: "The op's arguments." },
|
|
64
|
+
},
|
|
65
|
+
required: ["op"],
|
|
66
|
+
additionalProperties: false,
|
|
67
|
+
};
|
|
68
|
+
/** The computer's tools in `kind`, under its settings key, for a computer with `groups`. An act or an op changes the
|
|
69
|
+
* computer: never replayed. */
|
|
42
70
|
export function computerToolDefs(kind, groups) {
|
|
43
71
|
return Object.freeze([
|
|
44
72
|
// With apps and no ui group, an observe that names an app opens it first: it changes the screen, so it never runs again.
|
|
45
73
|
def(kind, "computer_observe", OBSERVE, groups.includes("ui") || !groups.includes("apps")),
|
|
46
74
|
def(kind, "computer_act", ACT, false, groups.includes("ui") ? undefined : ACT_WITHOUT_UI),
|
|
47
75
|
def(kind, "computer_screenshot", SCREENSHOT, true),
|
|
48
|
-
def(kind,
|
|
76
|
+
def(kind, COMPUTER_OP_TOOL, OP, false, OP_PARAMETERS),
|
|
49
77
|
handoffDef(kind),
|
|
50
78
|
]);
|
|
51
79
|
}
|
|
@@ -66,10 +94,22 @@ const surfaceExecutor = (kind, tool, op) => (args, call) => answerOf(call, async
|
|
|
66
94
|
const answer = await port.call(op, args ?? {}, call.signal);
|
|
67
95
|
return { text: answer.text, ...(answer.images.length > 0 ? { images: answer.images } : {}), structured: (withoutImageData(answer.value) ?? null) };
|
|
68
96
|
});
|
|
69
|
-
/**
|
|
97
|
+
/** computer_op's run: `op` with `args` on the hands' computer port. An op a step tool runs is sent to that tool, so each
|
|
98
|
+
* is held and recorded as that tool's call; any other the port refuses where the computer does not offer it. */
|
|
99
|
+
const opExecutor = (kind) => (args, call) => {
|
|
100
|
+
const { op, args: given } = (args ?? {});
|
|
101
|
+
const tool = typeof op === "string" ? STEP_TOOL_OPS[op] : undefined;
|
|
102
|
+
if (tool)
|
|
103
|
+
return answerOf(call, async () => { throw new Error(`${COMPUTER_OP_TOOL} does not run ${op}: call ${tool}, which does`); });
|
|
104
|
+
if (typeof op !== "string" || !Object.hasOwn(COMPUTER_OPS, op))
|
|
105
|
+
return answerOf(call, async () => { throw new Error(`${COMPUTER_OP_TOOL} runs one of ${Object.keys(COMPUTER_OPS).join(", ")}: not ${JSON.stringify(op)}`); });
|
|
106
|
+
return surfaceExecutor(kind, COMPUTER_OP_TOOL, op)(given !== null && typeof given === "object" && !Array.isArray(given) ? given : {}, call);
|
|
107
|
+
};
|
|
108
|
+
/** The kernel's own executors of the computer's tools in `kind`. */
|
|
70
109
|
export const computerExecutors = (kind) => ({
|
|
71
110
|
computer_observe: surfaceExecutor(kind, "computer_observe", "observe"),
|
|
72
111
|
computer_act: surfaceExecutor(kind, "computer_act", "act"),
|
|
73
112
|
computer_screenshot: surfaceExecutor(kind, "computer_screenshot", "screenshot"),
|
|
113
|
+
[COMPUTER_OP_TOOL]: opExecutor(kind),
|
|
74
114
|
[HANDOFF_TOOL]: handoffExecutor(kind),
|
|
75
115
|
});
|
|
@@ -9,8 +9,8 @@ export declare const SHELL_FAMILY: Readonly<Record<string, string>>;
|
|
|
9
9
|
export declare const SHELL_WATCHERS: ReadonlySet<string>;
|
|
10
10
|
/** Shell tool `name`'s permission, given bash's: a watcher's ask is allow; any other keeps bash's. */
|
|
11
11
|
export declare const shellPermission: (name: string, bash: Permission) => Permission;
|
|
12
|
-
/** The shell tool a call named `name` is,
|
|
13
|
-
*
|
|
12
|
+
/** The shell tool a call named `name` is, when it is one held to another's setting: its own name, which its request, its
|
|
13
|
+
* refusal and its approved call keep. */
|
|
14
14
|
export declare const shellToolOf: (name: string) => string | undefined;
|
|
15
15
|
/** A host tool's permission when no setting names it, by where the run's shell is. On a computer every host tool is
|
|
16
16
|
* allowed, as main's defaults are. With no computer, bash runs in the run's own folder on this machine, so it is asked
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// The tool gate's shell family
|
|
1
|
+
// The tool gate's shell family (serve's tool-gate.ts reads it from here), and the host tools' default permissions by
|
|
2
2
|
// where the run's shell is (the cutover's ruled change: with no computer, bash runs in the run's folder and is asked
|
|
3
3
|
// first, and the background tools are refused).
|
|
4
4
|
import { BACKGROUND_TOOLS, SHELL_TOOLS } from "./limits.js";
|
|
@@ -12,12 +12,9 @@ export const SHELL_FAMILY = Object.freeze(Object.fromEntries(SHELL_TOOLS.map((na
|
|
|
12
12
|
export const SHELL_WATCHERS = new Set(["bash_output", "bash_list", "bash_signal"]);
|
|
13
13
|
/** Shell tool `name`'s permission, given bash's: a watcher's ask is allow; any other keeps bash's. */
|
|
14
14
|
export const shellPermission = (name, bash) => (bash === "ask" && SHELL_WATCHERS.has(name) ? "allow" : bash);
|
|
15
|
-
/** The shell tool a call named `name` is,
|
|
16
|
-
*
|
|
17
|
-
export const shellToolOf = (name) =>
|
|
18
|
-
const own = name.replace(/^mcp__hands__/, "");
|
|
19
|
-
return Object.hasOwn(SHELL_FAMILY, own) && SHELL_FAMILY[own] !== own ? own : undefined;
|
|
20
|
-
};
|
|
15
|
+
/** The shell tool a call named `name` is, when it is one held to another's setting: its own name, which its request, its
|
|
16
|
+
* refusal and its approved call keep. */
|
|
17
|
+
export const shellToolOf = (name) => (Object.hasOwn(SHELL_FAMILY, name) && SHELL_FAMILY[name] !== name ? name : undefined);
|
|
21
18
|
/** A host tool's permission when no setting names it, by where the run's shell is. On a computer every host tool is
|
|
22
19
|
* allowed, as main's defaults are. With no computer, bash runs in the run's own folder on this machine, so it is asked
|
|
23
20
|
* first, and the background tools, which need a computer, are refused. */
|
|
@@ -127,7 +127,7 @@ export type Hands = {
|
|
|
127
127
|
}, signal?: AbortSignal): Promise<ExecResult>;
|
|
128
128
|
/** The attached computer's processes; absent with no computer, where the background tools are refused. */
|
|
129
129
|
readonly processes?: Processes;
|
|
130
|
-
/** A desktop's or a phone's surface (computer_observe, computer_act, computer_screenshot
|
|
130
|
+
/** A desktop's or a phone's surface (computer_observe, computer_act, computer_screenshot); absent
|
|
131
131
|
* where the computer is neither, and those tools are refused in words. */
|
|
132
132
|
readonly computer?: ComputerPort;
|
|
133
133
|
/** A desktop's or a phone's hand-over to the person (the run's hold); absent where there is none. */
|
|
@@ -163,6 +163,5 @@ export type ToolCall = {
|
|
|
163
163
|
export declare function answerOf(call: ToolCall, run: () => Promise<ToolAnswer>): Promise<ToolAnswer>;
|
|
164
164
|
/** A host tool's run. */
|
|
165
165
|
export type Executor = (args: unknown, call: ToolCall) => Promise<ToolAnswer>;
|
|
166
|
-
/** What the host gives the registry, by tool name: read, write and edit, bash's own run, which the registry bounds
|
|
167
|
-
|
|
168
|
-
export type HostExecutors = Readonly<Partial<Record<"read" | "write" | "edit" | "bash" | "computer_script", Executor>>>;
|
|
166
|
+
/** What the host gives the registry, by tool name: read, write and edit, and bash's own run, which the registry bounds. */
|
|
167
|
+
export type HostExecutors = Readonly<Partial<Record<"read" | "write" | "edit" | "bash", Executor>>>;
|
|
@@ -2,7 +2,7 @@ export { BACKGROUND_EXECUTORS, endBackground, NEEDS_A_COMPUTER, NO_SHELL } from
|
|
|
2
2
|
export { boundedBash } from "./bash.js";
|
|
3
3
|
export { bashTimeoutWords, CAPABILITY_KINDS, hostToolDefs } from "./defs.js";
|
|
4
4
|
export type { ShellPlace, ToolDef } from "./defs.js";
|
|
5
|
-
export { COMPUTER_TOOLS, computerExecutors, computerToolDefs, computerToolsFor,
|
|
5
|
+
export { COMPUTER_OP_TOOL, COMPUTER_OPS, COMPUTER_TOOLS, computerExecutors, computerOpsFor, computerToolDefs, computerToolsFor, STEP_TOOL_OPS } from "./computer.js";
|
|
6
6
|
export { COMPUTER_TOOL_SCHEMAS, computerDeclarations } from "./computer-surface.generated.js";
|
|
7
7
|
export { defaultPermissionOf, SHELL_FAMILY, SHELL_WATCHERS, shellPermission, shellToolOf } from "./gate.js";
|
|
8
8
|
export type { Permission } from "./gate.js";
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
export { BACKGROUND_EXECUTORS, endBackground, NEEDS_A_COMPUTER, NO_SHELL } from "./background.js";
|
|
6
6
|
export { boundedBash } from "./bash.js";
|
|
7
7
|
export { bashTimeoutWords, CAPABILITY_KINDS, hostToolDefs } from "./defs.js";
|
|
8
|
-
export { COMPUTER_TOOLS, computerExecutors, computerToolDefs, computerToolsFor,
|
|
8
|
+
export { COMPUTER_OP_TOOL, COMPUTER_OPS, COMPUTER_TOOLS, computerExecutors, computerOpsFor, computerToolDefs, computerToolsFor, STEP_TOOL_OPS } from "./computer.js";
|
|
9
9
|
export { COMPUTER_TOOL_SCHEMAS, computerDeclarations } from "./computer-surface.generated.js";
|
|
10
10
|
export { defaultPermissionOf, SHELL_FAMILY, SHELL_WATCHERS, shellPermission, shellToolOf } from "./gate.js";
|
|
11
11
|
export { answerOf, exitCodeOf, needsKindWords } from "./hands.js";
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
// The kernel's one registry of host tools: the model-visible definitions (defs.ts), each with its executor. read,
|
|
2
2
|
// write and edit, and bash's own run, are the host's (pi's tools over the run's hands, until their semantics move
|
|
3
3
|
// here); the kernel bounds bash and runs the five background tools itself. A desktop or a phone adds the computer's
|
|
4
|
-
// tools after them (computer.ts), as its groups offer them, under its kind's settings key: observe, act
|
|
5
|
-
// run on the kernel's own executors over the hands'
|
|
6
|
-
//
|
|
4
|
+
// tools after them (computer.ts), as its groups offer them, under its kind's settings key: observe, act, screenshot,
|
|
5
|
+
// computer_op (a codemode script's every other op) and the hand-over run on the kernel's own executors over the hands'
|
|
6
|
+
// computer port. A registry missing an executor for a
|
|
7
|
+
// tool the model sees is refused when it is built,
|
|
7
8
|
// so a run never offers a tool it cannot run. A computer's tool the run does not have is refused in words that name
|
|
8
9
|
// the setting.
|
|
9
10
|
import { BACKGROUND_EXECUTORS, NEEDS_A_COMPUTER, NO_SHELL } from "./background.js";
|
|
10
11
|
import { boundedBash } from "./bash.js";
|
|
11
|
-
import { COMPUTER_TOOLS, computerExecutors, computerToolDefs, computerToolsFor
|
|
12
|
+
import { COMPUTER_TOOLS, computerExecutors, computerToolDefs, computerToolsFor } from "./computer.js";
|
|
12
13
|
import { CAPABILITY_KINDS, hostToolDefs } from "./defs.js";
|
|
13
14
|
import { answerOf, needsKindWords } from "./hands.js";
|
|
14
15
|
import { BACKGROUND_TOOLS } from "./limits.js";
|
|
@@ -36,11 +37,7 @@ export function createRegistry(options) {
|
|
|
36
37
|
for (const def of computerToolDefs(kind, groups)) {
|
|
37
38
|
if (!offered.includes(def.name))
|
|
38
39
|
continue;
|
|
39
|
-
|
|
40
|
-
// The script is the host's to run (its isolate): a host that gives none lists none.
|
|
41
|
-
if (!run)
|
|
42
|
-
continue;
|
|
43
|
-
runs.set(def.name, run);
|
|
40
|
+
runs.set(def.name, kernel[def.name]);
|
|
44
41
|
defs.push(def);
|
|
45
42
|
}
|
|
46
43
|
}
|
|
@@ -53,9 +53,12 @@ export declare function recoveryNote(state: SessionState, options?: RecoveryOpti
|
|
|
53
53
|
* The note as a harness carries it outside the user's turn (a system prompt, a developer item): from the harness, about the
|
|
54
54
|
* turn before, in the harness's own words only. It names each call and says how it ended, and never quotes what a tool was
|
|
55
55
|
* given or answered with (that is data the agent read, not something the harness may speak for), a prompt, or a pending message.
|
|
56
|
+
* `shown`: the calls whose result or error the conversation already carries, in the recovered output of the script that made
|
|
57
|
+
* them (Codex's code mode): for a completed or failed one the note says it is there, never that it is not shown. With none
|
|
58
|
+
* (every other harness), each call's output is not shown here, as it says.
|
|
56
59
|
* `undefined` when no turn is open.
|
|
57
60
|
*/
|
|
58
|
-
export declare function recoverySystemNote(state: SessionState): string | undefined;
|
|
61
|
+
export declare function recoverySystemNote(state: SessionState, shown?: ReadonlySet<string>): string | undefined;
|
|
59
62
|
/** The user's turn that resumes a cut-off turn: only the user's words (the prompts the session never got, then the messages sent since), or a bare nudge. */
|
|
60
63
|
export declare function recoveryUserTurn(lost: string[], pending: string[]): string;
|
|
61
64
|
/**
|
package/dist/process/recovery.js
CHANGED
|
@@ -42,13 +42,17 @@ export function recoveryNote(state, options = {}) {
|
|
|
42
42
|
* The note as a harness carries it outside the user's turn (a system prompt, a developer item): from the harness, about the
|
|
43
43
|
* turn before, in the harness's own words only. It names each call and says how it ended, and never quotes what a tool was
|
|
44
44
|
* given or answered with (that is data the agent read, not something the harness may speak for), a prompt, or a pending message.
|
|
45
|
+
* `shown`: the calls whose result or error the conversation already carries, in the recovered output of the script that made
|
|
46
|
+
* them (Codex's code mode): for a completed or failed one the note says it is there, never that it is not shown. With none
|
|
47
|
+
* (every other harness), each call's output is not shown here, as it says.
|
|
45
48
|
* `undefined` when no turn is open.
|
|
46
49
|
*/
|
|
47
|
-
export function recoverySystemNote(state) {
|
|
50
|
+
export function recoverySystemNote(state, shown = new Set()) {
|
|
48
51
|
const turn = openTurn(state);
|
|
49
52
|
if (!turn)
|
|
50
53
|
return undefined;
|
|
51
54
|
const calls = turn.calls.map((e) => recoveryCall(e, 0));
|
|
55
|
+
const carried = (c) => shown.has(c.callId) && (c.status === 'completed' || c.status === 'failed');
|
|
52
56
|
const lines = [
|
|
53
57
|
'The harness, not the user, adds this about the turn before this one:',
|
|
54
58
|
'[Recovery] Your previous turn was interrupted: the process running it stopped, and this session was resumed from its durable log.',
|
|
@@ -59,12 +63,20 @@ export function recoverySystemNote(state) {
|
|
|
59
63
|
else {
|
|
60
64
|
lines.push('What the log records about the tool calls since that turn began (none was run again):');
|
|
61
65
|
for (const c of calls)
|
|
62
|
-
lines.push(`- ${c.name}, call ${c.callId}: ${ended(c)}`);
|
|
63
|
-
|
|
66
|
+
lines.push(`- ${c.name}, call ${c.callId}: ${carried(c) ? endedShown(c) : ended(c)}`);
|
|
67
|
+
const unshown = calls.filter((c) => !carried(c)).length;
|
|
68
|
+
if (unshown === calls.length)
|
|
69
|
+
lines.push('', 'The output of those calls is not shown here. Check the actual state before relying on it.');
|
|
70
|
+
else if (unshown > 0)
|
|
71
|
+
lines.push('', 'The output of the other calls is not shown here. Check the actual state before relying on it.');
|
|
64
72
|
}
|
|
65
73
|
lines.push('', 'Check the actual state before repeating anything that may have taken effect, then carry on with the task.');
|
|
66
74
|
return lines.join('\n');
|
|
67
75
|
}
|
|
76
|
+
/** How a call whose outcome the conversation carries ended (`shown`, recoverySystemNote): where that outcome is. */
|
|
77
|
+
function endedShown(c) {
|
|
78
|
+
return c.status === 'failed' ? 'failed. Its error is in the output of the script that made the call, above.' : 'completed. Its result is in the output of the script that made the call, above.';
|
|
79
|
+
}
|
|
68
80
|
function ended(c) {
|
|
69
81
|
switch (c.status) {
|
|
70
82
|
case 'completed':
|
package/dist/process/resume.d.ts
CHANGED
|
@@ -13,8 +13,11 @@ export interface CallOutcome {
|
|
|
13
13
|
}
|
|
14
14
|
/** The dangling calls that never reached their intent: no effect of theirs in the drive's records. */
|
|
15
15
|
export declare function unstartedCalls(ctx: DriveContext, dangling: readonly DanglingCall[]): DanglingCall[];
|
|
16
|
+
/** The dangling calls whose end the drive's records hold: completed, or failed of their own accord (not cut off). */
|
|
17
|
+
export declare function endedCalls(ctx: DriveContext, dangling: readonly DanglingCall[]): DanglingCall[];
|
|
16
18
|
/**
|
|
17
|
-
* Each dangling call that never reached its intent through `bridge` (gate, intent, call, result),
|
|
18
|
-
* the
|
|
19
|
+
* Each dangling call that never reached its intent through `bridge` (gate, intent, call, result), and each one whose end
|
|
20
|
+
* the records hold read back through it (its memo: nothing runs, nothing is asked), then `settle`d into the CLI's
|
|
21
|
+
* transcript, one call after another in the transcript's order. A call cut off is left as it is.
|
|
19
22
|
*/
|
|
20
23
|
export declare function resumeUnstartedCalls(ctx: DriveContext, bridge: ToolBridge, dangling: readonly DanglingCall[], settle: (call: DanglingCall, outcome: CallOutcome) => Promise<void>): Promise<void>;
|
package/dist/process/resume.js
CHANGED
|
@@ -4,12 +4,24 @@ export function unstartedCalls(ctx, dangling) {
|
|
|
4
4
|
const effects = ctx.state().effects;
|
|
5
5
|
return dangling.filter((call) => !effects.has(toolKey(call.callId)));
|
|
6
6
|
}
|
|
7
|
+
/** The dangling calls whose end the drive's records hold: completed, or failed of their own accord (not cut off). */
|
|
8
|
+
export function endedCalls(ctx, dangling) {
|
|
9
|
+
const effects = ctx.state().effects;
|
|
10
|
+
return dangling.filter((call) => {
|
|
11
|
+
const effect = effects.get(toolKey(call.callId));
|
|
12
|
+
return effect?.status === 'completed' || (effect?.status === 'failed' && effect.error?.interrupted !== true);
|
|
13
|
+
});
|
|
14
|
+
}
|
|
7
15
|
/**
|
|
8
|
-
* Each dangling call that never reached its intent through `bridge` (gate, intent, call, result),
|
|
9
|
-
* the
|
|
16
|
+
* Each dangling call that never reached its intent through `bridge` (gate, intent, call, result), and each one whose end
|
|
17
|
+
* the records hold read back through it (its memo: nothing runs, nothing is asked), then `settle`d into the CLI's
|
|
18
|
+
* transcript, one call after another in the transcript's order. A call cut off is left as it is.
|
|
10
19
|
*/
|
|
11
20
|
export async function resumeUnstartedCalls(ctx, bridge, dangling, settle) {
|
|
12
|
-
|
|
21
|
+
const taken = new Set([...unstartedCalls(ctx, dangling), ...endedCalls(ctx, dangling)]);
|
|
22
|
+
for (const call of dangling) {
|
|
23
|
+
if (!taken.has(call))
|
|
24
|
+
continue;
|
|
13
25
|
const result = await bridge.call(call.tool, call.args, call.callId);
|
|
14
26
|
await settle(call, { text: result.text, isError: result.isError });
|
|
15
27
|
}
|
package/dist/process/tools.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type DriveContext, type EffectError, type HostTool, type ReplayPolicy } from '../kernel/index.js';
|
|
2
|
-
import { type GateAnswer, type GateCall, type JsonValue, type ToolImage } from '../types.js';
|
|
2
|
+
import { type GateAnswer, type GateCall, type HostTool as AdapterTool, type JsonValue, type ToolImage } from '../types.js';
|
|
3
3
|
/**
|
|
4
4
|
* The tool bridge: `HostTool`s as a harness-neutral list a port maps onto its
|
|
5
5
|
* harness's own tool mechanism (Codex's dynamic tools).
|
|
@@ -123,3 +123,28 @@ export declare function boundResult(text: string): {
|
|
|
123
123
|
};
|
|
124
124
|
/** At most `max` characters, with a marker when cut. */
|
|
125
125
|
export declare function clip(text: string, max: number): string;
|
|
126
|
+
/** What a harness adds to the one conversion of a host tool into the bridge's (`bridgeTool`): each is absent where it has none. */
|
|
127
|
+
export interface BridgeHooks {
|
|
128
|
+
/** The name the harness gives the tool (Codex's `mcp_` form for an MCP tool); with `nestedName` its inverse for a nested call. */
|
|
129
|
+
name?: string;
|
|
130
|
+
description?: string;
|
|
131
|
+
/** A tool that nests calls names them by the host's names; the bridge holds them by the harness's: this maps one to the other. */
|
|
132
|
+
nestedName?(name: string): string;
|
|
133
|
+
/** Run once the call's intent is in the records, before the tool runs: the harness's live `tool_start`, its pause check. A throw is the call's error. */
|
|
134
|
+
start?(call: {
|
|
135
|
+
callId: string;
|
|
136
|
+
attempt: number;
|
|
137
|
+
args: unknown;
|
|
138
|
+
}): void;
|
|
139
|
+
/** The problem with a result's images for this harness's model, or undefined: the call's error, by the tool's name and the image. */
|
|
140
|
+
imageProblems?(images: readonly ToolImage[]): string | undefined;
|
|
141
|
+
/** The tool parked the call for a person's approval: recorded so, before its answer. */
|
|
142
|
+
parked?(ctx: DriveContext, callId: string): Promise<unknown>;
|
|
143
|
+
}
|
|
144
|
+
/** A host tool (`call(args, context)`) as the bridge's tool (`execute(input, io)`), once for every harness: the same name,
|
|
145
|
+
* schema, replay policy, `nests` and `deferred`; the call runs as a kernel effect, whose intent is durable in the session's
|
|
146
|
+
* records before `execute` is entered, so a crash inside a `never` tool is always reported, never repeated. The effect's
|
|
147
|
+
* step is `tool:<the harness's call id>#<attempt>`, so the host sees the harness's call id and the attempt (above 0 when
|
|
148
|
+
* a crash cut an earlier one and the kernel runs the call again). A result that is an error is the call's failure; its
|
|
149
|
+
* value and images are recorded with the outcome (a script's nested call is answered them). */
|
|
150
|
+
export declare function bridgeTool(tool: AdapterTool, hooks?: BridgeHooks): HostTool;
|
package/dist/process/tools.js
CHANGED
|
@@ -56,17 +56,26 @@ export function toolBridge(ctx, tools, options = {}) {
|
|
|
56
56
|
/** The session's tools as calls nested in `outer` (NestedCalls): each through its own run, gate first, keyed
|
|
57
57
|
* `tool:<outer>/<key>`, so a rerun of `outer` gets a recorded one's answer and never runs it twice. */
|
|
58
58
|
const nestedIn = (outer, signal) => ({
|
|
59
|
-
async call(name, args, key) {
|
|
59
|
+
async call(name, args, key, own) {
|
|
60
60
|
const run = runs.get(name);
|
|
61
61
|
if (!run)
|
|
62
62
|
return { text: `unknown tool: ${name}`, isError: true, refused: true, fresh: true };
|
|
63
63
|
const callId = `${outer}/${key}`;
|
|
64
64
|
const fresh = !ctx.isMemoized(toolKey(callId));
|
|
65
|
-
const { interrupted, ...result } = await run(args, callId, signal, true);
|
|
65
|
+
const { interrupted, ...result } = await run(args, callId, own ? AbortSignal.any([signal, own]) : signal, true);
|
|
66
66
|
return { ...result, ...(interrupted ? { interrupted: true } : {}), fresh };
|
|
67
67
|
},
|
|
68
|
-
// A value the call's first run fixed, as a replay-safe effect of its own: its rerun reads the first one.
|
|
69
|
-
|
|
68
|
+
// A value the call's first run fixed, as a replay-safe effect of its own: its rerun reads the first one. A value given
|
|
69
|
+
// as a function is called only while none is recorded, and outside the effect, so a throw of its (a cap's refusal, a
|
|
70
|
+
// stop) is never recorded as the memo's outcome: the next run calls it again.
|
|
71
|
+
async memo(key, value) {
|
|
72
|
+
const memoKey = `nested:${outer}/${key}`;
|
|
73
|
+
const options = { name: 'nested.memo', replay: 'safe', input: null };
|
|
74
|
+
if (typeof value !== 'function' || ctx.isMemoized(memoKey))
|
|
75
|
+
return ctx.effect(memoKey, options, async () => value);
|
|
76
|
+
const fixed = await value();
|
|
77
|
+
return ctx.effect(memoKey, options, async () => fixed);
|
|
78
|
+
},
|
|
70
79
|
});
|
|
71
80
|
const bridged = tools.map((tool) => {
|
|
72
81
|
const run = async (args, callId, signal, nested) => {
|
|
@@ -202,3 +211,37 @@ export function boundResult(text) {
|
|
|
202
211
|
export function clip(text, max) {
|
|
203
212
|
return text.length <= max ? text : `${text.slice(0, max)}… [${text.length - max} more characters]`;
|
|
204
213
|
}
|
|
214
|
+
/** A host tool (`call(args, context)`) as the bridge's tool (`execute(input, io)`), once for every harness: the same name,
|
|
215
|
+
* schema, replay policy, `nests` and `deferred`; the call runs as a kernel effect, whose intent is durable in the session's
|
|
216
|
+
* records before `execute` is entered, so a crash inside a `never` tool is always reported, never repeated. The effect's
|
|
217
|
+
* step is `tool:<the harness's call id>#<attempt>`, so the host sees the harness's call id and the attempt (above 0 when
|
|
218
|
+
* a crash cut an earlier one and the kernel runs the call again). A result that is an error is the call's failure; its
|
|
219
|
+
* value and images are recorded with the outcome (a script's nested call is answered them). */
|
|
220
|
+
export function bridgeTool(tool, hooks = {}) {
|
|
221
|
+
return {
|
|
222
|
+
name: hooks.name ?? tool.name,
|
|
223
|
+
description: hooks.description ?? tool.description,
|
|
224
|
+
inputSchema: tool.inputSchema,
|
|
225
|
+
replay: tool.replay,
|
|
226
|
+
...(tool.nests ? { nests: true } : {}),
|
|
227
|
+
...(tool.deferred ? { deferred: true } : {}),
|
|
228
|
+
async execute(input, io) {
|
|
229
|
+
const callId = (io.stepId ?? '').replace(/^tool:/, '').replace(/#\d+$/, '');
|
|
230
|
+
const attempt = Number(/#(\d+)$/.exec(io.stepId ?? '')?.[1] ?? 0);
|
|
231
|
+
hooks.start?.({ callId, attempt, args: input });
|
|
232
|
+
const nested = io.nested && hooks.nestedName ? { call: (name, args, key, signal) => io.nested.call(hooks.nestedName(name), args, key, signal), memo: io.nested.memo } : io.nested;
|
|
233
|
+
const result = await tool.call(input, { callId, signal: io.signal, attempt, ...(nested ? { nested } : {}) });
|
|
234
|
+
if (result.parked && hooks.parked)
|
|
235
|
+
await hooks.parked(io.ctx, callId);
|
|
236
|
+
if (result.isError)
|
|
237
|
+
throw new Error(result.text);
|
|
238
|
+
const structured = result.structured !== undefined ? { structured: result.structured } : {};
|
|
239
|
+
if (!result.images?.length)
|
|
240
|
+
return result.structured === undefined ? result.text : { text: result.text, ...structured };
|
|
241
|
+
const wrong = hooks.imageProblems?.(result.images);
|
|
242
|
+
if (wrong)
|
|
243
|
+
throw new Error(wrong);
|
|
244
|
+
return { text: result.text, images: result.images.map((image) => ({ mimeType: image.mimeType, data: image.data })), ...structured };
|
|
245
|
+
},
|
|
246
|
+
};
|
|
247
|
+
}
|
package/dist/run-booking.d.ts
CHANGED
|
@@ -8,5 +8,24 @@ export interface RunSessions {
|
|
|
8
8
|
* cost, what is under way at the most it may take); null: unknown. None: nothing is. */
|
|
9
9
|
beside?(): Promise<number | null>;
|
|
10
10
|
}
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
export interface RunBookingOptions {
|
|
12
|
+
/** Whether the run has parked (a step refused for room it could not wait for, a paid call's refusal): a request waiting
|
|
13
|
+
* for room leaves the line refused in its own words, so nothing more is sent until a raise. */
|
|
14
|
+
parked?(): boolean;
|
|
15
|
+
/** How long a waiter waits with none of the steps it waits on settling before it is refused (default WAIT_STALL_MS). */
|
|
16
|
+
stallMs?: number;
|
|
17
|
+
/** The run's stop (a stop, a cancel, a lost claim): every waiter leaves the line at it, admitted for nothing. */
|
|
18
|
+
stopped?: AbortSignal;
|
|
19
|
+
}
|
|
20
|
+
/** How soon a waiter reads the run's records again, first and at the slowest (it doubles between them). */
|
|
21
|
+
export declare const WAIT_POLL_FIRST_MS = 50;
|
|
22
|
+
export declare const WAIT_POLL_MOST_MS = 1000;
|
|
23
|
+
/** How long a request waits for room with none of the steps it waits on settling: past it, it is refused for room and the
|
|
24
|
+
* run parks (a step that never settles, which nothing here expects, never hangs the run). */
|
|
25
|
+
export declare const WAIT_STALL_MS: number;
|
|
26
|
+
/** How long an admission may go unrecorded before no one waits on it: its record is written just after its admission,
|
|
27
|
+
* so one missing this long never will be (its write refused, its booking stopped). It is still held, never freed. */
|
|
28
|
+
export declare const RECORD_WAIT_MS = 30000;
|
|
29
|
+
/** `before`, asked about each request with the run's totals over `sessions` in place of its session's own; a refusal that
|
|
30
|
+
* waits is asked again once the room may be there (above). */
|
|
31
|
+
export declare function runBooking(sessions: RunSessions, before: BeforeRequest, options?: RunBookingOptions): BeforeRequest;
|