package claude import ( "path/filepath" "regexp" "strconv" "strings" "unicode/utf16" ) // Where the pinned CLI (2.1.312) keeps a session's transcript, read from // the binary (slice F task 1, S6): $CLAUDE_CONFIG_DIR/projects// // .jsonl, where is CLAUDE_CODE_PROJECT_DIR_NAME when // CLAUDE_CONFIG_DIR is set and the name is valid, and otherwise is made // from the cwd's realpath. // // A Mac launch pins the name (Paul's ruling via the controller, // 2026-11-28): with no namespace the cwd is the attempt's own worktree, // and the pin keeps a kept session resumable in any later worktree, // exactly where a Linux launch (cwd /workspace) keeps it. const ( // ProjectDirEnv is the variable the CLI reads the pinned name from. ProjectDirEnv = "CLAUDE_CODE_PROJECT_DIR_NAME" // ProjectDirName is the name a Mac launch pins: what /workspace gives. ProjectDirName = "-workspace" ) // PinProjectDir sets the pin in a launch environment. A darwin launch's // certified environment is built with it. func PinProjectDir(env map[string]string) { env[ProjectDirEnv] = ProjectDirName } var ( projectDirNamePattern = regexp.MustCompile(`^[A-Za-z0-9_-]{1,64}$`) projectDirReserved = regexp.MustCompile(`(?i)^(con|prn|aux|nul|com[0-9]|lpt[0-9])$`) ) // ProjectDir is the directory name under projects the CLI uses for a // session started in cwd with env. cwd must be what the CLI resolves its // working directory to (RealCwd of it); a non-ASCII cwd is taken as // already in NFC, as the CLI normalises it. Without the pin the name is // the path with every UTF-16 unit outside [A-Za-z0-9] replaced by "-", // cut at 200 characters with "-" and the base-36 of the absolute Java // string hash of the path appended. func ProjectDir(cwd string, env map[string]string) string { if name := env[ProjectDirEnv]; env["CLAUDE_CONFIG_DIR"] != "" && projectDirNamePattern.MatchString(name) && !projectDirReserved.MatchString(name) { return name } units := utf16.Encode([]rune(cwd)) var b strings.Builder var hash int32 for _, u := range units { if u >= '0' && u <= '9' || u >= 'A' && u <= 'Z' || u >= 'a' && u <= 'z' { b.WriteByte(byte(u)) } else { b.WriteByte('-') } hash = hash*31 + int32(u) } name := b.String() if len(name) <= 200 { return name } abs := int64(hash) if abs < 0 { abs = -abs } return name[:200] + "-" + strconv.FormatInt(abs, 36) } // RealCwd is cwd as the CLI resolves it: its realpath, or cwd itself // when that cannot be resolved. func RealCwd(cwd string) string { if real, err := filepath.EvalSymlinks(cwd); err == nil { return real } return cwd }