From c73bd899fd93f5bab40c07a957c6574835ef91b9 Mon Sep 17 00:00:00 2001 From: ggbdpq Date: Sat, 3 Oct 2026 06:24:03 +0800 Subject: [PATCH] fix(claude): install managed hooks into the config dir Claude actually reads MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Orca's Claude agent-status hook installer resolved its target as `~/.claude/settings.json` unconditionally, while every other part of Orca's Claude code resolves the CLI's config dir through `CLAUDE_CONFIG_DIR`. When that variable is set, Claude Code reads user settings from `$CLAUDE_CONFIG_DIR/settings.json`, so the managed hooks never fired for those sessions and an unrelated `~/.claude/settings.json` got modified instead (#24873). `getConfigPath` now routes the `.claude` settings target through the existing `defaultClaudeConfigDir()` helper — the same resolution the rest of Orca's Claude code uses: a set `CLAUDE_CONFIG_DIR` wins, a whitespace-only value falls back to `~/.claude`. Claude-compatible CLIs (`.openclaude` etc.) keep their own home directories; `CLAUDE_CONFIG_DIR` is a Claude CLI variable and says nothing about theirs. Fixes #24873 Generated-by: GLM-5.3-Flash (ZCode) --- src/main/claude/claude-config-dir-pin.ts | 5 ++++ src/main/claude/hook-settings.test.ts | 38 ++++++++++++++++++++++++ src/main/claude/hook-settings.ts | 4 +-- 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 src/main/claude/hook-settings.test.ts diff --git a/src/main/claude/claude-config-dir-pin.ts b/src/main/claude/claude-config-dir-pin.ts index da6fcac0328c..123fa7fdcef1 100644 --- a/src/main/claude/claude-config-dir-pin.ts +++ b/src/main/claude/claude-config-dir-pin.ts @@ -30,6 +30,11 @@ function samePath(a: string, b: string, platform: NodeJS.Platform): boolean { // Pin only nondefault homes: even an explicit default changes the CLI's Keychain identity. // Preserve the trimmed path's spelling; normalization is only for comparison. +/** Only the Claude CLI honors $CLAUDE_CONFIG_DIR; Claude-compatible variants keep their home-relative dir. */ +export function claudeCompatibleConfigDir(configDirName: string): string { + return configDirName === '.claude' ? defaultClaudeConfigDir() : join(homedir(), configDirName) +} + export function claudeConfigDirEnvPatch( accountHome: string, options: { env?: NodeJS.ProcessEnv; platform?: NodeJS.Platform } = {} diff --git a/src/main/claude/hook-settings.test.ts b/src/main/claude/hook-settings.test.ts new file mode 100644 index 000000000000..3e5ab072a779 --- /dev/null +++ b/src/main/claude/hook-settings.test.ts @@ -0,0 +1,38 @@ +// Why: locks the hook settings target to the config dir the Claude CLI actually reads, +// so a set CLAUDE_CONFIG_DIR cannot strand Orca's managed hooks in an unread ~/.claude (#24873). +import { homedir } from 'node:os' +import { join } from 'node:path' +import { afterEach, describe, expect, it, vi } from 'vitest' +import { CLAUDE_HOOK_SETTINGS, getConfigPath, OPENCLAUDE_HOOK_SETTINGS } from './hook-settings' + +afterEach(() => { + vi.unstubAllEnvs() + delete process.env.CLAUDE_CONFIG_DIR +}) + +describe('getConfigPath', () => { + it('follows a set CLAUDE_CONFIG_DIR, the same resolution as the rest of Orca\u2019s Claude code', () => { + vi.stubEnv('CLAUDE_CONFIG_DIR', join('/config', 'claude-alt')) + expect(getConfigPath()).toBe(join('/config', 'claude-alt', 'settings.json')) + }) + + it('keeps the legacy ~/.claude path when the variable is unset, so the fallback is unchanged', () => { + delete process.env.CLAUDE_CONFIG_DIR + expect(getConfigPath()).toBe(join(homedir(), '.claude', 'settings.json')) + }) + + it('falls back on a whitespace-only value and keeps a real value verbatim, like defaultClaudeConfigDir', () => { + vi.stubEnv('CLAUDE_CONFIG_DIR', ' ') + expect(getConfigPath()).toBe(join(homedir(), '.claude', 'settings.json')) + vi.stubEnv('CLAUDE_CONFIG_DIR', `${join('/config', 'claude-alt')}/`) + expect(getConfigPath()).toBe(join('/config', 'claude-alt', 'settings.json')) + }) + + it('leaves Claude-compatible CLIs on their own home directory', () => { + vi.stubEnv('CLAUDE_CONFIG_DIR', join('/config', 'claude-alt')) + expect(getConfigPath(OPENCLAUDE_HOOK_SETTINGS)).toBe( + join(homedir(), '.openclaude', 'settings.json') + ) + expect(getConfigPath(CLAUDE_HOOK_SETTINGS)).toBe(join('/config', 'claude-alt', 'settings.json')) + }) +}) diff --git a/src/main/claude/hook-settings.ts b/src/main/claude/hook-settings.ts index edfc429d4b8e..acd117ba2f36 100644 --- a/src/main/claude/hook-settings.ts +++ b/src/main/claude/hook-settings.ts @@ -1,4 +1,4 @@ -import { homedir } from 'node:os' +import { claudeCompatibleConfigDir } from './claude-config-dir-pin' import { basename, extname, join, win32 } from 'node:path' import { buildManagedCommandHook, @@ -51,7 +51,7 @@ export const OPENCLAUDE_HOOK_SETTINGS: ClaudeCompatibleHookSettings = { export type NativeHookFormOptions = { execForm?: boolean } export function getConfigPath(settings = CLAUDE_HOOK_SETTINGS, configDir?: string): string { - return join(configDir ?? join(homedir(), settings.configDirName), 'settings.json') + return join(configDir ?? claudeCompatibleConfigDir(settings.configDirName), 'settings.json') } export function getStatusLineScriptBaseName(settings = CLAUDE_HOOK_SETTINGS): string {