Skip to content

fix(hooks): use socket-based Kitty remote control to prevent escape sequence leaks#493

Open
Mutdogus wants to merge 1 commit intodanielmiessler:mainfrom
Mutdogus:fix/kitty-escape-sequence-leak
Open

fix(hooks): use socket-based Kitty remote control to prevent escape sequence leaks#493
Mutdogus wants to merge 1 commit intodanielmiessler:mainfrom
Mutdogus:fix/kitty-escape-sequence-leak

Conversation

@Mutdogus
Copy link

Summary

Fixes P@kitty-cmd escape sequence artifacts appearing in terminal when using PAI with Kitty terminal.

Problem

When PAI hooks use kitty @ commands without a socket, Kitty responds via escape sequences. Since hooks run as subprocesses, these sequences cannot be properly consumed and leak into the terminal as visible garbage:

P@kitty-cmd{"ok": false, "error": "Remote control is disabled"}

This affects anyone using PAI with Kitty terminal.

Root Cause

Kitty's allow_remote_control yes mode uses escape sequences for IPC. When hooks run as detached subprocesses:

  1. They send commands via escape sequences to the PTY
  2. Kitty writes responses back to the PTY
  3. The subprocess can't read from the PTY
  4. Response leaks as visible text

Solution

  • Added getKittySocket() helper to all tab-related hooks
  • All kitty/kitten commands now use --to <socket> flag for socket-based communication
  • Gracefully skips tab updates if no socket available (prevents leaks on misconfigured systems)

Files Changed

  • Packs/pai-hook-system/src/hooks/UpdateTabTitle.hook.ts
  • Packs/pai-hook-system/src/hooks/SetQuestionTab.hook.ts
  • Packs/pai-hook-system/src/hooks/QuestionAnswered.hook.ts
  • Packs/pai-hook-system/src/hooks/handlers/tab-state.ts

User Configuration Required

Users need to configure Kitty for socket-based remote control by adding to ~/.config/kitty/kitty.conf:

allow_remote_control socket-only
listen_on unix:/tmp/kitty-$USER

Then restart Kitty for changes to take effect.

Testing

Tested on macOS with Kitty terminal - artifacts no longer appear after fix.


🤖 Generated with Claude Code

…equence leaks

When PAI hooks use `kitty @` commands without specifying a socket, Kitty
communicates via escape sequences. In subprocess contexts (how hooks run),
these escape sequences cannot be properly consumed, causing them to leak
into the terminal output as visible "P@kitty-cmd" garbage text.

This affects:
- UpdateTabTitle.hook.ts
- SetQuestionTab.hook.ts
- QuestionAnswered.hook.ts
- handlers/tab-state.ts

The fix adds a `getKittySocket()` helper that checks for:
1. KITTY_LISTEN_ON environment variable
2. Default socket at /tmp/kitty-$USER

All kitty/kitten commands now use `--to <socket>` flag for socket-based
communication, which doesn't suffer from escape sequence leaks.

Users need to configure Kitty with:
```
allow_remote_control socket-only
listen_on unix:/tmp/kitty-$USER
```

Fixes terminal artifacts like:
P@kitty-cmd{"ok": false, "error": "Remote control is disabled"}

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copy link

@A13xSM A13xSM left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This solution worked perfectly for me. It not only resolved the escape sequence leaks but also resolved a buffering issue where PAI would hang when loading, requiring input to refresh the buffer. Great fix.

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.

2 participants