From f8155e7d457dbc7cb4044fc25e9f19e676051ea6 Mon Sep 17 00:00:00 2001 From: justsisyphus Date: Fri, 23 Jan 2026 16:25:26 +0900 Subject: [PATCH] fix(session): preserve custom agent after switching (#1017) Use setSessionAgent (first-write wins) instead of updateSessionAgent in chat.message handler. This prevents the default agent from overwriting a custom agent that was set via UI switch. Fixes #893 Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode) Co-authored-by: justsisyphus Co-authored-by: Sisyphus --- .../claude-code-session-state/state.test.ts | 36 +++++++++++++++++++ src/index.ts | 2 +- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/features/claude-code-session-state/state.test.ts b/src/features/claude-code-session-state/state.test.ts index 35121140..459dfa7a 100644 --- a/src/features/claude-code-session-state/state.test.ts +++ b/src/features/claude-code-session-state/state.test.ts @@ -123,4 +123,40 @@ describe("claude-code-session-state", () => { expect(getSessionAgent(sessionID)).toBeUndefined() }) }) + + describe("issue #893: custom agent switch reset", () => { + test("should preserve custom agent when default agent is sent on subsequent messages", () => { + // #given - user switches to custom agent "MyCustomAgent" + const sessionID = "test-session-custom" + const customAgent = "MyCustomAgent" + const defaultAgent = "Sisyphus" + + // User switches to custom agent (via UI) + setSessionAgent(sessionID, customAgent) + expect(getSessionAgent(sessionID)).toBe(customAgent) + + // #when - first message after switch sends default agent + // This simulates the bug: input.agent = "Sisyphus" on first message + // Using setSessionAgent (first-write wins) should preserve custom agent + setSessionAgent(sessionID, defaultAgent) + + // #then - custom agent should be preserved, NOT overwritten + expect(getSessionAgent(sessionID)).toBe(customAgent) + }) + + test("should allow explicit agent update via updateSessionAgent", () => { + // #given - custom agent is set + const sessionID = "test-session-explicit" + const customAgent = "MyCustomAgent" + const newAgent = "AnotherAgent" + + setSessionAgent(sessionID, customAgent) + + // #when - explicit update (user intentionally switches) + updateSessionAgent(sessionID, newAgent) + + // #then - should be updated + expect(getSessionAgent(sessionID)).toBe(newAgent) + }) + }) }) diff --git a/src/index.ts b/src/index.ts index 4942fc46..f155c19d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -314,7 +314,7 @@ const OhMyOpenCodePlugin: Plugin = async (ctx) => { "chat.message": async (input, output) => { if (input.agent) { - updateSessionAgent(input.sessionID, input.agent); + setSessionAgent(input.sessionID, input.agent); } const message = (output as { message: { variant?: string } }).message