Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/tsconfig.cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"../src/main/antigravity/windows-hook-json-post.ts",
"../src/main/antigravity/hook-service.ts",
"../src/main/antigravity/hooks-json-bundle.ts",
"../src/main/claude/claude-config-dir-pin.ts",
"../src/main/claude/hook-settings.ts",
"../src/main/claude/hook-service.ts",
"../src/main/claude/statusline-script.ts",
Expand Down
77 changes: 77 additions & 0 deletions src/main/claude/hook-settings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs'
import { homedir, tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'

vi.mock('electron', () => ({
app: {
getPath: () => '/tmp/userData'
}
}))

import { ClaudeHookService } from './hook-service'
import { CLAUDE_HOOK_SETTINGS, OPENCLAUDE_HOOK_SETTINGS, getConfigPath } from './hook-settings'

// Why: the CLI reads settings.json from this variable when it names a dir, which is also how an
// account with a non-default home is launched, so hooks have to follow it (#25242).
const RELOCATED_DIR = join('tmp', 'orca-relocated-claude')

describe('getConfigPath config dir resolution', () => {
afterEach(() => {
vi.unstubAllEnvs()
})

it('uses the CLI default dir when no variable names one', () => {
vi.stubEnv('CLAUDE_CONFIG_DIR', '')
expect(getConfigPath(CLAUDE_HOOK_SETTINGS)).toBe(join(homedir(), '.claude', 'settings.json'))
})

it('follows a relocated config dir', () => {
vi.stubEnv('CLAUDE_CONFIG_DIR', RELOCATED_DIR)
expect(getConfigPath(CLAUDE_HOOK_SETTINGS)).toBe(join(RELOCATED_DIR, 'settings.json'))
})

it('ignores surrounding whitespace in the variable', () => {
vi.stubEnv('CLAUDE_CONFIG_DIR', ` ${RELOCATED_DIR} `)
expect(getConfigPath(CLAUDE_HOOK_SETTINGS)).toBe(join(RELOCATED_DIR, 'settings.json'))
})

it('leaves a fork descriptor on its own dir while the variable is set', () => {
vi.stubEnv('CLAUDE_CONFIG_DIR', RELOCATED_DIR)
expect(getConfigPath(OPENCLAUDE_HOOK_SETTINGS)).toBe(
join(homedir(), '.openclaude', 'settings.json')
)
})
})

// Why end-to-end: the reported symptom is a hook-driven feature reading inactive because the hooks
// land in a settings file the CLI never opens, so the install has to be checked, not just the path.
describe('Claude hook install with a relocated config dir', () => {
let tmpHome: string
let relocatedDir: string

beforeEach(() => {
tmpHome = mkdtempSync(join(tmpdir(), 'orca-claude-hook-home-'))
relocatedDir = mkdtempSync(join(tmpdir(), 'orca-claude-hook-relocated-'))
vi.stubEnv('HOME', tmpHome)
vi.stubEnv('USERPROFILE', tmpHome)
vi.stubEnv('CLAUDE_CONFIG_DIR', relocatedDir)
})

afterEach(() => {
vi.unstubAllEnvs()
rmSync(tmpHome, { recursive: true, force: true })
rmSync(relocatedDir, { recursive: true, force: true })
})

it('writes the managed hooks where the CLI reads settings', () => {
const status = new ClaudeHookService().install({ claudeVersion: '2.1.261' })

expect(status.configPath).toBe(join(relocatedDir, 'settings.json'))
expect(status.state).toBe('installed')
const written = JSON.parse(readFileSync(status.configPath, 'utf-8'))
expect(Object.keys(written.hooks ?? {}).length).toBeGreaterThan(0)
// The default dir must not keep a settings file the CLI will never read.
expect(existsSync(join(tmpHome, '.claude', 'settings.json'))).toBe(false)
})
})
20 changes: 18 additions & 2 deletions src/main/claude/hook-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { quotePowerShellLiteral } from '../../shared/powershell-native-argument'
import { wrapRuntimeHomeHookCommand } from '../agent-hooks/runtime-home-hook-command'
import { wrapWindowsDirectCmdHookCommand } from '../agent-hooks/windows-direct-cmd-hook-command'
import type { ClaudeManagedHookPlan } from './claude-managed-hook-events'
import { defaultClaudeConfigDir } from './claude-config-dir-pin'

export type ClaudeCompatibleHookSettings = {
configDirName: '.claude' | '.openclaude' | '.qoder' | '.qoder-cn' | '.qwen' | '.codebuddy'
Expand All @@ -28,12 +29,15 @@ export type ClaudeCompatibleHookSettings = {
| 'codebuddy-hook'
usesWindowsCompatLauncher: boolean
windowsHookShell?: 'powershell'
/** The variable that relocates this CLI's config dir, when it has one. */
configDirEnvVar?: 'CLAUDE_CONFIG_DIR'
}

export const CLAUDE_HOOK_SETTINGS: ClaudeCompatibleHookSettings = {
configDirName: '.claude',
scriptBaseName: 'claude-hook',
usesWindowsCompatLauncher: true
usesWindowsCompatLauncher: true,
configDirEnvVar: 'CLAUDE_CONFIG_DIR'
}

export const OPENCLAUDE_HOOK_SETTINGS: ClaudeCompatibleHookSettings = {
Expand All @@ -42,8 +46,20 @@ export const OPENCLAUDE_HOOK_SETTINGS: ClaudeCompatibleHookSettings = {
usesWindowsCompatLauncher: false
}

/**
* Why: when the variable names a dir, the CLI reads its `settings.json` from there and never sees
* hooks written under the default dir — which is how Orca already launches an account whose home is
* not the CLI default (`claudeConfigDirEnvPatch`), so those accounts ran no hooks at all (#25242).
* The fork CLIs sharing this descriptor keep their own dir names and declare no variable.
*/
function hookConfigDir(settings: ClaudeCompatibleHookSettings): string {
return settings.configDirEnvVar
? defaultClaudeConfigDir()
: join(homedir(), settings.configDirName)
}

export function getConfigPath(settings = CLAUDE_HOOK_SETTINGS): string {
return join(homedir(), settings.configDirName, 'settings.json')
return join(hookConfigDir(settings), 'settings.json')
}

export function getStatusLineScriptBaseName(settings = CLAUDE_HOOK_SETTINGS): string {
Expand Down
Loading