Skip to content

The agent that grows with you

refactor(desktop): one path-comparison helper instead of two

Brooklyn Nicholsonbd39673b8f8e
diff --git a/apps/desktop/src/app/right-sidebar/files/ipc.ts b/apps/desktop/src/app/right-sidebar/files/ipc.ts
index 29fc4685925..3141b23281b 100644
--- a/apps/desktop/src/app/right-sidebar/files/ipc.ts
+++ b/apps/desktop/src/app/right-sidebar/files/ipc.ts
@@ -3,6 +3,7 @@ import ignore from 'ignore'
import type { HermesReadDirEntry, HermesReadDirResult } from '@/global'
import { desktopFsCacheKey, desktopGitRoot, readDesktopDir, readDesktopFileDataUrl } from '@/lib/desktop-fs'
import { ALWAYS_EXCLUDED } from '@/lib/excluded-paths'
+import { cleanPath, comparisonPath } from '@/lib/path-compare'
export type ProjectTreeEntry = HermesReadDirEntry
@@ -28,21 +29,10 @@ function decodeDataUrl(dataUrl: string) {
return new TextDecoder().decode(bytes)
}
-function clean(path: string) {
- return path.replace(/\\/g, '/').replace(/\/+$/, '') || '/'
-}
-
-// Windows path identity is case-insensitive. Fold only comparison keys so the
-// relative path returned below keeps the filesystem's original spelling;
-// POSIX paths remain case-sensitive.
-function comparisonPath(path: string) {
- return /^[A-Za-z]:(?:\/|$)/.test(path) || path.startsWith('//') ? path.toLowerCase() : path
-}
-
/** Strict POSIX-style relative path; null if `child` is not inside `root`. */
function relativeTo(root: string, child: string) {
- const r = clean(root)
- const c = clean(child)
+ const r = cleanPath(root)
+ const c = cleanPath(child)
const rKey = comparisonPath(r)
const cKey = comparisonPath(c)
@@ -55,7 +45,7 @@ function relativeTo(root: string, child: string) {
/** Repo-root → repo-root/a → repo-root/a/b → … for every dir between root and `dir`. */
function ancestorDirs(root: string, dir: string) {
- const r = clean(root)
+ const r = cleanPath(root)
const rel = relativeTo(r, dir)
if (rel === null || rel === '') {
@@ -74,11 +64,11 @@ function ancestorDirs(root: string, dir: string) {
}
async function gitRootFor(start: string) {
- const key = `${desktopFsCacheKey()}:${clean(start)}`
+ const key = `${desktopFsCacheKey()}:${cleanPath(start)}`
let cached = gitRootCache.get(key)
if (!cached) {
- cached = desktopGitRoot(clean(start))
+ cached = desktopGitRoot(cleanPath(start))
gitRootCache.set(key, cached)
}
@@ -103,11 +93,11 @@ async function readGitignore(dir: string): Promise<GitignoreRule | null> {
}
async function gitignoreFor(dir: string) {
- const key = `${desktopFsCacheKey()}:${clean(dir)}`
+ const key = `${desktopFsCacheKey()}:${cleanPath(dir)}`
let cached = gitignoreCache.get(key)
if (!cached) {
- cached = readGitignore(clean(dir))
+ cached = readGitignore(cleanPath(dir))
gitignoreCache.set(key, cached)
}
@@ -159,7 +149,7 @@ export function clearProjectDirCache(rootPath?: string) {
return
}
- const key = `${desktopFsCacheKey()}:${clean(rootPath)}`
+ const key = `${desktopFsCacheKey()}:${cleanPath(rootPath)}`
gitRootCache.delete(key)
gitignoreCache.delete(key)
}
diff --git a/apps/desktop/src/lib/path-compare.test.ts b/apps/desktop/src/lib/path-compare.test.ts
new file mode 100644
index 00000000000..30e3184f888
--- /dev/null
+++ b/apps/desktop/src/lib/path-compare.test.ts
@@ -0,0 +1,38 @@
+import { describe, expect, it } from 'vitest'
+
+import { cleanPath, comparisonPath, isUnderPath } from './path-compare'
+
+describe('cleanPath', () => {
+ it('unifies separators and drops trailing slashes', () => {
+ expect(cleanPath('C:\\Repos\\App\\')).toBe('C:/Repos/App')
+ expect(cleanPath(' /home/user/repo// ')).toBe('/home/user/repo')
+ })
+
+ it('keeps root rather than collapsing to empty', () => {
+ expect(cleanPath('/')).toBe('/')
+ })
+})
+
+describe('comparisonPath', () => {
+ it('folds case for Windows drive and UNC paths only', () => {
+ expect(comparisonPath('C:/Repos/App')).toBe('c:/repos/app')
+ expect(comparisonPath('//server/Share')).toBe('//server/share')
+ expect(comparisonPath('/home/User/Repo')).toBe('/home/User/Repo')
+ })
+})
+
+describe('isUnderPath', () => {
+ it('matches a nested path across separator and case differences', () => {
+ expect(isUnderPath('C:\\Repos\\App', 'c:/repos/app/src')).toBe(true)
+ expect(isUnderPath('C:/Repos/App/', 'C:\\Repos\\App')).toBe(true)
+ })
+
+ it('stays case-sensitive on POSIX', () => {
+ expect(isUnderPath('/home/user/repo', '/home/user/repo/src')).toBe(true)
+ expect(isUnderPath('/home/user/repo', '/home/user/Repo/src')).toBe(false)
+ })
+
+ it('does not treat a sibling with a shared prefix as nested', () => {
+ expect(isUnderPath('/repos/app', '/repos/app-retry')).toBe(false)
+ })
+})
diff --git a/apps/desktop/src/lib/path-compare.ts b/apps/desktop/src/lib/path-compare.ts
new file mode 100644
index 00000000000..e477a578cfa
--- /dev/null
+++ b/apps/desktop/src/lib/path-compare.ts
@@ -0,0 +1,23 @@
+/** Comparing two paths for identity or containment, across host spellings.
+ *
+ * A cwd reaches us from the backend, a picker, a session row and a project
+ * record, and the four don't agree on separator, trailing slash or drive-letter
+ * case. Compare through these rather than `===` / `startsWith`.
+ */
+
+/** POSIX-style spelling: one separator, no trailing slash. */
+export const cleanPath = (path: string): string => path.trim().replace(/\\/g, '/').replace(/\/+$/, '') || '/'
+
+/** Case-folded comparison key. Windows drive/UNC paths are case-insensitive;
+ * POSIX paths are not, and callers that display a path want its real spelling,
+ * so fold only the key. Expects an already-`cleanPath`ed value. */
+export const comparisonPath = (path: string): string =>
+ /^[A-Za-z]:(?:\/|$)/.test(path) || path.startsWith('//') ? path.toLowerCase() : path
+
+/** True when `child` IS `parent` or lives underneath it. */
+export const isUnderPath = (parent: string, child: string): boolean => {
+ const p = comparisonPath(cleanPath(parent))
+ const c = comparisonPath(cleanPath(child))
+
+ return c === p || c.startsWith(`${p}/`)
+}
diff --git a/apps/desktop/src/store/projects.ts b/apps/desktop/src/store/projects.ts
index 8629fa80a65..5d7a0a222d6 100644
--- a/apps/desktop/src/store/projects.ts
+++ b/apps/desktop/src/store/projects.ts
@@ -11,6 +11,7 @@ import { translateNow } from '@/i18n'
import { desktopDefaultCwd, isDesktopFsRemoteMode, selectDesktopPaths, writeDesktopFileText } from '@/lib/desktop-fs'
import { desktopGit } from '@/lib/desktop-git'
import { isMissingRpcMethod } from '@/lib/gateway-rpc'
+import { isUnderPath } from '@/lib/path-compare'
import { persistentAtom } from '@/lib/persisted'
import { $gateway, activeGateway, ensureActiveGatewayOpen } from '@/store/gateway'
import { setSidebarAgentsGrouped } from '@/store/layout'
@@ -229,27 +230,6 @@ export function resolveNewSessionCwd(): string {
return workspaceCwdForNewSession()
}
-// Comparison-only path spelling: accept either separator everywhere, trim a
-// trailing separator, and fold case for Windows drive/UNC paths. The backend
-// uses the same host-aware identity rule when assigning sessions to projects.
-const comparisonPath = (path: string): string => {
- const raw = path.trim()
- const windows = /^[A-Za-z]:[/\\]/.test(raw) || raw.startsWith('\\') || raw.startsWith('//')
- const normalized = raw.replace(/\\/g, '/').replace(/\/+$/, '')
-
- return windows ? normalized.toLowerCase() : normalized
-}
-
-const underPath = (parent: string, child: string): boolean => {
- const normalizedParent = comparisonPath(parent)
- const normalizedChild = comparisonPath(child)
-
- return (
- normalizedChild === normalizedParent ||
- normalizedChild.startsWith(normalizedParent.endsWith('/') ? normalizedParent : `${normalizedParent}/`)
- )
-}
-
// The project (explicit or auto) that owns `cwd`, by longest path match across
// the live tree. Null when no project covers it (it'll surface as a fresh
// auto-project on the next tree refresh).
@@ -266,7 +246,7 @@ export function projectIdForCwd(cwd: string): null | string {
for (const path of paths) {
const p = (path || '').trim()
- if (p && underPath(p, cwd) && p.length > bestLen) {
+ if (p && isUnderPath(p, cwd) && p.length > bestLen) {
bestLen = p.length
best = project.id
}
@@ -304,7 +284,7 @@ export function projectNameForCwd(cwd: string): null | string {
for (const path of paths) {
const p = (path || '').trim()
- if (p && underPath(p, target) && p.length > bestLen) {
+ if (p && isUnderPath(p, target) && p.length > bestLen) {
bestLen = p.length
best = project.label
}