fix(opencode): keep the session model on injected prompts - #38959
fix(opencode): keep the session model on injected prompts#38959iceteaSA wants to merge 1 commit into
Conversation
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
|
Adding field measurements and a scope note, since this has been sitting a while and I've since learned the fix here is narrower than the bug. Measured exposure. On one long-running box (
So for a config that pins an agent model, the resolution order Scope note against this PR. What I did here fixes the caller: I've since been running a resolver-side variant instead, which changes const switched = !!current.agent && ag.name !== current.agent
const sessionModel = !switched && current.model ? { /* from session row */ } : undefined
const model = input.model ?? sessionModel ?? prev?.info.model ?? ag.model ?? (yield* currentModel(input.sessionID))The That approach covers every injector at once rather than one call site at a time. I'm not pushing it into this PR unprompted — it's a broader change and the ordering question is really a maintainer call. Happy to do either:
Say which you'd prefer and I'll turn it around. I have the resolver version already running with tests, though they're written against a diverged local tree, so I'd rewrite them against One caveat on my numbers, stated plainly: they come from a single heavily-used installation, so treat them as an existence proof for the exposure rather than a population estimate. Anyone whose agents don't pin a model won't see this at all. |
Issue for this PR
Closes #38770
Also relevant to #28735 and #23369, which describe the same symptom through paths this does not fully cover.
Type of change
What does this PR do?
A background subagent finishing can silently switch the parent session's model to the agent's configured default. Since Anthropic prompt caching keys on the model, the next turn misses the cache prefix and re-uploads the entire context — on a long session that is the expensive kind of silent. #38770 reports being billed on both models sequentially, which is the same thing seen from the invoice side.
createUserMessageresolves an injected prompt's model as:So any injected prompt that omits
modellands onag.model— the configured default — regardless of what the session was actually using. Two call sites ondevdo exactly that:TaskTool.injectBackgroundResult(packages/opencode/src/tool/task.ts) passesagentandvariantbut nomodel. This is #38770: the completion notification for a background child re-prompts the parent, and the parent's model changes underneath them. It now reads the parent's latest user message and forwards that model explicitly, with the existingvariantretained as a fallback.That also explains the detail in #38770 that looks strange at first — the agent kept reporting the old model as its identity. The system prompt was assembled before the switch; nothing re-runs it when an injected message changes the resolved model, so the prompt and the billed model disagree until the next assembly.
The shell-execution message (
packages/opencode/src/session/prompt.ts) carries provider and model but drops the variant, so a session runningxhighquietly falls back to default reasoning effort. The variant is now preserved when the resolved model carries one. Separate bug, same shape, found while fixing the first.Both fixes only supply what was already implied — neither changes agent resolution, so a genuine agent switch still adopts the new agent's model as before.
I found this from the other end: a live session moved
claude-fable-5→claude-opus-5at a user message with no text (the injected notification), timestamp-matched to its child session completing, landing on exactly that agent's configured default.How did you verify your code works?
Red-first, one test per site, each failing for its own distinct reason against unmodified
dev:Mutation-checked separately, so neither fix is riding on the other's coverage:
Full suite in
packages/opencode: 3208 pass / 0 fail.bun typecheckexit 0.Note on scope: #35195 fixes this class more broadly at the shared resolution point, by consulting the durable session row before the agent default — these two sites would be covered by it. This PR fixes the call sites directly, which is independently useful and does not conflict with that approach.
Screenshots / recordings
Not a UI change.
Checklist