feat(cli): inherit local TUI permission mode on local→remote switch

Add permission mode tracking across the local→remote switch so mode changes
made inside the interactive Claude TUI (shift+tab) are inherited when
launching remote. Previously, a mode picked in local TUI was invisible to
remote sessions.

Implementation:
- generateHookSettings: add trackPermissionMode option to register UserPromptSubmit
  and PreToolUse hooks (their payloads carry permission_mode; SessionStart's does
  not). Export buildHookSettings for testing and make matcher optional.
- New hookPermissionMode.ts: normalizer for hook permission_mode → HAPI mode
  ('manual' → 'default'; unknown modes like 'dontAsk' → null/ignored).
- runClaude.ts: generate a second, local-TUI-only hook settings file with
  trackPermissionMode enabled (remote SDK process keeps the SessionStart-only
  file — these hooks block Claude per prompt/tool, and remote state is owned
  by hub/RPC). Hook callback inherits mode when session.mode === 'local': updates
  currentPermissionMode, syncs session, pushes keepalive, emits permission-mode-
  changed event.
- session.ts, loop.ts, claudeLocalLauncher.ts: plumb localHookSettingsPath
  (defaults to hookSettingsPath when not provided).
This commit is contained in:
weishu
2026-08-01 23:40:46 +08:00
parent 9727f7e4a0
commit 3e61d276bd
9 changed files with 138 additions and 15 deletions
@@ -0,0 +1,17 @@
import { describe, expect, it } from 'vitest'
import { buildHookSettings } from './generateHookSettings'
describe('buildHookSettings', () => {
it('registers only SessionStart by default', () => {
const settings = buildHookSettings('forward-cmd')
expect(Object.keys(settings.hooks)).toEqual(['SessionStart'])
expect(settings.hooks.SessionStart[0].hooks[0].command).toBe('forward-cmd')
})
it('adds permission-mode-carrying hooks when trackPermissionMode is set', () => {
const settings = buildHookSettings('forward-cmd', undefined, true)
expect(settings.hooks.UserPromptSubmit?.[0].hooks[0].command).toBe('forward-cmd')
expect(settings.hooks.PreToolUse?.[0].matcher).toBe('*')
expect(settings.hooks.PreToolUse?.[0].hooks[0].command).toBe('forward-cmd')
})
})
@@ -5,7 +5,7 @@ import { logger } from '@/ui/logger';
import { getHappyCliCommand } from '@/utils/spawnHappyCLI';
type HookCommandConfig = {
matcher: string;
matcher?: string;
hooks: Array<{
type: 'command';
command: string;
@@ -18,6 +18,8 @@ type HookSettings = {
};
hooks: {
SessionStart: HookCommandConfig[];
UserPromptSubmit?: HookCommandConfig[];
PreToolUse?: HookCommandConfig[];
};
};
@@ -25,6 +27,14 @@ export type HookSettingsOptions = {
filenamePrefix: string;
logLabel: string;
hooksEnabled?: boolean;
/**
* Also forward UserPromptSubmit and PreToolUse hooks. Unlike SessionStart,
* their payloads carry `permission_mode`, letting HAPI track the mode the
* user picks inside the interactive TUI (shift+tab). Keep this off for the
* remote SDK process: these hooks block Claude while the forwarder runs,
* and remote permission state is owned by the hub/RPC path anyway.
*/
trackPermissionMode?: boolean;
};
function shellQuote(value: string): string {
@@ -43,20 +53,22 @@ function shellJoin(parts: string[]): string {
return parts.map(shellQuote).join(' ');
}
function buildHookSettings(command: string, hooksEnabled?: boolean): HookSettings {
const hooks: HookSettings['hooks'] = {
SessionStart: [
export function buildHookSettings(command: string, hooksEnabled?: boolean, trackPermissionMode?: boolean): HookSettings {
const commandHook = {
hooks: [
{
matcher: '*',
hooks: [
{
type: 'command',
command
}
]
type: 'command' as const,
command
}
]
};
const hooks: HookSettings['hooks'] = {
SessionStart: [{ matcher: '*', ...commandHook }]
};
if (trackPermissionMode) {
hooks.UserPromptSubmit = [commandHook];
hooks.PreToolUse = [{ matcher: '*', ...commandHook }];
}
const settings: HookSettings = { hooks };
if (hooksEnabled !== undefined) {
@@ -88,7 +100,7 @@ export function generateHookSettingsFile(
]);
const hookCommand = shellJoin([command, ...args]);
const settings = buildHookSettings(hookCommand, options.hooksEnabled);
const settings = buildHookSettings(hookCommand, options.hooksEnabled, options.trackPermissionMode);
writeFileSync(filepath, JSON.stringify(settings, null, 4));
logger.debug(`[${options.logLabel}] Created hook settings file: ${filepath}`);