@code-yeongyu/senpi-agent-core 2026.11.4-2 → 2026.11.6-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent-loop.js +14 -140
- package/dist/agent.d.ts +4 -0
- package/dist/agent.js +4 -0
- package/dist/conformance/index.d.ts +12 -1
- package/dist/conformance/index.js +6 -0
- package/dist/conformance/loop-core-manifest.d.ts +1 -1
- package/dist/conformance/loop-core-manifest.js +18 -0
- package/dist/conformance/registry.js +11 -17
- package/dist/durable/session-store.d.ts +42 -0
- package/dist/durable/session-store.js +124 -0
- package/dist/durable/store-memory.d.ts +2 -2
- package/dist/durable/store-memory.js +36 -34
- package/dist/durable/store.d.ts +9 -0
- package/dist/durable/testing/store-harness.d.ts +1 -0
- package/dist/durable/testing/store-harness.js +1 -0
- package/dist/durable/testing/sync-commit-cases.d.ts +20 -0
- package/dist/durable/testing/sync-commit-cases.js +89 -0
- package/dist/egress/types.d.ts +102 -0
- package/dist/egress/types.js +7 -0
- package/dist/execution/canonical-digest.d.ts +10 -0
- package/dist/execution/canonical-digest.js +20 -0
- package/dist/execution/chain-count.d.ts +26 -0
- package/dist/execution/chain-count.js +45 -0
- package/dist/execution/coordinator-plan.d.ts +1 -2
- package/dist/execution/coordinator-plan.js +4 -17
- package/dist/execution/device-files-node.js +40 -1
- package/dist/execution/device-files.d.ts +39 -1
- package/dist/execution/device-files.js +48 -1
- package/dist/execution/ledger-types.d.ts +3 -1
- package/dist/execution/ledger.d.ts +9 -3
- package/dist/execution/ledger.js +8 -2
- package/dist/execution/loop-hook-host-hand.d.ts +33 -0
- package/dist/execution/loop-hook-host-hand.js +71 -0
- package/dist/execution/loop-hook-host-protocol.d.ts +46 -0
- package/dist/execution/loop-hook-host-protocol.js +47 -0
- package/dist/execution/loop-hook-host.d.ts +40 -0
- package/dist/execution/loop-hook-host.js +87 -0
- package/dist/execution/plane-session.d.ts +56 -0
- package/dist/execution/plane-session.js +178 -0
- package/dist/execution/plane.d.ts +5 -2
- package/dist/execution/plane.js +1 -1
- package/dist/execution/prepared/executor-body.d.ts +22 -0
- package/dist/execution/prepared/executor-body.js +73 -0
- package/dist/execution/prepared/executor-freeze.d.ts +22 -0
- package/dist/execution/prepared/executor-freeze.js +55 -0
- package/dist/execution/prepared/executor-pipeline.d.ts +11 -0
- package/dist/execution/prepared/executor-pipeline.js +26 -0
- package/dist/execution/prepared/executor-types.d.ts +131 -0
- package/dist/execution/prepared/executor-types.js +2 -0
- package/dist/execution/prepared/executor.d.ts +8 -0
- package/dist/execution/prepared/executor.js +260 -0
- package/dist/execution/prepared/memory-batch-store.d.ts +4 -0
- package/dist/execution/prepared/memory-batch-store.js +4 -0
- package/dist/execution/prepared/pipeline-prepared-batch.d.ts +27 -0
- package/dist/execution/prepared/pipeline-prepared-batch.js +175 -0
- package/dist/execution/prepared/pipeline-prepared.d.ts +10 -0
- package/dist/execution/prepared/pipeline-prepared.js +54 -0
- package/dist/execution/remote-device-files.js +33 -4
- package/dist/execution/remote-epoch-gate.d.ts +19 -0
- package/dist/execution/remote-epoch-gate.js +46 -0
- package/dist/execution/remote-epoch.d.ts +53 -0
- package/dist/execution/remote-epoch.js +74 -0
- package/dist/execution/remote-executor.d.ts +8 -1
- package/dist/execution/remote-executor.js +24 -6
- package/dist/execution/remote-file-call.js +1 -0
- package/dist/execution/remote-hand-ledger.d.ts +11 -3
- package/dist/execution/remote-hand-ledger.js +31 -15
- package/dist/execution/remote-link.d.ts +6 -1
- package/dist/execution/remote-link.js +50 -14
- package/dist/execution/remote-protocol.d.ts +30 -1
- package/dist/execution/remote-protocol.js +30 -0
- package/dist/execution/remote-tool-host.d.ts +12 -0
- package/dist/execution/remote-tool-host.js +3 -1
- package/dist/execution/testing/epoch-cases.d.ts +8 -0
- package/dist/execution/testing/epoch-cases.js +84 -0
- package/dist/execution/testing/epoch-harness.d.ts +47 -0
- package/dist/execution/testing/epoch-harness.js +172 -0
- package/dist/execution/testing/epoch-registry.d.ts +3 -0
- package/dist/execution/testing/epoch-registry.js +17 -0
- package/dist/execution/testing/hand-harness.d.ts +5 -0
- package/dist/execution/testing/hand-harness.js +57 -24
- package/dist/execution/testing/hand-registry.d.ts +3 -0
- package/dist/execution/testing/hand-registry.js +20 -0
- package/dist/execution/testing/kill-epoch.d.ts +9 -0
- package/dist/execution/testing/kill-epoch.js +71 -0
- package/dist/execution/testing/kill-hook.d.ts +14 -0
- package/dist/execution/testing/kill-hook.js +117 -0
- package/dist/execution/testing/multi-epoch-cases.d.ts +4 -0
- package/dist/execution/testing/multi-epoch-cases.js +29 -0
- package/dist/execution/testing/multi-hand-cases.js +1 -1
- package/dist/execution/tool-host.d.ts +16 -1
- package/dist/execution/tool-host.js +18 -1
- package/dist/harness/agent-harness.d.ts +6 -0
- package/dist/harness/execution/tool-host-call.d.ts +14 -0
- package/dist/harness/execution/tool-host-call.js +56 -0
- package/dist/harness/execution/tools.d.ts +2 -1
- package/dist/harness/execution/tools.js +45 -17
- package/dist/harness/runtime/drive/tools.js +1 -1
- package/dist/harness/runtime/harness.js +1 -0
- package/dist/harness/runtime/types.d.ts +1 -0
- package/dist/harness/tools/read.js +1 -1
- package/dist/index.d.ts +19 -2
- package/dist/index.js +15 -1
- package/dist/public-entry.d.ts +10 -0
- package/dist/public-entry.js +23 -0
- package/dist/step-commit.d.ts +24 -0
- package/dist/step-commit.js +24 -0
- package/dist/tool-name-alias.d.ts +1 -7
- package/dist/tool-name-alias.js +0 -17
- package/dist/types.d.ts +37 -1
- package/package.json +3 -3
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { JsonValue } from "@earendil-works/pi-ai";
|
|
2
|
+
import type { Transport } from "../transport/transport.ts";
|
|
3
|
+
/** Runs the user's hooks for one loop event on this machine and returns what they produced. */
|
|
4
|
+
export type LoopHookRunner = (event: string, payload: JsonValue) => Promise<JsonValue>;
|
|
5
|
+
/** The points of one hook call on the hand, in order; a kill-point probe may stop the process at any of them. */
|
|
6
|
+
export type LoopHookPoint = "received" | "started" | "ran" | "replied";
|
|
7
|
+
export interface LoopHookHandOptions {
|
|
8
|
+
/**
|
|
9
|
+
* The user machine's end of the link to the loop host. It must outlive this process (the machine's long-lived link
|
|
10
|
+
* endpoint): a `started` still unacknowledged when the endpoint dies is lost, and the loop host then sends the call
|
|
11
|
+
* again to the replacement process, which runs the hooks a second time.
|
|
12
|
+
*/
|
|
13
|
+
readonly transport: Transport;
|
|
14
|
+
readonly lane: string;
|
|
15
|
+
readonly run: LoopHookRunner;
|
|
16
|
+
/** This process's identity; a new one per process start. */
|
|
17
|
+
readonly instance?: string;
|
|
18
|
+
/** Test-only probe: throwing here stops this process at that point, as a kill does. */
|
|
19
|
+
readonly onPoint?: (point: LoopHookPoint, callId: string) => void;
|
|
20
|
+
readonly onFault?: (error: unknown) => void;
|
|
21
|
+
}
|
|
22
|
+
export interface LoopHookHand {
|
|
23
|
+
/** Resolves once this process announced itself to the loop host. */
|
|
24
|
+
readonly ready: Promise<void>;
|
|
25
|
+
close(): void;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The user machine's side of the loop-hook link (S33): runs each hook call once per process and answers a repeated call
|
|
29
|
+
* from its stored answer. It announces itself first, and says `started` before any hook runs, so the loop host can tell
|
|
30
|
+
* a call this process never began (safe to send again) from one it may have run (never sent again).
|
|
31
|
+
*/
|
|
32
|
+
export declare function serveLoopHooks(options: LoopHookHandOptions): LoopHookHand;
|
|
33
|
+
//# sourceMappingURL=loop-hook-host-hand.d.ts.map
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { decodeLoopHookMessage, encodeLoopHookMessage, } from "./loop-hook-host-protocol.js";
|
|
2
|
+
/**
|
|
3
|
+
* The user machine's side of the loop-hook link (S33): runs each hook call once per process and answers a repeated call
|
|
4
|
+
* from its stored answer. It announces itself first, and says `started` before any hook runs, so the loop host can tell
|
|
5
|
+
* a call this process never began (safe to send again) from one it may have run (never sent again).
|
|
6
|
+
*/
|
|
7
|
+
export function serveLoopHooks(options) {
|
|
8
|
+
const { transport, lane } = options;
|
|
9
|
+
const instance = options.instance ?? crypto.randomUUID();
|
|
10
|
+
const answers = new Map();
|
|
11
|
+
let dead = false;
|
|
12
|
+
const send = (message) => transport.send(lane, encodeLoopHookMessage(message));
|
|
13
|
+
const point = (at, callId) => {
|
|
14
|
+
if (dead)
|
|
15
|
+
throw new Error("this hand process was stopped");
|
|
16
|
+
try {
|
|
17
|
+
options.onPoint?.(at, callId);
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
dead = true;
|
|
21
|
+
throw error;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
const serve = async (call) => {
|
|
25
|
+
if (answers.has(call.callId)) {
|
|
26
|
+
const answer = answers.get(call.callId);
|
|
27
|
+
if (answer !== undefined)
|
|
28
|
+
await send(answer);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
point("received", call.callId);
|
|
32
|
+
answers.set(call.callId, undefined);
|
|
33
|
+
await send({ kind: "hook.started", callId: call.callId, instance });
|
|
34
|
+
point("started", call.callId);
|
|
35
|
+
let answer;
|
|
36
|
+
try {
|
|
37
|
+
answer = { kind: "hook.reply", callId: call.callId, result: await options.run(call.event, call.payload) };
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
answer = {
|
|
41
|
+
kind: "hook.failed",
|
|
42
|
+
callId: call.callId,
|
|
43
|
+
error: error instanceof Error ? error.message : String(error),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
point("ran", call.callId);
|
|
47
|
+
answers.set(call.callId, answer);
|
|
48
|
+
await send(answer);
|
|
49
|
+
point("replied", call.callId);
|
|
50
|
+
};
|
|
51
|
+
transport.onFrame(async (frame) => {
|
|
52
|
+
if (dead)
|
|
53
|
+
return;
|
|
54
|
+
const message = decodeLoopHookMessage(frame.payload);
|
|
55
|
+
if (message?.kind !== "hook.call")
|
|
56
|
+
return;
|
|
57
|
+
try {
|
|
58
|
+
await serve(message);
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
options.onFault?.(error);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
return {
|
|
65
|
+
ready: send({ kind: "hook.hello", instance }),
|
|
66
|
+
close() {
|
|
67
|
+
dead = true;
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=loop-hook-host-hand.js.map
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { JsonValue } from "@earendil-works/pi-ai";
|
|
2
|
+
/**
|
|
3
|
+
* The loop-hook link (brain/hand split S33, decisions C8, K5, K7): when the loop runs away from the user's machine, the
|
|
4
|
+
* loop host asks that machine to run the user's loop hooks (shell hooks and the hook halves of extensions) and waits for
|
|
5
|
+
* the result before it builds the provider request. Every message travels on one lane, so a hook's `started` reaches
|
|
6
|
+
* the loop host before the `hello` of a hand process that replaced the one that started it, PROVIDED the hand's
|
|
7
|
+
* transport endpoint outlives the hand process: `started` is in the endpoint's send window once `send` resolves, not yet
|
|
8
|
+
* acknowledged, and the transport delivers it only while both endpoints live. If the endpoint dies with the process and
|
|
9
|
+
* takes an unacknowledged `started` with it, the loop host never learns the call began and sends it again to the
|
|
10
|
+
* replacement, so a hook that had run runs a second time. At-most-once dispatch therefore holds only on a hand whose
|
|
11
|
+
* endpoint is the machine's long-lived link endpoint, not one owned by the hand process.
|
|
12
|
+
*/
|
|
13
|
+
export type LoopHookMessage = LoopHookCall | LoopHookHello | LoopHookStarted | LoopHookReply | LoopHookFailed;
|
|
14
|
+
/** Loop host to hand: run the hooks for `event`. A re-send after a hand restart carries the same `callId`. */
|
|
15
|
+
export interface LoopHookCall {
|
|
16
|
+
readonly kind: "hook.call";
|
|
17
|
+
readonly callId: string;
|
|
18
|
+
readonly event: string;
|
|
19
|
+
readonly payload: JsonValue;
|
|
20
|
+
}
|
|
21
|
+
/** Hand to loop host, once per hand process, before it serves anything. */
|
|
22
|
+
export interface LoopHookHello {
|
|
23
|
+
readonly kind: "hook.hello";
|
|
24
|
+
readonly instance: string;
|
|
25
|
+
}
|
|
26
|
+
/** Hand to loop host, sent before the hooks run: from here on the call may have had effects. */
|
|
27
|
+
export interface LoopHookStarted {
|
|
28
|
+
readonly kind: "hook.started";
|
|
29
|
+
readonly callId: string;
|
|
30
|
+
readonly instance: string;
|
|
31
|
+
}
|
|
32
|
+
export interface LoopHookReply {
|
|
33
|
+
readonly kind: "hook.reply";
|
|
34
|
+
readonly callId: string;
|
|
35
|
+
readonly result: JsonValue;
|
|
36
|
+
}
|
|
37
|
+
/** The hooks ran and failed; the message is the hook's own. */
|
|
38
|
+
export interface LoopHookFailed {
|
|
39
|
+
readonly kind: "hook.failed";
|
|
40
|
+
readonly callId: string;
|
|
41
|
+
readonly error: string;
|
|
42
|
+
}
|
|
43
|
+
export declare function encodeLoopHookMessage(message: LoopHookMessage): Uint8Array;
|
|
44
|
+
/** Parses one frame payload as a loop-hook message; anything else is `undefined`. */
|
|
45
|
+
export declare function decodeLoopHookMessage(payload: Uint8Array): LoopHookMessage | undefined;
|
|
46
|
+
//# sourceMappingURL=loop-hook-host-protocol.d.ts.map
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
const KINDS = [
|
|
2
|
+
"hook.call",
|
|
3
|
+
"hook.hello",
|
|
4
|
+
"hook.started",
|
|
5
|
+
"hook.reply",
|
|
6
|
+
"hook.failed",
|
|
7
|
+
];
|
|
8
|
+
const encoder = new TextEncoder();
|
|
9
|
+
const decoder = new TextDecoder();
|
|
10
|
+
export function encodeLoopHookMessage(message) {
|
|
11
|
+
return encoder.encode(JSON.stringify(message));
|
|
12
|
+
}
|
|
13
|
+
const isRecord = (value) => typeof value === "object" && value !== null;
|
|
14
|
+
const isId = (value) => typeof value === "string" && value.length > 0;
|
|
15
|
+
/** Parses one frame payload as a loop-hook message; anything else is `undefined`. */
|
|
16
|
+
export function decodeLoopHookMessage(payload) {
|
|
17
|
+
let value;
|
|
18
|
+
try {
|
|
19
|
+
value = JSON.parse(decoder.decode(payload));
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
if (!isRecord(value) || !KINDS.includes(value.kind))
|
|
25
|
+
return undefined;
|
|
26
|
+
switch (value.kind) {
|
|
27
|
+
case "hook.call":
|
|
28
|
+
return isId(value.callId) && isId(value.event) && "payload" in value
|
|
29
|
+
? { kind: "hook.call", callId: value.callId, event: value.event, payload: value.payload }
|
|
30
|
+
: undefined;
|
|
31
|
+
case "hook.hello":
|
|
32
|
+
return isId(value.instance) ? { kind: "hook.hello", instance: value.instance } : undefined;
|
|
33
|
+
case "hook.started":
|
|
34
|
+
return isId(value.callId) && isId(value.instance)
|
|
35
|
+
? { kind: "hook.started", callId: value.callId, instance: value.instance }
|
|
36
|
+
: undefined;
|
|
37
|
+
case "hook.reply":
|
|
38
|
+
return isId(value.callId) && "result" in value
|
|
39
|
+
? { kind: "hook.reply", callId: value.callId, result: value.result }
|
|
40
|
+
: undefined;
|
|
41
|
+
default:
|
|
42
|
+
return isId(value.callId) && typeof value.error === "string"
|
|
43
|
+
? { kind: "hook.failed", callId: value.callId, error: value.error }
|
|
44
|
+
: undefined;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=loop-hook-host-protocol.js.map
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { JsonValue } from "@earendil-works/pi-ai";
|
|
2
|
+
import type { Transport } from "../transport/transport.ts";
|
|
3
|
+
/**
|
|
4
|
+
* The loop host's side of the loop-hook link (brain/hand split S33; decisions C8, K5, K7). When the loop runs on a
|
|
5
|
+
* machine that is not the user's (a hosted loop), the user's loop hooks run on the user's machine: `run` sends one hook
|
|
6
|
+
* call there and resolves with its result, and the caller builds the provider request only from that result, so no
|
|
7
|
+
* provider request leaves without it. While that machine is unreachable the call simply waits (the work is paused), and
|
|
8
|
+
* the transport delivers it once the machine is back, so it resumes exactly once. A fused or split-local session never
|
|
9
|
+
* builds this host: its hooks run in its own process, as today.
|
|
10
|
+
*
|
|
11
|
+
* Restarts: a call the previous hand process never started is sent again, with the same ID, to the new one. A call it
|
|
12
|
+
* had started (and so may have run) is never sent again: it ends with {@link LoopHookRestartedError}. "Started" is what
|
|
13
|
+
* this host received, so this holds only while the hand's transport endpoint outlives the hand process (see
|
|
14
|
+
* `loop-hook-host-protocol.ts`).
|
|
15
|
+
*/
|
|
16
|
+
export interface LoopHookHost {
|
|
17
|
+
/** Runs the user's hooks for `event` on the user's machine. Rejects with the hook's failure, a restart or an abort. */
|
|
18
|
+
run(event: string, payload: JsonValue, signal?: AbortSignal): Promise<JsonValue>;
|
|
19
|
+
/** IDs of the calls still waiting for the user's machine. */
|
|
20
|
+
waiting(): readonly string[];
|
|
21
|
+
close(): void;
|
|
22
|
+
}
|
|
23
|
+
export interface LoopHookHostOptions {
|
|
24
|
+
/** The loop host's end of the link to the user's machine. */
|
|
25
|
+
readonly transport: Transport;
|
|
26
|
+
/** The lane the link uses; one per session. */
|
|
27
|
+
readonly lane: string;
|
|
28
|
+
readonly mintId?: () => string;
|
|
29
|
+
}
|
|
30
|
+
/** The hooks ran on the user's machine and failed there; the message is the hook's own. */
|
|
31
|
+
export declare class LoopHookFailedError extends Error {
|
|
32
|
+
constructor(message: string);
|
|
33
|
+
}
|
|
34
|
+
/** The hand process that started the hooks was replaced before it answered; they may have run. Never re-run. */
|
|
35
|
+
export declare class LoopHookRestartedError extends Error {
|
|
36
|
+
readonly uncertain = true;
|
|
37
|
+
constructor(event: string);
|
|
38
|
+
}
|
|
39
|
+
export declare function createLoopHookHost(options: LoopHookHostOptions): LoopHookHost;
|
|
40
|
+
//# sourceMappingURL=loop-hook-host.d.ts.map
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { decodeLoopHookMessage, encodeLoopHookMessage } from "./loop-hook-host-protocol.js";
|
|
2
|
+
/** The hooks ran on the user's machine and failed there; the message is the hook's own. */
|
|
3
|
+
export class LoopHookFailedError extends Error {
|
|
4
|
+
constructor(message) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = "LoopHookFailedError";
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
/** The hand process that started the hooks was replaced before it answered; they may have run. Never re-run. */
|
|
10
|
+
export class LoopHookRestartedError extends Error {
|
|
11
|
+
constructor(event) {
|
|
12
|
+
super(`The user's machine restarted while it ran the ${event} hooks; they were not run again and may have run.`);
|
|
13
|
+
this.uncertain = true;
|
|
14
|
+
this.name = "LoopHookRestartedError";
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export function createLoopHookHost(options) {
|
|
18
|
+
const { transport, lane } = options;
|
|
19
|
+
const mintId = options.mintId ?? (() => crypto.randomUUID());
|
|
20
|
+
const pending = new Map();
|
|
21
|
+
let closed = false;
|
|
22
|
+
const send = (call) => {
|
|
23
|
+
transport.send(lane, encodeLoopHookMessage(call)).catch((error) => {
|
|
24
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
25
|
+
settle(call.callId, (waiter) => waiter.reject(new Error(`The link to the user's machine closed: ${reason}`)));
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
const settle = (callId, finish) => {
|
|
29
|
+
const waiter = pending.get(callId);
|
|
30
|
+
if (waiter === undefined)
|
|
31
|
+
return;
|
|
32
|
+
pending.delete(callId);
|
|
33
|
+
finish(waiter);
|
|
34
|
+
};
|
|
35
|
+
transport.onFrame((frame) => {
|
|
36
|
+
const message = decodeLoopHookMessage(frame.payload);
|
|
37
|
+
if (message === undefined)
|
|
38
|
+
return;
|
|
39
|
+
switch (message.kind) {
|
|
40
|
+
case "hook.started": {
|
|
41
|
+
const waiter = pending.get(message.callId);
|
|
42
|
+
if (waiter !== undefined)
|
|
43
|
+
waiter.startedBy = message.instance;
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
case "hook.reply":
|
|
47
|
+
return settle(message.callId, (waiter) => waiter.resolve(message.result));
|
|
48
|
+
case "hook.failed":
|
|
49
|
+
return settle(message.callId, (waiter) => waiter.reject(new LoopHookFailedError(message.error)));
|
|
50
|
+
case "hook.hello":
|
|
51
|
+
for (const waiter of [...pending.values()]) {
|
|
52
|
+
if (waiter.startedBy === undefined)
|
|
53
|
+
send(waiter.call);
|
|
54
|
+
else if (waiter.startedBy !== message.instance)
|
|
55
|
+
settle(waiter.call.callId, (stale) => stale.reject(new LoopHookRestartedError(stale.call.event)));
|
|
56
|
+
}
|
|
57
|
+
return;
|
|
58
|
+
case "hook.call":
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
return {
|
|
63
|
+
run(event, payload, signal) {
|
|
64
|
+
if (closed)
|
|
65
|
+
return Promise.reject(new Error("The loop-hook link is closed"));
|
|
66
|
+
if (signal?.aborted)
|
|
67
|
+
return Promise.reject(new Error("Operation aborted"));
|
|
68
|
+
const call = { kind: "hook.call", callId: mintId(), event, payload };
|
|
69
|
+
const result = new Promise((resolve, reject) => {
|
|
70
|
+
pending.set(call.callId, { call, resolve, reject });
|
|
71
|
+
});
|
|
72
|
+
const onAbort = () => settle(call.callId, (waiter) => waiter.reject(new Error("Operation aborted")));
|
|
73
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
74
|
+
void result.then(() => signal?.removeEventListener("abort", onAbort), () => signal?.removeEventListener("abort", onAbort));
|
|
75
|
+
send(call);
|
|
76
|
+
return result;
|
|
77
|
+
},
|
|
78
|
+
waiting: () => [...pending.keys()],
|
|
79
|
+
close() {
|
|
80
|
+
closed = true;
|
|
81
|
+
for (const callId of [...pending.keys()])
|
|
82
|
+
settle(callId, (waiter) => waiter.reject(new Error("The loop-hook link was closed before the hooks ran")));
|
|
83
|
+
transport.close();
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=loop-hook-host.js.map
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { AgentToolResult } from "../types.ts";
|
|
2
|
+
import type { AgentLoopConfig } from "../types.ts";
|
|
3
|
+
import { type NestedRequest, type PlaneHandler } from "./plane.ts";
|
|
4
|
+
import { type ToolHost } from "./tool-host.ts";
|
|
5
|
+
/**
|
|
6
|
+
* The session plane (brain/hand split, S32): tools that act on the session (todo, thread tools, task/subagent spawn,
|
|
7
|
+
* `ask_user`, and the other built-ins declared `runs: "brain"`) run where the loop runs. A model call to one of them
|
|
8
|
+
* never reaches the hand, and a hand-run tool that needs one asks the brain that owns its call on this plane.
|
|
9
|
+
*/
|
|
10
|
+
export declare const SESSION_PLANE = "session";
|
|
11
|
+
/** One loop-side tool call a hand-run tool asks the brain for. */
|
|
12
|
+
export interface SessionPlaneRequest {
|
|
13
|
+
readonly tool: string;
|
|
14
|
+
readonly arguments: Readonly<Record<string, unknown>>;
|
|
15
|
+
}
|
|
16
|
+
/** Parses a request body, keeping only the fields of {@link SessionPlaneRequest}; refuses a malformed one. */
|
|
17
|
+
export declare function decodeSessionPlaneRequest(body: unknown): SessionPlaneRequest;
|
|
18
|
+
/** The registered tools a split session runs loop-side: exactly those declared `runs: "brain"`. */
|
|
19
|
+
export declare function loopSideToolNames(tools: Iterable<{
|
|
20
|
+
readonly name: string;
|
|
21
|
+
readonly runs?: "brain" | "hand";
|
|
22
|
+
}>): ReadonlySet<string>;
|
|
23
|
+
/**
|
|
24
|
+
* The brain's session plane: runs one loop-side tool call with the session's own tools and returns its result. A tool
|
|
25
|
+
* that is not loop-side is refused `not_loop_side`: a hand's tools never run on the brain through this plane.
|
|
26
|
+
*/
|
|
27
|
+
export declare function serveSessionPlane(options: {
|
|
28
|
+
readonly loopSide: ReadonlySet<string>;
|
|
29
|
+
readonly run: (request: SessionPlaneRequest, signal: AbortSignal) => Promise<AgentToolResult<unknown>>;
|
|
30
|
+
}): PlaneHandler;
|
|
31
|
+
/** A hand's client for the session plane, over the running call's nested requests. */
|
|
32
|
+
export declare function createSessionPlaneClient(nested: NestedRequest): {
|
|
33
|
+
call(request: SessionPlaneRequest, signal?: AbortSignal): Promise<AgentToolResult<unknown>>;
|
|
34
|
+
};
|
|
35
|
+
type LoopToolSource = Pick<AgentLoopConfig, "resolveUnknownToolCall" | "beforeToolCall" | "afterToolCall" | "removedToolHints" | "afterToolFreeze" | "batchStoreFactory">;
|
|
36
|
+
/**
|
|
37
|
+
* The loop's own pipeline as a ToolHost (the prepared pipeline a fused session runs), for a split session's loop-side
|
|
38
|
+
* calls. It reads `agent`'s hooks and batch store at each batch, so hooks installed after it was made still apply.
|
|
39
|
+
*/
|
|
40
|
+
export declare function createLoopToolHost(agent: LoopToolSource): ToolHost;
|
|
41
|
+
/**
|
|
42
|
+
* The ToolHost of a split session: each call to a loop-side tool runs on `loop` (the session's own pipeline, in its
|
|
43
|
+
* process) and every other call on `hand`. A batch of one side goes to that host whole. A mixed batch keeps the mode:
|
|
44
|
+
* `parallel` runs both sides at once and emits the result messages in source order once both finished, as the fused
|
|
45
|
+
* pipeline does; `sequential` runs each run of same-side calls in source order and starts none after an abort. Results come
|
|
46
|
+
* back in the model's source order and the batch terminates only when every call asked to.
|
|
47
|
+
*/
|
|
48
|
+
export declare function createSplitToolHost(options: {
|
|
49
|
+
readonly loopSide: (toolName: string) => boolean;
|
|
50
|
+
/** The tools declared `executionMode: "sequential"`: in a parallel batch each waits for every earlier call. */
|
|
51
|
+
readonly isSequential: (toolName: string) => boolean;
|
|
52
|
+
readonly loop: ToolHost;
|
|
53
|
+
readonly hand: ToolHost;
|
|
54
|
+
}): ToolHost;
|
|
55
|
+
export {};
|
|
56
|
+
//# sourceMappingURL=plane-session.d.ts.map
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { PlaneRefusedError } from "./plane.js";
|
|
2
|
+
import { createPreparedToolHost } from "./prepared/pipeline-prepared.js";
|
|
3
|
+
import { BatchStoreMissingError, PreflightCarryMissingError, } from "./tool-host.js";
|
|
4
|
+
/**
|
|
5
|
+
* The session plane (brain/hand split, S32): tools that act on the session (todo, thread tools, task/subagent spawn,
|
|
6
|
+
* `ask_user`, and the other built-ins declared `runs: "brain"`) run where the loop runs. A model call to one of them
|
|
7
|
+
* never reaches the hand, and a hand-run tool that needs one asks the brain that owns its call on this plane.
|
|
8
|
+
*/
|
|
9
|
+
export const SESSION_PLANE = "session";
|
|
10
|
+
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
11
|
+
/** Parses a request body, keeping only the fields of {@link SessionPlaneRequest}; refuses a malformed one. */
|
|
12
|
+
export function decodeSessionPlaneRequest(body) {
|
|
13
|
+
if (!isRecord(body) || typeof body.tool !== "string" || body.tool.length === 0)
|
|
14
|
+
throw new PlaneRefusedError("malformed_request: tool");
|
|
15
|
+
if (!isRecord(body.arguments))
|
|
16
|
+
throw new PlaneRefusedError("malformed_request: arguments");
|
|
17
|
+
return { tool: body.tool, arguments: body.arguments };
|
|
18
|
+
}
|
|
19
|
+
/** The registered tools a split session runs loop-side: exactly those declared `runs: "brain"`. */
|
|
20
|
+
export function loopSideToolNames(tools) {
|
|
21
|
+
const names = new Set();
|
|
22
|
+
for (const tool of tools)
|
|
23
|
+
if (tool.runs === "brain")
|
|
24
|
+
names.add(tool.name);
|
|
25
|
+
return names;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The brain's session plane: runs one loop-side tool call with the session's own tools and returns its result. A tool
|
|
29
|
+
* that is not loop-side is refused `not_loop_side`: a hand's tools never run on the brain through this plane.
|
|
30
|
+
*/
|
|
31
|
+
export function serveSessionPlane(options) {
|
|
32
|
+
return async (body, signal) => {
|
|
33
|
+
const request = decodeSessionPlaneRequest(body);
|
|
34
|
+
if (!options.loopSide.has(request.tool))
|
|
35
|
+
throw new Error(`not_loop_side: ${request.tool}`);
|
|
36
|
+
const result = await options.run(request, signal);
|
|
37
|
+
return JSON.parse(JSON.stringify(result));
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/** A hand's client for the session plane, over the running call's nested requests. */
|
|
41
|
+
export function createSessionPlaneClient(nested) {
|
|
42
|
+
return {
|
|
43
|
+
async call(request, signal) {
|
|
44
|
+
const reply = await nested(SESSION_PLANE, JSON.parse(JSON.stringify(request)), signal);
|
|
45
|
+
if (!isRecord(reply) || !Array.isArray(reply.content))
|
|
46
|
+
throw new PlaneRefusedError("malformed_reply");
|
|
47
|
+
return reply;
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The loop's own pipeline as a ToolHost (the prepared pipeline a fused session runs), for a split session's loop-side
|
|
53
|
+
* calls. It reads `agent`'s hooks and batch store at each batch, so hooks installed after it was made still apply.
|
|
54
|
+
*/
|
|
55
|
+
export function createLoopToolHost(agent) {
|
|
56
|
+
return {
|
|
57
|
+
execute(call, options) {
|
|
58
|
+
const { batchStoreFactory, afterToolFreeze } = agent;
|
|
59
|
+
if (batchStoreFactory === undefined)
|
|
60
|
+
throw new BatchStoreMissingError();
|
|
61
|
+
if (afterToolFreeze === undefined)
|
|
62
|
+
throw new PreflightCarryMissingError();
|
|
63
|
+
return createPreparedToolHost({ ...pick(agent), afterToolFreeze }, batchStoreFactory).execute(call, options);
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function pick(agent) {
|
|
68
|
+
return {
|
|
69
|
+
resolveUnknownToolCall: agent.resolveUnknownToolCall,
|
|
70
|
+
beforeToolCall: agent.beforeToolCall,
|
|
71
|
+
afterToolCall: agent.afterToolCall,
|
|
72
|
+
removedToolHints: agent.removedToolHints,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Splits a batch into stages run one after another, each a set of same-side segments run at once; `owner[i]` is the
|
|
77
|
+
* segment that runs call `i`. `parallel` keeps the loop's dependency order (`batchDependencies`): a sequential call is a
|
|
78
|
+
* stage of its own, so it waits for every earlier call and every later call waits for it; the calls between two
|
|
79
|
+
* sequential calls share one stage. Otherwise each run of same-side calls is a stage.
|
|
80
|
+
*/
|
|
81
|
+
function stagesOf(calls, sideOf, isSequential, parallel) {
|
|
82
|
+
const stages = [];
|
|
83
|
+
const segments = [];
|
|
84
|
+
const owner = [];
|
|
85
|
+
let open = false;
|
|
86
|
+
for (const call of calls) {
|
|
87
|
+
const side = sideOf(call);
|
|
88
|
+
const barrier = parallel && isSequential(call);
|
|
89
|
+
const stage = stages.at(-1);
|
|
90
|
+
const joinable = open && !barrier && stage !== undefined && (parallel || stage[0]?.side === side);
|
|
91
|
+
if (!joinable)
|
|
92
|
+
stages.push([]);
|
|
93
|
+
const current = stages.at(-1) ?? [];
|
|
94
|
+
let segment = current.find((entry) => entry.side === side);
|
|
95
|
+
if (segment === undefined) {
|
|
96
|
+
segment = { side, calls: [] };
|
|
97
|
+
current.push(segment);
|
|
98
|
+
segments.push(segment);
|
|
99
|
+
}
|
|
100
|
+
segment.calls.push(call);
|
|
101
|
+
owner.push(segments.indexOf(segment));
|
|
102
|
+
open = !barrier;
|
|
103
|
+
}
|
|
104
|
+
return { stages, segments, owner };
|
|
105
|
+
}
|
|
106
|
+
/** Items per call ID in arrival order (a batch may repeat an ID, senpi#5796). */
|
|
107
|
+
function queueBy(items, idOf) {
|
|
108
|
+
const queues = new Map();
|
|
109
|
+
for (const item of items) {
|
|
110
|
+
const queue = queues.get(idOf(item)) ?? [];
|
|
111
|
+
queue.push(item);
|
|
112
|
+
queues.set(idOf(item), queue);
|
|
113
|
+
}
|
|
114
|
+
return queues;
|
|
115
|
+
}
|
|
116
|
+
/** Passes every event through except tool result messages, which it holds in `held` as start/end pairs. */
|
|
117
|
+
function holdingResults(emit, held) {
|
|
118
|
+
return (event) => {
|
|
119
|
+
if ((event.type !== "message_start" && event.type !== "message_end") || event.message.role !== "toolResult")
|
|
120
|
+
return emit(event);
|
|
121
|
+
const { toolCallId } = event.message;
|
|
122
|
+
const open = event.type === "message_end" ? held.findLast((entry) => entry.toolCallId === toolCallId) : undefined;
|
|
123
|
+
if (open !== undefined && open.events.at(-1)?.type === "message_start")
|
|
124
|
+
open.events.push(event);
|
|
125
|
+
else
|
|
126
|
+
held.push({ toolCallId, events: [event] });
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* The ToolHost of a split session: each call to a loop-side tool runs on `loop` (the session's own pipeline, in its
|
|
131
|
+
* process) and every other call on `hand`. A batch of one side goes to that host whole. A mixed batch keeps the mode:
|
|
132
|
+
* `parallel` runs both sides at once and emits the result messages in source order once both finished, as the fused
|
|
133
|
+
* pipeline does; `sequential` runs each run of same-side calls in source order and starts none after an abort. Results come
|
|
134
|
+
* back in the model's source order and the batch terminates only when every call asked to.
|
|
135
|
+
*/
|
|
136
|
+
export function createSplitToolHost(options) {
|
|
137
|
+
const sideOf = (call) => (options.loopSide(call.name) ? "loop" : "hand");
|
|
138
|
+
const isSequential = (call) => options.isSequential(call.name);
|
|
139
|
+
const hostOf = (side) => (side === "loop" ? options.loop : options.hand);
|
|
140
|
+
return {
|
|
141
|
+
async execute(call, hostOptions) {
|
|
142
|
+
const parallel = call.mode === "parallel";
|
|
143
|
+
const { stages, segments, owner } = stagesOf(call.calls, sideOf, isSequential, parallel);
|
|
144
|
+
const [only] = segments;
|
|
145
|
+
if (segments.every((segment) => segment.side === only?.side))
|
|
146
|
+
return hostOf(only?.side ?? "hand").execute(call, hostOptions);
|
|
147
|
+
const held = segments.map(() => []);
|
|
148
|
+
const run = async (segment) => {
|
|
149
|
+
const index = segments.indexOf(segment);
|
|
150
|
+
results[index] = await hostOf(segment.side).execute({ ...call, calls: segment.calls }, parallel ? { ...hostOptions, emit: holdingResults(hostOptions.emit, held[index] ?? []) } : hostOptions);
|
|
151
|
+
};
|
|
152
|
+
const results = segments.map(() => undefined);
|
|
153
|
+
for (const stage of stages) {
|
|
154
|
+
await Promise.all(stage.map(run));
|
|
155
|
+
if (hostOptions.signal?.aborted)
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
const messages = results.map((result) => queueBy(result?.messages ?? [], (message) => message.toolCallId));
|
|
159
|
+
const events = held.map((entries) => queueBy(entries, (entry) => entry.toolCallId));
|
|
160
|
+
const ordered = [];
|
|
161
|
+
for (const [index, raw] of call.calls.entries()) {
|
|
162
|
+
const segment = owner[index] ?? -1;
|
|
163
|
+
for (const event of events[segment]?.get(raw.id)?.shift()?.events ?? [])
|
|
164
|
+
await hostOptions.emit(event);
|
|
165
|
+
const message = messages[segment]?.get(raw.id)?.shift();
|
|
166
|
+
if (message !== undefined)
|
|
167
|
+
ordered.push(message);
|
|
168
|
+
}
|
|
169
|
+
const ran = results.filter((result) => (result?.messages.length ?? 0) > 0);
|
|
170
|
+
return {
|
|
171
|
+
messages: ordered,
|
|
172
|
+
// As the fused pipeline: only segments whose calls ran decide; a batch where nothing ran does not terminate.
|
|
173
|
+
terminate: ran.length > 0 && ran.every((result) => result.terminate),
|
|
174
|
+
};
|
|
175
|
+
},
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
//# sourceMappingURL=plane-session.js.map
|
|
@@ -53,8 +53,11 @@ export declare function decodePlaneMessage(payload: Uint8Array): PlaneMessage |
|
|
|
53
53
|
export declare function createPlaneServer(options: {
|
|
54
54
|
readonly transport: Transport;
|
|
55
55
|
readonly planes: Readonly<Record<string, PlaneHandler>>;
|
|
56
|
-
/**
|
|
57
|
-
|
|
56
|
+
/**
|
|
57
|
+
* The lane of the live parent with this execution ID that may host a request on `plane`, or `undefined` once it ended
|
|
58
|
+
* (or never existed, or may not host that plane). `plane` is absent for an abort of a request already admitted.
|
|
59
|
+
*/
|
|
60
|
+
readonly liveLane: (executionId: string, plane?: string) => string | undefined;
|
|
58
61
|
}): {
|
|
59
62
|
receive(lane: string, message: PlaneMessage): void;
|
|
60
63
|
parentEnded(executionId: string): void;
|
package/dist/execution/plane.js
CHANGED
|
@@ -53,7 +53,7 @@ export function createPlaneServer(options) {
|
|
|
53
53
|
const serve = (lane, request) => {
|
|
54
54
|
const { parent, childRequestId } = request;
|
|
55
55
|
const refuse = (reason) => send(lane, { kind: "plane.refused", parent, childRequestId, reason });
|
|
56
|
-
const parentLane = options.liveLane(parent);
|
|
56
|
+
const parentLane = options.liveLane(parent, request.plane);
|
|
57
57
|
if (parentLane === undefined)
|
|
58
58
|
return refuse("parent_terminal");
|
|
59
59
|
if (parentLane !== lane)
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { AgentContext, AgentTool, AgentToolCall, AgentToolResult, AgentToolUpdateCallback } from "../../types.ts";
|
|
2
|
+
import type { AssistantMessage } from "@earendil-works/pi-ai";
|
|
3
|
+
import type { PreparationPipeline } from "./executor-types.ts";
|
|
4
|
+
/** What release needs from preparation; it lives in memory with the executor that prepared the call. */
|
|
5
|
+
export interface PreparedBody {
|
|
6
|
+
readonly tool: AgentTool;
|
|
7
|
+
readonly toolCall: AgentToolCall;
|
|
8
|
+
readonly args: unknown;
|
|
9
|
+
readonly assistantMessage: AssistantMessage;
|
|
10
|
+
readonly context: AgentContext;
|
|
11
|
+
/** The model's own name when it resolved to another tool; today's result carries a correction notice for it. */
|
|
12
|
+
readonly requestedName?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface BodyOutcome {
|
|
15
|
+
readonly result: AgentToolResult<unknown>;
|
|
16
|
+
readonly isError: boolean;
|
|
17
|
+
}
|
|
18
|
+
export declare function errorResult(message: string): AgentToolResult<unknown>;
|
|
19
|
+
export declare function messageOf(error: unknown): string;
|
|
20
|
+
export declare function runBody(body: PreparedBody, signal: AbortSignal | undefined, onUpdate: AgentToolUpdateCallback<unknown>): Promise<BodyOutcome>;
|
|
21
|
+
export declare function finalizeBody(body: PreparedBody, executed: BodyOutcome, pipeline: PreparationPipeline, signal: AbortSignal | undefined): Promise<BodyOutcome>;
|
|
22
|
+
//# sourceMappingURL=executor-body.d.ts.map
|