Skip to content

Keep credits and model_credits paired on fully priced runs in routing history - #80784

Merged
rabeckett merged 1 commit into
mainfrom
fix/pair-credits-and-model-credits
Nov 24, 2026
Merged

rabeckett merged 1 commit into
mainfrom
fix/pair-credits-and-model-credits

Conversation

@rabeckett

Copy link
Copy Markdown
Collaborator

Fixes #80782

In adaptive routing (#79102), push_routing_history.cjs (runCost) previously accumulated total credits across all non-classifier agent requests and model_credits across main-conversation requests independently. When a sub-agent request lacked aic, total credits became null while model_credits still recorded the main conversation's spend. In routing_history.cjs, averages() folded model_credits whenever entry[4] was non-null, even when entry[3] (credits) was null. Computing the two EMAs over different run subsets allowed model_credits to exceed credits, which causes AWF's challenger cost estimation (credits + model_credits * (ratio - 1)) to produce negative credit estimates.

This PR keeps credits and model_credits paired on the same fully priced runs in both the writer and the reducer, so 0 <= model_credits <= credits holds both for new runs and when re-reading legacy history entries.

Example

Suppose a pair records two runs:

  1. Run 1 spends 30 credits on the main model plus an unpriced sub-agent request, leaving a legacy history entry [1, "2026-11-01T00:00:00Z", true, null, 30.0, [40000, 300000, 0, 9000, null]].
  2. Run 2 spends 10 credits total, all on the main model: [2, "2026-11-02T00:00:00Z", true, 10.0, 10.0, null].
Field in state.json after Run 2 Before this PR With this PR
credits 10.0 10.0
model_credits 28.0 (0.9 * 30.0 + 0.1 * 10.0) 10.0
bills 1 1
AWF challenger estimate at 0.1x rate -15.2 credits 1.0 credits

For new runs like Run 1, runCost now emits credits: null and model_credits: null, while still preserving main-conversation tokens.

Changes

  • actions/setup/js/push_routing_history.cjs: runCost emits model_credits: null whenever total credits is null (while keeping main-conversation tokens intact).
  • actions/setup/js/routing_history.cjs: averages folds model_credits only on entries where total credits is non-null (falling back to total credits when model_credits is null, as before), and exports averages alongside counts, memory, and rate.
  • actions/setup/js/fixtures/routing-history/aw2-reducer.json: syncs the partial-bill-ignores-standalone-model-share oracle vector from the shared adaptive-routing oracle.
  • actions/setup/js/push_routing_history.test.cjs and actions/setup/js/routing_history.test.cjs: adds focused tests for a priced main request paired with an unpriced sub-agent request and for legacy [null, 30.0] followed by [10.0, 10.0] histories.

Verification

Validated on 8abe4b2dda:

  • npx vitest run --no-file-parallelism --testTimeout=30000 routing_history.test.cjs push_routing_history.test.cjs: 2 test files, 63 passed.
  • npm run typecheck: passed.
  • npx prettier --check: passed on all modified .cjs files (aw2-reducer.json is in .prettierignore).

… history

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>
@rabeckett
rabeckett marked this pull request as ready for review November 24, 2026 19:41
Copilot AI adaptive review requested due to automatic review settings November 24, 2026 19:41
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

✅ Test Quality Sentinel completed test quality analysis.

🧪 Test quality analysis by Test Quality Sentinel · copilot · routed: haiku55 xhigh · ~4 AIC · ⌖ ~2 AIC · ⊞ 7.4K · history

@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

✅ PR Code Quality Reviewer completed the code quality review.

PR Code Quality Reviewer

Outcome: reported_action

The agent completed and reported one or more actions. The no-op output is a completion signal, not a claim that no action was taken.

The [aw] No-Op Runs tracker omits agent messages. The category uses structured run status, not interpretation of agent prose.

No-op message

Completed PR #80784 review: submitted one overall COMMENT review with no findings, and recorded the no_findings outcome for head 8abe4b2.

🔎 Code quality review by PR Code Quality Reviewer · copilot · gpt56 · ~2 AIC · ⌖ ~1 AIC · ⊞ 1.7K · history

@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

✅ PR Specification Reviewer completed successfully!

PR Specification Reviewer

Outcome: reported_action

The agent completed and reported one or more actions. The no-op output is a completion signal, not a claim that no action was taken.

The [aw] No-Op Runs tracker omits agent messages. The category uses structured run status, not interpretation of agent prose.

No-op message

PR #80784 reviewed at head 8abe4b2: no applicable specification regression or synchronization proposal found; no GitHub review action was needed.

Generated by PR Specification Reviewer for #80784 · copilot · gpt56 · ~3 AIC · ⌖ ~1 AIC · ⊞ 1.8K · history

@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

✅ Ponytail Reviewer completed successfully!

Ponytail Reviewer

Outcome: reported_no_action

The agent completed and reported no action. A no-op output alone does not verify that all required work was completed or that publication was possible.

The [aw] No-Op Runs tracker omits agent messages. The category uses structured run status, not interpretation of agent prose.

No-op message

Lean already. Ship.

Generated by Ponytail Reviewer for #80784 · codex · routed: gpt61 medium · ~5 AIC · ⌖ ~1 AIC · ⊞ 176 · history

@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

🧠 Matt Pocock Skills Reviewer has completed the skills-based review. ✅

🧠 Reviewed using Matt Pocock's skills by Matt Pocock Skills Reviewer · copilot · routed: haiku55 xhigh · ~7 AIC · ⌖ ~2 AIC · ⊞ 5.1K · history

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the changed lines in the cost pairing and legacy averaging updates. I found no actionable correctness, regression, performance, or maintainability issues blocking merge. The specialist pass completed; its only suggestion relied on malformed legacy entries with model_credits greater than total credits, which is outside the demonstrated data contract and was not retained as a finding.

🔎 Code quality review by PR Code Quality Reviewer · copilot · gpt50 · ~2 AIC · ⌖ ~1 AIC · ⊞ 1.7K
Comment /review to run again

@github-actions

Copy link
Copy Markdown
Contributor

Comment Memory

Peek at saved memory (review-history)
{"id":"ldg-0480fb5d-80ac-41fb-aae4-6b681115964d","parents":[],"payload":{"id":"ldg-bf790107-e1fc-4bad-aa6e-85a6424e04b3","operation":"append","value":{"finding_count":0,"head_sha":"8abe4b2dda7811b90fba0a5257f4bdbe869fef92","outcome":"no_findings"}},"sha":"sha256:f0fd87568cff5cfd1445d57a7bccd56938d7ac6031fad8d8a999a559a99bd9bb","timestamp":"2026-11-24T19:48:13.822Z","type":"ledger_append","version":1}
History

@github-actions

Copy link
Copy Markdown
Contributor

🧪 Test Quality Sentinel Report

✅ Test Quality Score: 90/100 — Excellent

Analyzed 2 test(s): 2 design, 0 implementation, 0 violation(s).

📊 Metrics (2 tests)
Metric Value
Analyzed 2 (Go: 0, JS: 2)
✅ Design 2 (100%)
⚠️ Implementation 0 (0%)
Edge/error coverage 2 (100%)
Duplicate clusters 0
Inflation YES (push_routing_history.test.cjs: 18 added test lines vs 6 added production lines, 3:1)
🚨 Violations 0
Test File Classification Issues
emits null model_credits when an unpriced sub-agent makes total credits null, while preserving main tokens actions/setup/js/push_routing_history.test.cjs:239 behavioral_contract / high_value / design_test None
ignores standalone model_credits on legacy entries whose total credits are null actions/setup/js/routing_history.test.cjs:73 behavioral_contract / high_value / design_test None
⚠️ Flagged Tests (1)

Test inflation (file-level): actions/setup/js/push_routing_history.test.cjs adds 18 lines against 6 production lines (3:1, above the 2:1 threshold). The excess comes from one new test whose fixtures span several lines. Both new tests assert exact outputs and guard real regressions: the pre-PR code returns model_credits 30 in each case, where the tests expect null. Nothing is padded or duplicated, so this costs 10 points but does not fail the PR.

Verdict

✅ passed. 0% implementation tests (threshold: 30%).

Note: vitest could not be executed in this review environment, so the verdict rests on tracing the new tests through runCost and averages. CI should confirm they pass.

🧪 Test quality analysis by Test Quality Sentinel · copilot · routed: haiku55 xhigh · ~4 AIC · ⌖ ~2 AIC · ⊞ 7.4K · history
Comment /review to run again

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Test Quality Sentinel: 90/100. 0% implementation tests (threshold: 30%). No guideline violations.

🧪 Test quality analysis by Test Quality Sentinel · copilot · routed: haiku55 xhigh · ~4 AIC · ⌖ ~2 AIC · ⊞ 7.4K
Comment /review to run again

@github-actions

Copy link
Copy Markdown
Contributor

Comment Memory

Peek at saved memory (spec-review-history)
{"id":"ldg-b9a4111f-c1e5-40c4-856b-477a58d7d552","parents":[],"payload":{"id":"ldg-1f771667-e685-485a-9184-1f6e6faa3775","operation":"append","value":{"finding_count":0,"head_sha":"8abe4b2dda7811b90fba0a5257f4bdbe869fef92","outcome":"aligned"}},"sha":"sha256:3d5b197a335f576535f217b4baf1d2fed5001a1a0638e9675f74e1276bb6283d","timestamp":"2026-11-24T19:49:21.558Z","type":"ledger_append","version":1}
History

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The writer and legacy-history reducer changes affect cost evidence used by AWF’s live challenger selection and budget decisions, creating cross-component operational consequences beyond limited-impact automated approval scope.

No open issues 🎉

What changed in this PR

This PR fixes adaptive routing cost accounting so total credits and model_credits are averaged over the same fully priced runs, preventing legacy partial bills from skewing challenger cost estimates.

Changes:

  • Emit null model_credits when total credits are unknown, while preserving main-conversation token counts.
  • Ignore standalone model costs from partially priced runs in the reducer.
  • Add regression tests for unpriced sub-agent requests and legacy partial-bill histories.
File Description
actions/​setup/​js/​routing_history.test.cjs Tests that legacy partial bills do not affect either cost average.
actions/​setup/​js/​routing_history.cjs Pairs both cost averages on fully priced entries and exports averages.
actions/​setup/​js/​push_routing_history.test.cjs Tests null cost pairing while retaining main-conversation tokens.
actions/​setup/​js/​push_routing_history.cjs Makes model credits null whenever total credits are unknown.

Review effort: Balanced (adaptive)

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review summary

No blocking issues. credits and model_credits are now paired on the same fully priced runs in both the writer (runCost) and the reducer (averages), which keeps the stated invariant 0 is at most model_credits is at most credits.

Verified against head 8abe4b2:

  • Replayed all 30 run-bearing oracle cases and 8 timelines in aw2-reducer.json through applyRun with Node. All pass, including the synced partial-bill-ignores-standalone-model-share vector (credits 10, model_credits 10, bills 1).
  • runCost on the PR scenario returns credits null, model_credits null, tokens [600,400,0,200,null], matching the new test expectation.
  • Fuzzed averages over 20,000 random histories with zero violations of the invariant.
  • pkg/cli/routing_command.go already models model_credits as a nullable float, so the new null is handled downstream.

Non-blocking note: a run with any unpriced sub-agent now contributes nothing to a pair model-share average, not only to credits. This follows the fully-priced rule the PR documents, so no change is requested.

Limitation

node_modules is absent on this runner, so vitest and npm run typecheck were not executed here. The checks above call the modules directly. CI should run routing_history.test.cjs and push_routing_history.test.cjs.

🧠 Reviewed using Matt Pocock's skills by Matt Pocock Skills Reviewer · copilot · routed: haiku55 xhigh · ~7 AIC · ⌖ ~2 AIC · ⊞ 5.1K
Comment /matt to run again

@rabeckett
rabeckett merged commit 3ef6759 into main Nov 24, 2026
407 checks passed
@rabeckett
rabeckett deleted the fix/pair-credits-and-model-credits branch November 24, 2026 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Keep credits and model_credits paired on the same fully priced runs in routing history

2 participants