Skip to content

Commit 2d18f96

Browse files
committed
fix(app): resolve a session's project by directory, not by shared ID
A project ID does not identify an opened directory. Project.resolve keys it off the git remote, falling back to a cached ID and then the repo's root commit, so separate clones, sibling worktrees and a directory nested inside another's repo all resolve to the same ID - as does every directory outside a repo, under the global ID. projectForSession looked that ID up first, so every session across those directories resolved to whichever project was opened first: tabs for a second project showed the first project's name and avatar, and the same misattribution reached the command palette and the home session list. The server compounds it by keeping the first directory's worktree on the shared row. Match the worktree or a sandbox path first, since either identifies an opened project on its own, and fall back to the ID only for sessions below a repo root where no path matches exactly. This does not change how project identity is derived (anomalyco#36233); it stops the UI misattributing sessions when two directories collide on one ID.
1 parent 7534d23 commit 2d18f96

2 files changed

Lines changed: 69 additions & 3 deletions

File tree

‎packages/app/src/pages/layout/helpers.test.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
homeProjectDirectories,
1919
homeSessionServerStatus,
2020
latestRootSession,
21+
projectForSession,
2122
toggleHomeProjectSelection,
2223
} from "./helpers"
2324
import { pathKey } from "@/utils/path-key"
@@ -319,3 +320,60 @@ describe("layout workspace helpers", () => {
319320
expect(errorMessage("unknown", "fallback")).toBe("fallback")
320321
})
321322
})
323+
324+
describe("projectForSession", () => {
325+
// Project.resolve keys the ID off the git remote, so directories that share a
326+
// repo identity - separate clones, sibling worktrees, or one nested inside the
327+
// other's repo - all carry the same one.
328+
const projectB = { id: "github.com/me/repo", worktree: "/home/me/projects/beta" }
329+
const projectT = { id: "github.com/me/repo", worktree: "/home/me/projects/tango" }
330+
331+
test("resolves each project by directory when they share a repo identity", () => {
332+
const projects = [projectB, projectT]
333+
const id = "github.com/me/repo"
334+
335+
expect(
336+
projectForSession(session({ id: "ses_b", directory: "/home/me/projects/beta", projectID: id }), projects),
337+
).toBe(projectB)
338+
expect(
339+
projectForSession(session({ id: "ses_t", directory: "/home/me/projects/tango", projectID: id }), projects),
340+
).toBe(projectT)
341+
})
342+
343+
test("resolves each project by directory when they share the global ID", () => {
344+
const outsideA = { id: "global", worktree: "/home/me/notes" }
345+
const outsideB = { id: "global", worktree: "/home/me/scratch" }
346+
const projects = [outsideA, outsideB]
347+
348+
expect(
349+
projectForSession(session({ id: "ses_a", directory: "/home/me/notes", projectID: "global" }), projects),
350+
).toBe(outsideA)
351+
expect(
352+
projectForSession(session({ id: "ses_b", directory: "/home/me/scratch", projectID: "global" }), projects),
353+
).toBe(outsideB)
354+
})
355+
356+
test("falls back to the ID for a session below a repo root", () => {
357+
const repo = { id: "prj_repo", worktree: "/home/me/repo" }
358+
359+
expect(
360+
projectForSession(session({ id: "ses_nested", directory: "/home/me/repo/packages/app", projectID: "prj_repo" }), [
361+
repo,
362+
]),
363+
).toBe(repo)
364+
})
365+
366+
test("matches a sandbox directory", () => {
367+
const project = { id: "prj_one", worktree: "/home/me/repo", sandboxes: ["/home/me/sandbox"] }
368+
369+
expect(
370+
projectForSession(session({ id: "ses_sb", directory: "/home/me/sandbox", projectID: "other" }), [project]),
371+
).toBe(project)
372+
})
373+
374+
test("returns undefined when nothing matches", () => {
375+
expect(
376+
projectForSession(session({ id: "ses_x", directory: "/home/me/elsewhere", projectID: "nope" }), [projectB]),
377+
).toBeUndefined()
378+
})
379+
})

‎packages/app/src/pages/layout/helpers.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,21 @@ export function projectForSession<T extends { id?: string; worktree: string; san
106106
projects: T[],
107107
byID: Map<string, T> = new Map(projects.flatMap((project) => (project.id ? [[project.id, project] as const] : []))),
108108
) {
109-
const direct = byID.get(session.projectID)
110-
if (direct) return direct
109+
// Match the directory before the ID. A project ID does not identify an opened
110+
// directory: Project.resolve keys it off the git remote (falling back to the
111+
// repo's root commit), so separate clones, sibling worktrees, and a directory
112+
// nested inside another's repo all share one — as does everything outside a
113+
// repo, under the global ID. Keying off the ID first collapses every session
114+
// in those directories onto whichever was opened first. A worktree or sandbox
115+
// path identifies an opened project on its own; the ID is only needed for
116+
// sessions below a repo root, where no path matches exactly.
111117
const directory = pathKey(session.directory)
112-
return projects.find(
118+
const byPath = projects.find(
113119
(project) =>
114120
pathKey(project.worktree) === directory || project.sandboxes?.some((sandbox) => pathKey(sandbox) === directory),
115121
)
122+
if (byPath) return byPath
123+
return byID.get(session.projectID)
116124
}
117125

118126
export const errorMessage = (err: unknown, fallback: string) => {

0 commit comments

Comments
 (0)