fix(sidebar): prevent home-project archived session overlap crash (#2017)

* fix(sidebar): scope archived sessions to deepest project

* fix(sidebar): prefer session directory over worktree

---------

Co-authored-by: bashrusakh <bashrusakh@users.noreply.github.com>
This commit is contained in:
Leonid
2026-07-11 16:03:37 +03:00
committed by GitHub
co-authored by bashrusakh
parent e5bba59a75
commit fbcf4ea2b9
5 changed files with 196 additions and 37 deletions
@@ -1,6 +1,7 @@
import { describe, expect, test } from 'bun:test';
import type { Session } from '@opencode-ai/sdk/v2';
import { isPathWithinProject } from './utils';
import { isPathWithinProject, isSessionRelatedToProject } from './utils';
describe('isPathWithinProject', () => {
test('matches child directories for root projects', () => {
@@ -27,3 +28,86 @@ describe('isPathWithinProject', () => {
expect(isPathWithinProject('/workspace/app/sub/dir', '/workspace/app')).toBe(true);
});
});
describe('isSessionRelatedToProject', () => {
test('prefers the most specific project root for archived session directories', () => {
const session = {
id: 'ses_parent_child',
directory: '/home/user/proj/foo/src',
} as unknown as Session;
const knownProjectDirectories = new Set(['/home/user', '/home/user/proj/foo']);
expect(
isSessionRelatedToProject(session, '/home/user', new Set(['/home/user']), knownProjectDirectories),
).toBe(false);
expect(
isSessionRelatedToProject(
session,
'/home/user/proj/foo',
new Set(['/home/user/proj/foo']),
knownProjectDirectories,
),
).toBe(true);
});
test('prefers the most specific project worktree when session directory is missing', () => {
const session = {
id: 'ses_project_worktree',
project: {
worktree: '/home/user/proj/foo',
},
} as unknown as Session;
const knownProjectDirectories = new Set(['/home/user', '/home/user/proj/foo']);
expect(
isSessionRelatedToProject(session, '/home/user', new Set(['/home/user']), knownProjectDirectories),
).toBe(false);
expect(
isSessionRelatedToProject(
session,
'/home/user/proj/foo',
new Set(['/home/user/proj/foo']),
knownProjectDirectories,
),
).toBe(true);
});
test('prefers explicit session directory over broader project worktree metadata', () => {
const session = {
id: 'ses_directory_beats_worktree',
directory: '/home/user/proj/foo/src',
project: {
worktree: '/home/user',
},
} as unknown as Session;
const knownProjectDirectories = new Set(['/home/user', '/home/user/proj/foo']);
expect(
isSessionRelatedToProject(session, '/home/user', new Set(['/home/user']), knownProjectDirectories),
).toBe(false);
expect(
isSessionRelatedToProject(
session,
'/home/user/proj/foo',
new Set(['/home/user/proj/foo']),
knownProjectDirectories,
),
).toBe(true);
});
test('keeps descendant sessions on the broad project when no child project matches', () => {
const session = {
id: 'ses_home_misc',
directory: '/home/user/misc/sandbox',
} as unknown as Session;
const knownProjectDirectories = new Set(['/home/user', '/home/user/proj/foo']);
expect(
isSessionRelatedToProject(session, '/home/user', new Set(['/home/user']), knownProjectDirectories),
).toBe(true);
});
});