@code-yeongyu/senpi-agent-core 2026.11.3-3 → 2026.11.5-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 +20 -186
- package/dist/agent.d.ts +4 -0
- package/dist/agent.js +4 -0
- package/dist/conformance/index.d.ts +30 -0
- package/dist/conformance/index.js +21 -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 +20 -18
- 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/canonical-json.d.ts +3 -0
- package/dist/execution/canonical-json.js +12 -0
- package/dist/execution/chain-count.d.ts +26 -0
- package/dist/execution/chain-count.js +45 -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-plan.d.ts +1 -2
- package/dist/execution/coordinator-plan.js +4 -17
- 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 +150 -0
- package/dist/execution/device-files.d.ts +109 -0
- package/dist/execution/device-files.js +124 -0
- 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/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-session.d.ts +56 -0
- package/dist/execution/plane-session.js +178 -0
- package/dist/execution/plane.d.ts +75 -0
- package/dist/execution/plane.js +172 -0
- 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 +17 -0
- package/dist/execution/prepared/executor-freeze.js +42 -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 +261 -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.d.ts +12 -0
- package/dist/execution/remote-device-files.js +113 -0
- 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 +17 -0
- package/dist/execution/remote-executor.js +70 -18
- package/dist/execution/remote-file-call.d.ts +14 -0
- package/dist/execution/remote-file-call.js +31 -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 +41 -0
- package/dist/execution/remote-link.js +153 -0
- package/dist/execution/remote-protocol.d.ts +50 -4
- package/dist/execution/remote-protocol.js +48 -2
- package/dist/execution/remote-tool-host.d.ts +23 -3
- package/dist/execution/remote-tool-host.js +25 -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/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 +3 -0
- package/dist/execution/testing/hand-harness.js +27 -22
- 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/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-epoch-cases.d.ts +4 -0
- package/dist/execution/testing/multi-epoch-cases.js +29 -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 +23 -3
- package/dist/execution/tool-host.js +18 -1
- 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/agent-harness.d.ts +6 -0
- package/dist/harness/execution/tool-host-call.d.ts +13 -0
- package/dist/harness/execution/tool-host-call.js +55 -0
- package/dist/harness/execution/tools.d.ts +2 -1
- package/dist/harness/execution/tools.js +32 -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/index.d.ts +27 -2
- package/dist/index.js +24 -1
- package/dist/node.d.ts +1 -0
- package/dist/node.js +1 -0
- 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/transport/testing/multi-cases.js +8 -8
- package/dist/types.d.ts +37 -1
- package/package.json +3 -3
|
@@ -21,10 +21,18 @@ export type HandAdmission = {
|
|
|
21
21
|
readonly kind: "in_flight";
|
|
22
22
|
};
|
|
23
23
|
export interface HandLedger {
|
|
24
|
-
|
|
24
|
+
/**
|
|
25
|
+
* `admits` is the sender's epoch check (S35), run inside every step of the walk: once it fails, the walk stops, a
|
|
26
|
+
* record it already wrote is cancelled (the call never dispatches), and the invoke is refused `epoch_fenced`.
|
|
27
|
+
*/
|
|
28
|
+
admit(invoke: RemoteInvoke, admits?: () => boolean): Promise<HandAdmission>;
|
|
25
29
|
settle(invoke: RemoteInvoke, result: RemoteToolResult): Promise<RemoteHandMessage>;
|
|
26
|
-
ack
|
|
27
|
-
|
|
30
|
+
/** `admits` is the sender's epoch check, run inside the ack's admission slot: a fenced ack changes nothing. */
|
|
31
|
+
ack(executionId: string, admits?: () => boolean): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* Runs the ledger's restart recovery, then returns every outcome the session has not acknowledged, by lane. Only the
|
|
34
|
+
* first call recovers: a later link's executor on the same live hand gets nothing, and its calls keep running.
|
|
35
|
+
*/
|
|
28
36
|
recover(): Promise<Array<{
|
|
29
37
|
readonly lane: string;
|
|
30
38
|
readonly message: RemoteHandMessage;
|
|
@@ -34,7 +34,9 @@ function asStoreValue(value) {
|
|
|
34
34
|
export function createHandLedger(options) {
|
|
35
35
|
const { ledger, executor, issuer, workspace, now, leaseMs } = options;
|
|
36
36
|
const keyOf = (executionId) => ({ issuer, commandId: executionId });
|
|
37
|
-
const apply = (executionId, event, actor) => ledger.apply({ key: keyOf(executionId), generation: GENERATION, actor: { id: actor }, now: now(), event });
|
|
37
|
+
const apply = (executionId, event, actor, guard) => ledger.apply({ key: keyOf(executionId), generation: GENERATION, actor: { id: actor }, now: now(), event }, guard);
|
|
38
|
+
let recovered;
|
|
39
|
+
const fencedBy = (admits) => admits === undefined ? undefined : () => (admits() ? undefined : "epoch_fenced");
|
|
38
40
|
/** Calls this process admitted or runs; a duplicate invoke for one of them waits on that run. */
|
|
39
41
|
const owned = new Set();
|
|
40
42
|
const replyFrom = (reply, executionId) => {
|
|
@@ -44,7 +46,7 @@ export function createHandLedger(options) {
|
|
|
44
46
|
return { kind: "refused", executionId, reason: "result_expired" };
|
|
45
47
|
return outcomeOf(reply.entry) ?? { kind: "refused", executionId, reason: "not_settled" };
|
|
46
48
|
};
|
|
47
|
-
const admitNew = (invoke, digest) => {
|
|
49
|
+
const admitNew = (invoke, digest, guard) => {
|
|
48
50
|
const admittedAt = now();
|
|
49
51
|
return ledger.admit({
|
|
50
52
|
issuer,
|
|
@@ -57,7 +59,13 @@ export function createHandLedger(options) {
|
|
|
57
59
|
admittedAt,
|
|
58
60
|
leaseUntil: admittedAt + leaseMs,
|
|
59
61
|
reconcilable: false,
|
|
60
|
-
});
|
|
62
|
+
}, guard);
|
|
63
|
+
};
|
|
64
|
+
/** A fenced walk: the record it wrote (if any) is cancelled so it never dispatches, and the invoke is refused. */
|
|
65
|
+
const fenced = async (executionId) => {
|
|
66
|
+
if ((await ledger.read(keyOf(executionId))) !== undefined)
|
|
67
|
+
await apply(executionId, { kind: "cancel" }, issuer);
|
|
68
|
+
return { kind: "reply", message: { kind: "refused", executionId, reason: "epoch_fenced" } };
|
|
61
69
|
};
|
|
62
70
|
/**
|
|
63
71
|
* Walks a call to `dispatched` from whatever its record holds: a new call from admission, a `prepared` or
|
|
@@ -65,8 +73,9 @@ export function createHandLedger(options) {
|
|
|
65
73
|
* that holds another request under this execution ID (other name, arguments or scope) is refused
|
|
66
74
|
* `identity_conflict`, as the ledger refuses such an admission: a resumed call runs only what was frozen.
|
|
67
75
|
*/
|
|
68
|
-
const advance = async (invoke) => {
|
|
76
|
+
const advance = async (invoke, guard) => {
|
|
69
77
|
const { executionId } = invoke;
|
|
78
|
+
const isFenced = (reply) => reply.status === "refused" && reply.reason === "epoch_fenced";
|
|
70
79
|
const args = asStoreValue(invoke.arguments);
|
|
71
80
|
const frozen = { name: invoke.name, args, digest: JSON.stringify([invoke.name, args]) };
|
|
72
81
|
const prior = await ledger.read(keyOf(executionId));
|
|
@@ -76,23 +85,27 @@ export function createHandLedger(options) {
|
|
|
76
85
|
if (settled !== undefined)
|
|
77
86
|
return { kind: "reply", message: settled };
|
|
78
87
|
const fromStart = [
|
|
79
|
-
() => admitNew(invoke, frozen.digest),
|
|
80
|
-
() => apply(executionId, { kind: "prepare" }, executor),
|
|
81
|
-
() => apply(executionId, { kind: "effect" }, executor),
|
|
82
|
-
() => apply(executionId, { kind: "freeze", call: frozen, awaitingConsent: false }, executor),
|
|
88
|
+
() => admitNew(invoke, frozen.digest, guard),
|
|
89
|
+
() => apply(executionId, { kind: "prepare" }, executor, guard),
|
|
90
|
+
() => apply(executionId, { kind: "effect" }, executor, guard),
|
|
91
|
+
() => apply(executionId, { kind: "freeze", call: frozen, awaitingConsent: false }, executor, guard),
|
|
83
92
|
];
|
|
84
93
|
const state = prior?.progress.state;
|
|
85
94
|
const steps = state === undefined ? fromStart : state === "prepared" || state === "released" ? [] : undefined;
|
|
86
95
|
if (steps === undefined)
|
|
87
96
|
return { kind: "in_flight" };
|
|
88
|
-
for (const step of [...steps, () => apply(executionId, { kind: "release" }, issuer)]) {
|
|
97
|
+
for (const step of [...steps, () => apply(executionId, { kind: "release" }, issuer, guard)]) {
|
|
89
98
|
const reply = await step();
|
|
99
|
+
if (isFenced(reply))
|
|
100
|
+
return fenced(executionId);
|
|
90
101
|
if (reply.status === "replayed" && reply.entry.progress.state !== "settled")
|
|
91
102
|
continue;
|
|
92
103
|
if (reply.status !== "applied" || reply.entry.progress.state === "settled")
|
|
93
104
|
return { kind: "reply", message: replyFrom(reply, executionId) };
|
|
94
105
|
}
|
|
95
|
-
const dispatched = await apply(executionId, { kind: "dispatch" }, executor);
|
|
106
|
+
const dispatched = await apply(executionId, { kind: "dispatch" }, executor, guard);
|
|
107
|
+
if (isFenced(dispatched))
|
|
108
|
+
return fenced(executionId);
|
|
96
109
|
if (dispatched.status === "applied" && dispatched.entry.progress.state === "dispatched")
|
|
97
110
|
return { kind: "run" };
|
|
98
111
|
if (dispatched.status === "replayed" && dispatched.entry.progress.state !== "settled")
|
|
@@ -100,12 +113,12 @@ export function createHandLedger(options) {
|
|
|
100
113
|
return { kind: "reply", message: replyFrom(dispatched, executionId) };
|
|
101
114
|
};
|
|
102
115
|
return {
|
|
103
|
-
async admit(invoke) {
|
|
116
|
+
async admit(invoke, admits) {
|
|
104
117
|
const { executionId } = invoke;
|
|
105
118
|
if (owned.has(executionId))
|
|
106
119
|
return { kind: "in_flight" };
|
|
107
120
|
owned.add(executionId);
|
|
108
|
-
const admission = await advance(invoke).catch((error) => {
|
|
121
|
+
const admission = await advance(invoke, fencedBy(admits)).catch((error) => {
|
|
109
122
|
owned.delete(executionId);
|
|
110
123
|
throw error;
|
|
111
124
|
});
|
|
@@ -122,11 +135,14 @@ export function createHandLedger(options) {
|
|
|
122
135
|
owned.delete(invoke.executionId);
|
|
123
136
|
}
|
|
124
137
|
},
|
|
125
|
-
async ack(executionId) {
|
|
126
|
-
await apply(executionId, { kind: "ack" }, issuer);
|
|
138
|
+
async ack(executionId, admits) {
|
|
139
|
+
await apply(executionId, { kind: "ack" }, issuer, fencedBy(admits));
|
|
127
140
|
},
|
|
128
141
|
async recover() {
|
|
129
|
-
|
|
142
|
+
if (recovered !== undefined)
|
|
143
|
+
return recovered.then(() => []);
|
|
144
|
+
recovered = ledger.recover(now());
|
|
145
|
+
await recovered;
|
|
130
146
|
return (await ledger.scan()).flatMap((entry) => {
|
|
131
147
|
const message = entry.progress.state === "settled" ? outcomeOf(entry) : undefined;
|
|
132
148
|
return message === undefined ? [] : [{ lane: entry.scope, message }];
|
|
@@ -0,0 +1,41 @@
|
|
|
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
|
+
/** The session's epoch never opened the lane (S35): a newer owner holds it, or the hand refused this sender. */
|
|
24
|
+
| {
|
|
25
|
+
readonly kind: "epoch_refused";
|
|
26
|
+
readonly reason: string;
|
|
27
|
+
};
|
|
28
|
+
export interface HandLink {
|
|
29
|
+
dispatch(message: RemoteInvoke | RemoteFile, signal?: AbortSignal): Promise<HandReply>;
|
|
30
|
+
/** The hand's `hello`, waiting at most `ms` for it (never past `signal`); `undefined` when none came. */
|
|
31
|
+
hello(ms: number, signal?: AbortSignal): Promise<RemoteHello | undefined>;
|
|
32
|
+
close(): void;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The session's end of the link to a hand (S27a, S40): every waiting call and file operation by execution ID, their
|
|
36
|
+
* replies and acks, a forwarded abort, and the hand's `hello`. The only re-send is on a `hello` that says `durable`: a
|
|
37
|
+
* hand with a record answers what it already holds, while a hand without one would run a re-sent call again or refuse
|
|
38
|
+
* it as a duplicate, so its waiting calls are left to the paths that already end them.
|
|
39
|
+
*/
|
|
40
|
+
export declare function createHandLink(transport: Transport, planeHandlers?: Readonly<Record<string, PlaneHandler>>, filePlanes?: readonly string[], epoch?: number): HandLink;
|
|
41
|
+
//# sourceMappingURL=remote-link.d.ts.map
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { createPlaneServer, decodePlaneMessage } from "./plane.js";
|
|
2
|
+
import { createEpochGate } from "./remote-epoch-gate.js";
|
|
3
|
+
import { decodeRemoteMessage, encodeRemoteMessage, } from "./remote-protocol.js";
|
|
4
|
+
/**
|
|
5
|
+
* The session's end of the link to a hand (S27a, S40): every waiting call and file operation by execution ID, their
|
|
6
|
+
* replies and acks, a forwarded abort, and the hand's `hello`. The only re-send is on a `hello` that says `durable`: a
|
|
7
|
+
* hand with a record answers what it already holds, while a hand without one would run a re-sent call again or refuse
|
|
8
|
+
* it as a duplicate, so its waiting calls are left to the paths that already end them.
|
|
9
|
+
*/
|
|
10
|
+
export function createHandLink(transport, planeHandlers = {}, filePlanes = [], epoch) {
|
|
11
|
+
const pending = new Map();
|
|
12
|
+
const stamp = epoch === undefined ? {} : { epoch };
|
|
13
|
+
/** Messages handed to the transport: a hand's `hello` re-sends only these, never one still waiting on its floor. */
|
|
14
|
+
const sent = new Set();
|
|
15
|
+
// Nested requests (S31) are admitted only while their parent CALL is live on its lane. A pending file operation
|
|
16
|
+
// (S40) hosts only the planes in `filePlanes` (a two-sided extension's forwarded tool events, S29); every other
|
|
17
|
+
// plane under it is refused as if it had no live parent. An abort names no plane and reaches what was admitted.
|
|
18
|
+
const planes = createPlaneServer({
|
|
19
|
+
transport,
|
|
20
|
+
planes: planeHandlers,
|
|
21
|
+
liveLane: (executionId, plane) => {
|
|
22
|
+
const waiting = pending.get(executionId)?.message;
|
|
23
|
+
if (waiting?.kind === "invoke")
|
|
24
|
+
return waiting.scope.id;
|
|
25
|
+
if (waiting?.kind === "file" && (plane === undefined || filePlanes.includes(plane)))
|
|
26
|
+
return waiting.scope.id;
|
|
27
|
+
return undefined;
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
let announced;
|
|
31
|
+
// A wait that ran out is remembered: later operations answer at once instead of each waiting again.
|
|
32
|
+
let waitedOut = false;
|
|
33
|
+
const helloWaiters = new Set();
|
|
34
|
+
const settle = (executionId, reply) => {
|
|
35
|
+
const waiter = pending.get(executionId);
|
|
36
|
+
if (waiter === undefined)
|
|
37
|
+
return;
|
|
38
|
+
pending.delete(executionId);
|
|
39
|
+
sent.delete(executionId);
|
|
40
|
+
planes.parentEnded(executionId);
|
|
41
|
+
waiter.resolve(reply);
|
|
42
|
+
};
|
|
43
|
+
const send = (lane, message) => transport.send(lane, encodeRemoteMessage(message));
|
|
44
|
+
const gate = epoch === undefined ? undefined : createEpochGate(epoch, (lane, floor) => send(lane, floor));
|
|
45
|
+
/** Runs `go` once the lane's floor is acked at this epoch (at once without an epoch); a refusal ends the message. */
|
|
46
|
+
const whenOpen = (lane, executionId, go) => {
|
|
47
|
+
if (gate === undefined)
|
|
48
|
+
return go();
|
|
49
|
+
void gate.open(lane).then((refusal) => {
|
|
50
|
+
if (!pending.has(executionId))
|
|
51
|
+
return;
|
|
52
|
+
if (refusal === undefined)
|
|
53
|
+
return go();
|
|
54
|
+
settle(executionId, { kind: "epoch_refused", reason: refusal });
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
transport.onFrame((frame) => {
|
|
58
|
+
const nested = decodePlaneMessage(frame.payload);
|
|
59
|
+
if (nested !== undefined)
|
|
60
|
+
return planes.receive(frame.lane, nested);
|
|
61
|
+
let message;
|
|
62
|
+
try {
|
|
63
|
+
message = decodeRemoteMessage(frame.payload);
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const ack = (executionId) => void send(frame.lane, { kind: "ack", executionId, ...stamp }).catch(() => undefined);
|
|
69
|
+
if (message.kind === "hello") {
|
|
70
|
+
announced = message;
|
|
71
|
+
for (const waiter of [...helloWaiters])
|
|
72
|
+
waiter(message);
|
|
73
|
+
gate?.resend();
|
|
74
|
+
if (message.durable)
|
|
75
|
+
for (const { message: waiting } of pending.values()) {
|
|
76
|
+
if (!sent.has(waiting.executionId))
|
|
77
|
+
continue;
|
|
78
|
+
whenOpen(waiting.scope.id, waiting.executionId, () => void send(waiting.scope.id, waiting).catch(() => undefined));
|
|
79
|
+
}
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (message.kind === "floor_ack" || message.kind === "floor_refused")
|
|
83
|
+
return gate?.answer(frame.lane, message);
|
|
84
|
+
if (message.kind === "settled") {
|
|
85
|
+
settle(message.executionId, { kind: "settled", result: message.result });
|
|
86
|
+
ack(message.executionId);
|
|
87
|
+
}
|
|
88
|
+
else if (message.kind === "terminal") {
|
|
89
|
+
settle(message.executionId, { kind: "terminal", terminal: message });
|
|
90
|
+
ack(message.executionId);
|
|
91
|
+
}
|
|
92
|
+
else if (message.kind === "refused")
|
|
93
|
+
settle(message.executionId, { kind: "refused", reason: message.reason });
|
|
94
|
+
});
|
|
95
|
+
return {
|
|
96
|
+
dispatch(unstamped, signal) {
|
|
97
|
+
const message = { ...unstamped, ...stamp };
|
|
98
|
+
const { executionId } = message;
|
|
99
|
+
const replied = new Promise((resolve) => {
|
|
100
|
+
pending.set(executionId, { message, resolve });
|
|
101
|
+
});
|
|
102
|
+
if (signal?.aborted) {
|
|
103
|
+
settle(executionId, { kind: "aborted", sent: false });
|
|
104
|
+
return replied;
|
|
105
|
+
}
|
|
106
|
+
const onAbort = () => {
|
|
107
|
+
if (!pending.has(executionId))
|
|
108
|
+
return;
|
|
109
|
+
const reached = sent.has(executionId);
|
|
110
|
+
if (reached)
|
|
111
|
+
void send(message.scope.id, { kind: "abort", executionId, ...stamp }).catch(() => undefined);
|
|
112
|
+
settle(executionId, { kind: "aborted", sent: reached });
|
|
113
|
+
};
|
|
114
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
115
|
+
void replied.then(() => signal?.removeEventListener("abort", onAbort));
|
|
116
|
+
whenOpen(message.scope.id, executionId, () => {
|
|
117
|
+
sent.add(executionId);
|
|
118
|
+
send(message.scope.id, message).catch((error) => {
|
|
119
|
+
settle(executionId, {
|
|
120
|
+
kind: "unreachable",
|
|
121
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
return replied;
|
|
126
|
+
},
|
|
127
|
+
hello(ms, signal) {
|
|
128
|
+
if (announced !== undefined || waitedOut || signal?.aborted)
|
|
129
|
+
return Promise.resolve(announced);
|
|
130
|
+
return new Promise((resolve) => {
|
|
131
|
+
const done = (hello) => {
|
|
132
|
+
clearTimeout(timer);
|
|
133
|
+
helloWaiters.delete(done);
|
|
134
|
+
signal?.removeEventListener("abort", onAbort);
|
|
135
|
+
resolve(hello);
|
|
136
|
+
};
|
|
137
|
+
const onAbort = () => done(undefined);
|
|
138
|
+
const timer = setTimeout(() => {
|
|
139
|
+
waitedOut = true;
|
|
140
|
+
done(undefined);
|
|
141
|
+
}, ms);
|
|
142
|
+
helloWaiters.add(done);
|
|
143
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
144
|
+
});
|
|
145
|
+
},
|
|
146
|
+
close() {
|
|
147
|
+
for (const executionId of [...pending.keys()])
|
|
148
|
+
settle(executionId, { kind: "closed" });
|
|
149
|
+
transport.close();
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
//# 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,20 +9,63 @@ 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 | RemoteFloor | RemoteFloorAck | RemoteFloorRefused | 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[];
|
|
22
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
|
+
/** The sender's owner epoch, as on an invoke: a file operation is fenced by the same floor (S35). */
|
|
37
|
+
readonly epoch?: number;
|
|
38
|
+
readonly scope: ExecutionScope;
|
|
39
|
+
readonly op: DeviceFileOp;
|
|
40
|
+
readonly args: JsonValue;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The session's owner at `epoch` asks the hand to fence every lower epoch on this scope lane (brain/hand S35; relay
|
|
44
|
+
* design 4.4, the SP6 `epoch.floor` record). The owner dispatches the lane's calls only after the hand acked exactly
|
|
45
|
+
* its epoch.
|
|
46
|
+
*/
|
|
47
|
+
export interface RemoteFloor {
|
|
48
|
+
readonly kind: "floor";
|
|
49
|
+
readonly epoch: number;
|
|
50
|
+
}
|
|
51
|
+
/** The floor in force on the lane: the requested epoch, or a higher one when a newer owner already raised it. */
|
|
52
|
+
export interface RemoteFloorAck {
|
|
53
|
+
readonly kind: "floor_ack";
|
|
54
|
+
readonly floor: number;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Why a hand refused a floor (SP6's refusals, plus `epoch_unsupported` from a hand that keeps no floor): only the
|
|
58
|
+
* scope's owner raises it, by one epoch at a time, and another owner at the floor's epoch is fenced.
|
|
59
|
+
*/
|
|
60
|
+
export type EpochRefusal = "peer_unauthorized" | "epoch_fenced" | "epoch_step_exceeded" | "epoch_unsupported";
|
|
61
|
+
export interface RemoteFloorRefused {
|
|
62
|
+
readonly kind: "floor_refused";
|
|
63
|
+
readonly reason: EpochRefusal;
|
|
64
|
+
}
|
|
65
|
+
/** `epoch` on an invoke, abort or ack: the sender's owner epoch; a hand refuses one below the lane's floor. */
|
|
23
66
|
export interface RemoteInvoke {
|
|
24
67
|
readonly kind: "invoke";
|
|
68
|
+
readonly epoch?: number;
|
|
25
69
|
/** Unique per call per session; a hand runs one execution ID at most once. */
|
|
26
70
|
readonly executionId: string;
|
|
27
71
|
readonly batchId: string;
|
|
@@ -36,11 +80,13 @@ export interface RemoteInvoke {
|
|
|
36
80
|
export interface RemoteAbort {
|
|
37
81
|
readonly kind: "abort";
|
|
38
82
|
readonly executionId: string;
|
|
83
|
+
readonly epoch?: number;
|
|
39
84
|
}
|
|
40
85
|
/** The session has its outcome: the hand may forget it (a later replay is `result_expired`). */
|
|
41
86
|
export interface RemoteAck {
|
|
42
87
|
readonly kind: "ack";
|
|
43
88
|
readonly executionId: string;
|
|
89
|
+
readonly epoch?: number;
|
|
44
90
|
}
|
|
45
91
|
/** The reasons a call can end on a hand without a tool result (the endpoint state machine's terminal reasons). */
|
|
46
92
|
export type RemoteTerminalReason = "cancelled" | "lease_expired" | "hand_restarted" | "uncertain";
|
|
@@ -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 {
|
|
@@ -7,6 +8,12 @@ export class RemoteProtocolError extends Error {
|
|
|
7
8
|
}
|
|
8
9
|
}
|
|
9
10
|
const TERMINAL_REASONS = ["cancelled", "lease_expired", "hand_restarted", "uncertain"];
|
|
11
|
+
const EPOCH_REFUSALS = [
|
|
12
|
+
"peer_unauthorized",
|
|
13
|
+
"epoch_fenced",
|
|
14
|
+
"epoch_step_exceeded",
|
|
15
|
+
"epoch_unsupported",
|
|
16
|
+
];
|
|
10
17
|
const encoder = new TextEncoder();
|
|
11
18
|
const decoder = new TextDecoder();
|
|
12
19
|
export function encodeRemoteMessage(message) {
|
|
@@ -14,6 +21,25 @@ export function encodeRemoteMessage(message) {
|
|
|
14
21
|
}
|
|
15
22
|
const isRecord = (value) => typeof value === "object" && value !== null;
|
|
16
23
|
const isString = (value) => typeof value === "string" && value.length > 0;
|
|
24
|
+
const isEpoch = (value) => typeof value === "number" && Number.isSafeInteger(value) && value >= 1;
|
|
25
|
+
function decodeFloorMessage(value) {
|
|
26
|
+
switch (value.kind) {
|
|
27
|
+
case "floor":
|
|
28
|
+
if (!isEpoch(value.epoch))
|
|
29
|
+
throw new RemoteProtocolError("malformed floor");
|
|
30
|
+
return { kind: "floor", epoch: value.epoch };
|
|
31
|
+
case "floor_ack":
|
|
32
|
+
if (!isEpoch(value.floor))
|
|
33
|
+
throw new RemoteProtocolError("malformed floor ack");
|
|
34
|
+
return { kind: "floor_ack", floor: value.floor };
|
|
35
|
+
case "floor_refused":
|
|
36
|
+
if (!EPOCH_REFUSALS.includes(value.reason))
|
|
37
|
+
throw new RemoteProtocolError("malformed floor refusal");
|
|
38
|
+
return { kind: "floor_refused", reason: value.reason };
|
|
39
|
+
default:
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
17
43
|
/** Parses one frame payload. Anything that is not exactly one of the messages is refused; nothing guesses. */
|
|
18
44
|
export function decodeRemoteMessage(payload) {
|
|
19
45
|
let value;
|
|
@@ -24,12 +50,22 @@ export function decodeRemoteMessage(payload) {
|
|
|
24
50
|
throw new RemoteProtocolError("payload is not JSON");
|
|
25
51
|
}
|
|
26
52
|
if (isRecord(value) && value.kind === "hello") {
|
|
27
|
-
|
|
53
|
+
const { instance, durable, ops } = value;
|
|
54
|
+
if (!isString(instance))
|
|
55
|
+
throw new RemoteProtocolError("malformed hello");
|
|
56
|
+
if (durable !== undefined && typeof durable !== "boolean")
|
|
57
|
+
throw new RemoteProtocolError("malformed hello");
|
|
58
|
+
if (ops !== undefined && (!Array.isArray(ops) || !ops.every(isString)))
|
|
28
59
|
throw new RemoteProtocolError("malformed hello");
|
|
29
|
-
return { kind: "hello", instance:
|
|
60
|
+
return { kind: "hello", instance, durable: durable ?? true, ops: ops ?? [] };
|
|
30
61
|
}
|
|
62
|
+
const floor = isRecord(value) ? decodeFloorMessage(value) : undefined;
|
|
63
|
+
if (floor !== undefined)
|
|
64
|
+
return floor;
|
|
31
65
|
if (!isRecord(value) || !isString(value.executionId))
|
|
32
66
|
throw new RemoteProtocolError("no execution ID");
|
|
67
|
+
if (value.epoch !== undefined && !isEpoch(value.epoch))
|
|
68
|
+
throw new RemoteProtocolError("malformed epoch");
|
|
33
69
|
switch (value.kind) {
|
|
34
70
|
case "invoke": {
|
|
35
71
|
const scope = value.scope;
|
|
@@ -46,6 +82,16 @@ export function decodeRemoteMessage(payload) {
|
|
|
46
82
|
throw new RemoteProtocolError("malformed invoke");
|
|
47
83
|
return value;
|
|
48
84
|
}
|
|
85
|
+
case "file": {
|
|
86
|
+
const scope = value.scope;
|
|
87
|
+
if (!isRecord(scope) ||
|
|
88
|
+
!isString(scope.id) ||
|
|
89
|
+
typeof scope.root !== "string" ||
|
|
90
|
+
!DEVICE_FILE_OPS.includes(value.op) ||
|
|
91
|
+
!isRecord(value.args))
|
|
92
|
+
throw new RemoteProtocolError("malformed file operation");
|
|
93
|
+
return value;
|
|
94
|
+
}
|
|
49
95
|
case "abort":
|
|
50
96
|
return value;
|
|
51
97
|
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,34 @@ 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>>;
|
|
25
|
+
/**
|
|
26
|
+
* The planes a pending device file operation (S40) may host, on its own lane: a two-sided extension's forwarded tool
|
|
27
|
+
* events (S29), so its handler runs on the loop's copy for a file operation as for a tool call. Absent: none, so
|
|
28
|
+
* every nested request under a file operation is refused `parent_terminal`.
|
|
29
|
+
*/
|
|
30
|
+
readonly filePlanes?: readonly string[];
|
|
31
|
+
/**
|
|
32
|
+
* The session's owner epoch (brain/hand S35). With it, each scope lane first raises the hand's floor to this epoch
|
|
33
|
+
* and dispatches only after the hand acked exactly it; every invoke, file operation, abort and ack carries it.
|
|
34
|
+
* Without it, nothing about epochs is sent (a session that never moves).
|
|
35
|
+
*/
|
|
36
|
+
readonly epoch?: number;
|
|
17
37
|
}
|
|
18
38
|
/**
|
|
19
39
|
* The ToolHost that runs a session's tool calls on a hand (brain/hand split, S27a). The hand resolves, prepares,
|
|
20
40
|
* validates, runs the extensions' `tool_call`/`tool_result` handlers (permission checks among them) and executes each
|
|
21
41
|
* call; this host keeps the loop's side: tool events, the batch's ordering, a forwarded abort and the result messages in
|
|
22
42
|
* 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.
|
|
43
|
+
* re-send is on a durable hand's `hello` (see `createHandLink`). `deviceFiles(scope)` reaches the hand's files (S40).
|
|
25
44
|
*/
|
|
26
45
|
export declare function createRemoteToolHost(options: RemoteToolHostOptions): ToolHost & {
|
|
46
|
+
deviceFiles(scope: ExecutionScope): DeviceFiles;
|
|
27
47
|
close(): void;
|
|
28
48
|
};
|
|
29
49
|
//# sourceMappingURL=remote-tool-host.d.ts.map
|