| 1 | package claude |
| 2 | |
| 3 | import ( |
| 4 | "path/filepath" |
| 5 | "regexp" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | "unicode/utf16" |
| 9 | ) |
| 10 | |
| 11 | // Where the pinned CLI (2.1.312) keeps a session's transcript, read from |
| 12 | // the binary (slice F task 1, S6): $CLAUDE_CONFIG_DIR/projects/<name>/ |
| 13 | // <session>.jsonl, where <name> is CLAUDE_CODE_PROJECT_DIR_NAME when |
| 14 | // CLAUDE_CONFIG_DIR is set and the name is valid, and otherwise is made |
| 15 | // from the cwd's realpath. |
| 16 | // |
| 17 | // A Mac launch pins the name (Paul's ruling via the controller, |
| 18 | // 2026-11-28): with no namespace the cwd is the attempt's own worktree, |
| 19 | // and the pin keeps a kept session resumable in any later worktree, |
| 20 | // exactly where a Linux launch (cwd /workspace) keeps it. |
| 21 | const ( |
| 22 | // ProjectDirEnv is the variable the CLI reads the pinned name from. |
| 23 | ProjectDirEnv = "CLAUDE_CODE_PROJECT_DIR_NAME" |
| 24 | // ProjectDirName is the name a Mac launch pins: what /workspace gives. |
| 25 | ProjectDirName = "-workspace" |
| 26 | ) |
| 27 | |
| 28 | // PinProjectDir sets the pin in a launch environment. A darwin launch's |
| 29 | // certified environment is built with it. |
| 30 | func PinProjectDir(env map[string]string) { |
| 31 | env[ProjectDirEnv] = ProjectDirName |
| 32 | } |
| 33 | |
| 34 | var ( |
| 35 | projectDirNamePattern = regexp.MustCompile(`^[A-Za-z0-9_-]{1,64}$`) |
| 36 | projectDirReserved = regexp.MustCompile(`(?i)^(con|prn|aux|nul|com[0-9]|lpt[0-9])$`) |
| 37 | ) |
| 38 | |
| 39 | // ProjectDir is the directory name under projects the CLI uses for a |
| 40 | // session started in cwd with env. cwd must be what the CLI resolves its |
| 41 | // working directory to (RealCwd of it); a non-ASCII cwd is taken as |
| 42 | // already in NFC, as the CLI normalises it. Without the pin the name is |
| 43 | // the path with every UTF-16 unit outside [A-Za-z0-9] replaced by "-", |
| 44 | // cut at 200 characters with "-" and the base-36 of the absolute Java |
| 45 | // string hash of the path appended. |
| 46 | func ProjectDir(cwd string, env map[string]string) string { |
| 47 | if name := env[ProjectDirEnv]; env["CLAUDE_CONFIG_DIR"] != "" && projectDirNamePattern.MatchString(name) && !projectDirReserved.MatchString(name) { |
| 48 | return name |
| 49 | } |
| 50 | units := utf16.Encode([]rune(cwd)) |
| 51 | var b strings.Builder |
| 52 | var hash int32 |
| 53 | for _, u := range units { |
| 54 | if u >= '0' && u <= '9' || u >= 'A' && u <= 'Z' || u >= 'a' && u <= 'z' { |
| 55 | b.WriteByte(byte(u)) |
| 56 | } else { |
| 57 | b.WriteByte('-') |
| 58 | } |
| 59 | hash = hash*31 + int32(u) |
| 60 | } |
| 61 | name := b.String() |
| 62 | if len(name) <= 200 { |
| 63 | return name |
| 64 | } |
| 65 | abs := int64(hash) |
| 66 | if abs < 0 { |
| 67 | abs = -abs |
| 68 | } |
| 69 | return name[:200] + "-" + strconv.FormatInt(abs, 36) |
| 70 | } |
| 71 | |
| 72 | // RealCwd is cwd as the CLI resolves it: its realpath, or cwd itself |
| 73 | // when that cannot be resolved. |
| 74 | func RealCwd(cwd string) string { |
| 75 | if real, err := filepath.EvalSymlinks(cwd); err == nil { |
| 76 | return real |
| 77 | } |
| 78 | return cwd |
| 79 | } |
| 80 | |