Skip to content

Commit 3ef6759

Browse files
rabeckettCopilot
andauthored
Keep credits and model_credits paired on fully priced runs in routing history (#80784)
Emit `model_credits: null` in `runCost` whenever total `credits` is `null` so partial bills never record a standalone main-model share while still preserving main-conversation token counts. In `averages()`, fold `model_credits` only on entries with a non-null total `credits` (falling back to total `credits` when `model_credits` is `null`), keeping both EMAs over the same sample set and guaranteeing `0 <= model_credits <= credits` even for legacy partial-bill history entries. Fixes #80782 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 3232d35 commit 3ef6759

5 files changed

Lines changed: 39 additions & 11 deletions

File tree

‎actions/setup/js/fixtures/routing-history/aw2-reducer.json‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎actions/setup/js/push_routing_history.cjs‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ function requestTokens(used) {
179179
/**
180180
* The run's cost from AWF's records (contract 4): `credits` over every agent request except the
181181
* router's classifier call, and `model_credits` and token counts over the main conversation's
182-
* requests. Each is null when a request it covers lacks a price or a count.
182+
* requests. `credits` is null when any covered request lacks a price, `model_credits` is null
183+
* whenever `credits` is null or there are no main requests, and `tokens` is null when a main
184+
* request lacks a count.
183185
* @param {Record<string, any>[]} usage firewall.token_usage data
184186
* @param {Record<string, any>[]} routing firewall.model_routing data with raw request fields
185187
* @param {unknown} subAgentModels
@@ -222,9 +224,10 @@ function runCost(usage, routing, subAgentModels) {
222224
reasoning = reasoning === null || reasoned === null ? null : reasoning + reasoned;
223225
}
224226
const round = (/** @type {number | null} */ value) => (value === null ? null : Number(value.toFixed(6)));
227+
const totalCredits = requests > 0 ? round(credits) : null;
225228
return {
226-
credits: requests > 0 ? round(credits) : null,
227-
model_credits: main > 0 ? round(modelCredits) : null,
229+
credits: totalCredits,
230+
model_credits: totalCredits !== null && main > 0 ? round(modelCredits) : null,
228231
tokens: main > 0 && tokens !== null ? [...tokens, reasoning] : null,
229232
};
230233
}

‎actions/setup/js/push_routing_history.test.cjs‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const req = require;
99
const fixtures = req("./fixtures/routing_analytics_runs.cjs");
1010
const { readJsonlFile } = req("./jsonl_helpers.cjs");
1111
const { applyRun, emptyState, trimRuns } = req("./routing_history.cjs");
12-
const { ineligibility, main, readConfig, sessionEvidence } = req("./push_routing_history.cjs");
12+
const { ineligibility, main, readConfig, runCost, sessionEvidence } = req("./push_routing_history.cjs");
1313

1414
const BRANCH = "memory/adaptive-routing/triage";
1515
const GENERATION = `${"a".repeat(64)}:${"b".repeat(64)}`;
@@ -235,6 +235,23 @@ describe("push_routing_history evidence", () => {
235235
];
236236
expect(sessionEvidence(events)).toEqual({ observation: null, pair: "anthropic/model-b@none", cost: { credits: null, model_credits: null, tokens: null } });
237237
});
238+
239+
it("emits null model_credits when an unpriced sub-agent makes total credits null, while preserving main tokens", () => {
240+
const routing = [
241+
{ stage: "selection", selectedProvider: "copilot", wireModel: "model-a", selectedEffort: "high" },
242+
{ stage: "request", request_id: "r-main", routed: "as_selected", requested_model: "model-a" },
243+
{ stage: "request", request_id: "r-sub", routed: "deviated", requested_model: "sub-model" },
244+
];
245+
const usage = [
246+
{ requestId: "r-main", provider: "copilot", aic: 30, usage: { inputTokens: 1000, cacheReadInputTokens: 400, cacheCreationInputTokens: 0, outputTokens: 200 } },
247+
{ requestId: "r-sub", provider: "copilot", usage: { inputTokens: 500, cacheReadInputTokens: 0, cacheCreationInputTokens: 0, outputTokens: 100 } },
248+
];
249+
expect(runCost(usage, routing, ["sub-model"])).toEqual({
250+
credits: null,
251+
model_credits: null,
252+
tokens: [600, 400, 0, 200, null],
253+
});
254+
});
238255
});
239256

240257
describe("push_routing_history configuration", () => {

‎actions/setup/js/routing_history.cjs‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ function rate(wins, losses, hopeful) {
9191

9292
/**
9393
* A pair's average cost over its entries, oldest first: the first known cost sets it and each
94-
* later one moves it by 0.9 x old + 0.1 x new. An entry with a cost but no model share counts
95-
* all of it as the model's.
94+
* later one moves it by 0.9 x old + 0.1 x new. `model_credits` is averaged over the exact same
95+
* entries that have a known total cost, falling back to total cost when the model share is null,
96+
* so `0 <= model_credits <= credits` holds even for legacy partial-bill entries.
9697
* @param {HistoryEntry[]} entries
9798
*/
9899
function averages(entries) {
@@ -103,13 +104,12 @@ function averages(entries) {
103104
let bills = 0;
104105
for (const entry of entries) {
105106
const cost = entry[3];
106-
let model = entry[4];
107107
if (cost !== null) {
108108
credits = credits === null ? cost : 0.9 * credits + 0.1 * cost;
109109
bills += 1;
110-
if (model === null) model = cost;
110+
const model = entry[4] ?? cost;
111+
modelCredits = modelCredits === null ? model : 0.9 * modelCredits + 0.1 * model;
111112
}
112-
if (model !== null) modelCredits = modelCredits === null ? model : 0.9 * modelCredits + 0.1 * model;
113113
}
114114
return { credits, modelCredits, bills };
115115
}
@@ -438,6 +438,7 @@ module.exports = {
438438
BOUNDS,
439439
NOTES,
440440
applyRun,
441+
averages,
441442
counts,
442443
emptyState,
443444
memory,

‎actions/setup/js/routing_history.test.cjs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, expect, it } from "vitest";
22

33
const req = require;
44
const vectors = req("./fixtures/routing-history/aw2-reducer.json");
5-
const { applyRun, emptyState, trimRuns } = req("./routing_history.cjs");
5+
const { applyRun, averages, emptyState, trimRuns } = req("./routing_history.cjs");
66

77
// Numbers agree to a relative tolerance of 1e-9 (vectors.arithmetic); everything else exactly.
88
function expectSame(actual, expected, path = "state") {
@@ -69,4 +69,11 @@ describe("routing history state", () => {
6969
updated: null,
7070
});
7171
});
72+
73+
it("ignores standalone model_credits on legacy entries whose total credits are null", () => {
74+
const partial = [1, "2026-11-01T00:00:00Z", true, null, 30.0, [40000, 300000, 0, 9000, null]];
75+
const full = [2, "2026-11-02T00:00:00Z", true, 10.0, 10.0, null];
76+
expect(averages([partial])).toEqual({ credits: null, modelCredits: null, bills: 0 });
77+
expect(averages([partial, full])).toEqual({ credits: 10.0, modelCredits: 10.0, bills: 1 });
78+
});
7279
});

0 commit comments

Comments
 (0)