Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions server/sessionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2792,13 +2792,18 @@ class SessionManager extends EventEmitter {
if (!session) return [sid];

const ws = String(workspaceId || session?.workspace || '').trim() || null;
const worktreeId = String(session?.worktreeId || '').trim().toLowerCase();
const repositoryName = String(session?.repositoryName || '').trim().toLowerCase();
const parsedWorktreeId = worktreeId || String(sid).replace(/-(claude|codex|server)$/i, '').split('-').pop()?.toLowerCase() || '';
const composedKey = repositoryName && parsedWorktreeId ? `${repositoryName}-${parsedWorktreeId}` : '';
const parsedFromSid = parseWorktreeKey(sid);
const worktreeId = String(session?.worktreeId || '').trim().toLowerCase()
|| String(parsedFromSid?.worktreeId || '').trim().toLowerCase();
const repositoryName = String(session?.repositoryName || '').trim().toLowerCase()
|| String(parsedFromSid?.repositoryName || '').trim().toLowerCase();
const composedKey = repositoryName && worktreeId ? `${repositoryName}-${worktreeId}` : '';

const group = new Set([sid]);
const keys = [composedKey, parsedWorktreeId].filter(Boolean);
// Fall back to the bare worktree id only when the repository is unknown:
// mixed-repo workspaces reuse worktree names like "work1", so a bare-key
// lookup would group (and close) every repo's work1 sessions together.
const keys = [composedKey || worktreeId].filter(Boolean);
for (const key of keys) {
const ids = this.getSessionIdsForWorktree({
workspaceId: ws,
Expand Down
65 changes: 65 additions & 0 deletions tests/unit/sessionManager.closeSession.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,71 @@ describe('SessionManager.closeSession', () => {
expect(ids).toEqual(['zoo-game-work2-claude', 'zoo-game-work2-server']);
});

test('getSessionGroupIds stays scoped to one repo when repos share a worktree name', () => {
const io = { emit: jest.fn() };
const sm = new SessionManager(io, null);

for (const repo of ['sandbox', 'other-repo', 'cat-code-academy']) {
for (const type of ['claude', 'server']) {
sm.sessions.set(`${repo}-work1-${type}`, {
id: `${repo}-work1-${type}`,
type,
workspace: 'ws1',
repositoryName: repo,
worktreeId: 'work1',
pty: null
});
}
}

const ids = sm.getSessionGroupIds('sandbox-work1-claude');
expect(ids).toEqual(['sandbox-work1-claude', 'sandbox-work1-server']);
});

test('getSessionGroupIds derives the repo from the session id when repositoryName is missing', () => {
const io = { emit: jest.fn() };
const sm = new SessionManager(io, null);

for (const repo of ['sandbox', 'other-repo']) {
for (const type of ['claude', 'server']) {
sm.sessions.set(`${repo}-work1-${type}`, {
id: `${repo}-work1-${type}`,
type,
workspace: 'ws1',
repositoryName: '',
worktreeId: 'work1',
pty: null
});
}
}

const ids = sm.getSessionGroupIds('sandbox-work1-claude');
expect(ids).toEqual(['sandbox-work1-claude', 'sandbox-work1-server']);
});

test('getSessionGroupIds does not cross repos that share a worktree number in the same workspace', () => {
const io = { emit: jest.fn() };
const sm = new SessionManager(io, null);

// Mixed-repo workspace where several repos each have a "work1" (and one "work4").
// Closing sandbox's terminal must target ONLY sandbox's sessions — not every
// repo's work1. Previously the group was also looked up by the bare worktree
// number "work1", which matched all of them and closed every tiled terminal.
const seed = (id, repo, wt, type) => sm.sessions.set(id, {
id, type, workspace: 'learning', repositoryName: repo, worktreeId: wt, pty: null
});
seed('sandbox-work1-claude', 'sandbox', 'work1', 'claude');
seed('sandbox-work1-server', 'sandbox', 'work1', 'server');
seed('agent-workspace-work1-claude', 'agent-workspace', 'work1', 'claude');
seed('agent-workspace-work1-server', 'agent-workspace', 'work1', 'server');
seed('USDUC-work1-claude', 'USDUC', 'work1', 'claude');
seed('Cat-Code-Academy-work1-claude', 'Cat-Code-Academy', 'work1', 'claude');
seed('Test-game-work4-claude', 'Test-game', 'work4', 'claude');

const ids = sm.getSessionGroupIds('sandbox-work1-claude');
expect(ids).toEqual(['sandbox-work1-claude', 'sandbox-work1-server']);
});

test('getSessionGroupIds returns original id when session is missing', () => {
const io = { emit: jest.fn() };
const sm = new SessionManager(io, null);
Expand Down
Loading