From f2b05d29a865f788ad7f83fc9f6b5e3061703275 Mon Sep 17 00:00:00 2001 From: Hrijul Dey <44521405+hr1juldey@users.noreply.github.com> Date: Wed, 18 Nov 2026 23:51:54 +0530 Subject: [PATCH] =?UTF-8?q?fix(upkeep):=20the=20home=20folder=20is=20not?= =?UTF-8?q?=20a=20repository=20=E2=80=94=20never=20refresh=20its=20wiring?= =?UTF-8?q?=20(#829)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit User-scope agent wiring (~/.claude/helpers/*.cjs, ~/.claude/skills, the graft MCP entry in ~/.claude.json) made the home folder look like a wired repository: wiredHostIds(~) reads ~/.claude/helpers, so a version-change upkeep ran a full wiring refresh against ~ — clearOldLayout deleted the user-scope helpers and skills, and disableGraftServerIn disabled the graft MCP server with nothing registered in its place, on every version change. reconcileWiring now gives the home folder the same refusal "trail init" already gives it (homeFolderRefusal), before anything is read or written: no stamp replay, no rewrite, no deletion. The guard is positional — only ~ itself is refused; a real repository under the same home refreshes exactly as before. The explicit-init path was already guarded in runInitCommand; this closes the automatic path, which is the one that fired without the person choosing anything. --- src/upkeep.ts | 10 ++++++++++ test/upkeep.test.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/src/upkeep.ts b/src/upkeep.ts index 0c5bbf2bb..2a3f94f5b 100644 --- a/src/upkeep.ts +++ b/src/upkeep.ts @@ -29,6 +29,7 @@ import { spawn } from 'node:child_process'; import { join } from 'node:path'; import { homedir } from 'node:os'; import { readJson, writeJsonAtomic, cacheDir } from './util/state.js'; +import { homeFolderRefusal } from './init-guard.js'; import { HOSTS, hostRelPath } from './hosts/registry.js'; import { markersFor } from './hosts/sections.js'; import { getNpmViewVersion, readCurrentVersion } from './cli-meta.js'; @@ -288,9 +289,18 @@ export function reconcileWiring( wired?: (repo: string) => string[]; rewrite: (repo: string, hosts: string[], opts: WiringOpts, toTrail: boolean) => void; toTrail?: (repo: string) => boolean; + /** Overrides homedir() in tests. */ + home?: string; }, ): WiringRefresh | null { try { + // #829: user-scope agent wiring (~/.claude/helpers, ~/.claude/skills, the + // graft MCP entry) makes the home folder look like a wired repository, and + // a version-change upkeep would then refresh ~ — deleting user-scope + // helpers and skills and disabling the graft MCP server with nothing + // registered in its place. The home folder is not a repository; the same + // refusal `init` already gives, applied before anything is read or written. + if (homeFolderRefusal(repo, deps.home) !== null) return null; const stamp = readStamp(repo); const toTrail = deps.toTrail?.(repo) ?? false; if (stamp && stamp.version === current && !toTrail) return null; diff --git a/test/upkeep.test.ts b/test/upkeep.test.ts index d6a56fc76..6af9bf5f5 100644 --- a/test/upkeep.test.ts +++ b/test/upkeep.test.ts @@ -254,3 +254,46 @@ test('formatWiringRefresh discloses machine-wide writes', () => { assert.ok(local); assert.doesNotMatch(local, /~\/\.codex/); }); + +/* + * Issue #829: user-scope agent wiring (~/.claude/helpers/*.cjs, ~/.claude/ + * skills, the graft MCP entry in ~/.claude.json) made the home folder look + * like a wired repository, so a version-change upkeep ran a full wiring + * refresh against ~ — deleting user-scope helpers and skills and disabling + * the graft MCP server with nothing registered in its place. The home folder + * is not a repository; reconcileWiring must refuse it no matter what its disk + * looks like, while a real repo under the same home keeps refreshing. + */ +test("reconcileWiring refuses the home folder: user-scope wiring is not a repo (#829)", () => { + const home = tmpRepo("upkeep-home-folder"); + mkdirSync(join(home, ".claude", "helpers"), { recursive: true }); + const helper = join(home, ".claude", "helpers", "graft-hooks.cjs"); + writeFileSync(helper, "module.exports = {}; // user-scope wiring, not a repo's"); + + let rewrote = 0; + const refreshed = reconcileWiring(home, "9.9.9", { rewrite: () => { rewrote++; }, home }); + assert.equal(refreshed, null, "home is not a repository — nothing to reconcile"); + assert.equal(rewrote, 0, "no wiring rewrite ran against the home folder"); + assert.ok(existsSync(helper), "the user-scope helper survives upkeep"); +}); + +test("a home stamped by an older version stays refused on upgrade (#829)", () => { + const home = tmpRepo("upkeep-home-stamped"); + mkdirSync(join(home, ".trail"), { recursive: true }); + writeStamp(home, "0.28.0", ["claude"], { global: false, hooks: true }); + let rewrote = 0; + const refreshed = reconcileWiring(home, "0.29.0", { rewrite: () => { rewrote++; }, home }); + assert.equal(refreshed, null, "an init-wired home must not refresh either — the guard is positional, not stamp-based"); + assert.equal(rewrote, 0); +}); + +test("a real repository under the same home still refreshes (#829)", () => { + const home = tmpRepo("upkeep-home-parent"); + const repo = join(home, "code", "assign"); + mkdirSync(join(repo, ".trail"), { recursive: true }); + writeFileSync(join(repo, ".trail", "hook.cjs"), ""); + let rewrote = 0; + const refreshed = reconcileWiring(repo, "9.9.9", { rewrite: () => { rewrote++; }, home }); + assert.ok(refreshed, "the guard is positional: only ~ itself is refused"); + assert.equal(rewrote, 1, "and the refresh really ran"); +});