Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
fix(session): truncate project-dir names over 200 characters with a hash
Jack Rudenko2ec4d8d8cd14
diff --git a/packages/cli/src/channel/parent-proof.ts b/packages/cli/src/channel/parent-proof.tsindex 25fad340..e7ef4d95 100644--- a/packages/cli/src/channel/parent-proof.ts+++ b/packages/cli/src/channel/parent-proof.ts@@ -220,14 +220,31 @@ export async function gatherProofCandidates(opts: {// ─── The proof ───────────────────────────────────────────────────────────────+/** Claude Code keeps a project directory name to this many slug characters. */+export const PROJECT_DIR_SLUG_MAX = 200;++/** Claude Code's 32-bit string hash (`(h << 5) - h + charCode`, per UTF-16 unit). */+function claudeCodeStringHash(s: string): number {+ let h = 0;+ for (let i = 0; i < s.length; i++) h = ((h << 5) - h + s.charCodeAt(i)) | 0;+ return h;+}+/*** Claude Code's project directory name for a cwd: every character outside- * `[A-Za-z0-9]` becomes `-` (so `/.claude/` becomes `--claude-`). Observed- * naming, used ONLY as the fast path: a future rule change degrades to the- * directory listing, never to a wrong answer.+ * `[A-Za-z0-9]` becomes `-` (so `/.claude/` becomes `--claude-`). A slug longer than+ * 200 characters is cut to 200 and suffixed `-<base36 |hash(cwd)|>`, the hash taken+ * over the WHOLE path, not the slug. Read from Claude Code 2.1.291's own bundle+ * (`k(e)=e.replace(/[^a-zA-Z0-9]/g,"-")`, `eI(e)` truncates at `Rle=200` and appends+ * `Le(e)=Math.abs(hash(e)).toString(36)`) and MEASURED: a live 2.1.291 run in a+ * 259-character cwd wrote `<200 slug chars>-g32rlu`, which this function reproduces.+ * Used as the fast path of the proof (a future rule change degrades to the directory+ * listing) and, through `slugForPath`, for every transcript path claudish derives.*/export function projectDirNameFor(cwd: string): string {- return cwd.replace(/[^A-Za-z0-9]/g, "-");+ const slug = cwd.replace(/[^A-Za-z0-9]/g, "-");+ if (slug.length <= PROJECT_DIR_SLUG_MAX) return slug;+ return `${slug.slice(0, PROJECT_DIR_SLUG_MAX)}-${Math.abs(claudeCodeStringHash(cwd)).toString(36)}`;}/**diff --git a/packages/cli/src/pane/test-helpers/fake-interactive-child.ts b/packages/cli/src/pane/test-helpers/fake-interactive-child.tsindex 13c83447..8e852be7 100755--- a/packages/cli/src/pane/test-helpers/fake-interactive-child.ts+++ b/packages/cli/src/pane/test-helpers/fake-interactive-child.ts@@ -34,6 +34,7 @@ import { spawn } from "node:child_process";import { createHash, randomUUID } from "node:crypto";import { appendFileSync, mkdirSync, readFileSync, realpathSync, writeFileSync } from "node:fs";import { dirname, join } from "node:path";+import { projectDirNameFor } from "../../channel/parent-proof.js";/* ───────────────────────────── argv and env ───────────────────────────── */@@ -128,12 +129,7 @@ const REFUSAL_SLICE = lines("corpus-redacted/refusal-then-fallback").slice(30, 3/* ───────────────────────────── transcript writer ───────────────────────────── */const configDir = env.CLAUDE_CONFIG_DIR || join(env.HOME ?? "/tmp", ".claude");-const transcript = join(- configDir,- "projects",- cwd.replace(/[^a-zA-Z0-9]/g, "-"),- `${sessionId}.jsonl`-);+const transcript = join(configDir, "projects", projectDirNameFor(cwd), `${sessionId}.jsonl`);let parent: string | null = null;let msgCounter = 0;const writeTranscript = ![diff --git a/packages/cli/src/pane/test-helpers/hermetic-env.ts b/packages/cli/src/pane/test-helpers/hermetic-env.tsindex ae7c70de..35a6d65c 100644--- a/packages/cli/src/pane/test-helpers/hermetic-env.ts+++ b/packages/cli/src/pane/test-helpers/hermetic-env.ts@@ -25,6 +25,7 @@ import {} from "node:fs";import { homedir, tmpdir } from "node:os";import { join } from "node:path";+import { projectDirNameFor } from "../../channel/parent-proof.js";import { findMagmuxBinaryOrNull } from "../../launcher/magmux-binary.js";import { readProcessTable } from "../process-identity.js";@@ -82,7 +83,7 @@ export function makePaneTestEnv(extra: Record<string, string> = {}): PaneTestEnvsockRoot,env,transcriptPathFor: (uuid) =>- join(configDir, "projects", realCwd.replace(/[^a-zA-Z0-9]/g, "-"), `${uuid}.jsonl`),+ join(configDir, "projects", projectDirNameFor(realCwd), `${uuid}.jsonl`),cleanup() {rmSync(tmp, { recursive: true, force: true });rmSync(sockRoot, { recursive: true, force: true });diff --git a/packages/cli/src/session-events/index.ts b/packages/cli/src/session-events/index.tsindex e366495d..2986a450 100644--- a/packages/cli/src/session-events/index.ts+++ b/packages/cli/src/session-events/index.ts@@ -14,6 +14,7 @@import { existsSync, readFileSync, readdirSync } from "node:fs";import { homedir } from "node:os";import { join } from "node:path";+import { projectDirNameFor } from "../channel/parent-proof.js";import { log } from "../logger.js";import { translateLine } from "./event-translator.js";import { initialState, reduceEvent } from "./session-state.js";@@ -69,9 +70,9 @@ export function extractSessionId(metadata: unknown): string | undefined {return match?.[1];}-/** Claude Code's project-dir slug: every non-alphanumeric char becomes "-". */+/** Claude Code's project-dir name for a cwd: the one rule, `projectDirNameFor`. */export function slugFromCwd(cwd: string): string {- return cwd.replace(/[^a-zA-Z0-9]/g, "-");+ return projectDirNameFor(cwd);}export class SessionEventRegistry {diff --git a/packages/cli/src/session/session-discovery.test.ts b/packages/cli/src/session/session-discovery.test.tsindex 8d72f907..ee8502fb 100644--- a/packages/cli/src/session/session-discovery.test.ts+++ b/packages/cli/src/session/session-discovery.test.ts@@ -106,6 +106,27 @@ describe("session discovery pure helpers", () => {}});+ test("a slug over 200 characters is cut and hashed exactly as Claude Code 2.1.291 named it", () => {+ // A live 2.1.291 run (code-review iteration 1, phase2/runs/cr1-longcwd) in this+ // 259-character cwd wrote its transcript under the directory named below.+ const cwd = `/private/tmp/cc2/cr1-longcwd/cwd/${[+ "a".repeat(60),+ "b-b.b_b".repeat(9),+ "c".repeat(60),+ "dé ü".repeat(10),+ ].join("/")}`;+ const measured =+ "-private-tmp-cc2-cr1-longcwd-cwd-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-b-b-b-bb-b-b-bb-b-b-bb-b-b-bb-b-b-bb-b-b-bb-b-b-bb-b-b-bb-b-b-b-cccccccccccccccccccccccccccccccccccccccccc-g32rlu";+ expect(slugForPath(cwd)).toBe(measured);+ expect(transcriptPathFor(cwd, "u-1", "/cfg/projects")).toBe(+ `/cfg/projects/${measured}/u-1.jsonl`+ );+ // exactly 200 slug characters stay as they are+ const at200 = `/${"x".repeat(199)}`;+ expect(slugForPath(at200)).toBe(`-${"x".repeat(199)}`);+ expect(slugForPath(`${at200}y`)).toMatch(/^-x{199}-[0-9a-z]+$/);+ });+test("isActive respects the explicit recency-window boundary", () => {const now = 1_000_000;