fix(#4197): dedup the update-context fast path against the selected global dir (#4413)

* fix(#4197): dedup the update-context fast path against the selected global candidate

The preferredConfigDir fast path derived scope from a cwd-relative match
alone, so a global install reported LOCAL whenever the shell sat in
$HOME — and run_update then drove the installer through its --local arm
(settings.local.json + the #338 relocation) against a global install.

Extract resolveGlobalCandidate (env candidates first, then $HOME-relative,
first hasInstall hit wins) and use it in BOTH paths: the fast path now
answers LOCAL only for a cwd-relative match that is not the selected
global dir, which is the same dedup the cascade applies at its isLocal
check. A preferred dir that is also the env-directed global now answers
GLOBAL on both paths (the cascade's answer), pinned by a parity test.

The discriminator is the selected global candidate, not the $HOME
pathname: with CLAUDE_CONFIG_DIR directing the global elsewhere,
$HOME/.claude probed from cwd === $HOME is a genuine local install, and
a pathname check would re-break parity (regression-pinned).

* chore(#4197): add changeset

* chore(#4197): backfill PR number in changeset

---------

Co-authored-by: agent-4197 <agent-4197@gsd.local>
This commit is contained in:
Tom Boucher
2026-09-06 14:09:32 -04:00
committed by GitHub
parent ed133cc116
commit acb3cc974b
3 changed files with 133 additions and 14 deletions

View File

@@ -130,6 +130,27 @@ function preferFirst(entries: RuntimeDirEntry[], preferred: string): RuntimeDirE
return [...pref, ...rest];
}
// GLOBAL probe: absolute env candidates first (in preferFirst order, first
// hasInstall hit wins), then $HOME-relative. Single resolver shared by the
// preferredConfigDir fast path's same-path dedup and the full cascade (#4197),
// so both compare against the global dir the resolution would actually select —
// an env-directed candidate, not necessarily the $HOME-relative pathname.
function resolveGlobalCandidate(
fs: FsAdapter,
env: Record<string, string | undefined>,
home: string,
preferred: string,
): { runtime: string; dir: string } {
for (const [rt, absdir] of preferFirst(envRuntimeDirs({ env, home }), preferred)) {
if (hasInstall(fs, absdir)) return { runtime: rt, dir: path.resolve(absdir) };
}
for (const [rt, reldir] of preferFirst(RUNTIME_DIRS, preferred)) {
const cand = path.resolve(home, reldir);
if (hasInstall(fs, cand)) return { runtime: rt, dir: cand };
}
return { runtime: '', dir: '' };
}
export interface ResolveUpdateContextOpts {
home: string;
cwd: string;
@@ -164,9 +185,15 @@ export function resolveUpdateContext({
// Fast path: a validated preferredConfigDir (custom --config-dir install).
if (preferredConfigDir && hasInstall(fs, preferredConfigDir)) {
const resolvedPref = path.resolve(preferredConfigDir);
// Same-path dedup the cascade applies (#4197): a preferred dir that IS the
// selected global install (an env candidate or the $HOME-relative dir) is
// GLOBAL even when cwd === $HOME also makes it the cwd-relative match.
const { dir: globalDir } = resolveGlobalCandidate(fs, env, home, preferred);
let scope: 'LOCAL' | 'GLOBAL' = 'GLOBAL';
for (const [, reldir] of RUNTIME_DIRS) {
if (path.resolve(cwd, reldir) === resolvedPref) { scope = 'LOCAL'; break; }
if (resolvedPref !== globalDir) {
for (const [, reldir] of RUNTIME_DIRS) {
if (path.resolve(cwd, reldir) === resolvedPref) { scope = 'LOCAL'; break; }
}
}
return {
installedVersion: trustedVersionAt(fs, preferredConfigDir) ?? '0.0.0',
@@ -176,7 +203,6 @@ export function resolveUpdateContext({
};
}
const orderedEnv = preferFirst(envRuntimeDirs({ env, home }), preferred);
const orderedRuntime = preferFirst(RUNTIME_DIRS, preferred);
// LOCAL probe (relative to cwd).
@@ -186,17 +212,9 @@ export function resolveUpdateContext({
if (hasInstall(fs, cand)) { localRuntime = rt; localDir = cand; break; }
}
// GLOBAL probe: absolute env candidates first, then $HOME-relative.
let globalRuntime = '', globalDir = '';
for (const [rt, absdir] of orderedEnv) {
if (hasInstall(fs, absdir)) { globalRuntime = rt; globalDir = path.resolve(absdir); break; }
}
if (!globalRuntime) {
for (const [rt, reldir] of orderedRuntime) {
const cand = path.resolve(home, reldir);
if (hasInstall(fs, cand)) { globalRuntime = rt; globalDir = cand; break; }
}
}
// GLOBAL probe: absolute env candidates first, then $HOME-relative — the
// same resolver the fast path dedups against.
const { runtime: globalRuntime, dir: globalDir } = resolveGlobalCandidate(fs, env, home, preferred);
const localValid = trustedVersionAt(fs, localDir);
const isLocal = !!localValid && (!globalDir || localDir !== globalDir);