@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.
package/src/index.ts ADDED
@@ -0,0 +1,2025 @@
1
+ import * as readline from "node:readline/promises";
2
+ import type Anthropic from "@anthropic-ai/sdk";
3
+ import { type AbortTree, createAbortTree } from "@crewhaus/abort-controller";
4
+ import {
5
+ type ProviderAdapter,
6
+ type ProviderId,
7
+ collectFinalMessage,
8
+ consumeStream,
9
+ } from "@crewhaus/adapter-anthropic";
10
+ import type {
11
+ CrewMailbox,
12
+ RuntimeBridge,
13
+ SpawnSubAgentFn,
14
+ SubAgentDefinition,
15
+ } from "@crewhaus/agent-context-isolation";
16
+ import { type WrappedAdapter, wrap as wrapWithCircuitBreaker } from "@crewhaus/circuit-breaker";
17
+ import { autoCompact } from "@crewhaus/compaction-autocompact";
18
+ import { snip } from "@crewhaus/compaction-snip";
19
+ import { type EgressVerdict, classifyEgress, summarizeEgress } from "@crewhaus/egress-classifier";
20
+ import { RuntimeError } from "@crewhaus/errors";
21
+ import { type EventKind, type EventLog, openEventLog } from "@crewhaus/event-log";
22
+ import { type HookDef, type HookEvent, aggregateDecisions, runHooks } from "@crewhaus/hooks-engine";
23
+ import { resolveModel } from "@crewhaus/model-router";
24
+ import {
25
+ BUILTIN_DEFAULT_RULES,
26
+ type JustificationJudge,
27
+ type PermissionMode,
28
+ type RuleSet,
29
+ emptyRuleSet,
30
+ evaluateJustification,
31
+ evaluateWithReason,
32
+ ruleBasedJustificationJudge,
33
+ } from "@crewhaus/permission-engine";
34
+ import { manage as manageCacheMarkers } from "@crewhaus/prompt-cache-manager";
35
+ import {
36
+ buildRedactionNotice,
37
+ classifyText,
38
+ llmClassifierEnabled,
39
+ } from "@crewhaus/prompt-injection-detector";
40
+ import type { RateLimiter } from "@crewhaus/rate-limiter";
41
+ import {
42
+ type RecoveryState,
43
+ advanceState,
44
+ initialRecoveryState,
45
+ recover,
46
+ } from "@crewhaus/recovery-engine";
47
+ import { type RunContext, createRunContext, tagContent } from "@crewhaus/run-context";
48
+ import { type SessionStore, createSessionStore } from "@crewhaus/session-store";
49
+ import { type SkillRef, formatSkillsForPrompt } from "@crewhaus/skills-registry";
50
+ import { type SlashCommand, expand as expandSlash } from "@crewhaus/slash-commands";
51
+ import { type Store, createStore } from "@crewhaus/state-store";
52
+ import { executeStreaming } from "@crewhaus/streaming-tool-executor";
53
+ import { TokenBudget, estimateTokens } from "@crewhaus/token-budget";
54
+ import type { RegisteredTool } from "@crewhaus/tool-catalog";
55
+ import { executeTool } from "@crewhaus/tool-executor";
56
+ import { type LoopDetection, detectLoop } from "@crewhaus/tool-loop-detection";
57
+ import { partitionToolCalls } from "@crewhaus/tool-orchestrator";
58
+ import { storeAndPreview } from "@crewhaus/tool-result-store";
59
+ import { TraceEventBus } from "@crewhaus/trace-event-bus";
60
+ import {
61
+ type ToolUseBlock as TsmToolUseBlock,
62
+ type TurnState,
63
+ initialState,
64
+ transition,
65
+ } from "@crewhaus/turn-state-machine";
66
+ import { zodToJsonSchema } from "zod-to-json-schema";
67
+ import {
68
+ type StreamRenderer,
69
+ createCliMarkdownRenderer,
70
+ isCliMarkdownEnabled,
71
+ } from "./cli-markdown";
72
+ import { attachDefaultSubscribers } from "./observability";
73
+ import { loadProjectMemory } from "./project-memory";
74
+
75
+ /**
76
+ * Slice-scope runtime: a multi-turn streaming chat loop with prompt
77
+ * caching, tool execution, pre-turn context compaction, recovery taxonomy,
78
+ * permission gating, cooperative cancellation, and a partitioned/streaming
79
+ * tool layer. Maps to catalog R1 runtime-orchestrator + R1 recovery-engine
80
+ * + R8 permission-engine + R1 abort-controller + R3 tool-orchestrator /
81
+ * tool-loop-detection / tool-result-store / streaming-tool-executor, plus
82
+ * R2 model-adapter (Anthropic only).
83
+ *
84
+ * Auth resolution: ANTHROPIC_AUTH_TOKEN (Claude Pro/Max OAuth) takes
85
+ * precedence over ANTHROPIC_API_KEY (pay-per-token API key). OAuth tokens
86
+ * are detected by the `sk-ant-oat` prefix and given the beta + identity
87
+ * headers required to route through subscription billing.
88
+ *
89
+ * Tool support: when `tools` is provided, the loop forwards the tool
90
+ * definitions (Zod → JSON Schema) to the model, runs each tool through
91
+ * the permission engine, then executes via @crewhaus/tool-executor before
92
+ * re-calling the model. The state machine reaches Done(no_tools) when the
93
+ * assistant returns no further tool_use blocks for a turn.
94
+ *
95
+ * Tool layer enrichment (Section 8): tool execution is partitioned via
96
+ * `@crewhaus/tool-orchestrator` so concurrency-safe read-only calls run
97
+ * via `Promise.all` while destructive calls run serially. After each
98
+ * batch, `@crewhaus/tool-loop-detection` scans the per-run history; on a
99
+ * hit, a synthetic warning user message is appended (deduped per
100
+ * signature) so the model can self-correct. Every tool result flows
101
+ * through `@crewhaus/tool-result-store` — outputs over 10 KB are
102
+ * persisted to `.crewhaus/tool-results/<runId>/<toolUseId>.txt` and the
103
+ * model sees a preview pointing at the full file. Behind a
104
+ * `streaming: true` option, the loop swaps to
105
+ * `@crewhaus/streaming-tool-executor`, which dispatches tools mid-stream
106
+ * via the SDK's `contentBlock` event.
107
+ *
108
+ * Compaction: at the start of each user turn, estimate the conversation's
109
+ * token count against `contextLimit` (default 200_000). When usage crosses
110
+ * `compactionThreshold` (default 0.85), apply the cheap snip first, then
111
+ * fall back to autocompact if still over. Reactive compaction is also
112
+ * triggered in-turn when the recovery engine returns a `compact` action.
113
+ *
114
+ * Recovery: every model call is wrapped in try/catch. Errors flow through
115
+ * the `recovery-engine` taxonomy: prompt_too_long → reactive compact;
116
+ * max_output_tokens → continue; overloaded/5xx → exponential-backoff
117
+ * retry; invalid_request → tombstone. Budgets enforced per turn.
118
+ *
119
+ * Permissions: every tool_use is evaluated by `permission-engine`. In
120
+ * default and auto modes, an `ask` decision prompts the user via stdin in
121
+ * REPL mode (single-turn mode treats `ask` as deny — non-interactive).
122
+ *
123
+ * Cancellation: the `runContext.abortSignal` becomes the root of an abort
124
+ * tree; each turn gets a child, each tool call gets a grandchild. First
125
+ * SIGINT cancels the current turn; second SIGINT exits the process.
126
+ *
127
+ * Persistence (Section 10): every `runChatLoop` invocation either creates
128
+ * a new session via `@crewhaus/session-store` or resumes an existing one
129
+ * (`opts.resume`); the session's `.json` metadata file lives at
130
+ * `.crewhaus/sessions/<id>.json` alongside an append-only `.jsonl`
131
+ * transcript opened via `@crewhaus/event-log`. Every user input,
132
+ * assistant turn, tool_use, tool_result, recovery error, and compaction
133
+ * is appended as one JSON line. On `--resume`, the runtime walks the
134
+ * `.jsonl` and replays `user_message` + `assistant_message` events into
135
+ * the message history before the loop starts. A per-run
136
+ * `@crewhaus/state-store` is also instantiated as a coordination surface
137
+ * for hooks/skills/tools (consumed by Section 11+). Eviction: any
138
+ * session whose `.json` mtime is older than 30 days is purged on the
139
+ * next `sessionStore.list()` (called once at the top of every
140
+ * `runChatLoop`).
141
+ */
142
+
143
+ const DEFAULT_CONTEXT_LIMIT = 200_000;
144
+ const DEFAULT_COMPACTION_THRESHOLD = 0.85;
145
+ const DEFAULT_SNIP_KEEP_HEAD = 4;
146
+ const DEFAULT_SNIP_KEEP_TAIL = 20;
147
+
148
+ // Section 17 — `resolveAuth` / `createAnthropicClient` / `ResolvedAuth` /
149
+ // `OAUTH_BETAS` / `CLAUDE_CODE_HEADERS` / `CLAUDE_CODE_SYSTEM_PREFIX`
150
+ // moved into `@crewhaus/adapter-anthropic`. Re-exported here so legacy
151
+ // importers (eval-judge fallback, downstream tests) keep compiling.
152
+ export {
153
+ CLAUDE_CODE_HEADERS,
154
+ CLAUDE_CODE_SYSTEM_PREFIX,
155
+ OAUTH_BETAS,
156
+ createAnthropicClient,
157
+ resolveAuth,
158
+ } from "@crewhaus/adapter-anthropic";
159
+ export type { ResolvedAuth } from "@crewhaus/adapter-anthropic";
160
+
161
+ /**
162
+ * Walk an `EventLog` and reconstruct the SDK message history. Only
163
+ * `user_message` and `assistant_message` events contribute — `tool_use`
164
+ * and `tool_result` events are audit-only because the same data already
165
+ * lives inside the assistant/user message content arrays. `error` and
166
+ * `compaction` events are observability-only and skipped.
167
+ *
168
+ * Exported so consumers (the `--resume` path in `apps/cli`, the T4
169
+ * replay test, and any future inspector) can reproduce the same history
170
+ * the runtime would build.
171
+ */
172
+ export async function replayMessageHistory(log: EventLog): Promise<Anthropic.MessageParam[]> {
173
+ const messages: Anthropic.MessageParam[] = [];
174
+ // Crew sessions and sub-agent spawners share one append-only log.
175
+ // Events emitted between a2a_turn_start / a2a_turn_end (Section 22)
176
+ // or sub_agent_start / sub_agent_end (Section 13) are nested
177
+ // transcripts that should NOT bleed into the outer role's history —
178
+ // their `user_message` and `assistant_message` events would otherwise
179
+ // sit between the parent's tool_use and its tool_result, breaking
180
+ // Claude API's adjacency requirement. Track nesting depth and only
181
+ // surface user/assistant events at depth 0.
182
+ let depth = 0;
183
+ for await (const ev of log.read()) {
184
+ if (ev.kind === "a2a_turn_start" || ev.kind === "sub_agent_start") {
185
+ depth += 1;
186
+ continue;
187
+ }
188
+ if (ev.kind === "a2a_turn_end" || ev.kind === "sub_agent_end") {
189
+ depth = Math.max(0, depth - 1);
190
+ continue;
191
+ }
192
+ if (depth > 0) continue;
193
+ if (ev.kind === "user_message") {
194
+ const p = ev.payload as { content: Anthropic.MessageParam["content"] };
195
+ messages.push({ role: "user", content: p.content });
196
+ } else if (ev.kind === "assistant_message") {
197
+ const p = ev.payload as { content: Anthropic.MessageParam["content"] };
198
+ messages.push({ role: "assistant", content: p.content });
199
+ }
200
+ }
201
+ return messages;
202
+ }
203
+
204
+ export type RunChatLoopOptions = {
205
+ model: string;
206
+ instructions: string;
207
+ maxTokens?: number;
208
+ /**
209
+ * Section 17 — optional override for the model used by
210
+ * `compaction-autocompact`. When undefined, compaction reuses
211
+ * `opts.model` (and therefore the same adapter). Set to a different
212
+ * model string (or even a different provider via the prefix
213
+ * conventions, e.g. `openai/gpt-4o-mini`) to route compaction
214
+ * elsewhere.
215
+ */
216
+ compactionModel?: string;
217
+ /**
218
+ * Test injection backdoor: pre-built ProviderAdapter that bypasses
219
+ * the model-router. When set, `model` is still the user-facing
220
+ * `agent.model` (used for trace events and as the `req.model` field
221
+ * passed to the adapter), but no router lookup happens.
222
+ *
223
+ * Used by sub-agent-spawner.test.ts and runtime-core.test.ts to
224
+ * inject scripted adapters; replaces the previous `client` /
225
+ * `isOAuth` injection contract.
226
+ */
227
+ _adapter?: ProviderAdapter;
228
+ /**
229
+ * Section 17 — same as `_adapter` but for the compaction model.
230
+ * When omitted, defaults to `_adapter` (or the routed primary).
231
+ */
232
+ _compactionAdapter?: ProviderAdapter;
233
+ /** Override the input stream (testing). Defaults to process.stdin. */
234
+ input?: NodeJS.ReadableStream;
235
+ /** Tools the model may invoke. When empty/undefined, tools are not advertised. */
236
+ tools?: ReadonlyArray<RegisteredTool>;
237
+ /** Per-run identity, abort signal, and logger. Defaults to a fresh `createRunContext()`. */
238
+ runContext?: RunContext;
239
+ /** Context-window limit in tokens. Defaults to 200_000 (Claude opus/sonnet 4.x). */
240
+ contextLimit?: number;
241
+ /** Fraction of `contextLimit` at which compaction kicks in. Defaults to 0.85. */
242
+ compactionThreshold?: number;
243
+ /** Number of head messages to keep when snipping. Defaults to 4. */
244
+ snipKeepHead?: number;
245
+ /** Number of tail messages to keep when snipping. Defaults to 20. */
246
+ snipKeepTail?: number;
247
+ /**
248
+ * Pre-loaded conversation history. In `singleTurn` mode this is the
249
+ * entire input; the last entry must be `role: "user"`.
250
+ */
251
+ seedMessages?: ReadonlyArray<Anthropic.MessageParam>;
252
+ /**
253
+ * Single-shot mode: run exactly one user→assistant turn (with the tool
254
+ * inner-loop until Done) using `seedMessages`, then return the terminal
255
+ * assistant text. Skips the stdin REPL entirely. Used by the workflow
256
+ * target to run each step as a discrete turn.
257
+ */
258
+ singleTurn?: boolean;
259
+ /**
260
+ * Permission mode. Defaults to "default" (ask for tools without an
261
+ * explicit allow rule). The `bypass` mode is only legal when supplied
262
+ * directly by the runtime caller (the CLI flag parser); the spec/config
263
+ * loaders reject bypass at parse time.
264
+ */
265
+ permissionMode?: PermissionMode;
266
+ /** Layered rule sources. Defaults to `{ ..., builtin: BUILTIN_DEFAULT_RULES }`. */
267
+ permissionRules?: RuleSet;
268
+ /**
269
+ * Install a SIGINT handler that aborts the current turn on first press
270
+ * and exits on the second. Defaults to true in REPL mode when stdin is
271
+ * a TTY; defaults to false otherwise (singleTurn, piped input, tests).
272
+ */
273
+ installSigintHandler?: boolean;
274
+ /**
275
+ * When true, drive the model stream with `@crewhaus/streaming-tool-executor`
276
+ * so tool execution starts as soon as each `tool_use` block completes
277
+ * (mid-stream) rather than waiting for the full response. Default: false.
278
+ */
279
+ streaming?: boolean;
280
+ /**
281
+ * Override the directory under which session metadata `.json` files and
282
+ * event-log `.jsonl` files live. Defaults to `.crewhaus/sessions`.
283
+ */
284
+ sessionRootDir?: string;
285
+ /**
286
+ * Human-readable label for new sessions; persisted to the session JSON.
287
+ * Typically the spec `name`. Defaults to `(unnamed)`.
288
+ */
289
+ sessionName?: string;
290
+ /**
291
+ * Target shape this session belongs to (`"cli"`, `"workflow"`, …); persisted
292
+ * to the session JSON for future filtering. Defaults to `"cli"`.
293
+ */
294
+ sessionTarget?: string;
295
+ /**
296
+ * Resume an existing session: load its metadata, replay its event log
297
+ * into the message history (only `user_message` + `assistant_message`
298
+ * events; tool_use/tool_result are audit-only and already nested inside
299
+ * those messages), then continue the loop. Mutually exclusive with
300
+ * `seedMessages` (the resume payload becomes the seed). When the caller
301
+ * supplies a `runContext`, its `sessionId` must already match
302
+ * `resume.sessionId`; otherwise the runtime reseats the sessionId in
303
+ * the run context for this run.
304
+ */
305
+ resume?: { readonly sessionId: string };
306
+ /**
307
+ * Section 11 — lifecycle hooks discovered from `.crewhaus/settings.json`.
308
+ * The runtime fires events at key moments (session-start, pre/post tool,
309
+ * pre/post model, pre/post compact, pre-slash, stop). A `deny` decision
310
+ * on `pre-tool` short-circuits with the hook's reason as the result; on
311
+ * `pre-model` it appends `[blocked by hook]` and ends the turn.
312
+ */
313
+ hooks?: ReadonlyArray<HookDef>;
314
+ /**
315
+ * Section 11 — discovered skills, advertised in the system prompt. The
316
+ * caller is responsible for adding `createSkillTool(skills)` to the
317
+ * `tools` array; runtime-core only formats them into the system block.
318
+ */
319
+ skills?: ReadonlyArray<SkillRef>;
320
+ /**
321
+ * Section 11 — slash-command registry. When the user input starts with
322
+ * `/<name>` and `<name>` is in this map, the body is expanded with
323
+ * `$ARGUMENTS` substitution before being sent to the model. Fires
324
+ * `pre-slash` first; deny falls through to the original input.
325
+ */
326
+ slashCommands?: ReadonlyMap<string, SlashCommand>;
327
+ /**
328
+ * Section 13 — inline sub-agent definitions exposed via the `Task` tool.
329
+ * Threaded into the `RuntimeBridge` and surfaced to framework-aware
330
+ * tools (only `Task` today). Codegen sets this from the IR; ordinary
331
+ * runChatLoop callers leave it undefined.
332
+ */
333
+ subAgents?: ReadonlyMap<string, SubAgentDefinition>;
334
+ /**
335
+ * Section 13 — sub-agent spawner injection. Codegen passes
336
+ * `spawnSubAgent` from `@crewhaus/sub-agent-spawner`. The runtime stamps
337
+ * it onto the bridge; the Task tool dispatches via it. Inverted-DI to
338
+ * avoid a runtime-core → sub-agent-spawner cycle (the spawner consumes
339
+ * runChatLoop).
340
+ */
341
+ spawnSubAgent?: SpawnSubAgentFn;
342
+ /**
343
+ * Section 22 — crew orchestrator injection. The orchestrator
344
+ * implements `CrewMailbox` and threads itself in here per role-turn so
345
+ * the in-band Handoff and A2A SendMessage tools can record intent /
346
+ * make synchronous peer requests through `ctx.bridge.crewMailbox`.
347
+ * Inverted-DI to avoid a runtime-core → crew-orchestrator cycle (the
348
+ * orchestrator consumes runChatLoop).
349
+ */
350
+ crewMailbox?: CrewMailbox;
351
+ /**
352
+ * Section 18 — set true when the bundle has wired a non-noop sandbox
353
+ * backend. The permission engine refuses to grant `allow` for tools
354
+ * with `requiresSandbox: true` unless this is set. Codegen flips this
355
+ * automatically when the spec lists any `requiresSandbox` tool.
356
+ */
357
+ sandboxAvailable?: boolean;
358
+ /**
359
+ * Section 18 — when set, suspicious or malicious tool outputs are
360
+ * classified by an LLM after the regex/structural layers. The runtime
361
+ * supplies the function; the prompt-injection-detector calls it as
362
+ * a best-effort tier-3. When omitted (the common case), the classifier
363
+ * stays at tiers 1-2.
364
+ */
365
+ promptInjectionLlmClassifier?: (
366
+ text: string,
367
+ ) => Promise<{ verdict: "clean" | "suspicious" | "malicious"; rationale?: string } | undefined>;
368
+ /**
369
+ * Pillar 3 intent gate — when supplied, runtime-core wires this judge
370
+ * into `evaluateJustification` for every tool call whose descriptor
371
+ * sets `requireJustification: true`. Defaults to the rule-based judge
372
+ * exported by `permission-engine`. Production deployments should pass
373
+ * an LLM-backed judge that compares the agent's stated justification
374
+ * against the session goal with model-quality reasoning.
375
+ */
376
+ justificationJudge?: JustificationJudge;
377
+ /**
378
+ * Section 27 — wrap the resolved primary `ProviderAdapter` in a circuit
379
+ * breaker before any `stream()` call. When the breaker trips, subsequent
380
+ * model calls reject immediately rather than hammering a degraded
381
+ * upstream. Set via codegen when the spec lists `circuit_breaker:` opts.
382
+ */
383
+ circuitBreaker?: {
384
+ readonly failureThreshold?: number;
385
+ readonly windowMs?: number;
386
+ readonly cooldownMs?: number;
387
+ };
388
+ /**
389
+ * Section 27 — pre-call rate gating. The runtime calls
390
+ * `rateLimiter.acquire(rateLimitKeys, 1)` before each model invocation.
391
+ * Codegen sets `rateLimitKeys` to `[provider, tenant?]` from the
392
+ * spec's tools/permissions/tenancy block. Without `rateLimiter` set,
393
+ * gating is skipped (no allow-list error).
394
+ */
395
+ rateLimiter?: RateLimiter;
396
+ /** Keys to acquire per model call. Default: `[]`. */
397
+ rateLimitKeys?: ReadonlyArray<{
398
+ readonly dimension: "tenant" | "provider" | "tool";
399
+ readonly id: string;
400
+ }>;
401
+ /**
402
+ * Section 27 — when set, the system block array goes through
403
+ * `prompt-cache-manager.manage()` once at run start; if a fresh marker is
404
+ * injected the new `lastRotatedAt` is exposed via the returned bus event.
405
+ * Pass `0` (or omit) to force a refresh on the first turn.
406
+ */
407
+ promptCacheLastRotatedAt?: number;
408
+ };
409
+
410
+ /**
411
+ * Runs the streaming chat loop and returns the terminal assistant text.
412
+ *
413
+ * - REPL mode (default): reads stdin in a loop, prints assistant turns to
414
+ * stdout, returns `""` when the loop exits.
415
+ * - Single-turn mode (`singleTurn: true`): runs one turn from the seeded
416
+ * message history and returns the concatenated text content of the final
417
+ * assistant message. Does not touch stdin.
418
+ */
419
+ export async function runChatLoop(opts: RunChatLoopOptions): Promise<string> {
420
+ // Section 17 — resolve the primary adapter via the model-router.
421
+ // The router lazy-loads the matching provider package; an
422
+ // Anthropic-only spec never pulls AWS / OpenAI / Gemini SDKs.
423
+ // Section 17 — resolve the primary adapter via the model-router and
424
+ // capture the *stripped* modelId (the form the provider expects, e.g.
425
+ // `openai/gpt-4o-mini` → `gpt-4o-mini`). When the caller injects an
426
+ // `_adapter`, default to `opts.model` for the wire model id — tests
427
+ // typically pass synthetic ids the stub adapter ignores.
428
+ const primaryResolution = opts._adapter
429
+ ? { adapter: opts._adapter, modelId: opts.model, providerId: opts._adapter.providerId }
430
+ : await resolveModel(opts.model);
431
+ // Section 27 — wrap the primary adapter in a circuit breaker when
432
+ // `circuitBreaker` opts are set (or by default in production codegen).
433
+ // The breaker is hung off the run-context's bus so state-change events
434
+ // surface for audit-log + structured-event-printer + OTel.
435
+ const baseAdapter: ProviderAdapter = primaryResolution.adapter;
436
+ let breakerWrap: WrappedAdapter | undefined;
437
+ const adapter: ProviderAdapter = (() => {
438
+ if (opts.circuitBreaker === undefined) return baseAdapter;
439
+ breakerWrap = wrapWithCircuitBreaker(baseAdapter, {
440
+ ...opts.circuitBreaker,
441
+ adapterName: baseAdapter.providerId,
442
+ bus: opts.runContext?.eventBus,
443
+ });
444
+ return breakerWrap;
445
+ })();
446
+ void breakerWrap; // exposed via runtime stats once gateway/eval consumers land
447
+ const providerId: ProviderId = primaryResolution.providerId;
448
+ const wireModelId: string = primaryResolution.modelId;
449
+ let compactionAdapter: ProviderAdapter;
450
+ let compactionWireModelId: string;
451
+ if (opts._compactionAdapter !== undefined) {
452
+ compactionAdapter = opts._compactionAdapter;
453
+ compactionWireModelId = opts.compactionModel ?? wireModelId;
454
+ } else if (opts.compactionModel !== undefined) {
455
+ const c = await resolveModel(opts.compactionModel);
456
+ compactionAdapter = c.adapter;
457
+ compactionWireModelId = c.modelId;
458
+ } else {
459
+ compactionAdapter = adapter;
460
+ compactionWireModelId = wireModelId;
461
+ }
462
+ const maxTokens = opts.maxTokens ?? 4096;
463
+
464
+ // Mutual exclusion: resume takes priority and replaces any seedMessages
465
+ // in REPL mode (the resume payload becomes the seed). Section 12 carves
466
+ // out an exception for singleTurn + resume: the seed is the NEW inbound
467
+ // message, the resumed history is the prefix.
468
+ if (opts.resume !== undefined && opts.seedMessages !== undefined && opts.singleTurn !== true) {
469
+ throw new RuntimeError(
470
+ "runChatLoop: `resume` and `seedMessages` are mutually exclusive in REPL mode — the resume payload becomes the seed",
471
+ );
472
+ }
473
+ // Section 12: singleTurn + resume IS supported — used by the channel-bot
474
+ // session-router for "resume the thread, append the inbound message, run
475
+ // one turn". The seed must contain only the new turn's input (a single
476
+ // user message); the replayed event-log history becomes the prefix and
477
+ // is NOT re-logged.
478
+ if (
479
+ opts.runContext !== undefined &&
480
+ opts.resume !== undefined &&
481
+ opts.runContext.sessionId !== opts.resume.sessionId
482
+ ) {
483
+ throw new RuntimeError(
484
+ "runChatLoop: opts.runContext.sessionId must match opts.resume.sessionId when both are supplied",
485
+ );
486
+ }
487
+
488
+ // Persistence boot: session create/resume + event log open. Runs first
489
+ // so the run context's sessionId — possibly carried over from a prior
490
+ // run — is the one the rest of the loop logs against.
491
+ const sessionRootDir = opts.sessionRootDir ?? process.env["CREWHAUS_SESSION_DIR"] ?? undefined;
492
+ const sessionStore: SessionStore = createSessionStore({ rootDir: sessionRootDir });
493
+ // Housekeeping side-effect: evicts any session whose mtime is older than
494
+ // the TTL (default 30 days). The returned list is intentionally discarded.
495
+ await sessionStore.list();
496
+
497
+ let sessionId: string;
498
+ let resumedMessages: Anthropic.MessageParam[] | undefined;
499
+ if (opts.resume) {
500
+ const existing = await sessionStore.get(opts.resume.sessionId);
501
+ if (existing === null) {
502
+ throw new RuntimeError(
503
+ `cannot --resume "${opts.resume.sessionId}": session not found in ${sessionRootDir ?? ".crewhaus/sessions"}`,
504
+ );
505
+ }
506
+ sessionId = existing.id;
507
+ const replayLog = await openEventLog(sessionId, { rootDir: sessionRootDir });
508
+ resumedMessages = await replayMessageHistory(replayLog);
509
+ await replayLog.close();
510
+ } else {
511
+ // If the caller supplied a runContext, honour its sessionId (already
512
+ // sess_<16 hex> by construction) so logs and the persisted file agree.
513
+ // Otherwise, let session-store mint a fresh id.
514
+ const created = await sessionStore.create({
515
+ id: opts.runContext?.sessionId,
516
+ name: opts.sessionName ?? "(unnamed)",
517
+ target: opts.sessionTarget ?? "cli",
518
+ model: opts.model,
519
+ });
520
+ sessionId = created.id;
521
+ }
522
+
523
+ // Use the supplied runContext as-is when it agrees with the resolved
524
+ // sessionId; otherwise build a fresh context bound to that sessionId.
525
+ // Tests that pass their own runContext rely on observing turnNumber on
526
+ // it after the loop returns, so we never silently swap it out.
527
+ const runContext: RunContext = opts.runContext ?? createRunContext({ sessionId });
528
+
529
+ // Section 15 — every observability subscriber (otel, metrics, printer)
530
+ // hangs off this single bus. The default constructor in `createRunContext`
531
+ // mints a no-subscriber bus; here we attach the env-gated set so a fresh
532
+ // run with `OTEL_EXPORTER_OTLP_ENDPOINT` set automatically exports.
533
+ const bus: TraceEventBus = runContext.eventBus;
534
+ void TraceEventBus; // keep type import alive for future direct constructions
535
+ const subscribers = await attachDefaultSubscribers(bus, runContext);
536
+
537
+ // M2.1 — CLI markdown renderer (env-gated). When CREWHAUS_CLI_MARKDOWN=1,
538
+ // text-delta chunks flow through a streaming markdown → ANSI transform so
539
+ // the user sees rendered bold/italic/headers/lists/code-fences instead of
540
+ // raw asterisks. The renderer is line-buffered; chunks accumulate until a
541
+ // `\n` arrives, then the rendered line writes to stdout. `end()` is
542
+ // called after the per-turn `\n` to flush any tail content.
543
+ const mdRenderer: StreamRenderer | undefined = isCliMarkdownEnabled()
544
+ ? createCliMarkdownRenderer()
545
+ : undefined;
546
+ const writeText = (chunk: string): void => {
547
+ if (mdRenderer) mdRenderer.push(chunk);
548
+ else process.stdout.write(chunk);
549
+ };
550
+ const endTurn = (): void => {
551
+ if (mdRenderer) mdRenderer.end();
552
+ process.stdout.write("\n");
553
+ };
554
+
555
+ const eventLog: EventLog = await openEventLog(sessionId, { rootDir: sessionRootDir });
556
+
557
+ // Per-run state container — coordination surface for hooks/skills/tools
558
+ // landing in Section 11+. Section 10 only ships the plumbing; the
559
+ // underscore prefix marks the intentional non-consumer.
560
+ const _runState: Store<Record<string, unknown>> = createStore({});
561
+ void _runState;
562
+
563
+ // Section 13 — RuntimeBridge. Built once per run, handed to every
564
+ // tool execution through the executor's ExecutionContext. Framework-
565
+ // aware tools (only `Task` today) cast `ctx.bridge` to RuntimeBridge.
566
+ // Built lazily inside executeOneToolUse so it captures the latest
567
+ // permission-mode/-rules and tool list — the values themselves are
568
+ // stable for the run, but the closure keeps the call-site at the
569
+ // tool-execute boundary tidy.
570
+
571
+ // Tight helper so call sites stay one line. Errors propagate so an I/O
572
+ // failure on the audit trail surfaces rather than silently dropping.
573
+ async function logEvent(kind: EventKind, payload: unknown): Promise<void> {
574
+ await eventLog.append({ kind, payload });
575
+ }
576
+
577
+ // Section 11 — fire matching hooks for `event` and aggregate the result.
578
+ // No-ops when no hooks are configured. The aggregated decision exposes
579
+ // `allowed` / `reason` / `mutate`; v1 callers honour `mutate` only for
580
+ // `pre-slash` (the `expanded` field). Errors are caught and surfaced as
581
+ // a deny so a misbehaving hook implementation does not crash the run.
582
+ const hooks = opts.hooks ?? [];
583
+ async function fireHook(
584
+ event: HookEvent,
585
+ payload: unknown,
586
+ ): Promise<{ allowed: boolean; reason?: string; mutate?: Record<string, unknown> }> {
587
+ if (hooks.length === 0) return { allowed: true };
588
+ try {
589
+ const results = await runHooks(event, payload, hooks, {
590
+ logger: runContext.logger,
591
+ eventBus: bus,
592
+ });
593
+ return aggregateDecisions(results);
594
+ } catch (err) {
595
+ runContext.logger.warn("hook firing failed", {
596
+ event,
597
+ error: (err as Error).message,
598
+ });
599
+ return { allowed: true };
600
+ }
601
+ }
602
+
603
+ // Section 11 — `session-start` fires once after the persistence + run
604
+ // context boot is complete. Observational only (no deny/block effect).
605
+ await fireHook("session-start", {
606
+ sessionId,
607
+ model: opts.model,
608
+ target: opts.sessionTarget ?? "cli",
609
+ resumed: opts.resume !== undefined,
610
+ });
611
+
612
+ const contextLimit = opts.contextLimit ?? DEFAULT_CONTEXT_LIMIT;
613
+ const compactionThreshold = opts.compactionThreshold ?? DEFAULT_COMPACTION_THRESHOLD;
614
+ const snipKeepHead = opts.snipKeepHead ?? DEFAULT_SNIP_KEEP_HEAD;
615
+ const snipKeepTail = opts.snipKeepTail ?? DEFAULT_SNIP_KEEP_TAIL;
616
+ const permissionMode: PermissionMode = opts.permissionMode ?? "default";
617
+ const permissionRules: RuleSet = opts.permissionRules ?? {
618
+ ...emptyRuleSet,
619
+ builtin: BUILTIN_DEFAULT_RULES,
620
+ };
621
+
622
+ const userInstructions: Anthropic.TextBlockParam = {
623
+ type: "text",
624
+ text: opts.instructions,
625
+ cache_control: { type: "ephemeral" },
626
+ };
627
+ // Section 11 — append a "Available skills:" block to the system prompt
628
+ // when skills are configured. The block lists names + descriptions only;
629
+ // the model reaches each skill's body by calling the `Skill` tool (which
630
+ // the caller is responsible for adding to the tools array).
631
+ const skills = opts.skills ?? [];
632
+ const skillsText = skills.length > 0 ? formatSkillsForPrompt(skills) : "";
633
+ const skillsBlock: Anthropic.TextBlockParam[] =
634
+ skillsText.length > 0
635
+ ? [{ type: "text", text: skillsText, cache_control: { type: "ephemeral" } }]
636
+ : [];
637
+ // M3.1 — auto-load project memory files (AGENTS.md / CLAUDE.md /
638
+ // CODE-COMPANION.md / AGENT.md) from cwd at session start. Follows the
639
+ // vendor-neutral agents.md convention; compatible with Claude Code's
640
+ // CLAUDE.md auto-load behaviour. Pillar 3 compliance: each file's content
641
+ // is classified via boundary-classifier with origin "user" inside
642
+ // loadProjectMemory() (the user's repo is developer-trusted).
643
+ const projectMemory = await loadProjectMemory();
644
+ const projectMemoryBlock: Anthropic.TextBlockParam[] =
645
+ projectMemory.prompt.length > 0
646
+ ? [
647
+ {
648
+ type: "text",
649
+ text: projectMemory.prompt,
650
+ cache_control: { type: "ephemeral" },
651
+ },
652
+ ]
653
+ : [];
654
+ if (projectMemory.files.length > 0) {
655
+ process.stdout.write(
656
+ `[memory] loaded ${projectMemory.files.length} project memory file(s): ${projectMemory.files
657
+ .map((f) => f.filename)
658
+ .join(", ")}\n`,
659
+ );
660
+ }
661
+ // Section 17 — runtime-core no longer prepends the Claude Code OAuth
662
+ // prefix; `adapter-anthropic` handles that internally so each adapter
663
+ // owns its provider-specific auth-shape requirements.
664
+ let systemBlocks: Anthropic.TextBlockParam[] = [
665
+ userInstructions,
666
+ ...projectMemoryBlock,
667
+ ...skillsBlock,
668
+ ];
669
+
670
+ // Section 27 — rotate cache markers on the system block array if the
671
+ // last refresh is older than the rotation interval. Skips automatically
672
+ // when the adapter doesn't support explicit caching (OpenAI / Bedrock
673
+ // Llama / Mistral). This keeps long-running CHN/MGD/RES daemons under
674
+ // Anthropic's 30-day TTL.
675
+ if (adapter.features.caching === "explicit") {
676
+ const cacheManaged = manageCacheMarkers(
677
+ systemBlocks.map((b) => ({
678
+ type: "text" as const,
679
+ text: b.text,
680
+ ...(b.cache_control !== undefined ? { cache_control: b.cache_control } : {}),
681
+ })),
682
+ {
683
+ features: adapter.features,
684
+ ...(opts.promptCacheLastRotatedAt !== undefined
685
+ ? { lastRotatedAt: opts.promptCacheLastRotatedAt }
686
+ : {}),
687
+ },
688
+ );
689
+ if (cacheManaged.rotated) {
690
+ systemBlocks = cacheManaged.blocks.map((b) => ({
691
+ type: "text",
692
+ text: b.text,
693
+ ...(b.cache_control !== undefined ? { cache_control: b.cache_control } : {}),
694
+ })) as Anthropic.TextBlockParam[];
695
+ }
696
+ }
697
+
698
+ const tools = opts.tools ?? [];
699
+ const anthropicTools: Anthropic.Tool[] | undefined =
700
+ tools.length > 0
701
+ ? tools.map((t) => ({
702
+ name: t.name,
703
+ description: t.description,
704
+ input_schema: (t.jsonSchema ??
705
+ zodToJsonSchema(t.inputSchema, {
706
+ $refStrategy: "none",
707
+ })) as Anthropic.Tool.InputSchema,
708
+ }))
709
+ : undefined;
710
+ const toolByName = new Map(tools.map((t) => [t.name, t]));
711
+
712
+ // Root abort tree for the whole run; each turn gets a child.
713
+ const runAbort = createAbortTree(runContext.abortSignal);
714
+ // Per-turn abort tree, replaced at the start of each turn so a SIGINT
715
+ // during turn N doesn't leave turn N+1 born-aborted.
716
+ let turnAbort: AbortTree = runAbort.child();
717
+ // Latest error captured from a model call, fed to recover() on entry to NeedRecovery.
718
+ let lastErrorForRecovery: unknown = undefined;
719
+
720
+ // Permission-ask prompter. Set in REPL mode after the readline interface
721
+ // exists; remains undefined in single-turn mode (where ask collapses to deny).
722
+ // The initial assignment makes the second assignment legal under biome's
723
+ // `useConst` rule (which fires on exactly-one-assignment lets).
724
+ let askApproval: ((toolName: string, input: unknown) => Promise<boolean>) | undefined = undefined;
725
+
726
+ // Per-run state for tool-loop detection and warning de-dup. Both span
727
+ // turns within a single `runChatLoop` invocation so cross-turn loops
728
+ // (e.g. "keep running date") get caught and warned at most once per
729
+ // signature.
730
+ const toolUseHistory: TsmToolUseBlock[] = [];
731
+ const warnedLoopSignatures = new Set<string>();
732
+
733
+ /**
734
+ * Execute one tool_use block end-to-end: permission gate → executeTool
735
+ * with the per-tool abort signal → wrap large output through
736
+ * `tool-result-store`. Returns a fully-formed `ToolResultBlockParam`
737
+ * suitable for appending to message history.
738
+ *
739
+ * Used by both the partitioned (post-stream) path and the streaming
740
+ * path so the permission/abort/store contract is uniform.
741
+ */
742
+ async function executeOneToolUse(tu: TsmToolUseBlock): Promise<Anthropic.ToolResultBlockParam> {
743
+ process.stdout.write(`[tool: ${tu.name}]\n`);
744
+ await logEvent("tool_use", { id: tu.id, name: tu.name, input: tu.input });
745
+ const inputBytes = Buffer.byteLength(JSON.stringify(tu.input ?? null), "utf8");
746
+ const toolStartEnvelope = bus.envelope();
747
+ bus.publish({
748
+ ...toolStartEnvelope,
749
+ kind: "tool_call_start",
750
+ toolUseId: tu.id,
751
+ toolName: tu.name,
752
+ inputBytes,
753
+ });
754
+ const t0Tool = performance.now();
755
+ const tool = toolByName.get(tu.name);
756
+ const finish = async (
757
+ result: Anthropic.ToolResultBlockParam,
758
+ ): Promise<Anthropic.ToolResultBlockParam> => {
759
+ await logEvent("tool_result", {
760
+ toolUseId: tu.id,
761
+ content:
762
+ typeof result.content === "string"
763
+ ? result.content
764
+ : summariseNonStringContent(result.content),
765
+ isError: result.is_error === true,
766
+ });
767
+ const outputContent = result.content;
768
+ const outputBytes =
769
+ typeof outputContent === "string"
770
+ ? Buffer.byteLength(outputContent, "utf8")
771
+ : Buffer.byteLength(JSON.stringify(outputContent ?? null), "utf8");
772
+ bus.publish({
773
+ ...bus.envelope(),
774
+ spanId: toolStartEnvelope.spanId,
775
+ kind: "tool_call_end",
776
+ toolUseId: tu.id,
777
+ toolName: tu.name,
778
+ isError: result.is_error === true,
779
+ outputBytes,
780
+ durationMs: performance.now() - t0Tool,
781
+ });
782
+ // Section 11 — fire post-tool after the result is captured. Decision
783
+ // is observational here: a deny on post-tool logs a warning but does
784
+ // not rewrite the result (the model has already received it once on
785
+ // the prior turn in some streaming scenarios).
786
+ const post = await fireHook("post-tool", {
787
+ id: tu.id,
788
+ name: tu.name,
789
+ isError: result.is_error === true,
790
+ });
791
+ if (!post.allowed) {
792
+ runContext.logger.warn("post-tool hook denied", {
793
+ tool: tu.name,
794
+ reason: post.reason,
795
+ });
796
+ }
797
+ return result;
798
+ };
799
+ if (!tool) {
800
+ return finish({
801
+ type: "tool_result",
802
+ tool_use_id: tu.id,
803
+ content: `unknown tool "${tu.name}"`,
804
+ is_error: true,
805
+ });
806
+ }
807
+
808
+ // Section 11 — pre-tool hook: short-circuit with the hook's reason
809
+ // when any matching hook returns deny/block.
810
+ const preHook = await fireHook("pre-tool", { id: tu.id, name: tu.name, input: tu.input });
811
+ if (!preHook.allowed) {
812
+ return finish({
813
+ type: "tool_result",
814
+ tool_use_id: tu.id,
815
+ content: `[blocked by hook] ${preHook.reason ?? "denied"}`,
816
+ is_error: true,
817
+ });
818
+ }
819
+
820
+ const decisionDetails = evaluateWithReason(
821
+ {
822
+ toolName: tu.name,
823
+ input: tu.input,
824
+ readOnly: tool.readOnly,
825
+ destructive: tool.destructive,
826
+ requiresSandbox: tool.requiresSandbox,
827
+ },
828
+ permissionMode,
829
+ permissionRules,
830
+ { sandboxAvailable: opts.sandboxAvailable === true },
831
+ );
832
+ const decision = decisionDetails.decision;
833
+ bus.publish({
834
+ ...bus.envelope(),
835
+ kind: "permission_decision",
836
+ toolName: tu.name,
837
+ decision,
838
+ mode: permissionMode,
839
+ ...(decisionDetails.reason !== undefined ? { reason: decisionDetails.reason } : {}),
840
+ });
841
+ let approved = decision === "allow";
842
+ let denialMessage: string | undefined;
843
+ if (decision === "deny") {
844
+ denialMessage = decisionDetails.reason ?? "tool denied by permission policy";
845
+ } else if (decision === "ask") {
846
+ if (askApproval !== undefined) {
847
+ approved = await askApproval(tu.name, tu.input);
848
+ if (!approved) denialMessage = "tool denied by user";
849
+ } else {
850
+ denialMessage =
851
+ `tool denied: \`${tu.name}\` defaulted to "ask" and single-turn mode has no interactive surface to prompt on. ` +
852
+ `Add an explicit rule to permissions.rules in your spec, e.g. \`{ type: alwaysAllow, pattern: ${tu.name} }\`, or run in REPL mode where "ask" can prompt.`;
853
+ }
854
+ }
855
+ if (!approved) {
856
+ return finish({
857
+ type: "tool_result",
858
+ tool_use_id: tu.id,
859
+ content: denialMessage ?? "tool denied",
860
+ is_error: true,
861
+ });
862
+ }
863
+
864
+ // Pillar 3 intent gate — when the tool descriptor demands a justification,
865
+ // require the model to include one in the input and evaluate it via the
866
+ // configured judge (rule-based by default; runtime callers can pass an
867
+ // LLM-backed judge for production). The justification is captured to
868
+ // audit-log verbatim. The session goal we compare against is the spec's
869
+ // `instructions` field — fixed at compile time, not influenced by runtime
870
+ // input, so an attacker who controls the user prompt cannot also
871
+ // re-define the goal under which their justification gets scored.
872
+ if (tool.requireJustification === true) {
873
+ const input = tu.input as Record<string, unknown> | undefined;
874
+ const rawJustification = input?.["justification"];
875
+ const justification = typeof rawJustification === "string" ? rawJustification : "";
876
+ const judge = opts.justificationJudge ?? ruleBasedJustificationJudge;
877
+ const verdict = await evaluateJustification(
878
+ {
879
+ toolName: tu.name,
880
+ justification,
881
+ sessionGoal: opts.instructions ?? "",
882
+ input: tu.input,
883
+ },
884
+ judge,
885
+ );
886
+ bus.publish({
887
+ ...bus.envelope(),
888
+ kind: "permission_decision",
889
+ toolName: tu.name,
890
+ decision: verdict.allow ? "allow" : "deny",
891
+ mode: permissionMode,
892
+ reason: `justification: ${verdict.reason} [judge=${verdict.judgeModel}]`,
893
+ });
894
+ runContext.logger.info("justification evaluated", {
895
+ toolUseId: tu.id,
896
+ toolName: tu.name,
897
+ allow: verdict.allow,
898
+ judgeModel: verdict.judgeModel,
899
+ confidence: verdict.confidence,
900
+ });
901
+ if (!verdict.allow) {
902
+ return finish({
903
+ type: "tool_result",
904
+ tool_use_id: tu.id,
905
+ content: `[justification denied] ${verdict.reason}. Reformulate the call with a justification that ties to the session's declared goal, or omit this tool from the call.`,
906
+ is_error: true,
907
+ });
908
+ }
909
+ }
910
+
911
+ // Pillar 3 sink-side fabric — for tools that transmit to an external
912
+ // sink, scan the input payload against the run-context's data-lineage
913
+ // map. A `block` verdict means the payload contains content that was
914
+ // tagged from a non-`"user"` origin and policy disallows transmission;
915
+ // we deny the call before it fires. `warn` logs but proceeds.
916
+ //
917
+ // The runtime treats every external tool as `"external-configured"`
918
+ // (the spec listed it). Dynamically-discovered sinks (an MCP server
919
+ // the agent loaded mid-session) should use `"external-dynamic"` to
920
+ // get stricter default policy; that wiring lives in `tool-mcp`'s
921
+ // dynamic-tool path and is a v0.3.x follow-up.
922
+ if (tool.scope === "external") {
923
+ const payload = JSON.stringify(tu.input ?? null);
924
+ const egress = await classifyEgress(payload, runContext, {
925
+ sinkId: tu.name,
926
+ sinkScope: "external-configured",
927
+ });
928
+ const egressOutcome =
929
+ egress.verdict === "pass"
930
+ ? "egress-passed"
931
+ : egress.verdict === "warn"
932
+ ? "egress-warned"
933
+ : "egress-blocked";
934
+ bus.publish({
935
+ ...bus.envelope(),
936
+ kind: "permission_decision",
937
+ toolName: tu.name,
938
+ decision: egress.verdict === "block" ? "deny" : "allow",
939
+ mode: permissionMode,
940
+ outcome: egressOutcome,
941
+ ...(egress.originsFound.length > 0 ? { reason: `egress: ${summarizeEgress(egress)}` } : {}),
942
+ });
943
+ if (egress.verdict !== "pass") {
944
+ runContext.logger.warn("egress classification non-clean", {
945
+ toolUseId: tu.id,
946
+ toolName: tu.name,
947
+ verdict: egress.verdict satisfies EgressVerdict,
948
+ originsFound: egress.originsFound,
949
+ matchCount: egress.matchCount,
950
+ });
951
+ }
952
+ if (egress.verdict === "block") {
953
+ return finish({
954
+ type: "tool_result",
955
+ tool_use_id: tu.id,
956
+ content:
957
+ "[egress denied] outbound payload contains content tagged from a non-user origin under a strict-policy sink. " +
958
+ "Inspect the prior tool/sub-agent results that fed this call's input; the egress classifier's `dataLineage` records which boundary site introduced the flagged substring.",
959
+ is_error: true,
960
+ });
961
+ }
962
+ }
963
+
964
+ const toolAbort = turnAbort.child();
965
+ // Build the bridge for this call. Skipped (left undefined) when no
966
+ // spawnSubAgent injection is provided — non-Task tools don't need it.
967
+ const bridge: RuntimeBridge | undefined =
968
+ opts.spawnSubAgent !== undefined || opts.crewMailbox !== undefined
969
+ ? {
970
+ runContext,
971
+ eventLog,
972
+ permissionMode,
973
+ permissionRules,
974
+ tools,
975
+ model: opts.model,
976
+ maxTokens,
977
+ ...(sessionRootDir !== undefined ? { sessionRootDir } : {}),
978
+ hooks,
979
+ ...(opts.subAgents !== undefined ? { subAgents: opts.subAgents } : {}),
980
+ ...(opts.spawnSubAgent !== undefined ? { spawnSubAgent: opts.spawnSubAgent } : {}),
981
+ ...(opts.crewMailbox !== undefined ? { crewMailbox: opts.crewMailbox } : {}),
982
+ }
983
+ : undefined;
984
+ const raw = await executeTool(tool, tu.input, {
985
+ toolUseId: tu.id,
986
+ signal: toolAbort.signal,
987
+ ...(bridge !== undefined ? { bridge } : {}),
988
+ // Section 18 — runtime-core wires this so streaming tools
989
+ // (`tool-code-execution`) can publish `tool_stream_chunk` events as
990
+ // they pipe stdout/stderr from the sandbox container.
991
+ onStreamChunk: (stream, chunk) => {
992
+ bus.publish(
993
+ {
994
+ ...bus.envelope(),
995
+ spanId: toolStartEnvelope.spanId,
996
+ kind: "tool_stream_chunk",
997
+ toolUseId: tu.id,
998
+ toolName: tu.name,
999
+ stream,
1000
+ bytes: Buffer.byteLength(chunk, "utf8"),
1001
+ },
1002
+ { ephemeral: true },
1003
+ );
1004
+ },
1005
+ });
1006
+ const stored = await storeAndPreview(
1007
+ { toolUseId: raw.toolUseId, content: raw.content, isError: raw.isError },
1008
+ { runId: runContext.runId, toolUseId: tu.id },
1009
+ );
1010
+ if (stored.persisted) {
1011
+ runContext.logger.info("tool result persisted", {
1012
+ toolUseId: tu.id,
1013
+ toolName: tu.name,
1014
+ fullPath: stored.fullPath,
1015
+ });
1016
+ }
1017
+ // Section 18 — post-tool prompt-injection classifier. Runs after the
1018
+ // tool result has been stored but before the model sees it. On a
1019
+ // malicious verdict the previewContent is replaced with a redaction
1020
+ // notice; on suspicious it's kept but the trace event records a
1021
+ // warning. Tools opt out by setting `classifyOutput: false` (only the
1022
+ // in-process Task wrapper does today).
1023
+ const finalPreview = await applyInjectionClassification(
1024
+ tool,
1025
+ tu,
1026
+ stored.previewContent,
1027
+ raw.isError,
1028
+ );
1029
+ // Pillar 3 sink-side fabric — tag the (post-classification) tool result
1030
+ // into run-context's dataLineage so the egress classifier can detect
1031
+ // exfiltration of this content on a subsequent external-tool call.
1032
+ // Skipped for the in-process Task wrapper (`classifyOutput: false`)
1033
+ // because the sub-agent's finalMessage is already tagged at the
1034
+ // sub-agent-spawner boundary with the more-specific "subagent" origin.
1035
+ // MCP tools tag separately inside tool-mcp with the "mcp" origin so
1036
+ // their lineage carries the more-precise provenance.
1037
+ if (tool.classifyOutput !== false && !raw.isError) {
1038
+ const taggable =
1039
+ typeof finalPreview === "string"
1040
+ ? finalPreview
1041
+ : finalPreview.map((b) => (b.type === "text" ? b.text : "")).join("\n");
1042
+ if (taggable.length > 0) {
1043
+ tagContent(runContext, taggable, "tool");
1044
+ }
1045
+ }
1046
+ return finish({
1047
+ type: "tool_result",
1048
+ tool_use_id: tu.id,
1049
+ // Section 14 — pass non-string content (image content arrays) through
1050
+ // verbatim so the model sees the image. Anthropic's ToolResultBlockParam
1051
+ // already accepts string | Array<TextBlockParam | ImageBlockParam>.
1052
+ content:
1053
+ typeof finalPreview === "string"
1054
+ ? finalPreview
1055
+ : (finalPreview as ReadonlyArray<
1056
+ Anthropic.TextBlockParam | Anthropic.ImageBlockParam
1057
+ > as Anthropic.ToolResultBlockParam["content"]),
1058
+ is_error: raw.isError,
1059
+ });
1060
+ }
1061
+
1062
+ /**
1063
+ * Section 18 — classify the previewContent we are about to hand to the
1064
+ * model. Returns the (possibly rewritten) content. Publishes
1065
+ * `permission_decision { outcome }` for any non-clean classification so
1066
+ * the audit trail records exactly what was redacted/warned.
1067
+ */
1068
+ let injectionWarningEmitted = false;
1069
+ async function applyInjectionClassification(
1070
+ tool: RegisteredTool,
1071
+ tu: TsmToolUseBlock,
1072
+ previewContent: string | ReadonlyArray<Anthropic.TextBlockParam | Anthropic.ImageBlockParam>,
1073
+ isError: boolean,
1074
+ ): Promise<string | ReadonlyArray<Anthropic.TextBlockParam | Anthropic.ImageBlockParam>> {
1075
+ if (tool.classifyOutput === false) return previewContent;
1076
+ if (isError) return previewContent;
1077
+ // Concatenate text-only content for classification. Image blocks are
1078
+ // never injection vectors at the byte level we examine here.
1079
+ const textForClassification =
1080
+ typeof previewContent === "string"
1081
+ ? previewContent
1082
+ : previewContent.map((b) => (b.type === "text" ? b.text : "")).join("\n");
1083
+ if (textForClassification.length === 0) return previewContent;
1084
+
1085
+ const llmEnabled =
1086
+ opts.promptInjectionLlmClassifier !== undefined && llmClassifierEnabled(process.env);
1087
+ const verdict = await classifyText(
1088
+ textForClassification,
1089
+ llmEnabled ? { llmClassifier: opts.promptInjectionLlmClassifier } : {},
1090
+ );
1091
+ if (verdict.classification === "clean") return previewContent;
1092
+
1093
+ const ruleIds = [...new Set(verdict.hits.map((h) => h.rule))];
1094
+ if (verdict.classification === "malicious") {
1095
+ const notice = buildRedactionNotice(verdict.hits);
1096
+ bus.publish({
1097
+ ...bus.envelope(),
1098
+ kind: "permission_decision",
1099
+ toolName: tu.name,
1100
+ decision: "allow",
1101
+ mode: permissionMode,
1102
+ outcome: "redacted",
1103
+ rules: ruleIds,
1104
+ reason: `prompt injection in tool output (${ruleIds.slice(0, 3).join(", ")})`,
1105
+ });
1106
+ runContext.logger.warn("tool output redacted (prompt injection detected)", {
1107
+ toolUseId: tu.id,
1108
+ toolName: tu.name,
1109
+ rules: ruleIds,
1110
+ });
1111
+ return notice;
1112
+ }
1113
+ // suspicious — keep content but emit a once-per-session warning.
1114
+ bus.publish({
1115
+ ...bus.envelope(),
1116
+ kind: "permission_decision",
1117
+ toolName: tu.name,
1118
+ decision: "allow",
1119
+ mode: permissionMode,
1120
+ outcome: "warned",
1121
+ rules: ruleIds,
1122
+ reason: `suspicious tool output (${ruleIds.slice(0, 3).join(", ")})`,
1123
+ });
1124
+ if (!injectionWarningEmitted) {
1125
+ injectionWarningEmitted = true;
1126
+ runContext.logger.warn(
1127
+ "suspicious tool output — kept but flagged (further suspicious outputs in this session will not be re-warned)",
1128
+ { toolUseId: tu.id, toolName: tu.name, rules: ruleIds },
1129
+ );
1130
+ }
1131
+ return previewContent;
1132
+ }
1133
+
1134
+ /**
1135
+ * Section 14 — short summary used in the audit-log payload when a tool
1136
+ * returns a non-string content array (e.g. ReadImage's image block). The
1137
+ * full content still reaches the model via tool_result.content above; the
1138
+ * audit log just gets a length-aware tag.
1139
+ */
1140
+ function summariseNonStringContent(content: Anthropic.ToolResultBlockParam["content"]): string {
1141
+ if (content === undefined || typeof content === "string") {
1142
+ return content === undefined ? "[no content]" : `[${content.length} chars]`;
1143
+ }
1144
+ let images = 0;
1145
+ let texts = 0;
1146
+ let other = 0;
1147
+ let totalChars = 0;
1148
+ for (const block of content) {
1149
+ if (block.type === "image") {
1150
+ images++;
1151
+ if (block.source.type === "base64") totalChars += block.source.data.length;
1152
+ } else if (block.type === "text") {
1153
+ texts++;
1154
+ totalChars += block.text.length;
1155
+ } else {
1156
+ other++;
1157
+ }
1158
+ }
1159
+ const parts: string[] = [];
1160
+ if (images > 0) parts.push(`${images} image block${images > 1 ? "s" : ""}`);
1161
+ if (texts > 0) parts.push(`${texts} text block${texts > 1 ? "s" : ""}`);
1162
+ if (other > 0) parts.push(`${other} other block${other > 1 ? "s" : ""}`);
1163
+ return `[${parts.join(", ")}, ${totalChars} chars]`;
1164
+ }
1165
+
1166
+ /**
1167
+ * Run a list of tool calls honouring the orchestrator's partition:
1168
+ * concurrent-safe batches via `Promise.all`, then serial calls one at
1169
+ * a time. Results are returned in the original `toolUses` order so
1170
+ * they line up with the assistant turn's tool_use blocks.
1171
+ */
1172
+ async function runToolBatch(
1173
+ toolUses: ReadonlyArray<TsmToolUseBlock>,
1174
+ ): Promise<Anthropic.ToolResultBlockParam[]> {
1175
+ const partition = partitionToolCalls(toolUses, (n) => toolByName.get(n));
1176
+ runContext.logger.debug("tool partition", {
1177
+ concurrent: partition.concurrent.map((b) => b.length),
1178
+ serial: partition.serial.length,
1179
+ });
1180
+ const byId = new Map<string, Anthropic.ToolResultBlockParam>();
1181
+ for (const batch of partition.concurrent) {
1182
+ const settled = await Promise.all(batch.map((tu) => executeOneToolUse(tu)));
1183
+ for (const r of settled) byId.set(r.tool_use_id, r);
1184
+ }
1185
+ for (const tu of partition.serial) {
1186
+ const r = await executeOneToolUse(tu);
1187
+ byId.set(r.tool_use_id, r);
1188
+ }
1189
+ return toolUses.map((tu) => {
1190
+ const r = byId.get(tu.id);
1191
+ if (r === undefined) {
1192
+ return {
1193
+ type: "tool_result",
1194
+ tool_use_id: tu.id,
1195
+ content: "internal error: missing tool result",
1196
+ is_error: true,
1197
+ };
1198
+ }
1199
+ return r;
1200
+ });
1201
+ }
1202
+
1203
+ /**
1204
+ * Convert a freshly-detected loop into a synthetic user message that
1205
+ * nudges the model to break the cycle. Returns `null` if no loop is
1206
+ * currently detected, or if this signature has already produced a
1207
+ * warning earlier in the run.
1208
+ */
1209
+ function maybeBuildLoopWarning(): Anthropic.MessageParam | null {
1210
+ const detection: LoopDetection | null = detectLoop(toolUseHistory);
1211
+ if (detection === null) return null;
1212
+ if (warnedLoopSignatures.has(detection.signature)) return null;
1213
+ warnedLoopSignatures.add(detection.signature);
1214
+ runContext.logger.warn("tool loop detected", {
1215
+ signature: detection.signature,
1216
+ toolName: detection.toolName,
1217
+ count: detection.count,
1218
+ });
1219
+ return {
1220
+ role: "user",
1221
+ content: `[runtime] possible loop detected: tool "${detection.toolName}" has been called ${detection.count} times with the same input within the last ${detection.windowSize} calls. Reconsider before repeating; respond with a different approach or final text.`,
1222
+ };
1223
+ }
1224
+
1225
+ /**
1226
+ * Run one user→assistant turn through the state machine. Mutates
1227
+ * `messages` in place (pushes the assistant turn and any tool_results).
1228
+ * Returns the final assistant content blocks so callers can extract
1229
+ * text. Closes over client/model/maxTokens/etc.
1230
+ */
1231
+ async function runOneTurn(
1232
+ messages: Anthropic.MessageParam[],
1233
+ ): Promise<{ terminalContent: Anthropic.ContentBlock[] }> {
1234
+ let state: TurnState = initialState;
1235
+ let terminalContent: Anthropic.ContentBlock[] = [];
1236
+ let recovery: RecoveryState = initialRecoveryState;
1237
+
1238
+ while (state.kind !== "Done") {
1239
+ if (turnAbort.signal.aborted) {
1240
+ state = transition(state, { kind: "Aborted" });
1241
+ continue;
1242
+ }
1243
+
1244
+ switch (state.kind) {
1245
+ case "NeedModel": {
1246
+ process.stdout.write("agent> ");
1247
+ // Section 11 — pre-model hook. Deny short-circuits the turn with
1248
+ // a synthetic "[blocked by hook]" assistant message so the loop
1249
+ // exits cleanly (state transitions through ModelReturnedText →
1250
+ // Done(no_tools)).
1251
+ const preModel = await fireHook("pre-model", {
1252
+ model: opts.model,
1253
+ streaming: opts.streaming === true,
1254
+ messageCount: messages.length,
1255
+ });
1256
+ if (!preModel.allowed) {
1257
+ const blockedText = `[blocked by hook] ${preModel.reason ?? "denied"}`;
1258
+ process.stdout.write(`${blockedText}\n`);
1259
+ const blockedBlock: Anthropic.TextBlock = {
1260
+ type: "text",
1261
+ text: blockedText,
1262
+ citations: null,
1263
+ };
1264
+ messages.push({ role: "assistant", content: [blockedBlock] });
1265
+ await logEvent("assistant_message", { content: [blockedBlock] });
1266
+ terminalContent = [blockedBlock];
1267
+ state = transition(state, { kind: "ModelReturnedText" });
1268
+ break;
1269
+ }
1270
+ try {
1271
+ // Section 27 — rate-limit the model call before opening the
1272
+ // stream. The keys are caller-configured (typically
1273
+ // `[provider, tenant?]`); a missing bucket fails closed and
1274
+ // surfaces the RateLimitError up to recovery-engine.
1275
+ if (opts.rateLimiter && opts.rateLimitKeys && opts.rateLimitKeys.length > 0) {
1276
+ await opts.rateLimiter.acquire(opts.rateLimitKeys, 1);
1277
+ }
1278
+ const modelStartEnv = bus.envelope();
1279
+ bus.publish({
1280
+ ...modelStartEnv,
1281
+ kind: "model_request",
1282
+ model: opts.model,
1283
+ provider: providerId,
1284
+ messageCount: messages.length,
1285
+ toolCount: anthropicTools?.length ?? 0,
1286
+ streaming: opts.streaming === true,
1287
+ });
1288
+ const t0Model = performance.now();
1289
+ let streamChunkIndex = 0;
1290
+
1291
+ // Section 17 — `adapter.stream(req)` returns
1292
+ // `AsyncIterable<StreamEvent>`; we accumulate via
1293
+ // `consumeStream` (callbacks for stdout streaming + token
1294
+ // telemetry) and pull the final canonical message at the
1295
+ // end. The canonical content blocks are wire-compatible
1296
+ // with `Anthropic.ContentBlock` so existing message-history
1297
+ // bookkeeping continues to work.
1298
+ // Note: `messages` is `Anthropic.MessageParam[]` whose
1299
+ // content union is structurally a superset of canonical
1300
+ // (it includes `document`, deprecated `tool_result`
1301
+ // shapes, etc.). Cast to the canonical surface — the
1302
+ // bookkeeping is wire-compatible for the block kinds we
1303
+ // actually consume (text / image / tool_use / tool_result
1304
+ // / thinking).
1305
+ const reqStream = adapter.stream({
1306
+ model: wireModelId,
1307
+ system: systemBlocks.map((b) => ({
1308
+ type: "text" as const,
1309
+ text: b.text,
1310
+ ...(b.cache_control !== undefined ? { cache_control: b.cache_control } : {}),
1311
+ })),
1312
+ messages: messages as Parameters<ProviderAdapter["stream"]>[0]["messages"],
1313
+ tools:
1314
+ anthropicTools !== undefined
1315
+ ? anthropicTools.map((t) => ({
1316
+ name: t.name,
1317
+ description: t.description ?? "",
1318
+ input_schema: t.input_schema as Record<string, unknown>,
1319
+ }))
1320
+ : undefined,
1321
+ maxTokens,
1322
+ signal: turnAbort.signal,
1323
+ });
1324
+
1325
+ if (opts.streaming) {
1326
+ // Streaming path: dispatch tools mid-stream via the
1327
+ // refactored streaming-tool-executor (now consumes
1328
+ // AsyncIterable<StreamEvent> directly).
1329
+ void modelStartEnv;
1330
+ const onTextDelta = (chunk: string): void => {
1331
+ writeText(chunk);
1332
+ bus.publish(
1333
+ {
1334
+ ...bus.envelope(),
1335
+ kind: "model_stream_token",
1336
+ chunkIndex: streamChunkIndex++,
1337
+ deltaChars: chunk.length,
1338
+ },
1339
+ { ephemeral: true },
1340
+ );
1341
+ };
1342
+ const { finalContent, toolResults, stopReason, usage } = await executeStreaming(
1343
+ reqStream,
1344
+ {
1345
+ toolByName,
1346
+ abortSignal: turnAbort.signal,
1347
+ onTextDelta,
1348
+ runTool: (block) =>
1349
+ executeOneToolUse({
1350
+ id: block.id,
1351
+ name: block.name,
1352
+ input: block.input,
1353
+ }),
1354
+ onEvent: (e) => {
1355
+ runContext.logger.debug("streaming-tool", { ...e });
1356
+ },
1357
+ },
1358
+ );
1359
+ endTurn();
1360
+
1361
+ messages.push({
1362
+ role: "assistant",
1363
+ content: finalContent as Anthropic.MessageParam["content"],
1364
+ });
1365
+ await logEvent("assistant_message", { content: finalContent });
1366
+ terminalContent = [...finalContent] as Anthropic.ContentBlock[];
1367
+ bus.publish({
1368
+ ...bus.envelope(),
1369
+ spanId: modelStartEnv.spanId,
1370
+ kind: "model_response",
1371
+ model: opts.model,
1372
+ provider: providerId,
1373
+ stopReason: stopReason ?? "end_turn",
1374
+ usage,
1375
+ durationMs: performance.now() - t0Model,
1376
+ });
1377
+ await fireHook("post-model", {
1378
+ streaming: true,
1379
+ contentBlocks: finalContent.length,
1380
+ });
1381
+
1382
+ const toolUses = finalContent.filter(
1383
+ (b): b is Extract<typeof b, { type: "tool_use" }> => b.type === "tool_use",
1384
+ );
1385
+ if (toolUses.length === 0) {
1386
+ state = transition(state, { kind: "ModelReturnedText" });
1387
+ break;
1388
+ }
1389
+
1390
+ for (const tu of toolUses) {
1391
+ toolUseHistory.push({ id: tu.id, name: tu.name, input: tu.input });
1392
+ }
1393
+ messages.push({ role: "user", content: [...toolResults] });
1394
+ await logEvent("user_message", { content: [...toolResults] });
1395
+ const warning = maybeBuildLoopWarning();
1396
+ if (warning !== null) {
1397
+ messages.push(warning);
1398
+ await logEvent("user_message", { content: warning.content });
1399
+ }
1400
+
1401
+ // Synthesize the two transitions the state machine expects:
1402
+ // tools have already executed during the stream so we
1403
+ // collapse ModelReturnedToolUse → ToolsExecuted into a
1404
+ // single hop back to NeedModel.
1405
+ const tsmBlocks: TsmToolUseBlock[] = toolUses.map((t) => ({
1406
+ id: t.id,
1407
+ name: t.name,
1408
+ input: t.input,
1409
+ }));
1410
+ state = transition(state, { kind: "ModelReturnedToolUse", toolUses: tsmBlocks });
1411
+ state = transition(state, { kind: "ToolsExecuted" });
1412
+ break;
1413
+ }
1414
+
1415
+ // Non-streaming path: drain the AsyncIterable into a final
1416
+ // `ProviderMessage` while emitting text deltas to stdout.
1417
+ const final = await consumeStream(reqStream, {
1418
+ onTextDelta: (chunk) => {
1419
+ writeText(chunk);
1420
+ bus.publish(
1421
+ {
1422
+ ...bus.envelope(),
1423
+ kind: "model_stream_token",
1424
+ chunkIndex: streamChunkIndex++,
1425
+ deltaChars: chunk.length,
1426
+ },
1427
+ { ephemeral: true },
1428
+ );
1429
+ },
1430
+ });
1431
+ endTurn();
1432
+
1433
+ bus.publish({
1434
+ ...bus.envelope(),
1435
+ spanId: modelStartEnv.spanId,
1436
+ kind: "model_response",
1437
+ model: opts.model,
1438
+ provider: providerId,
1439
+ stopReason: final.stopReason,
1440
+ usage: final.usage,
1441
+ durationMs: performance.now() - t0Model,
1442
+ });
1443
+
1444
+ // Persist the assistant turn with the FULL content-block array;
1445
+ // tool_use blocks must survive into history so subsequent
1446
+ // tool_result references resolve.
1447
+ messages.push({
1448
+ role: "assistant",
1449
+ content: final.content as Anthropic.MessageParam["content"],
1450
+ });
1451
+ await logEvent("assistant_message", { content: final.content });
1452
+ terminalContent = [...final.content] as Anthropic.ContentBlock[];
1453
+ await fireHook("post-model", {
1454
+ streaming: false,
1455
+ stopReason: final.stopReason,
1456
+ contentBlocks: final.content.length,
1457
+ });
1458
+
1459
+ // Synthetic max_output_tokens recovery: stop_reason "max_tokens"
1460
+ // means the model was cut off mid-reply. Route through the
1461
+ // recovery state machine so we ask it to continue.
1462
+ if (final.stopReason === "max_tokens") {
1463
+ lastErrorForRecovery = {
1464
+ name: "MaxTokensError",
1465
+ error: { type: "max_output_tokens" },
1466
+ message: "stop_reason: max_tokens",
1467
+ };
1468
+ state = transition(state, {
1469
+ kind: "RecoverableError",
1470
+ error: { name: "MaxTokensError", message: "stop_reason: max_tokens" },
1471
+ });
1472
+ break;
1473
+ }
1474
+
1475
+ const toolUses = final.content.filter(
1476
+ (b): b is Extract<typeof b, { type: "tool_use" }> => b.type === "tool_use",
1477
+ );
1478
+ if (toolUses.length === 0) {
1479
+ state = transition(state, { kind: "ModelReturnedText" });
1480
+ } else {
1481
+ const tsmBlocks: TsmToolUseBlock[] = toolUses.map((t) => ({
1482
+ id: t.id,
1483
+ name: t.name,
1484
+ input: t.input,
1485
+ }));
1486
+ for (const tu of tsmBlocks) toolUseHistory.push(tu);
1487
+ state = transition(state, { kind: "ModelReturnedToolUse", toolUses: tsmBlocks });
1488
+ }
1489
+ } catch (err) {
1490
+ if (isAbortError(err)) {
1491
+ state = transition(state, { kind: "Aborted" });
1492
+ break;
1493
+ }
1494
+ lastErrorForRecovery = err;
1495
+ const errObj = err as { name?: unknown; message?: unknown };
1496
+ const errorName = typeof errObj.name === "string" ? errObj.name : "Error";
1497
+ const errorMessage = typeof errObj.message === "string" ? errObj.message : String(err);
1498
+ await logEvent("error", { name: errorName, message: errorMessage });
1499
+ state = transition(state, {
1500
+ kind: "RecoverableError",
1501
+ error: { name: errorName, message: errorMessage },
1502
+ });
1503
+ }
1504
+ break;
1505
+ }
1506
+
1507
+ case "NeedTools": {
1508
+ const toolResults = await runToolBatch(state.toolUses);
1509
+ messages.push({ role: "user", content: toolResults });
1510
+ await logEvent("user_message", { content: toolResults });
1511
+ const warning = maybeBuildLoopWarning();
1512
+ if (warning !== null) {
1513
+ messages.push(warning);
1514
+ await logEvent("user_message", { content: warning.content });
1515
+ }
1516
+ state = transition(state, { kind: "ToolsExecuted" });
1517
+ break;
1518
+ }
1519
+
1520
+ case "NeedCompaction":
1521
+ // Reserved: pre-turn compaction is run outside the state machine;
1522
+ // the recovery branch handles in-turn reactive compaction inline.
1523
+ state = transition(state, { kind: "CompactionDone" });
1524
+ break;
1525
+
1526
+ case "NeedRecovery": {
1527
+ const action = recover(lastErrorForRecovery, recovery);
1528
+ recovery = advanceState(recovery, action);
1529
+ runContext.logger.info("recovery.action", {
1530
+ kind: action.kind,
1531
+ errorName: state.error.name,
1532
+ recoveryDepth: recovery.retryCount + recovery.compactCount + recovery.continueCount,
1533
+ });
1534
+ bus.publish({
1535
+ ...bus.envelope(),
1536
+ kind: "error_recovered",
1537
+ action: action.kind,
1538
+ errorName: state.error.name,
1539
+ depth: recovery.retryCount + recovery.compactCount + recovery.continueCount,
1540
+ });
1541
+ switch (action.kind) {
1542
+ case "compact": {
1543
+ const before = messages.length;
1544
+ await fireHook("pre-compact", { kind: "reactive", before });
1545
+ const compacted = await forceCompact({
1546
+ messages,
1547
+ adapter: compactionAdapter,
1548
+ model: compactionWireModelId,
1549
+ snipKeepHead,
1550
+ snipKeepTail,
1551
+ logger: runContext.logger,
1552
+ });
1553
+ messages.length = 0;
1554
+ messages.push(...compacted);
1555
+ await logEvent("compaction", {
1556
+ kind: "reactive",
1557
+ before,
1558
+ after: messages.length,
1559
+ });
1560
+ bus.publish({
1561
+ ...bus.envelope(),
1562
+ kind: "compaction_fired",
1563
+ subKind: "reactive",
1564
+ before,
1565
+ after: messages.length,
1566
+ phase: "reactive",
1567
+ });
1568
+ await fireHook("post-compact", {
1569
+ kind: "reactive",
1570
+ before,
1571
+ after: messages.length,
1572
+ });
1573
+ state = transition(state, { kind: "RecoveryDone" });
1574
+ break;
1575
+ }
1576
+ case "retry":
1577
+ await new Promise((resolve) => setTimeout(resolve, action.delayMs));
1578
+ state = transition(state, { kind: "RecoveryDone" });
1579
+ break;
1580
+ case "continue": {
1581
+ const continueMsg = "Please continue from where you left off.";
1582
+ messages.push({ role: "user", content: continueMsg });
1583
+ await logEvent("user_message", { content: continueMsg });
1584
+ state = transition(state, { kind: "RecoveryDone" });
1585
+ break;
1586
+ }
1587
+ case "tombstone": {
1588
+ const lastIdx = messages.length - 1;
1589
+ if (lastIdx >= 0 && messages[lastIdx]?.role === "assistant") {
1590
+ messages.pop();
1591
+ }
1592
+ const tombstoneMsg =
1593
+ "[previous assistant turn was rejected as invalid; please retry]";
1594
+ messages.push({ role: "user", content: tombstoneMsg });
1595
+ await logEvent("user_message", { content: tombstoneMsg });
1596
+ state = transition(state, { kind: "RecoveryDone" });
1597
+ break;
1598
+ }
1599
+ case "fail":
1600
+ throw new RuntimeError(`recovery failed: ${action.reason}`);
1601
+ }
1602
+ break;
1603
+ }
1604
+ }
1605
+ }
1606
+
1607
+ return { terminalContent };
1608
+ }
1609
+
1610
+ // Single-shot path used by the workflow target and (Section 12) the
1611
+ // channel-bot session router. Runs one user→assistant turn (with the tool
1612
+ // inner-loop until Done), persists the transcript, returns the terminal
1613
+ // assistant text. Never reads stdin.
1614
+ if (opts.singleTurn) {
1615
+ const seed = opts.seedMessages ?? [];
1616
+ const last = seed[seed.length - 1];
1617
+ if (!last || last.role !== "user") {
1618
+ throw new RuntimeError(
1619
+ 'runChatLoop({ singleTurn: true }) requires `seedMessages` to end with a `role: "user"` entry',
1620
+ );
1621
+ }
1622
+ try {
1623
+ // When resuming, the replayed event-log history becomes the prefix
1624
+ // (already on disk — never re-logged). The seed is the new turn's
1625
+ // input, appended at the end. Without resume (workflow target), the
1626
+ // seed IS the entire turn and is logged through.
1627
+ const replayed = resumedMessages ?? [];
1628
+ let messages: Anthropic.MessageParam[] = [...replayed, ...seed];
1629
+ for (const m of seed) {
1630
+ if (m.role === "user") {
1631
+ await logEvent("user_message", { content: m.content });
1632
+ } else if (m.role === "assistant") {
1633
+ await logEvent("assistant_message", { content: m.content });
1634
+ }
1635
+ }
1636
+ runContext.turnNumber += 1;
1637
+ bus.setTurnNumber(runContext.turnNumber);
1638
+ const t0SingleTurn = performance.now();
1639
+ bus.publish({
1640
+ ...bus.envelope(),
1641
+ kind: "turn_start",
1642
+ turn: runContext.turnNumber,
1643
+ messageCount: messages.length,
1644
+ });
1645
+ runContext.logger.debug("turn start (single)", {
1646
+ turn: runContext.turnNumber,
1647
+ messages: messages.length,
1648
+ });
1649
+
1650
+ messages = await maybeCompact(
1651
+ {
1652
+ messages,
1653
+ adapter: compactionAdapter,
1654
+ model: compactionWireModelId,
1655
+ contextLimit,
1656
+ compactionThreshold,
1657
+ snipKeepHead,
1658
+ snipKeepTail,
1659
+ logger: runContext.logger,
1660
+ },
1661
+ async (info) => {
1662
+ await logEvent("compaction", info);
1663
+ bus.publish({
1664
+ ...bus.envelope(),
1665
+ kind: "compaction_fired",
1666
+ subKind: info.kind,
1667
+ before: info.before,
1668
+ after: info.after,
1669
+ phase: "pre-turn",
1670
+ });
1671
+ // Fire one pre-compact (info has the before-count from the work
1672
+ // that just ran) followed by post-compact. Pre-compact in this
1673
+ // path is observational — by the time onCompaction fires the
1674
+ // step has already happened — but the pair lets hooks see both
1675
+ // events for symmetry with the reactive path.
1676
+ await fireHook("pre-compact", { ...info, phase: "pre-turn" });
1677
+ await fireHook("post-compact", { ...info, phase: "pre-turn" });
1678
+ },
1679
+ );
1680
+
1681
+ const { terminalContent } = await runOneTurn(messages);
1682
+ bus.publish({
1683
+ ...bus.envelope(),
1684
+ kind: "turn_end",
1685
+ turn: runContext.turnNumber,
1686
+ durationMs: performance.now() - t0SingleTurn,
1687
+ });
1688
+ return terminalContent
1689
+ .filter((b): b is Anthropic.TextBlock => b.type === "text")
1690
+ .map((b) => b.text)
1691
+ .join("");
1692
+ } finally {
1693
+ await fireHook("stop", {
1694
+ sessionId,
1695
+ turnCount: runContext.turnNumber,
1696
+ reason: "complete",
1697
+ });
1698
+ await subscribers.flushAll();
1699
+ await subscribers.shutdownAll();
1700
+ await sessionStore
1701
+ .update(sessionId, { lastTurnIndex: runContext.turnNumber })
1702
+ .catch((err) => {
1703
+ runContext.logger.warn("session-store: lastTurnIndex update failed", {
1704
+ error: (err as Error).message,
1705
+ });
1706
+ });
1707
+ await eventLog.close();
1708
+ }
1709
+ }
1710
+
1711
+ // REPL mode (existing behavior). When resuming, the prior session's
1712
+ // transcript becomes the seed; otherwise honour any caller-supplied
1713
+ // seedMessages.
1714
+ let messages: Anthropic.MessageParam[] = resumedMessages
1715
+ ? [...resumedMessages]
1716
+ : opts.seedMessages
1717
+ ? [...opts.seedMessages]
1718
+ : [];
1719
+
1720
+ const rl = readline.createInterface({
1721
+ input: opts.input ?? process.stdin,
1722
+ output: process.stdout,
1723
+ });
1724
+
1725
+ // EOF on stdin (e.g. when input is piped in non-interactively) auto-closes
1726
+ // the readline interface. The next `rl.question` call would then crash with
1727
+ // ERR_USE_AFTER_CLOSE, so race each prompt against the close event and
1728
+ // also catch the post-close throw — close can fire either before we issue
1729
+ // the next prompt or while we're already awaiting one.
1730
+ const STDIN_CLOSED = Symbol("stdin-closed");
1731
+ const closedSignal = new Promise<typeof STDIN_CLOSED>((resolve) => {
1732
+ rl.once("close", () => resolve(STDIN_CLOSED));
1733
+ });
1734
+
1735
+ // Wire the permission-ask prompter once the readline interface exists.
1736
+ // Treats a closed stdin as "deny" so a piped non-interactive input does
1737
+ // not stall on the approval prompt.
1738
+ askApproval = async (toolName: string, input: unknown): Promise<boolean> => {
1739
+ const inputPreview = JSON.stringify(input).slice(0, 200);
1740
+ try {
1741
+ const result = await Promise.race([
1742
+ rl.question(`approve ${toolName} ${inputPreview} [y/N] > `),
1743
+ closedSignal,
1744
+ ]);
1745
+ if (result === STDIN_CLOSED) return false;
1746
+ const normalized = result.trim().toLowerCase();
1747
+ return normalized === "y" || normalized === "yes";
1748
+ } catch (err) {
1749
+ if ((err as NodeJS.ErrnoException).code === "ERR_USE_AFTER_CLOSE") return false;
1750
+ throw err;
1751
+ }
1752
+ };
1753
+
1754
+ const providerNote = providerId === "anthropic" ? "" : ` [${providerId}]`;
1755
+ process.stdout.write(`agent ready (model: ${opts.model})${providerNote}. type "exit" to quit.\n`);
1756
+
1757
+ // M3.2 — plan-mode UX indicator. When `permissions.mode: plan` is
1758
+ // active, the LLM-policy permission engine is the gate; show the
1759
+ // user it's on so they know destructive tools will be pushed back on.
1760
+ if (permissionMode === "plan") {
1761
+ process.stdout.write(
1762
+ "\x1b[1m\x1b[33m[plan mode]\x1b[0m destructive tools (Write, Edit, Bash) gate through LLM policy; type a request to plan, then approve before execution.\n",
1763
+ );
1764
+ } else if (permissionMode === "auto") {
1765
+ process.stdout.write(
1766
+ "\x1b[1m\x1b[35m[auto mode]\x1b[0m all rules apply, no prompts — the agent acts under your declared permissions.\n",
1767
+ );
1768
+ } else if (permissionMode === "bypass") {
1769
+ process.stdout.write(
1770
+ "\x1b[1m\x1b[31m[BYPASS mode]\x1b[0m all permission rules are disabled. Use only on isolated machines.\n",
1771
+ );
1772
+ }
1773
+
1774
+ // SIGINT handler: first press during a turn aborts the turn; second
1775
+ // press exits. Counter resets at the start of each new turn. Default-on
1776
+ // whenever the runtime owns process.stdin (i.e. the test option
1777
+ // `opts.input` is unset). Tests that supply their own input stream skip
1778
+ // installation so they don't interfere with the test runner's signals.
1779
+ const shouldInstallSigint = opts.installSigintHandler ?? opts.input === undefined;
1780
+ let sigintPresses = 0;
1781
+ const sigintHandler = (): void => {
1782
+ sigintPresses += 1;
1783
+ if (sigintPresses === 1) {
1784
+ process.stdout.write("\n[turn aborted; press Ctrl-C again to exit]\n");
1785
+ turnAbort.abort();
1786
+ } else {
1787
+ process.stdout.write("\n[exiting]\n");
1788
+ process.exit(130);
1789
+ }
1790
+ };
1791
+ if (shouldInstallSigint) {
1792
+ process.on("SIGINT", sigintHandler);
1793
+ }
1794
+
1795
+ try {
1796
+ while (true) {
1797
+ if (runAbort.signal.aborted) break;
1798
+
1799
+ // Fresh turn-abort tree so a previous turn's abort doesn't bleed in.
1800
+ turnAbort = runAbort.child();
1801
+ sigintPresses = 0;
1802
+
1803
+ let userInput: string;
1804
+ try {
1805
+ const result = await Promise.race([rl.question("\nyou> "), closedSignal]);
1806
+ if (result === STDIN_CLOSED) break;
1807
+ userInput = result.trim();
1808
+ } catch (err) {
1809
+ if ((err as NodeJS.ErrnoException).code === "ERR_USE_AFTER_CLOSE") break;
1810
+ throw err;
1811
+ }
1812
+ if (userInput === "") continue;
1813
+ if (userInput === "exit" || userInput === "quit") break;
1814
+
1815
+ // Section 11 — slash-command expansion. When the user input matches
1816
+ // a registered slash command, the markdown body (with $ARGUMENTS
1817
+ // substitution) becomes the effective user message. A `pre-slash`
1818
+ // hook fires first; deny falls through with the original input,
1819
+ // mutate.expanded overrides the substituted text.
1820
+ let effectiveInput = userInput;
1821
+ if (opts.slashCommands && opts.slashCommands.size > 0) {
1822
+ const slash = expandSlash(userInput, opts.slashCommands);
1823
+ if (slash.handled) {
1824
+ const decision = await fireHook("pre-slash", {
1825
+ name: slash.command?.name,
1826
+ arguments: slash.arguments,
1827
+ expanded: slash.expanded,
1828
+ });
1829
+ if (decision.allowed) {
1830
+ const override = decision.mutate?.["expanded"];
1831
+ effectiveInput = typeof override === "string" ? override : slash.expanded;
1832
+ } else {
1833
+ runContext.logger.info("pre-slash hook denied", {
1834
+ name: slash.command?.name,
1835
+ reason: decision.reason,
1836
+ });
1837
+ }
1838
+ }
1839
+ }
1840
+
1841
+ messages.push({ role: "user", content: effectiveInput });
1842
+ await logEvent("user_message", { content: effectiveInput });
1843
+ runContext.turnNumber += 1;
1844
+ bus.setTurnNumber(runContext.turnNumber);
1845
+ const t0Turn = performance.now();
1846
+ bus.publish({
1847
+ ...bus.envelope(),
1848
+ kind: "turn_start",
1849
+ turn: runContext.turnNumber,
1850
+ messageCount: messages.length,
1851
+ });
1852
+ runContext.logger.debug("turn start", {
1853
+ turn: runContext.turnNumber,
1854
+ messages: messages.length,
1855
+ });
1856
+
1857
+ // Pre-turn budget check: snip first (free), then autocompact if still over.
1858
+ messages = await maybeCompact(
1859
+ {
1860
+ messages,
1861
+ adapter: compactionAdapter,
1862
+ model: compactionWireModelId,
1863
+ contextLimit,
1864
+ compactionThreshold,
1865
+ snipKeepHead,
1866
+ snipKeepTail,
1867
+ logger: runContext.logger,
1868
+ },
1869
+ async (info) => {
1870
+ await logEvent("compaction", info);
1871
+ bus.publish({
1872
+ ...bus.envelope(),
1873
+ kind: "compaction_fired",
1874
+ subKind: info.kind,
1875
+ before: info.before,
1876
+ after: info.after,
1877
+ phase: "pre-turn",
1878
+ });
1879
+ await fireHook("pre-compact", { ...info, phase: "pre-turn" });
1880
+ await fireHook("post-compact", { ...info, phase: "pre-turn" });
1881
+ },
1882
+ );
1883
+
1884
+ try {
1885
+ await runOneTurn(messages);
1886
+ } catch (err) {
1887
+ if (isAbortError(err)) {
1888
+ // Already-handled abort; loop back to the prompt.
1889
+ process.stdout.write("\n[turn aborted]\n");
1890
+ } else {
1891
+ throw err;
1892
+ }
1893
+ }
1894
+
1895
+ bus.publish({
1896
+ ...bus.envelope(),
1897
+ kind: "turn_end",
1898
+ turn: runContext.turnNumber,
1899
+ durationMs: performance.now() - t0Turn,
1900
+ });
1901
+ runContext.logger.debug("turn end", {
1902
+ turn: runContext.turnNumber,
1903
+ });
1904
+ }
1905
+ } finally {
1906
+ await fireHook("stop", {
1907
+ sessionId,
1908
+ turnCount: runContext.turnNumber,
1909
+ reason: runAbort.signal.aborted ? "abort" : "exit",
1910
+ });
1911
+ await subscribers.flushAll();
1912
+ await subscribers.shutdownAll();
1913
+ if (shouldInstallSigint) {
1914
+ process.removeListener("SIGINT", sigintHandler);
1915
+ }
1916
+ rl.close();
1917
+ await sessionStore.update(sessionId, { lastTurnIndex: runContext.turnNumber }).catch((err) => {
1918
+ runContext.logger.warn("session-store: lastTurnIndex update failed", {
1919
+ error: (err as Error).message,
1920
+ });
1921
+ });
1922
+ await eventLog.close();
1923
+ }
1924
+
1925
+ return "";
1926
+ }
1927
+
1928
+ function isAbortError(err: unknown): boolean {
1929
+ // Section 17 — runtime-core no longer imports the Anthropic SDK at
1930
+ // runtime, so we name-match instead of `instanceof`. The provider
1931
+ // adapters preserve the SDK error names through their normalisation
1932
+ // layers, so this catches both Anthropic and OpenAI/Gemini/Bedrock
1933
+ // abort errors uniformly.
1934
+ const name = (err as { name?: unknown }).name;
1935
+ return typeof name === "string" && (name === "AbortError" || name === "APIUserAbortError");
1936
+ }
1937
+
1938
+ type CompactionInfo = {
1939
+ readonly kind: "snip" | "autocompact";
1940
+ readonly before: number;
1941
+ readonly after: number;
1942
+ };
1943
+
1944
+ type CompactArgs = {
1945
+ messages: Anthropic.MessageParam[];
1946
+ adapter: ProviderAdapter;
1947
+ model: string;
1948
+ contextLimit: number;
1949
+ compactionThreshold: number;
1950
+ snipKeepHead: number;
1951
+ snipKeepTail: number;
1952
+ logger: RunContext["logger"];
1953
+ };
1954
+
1955
+ type OnCompaction = (info: CompactionInfo) => Promise<void>;
1956
+
1957
+ /**
1958
+ * Pre-turn compaction ladder: estimate → snip → re-estimate → autocompact.
1959
+ * Returns the (possibly replaced) messages array. Pure with respect to
1960
+ * the input array; callers reassign. The optional `onCompaction` callback
1961
+ * fires once per applied step so the runtime can append a `compaction`
1962
+ * event to the session log without duplicating the cost-estimation logic.
1963
+ */
1964
+ async function maybeCompact(
1965
+ args: CompactArgs,
1966
+ onCompaction?: OnCompaction,
1967
+ ): Promise<Anthropic.MessageParam[]> {
1968
+ const {
1969
+ messages,
1970
+ adapter,
1971
+ model,
1972
+ contextLimit,
1973
+ compactionThreshold,
1974
+ snipKeepHead,
1975
+ snipKeepTail,
1976
+ logger,
1977
+ } = args;
1978
+
1979
+ const initialBudget = new TokenBudget(contextLimit);
1980
+ initialBudget.add(estimateTokens(messages), 0);
1981
+ if (!initialBudget.isApproachingLimit(compactionThreshold)) {
1982
+ return messages;
1983
+ }
1984
+
1985
+ const snipped = snip(messages, snipKeepHead, snipKeepTail);
1986
+ logger.info("snip applied", { before: messages.length, after: snipped.length });
1987
+ if (onCompaction !== undefined) {
1988
+ await onCompaction({ kind: "snip", before: messages.length, after: snipped.length });
1989
+ }
1990
+
1991
+ const postSnipBudget = new TokenBudget(contextLimit);
1992
+ postSnipBudget.add(estimateTokens(snipped), 0);
1993
+ if (!postSnipBudget.isApproachingLimit(compactionThreshold)) {
1994
+ return snipped;
1995
+ }
1996
+
1997
+ logger.info("autocompact triggered", { tokensAfterSnip: postSnipBudget.used });
1998
+ const after = await autoCompact(snipped, adapter, model);
1999
+ if (onCompaction !== undefined) {
2000
+ await onCompaction({ kind: "autocompact", before: snipped.length, after: after.length });
2001
+ }
2002
+ return after;
2003
+ }
2004
+
2005
+ type ForceCompactArgs = {
2006
+ messages: Anthropic.MessageParam[];
2007
+ adapter: ProviderAdapter;
2008
+ model: string;
2009
+ snipKeepHead: number;
2010
+ snipKeepTail: number;
2011
+ logger: RunContext["logger"];
2012
+ };
2013
+
2014
+ /**
2015
+ * Reactive compaction triggered by the recovery engine on a prompt_too_long
2016
+ * error. Unlike pre-turn `maybeCompact`, this one runs unconditionally:
2017
+ * snip first, then autocompact (whose model summary reliably trims the
2018
+ * largest blob).
2019
+ */
2020
+ async function forceCompact(args: ForceCompactArgs): Promise<Anthropic.MessageParam[]> {
2021
+ const { messages, adapter, model, snipKeepHead, snipKeepTail, logger } = args;
2022
+ const snipped = snip(messages, snipKeepHead, snipKeepTail);
2023
+ logger.info("reactive snip applied", { before: messages.length, after: snipped.length });
2024
+ return await autoCompact(snipped, adapter, model);
2025
+ }