@crewhaus/runtime-core 0.1.3 → 0.1.5

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