@code-yeongyu/senpi-agent-core 2026.11.3-2 → 2026.11.3-3
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/conformance/index.d.ts +14 -0
- package/dist/conformance/index.js +8 -0
- package/dist/conformance/registry.js +22 -0
- package/dist/execution/coordinator-codec.d.ts +5 -0
- package/dist/execution/coordinator-codec.js +93 -0
- package/dist/execution/coordinator-plan.d.ts +33 -0
- package/dist/execution/coordinator-plan.js +106 -0
- package/dist/execution/coordinator-types.d.ts +116 -0
- package/dist/execution/coordinator-types.js +2 -0
- package/dist/execution/coordinator.d.ts +25 -0
- package/dist/execution/coordinator.js +132 -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/remote-executor.d.ts +64 -0
- package/dist/execution/remote-executor.js +159 -0
- package/dist/execution/remote-hand-ledger.d.ts +40 -0
- package/dist/execution/remote-hand-ledger.js +137 -0
- package/dist/execution/remote-protocol.d.ts +87 -0
- package/dist/execution/remote-protocol.js +88 -0
- package/dist/execution/remote-tool-host.d.ts +29 -0
- package/dist/execution/remote-tool-host.js +155 -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 +141 -0
- package/dist/execution/testing/batch-harness.d.ts +36 -0
- package/dist/execution/testing/batch-harness.js +208 -0
- package/dist/execution/testing/batch-registry.d.ts +3 -0
- package/dist/execution/testing/batch-registry.js +30 -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/multi-hand-cases.d.ts +6 -0
- package/dist/execution/testing/multi-hand-cases.js +44 -0
- package/dist/harness/env/nodejs.js +4 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +8 -0
- package/package.json +3 -3
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type BatchCoordinator } from "../coordinator.ts";
|
|
2
|
+
import type { BatchPlan, CallOutcome } from "../coordinator-types.ts";
|
|
3
|
+
import type { BatchCaseAdapters, PausePoint } from "./batch-executor.ts";
|
|
4
|
+
/** A coordinator write named by its step and call: `manifest`, `prepare:a`, `decision`, `release:a`, `outcome:a`. */
|
|
5
|
+
export type BrainKill = {
|
|
6
|
+
readonly label: string;
|
|
7
|
+
readonly at: "before" | "after";
|
|
8
|
+
};
|
|
9
|
+
export declare class BrainKilledError extends Error {
|
|
10
|
+
constructor(label: string);
|
|
11
|
+
}
|
|
12
|
+
export type CallSpec = readonly [executionId: string, executor: string, name?: string, sequential?: "sequential"];
|
|
13
|
+
export declare function batchPlan(batchId: string, calls: readonly CallSpec[]): BatchPlan;
|
|
14
|
+
export declare const resultOf: (executionId: string, name?: string) => CallOutcome;
|
|
15
|
+
export type BatchHarness = Awaited<ReturnType<typeof batchHarness>>;
|
|
16
|
+
/** Two executors (`e1`, `e2`) and one brain, each on its own durable medium, with pausable steps and kill controls. */
|
|
17
|
+
export declare function batchHarness(adapters: BatchCaseAdapters): Promise<{
|
|
18
|
+
trace: string[];
|
|
19
|
+
count: (event: string) => number;
|
|
20
|
+
/** Resolves with every durable brain write so far, at the moment the brain's `label` write becomes durable. */
|
|
21
|
+
written: (label: string) => Promise<readonly string[]>;
|
|
22
|
+
/** Hold `executionId` at `point`: `reached` resolves when the step gets there, `open()` lets it continue. */
|
|
23
|
+
hold: (executionId: string, point: PausePoint) => {
|
|
24
|
+
reached: Promise<void>;
|
|
25
|
+
open: () => void;
|
|
26
|
+
};
|
|
27
|
+
/** A fresh brain process over the brain's durable medium, optionally dying around one of its writes. */
|
|
28
|
+
brain: (dieAt?: BrainKill) => Promise<BatchCoordinator>;
|
|
29
|
+
/** Kill the executor process and start a new one on the same durable medium. */
|
|
30
|
+
restartExecutor: (id: string) => Promise<void>;
|
|
31
|
+
/** Kill the executor and start it on a new, empty medium: every execution it held is lost. */
|
|
32
|
+
loseExecutor: (id: string) => Promise<void>;
|
|
33
|
+
ledgerState: (id: string, executionId: string) => Promise<string | undefined>;
|
|
34
|
+
}>;
|
|
35
|
+
export declare function expectRejects(operation: Promise<unknown>, type: new (...args: never[]) => Error, what: string): Promise<void>;
|
|
36
|
+
//# sourceMappingURL=batch-harness.d.ts.map
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { createAdmissionLine } from "../../durable/store-admission.js";
|
|
2
|
+
import { createBatchCoordinator } from "../coordinator.js";
|
|
3
|
+
const ADMITTED_AT = 100;
|
|
4
|
+
const NOW = ADMITTED_AT + 1;
|
|
5
|
+
export class BrainKilledError extends Error {
|
|
6
|
+
constructor(label) {
|
|
7
|
+
super(`brain killed around ${label}`);
|
|
8
|
+
this.name = "BrainKilledError";
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
const isObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
12
|
+
function labelOf(writes) {
|
|
13
|
+
for (const write of writes) {
|
|
14
|
+
if (write.op !== "append" || write.kind !== "execution.batch")
|
|
15
|
+
continue;
|
|
16
|
+
const data = write.data;
|
|
17
|
+
if (!isObject(data))
|
|
18
|
+
return undefined;
|
|
19
|
+
return typeof data.executionId === "string" ? `${String(data.step)}:${data.executionId}` : String(data.step);
|
|
20
|
+
}
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
function withCommit(store, commit) {
|
|
24
|
+
return {
|
|
25
|
+
readEpoch: () => store.readEpoch(),
|
|
26
|
+
readHead: () => store.readHead(),
|
|
27
|
+
claim: (expected, next) => store.claim(expected, next),
|
|
28
|
+
commit,
|
|
29
|
+
mintId: () => store.mintId(),
|
|
30
|
+
scanRecords: (query) => store.scanRecords(query),
|
|
31
|
+
getDocument: (key) => store.getDocument(key),
|
|
32
|
+
scanDocuments: (prefix) => store.scanDocuments(prefix),
|
|
33
|
+
exportSnapshot: () => store.exportSnapshot(),
|
|
34
|
+
importSnapshot: (epoch, snapshot) => store.importSnapshot(epoch, snapshot),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The brain's store: `onDurable` sees the label of every durable write; with `kill`, the brain dies before that write
|
|
39
|
+
* (nothing of it durable) or after it is durable, and never writes again.
|
|
40
|
+
*/
|
|
41
|
+
function brainStore(store, onDurable, kill) {
|
|
42
|
+
let dead = false;
|
|
43
|
+
return withCommit(store, async (epoch, writes, adopt) => {
|
|
44
|
+
const label = labelOf(writes) ?? "";
|
|
45
|
+
if (dead)
|
|
46
|
+
throw new BrainKilledError(label);
|
|
47
|
+
const hit = kill !== undefined && label === kill.label;
|
|
48
|
+
if (hit && kill.at === "before") {
|
|
49
|
+
dead = true;
|
|
50
|
+
throw new BrainKilledError(label);
|
|
51
|
+
}
|
|
52
|
+
const result = await store.commit(epoch, writes, adopt);
|
|
53
|
+
onDurable(label);
|
|
54
|
+
if (hit) {
|
|
55
|
+
dead = true;
|
|
56
|
+
throw new BrainKilledError(label);
|
|
57
|
+
}
|
|
58
|
+
return result;
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
function deadAfter(store, killed) {
|
|
62
|
+
return withCommit(store, (epoch, writes, adopt) => {
|
|
63
|
+
if (killed.aborted)
|
|
64
|
+
return Promise.reject(new Error("a killed executor cannot write"));
|
|
65
|
+
return store.commit(epoch, writes, adopt);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
export function batchPlan(batchId, calls) {
|
|
69
|
+
return {
|
|
70
|
+
batchId,
|
|
71
|
+
scope: "scope",
|
|
72
|
+
workspace: "workspace",
|
|
73
|
+
generation: 1,
|
|
74
|
+
admittedAt: ADMITTED_AT,
|
|
75
|
+
leaseUntil: ADMITTED_AT + 60_000,
|
|
76
|
+
calls: calls.map(([executionId, executor, name = "read", sequential]) => ({
|
|
77
|
+
executionId,
|
|
78
|
+
executor,
|
|
79
|
+
name,
|
|
80
|
+
args: { executionId },
|
|
81
|
+
sequential: sequential === "sequential",
|
|
82
|
+
})),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
export const resultOf = (executionId, name = "read") => ({
|
|
86
|
+
kind: "result",
|
|
87
|
+
value: { executionId, name },
|
|
88
|
+
});
|
|
89
|
+
/** Two executors (`e1`, `e2`) and one brain, each on its own durable medium, with pausable steps and kill controls. */
|
|
90
|
+
export async function batchHarness(adapters) {
|
|
91
|
+
const trace = [];
|
|
92
|
+
const gates = new Map();
|
|
93
|
+
const gate = (key) => {
|
|
94
|
+
let found = gates.get(key);
|
|
95
|
+
if (found === undefined) {
|
|
96
|
+
found = { reached: Promise.withResolvers() };
|
|
97
|
+
gates.set(key, found);
|
|
98
|
+
}
|
|
99
|
+
return found;
|
|
100
|
+
};
|
|
101
|
+
const pause = async (executionId, point) => {
|
|
102
|
+
const found = gate(`${executionId}/${point}`);
|
|
103
|
+
found.reached.resolve();
|
|
104
|
+
if (found.open !== undefined)
|
|
105
|
+
await found.open.promise;
|
|
106
|
+
};
|
|
107
|
+
const claimed = async () => {
|
|
108
|
+
const medium = await adapters.store.createMedium();
|
|
109
|
+
await (await adapters.store.open(medium)).claim(0, 1);
|
|
110
|
+
return medium;
|
|
111
|
+
};
|
|
112
|
+
const executors = new Map();
|
|
113
|
+
const start = async (id, medium) => {
|
|
114
|
+
const killed = new AbortController();
|
|
115
|
+
const store = deadAfter(await adapters.store.open(medium), killed.signal);
|
|
116
|
+
const endpoint = await adapters.executor.open({
|
|
117
|
+
id,
|
|
118
|
+
store,
|
|
119
|
+
epoch: 1,
|
|
120
|
+
now: () => NOW,
|
|
121
|
+
trace,
|
|
122
|
+
pause,
|
|
123
|
+
killed: killed.signal,
|
|
124
|
+
});
|
|
125
|
+
executors.set(id, { medium, killed, endpoint });
|
|
126
|
+
};
|
|
127
|
+
const current = (id) => {
|
|
128
|
+
const found = executors.get(id);
|
|
129
|
+
if (found === undefined)
|
|
130
|
+
throw new Error(`no executor ${id}`);
|
|
131
|
+
return found.endpoint;
|
|
132
|
+
};
|
|
133
|
+
const proxy = (id) => ({
|
|
134
|
+
prepare: (request) => current(id).prepare(request),
|
|
135
|
+
release: (ref) => current(id).release(ref),
|
|
136
|
+
abort: (ref) => current(id).abort(ref),
|
|
137
|
+
});
|
|
138
|
+
const kill = (id) => {
|
|
139
|
+
const found = executors.get(id);
|
|
140
|
+
if (found === undefined)
|
|
141
|
+
throw new Error(`no executor ${id}`);
|
|
142
|
+
found.killed.abort();
|
|
143
|
+
return found.medium;
|
|
144
|
+
};
|
|
145
|
+
for (const id of ["e1", "e2"])
|
|
146
|
+
await start(id, await claimed());
|
|
147
|
+
const brainMedium = await claimed();
|
|
148
|
+
const brainWrites = [];
|
|
149
|
+
const writeWaiters = new Map();
|
|
150
|
+
const onDurable = (label) => {
|
|
151
|
+
brainWrites.push(label);
|
|
152
|
+
writeWaiters.get(label)?.resolve([...brainWrites]);
|
|
153
|
+
};
|
|
154
|
+
return {
|
|
155
|
+
trace,
|
|
156
|
+
count: (event) => trace.filter((entry) => entry === event).length,
|
|
157
|
+
/** Resolves with every durable brain write so far, at the moment the brain's `label` write becomes durable. */
|
|
158
|
+
written: (label) => {
|
|
159
|
+
const waiter = Promise.withResolvers();
|
|
160
|
+
writeWaiters.set(label, waiter);
|
|
161
|
+
return waiter.promise;
|
|
162
|
+
},
|
|
163
|
+
/** Hold `executionId` at `point`: `reached` resolves when the step gets there, `open()` lets it continue. */
|
|
164
|
+
hold: (executionId, point) => {
|
|
165
|
+
const found = gate(`${executionId}/${point}`);
|
|
166
|
+
const open = Promise.withResolvers();
|
|
167
|
+
found.open = open;
|
|
168
|
+
return { reached: found.reached.promise, open: () => open.resolve() };
|
|
169
|
+
},
|
|
170
|
+
/** A fresh brain process over the brain's durable medium, optionally dying around one of its writes. */
|
|
171
|
+
brain: async (dieAt) => {
|
|
172
|
+
const opened = await adapters.store.open(brainMedium);
|
|
173
|
+
return createBatchCoordinator({
|
|
174
|
+
issuer: "brain",
|
|
175
|
+
store: brainStore(opened, onDurable, dieAt),
|
|
176
|
+
epoch: 1,
|
|
177
|
+
admission: createAdmissionLine(),
|
|
178
|
+
executor: proxy,
|
|
179
|
+
});
|
|
180
|
+
},
|
|
181
|
+
/** Kill the executor process and start a new one on the same durable medium. */
|
|
182
|
+
restartExecutor: async (id) => start(id, kill(id)),
|
|
183
|
+
/** Kill the executor and start it on a new, empty medium: every execution it held is lost. */
|
|
184
|
+
loseExecutor: async (id) => {
|
|
185
|
+
kill(id);
|
|
186
|
+
await start(id, await claimed());
|
|
187
|
+
},
|
|
188
|
+
ledgerState: async (id, executionId) => {
|
|
189
|
+
const store = await adapters.store.open(executors.get(id)?.medium ?? "");
|
|
190
|
+
const row = await store.getDocument(`execution.ledger:${JSON.stringify(["brain", executionId])}`);
|
|
191
|
+
if (!isObject(row) || !isObject(row.progress))
|
|
192
|
+
return undefined;
|
|
193
|
+
return String(row.progress.state);
|
|
194
|
+
},
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
export async function expectRejects(operation, type, what) {
|
|
198
|
+
try {
|
|
199
|
+
await operation;
|
|
200
|
+
}
|
|
201
|
+
catch (error) {
|
|
202
|
+
if (error instanceof type)
|
|
203
|
+
return;
|
|
204
|
+
throw new Error(`${what}: expected ${type.name}, got ${String(error)}`);
|
|
205
|
+
}
|
|
206
|
+
throw new Error(`${what}: expected ${type.name}, but it resolved`);
|
|
207
|
+
}
|
|
208
|
+
//# sourceMappingURL=batch-harness.js.map
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { KILL_BATCH_DECLARATIONS } from "./kill-batch.js";
|
|
2
|
+
const BATCH_CASES_IMPL = "packages/agent/src/execution/testing/batch-cases.ts";
|
|
3
|
+
const INTRO = "#5416";
|
|
4
|
+
const batchCase = (id, expected) => ({
|
|
5
|
+
id,
|
|
6
|
+
impl: BATCH_CASES_IMPL,
|
|
7
|
+
intro: INTRO,
|
|
8
|
+
adapters: ["store", "executor"],
|
|
9
|
+
expected,
|
|
10
|
+
});
|
|
11
|
+
export const BATCH_REGISTRY = [
|
|
12
|
+
batchCase("BATCH-01", "the brain crashes after the manifest: the same execution ids are re-prepared; nothing had run"),
|
|
13
|
+
batchCase("BATCH-02", "the brain crashes after the prepares: prepare is re-sent, stored replies return, hooks never rerun"),
|
|
14
|
+
batchCase("BATCH-03", "the brain crashes inside a released wave: released calls get a re-sent release, unmarked ready calls are released, the next wave waits"),
|
|
15
|
+
batchCase("BATCH-05", "an executor restart keeps prepared and released calls, and release proceeds"),
|
|
16
|
+
batchCase("BATCH-06", "a lost prepared execution answers unknown_execution and is uncertain; siblings settle"),
|
|
17
|
+
batchCase("BATCH-07", "prepare runs in source order across executors; each release waits for its dependencies' final outcomes"),
|
|
18
|
+
batchCase("BATCH-08", "a batch_abort reply ABORTs the batch: no later prepare, no body, abort re-sent on recovery"),
|
|
19
|
+
batchCase("BATCH-09", "a blocked call is a per-call result; the other calls are released"),
|
|
20
|
+
batchCase("REPLAY-01", "an executor restart during preparation replays like today (X12): placeholder result, never rerun, a reissued call runs once"),
|
|
21
|
+
batchCase("REPLAY-02", "the same beside a prepared sibling: the sibling is aborted unrun, both replay with the placeholder"),
|
|
22
|
+
...KILL_BATCH_DECLARATIONS.map(({ id, scenario, kill }) => ({
|
|
23
|
+
id,
|
|
24
|
+
impl: "packages/agent/src/execution/testing/kill-batch.ts",
|
|
25
|
+
intro: INTRO,
|
|
26
|
+
adapters: ["store", "executor"],
|
|
27
|
+
expected: `everywhere: kill the brain ${kill.at} its ${kill.label} write (${scenario}); recovery reaches the uncrashed outcomes with stable ids, each step run once`,
|
|
28
|
+
})),
|
|
29
|
+
];
|
|
30
|
+
//# sourceMappingURL=batch-registry.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { CaseImplementation } from "../../conformance/runner.ts";
|
|
2
|
+
import { type HandCaseAdapters } from "./hand-harness.ts";
|
|
3
|
+
type HandCase = CaseImplementation<HandCaseAdapters>;
|
|
4
|
+
export declare const FAULT_HAND_CASES: ReadonlyMap<string, HandCase>;
|
|
5
|
+
export {};
|
|
6
|
+
//# sourceMappingURL=fault-hand-cases.d.ts.map
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { expectEqual } from "../../durable/testing/store-harness.js";
|
|
2
|
+
import { createCarrierFault, FAULT_PROFILE_NAMES, } from "../../transport/testing/fault-transport.js";
|
|
3
|
+
import { call, openHandRig } from "./hand-harness.js";
|
|
4
|
+
const SCOPES = ["a", "b"];
|
|
5
|
+
const CALLS = 12;
|
|
6
|
+
/** The sends span 360 ms, past the flap profile's second drop (260 ms), so every profile acts while calls are live. */
|
|
7
|
+
const PACE_MS = 30;
|
|
8
|
+
const ids = (scope) => Array.from({ length: CALLS }, (_, index) => `${scope}${index + 1}`);
|
|
9
|
+
const texts = (messages) => messages.map((message) => message.content.map((part) => (part.type === "text" ? part.text : "")).join(""));
|
|
10
|
+
const acted = {
|
|
11
|
+
slow: (stats) => stats.refused > 0,
|
|
12
|
+
lossy: (stats) => stats.lost > 0,
|
|
13
|
+
laggy: (stats) => stats.reordered > 0,
|
|
14
|
+
flap: (stats) => stats.drops >= 2,
|
|
15
|
+
"drop-reattach": (stats) => stats.drops >= 2,
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Calls of two scopes sent one by one across the faulted link: every call settles with exactly the result an unfaulted
|
|
19
|
+
* run gives, each body runs once, and each scope's calls start in their lane order.
|
|
20
|
+
*/
|
|
21
|
+
const underProfile = (name) => async (adapters) => {
|
|
22
|
+
const stats = { refused: 0, lost: 0, reordered: 0, drops: 0 };
|
|
23
|
+
const rig = await openHandRig(adapters, { placement: "cross-machine", fault: createCarrierFault(name, stats) });
|
|
24
|
+
const what = `FAULT-HAND ${name}`;
|
|
25
|
+
const batches = [];
|
|
26
|
+
for (let index = 0; index < CALLS; index++) {
|
|
27
|
+
for (const scope of SCOPES)
|
|
28
|
+
batches.push(rig.execute(scope, [call(ids(scope)[index] ?? "")], "sequential"));
|
|
29
|
+
await rig.advance(PACE_MS);
|
|
30
|
+
}
|
|
31
|
+
const results = await rig.settle(Promise.all(batches), `${what}: every call settled`);
|
|
32
|
+
expectEqual(results.flatMap(({ messages }) => texts(messages)).sort(), SCOPES.flatMap((scope) => ids(scope).map((id) => `ran:${id}`)).sort(), `${what}: results equal the unfaulted run`);
|
|
33
|
+
for (const scope of SCOPES)
|
|
34
|
+
expectEqual(rig.bodies.filter((body) => body.startsWith(`${scope}:`)), ids(scope).map((id) => `${scope}:${id}`), `${what}: scope ${scope} ran each call once, in lane order`);
|
|
35
|
+
expectEqual(rig.faults, [], `${what}: the hand reported no fault`);
|
|
36
|
+
if (!acted[name](stats))
|
|
37
|
+
throw new Error(`${what}: the profile never acted (${JSON.stringify(stats)})`);
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* The hand becomes unreachable for far longer than the transport's resume window while several sessions' calls run on
|
|
41
|
+
* it: every dependent call pauses (no outcome is invented and nothing reruns), and after the hand is reachable again each
|
|
42
|
+
* scope's call settles exactly once with its own result.
|
|
43
|
+
*/
|
|
44
|
+
const unreachableThenBack = async (adapters) => {
|
|
45
|
+
const rig = await openHandRig(adapters, { placement: "cross-machine" });
|
|
46
|
+
rig.gateBodies();
|
|
47
|
+
const settledScopes = new Set();
|
|
48
|
+
const batches = SCOPES.map((scope) => {
|
|
49
|
+
const batch = rig.execute(scope, [call(`${scope}1`)]);
|
|
50
|
+
void batch.then(() => settledScopes.add(scope));
|
|
51
|
+
return batch;
|
|
52
|
+
});
|
|
53
|
+
await rig.runUntil(() => rig.bodies.length === SCOPES.length, "FAULT-HAND-MULTI-01: every scope's call is running");
|
|
54
|
+
rig.disconnect();
|
|
55
|
+
for (const scope of SCOPES)
|
|
56
|
+
rig.release(`${scope}1`);
|
|
57
|
+
await rig.advance(60_000);
|
|
58
|
+
expectEqual([...settledScopes], [], "FAULT-HAND-MULTI-01: dependent calls pause while the hand is unreachable");
|
|
59
|
+
rig.reconnect();
|
|
60
|
+
const results = await rig.settle(Promise.all(batches), "FAULT-HAND-MULTI-01: every scope resumed after reconnect");
|
|
61
|
+
expectEqual(results.map(({ messages }) => texts(messages)), SCOPES.map((scope) => [`ran:${scope}1`]), "FAULT-HAND-MULTI-01: each scope settled with its own result");
|
|
62
|
+
await rig.advance(5_000);
|
|
63
|
+
expectEqual(rig.bodies, SCOPES.map((scope) => `${scope}:${scope}1`), "FAULT-HAND-MULTI-01: resumed once: no body ran twice");
|
|
64
|
+
expectEqual(rig.faults, [], "FAULT-HAND-MULTI-01: the hand reported no fault");
|
|
65
|
+
};
|
|
66
|
+
export const FAULT_HAND_CASES = new Map([
|
|
67
|
+
...FAULT_PROFILE_NAMES.map((name, index) => [`FAULT-HAND-0${index + 1}`, underProfile(name)]),
|
|
68
|
+
["FAULT-HAND-MULTI-01", unreachableThenBack],
|
|
69
|
+
]);
|
|
70
|
+
//# sourceMappingURL=fault-hand-cases.js.map
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { AgentToolCall, ToolExecutionMode } from "../../types.ts";
|
|
2
|
+
import { type ManualTimers } from "../../durable/testing/manual-timers.ts";
|
|
3
|
+
import type { StoreCaseAdapters } from "../../durable/testing/store-harness.ts";
|
|
4
|
+
import type { CarrierFault, TransportCaseAdapters } from "../../transport/testing/transport-harness.ts";
|
|
5
|
+
import type { ExecutionEntry } from "../ledger-types.ts";
|
|
6
|
+
import { type HandPlacement } from "../remote-tool-host.ts";
|
|
7
|
+
import type { ToolHostResult } from "../tool-host.ts";
|
|
8
|
+
/** What the KILL-HAND, MULTI-HAND and FAULT-HAND cases need from a host: a durable store and a transport. */
|
|
9
|
+
export type HandCaseAdapters = StoreCaseAdapters & TransportCaseAdapters;
|
|
10
|
+
/** Kill the hand at the `commit`-th ledger commit (0-based), before it or once it is durable. */
|
|
11
|
+
export interface KillPoint {
|
|
12
|
+
readonly commit: number;
|
|
13
|
+
readonly at: "before" | "durable";
|
|
14
|
+
}
|
|
15
|
+
export interface HandRig {
|
|
16
|
+
/** Every tool body start, as `<scope>:<toolCallId>`, across all hand processes. */
|
|
17
|
+
readonly bodies: readonly string[];
|
|
18
|
+
/** Faults the hand processes reported (a killed hand reports the kill). */
|
|
19
|
+
readonly faults: readonly unknown[];
|
|
20
|
+
readonly killed: Promise<void>;
|
|
21
|
+
execute(scope: string, calls: readonly AgentToolCall[], mode?: ToolExecutionMode): Promise<ToolHostResult>;
|
|
22
|
+
/** A new hand process on the reopened store and the same link; resolves once it announced itself. */
|
|
23
|
+
restart(): Promise<void>;
|
|
24
|
+
/** Hold every body until `release(toolCallId)`. */
|
|
25
|
+
gateBodies(): void;
|
|
26
|
+
release(toolCallId: string): void;
|
|
27
|
+
/**
|
|
28
|
+
* Lets `ms` of transport time pass in small steps, running the work each due timer starts before the next step, so a
|
|
29
|
+
* check made afterwards sees everything that time could have produced. Cases wait only through this, `settle` and
|
|
30
|
+
* `runUntil`: a bare timer advance fires timers without letting the hand's settle chain run, and an assertion that
|
|
31
|
+
* nothing happened would then pass whether or not the case's precondition held.
|
|
32
|
+
*/
|
|
33
|
+
advance(ms: number): Promise<void>;
|
|
34
|
+
/** Advances transport time until `promise` settles; fails past the budget. */
|
|
35
|
+
settle<T>(promise: Promise<T>, what: string, budgetMs?: number): Promise<T>;
|
|
36
|
+
runUntil(done: () => boolean, what: string, budgetMs?: number): Promise<void>;
|
|
37
|
+
/** Drops the carrier between session and hand (the hand becomes unreachable) and attaches it again. */
|
|
38
|
+
disconnect(): void;
|
|
39
|
+
reconnect(): void;
|
|
40
|
+
rows(): Promise<ExecutionEntry[]>;
|
|
41
|
+
}
|
|
42
|
+
export declare function call(id: string): AgentToolCall;
|
|
43
|
+
export declare function openHandRig(adapters: HandCaseAdapters, options: {
|
|
44
|
+
readonly placement: HandPlacement;
|
|
45
|
+
readonly killAt?: KillPoint;
|
|
46
|
+
readonly fault?: (timers: ManualTimers) => CarrierFault;
|
|
47
|
+
}): Promise<HandRig>;
|
|
48
|
+
//# sourceMappingURL=hand-harness.d.ts.map
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { createAdmissionLine } from "../../durable/store-admission.js";
|
|
2
|
+
import { createManualTimers } from "../../durable/testing/manual-timers.js";
|
|
3
|
+
import { claimedStore } from "../../durable/testing/store-cases.js";
|
|
4
|
+
import { createInProcessNetwork } from "../../transport/in-process-carrier.js";
|
|
5
|
+
import { localEndpoint } from "../../transport/local-endpoint.js";
|
|
6
|
+
import { createExecutionLedger } from "../ledger.js";
|
|
7
|
+
import { createRemoteExecutor } from "../remote-executor.js";
|
|
8
|
+
import { createHandLedger } from "../remote-hand-ledger.js";
|
|
9
|
+
import { createRemoteToolHost } from "../remote-tool-host.js";
|
|
10
|
+
const TICK_MS = 5;
|
|
11
|
+
const LEASE_MS = 3_600_000;
|
|
12
|
+
const drain = async () => {
|
|
13
|
+
for (let turn = 0; turn < 32; turn++)
|
|
14
|
+
await Promise.resolve();
|
|
15
|
+
};
|
|
16
|
+
export function call(id) {
|
|
17
|
+
return { type: "toolCall", id, name: "probe", arguments: { id } };
|
|
18
|
+
}
|
|
19
|
+
export async function openHandRig(adapters, options) {
|
|
20
|
+
const timers = createManualTimers(1_000);
|
|
21
|
+
const network = createInProcessNetwork({ schedule: (deliver) => void timers.setTimer(deliver, 0) });
|
|
22
|
+
const sessionSide = adapters.transport.createSide(timers);
|
|
23
|
+
const handSide = adapters.transport.createSide(timers);
|
|
24
|
+
const installed = options.fault?.(timers);
|
|
25
|
+
const outer = (side) => (installed === undefined ? side : installed.wrap(side));
|
|
26
|
+
const endpoint = localEndpoint(adapters.platform, "hand-conformance", "/run/senpi-conformance");
|
|
27
|
+
network.listen(endpoint, outer(handSide));
|
|
28
|
+
const link = network.dial(endpoint, outer(sessionSide));
|
|
29
|
+
link.connect();
|
|
30
|
+
installed?.start(link);
|
|
31
|
+
const { medium } = await claimedStore(adapters.store);
|
|
32
|
+
const bodies = [];
|
|
33
|
+
const faults = [];
|
|
34
|
+
const gates = new Map();
|
|
35
|
+
let gated = false;
|
|
36
|
+
const killed = Promise.withResolvers();
|
|
37
|
+
const scopeRunner = (scopeId) => ({
|
|
38
|
+
run: async (handCall) => {
|
|
39
|
+
bodies.push(`${scopeId}:${handCall.toolCallId}`);
|
|
40
|
+
if (gated) {
|
|
41
|
+
const gate = gates.get(handCall.toolCallId) ?? Promise.withResolvers();
|
|
42
|
+
gates.set(handCall.toolCallId, gate);
|
|
43
|
+
await gate.promise;
|
|
44
|
+
}
|
|
45
|
+
return { content: [{ type: "text", text: `ran:${handCall.toolCallId}` }], details: {} };
|
|
46
|
+
},
|
|
47
|
+
isSequential: () => false,
|
|
48
|
+
dispose: async () => { },
|
|
49
|
+
});
|
|
50
|
+
const startHand = (store) => {
|
|
51
|
+
const ledger = createExecutionLedger({ store, epoch: 1, admission: createAdmissionLine() });
|
|
52
|
+
return createRemoteExecutor({
|
|
53
|
+
authority: { kind: "self" },
|
|
54
|
+
transport: handSide,
|
|
55
|
+
openScope: async (scope) => scopeRunner(scope.id),
|
|
56
|
+
ledger: createHandLedger({
|
|
57
|
+
ledger,
|
|
58
|
+
executor: "hand",
|
|
59
|
+
issuer: "session",
|
|
60
|
+
workspace: "workspace",
|
|
61
|
+
now: timers.now,
|
|
62
|
+
leaseMs: LEASE_MS,
|
|
63
|
+
}),
|
|
64
|
+
onFault: (error) => faults.push(error),
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
let commit = -1;
|
|
68
|
+
let dead = false;
|
|
69
|
+
const killing = (point) => {
|
|
70
|
+
if (dead)
|
|
71
|
+
throw new Error("the hand was killed");
|
|
72
|
+
if (point.op !== "commit")
|
|
73
|
+
return;
|
|
74
|
+
if (point.at === "before")
|
|
75
|
+
commit += 1;
|
|
76
|
+
const kill = options.killAt;
|
|
77
|
+
if (kill !== undefined && kill.commit === commit && kill.at === point.at) {
|
|
78
|
+
dead = true;
|
|
79
|
+
killed.resolve();
|
|
80
|
+
throw new Error(`hand killed ${point.at} commit ${commit}`);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
let current = await adapters.store.open(medium, { onWritePoint: killing });
|
|
84
|
+
await startHand(current).ready;
|
|
85
|
+
const host = createRemoteToolHost({ transport: sessionSide, placement: options.placement });
|
|
86
|
+
const pump = async (stop, ms) => {
|
|
87
|
+
const end = timers.now() + ms;
|
|
88
|
+
await drain();
|
|
89
|
+
while (!stop() && timers.now() < end) {
|
|
90
|
+
await timers.advanceTo(Math.min(end, timers.now() + TICK_MS));
|
|
91
|
+
await drain();
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
const runUntil = async (done, what, budgetMs = 600_000) => {
|
|
95
|
+
await pump(done, budgetMs);
|
|
96
|
+
if (!done())
|
|
97
|
+
throw new Error(`${what}: not reached within ${budgetMs} ms of transport time`);
|
|
98
|
+
};
|
|
99
|
+
const settle = async (promise, what, budgetMs = 600_000) => {
|
|
100
|
+
let outcome;
|
|
101
|
+
promise.then((value) => void (outcome = { value }), (error) => void (outcome = { error }));
|
|
102
|
+
await runUntil(() => outcome !== undefined, what, budgetMs);
|
|
103
|
+
if (outcome === undefined || "error" in outcome)
|
|
104
|
+
throw outcome?.error;
|
|
105
|
+
return outcome.value;
|
|
106
|
+
};
|
|
107
|
+
return {
|
|
108
|
+
advance: (ms) => pump(() => false, ms),
|
|
109
|
+
bodies,
|
|
110
|
+
faults,
|
|
111
|
+
killed: killed.promise,
|
|
112
|
+
execute: (scope, calls, mode = "parallel") => host.execute({ scope: { id: scope, root: `/work/${scope}` }, calls, mode }, { emit: () => { } }),
|
|
113
|
+
async restart() {
|
|
114
|
+
current = await adapters.store.open(medium);
|
|
115
|
+
await settle(startHand(current).ready, "the restarted hand recovered");
|
|
116
|
+
},
|
|
117
|
+
gateBodies: () => {
|
|
118
|
+
gated = true;
|
|
119
|
+
},
|
|
120
|
+
release(toolCallId) {
|
|
121
|
+
const gate = gates.get(toolCallId) ?? Promise.withResolvers();
|
|
122
|
+
gates.set(toolCallId, gate);
|
|
123
|
+
gate.resolve();
|
|
124
|
+
},
|
|
125
|
+
settle,
|
|
126
|
+
runUntil,
|
|
127
|
+
disconnect: () => link.drop(),
|
|
128
|
+
reconnect: () => link.connect(),
|
|
129
|
+
rows: async () => createExecutionLedger({
|
|
130
|
+
store: await adapters.store.open(medium),
|
|
131
|
+
epoch: 1,
|
|
132
|
+
admission: createAdmissionLine(),
|
|
133
|
+
}).scan(),
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
//# sourceMappingURL=hand-harness.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { CaseImplementation } from "../../conformance/runner.ts";
|
|
2
|
+
import type { BatchCaseAdapters } from "./batch-executor.ts";
|
|
3
|
+
import { type BrainKill } from "./batch-harness.ts";
|
|
4
|
+
export interface KillBatchDeclaration {
|
|
5
|
+
readonly id: string;
|
|
6
|
+
readonly scenario: string;
|
|
7
|
+
readonly kill: BrainKill;
|
|
8
|
+
readonly placement: "everywhere";
|
|
9
|
+
}
|
|
10
|
+
export declare const KILL_BATCH_DECLARATIONS: readonly KillBatchDeclaration[];
|
|
11
|
+
export declare const KILL_BATCH_CASES: ReadonlyMap<string, CaseImplementation<BatchCaseAdapters>>;
|
|
12
|
+
//# sourceMappingURL=kill-batch.d.ts.map
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { expectEqual } from "../../durable/testing/store-harness.js";
|
|
2
|
+
import { BrainKilledError, batchHarness, batchPlan, expectRejects, resultOf, } from "./batch-harness.js";
|
|
3
|
+
const notRun = { kind: "not_run", cause: "batch_abort" };
|
|
4
|
+
const SCENARIOS = [
|
|
5
|
+
{
|
|
6
|
+
name: "a released wave then a sequential call",
|
|
7
|
+
calls: [
|
|
8
|
+
["a", "e1"],
|
|
9
|
+
["b", "e2"],
|
|
10
|
+
["c", "e1", "read", "sequential"],
|
|
11
|
+
],
|
|
12
|
+
writes: ["manifest", "prepare:a", "prepare:b", "prepare:c", "decision", "release:a", "release:b"].concat([
|
|
13
|
+
"outcome:a",
|
|
14
|
+
"outcome:b",
|
|
15
|
+
"release:c",
|
|
16
|
+
"outcome:c",
|
|
17
|
+
]),
|
|
18
|
+
expected: { decision: "RELEASE", outcomes: [resultOf("a"), resultOf("b"), resultOf("c")] },
|
|
19
|
+
prepared: ["a", "b", "c"],
|
|
20
|
+
ran: ["a", "b", "c"],
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
name: "a batch_abort reply",
|
|
24
|
+
calls: [
|
|
25
|
+
["a", "e1"],
|
|
26
|
+
["b", "e2", "stop"],
|
|
27
|
+
["c", "e1"],
|
|
28
|
+
],
|
|
29
|
+
writes: ["manifest", "prepare:a", "prepare:b", "decision", "outcome:a", "outcome:b", "outcome:c"],
|
|
30
|
+
expected: { decision: "ABORT", outcomes: [notRun, notRun, notRun] },
|
|
31
|
+
prepared: ["a", "b"],
|
|
32
|
+
ran: [],
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
name: "a blocked call beside a released call (the executor's blocked reply must be durable)",
|
|
36
|
+
calls: [
|
|
37
|
+
["a", "e1", "deny"],
|
|
38
|
+
["b", "e2"],
|
|
39
|
+
],
|
|
40
|
+
writes: ["manifest", "prepare:a", "prepare:b", "decision", "outcome:a", "release:b", "outcome:b"],
|
|
41
|
+
expected: { decision: "RELEASE", outcomes: [{ kind: "blocked", result: "blocked: deny" }, resultOf("b")] },
|
|
42
|
+
prepared: ["a", "b"],
|
|
43
|
+
ran: ["b"],
|
|
44
|
+
},
|
|
45
|
+
];
|
|
46
|
+
const AT = ["before", "after"];
|
|
47
|
+
const cases = SCENARIOS.flatMap((scenario, scenarioIndex) => scenario.writes.flatMap((label, writeIndex) => AT.map((at, atIndex) => {
|
|
48
|
+
const number = SCENARIOS.slice(0, scenarioIndex).reduce((sum, s) => sum + s.writes.length, 0) + writeIndex + 1;
|
|
49
|
+
const id = `KILL-BATCH-${String(number).padStart(2, "0")}-${atIndex + 1}`;
|
|
50
|
+
const kill = { label, at };
|
|
51
|
+
const run = async (adapters) => {
|
|
52
|
+
const h = await batchHarness(adapters);
|
|
53
|
+
const plan = batchPlan(id.toLowerCase(), scenario.calls);
|
|
54
|
+
await expectRejects((await h.brain(kill)).run(plan), BrainKilledError, `${at} ${label}`);
|
|
55
|
+
expectEqual(await (await h.brain()).run(plan), scenario.expected, "recovery reaches the uncrashed outcomes");
|
|
56
|
+
for (const [executionId, executor] of scenario.calls) {
|
|
57
|
+
const preparedOnce = scenario.prepared.includes(executionId) ? 1 : 0;
|
|
58
|
+
for (const step of ["shim", "hook"])
|
|
59
|
+
expectEqual(h.count(`${step}:${executionId}`), preparedOnce, `${step}:${executionId} once at most`);
|
|
60
|
+
const ranOnce = scenario.ran.includes(executionId) ? 1 : 0;
|
|
61
|
+
expectEqual(h.count(`body:${executionId}`), ranOnce, `body:${executionId}`);
|
|
62
|
+
if (preparedOnce === 1)
|
|
63
|
+
expectEqual(await h.ledgerState(executor, executionId), "settled", "stable ids");
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
return { id, scenario: scenario.name, kill, placement: "everywhere", run };
|
|
67
|
+
})));
|
|
68
|
+
export const KILL_BATCH_DECLARATIONS = cases.map(({ run: _run, ...entry }) => entry);
|
|
69
|
+
export const KILL_BATCH_CASES = new Map(cases.map(({ id, run }) => [id, run]));
|
|
70
|
+
//# sourceMappingURL=kill-batch.js.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { CaseImplementation } from "../../conformance/runner.ts";
|
|
2
|
+
import type { HandPlacement } from "../remote-tool-host.ts";
|
|
3
|
+
import { type HandCaseAdapters, type KillPoint } from "./hand-harness.ts";
|
|
4
|
+
/** The ledger commits one call makes on a hand, in order. */
|
|
5
|
+
declare const COMMITS: readonly ["admit", "prepare", "effect", "freeze", "release", "dispatch", "settle", "ack"];
|
|
6
|
+
export interface KillHandDeclaration {
|
|
7
|
+
readonly id: string;
|
|
8
|
+
readonly operation: (typeof COMMITS)[number];
|
|
9
|
+
readonly at: KillPoint["at"];
|
|
10
|
+
readonly placement: HandPlacement;
|
|
11
|
+
readonly expected: string;
|
|
12
|
+
}
|
|
13
|
+
export declare const KILL_HAND_DECLARATIONS: readonly KillHandDeclaration[];
|
|
14
|
+
export declare const KILL_HAND_CASES: ReadonlyMap<string, CaseImplementation<HandCaseAdapters>>;
|
|
15
|
+
export {};
|
|
16
|
+
//# sourceMappingURL=kill-hand.d.ts.map
|