Skip to content

Commit 1f84425

Browse files
committed
fix(routing): enforce hard cost limits on unknown estimates
1 parent 2468502 commit 1f84425

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

‎src/routing/evaluator.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,14 +304,22 @@ export function evaluatePolicyProfile(
304304
const excludedByUnknown = unknown && profile.unknownEvidence.capability === "exclude";
305305
const costLimit = profile.limits.maxEstimatedCostUsd;
306306
const estimatedCost = evidence.cost?.estimatedUsd;
307+
// A hard ceiling requires evidence that the request fits below it. Routing
308+
// with an unknown estimate would otherwise turn the ceiling into the
309+
// profile's (permissive by default) unknown-cost scoring preference.
310+
const unknownCostAtLimit = costLimit !== undefined
311+
&& (typeof estimatedCost !== "number" || !Number.isFinite(estimatedCost));
307312
const overCostLimit = costLimit !== undefined
308313
&& typeof estimatedCost === "number"
309314
&& Number.isFinite(estimatedCost)
310315
&& estimatedCost > costLimit;
316+
if (unknownCostAtLimit) {
317+
exclusions.push({ code: "unknown-price", detail: "maxEstimatedCostUsd" });
318+
}
311319
if (overCostLimit) {
312320
exclusions.push({ code: "cost-limit", detail: "maxEstimatedCostUsd" });
313321
}
314-
let eligible = !unsatisfied && !excludedByUnknown && !overCostLimit;
322+
let eligible = !unsatisfied && !excludedByUnknown && !unknownCostAtLimit && !overCostLimit;
315323

316324
// Health scoring (RI-06): live hard cooldown is authoritative and
317325
// excludes; unknown health follows the profile's unknownEvidence policy;
@@ -349,7 +357,7 @@ export function evaluatePolicyProfile(
349357
// above; unknown cost follows the profile's unknownEvidence policy.
350358
const cost = evidence.cost;
351359
let costValue = cost ? costScore(cost) : null;
352-
if (costValue === null && profile.unknownEvidence.cost === "exclude") {
360+
if (costValue === null && profile.unknownEvidence.cost === "exclude" && !unknownCostAtLimit) {
353361
exclusions.push({ code: "unknown-price" });
354362
eligible = false;
355363
} else if (costValue === null && profile.unknownEvidence.cost === "penalize") {

‎tests/cost-scoring.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { tmpdir } from "node:os";
44
import { join } from "node:path";
55
import { costEvidenceForCandidate, costScore } from "../src/routing/cost";
66
import { evaluatePolicyProfile, COST_UNKNOWN_PENALTY_SCORE } from "../src/routing/evaluator";
7+
import { NoEligiblePolicyCandidateError, routeModel } from "../src/router";
78
import type { OcxConfig } from "../src/types";
89

910
let testDir = "";
@@ -100,6 +101,29 @@ describe("cost-aware scoring (RI-08)", () => {
100101
expect(result.selectedIndex).toBeNull();
101102
});
102103

104+
test("runtime routing fails closed when a hard cost ceiling cannot be estimated", () => {
105+
const limited = config({
106+
routingProfiles: {
107+
cost: {
108+
candidates: [{ provider: "anthropic", model: "claude-opus-5" }],
109+
limits: { maxEstimatedCostUsd: 0.000001 },
110+
},
111+
},
112+
});
113+
114+
expect(() => routeModel(limited, "policy/cost")).toThrow(NoEligiblePolicyCandidateError);
115+
try {
116+
routeModel(limited, "policy/cost");
117+
} catch (error) {
118+
const trace = (error as NoEligiblePolicyCandidateError).trace;
119+
expect(trace.candidates[0]).toMatchObject({
120+
eligible: false,
121+
cost: { incomplete: true, limitUsd: 0.000001 },
122+
exclusions: [{ code: "unknown-price", detail: "maxEstimatedCostUsd" }],
123+
});
124+
}
125+
});
126+
103127
test("unknown cost follows the profile policy (exclude / penalize / allow)", async () => {
104128
const strict = config({
105129
routingProfiles: {

0 commit comments

Comments
 (0)