Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
fix(session): slug every non-alphanumeric cwd character in transcriptPathFor
Jack Rudenko31c908d5a22c
diff --git a/packages/cli/src/session/session-discovery.test.ts b/packages/cli/src/session/session-discovery.test.tsindex f0ec750d..07980837 100644--- a/packages/cli/src/session/session-discovery.test.ts+++ b/packages/cli/src/session/session-discovery.test.ts@@ -2,6 +2,7 @@ import { afterAll, describe, expect, test } from "bun:test";import { mkdirSync, mkdtempSync, rmSync, utimesSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";+import { projectDirNameFor } from "../channel/parent-proof.js";import {ACTIVE_WINDOW_MS,type SessionRow,@@ -61,6 +62,24 @@ describe("session discovery pure helpers", () => {expect(slugForPath("/Users/x/.claude/worktrees/y")).toBe("-Users-x--claude-worktrees-y");});+ test("slugForPath replaces EVERY non-alphanumeric character, as Claude Code does", () => {+ // The directory Claude Code 2.1.287 created for a live run whose cwd was+ // `…/research-scratch/fresh_cwd.v1` (research-transcript-and-launch.md §1.1):+ // the `_` became `-` along with `/` and `.`.+ const cwd =+ "/Users/jack/mag/claudish/.claude/worktrees/claudish-mcp-magmux/ai-docs/sessions/dev-feature-mcp-magmux-panes-20261002-a7c3/research-scratch/fresh_cwd.v1";+ expect(slugForPath(cwd)).toBe(+ "-Users-jack-mag-claudish--claude-worktrees-claudish-mcp-magmux-ai-docs-sessions-dev-feature-mcp-magmux-panes-20261002-a7c3-research-scratch-fresh-cwd-v1"+ );+ expect(slugForPath("/tmp/a b@c+d")).toBe("-tmp-a-b-c-d");+ });++ test("slugForPath is the parent proof's projectDirNameFor, so the two cannot drift", () => {+ for (const p of ["/a/b_c.d", "/x y/@z", "/Users/x/.claude/worktrees/y"]) {+ expect(slugForPath(p)).toBe(projectDirNameFor(p));+ }+ });+test("isActive respects the explicit recency-window boundary", () => {const now = 1_000_000;diff --git a/packages/cli/src/session/session-discovery.ts b/packages/cli/src/session/session-discovery.tsindex 42b15819..7a089707 100644--- a/packages/cli/src/session/session-discovery.ts+++ b/packages/cli/src/session/session-discovery.ts@@ -23,6 +23,7 @@ import { execFile, execFileSync } from "node:child_process";import { closeSync, openSync, readSync, readdirSync, realpathSync, statSync } from "node:fs";import { homedir } from "node:os";import { basename, join } from "node:path";+import { projectDirNameFor } from "../channel/parent-proof.js";/** Head bytes scanned for the `entrypoint` marker. See `isAgentSession`. */const ENTRYPOINT_BYTES = 8192;@@ -31,17 +32,25 @@ const ENTRYPOINT_BYTES = 8192;export const PROJECTS_DIR = join(homedir(), ".claude", "projects");/**- * Claude Code's directory name for a working directory: every `/` and `.` becomes `-`.- * MEASURED against real directories — `/Users/jack/mag/claudish/.claude/worktrees/x`- * becomes `-Users-jack-mag-claudish--claude-worktrees-x`, the doubled dash being the- * `/` and the `.` of `/.claude` in sequence.+ * Claude Code's directory name for a working directory: every character outside+ * `[A-Za-z0-9]` becomes `-`. MEASURED against real directories —+ * `/Users/jack/mag/claudish/.claude/worktrees/x` becomes+ * `-Users-jack-mag-claudish--claude-worktrees-x`, the doubled dash being the `/` and+ * the `.` of `/.claude` in sequence, and a live run in a cwd named `fresh_cwd.v1`+ * created `…-fresh-cwd-v1`: the `_` is replaced too. This used to replace only `/` and+ * `.`, so a cwd with `_`, a space or `@` produced a transcript path that did not exist.+ *+ * ONE rule: this is `projectDirNameFor` (`channel/parent-proof.ts`), the function the+ * parent-conversation proof searches with, so the path claudish derives for a child's+ * transcript and the directory the proof looks in cannot drift apart. Callers pass a+ * realpath (`transcriptPathFor` resolves one; git hands out real paths).** The mapping is deliberately NOT inverted anywhere in this file. It is lossy — a `-`- * in the slug could have been `/`, `.` or a literal `-` — so un-slugging a path would- * guess. Every real path here comes from git or from the caller instead.+ * in the slug could have been any non-alphanumeric character — so un-slugging a path+ * would guess. Every real path here comes from git or from the caller instead.*/export function slugForPath(absPath: string): string {- return absPath.replace(/[/.]/g, "-");+ return projectDirNameFor(absPath);}/**