@code-yeongyu/senpi-agent-core 2026.11.3-2 → 2026.11.4-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/agent-loop.js +7 -47
- package/dist/conformance/index.d.ts +37 -0
- package/dist/conformance/index.js +25 -0
- package/dist/conformance/loop-core-manifest.d.ts +1 -1
- package/dist/conformance/loop-core-manifest.js +1 -0
- package/dist/conformance/registry.js +31 -1
- package/dist/execution/canonical-json.d.ts +3 -0
- package/dist/execution/canonical-json.js +12 -0
- package/dist/execution/coordinator-codec.d.ts +5 -0
- package/dist/execution/coordinator-codec.js +104 -0
- package/dist/execution/coordinator-consent.d.ts +29 -0
- package/dist/execution/coordinator-consent.js +63 -0
- package/dist/execution/coordinator-plan.d.ts +33 -0
- package/dist/execution/coordinator-plan.js +106 -0
- package/dist/execution/coordinator-sequential.d.ts +15 -0
- package/dist/execution/coordinator-sequential.js +34 -0
- package/dist/execution/coordinator-types.d.ts +161 -0
- package/dist/execution/coordinator-types.js +2 -0
- package/dist/execution/coordinator.d.ts +27 -0
- package/dist/execution/coordinator.js +164 -0
- package/dist/execution/device-files-node.d.ts +8 -0
- package/dist/execution/device-files-node.js +112 -0
- package/dist/execution/device-files.d.ts +80 -0
- package/dist/execution/device-files.js +87 -0
- package/dist/execution/grant-verify.d.ts +70 -0
- package/dist/execution/grant-verify.js +87 -0
- package/dist/execution/ledger.d.ts +2 -0
- package/dist/execution/ledger.js +1 -0
- package/dist/execution/nested-codec.d.ts +5 -0
- package/dist/execution/nested-codec.js +41 -0
- package/dist/execution/nested-types.d.ts +92 -0
- package/dist/execution/nested-types.js +3 -0
- package/dist/execution/nested.d.ts +48 -0
- package/dist/execution/nested.js +206 -0
- package/dist/execution/plane-model.d.ts +33 -0
- package/dist/execution/plane-model.js +80 -0
- package/dist/execution/plane.d.ts +72 -0
- package/dist/execution/plane.js +172 -0
- package/dist/execution/remote-device-files.d.ts +12 -0
- package/dist/execution/remote-device-files.js +84 -0
- package/dist/execution/remote-executor.d.ts +74 -0
- package/dist/execution/remote-executor.js +193 -0
- package/dist/execution/remote-file-call.d.ts +14 -0
- package/dist/execution/remote-file-call.js +30 -0
- package/dist/execution/remote-hand-ledger.d.ts +40 -0
- package/dist/execution/remote-hand-ledger.js +137 -0
- package/dist/execution/remote-link.d.ts +36 -0
- package/dist/execution/remote-link.js +117 -0
- package/dist/execution/remote-protocol.d.ts +104 -0
- package/dist/execution/remote-protocol.js +104 -0
- package/dist/execution/remote-tool-host.d.ts +37 -0
- package/dist/execution/remote-tool-host.js +114 -0
- package/dist/execution/scope-lifecycle.d.ts +81 -0
- package/dist/execution/scope-lifecycle.js +173 -0
- package/dist/execution/testing/batch-cases.d.ts +6 -0
- package/dist/execution/testing/batch-cases.js +210 -0
- package/dist/execution/testing/batch-executor.d.ts +34 -0
- package/dist/execution/testing/batch-executor.js +170 -0
- package/dist/execution/testing/batch-harness.d.ts +58 -0
- package/dist/execution/testing/batch-harness.js +286 -0
- package/dist/execution/testing/batch-registry.d.ts +3 -0
- package/dist/execution/testing/batch-registry.js +30 -0
- package/dist/execution/testing/cancel-cases.d.ts +8 -0
- package/dist/execution/testing/cancel-cases.js +64 -0
- package/dist/execution/testing/consent-cases.d.ts +14 -0
- package/dist/execution/testing/consent-cases.js +66 -0
- package/dist/execution/testing/fault-hand-cases.d.ts +6 -0
- package/dist/execution/testing/fault-hand-cases.js +70 -0
- package/dist/execution/testing/hand-harness.d.ts +48 -0
- package/dist/execution/testing/hand-harness.js +136 -0
- package/dist/execution/testing/kill-batch.d.ts +12 -0
- package/dist/execution/testing/kill-batch.js +70 -0
- package/dist/execution/testing/kill-hand.d.ts +16 -0
- package/dist/execution/testing/kill-hand.js +75 -0
- package/dist/execution/testing/kill-nest.d.ts +6 -0
- package/dist/execution/testing/kill-nest.js +55 -0
- package/dist/execution/testing/kill-wakex.d.ts +6 -0
- package/dist/execution/testing/kill-wakex.js +81 -0
- package/dist/execution/testing/lifecycle-cases.d.ts +9 -0
- package/dist/execution/testing/lifecycle-cases.js +60 -0
- package/dist/execution/testing/lifecycle-registry.d.ts +3 -0
- package/dist/execution/testing/lifecycle-registry.js +19 -0
- package/dist/execution/testing/multi-hand-cases.d.ts +6 -0
- package/dist/execution/testing/multi-hand-cases.js +44 -0
- package/dist/execution/testing/multi-lifecycle-cases.d.ts +4 -0
- package/dist/execution/testing/multi-lifecycle-cases.js +34 -0
- package/dist/execution/testing/multi-seq-cases.d.ts +4 -0
- package/dist/execution/testing/multi-seq-cases.js +46 -0
- package/dist/execution/testing/nest-cases.d.ts +39 -0
- package/dist/execution/testing/nest-cases.js +181 -0
- package/dist/execution/testing/nest-registry.d.ts +3 -0
- package/dist/execution/testing/nest-registry.js +27 -0
- package/dist/execution/testing/scope-cases.d.ts +6 -0
- package/dist/execution/testing/scope-cases.js +49 -0
- package/dist/execution/testing/seq-cases.d.ts +6 -0
- package/dist/execution/testing/seq-cases.js +71 -0
- package/dist/execution/testing/seq-registry.d.ts +3 -0
- package/dist/execution/testing/seq-registry.js +32 -0
- package/dist/execution/testing/wake-exec-cases.d.ts +20 -0
- package/dist/execution/testing/wake-exec-cases.js +147 -0
- package/dist/execution/testing/wake-registry.d.ts +3 -0
- package/dist/execution/testing/wake-registry.js +26 -0
- package/dist/execution/tool-host.d.ts +10 -2
- package/dist/execution/tool-outcome-events.d.ts +11 -0
- package/dist/execution/tool-outcome-events.js +47 -0
- package/dist/execution/wake-inbox.d.ts +25 -0
- package/dist/execution/wake-inbox.js +85 -0
- package/dist/execution/wake-outbox.d.ts +30 -0
- package/dist/execution/wake-outbox.js +87 -0
- package/dist/execution/wake-types.d.ts +45 -0
- package/dist/execution/wake-types.js +3 -0
- package/dist/harness/env/nodejs.js +4 -0
- package/dist/index.d.ts +21 -1
- package/dist/index.js +19 -1
- package/dist/node.d.ts +1 -0
- package/dist/node.js +1 -0
- package/dist/transport/testing/multi-cases.js +8 -8
- package/package.json +3 -3
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { AssistantMessage, Context, ThinkingLevel } from "@earendil-works/pi-ai";
|
|
2
|
+
import { type NestedRequest, type PlaneHandler } from "./plane.ts";
|
|
3
|
+
/** The plane a hand's model-backed tools (a completion, a model-backed search) use to reach a model (S31, F40). */
|
|
4
|
+
export declare const MODEL_PLANE = "model";
|
|
5
|
+
/**
|
|
6
|
+
* One completion a hand asks the brain for. It names a model only by provider and ID; the brain resolves it against
|
|
7
|
+
* its own registry and credentials, or uses the session's model when `model` is absent. Nothing a hand sends can choose
|
|
8
|
+
* an endpoint, a key or a header: the codec keeps only these fields.
|
|
9
|
+
*/
|
|
10
|
+
export interface ModelPlaneRequest {
|
|
11
|
+
readonly model?: {
|
|
12
|
+
readonly provider: string;
|
|
13
|
+
readonly id: string;
|
|
14
|
+
};
|
|
15
|
+
readonly context: Context;
|
|
16
|
+
readonly options?: {
|
|
17
|
+
readonly maxTokens?: number;
|
|
18
|
+
readonly temperature?: number;
|
|
19
|
+
readonly reasoning?: ThinkingLevel;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** Parses a request body, keeping only the fields of {@link ModelPlaneRequest}; refuses a malformed one. */
|
|
23
|
+
export declare function decodeModelPlaneRequest(body: unknown): ModelPlaneRequest;
|
|
24
|
+
/**
|
|
25
|
+
* The brain's model plane: `complete` runs one request with the brain's own model registry and credentials (fused and
|
|
26
|
+
* headless: this machine's, as today; a hosted brain's credential source is card C6) and is aborted with the request.
|
|
27
|
+
*/
|
|
28
|
+
export declare function serveModelPlane(complete: (request: ModelPlaneRequest, signal: AbortSignal) => Promise<AssistantMessage>): PlaneHandler;
|
|
29
|
+
/** A hand's client for the model plane, over the running call's nested requests. */
|
|
30
|
+
export declare function createModelPlaneClient(nested: NestedRequest): {
|
|
31
|
+
complete(request: ModelPlaneRequest, signal?: AbortSignal): Promise<AssistantMessage>;
|
|
32
|
+
};
|
|
33
|
+
//# sourceMappingURL=plane-model.d.ts.map
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { PlaneRefusedError } from "./plane.js";
|
|
2
|
+
/** The plane a hand's model-backed tools (a completion, a model-backed search) use to reach a model (S31, F40). */
|
|
3
|
+
export const MODEL_PLANE = "model";
|
|
4
|
+
const REASONING = ["minimal", "low", "medium", "high", "xhigh", "max"];
|
|
5
|
+
const isRecord = (value) => typeof value === "object" && value !== null;
|
|
6
|
+
function refuse(detail) {
|
|
7
|
+
throw new PlaneRefusedError(`malformed_request: ${detail}`);
|
|
8
|
+
}
|
|
9
|
+
function decodeModel(value) {
|
|
10
|
+
if (value === undefined)
|
|
11
|
+
return undefined;
|
|
12
|
+
if (!isRecord(value) || typeof value.provider !== "string" || typeof value.id !== "string")
|
|
13
|
+
refuse("model");
|
|
14
|
+
return { provider: value.provider, id: value.id };
|
|
15
|
+
}
|
|
16
|
+
function decodeContext(value) {
|
|
17
|
+
if (!isRecord(value) || !Array.isArray(value.messages))
|
|
18
|
+
refuse("context");
|
|
19
|
+
if (value.systemPrompt !== undefined && typeof value.systemPrompt !== "string")
|
|
20
|
+
refuse("systemPrompt");
|
|
21
|
+
if (value.tools !== undefined && !Array.isArray(value.tools))
|
|
22
|
+
refuse("tools");
|
|
23
|
+
return {
|
|
24
|
+
...(value.systemPrompt === undefined ? {} : { systemPrompt: value.systemPrompt }),
|
|
25
|
+
messages: value.messages,
|
|
26
|
+
...(value.tools === undefined ? {} : { tools: value.tools }),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function decodeOptions(value) {
|
|
30
|
+
if (value === undefined)
|
|
31
|
+
return undefined;
|
|
32
|
+
if (!isRecord(value))
|
|
33
|
+
refuse("options");
|
|
34
|
+
const { maxTokens, temperature, reasoning } = value;
|
|
35
|
+
if (maxTokens !== undefined && (typeof maxTokens !== "number" || !Number.isSafeInteger(maxTokens) || maxTokens <= 0))
|
|
36
|
+
refuse("maxTokens");
|
|
37
|
+
if (temperature !== undefined && (typeof temperature !== "number" || !Number.isFinite(temperature)))
|
|
38
|
+
refuse("temperature");
|
|
39
|
+
if (reasoning !== undefined && !REASONING.includes(reasoning))
|
|
40
|
+
refuse("reasoning");
|
|
41
|
+
return {
|
|
42
|
+
...(maxTokens === undefined ? {} : { maxTokens: maxTokens }),
|
|
43
|
+
...(temperature === undefined ? {} : { temperature: temperature }),
|
|
44
|
+
...(reasoning === undefined ? {} : { reasoning: reasoning }),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/** Parses a request body, keeping only the fields of {@link ModelPlaneRequest}; refuses a malformed one. */
|
|
48
|
+
export function decodeModelPlaneRequest(body) {
|
|
49
|
+
if (!isRecord(body))
|
|
50
|
+
refuse("body");
|
|
51
|
+
const model = decodeModel(body.model);
|
|
52
|
+
const options = decodeOptions(body.options);
|
|
53
|
+
return {
|
|
54
|
+
...(model === undefined ? {} : { model }),
|
|
55
|
+
context: decodeContext(body.context),
|
|
56
|
+
...(options === undefined ? {} : { options }),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The brain's model plane: `complete` runs one request with the brain's own model registry and credentials (fused and
|
|
61
|
+
* headless: this machine's, as today; a hosted brain's credential source is card C6) and is aborted with the request.
|
|
62
|
+
*/
|
|
63
|
+
export function serveModelPlane(complete) {
|
|
64
|
+
return async (body, signal) => {
|
|
65
|
+
const message = await complete(decodeModelPlaneRequest(body), signal);
|
|
66
|
+
return JSON.parse(JSON.stringify(message));
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/** A hand's client for the model plane, over the running call's nested requests. */
|
|
70
|
+
export function createModelPlaneClient(nested) {
|
|
71
|
+
return {
|
|
72
|
+
async complete(request, signal) {
|
|
73
|
+
const reply = await nested(MODEL_PLANE, JSON.parse(JSON.stringify(request)), signal);
|
|
74
|
+
if (!isRecord(reply) || reply.role !== "assistant" || !Array.isArray(reply.content))
|
|
75
|
+
throw new PlaneRefusedError("malformed_reply");
|
|
76
|
+
return reply;
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=plane-model.js.map
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { JsonValue } from "@earendil-works/pi-ai";
|
|
2
|
+
import type { Transport } from "../transport/transport.ts";
|
|
3
|
+
/**
|
|
4
|
+
* Nested requests from a hand back to the brain that owns the running call (brain/hand split, design 5.6.3): a
|
|
5
|
+
* model-backed tool's completion (S31) and, later, session-plane work. A request travels on its parent call's own scope
|
|
6
|
+
* lane and names that parent; the brain admits it only while the parent is live on that lane, and it is never sent
|
|
7
|
+
* hand-to-hand. The bodies are a plane's own payloads; this layer only carries, admits, deduplicates and cancels them.
|
|
8
|
+
*/
|
|
9
|
+
export type PlaneMessage = PlaneRequest | PlaneAbort | PlaneReply | PlaneRefused;
|
|
10
|
+
interface PlaneAddress {
|
|
11
|
+
/** The execution ID of the call the request is nested in. */
|
|
12
|
+
readonly parent: string;
|
|
13
|
+
/** Unique per parent; a replayed request with the same ID gets the same answer and is served once. */
|
|
14
|
+
readonly childRequestId: string;
|
|
15
|
+
}
|
|
16
|
+
export interface PlaneRequest extends PlaneAddress {
|
|
17
|
+
readonly kind: "plane.request";
|
|
18
|
+
readonly plane: string;
|
|
19
|
+
readonly body: JsonValue;
|
|
20
|
+
}
|
|
21
|
+
export interface PlaneAbort extends PlaneAddress {
|
|
22
|
+
readonly kind: "plane.abort";
|
|
23
|
+
}
|
|
24
|
+
export interface PlaneReply extends PlaneAddress {
|
|
25
|
+
readonly kind: "plane.reply";
|
|
26
|
+
readonly body: JsonValue;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The brain did not serve the request. `parent_terminal`: its parent is not a live call; `scope_lane_mismatch`: it came
|
|
30
|
+
* on another scope's lane; `plane_unavailable`: the brain serves no such plane; `plane_failed: ...`: the plane's
|
|
31
|
+
* handler failed.
|
|
32
|
+
*/
|
|
33
|
+
export interface PlaneRefused extends PlaneAddress {
|
|
34
|
+
readonly kind: "plane.refused";
|
|
35
|
+
readonly reason: string;
|
|
36
|
+
}
|
|
37
|
+
/** Serves one plane on the brain. `signal` aborts when the request or its parent call is aborted or ends. */
|
|
38
|
+
export type PlaneHandler = (body: JsonValue, signal: AbortSignal) => Promise<JsonValue>;
|
|
39
|
+
/** What a running call on a hand uses to ask the brain that owns it. Rejects with {@link PlaneRefusedError}. */
|
|
40
|
+
export type NestedRequest = (plane: string, body: JsonValue, signal?: AbortSignal) => Promise<JsonValue>;
|
|
41
|
+
export declare class PlaneRefusedError extends Error {
|
|
42
|
+
readonly reason: string;
|
|
43
|
+
constructor(reason: string);
|
|
44
|
+
}
|
|
45
|
+
export declare function encodePlaneMessage(message: PlaneMessage): Uint8Array;
|
|
46
|
+
/** Parses one frame payload as a plane message; anything else (a tool-call message included) is `undefined`. */
|
|
47
|
+
export declare function decodePlaneMessage(payload: Uint8Array): PlaneMessage | undefined;
|
|
48
|
+
/**
|
|
49
|
+
* The brain's side: admits a request (and an abort) only while its parent is a live call on the lane it came on, serves each
|
|
50
|
+
* (parent, child request) once and answers a replay from the stored answer, and aborts a parent's open requests when
|
|
51
|
+
* the parent ends. Answers are kept for the parent's life only.
|
|
52
|
+
*/
|
|
53
|
+
export declare function createPlaneServer(options: {
|
|
54
|
+
readonly transport: Transport;
|
|
55
|
+
readonly planes: Readonly<Record<string, PlaneHandler>>;
|
|
56
|
+
/** The lane of the live call with this execution ID, or `undefined` once it ended (or never existed). */
|
|
57
|
+
readonly liveLane: (executionId: string) => string | undefined;
|
|
58
|
+
}): {
|
|
59
|
+
receive(lane: string, message: PlaneMessage): void;
|
|
60
|
+
parentEnded(executionId: string): void;
|
|
61
|
+
};
|
|
62
|
+
/** The hand's side: issues a running call's nested requests on its lane and fails them all once the call ends. */
|
|
63
|
+
export declare function createPlaneClient(options: {
|
|
64
|
+
readonly transport: Transport;
|
|
65
|
+
readonly mintId?: () => string;
|
|
66
|
+
}): {
|
|
67
|
+
requestFor(parent: string, lane: string, parentSignal: AbortSignal): NestedRequest;
|
|
68
|
+
receive(message: PlaneMessage): void;
|
|
69
|
+
parentEnded(parent: string): void;
|
|
70
|
+
};
|
|
71
|
+
export {};
|
|
72
|
+
//# sourceMappingURL=plane.d.ts.map
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
export class PlaneRefusedError extends Error {
|
|
2
|
+
constructor(reason) {
|
|
3
|
+
super(`The brain refused this request: ${reason}`);
|
|
4
|
+
this.name = "PlaneRefusedError";
|
|
5
|
+
this.reason = reason;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
const KINDS = ["plane.request", "plane.abort", "plane.reply", "plane.refused"];
|
|
9
|
+
const encoder = new TextEncoder();
|
|
10
|
+
const decoder = new TextDecoder();
|
|
11
|
+
export function encodePlaneMessage(message) {
|
|
12
|
+
return encoder.encode(JSON.stringify(message));
|
|
13
|
+
}
|
|
14
|
+
const isRecord = (value) => typeof value === "object" && value !== null;
|
|
15
|
+
const isId = (value) => typeof value === "string" && value.length > 0;
|
|
16
|
+
/** Parses one frame payload as a plane message; anything else (a tool-call message included) is `undefined`. */
|
|
17
|
+
export function decodePlaneMessage(payload) {
|
|
18
|
+
let value;
|
|
19
|
+
try {
|
|
20
|
+
value = JSON.parse(decoder.decode(payload));
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
if (!isRecord(value) || !KINDS.includes(value.kind))
|
|
26
|
+
return undefined;
|
|
27
|
+
if (!isId(value.parent) || !isId(value.childRequestId))
|
|
28
|
+
return undefined;
|
|
29
|
+
const address = { parent: value.parent, childRequestId: value.childRequestId };
|
|
30
|
+
switch (value.kind) {
|
|
31
|
+
case "plane.request":
|
|
32
|
+
return isId(value.plane) && "body" in value
|
|
33
|
+
? { kind: "plane.request", ...address, plane: value.plane, body: value.body }
|
|
34
|
+
: undefined;
|
|
35
|
+
case "plane.abort":
|
|
36
|
+
return { kind: "plane.abort", ...address };
|
|
37
|
+
case "plane.reply":
|
|
38
|
+
return "body" in value ? { kind: "plane.reply", ...address, body: value.body } : undefined;
|
|
39
|
+
default:
|
|
40
|
+
return typeof value.reason === "string"
|
|
41
|
+
? { kind: "plane.refused", ...address, reason: value.reason }
|
|
42
|
+
: undefined;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The brain's side: admits a request (and an abort) only while its parent is a live call on the lane it came on, serves each
|
|
47
|
+
* (parent, child request) once and answers a replay from the stored answer, and aborts a parent's open requests when
|
|
48
|
+
* the parent ends. Answers are kept for the parent's life only.
|
|
49
|
+
*/
|
|
50
|
+
export function createPlaneServer(options) {
|
|
51
|
+
const served = new Map();
|
|
52
|
+
const send = (lane, message) => void options.transport.send(lane, encodePlaneMessage(message)).catch(() => undefined);
|
|
53
|
+
const serve = (lane, request) => {
|
|
54
|
+
const { parent, childRequestId } = request;
|
|
55
|
+
const refuse = (reason) => send(lane, { kind: "plane.refused", parent, childRequestId, reason });
|
|
56
|
+
const parentLane = options.liveLane(parent);
|
|
57
|
+
if (parentLane === undefined)
|
|
58
|
+
return refuse("parent_terminal");
|
|
59
|
+
if (parentLane !== lane)
|
|
60
|
+
return refuse("scope_lane_mismatch");
|
|
61
|
+
const children = served.get(parent) ?? new Map();
|
|
62
|
+
served.set(parent, children);
|
|
63
|
+
const known = children.get(childRequestId);
|
|
64
|
+
if (known !== undefined) {
|
|
65
|
+
if (known.answer !== undefined)
|
|
66
|
+
send(lane, known.answer);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const handler = Object.hasOwn(options.planes, request.plane) ? options.planes[request.plane] : undefined;
|
|
70
|
+
if (handler === undefined)
|
|
71
|
+
return refuse("plane_unavailable");
|
|
72
|
+
const entry = { controller: new AbortController() };
|
|
73
|
+
children.set(childRequestId, entry);
|
|
74
|
+
void handler(request.body, entry.controller.signal)
|
|
75
|
+
.then((body) => ({ kind: "plane.reply", parent, childRequestId, body }), (error) => ({
|
|
76
|
+
kind: "plane.refused",
|
|
77
|
+
parent,
|
|
78
|
+
childRequestId,
|
|
79
|
+
reason: `plane_failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
80
|
+
}))
|
|
81
|
+
.then((answer) => {
|
|
82
|
+
if (served.get(parent)?.get(childRequestId) !== entry)
|
|
83
|
+
return;
|
|
84
|
+
entry.answer = answer;
|
|
85
|
+
send(lane, answer);
|
|
86
|
+
});
|
|
87
|
+
};
|
|
88
|
+
return {
|
|
89
|
+
receive(lane, message) {
|
|
90
|
+
if (message.kind === "plane.request")
|
|
91
|
+
serve(lane, message);
|
|
92
|
+
else if (message.kind === "plane.abort" && options.liveLane(message.parent) === lane)
|
|
93
|
+
served.get(message.parent)?.get(message.childRequestId)?.controller.abort();
|
|
94
|
+
},
|
|
95
|
+
parentEnded(executionId) {
|
|
96
|
+
const children = served.get(executionId);
|
|
97
|
+
served.delete(executionId);
|
|
98
|
+
for (const entry of children?.values() ?? [])
|
|
99
|
+
entry.controller.abort();
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/** The hand's side: issues a running call's nested requests on its lane and fails them all once the call ends. */
|
|
104
|
+
export function createPlaneClient(options) {
|
|
105
|
+
const mintId = options.mintId ?? (() => crypto.randomUUID());
|
|
106
|
+
const waiting = new Map();
|
|
107
|
+
const live = new Set();
|
|
108
|
+
return {
|
|
109
|
+
requestFor(parent, lane, parentSignal) {
|
|
110
|
+
live.add(parent);
|
|
111
|
+
return (plane, body, signal) => new Promise((resolve, reject) => {
|
|
112
|
+
if (!live.has(parent) || parentSignal.aborted || signal?.aborted)
|
|
113
|
+
return reject(new PlaneRefusedError("parent_terminal"));
|
|
114
|
+
const childRequestId = mintId();
|
|
115
|
+
const children = waiting.get(parent) ?? new Map();
|
|
116
|
+
waiting.set(parent, children);
|
|
117
|
+
const abort = () => {
|
|
118
|
+
if (!children.delete(childRequestId))
|
|
119
|
+
return;
|
|
120
|
+
void options.transport
|
|
121
|
+
.send(lane, encodePlaneMessage({ kind: "plane.abort", parent, childRequestId }))
|
|
122
|
+
.catch(() => undefined);
|
|
123
|
+
reject(new Error("Operation aborted"));
|
|
124
|
+
};
|
|
125
|
+
const release = () => {
|
|
126
|
+
parentSignal.removeEventListener("abort", abort);
|
|
127
|
+
signal?.removeEventListener("abort", abort);
|
|
128
|
+
};
|
|
129
|
+
children.set(childRequestId, {
|
|
130
|
+
resolve: (value) => {
|
|
131
|
+
release();
|
|
132
|
+
resolve(value);
|
|
133
|
+
},
|
|
134
|
+
reject: (error) => {
|
|
135
|
+
release();
|
|
136
|
+
reject(error);
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
parentSignal.addEventListener("abort", abort, { once: true });
|
|
140
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
141
|
+
const request = { kind: "plane.request", parent, childRequestId, plane, body };
|
|
142
|
+
options.transport.send(lane, encodePlaneMessage(request)).catch((error) => {
|
|
143
|
+
if (!children.delete(childRequestId))
|
|
144
|
+
return;
|
|
145
|
+
release();
|
|
146
|
+
reject(new PlaneRefusedError(`brain_unreachable: ${error instanceof Error ? error.message : String(error)}`));
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
},
|
|
150
|
+
receive(message) {
|
|
151
|
+
if (message.kind !== "plane.reply" && message.kind !== "plane.refused")
|
|
152
|
+
return;
|
|
153
|
+
const children = waiting.get(message.parent);
|
|
154
|
+
const waiter = children?.get(message.childRequestId);
|
|
155
|
+
if (waiter === undefined)
|
|
156
|
+
return;
|
|
157
|
+
children?.delete(message.childRequestId);
|
|
158
|
+
if (message.kind === "plane.reply")
|
|
159
|
+
waiter.resolve(message.body);
|
|
160
|
+
else
|
|
161
|
+
waiter.reject(new PlaneRefusedError(message.reason));
|
|
162
|
+
},
|
|
163
|
+
parentEnded(parent) {
|
|
164
|
+
live.delete(parent);
|
|
165
|
+
const children = waiting.get(parent);
|
|
166
|
+
waiting.delete(parent);
|
|
167
|
+
for (const waiter of children?.values() ?? [])
|
|
168
|
+
waiter.reject(new PlaneRefusedError("parent_terminal"));
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
//# sourceMappingURL=plane.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type DeviceFiles } from "./device-files.ts";
|
|
2
|
+
import type { HandLink } from "./remote-link.ts";
|
|
3
|
+
import type { ExecutionScope } from "./tool-host.ts";
|
|
4
|
+
/** How long an operation waits for the hand's `hello` before answering `unsupported` (C1). */
|
|
5
|
+
export declare const HELLO_WAIT_MS = 5000;
|
|
6
|
+
/**
|
|
7
|
+
* A session's {@link DeviceFiles} on a hand (S40): each operation is one `file` message on the scope's lane, after the
|
|
8
|
+
* hand announced the channel in its `hello` (or `unsupported`, sent nowhere). A write over the limit is refused here,
|
|
9
|
+
* before anything is sent; the hand checks the limit again before it reads or writes.
|
|
10
|
+
*/
|
|
11
|
+
export declare function createRemoteDeviceFiles(link: HandLink, scope: ExecutionScope, mintId: () => string): DeviceFiles;
|
|
12
|
+
//# sourceMappingURL=remote-device-files.d.ts.map
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { DeviceFileError, decodeBase64, encodeBase64, FILE_CHANNEL, FILE_CHANNEL_MAX_BYTES, fileOutcomeOf, } from "./device-files.js";
|
|
2
|
+
/** How long an operation waits for the hand's `hello` before answering `unsupported` (C1). */
|
|
3
|
+
export const HELLO_WAIT_MS = 5000;
|
|
4
|
+
const GRANT_REFUSALS = new Set(["grant_missing", "grant_expired", "workspace_unknown", "capability_missing"]);
|
|
5
|
+
function refusalError(reason) {
|
|
6
|
+
if (GRANT_REFUSALS.has(reason))
|
|
7
|
+
return new DeviceFileError("refused_by_grant", reason);
|
|
8
|
+
if (reason === "hand_closed" || reason.startsWith("scope_unavailable"))
|
|
9
|
+
return new DeviceFileError("hand_unreachable", reason);
|
|
10
|
+
return new DeviceFileError("protocol", reason);
|
|
11
|
+
}
|
|
12
|
+
function valueOf(reply) {
|
|
13
|
+
switch (reply.kind) {
|
|
14
|
+
case "settled": {
|
|
15
|
+
const outcome = fileOutcomeOf(reply.result.details);
|
|
16
|
+
if (outcome === undefined)
|
|
17
|
+
throw new DeviceFileError("protocol", "the hand sent no file outcome");
|
|
18
|
+
if (!outcome.ok)
|
|
19
|
+
throw new DeviceFileError(outcome.code, outcome.message);
|
|
20
|
+
return outcome.value;
|
|
21
|
+
}
|
|
22
|
+
case "terminal": {
|
|
23
|
+
const { reason, uncertain } = reply.terminal;
|
|
24
|
+
throw new DeviceFileError("interrupted", `the operation ended without an outcome (${reason})`, {
|
|
25
|
+
reason,
|
|
26
|
+
uncertain,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
case "refused":
|
|
30
|
+
throw refusalError(reply.reason);
|
|
31
|
+
case "aborted":
|
|
32
|
+
throw new DeviceFileError("aborted", "the operation was aborted");
|
|
33
|
+
case "unreachable":
|
|
34
|
+
throw new DeviceFileError("hand_unreachable", reply.reason);
|
|
35
|
+
case "closed":
|
|
36
|
+
throw new DeviceFileError("hand_unreachable", "the hand link was closed");
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* A session's {@link DeviceFiles} on a hand (S40): each operation is one `file` message on the scope's lane, after the
|
|
41
|
+
* hand announced the channel in its `hello` (or `unsupported`, sent nowhere). A write over the limit is refused here,
|
|
42
|
+
* before anything is sent; the hand checks the limit again before it reads or writes.
|
|
43
|
+
*/
|
|
44
|
+
export function createRemoteDeviceFiles(link, scope, mintId) {
|
|
45
|
+
const call = async (op, args, signal) => {
|
|
46
|
+
const hello = await link.hello(HELLO_WAIT_MS, signal);
|
|
47
|
+
if (signal?.aborted)
|
|
48
|
+
throw new DeviceFileError("aborted", "the operation was aborted");
|
|
49
|
+
if (hello === undefined || !hello.ops.includes(FILE_CHANNEL))
|
|
50
|
+
throw new DeviceFileError("unsupported", "the hand serves no file channel");
|
|
51
|
+
return valueOf(await link.dispatch({ kind: "file", executionId: mintId(), scope, op, args }, signal));
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
read: async (path, options) => {
|
|
55
|
+
const value = await call("read", { path }, options?.signal);
|
|
56
|
+
if (typeof value !== "string")
|
|
57
|
+
throw new DeviceFileError("protocol", "malformed read outcome");
|
|
58
|
+
return decodeBase64(value);
|
|
59
|
+
},
|
|
60
|
+
write: async (path, data, options) => {
|
|
61
|
+
if (data.byteLength > FILE_CHANNEL_MAX_BYTES)
|
|
62
|
+
throw new DeviceFileError("too_large", path);
|
|
63
|
+
const exclusive = options?.exclusive === true;
|
|
64
|
+
await call("write", { path, data: encodeBase64(data), exclusive }, options?.signal);
|
|
65
|
+
},
|
|
66
|
+
list: async (path, options) => {
|
|
67
|
+
const value = await call("list", { path }, options?.signal);
|
|
68
|
+
if (!Array.isArray(value) || !value.every((entry) => typeof entry === "string"))
|
|
69
|
+
throw new DeviceFileError("protocol", "malformed list outcome");
|
|
70
|
+
return value;
|
|
71
|
+
},
|
|
72
|
+
stat: async (path, options) => (await call("stat", { path }, options?.signal)),
|
|
73
|
+
rename: async (from, to, options) => {
|
|
74
|
+
await call("rename", { path: from, to }, options?.signal);
|
|
75
|
+
},
|
|
76
|
+
remove: async (path, options) => {
|
|
77
|
+
await call("remove", { path }, options?.signal);
|
|
78
|
+
},
|
|
79
|
+
mkdir: async (path, options) => {
|
|
80
|
+
await call("mkdir", { path }, options?.signal);
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=remote-device-files.js.map
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { Transport } from "../transport/transport.ts";
|
|
2
|
+
import type { AgentToolResult } from "../types.ts";
|
|
3
|
+
import { type NestedRequest } from "./plane.ts";
|
|
4
|
+
import { type HandAuthority } from "./grant-verify.ts";
|
|
5
|
+
import type { HandLedger } from "./remote-hand-ledger.ts";
|
|
6
|
+
import type { ExecutionScope } from "./tool-host.ts";
|
|
7
|
+
/** One call as a hand runs it: the raw name and arguments, never anything about the session that sent it. */
|
|
8
|
+
export interface HandCall {
|
|
9
|
+
readonly toolCallId: string;
|
|
10
|
+
readonly name: string;
|
|
11
|
+
readonly arguments: unknown;
|
|
12
|
+
readonly signal: AbortSignal;
|
|
13
|
+
/** Asks the brain that owns this call (a model-backed tool's completion, S31); fails once the call has ended. */
|
|
14
|
+
readonly nested: NestedRequest;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* What a hand runs a scope's calls on. `run` owns the whole call: name resolution, argument preparation and
|
|
18
|
+
* validation, the extensions' `tool_call`/`tool_result` handlers and the tool; it never rejects for a tool failure.
|
|
19
|
+
*/
|
|
20
|
+
export interface HandScope {
|
|
21
|
+
run(call: HandCall): Promise<AgentToolResult<unknown>>;
|
|
22
|
+
/** A tool that must not overlap other calls of its scope (`executionMode: "sequential"`). */
|
|
23
|
+
isSequential(name: string): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Runs one device file operation (S40) after its grant was checked: the scope's `tool_call` handlers, filesystem
|
|
26
|
+
* policy and the operation itself, the outcome in `details.file`. Absent: the scope serves no file channel.
|
|
27
|
+
*/
|
|
28
|
+
file?(op: string, args: unknown, signal: AbortSignal): Promise<AgentToolResult<unknown>>;
|
|
29
|
+
dispose(): Promise<void>;
|
|
30
|
+
}
|
|
31
|
+
export type OpenHandScope = (scope: ExecutionScope) => Promise<HandScope>;
|
|
32
|
+
export interface RemoteExecutorOptions {
|
|
33
|
+
/** The hand's end of the link; lanes are execution scope IDs. */
|
|
34
|
+
readonly transport: Transport;
|
|
35
|
+
/** Opens a scope lazily on its first call, bound to the scope's root (the canonical one a peer's grant check resolved). */
|
|
36
|
+
readonly openScope: OpenHandScope;
|
|
37
|
+
/**
|
|
38
|
+
* Whose calls this executor runs. A peer's call is checked against its grant before anything of it is recorded or
|
|
39
|
+
* opened, and a replaced grant snapshot re-checks every running call, aborting the ones it no longer allows (S27b).
|
|
40
|
+
*/
|
|
41
|
+
readonly authority: HandAuthority;
|
|
42
|
+
/**
|
|
43
|
+
* The hand's durable record of its calls. With it, a call is recorded as dispatched before it runs, a restarted hand
|
|
44
|
+
* answers its interrupted calls `hand_restarted` without running them again, and a re-delivered invoke gets the
|
|
45
|
+
* recorded outcome. Without it, the hand remembers execution IDs only while it lives.
|
|
46
|
+
*/
|
|
47
|
+
readonly ledger?: HandLedger;
|
|
48
|
+
/** This hand process's identity in its `hello`; a fresh one per start by default. */
|
|
49
|
+
readonly instance?: string;
|
|
50
|
+
/** Announce the device file channel (S40) in `hello`; its scopes then serve `HandScope.file`. */
|
|
51
|
+
readonly fileChannel?: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* A call the hand could not record or answer (its durable record failed): the call gets no reply from this process,
|
|
54
|
+
* as from a hand that died, and a restarted hand answers it from the record. A failed restart recovery is reported
|
|
55
|
+
* here too, and this process then answers no frame. Reported here, never swallowed. The embedder owns the restart:
|
|
56
|
+
* close this executor and start a new one on the same store, or the call stays unanswered while this process lives.
|
|
57
|
+
*/
|
|
58
|
+
readonly onFault: (error: unknown) => void;
|
|
59
|
+
}
|
|
60
|
+
export interface RemoteExecutor {
|
|
61
|
+
/** Execution IDs this hand has accepted, in arrival order (each runs at most once). */
|
|
62
|
+
readonly accepted: () => readonly string[];
|
|
63
|
+
/** Settles once a restarted hand has recovered its record and re-sent the outcomes the session has not acknowledged. */
|
|
64
|
+
readonly ready: Promise<void>;
|
|
65
|
+
close(): Promise<void>;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* The hand's side of remote tool execution (brain/hand split, S27a). Each scope keeps its own state, opened lazily and
|
|
69
|
+
* bound to the root it was opened with: a later call naming the same scope with another root is refused, as is any
|
|
70
|
+
* execution ID the hand has already accepted (dispatch at most once). The session reference a call carries is never
|
|
71
|
+
* passed on: the hand keys on the scope and does not know the session (R16).
|
|
72
|
+
*/
|
|
73
|
+
export declare function createRemoteExecutor(options: RemoteExecutorOptions): RemoteExecutor;
|
|
74
|
+
//# sourceMappingURL=remote-executor.d.ts.map
|