From 4fb35397eecc269183a02e2cf34e2d3a8f160534 Mon Sep 17 00:00:00 2001 From: David Young Date: Wed, 25 Nov 2026 07:52:09 -0700 Subject: [PATCH] Claude notifications: clicking one brings back the app Claude runs in The hooks ran notify-send from inside Claude, so GNOME couldn't tell which app the notification came from and clicking it did nothing. system/claude-notify finds the terminal or editor among Claude's parent processes (or tmux's client, or the Flatpak) and names it in the desktop-entry hint. The title shows the project folder and the body Claude's own message. claude.sh swaps the old notify-send hooks for it, only where they're still the ones CoreDesktop wrote, and 70-claude.sh now runs claude.sh on every update so existing machines get it. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- claude.sh | 29 +++++++-- system/claude-notify | 84 ++++++++++++++++++++++++ tests/claude-notify.bats | 136 +++++++++++++++++++++++++++++++++++++++ updates.d/70-claude.sh | 15 ++--- 5 files changed, 251 insertions(+), 15 deletions(-) create mode 100755 system/claude-notify create mode 100644 tests/claude-notify.bats diff --git a/README.md b/README.md index 60ead31..51f4a25 100644 --- a/README.md +++ b/README.md @@ -198,7 +198,7 @@ Besides installing software, CoreDesktop changes these settings. They're listed - **Services turned on**: Avahi, Bluetooth, CUPS (printing), fwupd, GDM, NetworkManager, reflector (weekly), sshd, Docker, Tailscale, ufw and systemd-oomd. Your user is added to the docker group. - **Snapper** (btrfs only): a config for / in /etc/snapper/configs/root. - **NVIDIA** (only if it installs the driver): /etc/mkinitcpio.conf.d/nvidia.conf and NVIDIA's suspend services. -- **Your home folder**: lines in ~/.bashrc (CoreDesktop's shell setup, ~/.local/bin on PATH, the Bitwarden SSH agent), the commands in ~/.local/bin (update, refresh, sibling, version, configure, audit, report, webapp), GNOME extensions and their settings, Claude Code's ~/.claude/settings.json if you had none, a user timer that checks for updates, a user service that pauses your Core's email if its password stops working (see Configure), and Taildrop's user service and "Send with Tailscale" in ~/.local/share/nautilus/scripts (see Configure). +- **Your home folder**: lines in ~/.bashrc (CoreDesktop's shell setup, ~/.local/bin on PATH, the Bitwarden SSH agent), the commands in ~/.local/bin (update, refresh, sibling, version, configure, audit, report, webapp), GNOME extensions and their settings, Claude Code's ~/.claude/settings.json if you had none (and, if you did, its notification hooks while they're still the ones CoreDesktop wrote), a user timer that checks for updates, a user service that pauses your Core's email if its password stops working (see Configure), and Taildrop's user service and "Send with Tailscale" in ~/.local/share/nautilus/scripts (see Configure). - **configure** adds more, only for what you set up: your Nextcloud files mount and network storage in /etc/fstab (SMB passwords in /etc/coredesktop/smb, SFTP mounts as user services named coredesktop-sftp-*), printers in CUPS, GNOME Online Accounts, passwords in your GNOME keyring, the backup timer, and for a security key or fingerprint, lines in /etc/pam.d/sudo and /etc/pam.d/polkit-1 (a copy of /usr/lib/pam.d/polkit-1), with security keys in /etc/u2f_mappings and the lid check in /usr/local/lib/coredesktop/lid-closed. ## Keep your System Up-to-date diff --git a/claude.sh b/claude.sh index c98af90..127cfa6 100755 --- a/claude.sh +++ b/claude.sh @@ -21,14 +21,35 @@ else echo "\~/.local/bin added to PATH in .bashrc" fi -# Desktop notifications via notify-send (libnotify) +# Desktop notifications, through system/claude-notify so that clicking one +# brings back the app Claude is running in SETTINGS_FILE="$HOME/.claude/settings.json" +# The hook runs in a shell, which expands ~ +# shellcheck disable=SC2088 +NOTIFY="~/.local/share/coredesktop/system/claude-notify" if [ -f "$SETTINGS_FILE" ]; then echo "Claude Code settings already exist at $SETTINGS_FILE" + # Settings written before claude-notify ran notify-send itself, so clicking + # did nothing. Change those two hooks, and only those: any others are yours. + tmp="$(mktemp "$SETTINGS_FILE.XXXXXX")" + if jq --arg notify "$NOTIFY" \ + --arg old_input "notify-send 'Claude Code' 'Needs your input'" \ + --arg old_done "notify-send 'Claude Code' 'Task completed'" ' + def swap(old; new): + if type == "object" and .command == old then .command = new else . end; + (.hooks.Notification[]?.hooks[]?) |= swap($old_input; "\($notify) '"'Needs your input'"'") + | (.hooks.Stop[]?.hooks[]?) |= swap($old_done; "\($notify) '"'Task completed'"'") + ' "$SETTINGS_FILE" > "$tmp" \ + && ! jq -e --slurpfile new "$tmp" '. == $new[0]' "$SETTINGS_FILE" > /dev/null; then + echo "Making Claude Code's notifications clickable..." + mv -f "$tmp" "$SETTINGS_FILE" + else + rm -f "$tmp" + fi else echo "Writing Claude Code settings to $SETTINGS_FILE..." mkdir -p "$(dirname "$SETTINGS_FILE")" - cat > "$SETTINGS_FILE" <<'EOF' + cat > "$SETTINGS_FILE" <, with the hook's JSON on stdin. is shown when +# the JSON has no message of its own, as with Stop. +# +# GNOME brings an app forward when its notification is clicked only if it +# knows which app sent it. notify-send runs from Claude, not from the app, so +# the notification names the app itself in its desktop-entry hint: the first +# terminal or editor found among the processes Claude runs under. Clicking +# brings forward that app's most recently used window. +# +# CLAUDE_NOTIFY_PROC (default /proc) and CLAUDE_NOTIFY_PID (default this +# script's parent) are for the tests. + +text="${1:-Needs your input}" +proc="${CLAUDE_NOTIFY_PROC:-/proc}" + +input="" +[ -t 0 ] || input="$(cat)" +message="$(jq -r '.message // empty' <<< "$input" 2> /dev/null)" +cwd="$(jq -r '.cwd // empty' <<< "$input" 2> /dev/null)" +[ -n "$cwd" ] || cwd="$PWD" + +# The desktop entries an app may have, by the name of its process +entries_for() { + case "$1" in + kgx) echo org.gnome.Console ;; + ptyxis | ptyxis-agent) echo org.gnome.Ptyxis ;; + gnome-terminal*) echo org.gnome.Terminal ;; + ghostty) echo com.mitchellh.ghostty ;; + kitty) echo kitty ;; + alacritty) echo Alacritty ;; + wezterm-gui) echo org.wezfurlong.wezterm ;; + cosmic-term) echo com.system76.CosmicTerm ;; + codium) echo com.vscodium.codium codium vscodium ;; + code | code-oss) echo code com.visualstudio.code code-oss ;; + esac +} + +installed() { + local dir + local IFS=: + for dir in "${XDG_DATA_HOME:-$HOME/.local/share}" ${XDG_DATA_DIRS:-/usr/local/share:/usr/share}; do + [ -f "$dir/applications/$1.desktop" ] && return 0 + done + return 1 +} + +find_entry() { + # Inside a Flatpak (VS Codium's, say), the app is the Flatpak + if [ -n "$FLATPAK_ID" ]; then + echo "$FLATPAK_ID" + return + fi + + # tmux's server doesn't run under the terminal, its client does + local pid="${CLAUDE_NOTIFY_PID:-$PPID}" + if [ -n "$TMUX" ]; then + pid="$(tmux display-message -p '#{client_pid}' 2> /dev/null)" || return + fi + + local comm entry hops=0 + while [ -n "$pid" ] && [ "$pid" -gt 1 ] && [ "$hops" -lt 30 ]; do + comm="$(cat "$proc/$pid/comm" 2> /dev/null)" || return + for entry in $(entries_for "$comm"); do + if installed "$entry"; then + echo "$entry" + return + fi + done + pid="$(awk '/^PPid:/ { print $2 }' "$proc/$pid/status" 2> /dev/null)" + hops=$((hops + 1)) + done +} + +args=(--app-name="Claude Code" --icon=dialog-information) +entry="$(find_entry)" +[ -n "$entry" ] && args+=(--hint="string:desktop-entry:$entry") + +exec notify-send "${args[@]}" "Claude Code: ${cwd##*/}" "${message:-$text}" diff --git a/tests/claude-notify.bats b/tests/claude-notify.bats new file mode 100644 index 0000000..4e22e1c --- /dev/null +++ b/tests/claude-notify.bats @@ -0,0 +1,136 @@ +#!/usr/bin/env bats + +load test_helper + +# Claude Code's notifications (system/claude-notify) and claude.sh's hooks, +# with a fake /proc and fake desktop entries + +setup() { + common_setup + stub notify-send + export CLAUDE_NOTIFY_PROC="$BATS_TEST_TMPDIR/proc" + export XDG_DATA_DIRS="$BATS_TEST_TMPDIR/share" + mkdir -p "$XDG_DATA_DIRS/applications" + unset TMUX FLATPAK_ID XDG_DATA_HOME +} + +# process PID PPID COMM +process() { + mkdir -p "$CLAUDE_NOTIFY_PROC/$1" + echo "$3" > "$CLAUDE_NOTIFY_PROC/$1/comm" + printf 'Name:\t%s\nPPid:\t%s\n' "$3" "$2" > "$CLAUDE_NOTIFY_PROC/$1/status" +} + +app() { + touch "$XDG_DATA_DIRS/applications/$1.desktop" +} + +@test "a notification from Claude in Console names Console, so clicking it brings Console back" { + app org.gnome.Console + process 40 1 systemd + process 30 40 kgx + process 20 30 bash + process 10 20 claude + CLAUDE_NOTIFY_PID=10 run bash "$REPO/system/claude-notify" 'Needs your input' <<< '{"message": "Claude needs your permission to use Bash", "cwd": "/home/me/project"}' + [ "$status" -eq 0 ] + grep -qF -- '--hint=string:desktop-entry:org.gnome.Console Claude Code: project Claude needs your permission to use Bash' "$STUB_LOG" +} + +@test "without a message of its own, the notification shows the text it's given" { + process 10 1 claude + CLAUDE_NOTIFY_PID=10 run bash "$REPO/system/claude-notify" 'Task completed' <<< '{"cwd": "/home/me/project"}' + [ "$status" -eq 0 ] + grep -q 'Claude Code: project Task completed$' "$STUB_LOG" +} + +@test "in VS Codium, the notification names whichever VS Codium is installed" { + app codium + process 30 1 codium + process 20 30 bash + process 10 20 claude + CLAUDE_NOTIFY_PID=10 run bash "$REPO/system/claude-notify" <<< '{}' + grep -qF -- '--hint=string:desktop-entry:codium ' "$STUB_LOG" +} + +@test "in VS Codium's Flatpak, the notification names the Flatpak" { + FLATPAK_ID=com.vscodium.codium run bash "$REPO/system/claude-notify" <<< '{}' + grep -qF -- '--hint=string:desktop-entry:com.vscodium.codium ' "$STUB_LOG" +} + +@test "in tmux, the app is the one tmux's client runs in" { + app org.gnome.Console + process 30 1 kgx + process 25 30 tmux + stub tmux 'echo 25' + TMUX=/tmp/tmux-1000/default,1,0 run bash "$REPO/system/claude-notify" <<< '{}' + grep -qF -- '--hint=string:desktop-entry:org.gnome.Console ' "$STUB_LOG" +} + +@test "with no app it knows, the notification is still sent" { + process 20 1 sshd + process 10 20 claude + CLAUDE_NOTIFY_PID=10 run bash "$REPO/system/claude-notify" <<< '{}' + [ "$status" -eq 0 ] + grep -q '^notify-send ' "$STUB_LOG" + ! grep -q 'desktop-entry' "$STUB_LOG" +} + +# --- claude.sh ----------------------------------------------------------------- + +claude_setup() { + stub claude 'echo 2.0.0' + echo 'export PATH="$HOME/.local/bin:$PATH"' > "$HOME/.bashrc" + mkdir -p "$HOME/.claude" +} + +@test "claude.sh writes hooks that run claude-notify" { + claude_setup + run bash "$REPO/claude.sh" + [ "$status" -eq 0 ] + [ "$(jq -r '.hooks.Notification[0].hooks[0].command' "$HOME/.claude/settings.json")" = \ + "~/.local/share/coredesktop/system/claude-notify 'Needs your input'" ] + [ "$(jq -r '.hooks.Stop[0].hooks[0].command' "$HOME/.claude/settings.json")" = \ + "~/.local/share/coredesktop/system/claude-notify 'Task completed'" ] +} + +@test "claude.sh changes the old notify-send hooks, and keeps everything else" { + claude_setup + cat > "$HOME/.claude/settings.json" << 'EOF' +{ + "model": "opus", + "hooks": { + "Notification": [{"matcher": "", "hooks": [{"type": "command", "command": "notify-send 'Claude Code' 'Needs your input'"}]}], + "Stop": [{"matcher": "", "hooks": [{"type": "command", "command": "notify-send 'Claude Code' 'Task completed'"}, + {"type": "command", "command": "my-own-hook"}]}] + } +} +EOF + run bash "$REPO/claude.sh" + [ "$status" -eq 0 ] + [[ "$output" == *"Making Claude Code's notifications clickable"* ]] + settings="$HOME/.claude/settings.json" + [ "$(jq -r '.model' "$settings")" = opus ] + [ "$(jq -r '.hooks.Notification[0].hooks[0].command' "$settings")" = \ + "~/.local/share/coredesktop/system/claude-notify 'Needs your input'" ] + [ "$(jq -r '.hooks.Stop[0].hooks[0].command' "$settings")" = \ + "~/.local/share/coredesktop/system/claude-notify 'Task completed'" ] + [ "$(jq -r '.hooks.Stop[0].hooks[1].command' "$settings")" = my-own-hook ] + + # A second run has nothing to change + run bash "$REPO/claude.sh" + [[ "$output" != *"Making Claude Code's notifications clickable"* ]] +} + +@test "claude.sh leaves hooks you changed yourself, and settings without hooks" { + claude_setup + echo '{"hooks": {"Stop": [{"hooks": [{"type": "command", "command": "notify-send Done"}]}]}}' > "$HOME/.claude/settings.json" + cp "$HOME/.claude/settings.json" "$BATS_TEST_TMPDIR/before.json" + run bash "$REPO/claude.sh" + [ "$status" -eq 0 ] + cmp "$HOME/.claude/settings.json" "$BATS_TEST_TMPDIR/before.json" + + echo '{"model": "opus"}' > "$HOME/.claude/settings.json" + run bash "$REPO/claude.sh" + [ "$status" -eq 0 ] + [ "$(cat "$HOME/.claude/settings.json")" = '{"model": "opus"}' ] +} diff --git a/updates.d/70-claude.sh b/updates.d/70-claude.sh index 1143163..682a33b 100755 --- a/updates.d/70-claude.sh +++ b/updates.d/70-claude.sh @@ -2,13 +2,8 @@ COREDESKTOP="$HOME/.local/share/coredesktop" -# claude.sh has run if Claude Code is installed, ~/.local/bin is on PATH in -# .bashrc, and the notification settings file exists -if command -v claude &>/dev/null \ - && grep -qF 'export PATH="$HOME/.local/bin:$PATH"' ~/.bashrc 2>/dev/null \ - && [ -f "$HOME/.claude/settings.json" ]; then - echo "Claude Code already set up." -else - echo "Setting up Claude Code..." - bash "$COREDESKTOP/claude.sh" -fi +# claude.sh installs Claude Code if it's missing, puts ~/.local/bin on PATH and +# sets up its notifications. Each step does nothing if it's already done, so it +# runs on every update, and changes to the notifications reach this machine. +echo "Setting up Claude Code..." +bash "$COREDESKTOP/claude.sh"