@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,193 @@
|
|
|
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";
|
|
4
|
+
import { decodeRemoteMessage, encodeRemoteMessage, HAND_CONTROL_LANE, toRemoteToolResult, } from "./remote-protocol.js";
|
|
5
|
+
import { authorize } from "./grant-verify.js";
|
|
6
|
+
/**
|
|
7
|
+
* The hand's side of remote tool execution (brain/hand split, S27a). Each scope keeps its own state, opened lazily and
|
|
8
|
+
* bound to the root it was opened with: a later call naming the same scope with another root is refused, as is any
|
|
9
|
+
* execution ID the hand has already accepted (dispatch at most once). The session reference a call carries is never
|
|
10
|
+
* passed on: the hand keys on the scope and does not know the session (R16).
|
|
11
|
+
*/
|
|
12
|
+
export function createRemoteExecutor(options) {
|
|
13
|
+
const { transport, openScope, ledger, authority } = options;
|
|
14
|
+
const scopes = new Map();
|
|
15
|
+
const accepted = [];
|
|
16
|
+
const seen = new Set();
|
|
17
|
+
const running = new Map();
|
|
18
|
+
const planes = createPlaneClient({ transport });
|
|
19
|
+
let closed = false;
|
|
20
|
+
const stopWatching = authority.kind === "peer"
|
|
21
|
+
? authority.verifier.onReplace(() => {
|
|
22
|
+
for (const { controller, root } of running.values())
|
|
23
|
+
if (!authority.verifier.verify(authority.peer, root).ok)
|
|
24
|
+
controller.abort();
|
|
25
|
+
})
|
|
26
|
+
: () => { };
|
|
27
|
+
const reply = (lane, message) => {
|
|
28
|
+
void transport.send(lane, encodeRemoteMessage(message)).catch(() => undefined);
|
|
29
|
+
};
|
|
30
|
+
const refuse = (lane, executionId, reason) => reply(lane, { kind: "refused", executionId, reason });
|
|
31
|
+
const stateOf = (scope) => {
|
|
32
|
+
const known = scopes.get(scope.id);
|
|
33
|
+
if (known !== undefined)
|
|
34
|
+
return known.root === scope.root ? known : "scope_binding_mismatch";
|
|
35
|
+
const opened = openScope(scope);
|
|
36
|
+
opened.catch(() => undefined);
|
|
37
|
+
const state = { root: scope.root, opened, lastSequential: undefined, wave: [] };
|
|
38
|
+
scopes.set(scope.id, state);
|
|
39
|
+
return state;
|
|
40
|
+
};
|
|
41
|
+
const invoke = async (lane, call, file) => {
|
|
42
|
+
if (closed)
|
|
43
|
+
return refuse(lane, call.executionId, "hand_closed");
|
|
44
|
+
if (lane !== call.scope.id)
|
|
45
|
+
return refuse(lane, call.executionId, "scope_lane_mismatch");
|
|
46
|
+
const verdict = authorize(authority, call.scope.root);
|
|
47
|
+
if (!verdict.ok)
|
|
48
|
+
return refuse(lane, call.executionId, verdict.reason);
|
|
49
|
+
if (ledger === undefined && seen.has(call.executionId))
|
|
50
|
+
return refuse(lane, call.executionId, "duplicate_execution");
|
|
51
|
+
seen.add(call.executionId);
|
|
52
|
+
const state = stateOf({ id: call.scope.id, root: verdict.root });
|
|
53
|
+
if (typeof state === "string")
|
|
54
|
+
return refuse(lane, call.executionId, state);
|
|
55
|
+
let scope;
|
|
56
|
+
try {
|
|
57
|
+
scope = await state.opened;
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
61
|
+
return refuse(lane, call.executionId, `scope_unavailable: ${reason}`);
|
|
62
|
+
}
|
|
63
|
+
if (ledger !== undefined) {
|
|
64
|
+
const admission = await ledger.admit(call);
|
|
65
|
+
if (admission.kind === "reply")
|
|
66
|
+
return reply(lane, admission.message);
|
|
67
|
+
if (admission.kind === "in_flight")
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
accepted.push(call.executionId);
|
|
71
|
+
const controller = new AbortController();
|
|
72
|
+
running.set(call.executionId, { controller, root: verdict.root });
|
|
73
|
+
const sequential = file !== undefined || scope.isSequential(call.name);
|
|
74
|
+
const dependencies = sequential
|
|
75
|
+
? [...(state.lastSequential ? [state.lastSequential] : []), ...state.wave]
|
|
76
|
+
: state.lastSequential
|
|
77
|
+
? [state.lastSequential]
|
|
78
|
+
: [];
|
|
79
|
+
const done = (async () => {
|
|
80
|
+
await Promise.allSettled(dependencies);
|
|
81
|
+
let result;
|
|
82
|
+
try {
|
|
83
|
+
// The grant must still hold when the call starts: a snapshot installed while it waited (scope opening,
|
|
84
|
+
// durable admission, earlier calls of its scope) may have revoked it, and then the tool never runs.
|
|
85
|
+
if (controller.signal.aborted || !authorize(authority, call.scope.root).ok)
|
|
86
|
+
throw new Error("The call's grant was revoked before it ran");
|
|
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);
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
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
|
+
}
|
|
115
|
+
}
|
|
116
|
+
finally {
|
|
117
|
+
running.delete(call.executionId);
|
|
118
|
+
planes.parentEnded(call.executionId);
|
|
119
|
+
}
|
|
120
|
+
const settled = toRemoteToolResult(result);
|
|
121
|
+
reply(lane, ledger === undefined
|
|
122
|
+
? { kind: "settled", executionId: call.executionId, result: settled }
|
|
123
|
+
: await ledger.settle(call, settled));
|
|
124
|
+
})();
|
|
125
|
+
done.catch((error) => options.onFault(error));
|
|
126
|
+
if (sequential) {
|
|
127
|
+
state.lastSequential = done;
|
|
128
|
+
state.wave = [];
|
|
129
|
+
}
|
|
130
|
+
else
|
|
131
|
+
state.wave.push(done);
|
|
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.
|
|
135
|
+
const ready = (async () => {
|
|
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
|
+
});
|
|
145
|
+
})();
|
|
146
|
+
ready.catch((error) => options.onFault(error));
|
|
147
|
+
transport.onFrame(async (frame) => {
|
|
148
|
+
try {
|
|
149
|
+
await ready;
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const nested = decodePlaneMessage(frame.payload);
|
|
155
|
+
if (nested !== undefined)
|
|
156
|
+
return planes.receive(nested);
|
|
157
|
+
let message;
|
|
158
|
+
try {
|
|
159
|
+
message = decodeRemoteMessage(frame.payload);
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
try {
|
|
165
|
+
if (message.kind === "invoke")
|
|
166
|
+
await invoke(frame.lane, message);
|
|
167
|
+
else if (message.kind === "file")
|
|
168
|
+
await invoke(frame.lane, fileCall(message), { op: message.op });
|
|
169
|
+
else if (message.kind === "abort")
|
|
170
|
+
running.get(message.executionId)?.controller.abort();
|
|
171
|
+
else if (message.kind === "ack")
|
|
172
|
+
await ledger?.ack(message.executionId);
|
|
173
|
+
}
|
|
174
|
+
catch (error) {
|
|
175
|
+
options.onFault(error);
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
return {
|
|
179
|
+
accepted: () => [...accepted],
|
|
180
|
+
ready,
|
|
181
|
+
async close() {
|
|
182
|
+
closed = true;
|
|
183
|
+
stopWatching();
|
|
184
|
+
for (const { controller } of running.values())
|
|
185
|
+
controller.abort();
|
|
186
|
+
const opened = await Promise.allSettled([...scopes.values()].map((state) => state.opened));
|
|
187
|
+
scopes.clear();
|
|
188
|
+
await Promise.allSettled(opened.flatMap((outcome) => (outcome.status === "fulfilled" ? [outcome.value.dispose()] : [])));
|
|
189
|
+
transport.close();
|
|
190
|
+
},
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
//# sourceMappingURL=remote-executor.js.map
|
|
@@ -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,40 @@
|
|
|
1
|
+
import type { ExecutionLedger } from "./ledger.ts";
|
|
2
|
+
import type { RemoteHandMessage, RemoteInvoke, RemoteToolResult } from "./remote-protocol.ts";
|
|
3
|
+
export interface HandLedgerOptions {
|
|
4
|
+
readonly ledger: ExecutionLedger;
|
|
5
|
+
/** This hand's identity, the ledger's executor. */
|
|
6
|
+
readonly executor: string;
|
|
7
|
+
/** The authenticated session the calls come from, the ledger's issuer. */
|
|
8
|
+
readonly issuer: string;
|
|
9
|
+
readonly workspace: string;
|
|
10
|
+
readonly now: () => number;
|
|
11
|
+
/** How long an admitted call may live; the ledger caps it at 24 hours. */
|
|
12
|
+
readonly leaseMs: number;
|
|
13
|
+
}
|
|
14
|
+
/** What an invoke may do: run now, be answered from the record (an outcome or a refusal), or wait on its own run. */
|
|
15
|
+
export type HandAdmission = {
|
|
16
|
+
readonly kind: "run";
|
|
17
|
+
} | {
|
|
18
|
+
readonly kind: "reply";
|
|
19
|
+
readonly message: RemoteHandMessage;
|
|
20
|
+
} | {
|
|
21
|
+
readonly kind: "in_flight";
|
|
22
|
+
};
|
|
23
|
+
export interface HandLedger {
|
|
24
|
+
admit(invoke: RemoteInvoke): Promise<HandAdmission>;
|
|
25
|
+
settle(invoke: RemoteInvoke, result: RemoteToolResult): Promise<RemoteHandMessage>;
|
|
26
|
+
ack(executionId: string): Promise<void>;
|
|
27
|
+
/** Runs the ledger's restart recovery, then returns every outcome the session has not acknowledged, by lane. */
|
|
28
|
+
recover(): Promise<Array<{
|
|
29
|
+
readonly lane: string;
|
|
30
|
+
readonly message: RemoteHandMessage;
|
|
31
|
+
}>>;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* A hand's durable record of the calls it runs (brain/hand split, S27a), on the execution ledger. A call is admitted and
|
|
35
|
+
* walked to `dispatched` BEFORE its handlers and tool run, so a hand that dies mid-call recovers it as `hand_restarted`
|
|
36
|
+
* (uncertain) and never runs it again; a re-delivered invoke is answered from the record. Today's run is one unit
|
|
37
|
+
* (handlers, then the tool), so a call is frozen with its raw name and arguments.
|
|
38
|
+
*/
|
|
39
|
+
export declare function createHandLedger(options: HandLedgerOptions): HandLedger;
|
|
40
|
+
//# sourceMappingURL=remote-hand-ledger.d.ts.map
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
const GENERATION = 1;
|
|
2
|
+
function outcomeOf(entry) {
|
|
3
|
+
const executionId = entry.commandId;
|
|
4
|
+
if (entry.progress.state === "acked")
|
|
5
|
+
return { kind: "refused", executionId, reason: "result_expired" };
|
|
6
|
+
if (entry.progress.state !== "settled")
|
|
7
|
+
return undefined;
|
|
8
|
+
const { outcome } = entry.progress;
|
|
9
|
+
if (outcome.reason === "result") {
|
|
10
|
+
const result = JSON.parse(String(outcome.value));
|
|
11
|
+
return { kind: "settled", executionId, result };
|
|
12
|
+
}
|
|
13
|
+
// A hand settles a call only with `result` (settle) or a recovery terminal. A `blocked` outcome is the brain's
|
|
14
|
+
// prepared executor's (S09): a record holding one was not written by this hand, so it is refused, never reported as run.
|
|
15
|
+
if (outcome.reason === "blocked")
|
|
16
|
+
return { kind: "refused", executionId, reason: "foreign_outcome" };
|
|
17
|
+
return {
|
|
18
|
+
kind: "terminal",
|
|
19
|
+
executionId,
|
|
20
|
+
reason: outcome.reason,
|
|
21
|
+
uncertain: outcome.uncertain,
|
|
22
|
+
hooksRan: outcome.hooksRan,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function asStoreValue(value) {
|
|
26
|
+
return value === undefined ? null : JSON.parse(JSON.stringify(value));
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* A hand's durable record of the calls it runs (brain/hand split, S27a), on the execution ledger. A call is admitted and
|
|
30
|
+
* walked to `dispatched` BEFORE its handlers and tool run, so a hand that dies mid-call recovers it as `hand_restarted`
|
|
31
|
+
* (uncertain) and never runs it again; a re-delivered invoke is answered from the record. Today's run is one unit
|
|
32
|
+
* (handlers, then the tool), so a call is frozen with its raw name and arguments.
|
|
33
|
+
*/
|
|
34
|
+
export function createHandLedger(options) {
|
|
35
|
+
const { ledger, executor, issuer, workspace, now, leaseMs } = options;
|
|
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 });
|
|
38
|
+
/** Calls this process admitted or runs; a duplicate invoke for one of them waits on that run. */
|
|
39
|
+
const owned = new Set();
|
|
40
|
+
const replyFrom = (reply, executionId) => {
|
|
41
|
+
if (reply.status === "refused")
|
|
42
|
+
return { kind: "refused", executionId, reason: reply.reason };
|
|
43
|
+
if (reply.status === "result_expired")
|
|
44
|
+
return { kind: "refused", executionId, reason: "result_expired" };
|
|
45
|
+
return outcomeOf(reply.entry) ?? { kind: "refused", executionId, reason: "not_settled" };
|
|
46
|
+
};
|
|
47
|
+
const admitNew = (invoke, digest) => {
|
|
48
|
+
const admittedAt = now();
|
|
49
|
+
return ledger.admit({
|
|
50
|
+
issuer,
|
|
51
|
+
commandId: invoke.executionId,
|
|
52
|
+
executor,
|
|
53
|
+
workspace,
|
|
54
|
+
scope: invoke.scope.id,
|
|
55
|
+
generation: GENERATION,
|
|
56
|
+
requestDigest: digest,
|
|
57
|
+
admittedAt,
|
|
58
|
+
leaseUntil: admittedAt + leaseMs,
|
|
59
|
+
reconcilable: false,
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Walks a call to `dispatched` from whatever its record holds: a new call from admission, a `prepared` or
|
|
64
|
+
* `released` call that survived a restart from there. It runs only when THIS walk's `dispatch` applied. A record
|
|
65
|
+
* that holds another request under this execution ID (other name, arguments or scope) is refused
|
|
66
|
+
* `identity_conflict`, as the ledger refuses such an admission: a resumed call runs only what was frozen.
|
|
67
|
+
*/
|
|
68
|
+
const advance = async (invoke) => {
|
|
69
|
+
const { executionId } = invoke;
|
|
70
|
+
const args = asStoreValue(invoke.arguments);
|
|
71
|
+
const frozen = { name: invoke.name, args, digest: JSON.stringify([invoke.name, args]) };
|
|
72
|
+
const prior = await ledger.read(keyOf(executionId));
|
|
73
|
+
if (prior !== undefined && (prior.requestDigest !== frozen.digest || prior.scope !== invoke.scope.id))
|
|
74
|
+
return { kind: "reply", message: { kind: "refused", executionId, reason: "identity_conflict" } };
|
|
75
|
+
const settled = prior === undefined ? undefined : outcomeOf(prior);
|
|
76
|
+
if (settled !== undefined)
|
|
77
|
+
return { kind: "reply", message: settled };
|
|
78
|
+
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),
|
|
83
|
+
];
|
|
84
|
+
const state = prior?.progress.state;
|
|
85
|
+
const steps = state === undefined ? fromStart : state === "prepared" || state === "released" ? [] : undefined;
|
|
86
|
+
if (steps === undefined)
|
|
87
|
+
return { kind: "in_flight" };
|
|
88
|
+
for (const step of [...steps, () => apply(executionId, { kind: "release" }, issuer)]) {
|
|
89
|
+
const reply = await step();
|
|
90
|
+
if (reply.status === "replayed" && reply.entry.progress.state !== "settled")
|
|
91
|
+
continue;
|
|
92
|
+
if (reply.status !== "applied" || reply.entry.progress.state === "settled")
|
|
93
|
+
return { kind: "reply", message: replyFrom(reply, executionId) };
|
|
94
|
+
}
|
|
95
|
+
const dispatched = await apply(executionId, { kind: "dispatch" }, executor);
|
|
96
|
+
if (dispatched.status === "applied" && dispatched.entry.progress.state === "dispatched")
|
|
97
|
+
return { kind: "run" };
|
|
98
|
+
if (dispatched.status === "replayed" && dispatched.entry.progress.state !== "settled")
|
|
99
|
+
return { kind: "in_flight" };
|
|
100
|
+
return { kind: "reply", message: replyFrom(dispatched, executionId) };
|
|
101
|
+
};
|
|
102
|
+
return {
|
|
103
|
+
async admit(invoke) {
|
|
104
|
+
const { executionId } = invoke;
|
|
105
|
+
if (owned.has(executionId))
|
|
106
|
+
return { kind: "in_flight" };
|
|
107
|
+
owned.add(executionId);
|
|
108
|
+
const admission = await advance(invoke).catch((error) => {
|
|
109
|
+
owned.delete(executionId);
|
|
110
|
+
throw error;
|
|
111
|
+
});
|
|
112
|
+
if (admission.kind !== "run")
|
|
113
|
+
owned.delete(executionId);
|
|
114
|
+
return admission;
|
|
115
|
+
},
|
|
116
|
+
async settle(invoke, result) {
|
|
117
|
+
try {
|
|
118
|
+
const reply = await apply(invoke.executionId, { kind: "result", value: JSON.stringify(result) }, executor);
|
|
119
|
+
return replyFrom(reply, invoke.executionId);
|
|
120
|
+
}
|
|
121
|
+
finally {
|
|
122
|
+
owned.delete(invoke.executionId);
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
async ack(executionId) {
|
|
126
|
+
await apply(executionId, { kind: "ack" }, issuer);
|
|
127
|
+
},
|
|
128
|
+
async recover() {
|
|
129
|
+
await ledger.recover(now());
|
|
130
|
+
return (await ledger.scan()).flatMap((entry) => {
|
|
131
|
+
const message = entry.progress.state === "settled" ? outcomeOf(entry) : undefined;
|
|
132
|
+
return message === undefined ? [] : [{ lane: entry.scope, message }];
|
|
133
|
+
});
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
//# sourceMappingURL=remote-hand-ledger.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
|