From ae32a1b865002c5438fa5e08d0141f33a7c3b034 Mon Sep 17 00:00:00 2001 From: Matt Van Horn Date: Fri, 27 Mar 2026 13:41:29 -0700 Subject: [PATCH] feat: filter session history by workspace directory (#11813) * feat: filter session history by workspace directory Add optional workspaceDirectory field to ListHistoryOptions so callers can scope session lists to a specific workspace. Filtering is case-insensitive and applied before pagination. Omitting the field preserves the existing behavior of returning all sessions. Fixes #9936 Co-Authored-By: Claude Opus 4.6 * fix: address bot review - typeof guard and test coverage - Use typeof check instead of truthiness for workspaceDirectory to handle malformed session metadata safely - Rename test to match actual coverage (limit only) - Add separate test for combined limit+offset with workspace filter --------- Co-authored-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> Co-authored-by: Claude Opus 4.6 --- core/protocol/core.ts | 1 + core/util/history.test.ts | 81 +++++++++++++++++++++++++++++++++++++++ core/util/history.ts | 10 +++++ 3 files changed, 92 insertions(+) diff --git a/core/protocol/core.ts b/core/protocol/core.ts index 758a08faf..17626280d 100644 --- a/core/protocol/core.ts +++ b/core/protocol/core.ts @@ -63,6 +63,7 @@ export enum OnboardingModes { export interface ListHistoryOptions { offset?: number; limit?: number; + workspaceDirectory?: string; } export type ToCoreFromIdeOrWebviewProtocol = { diff --git a/core/util/history.test.ts b/core/util/history.test.ts index b27a09a4f..a9f9158b1 100644 --- a/core/util/history.test.ts +++ b/core/util/history.test.ts @@ -74,6 +74,87 @@ describe("Full session lifecycle", () => { }); }); +describe("Workspace directory filtering", () => { + beforeAll(() => { + historyManager.clearAll(); + historyManager.save({ + history: [], + title: "Project A session 1", + workspaceDirectory: "/home/user/project-a", + sessionId: "ws-a-1", + }); + historyManager.save({ + history: [], + title: "Project B session 1", + workspaceDirectory: "/home/user/project-b", + sessionId: "ws-b-1", + }); + historyManager.save({ + history: [], + title: "Project A session 2", + workspaceDirectory: "/home/user/project-a", + sessionId: "ws-a-2", + }); + historyManager.save({ + history: [], + title: "No workspace", + workspaceDirectory: "", + sessionId: "ws-none", + }); + }); + + afterAll(() => { + historyManager.clearAll(); + }); + + test("Filter sessions by workspace directory", () => { + const sessions = historyManager.list({ + workspaceDirectory: "/home/user/project-a", + }); + expect(sessions.length).toBe(2); + expect( + sessions.every((s) => s.workspaceDirectory === "/home/user/project-a"), + ).toBe(true); + }); + + test("Omitting workspace returns all sessions", () => { + const sessions = historyManager.list({}); + expect(sessions.length).toBe(4); + }); + + test("Workspace filter is case-insensitive", () => { + const sessions = historyManager.list({ + workspaceDirectory: "/HOME/USER/PROJECT-A", + }); + expect(sessions.length).toBe(2); + }); + + test("Non-matching workspace returns empty list", () => { + const sessions = historyManager.list({ + workspaceDirectory: "/home/user/nonexistent", + }); + expect(sessions.length).toBe(0); + }); + + test("Workspace filter works with limit", () => { + const sessions = historyManager.list({ + workspaceDirectory: "/home/user/project-a", + limit: 1, + }); + expect(sessions.length).toBe(1); + }); + + test("Workspace filter works with limit and offset", () => { + const sessions = historyManager.list({ + workspaceDirectory: "/home/user/project-a", + limit: 1, + offset: 1, + }); + expect(sessions.length).toBe(1); + expect(sessions[0].sessionId).toBe("ws-a-1"); + }); +}); + describe("Many sessions created", () => { test("Create 100 sessions and list all", () => { for (let i = 0; i < 100; i++) { diff --git a/core/util/history.ts b/core/util/history.ts index 44241c420..0f5af729e 100644 --- a/core/util/history.ts +++ b/core/util/history.ts @@ -38,6 +38,16 @@ export class HistoryManager { }) .reverse(); + // Filter by workspace directory if provided + if (options.workspaceDirectory) { + const target = options.workspaceDirectory.toLowerCase(); + sessions = sessions.filter( + (session) => + typeof session.workspaceDirectory === "string" && + session.workspaceDirectory.toLowerCase() === target, + ); + } + // Apply limit and offset if (options.limit) { const offset = options.offset || 0;