diff --git a/src/services/acp/__tests__/agent.test.ts b/src/services/acp/__tests__/agent.test.ts index 3aee6c52a..be1e87b5f 100644 --- a/src/services/acp/__tests__/agent.test.ts +++ b/src/services/acp/__tests__/agent.test.ts @@ -1226,19 +1226,20 @@ describe('AcpAgent', () => { ) }) - test('prompt does not trigger additional switchSession for multi-session', async () => { + test('prompt switches global sessionId to the correct session', async () => { const agent = new AcpAgent(makeConn()) await agent.newSession({ cwd: '/tmp' } as any) await agent.newSession({ cwd: '/tmp' } as any) mockSwitchSession.mockClear() - // Prompts should not call switchSession — alignment happens at session creation + // Prompts must switch global state so recordTranscript writes to + // the correct session file in multi-session scenarios. const s1 = agent.sessions.keys().next().value await agent.prompt({ sessionId: s1, prompt: [{ type: 'text', text: 'hello' }], } as any) - expect(mockSwitchSession).not.toHaveBeenCalled() + expect(mockSwitchSession).toHaveBeenCalledWith(s1, null) }) }) }) diff --git a/src/services/acp/agent.ts b/src/services/acp/agent.ts index 617879103..8868b7367 100644 --- a/src/services/acp/agent.ts +++ b/src/services/acp/agent.ts @@ -300,6 +300,10 @@ export class AcpAgent implements Agent { // After a previous interrupt(), the internal controller is stuck in // aborted state — without this, submitMessage() fails immediately. session.queryEngine.resetAbortController() + // Switch global session state so recordTranscript writes to the correct + // session file. Without this, multi-session scenarios (or creating a new + // session after another) write transcript data to the wrong file. + switchSession(params.sessionId as SessionId, getSessionProjectDir()) const sdkMessages = session.queryEngine.submitMessage(promptInput)