Skip to content

Add ESC key listener to detach from async tool tasks#72

Merged
jancurn merged 2 commits intomainfrom
claude/async-detach-esc-key-mNL89
Mar 13, 2026
Merged

Add ESC key listener to detach from async tool tasks#72
jancurn merged 2 commits intomainfrom
claude/async-detach-esc-key-mNL89

Conversation

@jancurn
Copy link
Member

@jancurn jancurn commented Mar 13, 2026

Summary

This PR adds the ability to detach from running async tool tasks by pressing the ESC key in human-readable output mode. When a task is running in the terminal, users can now press ESC to detach and let the task continue in the background, with the task ID displayed for later reference.

Key Changes

  • New setupEscListener() function: Sets up a raw mode stdin listener that detects ESC key presses and returns a promise that resolves when ESC is pressed. Only activates in TTY mode and includes proper cleanup of stdin state.
  • Task detachment logic: Modified callTool() to race the task completion promise against the ESC listener promise, allowing early exit when ESC is pressed.
  • UI enhancements:
    • Captures the task ID from task updates
    • Displays "(ESC to detach)" hint in the spinner text when a task ID is available and ESC listener is active
    • Shows a confirmation message with the task ID when detaching
  • Conditional activation: ESC listener only activates in human output mode and when stdin is not already in raw mode (to avoid conflicts with interactive shells).
  • Proper cleanup: Ensures stdin state is restored and event listeners are removed in both success and error paths.

Implementation Details

  • The ESC listener is only enabled when outputMode === 'human' and !process.stdin.isRaw to prevent interference with interactive shells
  • Uses Promise.race() to allow either task completion or ESC key press to resolve first
  • Maintains backward compatibility - the feature is transparent when not using human output mode
  • Includes guard checks to prevent double-cleanup and handle edge cases

https://claude.ai/code/session_01X3RL2TuBha7jrzCZdmtkPJ

claude and others added 2 commits March 13, 2026 09:33
When using --async for tool calls, users can now press ESC to detach
from a running task. The task continues in the background and the task
ID is printed, matching the behavior of --detach. The ESC hint is shown
in the spinner after the task ID is received from the server.

https://claude.ai/code/session_01X3RL2TuBha7jrzCZdmtkPJ
@jancurn jancurn merged commit 8da1e30 into main Mar 13, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants