@code-yeongyu/senpi-agent-core 2026.11.3-3 → 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 +23 -0
- package/dist/conformance/index.js +17 -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 +9 -1
- package/dist/execution/canonical-json.d.ts +3 -0
- package/dist/execution/canonical-json.js +12 -0
- package/dist/execution/coordinator-codec.js +13 -2
- package/dist/execution/coordinator-consent.d.ts +29 -0
- package/dist/execution/coordinator-consent.js +63 -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 +45 -0
- package/dist/execution/coordinator.d.ts +3 -1
- package/dist/execution/coordinator.js +35 -3
- 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/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 +10 -0
- package/dist/execution/remote-executor.js +49 -15
- package/dist/execution/remote-file-call.d.ts +14 -0
- package/dist/execution/remote-file-call.js +30 -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 +21 -4
- package/dist/execution/remote-protocol.js +18 -2
- package/dist/execution/remote-tool-host.d.ts +11 -3
- package/dist/execution/remote-tool-host.js +23 -64
- package/dist/execution/scope-lifecycle.d.ts +81 -0
- package/dist/execution/scope-lifecycle.js +173 -0
- package/dist/execution/testing/batch-executor.js +32 -3
- package/dist/execution/testing/batch-harness.d.ts +25 -3
- package/dist/execution/testing/batch-harness.js +83 -5
- 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/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-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/index.d.ts +13 -2
- package/dist/index.js +11 -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
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Transport } from "../transport/transport.ts";
|
|
2
2
|
import type { AgentToolResult } from "../types.ts";
|
|
3
|
+
import { type NestedRequest } from "./plane.ts";
|
|
3
4
|
import { type HandAuthority } from "./grant-verify.ts";
|
|
4
5
|
import type { HandLedger } from "./remote-hand-ledger.ts";
|
|
5
6
|
import type { ExecutionScope } from "./tool-host.ts";
|
|
@@ -9,6 +10,8 @@ export interface HandCall {
|
|
|
9
10
|
readonly name: string;
|
|
10
11
|
readonly arguments: unknown;
|
|
11
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;
|
|
12
15
|
}
|
|
13
16
|
/**
|
|
14
17
|
* What a hand runs a scope's calls on. `run` owns the whole call: name resolution, argument preparation and
|
|
@@ -18,6 +21,11 @@ export interface HandScope {
|
|
|
18
21
|
run(call: HandCall): Promise<AgentToolResult<unknown>>;
|
|
19
22
|
/** A tool that must not overlap other calls of its scope (`executionMode: "sequential"`). */
|
|
20
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>>;
|
|
21
29
|
dispose(): Promise<void>;
|
|
22
30
|
}
|
|
23
31
|
export type OpenHandScope = (scope: ExecutionScope) => Promise<HandScope>;
|
|
@@ -39,6 +47,8 @@ export interface RemoteExecutorOptions {
|
|
|
39
47
|
readonly ledger?: HandLedger;
|
|
40
48
|
/** This hand process's identity in its `hello`; a fresh one per start by default. */
|
|
41
49
|
readonly instance?: string;
|
|
50
|
+
/** Announce the device file channel (S40) in `hello`; its scopes then serve `HandScope.file`. */
|
|
51
|
+
readonly fileChannel?: boolean;
|
|
42
52
|
/**
|
|
43
53
|
* A call the hand could not record or answer (its durable record failed): the call gets no reply from this process,
|
|
44
54
|
* as from a hand that died, and a restarted hand answers it from the record. A failed restart recovery is reported
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { FILE_CHANNEL } from "./device-files.js";
|
|
2
|
+
import { createPlaneClient, decodePlaneMessage } from "./plane.js";
|
|
3
|
+
import { fileCall, fileRefusal, fileStopped } from "./remote-file-call.js";
|
|
1
4
|
import { decodeRemoteMessage, encodeRemoteMessage, HAND_CONTROL_LANE, toRemoteToolResult, } from "./remote-protocol.js";
|
|
2
5
|
import { authorize } from "./grant-verify.js";
|
|
3
6
|
/**
|
|
@@ -12,6 +15,7 @@ export function createRemoteExecutor(options) {
|
|
|
12
15
|
const accepted = [];
|
|
13
16
|
const seen = new Set();
|
|
14
17
|
const running = new Map();
|
|
18
|
+
const planes = createPlaneClient({ transport });
|
|
15
19
|
let closed = false;
|
|
16
20
|
const stopWatching = authority.kind === "peer"
|
|
17
21
|
? authority.verifier.onReplace(() => {
|
|
@@ -34,7 +38,7 @@ export function createRemoteExecutor(options) {
|
|
|
34
38
|
scopes.set(scope.id, state);
|
|
35
39
|
return state;
|
|
36
40
|
};
|
|
37
|
-
const invoke = async (lane, call) => {
|
|
41
|
+
const invoke = async (lane, call, file) => {
|
|
38
42
|
if (closed)
|
|
39
43
|
return refuse(lane, call.executionId, "hand_closed");
|
|
40
44
|
if (lane !== call.scope.id)
|
|
@@ -66,7 +70,7 @@ export function createRemoteExecutor(options) {
|
|
|
66
70
|
accepted.push(call.executionId);
|
|
67
71
|
const controller = new AbortController();
|
|
68
72
|
running.set(call.executionId, { controller, root: verdict.root });
|
|
69
|
-
const sequential = scope.isSequential(call.name);
|
|
73
|
+
const sequential = file !== undefined || scope.isSequential(call.name);
|
|
70
74
|
const dependencies = sequential
|
|
71
75
|
? [...(state.lastSequential ? [state.lastSequential] : []), ...state.wave]
|
|
72
76
|
: state.lastSequential
|
|
@@ -80,19 +84,38 @@ export function createRemoteExecutor(options) {
|
|
|
80
84
|
// durable admission, earlier calls of its scope) may have revoked it, and then the tool never runs.
|
|
81
85
|
if (controller.signal.aborted || !authorize(authority, call.scope.root).ok)
|
|
82
86
|
throw new Error("The call's grant was revoked before it ran");
|
|
83
|
-
result =
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
87
|
+
result =
|
|
88
|
+
file === undefined
|
|
89
|
+
? await scope.run({
|
|
90
|
+
toolCallId: call.toolCallId,
|
|
91
|
+
name: call.name,
|
|
92
|
+
arguments: call.arguments,
|
|
93
|
+
signal: controller.signal,
|
|
94
|
+
nested: planes.requestFor(call.executionId, lane, controller.signal),
|
|
95
|
+
})
|
|
96
|
+
: scope.file === undefined
|
|
97
|
+
? fileRefusal("unsupported", "this hand serves no file channel")
|
|
98
|
+
: await scope.file(file.op, call.arguments, controller.signal);
|
|
99
|
+
// A file operation that ended aborted ships no payload: a revoked grant is a refusal (R7), never data.
|
|
100
|
+
if (file !== undefined && controller.signal.aborted)
|
|
101
|
+
result = fileStopped(authorize(authority, call.scope.root).ok);
|
|
89
102
|
}
|
|
90
103
|
catch (error) {
|
|
91
|
-
|
|
92
|
-
|
|
104
|
+
if (file !== undefined) {
|
|
105
|
+
const granted = authorize(authority, call.scope.root).ok;
|
|
106
|
+
result =
|
|
107
|
+
controller.signal.aborted || !granted
|
|
108
|
+
? fileStopped(granted)
|
|
109
|
+
: fileRefusal("io", error instanceof Error ? error.message : String(error));
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
const text = error instanceof Error ? error.message : String(error);
|
|
113
|
+
result = { content: [{ type: "text", text }], details: {}, isError: true };
|
|
114
|
+
}
|
|
93
115
|
}
|
|
94
116
|
finally {
|
|
95
117
|
running.delete(call.executionId);
|
|
118
|
+
planes.parentEnded(call.executionId);
|
|
96
119
|
}
|
|
97
120
|
const settled = toRemoteToolResult(result);
|
|
98
121
|
reply(lane, ledger === undefined
|
|
@@ -107,12 +130,18 @@ export function createRemoteExecutor(options) {
|
|
|
107
130
|
else
|
|
108
131
|
state.wave.push(done);
|
|
109
132
|
};
|
|
133
|
+
// The connect hook: one `hello` per hand start, after recovery. The reliable transport re-delivers it across carrier
|
|
134
|
+
// re-attaches, so only a new hand process announces a new identity.
|
|
110
135
|
const ready = (async () => {
|
|
111
|
-
if (ledger
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
136
|
+
if (ledger !== undefined)
|
|
137
|
+
for (const { lane, message } of await ledger.recover())
|
|
138
|
+
reply(lane, message);
|
|
139
|
+
reply(HAND_CONTROL_LANE, {
|
|
140
|
+
kind: "hello",
|
|
141
|
+
instance: options.instance ?? crypto.randomUUID(),
|
|
142
|
+
durable: ledger !== undefined,
|
|
143
|
+
ops: options.fileChannel === true ? [FILE_CHANNEL] : [],
|
|
144
|
+
});
|
|
116
145
|
})();
|
|
117
146
|
ready.catch((error) => options.onFault(error));
|
|
118
147
|
transport.onFrame(async (frame) => {
|
|
@@ -122,6 +151,9 @@ export function createRemoteExecutor(options) {
|
|
|
122
151
|
catch {
|
|
123
152
|
return;
|
|
124
153
|
}
|
|
154
|
+
const nested = decodePlaneMessage(frame.payload);
|
|
155
|
+
if (nested !== undefined)
|
|
156
|
+
return planes.receive(nested);
|
|
125
157
|
let message;
|
|
126
158
|
try {
|
|
127
159
|
message = decodeRemoteMessage(frame.payload);
|
|
@@ -132,6 +164,8 @@ export function createRemoteExecutor(options) {
|
|
|
132
164
|
try {
|
|
133
165
|
if (message.kind === "invoke")
|
|
134
166
|
await invoke(frame.lane, message);
|
|
167
|
+
else if (message.kind === "file")
|
|
168
|
+
await invoke(frame.lane, fileCall(message), { op: message.op });
|
|
135
169
|
else if (message.kind === "abort")
|
|
136
170
|
running.get(message.executionId)?.controller.abort();
|
|
137
171
|
else if (message.kind === "ack")
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { AgentToolResult } from "../types.ts";
|
|
2
|
+
import type { DeviceFileErrorCode, DeviceFileOutcome } from "./device-files.ts";
|
|
3
|
+
import type { RemoteFile, RemoteInvoke } from "./remote-protocol.ts";
|
|
4
|
+
/**
|
|
5
|
+
* A device file operation as the hand's admission sees it (S40): the same record an invoke has, so the operation goes
|
|
6
|
+
* through the same lane, grant, scope binding, execution ID and durable-ledger checks. The name is never a tool's: the
|
|
7
|
+
* hand chose the file path from the message kind, and an invoke carrying this name is an unknown tool.
|
|
8
|
+
*/
|
|
9
|
+
export declare function fileCall(message: RemoteFile): RemoteInvoke;
|
|
10
|
+
export declare function fileResult(outcome: DeviceFileOutcome): AgentToolResult<unknown>;
|
|
11
|
+
export declare function fileRefusal(code: DeviceFileErrorCode, message: string): AgentToolResult<unknown>;
|
|
12
|
+
/** An operation that ended aborted: a refusal when its grant no longer holds, never any data. */
|
|
13
|
+
export declare function fileStopped(granted: boolean): AgentToolResult<unknown>;
|
|
14
|
+
//# sourceMappingURL=remote-file-call.d.ts.map
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A device file operation as the hand's admission sees it (S40): the same record an invoke has, so the operation goes
|
|
3
|
+
* through the same lane, grant, scope binding, execution ID and durable-ledger checks. The name is never a tool's: the
|
|
4
|
+
* hand chose the file path from the message kind, and an invoke carrying this name is an unknown tool.
|
|
5
|
+
*/
|
|
6
|
+
export function fileCall(message) {
|
|
7
|
+
return {
|
|
8
|
+
kind: "invoke",
|
|
9
|
+
executionId: message.executionId,
|
|
10
|
+
batchId: message.executionId,
|
|
11
|
+
index: 0,
|
|
12
|
+
scope: message.scope,
|
|
13
|
+
toolCallId: message.executionId,
|
|
14
|
+
name: `file.${message.op}`,
|
|
15
|
+
arguments: message.args,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export function fileResult(outcome) {
|
|
19
|
+
return { content: [], details: { file: outcome }, isError: !outcome.ok };
|
|
20
|
+
}
|
|
21
|
+
export function fileRefusal(code, message) {
|
|
22
|
+
return fileResult({ ok: false, code, message });
|
|
23
|
+
}
|
|
24
|
+
/** An operation that ended aborted: a refusal when its grant no longer holds, never any data. */
|
|
25
|
+
export function fileStopped(granted) {
|
|
26
|
+
return granted
|
|
27
|
+
? fileRefusal("aborted", "the operation was aborted")
|
|
28
|
+
: fileRefusal("refused_by_grant", "the grant was revoked");
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=remote-file-call.js.map
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { Transport } from "../transport/transport.ts";
|
|
2
|
+
import { type PlaneHandler } from "./plane.ts";
|
|
3
|
+
import { type RemoteFile, type RemoteHello, type RemoteInvoke, type RemoteTerminal, type RemoteToolResult } from "./remote-protocol.ts";
|
|
4
|
+
/** How a dispatched call or file operation ended, as the session saw it. */
|
|
5
|
+
export type HandReply = {
|
|
6
|
+
readonly kind: "settled";
|
|
7
|
+
readonly result: RemoteToolResult;
|
|
8
|
+
} | {
|
|
9
|
+
readonly kind: "terminal";
|
|
10
|
+
readonly terminal: RemoteTerminal;
|
|
11
|
+
} | {
|
|
12
|
+
readonly kind: "refused";
|
|
13
|
+
readonly reason: string;
|
|
14
|
+
} | {
|
|
15
|
+
readonly kind: "aborted";
|
|
16
|
+
readonly sent: boolean;
|
|
17
|
+
} | {
|
|
18
|
+
readonly kind: "unreachable";
|
|
19
|
+
readonly reason: string;
|
|
20
|
+
} | {
|
|
21
|
+
readonly kind: "closed";
|
|
22
|
+
};
|
|
23
|
+
export interface HandLink {
|
|
24
|
+
dispatch(message: RemoteInvoke | RemoteFile, signal?: AbortSignal): Promise<HandReply>;
|
|
25
|
+
/** The hand's `hello`, waiting at most `ms` for it (never past `signal`); `undefined` when none came. */
|
|
26
|
+
hello(ms: number, signal?: AbortSignal): Promise<RemoteHello | undefined>;
|
|
27
|
+
close(): void;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The session's end of the link to a hand (S27a, S40): every waiting call and file operation by execution ID, their
|
|
31
|
+
* replies and acks, a forwarded abort, and the hand's `hello`. The only re-send is on a `hello` that says `durable`: a
|
|
32
|
+
* hand with a record answers what it already holds, while a hand without one would run a re-sent call again or refuse
|
|
33
|
+
* it as a duplicate, so its waiting calls are left to the paths that already end them.
|
|
34
|
+
*/
|
|
35
|
+
export declare function createHandLink(transport: Transport, planeHandlers?: Readonly<Record<string, PlaneHandler>>): HandLink;
|
|
36
|
+
//# sourceMappingURL=remote-link.d.ts.map
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { createPlaneServer, decodePlaneMessage } from "./plane.js";
|
|
2
|
+
import { decodeRemoteMessage, encodeRemoteMessage, } from "./remote-protocol.js";
|
|
3
|
+
/**
|
|
4
|
+
* The session's end of the link to a hand (S27a, S40): every waiting call and file operation by execution ID, their
|
|
5
|
+
* replies and acks, a forwarded abort, and the hand's `hello`. The only re-send is on a `hello` that says `durable`: a
|
|
6
|
+
* hand with a record answers what it already holds, while a hand without one would run a re-sent call again or refuse
|
|
7
|
+
* it as a duplicate, so its waiting calls are left to the paths that already end them.
|
|
8
|
+
*/
|
|
9
|
+
export function createHandLink(transport, planeHandlers = {}) {
|
|
10
|
+
const pending = new Map();
|
|
11
|
+
// Nested requests (S31) are admitted only while their parent CALL is live on its lane; a file operation hosts none.
|
|
12
|
+
const planes = createPlaneServer({
|
|
13
|
+
transport,
|
|
14
|
+
planes: planeHandlers,
|
|
15
|
+
liveLane: (executionId) => {
|
|
16
|
+
const waiting = pending.get(executionId)?.message;
|
|
17
|
+
return waiting?.kind === "invoke" ? waiting.scope.id : undefined;
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
let announced;
|
|
21
|
+
// A wait that ran out is remembered: later operations answer at once instead of each waiting again.
|
|
22
|
+
let waitedOut = false;
|
|
23
|
+
const helloWaiters = new Set();
|
|
24
|
+
const settle = (executionId, reply) => {
|
|
25
|
+
const waiter = pending.get(executionId);
|
|
26
|
+
if (waiter === undefined)
|
|
27
|
+
return;
|
|
28
|
+
pending.delete(executionId);
|
|
29
|
+
planes.parentEnded(executionId);
|
|
30
|
+
waiter.resolve(reply);
|
|
31
|
+
};
|
|
32
|
+
const send = (lane, message) => transport.send(lane, encodeRemoteMessage(message));
|
|
33
|
+
transport.onFrame((frame) => {
|
|
34
|
+
const nested = decodePlaneMessage(frame.payload);
|
|
35
|
+
if (nested !== undefined)
|
|
36
|
+
return planes.receive(frame.lane, nested);
|
|
37
|
+
let message;
|
|
38
|
+
try {
|
|
39
|
+
message = decodeRemoteMessage(frame.payload);
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const ack = (executionId) => void send(frame.lane, { kind: "ack", executionId }).catch(() => undefined);
|
|
45
|
+
if (message.kind === "hello") {
|
|
46
|
+
announced = message;
|
|
47
|
+
for (const waiter of [...helloWaiters])
|
|
48
|
+
waiter(message);
|
|
49
|
+
if (message.durable)
|
|
50
|
+
for (const { message: waiting } of pending.values())
|
|
51
|
+
void send(waiting.scope.id, waiting).catch(() => undefined);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (message.kind === "settled") {
|
|
55
|
+
settle(message.executionId, { kind: "settled", result: message.result });
|
|
56
|
+
ack(message.executionId);
|
|
57
|
+
}
|
|
58
|
+
else if (message.kind === "terminal") {
|
|
59
|
+
settle(message.executionId, { kind: "terminal", terminal: message });
|
|
60
|
+
ack(message.executionId);
|
|
61
|
+
}
|
|
62
|
+
else if (message.kind === "refused")
|
|
63
|
+
settle(message.executionId, { kind: "refused", reason: message.reason });
|
|
64
|
+
});
|
|
65
|
+
return {
|
|
66
|
+
dispatch(message, signal) {
|
|
67
|
+
const { executionId } = message;
|
|
68
|
+
const replied = new Promise((resolve) => {
|
|
69
|
+
pending.set(executionId, { message, resolve });
|
|
70
|
+
});
|
|
71
|
+
if (signal?.aborted) {
|
|
72
|
+
settle(executionId, { kind: "aborted", sent: false });
|
|
73
|
+
return replied;
|
|
74
|
+
}
|
|
75
|
+
const onAbort = () => {
|
|
76
|
+
if (!pending.has(executionId))
|
|
77
|
+
return;
|
|
78
|
+
void send(message.scope.id, { kind: "abort", executionId }).catch(() => undefined);
|
|
79
|
+
settle(executionId, { kind: "aborted", sent: true });
|
|
80
|
+
};
|
|
81
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
82
|
+
void replied.then(() => signal?.removeEventListener("abort", onAbort));
|
|
83
|
+
send(message.scope.id, message).catch((error) => {
|
|
84
|
+
settle(executionId, {
|
|
85
|
+
kind: "unreachable",
|
|
86
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
return replied;
|
|
90
|
+
},
|
|
91
|
+
hello(ms, signal) {
|
|
92
|
+
if (announced !== undefined || waitedOut || signal?.aborted)
|
|
93
|
+
return Promise.resolve(announced);
|
|
94
|
+
return new Promise((resolve) => {
|
|
95
|
+
const done = (hello) => {
|
|
96
|
+
clearTimeout(timer);
|
|
97
|
+
helloWaiters.delete(done);
|
|
98
|
+
signal?.removeEventListener("abort", onAbort);
|
|
99
|
+
resolve(hello);
|
|
100
|
+
};
|
|
101
|
+
const onAbort = () => done(undefined);
|
|
102
|
+
const timer = setTimeout(() => {
|
|
103
|
+
waitedOut = true;
|
|
104
|
+
done(undefined);
|
|
105
|
+
}, ms);
|
|
106
|
+
helloWaiters.add(done);
|
|
107
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
108
|
+
});
|
|
109
|
+
},
|
|
110
|
+
close() {
|
|
111
|
+
for (const executionId of [...pending.keys()])
|
|
112
|
+
settle(executionId, { kind: "closed" });
|
|
113
|
+
transport.close();
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=remote-link.js.map
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ImageContent, JsonValue, TextContent } from "@earendil-works/pi-ai";
|
|
2
2
|
import type { AgentToolResult } from "../types.ts";
|
|
3
|
+
import { type DeviceFileOp } from "./device-files.ts";
|
|
3
4
|
import type { ExecutionScope } from "./tool-host.ts";
|
|
4
5
|
/**
|
|
5
6
|
* The wire messages between a session's remote ToolHost and the hand that runs its tool calls (brain/hand split, S27a).
|
|
@@ -8,17 +9,33 @@ import type { ExecutionScope } from "./tool-host.ts";
|
|
|
8
9
|
* The hand receives the model's raw call (name and arguments, before preparation, validation and hooks) and runs all of
|
|
9
10
|
* those steps itself. `sessionRef` is what the session chose to say about itself: the hand passes it to nothing.
|
|
10
11
|
*/
|
|
11
|
-
export type RemoteHandMessage = RemoteInvoke | RemoteAbort | RemoteAck | RemoteSettled | RemoteTerminal | RemoteRefused | RemoteHello;
|
|
12
|
+
export type RemoteHandMessage = RemoteInvoke | RemoteAbort | RemoteAck | RemoteSettled | RemoteTerminal | RemoteRefused | RemoteHello | RemoteFile;
|
|
12
13
|
/** The lane a hand announces itself on; execution scope lanes carry everything else. */
|
|
13
14
|
export declare const HAND_CONTROL_LANE = "hand.control";
|
|
14
15
|
/**
|
|
15
|
-
*
|
|
16
|
-
* acknowledged.
|
|
17
|
-
* runs only a call whose admission never became durable
|
|
16
|
+
* Every hand announces itself once per process start (after re-sending the outcomes its durable record holds that the
|
|
17
|
+
* session has not acknowledged). Only a `durable` hand gets the calls still waiting re-sent: its record answers a call
|
|
18
|
+
* it already holds and runs only a call whose admission never became durable; a hand without a record would run them
|
|
19
|
+
* again or refuse them as duplicates. `ops` names the channels it serves beside tool calls (`file/1`, S40). An older
|
|
20
|
+
* hand sends neither field and announces itself only with a record, so it reads as durable with no channels.
|
|
18
21
|
*/
|
|
19
22
|
export interface RemoteHello {
|
|
20
23
|
readonly kind: "hello";
|
|
21
24
|
readonly instance: string;
|
|
25
|
+
readonly durable: boolean;
|
|
26
|
+
readonly ops: readonly string[];
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* One device file operation (S40), a message kind of its own: the tool host never sends it, and an `invoke` named like
|
|
30
|
+
* an operation is an unknown tool. It shares the invoke's admission (lane, grant, scope binding, execution ID) and is
|
|
31
|
+
* answered like a call (`settled` with the outcome in `details.file`, `refused`, `terminal`).
|
|
32
|
+
*/
|
|
33
|
+
export interface RemoteFile {
|
|
34
|
+
readonly kind: "file";
|
|
35
|
+
readonly executionId: string;
|
|
36
|
+
readonly scope: ExecutionScope;
|
|
37
|
+
readonly op: DeviceFileOp;
|
|
38
|
+
readonly args: JsonValue;
|
|
22
39
|
}
|
|
23
40
|
export interface RemoteInvoke {
|
|
24
41
|
readonly kind: "invoke";
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { DEVICE_FILE_OPS } from "./device-files.js";
|
|
1
2
|
/** The lane a hand announces itself on; execution scope lanes carry everything else. */
|
|
2
3
|
export const HAND_CONTROL_LANE = "hand.control";
|
|
3
4
|
export class RemoteProtocolError extends Error {
|
|
@@ -24,9 +25,14 @@ export function decodeRemoteMessage(payload) {
|
|
|
24
25
|
throw new RemoteProtocolError("payload is not JSON");
|
|
25
26
|
}
|
|
26
27
|
if (isRecord(value) && value.kind === "hello") {
|
|
27
|
-
|
|
28
|
+
const { instance, durable, ops } = value;
|
|
29
|
+
if (!isString(instance))
|
|
28
30
|
throw new RemoteProtocolError("malformed hello");
|
|
29
|
-
|
|
31
|
+
if (durable !== undefined && typeof durable !== "boolean")
|
|
32
|
+
throw new RemoteProtocolError("malformed hello");
|
|
33
|
+
if (ops !== undefined && (!Array.isArray(ops) || !ops.every(isString)))
|
|
34
|
+
throw new RemoteProtocolError("malformed hello");
|
|
35
|
+
return { kind: "hello", instance, durable: durable ?? true, ops: ops ?? [] };
|
|
30
36
|
}
|
|
31
37
|
if (!isRecord(value) || !isString(value.executionId))
|
|
32
38
|
throw new RemoteProtocolError("no execution ID");
|
|
@@ -46,6 +52,16 @@ export function decodeRemoteMessage(payload) {
|
|
|
46
52
|
throw new RemoteProtocolError("malformed invoke");
|
|
47
53
|
return value;
|
|
48
54
|
}
|
|
55
|
+
case "file": {
|
|
56
|
+
const scope = value.scope;
|
|
57
|
+
if (!isRecord(scope) ||
|
|
58
|
+
!isString(scope.id) ||
|
|
59
|
+
typeof scope.root !== "string" ||
|
|
60
|
+
!DEVICE_FILE_OPS.includes(value.op) ||
|
|
61
|
+
!isRecord(value.args))
|
|
62
|
+
throw new RemoteProtocolError("malformed file operation");
|
|
63
|
+
return value;
|
|
64
|
+
}
|
|
49
65
|
case "abort":
|
|
50
66
|
return value;
|
|
51
67
|
case "ack":
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { Transport } from "../transport/transport.ts";
|
|
2
|
-
import type {
|
|
2
|
+
import type { DeviceFiles } from "./device-files.ts";
|
|
3
|
+
import type { PlaneHandler } from "./plane.ts";
|
|
4
|
+
import type { ExecutionScope, ToolHost } from "./tool-host.ts";
|
|
3
5
|
/**
|
|
4
6
|
* Where the hand runs relative to the session. It decides what a call that ended without a result (the hand restarted
|
|
5
7
|
* while it ran) looks like to the model, and is always stated, never inferred:
|
|
@@ -14,16 +16,22 @@ export interface RemoteToolHostOptions {
|
|
|
14
16
|
/** What the session says about itself; the hand never acts on it (R16). */
|
|
15
17
|
readonly sessionRef?: string;
|
|
16
18
|
readonly mintId?: () => string;
|
|
19
|
+
/**
|
|
20
|
+
* The planes this session serves to its running calls' nested requests (S31), keyed by plane name (`MODEL_PLANE`).
|
|
21
|
+
* A request is admitted only while its parent call is live on the lane it came on; absent: every request is refused
|
|
22
|
+
* `plane_unavailable`.
|
|
23
|
+
*/
|
|
24
|
+
readonly planes?: Readonly<Record<string, PlaneHandler>>;
|
|
17
25
|
}
|
|
18
26
|
/**
|
|
19
27
|
* The ToolHost that runs a session's tool calls on a hand (brain/hand split, S27a). The hand resolves, prepares,
|
|
20
28
|
* validates, runs the extensions' `tool_call`/`tool_result` handlers (permission checks among them) and executes each
|
|
21
29
|
* call; this host keeps the loop's side: tool events, the batch's ordering, a forwarded abort and the result messages in
|
|
22
30
|
* source order. A call whose send is refused (the transport closed) becomes an error result, never a retry. The one
|
|
23
|
-
* re-send is on a hand's `hello` (
|
|
24
|
-
* the hand answers each one its record already holds instead of running it a second time.
|
|
31
|
+
* re-send is on a durable hand's `hello` (see `createHandLink`). `deviceFiles(scope)` reaches the hand's files (S40).
|
|
25
32
|
*/
|
|
26
33
|
export declare function createRemoteToolHost(options: RemoteToolHostOptions): ToolHost & {
|
|
34
|
+
deviceFiles(scope: ExecutionScope): DeviceFiles;
|
|
27
35
|
close(): void;
|
|
28
36
|
};
|
|
29
37
|
//# sourceMappingURL=remote-tool-host.d.ts.map
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { TOOL_RESULT_PLACEHOLDER } from "@earendil-works/pi-ai";
|
|
2
|
-
import {
|
|
2
|
+
import { createRemoteDeviceFiles } from "./remote-device-files.js";
|
|
3
|
+
import { createHandLink } from "./remote-link.js";
|
|
3
4
|
function errorResult(text) {
|
|
4
5
|
return { content: [{ type: "text", text }], details: {}, isError: true };
|
|
5
6
|
}
|
|
@@ -21,69 +22,30 @@ function terminalResult(placement, terminal) {
|
|
|
21
22
|
* validates, runs the extensions' `tool_call`/`tool_result` handlers (permission checks among them) and executes each
|
|
22
23
|
* call; this host keeps the loop's side: tool events, the batch's ordering, a forwarded abort and the result messages in
|
|
23
24
|
* source order. A call whose send is refused (the transport closed) becomes an error result, never a retry. The one
|
|
24
|
-
* re-send is on a hand's `hello` (
|
|
25
|
-
* the hand answers each one its record already holds instead of running it a second time.
|
|
25
|
+
* re-send is on a durable hand's `hello` (see `createHandLink`). `deviceFiles(scope)` reaches the hand's files (S40).
|
|
26
26
|
*/
|
|
27
27
|
export function createRemoteToolHost(options) {
|
|
28
|
-
const { transport } = options;
|
|
29
28
|
const mintId = options.mintId ?? (() => crypto.randomUUID());
|
|
30
|
-
const
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
const ack = (executionId) => void transport.send(frame.lane, encodeRemoteMessage({ kind: "ack", executionId })).catch(() => undefined);
|
|
47
|
-
if (message.kind === "hello") {
|
|
48
|
-
for (const { invoke } of pending.values())
|
|
49
|
-
void transport.send(invoke.scope.id, encodeRemoteMessage(invoke)).catch(() => undefined);
|
|
50
|
-
return;
|
|
29
|
+
const link = createHandLink(options.transport, options.planes ?? {});
|
|
30
|
+
const resultOf = (reply) => {
|
|
31
|
+
switch (reply.kind) {
|
|
32
|
+
case "settled":
|
|
33
|
+
return reply.result;
|
|
34
|
+
case "terminal":
|
|
35
|
+
return terminalResult(options.placement, reply.terminal);
|
|
36
|
+
case "refused":
|
|
37
|
+
return errorResult(`The hand refused this tool call: ${reply.reason}`);
|
|
38
|
+
case "aborted":
|
|
39
|
+
return errorResult(reply.sent ? "Tool execution aborted" : "Operation aborted");
|
|
40
|
+
case "unreachable":
|
|
41
|
+
return errorResult(`The hand is unreachable: ${reply.reason}`);
|
|
42
|
+
case "closed":
|
|
43
|
+
return errorResult("The hand link was closed before the call settled");
|
|
51
44
|
}
|
|
52
|
-
if (message.kind === "settled") {
|
|
53
|
-
settle(message.executionId, message.result);
|
|
54
|
-
ack(message.executionId);
|
|
55
|
-
}
|
|
56
|
-
else if (message.kind === "terminal") {
|
|
57
|
-
settle(message.executionId, terminalResult(options.placement, message));
|
|
58
|
-
ack(message.executionId);
|
|
59
|
-
}
|
|
60
|
-
else if (message.kind === "refused")
|
|
61
|
-
settle(message.executionId, errorResult(`The hand refused this tool call: ${message.reason}`));
|
|
62
|
-
});
|
|
63
|
-
const dispatch = (invoke, signal) => {
|
|
64
|
-
const settled = new Promise((resolve) => {
|
|
65
|
-
pending.set(invoke.executionId, { invoke, resolve });
|
|
66
|
-
});
|
|
67
|
-
if (signal?.aborted)
|
|
68
|
-
settle(invoke.executionId, errorResult("Operation aborted"));
|
|
69
|
-
else {
|
|
70
|
-
const onAbort = () => {
|
|
71
|
-
if (!pending.has(invoke.executionId))
|
|
72
|
-
return;
|
|
73
|
-
void transport
|
|
74
|
-
.send(invoke.scope.id, encodeRemoteMessage({ kind: "abort", executionId: invoke.executionId }))
|
|
75
|
-
.catch(() => undefined);
|
|
76
|
-
settle(invoke.executionId, errorResult("Tool execution aborted"));
|
|
77
|
-
};
|
|
78
|
-
signal?.addEventListener("abort", onAbort, { once: true });
|
|
79
|
-
void settled.then(() => signal?.removeEventListener("abort", onAbort));
|
|
80
|
-
transport.send(invoke.scope.id, encodeRemoteMessage(invoke)).catch((error) => {
|
|
81
|
-
const reason = error instanceof Error ? error.message : String(error);
|
|
82
|
-
settle(invoke.executionId, errorResult(`The hand is unreachable: ${reason}`));
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
return { settled };
|
|
86
45
|
};
|
|
46
|
+
const dispatch = (invoke, signal) => ({
|
|
47
|
+
settled: link.dispatch(invoke, signal).then(resultOf),
|
|
48
|
+
});
|
|
87
49
|
return {
|
|
88
50
|
async execute(call, hostOptions) {
|
|
89
51
|
const { emit, signal } = hostOptions;
|
|
@@ -145,11 +107,8 @@ export function createRemoteToolHost(options) {
|
|
|
145
107
|
terminate: results.length > 0 && results.every((result) => result.terminate === true),
|
|
146
108
|
};
|
|
147
109
|
},
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
settle(executionId, errorResult("The hand link was closed before the call settled"));
|
|
151
|
-
transport.close();
|
|
152
|
-
},
|
|
110
|
+
deviceFiles: (scope) => createRemoteDeviceFiles(link, scope, mintId),
|
|
111
|
+
close: () => link.close(),
|
|
153
112
|
};
|
|
154
113
|
}
|
|
155
114
|
//# sourceMappingURL=remote-tool-host.js.map
|