Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(core): heal a project name that is only its own path, on read

Erik Zwep2eed6b8d86b3
diff --git a/src/core/workspace-files.ts b/src/core/workspace-files.ts
index 277507e28..8abe27cb3 100644
--- a/src/core/workspace-files.ts
+++ b/src/core/workspace-files.ts
@@ -1,4 +1,5 @@
import path from 'path'
+import { healPathAsName } from '../shared/project-name'
import type { AgentPermissionMode } from '../shared/agents/config'
import { collisionSeed, derivedProjectId, legacyFileId } from '../shared/project-id'
import {
@@ -324,7 +325,12 @@ export function fileToProject(
const icon = sanitizeProjectIcon(f.icon)
return {
id: base.id,
- name: f.name,
+ // A project whose stored name IS its own path is one this machine (or a teammate's) created
+ // before the basename fix, when a Windows cwd yielded the whole path instead of the folder.
+ // Heal it on read: a name equal to the cwd carries no information the cwd does not already
+ // carry, so re-deriving it cannot lose a name the user chose. Anything else is left ALONE —
+ // a deliberate rename that happens to look path-ish stays exactly as typed.
+ name: healPathAsName(f.name, base.cwd),
color: f.color,
...(icon ? { icon } : {}),
viewport: base.viewport ?? f.viewport ?? framingViewport(f.nodes),
diff --git a/src/renderer/lib/projectOpen.ts b/src/renderer/lib/projectOpen.ts
index 900b8da18..1361db55a 100644
--- a/src/renderer/lib/projectOpen.ts
+++ b/src/renderer/lib/projectOpen.ts
@@ -30,23 +30,10 @@ export function normalizeProjectCwd(resolvedCwd: string): string {
return stripped === '' || /^[A-Za-z]:$/.test(stripped) ? resolvedCwd : stripped
}
-/**
- * The folder name a path ends in — the ONE basename for project naming, shared by every site that
- * used to inline `split('/')`.
- *
- * That inline version only split on forward slashes, so on Windows a path like
- * `C:\\Users\\me\\code\\my-app` came back as ONE segment: the whole path. Every project
- * there was named after its absolute path, in the sidebar, in the tab bar, and — because `name` is
- * git-shared through `project.json` — for every teammate who pulled that file too.
- *
- * Backslashes are folded to `/` before splitting, the way `session-host/host.ts` and
- * `session-host/session.ts` already do it, so one implementation answers on both platforms. A path
- * that is nothing but separators (or empty) has no folder name and yields `''`; callers keep their
- * own fallback, since `'Project'` is right for a project and wrong for a notification label.
- */
-export function folderName(p: string): string {
- return p.replace(/\\/g, '/').split('/').filter(Boolean).pop() ?? ''
-}
+// `folderName` lives in shared/ so the core-side project loader can use the same rule; it stays
+// exported from here too, because that is where every renderer caller already imports it from.
+import { folderName } from '@shared/project-name'
+export { folderName }
export function findProjectByCwd<T extends { cwd?: string }>(
projects: readonly T[],
diff --git a/src/shared/project-name.test.ts b/src/shared/project-name.test.ts
new file mode 100644
index 000000000..412b3e383
--- /dev/null
+++ b/src/shared/project-name.test.ts
@@ -0,0 +1,65 @@
+import { describe, it, expect } from 'vitest'
+import { folderName, healPathAsName } from './project-name'
+
+describe('folderName', () => {
+ it('takes the last segment of a POSIX path', () => {
+ expect(folderName('/Users/me/code/my-app')).toBe('my-app')
+ expect(folderName('/Users/me/code/my-app/')).toBe('my-app')
+ expect(folderName('/Users/me/code//my-app//')).toBe('my-app')
+ })
+
+ it('takes the last segment of a Windows path', () => {
+ expect(folderName('C:\\Users\\me\\code\\my-app')).toBe('my-app')
+ expect(folderName('\\\\server\\share\\my-app')).toBe('my-app')
+ expect(folderName('C:\\Users\\me/code\\my-app')).toBe('my-app')
+ })
+
+ it('treats a backslash as a plain character in a POSIX path', () => {
+ // `\\` is legal IN a Linux filename, so folding it to a separator would split one folder in two.
+ expect(folderName('/repo/a\\b')).toBe('a\\b')
+ })
+
+ it('answers empty where there is no folder, so callers own their fallback', () => {
+ expect(folderName('')).toBe('')
+ expect(folderName('/')).toBe('')
+ })
+})
+
+describe('healPathAsName', () => {
+ const cwd = 'C:\\Users\\me\\code\\my-app'
+
+ it('replaces a name that is only the path - the upgrade case', () => {
+ // Nothing re-derives a STORED name, so without this every project a user already had keeps the
+ // pre-fix name forever and only a manual rename clears it.
+ expect(healPathAsName(cwd, cwd)).toBe('my-app')
+ expect(healPathAsName('/home/me/code/my-app', '/home/me/code/my-app')).toBe('my-app')
+ })
+
+ it('ignores separator and case differences between the two', () => {
+ expect(healPathAsName('C:/Users/me/code/my-app', cwd)).toBe('my-app')
+ expect(healPathAsName('c:\\users\\me\\code\\my-app\\', cwd)).toBe('my-app')
+ })
+
+ it('is case-SENSITIVE for POSIX paths', () => {
+ // /repo/Foo and /repo/foo can be different directories on Linux, so a name that differs only
+ // in case is a name the user chose, not the path repeated.
+ expect(healPathAsName('/repo/Foo', '/repo/foo')).toBe('/repo/Foo')
+ expect(healPathAsName('/repo/foo', '/repo/foo')).toBe('foo')
+ })
+
+ it('does not treat a backslash in a POSIX path as a separator', () => {
+ expect(healPathAsName('/repo/a\\b', '/repo/a\\b')).toBe('a\\b')
+ })
+
+ it('leaves a name the user chose alone, even a path-ish one', () => {
+ expect(healPathAsName('my-app', cwd)).toBe('my-app')
+ expect(healPathAsName('Client X / my-app', cwd)).toBe('Client X / my-app')
+ // A DIFFERENT path is not this project's own path, so it is a deliberate name.
+ expect(healPathAsName('C:\\Users\\me\\code\\other', cwd)).toBe('C:\\Users\\me\\code\\other')
+ })
+
+ it('is a no-op without a cwd or a name', () => {
+ expect(healPathAsName(cwd, undefined)).toBe(cwd)
+ expect(healPathAsName('', cwd)).toBe('')
+ })
+})
diff --git a/src/shared/project-name.ts b/src/shared/project-name.ts
new file mode 100644
index 000000000..1c8faedb7
--- /dev/null
+++ b/src/shared/project-name.ts
@@ -0,0 +1,67 @@
+/**
+ * One basename for project naming, shared by `src/core` and `src/renderer`.
+ *
+ * It used to be inlined as `split('/')` at five sites, which on Windows returns the WHOLE path as
+ * a single segment — so a project created there was named after its absolute path, in the sidebar,
+ * in the tab bar, and (because `name` is git-shared through `project.json`) for every teammate who
+ * pulled that file too.
+ *
+ * Lives in `shared/` rather than the renderer because the core-side project loader needs the same
+ * rule to heal a name that is only the path — and `src/core` may not import from `src/renderer`.
+ *
+ * A path that is nothing but separators (or empty) has no folder name and yields `''`; callers
+ * keep their own fallback, since `'Project'` is right for a project and wrong for a notification
+ * label.
+ */
+export function folderName(p: string): string {
+ return separatorsToSlash(p).split('/').filter(Boolean).pop() ?? ''
+}
+
+/**
+ * A path SHAPED like Windows: a drive letter, or a UNC share.
+ *
+ * The distinction is load-bearing, not pedantry. On POSIX a backslash is a perfectly legal
+ * character IN a filename, and paths are case-SENSITIVE — so folding either one globally would
+ * mangle a real Linux path (`/repo/a\b` is ONE folder, and `/repo/Foo` is not
+ * `/repo/foo`). Applying those rules only where they hold keeps this correct on both platforms.
+ */
+function looksWindows(p: string): boolean {
+ return /^[A-Za-z]:[\\/]/.test(p) || p.startsWith('\\\\')
+}
+
+/** Backslashes are separators only in a Windows-shaped path; elsewhere they are just characters. */
+function separatorsToSlash(p: string): string {
+ return looksWindows(p) ? p.replace(/\\/g, '/') : p
+}
+
+/**
+ * Is this name simply the path again? Compared the way the platform the path belongs to would:
+ * case-insensitively and slash-agnostically for a Windows path, exactly for a POSIX one, with a
+ * trailing separator never counting as a difference on either.
+ */
+function samePath(a: string, b: string): boolean {
+ const windows = looksWindows(a) || looksWindows(b)
+ const norm = (v: string): string => {
+ const slashed = windows ? v.replace(/\\/g, '/') : v
+ const trimmed = slashed.length > 1 ? slashed.replace(/\/+$/, '') : slashed
+ return windows ? trimmed.toLowerCase() : trimmed
+ }
+ return norm(a) === norm(b)
+}
+
+/**
+ * The stored project name, unless it is nothing but the project's own path.
+ *
+ * A project whose name IS its cwd was created before the basename fix, when a Windows path yielded
+ * the whole thing. Healing it on READ matters because nothing re-derives a stored name: without
+ * this, every project a user already had keeps the bad name forever and only a manual rename fixes
+ * it — which is exactly what an upgrading user reports as "still broken".
+ *
+ * Deliberately narrow: only a name EQUAL to the cwd is replaced, because such a name carries no
+ * information the cwd does not already carry, so re-deriving cannot lose something the user chose.
+ * A deliberate rename that merely looks path-ish — or names a DIFFERENT path — is left as typed.
+ */
+export function healPathAsName(name: string, cwd: string | undefined): string {
+ if (!cwd || !name || !samePath(name, cwd)) return name
+ return folderName(cwd) || name
+}