diff --git a/src/features/claude-code-session-state/state.test.ts b/src/features/claude-code-session-state/state.test.ts index 351211409..459dfa7a2 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 4942fc46e..f155c19d7 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