@code-yeongyu/senpi-agent-core 2026.11.3-3 → 2026.11.4-2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent-loop.js +7 -47
- package/dist/conformance/index.d.ts +23 -0
- package/dist/conformance/index.js +17 -0
- package/dist/conformance/loop-core-manifest.d.ts +1 -1
- package/dist/conformance/loop-core-manifest.js +1 -0
- package/dist/conformance/registry.js +9 -1
- package/dist/execution/canonical-json.d.ts +3 -0
- package/dist/execution/canonical-json.js +12 -0
- package/dist/execution/coordinator-codec.js +13 -2
- package/dist/execution/coordinator-consent.d.ts +29 -0
- package/dist/execution/coordinator-consent.js +63 -0
- package/dist/execution/coordinator-sequential.d.ts +15 -0
- package/dist/execution/coordinator-sequential.js +34 -0
- package/dist/execution/coordinator-types.d.ts +45 -0
- package/dist/execution/coordinator.d.ts +3 -1
- package/dist/execution/coordinator.js +35 -3
- package/dist/execution/device-files-node.d.ts +8 -0
- package/dist/execution/device-files-node.js +112 -0
- package/dist/execution/device-files.d.ts +80 -0
- package/dist/execution/device-files.js +87 -0
- package/dist/execution/nested-codec.d.ts +5 -0
- package/dist/execution/nested-codec.js +41 -0
- package/dist/execution/nested-types.d.ts +92 -0
- package/dist/execution/nested-types.js +3 -0
- package/dist/execution/nested.d.ts +48 -0
- package/dist/execution/nested.js +206 -0
- package/dist/execution/plane-model.d.ts +33 -0
- package/dist/execution/plane-model.js +80 -0
- package/dist/execution/plane.d.ts +72 -0
- package/dist/execution/plane.js +172 -0
- package/dist/execution/remote-device-files.d.ts +12 -0
- package/dist/execution/remote-device-files.js +84 -0
- package/dist/execution/remote-executor.d.ts +10 -0
- package/dist/execution/remote-executor.js +49 -15
- package/dist/execution/remote-file-call.d.ts +14 -0
- package/dist/execution/remote-file-call.js +30 -0
- package/dist/execution/remote-link.d.ts +36 -0
- package/dist/execution/remote-link.js +117 -0
- package/dist/execution/remote-protocol.d.ts +21 -4
- package/dist/execution/remote-protocol.js +18 -2
- package/dist/execution/remote-tool-host.d.ts +11 -3
- package/dist/execution/remote-tool-host.js +23 -64
- package/dist/execution/scope-lifecycle.d.ts +81 -0
- package/dist/execution/scope-lifecycle.js +173 -0
- package/dist/execution/testing/batch-executor.js +32 -3
- package/dist/execution/testing/batch-harness.d.ts +25 -3
- package/dist/execution/testing/batch-harness.js +83 -5
- package/dist/execution/testing/cancel-cases.d.ts +8 -0
- package/dist/execution/testing/cancel-cases.js +64 -0
- package/dist/execution/testing/consent-cases.d.ts +14 -0
- package/dist/execution/testing/consent-cases.js +66 -0
- package/dist/execution/testing/kill-nest.d.ts +6 -0
- package/dist/execution/testing/kill-nest.js +55 -0
- package/dist/execution/testing/kill-wakex.d.ts +6 -0
- package/dist/execution/testing/kill-wakex.js +81 -0
- package/dist/execution/testing/lifecycle-cases.d.ts +9 -0
- package/dist/execution/testing/lifecycle-cases.js +60 -0
- package/dist/execution/testing/lifecycle-registry.d.ts +3 -0
- package/dist/execution/testing/lifecycle-registry.js +19 -0
- package/dist/execution/testing/multi-lifecycle-cases.d.ts +4 -0
- package/dist/execution/testing/multi-lifecycle-cases.js +34 -0
- package/dist/execution/testing/multi-seq-cases.d.ts +4 -0
- package/dist/execution/testing/multi-seq-cases.js +46 -0
- package/dist/execution/testing/nest-cases.d.ts +39 -0
- package/dist/execution/testing/nest-cases.js +181 -0
- package/dist/execution/testing/nest-registry.d.ts +3 -0
- package/dist/execution/testing/nest-registry.js +27 -0
- package/dist/execution/testing/scope-cases.d.ts +6 -0
- package/dist/execution/testing/scope-cases.js +49 -0
- package/dist/execution/testing/seq-cases.d.ts +6 -0
- package/dist/execution/testing/seq-cases.js +71 -0
- package/dist/execution/testing/seq-registry.d.ts +3 -0
- package/dist/execution/testing/seq-registry.js +32 -0
- package/dist/execution/testing/wake-exec-cases.d.ts +20 -0
- package/dist/execution/testing/wake-exec-cases.js +147 -0
- package/dist/execution/testing/wake-registry.d.ts +3 -0
- package/dist/execution/testing/wake-registry.js +26 -0
- package/dist/execution/tool-host.d.ts +10 -2
- package/dist/execution/tool-outcome-events.d.ts +11 -0
- package/dist/execution/tool-outcome-events.js +47 -0
- package/dist/execution/wake-inbox.d.ts +25 -0
- package/dist/execution/wake-inbox.js +85 -0
- package/dist/execution/wake-outbox.d.ts +30 -0
- package/dist/execution/wake-outbox.js +87 -0
- package/dist/execution/wake-types.d.ts +45 -0
- package/dist/execution/wake-types.js +3 -0
- package/dist/index.d.ts +13 -2
- package/dist/index.js +11 -1
- package/dist/node.d.ts +1 -0
- package/dist/node.js +1 -0
- package/dist/transport/testing/multi-cases.js +8 -8
- package/package.json +3 -3
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { DurableStore, StoreValue } from "../durable/store.ts";
|
|
2
|
+
import type { AdmissionLine } from "../durable/store-admission.ts";
|
|
3
|
+
/** What an endpoint owns for one scope (design 5.6.6). */
|
|
4
|
+
export type ScopeBinding = {
|
|
5
|
+
readonly issuer: string;
|
|
6
|
+
readonly workspace: string;
|
|
7
|
+
readonly scope: string;
|
|
8
|
+
readonly generation: number;
|
|
9
|
+
readonly allowedRoots: readonly string[];
|
|
10
|
+
};
|
|
11
|
+
export type ScopeRecord = {
|
|
12
|
+
readonly binding: ScopeBinding;
|
|
13
|
+
readonly state: "open" | "closed";
|
|
14
|
+
readonly lastActiveAt: number;
|
|
15
|
+
/** Live resources (executions, terminals, activities) by id: an idle TTL never closes a scope that holds one. */
|
|
16
|
+
readonly resources: {
|
|
17
|
+
readonly [id: string]: string;
|
|
18
|
+
};
|
|
19
|
+
/** Every committed lifecycle event, in admission order, with the effect its handler returned. */
|
|
20
|
+
readonly events: readonly {
|
|
21
|
+
readonly seq: number;
|
|
22
|
+
readonly kind: string;
|
|
23
|
+
readonly effect: StoreValue;
|
|
24
|
+
}[];
|
|
25
|
+
/** Terminal outcomes a forced close wrote, by resource id, before the scope closed. */
|
|
26
|
+
readonly outcomes: {
|
|
27
|
+
readonly [id: string]: StoreValue;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
export type ScopeRefusal = "unknown_scope" | "not_issuer" | "closed" | "live_resources" | "conflicting_scope";
|
|
31
|
+
/** A call for a scope that is unknown or closed: it never runs. */
|
|
32
|
+
export declare class ScopeUnavailableError extends Error {
|
|
33
|
+
readonly scope: string;
|
|
34
|
+
readonly reason: "unknown_scope" | "closed";
|
|
35
|
+
constructor(scope: string, reason: "unknown_scope" | "closed");
|
|
36
|
+
}
|
|
37
|
+
export interface ScopeLifecycleOptions {
|
|
38
|
+
readonly store: DurableStore;
|
|
39
|
+
readonly epoch: number;
|
|
40
|
+
readonly admission: AdmissionLine;
|
|
41
|
+
readonly clock: () => number;
|
|
42
|
+
readonly idleTtlMs: number;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Scope lifecycle on an endpoint (design 5.6.6). A lifecycle event is admitted only after its handler's effect commits,
|
|
46
|
+
* and a call submitted after it runs only then; only the issuer closes; a TTL never closes a scope with live resources;
|
|
47
|
+
* a forced close writes every live resource's terminal outcome before the scope closes.
|
|
48
|
+
*/
|
|
49
|
+
export interface ScopeLifecycle {
|
|
50
|
+
open(binding: ScopeBinding): Promise<{
|
|
51
|
+
readonly kind: "opened";
|
|
52
|
+
} | {
|
|
53
|
+
readonly kind: "refused";
|
|
54
|
+
readonly reason: ScopeRefusal;
|
|
55
|
+
}>;
|
|
56
|
+
lifecycle(actor: string, scope: string, kind: string, handler: () => Promise<StoreValue>): Promise<{
|
|
57
|
+
readonly kind: "committed";
|
|
58
|
+
readonly seq: number;
|
|
59
|
+
} | {
|
|
60
|
+
readonly kind: "refused";
|
|
61
|
+
readonly reason: ScopeRefusal;
|
|
62
|
+
}>;
|
|
63
|
+
/** Runs `run` after every lifecycle event of the scope submitted before it has committed; throws ScopeUnavailableError for a closed or unknown scope. */
|
|
64
|
+
call<T>(scope: string, run: () => Promise<T>): Promise<T>;
|
|
65
|
+
hold(scope: string, resource: string, kind: string): Promise<void>;
|
|
66
|
+
release(scope: string, resource: string): Promise<void>;
|
|
67
|
+
close(actor: string, scope: string, options?: {
|
|
68
|
+
readonly force?: boolean;
|
|
69
|
+
readonly terminate?: (resource: string) => Promise<StoreValue>;
|
|
70
|
+
}): Promise<{
|
|
71
|
+
readonly kind: "closed";
|
|
72
|
+
} | {
|
|
73
|
+
readonly kind: "refused";
|
|
74
|
+
readonly reason: ScopeRefusal;
|
|
75
|
+
}>;
|
|
76
|
+
/** Closes every open scope idle longer than the TTL that holds no live resource; returns their names. */
|
|
77
|
+
sweepIdle(): Promise<readonly string[]>;
|
|
78
|
+
read(scope: string): Promise<ScopeRecord | undefined>;
|
|
79
|
+
}
|
|
80
|
+
export declare function createScopeLifecycle(options: ScopeLifecycleOptions): ScopeLifecycle;
|
|
81
|
+
//# sourceMappingURL=scope-lifecycle.d.ts.map
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { canonicalJson } from "./canonical-json.js";
|
|
2
|
+
/** A call for a scope that is unknown or closed: it never runs. */
|
|
3
|
+
export class ScopeUnavailableError extends Error {
|
|
4
|
+
constructor(scope, reason) {
|
|
5
|
+
super(`scope ${scope} is not open: ${reason}`);
|
|
6
|
+
this.name = "ScopeUnavailableError";
|
|
7
|
+
this.scope = scope;
|
|
8
|
+
this.reason = reason;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
const PREFIX = "execution.scope:";
|
|
12
|
+
const isObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
13
|
+
function decode(value) {
|
|
14
|
+
if (isObject(value) &&
|
|
15
|
+
isObject(value.binding) &&
|
|
16
|
+
(value.state === "open" || value.state === "closed") &&
|
|
17
|
+
typeof value.lastActiveAt === "number" &&
|
|
18
|
+
isObject(value.resources) &&
|
|
19
|
+
Array.isArray(value.events) &&
|
|
20
|
+
isObject(value.outcomes))
|
|
21
|
+
return value;
|
|
22
|
+
throw new Error("corrupt scope record");
|
|
23
|
+
}
|
|
24
|
+
export function createScopeLifecycle(options) {
|
|
25
|
+
const { store, epoch, admission, clock, idleTtlMs } = options;
|
|
26
|
+
const keyOf = (scope) => `${PREFIX}${JSON.stringify([scope])}`;
|
|
27
|
+
const read = async (scope) => {
|
|
28
|
+
const value = await store.getDocument(keyOf(scope));
|
|
29
|
+
return value === undefined ? undefined : decode(value);
|
|
30
|
+
};
|
|
31
|
+
const write = async (record, step, executionId) => {
|
|
32
|
+
await store.commit(epoch, [
|
|
33
|
+
{
|
|
34
|
+
op: "append",
|
|
35
|
+
id: store.mintId(),
|
|
36
|
+
kind: "execution.scope",
|
|
37
|
+
data: { scope: record.binding.scope, step, executionId },
|
|
38
|
+
},
|
|
39
|
+
{ op: "put", key: keyOf(record.binding.scope), data: record },
|
|
40
|
+
], () => { });
|
|
41
|
+
};
|
|
42
|
+
const update = (scope, decide) => admission.admit(async () => decide(await read(scope)));
|
|
43
|
+
/** Per scope, the last admitted lifecycle event or close: a later call, event or close waits for it. */
|
|
44
|
+
const tails = new Map();
|
|
45
|
+
const after = (scope, step) => {
|
|
46
|
+
const run = (tails.get(scope) ?? Promise.resolve()).then(step, step);
|
|
47
|
+
const settled = run.catch(() => undefined);
|
|
48
|
+
tails.set(scope, settled);
|
|
49
|
+
void settled.then(() => {
|
|
50
|
+
if (tails.get(scope) === settled)
|
|
51
|
+
tails.delete(scope);
|
|
52
|
+
});
|
|
53
|
+
return run;
|
|
54
|
+
};
|
|
55
|
+
const touch = (scope) => update(scope, async (record) => {
|
|
56
|
+
if (record?.state !== "open")
|
|
57
|
+
return record?.state;
|
|
58
|
+
await write({ ...record, lastActiveAt: clock() }, "active", null);
|
|
59
|
+
return "open";
|
|
60
|
+
});
|
|
61
|
+
/**
|
|
62
|
+
* Closes from the record as it is inside the admission line, so a resource held after any earlier read is seen: an
|
|
63
|
+
* unforced close with a live resource is refused; a forced one terminates each resource without an outcome, writes
|
|
64
|
+
* the outcome, and looks again until none is left.
|
|
65
|
+
*/
|
|
66
|
+
const finishClose = async (scope, force, terminate) => {
|
|
67
|
+
for (;;) {
|
|
68
|
+
const step = await update(scope, async (latest) => {
|
|
69
|
+
if (latest === undefined)
|
|
70
|
+
return { kind: "refused", reason: "unknown_scope" };
|
|
71
|
+
if (latest.state === "closed")
|
|
72
|
+
return { kind: "closed" };
|
|
73
|
+
const pending = Object.keys(latest.resources).filter((resource) => latest.outcomes[resource] === undefined);
|
|
74
|
+
if (pending.length > 0 && !force)
|
|
75
|
+
return { kind: "refused", reason: "live_resources" };
|
|
76
|
+
if (pending.length > 0)
|
|
77
|
+
return { kind: "terminate", pending };
|
|
78
|
+
await write({ ...latest, state: "closed", resources: {} }, "close", null);
|
|
79
|
+
return { kind: "closed" };
|
|
80
|
+
});
|
|
81
|
+
if (step.kind !== "terminate")
|
|
82
|
+
return step;
|
|
83
|
+
if (terminate === undefined)
|
|
84
|
+
throw new Error(`forced close of ${scope} needs a terminate port`);
|
|
85
|
+
for (const resource of step.pending) {
|
|
86
|
+
const outcome = await terminate(resource);
|
|
87
|
+
await update(scope, async (latest) => {
|
|
88
|
+
if (latest === undefined || latest.state === "closed" || latest.outcomes[resource] !== undefined)
|
|
89
|
+
return;
|
|
90
|
+
await write({ ...latest, outcomes: { ...latest.outcomes, [resource]: outcome } }, "outcome", resource);
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
return {
|
|
96
|
+
read,
|
|
97
|
+
open: (binding) => update(binding.scope, async (record) => {
|
|
98
|
+
if (!Number.isFinite(binding.generation))
|
|
99
|
+
throw new Error(`scope ${binding.scope} has a non-finite generation`);
|
|
100
|
+
if (record !== undefined)
|
|
101
|
+
return canonicalJson(record.binding) === canonicalJson(binding) && record.state === "open"
|
|
102
|
+
? { kind: "opened" }
|
|
103
|
+
: { kind: "refused", reason: "conflicting_scope" };
|
|
104
|
+
await write({ binding, state: "open", lastActiveAt: clock(), resources: {}, events: [], outcomes: {} }, "open", null);
|
|
105
|
+
return { kind: "opened" };
|
|
106
|
+
}),
|
|
107
|
+
lifecycle: (actor, scope, kind, handler) => after(scope, async () => {
|
|
108
|
+
const record = await read(scope);
|
|
109
|
+
if (record === undefined)
|
|
110
|
+
return { kind: "refused", reason: "unknown_scope" };
|
|
111
|
+
if (record.binding.issuer !== actor)
|
|
112
|
+
return { kind: "refused", reason: "not_issuer" };
|
|
113
|
+
if (record.state === "closed")
|
|
114
|
+
return { kind: "refused", reason: "closed" };
|
|
115
|
+
const effect = await handler();
|
|
116
|
+
return update(scope, async (latest) => {
|
|
117
|
+
if (latest === undefined || latest.state === "closed")
|
|
118
|
+
return { kind: "refused", reason: "closed" };
|
|
119
|
+
const seq = latest.events.length + 1;
|
|
120
|
+
await write({ ...latest, lastActiveAt: clock(), events: [...latest.events, { seq, kind, effect }] }, "lifecycle", `${kind}#${seq}`);
|
|
121
|
+
return { kind: "committed", seq };
|
|
122
|
+
});
|
|
123
|
+
}),
|
|
124
|
+
call: (scope, run) => (tails.get(scope) ?? Promise.resolve()).then(async () => {
|
|
125
|
+
const state = await touch(scope);
|
|
126
|
+
if (state !== "open")
|
|
127
|
+
throw new ScopeUnavailableError(scope, state === undefined ? "unknown_scope" : "closed");
|
|
128
|
+
return run();
|
|
129
|
+
}),
|
|
130
|
+
hold: (scope, resource, kind) => update(scope, async (record) => {
|
|
131
|
+
if (record?.state !== "open")
|
|
132
|
+
throw new ScopeUnavailableError(scope, record === undefined ? "unknown_scope" : "closed");
|
|
133
|
+
await write({ ...record, lastActiveAt: clock(), resources: { ...record.resources, [resource]: kind } }, "hold", resource);
|
|
134
|
+
}),
|
|
135
|
+
release: (scope, resource) => update(scope, async (record) => {
|
|
136
|
+
if (record === undefined || record.resources[resource] === undefined)
|
|
137
|
+
return;
|
|
138
|
+
const { [resource]: _released, ...resources } = record.resources;
|
|
139
|
+
await write({ ...record, lastActiveAt: clock(), resources }, "release", resource);
|
|
140
|
+
}),
|
|
141
|
+
close: (actor, scope, closeOptions = {}) => after(scope, async () => {
|
|
142
|
+
const record = await read(scope);
|
|
143
|
+
if (record === undefined)
|
|
144
|
+
return { kind: "refused", reason: "unknown_scope" };
|
|
145
|
+
if (record.binding.issuer !== actor)
|
|
146
|
+
return { kind: "refused", reason: "not_issuer" };
|
|
147
|
+
return finishClose(scope, closeOptions.force === true, closeOptions.terminate);
|
|
148
|
+
}),
|
|
149
|
+
sweepIdle: async () => {
|
|
150
|
+
const closed = [];
|
|
151
|
+
for (const { data } of await store.scanDocuments(PREFIX)) {
|
|
152
|
+
const candidate = decode(data);
|
|
153
|
+
const scope = candidate.binding.scope;
|
|
154
|
+
if (candidate.state !== "open" || Object.keys(candidate.resources).length > 0)
|
|
155
|
+
continue;
|
|
156
|
+
if (clock() - candidate.lastActiveAt <= idleTtlMs)
|
|
157
|
+
continue;
|
|
158
|
+
const done = await after(scope, () => update(scope, async (latest) => {
|
|
159
|
+
if (latest?.state !== "open" || Object.keys(latest.resources).length > 0)
|
|
160
|
+
return false;
|
|
161
|
+
if (clock() - latest.lastActiveAt <= idleTtlMs)
|
|
162
|
+
return false;
|
|
163
|
+
await write({ ...latest, state: "closed" }, "close", null);
|
|
164
|
+
return true;
|
|
165
|
+
}));
|
|
166
|
+
if (done)
|
|
167
|
+
closed.push(scope);
|
|
168
|
+
}
|
|
169
|
+
return closed;
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
//# sourceMappingURL=scope-lifecycle.js.map
|
|
@@ -2,6 +2,12 @@ import { createAdmissionLine } from "../../durable/store-admission.js";
|
|
|
2
2
|
import { createExecutionLedger } from "../ledger.js";
|
|
3
3
|
/** What the scripted `tool_call` chain decides for a tool name; unknown names are allowed. */
|
|
4
4
|
const BATCH_TOOLS = { deny: "blocked", stop: "batch_abort" };
|
|
5
|
+
/**
|
|
6
|
+
* Tools whose body needs consent (design 5.6.1). `gated`'s shim changes the call, so its frozen call always differs
|
|
7
|
+
* from the raw one; `ask`'s shim keeps it, so a Gate A approval of the raw digest also covers the frozen call.
|
|
8
|
+
*/
|
|
9
|
+
const CONSENT_TOOLS = new Set(["gated", "ask"]);
|
|
10
|
+
const frozenDigestOf = (name, requestDigest) => name === "ask" ? requestDigest : `${requestDigest}:frozen`;
|
|
5
11
|
export class ExecutorKilledError extends Error {
|
|
6
12
|
constructor(id) {
|
|
7
13
|
super(`executor ${id} was killed`);
|
|
@@ -55,6 +61,8 @@ export const referenceBatchExecutor = {
|
|
|
55
61
|
}
|
|
56
62
|
if (!("call" in progress))
|
|
57
63
|
throw new Error(`no stored prepare reply in state ${progress.state}`);
|
|
64
|
+
if (progress.state === "prepared_awaiting_consent")
|
|
65
|
+
return { kind: "prepared_awaiting_consent", frozenDigest: progress.call.digest };
|
|
58
66
|
return { kind: "prepared", frozenDigest: progress.call.digest };
|
|
59
67
|
};
|
|
60
68
|
const settled = async (ref) => {
|
|
@@ -92,8 +100,9 @@ export const referenceBatchExecutor = {
|
|
|
92
100
|
const block = { kind: "block", value: { kind: decision, name: call.name } };
|
|
93
101
|
return stored(expectEntry(await apply(request, block, id), "block"));
|
|
94
102
|
}
|
|
95
|
-
const frozen = { ...call, digest:
|
|
96
|
-
const
|
|
103
|
+
const frozen = { ...call, digest: frozenDigestOf(call.name, requestDigest) };
|
|
104
|
+
const awaitingConsent = CONSENT_TOOLS.has(call.name) && request.authorizedDigest !== frozen.digest;
|
|
105
|
+
const freeze = { kind: "freeze", call: frozen, awaitingConsent };
|
|
97
106
|
return stored(expectEntry(await apply(request, freeze, id), "freeze"));
|
|
98
107
|
};
|
|
99
108
|
const release = async (ref) => {
|
|
@@ -122,6 +131,18 @@ export const referenceBatchExecutor = {
|
|
|
122
131
|
throw new Error(`abort refused: ${reply.reason}`);
|
|
123
132
|
return settled(ref);
|
|
124
133
|
};
|
|
134
|
+
const consent = async (ref, frozenDigest) => {
|
|
135
|
+
const entry = await ledger.read(keyOf(ref));
|
|
136
|
+
if (entry === undefined)
|
|
137
|
+
return { kind: "unknown_execution" };
|
|
138
|
+
if ("call" in entry.progress && entry.progress.call.digest !== frozenDigest)
|
|
139
|
+
throw new Error(`consent for ${ref.executionId} names another frozen call`);
|
|
140
|
+
const reply = await apply(ref, { kind: "consent" }, ref.issuer);
|
|
141
|
+
if (reply.status === "refused" && reply.reason !== "terminal" && reply.reason !== "invalid_transition")
|
|
142
|
+
throw new Error(`consent refused: ${reply.reason}`);
|
|
143
|
+
const after = await ledger.read(keyOf(ref));
|
|
144
|
+
return after?.progress.state === "prepared" ? { kind: "prepared" } : settled(ref);
|
|
145
|
+
};
|
|
125
146
|
const once = (run) => {
|
|
126
147
|
const inflight = new Map();
|
|
127
148
|
return (input) => {
|
|
@@ -135,7 +156,15 @@ export const referenceBatchExecutor = {
|
|
|
135
156
|
return started;
|
|
136
157
|
};
|
|
137
158
|
};
|
|
138
|
-
return {
|
|
159
|
+
return {
|
|
160
|
+
prepare: once(prepare),
|
|
161
|
+
release: once(release),
|
|
162
|
+
abort: once(abort),
|
|
163
|
+
consent: (ref, frozenDigest) => {
|
|
164
|
+
alive();
|
|
165
|
+
return consent(ref, frozenDigest);
|
|
166
|
+
},
|
|
167
|
+
};
|
|
139
168
|
},
|
|
140
169
|
};
|
|
141
170
|
//# sourceMappingURL=batch-executor.js.map
|
|
@@ -1,5 +1,10 @@
|
|
|
1
|
+
import type { StoreValue } from "../../durable/store.ts";
|
|
1
2
|
import { type BatchCoordinator } from "../coordinator.ts";
|
|
2
|
-
import
|
|
3
|
+
import { type NestedAdmissionControl } from "../nested.ts";
|
|
4
|
+
import { type ScopeLifecycle } from "../scope-lifecycle.ts";
|
|
5
|
+
import { type WakeInbox } from "../wake-inbox.ts";
|
|
6
|
+
import { type WakeOutbox } from "../wake-outbox.ts";
|
|
7
|
+
import type { BatchConsent, BatchExecutor, BatchPlan, CallOutcome } from "../coordinator-types.ts";
|
|
3
8
|
import type { BatchCaseAdapters, PausePoint } from "./batch-executor.ts";
|
|
4
9
|
/** A coordinator write named by its step and call: `manifest`, `prepare:a`, `decision`, `release:a`, `outcome:a`. */
|
|
5
10
|
export type BrainKill = {
|
|
@@ -24,8 +29,25 @@ export declare function batchHarness(adapters: BatchCaseAdapters): Promise<{
|
|
|
24
29
|
reached: Promise<void>;
|
|
25
30
|
open: () => void;
|
|
26
31
|
};
|
|
27
|
-
/**
|
|
28
|
-
|
|
32
|
+
/**
|
|
33
|
+
* A fresh brain process over the brain's durable medium, optionally dying around one of its writes. Two issuers
|
|
34
|
+
* on one harness are two sessions' brains sharing the executors.
|
|
35
|
+
*/
|
|
36
|
+
brain: (dieAt?: BrainKill, issuer?: string, consent?: BatchConsent) => Promise<BatchCoordinator>;
|
|
37
|
+
/** The brain's nested admission over the same durable medium, optionally dying around one of its writes. */
|
|
38
|
+
nested: (dieAt?: BrainKill, issuer?: string) => Promise<NestedAdmissionControl>;
|
|
39
|
+
/** A hand's durable wake outbox on its own medium (one per hand), optionally dying around one of its writes. */
|
|
40
|
+
wakeOutbox: (hand: string, dieAt?: BrainKill) => Promise<WakeOutbox>;
|
|
41
|
+
/** The endpoint's scope lifecycle on its own medium, optionally dying around one of its writes. */
|
|
42
|
+
scopes: (idleTtlMs: number, dieAt?: BrainKill) => Promise<ScopeLifecycle>;
|
|
43
|
+
/** The `wake_expired` records a hand's outbox logged, in log order. */
|
|
44
|
+
wakeLog: (hand: string) => Promise<readonly StoreValue[]>;
|
|
45
|
+
/** The brain's wake inbox over its medium; `owner` binds each scope to its authenticated hand. */
|
|
46
|
+
wakeInbox: (owner: (scope: string) => string | undefined, dieAt?: BrainKill) => Promise<WakeInbox>;
|
|
47
|
+
/** Moves every executor's clock: a lease that ends before the new time is expired on the next ledger event. */
|
|
48
|
+
advance: (ms: number) => void;
|
|
49
|
+
/** The executor proxy for `id`, as the brain reaches it (a late approval goes through it). */
|
|
50
|
+
executor: (id: string) => BatchExecutor;
|
|
29
51
|
/** Kill the executor process and start a new one on the same durable medium. */
|
|
30
52
|
restartExecutor: (id: string) => Promise<void>;
|
|
31
53
|
/** Kill the executor and start it on a new, empty medium: every execution it held is lost. */
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { createAdmissionLine } from "../../durable/store-admission.js";
|
|
2
2
|
import { createBatchCoordinator } from "../coordinator.js";
|
|
3
|
+
import { createNestedAdmission } from "../nested.js";
|
|
4
|
+
import { createScopeLifecycle } from "../scope-lifecycle.js";
|
|
5
|
+
import { createWakeInbox } from "../wake-inbox.js";
|
|
6
|
+
import { createWakeOutbox } from "../wake-outbox.js";
|
|
3
7
|
const ADMITTED_AT = 100;
|
|
4
8
|
const NOW = ADMITTED_AT + 1;
|
|
5
9
|
export class BrainKilledError extends Error {
|
|
@@ -11,7 +15,7 @@ export class BrainKilledError extends Error {
|
|
|
11
15
|
const isObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
12
16
|
function labelOf(writes) {
|
|
13
17
|
for (const write of writes) {
|
|
14
|
-
if (write.op !== "append" || write.kind
|
|
18
|
+
if (write.op !== "append" || !write.kind.startsWith("execution."))
|
|
15
19
|
continue;
|
|
16
20
|
const data = write.data;
|
|
17
21
|
if (!isObject(data))
|
|
@@ -109,6 +113,7 @@ export async function batchHarness(adapters) {
|
|
|
109
113
|
await (await adapters.store.open(medium)).claim(0, 1);
|
|
110
114
|
return medium;
|
|
111
115
|
};
|
|
116
|
+
let clock = NOW;
|
|
112
117
|
const executors = new Map();
|
|
113
118
|
const start = async (id, medium) => {
|
|
114
119
|
const killed = new AbortController();
|
|
@@ -117,7 +122,7 @@ export async function batchHarness(adapters) {
|
|
|
117
122
|
id,
|
|
118
123
|
store,
|
|
119
124
|
epoch: 1,
|
|
120
|
-
now: () =>
|
|
125
|
+
now: () => clock,
|
|
121
126
|
trace,
|
|
122
127
|
pause,
|
|
123
128
|
killed: killed.signal,
|
|
@@ -134,6 +139,12 @@ export async function batchHarness(adapters) {
|
|
|
134
139
|
prepare: (request) => current(id).prepare(request),
|
|
135
140
|
release: (ref) => current(id).release(ref),
|
|
136
141
|
abort: (ref) => current(id).abort(ref),
|
|
142
|
+
consent: (ref, frozenDigest) => {
|
|
143
|
+
const endpoint = current(id);
|
|
144
|
+
if (endpoint.consent === undefined)
|
|
145
|
+
throw new Error(`executor ${id} takes no consent`);
|
|
146
|
+
return endpoint.consent(ref, frozenDigest);
|
|
147
|
+
},
|
|
137
148
|
});
|
|
138
149
|
const kill = (id) => {
|
|
139
150
|
const found = executors.get(id);
|
|
@@ -145,6 +156,8 @@ export async function batchHarness(adapters) {
|
|
|
145
156
|
for (const id of ["e1", "e2"])
|
|
146
157
|
await start(id, await claimed());
|
|
147
158
|
const brainMedium = await claimed();
|
|
159
|
+
const wakeMedia = new Map();
|
|
160
|
+
let scopeMedium;
|
|
148
161
|
const brainWrites = [];
|
|
149
162
|
const writeWaiters = new Map();
|
|
150
163
|
const onDurable = (label) => {
|
|
@@ -167,17 +180,82 @@ export async function batchHarness(adapters) {
|
|
|
167
180
|
found.open = open;
|
|
168
181
|
return { reached: found.reached.promise, open: () => open.resolve() };
|
|
169
182
|
},
|
|
170
|
-
/**
|
|
171
|
-
|
|
183
|
+
/**
|
|
184
|
+
* A fresh brain process over the brain's durable medium, optionally dying around one of its writes. Two issuers
|
|
185
|
+
* on one harness are two sessions' brains sharing the executors.
|
|
186
|
+
*/
|
|
187
|
+
brain: async (dieAt, issuer = "brain", consent) => {
|
|
172
188
|
const opened = await adapters.store.open(brainMedium);
|
|
173
189
|
return createBatchCoordinator({
|
|
174
|
-
|
|
190
|
+
...(consent === undefined ? {} : { consent }),
|
|
191
|
+
issuer,
|
|
175
192
|
store: brainStore(opened, onDurable, dieAt),
|
|
176
193
|
epoch: 1,
|
|
177
194
|
admission: createAdmissionLine(),
|
|
178
195
|
executor: proxy,
|
|
179
196
|
});
|
|
180
197
|
},
|
|
198
|
+
/** The brain's nested admission over the same durable medium, optionally dying around one of its writes. */
|
|
199
|
+
nested: async (dieAt, issuer = "brain") => {
|
|
200
|
+
const opened = await adapters.store.open(brainMedium);
|
|
201
|
+
return createNestedAdmission({
|
|
202
|
+
issuer,
|
|
203
|
+
store: brainStore(opened, onDurable, dieAt),
|
|
204
|
+
epoch: 1,
|
|
205
|
+
admission: createAdmissionLine(),
|
|
206
|
+
clock: () => clock,
|
|
207
|
+
});
|
|
208
|
+
},
|
|
209
|
+
/** A hand's durable wake outbox on its own medium (one per hand), optionally dying around one of its writes. */
|
|
210
|
+
wakeOutbox: async (hand, dieAt) => {
|
|
211
|
+
let medium = wakeMedia.get(hand);
|
|
212
|
+
if (medium === undefined) {
|
|
213
|
+
medium = await claimed();
|
|
214
|
+
wakeMedia.set(hand, medium);
|
|
215
|
+
}
|
|
216
|
+
return createWakeOutbox({
|
|
217
|
+
hand,
|
|
218
|
+
store: brainStore(await adapters.store.open(medium), () => { }, dieAt),
|
|
219
|
+
epoch: 1,
|
|
220
|
+
admission: createAdmissionLine(),
|
|
221
|
+
clock: () => clock,
|
|
222
|
+
});
|
|
223
|
+
},
|
|
224
|
+
/** The endpoint's scope lifecycle on its own medium, optionally dying around one of its writes. */
|
|
225
|
+
scopes: async (idleTtlMs, dieAt) => {
|
|
226
|
+
if (scopeMedium === undefined)
|
|
227
|
+
scopeMedium = await claimed();
|
|
228
|
+
return createScopeLifecycle({
|
|
229
|
+
store: brainStore(await adapters.store.open(scopeMedium), () => { }, dieAt),
|
|
230
|
+
epoch: 1,
|
|
231
|
+
admission: createAdmissionLine(),
|
|
232
|
+
clock: () => clock,
|
|
233
|
+
idleTtlMs,
|
|
234
|
+
});
|
|
235
|
+
},
|
|
236
|
+
/** The `wake_expired` records a hand's outbox logged, in log order. */
|
|
237
|
+
wakeLog: async (hand) => {
|
|
238
|
+
const medium = wakeMedia.get(hand);
|
|
239
|
+
if (medium === undefined)
|
|
240
|
+
return [];
|
|
241
|
+
const store = await adapters.store.open(medium);
|
|
242
|
+
return (await store.scanRecords({ kind: "execution.wake-expired" })).map((record) => record.data);
|
|
243
|
+
},
|
|
244
|
+
/** The brain's wake inbox over its medium; `owner` binds each scope to its authenticated hand. */
|
|
245
|
+
wakeInbox: async (owner, dieAt) => createWakeInbox({
|
|
246
|
+
issuer: "brain",
|
|
247
|
+
store: brainStore(await adapters.store.open(brainMedium), onDurable, dieAt),
|
|
248
|
+
epoch: 1,
|
|
249
|
+
admission: createAdmissionLine(),
|
|
250
|
+
clock: () => clock,
|
|
251
|
+
owner,
|
|
252
|
+
}),
|
|
253
|
+
/** Moves every executor's clock: a lease that ends before the new time is expired on the next ledger event. */
|
|
254
|
+
advance: (ms) => {
|
|
255
|
+
clock += ms;
|
|
256
|
+
},
|
|
257
|
+
/** The executor proxy for `id`, as the brain reaches it (a late approval goes through it). */
|
|
258
|
+
executor: proxy,
|
|
181
259
|
/** Kill the executor process and start a new one on the same durable medium. */
|
|
182
260
|
restartExecutor: async (id) => start(id, kill(id)),
|
|
183
261
|
/** Kill the executor and start it on a new, empty medium: every execution it held is lost. */
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { CaseImplementation } from "../../conformance/runner.ts";
|
|
2
|
+
import type { NestedCancelTarget } from "../nested-types.ts";
|
|
3
|
+
import type { BatchCaseAdapters } from "./batch-executor.ts";
|
|
4
|
+
type CancelCase = CaseImplementation<BatchCaseAdapters>;
|
|
5
|
+
export declare const cancelTargetOf: (childRequestId: string, executor: string) => NestedCancelTarget;
|
|
6
|
+
export declare const CANCEL_CASES: ReadonlyMap<string, CancelCase>;
|
|
7
|
+
export {};
|
|
8
|
+
//# sourceMappingURL=cancel-cases.d.ts.map
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { expectEqual } from "../../durable/testing/store-harness.js";
|
|
2
|
+
import { nestedChildId } from "../nested.js";
|
|
3
|
+
import { admitted, nestParent, nestRequest, openedParent, refusedWith } from "./nest-cases.js";
|
|
4
|
+
export const cancelTargetOf = (childRequestId, executor) => ({
|
|
5
|
+
ref: { issuer: "brain", executionId: nestedChildId("p", childRequestId), generation: 1 },
|
|
6
|
+
executor,
|
|
7
|
+
});
|
|
8
|
+
/** CANCEL-01: the parent cancel commits before the child's admission: the child is refused, its activity too. */
|
|
9
|
+
const cancelFirst = async (adapters) => {
|
|
10
|
+
const { nested } = await openedParent(adapters);
|
|
11
|
+
await nested.release("p");
|
|
12
|
+
await nested.registerActivity({
|
|
13
|
+
activityId: "a1",
|
|
14
|
+
parent: "p",
|
|
15
|
+
executor: "e1",
|
|
16
|
+
scope: "scope",
|
|
17
|
+
targets: ["model"],
|
|
18
|
+
budget: 2,
|
|
19
|
+
expiresAt: nestParent.leaseUntil,
|
|
20
|
+
});
|
|
21
|
+
expectEqual(await nested.cancel("p"), [], "no child to cascade to");
|
|
22
|
+
expectEqual(await nested.admit(nestRequest()), refusedWith("parent_terminal"), "a child racing the cancel");
|
|
23
|
+
expectEqual(await nested.admit(nestRequest({ activityId: "a1" })), refusedWith("activity_ended"), "the cancel binding ended the activity");
|
|
24
|
+
expectEqual(await nested.registerActivity({
|
|
25
|
+
activityId: "a2",
|
|
26
|
+
parent: "p",
|
|
27
|
+
executor: "e1",
|
|
28
|
+
scope: "scope",
|
|
29
|
+
targets: ["model"],
|
|
30
|
+
budget: 1,
|
|
31
|
+
expiresAt: nestParent.leaseUntil,
|
|
32
|
+
}), refusedWith("parent_terminal"), "no new activity after the cancel");
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* CANCEL-02: the child's admission commits before the parent cancel: the child exists and the cancel durably cascades
|
|
36
|
+
* to its executor, delivered once it is taken and re-sent by a restarted brain until then.
|
|
37
|
+
*/
|
|
38
|
+
const admittedFirst = async (adapters) => {
|
|
39
|
+
const { h, nested } = await openedParent(adapters);
|
|
40
|
+
await nested.release("p");
|
|
41
|
+
expectEqual(await nested.admit(nestRequest({ target: "e2" })), admitted("c1", "e2", false), "admitted before the cancel");
|
|
42
|
+
expectEqual(await nested.cancel("p"), [cancelTargetOf("c1", "e2")], "the cancel names the child");
|
|
43
|
+
expectEqual((await nested.read("p"))?.cancels, { [nestedChildId("p", "c1")]: "queued" }, "the cascade is durable before delivery");
|
|
44
|
+
const refused = [];
|
|
45
|
+
await nested
|
|
46
|
+
.deliverCancels("p", async (target) => {
|
|
47
|
+
refused.push(target);
|
|
48
|
+
throw new Error("the child's executor is unreachable");
|
|
49
|
+
})
|
|
50
|
+
.catch(() => undefined);
|
|
51
|
+
const restarted = await h.nested();
|
|
52
|
+
const sent = [];
|
|
53
|
+
expectEqual(await restarted.deliverCancels("p", async (target) => {
|
|
54
|
+
sent.push(target);
|
|
55
|
+
}), 1, "a restarted brain re-sends it");
|
|
56
|
+
expectEqual([refused, sent], [[cancelTargetOf("c1", "e2")], [cancelTargetOf("c1", "e2")]], "both sends");
|
|
57
|
+
expectEqual(await restarted.deliverCancels("p", async () => { }), 0, "delivered once taken");
|
|
58
|
+
expectEqual(await restarted.admit(nestRequest({ target: "e2" })), admitted("c1", "e2", true), "the admitted child still exists");
|
|
59
|
+
};
|
|
60
|
+
export const CANCEL_CASES = new Map([
|
|
61
|
+
["CANCEL-01", cancelFirst],
|
|
62
|
+
["CANCEL-02", admittedFirst],
|
|
63
|
+
]);
|
|
64
|
+
//# sourceMappingURL=cancel-cases.js.map
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { CaseImplementation } from "../../conformance/runner.ts";
|
|
2
|
+
import type { BatchConsent, ConsentAnswer, ConsentRequest } from "../coordinator-types.ts";
|
|
3
|
+
import type { BatchCaseAdapters } from "./batch-executor.ts";
|
|
4
|
+
type ConsentCase = CaseImplementation<BatchCaseAdapters>;
|
|
5
|
+
/** An approver that hands each request to the case and answers when the case says so. */
|
|
6
|
+
export declare function scriptedConsent(needsConsent?: (name: string) => boolean): {
|
|
7
|
+
port: BatchConsent;
|
|
8
|
+
requests: ConsentRequest[];
|
|
9
|
+
asked: Promise<void>;
|
|
10
|
+
answer: (given: ConsentAnswer) => void;
|
|
11
|
+
};
|
|
12
|
+
export declare const CONSENT_CASES: ReadonlyMap<string, ConsentCase>;
|
|
13
|
+
export {};
|
|
14
|
+
//# sourceMappingURL=consent-cases.d.ts.map
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { expectEqual } from "../../durable/testing/store-harness.js";
|
|
2
|
+
import { batchHarness, batchPlan } from "./batch-harness.js";
|
|
3
|
+
/** A lease that has ended: the harness admits every call with a 60 s lease. */
|
|
4
|
+
const PAST_LEASE_MS = 120_000;
|
|
5
|
+
/** An approver that hands each request to the case and answers when the case says so. */
|
|
6
|
+
export function scriptedConsent(needsConsent = () => false) {
|
|
7
|
+
const requests = [];
|
|
8
|
+
const waiting = [];
|
|
9
|
+
const asked = Promise.withResolvers();
|
|
10
|
+
const port = {
|
|
11
|
+
needsConsent: ({ name }) => needsConsent(name),
|
|
12
|
+
approve: (request) => {
|
|
13
|
+
requests.push(request);
|
|
14
|
+
const answered = Promise.withResolvers();
|
|
15
|
+
waiting.push({ request, answer: answered.resolve });
|
|
16
|
+
asked.resolve();
|
|
17
|
+
return answered.promise;
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
return {
|
|
21
|
+
port,
|
|
22
|
+
requests,
|
|
23
|
+
asked: asked.promise,
|
|
24
|
+
answer: (given) => {
|
|
25
|
+
const next = waiting.shift();
|
|
26
|
+
if (next === undefined)
|
|
27
|
+
throw new Error("no consent request is waiting");
|
|
28
|
+
next.answer(given);
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/** S10's rule: a call settled during preparation ABORTs the rest as an interrupted preparation (replayed as today). */
|
|
33
|
+
const notRun = { kind: "not_run", cause: "interrupted_preparation" };
|
|
34
|
+
async function expiredWhileAwaiting(h, answer, lateApproval) {
|
|
35
|
+
const plan = batchPlan(lateApproval ? "consent-02" : "consent-01", [
|
|
36
|
+
["a", "e1"],
|
|
37
|
+
["g", "e1", "gated"],
|
|
38
|
+
["c", "e2"],
|
|
39
|
+
]);
|
|
40
|
+
const approver = scriptedConsent();
|
|
41
|
+
const run = (await h.brain(undefined, "brain", approver.port)).run(plan);
|
|
42
|
+
await approver.asked;
|
|
43
|
+
h.advance(PAST_LEASE_MS);
|
|
44
|
+
approver.answer(answer);
|
|
45
|
+
const result = await run;
|
|
46
|
+
const expired = { kind: "interrupted", reason: "lease_expired", uncertain: true };
|
|
47
|
+
expectEqual(result, { decision: "ABORT", outcomes: [notRun, expired, notRun] }, "lease_expired, the rest ABORTed");
|
|
48
|
+
expectEqual(approver.requests.map(({ executionId, gate }) => [executionId, gate]), [["g", "frozen"]], "one approval request, bound to the frozen call");
|
|
49
|
+
expectEqual(h.count("body:a") + h.count("body:g") + h.count("body:c"), 0, "no body ran");
|
|
50
|
+
expectEqual(h.count("shim:g"), 1, "no automatic successor: g was never prepared again");
|
|
51
|
+
const frozenDigest = approver.requests[0]?.digest ?? "";
|
|
52
|
+
const late = await h.executor("e1").consent?.({ issuer: "brain", executionId: "g", generation: 1 }, frozenDigest);
|
|
53
|
+
expectEqual(late?.kind === "settled" ? late.outcome.reason : late?.kind, "lease_expired", "a later approval is rejected with the endpoint's own lease_expired outcome");
|
|
54
|
+
expectEqual(await h.ledgerState("e1", "g"), "settled", "the endpoint is terminal");
|
|
55
|
+
expectEqual(await (await h.brain(undefined, "brain", approver.port)).run(plan), result, "recovery decides the same");
|
|
56
|
+
expectEqual(approver.requests.length, 1, "recovery asks nobody again");
|
|
57
|
+
}
|
|
58
|
+
/** CONSENT-01 (S12, cross-machine): the lease expires while g awaits Gate B consent. */
|
|
59
|
+
const expiryWhileAwaiting = async (adapters) => expiredWhileAwaiting(await batchHarness(adapters), "expired", false);
|
|
60
|
+
/** CONSENT-02 (S12, cross-machine): the approval for g arrives after its lease expired, and is rejected. */
|
|
61
|
+
const lateApproval = async (adapters) => expiredWhileAwaiting(await batchHarness(adapters), "approved", true);
|
|
62
|
+
export const CONSENT_CASES = new Map([
|
|
63
|
+
["CONSENT-01", expiryWhileAwaiting],
|
|
64
|
+
["CONSENT-02", lateApproval],
|
|
65
|
+
]);
|
|
66
|
+
//# sourceMappingURL=consent-cases.js.map
|