|
1 | 1 | import { existsSync, readFileSync, readdirSync, realpathSync, statSync } from "node:fs"; |
2 | 2 | import { homedir } from "node:os"; |
3 | | -import { join, posix, resolve } from "node:path"; |
| 3 | +import { join, posix, resolve, win32 } from "node:path"; |
4 | 4 | import { expandUserPath } from "../config"; |
5 | 5 |
|
6 | 6 | export type CodexHomeDeps = { |
@@ -144,3 +144,59 @@ export function resolveCodexHomeDir(deps: CodexHomeDeps = {}): string { |
144 | 144 | if (raw) return resolve(expandUserPath(raw)); |
145 | 145 | return defaultCodexHome(deps); |
146 | 146 | } |
| 147 | + |
| 148 | +export type OrcaCodexHomeDiagnostic = { |
| 149 | + applicable: boolean; |
| 150 | + mismatch: boolean; |
| 151 | + effectiveCodexHome: string; |
| 152 | + appCodexHome: string; |
| 153 | + orcaCodexHome: string | null; |
| 154 | + warning: string | null; |
| 155 | + action: string | null; |
| 156 | +}; |
| 157 | + |
| 158 | +type OrcaCodexHomeDeps = CodexHomeDeps & { |
| 159 | + effectiveCodexHome?: string; |
| 160 | + appCodexHome?: string; |
| 161 | +}; |
| 162 | + |
| 163 | +function normalizedWindowsPath(path: string): string { |
| 164 | + return path.trim().replaceAll("/", "\\").replace(/\\+$/, "").toLowerCase(); |
| 165 | +} |
| 166 | + |
| 167 | +/** |
| 168 | + * High-confidence Orca/ChatGPT dual-home diagnosis. Explicit CODEX_HOME remains |
| 169 | + * authoritative; this only explains when an Orca-owned shell targets a home the |
| 170 | + * Windows ChatGPT/Codex app does not read. |
| 171 | + */ |
| 172 | +export function collectOrcaCodexHomeDiagnostic(deps: OrcaCodexHomeDeps = {}): OrcaCodexHomeDiagnostic { |
| 173 | + const platform = deps.platform ?? process.platform; |
| 174 | + const env = deps.env ?? process.env; |
| 175 | + const effectiveCodexHome = deps.effectiveCodexHome ?? resolveCodexHomeDir(deps); |
| 176 | + const appCodexHome = deps.appCodexHome |
| 177 | + ?? (platform === "win32" ? win32.join((deps.homedir ?? homedir)(), ".codex") : join((deps.homedir ?? homedir)(), ".codex")); |
| 178 | + const explicitHome = env.CODEX_HOME?.trim() ?? ""; |
| 179 | + const orcaCodexHome = env.ORCA_CODEX_HOME?.trim() || null; |
| 180 | + const normalizedEffective = normalizedWindowsPath(effectiveCodexHome); |
| 181 | + const normalizedOrca = orcaCodexHome ? normalizedWindowsPath(orcaCodexHome) : ""; |
| 182 | + const normalizedApp = normalizedWindowsPath(appCodexHome); |
| 183 | + const applicable = platform === "win32" |
| 184 | + && !!explicitHome |
| 185 | + && !!orcaCodexHome |
| 186 | + && normalizedEffective === normalizedOrca |
| 187 | + && /(?:^|\\)orca\\codex-runtime-home\\home$/i.test(normalizedOrca); |
| 188 | + const mismatch = applicable && normalizedEffective !== normalizedApp; |
| 189 | + return { |
| 190 | + applicable, |
| 191 | + mismatch, |
| 192 | + effectiveCodexHome, |
| 193 | + appCodexHome, |
| 194 | + orcaCodexHome, |
| 195 | + warning: mismatch |
| 196 | + ? `CODEX_HOME targets Orca's runtime home (${effectiveCodexHome}), while the Windows ChatGPT/Codex app uses ${appCodexHome}; OpenCodex injection will not reach that app.` |
| 197 | + : null, |
| 198 | + action: mismatch |
| 199 | + ? `Run ocx for the app with CODEX_HOME=${appCodexHome} and ORCA_CODEX_HOME unset.` |
| 200 | + : null, |
| 201 | + }; |
| 202 | +} |
0 commit comments