@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
|
@@ -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
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { AssistantMessage, Context, ThinkingLevel } from "@earendil-works/pi-ai";
|
|
2
|
+
import { type NestedRequest, type PlaneHandler } from "./plane.ts";
|
|
3
|
+
/** The plane a hand's model-backed tools (a completion, a model-backed search) use to reach a model (S31, F40). */
|
|
4
|
+
export declare const MODEL_PLANE = "model";
|
|
5
|
+
/**
|
|
6
|
+
* One completion a hand asks the brain for. It names a model only by provider and ID; the brain resolves it against
|
|
7
|
+
* its own registry and credentials, or uses the session's model when `model` is absent. Nothing a hand sends can choose
|
|
8
|
+
* an endpoint, a key or a header: the codec keeps only these fields.
|
|
9
|
+
*/
|
|
10
|
+
export interface ModelPlaneRequest {
|
|
11
|
+
readonly model?: {
|
|
12
|
+
readonly provider: string;
|
|
13
|
+
readonly id: string;
|
|
14
|
+
};
|
|
15
|
+
readonly context: Context;
|
|
16
|
+
readonly options?: {
|
|
17
|
+
readonly maxTokens?: number;
|
|
18
|
+
readonly temperature?: number;
|
|
19
|
+
readonly reasoning?: ThinkingLevel;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** Parses a request body, keeping only the fields of {@link ModelPlaneRequest}; refuses a malformed one. */
|
|
23
|
+
export declare function decodeModelPlaneRequest(body: unknown): ModelPlaneRequest;
|
|
24
|
+
/**
|
|
25
|
+
* The brain's model plane: `complete` runs one request with the brain's own model registry and credentials (fused and
|
|
26
|
+
* headless: this machine's, as today; a hosted brain's credential source is card C6) and is aborted with the request.
|
|
27
|
+
*/
|
|
28
|
+
export declare function serveModelPlane(complete: (request: ModelPlaneRequest, signal: AbortSignal) => Promise<AssistantMessage>): PlaneHandler;
|
|
29
|
+
/** A hand's client for the model plane, over the running call's nested requests. */
|
|
30
|
+
export declare function createModelPlaneClient(nested: NestedRequest): {
|
|
31
|
+
complete(request: ModelPlaneRequest, signal?: AbortSignal): Promise<AssistantMessage>;
|
|
32
|
+
};
|
|
33
|
+
//# sourceMappingURL=plane-model.d.ts.map
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { PlaneRefusedError } from "./plane.js";
|
|
2
|
+
/** The plane a hand's model-backed tools (a completion, a model-backed search) use to reach a model (S31, F40). */
|
|
3
|
+
export const MODEL_PLANE = "model";
|
|
4
|
+
const REASONING = ["minimal", "low", "medium", "high", "xhigh", "max"];
|
|
5
|
+
const isRecord = (value) => typeof value === "object" && value !== null;
|
|
6
|
+
function refuse(detail) {
|
|
7
|
+
throw new PlaneRefusedError(`malformed_request: ${detail}`);
|
|
8
|
+
}
|
|
9
|
+
function decodeModel(value) {
|
|
10
|
+
if (value === undefined)
|
|
11
|
+
return undefined;
|
|
12
|
+
if (!isRecord(value) || typeof value.provider !== "string" || typeof value.id !== "string")
|
|
13
|
+
refuse("model");
|
|
14
|
+
return { provider: value.provider, id: value.id };
|
|
15
|
+
}
|
|
16
|
+
function decodeContext(value) {
|
|
17
|
+
if (!isRecord(value) || !Array.isArray(value.messages))
|
|
18
|
+
refuse("context");
|
|
19
|
+
if (value.systemPrompt !== undefined && typeof value.systemPrompt !== "string")
|
|
20
|
+
refuse("systemPrompt");
|
|
21
|
+
if (value.tools !== undefined && !Array.isArray(value.tools))
|
|
22
|
+
refuse("tools");
|
|
23
|
+
return {
|
|
24
|
+
...(value.systemPrompt === undefined ? {} : { systemPrompt: value.systemPrompt }),
|
|
25
|
+
messages: value.messages,
|
|
26
|
+
...(value.tools === undefined ? {} : { tools: value.tools }),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function decodeOptions(value) {
|
|
30
|
+
if (value === undefined)
|
|
31
|
+
return undefined;
|
|
32
|
+
if (!isRecord(value))
|
|
33
|
+
refuse("options");
|
|
34
|
+
const { maxTokens, temperature, reasoning } = value;
|
|
35
|
+
if (maxTokens !== undefined && (typeof maxTokens !== "number" || !Number.isSafeInteger(maxTokens) || maxTokens <= 0))
|
|
36
|
+
refuse("maxTokens");
|
|
37
|
+
if (temperature !== undefined && (typeof temperature !== "number" || !Number.isFinite(temperature)))
|
|
38
|
+
refuse("temperature");
|
|
39
|
+
if (reasoning !== undefined && !REASONING.includes(reasoning))
|
|
40
|
+
refuse("reasoning");
|
|
41
|
+
return {
|
|
42
|
+
...(maxTokens === undefined ? {} : { maxTokens: maxTokens }),
|
|
43
|
+
...(temperature === undefined ? {} : { temperature: temperature }),
|
|
44
|
+
...(reasoning === undefined ? {} : { reasoning: reasoning }),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/** Parses a request body, keeping only the fields of {@link ModelPlaneRequest}; refuses a malformed one. */
|
|
48
|
+
export function decodeModelPlaneRequest(body) {
|
|
49
|
+
if (!isRecord(body))
|
|
50
|
+
refuse("body");
|
|
51
|
+
const model = decodeModel(body.model);
|
|
52
|
+
const options = decodeOptions(body.options);
|
|
53
|
+
return {
|
|
54
|
+
...(model === undefined ? {} : { model }),
|
|
55
|
+
context: decodeContext(body.context),
|
|
56
|
+
...(options === undefined ? {} : { options }),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The brain's model plane: `complete` runs one request with the brain's own model registry and credentials (fused and
|
|
61
|
+
* headless: this machine's, as today; a hosted brain's credential source is card C6) and is aborted with the request.
|
|
62
|
+
*/
|
|
63
|
+
export function serveModelPlane(complete) {
|
|
64
|
+
return async (body, signal) => {
|
|
65
|
+
const message = await complete(decodeModelPlaneRequest(body), signal);
|
|
66
|
+
return JSON.parse(JSON.stringify(message));
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/** A hand's client for the model plane, over the running call's nested requests. */
|
|
70
|
+
export function createModelPlaneClient(nested) {
|
|
71
|
+
return {
|
|
72
|
+
async complete(request, signal) {
|
|
73
|
+
const reply = await nested(MODEL_PLANE, JSON.parse(JSON.stringify(request)), signal);
|
|
74
|
+
if (!isRecord(reply) || reply.role !== "assistant" || !Array.isArray(reply.content))
|
|
75
|
+
throw new PlaneRefusedError("malformed_reply");
|
|
76
|
+
return reply;
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=plane-model.js.map
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { AgentToolResult } from "../types.ts";
|
|
2
|
+
import type { AgentLoopConfig } from "../types.ts";
|
|
3
|
+
import { type NestedRequest, type PlaneHandler } from "./plane.ts";
|
|
4
|
+
import { type ToolHost } from "./tool-host.ts";
|
|
5
|
+
/**
|
|
6
|
+
* The session plane (brain/hand split, S32): tools that act on the session (todo, thread tools, task/subagent spawn,
|
|
7
|
+
* `ask_user`, and the other built-ins declared `runs: "brain"`) run where the loop runs. A model call to one of them
|
|
8
|
+
* never reaches the hand, and a hand-run tool that needs one asks the brain that owns its call on this plane.
|
|
9
|
+
*/
|
|
10
|
+
export declare const SESSION_PLANE = "session";
|
|
11
|
+
/** One loop-side tool call a hand-run tool asks the brain for. */
|
|
12
|
+
export interface SessionPlaneRequest {
|
|
13
|
+
readonly tool: string;
|
|
14
|
+
readonly arguments: Readonly<Record<string, unknown>>;
|
|
15
|
+
}
|
|
16
|
+
/** Parses a request body, keeping only the fields of {@link SessionPlaneRequest}; refuses a malformed one. */
|
|
17
|
+
export declare function decodeSessionPlaneRequest(body: unknown): SessionPlaneRequest;
|
|
18
|
+
/** The registered tools a split session runs loop-side: exactly those declared `runs: "brain"`. */
|
|
19
|
+
export declare function loopSideToolNames(tools: Iterable<{
|
|
20
|
+
readonly name: string;
|
|
21
|
+
readonly runs?: "brain" | "hand";
|
|
22
|
+
}>): ReadonlySet<string>;
|
|
23
|
+
/**
|
|
24
|
+
* The brain's session plane: runs one loop-side tool call with the session's own tools and returns its result. A tool
|
|
25
|
+
* that is not loop-side is refused `not_loop_side`: a hand's tools never run on the brain through this plane.
|
|
26
|
+
*/
|
|
27
|
+
export declare function serveSessionPlane(options: {
|
|
28
|
+
readonly loopSide: ReadonlySet<string>;
|
|
29
|
+
readonly run: (request: SessionPlaneRequest, signal: AbortSignal) => Promise<AgentToolResult<unknown>>;
|
|
30
|
+
}): PlaneHandler;
|
|
31
|
+
/** A hand's client for the session plane, over the running call's nested requests. */
|
|
32
|
+
export declare function createSessionPlaneClient(nested: NestedRequest): {
|
|
33
|
+
call(request: SessionPlaneRequest, signal?: AbortSignal): Promise<AgentToolResult<unknown>>;
|
|
34
|
+
};
|
|
35
|
+
type LoopToolSource = Pick<AgentLoopConfig, "resolveUnknownToolCall" | "beforeToolCall" | "afterToolCall" | "removedToolHints" | "afterToolFreeze" | "batchStoreFactory">;
|
|
36
|
+
/**
|
|
37
|
+
* The loop's own pipeline as a ToolHost (the prepared pipeline a fused session runs), for a split session's loop-side
|
|
38
|
+
* calls. It reads `agent`'s hooks and batch store at each batch, so hooks installed after it was made still apply.
|
|
39
|
+
*/
|
|
40
|
+
export declare function createLoopToolHost(agent: LoopToolSource): ToolHost;
|
|
41
|
+
/**
|
|
42
|
+
* The ToolHost of a split session: each call to a loop-side tool runs on `loop` (the session's own pipeline, in its
|
|
43
|
+
* process) and every other call on `hand`. A batch of one side goes to that host whole. A mixed batch keeps the mode:
|
|
44
|
+
* `parallel` runs both sides at once and emits the result messages in source order once both finished, as the fused
|
|
45
|
+
* pipeline does; `sequential` runs each run of same-side calls in source order and starts none after an abort. Results come
|
|
46
|
+
* back in the model's source order and the batch terminates only when every call asked to.
|
|
47
|
+
*/
|
|
48
|
+
export declare function createSplitToolHost(options: {
|
|
49
|
+
readonly loopSide: (toolName: string) => boolean;
|
|
50
|
+
/** The tools declared `executionMode: "sequential"`: in a parallel batch each waits for every earlier call. */
|
|
51
|
+
readonly isSequential: (toolName: string) => boolean;
|
|
52
|
+
readonly loop: ToolHost;
|
|
53
|
+
readonly hand: ToolHost;
|
|
54
|
+
}): ToolHost;
|
|
55
|
+
export {};
|
|
56
|
+
//# sourceMappingURL=plane-session.d.ts.map
|