@crewhaus/runtime-core 0.2.0 → 0.2.1

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.
Files changed (3) hide show
  1. package/dist/index.d.ts +64 -0
  2. package/dist/index.js +245 -17
  3. package/package.json +40 -39
package/dist/index.d.ts CHANGED
@@ -7,12 +7,22 @@ import { type HookDef } from "@crewhaus/hooks-engine";
7
7
  import { type JustificationJudge, type PermissionMode, type RuleSet } from "@crewhaus/permission-engine";
8
8
  import type { RateLimiter } from "@crewhaus/rate-limiter";
9
9
  import { type NamedFailureClass } from "@crewhaus/recovery-engine";
10
+ import { type Scoreboard } from "@crewhaus/routing-store";
10
11
  import { type RunContext } from "@crewhaus/run-context";
11
12
  import { type SkillRef } from "@crewhaus/skills-registry";
12
13
  import { type SlashCommand } from "@crewhaus/slash-commands";
13
14
  import type { RegisteredTool } from "@crewhaus/tool-catalog";
14
15
  import { type AlertSink } from "./observability";
15
16
  import type { SloMitigationSink, SloTargets } from "./slo-monitor";
17
+ /**
18
+ * Run `items` through `fn` with at most `limit` in flight at once,
19
+ * preserving input order in the returned results. A pool of `limit`
20
+ * workers pull from a shared cursor, so a slow item never blocks a fast
21
+ * one behind it (unlike fixed-size `Promise.all` chunks). `limit <= 0`
22
+ * is clamped to 1; a limit larger than the batch just runs everything.
23
+ * Exported for unit testing of the concurrency bound.
24
+ */
25
+ export declare function mapWithConcurrency<T, R>(items: ReadonlyArray<T>, limit: number, fn: (item: T, index: number) => Promise<R>): Promise<R[]>;
16
26
  export { CLAUDE_CODE_HEADERS, CLAUDE_CODE_SYSTEM_PREFIX, OAUTH_BETAS, createAnthropicClient, resolveAuth, } from "@crewhaus/adapter-anthropic";
17
27
  export type { ResolvedAuth } from "@crewhaus/adapter-anthropic";
18
28
  export { createJanitor } from "./janitor";
@@ -425,6 +435,14 @@ export type RunChatLoopOptions = {
425
435
  * agentic turns, or lower to tighten.
426
436
  */
427
437
  maxToolIterations?: number;
438
+ /**
439
+ * Max number of concurrency-safe tool calls executed in parallel within
440
+ * a single turn. Chiefly bounds parallel sub-agent (`Task`) fan-out so a
441
+ * wide dispatch can't open unbounded model connections; parallel reads
442
+ * are also capped but their per-call cost is negligible. Defaults to
443
+ * `DEFAULT_MAX_CONCURRENT_TOOLS` (4). `<= 0` is clamped to 1 (serial).
444
+ */
445
+ maxConcurrentTools?: number;
428
446
  /**
429
447
  * Section 27 / item 22 — wrap the resolved primary `ProviderAdapter` in a
430
448
  * circuit breaker before any `stream()` call. When the breaker trips,
@@ -484,6 +502,52 @@ export type RunChatLoopOptions = {
484
502
  * undefined (the runtime resolves both tiers through the normal router).
485
503
  */
486
504
  _tierAdapters?: ReadonlyMap<string, ProviderAdapter>;
505
+ /**
506
+ * Adaptive model routing (spec `agent.model_pool`). When set, every
507
+ * candidate adapter resolves at boot and each turn a `PolicyRouter` selects
508
+ * one from deterministic signals (`static`/`heuristic`) or the durable reward
509
+ * scoreboard (`learned`), publishing a `model_route` trace event. After each
510
+ * turn the observed outcome (success, latency, cost) is folded into the
511
+ * scoreboard so selection improves the more the harness runs. Mutually
512
+ * exclusive with `modelTiers`/`modelFallbacks` (enforced in the spec); the
513
+ * pool takes precedence if all are somehow set. Absent → single-model path.
514
+ */
515
+ modelPool?: {
516
+ readonly candidates: ReadonlyArray<{
517
+ readonly model: string;
518
+ readonly tags: readonly string[];
519
+ }>;
520
+ readonly policy: "static" | "heuristic" | "learned";
521
+ readonly objective?: {
522
+ readonly quality?: number;
523
+ readonly cost?: number;
524
+ readonly latency?: number;
525
+ };
526
+ readonly routing?: {
527
+ readonly contextTokenThreshold?: number;
528
+ readonly toolsToDefault?: boolean;
529
+ readonly firstTurnToDefault?: boolean;
530
+ readonly priorToolDensityThreshold?: number;
531
+ readonly strongTag?: string;
532
+ readonly cheapTag?: string;
533
+ };
534
+ readonly learning?: {
535
+ readonly minSamplesPerArm?: number;
536
+ readonly costRefUsd?: number;
537
+ readonly latencyRefMs?: number;
538
+ };
539
+ };
540
+ /**
541
+ * Test injection for the pool candidate adapters, keyed by their spec model
542
+ * string (mirrors `_tierAdapters`). Production callers leave it undefined.
543
+ */
544
+ _poolAdapters?: ReadonlyMap<string, ProviderAdapter>;
545
+ /**
546
+ * Test injection for the reward scoreboard. Production callers leave it
547
+ * undefined (the runtime opens a file-backed scoreboard beside the sessions
548
+ * dir); tests inject an in-memory or tmp-dir instance to assert learning.
549
+ */
550
+ _scoreboard?: Scoreboard;
487
551
  /**
488
552
  * Section 55 (Track A) / item 23 — the spec's `failure_taxonomy`, lowered
489
553
  * from the IR. Consulted by `recovery-engine.recover()` BEFORE its
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { dirname as pathDirname } from "node:path";
1
2
  import * as readline from "node:readline/promises";
2
3
  import { createAbortTree } from "@crewhaus/abort-controller";
3
4
  import { collectFinalMessage, consumeStream, } from "@crewhaus/adapter-anthropic";
@@ -5,16 +6,17 @@ import { classifyBoundary, setDefaultBoundaryLlmClassifier } from "@crewhaus/bou
5
6
  import { wrap as wrapWithCircuitBreaker } from "@crewhaus/circuit-breaker";
6
7
  import { autoCompact } from "@crewhaus/compaction-autocompact";
7
8
  import { snip } from "@crewhaus/compaction-snip";
8
- import { createCostTracker } from "@crewhaus/cost-tracker";
9
+ import { DEFAULT_PRICING, computeCostMicros, createCostTracker, resolvePricing, } from "@crewhaus/cost-tracker";
9
10
  import { classifyEgress, summarizeEgress, } from "@crewhaus/egress-classifier";
10
11
  import { ConfigError, RuntimeError } from "@crewhaus/errors";
11
12
  import { openEventLog } from "@crewhaus/event-log";
12
13
  import { aggregateDecisions, runHooks } from "@crewhaus/hooks-engine";
13
- import { createFailoverChain, createTierRouter, parseModelString, resolveModel, } from "@crewhaus/model-router";
14
+ import { createFailoverChain, createPolicyRouter, createTierRouter, parseModelString, resolveModel, } from "@crewhaus/model-router";
14
15
  import { BUILTIN_DEFAULT_RULES, emptyRuleSet, evaluateJustification, evaluateWithReason, ruleBasedJustificationJudge, } from "@crewhaus/permission-engine";
15
16
  import { manage as manageCacheMarkers } from "@crewhaus/prompt-cache-manager";
16
17
  import { buildRedactionNotice, classifyText, llmClassifierEnabled, } from "@crewhaus/prompt-injection-detector";
17
18
  import { advanceState, initialRecoveryState, recover, } from "@crewhaus/recovery-engine";
19
+ import { computeReward, openScoreboard, } from "@crewhaus/routing-store";
18
20
  import { createRunContext, tagContent } from "@crewhaus/run-context";
19
21
  import { createSessionStore } from "@crewhaus/session-store";
20
22
  import { formatSkillsForPrompt } from "@crewhaus/skills-registry";
@@ -107,9 +109,39 @@ const DEFAULT_CONTEXT_LIMIT = 200_000;
107
109
  // agentic turn reaches it, low enough to stop a runaway/injected tool loop
108
110
  // before it burns a provider quota.
109
111
  const DEFAULT_MAX_TOOL_ITERATIONS = 500;
112
+ // Upper bound on how many concurrency-safe tool calls in one turn run at
113
+ // once. Bounds parallel sub-agent (`Task`) fan-out so a wide dispatch
114
+ // can't open unbounded model connections and trip a provider rate limit;
115
+ // also caps parallel reads, whose per-call cost is negligible. Configurable
116
+ // via `runChatLoop({ maxConcurrentTools })`.
117
+ const DEFAULT_MAX_CONCURRENT_TOOLS = 4;
110
118
  const DEFAULT_COMPACTION_THRESHOLD = 0.85;
111
119
  const DEFAULT_SNIP_KEEP_HEAD = 4;
112
120
  const DEFAULT_SNIP_KEEP_TAIL = 20;
121
+ /**
122
+ * Run `items` through `fn` with at most `limit` in flight at once,
123
+ * preserving input order in the returned results. A pool of `limit`
124
+ * workers pull from a shared cursor, so a slow item never blocks a fast
125
+ * one behind it (unlike fixed-size `Promise.all` chunks). `limit <= 0`
126
+ * is clamped to 1; a limit larger than the batch just runs everything.
127
+ * Exported for unit testing of the concurrency bound.
128
+ */
129
+ export async function mapWithConcurrency(items, limit, fn) {
130
+ const results = new Array(items.length);
131
+ const bound = Math.max(1, Math.min(limit, items.length));
132
+ let cursor = 0;
133
+ const worker = async () => {
134
+ while (true) {
135
+ const i = cursor;
136
+ cursor += 1;
137
+ if (i >= items.length)
138
+ return;
139
+ results[i] = await fn(items[i], i);
140
+ }
141
+ };
142
+ await Promise.all(Array.from({ length: bound }, () => worker()));
143
+ return results;
144
+ }
113
145
  // Section 17 — `resolveAuth` / `createAnthropicClient` / `ResolvedAuth` /
114
146
  // `OAUTH_BETAS` / `CLAUDE_CODE_HEADERS` / `CLAUDE_CODE_SYSTEM_PREFIX`
115
147
  // moved into `@crewhaus/adapter-anthropic`. Re-exported here so legacy
@@ -335,6 +367,23 @@ function bestEffortWireModelId(modelString) {
335
367
  return modelString;
336
368
  }
337
369
  }
370
+ /**
371
+ * A short, stable fingerprint of a `model_pool` config, stamped on every
372
+ * `model_route` event as `policyVersion` so a learned decision can be tied back
373
+ * to the exact policy that made it. Deterministic (djb2 over the candidate
374
+ * roster + policy + objective) — no clock, no randomness.
375
+ */
376
+ function poolFingerprint(pool) {
377
+ const canonical = JSON.stringify({
378
+ p: pool.policy,
379
+ c: pool.candidates.map((c) => [c.model, [...c.tags].sort()]),
380
+ o: pool.objective ?? null,
381
+ });
382
+ let hash = 5381;
383
+ for (let i = 0; i < canonical.length; i++)
384
+ hash = ((hash << 5) + hash + canonical.charCodeAt(i)) >>> 0;
385
+ return `pool-${hash.toString(36)}`;
386
+ }
338
387
  export async function runChatLoop(opts) {
339
388
  // Pillar 3 — make the model-backed Layer-3 classifier reachable at EVERY
340
389
  // trust boundary (MCP / sub-agent / channel / federation / skill /
@@ -510,6 +559,80 @@ export async function runChatLoop(opts) {
510
559
  // Housekeeping side-effect: evicts any session whose mtime is older than
511
560
  // the TTL (default 30 days). The returned list is intentionally discarded.
512
561
  await sessionStore.list();
562
+ // Adaptive model routing (spec `agent.model_pool`). Resolve every candidate
563
+ // adapter ONCE at boot (mirroring the tier router), open the durable reward
564
+ // scoreboard beside the sessions dir, and build the PolicyRouter. The
565
+ // scoreboard is opened whenever a pool is present — not just for `learned` —
566
+ // so `route status` has data and a later switch to `learned` inherits the
567
+ // accumulated history. `recordPoolOutcome`/`poolCostUsd` fold each turn's
568
+ // observed result back in; they no-op when no pool is active.
569
+ let poolRouter;
570
+ let scoreboard;
571
+ let rewardConfig;
572
+ let poolPolicyVersion;
573
+ if (opts.modelPool !== undefined) {
574
+ const pool = opts.modelPool;
575
+ const candidates = [];
576
+ for (const c of pool.candidates) {
577
+ const injected = opts._poolAdapters?.get(c.model);
578
+ if (injected !== undefined) {
579
+ candidates.push({
580
+ adapter: injected,
581
+ modelId: bestEffortWireModelId(c.model),
582
+ modelString: c.model,
583
+ tags: c.tags,
584
+ });
585
+ }
586
+ else {
587
+ const r = await resolveModel(c.model);
588
+ candidates.push({
589
+ adapter: r.adapter,
590
+ modelId: r.modelId,
591
+ modelString: c.model,
592
+ tags: c.tags,
593
+ });
594
+ }
595
+ }
596
+ const routingRoot = sessionRootDir !== undefined ? pathDirname(sessionRootDir) : ".crewhaus";
597
+ scoreboard = opts._scoreboard ?? openScoreboard(routingRoot);
598
+ const sb = scoreboard;
599
+ rewardConfig = {
600
+ ...(pool.objective !== undefined ? { objective: pool.objective } : {}),
601
+ ...(pool.learning?.costRefUsd !== undefined ? { costRefUsd: pool.learning.costRefUsd } : {}),
602
+ ...(pool.learning?.latencyRefMs !== undefined
603
+ ? { latencyRefMs: pool.learning.latencyRefMs }
604
+ : {}),
605
+ };
606
+ poolPolicyVersion = poolFingerprint(pool);
607
+ poolRouter = createPolicyRouter({
608
+ candidates,
609
+ policy: pool.policy,
610
+ ...(pool.routing !== undefined ? { routing: pool.routing } : {}),
611
+ ...(pool.learning?.minSamplesPerArm !== undefined
612
+ ? { learning: { minSamplesPerArm: pool.learning.minSamplesPerArm } }
613
+ : {}),
614
+ // The learned policy reads the live scoreboard; static/heuristic ignore it.
615
+ ...(pool.policy === "learned" ? { score: (rk, m) => sb.score(rk, m) } : {}),
616
+ });
617
+ }
618
+ // Fold one turn's observed outcome into the scoreboard (no-op without a pool).
619
+ const recordPoolOutcome = (turn, obs) => {
620
+ // Capture into consts so the narrowing survives (they are closed-over lets).
621
+ const sb = scoreboard;
622
+ const rc = rewardConfig;
623
+ if (turn === undefined || sb === undefined || rc === undefined)
624
+ return;
625
+ sb.record(turn.routeKey, turn.modelString, computeReward(obs, rc), obs);
626
+ };
627
+ // Per-turn USD cost from token usage + the static pricing table. Undefined
628
+ // when the served model isn't on the table (the reward then drops the cost
629
+ // term and reweights over quality + latency).
630
+ const poolCostUsd = (provider, wireModelId, usage) => {
631
+ const row = resolvePricing(DEFAULT_PRICING, provider, wireModelId);
632
+ if (row === undefined)
633
+ return undefined;
634
+ return (computeCostMicros(row, usage.input, usage.output, usage.cacheRead ?? 0, usage.cacheCreate ?? 0) / 1_000_000);
635
+ };
513
636
  let sessionId;
514
637
  let resumedMessages;
515
638
  if (opts.resume) {
@@ -1510,10 +1633,15 @@ export async function runChatLoop(opts) {
1510
1633
  * they line up with the assistant turn's tool_use blocks.
1511
1634
  */
1512
1635
  async function runToolBatch(toolUses) {
1513
- const partition = partitionToolCalls(toolUses, (n) => toolByName.get(n));
1636
+ // Pass the full catalog so per-call classifiers (Task) can resolve a
1637
+ // dispatch's child tool set; without it, classifier-based tools stay
1638
+ // serial (fail-closed).
1639
+ const partition = partitionToolCalls(toolUses, (n) => toolByName.get(n), tools);
1640
+ const maxConcurrentTools = opts.maxConcurrentTools ?? DEFAULT_MAX_CONCURRENT_TOOLS;
1514
1641
  runContext.logger.debug("tool partition", {
1515
1642
  concurrent: partition.concurrent.map((b) => b.length),
1516
1643
  serial: partition.serial.length,
1644
+ maxConcurrentTools,
1517
1645
  });
1518
1646
  // Map each tool_use's identity to its slot in the original order so
1519
1647
  // results can be placed back in order regardless of the
@@ -1525,7 +1653,7 @@ export async function runChatLoop(opts) {
1525
1653
  toolUses.forEach((tu, idx) => indexByBlock.set(tu, idx));
1526
1654
  const results = new Array(toolUses.length);
1527
1655
  for (const batch of partition.concurrent) {
1528
- const settled = await Promise.all(batch.map((tu) => executeOneToolUse(tu)));
1656
+ const settled = await mapWithConcurrency(batch, maxConcurrentTools, (tu) => executeOneToolUse(tu));
1529
1657
  batch.forEach((tu, i) => {
1530
1658
  // biome-ignore lint/style/noNonNullAssertion: every block came from toolUses, so its index is registered.
1531
1659
  results[indexByBlock.get(tu)] = settled[i];
@@ -1580,6 +1708,10 @@ export async function runChatLoop(opts) {
1580
1708
  let thisTurnToolUseCount = 0;
1581
1709
  let escalateTier = false;
1582
1710
  let servedFastTier = false;
1711
+ // Adaptive model routing — once a pool candidate fails, stick with the
1712
+ // strongest candidate for the rest of the run (misroute recovery, mirroring
1713
+ // the tier router's fast→default escalation latch).
1714
+ let escalatePool = false;
1583
1715
  while (state.kind !== "Done") {
1584
1716
  if (turnAbort.signal.aborted) {
1585
1717
  state = transition(state, { kind: "Aborted" });
@@ -1617,6 +1749,10 @@ export async function runChatLoop(opts) {
1617
1749
  // gap before the model responds now shows live progress. Cleared in
1618
1750
  // the catch below on any model-call error.
1619
1751
  out.spinner.start("thinking", { prefix: "agent> " });
1752
+ // Adaptive model routing — declared OUTSIDE the try so the catch can
1753
+ // read the chosen candidate + start time to record a failure reward.
1754
+ let poolTurn;
1755
+ let modelCallStartMs = performance.now();
1620
1756
  try {
1621
1757
  // Section 27 — rate-limit the model call before opening the
1622
1758
  // stream. The keys are caller-configured (typically
@@ -1645,13 +1781,55 @@ export async function runChatLoop(opts) {
1645
1781
  // The adapter this turn streams through: the primary/chain by
1646
1782
  // default, or a tier adapter when the two-tier router is active.
1647
1783
  let turnAdapter = adapter;
1648
- // Item 26 — two-tier turn-difficulty router. Pick a tier from
1649
- // DETERMINISTIC per-turn signals (context size, tools-in-play,
1650
- // turn index, prior-turn tool_use density); a fast-tier misroute
1651
- // recovery forces `default` (see the catch below). Publish the
1652
- // decision as a `model_tier_route` event, then stream through the
1653
- // chosen tier's already-resolved adapter.
1654
- if (tierRouter !== undefined) {
1784
+ // Adaptive model routing — the PolicyRouter picks a pool candidate
1785
+ // this turn (static / heuristic / learned) from the SAME
1786
+ // deterministic signals as the tier router. `escalatePool` forces
1787
+ // the strongest candidate after a prior pool failure (misroute
1788
+ // recovery). Publish a `model_route` event and stream through the
1789
+ // chosen candidate; the outcome is folded into the reward
1790
+ // scoreboard post-turn. Takes precedence over the tier router (the
1791
+ // two are mutually exclusive in the spec).
1792
+ if (poolRouter !== undefined) {
1793
+ const base = poolRouter.route({
1794
+ contextTokens: estimateTokens(messages),
1795
+ toolsInPlay: (anthropicTools?.length ?? 0) > 0,
1796
+ turnIndex: Math.max(0, runContext.turnNumber - 1),
1797
+ priorTurnToolUseCount,
1798
+ });
1799
+ const decision = escalatePool
1800
+ ? {
1801
+ ...base,
1802
+ candidate: poolRouter.escalation(),
1803
+ reason: `escalated to strongest candidate after a pool failure (was: ${base.reason})`,
1804
+ explored: false,
1805
+ }
1806
+ : base;
1807
+ turnAdapter = decision.candidate.adapter;
1808
+ reqWireModelId = decision.candidate.modelId;
1809
+ reqProviderId = decision.candidate.adapter.providerId;
1810
+ reqSpecModel = decision.candidate.modelString;
1811
+ poolTurn = {
1812
+ modelString: decision.candidate.modelString,
1813
+ routeKey: decision.routeKey,
1814
+ };
1815
+ bus.publish({
1816
+ ...bus.envelope(),
1817
+ kind: "model_route",
1818
+ routeKey: decision.routeKey,
1819
+ model: decision.candidate.modelId,
1820
+ policy: decision.policy,
1821
+ reason: decision.reason,
1822
+ ...(decision.explored ? { explored: true } : {}),
1823
+ ...(poolPolicyVersion !== undefined ? { policyVersion: poolPolicyVersion } : {}),
1824
+ });
1825
+ }
1826
+ else if (tierRouter !== undefined) {
1827
+ // Item 26 — two-tier turn-difficulty router. Pick a tier from
1828
+ // DETERMINISTIC per-turn signals (context size, tools-in-play,
1829
+ // turn index, prior-turn tool_use density); a fast-tier misroute
1830
+ // recovery forces `default` (see the catch below). Publish the
1831
+ // decision as a `model_tier_route` event, then stream through the
1832
+ // chosen tier's already-resolved adapter.
1655
1833
  const decision = escalateTier
1656
1834
  ? { tier: "default", reason: "escalated after fast-tier failure" }
1657
1835
  : tierRouter.route({
@@ -1689,6 +1867,7 @@ export async function runChatLoop(opts) {
1689
1867
  streaming: opts.streaming === true,
1690
1868
  });
1691
1869
  const t0Model = performance.now();
1870
+ modelCallStartMs = t0Model;
1692
1871
  let streamChunkIndex = 0;
1693
1872
  // Section 17 — `adapter.stream(req)` returns
1694
1873
  // `AsyncIterable<StreamEvent>`; we accumulate via
@@ -1743,6 +1922,10 @@ export async function runChatLoop(opts) {
1743
1922
  toolByName,
1744
1923
  abortSignal: turnAbort.signal,
1745
1924
  onTextDelta,
1925
+ // Honor the same concurrency bound as the non-streaming
1926
+ // path — otherwise `maxConcurrentTools` would be silently
1927
+ // ignored under `streaming: true`.
1928
+ maxConcurrent: opts.maxConcurrentTools ?? DEFAULT_MAX_CONCURRENT_TOOLS,
1746
1929
  runTool: (block) => executeOneToolUse({
1747
1930
  id: block.id,
1748
1931
  name: block.name,
@@ -1778,8 +1961,15 @@ export async function runChatLoop(opts) {
1778
1961
  // Item 22 — lastServed() is exact: the candidate that actually
1779
1962
  // streamed this response. Cost-tracker prices off this pair.
1780
1963
  const servedStreaming = failoverChain?.lastServed();
1781
- const respModelIdS = servedStreaming?.modelId ?? wireModelId;
1782
- const respSpecModelS = servedStreaming?.modelString ?? degradedSpecModel ?? opts.model;
1964
+ // With a pool, the served identity is the chosen candidate (no
1965
+ // failover chain when pooling), so cost-tracker prices on it.
1966
+ const respModelIdS = poolTurn !== undefined ? reqWireModelId : (servedStreaming?.modelId ?? wireModelId);
1967
+ const respSpecModelS = poolTurn !== undefined
1968
+ ? poolTurn.modelString
1969
+ : (servedStreaming?.modelString ?? degradedSpecModel ?? opts.model);
1970
+ const respProviderS = poolTurn !== undefined
1971
+ ? reqProviderId
1972
+ : (servedStreaming?.providerId ?? providerId);
1783
1973
  bus.publish({
1784
1974
  ...bus.envelope(),
1785
1975
  spanId: modelStartEnv.spanId,
@@ -1788,11 +1978,21 @@ export async function runChatLoop(opts) {
1788
1978
  // pricing on this field (see the model_request publish above).
1789
1979
  model: respModelIdS,
1790
1980
  ...(respSpecModelS !== respModelIdS ? { specModel: respSpecModelS } : {}),
1791
- provider: servedStreaming?.providerId ?? providerId,
1981
+ provider: respProviderS,
1792
1982
  stopReason: stopReason ?? "end_turn",
1793
1983
  usage,
1794
1984
  durationMs: performance.now() - t0Model,
1795
1985
  });
1986
+ // Adaptive model routing — fold this successful turn into the
1987
+ // reward scoreboard so the learned policy improves next run.
1988
+ if (poolTurn !== undefined) {
1989
+ const costUsd = poolCostUsd(respProviderS, respModelIdS, usage);
1990
+ recordPoolOutcome(poolTurn, {
1991
+ success: true,
1992
+ latencyMs: performance.now() - t0Model,
1993
+ ...(costUsd !== undefined ? { costUsd } : {}),
1994
+ });
1995
+ }
1796
1996
  await fireHook("post-model", {
1797
1997
  streaming: true,
1798
1998
  contentBlocks: finalContent.length,
@@ -1844,8 +2044,12 @@ export async function runChatLoop(opts) {
1844
2044
  // Item 22 — lastServed() is exact: the candidate that actually
1845
2045
  // streamed this response. Cost-tracker prices off this pair.
1846
2046
  const servedCandidate = failoverChain?.lastServed();
1847
- const respWireModelId = servedCandidate?.modelId ?? wireModelId;
1848
- const respSpecModel = servedCandidate?.modelString ?? degradedSpecModel ?? opts.model;
2047
+ // With a pool, the served identity is the chosen candidate.
2048
+ const respWireModelId = poolTurn !== undefined ? reqWireModelId : (servedCandidate?.modelId ?? wireModelId);
2049
+ const respSpecModel = poolTurn !== undefined
2050
+ ? poolTurn.modelString
2051
+ : (servedCandidate?.modelString ?? degradedSpecModel ?? opts.model);
2052
+ const respProvider = poolTurn !== undefined ? reqProviderId : (servedCandidate?.providerId ?? providerId);
1849
2053
  bus.publish({
1850
2054
  ...bus.envelope(),
1851
2055
  spanId: modelStartEnv.spanId,
@@ -1854,11 +2058,21 @@ export async function runChatLoop(opts) {
1854
2058
  // pricing on this field (see the model_request publish above).
1855
2059
  model: respWireModelId,
1856
2060
  ...(respSpecModel !== respWireModelId ? { specModel: respSpecModel } : {}),
1857
- provider: servedCandidate?.providerId ?? providerId,
2061
+ provider: respProvider,
1858
2062
  stopReason: final.stopReason,
1859
2063
  usage: final.usage,
1860
2064
  durationMs: performance.now() - t0Model,
1861
2065
  });
2066
+ // Adaptive model routing — fold this successful turn into the
2067
+ // reward scoreboard so the learned policy improves next run.
2068
+ if (poolTurn !== undefined) {
2069
+ const costUsd = poolCostUsd(respProvider, respWireModelId, final.usage);
2070
+ recordPoolOutcome(poolTurn, {
2071
+ success: true,
2072
+ latencyMs: performance.now() - t0Model,
2073
+ ...(costUsd !== undefined ? { costUsd } : {}),
2074
+ });
2075
+ }
1862
2076
  // Persist the assistant turn. On a clean stop the FULL content-block
1863
2077
  // array is committed so subsequent `tool_result` references resolve.
1864
2078
  //
@@ -1936,6 +2150,20 @@ export async function runChatLoop(opts) {
1936
2150
  if (tierRouter !== undefined && servedFastTier) {
1937
2151
  escalateTier = true;
1938
2152
  }
2153
+ // Adaptive model routing — record the failed candidate as a
2154
+ // negative reward (drops the cost term: no usage on a throw) and
2155
+ // latch the escalation so the recovery retry re-runs on the
2156
+ // strongest candidate, unless the failed candidate WAS already the
2157
+ // strongest (then escalation buys nothing).
2158
+ if (poolRouter !== undefined && poolTurn !== undefined) {
2159
+ recordPoolOutcome(poolTurn, {
2160
+ success: false,
2161
+ latencyMs: performance.now() - modelCallStartMs,
2162
+ });
2163
+ if (poolTurn.modelString !== poolRouter.escalation().modelString) {
2164
+ escalatePool = true;
2165
+ }
2166
+ }
1939
2167
  const errObj = err;
1940
2168
  const errorName = typeof errObj.name === "string" ? errObj.name : "Error";
1941
2169
  const errorMessage = typeof errObj.message === "string" ? errObj.message : String(err);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crewhaus/runtime-core",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "description": "Minimal runtime imported by generated harness bundles",
6
6
  "main": "dist/index.js",
@@ -16,47 +16,48 @@
16
16
  },
17
17
  "dependencies": {
18
18
  "@anthropic-ai/sdk": "^0.96.0",
19
- "@crewhaus/abort-controller": "0.2.0",
20
- "@crewhaus/adapter-anthropic": "0.2.0",
21
- "@crewhaus/agent-context-isolation": "0.2.0",
22
- "@crewhaus/boundary-classifier": "0.2.0",
23
- "@crewhaus/circuit-breaker": "0.2.0",
24
- "@crewhaus/compaction-autocompact": "0.2.0",
25
- "@crewhaus/compaction-snip": "0.2.0",
26
- "@crewhaus/cost-tracker": "0.2.0",
27
- "@crewhaus/egress-classifier": "0.2.0",
28
- "@crewhaus/errors": "0.2.0",
29
- "@crewhaus/event-log": "0.2.0",
30
- "@crewhaus/hooks-engine": "0.2.0",
31
- "@crewhaus/model-router": "0.2.0",
32
- "@crewhaus/permission-engine": "0.2.0",
33
- "@crewhaus/prompt-cache-manager": "0.2.0",
34
- "@crewhaus/prompt-injection-detector": "0.2.0",
35
- "@crewhaus/rate-limiter": "0.2.0",
36
- "@crewhaus/recovery-engine": "0.2.0",
37
- "@crewhaus/run-context": "0.2.0",
38
- "@crewhaus/session-store": "0.2.0",
39
- "@crewhaus/skills-registry": "0.2.0",
40
- "@crewhaus/slash-commands": "0.2.0",
41
- "@crewhaus/state-store": "0.2.0",
42
- "@crewhaus/tenancy": "0.2.0",
43
- "@crewhaus/token-budget": "0.2.0",
44
- "@crewhaus/tool-catalog": "0.2.0",
45
- "@crewhaus/tool-executor": "0.2.0",
46
- "@crewhaus/tool-loop-detection": "0.2.0",
47
- "@crewhaus/tool-orchestrator": "0.2.0",
48
- "@crewhaus/tool-result-store": "0.2.0",
49
- "@crewhaus/streaming-tool-executor": "0.2.0",
50
- "@crewhaus/turn-state-machine": "0.2.0",
51
- "@crewhaus/trace-event-bus": "0.2.0",
52
- "@crewhaus/structured-event-printer": "0.2.0",
53
- "@crewhaus/metrics-collector": "0.2.0",
54
- "@crewhaus/otel-exporter": "0.2.0",
19
+ "@crewhaus/abort-controller": "0.2.1",
20
+ "@crewhaus/adapter-anthropic": "0.2.1",
21
+ "@crewhaus/agent-context-isolation": "0.2.1",
22
+ "@crewhaus/boundary-classifier": "0.2.1",
23
+ "@crewhaus/circuit-breaker": "0.2.1",
24
+ "@crewhaus/compaction-autocompact": "0.2.1",
25
+ "@crewhaus/compaction-snip": "0.2.1",
26
+ "@crewhaus/cost-tracker": "0.2.1",
27
+ "@crewhaus/egress-classifier": "0.2.1",
28
+ "@crewhaus/errors": "0.2.1",
29
+ "@crewhaus/event-log": "0.2.1",
30
+ "@crewhaus/hooks-engine": "0.2.1",
31
+ "@crewhaus/model-router": "0.2.1",
32
+ "@crewhaus/permission-engine": "0.2.1",
33
+ "@crewhaus/prompt-cache-manager": "0.2.1",
34
+ "@crewhaus/prompt-injection-detector": "0.2.1",
35
+ "@crewhaus/rate-limiter": "0.2.1",
36
+ "@crewhaus/recovery-engine": "0.2.1",
37
+ "@crewhaus/routing-store": "0.2.1",
38
+ "@crewhaus/run-context": "0.2.1",
39
+ "@crewhaus/session-store": "0.2.1",
40
+ "@crewhaus/skills-registry": "0.2.1",
41
+ "@crewhaus/slash-commands": "0.2.1",
42
+ "@crewhaus/state-store": "0.2.1",
43
+ "@crewhaus/tenancy": "0.2.1",
44
+ "@crewhaus/token-budget": "0.2.1",
45
+ "@crewhaus/tool-catalog": "0.2.1",
46
+ "@crewhaus/tool-executor": "0.2.1",
47
+ "@crewhaus/tool-loop-detection": "0.2.1",
48
+ "@crewhaus/tool-orchestrator": "0.2.1",
49
+ "@crewhaus/tool-result-store": "0.2.1",
50
+ "@crewhaus/streaming-tool-executor": "0.2.1",
51
+ "@crewhaus/turn-state-machine": "0.2.1",
52
+ "@crewhaus/trace-event-bus": "0.2.1",
53
+ "@crewhaus/structured-event-printer": "0.2.1",
54
+ "@crewhaus/metrics-collector": "0.2.1",
55
+ "@crewhaus/otel-exporter": "0.2.1",
55
56
  "zod-to-json-schema": "^3.23.5"
56
57
  },
57
58
  "devDependencies": {
58
- "@crewhaus/logging": "0.2.0",
59
- "@crewhaus/tool-builder": "0.2.0",
59
+ "@crewhaus/logging": "0.2.1",
60
+ "@crewhaus/tool-builder": "0.2.1",
60
61
  "zod": "^3.23.8"
61
62
  },
62
63
  "license": "Apache-2.0",