@crewhaus/runtime-core 0.1.0

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.
@@ -0,0 +1,1541 @@
1
+ import { afterAll, beforeAll, describe, expect, test } from "bun:test";
2
+ import { mkdtempSync, rmSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { PassThrough } from "node:stream";
6
+ import type Anthropic from "@anthropic-ai/sdk";
7
+ import { createLogger } from "@crewhaus/logging";
8
+ import { createRunContext } from "@crewhaus/run-context";
9
+ import { buildTool } from "@crewhaus/tool-builder";
10
+ import { z } from "zod";
11
+ import { replayMessageHistory, resolveAuth, runChatLoop } from "./index";
12
+
13
+ // Route session-store/event-log writes to a per-file tmpdir so tests do
14
+ // not pollute `.crewhaus/sessions/` in the repo. runtime-core honours
15
+ // CREWHAUS_SESSION_DIR when no explicit `sessionRootDir` is supplied.
16
+ const SHARED_SESSION_ROOT = mkdtempSync(join(tmpdir(), "crewhaus-runtime-core-tests-"));
17
+ beforeAll(() => {
18
+ process.env["CREWHAUS_SESSION_DIR"] = SHARED_SESSION_ROOT;
19
+ });
20
+ afterAll(() => {
21
+ process.env["CREWHAUS_SESSION_DIR"] = undefined;
22
+ rmSync(SHARED_SESSION_ROOT, { recursive: true, force: true });
23
+ });
24
+
25
+ describe("resolveAuth", () => {
26
+ test("returns mode=none when neither var is set", () => {
27
+ expect(resolveAuth({})).toEqual({ mode: "none" });
28
+ });
29
+
30
+ test("recognizes an OAuth token by the sk-ant-oat prefix", () => {
31
+ expect(resolveAuth({ ANTHROPIC_AUTH_TOKEN: "sk-ant-oat01-abc" })).toEqual({
32
+ mode: "oauth",
33
+ token: "sk-ant-oat01-abc",
34
+ });
35
+ });
36
+
37
+ test("treats a non-OAuth ANTHROPIC_AUTH_TOKEN as an API key", () => {
38
+ expect(resolveAuth({ ANTHROPIC_AUTH_TOKEN: "sk-ant-api01-xyz" })).toEqual({
39
+ mode: "api-key",
40
+ token: "sk-ant-api01-xyz",
41
+ });
42
+ });
43
+
44
+ test("falls back to ANTHROPIC_API_KEY when AUTH_TOKEN is missing", () => {
45
+ expect(resolveAuth({ ANTHROPIC_API_KEY: "sk-ant-api01-aaa" })).toEqual({
46
+ mode: "api-key",
47
+ token: "sk-ant-api01-aaa",
48
+ });
49
+ });
50
+
51
+ test("AUTH_TOKEN takes precedence over API_KEY", () => {
52
+ expect(
53
+ resolveAuth({
54
+ ANTHROPIC_AUTH_TOKEN: "sk-ant-oat01-abc",
55
+ ANTHROPIC_API_KEY: "sk-ant-api01-xyz",
56
+ }),
57
+ ).toEqual({ mode: "oauth", token: "sk-ant-oat01-abc" });
58
+ });
59
+
60
+ test("ignores empty-string env values", () => {
61
+ expect(
62
+ resolveAuth({ ANTHROPIC_AUTH_TOKEN: "", ANTHROPIC_API_KEY: "sk-ant-api01-only" }),
63
+ ).toEqual({ mode: "api-key", token: "sk-ant-api01-only" });
64
+ });
65
+ });
66
+
67
+ /**
68
+ * Section 17 — synthetic ProviderAdapter that returns a single
69
+ * text-only stream per call. Counts how many times the runtime invokes
70
+ * the adapter so EOF / loop-exit assertions stay accurate.
71
+ */
72
+ function makeStubAdapter(reply = "ok"): {
73
+ adapter: import("@crewhaus/adapter-anthropic").ProviderAdapter;
74
+ calls: () => number;
75
+ } {
76
+ let calls = 0;
77
+ const adapter: import("@crewhaus/adapter-anthropic").ProviderAdapter = {
78
+ providerId: "anthropic",
79
+ features: {
80
+ caching: "explicit",
81
+ tool_use: true,
82
+ vision: true,
83
+ thinking: true,
84
+ web_search: true,
85
+ },
86
+ estimateTokens: () => 0,
87
+ stream: () => {
88
+ calls++;
89
+ return (async function* () {
90
+ yield { kind: "message_start" } as const;
91
+ yield {
92
+ kind: "content_block_start",
93
+ index: 0,
94
+ block: { type: "text", text: "" },
95
+ } as const;
96
+ yield {
97
+ kind: "content_block_delta",
98
+ index: 0,
99
+ delta: { type: "text_delta", text: reply },
100
+ } as const;
101
+ yield { kind: "content_block_stop", index: 0 } as const;
102
+ yield { kind: "message_delta", stopReason: "end_turn" } as const;
103
+ yield { kind: "message_stop" } as const;
104
+ })();
105
+ },
106
+ };
107
+ return { adapter, calls: () => calls };
108
+ }
109
+
110
+ describe("runChatLoop stdin EOF handling", () => {
111
+ test("exits cleanly when the input stream is already at EOF", async () => {
112
+ const input = new PassThrough();
113
+ input.end();
114
+ const { adapter, calls } = makeStubAdapter();
115
+
116
+ await runChatLoop({ model: "test-model", instructions: "test", _adapter: adapter, input });
117
+
118
+ expect(calls()).toBe(0);
119
+ });
120
+
121
+ test("exits cleanly after consuming buffered input followed by EOF", async () => {
122
+ const input = new PassThrough();
123
+ input.write("hi\n");
124
+ input.end();
125
+ const { adapter, calls } = makeStubAdapter("hello back");
126
+
127
+ await runChatLoop({ model: "test-model", instructions: "test", _adapter: adapter, input });
128
+
129
+ expect(calls()).toBe(1);
130
+ });
131
+ });
132
+
133
+ /**
134
+ * Scripted ProviderAdapter (Section 17) that cycles through pre-baked
135
+ * content-block arrays per call, synthesising the canonical StreamEvent
136
+ * sequence. Captures the messages + tools the runtime sent so tests
137
+ * can assert tool_result wiring, advertise-then-omit behaviour, etc.
138
+ */
139
+ function makeScriptedClient(scripts: ReadonlyArray<Anthropic.ContentBlock[]>): {
140
+ adapter: import("@crewhaus/adapter-anthropic").ProviderAdapter;
141
+ callCount: () => number;
142
+ capturedMessages: () => ReadonlyArray<ReadonlyArray<Anthropic.MessageParam>>;
143
+ capturedTools: () => ReadonlyArray<Anthropic.Tool[] | undefined>;
144
+ } {
145
+ const captures: Anthropic.MessageParam[][] = [];
146
+ const tools: (Anthropic.Tool[] | undefined)[] = [];
147
+ let i = 0;
148
+ const adapter: import("@crewhaus/adapter-anthropic").ProviderAdapter = {
149
+ providerId: "anthropic",
150
+ features: {
151
+ caching: "explicit",
152
+ tool_use: true,
153
+ vision: true,
154
+ thinking: true,
155
+ web_search: true,
156
+ },
157
+ estimateTokens: () => 0,
158
+ stream: (req) => {
159
+ captures.push(req.messages.map((m) => ({ ...m })) as Anthropic.MessageParam[]);
160
+ tools.push(
161
+ req.tools !== undefined
162
+ ? req.tools.map((t) => ({
163
+ name: t.name,
164
+ description: t.description,
165
+ input_schema: t.input_schema as Anthropic.Tool.InputSchema,
166
+ }))
167
+ : undefined,
168
+ );
169
+ const content = scripts[Math.min(i, scripts.length - 1)] ?? [];
170
+ const hasToolUse = content.some((b) => b.type === "tool_use");
171
+ i++;
172
+ return (async function* () {
173
+ yield { kind: "message_start" } as const;
174
+ for (let idx = 0; idx < content.length; idx++) {
175
+ const block = content[idx];
176
+ if (block === undefined) continue;
177
+ if (block.type === "text") {
178
+ yield {
179
+ kind: "content_block_start",
180
+ index: idx,
181
+ block: { type: "text", text: "" },
182
+ } as const;
183
+ yield {
184
+ kind: "content_block_delta",
185
+ index: idx,
186
+ delta: { type: "text_delta", text: block.text },
187
+ } as const;
188
+ yield { kind: "content_block_stop", index: idx } as const;
189
+ } else if (block.type === "tool_use") {
190
+ yield {
191
+ kind: "content_block_start",
192
+ index: idx,
193
+ block: { type: "tool_use", id: block.id, name: block.name, input: {} },
194
+ } as const;
195
+ yield {
196
+ kind: "content_block_delta",
197
+ index: idx,
198
+ delta: { type: "input_json_delta", partial_json: JSON.stringify(block.input ?? {}) },
199
+ } as const;
200
+ yield { kind: "content_block_stop", index: idx } as const;
201
+ }
202
+ }
203
+ yield {
204
+ kind: "message_delta",
205
+ stopReason: hasToolUse ? "tool_use" : "end_turn",
206
+ } as const;
207
+ yield { kind: "message_stop" } as const;
208
+ })();
209
+ },
210
+ };
211
+ return {
212
+ adapter,
213
+ callCount: () => i,
214
+ capturedMessages: () => captures,
215
+ capturedTools: () => tools,
216
+ };
217
+ }
218
+
219
+ describe("runChatLoop tool execution", () => {
220
+ test("executes a tool_use block and continues the conversation", async () => {
221
+ const toolCalls: unknown[] = [];
222
+ const echoTool = buildTool({
223
+ name: "echo",
224
+ description: "echoes the input",
225
+ inputSchema: z.object({ msg: z.string() }),
226
+ execute: async (input) => {
227
+ toolCalls.push(input);
228
+ return `echoed: ${input.msg}`;
229
+ },
230
+ });
231
+
232
+ const { adapter, callCount, capturedMessages, capturedTools } = makeScriptedClient([
233
+ // 1st model turn: ask for a tool.
234
+ [
235
+ {
236
+ type: "tool_use",
237
+ id: "tu_1",
238
+ name: "echo",
239
+ input: { msg: "hi" },
240
+ } as Anthropic.ToolUseBlock,
241
+ ],
242
+ // 2nd model turn: text-only, terminates the inner loop.
243
+ [{ type: "text", text: "done", citations: null } as Anthropic.TextBlock],
244
+ ]);
245
+
246
+ const input = new PassThrough();
247
+ input.write("please echo\n");
248
+ input.end();
249
+
250
+ await runChatLoop({
251
+ model: "test-model",
252
+ instructions: "test",
253
+ _adapter: adapter,
254
+ input,
255
+ tools: [echoTool],
256
+ permissionMode: "bypass",
257
+ });
258
+
259
+ // The model was called twice for one user input: once that requested the
260
+ // tool, once after the tool result was injected.
261
+ expect(callCount()).toBe(2);
262
+ // The tool's execute spy was called exactly once with the validated input.
263
+ expect(toolCalls).toEqual([{ msg: "hi" }]);
264
+
265
+ // Tools advertised on every call (once advertised, stay advertised).
266
+ expect(capturedTools()[0]).toBeDefined();
267
+ expect(capturedTools()[0]?.[0]?.name).toBe("echo");
268
+
269
+ // The 2nd request must include a tool_result block referencing tu_1.
270
+ const secondCallMessages = capturedMessages()[1] ?? [];
271
+ const userToolResult = secondCallMessages.find(
272
+ (m) =>
273
+ m.role === "user" &&
274
+ Array.isArray(m.content) &&
275
+ m.content.some((b) => typeof b === "object" && b.type === "tool_result"),
276
+ );
277
+ expect(userToolResult).toBeDefined();
278
+ const resultBlocks = userToolResult?.content as Anthropic.ToolResultBlockParam[];
279
+ expect(resultBlocks[0]?.tool_use_id).toBe("tu_1");
280
+ expect(resultBlocks[0]?.is_error).toBe(false);
281
+ expect(resultBlocks[0]?.content).toBe("echoed: hi");
282
+ });
283
+
284
+ test("returns an is_error tool_result when the model names an unknown tool", async () => {
285
+ const knownTool = buildTool({
286
+ name: "known",
287
+ description: "noop",
288
+ inputSchema: z.object({}),
289
+ execute: async () => "ok",
290
+ });
291
+
292
+ const { adapter, callCount, capturedMessages } = makeScriptedClient([
293
+ [
294
+ {
295
+ type: "tool_use",
296
+ id: "tu_x",
297
+ name: "ghost",
298
+ input: {},
299
+ } as Anthropic.ToolUseBlock,
300
+ ],
301
+ [{ type: "text", text: "recovered", citations: null } as Anthropic.TextBlock],
302
+ ]);
303
+
304
+ const input = new PassThrough();
305
+ input.write("go\n");
306
+ input.end();
307
+
308
+ await runChatLoop({
309
+ model: "test-model",
310
+ instructions: "test",
311
+ _adapter: adapter,
312
+ input,
313
+ tools: [knownTool],
314
+ });
315
+
316
+ expect(callCount()).toBe(2);
317
+ const secondCall = capturedMessages()[1] ?? [];
318
+ const userMsg = secondCall[secondCall.length - 1];
319
+ const blocks = (userMsg?.content ?? []) as Anthropic.ToolResultBlockParam[];
320
+ expect(blocks[0]?.is_error).toBe(true);
321
+ expect(blocks[0]?.content).toContain('unknown tool "ghost"');
322
+ });
323
+
324
+ test("does not advertise tools when the tools list is empty", async () => {
325
+ const { adapter, capturedTools } = makeScriptedClient([
326
+ [{ type: "text", text: "hi", citations: null } as Anthropic.TextBlock],
327
+ ]);
328
+
329
+ const input = new PassThrough();
330
+ input.write("hi\n");
331
+ input.end();
332
+
333
+ await runChatLoop({ model: "test-model", instructions: "test", _adapter: adapter, input });
334
+
335
+ expect(capturedTools()[0]).toBeUndefined();
336
+ });
337
+
338
+ test("forwards tool.jsonSchema verbatim when present (over zodToJsonSchema)", async () => {
339
+ const customJsonSchema = {
340
+ type: "object" as const,
341
+ properties: {
342
+ message: { type: "string", description: "what to echo" },
343
+ flag: { type: "boolean", default: false },
344
+ },
345
+ required: ["message"],
346
+ additionalProperties: false,
347
+ };
348
+ const mcpStyleTool = buildTool({
349
+ name: "everything__echo",
350
+ description: "remote echo",
351
+ // The validator is permissive (mirrors how tool-mcp builds RegisteredTool).
352
+ inputSchema: z.unknown(),
353
+ jsonSchema: customJsonSchema,
354
+ execute: async () => "ok",
355
+ });
356
+
357
+ const { adapter, capturedTools } = makeScriptedClient([
358
+ [{ type: "text", text: "fine", citations: null } as Anthropic.TextBlock],
359
+ ]);
360
+
361
+ const input = new PassThrough();
362
+ input.write("hi\n");
363
+ input.end();
364
+
365
+ await runChatLoop({
366
+ model: "test-model",
367
+ instructions: "test",
368
+ _adapter: adapter,
369
+ input,
370
+ tools: [mcpStyleTool],
371
+ });
372
+
373
+ // The tool advertised on the first call must carry the exact JSON Schema
374
+ // bytes from the MCP-style tool, not the zodToJsonSchema(z.unknown()) shape.
375
+ expect(capturedTools()[0]?.[0]?.input_schema).toBe(
376
+ customJsonSchema as unknown as Anthropic.Tool.InputSchema,
377
+ );
378
+ });
379
+ });
380
+
381
+ /**
382
+ * Section 17 — combined stream-and-compaction stub. The runtime now
383
+ * routes both turns and `autoCompact` through `adapter.stream(...)`,
384
+ * so a single ProviderAdapter handles both surfaces. The `scripts`
385
+ * array drives normal turns; once exhausted, every subsequent call
386
+ * (which in practice are autoCompact calls) returns a fixed "compacted
387
+ * summary" text-only stream — preserving the prior separation of
388
+ * concerns at the assertion layer.
389
+ */
390
+ function makeFullClient(scripts: ReadonlyArray<Anthropic.ContentBlock[]>): {
391
+ adapter: import("@crewhaus/adapter-anthropic").ProviderAdapter;
392
+ streamCount: () => number;
393
+ createCount: () => number;
394
+ capturedStreamMessages: () => ReadonlyArray<ReadonlyArray<Anthropic.MessageParam>>;
395
+ } {
396
+ const captures: Anthropic.MessageParam[][] = [];
397
+ let streamIdx = 0;
398
+ let compactionCount = 0;
399
+ const adapter: import("@crewhaus/adapter-anthropic").ProviderAdapter = {
400
+ providerId: "anthropic",
401
+ features: {
402
+ caching: "explicit",
403
+ tool_use: true,
404
+ vision: true,
405
+ thinking: true,
406
+ web_search: true,
407
+ },
408
+ estimateTokens: () => 0,
409
+ stream: (req) => {
410
+ // Detect compaction vs. turn by sniffing the trailing user message
411
+ // for the SUMMARY_REQUEST sentinel. autoCompact appends a known
412
+ // "Summarize the prior conversation" prompt; turn calls don't.
413
+ const lastMsg = req.messages[req.messages.length - 1];
414
+ const lastContent = typeof lastMsg?.content === "string" ? lastMsg.content : "";
415
+ const isCompaction = /Summarize the prior conversation/.test(lastContent);
416
+ // Capture only turn calls so test assertions on
417
+ // `capturedStreamMessages()[0]` keep their pre-Section-17
418
+ // semantics (where compaction went through a separate API
419
+ // surface). Compaction calls are counted in `compactionCount`.
420
+ if (!isCompaction) {
421
+ captures.push(req.messages.map((m) => ({ ...m })) as Anthropic.MessageParam[]);
422
+ }
423
+ const content = isCompaction
424
+ ? ([
425
+ { type: "text", text: "compacted summary", citations: null } as Anthropic.TextBlock,
426
+ ] as Anthropic.ContentBlock[])
427
+ : (scripts[Math.min(streamIdx, scripts.length - 1)] ?? []);
428
+ if (isCompaction) compactionCount++;
429
+ else streamIdx++;
430
+ const hasToolUse = content.some((b) => b.type === "tool_use");
431
+ return (async function* () {
432
+ yield { kind: "message_start" } as const;
433
+ for (let idx = 0; idx < content.length; idx++) {
434
+ const block = content[idx];
435
+ if (block === undefined) continue;
436
+ if (block.type === "text") {
437
+ yield {
438
+ kind: "content_block_start",
439
+ index: idx,
440
+ block: { type: "text", text: "" },
441
+ } as const;
442
+ yield {
443
+ kind: "content_block_delta",
444
+ index: idx,
445
+ delta: { type: "text_delta", text: block.text },
446
+ } as const;
447
+ yield { kind: "content_block_stop", index: idx } as const;
448
+ } else if (block.type === "tool_use") {
449
+ yield {
450
+ kind: "content_block_start",
451
+ index: idx,
452
+ block: { type: "tool_use", id: block.id, name: block.name, input: {} },
453
+ } as const;
454
+ yield {
455
+ kind: "content_block_delta",
456
+ index: idx,
457
+ delta: { type: "input_json_delta", partial_json: JSON.stringify(block.input ?? {}) },
458
+ } as const;
459
+ yield { kind: "content_block_stop", index: idx } as const;
460
+ }
461
+ }
462
+ yield {
463
+ kind: "message_delta",
464
+ stopReason: hasToolUse ? "tool_use" : "end_turn",
465
+ } as const;
466
+ yield { kind: "message_stop" } as const;
467
+ })();
468
+ },
469
+ };
470
+ return {
471
+ adapter,
472
+ streamCount: () => streamIdx,
473
+ createCount: () => compactionCount,
474
+ capturedStreamMessages: () => captures,
475
+ };
476
+ }
477
+
478
+ describe("runChatLoop runContext", () => {
479
+ test("runs without an explicit runContext (default factory fires)", async () => {
480
+ const input = new PassThrough();
481
+ input.end();
482
+ const { adapter, calls } = makeStubAdapter();
483
+ await runChatLoop({ model: "test-model", instructions: "test", _adapter: adapter, input });
484
+ expect(calls()).toBe(0);
485
+ });
486
+
487
+ test("turnNumber increments on each user input", async () => {
488
+ const ctx = createRunContext();
489
+ expect(ctx.turnNumber).toBe(0);
490
+
491
+ const input = new PassThrough();
492
+ input.write("hello\n");
493
+ input.end();
494
+ const { adapter } = makeStubAdapter("ack");
495
+
496
+ await runChatLoop({
497
+ model: "test-model",
498
+ instructions: "test",
499
+ _adapter: adapter,
500
+ input,
501
+ runContext: ctx,
502
+ });
503
+
504
+ expect(ctx.turnNumber).toBe(1);
505
+ });
506
+
507
+ test("logger receives turn-start debug records", async () => {
508
+ const lines: string[] = [];
509
+ const logger = createLogger({
510
+ level: "debug",
511
+ format: "json",
512
+ sink: (line) => {
513
+ lines.push(line);
514
+ },
515
+ });
516
+ const ctx = createRunContext({ logger });
517
+ const input = new PassThrough();
518
+ input.write("hi\n");
519
+ input.end();
520
+ const { adapter } = makeStubAdapter("ack");
521
+
522
+ await runChatLoop({
523
+ model: "test-model",
524
+ instructions: "test",
525
+ _adapter: adapter,
526
+ input,
527
+ runContext: ctx,
528
+ });
529
+
530
+ const turnStarts = lines.filter((l) => l.includes('"msg":"turn start"'));
531
+ expect(turnStarts.length).toBe(1);
532
+ });
533
+ });
534
+
535
+ describe("runChatLoop pre-turn compaction", () => {
536
+ test("snips long history before calling the model", async () => {
537
+ // Pre-load the runtime via a long initial user message — bigger
538
+ // than 0.85 * 200 = 170 tokens at char/4 heuristic, so the budget
539
+ // trips on turn 1 and snip fires.
540
+ //
541
+ // We use a small contextLimit + matching threshold so we don't
542
+ // need to fabricate ~170k chars of input.
543
+ const longUser = "a".repeat(800); // ~200 estimated tokens
544
+ const input = new PassThrough();
545
+ input.write(`${longUser}\n`);
546
+ input.end();
547
+
548
+ const { adapter, capturedStreamMessages } = makeFullClient([
549
+ [{ type: "text", text: "ok", citations: null } as Anthropic.TextBlock],
550
+ ]);
551
+
552
+ await runChatLoop({
553
+ model: "test-model",
554
+ instructions: "test",
555
+ _adapter: adapter,
556
+ input,
557
+ contextLimit: 200,
558
+ compactionThreshold: 0.85,
559
+ snipKeepHead: 0,
560
+ snipKeepTail: 0,
561
+ });
562
+
563
+ // The model should still have been called, but the messages it saw
564
+ // include the snip marker (the snipped path runs even though it
565
+ // collapses to a single marker for keepHead/keepTail = 0/0).
566
+ const sentToModel = capturedStreamMessages()[0] ?? [];
567
+ const containsMarker = sentToModel.some(
568
+ (m) => typeof m.content === "string" && m.content.includes("[Context compacted:"),
569
+ );
570
+ expect(containsMarker).toBe(true);
571
+ });
572
+
573
+ test("falls back to autocompact when snip alone does not free enough", async () => {
574
+ // contextLimit = 100. After snip with keepHead=1/keepTail=1, we
575
+ // keep both message ends (each big), so usage stays >85% and the
576
+ // autocompact path triggers.
577
+ const big = "x".repeat(800); // ~200 tokens estimate
578
+ const input = new PassThrough();
579
+ input.write(`${big}\n`);
580
+ input.end();
581
+
582
+ const { adapter, createCount, capturedStreamMessages } = makeFullClient([
583
+ [{ type: "text", text: "ok", citations: null } as Anthropic.TextBlock],
584
+ ]);
585
+
586
+ await runChatLoop({
587
+ model: "test-model",
588
+ instructions: "test",
589
+ _adapter: adapter,
590
+ input,
591
+ contextLimit: 100,
592
+ compactionThreshold: 0.85,
593
+ snipKeepHead: 1,
594
+ snipKeepTail: 1,
595
+ });
596
+
597
+ expect(createCount()).toBe(1);
598
+ const sentToModel = capturedStreamMessages()[0] ?? [];
599
+ // After autocompact the history is the [marker, summary] pair.
600
+ const hasMarker = sentToModel.some(
601
+ (m) => typeof m.content === "string" && m.content.includes("Previous conversation summary"),
602
+ );
603
+ const hasSummary = sentToModel.some(
604
+ (m) => typeof m.content === "string" && m.content.includes("compacted summary"),
605
+ );
606
+ expect(hasMarker).toBe(true);
607
+ expect(hasSummary).toBe(true);
608
+ });
609
+
610
+ test("skips compaction entirely when budget is below threshold", async () => {
611
+ const input = new PassThrough();
612
+ input.write("hello\n"); // ~2 tokens
613
+ input.end();
614
+
615
+ const { adapter, createCount, capturedStreamMessages } = makeFullClient([
616
+ [{ type: "text", text: "hi back", citations: null } as Anthropic.TextBlock],
617
+ ]);
618
+
619
+ await runChatLoop({
620
+ model: "test-model",
621
+ instructions: "test",
622
+ _adapter: adapter,
623
+ input,
624
+ contextLimit: 200_000,
625
+ });
626
+
627
+ expect(createCount()).toBe(0);
628
+ const sentToModel = capturedStreamMessages()[0] ?? [];
629
+ const containsMarker = sentToModel.some(
630
+ (m) => typeof m.content === "string" && m.content.includes("[Context compacted:"),
631
+ );
632
+ expect(containsMarker).toBe(false);
633
+ });
634
+ });
635
+
636
+ describe("runChatLoop singleTurn mode", () => {
637
+ test("returns the terminal assistant text and does not read stdin", async () => {
638
+ const { adapter, callCount } = makeScriptedClient([
639
+ [{ type: "text", text: "hello from step", citations: null } as Anthropic.TextBlock],
640
+ ]);
641
+
642
+ // PassThrough that we never end and never write to: if singleTurn read
643
+ // from it the call would hang and time out the test.
644
+ const input = new PassThrough();
645
+
646
+ const result = await runChatLoop({
647
+ model: "test-model",
648
+ instructions: "test",
649
+ _adapter: adapter,
650
+ input,
651
+ singleTurn: true,
652
+ seedMessages: [{ role: "user", content: "go" }],
653
+ });
654
+
655
+ expect(result).toBe("hello from step");
656
+ expect(callCount()).toBe(1);
657
+ // Stream should not have been touched.
658
+ expect(input.readableEnded).toBe(false);
659
+ });
660
+
661
+ test("executes tool_use blocks and returns the post-tool assistant text", async () => {
662
+ const toolCalls: unknown[] = [];
663
+ const echoTool = buildTool({
664
+ name: "echo",
665
+ description: "echoes",
666
+ inputSchema: z.object({ msg: z.string() }),
667
+ execute: async (input) => {
668
+ toolCalls.push(input);
669
+ return `echoed: ${input.msg}`;
670
+ },
671
+ });
672
+
673
+ const { adapter, callCount } = makeScriptedClient([
674
+ [
675
+ {
676
+ type: "tool_use",
677
+ id: "tu_1",
678
+ name: "echo",
679
+ input: { msg: "hi" },
680
+ } as Anthropic.ToolUseBlock,
681
+ ],
682
+ [{ type: "text", text: "after tool", citations: null } as Anthropic.TextBlock],
683
+ ]);
684
+
685
+ const result = await runChatLoop({
686
+ model: "test-model",
687
+ instructions: "test",
688
+ _adapter: adapter,
689
+ singleTurn: true,
690
+ seedMessages: [{ role: "user", content: "please echo" }],
691
+ tools: [echoTool],
692
+ permissionMode: "bypass",
693
+ });
694
+
695
+ expect(result).toBe("after tool");
696
+ expect(callCount()).toBe(2);
697
+ expect(toolCalls).toEqual([{ msg: "hi" }]);
698
+ });
699
+
700
+ test("returns empty string when terminal assistant has no text blocks", async () => {
701
+ // Edge case: model returns only a tool_use, then on the follow-up returns
702
+ // nothing text-like (e.g. another tool_use that we treat as text-Done by
703
+ // simulating with an empty content array on the second turn). The state
704
+ // machine would not actually allow that in production, so use a single
705
+ // empty-text turn to model "no text content".
706
+ const { adapter } = makeScriptedClient([
707
+ [{ type: "text", text: "", citations: null } as Anthropic.TextBlock],
708
+ ]);
709
+
710
+ const result = await runChatLoop({
711
+ model: "test-model",
712
+ instructions: "test",
713
+ _adapter: adapter,
714
+ singleTurn: true,
715
+ seedMessages: [{ role: "user", content: "anything" }],
716
+ });
717
+
718
+ expect(result).toBe("");
719
+ });
720
+
721
+ test("permission policy denies a tool in default mode and writes a denial tool_result", async () => {
722
+ const echoTool = buildTool({
723
+ name: "echo",
724
+ description: "echoes",
725
+ inputSchema: z.object({ msg: z.string() }),
726
+ execute: async (input) => `echoed: ${input.msg}`,
727
+ });
728
+
729
+ const { adapter, callCount, capturedMessages } = makeScriptedClient([
730
+ [
731
+ {
732
+ type: "tool_use",
733
+ id: "tu_d",
734
+ name: "echo",
735
+ input: { msg: "hi" },
736
+ } as Anthropic.ToolUseBlock,
737
+ ],
738
+ [{ type: "text", text: "ack denied", citations: null } as Anthropic.TextBlock],
739
+ ]);
740
+
741
+ // single-turn: no rl, so any "ask" decision falls through to deny.
742
+ // default mode + empty rule set → ask → deny in single-turn mode.
743
+ await runChatLoop({
744
+ model: "test-model",
745
+ instructions: "test",
746
+ _adapter: adapter,
747
+ singleTurn: true,
748
+ seedMessages: [{ role: "user", content: "go" }],
749
+ tools: [echoTool],
750
+ permissionMode: "default",
751
+ });
752
+
753
+ expect(callCount()).toBe(2);
754
+ const secondCall = capturedMessages()[1] ?? [];
755
+ const blocks = (secondCall[secondCall.length - 1]?.content ??
756
+ []) as Anthropic.ToolResultBlockParam[];
757
+ expect(blocks[0]?.is_error).toBe(true);
758
+ expect(String(blocks[0]?.content)).toMatch(/tool denied/);
759
+ });
760
+
761
+ test("permission policy in plan mode denies any non-readOnly tool", async () => {
762
+ const writeTool = buildTool({
763
+ name: "write",
764
+ description: "writes",
765
+ inputSchema: z.object({ path: z.string() }),
766
+ destructive: true,
767
+ execute: async () => "wrote",
768
+ });
769
+
770
+ const { adapter, capturedMessages } = makeScriptedClient([
771
+ [
772
+ {
773
+ type: "tool_use",
774
+ id: "tu_w",
775
+ name: "write",
776
+ input: { path: "/tmp/x" },
777
+ } as Anthropic.ToolUseBlock,
778
+ ],
779
+ [{ type: "text", text: "ok", citations: null } as Anthropic.TextBlock],
780
+ ]);
781
+
782
+ await runChatLoop({
783
+ model: "test-model",
784
+ instructions: "test",
785
+ _adapter: adapter,
786
+ singleTurn: true,
787
+ seedMessages: [{ role: "user", content: "go" }],
788
+ tools: [writeTool],
789
+ permissionMode: "plan",
790
+ });
791
+
792
+ const secondCall = capturedMessages()[1] ?? [];
793
+ const blocks = (secondCall[secondCall.length - 1]?.content ??
794
+ []) as Anthropic.ToolResultBlockParam[];
795
+ expect(blocks[0]?.is_error).toBe(true);
796
+ expect(String(blocks[0]?.content)).toMatch(/tool denied/);
797
+ });
798
+
799
+ test("recovery: 5xx from the stream triggers retry, then succeeds on the next attempt", async () => {
800
+ let attempt = 0;
801
+ const adapter: import("@crewhaus/adapter-anthropic").ProviderAdapter = {
802
+ providerId: "anthropic",
803
+ features: {
804
+ caching: "explicit",
805
+ tool_use: true,
806
+ vision: true,
807
+ thinking: true,
808
+ web_search: true,
809
+ },
810
+ estimateTokens: () => 0,
811
+ stream: () => {
812
+ attempt++;
813
+ const myAttempt = attempt;
814
+ return (async function* () {
815
+ if (myAttempt === 1) {
816
+ throw {
817
+ name: "APIError",
818
+ status: 503,
819
+ error: { type: "api_error", message: "service unavailable" },
820
+ message: "503 Service Unavailable",
821
+ };
822
+ }
823
+ yield { kind: "message_start" } as const;
824
+ yield {
825
+ kind: "content_block_start",
826
+ index: 0,
827
+ block: { type: "text", text: "" },
828
+ } as const;
829
+ yield {
830
+ kind: "content_block_delta",
831
+ index: 0,
832
+ delta: { type: "text_delta", text: "ok after retry" },
833
+ } as const;
834
+ yield { kind: "content_block_stop", index: 0 } as const;
835
+ yield { kind: "message_delta", stopReason: "end_turn" } as const;
836
+ yield { kind: "message_stop" } as const;
837
+ })();
838
+ },
839
+ };
840
+
841
+ const result = await runChatLoop({
842
+ model: "test-model",
843
+ instructions: "test",
844
+ _adapter: adapter,
845
+ singleTurn: true,
846
+ seedMessages: [{ role: "user", content: "go" }],
847
+ permissionMode: "bypass",
848
+ });
849
+
850
+ expect(attempt).toBe(2); // first call failed, second succeeded
851
+ expect(result).toBe("ok after retry");
852
+ });
853
+
854
+ test("throws RuntimeError when seedMessages is missing or does not end with role:user", async () => {
855
+ const { adapter } = makeStubAdapter();
856
+
857
+ await expect(
858
+ runChatLoop({
859
+ model: "test-model",
860
+ instructions: "test",
861
+ _adapter: adapter,
862
+ singleTurn: true,
863
+ seedMessages: [],
864
+ }),
865
+ ).rejects.toThrow(/seedMessages.*role.*user/);
866
+
867
+ await expect(
868
+ runChatLoop({
869
+ model: "test-model",
870
+ instructions: "test",
871
+ _adapter: adapter,
872
+ singleTurn: true,
873
+ seedMessages: [{ role: "assistant", content: "wrong end" }],
874
+ }),
875
+ ).rejects.toThrow(/seedMessages.*role.*user/);
876
+
877
+ await expect(
878
+ runChatLoop({
879
+ model: "test-model",
880
+ instructions: "test",
881
+ _adapter: adapter,
882
+ singleTurn: true,
883
+ }),
884
+ ).rejects.toThrow(/seedMessages.*role.*user/);
885
+ });
886
+ });
887
+
888
+ // --- Section 8 integration: orchestrator, loop-detect, result-store, streaming ---
889
+
890
+ describe("runChatLoop — Section 8 orchestrator", () => {
891
+ test("two read-only tool calls execute concurrently in one turn", async () => {
892
+ const startedAt: number[] = [];
893
+ const finishedAt: number[] = [];
894
+ const slowRead = buildTool({
895
+ name: "Read",
896
+ description: "slow",
897
+ inputSchema: z.object({ id: z.string() }),
898
+ readOnly: true,
899
+ concurrencySafe: true,
900
+ execute: async (input) => {
901
+ startedAt.push(Date.now());
902
+ await new Promise((r) => setTimeout(r, 60));
903
+ finishedAt.push(Date.now());
904
+ return `read:${input.id}`;
905
+ },
906
+ });
907
+
908
+ const { adapter, callCount } = makeScriptedClient([
909
+ [
910
+ {
911
+ type: "tool_use",
912
+ id: "tu_a",
913
+ name: "Read",
914
+ input: { id: "a" },
915
+ } as Anthropic.ToolUseBlock,
916
+ {
917
+ type: "tool_use",
918
+ id: "tu_b",
919
+ name: "Read",
920
+ input: { id: "b" },
921
+ } as Anthropic.ToolUseBlock,
922
+ ],
923
+ [{ type: "text", text: "done", citations: null } as Anthropic.TextBlock],
924
+ ]);
925
+
926
+ const input = new PassThrough();
927
+ input.write("read both\n");
928
+ input.end();
929
+
930
+ const t0 = Date.now();
931
+ await runChatLoop({
932
+ model: "test-model",
933
+ instructions: "test",
934
+ _adapter: adapter,
935
+ input,
936
+ tools: [slowRead],
937
+ permissionMode: "bypass",
938
+ });
939
+ const elapsed = Date.now() - t0;
940
+
941
+ expect(callCount()).toBe(2);
942
+ // Two 60ms tools running in parallel finish in ~60ms total, not 120ms.
943
+ expect(elapsed).toBeLessThan(105);
944
+ // Both starts come before either finish.
945
+ expect(startedAt.length).toBe(2);
946
+ expect(finishedAt.length).toBe(2);
947
+ expect(Math.max(...startedAt)).toBeLessThan(Math.min(...finishedAt));
948
+ });
949
+ });
950
+
951
+ describe("runChatLoop — Section 8 loop detection", () => {
952
+ test("warns when the same tool call repeats >= threshold and stops re-warning", async () => {
953
+ const dateTool = buildTool({
954
+ name: "Bash",
955
+ description: "bash",
956
+ inputSchema: z.object({ command: z.string() }),
957
+ destructive: true,
958
+ execute: async (input) => `ran:${input.command}`,
959
+ });
960
+
961
+ // Three identical Bash calls in a row should trigger a loop warning.
962
+ // Then a final text turn ends the loop.
963
+ const sameCall = (id: string): Anthropic.ToolUseBlock =>
964
+ ({
965
+ type: "tool_use",
966
+ id,
967
+ name: "Bash",
968
+ input: { command: "date" },
969
+ }) as Anthropic.ToolUseBlock;
970
+ const { adapter, capturedMessages } = makeScriptedClient([
971
+ [sameCall("tu_1")],
972
+ [sameCall("tu_2")],
973
+ [sameCall("tu_3")],
974
+ [{ type: "text", text: "stopping", citations: null } as Anthropic.TextBlock],
975
+ ]);
976
+
977
+ const input = new PassThrough();
978
+ input.write("loop\n");
979
+ input.end();
980
+
981
+ await runChatLoop({
982
+ model: "test-model",
983
+ instructions: "test",
984
+ _adapter: adapter,
985
+ input,
986
+ tools: [dateTool],
987
+ permissionMode: "bypass",
988
+ });
989
+
990
+ // The 4th call's request body should contain a `[runtime] possible loop`
991
+ // user message somewhere in `messages`.
992
+ const fourthCall = capturedMessages()[3] ?? [];
993
+ const sawWarning = fourthCall.some(
994
+ (m) =>
995
+ m.role === "user" &&
996
+ typeof m.content === "string" &&
997
+ m.content.includes("[runtime] possible loop detected"),
998
+ );
999
+ expect(sawWarning).toBe(true);
1000
+ });
1001
+ });
1002
+
1003
+ describe("runChatLoop — Section 8 result store", () => {
1004
+ test("large tool output is persisted and replaced with a preview marker", async () => {
1005
+ const { mkdtempSync, rmSync, statSync } = await import("node:fs");
1006
+ const { tmpdir } = await import("node:os");
1007
+ const { join } = await import("node:path");
1008
+ const tmpRoot = mkdtempSync(join(tmpdir(), "crewhaus-runtime-store-"));
1009
+ const oldCwd = process.cwd();
1010
+ process.chdir(tmpRoot);
1011
+
1012
+ try {
1013
+ // Tool that returns >10KB of multi-line output. Make 250 lines of
1014
+ // 50 chars each (≈12.7KB) so the first-100-lines preview is shorter
1015
+ // than the full payload.
1016
+ const bigPayload = Array.from(
1017
+ { length: 250 },
1018
+ (_, i) => `line ${i.toString().padEnd(50, " ")}`,
1019
+ ).join("\n");
1020
+ const bigTool = buildTool({
1021
+ name: "BigRead",
1022
+ description: "produces a large string",
1023
+ inputSchema: z.object({}),
1024
+ readOnly: true,
1025
+ execute: async () => bigPayload,
1026
+ });
1027
+
1028
+ const { adapter, capturedMessages } = makeScriptedClient([
1029
+ [{ type: "tool_use", id: "tu_big", name: "BigRead", input: {} } as Anthropic.ToolUseBlock],
1030
+ [{ type: "text", text: "done", citations: null } as Anthropic.TextBlock],
1031
+ ]);
1032
+
1033
+ const input = new PassThrough();
1034
+ input.write("go\n");
1035
+ input.end();
1036
+
1037
+ await runChatLoop({
1038
+ model: "test-model",
1039
+ instructions: "test",
1040
+ _adapter: adapter,
1041
+ input,
1042
+ tools: [bigTool],
1043
+ permissionMode: "bypass",
1044
+ });
1045
+
1046
+ // The 2nd call's tool_result content must be a preview, not the full payload.
1047
+ const secondCall = capturedMessages()[1] ?? [];
1048
+ const userMsg = secondCall.find((m) => m.role === "user" && Array.isArray(m.content));
1049
+ const content = (userMsg?.content as Anthropic.ToolResultBlockParam[])[0]?.content;
1050
+ const preview = typeof content === "string" ? content : "";
1051
+ expect(preview.length).toBeLessThan(bigPayload.length);
1052
+ expect(preview).toContain("[truncated, full output at ");
1053
+ expect(preview).toContain(".crewhaus/tool-results/");
1054
+
1055
+ // The corresponding file exists with the full payload on disk.
1056
+ const match = preview.match(/full output at (.+?)\]$/);
1057
+ expect(match).not.toBeNull();
1058
+ const fullPath = match?.[1] ?? "";
1059
+ expect(statSync(fullPath).size).toBe(bigPayload.length);
1060
+ } finally {
1061
+ process.chdir(oldCwd);
1062
+ rmSync(tmpRoot, { recursive: true, force: true });
1063
+ }
1064
+ });
1065
+ });
1066
+
1067
+ describe("runChatLoop — Section 8 streaming flag", () => {
1068
+ test("streaming: true dispatches tools mid-stream and round-trips results", async () => {
1069
+ const events: { id: string; phase: "started" | "finished" }[] = [];
1070
+ const tool = buildTool({
1071
+ name: "Read",
1072
+ description: "fake",
1073
+ inputSchema: z.object({ id: z.string() }),
1074
+ readOnly: true,
1075
+ concurrencySafe: true,
1076
+ execute: async (input) => {
1077
+ events.push({ id: input.id, phase: "started" });
1078
+ events.push({ id: input.id, phase: "finished" });
1079
+ return `read:${input.id}`;
1080
+ },
1081
+ });
1082
+
1083
+ // Section 17 — adapter that yields tool_use blocks via the
1084
+ // canonical StreamEvent shape on the first call, text on the
1085
+ // second. The runtime's streaming-tool-executor consumes these and
1086
+ // dispatches each tool_use as soon as content_block_stop fires.
1087
+ const blocks1 = [
1088
+ { type: "tool_use", id: "tu_1", name: "Read", input: { id: "1" } } as Anthropic.ToolUseBlock,
1089
+ { type: "tool_use", id: "tu_2", name: "Read", input: { id: "2" } } as Anthropic.ToolUseBlock,
1090
+ ];
1091
+ const blocks2 = [{ type: "text", text: "done", citations: null } as Anthropic.TextBlock];
1092
+ let streamIdx = 0;
1093
+ const captures: Anthropic.MessageParam[][] = [];
1094
+ const adapter: import("@crewhaus/adapter-anthropic").ProviderAdapter = {
1095
+ providerId: "anthropic",
1096
+ features: {
1097
+ caching: "explicit",
1098
+ tool_use: true,
1099
+ vision: true,
1100
+ thinking: true,
1101
+ web_search: true,
1102
+ },
1103
+ estimateTokens: () => 0,
1104
+ stream: (req) => {
1105
+ captures.push(req.messages.map((m) => ({ ...m })) as Anthropic.MessageParam[]);
1106
+ const content =
1107
+ streamIdx === 0
1108
+ ? (blocks1 as Anthropic.ContentBlock[])
1109
+ : (blocks2 as Anthropic.ContentBlock[]);
1110
+ streamIdx++;
1111
+ const hasToolUse = content.some((b) => b.type === "tool_use");
1112
+ return (async function* () {
1113
+ yield { kind: "message_start" } as const;
1114
+ for (let idx = 0; idx < content.length; idx++) {
1115
+ const block = content[idx];
1116
+ if (block === undefined) continue;
1117
+ if (block.type === "text") {
1118
+ yield {
1119
+ kind: "content_block_start",
1120
+ index: idx,
1121
+ block: { type: "text", text: "" },
1122
+ } as const;
1123
+ yield {
1124
+ kind: "content_block_delta",
1125
+ index: idx,
1126
+ delta: { type: "text_delta", text: block.text },
1127
+ } as const;
1128
+ yield { kind: "content_block_stop", index: idx } as const;
1129
+ } else if (block.type === "tool_use") {
1130
+ yield {
1131
+ kind: "content_block_start",
1132
+ index: idx,
1133
+ block: { type: "tool_use", id: block.id, name: block.name, input: {} },
1134
+ } as const;
1135
+ yield {
1136
+ kind: "content_block_delta",
1137
+ index: idx,
1138
+ delta: {
1139
+ type: "input_json_delta",
1140
+ partial_json: JSON.stringify(block.input ?? {}),
1141
+ },
1142
+ } as const;
1143
+ yield { kind: "content_block_stop", index: idx } as const;
1144
+ }
1145
+ }
1146
+ yield {
1147
+ kind: "message_delta",
1148
+ stopReason: hasToolUse ? "tool_use" : "end_turn",
1149
+ } as const;
1150
+ yield { kind: "message_stop" } as const;
1151
+ })();
1152
+ },
1153
+ };
1154
+
1155
+ const input = new PassThrough();
1156
+ input.write("go\n");
1157
+ input.end();
1158
+
1159
+ await runChatLoop({
1160
+ model: "test-model",
1161
+ instructions: "test",
1162
+ _adapter: adapter,
1163
+ input,
1164
+ tools: [tool],
1165
+ streaming: true,
1166
+ permissionMode: "bypass",
1167
+ });
1168
+
1169
+ // Both tools ran (executions captured).
1170
+ const startedIds = events.filter((e) => e.phase === "started").map((e) => e.id);
1171
+ expect(startedIds.sort()).toEqual(["1", "2"]);
1172
+
1173
+ // The 2nd model call saw the assistant turn + tool_results in messages.
1174
+ const secondCall = captures[1] ?? [];
1175
+ const userMsg = secondCall.find((m) => m.role === "user" && Array.isArray(m.content));
1176
+ expect(userMsg).toBeDefined();
1177
+ const trBlocks = (userMsg?.content as Anthropic.ToolResultBlockParam[]) ?? [];
1178
+ expect(trBlocks.map((b) => b.tool_use_id).sort()).toEqual(["tu_1", "tu_2"]);
1179
+ });
1180
+ });
1181
+
1182
+ // ---------------------------------------------------------------------------
1183
+ // Section 10 — runChatLoop --resume + persistence integration
1184
+ // ---------------------------------------------------------------------------
1185
+
1186
+ describe("runChatLoop — Section 10 persistence", () => {
1187
+ test("session JSON is persisted with lastTurnIndex on REPL exit", async () => {
1188
+ const ctx = createRunContext();
1189
+ const { adapter } = makeScriptedClient([
1190
+ [{ type: "text", text: "ack", citations: null } as Anthropic.TextBlock],
1191
+ ]);
1192
+ const input = new PassThrough();
1193
+ input.write("hello\n");
1194
+ input.end();
1195
+
1196
+ await runChatLoop({
1197
+ model: "test-model",
1198
+ instructions: "test",
1199
+ _adapter: adapter,
1200
+ input,
1201
+ runContext: ctx,
1202
+ sessionName: "spec-name",
1203
+ sessionTarget: "cli",
1204
+ });
1205
+
1206
+ // Session metadata file exists with the expected shape and the
1207
+ // turnNumber the loop incremented.
1208
+ const file = join(SHARED_SESSION_ROOT, `${ctx.sessionId}.json`);
1209
+ const { readFile } = await import("node:fs/promises");
1210
+ const json = JSON.parse(await readFile(file, "utf8"));
1211
+ expect(json.id).toBe(ctx.sessionId);
1212
+ expect(json.name).toBe("spec-name");
1213
+ expect(json.target).toBe("cli");
1214
+ expect(json.model).toBe("test-model");
1215
+ expect(json.lastTurnIndex).toBe(ctx.turnNumber);
1216
+ expect(json.lastTurnIndex).toBeGreaterThanOrEqual(1);
1217
+ });
1218
+
1219
+ test("replayMessageHistory skips events bracketed by a2a_turn / sub_agent markers", async () => {
1220
+ // The parent role's tool_use (e.g. SendMessage) followed by the
1221
+ // peer's nested transcript would otherwise produce an unpaired
1222
+ // tool_use in the replayed history — Claude API rejects with
1223
+ // `tool_use ids were found without tool_result blocks immediately
1224
+ // after`. The marker pair lets replay skip the peer's events while
1225
+ // keeping the parent's tool_use / tool_result adjacent.
1226
+ const sessionId = "sess_bbbbbbbbbbbbbbbb";
1227
+ const { openEventLog } = await import("@crewhaus/event-log");
1228
+ const log = await openEventLog(sessionId, { rootDir: SHARED_SESSION_ROOT });
1229
+ // Parent emits a tool_use SendMessage in an assistant message.
1230
+ await log.append({ kind: "user_message", payload: { content: "begin" } });
1231
+ await log.append({
1232
+ kind: "assistant_message",
1233
+ payload: {
1234
+ content: [
1235
+ {
1236
+ type: "tool_use",
1237
+ id: "tu_send",
1238
+ name: "SendMessage",
1239
+ input: { target: "critic", payload: "q?" },
1240
+ },
1241
+ ],
1242
+ },
1243
+ });
1244
+ // A2A peer turn starts → these user/assistant events must be skipped.
1245
+ await log.append({ kind: "a2a_turn_start", payload: { from: "writer", to: "critic" } });
1246
+ await log.append({
1247
+ kind: "user_message",
1248
+ payload: { content: "[A2A from writer → critic]\n\nq?" },
1249
+ });
1250
+ await log.append({
1251
+ kind: "assistant_message",
1252
+ payload: { content: [{ type: "text", text: "critic answer", citations: null }] },
1253
+ });
1254
+ await log.append({ kind: "a2a_turn_end", payload: { from: "writer", to: "critic" } });
1255
+ // Parent receives the SendMessage tool_result.
1256
+ await log.append({
1257
+ kind: "user_message",
1258
+ payload: {
1259
+ content: [
1260
+ {
1261
+ type: "tool_result",
1262
+ tool_use_id: "tu_send",
1263
+ content: "critic answer",
1264
+ is_error: false,
1265
+ },
1266
+ ],
1267
+ },
1268
+ });
1269
+ await log.append({
1270
+ kind: "assistant_message",
1271
+ payload: { content: [{ type: "text", text: "final from writer", citations: null }] },
1272
+ });
1273
+ await log.close();
1274
+
1275
+ const reopened = await openEventLog(sessionId, { rootDir: SHARED_SESSION_ROOT });
1276
+ const replayed = await replayMessageHistory(reopened);
1277
+ await reopened.close();
1278
+
1279
+ // 4 messages: user "begin", assistant (tool_use), user (tool_result), assistant (final).
1280
+ // Peer's user/assistant in the middle are skipped.
1281
+ expect(replayed.length).toBe(4);
1282
+ expect(replayed[0]).toEqual({ role: "user", content: "begin" });
1283
+ expect(replayed[1]?.role).toBe("assistant");
1284
+ const block1 = (replayed[1]?.content as Anthropic.ContentBlock[])[0] as { type: string };
1285
+ expect(block1?.type).toBe("tool_use");
1286
+ expect(replayed[2]?.role).toBe("user");
1287
+ const block2 = (replayed[2]?.content as Anthropic.ToolResultBlockParam[])[0];
1288
+ expect(block2?.type).toBe("tool_result");
1289
+ expect(block2?.tool_use_id).toBe("tu_send");
1290
+ expect(replayed[3]?.role).toBe("assistant");
1291
+ });
1292
+
1293
+ test("replayMessageHistory tolerates nested a2a brackets (peer of a peer)", async () => {
1294
+ // If a peer's inline runChatLoop itself drives another A2A call,
1295
+ // the markers nest. Depth-counting keeps any nesting level skipped
1296
+ // until the outermost a2a_turn_end is seen.
1297
+ const sessionId = "sess_cccccccccccccccc";
1298
+ const { openEventLog } = await import("@crewhaus/event-log");
1299
+ const log = await openEventLog(sessionId, { rootDir: SHARED_SESSION_ROOT });
1300
+ await log.append({ kind: "user_message", payload: { content: "outer" } });
1301
+ await log.append({ kind: "a2a_turn_start", payload: { from: "a", to: "b" } });
1302
+ await log.append({ kind: "user_message", payload: { content: "level-1" } });
1303
+ await log.append({ kind: "a2a_turn_start", payload: { from: "b", to: "c" } });
1304
+ await log.append({ kind: "user_message", payload: { content: "level-2" } });
1305
+ await log.append({
1306
+ kind: "assistant_message",
1307
+ payload: { content: [{ type: "text", text: "level-2 reply", citations: null }] },
1308
+ });
1309
+ await log.append({ kind: "a2a_turn_end", payload: { from: "b", to: "c" } });
1310
+ await log.append({
1311
+ kind: "assistant_message",
1312
+ payload: { content: [{ type: "text", text: "level-1 reply", citations: null }] },
1313
+ });
1314
+ await log.append({ kind: "a2a_turn_end", payload: { from: "a", to: "b" } });
1315
+ await log.append({
1316
+ kind: "assistant_message",
1317
+ payload: { content: [{ type: "text", text: "outer reply", citations: null }] },
1318
+ });
1319
+ await log.close();
1320
+
1321
+ const reopened = await openEventLog(sessionId, { rootDir: SHARED_SESSION_ROOT });
1322
+ const replayed = await replayMessageHistory(reopened);
1323
+ await reopened.close();
1324
+
1325
+ // Only the outer user message and outer assistant reply survive.
1326
+ expect(replayed.length).toBe(2);
1327
+ expect(replayed[0]).toEqual({ role: "user", content: "outer" });
1328
+ expect(replayed[1]?.role).toBe("assistant");
1329
+ });
1330
+
1331
+ test("T4 replay: replayMessageHistory rebuilds an identical history from the JSONL", async () => {
1332
+ // Hand-craft a 4-message transcript via direct event-log appends so
1333
+ // the replay algorithm is exercised in isolation. tool_use /
1334
+ // tool_result events are interleaved as audit-only and must NOT
1335
+ // contribute to the reconstructed message history.
1336
+ const sessionId = "sess_aaaaaaaaaaaaaaaa";
1337
+ const { openEventLog } = await import("@crewhaus/event-log");
1338
+ const log = await openEventLog(sessionId, { rootDir: SHARED_SESSION_ROOT });
1339
+ await log.append({ kind: "user_message", payload: { content: "ping 1" } });
1340
+ await log.append({
1341
+ kind: "assistant_message",
1342
+ payload: { content: [{ type: "text", text: "pong 1", citations: null }] },
1343
+ });
1344
+ await log.append({ kind: "tool_use", payload: { id: "tu_1", name: "noop", input: {} } });
1345
+ await log.append({
1346
+ kind: "tool_result",
1347
+ payload: { toolUseId: "tu_1", content: "ok", isError: false },
1348
+ });
1349
+ await log.append({ kind: "user_message", payload: { content: "ping 2" } });
1350
+ await log.append({
1351
+ kind: "assistant_message",
1352
+ payload: { content: [{ type: "text", text: "pong 2", citations: null }] },
1353
+ });
1354
+ await log.append({ kind: "error", payload: { name: "Whatever", message: "transient" } });
1355
+ await log.close();
1356
+
1357
+ const reopened = await openEventLog(sessionId, { rootDir: SHARED_SESSION_ROOT });
1358
+ const replayed = await replayMessageHistory(reopened);
1359
+ await reopened.close();
1360
+
1361
+ expect(replayed.length).toBe(4);
1362
+ expect(replayed[0]).toEqual({ role: "user", content: "ping 1" });
1363
+ expect(replayed[1]?.role).toBe("assistant");
1364
+ expect((replayed[1]?.content as Anthropic.ContentBlock[])[0]?.type).toBe("text");
1365
+ expect(replayed[2]).toEqual({ role: "user", content: "ping 2" });
1366
+ expect(replayed[3]?.role).toBe("assistant");
1367
+ });
1368
+
1369
+ test("--resume threads the prior transcript into the next model call", async () => {
1370
+ // Run A: one user→assistant turn, persisted to its session file.
1371
+ const ctxA = createRunContext();
1372
+ const recorded = makeScriptedClient([
1373
+ [{ type: "text", text: "answer-1", citations: null } as Anthropic.TextBlock],
1374
+ ]);
1375
+ const inputA = new PassThrough();
1376
+ inputA.write("first message\n");
1377
+ inputA.end();
1378
+ await runChatLoop({
1379
+ model: "test-model",
1380
+ instructions: "test",
1381
+ _adapter: recorded.adapter,
1382
+ input: inputA,
1383
+ runContext: ctxA,
1384
+ });
1385
+
1386
+ // Run B: resume the same session, send one more user message. The
1387
+ // model's first call must see [user1, assistant1, user2] — proving
1388
+ // event-log replay reconstructed history and the runtime threaded
1389
+ // it into messages BEFORE pushing the new user input.
1390
+ const sessionId = ctxA.sessionId;
1391
+ const resumed = makeScriptedClient([
1392
+ [{ type: "text", text: "answer-2", citations: null } as Anthropic.TextBlock],
1393
+ ]);
1394
+ const ctxB = createRunContext({ sessionId });
1395
+ const inputB = new PassThrough();
1396
+ inputB.write("second message\n");
1397
+ inputB.end();
1398
+ await runChatLoop({
1399
+ model: "test-model",
1400
+ instructions: "test",
1401
+ _adapter: resumed.adapter,
1402
+ input: inputB,
1403
+ runContext: ctxB,
1404
+ resume: { sessionId },
1405
+ });
1406
+
1407
+ const firstCall = resumed.capturedMessages()[0] ?? [];
1408
+ expect(firstCall.length).toBe(3);
1409
+ expect(firstCall[0]?.role).toBe("user");
1410
+ expect(firstCall[0]?.content).toBe("first message");
1411
+ expect(firstCall[1]?.role).toBe("assistant");
1412
+ expect(firstCall[2]?.role).toBe("user");
1413
+ expect(firstCall[2]?.content).toBe("second message");
1414
+ });
1415
+
1416
+ test("--resume on a missing session throws RuntimeError", async () => {
1417
+ const { adapter } = makeScriptedClient([]);
1418
+ const input = new PassThrough();
1419
+ input.end();
1420
+ await expect(
1421
+ runChatLoop({
1422
+ model: "test-model",
1423
+ instructions: "test",
1424
+ _adapter: adapter,
1425
+ input,
1426
+ resume: { sessionId: "sess_0000000000000000" },
1427
+ }),
1428
+ ).rejects.toThrow(/cannot --resume/);
1429
+ });
1430
+
1431
+ test("rejects resume + seedMessages combo as mutually exclusive in REPL mode", async () => {
1432
+ const { adapter } = makeScriptedClient([]);
1433
+ await expect(
1434
+ runChatLoop({
1435
+ model: "test-model",
1436
+ instructions: "test",
1437
+ _adapter: adapter,
1438
+ input: new PassThrough(),
1439
+ resume: { sessionId: "sess_0000000000000000" },
1440
+ seedMessages: [{ role: "user", content: "hi" }],
1441
+ }),
1442
+ ).rejects.toThrow(/mutually exclusive/);
1443
+ });
1444
+
1445
+ // Section 12 — channel-bot session router uses singleTurn + resume to
1446
+ // "resume the thread, append the inbound message, run one turn". Verifies
1447
+ // (a) the model receives prior history + new seed, and (b) the event log
1448
+ // gains exactly the new turn's messages — replayed history is NOT re-logged.
1449
+ test("singleTurn + resume threads prior history without re-logging", async () => {
1450
+ // Run A: one REPL turn, persisted to its session.
1451
+ const ctxA = createRunContext();
1452
+ const recordedA = makeScriptedClient([
1453
+ [{ type: "text", text: "reply-1", citations: null } as Anthropic.TextBlock],
1454
+ ]);
1455
+ const inputA = new PassThrough();
1456
+ inputA.write("first\n");
1457
+ inputA.end();
1458
+ await runChatLoop({
1459
+ model: "test-model",
1460
+ instructions: "test",
1461
+ _adapter: recordedA.adapter,
1462
+ input: inputA,
1463
+ runContext: ctxA,
1464
+ });
1465
+
1466
+ const { openEventLog } = await import("@crewhaus/event-log");
1467
+ const beforeLog = await openEventLog(ctxA.sessionId, { rootDir: SHARED_SESSION_ROOT });
1468
+ const beforeEvents: Array<{ kind: string }> = [];
1469
+ for await (const ev of beforeLog.read()) beforeEvents.push({ kind: ev.kind });
1470
+ await beforeLog.close();
1471
+ const beforeUser = beforeEvents.filter((e) => e.kind === "user_message").length;
1472
+ const beforeAsst = beforeEvents.filter((e) => e.kind === "assistant_message").length;
1473
+ expect(beforeUser).toBe(1);
1474
+ expect(beforeAsst).toBe(1);
1475
+
1476
+ // Run B: singleTurn + resume + new seed (the channel-bot pattern).
1477
+ const sessionId = ctxA.sessionId;
1478
+ const recordedB = makeScriptedClient([
1479
+ [{ type: "text", text: "reply-2", citations: null } as Anthropic.TextBlock],
1480
+ ]);
1481
+ const ctxB = createRunContext({ sessionId });
1482
+ const reply = await runChatLoop({
1483
+ model: "test-model",
1484
+ instructions: "test",
1485
+ _adapter: recordedB.adapter,
1486
+ runContext: ctxB,
1487
+ resume: { sessionId },
1488
+ singleTurn: true,
1489
+ seedMessages: [{ role: "user", content: "second" }],
1490
+ });
1491
+ expect(reply).toBe("reply-2");
1492
+
1493
+ // Model received [u1, a1, u2] — replayed prefix + new seed.
1494
+ const firstCall = recordedB.capturedMessages()[0] ?? [];
1495
+ expect(firstCall.length).toBe(3);
1496
+ expect(firstCall[0]?.role).toBe("user");
1497
+ expect(firstCall[0]?.content).toBe("first");
1498
+ expect(firstCall[1]?.role).toBe("assistant");
1499
+ expect(firstCall[2]?.role).toBe("user");
1500
+ expect(firstCall[2]?.content).toBe("second");
1501
+
1502
+ // Event log gained exactly +1 user_message + +1 assistant_message —
1503
+ // replayed messages must not be re-logged.
1504
+ const afterLog = await openEventLog(sessionId, { rootDir: SHARED_SESSION_ROOT });
1505
+ const afterEvents: Array<{ kind: string }> = [];
1506
+ for await (const ev of afterLog.read()) afterEvents.push({ kind: ev.kind });
1507
+ await afterLog.close();
1508
+ const afterUser = afterEvents.filter((e) => e.kind === "user_message").length;
1509
+ const afterAsst = afterEvents.filter((e) => e.kind === "assistant_message").length;
1510
+ expect(afterUser - beforeUser).toBe(1);
1511
+ expect(afterAsst - beforeAsst).toBe(1);
1512
+ });
1513
+
1514
+ test("event log captures user_message + assistant_message events", async () => {
1515
+ const ctx = createRunContext();
1516
+ const { adapter } = makeScriptedClient([
1517
+ [{ type: "text", text: "thanks", citations: null } as Anthropic.TextBlock],
1518
+ ]);
1519
+ const input = new PassThrough();
1520
+ input.write("ping\n");
1521
+ input.end();
1522
+ await runChatLoop({
1523
+ model: "test-model",
1524
+ instructions: "test",
1525
+ _adapter: adapter,
1526
+ input,
1527
+ runContext: ctx,
1528
+ });
1529
+
1530
+ const { openEventLog } = await import("@crewhaus/event-log");
1531
+ const log = await openEventLog(ctx.sessionId, { rootDir: SHARED_SESSION_ROOT });
1532
+ const all: Array<{ kind: string; payload: unknown }> = [];
1533
+ for await (const ev of log.read()) all.push({ kind: ev.kind, payload: ev.payload });
1534
+ await log.close();
1535
+
1536
+ const kinds = all.map((e) => e.kind);
1537
+ expect(kinds).toContain("user_message");
1538
+ expect(kinds).toContain("assistant_message");
1539
+ expect(kinds.indexOf("user_message")).toBeLessThan(kinds.indexOf("assistant_message"));
1540
+ });
1541
+ });