@crewhaus/runtime-core 0.2.0 → 0.2.2
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/dist/index.d.ts +67 -0
- package/dist/index.js +279 -17
- 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,55 @@ 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
|
+
readonly explorationRate?: number;
|
|
539
|
+
readonly seed?: string;
|
|
540
|
+
readonly bandit?: "epsilon-greedy" | "thompson";
|
|
541
|
+
};
|
|
542
|
+
};
|
|
543
|
+
/**
|
|
544
|
+
* Test injection for the pool candidate adapters, keyed by their spec model
|
|
545
|
+
* string (mirrors `_tierAdapters`). Production callers leave it undefined.
|
|
546
|
+
*/
|
|
547
|
+
_poolAdapters?: ReadonlyMap<string, ProviderAdapter>;
|
|
548
|
+
/**
|
|
549
|
+
* Test injection for the reward scoreboard. Production callers leave it
|
|
550
|
+
* undefined (the runtime opens a file-backed scoreboard beside the sessions
|
|
551
|
+
* dir); tests inject an in-memory or tmp-dir instance to assert learning.
|
|
552
|
+
*/
|
|
553
|
+
_scoreboard?: Scoreboard;
|
|
487
554
|
/**
|
|
488
555
|
* Section 55 (Track A) / item 23 — the spec's `failure_taxonomy`, lowered
|
|
489
556
|
* 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,90 @@ 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 !== undefined
|
|
612
|
+
? {
|
|
613
|
+
learning: {
|
|
614
|
+
...(pool.learning.minSamplesPerArm !== undefined
|
|
615
|
+
? { minSamplesPerArm: pool.learning.minSamplesPerArm }
|
|
616
|
+
: {}),
|
|
617
|
+
...(pool.learning.explorationRate !== undefined
|
|
618
|
+
? { explorationRate: pool.learning.explorationRate }
|
|
619
|
+
: {}),
|
|
620
|
+
...(pool.learning.bandit !== undefined ? { bandit: pool.learning.bandit } : {}),
|
|
621
|
+
},
|
|
622
|
+
}
|
|
623
|
+
: {}),
|
|
624
|
+
// The learned policy reads the live scoreboard; static/heuristic ignore it.
|
|
625
|
+
...(pool.policy === "learned" ? { score: (rk, m) => sb.score(rk, m) } : {}),
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
// Fold one turn's observed outcome into the scoreboard (no-op without a pool).
|
|
629
|
+
const recordPoolOutcome = (turn, obs) => {
|
|
630
|
+
// Capture into consts so the narrowing survives (they are closed-over lets).
|
|
631
|
+
const sb = scoreboard;
|
|
632
|
+
const rc = rewardConfig;
|
|
633
|
+
if (turn === undefined || sb === undefined || rc === undefined)
|
|
634
|
+
return;
|
|
635
|
+
sb.record(turn.routeKey, turn.modelString, computeReward(obs, rc), obs);
|
|
636
|
+
};
|
|
637
|
+
// Per-turn USD cost from token usage + the static pricing table. Undefined
|
|
638
|
+
// when the served model isn't on the table (the reward then drops the cost
|
|
639
|
+
// term and reweights over quality + latency).
|
|
640
|
+
const poolCostUsd = (provider, wireModelId, usage) => {
|
|
641
|
+
const row = resolvePricing(DEFAULT_PRICING, provider, wireModelId);
|
|
642
|
+
if (row === undefined)
|
|
643
|
+
return undefined;
|
|
644
|
+
return (computeCostMicros(row, usage.input, usage.output, usage.cacheRead ?? 0, usage.cacheCreate ?? 0) / 1_000_000);
|
|
645
|
+
};
|
|
513
646
|
let sessionId;
|
|
514
647
|
let resumedMessages;
|
|
515
648
|
if (opts.resume) {
|
|
@@ -539,6 +672,12 @@ export async function runChatLoop(opts) {
|
|
|
539
672
|
// Tests that pass their own runContext rely on observing turnNumber on
|
|
540
673
|
// it after the loop returns, so we never silently swap it out.
|
|
541
674
|
const runContext = opts.runContext ?? createRunContext({ sessionId });
|
|
675
|
+
// Adaptive model routing — ε-greedy exploration seed. A spec-fixed
|
|
676
|
+
// `learning.seed` reproduces exploration across runs (tests); otherwise the
|
|
677
|
+
// sessionId seeds it, so each run explores differently yet still replays
|
|
678
|
+
// exactly from its own transcript. Constant per run; the per-turn draw mixes
|
|
679
|
+
// in turnIndex + band (see PolicyRouter.route).
|
|
680
|
+
const poolSeed = opts.modelPool?.learning?.seed ?? sessionId;
|
|
542
681
|
// Section 15 — every observability subscriber (otel, metrics, printer)
|
|
543
682
|
// hangs off this single bus. The default constructor in `createRunContext`
|
|
544
683
|
// mints a no-subscriber bus; here we attach the env-gated set so a fresh
|
|
@@ -1510,10 +1649,15 @@ export async function runChatLoop(opts) {
|
|
|
1510
1649
|
* they line up with the assistant turn's tool_use blocks.
|
|
1511
1650
|
*/
|
|
1512
1651
|
async function runToolBatch(toolUses) {
|
|
1513
|
-
|
|
1652
|
+
// Pass the full catalog so per-call classifiers (Task) can resolve a
|
|
1653
|
+
// dispatch's child tool set; without it, classifier-based tools stay
|
|
1654
|
+
// serial (fail-closed).
|
|
1655
|
+
const partition = partitionToolCalls(toolUses, (n) => toolByName.get(n), tools);
|
|
1656
|
+
const maxConcurrentTools = opts.maxConcurrentTools ?? DEFAULT_MAX_CONCURRENT_TOOLS;
|
|
1514
1657
|
runContext.logger.debug("tool partition", {
|
|
1515
1658
|
concurrent: partition.concurrent.map((b) => b.length),
|
|
1516
1659
|
serial: partition.serial.length,
|
|
1660
|
+
maxConcurrentTools,
|
|
1517
1661
|
});
|
|
1518
1662
|
// Map each tool_use's identity to its slot in the original order so
|
|
1519
1663
|
// results can be placed back in order regardless of the
|
|
@@ -1525,7 +1669,7 @@ export async function runChatLoop(opts) {
|
|
|
1525
1669
|
toolUses.forEach((tu, idx) => indexByBlock.set(tu, idx));
|
|
1526
1670
|
const results = new Array(toolUses.length);
|
|
1527
1671
|
for (const batch of partition.concurrent) {
|
|
1528
|
-
const settled = await
|
|
1672
|
+
const settled = await mapWithConcurrency(batch, maxConcurrentTools, (tu) => executeOneToolUse(tu));
|
|
1529
1673
|
batch.forEach((tu, i) => {
|
|
1530
1674
|
// biome-ignore lint/style/noNonNullAssertion: every block came from toolUses, so its index is registered.
|
|
1531
1675
|
results[indexByBlock.get(tu)] = settled[i];
|
|
@@ -1580,6 +1724,10 @@ export async function runChatLoop(opts) {
|
|
|
1580
1724
|
let thisTurnToolUseCount = 0;
|
|
1581
1725
|
let escalateTier = false;
|
|
1582
1726
|
let servedFastTier = false;
|
|
1727
|
+
// Adaptive model routing — once a pool candidate fails, stick with the
|
|
1728
|
+
// strongest candidate for the rest of the run (misroute recovery, mirroring
|
|
1729
|
+
// the tier router's fast→default escalation latch).
|
|
1730
|
+
let escalatePool = false;
|
|
1583
1731
|
while (state.kind !== "Done") {
|
|
1584
1732
|
if (turnAbort.signal.aborted) {
|
|
1585
1733
|
state = transition(state, { kind: "Aborted" });
|
|
@@ -1617,6 +1765,10 @@ export async function runChatLoop(opts) {
|
|
|
1617
1765
|
// gap before the model responds now shows live progress. Cleared in
|
|
1618
1766
|
// the catch below on any model-call error.
|
|
1619
1767
|
out.spinner.start("thinking", { prefix: "agent> " });
|
|
1768
|
+
// Adaptive model routing — declared OUTSIDE the try so the catch can
|
|
1769
|
+
// read the chosen candidate + start time to record a failure reward.
|
|
1770
|
+
let poolTurn;
|
|
1771
|
+
let modelCallStartMs = performance.now();
|
|
1620
1772
|
try {
|
|
1621
1773
|
// Section 27 — rate-limit the model call before opening the
|
|
1622
1774
|
// stream. The keys are caller-configured (typically
|
|
@@ -1645,13 +1797,73 @@ export async function runChatLoop(opts) {
|
|
|
1645
1797
|
// The adapter this turn streams through: the primary/chain by
|
|
1646
1798
|
// default, or a tier adapter when the two-tier router is active.
|
|
1647
1799
|
let turnAdapter = adapter;
|
|
1648
|
-
//
|
|
1649
|
-
//
|
|
1650
|
-
//
|
|
1651
|
-
//
|
|
1652
|
-
//
|
|
1653
|
-
// chosen
|
|
1654
|
-
|
|
1800
|
+
// Adaptive model routing — the PolicyRouter picks a pool candidate
|
|
1801
|
+
// this turn (static / heuristic / learned) from the SAME
|
|
1802
|
+
// deterministic signals as the tier router. `escalatePool` forces
|
|
1803
|
+
// the strongest candidate after a prior pool failure (misroute
|
|
1804
|
+
// recovery). Publish a `model_route` event and stream through the
|
|
1805
|
+
// chosen candidate; the outcome is folded into the reward
|
|
1806
|
+
// scoreboard post-turn. Takes precedence over the tier router (the
|
|
1807
|
+
// two are mutually exclusive in the spec).
|
|
1808
|
+
if (poolRouter !== undefined) {
|
|
1809
|
+
const base = poolRouter.route({
|
|
1810
|
+
contextTokens: estimateTokens(messages),
|
|
1811
|
+
toolsInPlay: (anthropicTools?.length ?? 0) > 0,
|
|
1812
|
+
turnIndex: Math.max(0, runContext.turnNumber - 1),
|
|
1813
|
+
priorTurnToolUseCount,
|
|
1814
|
+
}, poolSeed,
|
|
1815
|
+
// Monotonic exploration sequence: the transcript length keeps
|
|
1816
|
+
// advancing across `--resume` (and a channel-bot's resume-per-
|
|
1817
|
+
// message), so ε-greedy draws a fresh coin per decision instead
|
|
1818
|
+
// of freezing on the reset-to-0 turnIndex. Deterministic on
|
|
1819
|
+
// replay (the same transcript reconstructs the same length).
|
|
1820
|
+
messages.length);
|
|
1821
|
+
const decision = escalatePool
|
|
1822
|
+
? {
|
|
1823
|
+
...base,
|
|
1824
|
+
candidate: poolRouter.escalation(),
|
|
1825
|
+
reason: `escalated to strongest candidate after a pool failure (was: ${base.reason})`,
|
|
1826
|
+
explored: false,
|
|
1827
|
+
}
|
|
1828
|
+
: base;
|
|
1829
|
+
turnAdapter = decision.candidate.adapter;
|
|
1830
|
+
reqWireModelId = decision.candidate.modelId;
|
|
1831
|
+
reqProviderId = decision.candidate.adapter.providerId;
|
|
1832
|
+
reqSpecModel = decision.candidate.modelString;
|
|
1833
|
+
poolTurn = {
|
|
1834
|
+
modelString: decision.candidate.modelString,
|
|
1835
|
+
routeKey: decision.routeKey,
|
|
1836
|
+
};
|
|
1837
|
+
bus.publish({
|
|
1838
|
+
...bus.envelope(),
|
|
1839
|
+
kind: "model_route",
|
|
1840
|
+
routeKey: decision.routeKey,
|
|
1841
|
+
model: decision.candidate.modelId,
|
|
1842
|
+
policy: decision.policy,
|
|
1843
|
+
reason: decision.reason,
|
|
1844
|
+
...(decision.explored ? { explored: true } : {}),
|
|
1845
|
+
...(poolPolicyVersion !== undefined ? { policyVersion: poolPolicyVersion } : {}),
|
|
1846
|
+
});
|
|
1847
|
+
// Persist the decision so `crewhaus route explain <session>` can
|
|
1848
|
+
// replay per-turn routing after the fact. Non-conversational, so
|
|
1849
|
+
// `replayMessageHistory` ignores it and `--resume` is unaffected.
|
|
1850
|
+
await logEvent("model_route", {
|
|
1851
|
+
turnNumber: runContext.turnNumber,
|
|
1852
|
+
routeKey: decision.routeKey,
|
|
1853
|
+
model: decision.candidate.modelId,
|
|
1854
|
+
policy: decision.policy,
|
|
1855
|
+
reason: decision.reason,
|
|
1856
|
+
explored: decision.explored,
|
|
1857
|
+
...(poolPolicyVersion !== undefined ? { policyVersion: poolPolicyVersion } : {}),
|
|
1858
|
+
});
|
|
1859
|
+
}
|
|
1860
|
+
else if (tierRouter !== undefined) {
|
|
1861
|
+
// Item 26 — two-tier turn-difficulty router. Pick a tier from
|
|
1862
|
+
// DETERMINISTIC per-turn signals (context size, tools-in-play,
|
|
1863
|
+
// turn index, prior-turn tool_use density); a fast-tier misroute
|
|
1864
|
+
// recovery forces `default` (see the catch below). Publish the
|
|
1865
|
+
// decision as a `model_tier_route` event, then stream through the
|
|
1866
|
+
// chosen tier's already-resolved adapter.
|
|
1655
1867
|
const decision = escalateTier
|
|
1656
1868
|
? { tier: "default", reason: "escalated after fast-tier failure" }
|
|
1657
1869
|
: tierRouter.route({
|
|
@@ -1689,6 +1901,7 @@ export async function runChatLoop(opts) {
|
|
|
1689
1901
|
streaming: opts.streaming === true,
|
|
1690
1902
|
});
|
|
1691
1903
|
const t0Model = performance.now();
|
|
1904
|
+
modelCallStartMs = t0Model;
|
|
1692
1905
|
let streamChunkIndex = 0;
|
|
1693
1906
|
// Section 17 — `adapter.stream(req)` returns
|
|
1694
1907
|
// `AsyncIterable<StreamEvent>`; we accumulate via
|
|
@@ -1743,6 +1956,10 @@ export async function runChatLoop(opts) {
|
|
|
1743
1956
|
toolByName,
|
|
1744
1957
|
abortSignal: turnAbort.signal,
|
|
1745
1958
|
onTextDelta,
|
|
1959
|
+
// Honor the same concurrency bound as the non-streaming
|
|
1960
|
+
// path — otherwise `maxConcurrentTools` would be silently
|
|
1961
|
+
// ignored under `streaming: true`.
|
|
1962
|
+
maxConcurrent: opts.maxConcurrentTools ?? DEFAULT_MAX_CONCURRENT_TOOLS,
|
|
1746
1963
|
runTool: (block) => executeOneToolUse({
|
|
1747
1964
|
id: block.id,
|
|
1748
1965
|
name: block.name,
|
|
@@ -1778,8 +1995,15 @@ export async function runChatLoop(opts) {
|
|
|
1778
1995
|
// Item 22 — lastServed() is exact: the candidate that actually
|
|
1779
1996
|
// streamed this response. Cost-tracker prices off this pair.
|
|
1780
1997
|
const servedStreaming = failoverChain?.lastServed();
|
|
1781
|
-
|
|
1782
|
-
|
|
1998
|
+
// With a pool, the served identity is the chosen candidate (no
|
|
1999
|
+
// failover chain when pooling), so cost-tracker prices on it.
|
|
2000
|
+
const respModelIdS = poolTurn !== undefined ? reqWireModelId : (servedStreaming?.modelId ?? wireModelId);
|
|
2001
|
+
const respSpecModelS = poolTurn !== undefined
|
|
2002
|
+
? poolTurn.modelString
|
|
2003
|
+
: (servedStreaming?.modelString ?? degradedSpecModel ?? opts.model);
|
|
2004
|
+
const respProviderS = poolTurn !== undefined
|
|
2005
|
+
? reqProviderId
|
|
2006
|
+
: (servedStreaming?.providerId ?? providerId);
|
|
1783
2007
|
bus.publish({
|
|
1784
2008
|
...bus.envelope(),
|
|
1785
2009
|
spanId: modelStartEnv.spanId,
|
|
@@ -1788,11 +2012,21 @@ export async function runChatLoop(opts) {
|
|
|
1788
2012
|
// pricing on this field (see the model_request publish above).
|
|
1789
2013
|
model: respModelIdS,
|
|
1790
2014
|
...(respSpecModelS !== respModelIdS ? { specModel: respSpecModelS } : {}),
|
|
1791
|
-
provider:
|
|
2015
|
+
provider: respProviderS,
|
|
1792
2016
|
stopReason: stopReason ?? "end_turn",
|
|
1793
2017
|
usage,
|
|
1794
2018
|
durationMs: performance.now() - t0Model,
|
|
1795
2019
|
});
|
|
2020
|
+
// Adaptive model routing — fold this successful turn into the
|
|
2021
|
+
// reward scoreboard so the learned policy improves next run.
|
|
2022
|
+
if (poolTurn !== undefined) {
|
|
2023
|
+
const costUsd = poolCostUsd(respProviderS, respModelIdS, usage);
|
|
2024
|
+
recordPoolOutcome(poolTurn, {
|
|
2025
|
+
success: true,
|
|
2026
|
+
latencyMs: performance.now() - t0Model,
|
|
2027
|
+
...(costUsd !== undefined ? { costUsd } : {}),
|
|
2028
|
+
});
|
|
2029
|
+
}
|
|
1796
2030
|
await fireHook("post-model", {
|
|
1797
2031
|
streaming: true,
|
|
1798
2032
|
contentBlocks: finalContent.length,
|
|
@@ -1844,8 +2078,12 @@ export async function runChatLoop(opts) {
|
|
|
1844
2078
|
// Item 22 — lastServed() is exact: the candidate that actually
|
|
1845
2079
|
// streamed this response. Cost-tracker prices off this pair.
|
|
1846
2080
|
const servedCandidate = failoverChain?.lastServed();
|
|
1847
|
-
|
|
1848
|
-
const
|
|
2081
|
+
// With a pool, the served identity is the chosen candidate.
|
|
2082
|
+
const respWireModelId = poolTurn !== undefined ? reqWireModelId : (servedCandidate?.modelId ?? wireModelId);
|
|
2083
|
+
const respSpecModel = poolTurn !== undefined
|
|
2084
|
+
? poolTurn.modelString
|
|
2085
|
+
: (servedCandidate?.modelString ?? degradedSpecModel ?? opts.model);
|
|
2086
|
+
const respProvider = poolTurn !== undefined ? reqProviderId : (servedCandidate?.providerId ?? providerId);
|
|
1849
2087
|
bus.publish({
|
|
1850
2088
|
...bus.envelope(),
|
|
1851
2089
|
spanId: modelStartEnv.spanId,
|
|
@@ -1854,11 +2092,21 @@ export async function runChatLoop(opts) {
|
|
|
1854
2092
|
// pricing on this field (see the model_request publish above).
|
|
1855
2093
|
model: respWireModelId,
|
|
1856
2094
|
...(respSpecModel !== respWireModelId ? { specModel: respSpecModel } : {}),
|
|
1857
|
-
provider:
|
|
2095
|
+
provider: respProvider,
|
|
1858
2096
|
stopReason: final.stopReason,
|
|
1859
2097
|
usage: final.usage,
|
|
1860
2098
|
durationMs: performance.now() - t0Model,
|
|
1861
2099
|
});
|
|
2100
|
+
// Adaptive model routing — fold this successful turn into the
|
|
2101
|
+
// reward scoreboard so the learned policy improves next run.
|
|
2102
|
+
if (poolTurn !== undefined) {
|
|
2103
|
+
const costUsd = poolCostUsd(respProvider, respWireModelId, final.usage);
|
|
2104
|
+
recordPoolOutcome(poolTurn, {
|
|
2105
|
+
success: true,
|
|
2106
|
+
latencyMs: performance.now() - t0Model,
|
|
2107
|
+
...(costUsd !== undefined ? { costUsd } : {}),
|
|
2108
|
+
});
|
|
2109
|
+
}
|
|
1862
2110
|
// Persist the assistant turn. On a clean stop the FULL content-block
|
|
1863
2111
|
// array is committed so subsequent `tool_result` references resolve.
|
|
1864
2112
|
//
|
|
@@ -1936,6 +2184,20 @@ export async function runChatLoop(opts) {
|
|
|
1936
2184
|
if (tierRouter !== undefined && servedFastTier) {
|
|
1937
2185
|
escalateTier = true;
|
|
1938
2186
|
}
|
|
2187
|
+
// Adaptive model routing — record the failed candidate as a
|
|
2188
|
+
// negative reward (drops the cost term: no usage on a throw) and
|
|
2189
|
+
// latch the escalation so the recovery retry re-runs on the
|
|
2190
|
+
// strongest candidate, unless the failed candidate WAS already the
|
|
2191
|
+
// strongest (then escalation buys nothing).
|
|
2192
|
+
if (poolRouter !== undefined && poolTurn !== undefined) {
|
|
2193
|
+
recordPoolOutcome(poolTurn, {
|
|
2194
|
+
success: false,
|
|
2195
|
+
latencyMs: performance.now() - modelCallStartMs,
|
|
2196
|
+
});
|
|
2197
|
+
if (poolTurn.modelString !== poolRouter.escalation().modelString) {
|
|
2198
|
+
escalatePool = true;
|
|
2199
|
+
}
|
|
2200
|
+
}
|
|
1939
2201
|
const errObj = err;
|
|
1940
2202
|
const errorName = typeof errObj.name === "string" ? errObj.name : "Error";
|
|
1941
2203
|
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.
|
|
3
|
+
"version": "0.2.2",
|
|
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.
|
|
20
|
-
"@crewhaus/adapter-anthropic": "0.2.
|
|
21
|
-
"@crewhaus/agent-context-isolation": "0.2.
|
|
22
|
-
"@crewhaus/boundary-classifier": "0.2.
|
|
23
|
-
"@crewhaus/circuit-breaker": "0.2.
|
|
24
|
-
"@crewhaus/compaction-autocompact": "0.2.
|
|
25
|
-
"@crewhaus/compaction-snip": "0.2.
|
|
26
|
-
"@crewhaus/cost-tracker": "0.2.
|
|
27
|
-
"@crewhaus/egress-classifier": "0.2.
|
|
28
|
-
"@crewhaus/errors": "0.2.
|
|
29
|
-
"@crewhaus/event-log": "0.2.
|
|
30
|
-
"@crewhaus/hooks-engine": "0.2.
|
|
31
|
-
"@crewhaus/model-router": "0.2.
|
|
32
|
-
"@crewhaus/permission-engine": "0.2.
|
|
33
|
-
"@crewhaus/prompt-cache-manager": "0.2.
|
|
34
|
-
"@crewhaus/prompt-injection-detector": "0.2.
|
|
35
|
-
"@crewhaus/rate-limiter": "0.2.
|
|
36
|
-
"@crewhaus/recovery-engine": "0.2.
|
|
37
|
-
"@crewhaus/
|
|
38
|
-
"@crewhaus/
|
|
39
|
-
"@crewhaus/
|
|
40
|
-
"@crewhaus/
|
|
41
|
-
"@crewhaus/
|
|
42
|
-
"@crewhaus/
|
|
43
|
-
"@crewhaus/
|
|
44
|
-
"@crewhaus/
|
|
45
|
-
"@crewhaus/tool-
|
|
46
|
-
"@crewhaus/tool-
|
|
47
|
-
"@crewhaus/tool-
|
|
48
|
-
"@crewhaus/tool-
|
|
49
|
-
"@crewhaus/
|
|
50
|
-
"@crewhaus/
|
|
51
|
-
"@crewhaus/
|
|
52
|
-
"@crewhaus/
|
|
53
|
-
"@crewhaus/
|
|
54
|
-
"@crewhaus/
|
|
19
|
+
"@crewhaus/abort-controller": "0.2.2",
|
|
20
|
+
"@crewhaus/adapter-anthropic": "0.2.2",
|
|
21
|
+
"@crewhaus/agent-context-isolation": "0.2.2",
|
|
22
|
+
"@crewhaus/boundary-classifier": "0.2.2",
|
|
23
|
+
"@crewhaus/circuit-breaker": "0.2.2",
|
|
24
|
+
"@crewhaus/compaction-autocompact": "0.2.2",
|
|
25
|
+
"@crewhaus/compaction-snip": "0.2.2",
|
|
26
|
+
"@crewhaus/cost-tracker": "0.2.2",
|
|
27
|
+
"@crewhaus/egress-classifier": "0.2.2",
|
|
28
|
+
"@crewhaus/errors": "0.2.2",
|
|
29
|
+
"@crewhaus/event-log": "0.2.2",
|
|
30
|
+
"@crewhaus/hooks-engine": "0.2.2",
|
|
31
|
+
"@crewhaus/model-router": "0.2.2",
|
|
32
|
+
"@crewhaus/permission-engine": "0.2.2",
|
|
33
|
+
"@crewhaus/prompt-cache-manager": "0.2.2",
|
|
34
|
+
"@crewhaus/prompt-injection-detector": "0.2.2",
|
|
35
|
+
"@crewhaus/rate-limiter": "0.2.2",
|
|
36
|
+
"@crewhaus/recovery-engine": "0.2.2",
|
|
37
|
+
"@crewhaus/routing-store": "0.2.2",
|
|
38
|
+
"@crewhaus/run-context": "0.2.2",
|
|
39
|
+
"@crewhaus/session-store": "0.2.2",
|
|
40
|
+
"@crewhaus/skills-registry": "0.2.2",
|
|
41
|
+
"@crewhaus/slash-commands": "0.2.2",
|
|
42
|
+
"@crewhaus/state-store": "0.2.2",
|
|
43
|
+
"@crewhaus/tenancy": "0.2.2",
|
|
44
|
+
"@crewhaus/token-budget": "0.2.2",
|
|
45
|
+
"@crewhaus/tool-catalog": "0.2.2",
|
|
46
|
+
"@crewhaus/tool-executor": "0.2.2",
|
|
47
|
+
"@crewhaus/tool-loop-detection": "0.2.2",
|
|
48
|
+
"@crewhaus/tool-orchestrator": "0.2.2",
|
|
49
|
+
"@crewhaus/tool-result-store": "0.2.2",
|
|
50
|
+
"@crewhaus/streaming-tool-executor": "0.2.2",
|
|
51
|
+
"@crewhaus/turn-state-machine": "0.2.2",
|
|
52
|
+
"@crewhaus/trace-event-bus": "0.2.2",
|
|
53
|
+
"@crewhaus/structured-event-printer": "0.2.2",
|
|
54
|
+
"@crewhaus/metrics-collector": "0.2.2",
|
|
55
|
+
"@crewhaus/otel-exporter": "0.2.2",
|
|
55
56
|
"zod-to-json-schema": "^3.23.5"
|
|
56
57
|
},
|
|
57
58
|
"devDependencies": {
|
|
58
|
-
"@crewhaus/logging": "0.2.
|
|
59
|
-
"@crewhaus/tool-builder": "0.2.
|
|
59
|
+
"@crewhaus/logging": "0.2.2",
|
|
60
|
+
"@crewhaus/tool-builder": "0.2.2",
|
|
60
61
|
"zod": "^3.23.8"
|
|
61
62
|
},
|
|
62
63
|
"license": "Apache-2.0",
|