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

@@ -165,6 +165,102 @@ describe('resolveUpdateContext: runtime probing + env overrides', () => {
});
});
describe('resolveUpdateContext: preferredConfigDir fast-path dedup (#4197)', () => {
test('cwd === home does NOT misdetect as LOCAL on the preferredConfigDir fast path (dedup)', () => {
// #4197: the fast path derived scope from a cwd-relative match alone, so a
// global install probed with cwd === $HOME answered LOCAL. The cascade's
// same-path dedup (update.md: "local-over-global with same-path dedup (so
// CWD=$HOME does not misdetect as LOCAL)") must hold on the fast path too.
const fs = fakeFs({ [ver(`${HOME}/.claude`)]: '1.40.0\n', [marker(`${HOME}/.claude`)]: 'x' });
const r = resolveUpdateContext({
home: HOME, cwd: HOME, env: {}, fs,
preferredConfigDir: `${HOME}/.claude`, preferredRuntime: 'claude',
});
assert.equal(r.scope, 'GLOBAL');
});
test('same global install resolves identically from home and elsewhere on the fast path', () => {
const fs = fakeFs({ [ver(`${HOME}/.claude`)]: '1.40.0\n', [marker(`${HOME}/.claude`)]: 'x' });
const inputs = {
home: HOME, env: {}, fs,
preferredConfigDir: `${HOME}/.claude`, preferredRuntime: 'claude',
};
const fromElsewhere = resolveUpdateContext({ ...inputs, cwd: CWD });
assert.equal(fromElsewhere.scope, 'GLOBAL');
assert.deepEqual(
resolveUpdateContext({ ...inputs, cwd: HOME }),
fromElsewhere,
'scope must not depend on which directory the shell is sitting in',
);
});
test('genuine project-local install stays LOCAL on the fast path', () => {
// Control: the dedup must not over-correct into GLOBAL-always. A preferred
// dir that is the cwd-relative install and NOT the selected global is LOCAL.
const fs = fakeFs({ [ver(`${CWD}/.claude`)]: '1.39.0\n', [marker(`${CWD}/.claude`)]: 'x' });
const r = resolveUpdateContext({
home: HOME, cwd: CWD, env: {}, fs,
preferredConfigDir: `${CWD}/.claude`, preferredRuntime: 'claude',
});
assert.equal(r.scope, 'LOCAL');
assert.equal(r.installedVersion, '1.39.0');
assert.ok(sameDir(r.gsdDir, `${CWD}/.claude`), `gsdDir was ${r.gsdDir}`);
});
test('genuine project-local install stays LOCAL even with a global install also present', () => {
const fs = fakeFs({
[ver(`${CWD}/.claude`)]: '1.39.0\n', [marker(`${CWD}/.claude`)]: 'x',
[ver(`${HOME}/.claude`)]: '1.40.0\n', [marker(`${HOME}/.claude`)]: 'x',
});
const r = resolveUpdateContext({
home: HOME, cwd: CWD, env: {}, fs,
preferredConfigDir: `${CWD}/.claude`, preferredRuntime: 'claude',
});
assert.equal(r.scope, 'LOCAL');
assert.equal(r.installedVersion, '1.39.0');
});
test('env-directed global elsewhere: $HOME/.claude is LOCAL and the fast path agrees with the cascade', () => {
// The dedup must compare against the SELECTED global candidate (env-ranked),
// not the $HOME pathname: with CLAUDE_CONFIG_DIR pointing the global at
// /opt/claude-global, $HOME/.claude probed from cwd === $HOME is a genuine
// cwd-local install, and the cascade already answers LOCAL for it.
const custom = '/opt/claude-global';
const fs = fakeFs({
[ver(`${HOME}/.claude`)]: '1.39.0\n', [marker(`${HOME}/.claude`)]: 'x',
[ver(custom)]: '1.40.0\n', [marker(custom)]: 'x',
});
const env = { CLAUDE_CONFIG_DIR: custom };
const cascade = resolveUpdateContext({ home: HOME, cwd: HOME, env, fs });
const fast = resolveUpdateContext({
home: HOME, cwd: HOME, env, fs,
preferredConfigDir: `${HOME}/.claude`, preferredRuntime: 'claude',
});
assert.equal(cascade.scope, 'LOCAL');
assert.equal(fast.scope, cascade.scope);
assert.equal(fast.installedVersion, cascade.installedVersion);
assert.equal(fast.runtime, cascade.runtime);
assert.ok(sameDir(fast.gsdDir, cascade.gsdDir), `fast gsdDir ${fast.gsdDir} vs cascade ${cascade.gsdDir}`);
});
test('env candidate IS the preferred dir: fast path answers GLOBAL, matching the cascade', () => {
// A preferred dir that is also the env-directed global is the selected
// global — the cascade answers GLOBAL, and the fast path must agree.
const fs = fakeFs({ [ver(`${HOME}/.claude`)]: '1.40.0\n', [marker(`${HOME}/.claude`)]: 'x' });
const env = { CLAUDE_CONFIG_DIR: `${HOME}/.claude` };
const cascade = resolveUpdateContext({ home: HOME, cwd: HOME, env, fs });
const fast = resolveUpdateContext({
home: HOME, cwd: HOME, env, fs,
preferredConfigDir: `${HOME}/.claude`, preferredRuntime: 'claude',
});
assert.equal(cascade.scope, 'GLOBAL');
assert.equal(fast.scope, cascade.scope);
assert.equal(fast.installedVersion, cascade.installedVersion);
assert.equal(fast.runtime, cascade.runtime);
assert.ok(sameDir(fast.gsdDir, cascade.gsdDir), `fast gsdDir ${fast.gsdDir} vs cascade ${cascade.gsdDir}`);
});
});
describe('gsd-tools update-context (CLI): emits the JSON contract', () => {
test('--config-dir fixture resolves to the documented 4-field JSON', () => {
const tmp = nodeFs.mkdtempSync(path.join(os.tmpdir(), 'gsd-uc-'));