Skip to content

fix(session): resolve the turn's agent from live session state - #41396

Open
mewmewmemw wants to merge 3 commits into
anomalyco:devfrom
mewmewmemw:fix/session-agent-resolution
Open

fix(session): resolve the turn's agent from live session state#41396
mewmewmemw wants to merge 3 commits into
anomalyco:devfrom
mewmewmemw:fix/session-agent-resolution

Conversation

@mewmewmemw

@mewmewmemw mewmewmemw commented Aug 9, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #41387

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Two related fixes for the stale system prompt after switching a session's agent (Tab / switchAgent):

  1. createUserMessage (16a1511): when input.agent is absent (the V2 prompt flow does not pass an agent), the agent was resolved via agents.defaultInfo() — the configured default — ignoring the session's selected agent, and the setAgentModel block rolled session.agent back to the default, so the stale prompt persisted across turns. Now resolved as input.agent → session.agent → default, degrading to the default only when session.agent points to a removed agent.

  2. Prompt loop (4ebf96a): the loop built the turn's system prompt from the agent stamped on the latest user message rather than the live session.agent. Messages authored before a switch keep the old stamp, so turns kept running on the previous agent — observed live persisting across multiple consecutive turns, cleared only by a process restart. The loop now resolves the turn's agent from a fresh session read, falling back to the message stamp only when the session has none set.

How did you verify your code works?

Offline regression tests (in-process TestLLMServer, no network):

  • "first user message after switching session agent uses the new agent's system prompt" — red before fix 1, green after
  • "loop builds the first turn's system prompt from the session's selected agent after a durable switchAgent" — exercises the real durable AgentSwitched path; red before fix 2, green after
  • two additional V2 runner tests documenting that the V2 path re-reads the agent fresh each turn
    Suites: prompt.test.ts 59 pass / 0 fail; packages/opencode full suite 3260 pass / 0 fail; packages/core 1082 pass / 0 fail; typecheck clean in both packages.

Screenshots / recordings

If this is a UI change, please include a screenshot or recording.

Checklist

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

… is given

createUserMessage fell back to the default agent when input.agent was
absent, ignoring the session's selected agent (set via Tab / switchAgent).
It also rolled session.agent back to the default via setAgentModel, so the
stale system prompt persisted across turns. Resolve as:
input.agent -> session.agent -> default, degrading to the default only
when session.agent points to a removed agent.

Regression test added (offline, TestLLMServer).
@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. needs:issue labels Aug 9, 2026
@github-actions

github-actions Bot commented Aug 9, 2026

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 github-actions Bot removed needs:issue needs:compliance This means the issue will auto-close after 2 hours. labels Aug 9, 2026
@github-actions

github-actions Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@github-actions

github-actions Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

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

Based on my search results, I found one potentially related PR that warrants attention:

Related PR:

No other duplicate PRs were found. The current PR (#41396) appears to be the primary fix for issue #41387.

…f message stamp

The V1 prompt loop built the turn's system prompt from the agent stamped on
the latest user message. After a durable agent switch (Tab / switchAgent)
already-authored messages keep the old stamp, so turns kept running on the
previous agent until process restart. Resolve the turn's agent from the live
session selection (fresh read), falling back to the message stamp only when
the session has none set; degrade to the default agent when the session's
agent no longer exists.

Regression test added through the real durable AgentSwitched path (offline,
TestLLMServer); V2 runner behavior documented with two additional tests.
@mewmewmemw mewmewmemw changed the title fix(session): resolve agent from session state when no explicit agent is given fix(session): resolve the turn's agent from live session state Aug 9, 2026
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]: Tab agent switch: first message after the switch still runs on the previous agent's system prompt

1 participant