Skip to content

fix(session): preserve agent and model on injected prompts without explicit fields - #46106

Open
iceteaSA wants to merge 1 commit into
anomalyco:devfrom
iceteaSA:message-model-preserve
Open

fix(session): preserve agent and model on injected prompts without explicit fields#46106
iceteaSA wants to merge 1 commit into
anomalyco:devfrom
iceteaSA:message-model-preserve

Conversation

@iceteaSA

@iceteaSA iceteaSA commented Aug 29, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #46111. Revives @sjawhar's #35195 (bot-closed, unmerged) and the original report #21728 (also bot-closed) — extended here to preserve the session variant and to only adopt the new agent's model on a genuine agent switch.

Type of change

  • Bug fix

What does this PR do?

A prompt that arrives without explicit agent/model — a background-subagent completion notification, or any injected prompt — makes createUserMessage fall back to the default agent and that agent's configured model, silently switching the session. Provider prompt caching keys on the model, so it also drops the cached prefix and re-uploads the context. This is #21728, reported against prompt_async.

On current dev:

const agentName = input.agent
const ag = agentName ? yield* agents.get(agentName) : yield* agents.defaultInfo()
...
const model = input.model ?? ag.model ?? (yield* currentModel(input.sessionID))

Any injected prompt omitting the fields lands on the default agent and ag.model, whatever the session was using.

The fix reads the durable session row (which survives compaction and records the active agent/model) and the last agent-carrying user message before falling back:

  • agentName = input.agent ?? current.agent ?? prev?.info.agent
  • a genuine agent switch (switched = !!current.agent && ag.name !== current.agent) still adopts the new agent's model; an injected prompt for the current agent keeps the session model
  • model = input.model ?? sessionModel ?? prev?.info.model ?? ag.model ?? currentModel(...)
  • variant follows the same precedence, so a session on a non-default reasoning effort isn't reset

This is the broad fix at the shared resolution point #35195 aimed at, plus variant preservation and the agent-switch guard. Explicit agent/model behave exactly as before.

How did you verify your code works?

Five regression tests through the real producer (SessionPrompt.prompt), red-first. Reverse-applying only the src/ hunks (tests kept) shows them fail against unmodified dev:

production reverted, tests present:
  expected "build",            received "plan"          (agent preserved)
  expected provider "opencode", received "test"          (model preserved)
  expected "build",            received "plan"           (variant path)
  expected model "test-model",  received "other-model"
  59 pass, 4 fail
restored: 63 pass, 1 skip, 0 fail

Full suite packages/opencode: 3400 pass / 0 fail, +5 over baseline. bun typecheck clean in opencode and core.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

…plicit fields

An injected prompt omitting agent/model falls back to the default agent’s configured model, silently switching the session and busting the provider cache prefix.

createUserMessage reads the durable session row and the last agent-carrying user message before falling back to defaults; a genuine agent switch still adopts the new agent’s model, and variant is preserved on the same precedence.

Revives the fix from anomalyco#35195 (closed by the inactivity bot, unmerged), extended to also preserve the session variant and to only adopt the new agent’s model on a genuine agent switch.

Co-authored-by: Sami Jawhar <sjawhar@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Potential Duplicate/Related PRs Found

High relevance:

  1. fix(session): don't clobber user's model swap on model-less prompts #42894 - fix(session): don't clobber user's model swap on model-less prompts

    • Directly addresses the same issue: preserving user's model selection when prompts don't explicitly specify it. This appears to be a prior attempt at fixing the same root cause.
  2. fix(core): preserve conversation agent during compaction #45475 - fix(core): preserve conversation agent during compaction

    • Related to agent preservation across session state transitions (compaction), which complements this PR's focus on preserving agent on injected prompts.

Context (mentioned in PR description):

The PR description itself references:

These historical issues should be checked to understand the full context of this fix attempt.

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.

[BUG]: Injected prompts without agent/model silently switch the session's agent and model

1 participant