@openparachute/agent 0.2.2 → 0.2.3-rc.10
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/.parachute/module.json +3 -3
- package/package.json +4 -1
- package/src/agent-defs.ts +9 -0
- package/src/auth.ts +182 -14
- package/src/backends/registry.ts +65 -27
- package/src/daemon.ts +311 -12
- package/src/def-vault-triggers.ts +317 -0
- package/src/preflight.ts +139 -0
- package/src/spawn-agent.ts +16 -0
- package/src/step-up.ts +316 -0
- package/src/terminal-ui.ts +73 -0
- package/src/transports/http-ui.ts +10 -8
- package/src/transports/vault.ts +40 -22
- package/src/ui-kit.ts +6 -3
- package/src/ui-ticket.ts +121 -0
- package/web/ui/dist/assets/index-Dhr5Kl_d.css +1 -0
- package/web/ui/dist/assets/index-Di5MmFZR.js +60 -0
- package/web/ui/dist/index.html +2 -2
- package/src/_parked/interactive-spawn.test.ts +0 -324
- package/src/_parked/interactive-spawn.ts +0 -701
- package/src/agent-defs.test.ts +0 -1504
- package/src/agent-mcp-config.test.ts +0 -115
- package/src/agents.test.ts +0 -360
- package/src/auth.test.ts +0 -46
- package/src/backends/attached-queue.test.ts +0 -376
- package/src/backends/programmatic.test.ts +0 -1715
- package/src/backends/registry.test.ts +0 -1494
- package/src/backends/stream-json.test.ts +0 -570
- package/src/channel-backend-wiring.test.ts +0 -237
- package/src/credentials.test.ts +0 -274
- package/src/cron.test.ts +0 -342
- package/src/daemon-agent-def-api.test.ts +0 -166
- package/src/daemon-agent-defs-api.test.ts +0 -953
- package/src/daemon-agent-env-api.test.ts +0 -338
- package/src/daemon-attached-queue-store.test.ts +0 -65
- package/src/daemon-config-api.test.ts +0 -962
- package/src/daemon-jobs-api.test.ts +0 -271
- package/src/daemon-vault-chat.test.ts +0 -250
- package/src/daemon.test.ts +0 -746
- package/src/def-vaults.test.ts +0 -136
- package/src/delivery-state.test.ts +0 -110
- package/src/effective-env.test.ts +0 -114
- package/src/grants.test.ts +0 -638
- package/src/hub-jwt.test.ts +0 -161
- package/src/jobs.test.ts +0 -245
- package/src/mcp-http.test.ts +0 -265
- package/src/mint-token.test.ts +0 -152
- package/src/module-manifest.test.ts +0 -158
- package/src/programmatic-wiring.test.ts +0 -838
- package/src/registry.test.ts +0 -227
- package/src/resolve-port.test.ts +0 -64
- package/src/routing.test.ts +0 -184
- package/src/runner.test.ts +0 -506
- package/src/sandbox/config.test.ts +0 -150
- package/src/sandbox/egress.test.ts +0 -113
- package/src/sandbox/live-seatbelt.test.ts +0 -277
- package/src/sandbox/mounts.test.ts +0 -154
- package/src/sandbox/sandbox.test.ts +0 -168
- package/src/services-manifest.test.ts +0 -106
- package/src/spa-serve.test.ts +0 -116
- package/src/spawn-agent-cli.test.ts +0 -172
- package/src/spawn-agent.test.ts +0 -1218
- package/src/spawn-deps.test.ts +0 -54
- package/src/terminal-assets.test.ts +0 -50
- package/src/terminal.test.ts +0 -530
- package/src/transports/http-ui.test.ts +0 -455
- package/src/transports/telegram.test.ts +0 -174
- package/src/transports/vault.test.ts +0 -2011
- package/src/ui-kit.test.ts +0 -178
- package/web/ui/dist/assets/index-C-iWdFFV.css +0 -1
- package/web/ui/dist/assets/index-VFETBk0a.js +0 -60
- package/web/ui/tsconfig.json +0 -21
|
@@ -1,1494 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Tests for the daemon-level PROGRAMMATIC-AGENT registry + per-channel serial queue
|
|
3
|
-
* (`src/backends/registry.ts`) — the wiring that drives the {@link ProgrammaticBackend}.
|
|
4
|
-
*
|
|
5
|
-
* A FAKE backend (implements {@link AgentBackend}) lets us control each turn's
|
|
6
|
-
* outcome + timing without a real `claude -p`: a deferred-promise gate makes a turn
|
|
7
|
-
* "run" until the test releases it, so the FIFO / never-concurrent invariant is
|
|
8
|
-
* directly observable. The outbound writes go to a recorder array. No tmux, no
|
|
9
|
-
* vault, no hub.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import { describe, test, expect } from "bun:test";
|
|
13
|
-
import {
|
|
14
|
-
ProgrammaticAgentRegistry,
|
|
15
|
-
OUTBOUND_MAX_RETRIES,
|
|
16
|
-
PENDING_INBOUND_CAP,
|
|
17
|
-
MAX_DELEGATION_DEPTH,
|
|
18
|
-
isTransientOutboundError,
|
|
19
|
-
type WriteOutbound,
|
|
20
|
-
type WriteThread,
|
|
21
|
-
type WriteCallback,
|
|
22
|
-
type CallbackMeta,
|
|
23
|
-
type ThreadNote,
|
|
24
|
-
type TurnEventSink,
|
|
25
|
-
type TurnLifecycleEvent,
|
|
26
|
-
} from "./registry.ts";
|
|
27
|
-
import type {
|
|
28
|
-
AgentBackend,
|
|
29
|
-
AgentHandle,
|
|
30
|
-
AgentStatus,
|
|
31
|
-
DeliverResult,
|
|
32
|
-
InterimSink,
|
|
33
|
-
TurnSession,
|
|
34
|
-
} from "./types.ts";
|
|
35
|
-
import type { AgentSpec } from "../sandbox/types.ts";
|
|
36
|
-
|
|
37
|
-
/** A deferred promise — resolve it externally to release a gated turn. */
|
|
38
|
-
function deferred<T>(): { promise: Promise<T>; resolve: (v: T) => void } {
|
|
39
|
-
let resolve!: (v: T) => void;
|
|
40
|
-
const promise = new Promise<T>((r) => (resolve = r));
|
|
41
|
-
return { promise, resolve };
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* A controllable fake backend. `deliver` records each (channel, message), tracks how
|
|
46
|
-
* many turns are CONCURRENTLY in flight (the serial-queue invariant asserts this
|
|
47
|
-
* never exceeds 1), and resolves with whatever `nextResult` says. When `gate` is set,
|
|
48
|
-
* a turn blocks on it until the test releases it — so we can hold a turn "running"
|
|
49
|
-
* while we enqueue more.
|
|
50
|
-
*/
|
|
51
|
-
class FakeBackend implements AgentBackend {
|
|
52
|
-
readonly kind = "programmatic";
|
|
53
|
-
/** Per-call records, in arrival order — including the caller-resolved {@link TurnSession}. */
|
|
54
|
-
readonly calls: { channel: string; message: string; session: TurnSession }[] = [];
|
|
55
|
-
/** Max concurrent in-flight turns observed (must stay ≤ 1 for serial). */
|
|
56
|
-
maxConcurrent = 0;
|
|
57
|
-
private inFlight = 0;
|
|
58
|
-
/** Whether `stop` was called, per channel. */
|
|
59
|
-
readonly stopped = new Set<string>();
|
|
60
|
-
/** A gate the next turn waits on (release to let it finish). Reset per use. */
|
|
61
|
-
gate: { promise: Promise<void>; resolve: () => void } | null = null;
|
|
62
|
-
/**
|
|
63
|
-
* The result function — given the message + the turn's session id, returns the
|
|
64
|
-
* DeliverResult to resolve. The DEFAULT ECHOES `sessionId` (mirroring real claude, which
|
|
65
|
-
* always echoes the `--session-id`/`--resume` id it was handed on a successful turn) so a
|
|
66
|
-
* successful turn's thread note carries the established session — the FIX-2 invariant.
|
|
67
|
-
* An override that omits `sessionId` models a turn that failed BEFORE establishing one.
|
|
68
|
-
*/
|
|
69
|
-
resultFor: (message: string, sessionId: string) => DeliverResult = (m, sid) => ({
|
|
70
|
-
ok: true,
|
|
71
|
-
reply: "reply:" + m,
|
|
72
|
-
sessionId: sid,
|
|
73
|
-
});
|
|
74
|
-
/** If set, `deliver` THROWS this (to test the defensive catch). */
|
|
75
|
-
throwOnce: Error | null = null;
|
|
76
|
-
/** Interim events to emit (via `onInterim`) during the next turn — set per test. */
|
|
77
|
-
interimToEmit: Parameters<InterimSink>[0][] = [];
|
|
78
|
-
|
|
79
|
-
async start(spec: AgentSpec): Promise<AgentHandle> {
|
|
80
|
-
return { backend: this.kind, channel: spec.channels[0] as string, name: spec.name, spec };
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
async deliver(
|
|
84
|
-
handle: AgentHandle,
|
|
85
|
-
message: string,
|
|
86
|
-
session: TurnSession,
|
|
87
|
-
onInterim?: InterimSink,
|
|
88
|
-
): Promise<DeliverResult> {
|
|
89
|
-
this.calls.push({ channel: handle.channel, message, session });
|
|
90
|
-
this.inFlight++;
|
|
91
|
-
this.maxConcurrent = Math.max(this.maxConcurrent, this.inFlight);
|
|
92
|
-
try {
|
|
93
|
-
// Emit any configured interim events (mirrors the real backend streaming text +
|
|
94
|
-
// tool_use as the turn runs) so the registry's forwarding can be asserted.
|
|
95
|
-
if (onInterim) for (const e of this.interimToEmit) onInterim(e);
|
|
96
|
-
if (this.gate) await this.gate.promise;
|
|
97
|
-
if (this.throwOnce) {
|
|
98
|
-
const e = this.throwOnce;
|
|
99
|
-
this.throwOnce = null;
|
|
100
|
-
throw e;
|
|
101
|
-
}
|
|
102
|
-
return this.resultFor(message, session.id);
|
|
103
|
-
} finally {
|
|
104
|
-
this.inFlight--;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
async stop(handle: AgentHandle): Promise<void> {
|
|
109
|
-
this.stopped.add(handle.channel);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
async status(_handle: AgentHandle): Promise<AgentStatus> {
|
|
113
|
-
return { live: true };
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/** A recorder WriteOutbound — captures every posted reply. */
|
|
118
|
-
function recorder(): { calls: { channel: string; reply: string; inReplyTo?: string }[]; fn: WriteOutbound } {
|
|
119
|
-
const calls: { channel: string; reply: string; inReplyTo?: string }[] = [];
|
|
120
|
-
const fn: WriteOutbound = async (channel, reply, inReplyTo) => {
|
|
121
|
-
calls.push({ channel, reply, ...(inReplyTo ? { inReplyTo } : {}) });
|
|
122
|
-
};
|
|
123
|
-
return { calls, fn };
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* A recorder WriteThread — captures every `#agent/thread` note the registry writes.
|
|
128
|
-
*
|
|
129
|
-
* The thread-as-container lifecycle writes TWO notes per turn: a `phase:"start"`
|
|
130
|
-
* working-ensure BEFORE the turn, then a `phase:"end"` final record after. `threads`
|
|
131
|
-
* holds ALL writes in order; `ends()` / `starts()` filter by phase so a test can assert
|
|
132
|
-
* the FINAL records (the pre-thread-as-container assertions) without counting the
|
|
133
|
-
* working-ensure, or assert the working-ensure specifically.
|
|
134
|
-
*/
|
|
135
|
-
function threadRecorder(): {
|
|
136
|
-
threads: ThreadNote[];
|
|
137
|
-
ends: () => ThreadNote[];
|
|
138
|
-
starts: () => ThreadNote[];
|
|
139
|
-
fn: WriteThread;
|
|
140
|
-
} {
|
|
141
|
-
const threads: ThreadNote[] = [];
|
|
142
|
-
const fn: WriteThread = async (thread) => {
|
|
143
|
-
threads.push(thread);
|
|
144
|
-
};
|
|
145
|
-
return {
|
|
146
|
-
threads,
|
|
147
|
-
// `phase:"end"` is explicit on every registry-emitted final record; a write with no
|
|
148
|
-
// phase would also be a final record (back-compat), so treat absent as end too.
|
|
149
|
-
ends: () => threads.filter((t) => t.phase !== "start"),
|
|
150
|
-
starts: () => threads.filter((t) => t.phase === "start"),
|
|
151
|
-
fn,
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/** A multi-threaded spec (materializes one `#agent/thread` note per fire). */
|
|
156
|
-
const specMultiThreaded = (name: string, channel = name, definition?: string): AgentSpec => ({
|
|
157
|
-
name,
|
|
158
|
-
channels: [channel],
|
|
159
|
-
mode: "multi-threaded",
|
|
160
|
-
...(definition ? { definition } : {}),
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
/** A recorder TurnEventSink — captures every (channel, event) the registry emits. */
|
|
164
|
-
function turnRecorder(): { events: { channel: string; event: TurnLifecycleEvent }[]; fn: TurnEventSink } {
|
|
165
|
-
const events: { channel: string; event: TurnLifecycleEvent }[] = [];
|
|
166
|
-
const fn: TurnEventSink = (channel, event) => {
|
|
167
|
-
events.push({ channel, event });
|
|
168
|
-
};
|
|
169
|
-
return { events, fn };
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
const specFor = (name: string, channel = name): AgentSpec => ({ name, channels: [channel] });
|
|
173
|
-
|
|
174
|
-
/** Spin the microtask/timer queue until `pred()` is true or we give up. */
|
|
175
|
-
async function until(pred: () => boolean, tries = 200): Promise<void> {
|
|
176
|
-
for (let i = 0; i < tries && !pred(); i++) {
|
|
177
|
-
await new Promise<void>((r) => setTimeout(r, 1));
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
describe("ProgrammaticAgentRegistry — registration + indexes", () => {
|
|
182
|
-
test("register indexes by channel + name; has/get reflect it", async () => {
|
|
183
|
-
const backend = new FakeBackend();
|
|
184
|
-
const { fn } = recorder();
|
|
185
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: fn });
|
|
186
|
-
|
|
187
|
-
expect(reg.hasChannel("eng")).toBe(false);
|
|
188
|
-
const h = await reg.register(specFor("eng"));
|
|
189
|
-
expect(h.name).toBe("eng");
|
|
190
|
-
expect(h.channel).toBe("eng");
|
|
191
|
-
expect(reg.hasChannel("eng")).toBe(true);
|
|
192
|
-
expect(reg.hasName("eng")).toBe(true);
|
|
193
|
-
expect(reg.getByChannel("eng")?.name).toBe("eng");
|
|
194
|
-
expect(reg.getByName("eng")?.channel).toBe("eng");
|
|
195
|
-
expect(reg.list().map((x) => x.name)).toEqual(["eng"]);
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
test("deregister drops the indexes + tears down the backend handle (stop)", async () => {
|
|
199
|
-
const backend = new FakeBackend();
|
|
200
|
-
const { fn } = recorder();
|
|
201
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: fn });
|
|
202
|
-
await reg.register(specFor("eng"));
|
|
203
|
-
expect(await reg.deregister("eng")).toBe(true);
|
|
204
|
-
expect(reg.hasChannel("eng")).toBe(false);
|
|
205
|
-
expect(reg.hasName("eng")).toBe(false);
|
|
206
|
-
// deregister calls backend.stop (a no-op for programmatic) — it does NOT clear the
|
|
207
|
-
// thread-note session; re-registering should resume. Wiping continuity is resetSession.
|
|
208
|
-
expect(backend.stopped.has("eng")).toBe(true);
|
|
209
|
-
// A second deregister is a no-op false.
|
|
210
|
-
expect(await reg.deregister("eng")).toBe(false);
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
test("resetSession CLEARS the thread-note session (via clearSession) WITHOUT deregistering", async () => {
|
|
214
|
-
const backend = new FakeBackend();
|
|
215
|
-
const { fn } = recorder();
|
|
216
|
-
// Track the clearSession invocations + simulate the note's session being wiped: after a
|
|
217
|
-
// clear, readSession returns undefined for that (channel, name).
|
|
218
|
-
const cleared: { channel: string; name: string }[] = [];
|
|
219
|
-
let priorSession: string | undefined = "sess-OLD";
|
|
220
|
-
const clearSession = async (channel: string, name: string) => {
|
|
221
|
-
cleared.push({ channel, name });
|
|
222
|
-
priorSession = undefined; // the note's session is now empty.
|
|
223
|
-
};
|
|
224
|
-
const reader: { calls: { channel: string; name: string }[] } = { calls: [] };
|
|
225
|
-
const readSession = async (channel: string, name: string) => {
|
|
226
|
-
reader.calls.push({ channel, name });
|
|
227
|
-
return priorSession;
|
|
228
|
-
};
|
|
229
|
-
const threads = threadRecorder();
|
|
230
|
-
const reg = new ProgrammaticAgentRegistry({
|
|
231
|
-
backend,
|
|
232
|
-
writeOutbound: fn,
|
|
233
|
-
writeThread: threads.fn,
|
|
234
|
-
readSession,
|
|
235
|
-
clearSession,
|
|
236
|
-
});
|
|
237
|
-
await reg.register(specFor("eng"));
|
|
238
|
-
|
|
239
|
-
// RESET → invokes clearSession with the right (channel, name); does NOT deregister.
|
|
240
|
-
expect(await reg.resetSession("eng")).toBe(true);
|
|
241
|
-
expect(cleared).toEqual([{ channel: "eng", name: "eng" }]);
|
|
242
|
-
expect(reg.hasName("eng")).toBe(true); // still registered.
|
|
243
|
-
// reset() does NOT route through backend.stop anymore (the session lives on the note).
|
|
244
|
-
expect(backend.stopped.has("eng")).toBe(false);
|
|
245
|
-
|
|
246
|
-
// BONUS — after the reset, the next drain finds NO prior session → a fresh {resume:false}
|
|
247
|
-
// create (self-heal), proving reset actually wiped continuity (not a dead no-op).
|
|
248
|
-
reg.enqueue("eng", { content: "after reset" });
|
|
249
|
-
await until(() => backend.calls.length === 1);
|
|
250
|
-
expect(backend.calls[0]!.session.resume).toBe(false);
|
|
251
|
-
expect(backend.calls[0]!.session.id).toMatch(/^[0-9a-f-]{36}$/);
|
|
252
|
-
|
|
253
|
-
expect(await reg.resetSession("nope")).toBe(false);
|
|
254
|
-
});
|
|
255
|
-
});
|
|
256
|
-
|
|
257
|
-
describe("ProgrammaticAgentRegistry — inbound enqueue + outbound", () => {
|
|
258
|
-
test("a delivered turn writes a non-empty reply as an outbound note", async () => {
|
|
259
|
-
const backend = new FakeBackend();
|
|
260
|
-
const rec = recorder();
|
|
261
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
|
|
262
|
-
await reg.register(specFor("eng"));
|
|
263
|
-
|
|
264
|
-
expect(reg.enqueue("eng", { content: "hello", inReplyTo: "note-1" })).toBe(true);
|
|
265
|
-
await until(() => rec.calls.length === 1);
|
|
266
|
-
|
|
267
|
-
expect(backend.calls).toHaveLength(1);
|
|
268
|
-
expect(backend.calls[0]!.channel).toBe("eng");
|
|
269
|
-
expect(backend.calls[0]!.message).toBe("hello");
|
|
270
|
-
// No readSession wired → a single-threaded turn CREATES a fresh session (resume:false).
|
|
271
|
-
expect(backend.calls[0]!.session.resume).toBe(false);
|
|
272
|
-
expect(backend.calls[0]!.session.id).toMatch(/^[0-9a-f-]{36}$/);
|
|
273
|
-
expect(rec.calls).toEqual([{ channel: "eng", reply: "reply:hello", inReplyTo: "note-1" }]);
|
|
274
|
-
});
|
|
275
|
-
|
|
276
|
-
test("enqueue for an UNREGISTERED channel is a no-op false (caller falls back)", () => {
|
|
277
|
-
const backend = new FakeBackend();
|
|
278
|
-
const rec = recorder();
|
|
279
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
|
|
280
|
-
expect(reg.enqueue("ghost", { content: "x" })).toBe(false);
|
|
281
|
-
expect(backend.calls).toHaveLength(0);
|
|
282
|
-
});
|
|
283
|
-
|
|
284
|
-
test("an EMPTY reply writes NO outbound note (reviewer contract)", async () => {
|
|
285
|
-
const backend = new FakeBackend();
|
|
286
|
-
backend.resultFor = () => ({ ok: true, reply: "" });
|
|
287
|
-
const rec = recorder();
|
|
288
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
|
|
289
|
-
await reg.register(specFor("eng"));
|
|
290
|
-
|
|
291
|
-
reg.enqueue("eng", { content: "tool-only work" });
|
|
292
|
-
await until(() => backend.calls.length === 1);
|
|
293
|
-
// Give any erroneous outbound write a chance to land, then assert none did.
|
|
294
|
-
await new Promise<void>((r) => setTimeout(r, 5));
|
|
295
|
-
expect(backend.calls).toHaveLength(1);
|
|
296
|
-
expect(rec.calls).toHaveLength(0);
|
|
297
|
-
});
|
|
298
|
-
|
|
299
|
-
test("an ok:false turn writes a user-facing FAILURE note + does not crash/loop", async () => {
|
|
300
|
-
const backend = new FakeBackend();
|
|
301
|
-
backend.resultFor = () => ({ ok: false, error: "mint refused" });
|
|
302
|
-
const rec = recorder();
|
|
303
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
|
|
304
|
-
await reg.register(specFor("eng"));
|
|
305
|
-
|
|
306
|
-
reg.enqueue("eng", { content: "do it" });
|
|
307
|
-
await until(() => rec.calls.length === 1);
|
|
308
|
-
await new Promise<void>((r) => setTimeout(r, 5));
|
|
309
|
-
// Exactly ONE turn ran (the backend owns turn-retry, not the drain), and the drain
|
|
310
|
-
// posted a SINGLE user-facing failure note carrying the reason (no silent no-reply).
|
|
311
|
-
expect(backend.calls).toHaveLength(1);
|
|
312
|
-
expect(rec.calls).toHaveLength(1);
|
|
313
|
-
expect(rec.calls[0]!.reply).toContain("mint refused");
|
|
314
|
-
});
|
|
315
|
-
|
|
316
|
-
test("a deliver() that THROWS is caught — the worker survives + drains the rest", async () => {
|
|
317
|
-
const backend = new FakeBackend();
|
|
318
|
-
backend.throwOnce = new Error("surprise throw");
|
|
319
|
-
const rec = recorder();
|
|
320
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
|
|
321
|
-
await reg.register(specFor("eng"));
|
|
322
|
-
|
|
323
|
-
reg.enqueue("eng", { content: "first (throws)" });
|
|
324
|
-
reg.enqueue("eng", { content: "second (ok)" });
|
|
325
|
-
await until(() => rec.calls.length === 2);
|
|
326
|
-
// Both turns ran; the throw on the first didn't strand the second. The caught throw
|
|
327
|
-
// posts a user-facing failure note (carrying the reason); the second succeeds normally.
|
|
328
|
-
expect(backend.calls.map((c) => c.message)).toEqual(["first (throws)", "second (ok)"]);
|
|
329
|
-
expect(rec.calls).toHaveLength(2);
|
|
330
|
-
expect(rec.calls[0]!.reply).toContain("surprise throw");
|
|
331
|
-
expect(rec.calls[1]!).toEqual({ channel: "eng", reply: "reply:second (ok)" });
|
|
332
|
-
});
|
|
333
|
-
});
|
|
334
|
-
|
|
335
|
-
describe("ProgrammaticAgentRegistry — #agent/thread notes (unified lifecycle, BOTH modes)", () => {
|
|
336
|
-
test("a completed MULTI-THREADED turn materializes an #agent/thread note (status ok) carrying input/output/definition/mode/name", async () => {
|
|
337
|
-
const backend = new FakeBackend();
|
|
338
|
-
const rec = recorder();
|
|
339
|
-
const threads = threadRecorder();
|
|
340
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, writeThread: threads.fn });
|
|
341
|
-
await reg.register(specMultiThreaded("digest", "digest", "Agents/digest"));
|
|
342
|
-
|
|
343
|
-
reg.enqueue("digest", { content: "run the digest" });
|
|
344
|
-
await until(() => threads.ends().length === 1);
|
|
345
|
-
|
|
346
|
-
// Thread-as-container: ONE working-ensure (phase:start, status:working) BEFORE the turn,
|
|
347
|
-
// then ONE final record (phase:end, status:ok) after — for the same per-fire note.
|
|
348
|
-
expect(threads.starts()).toHaveLength(1);
|
|
349
|
-
expect(threads.starts()[0]!.status).toBe("working");
|
|
350
|
-
expect(threads.ends()).toHaveLength(1);
|
|
351
|
-
const thread = threads.ends()[0]!;
|
|
352
|
-
expect(thread.channel).toBe("digest");
|
|
353
|
-
expect(thread.name).toBe("digest");
|
|
354
|
-
expect(thread.status).toBe("ok");
|
|
355
|
-
expect(thread.mode).toBe("multi-threaded");
|
|
356
|
-
expect(thread.definition).toBe("Agents/digest");
|
|
357
|
-
expect(thread.input).toBe("run the digest");
|
|
358
|
-
expect(thread.output).toBe("reply:run the digest");
|
|
359
|
-
expect(typeof thread.started_at).toBe("string");
|
|
360
|
-
expect(typeof thread.ended_at).toBe("string");
|
|
361
|
-
// The start + end target the SAME per-fire note (same threadId) — no duplicate minted.
|
|
362
|
-
expect(threads.starts()[0]!.threadId).toBe(thread.threadId!);
|
|
363
|
-
// The dual-write is ADDITIVE: a non-empty reply writes EXACTLY one outbound
|
|
364
|
-
// (the chat delivery) AND exactly one FINAL thread note (the primary record).
|
|
365
|
-
await until(() => rec.calls.length === 1);
|
|
366
|
-
expect(rec.calls.length).toBe(1);
|
|
367
|
-
expect(threads.ends().length).toBe(1);
|
|
368
|
-
});
|
|
369
|
-
|
|
370
|
-
test("a SINGLE-THREADED turn ALSO materializes ONE #agent/thread note (the unified model — named after the def)", async () => {
|
|
371
|
-
const backend = new FakeBackend();
|
|
372
|
-
const rec = recorder();
|
|
373
|
-
const threads = threadRecorder();
|
|
374
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, writeThread: threads.fn });
|
|
375
|
-
// specFor → no mode → single-threaded (the default).
|
|
376
|
-
await reg.register(specFor("eng"));
|
|
377
|
-
|
|
378
|
-
reg.enqueue("eng", { content: "hello" });
|
|
379
|
-
await until(() => threads.ends().length === 1);
|
|
380
|
-
|
|
381
|
-
// BOTH modes materialize a thread note now (the structural unification): a
|
|
382
|
-
// single-threaded turn writes ONE FINAL record, mode single-threaded, NAMED AFTER THE
|
|
383
|
-
// DEF (the deterministic upsert key the transport derives the stable path from).
|
|
384
|
-
// Thread-as-container: a working-ensure (phase:start) preceded it (same upsert key).
|
|
385
|
-
expect(threads.starts()).toHaveLength(1);
|
|
386
|
-
expect(threads.starts()[0]!.name).toBe("eng");
|
|
387
|
-
expect(threads.ends()).toHaveLength(1);
|
|
388
|
-
expect(threads.ends()[0]!.mode).toBe("single-threaded");
|
|
389
|
-
expect(threads.ends()[0]!.name).toBe("eng");
|
|
390
|
-
expect(threads.ends()[0]!.channel).toBe("eng");
|
|
391
|
-
expect(threads.ends()[0]!.input).toBe("hello");
|
|
392
|
-
expect(threads.ends()[0]!.output).toBe("reply:hello");
|
|
393
|
-
// The single-threaded outbound reply was still written (no regression).
|
|
394
|
-
expect(rec.calls).toHaveLength(1);
|
|
395
|
-
});
|
|
396
|
-
|
|
397
|
-
test("a single-threaded agent over TWO turns records ONE thread (same name/channel — the upsert key) both turns, status carries forward", async () => {
|
|
398
|
-
const backend = new FakeBackend();
|
|
399
|
-
const rec = recorder();
|
|
400
|
-
const threads = threadRecorder();
|
|
401
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, writeThread: threads.fn });
|
|
402
|
-
await reg.register(specFor("eng")); // single-threaded (default).
|
|
403
|
-
|
|
404
|
-
// Two turns on the same channel — drained serially, FIFO.
|
|
405
|
-
reg.enqueue("eng", { content: "turn one" });
|
|
406
|
-
reg.enqueue("eng", { content: "turn two" });
|
|
407
|
-
await until(() => threads.ends().length === 2);
|
|
408
|
-
|
|
409
|
-
// recordThread (the FINAL record) is called for BOTH turns (the registry seam can't
|
|
410
|
-
// simulate the transport's read-existing upsert, so we assert the UPSERT KEY is stable
|
|
411
|
-
// across turns — same channel + same name + same mode — which the transport maps to the
|
|
412
|
-
// SAME deterministic path `Threads/<channel>/<name>`, overwriting in place. The per-turn
|
|
413
|
-
// turn_count/usage aggregation — incl. the start-ensure NOT double-counting — is covered
|
|
414
|
-
// at the vault-transport layer). Each turn ALSO emits its own working-ensure (phase:start).
|
|
415
|
-
expect(threads.starts()).toHaveLength(2);
|
|
416
|
-
expect(threads.ends()).toHaveLength(2);
|
|
417
|
-
const [t1, t2] = threads.ends();
|
|
418
|
-
expect(t1!.mode).toBe("single-threaded");
|
|
419
|
-
expect(t2!.mode).toBe("single-threaded");
|
|
420
|
-
expect(t1!.name).toBe("eng");
|
|
421
|
-
expect(t2!.name).toBe("eng"); // SAME upsert key → same note, upserted.
|
|
422
|
-
expect(t1!.channel).toBe("eng");
|
|
423
|
-
expect(t2!.channel).toBe("eng");
|
|
424
|
-
expect(t1!.input).toBe("turn one");
|
|
425
|
-
expect(t2!.input).toBe("turn two");
|
|
426
|
-
});
|
|
427
|
-
|
|
428
|
-
test("a multi-threaded fire writes a thread note per fire (each carries this fire's turn — distinct records)", async () => {
|
|
429
|
-
const backend = new FakeBackend();
|
|
430
|
-
const rec = recorder();
|
|
431
|
-
const threads = threadRecorder();
|
|
432
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, writeThread: threads.fn });
|
|
433
|
-
await reg.register(specMultiThreaded("digest"));
|
|
434
|
-
|
|
435
|
-
reg.enqueue("digest", { content: "fire A" });
|
|
436
|
-
reg.enqueue("digest", { content: "fire B" });
|
|
437
|
-
await until(() => threads.ends().length === 2);
|
|
438
|
-
|
|
439
|
-
// One FINAL thread note PER FIRE (today one fire = one thread = one note; the transport
|
|
440
|
-
// assigns each a fresh uuid path, so they're distinct records). Each fire's start-ensure
|
|
441
|
-
// shares that fire's threadId (so start + end target the SAME per-fire note).
|
|
442
|
-
expect(threads.ends()).toHaveLength(2);
|
|
443
|
-
expect(threads.ends().map((t) => t.input)).toEqual(["fire A", "fire B"]);
|
|
444
|
-
expect(threads.ends().every((t) => t.mode === "multi-threaded")).toBe(true);
|
|
445
|
-
expect(threads.starts()).toHaveLength(2);
|
|
446
|
-
// Per fire the working-ensure + final record share a threadId (distinct across fires).
|
|
447
|
-
expect(threads.starts()[0]!.threadId).toBe(threads.ends()[0]!.threadId!);
|
|
448
|
-
expect(threads.starts()[1]!.threadId).toBe(threads.ends()[1]!.threadId!);
|
|
449
|
-
expect(threads.ends()[0]!.threadId).not.toBe(threads.ends()[1]!.threadId);
|
|
450
|
-
});
|
|
451
|
-
|
|
452
|
-
test("a FAILED MULTI-THREADED turn still materializes an #agent/thread note with status:error + the reason", async () => {
|
|
453
|
-
const backend = new FakeBackend();
|
|
454
|
-
backend.resultFor = () => ({ ok: false, error: "mint refused" });
|
|
455
|
-
const rec = recorder();
|
|
456
|
-
const threads = threadRecorder();
|
|
457
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, writeThread: threads.fn });
|
|
458
|
-
await reg.register(specMultiThreaded("digest"));
|
|
459
|
-
|
|
460
|
-
reg.enqueue("digest", { content: "do it" });
|
|
461
|
-
await until(() => threads.ends().length === 1);
|
|
462
|
-
|
|
463
|
-
// The working-ensure (phase:start) still ran BEFORE the turn; the FINAL record is error.
|
|
464
|
-
expect(threads.starts()).toHaveLength(1);
|
|
465
|
-
expect(threads.ends()).toHaveLength(1);
|
|
466
|
-
expect(threads.ends()[0]!.mode).toBe("multi-threaded");
|
|
467
|
-
expect(threads.ends()[0]!.status).toBe("error");
|
|
468
|
-
expect(threads.ends()[0]!.output).toBe("mint refused");
|
|
469
|
-
// A user-facing failure note IS now written for a failed turn (carries the reason).
|
|
470
|
-
expect(rec.calls).toHaveLength(1);
|
|
471
|
-
expect(rec.calls[0]!.reply).toContain("mint refused");
|
|
472
|
-
});
|
|
473
|
-
|
|
474
|
-
test("a FAILED SINGLE-THREADED turn ALSO materializes an #agent/thread note with status:error (substantiates BOTH modes)", async () => {
|
|
475
|
-
const backend = new FakeBackend();
|
|
476
|
-
backend.resultFor = () => ({ ok: false, error: "mint refused" });
|
|
477
|
-
const rec = recorder();
|
|
478
|
-
const threads = threadRecorder();
|
|
479
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, writeThread: threads.fn });
|
|
480
|
-
// specFor → no mode → single-threaded (the default).
|
|
481
|
-
await reg.register(specFor("eng"));
|
|
482
|
-
|
|
483
|
-
reg.enqueue("eng", { content: "do it" });
|
|
484
|
-
await until(() => threads.ends().length === 1);
|
|
485
|
-
|
|
486
|
-
// The working-ensure (phase:start) still ran BEFORE the turn; the FINAL record is error.
|
|
487
|
-
expect(threads.starts()).toHaveLength(1);
|
|
488
|
-
expect(threads.ends()).toHaveLength(1);
|
|
489
|
-
expect(threads.ends()[0]!.mode).toBe("single-threaded");
|
|
490
|
-
expect(threads.ends()[0]!.name).toBe("eng");
|
|
491
|
-
expect(threads.ends()[0]!.status).toBe("error");
|
|
492
|
-
expect(threads.ends()[0]!.output).toBe("mint refused");
|
|
493
|
-
// A user-facing failure note IS now written for a failed turn (carries the reason).
|
|
494
|
-
expect(rec.calls).toHaveLength(1);
|
|
495
|
-
expect(rec.calls[0]!.reply).toContain("mint refused");
|
|
496
|
-
});
|
|
497
|
-
|
|
498
|
-
test("a turn with an empty reply STILL materializes a thread note (status ok, empty output)", async () => {
|
|
499
|
-
const backend = new FakeBackend();
|
|
500
|
-
backend.resultFor = () => ({ ok: true, reply: "" });
|
|
501
|
-
const rec = recorder();
|
|
502
|
-
const threads = threadRecorder();
|
|
503
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, writeThread: threads.fn });
|
|
504
|
-
await reg.register(specMultiThreaded("digest"));
|
|
505
|
-
|
|
506
|
-
reg.enqueue("digest", { content: "tool-only run" });
|
|
507
|
-
await until(() => threads.ends().length === 1);
|
|
508
|
-
// The working-ensure (phase:start, no fake reply) preceded the empty-reply final record.
|
|
509
|
-
expect(threads.starts()).toHaveLength(1);
|
|
510
|
-
expect(threads.starts()[0]!.output).toBe("");
|
|
511
|
-
expect(threads.ends()[0]!.status).toBe("ok");
|
|
512
|
-
expect(threads.ends()[0]!.output).toBe("");
|
|
513
|
-
// Empty reply → no outbound message note (the thread note IS the record).
|
|
514
|
-
expect(rec.calls).toHaveLength(0);
|
|
515
|
-
});
|
|
516
|
-
|
|
517
|
-
test("REGRESSION (c34db03, now BOTH modes): a turn whose outbound write THROWS still leaves a primary #agent/thread note (now re-recorded as error — FIX 1)", async () => {
|
|
518
|
-
const backend = new FakeBackend();
|
|
519
|
-
const threads = threadRecorder();
|
|
520
|
-
// A THROWING WriteOutbound — the thread note is written BEFORE the additive outbound
|
|
521
|
-
// (c34db03, now applied uniformly to BOTH modes), so the failed transcript write must
|
|
522
|
-
// NOT cost us the primary record. Use a SINGLE-THREADED spec to prove the c34db03
|
|
523
|
-
// ordering now protects single-threaded too. (`recorder()` can't throw; inline variant.)
|
|
524
|
-
// The error message carries NO HTTP status → classified TRANSIENT → it RETRIES the
|
|
525
|
-
// bounded budget (FIX 1, PR #3) before giving up, then re-records the thread as error.
|
|
526
|
-
let outboundAttempts = 0;
|
|
527
|
-
const throwingWriteOutbound: WriteOutbound = async () => {
|
|
528
|
-
outboundAttempts++;
|
|
529
|
-
throw new Error("vault write boom"); // no (NNN) status → transient → retried.
|
|
530
|
-
};
|
|
531
|
-
const reg = new ProgrammaticAgentRegistry({
|
|
532
|
-
backend,
|
|
533
|
-
writeOutbound: throwingWriteOutbound,
|
|
534
|
-
writeThread: threads.fn,
|
|
535
|
-
outboundRetryBaseMs: 0,
|
|
536
|
-
});
|
|
537
|
-
await reg.register(specFor("eng")); // single-threaded (default) — the ordering applies here now.
|
|
538
|
-
|
|
539
|
-
reg.enqueue("eng", { content: "fire it" });
|
|
540
|
-
// Thread-as-container + FIX 1: a working-ensure (phase:start) is written first, then the
|
|
541
|
-
// primary `ok` FINAL record, then after retries exhaust a `error` FINAL record re-records
|
|
542
|
-
// the UN-DELIVERED reply — so `ends()` (the final records) is exactly [ok, error].
|
|
543
|
-
await until(() => threads.ends().length === 2);
|
|
544
|
-
|
|
545
|
-
// The working-ensure preceded everything (status:working, no fake reply).
|
|
546
|
-
expect(threads.starts()).toHaveLength(1);
|
|
547
|
-
expect(threads.starts()[0]!.status).toBe("working");
|
|
548
|
-
// First FINAL (optimistic) record was `ok`; the second re-records the failure so the
|
|
549
|
-
// durable thread record does NOT falsely claim the reply landed.
|
|
550
|
-
expect(threads.ends()[0]!.status).toBe("ok");
|
|
551
|
-
expect(threads.ends()[0]!.output).toBe("reply:fire it");
|
|
552
|
-
expect(threads.ends()[1]!.status).toBe("error");
|
|
553
|
-
expect(threads.ends()[1]!.mode).toBe("single-threaded");
|
|
554
|
-
// The undelivered reply text is preserved in the error record for recovery.
|
|
555
|
-
expect(threads.ends()[1]!.output).toContain("reply:fire it");
|
|
556
|
-
// The re-record reuses the SAME per-turn threadId + sameTurn (no double-count, no dup).
|
|
557
|
-
expect(threads.ends()[1]!.threadId).toBe(threads.ends()[0]!.threadId!);
|
|
558
|
-
expect(threads.ends()[1]!.sameTurn).toBe(true);
|
|
559
|
-
// Transient → the outbound was retried the full budget (1 initial + OUTBOUND_MAX_RETRIES).
|
|
560
|
-
expect(outboundAttempts).toBe(1 + OUTBOUND_MAX_RETRIES);
|
|
561
|
-
});
|
|
562
|
-
});
|
|
563
|
-
|
|
564
|
-
describe("ProgrammaticAgentRegistry — thread≡session (the daemon owns the uuid)", () => {
|
|
565
|
-
/** A recorder readSession — captures every (channel, name) consulted; returns `prior`. */
|
|
566
|
-
function sessionReader(prior?: string): {
|
|
567
|
-
calls: { channel: string; name: string }[];
|
|
568
|
-
fn: (channel: string, name: string) => Promise<string | undefined>;
|
|
569
|
-
} {
|
|
570
|
-
const calls: { channel: string; name: string }[] = [];
|
|
571
|
-
const fn = async (channel: string, name: string) => {
|
|
572
|
-
calls.push({ channel, name });
|
|
573
|
-
return prior;
|
|
574
|
-
};
|
|
575
|
-
return { calls, fn };
|
|
576
|
-
}
|
|
577
|
-
|
|
578
|
-
test("single-threaded with a PRIOR session: consults readSession + passes {resume:true} to deliver", async () => {
|
|
579
|
-
const backend = new FakeBackend();
|
|
580
|
-
const rec = recorder();
|
|
581
|
-
const threads = threadRecorder();
|
|
582
|
-
const reader = sessionReader("11111111-1111-4111-8111-111111111111");
|
|
583
|
-
const reg = new ProgrammaticAgentRegistry({
|
|
584
|
-
backend,
|
|
585
|
-
writeOutbound: rec.fn,
|
|
586
|
-
writeThread: threads.fn,
|
|
587
|
-
readSession: reader.fn,
|
|
588
|
-
});
|
|
589
|
-
await reg.register(specFor("eng")); // single-threaded (default).
|
|
590
|
-
|
|
591
|
-
reg.enqueue("eng", { content: "hello" });
|
|
592
|
-
await until(() => backend.calls.length === 1);
|
|
593
|
-
|
|
594
|
-
// readSession was consulted with the channel + the def name (the deterministic key).
|
|
595
|
-
expect(reader.calls).toEqual([{ channel: "eng", name: "eng" }]);
|
|
596
|
-
// A prior session → RESUME it (continue the conversation), with that exact id.
|
|
597
|
-
expect(backend.calls[0]!.session).toEqual({
|
|
598
|
-
id: "11111111-1111-4111-8111-111111111111",
|
|
599
|
-
resume: true,
|
|
600
|
-
});
|
|
601
|
-
// The END record carries the session claude echoed (the persisted thread≡session record).
|
|
602
|
-
await until(() => threads.ends().length === 1);
|
|
603
|
-
expect(threads.ends()[0]!.session).toBe("11111111-1111-4111-8111-111111111111");
|
|
604
|
-
// The START-ensure carries NO session (FIX 2) — it runs before claude, so no session is
|
|
605
|
-
// established yet; persisting one there would brick the next turn if claude never inited.
|
|
606
|
-
// (Continuity for a single-threaded resume is preserved by the transport's prior-read.)
|
|
607
|
-
expect(threads.starts()[0]!.session).toBeUndefined();
|
|
608
|
-
});
|
|
609
|
-
|
|
610
|
-
test("single-threaded with NO prior session: consults readSession + passes {resume:false} + a fresh uuid", async () => {
|
|
611
|
-
const backend = new FakeBackend();
|
|
612
|
-
const rec = recorder();
|
|
613
|
-
const threads = threadRecorder();
|
|
614
|
-
const reader = sessionReader(undefined); // no prior — first turn.
|
|
615
|
-
const reg = new ProgrammaticAgentRegistry({
|
|
616
|
-
backend,
|
|
617
|
-
writeOutbound: rec.fn,
|
|
618
|
-
writeThread: threads.fn,
|
|
619
|
-
readSession: reader.fn,
|
|
620
|
-
});
|
|
621
|
-
await reg.register(specFor("eng"));
|
|
622
|
-
|
|
623
|
-
reg.enqueue("eng", { content: "hello" });
|
|
624
|
-
await until(() => backend.calls.length === 1);
|
|
625
|
-
|
|
626
|
-
expect(reader.calls).toEqual([{ channel: "eng", name: "eng" }]);
|
|
627
|
-
// No prior → CREATE a fresh session with a generated uuid (--session-id, not --resume).
|
|
628
|
-
expect(backend.calls[0]!.session.resume).toBe(false);
|
|
629
|
-
expect(backend.calls[0]!.session.id).toMatch(/^[0-9a-f-]{36}$/);
|
|
630
|
-
// The fresh uuid is the one persisted onto the thread note (so turn 2 can resume it).
|
|
631
|
-
await until(() => threads.ends().length === 1);
|
|
632
|
-
expect(threads.ends()[0]!.session).toBe(backend.calls[0]!.session.id);
|
|
633
|
-
});
|
|
634
|
-
|
|
635
|
-
test("multi-threaded NEVER consults readSession + ALWAYS passes {resume:false} with a fresh uuid", async () => {
|
|
636
|
-
const backend = new FakeBackend();
|
|
637
|
-
const rec = recorder();
|
|
638
|
-
const threads = threadRecorder();
|
|
639
|
-
const reader = sessionReader("should-never-be-used");
|
|
640
|
-
const reg = new ProgrammaticAgentRegistry({
|
|
641
|
-
backend,
|
|
642
|
-
writeOutbound: rec.fn,
|
|
643
|
-
writeThread: threads.fn,
|
|
644
|
-
readSession: reader.fn,
|
|
645
|
-
});
|
|
646
|
-
await reg.register(specMultiThreaded("digest", "digest"));
|
|
647
|
-
|
|
648
|
-
// Two fires — each must mint its OWN fresh session, never resume.
|
|
649
|
-
reg.enqueue("digest", { content: "fire one" });
|
|
650
|
-
await until(() => backend.calls.length === 1);
|
|
651
|
-
reg.enqueue("digest", { content: "fire two" });
|
|
652
|
-
await until(() => backend.calls.length === 2);
|
|
653
|
-
|
|
654
|
-
// readSession is NEVER consulted for a multi-threaded agent (each fire is a fresh thread).
|
|
655
|
-
expect(reader.calls).toHaveLength(0);
|
|
656
|
-
// Both fires CREATE fresh sessions (resume:false), with DISTINCT uuids.
|
|
657
|
-
expect(backend.calls[0]!.session.resume).toBe(false);
|
|
658
|
-
expect(backend.calls[1]!.session.resume).toBe(false);
|
|
659
|
-
expect(backend.calls[0]!.session.id).toMatch(/^[0-9a-f-]{36}$/);
|
|
660
|
-
expect(backend.calls[1]!.session.id).toMatch(/^[0-9a-f-]{36}$/);
|
|
661
|
-
expect(backend.calls[0]!.session.id).not.toBe(backend.calls[1]!.session.id);
|
|
662
|
-
// Each per-fire thread note carries its own fire's session.
|
|
663
|
-
await until(() => threads.ends().length === 2);
|
|
664
|
-
expect(threads.ends()[0]!.session).toBe(backend.calls[0]!.session.id);
|
|
665
|
-
expect(threads.ends()[1]!.session).toBe(backend.calls[1]!.session.id);
|
|
666
|
-
});
|
|
667
|
-
|
|
668
|
-
test("no readSession wired: a single-threaded turn still CREATES a fresh session", async () => {
|
|
669
|
-
const backend = new FakeBackend();
|
|
670
|
-
const rec = recorder();
|
|
671
|
-
const threads = threadRecorder();
|
|
672
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, writeThread: threads.fn });
|
|
673
|
-
await reg.register(specFor("eng"));
|
|
674
|
-
|
|
675
|
-
reg.enqueue("eng", { content: "hello" });
|
|
676
|
-
await until(() => backend.calls.length === 1);
|
|
677
|
-
|
|
678
|
-
expect(backend.calls[0]!.session.resume).toBe(false);
|
|
679
|
-
expect(backend.calls[0]!.session.id).toMatch(/^[0-9a-f-]{36}$/);
|
|
680
|
-
await until(() => threads.ends().length === 1);
|
|
681
|
-
expect(threads.ends()[0]!.session).toBe(backend.calls[0]!.session.id);
|
|
682
|
-
});
|
|
683
|
-
|
|
684
|
-
test("the captured backend sessionId (Claude's echoed id) is what lands on the thread note", async () => {
|
|
685
|
-
const backend = new FakeBackend();
|
|
686
|
-
// The backend echoes a DIFFERENT id than the one we passed (Claude's authoritative id).
|
|
687
|
-
backend.resultFor = (m) => ({ ok: true, reply: "reply:" + m, sessionId: "echoed-by-claude-id" });
|
|
688
|
-
const rec = recorder();
|
|
689
|
-
const threads = threadRecorder();
|
|
690
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, writeThread: threads.fn });
|
|
691
|
-
await reg.register(specFor("eng"));
|
|
692
|
-
|
|
693
|
-
reg.enqueue("eng", { content: "hello" });
|
|
694
|
-
await until(() => threads.ends().length === 1);
|
|
695
|
-
|
|
696
|
-
// The END record prefers Claude's echoed id (result.sessionId) over the uuid we passed.
|
|
697
|
-
expect(threads.ends()[0]!.session).toBe("echoed-by-claude-id");
|
|
698
|
-
});
|
|
699
|
-
|
|
700
|
-
test("FIX 2 — a failed turn that established NO session persists NONE → next turn self-heals (no brick)", async () => {
|
|
701
|
-
const backend = new FakeBackend();
|
|
702
|
-
// A turn that FAILS before claude ever creates a session: { ok:false } with NO sessionId
|
|
703
|
-
// (claude exited before emitting an init/result session_id). The OLD code persisted the
|
|
704
|
-
// passed uuid here → next turn `--resume`d a phantom id → "No conversation found" →
|
|
705
|
-
// permanent brick. FIX 2: persist NOTHING when claude echoed no session.
|
|
706
|
-
backend.resultFor = () => ({ ok: false, error: "claude exited 1 before init" });
|
|
707
|
-
const rec = recorder();
|
|
708
|
-
const threads = threadRecorder();
|
|
709
|
-
// Simulate the note: readSession returns whatever the last persisted end-record carried.
|
|
710
|
-
let stored: string | undefined; // no prior session.
|
|
711
|
-
const reg = new ProgrammaticAgentRegistry({
|
|
712
|
-
backend,
|
|
713
|
-
writeOutbound: rec.fn,
|
|
714
|
-
writeThread: async (t) => {
|
|
715
|
-
threads.threads.push(t);
|
|
716
|
-
// Mirror the transport's persistence: an end record with a session sets it; a start
|
|
717
|
-
// or a sessionless end leaves the prior value (the transport preserves single-threaded).
|
|
718
|
-
if (t.phase !== "start" && t.session) stored = t.session;
|
|
719
|
-
},
|
|
720
|
-
readSession: async () => stored,
|
|
721
|
-
});
|
|
722
|
-
await reg.register(specFor("eng")); // single-threaded (default).
|
|
723
|
-
|
|
724
|
-
// Turn 1 — fails before establishing a session.
|
|
725
|
-
reg.enqueue("eng", { content: "boom" });
|
|
726
|
-
await until(() => threads.ends().length === 1);
|
|
727
|
-
// The error end-record carries NO session (claude echoed none) — so the note stays clean.
|
|
728
|
-
expect(threads.ends()[0]!.status).toBe("error");
|
|
729
|
-
expect(threads.ends()[0]!.session).toBeUndefined();
|
|
730
|
-
expect(stored).toBeUndefined(); // nothing persisted → no phantom to --resume.
|
|
731
|
-
|
|
732
|
-
// Turn 2 — readSession finds no session → a FRESH {resume:false} create (self-heal,
|
|
733
|
-
// NOT a brick). The next turn is a clean new conversation, not a doomed --resume.
|
|
734
|
-
reg.enqueue("eng", { content: "again" });
|
|
735
|
-
await until(() => backend.calls.length === 2);
|
|
736
|
-
expect(backend.calls[1]!.session.resume).toBe(false);
|
|
737
|
-
expect(backend.calls[1]!.session.id).toMatch(/^[0-9a-f-]{36}$/);
|
|
738
|
-
});
|
|
739
|
-
});
|
|
740
|
-
|
|
741
|
-
describe("ProgrammaticAgentRegistry — outbound retry on transient failure (FIX 1, PR #3)", () => {
|
|
742
|
-
test("isTransientOutboundError: 5xx + network = transient; 4xx = permanent", () => {
|
|
743
|
-
expect(isTransientOutboundError(new Error("write reply failed (502) boom"))).toBe(true);
|
|
744
|
-
expect(isTransientOutboundError(new Error("write reply failed (503)"))).toBe(true);
|
|
745
|
-
expect(isTransientOutboundError(new Error("ECONNREFUSED"))).toBe(true); // no status → network.
|
|
746
|
-
expect(isTransientOutboundError(new Error("fetch failed"))).toBe(true);
|
|
747
|
-
expect(isTransientOutboundError(new Error("write reply failed (400) bad"))).toBe(false);
|
|
748
|
-
expect(isTransientOutboundError(new Error("write reply failed (401)"))).toBe(false);
|
|
749
|
-
expect(isTransientOutboundError(new Error("write reply failed (409)"))).toBe(false);
|
|
750
|
-
});
|
|
751
|
-
|
|
752
|
-
test("a transient-then-success outbound RETRIES and the reply LANDS (no loss, turn not re-run)", async () => {
|
|
753
|
-
const backend = new FakeBackend();
|
|
754
|
-
const threads = threadRecorder();
|
|
755
|
-
// Fail twice with a transient (5xx) error, then succeed — the retry must land the reply.
|
|
756
|
-
let attempts = 0;
|
|
757
|
-
const recorded: { reply: string }[] = [];
|
|
758
|
-
const flakyWriteOutbound: WriteOutbound = async (_channel, reply) => {
|
|
759
|
-
attempts++;
|
|
760
|
-
if (attempts <= 2) throw new Error("vault transport: write reply failed (502) blip");
|
|
761
|
-
recorded.push({ reply });
|
|
762
|
-
};
|
|
763
|
-
const reg = new ProgrammaticAgentRegistry({
|
|
764
|
-
backend,
|
|
765
|
-
writeOutbound: flakyWriteOutbound,
|
|
766
|
-
writeThread: threads.fn,
|
|
767
|
-
outboundRetryBaseMs: 0,
|
|
768
|
-
});
|
|
769
|
-
await reg.register(specFor("eng"));
|
|
770
|
-
|
|
771
|
-
reg.enqueue("eng", { content: "important" });
|
|
772
|
-
await until(() => recorded.length === 1);
|
|
773
|
-
await new Promise<void>((r) => setTimeout(r, 5));
|
|
774
|
-
|
|
775
|
-
// The reply landed on the 3rd attempt (1 initial + 2 retries == OUTBOUND_MAX_RETRIES).
|
|
776
|
-
expect(attempts).toBe(1 + OUTBOUND_MAX_RETRIES);
|
|
777
|
-
expect(recorded).toEqual([{ reply: "reply:important" }]);
|
|
778
|
-
// The backend ran the turn EXACTLY ONCE (no re-run / fork on the retry).
|
|
779
|
-
expect(backend.calls).toHaveLength(1);
|
|
780
|
-
// The FINAL thread note is the single `ok` record (the reply was ultimately delivered) —
|
|
781
|
-
// no error re-record because delivery succeeded. (A working-ensure preceded it.)
|
|
782
|
-
expect(threads.starts()).toHaveLength(1);
|
|
783
|
-
expect(threads.ends()).toHaveLength(1);
|
|
784
|
-
expect(threads.ends()[0]!.status).toBe("ok");
|
|
785
|
-
});
|
|
786
|
-
|
|
787
|
-
test("a PERSISTENT failure surfaces an error event + re-records the thread as error + does NOT claim success", async () => {
|
|
788
|
-
const backend = new FakeBackend();
|
|
789
|
-
const threads = threadRecorder();
|
|
790
|
-
const turn = turnRecorder();
|
|
791
|
-
let attempts = 0;
|
|
792
|
-
const alwaysFail: WriteOutbound = async () => {
|
|
793
|
-
attempts++;
|
|
794
|
-
throw new Error("vault transport: write reply failed (503) down");
|
|
795
|
-
};
|
|
796
|
-
const reg = new ProgrammaticAgentRegistry({
|
|
797
|
-
backend,
|
|
798
|
-
writeOutbound: alwaysFail,
|
|
799
|
-
writeThread: threads.fn,
|
|
800
|
-
onTurnEvent: turn.fn,
|
|
801
|
-
outboundRetryBaseMs: 0,
|
|
802
|
-
});
|
|
803
|
-
await reg.register(specFor("eng"));
|
|
804
|
-
|
|
805
|
-
reg.enqueue("eng", { content: "doomed" });
|
|
806
|
-
await until(() => threads.ends().length === 2);
|
|
807
|
-
await new Promise<void>((r) => setTimeout(r, 5));
|
|
808
|
-
|
|
809
|
-
// Retried the full budget then gave up (1 + OUTBOUND_MAX_RETRIES).
|
|
810
|
-
expect(attempts).toBe(1 + OUTBOUND_MAX_RETRIES);
|
|
811
|
-
// The live view resolved to ERROR (not `done`) — no silently-vanished reply.
|
|
812
|
-
const errorEvents = turn.events.filter((e) => e.event.kind === "error");
|
|
813
|
-
expect(errorEvents.length).toBeGreaterThanOrEqual(1);
|
|
814
|
-
expect(turn.events.some((e) => e.event.kind === "done")).toBe(false);
|
|
815
|
-
// The FINAL thread record does NOT falsely claim a clean ok: the second final record is
|
|
816
|
-
// error, carrying the un-delivered reply text for recovery. (A working-ensure preceded.)
|
|
817
|
-
expect(threads.ends()).toHaveLength(2);
|
|
818
|
-
expect(threads.ends()[1]!.status).toBe("error");
|
|
819
|
-
expect(threads.ends()[1]!.output).toContain("reply:doomed");
|
|
820
|
-
});
|
|
821
|
-
|
|
822
|
-
test("a PERMANENT (4xx) outbound failure does NOT retry — gives up immediately", async () => {
|
|
823
|
-
const backend = new FakeBackend();
|
|
824
|
-
const threads = threadRecorder();
|
|
825
|
-
let attempts = 0;
|
|
826
|
-
const reject4xx: WriteOutbound = async () => {
|
|
827
|
-
attempts++;
|
|
828
|
-
throw new Error("vault transport: write reply failed (400) bad request");
|
|
829
|
-
};
|
|
830
|
-
const reg = new ProgrammaticAgentRegistry({
|
|
831
|
-
backend,
|
|
832
|
-
writeOutbound: reject4xx,
|
|
833
|
-
writeThread: threads.fn,
|
|
834
|
-
outboundRetryBaseMs: 0,
|
|
835
|
-
});
|
|
836
|
-
await reg.register(specFor("eng"));
|
|
837
|
-
|
|
838
|
-
reg.enqueue("eng", { content: "rejected" });
|
|
839
|
-
await until(() => threads.ends().length === 2);
|
|
840
|
-
await new Promise<void>((r) => setTimeout(r, 5));
|
|
841
|
-
|
|
842
|
-
// A 4xx is a real rejection → exactly ONE attempt, no retry.
|
|
843
|
-
expect(attempts).toBe(1);
|
|
844
|
-
// The second FINAL record re-records the turn as error (the un-delivered reply).
|
|
845
|
-
expect(threads.ends()).toHaveLength(2);
|
|
846
|
-
expect(threads.ends()[1]!.status).toBe("error");
|
|
847
|
-
});
|
|
848
|
-
});
|
|
849
|
-
|
|
850
|
-
describe("ProgrammaticAgentRegistry — serial queue (the hard invariant)", () => {
|
|
851
|
-
test("two inbounds during a running turn are processed ONE AT A TIME, FIFO, never concurrent", async () => {
|
|
852
|
-
const backend = new FakeBackend();
|
|
853
|
-
const gate = deferred<void>();
|
|
854
|
-
backend.gate = { promise: gate.promise, resolve: gate.resolve };
|
|
855
|
-
const rec = recorder();
|
|
856
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
|
|
857
|
-
await reg.register(specFor("eng"));
|
|
858
|
-
|
|
859
|
-
// First enqueue starts a turn that blocks on the gate.
|
|
860
|
-
reg.enqueue("eng", { content: "m1" });
|
|
861
|
-
await until(() => backend.calls.length === 1);
|
|
862
|
-
expect(reg.statusOf("eng").state).toBe("working");
|
|
863
|
-
|
|
864
|
-
// Two more arrive WHILE the first turn is in flight — they queue.
|
|
865
|
-
reg.enqueue("eng", { content: "m2" });
|
|
866
|
-
reg.enqueue("eng", { content: "m3" });
|
|
867
|
-
// Still exactly one call has STARTED (the gate holds the first; the others wait).
|
|
868
|
-
expect(backend.calls).toHaveLength(1);
|
|
869
|
-
expect(reg.statusOf("eng")).toEqual({ state: "queued", queued: 2 });
|
|
870
|
-
|
|
871
|
-
// Release the gate: the worker drains m1, then m2, then m3 in order. Because the
|
|
872
|
-
// gate's promise is already resolved, subsequent turns don't block.
|
|
873
|
-
gate.resolve();
|
|
874
|
-
await until(() => rec.calls.length === 3);
|
|
875
|
-
|
|
876
|
-
expect(backend.calls.map((c) => c.message)).toEqual(["m1", "m2", "m3"]);
|
|
877
|
-
expect(rec.calls.map((c) => c.reply)).toEqual(["reply:m1", "reply:m2", "reply:m3"]);
|
|
878
|
-
// The invariant: never two concurrent turns for the same channel.
|
|
879
|
-
expect(backend.maxConcurrent).toBe(1);
|
|
880
|
-
// Queue fully drained → idle.
|
|
881
|
-
expect(reg.statusOf("eng").state).toBe("idle");
|
|
882
|
-
});
|
|
883
|
-
|
|
884
|
-
test("statusOf — idle with no work, working with one in flight, queued:N with a backlog", async () => {
|
|
885
|
-
const backend = new FakeBackend();
|
|
886
|
-
const gate = deferred<void>();
|
|
887
|
-
backend.gate = { promise: gate.promise, resolve: gate.resolve };
|
|
888
|
-
const rec = recorder();
|
|
889
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
|
|
890
|
-
await reg.register(specFor("eng"));
|
|
891
|
-
expect(reg.statusOf("eng")).toEqual({ state: "idle", queued: 0 });
|
|
892
|
-
|
|
893
|
-
reg.enqueue("eng", { content: "a" });
|
|
894
|
-
await until(() => backend.calls.length === 1);
|
|
895
|
-
expect(reg.statusOf("eng")).toEqual({ state: "working", queued: 0 });
|
|
896
|
-
|
|
897
|
-
reg.enqueue("eng", { content: "b" });
|
|
898
|
-
expect(reg.statusOf("eng")).toEqual({ state: "queued", queued: 1 });
|
|
899
|
-
|
|
900
|
-
gate.resolve();
|
|
901
|
-
await until(() => rec.calls.length === 2);
|
|
902
|
-
expect(reg.statusOf("eng")).toEqual({ state: "idle", queued: 0 });
|
|
903
|
-
});
|
|
904
|
-
});
|
|
905
|
-
|
|
906
|
-
describe("ProgrammaticAgentRegistry — streaming turn view (onTurnEvent)", () => {
|
|
907
|
-
test("forwards the backend's interim events + a final 'done' (keyed by channel)", async () => {
|
|
908
|
-
const backend = new FakeBackend();
|
|
909
|
-
backend.interimToEmit = [
|
|
910
|
-
{ kind: "init", sessionId: "s-1" },
|
|
911
|
-
{ kind: "text", text: "thinking…" },
|
|
912
|
-
{ kind: "tool", tool: "Read" },
|
|
913
|
-
];
|
|
914
|
-
backend.resultFor = () => ({ ok: true, reply: "final answer" });
|
|
915
|
-
const rec = recorder();
|
|
916
|
-
const turns = turnRecorder();
|
|
917
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, onTurnEvent: turns.fn });
|
|
918
|
-
await reg.register(specFor("eng"));
|
|
919
|
-
|
|
920
|
-
reg.enqueue("eng", { content: "hi" });
|
|
921
|
-
await until(() => rec.calls.length === 1);
|
|
922
|
-
// Let the trailing 'done' (emitted after the outbound write) land.
|
|
923
|
-
await until(() => turns.events.some((e) => e.event.kind === "done"));
|
|
924
|
-
|
|
925
|
-
expect(turns.events.map((e) => e.channel)).toEqual(["eng", "eng", "eng", "eng"]);
|
|
926
|
-
expect(turns.events.map((e) => e.event)).toEqual([
|
|
927
|
-
{ kind: "init", sessionId: "s-1" },
|
|
928
|
-
{ kind: "text", text: "thinking…" },
|
|
929
|
-
{ kind: "tool", tool: "Read" },
|
|
930
|
-
{ kind: "done", reply: "final answer" },
|
|
931
|
-
]);
|
|
932
|
-
// The durable outbound write is unchanged by the live view.
|
|
933
|
-
expect(rec.calls).toEqual([{ channel: "eng", reply: "final answer" }]);
|
|
934
|
-
});
|
|
935
|
-
|
|
936
|
-
test("an ok:false turn emits a 'error' lifecycle event (no stuck working state)", async () => {
|
|
937
|
-
const backend = new FakeBackend();
|
|
938
|
-
backend.resultFor = () => ({ ok: false, error: "mint refused" });
|
|
939
|
-
const rec = recorder();
|
|
940
|
-
const turns = turnRecorder();
|
|
941
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, onTurnEvent: turns.fn });
|
|
942
|
-
await reg.register(specFor("eng"));
|
|
943
|
-
|
|
944
|
-
reg.enqueue("eng", { content: "x" });
|
|
945
|
-
await until(() => rec.calls.length === 1);
|
|
946
|
-
|
|
947
|
-
expect(turns.events).toEqual([{ channel: "eng", event: { kind: "error", error: "mint refused" } }]);
|
|
948
|
-
// The failed turn ALSO posts a user-facing failure note (carrying the reason).
|
|
949
|
-
expect(rec.calls).toHaveLength(1);
|
|
950
|
-
expect(rec.calls[0]!.reply).toContain("mint refused");
|
|
951
|
-
});
|
|
952
|
-
|
|
953
|
-
test("a backend THROW also emits 'error' (the defensive catch resolves the live view)", async () => {
|
|
954
|
-
const backend = new FakeBackend();
|
|
955
|
-
backend.throwOnce = new Error("boom");
|
|
956
|
-
const rec = recorder();
|
|
957
|
-
const turns = turnRecorder();
|
|
958
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, onTurnEvent: turns.fn });
|
|
959
|
-
await reg.register(specFor("eng"));
|
|
960
|
-
|
|
961
|
-
reg.enqueue("eng", { content: "x" });
|
|
962
|
-
await until(() => turns.events.some((e) => e.event.kind === "error"));
|
|
963
|
-
|
|
964
|
-
expect(turns.events).toEqual([{ channel: "eng", event: { kind: "error", error: "boom" } }]);
|
|
965
|
-
});
|
|
966
|
-
|
|
967
|
-
test("an empty reply still emits 'done' (with reply '') so the live view finalizes", async () => {
|
|
968
|
-
const backend = new FakeBackend();
|
|
969
|
-
backend.resultFor = () => ({ ok: true, reply: "" });
|
|
970
|
-
const rec = recorder();
|
|
971
|
-
const turns = turnRecorder();
|
|
972
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, onTurnEvent: turns.fn });
|
|
973
|
-
await reg.register(specFor("eng"));
|
|
974
|
-
|
|
975
|
-
reg.enqueue("eng", { content: "tool-only" });
|
|
976
|
-
await until(() => turns.events.some((e) => e.event.kind === "done"));
|
|
977
|
-
|
|
978
|
-
expect(turns.events).toEqual([{ channel: "eng", event: { kind: "done", reply: "" } }]);
|
|
979
|
-
// No durable note for an empty reply (the existing contract), but the view finalizes.
|
|
980
|
-
expect(rec.calls).toHaveLength(0);
|
|
981
|
-
});
|
|
982
|
-
|
|
983
|
-
test("a throwing sink can't break the worker (the durable write still lands)", async () => {
|
|
984
|
-
const backend = new FakeBackend();
|
|
985
|
-
backend.interimToEmit = [{ kind: "text", text: "hi" }];
|
|
986
|
-
const rec = recorder();
|
|
987
|
-
const reg = new ProgrammaticAgentRegistry({
|
|
988
|
-
backend,
|
|
989
|
-
writeOutbound: rec.fn,
|
|
990
|
-
onTurnEvent: () => {
|
|
991
|
-
throw new Error("dead stream");
|
|
992
|
-
},
|
|
993
|
-
});
|
|
994
|
-
await reg.register(specFor("eng"));
|
|
995
|
-
|
|
996
|
-
reg.enqueue("eng", { content: "hi" });
|
|
997
|
-
await until(() => rec.calls.length === 1);
|
|
998
|
-
expect(rec.calls).toEqual([{ channel: "eng", reply: "reply:hi" }]);
|
|
999
|
-
});
|
|
1000
|
-
|
|
1001
|
-
test("with NO sink wired, turns run exactly as before (no throw, durable write lands)", async () => {
|
|
1002
|
-
const backend = new FakeBackend();
|
|
1003
|
-
backend.interimToEmit = [{ kind: "text", text: "ignored" }];
|
|
1004
|
-
const rec = recorder();
|
|
1005
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
|
|
1006
|
-
await reg.register(specFor("eng"));
|
|
1007
|
-
|
|
1008
|
-
reg.enqueue("eng", { content: "hi" });
|
|
1009
|
-
await until(() => rec.calls.length === 1);
|
|
1010
|
-
expect(rec.calls).toEqual([{ channel: "eng", reply: "reply:hi" }]);
|
|
1011
|
-
});
|
|
1012
|
-
});
|
|
1013
|
-
|
|
1014
|
-
describe("ProgrammaticAgentRegistry — pending-inbound queue + replay-on-register (agent#121)", () => {
|
|
1015
|
-
test("an inbound for an EXPECTED-but-not-yet-registered channel is QUEUED pending (not dropped)", () => {
|
|
1016
|
-
const backend = new FakeBackend();
|
|
1017
|
-
const rec = recorder();
|
|
1018
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
|
|
1019
|
-
|
|
1020
|
-
// No live agent yet — but the channel is EXPECTED (the def-instantiation path marked it
|
|
1021
|
-
// before bringing the agent up). enqueue() would no-op false here; queuePending OWNS it.
|
|
1022
|
-
reg.expectChannel("eng");
|
|
1023
|
-
expect(reg.hasChannel("eng")).toBe(false);
|
|
1024
|
-
expect(reg.enqueue("eng", { content: "early" })).toBe(false); // not live → enqueue declines.
|
|
1025
|
-
expect(reg.queuePending("eng", { content: "early" })).toBe("queued");
|
|
1026
|
-
expect(reg.pendingCount("eng")).toBe(1);
|
|
1027
|
-
// Nothing ran yet (no live agent), but nothing was lost either.
|
|
1028
|
-
expect(backend.calls).toHaveLength(0);
|
|
1029
|
-
});
|
|
1030
|
-
|
|
1031
|
-
test("queuePending for a genuinely UNKNOWN channel (not expected) returns 'unknown' (caller logs+drops)", () => {
|
|
1032
|
-
const backend = new FakeBackend();
|
|
1033
|
-
const rec = recorder();
|
|
1034
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
|
|
1035
|
-
// Never expected, never registered → nothing maps to it.
|
|
1036
|
-
expect(reg.queuePending("ghost", { content: "x" })).toBe("unknown");
|
|
1037
|
-
expect(reg.pendingCount("ghost")).toBe(0);
|
|
1038
|
-
});
|
|
1039
|
-
|
|
1040
|
-
test("on register() the channel's pending queue DRAINS into the serial worker, in arrival order (FIFO)", async () => {
|
|
1041
|
-
const backend = new FakeBackend();
|
|
1042
|
-
const rec = recorder();
|
|
1043
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
|
|
1044
|
-
|
|
1045
|
-
// Three inbound arrive BEFORE the agent is live — all buffered pending.
|
|
1046
|
-
reg.expectChannel("eng");
|
|
1047
|
-
expect(reg.queuePending("eng", { content: "first" })).toBe("queued");
|
|
1048
|
-
expect(reg.queuePending("eng", { content: "second" })).toBe("queued");
|
|
1049
|
-
expect(reg.queuePending("eng", { content: "third" })).toBe("queued");
|
|
1050
|
-
expect(reg.pendingCount("eng")).toBe(3);
|
|
1051
|
-
|
|
1052
|
-
// The agent registers → the buffer replays through the normal serial path, FIFO.
|
|
1053
|
-
await reg.register(specFor("eng"));
|
|
1054
|
-
await until(() => rec.calls.length === 3);
|
|
1055
|
-
|
|
1056
|
-
// The buffer is drained + the EXPECTED mark cleared (the live index is the truth now).
|
|
1057
|
-
expect(reg.pendingCount("eng")).toBe(0);
|
|
1058
|
-
expect(reg.isExpected("eng")).toBe(false);
|
|
1059
|
-
// Turns ran in arrival order (the serial worker drains FIFO).
|
|
1060
|
-
expect(backend.calls.map((c) => c.message)).toEqual(["first", "second", "third"]);
|
|
1061
|
-
expect(rec.calls.map((c) => c.reply)).toEqual(["reply:first", "reply:second", "reply:third"]);
|
|
1062
|
-
});
|
|
1063
|
-
|
|
1064
|
-
test("the pending buffer is CAPPED — past the cap the OLDEST is evicted (FIFO), newest kept", () => {
|
|
1065
|
-
const backend = new FakeBackend();
|
|
1066
|
-
const rec = recorder();
|
|
1067
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
|
|
1068
|
-
|
|
1069
|
-
reg.expectChannel("eng");
|
|
1070
|
-
// Fill to the cap, then push one MORE — the oldest ("m0") is evicted.
|
|
1071
|
-
for (let i = 0; i < PENDING_INBOUND_CAP; i++) {
|
|
1072
|
-
expect(reg.queuePending("eng", { content: `m${i}` })).toBe("queued");
|
|
1073
|
-
}
|
|
1074
|
-
expect(reg.pendingCount("eng")).toBe(PENDING_INBOUND_CAP);
|
|
1075
|
-
expect(reg.queuePending("eng", { content: "overflow" })).toBe("queued");
|
|
1076
|
-
// Still capped (didn't grow past the cap).
|
|
1077
|
-
expect(reg.pendingCount("eng")).toBe(PENDING_INBOUND_CAP);
|
|
1078
|
-
});
|
|
1079
|
-
|
|
1080
|
-
test("an UNKNOWN channel queuePending does NOT crash + leaves the registry usable", () => {
|
|
1081
|
-
const backend = new FakeBackend();
|
|
1082
|
-
const rec = recorder();
|
|
1083
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
|
|
1084
|
-
expect(() => reg.queuePending("ghost", { content: "x" })).not.toThrow();
|
|
1085
|
-
// The registry still works for a real registration afterward.
|
|
1086
|
-
expect(reg.queuePending("ghost", { content: "y" })).toBe("unknown");
|
|
1087
|
-
});
|
|
1088
|
-
|
|
1089
|
-
test("register() clears the EXPECTED mark even with an EMPTY pending buffer", async () => {
|
|
1090
|
-
const backend = new FakeBackend();
|
|
1091
|
-
const rec = recorder();
|
|
1092
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
|
|
1093
|
-
reg.expectChannel("eng");
|
|
1094
|
-
expect(reg.isExpected("eng")).toBe(true);
|
|
1095
|
-
await reg.register(specFor("eng"));
|
|
1096
|
-
expect(reg.isExpected("eng")).toBe(false);
|
|
1097
|
-
expect(reg.pendingCount("eng")).toBe(0);
|
|
1098
|
-
});
|
|
1099
|
-
|
|
1100
|
-
test("unexpectChannel drops a stale EXPECTED mark + its buffered pending (teardown)", () => {
|
|
1101
|
-
const backend = new FakeBackend();
|
|
1102
|
-
const rec = recorder();
|
|
1103
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
|
|
1104
|
-
reg.expectChannel("eng");
|
|
1105
|
-
reg.queuePending("eng", { content: "stranded" });
|
|
1106
|
-
expect(reg.pendingCount("eng")).toBe(1);
|
|
1107
|
-
reg.unexpectChannel("eng");
|
|
1108
|
-
expect(reg.isExpected("eng")).toBe(false);
|
|
1109
|
-
expect(reg.pendingCount("eng")).toBe(0);
|
|
1110
|
-
// A subsequent inbound for the now-unexpected channel is 'unknown' (correctly dropped).
|
|
1111
|
-
expect(reg.queuePending("eng", { content: "after" })).toBe("unknown");
|
|
1112
|
-
});
|
|
1113
|
-
|
|
1114
|
-
test("a channel-move re-register clears the OLD channel's expected mark + pending buffer (no leak)", async () => {
|
|
1115
|
-
const backend = new FakeBackend();
|
|
1116
|
-
const rec = recorder();
|
|
1117
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
|
|
1118
|
-
|
|
1119
|
-
// Register the agent on channel "old", then buffer a pending inbound for "old".
|
|
1120
|
-
await reg.register({ name: "mover", channels: ["old"] });
|
|
1121
|
-
reg.expectChannel("old");
|
|
1122
|
-
reg.queuePending("old", { content: "stranded" });
|
|
1123
|
-
expect(reg.pendingCount("old")).toBe(1);
|
|
1124
|
-
|
|
1125
|
-
// Re-register the SAME name onto a DIFFERENT wake channel — the old channel's indexes,
|
|
1126
|
-
// expected mark, and pending buffer must all be dropped (nothing routes to "old" now).
|
|
1127
|
-
await reg.register({ name: "mover", channels: ["new"] });
|
|
1128
|
-
expect(reg.hasChannel("old")).toBe(false);
|
|
1129
|
-
expect(reg.isExpected("old")).toBe(false);
|
|
1130
|
-
expect(reg.pendingCount("old")).toBe(0);
|
|
1131
|
-
expect(reg.hasChannel("new")).toBe(true);
|
|
1132
|
-
});
|
|
1133
|
-
|
|
1134
|
-
test("a pending inbound that arrives DURING a drain (after register) still replays in order", async () => {
|
|
1135
|
-
const backend = new FakeBackend();
|
|
1136
|
-
const rec = recorder();
|
|
1137
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn });
|
|
1138
|
-
|
|
1139
|
-
// Two pending before register.
|
|
1140
|
-
reg.expectChannel("eng");
|
|
1141
|
-
reg.queuePending("eng", { content: "p1" });
|
|
1142
|
-
reg.queuePending("eng", { content: "p2" });
|
|
1143
|
-
|
|
1144
|
-
await reg.register(specFor("eng"));
|
|
1145
|
-
// After register the channel is LIVE — a further inbound goes through enqueue directly.
|
|
1146
|
-
await until(() => rec.calls.length === 2);
|
|
1147
|
-
expect(reg.enqueue("eng", { content: "p3" })).toBe(true);
|
|
1148
|
-
await until(() => rec.calls.length === 3);
|
|
1149
|
-
|
|
1150
|
-
expect(backend.calls.map((c) => c.message)).toEqual(["p1", "p2", "p3"]);
|
|
1151
|
-
});
|
|
1152
|
-
});
|
|
1153
|
-
|
|
1154
|
-
describe("ProgrammaticAgentRegistry — thread-as-container working-ensure (Part B)", () => {
|
|
1155
|
-
test("the working-ensure (phase:start, status:working) is written BEFORE deliver() runs", async () => {
|
|
1156
|
-
const backend = new FakeBackend();
|
|
1157
|
-
// Gate the turn so we can observe the working-ensure write WHILE the turn is in flight.
|
|
1158
|
-
const gate = deferred<void>();
|
|
1159
|
-
backend.gate = { promise: gate.promise, resolve: gate.resolve };
|
|
1160
|
-
const rec = recorder();
|
|
1161
|
-
const threads = threadRecorder();
|
|
1162
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, writeThread: threads.fn });
|
|
1163
|
-
await reg.register(specFor("eng"));
|
|
1164
|
-
|
|
1165
|
-
reg.enqueue("eng", { content: "do work" });
|
|
1166
|
-
// Wait until deliver() is in flight (the turn was handed the message + is blocked on the
|
|
1167
|
-
// gate). The start-ensure is `await`ed BEFORE deliver() in the drain, so by the time
|
|
1168
|
-
// deliver() has been called the working-ensure MUST already be written — and the FINAL
|
|
1169
|
-
// (end) record must NOT be (the turn hasn't completed). This proves the ordering:
|
|
1170
|
-
// working-ensure strictly precedes the turn.
|
|
1171
|
-
await until(() => backend.calls.length === 1);
|
|
1172
|
-
expect(threads.starts()).toHaveLength(1);
|
|
1173
|
-
expect(threads.starts()[0]!.status).toBe("working");
|
|
1174
|
-
expect(threads.starts()[0]!.output).toBe(""); // NO fake reply while working.
|
|
1175
|
-
expect(threads.starts()[0]!.input).toBe("do work");
|
|
1176
|
-
// The turn is in flight but hasn't produced its end record yet (gated).
|
|
1177
|
-
expect(backend.calls).toHaveLength(1);
|
|
1178
|
-
expect(threads.ends()).toHaveLength(0);
|
|
1179
|
-
|
|
1180
|
-
// Release the turn → it completes → the FINAL (ok) record lands, same threadId.
|
|
1181
|
-
gate.resolve();
|
|
1182
|
-
await until(() => threads.ends().length === 1);
|
|
1183
|
-
expect(threads.ends()[0]!.status).toBe("ok");
|
|
1184
|
-
expect(threads.ends()[0]!.threadId).toBe(threads.starts()[0]!.threadId!);
|
|
1185
|
-
});
|
|
1186
|
-
|
|
1187
|
-
test("the start-ensure does NOT write a fake reply even though the turn ultimately replies", async () => {
|
|
1188
|
-
const backend = new FakeBackend();
|
|
1189
|
-
const rec = recorder();
|
|
1190
|
-
const threads = threadRecorder();
|
|
1191
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: rec.fn, writeThread: threads.fn });
|
|
1192
|
-
await reg.register(specFor("eng"));
|
|
1193
|
-
|
|
1194
|
-
reg.enqueue("eng", { content: "hi" });
|
|
1195
|
-
await until(() => threads.ends().length === 1);
|
|
1196
|
-
// The working-ensure carries status:working + empty output; the end carries the reply.
|
|
1197
|
-
expect(threads.starts()[0]!.status).toBe("working");
|
|
1198
|
-
expect(threads.starts()[0]!.output).toBe("");
|
|
1199
|
-
expect(threads.ends()[0]!.output).toBe("reply:hi");
|
|
1200
|
-
});
|
|
1201
|
-
|
|
1202
|
-
test("the OUTBOUND reply is stamped with the turn's thread id (definition→thread→message link); multi-threaded → the per-fire note leaf", async () => {
|
|
1203
|
-
const backend = new FakeBackend();
|
|
1204
|
-
const threads = threadRecorder();
|
|
1205
|
-
// A recorder that ALSO captures the threadId the worker passes to writeOutbound.
|
|
1206
|
-
const outbound: { reply: string; threadId?: string }[] = [];
|
|
1207
|
-
const writeOutbound: WriteOutbound = async (_channel, reply, _inReplyTo, threadId) => {
|
|
1208
|
-
outbound.push({ reply, ...(threadId ? { threadId } : {}) });
|
|
1209
|
-
};
|
|
1210
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound, writeThread: threads.fn });
|
|
1211
|
-
await reg.register(specMultiThreaded("digest"));
|
|
1212
|
-
|
|
1213
|
-
reg.enqueue("digest", { content: "go" });
|
|
1214
|
-
await until(() => outbound.length === 1);
|
|
1215
|
-
// The outbound carries the per-turn thread id, which for multi-threaded equals the
|
|
1216
|
-
// per-fire thread note's leaf — the explicit message↔thread link.
|
|
1217
|
-
expect(outbound[0]!.threadId).toBeDefined();
|
|
1218
|
-
expect(outbound[0]!.threadId).toBe(threads.ends()[0]!.threadId!);
|
|
1219
|
-
});
|
|
1220
|
-
});
|
|
1221
|
-
|
|
1222
|
-
// ───────────────────────────────────────────────────────────────────────────────
|
|
1223
|
-
// AGENT-TO-AGENT CALLBACK ROUTING ("reply_to") — design 2026-06-20-agent-callbacks.md.
|
|
1224
|
-
// A FAKE WriteCallback recorder captures every callback the drain delivers (its target
|
|
1225
|
-
// channel + content + the metadata contract), so we can assert: a reply_to message gets
|
|
1226
|
-
// exactly one callback w/ the right metadata; a no-reply_to message gets none; ok AND error
|
|
1227
|
-
// both fire; the depth guard suppresses; and N callbacks to one channel drain FIFO + none
|
|
1228
|
-
// is lost (the orchestrator-resume concurrency story).
|
|
1229
|
-
// ───────────────────────────────────────────────────────────────────────────────
|
|
1230
|
-
|
|
1231
|
-
/** A recorder WriteCallback — captures every callback the registry delivers, in order. */
|
|
1232
|
-
function callbackRecorder(): {
|
|
1233
|
-
calls: { channel: string; content: string; meta: CallbackMeta }[];
|
|
1234
|
-
fn: WriteCallback;
|
|
1235
|
-
} {
|
|
1236
|
-
const calls: { channel: string; content: string; meta: CallbackMeta }[] = [];
|
|
1237
|
-
const fn: WriteCallback = async (channel, content, meta) => {
|
|
1238
|
-
calls.push({ channel, content, meta });
|
|
1239
|
-
};
|
|
1240
|
-
return { calls, fn };
|
|
1241
|
-
}
|
|
1242
|
-
|
|
1243
|
-
/** A WriteOutbound that returns a deterministic note id, so source_message is assertable. */
|
|
1244
|
-
function recorderWithId(noteId = "outbound-note-1"): {
|
|
1245
|
-
calls: { channel: string; reply: string }[];
|
|
1246
|
-
fn: WriteOutbound;
|
|
1247
|
-
} {
|
|
1248
|
-
const calls: { channel: string; reply: string }[] = [];
|
|
1249
|
-
const fn: WriteOutbound = async (channel, reply) => {
|
|
1250
|
-
calls.push({ channel, reply });
|
|
1251
|
-
return { id: noteId };
|
|
1252
|
-
};
|
|
1253
|
-
return { calls, fn };
|
|
1254
|
-
}
|
|
1255
|
-
|
|
1256
|
-
describe("ProgrammaticAgentRegistry — agent-to-agent callbacks (reply_to)", () => {
|
|
1257
|
-
test("an inbound WITH reply_to → exactly ONE callback to the reply_to channel with the full metadata contract (ok)", async () => {
|
|
1258
|
-
const backend = new FakeBackend();
|
|
1259
|
-
backend.resultFor = (m) => ({ ok: true, reply: "done:" + m });
|
|
1260
|
-
const out = recorderWithId("reply-note-42");
|
|
1261
|
-
const cb = callbackRecorder();
|
|
1262
|
-
const reg = new ProgrammaticAgentRegistry({
|
|
1263
|
-
backend,
|
|
1264
|
-
writeOutbound: out.fn,
|
|
1265
|
-
writeCallback: cb.fn,
|
|
1266
|
-
});
|
|
1267
|
-
await reg.register(specFor("worker"));
|
|
1268
|
-
|
|
1269
|
-
reg.enqueue("worker", {
|
|
1270
|
-
content: "sub-task",
|
|
1271
|
-
inReplyTo: "inbound-note-7",
|
|
1272
|
-
replyTo: "orchestrator",
|
|
1273
|
-
correlationId: "corr-abc",
|
|
1274
|
-
delegationDepth: 2,
|
|
1275
|
-
});
|
|
1276
|
-
await until(() => cb.calls.length === 1);
|
|
1277
|
-
// Let any erroneous SECOND callback land, then assert there was exactly one.
|
|
1278
|
-
await new Promise<void>((r) => setTimeout(r, 5));
|
|
1279
|
-
expect(cb.calls).toHaveLength(1);
|
|
1280
|
-
|
|
1281
|
-
const { channel, content, meta } = cb.calls[0]!;
|
|
1282
|
-
expect(channel).toBe("orchestrator"); // delivered to the SENDER's channel.
|
|
1283
|
-
expect(content).toContain("[callback]");
|
|
1284
|
-
expect(content).not.toContain("done:sub-task"); // summary + link, NOT the full reply.
|
|
1285
|
-
expect(meta.callback).toBe("true");
|
|
1286
|
-
expect(meta.status).toBe("ok");
|
|
1287
|
-
expect(meta.source_channel).toBe("worker");
|
|
1288
|
-
expect(meta.source_message).toBe("reply-note-42"); // the delivered outbound note id.
|
|
1289
|
-
expect(meta.source_thread).toBeDefined(); // the per-turn thread id (pull link).
|
|
1290
|
-
expect(meta.correlation_id).toBe("corr-abc"); // echoed verbatim.
|
|
1291
|
-
expect(meta.delegation_depth).toBe("3"); // incoming 2 + 1 hop.
|
|
1292
|
-
// The callback note must NOT itself carry a reply_to (terminal — the loop guard).
|
|
1293
|
-
expect((meta as unknown as Record<string, unknown>).reply_to).toBeUndefined();
|
|
1294
|
-
});
|
|
1295
|
-
|
|
1296
|
-
test("an inbound WITHOUT reply_to → NO callback (a normal turn never emits one)", async () => {
|
|
1297
|
-
const backend = new FakeBackend();
|
|
1298
|
-
const out = recorderWithId();
|
|
1299
|
-
const cb = callbackRecorder();
|
|
1300
|
-
const reg = new ProgrammaticAgentRegistry({
|
|
1301
|
-
backend,
|
|
1302
|
-
writeOutbound: out.fn,
|
|
1303
|
-
writeCallback: cb.fn,
|
|
1304
|
-
});
|
|
1305
|
-
await reg.register(specFor("worker"));
|
|
1306
|
-
|
|
1307
|
-
reg.enqueue("worker", { content: "plain message" });
|
|
1308
|
-
await until(() => out.calls.length === 1);
|
|
1309
|
-
await new Promise<void>((r) => setTimeout(r, 5));
|
|
1310
|
-
expect(out.calls).toHaveLength(1); // the turn ran + replied normally,
|
|
1311
|
-
expect(cb.calls).toHaveLength(0); // but no callback fired.
|
|
1312
|
-
});
|
|
1313
|
-
|
|
1314
|
-
test("the callback fires on an ERROR turn too (status:error, no source_message)", async () => {
|
|
1315
|
-
const backend = new FakeBackend();
|
|
1316
|
-
backend.resultFor = () => ({ ok: false, error: "mint refused" });
|
|
1317
|
-
const out = recorderWithId();
|
|
1318
|
-
const cb = callbackRecorder();
|
|
1319
|
-
const reg = new ProgrammaticAgentRegistry({
|
|
1320
|
-
backend,
|
|
1321
|
-
writeOutbound: out.fn,
|
|
1322
|
-
writeCallback: cb.fn,
|
|
1323
|
-
});
|
|
1324
|
-
await reg.register(specFor("worker"));
|
|
1325
|
-
|
|
1326
|
-
reg.enqueue("worker", { content: "do it", replyTo: "orchestrator", delegationDepth: 0 });
|
|
1327
|
-
await until(() => cb.calls.length === 1);
|
|
1328
|
-
await new Promise<void>((r) => setTimeout(r, 5));
|
|
1329
|
-
expect(cb.calls).toHaveLength(1);
|
|
1330
|
-
// An error turn now posts a user-facing failure note to the worker's own channel
|
|
1331
|
-
// (in addition to the orchestrator callback) — carrying the reason.
|
|
1332
|
-
expect(out.calls).toHaveLength(1);
|
|
1333
|
-
expect(out.calls[0]!.reply).toContain("mint refused");
|
|
1334
|
-
const { meta, content } = cb.calls[0]!;
|
|
1335
|
-
expect(meta.status).toBe("error"); // but the orchestrator still learns it failed.
|
|
1336
|
-
expect(content).toContain("error");
|
|
1337
|
-
expect(meta.source_message).toBeUndefined(); // no delivered reply note.
|
|
1338
|
-
expect(meta.delegation_depth).toBe("1"); // 0 + 1.
|
|
1339
|
-
});
|
|
1340
|
-
|
|
1341
|
-
test("the callback fires when deliver() THROWS (defensive catch → status:error)", async () => {
|
|
1342
|
-
const backend = new FakeBackend();
|
|
1343
|
-
backend.throwOnce = new Error("surprise throw");
|
|
1344
|
-
const out = recorderWithId();
|
|
1345
|
-
const cb = callbackRecorder();
|
|
1346
|
-
const reg = new ProgrammaticAgentRegistry({
|
|
1347
|
-
backend,
|
|
1348
|
-
writeOutbound: out.fn,
|
|
1349
|
-
writeCallback: cb.fn,
|
|
1350
|
-
});
|
|
1351
|
-
await reg.register(specFor("worker"));
|
|
1352
|
-
|
|
1353
|
-
reg.enqueue("worker", { content: "boom", replyTo: "orchestrator" });
|
|
1354
|
-
await until(() => cb.calls.length === 1);
|
|
1355
|
-
expect(cb.calls[0]!.meta.status).toBe("error");
|
|
1356
|
-
});
|
|
1357
|
-
|
|
1358
|
-
test("the callback fires status:error when the outbound write FAILS after retries (reply produced but not delivered)", async () => {
|
|
1359
|
-
const backend = new FakeBackend();
|
|
1360
|
-
backend.resultFor = (m) => ({ ok: true, reply: "done:" + m });
|
|
1361
|
-
// Always-fail outbound (a permanent 4xx → no retry); the reply was produced but lost.
|
|
1362
|
-
const alwaysFail: WriteOutbound = async () => {
|
|
1363
|
-
throw new Error("vault transport: write reply failed (400) bad request");
|
|
1364
|
-
};
|
|
1365
|
-
const cb = callbackRecorder();
|
|
1366
|
-
const reg = new ProgrammaticAgentRegistry({
|
|
1367
|
-
backend,
|
|
1368
|
-
writeOutbound: alwaysFail,
|
|
1369
|
-
writeCallback: cb.fn,
|
|
1370
|
-
outboundRetryBaseMs: 1,
|
|
1371
|
-
});
|
|
1372
|
-
await reg.register(specFor("worker"));
|
|
1373
|
-
|
|
1374
|
-
reg.enqueue("worker", { content: "x", replyTo: "orchestrator" });
|
|
1375
|
-
await until(() => cb.calls.length === 1);
|
|
1376
|
-
// The orchestrator learns the turn did NOT truly succeed (the reply never landed).
|
|
1377
|
-
expect(cb.calls[0]!.meta.status).toBe("error");
|
|
1378
|
-
expect(cb.calls[0]!.meta.source_message).toBeUndefined(); // the note never landed.
|
|
1379
|
-
});
|
|
1380
|
-
|
|
1381
|
-
test("delegation_depth >= MAX → NO callback (the depth loop guard), turn still runs", async () => {
|
|
1382
|
-
const backend = new FakeBackend();
|
|
1383
|
-
backend.resultFor = (m) => ({ ok: true, reply: "done:" + m });
|
|
1384
|
-
const out = recorderWithId();
|
|
1385
|
-
const cb = callbackRecorder();
|
|
1386
|
-
const reg = new ProgrammaticAgentRegistry({
|
|
1387
|
-
backend,
|
|
1388
|
-
writeOutbound: out.fn,
|
|
1389
|
-
writeCallback: cb.fn,
|
|
1390
|
-
});
|
|
1391
|
-
await reg.register(specFor("worker"));
|
|
1392
|
-
|
|
1393
|
-
// An incoming message already AT the ceiling: a callback would push it over, so suppress.
|
|
1394
|
-
reg.enqueue("worker", {
|
|
1395
|
-
content: "deep",
|
|
1396
|
-
replyTo: "orchestrator",
|
|
1397
|
-
delegationDepth: MAX_DELEGATION_DEPTH,
|
|
1398
|
-
});
|
|
1399
|
-
await until(() => out.calls.length === 1); // the turn STILL ran + replied,
|
|
1400
|
-
await new Promise<void>((r) => setTimeout(r, 5));
|
|
1401
|
-
expect(out.calls).toHaveLength(1);
|
|
1402
|
-
expect(cb.calls).toHaveLength(0); // but the callback was suppressed by the depth guard.
|
|
1403
|
-
});
|
|
1404
|
-
|
|
1405
|
-
test("a message just UNDER the ceiling still gets a callback (boundary)", async () => {
|
|
1406
|
-
const backend = new FakeBackend();
|
|
1407
|
-
backend.resultFor = (m) => ({ ok: true, reply: "done:" + m });
|
|
1408
|
-
const out = recorderWithId();
|
|
1409
|
-
const cb = callbackRecorder();
|
|
1410
|
-
const reg = new ProgrammaticAgentRegistry({
|
|
1411
|
-
backend,
|
|
1412
|
-
writeOutbound: out.fn,
|
|
1413
|
-
writeCallback: cb.fn,
|
|
1414
|
-
});
|
|
1415
|
-
await reg.register(specFor("worker"));
|
|
1416
|
-
|
|
1417
|
-
reg.enqueue("worker", {
|
|
1418
|
-
content: "near-edge",
|
|
1419
|
-
replyTo: "orchestrator",
|
|
1420
|
-
delegationDepth: MAX_DELEGATION_DEPTH - 1,
|
|
1421
|
-
});
|
|
1422
|
-
await until(() => cb.calls.length === 1);
|
|
1423
|
-
expect(cb.calls).toHaveLength(1);
|
|
1424
|
-
expect(cb.calls[0]!.meta.delegation_depth).toBe(String(MAX_DELEGATION_DEPTH)); // the last hop.
|
|
1425
|
-
});
|
|
1426
|
-
|
|
1427
|
-
test("no WriteCallback wired → reply_to is inert (the turn runs normally, no crash)", async () => {
|
|
1428
|
-
const backend = new FakeBackend();
|
|
1429
|
-
backend.resultFor = (m) => ({ ok: true, reply: "done:" + m });
|
|
1430
|
-
const out = recorderWithId();
|
|
1431
|
-
// NOTE: writeCallback intentionally NOT passed.
|
|
1432
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: out.fn });
|
|
1433
|
-
await reg.register(specFor("worker"));
|
|
1434
|
-
|
|
1435
|
-
reg.enqueue("worker", { content: "x", replyTo: "orchestrator" });
|
|
1436
|
-
await until(() => out.calls.length === 1);
|
|
1437
|
-
expect(out.calls).toHaveLength(1); // the turn ran fine despite reply_to + no sink.
|
|
1438
|
-
});
|
|
1439
|
-
|
|
1440
|
-
test("a WriteCallback that THROWS does not strand the drain (best-effort, logged)", async () => {
|
|
1441
|
-
const backend = new FakeBackend();
|
|
1442
|
-
backend.resultFor = (m) => ({ ok: true, reply: "done:" + m });
|
|
1443
|
-
const out = recorderWithId();
|
|
1444
|
-
const throwingCb: WriteCallback = async () => {
|
|
1445
|
-
throw new Error("callback delivery boom");
|
|
1446
|
-
};
|
|
1447
|
-
const reg = new ProgrammaticAgentRegistry({
|
|
1448
|
-
backend,
|
|
1449
|
-
writeOutbound: out.fn,
|
|
1450
|
-
writeCallback: throwingCb,
|
|
1451
|
-
});
|
|
1452
|
-
await reg.register(specFor("worker"));
|
|
1453
|
-
|
|
1454
|
-
// Two reply_to messages; the first callback throws — the second turn must still drain.
|
|
1455
|
-
reg.enqueue("worker", { content: "first", replyTo: "orchestrator" });
|
|
1456
|
-
reg.enqueue("worker", { content: "second", replyTo: "orchestrator" });
|
|
1457
|
-
await until(() => out.calls.length === 2);
|
|
1458
|
-
expect(out.calls.map((c) => c.reply)).toEqual(["done:first", "done:second"]);
|
|
1459
|
-
});
|
|
1460
|
-
|
|
1461
|
-
test("CONCURRENCY: N callbacks returning to ONE orchestrator channel drain FIFO, none lost or clobbered", async () => {
|
|
1462
|
-
// The orchestrator-resume story: an orchestrator fires N sub-tasks; each worker's turn
|
|
1463
|
-
// completes and delivers a callback BACK to the orchestrator's channel. Those callbacks
|
|
1464
|
-
// arrive as inbound on the orchestrator's channel and are handled by ITS per-channel
|
|
1465
|
-
// serial drain — one at a time, FIFO, never concurrent (its --resume session carries
|
|
1466
|
-
// state across them). We exercise the DRAIN-SIDE FIFO property directly here (enqueue N
|
|
1467
|
-
// callback-shaped inbound messages on one channel + assert they drain in order, none
|
|
1468
|
-
// lost, the backend never ran two concurrently) — NOT the real vault-IPC delivery path
|
|
1469
|
-
// (callback note → trigger → /api/vault/inbound → emit), which the wiring + vault suites
|
|
1470
|
-
// cover. The serial drain is the same machinery either way, so this pins the invariant.
|
|
1471
|
-
const backend = new FakeBackend();
|
|
1472
|
-
backend.resultFor = (m) => ({ ok: true, reply: "ack:" + m });
|
|
1473
|
-
const out = recorderWithId();
|
|
1474
|
-
const reg = new ProgrammaticAgentRegistry({ backend, writeOutbound: out.fn });
|
|
1475
|
-
await reg.register(specFor("orchestrator"));
|
|
1476
|
-
|
|
1477
|
-
const N = 6;
|
|
1478
|
-
for (let i = 0; i < N; i++) {
|
|
1479
|
-
// A callback inbound carries NO reply_to (terminal) — exactly the shape the daemon
|
|
1480
|
-
// writes. The orchestrator processes each as "a sub-task finished" message.
|
|
1481
|
-
reg.enqueue("orchestrator", { content: `callback-${i}` });
|
|
1482
|
-
}
|
|
1483
|
-
await until(() => backend.calls.length === N);
|
|
1484
|
-
// FIFO: arrival order preserved, NONE lost or duplicated.
|
|
1485
|
-
expect(backend.calls.map((c) => c.message)).toEqual(
|
|
1486
|
-
Array.from({ length: N }, (_, i) => `callback-${i}`),
|
|
1487
|
-
);
|
|
1488
|
-
// The per-channel serial worker never ran two turns at once (the --resume invariant).
|
|
1489
|
-
expect(backend.maxConcurrent).toBe(1);
|
|
1490
|
-
expect(out.calls.map((c) => c.reply)).toEqual(
|
|
1491
|
-
Array.from({ length: N }, (_, i) => `ack:callback-${i}`),
|
|
1492
|
-
);
|
|
1493
|
-
});
|
|
1494
|
-
});
|