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 <noreply@anthropic.com>

* 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 <noreply@anthropic.com>
This commit is contained in:
Matt Van Horn 2026-03-27 13:41:29 -07:00 • committed by GitHub
parent 79b8910f5d
commit ae32a1b865
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 92 additions and 0 deletions

View file

@ -63,6 +63,7 @@ export enum OnboardingModes {
export interface ListHistoryOptions {
offset?: number;
limit?: number;
workspaceDirectory?: string;
}
export type ToCoreFromIdeOrWebviewProtocol = {

View file

@ -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++) {

View file

@ -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;