diff --git a/cli/src/claude/claudeLocalLauncher.test.ts b/cli/src/claude/claudeLocalLauncher.test.ts index a1d73298..d22ef812 100644 --- a/cli/src/claude/claudeLocalLauncher.test.ts +++ b/cli/src/claude/claudeLocalLauncher.test.ts @@ -41,6 +41,9 @@ function createSessionStub() { session: { localPermissionBridge: { start: vi.fn(), stop: vi.fn(), onTranscript: vi.fn() }, sessionId: 'test-session', + thinking: false, + localTurnStartedAt: 0, + onThinkingChange: vi.fn(), path: '/tmp/test', startedBy: 'terminal' as const, startingMode: 'local' as const, @@ -86,6 +89,40 @@ describe('claudeLocalLauncher message filtering', () => { expect(getMetadata().summary?.text).toBe('Native title') }) + it('clears thinking on exit and on an Esc interrupt, which fires no Stop hook', async () => { + const { session } = createSessionStub() + await claudeLocalLauncher(session as never) + expect(session.onThinkingChange).toHaveBeenCalledWith(false) + + session.onThinkingChange.mockClear() + session.thinking = true + harness.scannerOnMessage!({ type: 'user', uuid: 'u1', message: { role: 'user', content: 'keep going' } }) + expect(session.onThinkingChange).not.toHaveBeenCalled() + + harness.scannerOnMessage!({ + type: 'user', + uuid: 'u2', + message: { role: 'user', content: [{ type: 'text', text: '[Request interrupted by user for tool use]' }] } + }) + expect(session.onThinkingChange).toHaveBeenCalledWith(false) + }) + + it('ignores an interrupt marker delivered after a newer turn started', async () => { + const { session } = createSessionStub() + await claudeLocalLauncher(session as never) + session.onThinkingChange.mockClear() + session.thinking = true + session.localTurnStartedAt = Date.parse('2026-10-27T10:00:05Z') + + harness.scannerOnMessage!({ + type: 'user', + uuid: 'u1', + timestamp: '2026-10-27T10:00:04Z', + message: { role: 'user', content: '[Request interrupted by user]' } + }) + expect(session.onThinkingChange).not.toHaveBeenCalled() + }) + it('passes the current session model to the local Claude process', async () => { const { session } = createSessionStub() diff --git a/cli/src/claude/claudeLocalLauncher.ts b/cli/src/claude/claudeLocalLauncher.ts index f833e673..dd06370f 100644 --- a/cli/src/claude/claudeLocalLauncher.ts +++ b/cli/src/claude/claudeLocalLauncher.ts @@ -19,6 +19,14 @@ function stampContextWindow(message: RawJSONLines, contextModel: string | null): } } +function isInterruptMarker(message: RawJSONLines): boolean { + if (message.type !== 'user' || message.isSidechain) return false; + const content = message.message.content; + const blocks = typeof content === 'string' ? [{ type: 'text', text: content }] : content; + return Array.isArray(blocks) && blocks.some((block) => + block.type === 'text' && typeof block.text === 'string' && block.text.startsWith('[Request interrupted by user')); +} + export async function claudeLocalLauncher(session: Session): Promise<'switch' | 'exit'> { // Create scanner @@ -27,6 +35,13 @@ export async function claudeLocalLauncher(session: Session): Promise<'switch' | workingDirectory: session.path, onMessage: (message) => { session.localPermissionBridge.onTranscript(message); + // Esc-interrupted turns fire no Stop hook; Claude Code only + // writes this marker to the transcript. The scanner can deliver it + // after the next prompt's hook, so skip markers older than that turn. + if (session.thinking && isInterruptMarker(message) + && !(Date.parse(message.timestamp ?? '') < session.localTurnStartedAt)) { + session.onThinkingChange(false); + } // Preserve the AI-generated title emitted by Claude Code's native // interactive CLI. It is metadata, not a visible chat message. if (message.type === 'ai-title') { @@ -110,6 +125,7 @@ export async function claudeLocalLauncher(session: Session): Promise<'switch' | return await launcher.run(); } finally { // Cleanup + session.onThinkingChange(false); session.localPermissionBridge.stop(); session.removeSessionFoundCallback(handleSessionFound); await scanner.cleanup(); diff --git a/cli/src/claude/runClaude.ts b/cli/src/claude/runClaude.ts index b6a5bfe5..6b443766 100644 --- a/cli/src/claude/runClaude.ts +++ b/cli/src/claude/runClaude.ts @@ -186,6 +186,21 @@ export async function runClaude(options: StartOptions = {}): Promise { currentSession.localContextModel = data.to_model; } + // Local mode has no SDK stream; derive thinking from turn hooks. + // PreToolUse also covers a turn HAPI attached to mid-flight. + if (currentSession?.mode === 'local' && sessionId === currentSession.sessionId) { + const event = data.hook_event_name; + if (event === 'UserPromptSubmit') { + currentSession.localTurnStartedAt = Date.now(); + } + const thinking = event === 'UserPromptSubmit' || event === 'PreToolUse' ? true + : event === 'Stop' || event === 'StopFailure' || event === 'SessionEnd' ? false + : currentSession.thinking; + if (thinking !== currentSession.thinking) { + currentSession.onThinkingChange(thinking); + } + } + // Inherit the mode the user picked inside the interactive TUI // (shift+tab) so a later local→remote switch keeps it. Only the // local settings file registers the hooks that carry diff --git a/cli/src/claude/session.ts b/cli/src/claude/session.ts index e8178d7b..dca96a83 100644 --- a/cli/src/claude/session.ts +++ b/cli/src/claude/session.ts @@ -27,6 +27,8 @@ export class Session extends AgentSessionBase { localLaunchFailure: LocalLaunchFailure | null = null; /** Model the local TUI runs, "[1m]" suffix intact: launch seed, then PostModelSwitch. */ localContextModel: string | null = null; + /** Epoch ms of the latest local UserPromptSubmit hook; older interrupt markers are stale. */ + localTurnStartedAt = 0; private nativeSkillNames = new Set(); constructor(opts: { diff --git a/cli/src/modules/common/hooks/generateHookSettings.test.ts b/cli/src/modules/common/hooks/generateHookSettings.test.ts index ec25c062..3a0cce27 100644 --- a/cli/src/modules/common/hooks/generateHookSettings.test.ts +++ b/cli/src/modules/common/hooks/generateHookSettings.test.ts @@ -25,6 +25,9 @@ describe('buildHookSettings local approvals', () => { expect(settings.hooks.PostToolUse).toBeDefined() expect(settings.hooks.PostToolUseFailure).toBeDefined() expect(settings.hooks.SessionEnd).toBeDefined() + expect(settings.hooks.Stop).toBeDefined() + expect(settings.hooks.StopFailure).toBeDefined() + expect(buildHookSettings('forward-cmd', undefined, true, false).hooks.Stop).toBeUndefined() expect(settings.hooks.PermissionRequest?.[0].hooks[0]).toEqual({ type: 'command', command: 'forward-cmd', diff --git a/cli/src/modules/common/hooks/generateHookSettings.ts b/cli/src/modules/common/hooks/generateHookSettings.ts index b51d1ea3..26ff1351 100644 --- a/cli/src/modules/common/hooks/generateHookSettings.ts +++ b/cli/src/modules/common/hooks/generateHookSettings.ts @@ -28,6 +28,8 @@ type HookSettings = { PostModelSwitch?: HookCommandConfig[]; PostToolUse?: HookCommandConfig[]; PostToolUseFailure?: HookCommandConfig[]; + Stop?: HookCommandConfig[]; + StopFailure?: HookCommandConfig[]; SessionEnd?: HookCommandConfig[]; }; }; @@ -79,6 +81,8 @@ export function buildHookSettings( }]; hooks.PostToolUse = [{ matcher: '*', ...commandHook }]; hooks.PostToolUseFailure = [{ matcher: '*', ...commandHook }]; + hooks.Stop = [commandHook]; + hooks.StopFailure = [commandHook]; hooks.SessionEnd = [{ matcher: '*', ...commandHook }]; }