fix(frontend): resolve the deepest owning remote project and refresh VCS info after branch changes

findRemoteProjectForDirectory now returns the longest matching canonical path, so nested linked-server projects attribute sessions to the innermost project. Switching, creating and renaming a local branch invalidate that directory's VCS info query, so branch labels refresh immediately.
This commit is contained in:
Chris Scott committed 2026-10-24 10:37:41 -04:00
1 parent 4b7652451b
commit adb251081f
8 files changed
+128 -15

No files matched your search

+31
View File
@@ -851,6 +851,37 @@ describe('project helpers', () => {
expect(findRemoteProjectForDirectory(projects, '/work/beta/packages/web')?.id).toBe('p2')
})
it('picks the deepest project when one canonical directory contains another', () => {
const projects = [
project('p_parent', 'Repo', '/work/repo', { created: 1, updated: 2 }),
project('p_child', 'App', '/work/repo/packages/app', { created: 1, updated: 3 }),
]
expect(findRemoteProjectForDirectory(projects, '/work/repo')?.id).toBe('p_parent')
expect(findRemoteProjectForDirectory(projects, '/work/repo/packages/app')?.id).toBe('p_child')
expect(findRemoteProjectForDirectory(projects, '/work/repo/packages/app/src')?.id).toBe('p_child')
})
it('credits a session in a nested project to the deepest project', () => {
const projects = [
project('p_parent', 'Repo', '/work/repo', { created: 1, updated: 20 }),
project('p_child', 'App', '/work/repo/packages/app', { created: 1, updated: 10 }),
]
const ranked = rankProjectsBySessionActivity(projects, [
session('ses_nested', '/work/repo/packages/app/src', 400),
])
expect(ranked.find((entry) => entry.project.id === 'p_child')?.activityAt).toBe(400)
expect(ranked.find((entry) => entry.project.id === 'p_parent')?.activityAt).toBe(20)
})
it('returns no project when no canonical directory contains the directory', () => {
const projects = [project('p1', 'Alpha', '/work/alpha', { created: 1, updated: 2 })]
expect(findRemoteProjectForDirectory(projects, '/srv/elsewhere')).toBeUndefined()
})
it('does not match a directory that only shares a prefix with a canonical directory', () => {
const projects = [project('p1', 'Alpha', '/work/alpha', { created: 1, updated: 2 })]
+9 -4
View File
@@ -112,7 +112,7 @@ export function remoteProjectName(project: OpenCodeProject): string {
/**
* Finds the project that owns a directory, matching its canonical directory and anything nested
* inside it.
* inside it. Nested projects share a canonical prefix, so the deepest match owns the directory.
*
* @param projects Projects of the server that owns the directory.
* @param directory Directory whose owning project is resolved.
@@ -122,9 +122,14 @@ export function findRemoteProjectForDirectory(
projects: OpenCodeProject[],
directory: string,
): OpenCodeProject | undefined {
return projects.find(
(project) => directory === project.canonical || directory.startsWith(`${project.canonical}/`),
)
let owner: OpenCodeProject | undefined
for (const project of projects) {
if (directory !== project.canonical && !directory.startsWith(`${project.canonical}/`)) continue
if (!owner || project.canonical.length > owner.canonical.length) {
owner = project
}
}
return owner
}
export type VcsInfo = Awaited<ReturnType<OpenCodeApi['vcs']['get']>>['data']