diff --git a/extensions/cli/src/session.test.ts b/extensions/cli/src/session.test.ts index 51bb6456645..b0b13b99193 100644 --- a/extensions/cli/src/session.test.ts +++ b/extensions/cli/src/session.test.ts @@ -357,6 +357,31 @@ describe("SessionManager", () => { expect(result).toEqual(mockSession); expect(getCurrentSession()).toEqual(mockSession); }); + + it("should load the selected session when CONTINUE_CLI_TEST_SESSION_ID is set", () => { + const selectedSession: Session = { + sessionId: "selected-session-id", + title: "Selected Session", + workspaceDirectory: "/test/workspace", + history: [], + }; + mockHistoryManager.load.mockReturnValue(selectedSession); + process.env.CONTINUE_CLI_TEST_SESSION_ID = "selected-session-id"; + + try { + const result = loadSession(); + + expect(result).toEqual(selectedSession); + // It must resolve the session by id, not by most-recent mtime + expect(mockHistoryManager.load).toHaveBeenCalledWith( + "selected-session-id", + ); + expect(mockFs.readdirSync).not.toHaveBeenCalled(); + expect(getCurrentSession()).toEqual(selectedSession); + } finally { + delete process.env.CONTINUE_CLI_TEST_SESSION_ID; + } + }); }); describe("clearSession", () => { diff --git a/extensions/cli/src/session.ts b/extensions/cli/src/session.ts index 45f765a08ae..d793f3ce3f8 100644 --- a/extensions/cli/src/session.ts +++ b/extensions/cli/src/session.ts @@ -300,6 +300,18 @@ export function saveSession(): void { */ export function loadSession(): Session | null { try { + // If a specific session has been selected (e.g. via `cn ls`), resume that + // one instead of the most recent. The selection is communicated through the + // same env var that SessionManager uses to pin the current session id. + const selectedSessionId = process.env.CONTINUE_CLI_TEST_SESSION_ID; + if (selectedSessionId) { + const selectedSession = loadSessionById(selectedSessionId); + if (selectedSession) { + SessionManager.getInstance().setSession(selectedSession); + return selectedSession; + } + } + // For resume, we need to find the most recent session const sessionDir = getSessionDir(); if (!fs.existsSync(sessionDir)) {