Skip to content

fix(#1227): resolvedModel picks the phase's main model, not the first modelUsage key - #1229

Merged
admarble merged 3 commits into
mainfrom
fix/1227-resolved-model
Sep 30, 2026
Merged

admarble merged 3 commits into
mainfrom
fix/1227-resolved-model

Conversation

@admarble

@admarble admarble commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Delivers #1227.

What

resolvedModel was Object.keys(modelUsage)[0]. Since 2026-09-20, the first key has been a Haiku helper call on every phase, so #975's phasePolicies.<phase>.resolvedModel, #1198's run-log model, and the top-level metrics model all record Haiku.

The new selectResolvedModel(modelUsage, requested) in phase-executor.ts works in two steps:

  • It keeps the keys that match the dispatched model (agentConfig.model, an alias like opus or a full ID). If none match, it keeps all keys.
  • From those, it takes the key with the largest total token volume (input + output + cache read + cache creation).

Between those two steps it keeps only keys with cache reads, when any exist. Only the main loop re-reads a cached conversation. The advisor's read is never cached, and the Haiku helper had 0 cache reads in 140 of 140 recorded phaseUsage rows, while every main-model row had some. This covers phases with no configured model (no hint), where a short phase's advisor or helper read can be the bigger number. The probe shape in the tests is an Opus main model plus a Fable advisor.

phaseUsage rows are unchanged.

Acceptance criteria

  • AC-1: resolvedModel is the main model when a Haiku helper is listed first. Covered by phase-executor.test.ts > #1227 selectResolvedModel (the recorded fix(metrics): tokensUsed is 0 on every recorded run — capture-tokens.sh reads .usage (transcripts carry .message.usage) and run reads the wrong dir; wire SDK modelUsage tokens+costUSD per phase × model into metrics and stats #986 fixture, which is Haiku-first) and end to end by phase-executor.model-roles.test.ts (executePhaseWithRetry with a Haiku-first real-shape map).
  • AC-2: resolvedModel never selects the advisor key when the dispatched model's key is present. Covered by the unit test and by the model-roles test with phasePolicies.exec.model = "opus".
  • AC-3: the run log's model and the top-level model consume PhaseResult.resolvedModel unchanged (batch-executor.ts and run-orchestrator.ts:2056). The existing test "derives model from the first phase … with a resolvedModel" in run-orchestrator.test.ts covers that plumbing. The corrected value entering it is proven by the model-roles test above.
  • AC-4: the "First key is the primary model" comment is gone. The three other comments that stated the first-key rule are also fixed (normalizeModelUsage docblock, types.ts resolvedModel, agent-driver.ts modelUsage).
  • AC-1 also has a Haiku-at-~$0 + claude-sonnet-5 unit fixture in phase-executor.test.ts, the file the AC names.

The old #975 fixture used snake_case field names with the main model listed first. That shape passed with the bug in place, so I replaced it with the SDK's real camelCase fields and a Haiku-first order.

Verification

  • After the QA follow-up (f0ca16a): phase-executor*, run-orchestrator*, batch-executor* and metrics* test files, 22 files and 458 tests passed.
  • Before the follow-up: npm run build && npm test: 351 files, 8636 passed, 1 expected fail, 3 skipped.
  • Mutation-verified: AC-1 — return keys[0] instead of the volume reduce; 1227 picks the main model, not the Haiku helper listed first (recorded map) failed; restored.
  • Mutation-verified: AC-2 — removed the cache-read filter (candidates = pool); 1227 without a hint, prefers the cached main model over a larger uncached advisor or helper read failed; restored.
  • Mutation-verified: AC-2 — ignored the dispatched-model hint (candidates = keys); 1227 never selects the advisor key when the dispatched model's key is present failed; restored.

Side finding (not fixed here)

phase-executor.model-roles.test.ts drives the real exec path against process.cwd(). Running it on a dirty tree auto-commits your work as chore(#975): wip checkpoint …, which happened while I wrote this fix. CI is unaffected because the tree is clean there.

Relates to #975, #1198, #1138.

🤖 Generated with Claude Code

… modelUsage key

Map order is not dispatch order: a Haiku helper call is listed first on every phase since 2026-09-20, and an advisor model is its own key. selectResolvedModel prefers keys matching the dispatched model, then the largest token volume. Fixes the #975 phasePolicies.resolvedModel and the #1198 run-log/top-level model that derive from it.

Entire-Checkpoint: 5fb75d344561
…e first-key comments

QA follow-up: without a phase policy there is no hint, and a short phase's uncached advisor read or a helper's web-search read can out-volume the main model. Only the main loop re-reads a cached conversation (helper: 0 cache reads in 140/140 recorded rows; advisor reads are never cached), so filter to cached keys before comparing volume.

Entire-Checkpoint: a71cb33f89cf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant