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(windows): encode the claude cwd transcript dir with the CLI's own sanitizer

review9a4581cf71d0
diff --git a/src/core/transcript-reader.test.ts b/src/core/transcript-reader.test.ts
index 04cc98491..7f4a2875a 100644
--- a/src/core/transcript-reader.test.ts
+++ b/src/core/transcript-reader.test.ts
@@ -5,7 +5,10 @@ import {
pickSessionName,
readSessionName,
setRemoteTranscriptReader,
- TASK_NOTIFIED_RESULT
+ TASK_NOTIFIED_RESULT,
+ encodeTranscriptDir,
+ legacyEncodeTranscriptDir,
+ transcriptPathForCwd
} from './transcript-reader'
import type { TranscriptLine } from '../shared/types'
@@ -1255,3 +1258,76 @@ describe('prompts queued while a turn was running', () => {
expect(lines).toEqual([{ role: 'user', text: 'What is 2+2?' }])
})
})
+
+// The cwd fallback's directory name must be the one Claude Code itself writes, or the fallback
+// silently never hits. The CLI replaces EVERY non-alphanumeric character — the old `[/.]` rule
+// matched it only for paths made of letters, digits, '/' and '.', and never on Windows.
+describe('encodeTranscriptDir — Claude Code\'s own project-dir sanitizer', () => {
+ it('keeps the POSIX answers the old encoding already got right', () => {
+ expect(encodeTranscriptDir('/a/b.c')).toBe('-a-b-c')
+ expect(encodeTranscriptDir('/root/nodeterm')).toBe('-root-nodeterm')
+ })
+
+ it('encodes a Windows cwd the way the CLI does', () => {
+ expect(encodeTranscriptDir('C:\\Users\\me\\proj')).toBe('C--Users-me-proj')
+ })
+
+ it('replaces spaces and underscores too', () => {
+ expect(encodeTranscriptDir('/a/my proj_x')).toBe('-a-my-proj-x')
+ })
+
+ it('truncates past 200 characters with the CLI\'s hash suffix', () => {
+ const long = '/' + 'a'.repeat(250)
+ const enc = encodeTranscriptDir(long)
+ expect(enc.startsWith('-' + 'a'.repeat(199) + '-')).toBe(true)
+ // Expected suffix computed independently: the CLI's `(h<<5)-h+c|0`, Math.abs, base 36.
+ let h = 0
+ for (const ch of long) h = ((h << 5) - h + ch.charCodeAt(0)) | 0
+ expect(enc).toBe(`${('-' + 'a'.repeat(250)).slice(0, 200)}-${Math.abs(h).toString(36)}`)
+ expect(enc).toMatch(/^[A-Za-z0-9-]+$/)
+ })
+
+ it('the legacy encoding is the old `[/.]` rule', () => {
+ expect(legacyEncodeTranscriptDir('/a/my proj_x')).toBe('-a-my proj_x')
+ })
+})
+
+describe('transcriptPathForCwd', () => {
+ let home: string
+ const root = () => path.join(home, '.claude', 'projects')
+ const put = (dir: string, id: string) => {
+ fs.mkdirSync(path.join(root(), dir), { recursive: true })
+ const p = path.join(root(), dir, `${id}.jsonl`)
+ fs.writeFileSync(p, '{}\n')
+ return p
+ }
+
+ beforeEach(() => {
+ home = fs.mkdtempSync(path.join(os.tmpdir(), 'nt-cwd-fallback-'))
+ vi.spyOn(os, 'homedir').mockReturnValue(home)
+ })
+
+ afterEach(() => {
+ vi.restoreAllMocks()
+ fs.rmSync(home, { recursive: true, force: true })
+ })
+
+ it('resolves a Windows-style cwd to the CLI\'s `C--Users-me-proj` directory', async () => {
+ const p = put('C--Users-me-proj', 'sid-1')
+ expect(await transcriptPathForCwd('C:\\Users\\me\\proj')).toBe(p)
+ })
+
+ it('resolves a POSIX cwd with a space', async () => {
+ const p = put('-work-my-proj', 'sid-2')
+ expect(await transcriptPathForCwd('/work/my proj')).toBe(p)
+ })
+
+ it('falls back to the legacy directory when the exact one is absent', async () => {
+ const p = put('-work-my proj', 'sid-3')
+ expect(await transcriptPathForCwd('/work/my proj')).toBe(p)
+ })
+
+ it('answers undefined when neither exists', async () => {
+ expect(await transcriptPathForCwd('/nowhere/x')).toBeUndefined()
+ })
+})
diff --git a/src/core/transcript-reader.ts b/src/core/transcript-reader.ts
index 8a7f8a98e..e3e76984c 100644
--- a/src/core/transcript-reader.ts
+++ b/src/core/transcript-reader.ts
@@ -965,17 +965,40 @@ export async function readSessionName(
}
// Durable resolver by working directory: Claude stores a project's transcripts under
-// ~/.claude/projects/<cwd with every '/' and '.' replaced by '-'>/. We pick the most
-// recently modified .jsonl there — the node's active session. Unlike the sessionId path
+// ~/.claude/projects/<cwd with every non-alphanumeric character replaced by '-'>/. We pick the
+// most recently modified .jsonl there — the node's active session. Unlike the sessionId path
// this needs no live hook event, so the find-bar works even after a reload/restart or when
-// reattaching to a session this app instance didn't spawn. (Encoding leaves no '/', so it
+// reattaching to a session this app instance didn't spawn. (Encoding leaves no separator, so it
// can't traverse.) Limitation: multiple Claude nodes in the SAME cwd resolve to the same
// newest transcript — the sessionId path above is preferred when known for that reason.
+/** Claude Code's own cap on the encoded name; longer ones are truncated and get a hash suffix. */
+const TRANSCRIPT_DIR_MAX = 200
+
+/** The CLI's suffix hash: a 32-bit `h*31 + c` over UTF-16 code units, |abs|, base 36. */
+function transcriptDirHash(s: string): string {
+ let h = 0
+ for (let i = 0; i < s.length; i++) h = ((h << 5) - h + s.charCodeAt(i)) | 0
+ return Math.abs(h).toString(36)
+}
+
/** The per-cwd directory name Claude uses under a transcript root. Exported because the REMOTE
* locator (remote-transcript-locate.ts) must encode a host path the identical way — a second
- * copy that drifted would make the exact-path probe silently never hit. */
+ * copy that drifted would make the exact-path probe silently never hit.
+ *
+ * Mirrors the CLI's sanitizer (read out of Claude Code 2.1.296): EVERY character outside
+ * `[A-Za-z0-9]` becomes '-', so a Windows `C:\Users\me\proj` is `C--Users-me-proj` and a POSIX
+ * `/a/my proj_x` is `-a-my-proj-x`. Names past 200 characters are cut and suffixed with the
+ * CLI's hash. The earlier `[/.]`-only encoding (kept as `legacyEncodeTranscriptDir`) agreed with
+ * this only for paths made of letters, digits, '/' and '.', and never on Windows. */
export function encodeTranscriptDir(cwd: string): string {
+ const enc = cwd.replace(/[^A-Za-z0-9]/g, '-')
+ if (enc.length <= TRANSCRIPT_DIR_MAX) return enc
+ return `${enc.slice(0, TRANSCRIPT_DIR_MAX)}-${transcriptDirHash(cwd)}`
+}
+
+/** The pre-fix encoding, tried only as a second directory when the exact one misses. */
+export function legacyEncodeTranscriptDir(cwd: string): string {
return cwd.replace(/[/.]/g, '-')
}
@@ -984,7 +1007,15 @@ export async function transcriptPathForCwd(
accountId?: string
): Promise<string | undefined> {
if (!cwd) return undefined
- const dir = path.join(transcriptRoot(accountId), encodeTranscriptDir(cwd))
+ const root = transcriptRoot(accountId)
+ const exact = encodeTranscriptDir(cwd)
+ const legacy = legacyEncodeTranscriptDir(cwd)
+ const found = await newestJsonlIn(path.join(root, exact))
+ if (found || legacy === exact) return found
+ return newestJsonlIn(path.join(root, legacy))
+}
+
+async function newestJsonlIn(dir: string): Promise<string | undefined> {
let entries: string[]
try {
entries = await fs.promises.readdir(dir)