Skip to content

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

Description

@iceteaSA

Description

A prompt that arrives without explicit agent/model fields — a background-subagent completion notification, or any injected/fire-and-forget prompt — makes createUserMessage fall back to the default agent and that agent's configured model, silently switching the session's active agent and model. Provider prompt caching keys on the model, so the next turn also misses the cache prefix and re-uploads the context.

Root cause

packages/opencode/src/session/prompt.ts, in createUserMessage:

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

An omitted agent resolves to the default agent, and an omitted model to that agent's configured model. Neither consults the session's active agent/model, so any injected prompt without those fields redirects the session.

Steps to reproduce

  1. Set a session to a non-default agent and model.
  2. Deliver an injected prompt with no agent/model (e.g. a background subagent completing, which re-prompts the parent).
  3. The next assistant turn runs on the default agent's model, not the session's.

Impact

Agent-coordination and overnight sessions: completion notifications change the session's agent and model out from under it, causing unexpected model switches and cache-prefix busts (billed on both models across the switch).

Prior art

Reported before as #21728 (closed by the inactivity bot while assigned to a maintainer) and fixed in @sjawhar's #35195 (also bot-closed, unmerged). Filing a live tracking issue since both are closed. #46106 revives the fix, extended to preserve the session variant and to only adopt the new agent's model on a genuine agent switch.

OpenCode version

dev (reproduces on current dev)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions