Skip to content

feat(session): persisting agent for session#1842

Closed
spoons-and-mirrors wants to merge 5 commits into
anomalyco:devfrom
spoons-and-mirrors:feat/persisting-agent-per-session
Closed

feat(session): persisting agent for session#1842
spoons-and-mirrors wants to merge 5 commits into
anomalyco:devfrom
spoons-and-mirrors:feat/persisting-agent-per-session

Conversation

@spoons-and-mirrors

Copy link
Copy Markdown
Contributor

Summary

This PR implements per-session agent persistence, ensuring that the selected agent is remembered for each individual session and resolves issue #1812

Before

Previously, agent selection was a global setting. When a user switched agents in one session, that agent would remain active even when switching to other sessions. This meant that switching between sessions did not restore the agent that was last used in that specific context.

After

With this change, each session now stores its own agent. The behavior is as follows:

  • Agent Persistence: When you switch agents, the change is saved to the current session's metadata.
  • Session Switching: When you switch to a different session, the TUI automatically activates the agent that was last used in that session.
  • Session Creation: New sessions are created with the currently active agent.
  • Backward Compatibility: The changes are fully backward-compatible. A migration has been added to populate existing sessions with a default agent ("build").

This creates a more intuitive and seamless user experience, as the agent context is now tied directly to the session it's being used in.

Key Changes

Backend:

  • The session Info schema in packages/opencode/src/session/index.ts was extended with an optional agent field.
  • A PATCH /session/:id endpoint was added to packages/opencode/src/server/server.ts to handle session updates.
  • The POST /session endpoint now accepts an agent parameter on creation.
  • A migration was added in packages/opencode/src/storage/storage.ts to update existing session data.

Go SDK:

  • The Session struct in packages/sdk/go/session.go was updated to include the Agent field.
  • A Session.Update() method was added to call the new PATCH endpoint.
  • The Session.New() method was updated to accept an agent on creation.

TUI:

  • The SessionSelectedMsg and AgentSelectedMsg handlers in packages/tui/internal/tui/tui.go were updated to restore and persist the agent for the current session.
  • The cycleMode function in packages/tui/internal/app/app.go now makes a backend call to update the session when the agent is switched.

@spoons-and-mirrors spoons-and-mirrors changed the title feat(session): persisting agent for session [issue #1812](https://github.com/sst/opencode/issues/1812#issue-3309596744) feat(session): persisting agent for session Aug 12, 2025
@thdxr
thdxr requested a review from adamdotdevin August 12, 2025 03:57
@spoons-and-mirrors

Copy link
Copy Markdown
Contributor Author

very light conflict with #1854

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This violates the AGENTS.md guideline: "DO NOT do unnecessary destructuring of variables". Consider: Use c.req.valid("json")?.agent directly instead of destructuring into body variable first.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This violates the AGENTS.md guideline: "DO NOT do unnecessary destructuring of variables". The body variable is used only for body?.agent - consider using c.req.valid("json")?.agent directly.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This violates the AGENTS.md guideline: "DO NOT do unnecessary destructuring of variables". The body variable is destructured but used only once for body.agent - consider inlining this usage.

@thdxr

thdxr commented Aug 12, 2025

Copy link
Copy Markdown
Member

each message keeps track of the last agent that was used - can we use that instead to load the right agent when switching to a session?

@spoons-and-mirrors

Copy link
Copy Markdown
Contributor Author

Will look into it in a few 👌

@spoons-and-mirrors

Copy link
Copy Markdown
Contributor Author

closing the PR, wrong way to go at it indeed

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.

Feature request: Agent mode per session

2 participants