Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(security): post-upgrade's MCP refresh keeps each install on its own brain (outside-voice review, #8426)

garrytanf8d403a87b3f
diff --git a/src/core/setup/hook-launcher-refresh.ts b/src/core/setup/hook-launcher-refresh.ts
index 8085762a70..4a9e55a815 100644
--- a/src/core/setup/hook-launcher-refresh.ts
+++ b/src/core/setup/hook-launcher-refresh.ts
@@ -72,7 +72,9 @@ async function refreshSetup(log: (line: string) => void): Promise<void> {
const paths = claudeSetupPaths({ name: receipt.name, scope: receipt.scope, projectDir: receipt.project_dir });
const owned = new Set([...Object.values(receipt.hooks.owned), ...Object.values(receipt.hooks.pending)]);
const ours = claudeHookEntries(paths.settingsPath).filter((e) => e[GBRAIN_HOOK_MARKER_KEY] === GBRAIN_SETUP_MARKER_VALUE || owned.has(hookEntryHash(e) ?? '')).map((e) => e.command as string);
- const homeEnv = ours.map((c) => parseClaudeHookCommand(c)?.env.GBRAIN_HOME).find((h) => h !== undefined) ?? claudeMcpHome(paths.configPath, receipt.name, receipt.project_dir);
+ // Another install's setup hooks share this settings file, so the home comes from this install's own MCP entry or owned hooks.
+ const homeEnv = claudeMcpHome(paths.configPath, receipt.name, receipt.project_dir)
+ ?? claudeHookEntries(paths.settingsPath).filter((e) => owned.has(hookEntryHash(e) ?? '')).map((e) => parseClaudeHookCommand(e.command as string)?.env.GBRAIN_HOME).find((h) => h !== undefined);
const plan: ClaudeSetupPlan = { name: receipt.name, scope: receipt.scope, ...(receipt.project_dir ? { projectDir: receipt.project_dir } : {}), launcher: receipt.launcher, brainHome: receipt.gbrain_home, ...(homeEnv ? { gbrainHomeEnv: homeEnv } : {}),
sourceId: receipt.source_id, surface: receipt.surface, advertise: receipt.advertise ?? null, events: Object.keys({ ...receipt.hooks.owned, ...receipt.hooks.pending }) as ClaudeHookEvent[] };
// An MCP entry still exactly what setup last wrote predates the launcher (#8426): setup's own rewrite moves it.
diff --git a/test/setup.test.ts b/test/setup.test.ts
index f3141511af..1208c5106a 100644
--- a/test/setup.test.ts
+++ b/test/setup.test.ts
@@ -1095,6 +1095,32 @@ describe('hooks written before the hook launcher (#8424): the next setup and pos
expect((await setup(b, ['claude-code', '--json'])).doc.steps.map((s: any) => s.action)).toEqual(['keep', 'keep', 'keep']);
});
+ test('claude-code MCP (#8426): post-upgrade keeps each install on its own brain when another install\'s setup hooks share the settings file', async () => {
+ const b = box();
+ const brainA = join(b.root, 'brain-a');
+ const brainB = join(b.root, 'brain-b');
+ expect((await setup(b, ['claude-code', '--json'], { GBRAIN_HOME: brainA })).exitCode).toBe(0);
+ expect((await setup(b, ['claude-code', '--name', 'beta', '--no-hooks', '--json'], { GBRAIN_HOME: brainB })).exitCode).toBe(0);
+ const before = readJson(claudeJson(b)).mcpServers;
+ expect(before.gbrain.env.GBRAIN_HOME).toBe(brainA);
+ expect(before.beta.env.GBRAIN_HOME).toBe(brainB);
+ const cfg = readJson(claudeJson(b));
+ const old = { ...cfg.mcpServers.beta, command: cfg.mcpServers.beta.args[0], args: cfg.mcpServers.beta.args.slice(1) };
+ cfg.mcpServers.beta = old;
+ writeFileSync(claudeJson(b), JSON.stringify(cfg, null, 2));
+ const rb = receipt(b, 'beta');
+ const r = readJson(rb);
+ r.entry_hash = ownedEntryFingerprint(old);
+ writeFileSync(rb, JSON.stringify(r, null, 2));
+ const lines: string[] = [];
+ await withEnv({ ...env(b), GBRAIN_HOME: brainB }, async () => { await refreshHookLaunchers((l) => lines.push(l)); });
+ const after = readJson(claudeJson(b)).mcpServers;
+ expect(lines.join('\n')).toContain("gbrain setup's claude-code MCP server and hooks now start gbrain from its own directory");
+ expect(after.beta.command).toBe(launcher({ ...b, gbrainHome: brainB }));
+ expect(after.beta.env.GBRAIN_HOME).toBe(brainB);
+ expect(after.gbrain.env.GBRAIN_HOME).toBe(brainA);
+ });
+
test('claude-code MCP (#8426): doctor\'s rerun keeps a recorded --no-hooks, so moving the entry turns no hooks on', async () => {
const b = box();
expect((await setup(b, ['claude-code', '--no-hooks', '--json'])).exitCode).toBe(0);