Open History to see the last commit
1import type { ClaudeSettings, ProviderInstanceEnvironment } from "@t3tools/contracts";
2import * as Effect from "effect/Effect";
3import * as FileSystem from "effect/FileSystem";
4import * as Path from "effect/Path";
5import * as Schema from "effect/Schema";
6
7import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment";
8import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion";
9import * as HostProcess from "@t3tools/shared/HostProcess";
10
11import { readClaudeSettingsEnv } from "./ClaudeSkills.ts";
12
13const quotePath = Schema.encodeSync(Schema.fromJsonString(Schema.String));
14
15export const resolveClaudeHomePath = Effect.fn("resolveClaudeHomePath")(function* (
16 config: Pick<ClaudeSettings, "homePath">,
17 environment?: NodeJS.ProcessEnv,
18): Effect.fn.Return<string, never, Path.Path> {
19 const path = yield* Path.Path;
20 const home = yield* HostProcess.HomeDirectory;
21 const homePath = config.homePath.trim();
22 if (homePath.length > 0) {
23 return path.resolve(expandHomePath(homePath, home));
24 }
25 // Inherited env vars are not shell-expanded, so a literal `~` stays literal.
26 const inherited = environment?.CLAUDE_CONFIG_DIR?.trim() ?? "";
27 if (inherited.length > 0) {
28 return path.resolve(inherited);
29 }
30 return path.resolve(path.join(home, ".claude"));
31});
32
33export const makeClaudeEnvironment = Effect.fn("makeClaudeEnvironment")(function* (
34 config: Pick<ClaudeSettings, "homePath">,
35 baseEnv?: NodeJS.ProcessEnv,
36): Effect.fn.Return<NodeJS.ProcessEnv, never, Path.Path> {
37 const resolvedBaseEnv = baseEnv ?? process.env;
38 const homePath = config.homePath.trim();
39 if (homePath.length === 0) return resolvedBaseEnv;
40 const resolvedHomePath = yield* resolveClaudeHomePath(config);
41 return {
42 ...resolvedBaseEnv,
43 // Changing HOME hides macOS Keychain credentials; use CLAUDE_CONFIG_DIR to keep login access.
44 CLAUDE_CONFIG_DIR: resolvedHomePath,
45 };
46});
47
48// A Claude Code session that launched T3 exports these. A claude spawned with them acts as part of
49// that session: its session id, messaging socket and token, and effort.
50const CLAUDE_HOST_SESSION_VARIABLES = [
51 "CLAUDECODE",
52 "CLAUDE_CODE_SESSION_ID",
53 "CLAUDE_CODE_HOST_SESSION_ID",
54 "CLAUDE_CODE_CHILD_SESSION",
55 "CLAUDE_CODE_SESSION_ATTENDED",
56 "CLAUDE_CODE_MESSAGING_SOCKET",
57 "CLAUDE_CODE_MESSAGING_TOKEN",
58 "CLAUDE_CODE_EXECPATH",
59 "CLAUDE_CODE_DESKTOP_APP_VERSION",
60 "CLAUDE_EFFORT",
61 "CLAUDE_PID",
62 "CLAUDE_AGENT_SDK_VERSION",
63];
64
65/**
66 * Base env for the Claude processes T3 Code runs, before `makeClaudeEnvironment`.
67 * Claude Code reads its entrypoint from the process env, not its settings `env`, so T3 Code applies that override itself.
68 */
69export const mergeClaudeProcessEnvironment = Effect.fn("mergeClaudeProcessEnvironment")(function* (
70 config: Pick<ClaudeSettings, "homePath">,
71 instanceEnvironment: ProviderInstanceEnvironment | undefined,
72 hostEnvironment: NodeJS.ProcessEnv,
73) {
74 const baseEnvironment: NodeJS.ProcessEnv = {
75 ...hostEnvironment,
76 CLAUDE_CODE_ENTRYPOINT: "t3-code",
77 };
78 for (const name of CLAUDE_HOST_SESSION_VARIABLES) delete baseEnvironment[name];
79 const environment = yield* mergeProviderInstanceEnvironment(instanceEnvironment, baseEnvironment);
80 const settingsEntrypoint = (yield* readClaudeSettingsEnv(config, undefined, environment))
81 .CLAUDE_CODE_ENTRYPOINT;
82 return settingsEntrypoint
83 ? { ...environment, CLAUDE_CODE_ENTRYPOINT: settingsEntrypoint }
84 : environment;
85});
86
87/** managed-mcp.json excludes other MCP config and forbids --strict-mcp-config; omit the flag when present. */
88export const hasClaudeManagedMcpConfig = Effect.fn("hasClaudeManagedMcpConfig")(function* () {
89 const fileSystem = yield* FileSystem.FileSystem;
90 const platform = yield* HostProcess.Platform;
91 const managedMcpConfigPath =
92 platform === "darwin"
93 ? "/Library/Application Support/ClaudeCode/managed-mcp.json"
94 : platform === "win32"
95 ? "C:\\Program Files\\ClaudeCode\\managed-mcp.json"
96 : "/etc/claude-code/managed-mcp.json";
97 return yield* fileSystem.exists(managedMcpConfigPath).pipe(Effect.orElseSucceed(() => false));
98});
99
100export const makeClaudeContinuationGroupKey = Effect.fn("makeClaudeContinuationGroupKey")(
101 function* (
102 config: Pick<ClaudeSettings, "homePath">,
103 environment?: NodeJS.ProcessEnv,
104 ): Effect.fn.Return<string, never, Path.Path> {
105 const resolvedHomePath = yield* resolveClaudeHomePath(config, environment);
106 return `claude:home:${resolvedHomePath}`;
107 },
108);
109
110export const makeClaudeCapabilitiesCacheKey = Effect.fn("makeClaudeCapabilitiesCacheKey")(
111 function* (
112 config: Pick<ClaudeSettings, "binaryPath" | "homePath">,
113 cwd?: string,
114 environment?: NodeJS.ProcessEnv,
115 ): Effect.fn.Return<string, never, Path.Path> {
116 const resolvedHomePath = yield* resolveClaudeHomePath(config, environment);
117 return `${config.binaryPath}\0${resolvedHomePath}\0${cwd ?? ""}`;
118 },
119);
120
121/** Separate spawn env from login commands to preserve literal paths, including relative inherited values. */
122export const claudeSignedOutMessage = (input: {
123 readonly configDir: string | undefined;
124 readonly cwd: string;
125}): string => {
126 const configuration =
127 input.configDir !== undefined
128 ? ` from ${quotePath(input.cwd)}, with CLAUDE_CONFIG_DIR set to ${quotePath(input.configDir)}`
129 : "";
130 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.`;
131};