feat(tmux): route attention to Claude Code windows (#3-#9) #34

Merged
jmz merged 3 commits from feat/claude-tmux-attention into master 2026-11-07 15:37:51 +00:00
Member

Attention routing for Claude Code windows in tmux, built on the @claude_status the claude-status hook already keeps. One helper, ~/.local/bin/tmux-claude-attention, plus small changes to the hook and tmux.conf.local.

Closes #3, closes #4, closes #5, closes #6, closes #7, closes #8, closes #9

What each issue gets

  • #5 counts in status-right: red ?N needs permission, orange *N waiting, green *N done, ⋯N working (same glyphs and colours as the tab markers). The counts are stored in @claude_counts on each hook event, on window/session close (indexed set-hook, so reloading the config doesn't add duplicates) and once a minute from status-checks.sh, so a redraw never starts a shell.
  • #4 prefix + a: jumps to the next window that needs you, across sessions: needs-permission first, then waiting, then done. Repeatable and cycles. Listed in the prefix + ? cheat sheet.
  • #7 prominent permission state: a blinking red ? block. It is reverse video, so it still stands out in terminals that don't blink. noreverse noblink puts the tab back without touching its colours.
  • #6 terminal title: starts with the active window's state (❓ / 💤 / ✓ / ⏳) and ends with the fleet summary (@claude_title). This shows in the Ghostty tab and the OS window list.
  • #3 desktop notification: when a window goes to prompt or waiting and no focused client is showing it, an OSC 777 notification goes to the attached terminals (Ghostty, foot). It is written straight to the client tty, because tmux passthrough only reaches visible panes. A client inside an outer tmux gets the DCS-wrapped form. It fires once per state until the window is busy again. Turn it off with @claude_notify off, choose states with @claude_notify_states, or swap the transport with @claude_notify_cmd.
  • #9 away ping (opt-in, off by default): set @claude_away_cmd (for example set -g @claude_away_cmd 'matrix-bridge send'). When a window is still waiting on you after @claude_away_minutes (default 10), it runs once with the message as $1 (details are in CLAUDE_TMUX_* env). It is skipped if the window had output in the last minute, which covers an approved prompt where Claude keeps working (there is no "approved" hook event). Caveat: a CLI matrix-bridge send may contend with a running MCP instance for the bridge store (single-instance store takeover), so check that before enabling it.
  • #8 resurrect → claude --resume: the hook stores session_id and cwd from its JSON stdin on the pane (@claude_session_id / @claude_cwd). @resurrect-hook-post-save-all writes the panes still running Claude to ~/.local/share/tmux/claude-resume.tsv. @resurrect-hook-post-restore-all types cd -- <cwd> && claude --resume <id> into the restored shells. Disable with @claude_resurrect_resume off.

Behaviour change

The hook now sets @claude_status=busy instead of unsetting the option, so working windows can be told apart from plain shells. The tab markers only test idle/waiting/prompt, so tabs look the same. Busy only counts while Claude is still the pane's command.

No settings.json change is needed: the existing UserPromptSubmit / Stop / Notification hooks already pass the JSON on stdin. The helper is plain bash 3.2 (no associative arrays or mapfile), so it works with macOS /bin/bash.

Testing

  • bash test/test-tmux-claude-attention.sh passes 23/23. It runs on a private tmux server with its own socket and HOME, and covers states, session id, counts, stale busy, next ordering and wrap, notify debounce/re-arm/off, the away ping (fires, same stretch, stale timer) and resurrect save/restore/off.
  • Manual run with a real attached client (script pty): the off-screen window got ESC]777;notify;Claude · …;needs permission (main:1) and the focused window got nothing.
  • The rendered template (chezmoi execute-template, no apply) loads cleanly with source-file into a private server. The title and markers expand as expected.
  • shellcheck 0.11 is clean on all touched scripts.

Not verified: how blink and the OSC look in Ghostty on the Mac (no GUI here).

🤖 Generated with Claude Code

https://claude.ai/code/session_01XMf4EKNjKMd6bwCpS7K4jU

Attention routing for Claude Code windows in tmux, built on the `@claude_status` the claude-status hook already keeps. One helper, `~/.local/bin/tmux-claude-attention`, plus small changes to the hook and `tmux.conf.local`. Closes #3, closes #4, closes #5, closes #6, closes #7, closes #8, closes #9 ## What each issue gets - **#5 counts in status-right**: red `?N` needs permission, orange `*N` waiting, green `*N` done, `⋯N` working (same glyphs and colours as the tab markers). The counts are stored in `@claude_counts` on each hook event, on window/session close (indexed `set-hook`, so reloading the config doesn't add duplicates) and once a minute from `status-checks.sh`, so a redraw never starts a shell. - **#4 `prefix + a`**: jumps to the next window that needs you, across sessions: needs-permission first, then waiting, then done. Repeatable and cycles. Listed in the `prefix + ?` cheat sheet. - **#7 prominent permission state**: a blinking red ` ? ` block. It is reverse video, so it still stands out in terminals that don't blink. `noreverse noblink` puts the tab back without touching its colours. - **#6 terminal title**: starts with the active window's state (❓ / 💤 / ✓ / ⏳) and ends with the fleet summary (`@claude_title`). This shows in the Ghostty tab and the OS window list. - **#3 desktop notification**: when a window goes to prompt or waiting and no *focused* client is showing it, an OSC 777 notification goes to the attached terminals (Ghostty, foot). It is written straight to the client tty, because tmux passthrough only reaches visible panes. A client inside an outer tmux gets the DCS-wrapped form. It fires once per state until the window is busy again. Turn it off with `@claude_notify off`, choose states with `@claude_notify_states`, or swap the transport with `@claude_notify_cmd`. - **#9 away ping (opt-in, off by default)**: set `@claude_away_cmd` (for example `set -g @claude_away_cmd 'matrix-bridge send'`). When a window is still waiting on you after `@claude_away_minutes` (default 10), it runs once with the message as `$1` (details are in `CLAUDE_TMUX_*` env). It is skipped if the window had output in the last minute, which covers an approved prompt where Claude keeps working (there is no "approved" hook event). Caveat: a CLI `matrix-bridge send` may contend with a running MCP instance for the bridge store (single-instance store takeover), so check that before enabling it. - **#8 resurrect → `claude --resume`**: the hook stores `session_id` and `cwd` from its JSON stdin on the pane (`@claude_session_id` / `@claude_cwd`). `@resurrect-hook-post-save-all` writes the panes still running Claude to `~/.local/share/tmux/claude-resume.tsv`. `@resurrect-hook-post-restore-all` types `cd -- <cwd> && claude --resume <id>` into the restored shells. Disable with `@claude_resurrect_resume off`. ## Behaviour change The hook now sets `@claude_status=busy` instead of unsetting the option, so working windows can be told apart from plain shells. The tab markers only test idle/waiting/prompt, so tabs look the same. Busy only counts while Claude is still the pane's command. No `settings.json` change is needed: the existing UserPromptSubmit / Stop / Notification hooks already pass the JSON on stdin. The helper is plain bash 3.2 (no associative arrays or mapfile), so it works with macOS `/bin/bash`. ## Testing - `bash test/test-tmux-claude-attention.sh` passes 23/23. It runs on a private tmux server with its own socket and HOME, and covers states, session id, counts, stale busy, next ordering and wrap, notify debounce/re-arm/off, the away ping (fires, same stretch, stale timer) and resurrect save/restore/off. - Manual run with a real attached client (`script` pty): the off-screen window got `ESC]777;notify;Claude · …;needs permission (main:1)` and the focused window got nothing. - The rendered template (`chezmoi execute-template`, no apply) loads cleanly with `source-file` into a private server. The title and markers expand as expected. - `shellcheck` 0.11 is clean on all touched scripts. Not verified: how blink and the OSC look in Ghostty on the Mac (no GUI here). 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01XMf4EKNjKMd6bwCpS7K4jU
Builds on the per-window @claude_status that the claude-status hook already
keeps, with one helper (~/.local/bin/tmux-claude-attention):

- #5 status-right shows the state of every Claude window (red ?N needs
  permission, orange *N waiting, green *N done, ⋯N working). Counted on each
  hook event, on window/session close and once a minute (status-checks.sh)
  into @claude_counts, so a redraw runs no shell.
- #4 prefix + a jumps to the next window that needs you, across sessions:
  needs-permission, then waiting, then done; repeatable, cycles.
- #7 a window blocked on a permission decision gets a blinking red " ? "
  block (reverse video, so it still stands out without blink).
- #6 the terminal title (Ghostty tab, OS window) leads with the active
  window's state glyph and ends with the fleet summary.
- #3 prompt/waiting in a window that no focused client shows sends an
  OSC 777 desktop notification to the attached terminals; once per state
  until the window goes busy again. @claude_notify off disables it,
  @claude_notify_cmd swaps the transport.
- #9 opt-in away ping: with @claude_away_cmd set (e.g. 'matrix-bridge send'),
  a window still waiting on you after @claude_away_minutes (10) runs it once.
  Skipped when the window had output in the last minute (approved prompt,
  Claude still working).
- #8 the hook stores the session id and cwd per pane; resurrect's post-save
  hook records them and the post-restore hook types
  `cd <cwd> && claude --resume <id>` into the restored shells.

The hook now sets @claude_status=busy explicitly instead of unsetting it, so
working windows can be told apart from plain shells; tab markers only test
idle/waiting/prompt and are unchanged.

test/test-tmux-claude-attention.sh covers all of it on a private tmux server.

Closes #3, closes #4, closes #5, closes #6, closes #7, closes #8, closes #9

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01XMf4EKNjKMd6bwCpS7K4jU
- prefix + a: single quotes inside the run-shell string; session ids
  ("$1", ...) were expanded to nothing by /bin/sh (F1)
- counts, next and a new minute `sweep` only trust a state while some
  pane of the window still runs Claude; sweep clears stale
  @claude_status/@claude_notified (F2)
- status-checks.sh: mkdir lock dir with pid + stale-age takeover instead
  of flock (absent on macOS); TERM releases it at once (N1)
- hook reads only the first 8 KiB of the payload and drains the rest;
  first session_id/cwd occurrence wins (N2)
- resurrect-restore types `claude --resume` only into a bare shell (N4)
- document that `prompt` stays set after approval until Stop (N5)
- tests: binding line sourced and pressed on a control-mode client,
  stale states, sweep, lock, big payload, non-shell restore (51 cases)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Author
Member

Review (maximus): MERGE-WITH-NITS

Reviewed head a61c9f4 in a detached worktree. I did not run chezmoi apply.

Verified

  • bash test/test-tmux-claude-attention.sh: 51/51 pass on tmux 3.6a. The PR body still says 23/23, so update it.
  • shellcheck 0.11 is clean on the helper, the hook, status-checks.sh and the test.
  • I rendered tmux.conf.local.tmpl with chezmoi execute-template (throwaway config/state) and loaded it with the real oh-my-tmux into a fully isolated private server (env -i, own HOME/XDG/TMUX_TMPDIR). No errors. prefix a is bound with -r, the indexed window-unlinked[42] / session-closed[42] hooks are there, a prompt window renders #[fg=#d70000 reverse blink] ? #[noreverse noblink fg=default], and the title expands to ❓ host ❐ main ● 1 one. Spaces instead of commas in the #[...] styles inside #{?...} were the right call. No {{ in the new tmux strings, so the template is safe.
  • Injection: @claude_counts / @claude_title hold only digits and fixed glyphs/styles. Window names reach the OSC 777 only through clean(), which strips C0, DEL and ;. @claude_notify_cmd / @claude_away_cmd get the message as $1, never interpolated into the shell string. session_id is matched by [0-9A-Za-z-] and checked again on restore. cwd excludes " and \, and on restore it is %q-quoted and must pass -d. The next binding quotes $1-style session ids correctly.
  • Cost: the status redraw only reads options plus one in-process m/r. Counts run on hook events, on window/session close and in the minute sweep. The hook adds about 10 tmux client calls per event.
  • Hook not installed / helper missing: the hook checks -x and the markers render empty. bash 3.2 constructs only, and BSD-safe head -c/grep -o/find -mmin.

Nits (none blocking)

  1. resurrect-restore types claude --resume <id> and Enter (executable_tmux-claude-attention:326). After a reboot of a box with many Claude windows (spl-vm05 runs 5 to 7 agent windows overnight), every one of them starts at once. That's a memory/CPU spike against the box load budget, and it also resumes agent sessions that had already finished. Suggest typing the line without Enter so Bear presses Enter where he wants it (the same convention as for user-run commands), or defaulting @claude_resurrect_resume to off on servers.
  2. CLAUDE_CMD_RE (:56) doesn't match node. On a host where Claude came from npm, the minute sweep clears the tab marker within 60 s. Before this PR the marker stayed until the next event. That's fine for the native installer, which both hosts use as far as I can see.
  3. status-checks.sh:9 falls back to /tmp when XDG_RUNTIME_DIR is unset (macOS, some et sessions). On a multi-user host another user can pre-create /tmp/tmux-status-checks.lock.d and stop your loop (DoS only: the sticky bit stops the rm/mkdir, so there's no symlink write). ${TMPDIR:-/tmp} would be per-user on macOS.
  4. osc_notify writes to the client tty next to tmux. Rarely, it can land inside one of tmux's escape sequences and garble the screen until the next redraw. That's acceptable; just note it in the comment.
  5. on_screen relies on the focused client flag (tmux 3.3+). On older tmux every event notifies.
  6. bind -r a: within repeat-time after a jump, a typed a jumps again instead of reaching the pane. That's standard -r behaviour; just be aware of it.
  7. The status-checks lock rewrite (flock → mkdir) is a sensible macOS fix, but it's out of scope for #3-#9. Mention it in the PR body.

🤖 Generated with Claude Code

## Review (maximus): **MERGE-WITH-NITS** Reviewed head `a61c9f4` in a detached worktree. I did not run `chezmoi apply`. ### Verified - `bash test/test-tmux-claude-attention.sh`: **51/51 pass** on tmux 3.6a. The PR body still says 23/23, so update it. - shellcheck 0.11 is clean on the helper, the hook, `status-checks.sh` and the test. - I rendered `tmux.conf.local.tmpl` with `chezmoi execute-template` (throwaway config/state) and loaded it with the real oh-my-tmux into a fully isolated private server (`env -i`, own HOME/XDG/TMUX_TMPDIR). No errors. `prefix a` is bound with `-r`, the indexed `window-unlinked[42]` / `session-closed[42]` hooks are there, a `prompt` window renders ` #[fg=#d70000 reverse blink] ? #[noreverse noblink fg=default]`, and the title expands to `❓ host ❐ main ● 1 one`. Spaces instead of commas in the `#[...]` styles inside `#{?...}` were the right call. No `{{` in the new tmux strings, so the template is safe. - **Injection**: `@claude_counts` / `@claude_title` hold only digits and fixed glyphs/styles. Window names reach the OSC 777 only through `clean()`, which strips C0, DEL and `;`. `@claude_notify_cmd` / `@claude_away_cmd` get the message as `$1`, never interpolated into the shell string. `session_id` is matched by `[0-9A-Za-z-]` and checked again on restore. `cwd` excludes `"` and `\`, and on restore it is `%q`-quoted and must pass `-d`. The `next` binding quotes `$1`-style session ids correctly. - **Cost**: the status redraw only reads options plus one in-process `m/r`. Counts run on hook events, on window/session close and in the minute sweep. The hook adds about 10 tmux client calls per event. - **Hook not installed / helper missing**: the hook checks `-x` and the markers render empty. bash 3.2 constructs only, and BSD-safe `head -c`/`grep -o`/`find -mmin`. ### Nits (none blocking) 1. **resurrect-restore types `claude --resume <id>` *and Enter*** (`executable_tmux-claude-attention:326`). After a reboot of a box with many Claude windows (spl-vm05 runs 5 to 7 agent windows overnight), every one of them starts at once. That's a memory/CPU spike against the box load budget, and it also resumes agent sessions that had already finished. Suggest typing the line **without Enter** so Bear presses Enter where he wants it (the same convention as for user-run commands), or defaulting `@claude_resurrect_resume` to off on servers. 2. `CLAUDE_CMD_RE` (`:56`) doesn't match `node`. On a host where Claude came from npm, the minute sweep clears the tab marker within 60 s. Before this PR the marker stayed until the next event. That's fine for the native installer, which both hosts use as far as I can see. 3. `status-checks.sh:9` falls back to `/tmp` when `XDG_RUNTIME_DIR` is unset (macOS, some et sessions). On a multi-user host another user can pre-create `/tmp/tmux-status-checks.lock.d` and stop your loop (DoS only: the sticky bit stops the rm/mkdir, so there's no symlink write). `${TMPDIR:-/tmp}` would be per-user on macOS. 4. `osc_notify` writes to the client tty next to tmux. Rarely, it can land inside one of tmux's escape sequences and garble the screen until the next redraw. That's acceptable; just note it in the comment. 5. `on_screen` relies on the `focused` client flag (tmux 3.3+). On older tmux every event notifies. 6. `bind -r a`: within `repeat-time` after a jump, a typed `a` jumps again instead of reaching the pane. That's standard `-r` behaviour; just be aware of it. 7. The status-checks lock rewrite (flock → mkdir) is a sensible macOS fix, but it's out of scope for #3-#9. Mention it in the PR body. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Merge branch 'master' into feat/claude-tmux-attention
All checks were successful
Test Bootstrap Script / End-to-End Bootstrap Test (Alpine) (push) Successful in 45s
1e57e2653c
jmz merged commit 1e57e2653c into master 2026-11-07 15:37:51 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
public/dotfiles-public!34
No description provided.