@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,87 @@
|
|
|
1
|
+
/** The channel's name in a hand's `hello`. */
|
|
2
|
+
export const FILE_CHANNEL = "file/1";
|
|
3
|
+
/** The most data one operation reads or writes, in raw bytes, in both directions. */
|
|
4
|
+
export const FILE_CHANNEL_MAX_BYTES = 8 * 1024 * 1024;
|
|
5
|
+
export const DEVICE_FILE_OPS = ["read", "write", "list", "stat", "rename", "remove", "mkdir"];
|
|
6
|
+
/**
|
|
7
|
+
* A device file operation that did not complete. `interrupted` carries the hand's terminal reason; `uncertain` says the
|
|
8
|
+
* operation may have taken effect (a write may or may not have landed). Nothing is ever retried.
|
|
9
|
+
*/
|
|
10
|
+
export class DeviceFileError extends Error {
|
|
11
|
+
constructor(code, message, detail = {}) {
|
|
12
|
+
super(`${code}: ${message}`);
|
|
13
|
+
this.name = "DeviceFileError";
|
|
14
|
+
this.code = code;
|
|
15
|
+
this.reason = detail.reason;
|
|
16
|
+
this.uncertain = detail.uncertain ?? false;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const CODES = [
|
|
20
|
+
"not_found",
|
|
21
|
+
"not_a_file",
|
|
22
|
+
"exists",
|
|
23
|
+
"too_large",
|
|
24
|
+
"io",
|
|
25
|
+
"aborted",
|
|
26
|
+
"refused_by_grant",
|
|
27
|
+
"refused_by_policy",
|
|
28
|
+
"hand_unreachable",
|
|
29
|
+
"unsupported",
|
|
30
|
+
"interrupted",
|
|
31
|
+
"protocol",
|
|
32
|
+
];
|
|
33
|
+
function isDeviceFileErrorCode(value) {
|
|
34
|
+
return typeof value === "string" && CODES.includes(value);
|
|
35
|
+
}
|
|
36
|
+
const isRecord = (value) => typeof value === "object" && value !== null;
|
|
37
|
+
/** Parses an operation's arguments; `undefined` for anything malformed. */
|
|
38
|
+
export function decodeDeviceFileArgs(op, args) {
|
|
39
|
+
if (!isRecord(args) || typeof args.path !== "string" || args.path.length === 0)
|
|
40
|
+
return undefined;
|
|
41
|
+
const path = args.path;
|
|
42
|
+
switch (op) {
|
|
43
|
+
case "read":
|
|
44
|
+
case "list":
|
|
45
|
+
case "stat":
|
|
46
|
+
case "remove":
|
|
47
|
+
case "mkdir":
|
|
48
|
+
return { op, path };
|
|
49
|
+
case "write":
|
|
50
|
+
return typeof args.data === "string"
|
|
51
|
+
? { op, path, data: args.data, exclusive: args.exclusive === true }
|
|
52
|
+
: undefined;
|
|
53
|
+
case "rename":
|
|
54
|
+
return typeof args.to === "string" && args.to.length > 0 ? { op, path, to: args.to } : undefined;
|
|
55
|
+
default:
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export function encodeBase64(bytes) {
|
|
60
|
+
let binary = "";
|
|
61
|
+
for (let start = 0; start < bytes.length; start += 0x8000)
|
|
62
|
+
binary += String.fromCharCode(...bytes.subarray(start, start + 0x8000));
|
|
63
|
+
return btoa(binary);
|
|
64
|
+
}
|
|
65
|
+
export function decodeBase64(text) {
|
|
66
|
+
const binary = atob(text);
|
|
67
|
+
const bytes = new Uint8Array(binary.length);
|
|
68
|
+
for (let index = 0; index < binary.length; index++)
|
|
69
|
+
bytes[index] = binary.charCodeAt(index);
|
|
70
|
+
return bytes;
|
|
71
|
+
}
|
|
72
|
+
/** The raw byte length a base64 string decodes to, without decoding it. */
|
|
73
|
+
export function base64Length(text) {
|
|
74
|
+
const padding = text.endsWith("==") ? 2 : text.endsWith("=") ? 1 : 0;
|
|
75
|
+
return Math.floor((text.length * 3) / 4) - padding;
|
|
76
|
+
}
|
|
77
|
+
export function fileOutcomeOf(details) {
|
|
78
|
+
if (!isRecord(details) || !isRecord(details.file))
|
|
79
|
+
return undefined;
|
|
80
|
+
const file = details.file;
|
|
81
|
+
if (file.ok === true && "value" in file)
|
|
82
|
+
return { ok: true, value: file.value };
|
|
83
|
+
if (file.ok === false && isDeviceFileErrorCode(file.code) && typeof file.message === "string")
|
|
84
|
+
return { ok: false, code: file.code, message: file.message };
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=device-files.js.map
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A hand's offline check of the sender's capability grant (brain/hand split S27b; relay decision 0009, R14): before
|
|
3
|
+
* anything of a call runs, the hand verifies that the link's authenticated peer holds `execute` toward this hand for
|
|
4
|
+
* the workspace the call's root lies in, from a local snapshot of the verified roster. Nothing is contacted per call.
|
|
5
|
+
*
|
|
6
|
+
* The embedder (the device service) verifies the signed roster, installs snapshots with a non-decreasing `rv` and, during
|
|
7
|
+
* the v1 dual-serve, derives the rows by 0009's role backfill. It also owns the peer binding: one executor per
|
|
8
|
+
* authenticated-sender link, no other sender's frames routed into it, and a carrier re-attach only for the same sender.
|
|
9
|
+
*/
|
|
10
|
+
/** The capability a hand requires: a tool call is the shell/files/process effect 0014 and 0016 define as `execute`. */
|
|
11
|
+
export declare const HAND_CAPABILITY = "execute";
|
|
12
|
+
/** The workspace label of a configured root that names none (0009's default workspace). */
|
|
13
|
+
export declare const DEFAULT_WORKSPACE = "omo";
|
|
14
|
+
/** One 0009 grant row: `holder` may do `capability` on `target` in `workspace`. Exact; no capability implies another. */
|
|
15
|
+
export interface GrantRow {
|
|
16
|
+
readonly holder: string;
|
|
17
|
+
readonly target: string;
|
|
18
|
+
readonly workspace: string;
|
|
19
|
+
readonly capability: string;
|
|
20
|
+
}
|
|
21
|
+
/** A verified roster's grants as the hand holds them. `expiresAt` is the roster's `exp_ms`. */
|
|
22
|
+
export interface GrantSnapshot {
|
|
23
|
+
readonly rv: number;
|
|
24
|
+
readonly expiresAt: number;
|
|
25
|
+
readonly grants: readonly GrantRow[];
|
|
26
|
+
}
|
|
27
|
+
export type GrantRefusal = "grant_missing" | "grant_expired" | "workspace_unknown";
|
|
28
|
+
export type GrantVerdict = {
|
|
29
|
+
readonly ok: true;
|
|
30
|
+
readonly root: string;
|
|
31
|
+
} | {
|
|
32
|
+
readonly ok: false;
|
|
33
|
+
readonly reason: GrantRefusal;
|
|
34
|
+
};
|
|
35
|
+
export interface WorkspaceRoot {
|
|
36
|
+
readonly root: string;
|
|
37
|
+
readonly label?: string;
|
|
38
|
+
}
|
|
39
|
+
export interface GrantVerifierOptions {
|
|
40
|
+
/** This hand's device ID: the target of every grant it accepts. */
|
|
41
|
+
readonly self: string;
|
|
42
|
+
/** The roots this hand serves; a call's root must lie inside one of them. */
|
|
43
|
+
readonly workspaces: readonly WorkspaceRoot[];
|
|
44
|
+
/** The canonical form of an existing path (the real path); throws for a path that does not exist or cannot be read. */
|
|
45
|
+
readonly canonicalize: (path: string) => string;
|
|
46
|
+
readonly now?: () => number;
|
|
47
|
+
}
|
|
48
|
+
export interface GrantVerifier {
|
|
49
|
+
/** Reads one snapshot; never what the session says about itself (R16). */
|
|
50
|
+
verify(peer: string, scopeRoot: string): GrantVerdict;
|
|
51
|
+
/** Replaces the snapshot. A lower `rv` than the installed one is ignored (rollback); a non-finite `rv` or `expiresAt` throws. */
|
|
52
|
+
install(snapshot: GrantSnapshot): void;
|
|
53
|
+
/** Called after every replacement, so running calls can be re-verified. Returns the unsubscribe. */
|
|
54
|
+
onReplace(listener: () => void): () => void;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Whose calls an executor runs: its own device's (`self`: fused and split-local, no grant needed, 0009) or one
|
|
58
|
+
* authenticated peer's, every call checked by `verifier`. Required, so a peer link without a verifier cannot be built.
|
|
59
|
+
*/
|
|
60
|
+
export type HandAuthority = {
|
|
61
|
+
readonly kind: "self";
|
|
62
|
+
} | {
|
|
63
|
+
readonly kind: "peer";
|
|
64
|
+
readonly peer: string;
|
|
65
|
+
readonly verifier: GrantVerifier;
|
|
66
|
+
};
|
|
67
|
+
/** The root a call may open its scope on, or why it is refused; a self link keeps the call's root as given. */
|
|
68
|
+
export declare function authorize(authority: HandAuthority, scopeRoot: string): GrantVerdict;
|
|
69
|
+
export declare function createGrantVerifier(options: GrantVerifierOptions): GrantVerifier;
|
|
70
|
+
//# sourceMappingURL=grant-verify.d.ts.map
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A hand's offline check of the sender's capability grant (brain/hand split S27b; relay decision 0009, R14): before
|
|
3
|
+
* anything of a call runs, the hand verifies that the link's authenticated peer holds `execute` toward this hand for
|
|
4
|
+
* the workspace the call's root lies in, from a local snapshot of the verified roster. Nothing is contacted per call.
|
|
5
|
+
*
|
|
6
|
+
* The embedder (the device service) verifies the signed roster, installs snapshots with a non-decreasing `rv` and, during
|
|
7
|
+
* the v1 dual-serve, derives the rows by 0009's role backfill. It also owns the peer binding: one executor per
|
|
8
|
+
* authenticated-sender link, no other sender's frames routed into it, and a carrier re-attach only for the same sender.
|
|
9
|
+
*/
|
|
10
|
+
/** The capability a hand requires: a tool call is the shell/files/process effect 0014 and 0016 define as `execute`. */
|
|
11
|
+
export const HAND_CAPABILITY = "execute";
|
|
12
|
+
/** The workspace label of a configured root that names none (0009's default workspace). */
|
|
13
|
+
export const DEFAULT_WORKSPACE = "omo";
|
|
14
|
+
/** The root a call may open its scope on, or why it is refused; a self link keeps the call's root as given. */
|
|
15
|
+
export function authorize(authority, scopeRoot) {
|
|
16
|
+
return authority.kind === "self"
|
|
17
|
+
? { ok: true, root: scopeRoot }
|
|
18
|
+
: authority.verifier.verify(authority.peer, scopeRoot);
|
|
19
|
+
}
|
|
20
|
+
const SEPARATORS = /[\\/]+$/;
|
|
21
|
+
/** `child` is `root` or lies under it, compared by whole path components (`/ws` never contains `/ws2`). */
|
|
22
|
+
function contains(root, child) {
|
|
23
|
+
const base = root.replace(SEPARATORS, "");
|
|
24
|
+
if (child === base)
|
|
25
|
+
return true;
|
|
26
|
+
if (!child.startsWith(base))
|
|
27
|
+
return false;
|
|
28
|
+
const next = child.charAt(base.length);
|
|
29
|
+
return next === "/" || next === "\\";
|
|
30
|
+
}
|
|
31
|
+
export function createGrantVerifier(options) {
|
|
32
|
+
const { self, canonicalize } = options;
|
|
33
|
+
const now = options.now ?? Date.now;
|
|
34
|
+
const listeners = new Set();
|
|
35
|
+
let snapshot;
|
|
36
|
+
const canonical = (path) => {
|
|
37
|
+
try {
|
|
38
|
+
return canonicalize(path);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
const workspaceOf = (root) => {
|
|
45
|
+
let best;
|
|
46
|
+
for (const workspace of options.workspaces) {
|
|
47
|
+
const resolved = canonical(workspace.root);
|
|
48
|
+
if (resolved === undefined || !contains(resolved, root))
|
|
49
|
+
continue;
|
|
50
|
+
if (best === undefined || resolved.length > best.length)
|
|
51
|
+
best = { label: workspace.label ?? DEFAULT_WORKSPACE, length: resolved.length };
|
|
52
|
+
}
|
|
53
|
+
return best?.label;
|
|
54
|
+
};
|
|
55
|
+
return {
|
|
56
|
+
verify(peer, scopeRoot) {
|
|
57
|
+
const root = canonical(scopeRoot);
|
|
58
|
+
const workspace = root === undefined ? undefined : workspaceOf(root);
|
|
59
|
+
if (root === undefined || workspace === undefined)
|
|
60
|
+
return { ok: false, reason: "workspace_unknown" };
|
|
61
|
+
if (peer === self)
|
|
62
|
+
return { ok: true, root };
|
|
63
|
+
const current = snapshot;
|
|
64
|
+
if (current === undefined || now() >= current.expiresAt)
|
|
65
|
+
return { ok: false, reason: "grant_expired" };
|
|
66
|
+
const granted = current.grants.some((row) => row.holder === peer &&
|
|
67
|
+
row.target === self &&
|
|
68
|
+
row.workspace === workspace &&
|
|
69
|
+
row.capability === HAND_CAPABILITY);
|
|
70
|
+
return granted ? { ok: true, root } : { ok: false, reason: "grant_missing" };
|
|
71
|
+
},
|
|
72
|
+
install(next) {
|
|
73
|
+
if (!Number.isFinite(next.rv) || !Number.isFinite(next.expiresAt))
|
|
74
|
+
throw new Error("A grant snapshot needs a finite rv and expiresAt; the installed snapshot is kept");
|
|
75
|
+
if (snapshot !== undefined && next.rv < snapshot.rv)
|
|
76
|
+
return;
|
|
77
|
+
snapshot = next;
|
|
78
|
+
for (const listener of [...listeners])
|
|
79
|
+
listener();
|
|
80
|
+
},
|
|
81
|
+
onReplace(listener) {
|
|
82
|
+
listeners.add(listener);
|
|
83
|
+
return () => listeners.delete(listener);
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=grant-verify.js.map
|
|
@@ -7,6 +7,8 @@ export interface ExecutionLedger {
|
|
|
7
7
|
read(key: ExecutionKey): Promise<ExecutionEntry | undefined>;
|
|
8
8
|
/** Called once on endpoint restart; also expires overdue leases and reconciliation deadlines. */
|
|
9
9
|
recover(now: number): Promise<void>;
|
|
10
|
+
/** Every execution row this ledger holds, in key order. */
|
|
11
|
+
scan(): Promise<ExecutionEntry[]>;
|
|
10
12
|
}
|
|
11
13
|
/** All ledgers sharing a writable store epoch must share its single admission line. */
|
|
12
14
|
export declare function createExecutionLedger(options: {
|
package/dist/execution/ledger.js
CHANGED
|
@@ -84,6 +84,7 @@ export function createExecutionLedger(options) {
|
|
|
84
84
|
await persist(entry, "admit");
|
|
85
85
|
return applied(entry);
|
|
86
86
|
}),
|
|
87
|
+
scan: async () => (await store.scanDocuments(PREFIX)).map((document) => decodeExecution(document.data)),
|
|
87
88
|
recover: async (now) => {
|
|
88
89
|
for (const document of await store.scanDocuments(PREFIX)) {
|
|
89
90
|
const entry = decodeExecution(document.data);
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { StoreValue } from "../durable/store.ts";
|
|
2
|
+
import type { NestedRecord } from "./nested-types.ts";
|
|
3
|
+
/** Decodes the brain's nested record; anything else is a corrupt store, never a guess. */
|
|
4
|
+
export declare function decodeNestedRecord(value: StoreValue): NestedRecord;
|
|
5
|
+
//# sourceMappingURL=nested-codec.d.ts.map
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const object = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
2
|
+
const strings = (value, keys) => keys.every((key) => typeof value[key] === "string");
|
|
3
|
+
const numbers = (value, keys) => keys.every((key) => typeof value[key] === "number" && Number.isFinite(value[key]));
|
|
4
|
+
const stringList = (value) => Array.isArray(value) && value.every((entry) => typeof entry === "string");
|
|
5
|
+
function parent(value) {
|
|
6
|
+
return (object(value) &&
|
|
7
|
+
strings(value, ["executionId", "executor", "scope"]) &&
|
|
8
|
+
numbers(value, ["generation", "admittedAt", "leaseUntil", "budget"]) &&
|
|
9
|
+
object(value.capabilities) &&
|
|
10
|
+
stringList(value.capabilities.preparation) &&
|
|
11
|
+
stringList(value.capabilities.body));
|
|
12
|
+
}
|
|
13
|
+
function child(value) {
|
|
14
|
+
return (object(value) &&
|
|
15
|
+
strings(value, ["executionId", "target"]) &&
|
|
16
|
+
numbers(value, ["cost"]) &&
|
|
17
|
+
value.origin === "tool" &&
|
|
18
|
+
(value.activityId === null || typeof value.activityId === "string"));
|
|
19
|
+
}
|
|
20
|
+
function activity(value) {
|
|
21
|
+
return (object(value) &&
|
|
22
|
+
strings(value, ["activityId", "parent", "executor", "scope"]) &&
|
|
23
|
+
numbers(value, ["budget", "expiresAt", "debited"]) &&
|
|
24
|
+
stringList(value.targets) &&
|
|
25
|
+
typeof value.ended === "boolean");
|
|
26
|
+
}
|
|
27
|
+
const each = (value, check) => object(value) && Object.values(value).every(check);
|
|
28
|
+
/** Decodes the brain's nested record; anything else is a corrupt store, never a guess. */
|
|
29
|
+
export function decodeNestedRecord(value) {
|
|
30
|
+
if (object(value) &&
|
|
31
|
+
parent(value.parent) &&
|
|
32
|
+
(value.phase === "preparation" || value.phase === "body" || value.phase === "settled") &&
|
|
33
|
+
typeof value.cancelled === "boolean" &&
|
|
34
|
+
numbers(value, ["expiresAt", "debited"]) &&
|
|
35
|
+
each(value.children, child) &&
|
|
36
|
+
each(value.activities, activity) &&
|
|
37
|
+
each(value.cancels, (entry) => entry === "queued" || entry === "delivered"))
|
|
38
|
+
return value;
|
|
39
|
+
throw new Error("corrupt nested admission record");
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=nested-codec.js.map
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { ExecutionRef } from "./coordinator-types.ts";
|
|
2
|
+
/** 0010's lifetime of an execution: a nested set never outlives its parent's admission by more than this. */
|
|
3
|
+
export declare const NESTED_LIFETIME_MS: number;
|
|
4
|
+
/** The parent execution a nested set belongs to, as the brain recorded it when it sent the parent's prepare. */
|
|
5
|
+
export type NestedParent = {
|
|
6
|
+
readonly executionId: string;
|
|
7
|
+
readonly generation: number;
|
|
8
|
+
/** The executor the parent was sent to: the only sender whose nested requests name this parent. */
|
|
9
|
+
readonly executor: string;
|
|
10
|
+
readonly scope: string;
|
|
11
|
+
readonly admittedAt: number;
|
|
12
|
+
readonly leaseUntil: number;
|
|
13
|
+
/** The aggregate reservation every child (and every activity's child) is debited from. */
|
|
14
|
+
readonly budget: number;
|
|
15
|
+
/** Targets a nested request may name: `preparation` from the prepare on, `body` added at release. */
|
|
16
|
+
readonly capabilities: {
|
|
17
|
+
readonly preparation: readonly string[];
|
|
18
|
+
readonly body: readonly string[];
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
/** A coordinator-owned background activity of a detached cell (design 5.6.3), registered before the detached result. */
|
|
22
|
+
export type NestedActivity = {
|
|
23
|
+
readonly activityId: string;
|
|
24
|
+
readonly parent: string;
|
|
25
|
+
readonly executor: string;
|
|
26
|
+
readonly scope: string;
|
|
27
|
+
/** A subset of the parent's targets: the activity never holds authority the parent did not. */
|
|
28
|
+
readonly targets: readonly string[];
|
|
29
|
+
readonly budget: number;
|
|
30
|
+
readonly expiresAt: number;
|
|
31
|
+
};
|
|
32
|
+
/** A nested request as the brain's endpoint hands it over; `sender` is the authenticated hand, never a claim. */
|
|
33
|
+
export type NestedRequest = {
|
|
34
|
+
readonly sender: string;
|
|
35
|
+
readonly parent: string;
|
|
36
|
+
readonly parentGeneration: number;
|
|
37
|
+
readonly scope: string;
|
|
38
|
+
readonly childRequestId: string;
|
|
39
|
+
/** The executor the child runs on. */
|
|
40
|
+
readonly target: string;
|
|
41
|
+
readonly cost: number;
|
|
42
|
+
/** Set by a detached cell: admitted against the live activity, never by reviving the terminal invocation. */
|
|
43
|
+
readonly activityId?: string;
|
|
44
|
+
};
|
|
45
|
+
export type NestedRefusal = "unknown_parent" | "not_parent_executor" | "stale_generation" | "scope_mismatch" | "parent_terminal" | "expired" | "not_in_nested_set" | "budget_exhausted" | "invalid_cost" | "unknown_activity" | "activity_ended" | "conflicting_request";
|
|
46
|
+
/** A child is tool-originated work, never human intent. */
|
|
47
|
+
export type NestedChild = {
|
|
48
|
+
readonly executionId: string;
|
|
49
|
+
readonly target: string;
|
|
50
|
+
readonly cost: number;
|
|
51
|
+
readonly origin: "tool";
|
|
52
|
+
readonly activityId: string | null;
|
|
53
|
+
};
|
|
54
|
+
export type NestedAdmission = {
|
|
55
|
+
readonly kind: "admitted";
|
|
56
|
+
readonly child: ExecutionRef;
|
|
57
|
+
readonly target: string;
|
|
58
|
+
readonly replayed: boolean;
|
|
59
|
+
} | {
|
|
60
|
+
readonly kind: "refused";
|
|
61
|
+
readonly reason: NestedRefusal;
|
|
62
|
+
};
|
|
63
|
+
export type ActivityRefusal = "parent_terminal" | "unknown_parent" | "exceeds_parent" | "conflicting_activity";
|
|
64
|
+
export type ActivityRecord = NestedActivity & {
|
|
65
|
+
readonly debited: number;
|
|
66
|
+
readonly ended: boolean;
|
|
67
|
+
};
|
|
68
|
+
/** The brain's durable record of one parent's nested work. */
|
|
69
|
+
export type NestedRecord = {
|
|
70
|
+
readonly parent: NestedParent;
|
|
71
|
+
readonly phase: "preparation" | "body" | "settled";
|
|
72
|
+
readonly cancelled: boolean;
|
|
73
|
+
/** The nested set's expiry: follows the parent's renewals, never past its lease or its lifetime. */
|
|
74
|
+
readonly expiresAt: number;
|
|
75
|
+
readonly debited: number;
|
|
76
|
+
readonly children: {
|
|
77
|
+
readonly [childRequestId: string]: NestedChild;
|
|
78
|
+
};
|
|
79
|
+
readonly activities: {
|
|
80
|
+
readonly [activityId: string]: ActivityRecord;
|
|
81
|
+
};
|
|
82
|
+
/** The parent cancel's cascade, per child execution id: queued until its executor took it. */
|
|
83
|
+
readonly cancels: {
|
|
84
|
+
readonly [childExecutionId: string]: "queued" | "delivered";
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
/** Where a parent cancel is delivered: the child's executor, with 0010 cancel semantics. */
|
|
88
|
+
export type NestedCancelTarget = {
|
|
89
|
+
readonly ref: ExecutionRef;
|
|
90
|
+
readonly executor: string;
|
|
91
|
+
};
|
|
92
|
+
//# sourceMappingURL=nested-types.d.ts.map
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { DurableStore } from "../durable/store.ts";
|
|
2
|
+
import type { AdmissionLine } from "../durable/store-admission.ts";
|
|
3
|
+
import { type ActivityRefusal, type NestedActivity, type NestedAdmission, type NestedCancelTarget, type NestedParent, type NestedRecord, type NestedRequest } from "./nested-types.ts";
|
|
4
|
+
export interface NestedAdmissionOptions {
|
|
5
|
+
/** The brain's issuer: every parent and child here is one of its executions. */
|
|
6
|
+
readonly issuer: string;
|
|
7
|
+
readonly store: DurableStore;
|
|
8
|
+
readonly epoch: number;
|
|
9
|
+
/** The single admission line of the store's writable epoch: the brain is the single writer, so a cancel and a child
|
|
10
|
+
* admission are linearized here. */
|
|
11
|
+
readonly admission: AdmissionLine;
|
|
12
|
+
readonly clock: () => number;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Nested requests admitted on the brain that owns the parent execution (design 5.6.3). Every step is durable before
|
|
16
|
+
* the reply it causes; a replay of the same `(parent, childRequestId)` returns the same child and is never debited twice.
|
|
17
|
+
*/
|
|
18
|
+
export interface NestedAdmissionControl {
|
|
19
|
+
/** Writes the parent's nested set, scoped to preparation, before its prepare is sent. Idempotent. */
|
|
20
|
+
open(parent: NestedParent): Promise<{
|
|
21
|
+
readonly kind: "opened";
|
|
22
|
+
} | {
|
|
23
|
+
readonly kind: "refused";
|
|
24
|
+
readonly reason: "conflicting_parent";
|
|
25
|
+
}>;
|
|
26
|
+
release(parent: string): Promise<void>;
|
|
27
|
+
/** The parent's invocation is terminal: only its live activities still admit. */
|
|
28
|
+
settle(parent: string): Promise<void>;
|
|
29
|
+
/** Follows a parent renewal: the set's expiry moves forward, never past the lease or the parent's lifetime. */
|
|
30
|
+
renew(parent: string, leaseUntil: number): Promise<number>;
|
|
31
|
+
registerActivity(activity: NestedActivity): Promise<{
|
|
32
|
+
readonly kind: "registered";
|
|
33
|
+
} | {
|
|
34
|
+
readonly kind: "refused";
|
|
35
|
+
readonly reason: ActivityRefusal;
|
|
36
|
+
}>;
|
|
37
|
+
endActivity(parent: string, activityId: string): Promise<void>;
|
|
38
|
+
admit(request: NestedRequest): Promise<NestedAdmission>;
|
|
39
|
+
/** Cancels the parent and every child: the cascade is durably queued, then delivered by `deliverCancels`. */
|
|
40
|
+
cancel(parent: string): Promise<readonly NestedCancelTarget[]>;
|
|
41
|
+
/** Sends every queued cascade to its child's executor and marks it delivered; re-sent after a restart until then. */
|
|
42
|
+
deliverCancels(parent: string, send: (target: NestedCancelTarget) => Promise<void>): Promise<number>;
|
|
43
|
+
read(parent: string): Promise<NestedRecord | undefined>;
|
|
44
|
+
}
|
|
45
|
+
/** A child's execution id: encoded like the record keys, so no two (parent, childRequestId) pairs can collide. */
|
|
46
|
+
export declare const nestedChildId: (parent: string, childRequestId: string) => string;
|
|
47
|
+
export declare function createNestedAdmission(options: NestedAdmissionOptions): NestedAdmissionControl;
|
|
48
|
+
//# sourceMappingURL=nested.d.ts.map
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { canonicalJson } from "./canonical-json.js";
|
|
2
|
+
import { decodeNestedRecord } from "./nested-codec.js";
|
|
3
|
+
import { NESTED_LIFETIME_MS, } from "./nested-types.js";
|
|
4
|
+
const PREFIX = "execution.nested:";
|
|
5
|
+
/** A child's execution id: encoded like the record keys, so no two (parent, childRequestId) pairs can collide. */
|
|
6
|
+
export const nestedChildId = (parent, childRequestId) => JSON.stringify([parent, childRequestId]);
|
|
7
|
+
const validAmount = (value) => Number.isFinite(value) && value >= 0;
|
|
8
|
+
function refused(reason) {
|
|
9
|
+
return { kind: "refused", reason };
|
|
10
|
+
}
|
|
11
|
+
export function createNestedAdmission(options) {
|
|
12
|
+
const { issuer, store, epoch, admission, clock } = options;
|
|
13
|
+
const keyOf = (parent) => `${PREFIX}${JSON.stringify([issuer, parent])}`;
|
|
14
|
+
const read = async (parent) => {
|
|
15
|
+
const value = await store.getDocument(keyOf(parent));
|
|
16
|
+
return value === undefined ? undefined : decodeNestedRecord(value);
|
|
17
|
+
};
|
|
18
|
+
const write = async (record, step, executionId) => {
|
|
19
|
+
const data = { parent: record.parent.executionId, step, executionId };
|
|
20
|
+
await store.commit(epoch, [
|
|
21
|
+
{ op: "append", id: store.mintId(), kind: "execution.nested", data },
|
|
22
|
+
{ op: "put", key: keyOf(record.parent.executionId), data: record },
|
|
23
|
+
], () => { });
|
|
24
|
+
};
|
|
25
|
+
/** Reads, decides and writes inside one admission: nothing else on the brain's store interleaves. */
|
|
26
|
+
const update = (parent, decide) => admission.admit(async () => decide(await read(parent)));
|
|
27
|
+
const live = (record) => !record.cancelled && record.phase !== "settled";
|
|
28
|
+
const set = (record) => record.phase === "preparation"
|
|
29
|
+
? record.parent.capabilities.preparation
|
|
30
|
+
: [...record.parent.capabilities.preparation, ...record.parent.capabilities.body];
|
|
31
|
+
return {
|
|
32
|
+
read,
|
|
33
|
+
open: (parent) => update(parent.executionId, async (record) => {
|
|
34
|
+
if (!validAmount(parent.budget) ||
|
|
35
|
+
![parent.generation, parent.admittedAt, parent.leaseUntil].every((value) => Number.isFinite(value)))
|
|
36
|
+
throw new Error(`nested parent ${parent.executionId} has a non-finite number`);
|
|
37
|
+
if (record !== undefined)
|
|
38
|
+
return canonicalJson(record.parent) === canonicalJson(parent)
|
|
39
|
+
? { kind: "opened" }
|
|
40
|
+
: { kind: "refused", reason: "conflicting_parent" };
|
|
41
|
+
const expiresAt = Math.min(parent.leaseUntil, parent.admittedAt + NESTED_LIFETIME_MS);
|
|
42
|
+
const opened = {
|
|
43
|
+
parent,
|
|
44
|
+
phase: "preparation",
|
|
45
|
+
cancelled: false,
|
|
46
|
+
expiresAt,
|
|
47
|
+
debited: 0,
|
|
48
|
+
children: {},
|
|
49
|
+
activities: {},
|
|
50
|
+
cancels: {},
|
|
51
|
+
};
|
|
52
|
+
await write(opened, "open", null);
|
|
53
|
+
return { kind: "opened" };
|
|
54
|
+
}),
|
|
55
|
+
release: (parent) => update(parent, async (record) => {
|
|
56
|
+
if (record === undefined || record.phase !== "preparation" || record.cancelled)
|
|
57
|
+
return;
|
|
58
|
+
await write({ ...record, phase: "body" }, "release", null);
|
|
59
|
+
}),
|
|
60
|
+
settle: (parent) => update(parent, async (record) => {
|
|
61
|
+
if (record === undefined || !live(record))
|
|
62
|
+
return;
|
|
63
|
+
await write({ ...record, phase: "settled" }, "settle", null);
|
|
64
|
+
}),
|
|
65
|
+
renew: (parent, leaseUntil) => update(parent, async (record) => {
|
|
66
|
+
if (record === undefined)
|
|
67
|
+
throw new Error(`no nested set for ${parent}`);
|
|
68
|
+
const cap = record.parent.admittedAt + NESTED_LIFETIME_MS;
|
|
69
|
+
const expiresAt = Math.max(record.expiresAt, Math.min(leaseUntil, cap));
|
|
70
|
+
if (expiresAt !== record.expiresAt)
|
|
71
|
+
await write({ ...record, expiresAt }, "renew", null);
|
|
72
|
+
return expiresAt;
|
|
73
|
+
}),
|
|
74
|
+
registerActivity: (activity) => update(activity.parent, async (record) => {
|
|
75
|
+
if (record === undefined)
|
|
76
|
+
return { kind: "refused", reason: "unknown_parent" };
|
|
77
|
+
const existing = record.activities[activity.activityId];
|
|
78
|
+
if (existing !== undefined)
|
|
79
|
+
return canonicalJson({ ...existing, debited: 0, ended: false }) ===
|
|
80
|
+
canonicalJson({ ...activity, debited: 0, ended: false })
|
|
81
|
+
? { kind: "registered" }
|
|
82
|
+
: { kind: "refused", reason: "conflicting_activity" };
|
|
83
|
+
if (!live(record) || clock() > record.expiresAt)
|
|
84
|
+
return { kind: "refused", reason: "parent_terminal" };
|
|
85
|
+
const held = set(record);
|
|
86
|
+
const subset = activity.executor === record.parent.executor &&
|
|
87
|
+
activity.scope === record.parent.scope &&
|
|
88
|
+
activity.targets.every((target) => held.includes(target)) &&
|
|
89
|
+
activity.expiresAt <= record.expiresAt &&
|
|
90
|
+
validAmount(activity.budget) &&
|
|
91
|
+
activity.budget <= record.parent.budget - record.debited;
|
|
92
|
+
if (!subset)
|
|
93
|
+
return { kind: "refused", reason: "exceeds_parent" };
|
|
94
|
+
const activities = {
|
|
95
|
+
...record.activities,
|
|
96
|
+
[activity.activityId]: { ...activity, debited: 0, ended: false },
|
|
97
|
+
};
|
|
98
|
+
await write({ ...record, activities }, "activity", activity.activityId);
|
|
99
|
+
return { kind: "registered" };
|
|
100
|
+
}),
|
|
101
|
+
endActivity: (parent, activityId) => update(parent, async (record) => {
|
|
102
|
+
const activity = record?.activities[activityId];
|
|
103
|
+
if (record === undefined || activity === undefined || activity.ended)
|
|
104
|
+
return;
|
|
105
|
+
const activities = { ...record.activities, [activityId]: { ...activity, ended: true } };
|
|
106
|
+
await write({ ...record, activities }, "activity_end", activityId);
|
|
107
|
+
}),
|
|
108
|
+
admit: (request) => update(request.parent, async (record) => {
|
|
109
|
+
if (record === undefined)
|
|
110
|
+
return refused("unknown_parent");
|
|
111
|
+
const activity = request.activityId === undefined ? undefined : record.activities[request.activityId];
|
|
112
|
+
if (request.activityId !== undefined && activity === undefined)
|
|
113
|
+
return refused("unknown_activity");
|
|
114
|
+
const owner = activity ?? record.parent;
|
|
115
|
+
if (request.sender !== owner.executor)
|
|
116
|
+
return refused("not_parent_executor");
|
|
117
|
+
if (request.parentGeneration !== record.parent.generation)
|
|
118
|
+
return refused("stale_generation");
|
|
119
|
+
if (request.scope !== owner.scope)
|
|
120
|
+
return refused("scope_mismatch");
|
|
121
|
+
const id = nestedChildId(request.parent, request.childRequestId);
|
|
122
|
+
const ref = { issuer, executionId: id, generation: 1 };
|
|
123
|
+
const known = record.children[request.childRequestId];
|
|
124
|
+
if (known !== undefined)
|
|
125
|
+
return known.target === request.target &&
|
|
126
|
+
known.cost === request.cost &&
|
|
127
|
+
known.activityId === (request.activityId ?? null)
|
|
128
|
+
? { kind: "admitted", child: ref, target: known.target, replayed: true }
|
|
129
|
+
: refused("conflicting_request");
|
|
130
|
+
if (!validAmount(request.cost))
|
|
131
|
+
return refused("invalid_cost");
|
|
132
|
+
const now = clock();
|
|
133
|
+
if (activity !== undefined) {
|
|
134
|
+
if (activity.ended || record.cancelled || now > activity.expiresAt)
|
|
135
|
+
return refused("activity_ended");
|
|
136
|
+
if (!activity.targets.includes(request.target))
|
|
137
|
+
return refused("not_in_nested_set");
|
|
138
|
+
if (activity.debited + request.cost > activity.budget)
|
|
139
|
+
return refused("budget_exhausted");
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
if (!live(record))
|
|
143
|
+
return refused("parent_terminal");
|
|
144
|
+
if (now > record.expiresAt)
|
|
145
|
+
return refused("expired");
|
|
146
|
+
if (!set(record).includes(request.target))
|
|
147
|
+
return refused("not_in_nested_set");
|
|
148
|
+
}
|
|
149
|
+
if (record.debited + request.cost > record.parent.budget)
|
|
150
|
+
return refused("budget_exhausted");
|
|
151
|
+
const child = {
|
|
152
|
+
executionId: id,
|
|
153
|
+
target: request.target,
|
|
154
|
+
cost: request.cost,
|
|
155
|
+
origin: "tool",
|
|
156
|
+
activityId: request.activityId ?? null,
|
|
157
|
+
};
|
|
158
|
+
const activities = activity === undefined
|
|
159
|
+
? record.activities
|
|
160
|
+
: {
|
|
161
|
+
...record.activities,
|
|
162
|
+
[activity.activityId]: { ...activity, debited: activity.debited + request.cost },
|
|
163
|
+
};
|
|
164
|
+
await write({
|
|
165
|
+
...record,
|
|
166
|
+
debited: record.debited + request.cost,
|
|
167
|
+
children: { ...record.children, [request.childRequestId]: child },
|
|
168
|
+
activities,
|
|
169
|
+
}, "admit", id);
|
|
170
|
+
return { kind: "admitted", child: ref, target: request.target, replayed: false };
|
|
171
|
+
}),
|
|
172
|
+
cancel: (parent) => update(parent, async (record) => {
|
|
173
|
+
if (record === undefined)
|
|
174
|
+
return [];
|
|
175
|
+
const targets = Object.values(record.children).map((child) => ({
|
|
176
|
+
ref: { issuer, executionId: child.executionId, generation: 1 },
|
|
177
|
+
executor: child.target,
|
|
178
|
+
}));
|
|
179
|
+
if (record.cancelled)
|
|
180
|
+
return targets;
|
|
181
|
+
const activities = Object.fromEntries(Object.entries(record.activities).map(([id, activity]) => [id, { ...activity, ended: true }]));
|
|
182
|
+
const cancels = Object.fromEntries(Object.values(record.children).map((child) => [child.executionId, "queued"]));
|
|
183
|
+
await write({ ...record, cancelled: true, activities, cancels }, "cancel", null);
|
|
184
|
+
return targets;
|
|
185
|
+
}),
|
|
186
|
+
deliverCancels: async (parent, send) => {
|
|
187
|
+
const record = await read(parent);
|
|
188
|
+
if (record === undefined)
|
|
189
|
+
return 0;
|
|
190
|
+
let delivered = 0;
|
|
191
|
+
for (const child of Object.values(record.children)) {
|
|
192
|
+
if (record.cancels[child.executionId] !== "queued")
|
|
193
|
+
continue;
|
|
194
|
+
await send({ ref: { issuer, executionId: child.executionId, generation: 1 }, executor: child.target });
|
|
195
|
+
await update(parent, async (latest) => {
|
|
196
|
+
if (latest === undefined || latest.cancels[child.executionId] !== "queued")
|
|
197
|
+
return;
|
|
198
|
+
await write({ ...latest, cancels: { ...latest.cancels, [child.executionId]: "delivered" } }, "cancel_delivered", child.executionId);
|
|
199
|
+
});
|
|
200
|
+
delivered += 1;
|
|
201
|
+
}
|
|
202
|
+
return delivered;
|
|
203
|
+
},
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
//# sourceMappingURL=nested.js.map
|