|
1 | 1 | import { execFileSync } from "node:child_process"; |
2 | | -import { readFileSync, writeFileSync, unlinkSync, mkdirSync } from "node:fs"; |
3 | | -import { join } from "node:path"; |
| 2 | +import { accessSync, constants, readFileSync, writeFileSync, unlinkSync, mkdirSync, statSync } from "node:fs"; |
| 3 | +import { delimiter, join } from "node:path"; |
4 | 4 | import { getConfigDir } from "../config"; |
5 | 5 | import { resolveAutoContext, type AutoContextMode } from "../claude/context-windows"; |
6 | 6 | import { PROXY_MARKER, defaultAuthDetectDeps, detectClaudeAuth, ownAdmissionTokens } from "../claude/auth-detect"; |
@@ -122,11 +122,72 @@ export function uninstallShellHook(): { removed: boolean; reason?: string } { |
122 | 122 | const cleaned = content.replace(/\n?# opencodex claude-env hook\n\[.*claude-env\.sh.*\n?/g, "\n"); |
123 | 123 | writeFileSync(zshrcPath, cleaned, { encoding: "utf8", mode: 0o644 }); |
124 | 124 | return { removed: true }; |
125 | | - } catch { |
| 125 | + } catch (error) { |
| 126 | + if (error && typeof error === "object" && (error as { code?: unknown }).code === "ENOENT") { |
| 127 | + return { removed: false, reason: "not installed" }; |
| 128 | + } |
126 | 129 | return { removed: false, reason: "read/write failed" }; |
127 | 130 | } |
128 | 131 | } |
129 | 132 |
|
| 133 | +/** Whether a real `claude` executable is discoverable from this process's PATH. */ |
| 134 | +export function claudeCodeCliInstalled(pathValue = process.env.PATH): boolean { |
| 135 | + if (!pathValue) return false; |
| 136 | + for (const directory of pathValue.split(delimiter)) { |
| 137 | + // An empty PATH segment means the current directory. Do not let the proxy treat a |
| 138 | + // workspace-local file as a durable user installation. |
| 139 | + if (!directory) continue; |
| 140 | + const candidate = join(directory, "claude"); |
| 141 | + try { |
| 142 | + if (!statSync(candidate).isFile()) continue; |
| 143 | + accessSync(candidate, constants.X_OK); |
| 144 | + return true; |
| 145 | + } catch { |
| 146 | + // Keep scanning PATH after missing, non-file, and non-executable entries. |
| 147 | + } |
| 148 | + } |
| 149 | + return false; |
| 150 | +} |
| 151 | + |
| 152 | +/** |
| 153 | + * Keep the shell hook aligned with the integration that can actually consume it. |
| 154 | + * Claude Desktop uses its own profile and does not source `.zshrc`; this hook exists |
| 155 | + * only for plain Claude Code CLI launches. |
| 156 | + * |
| 157 | + * Reconciliation is PATH-sensitive by construction: "Claude Code is installed" is answered |
| 158 | + * from the PATH of whichever process calls this. A launchd/service context with a stripped |
| 159 | + * PATH can therefore fail to see a `claude` the user's interactive shell finds, and this will |
| 160 | + * remove the hook. That is the intended failure direction — removing an OpenCodex-owned block |
| 161 | + * is reversible on the next foreground `ocx start`, whereas leaving a hook pointing at an |
| 162 | + * uninstalled CLI is the stale state this reconciliation exists to clear. Only the block |
| 163 | + * carrying our own marker is ever touched; user lines are preserved. |
| 164 | + */ |
| 165 | +export function reconcileShellHook(systemEnvInjected: boolean): { |
| 166 | + changed: boolean; |
| 167 | + state: "installed" | "absent" | "failed"; |
| 168 | + reason?: string; |
| 169 | +} { |
| 170 | + if (process.platform !== "darwin") return { changed: false, state: "absent", reason: "not macOS" }; |
| 171 | + if (systemEnvInjected && claudeCodeCliInstalled()) { |
| 172 | + const result = installShellHook(); |
| 173 | + if (result.installed) return { changed: true, state: "installed" }; |
| 174 | + if (result.reason === "already installed") { |
| 175 | + return { changed: false, state: "installed", reason: result.reason }; |
| 176 | + } |
| 177 | + return { changed: false, state: "failed", reason: result.reason ?? "install failed" }; |
| 178 | + } |
| 179 | + |
| 180 | + const result = uninstallShellHook(); |
| 181 | + if (!result.removed && result.reason !== "not installed") { |
| 182 | + return { changed: false, state: "failed", reason: result.reason ?? "remove failed" }; |
| 183 | + } |
| 184 | + return { |
| 185 | + changed: result.removed, |
| 186 | + state: "absent", |
| 187 | + reason: systemEnvInjected ? "Claude Code not installed" : "system environment inactive", |
| 188 | + }; |
| 189 | +} |
| 190 | + |
130 | 191 | const SYSTEM_ENV_NAMES = [ |
131 | 192 | "ANTHROPIC_BASE_URL", |
132 | 193 | "CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY", |
|
0 commit comments