Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ClaudeHome.ts

90 lines3.7 KB

1import * as NodeOS from "node:os";
2​
3import type { ClaudeSettings } from "@t3tools/contracts";
4import * as Effect from "effect/Effect";
5import * as Path from "effect/Path";
6import * as Schema from "effect/Schema";
7​
8import { expandHomePath } from "../../pathExpansion.ts";
9​
10const quotePath = Schema.encodeSync(Schema.fromJsonString(Schema.String));
11​
12/**
13 * Resolve the Claude config directory the CLI would use: the instance's
14 * `homePath` (exported as `CLAUDE_CONFIG_DIR`), then an inherited
15 * `CLAUDE_CONFIG_DIR`, then Claude's default `~/.claude`. Empty must not
16 * fall back to bare `$HOME` — that leftover from the old HOME override
17 * produced a different continuation group than an explicit `~/.claude`.
18 */
19export const resolveClaudeHomePath = Effect.fn("resolveClaudeHomePath")(function* (
20 config: Pick<ClaudeSettings, "homePath">,
21 environment?: NodeJS.ProcessEnv,
22): Effect.fn.Return<string, never, Path.Path> {
23 const path = yield* Path.Path;
24 const homePath = config.homePath.trim();
25 if (homePath.length > 0) {
26 return path.resolve(expandHomePath(homePath));
27 }
28 // Inherited env vars are not shell-expanded, so a literal `~` stays literal.
29 const inherited = environment?.CLAUDE_CONFIG_DIR?.trim() ?? "";
30 if (inherited.length > 0) {
31 return path.resolve(inherited);
32 }
33 return path.resolve(path.join(NodeOS.homedir(), ".claude"));
34});
35​
36export const makeClaudeEnvironment = Effect.fn("makeClaudeEnvironment")(function* (
37 config: Pick<ClaudeSettings, "homePath">,
38 baseEnv?: NodeJS.ProcessEnv,
39): Effect.fn.Return<NodeJS.ProcessEnv, never, Path.Path> {
40 const resolvedBaseEnv = baseEnv ?? process.env;
41 const homePath = config.homePath.trim();
42 if (homePath.length === 0) return resolvedBaseEnv;
43 const resolvedHomePath = yield* resolveClaudeHomePath(config);
44 return {
45 ...resolvedBaseEnv,
46 // Isolate this instance's config via CLAUDE_CONFIG_DIR rather than HOME.
47 // Overriding HOME also relocates the macOS login keychain lookup
48 // ($HOME/Library/Keychains), so the spawned CLI can't find its stored
49 // OAuth credentials and reports "Not logged in". CLAUDE_CONFIG_DIR points
50 // Claude Code at its config dir directly while leaving HOME (and the
51 // keychain) intact.
52 CLAUDE_CONFIG_DIR: resolvedHomePath,
53 };
54});
55​
56export const makeClaudeContinuationGroupKey = Effect.fn("makeClaudeContinuationGroupKey")(
57 function* (
58 config: Pick<ClaudeSettings, "homePath">,
59 environment?: NodeJS.ProcessEnv,
60 ): Effect.fn.Return<string, never, Path.Path> {
61 const resolvedHomePath = yield* resolveClaudeHomePath(config, environment);
62 return `claude:home:${resolvedHomePath}`;
63 },
64);
65​
66export const makeClaudeCapabilitiesCacheKey = Effect.fn("makeClaudeCapabilitiesCacheKey")(
67 function* (
68 config: Pick<ClaudeSettings, "binaryPath" | "homePath">,
69 cwd?: string,
70 environment?: NodeJS.ProcessEnv,
71 ): Effect.fn.Return<string, never, Path.Path> {
72 const resolvedHomePath = yield* resolveClaudeHomePath(config, environment);
73 return `${config.binaryPath}\0${resolvedHomePath}\0${cwd ?? ""}`;
74 },
75);
76​
77/**
78 * Describe the spawned CLI's environment separately from the login command so
79 * paths remain literal on every shell, including relative inherited values.
80 */
81export const claudeSignedOutMessage = (input: {
82 readonly configDir: string | undefined;
83 readonly cwd: string;
84}): string => {
85 const configuration =
86 input.configDir !== undefined
87 ? ` from ${quotePath(input.cwd)}, with CLAUDE_CONFIG_DIR set to ${quotePath(input.configDir)}`
88 : "";
89 return `Claude could not authenticate. For subscription login, run \`claude auth login\` on this environment's machine${configuration}, then start a new thread. For API-key authentication, check this instance's configured credentials.`;
90};