Let somebody extend this without forking it, and say when things die

Two of Section F.

Hooks are the pressure valve. "Can Panama also do X when the theme
changes" is now a five-line file in ~/.config/panama/hooks rather than
a fork, a feature request, or a patch somebody rebases forever. Each
name takes a single file and a .d directory so several things can react
without fighting over one, and a broken hook is reported and stepped
over: somebody's script must never cost a theme change, an upgrade or a
login. Wired at theme-set, post-upgrade and post-migrate. This is the
thirty-line version of the plugin host the upstream ledger defers, and
it has no API to keep stable beyond "we will run your script and tell
you what happened".

Testing it caught a real bug the reading would not have: run_one
captured the script path but never shifted it off, so every hook got
its own filename as $1 and the real arguments arrived one place late. A
hook reading $1 as the colour scheme got a path.

The crash watcher notices when a program dumps core and says so. Under
GNOME, ABRT does this; here nothing did, and applications died silently,
which is most of how "Linux is flaky" gets earned.

Once per program per session is the entire design, not a nicety. This
machine's portal backend crashes between eleven and sixty times a day,
and a notification per crash would be one every few minutes for
something nobody can act on. The first is news; the fortieth is why
people turn notifications off. The health page keeps the running count.

It waits for the notification server before reporting, because the
crash most worth hearing about is the one that took the shell with it,
and it names the executable rather than the kernel's comm field, which
truncates at fifteen characters. Verified against real segfaults.
This commit is contained in:
Gabriel Brown
2026-08-22 08:11:49 -04:00
parent 41dd91eb75
commit 7a5e990439
14 changed files with 499 additions and 2 deletions
+73
View File
@@ -0,0 +1,73 @@
#!/usr/bin/env bash
# Tell somebody when a program crashes.
#
# On GNOME, ABRT says so. Under a hand-assembled Hyprland desktop nothing does,
# and applications die silently -- which is most of how "Linux is flaky" gets
# earned. Fedora ships systemd-coredump by default, so the information is
# already there; nobody is reading it.
#
# Follows the journal for systemd-coredump's own message id and reports each
# program once per session.
#
# ONCE PER SESSION IS THE WHOLE DESIGN. This machine's portal backend crashes
# between eleven and sixty times a day -- see the portal-stability check in
# panama-doctor -- and a notification per crash would be a notification every
# few minutes for something the user can do nothing about. The first one is
# news; the fortieth is why people turn notifications off. The health page
# carries the running count for anyone who wants it.
set -uo pipefail
PANAMA_PATH="${PANAMA_PATH:-$HOME/.local/share/Panama}"
# systemd-coredump's MESSAGE_ID. Matching on this rather than on text keeps
# working when the wording changes and never matches a program that merely
# mentions the word "crash" in its own logs.
readonly COREDUMP_MESSAGE_ID='fc2e22bc6ee647b6b90729ab34a250b1'
command -v journalctl >/dev/null 2>&1 || exit 0
command -v notify-send >/dev/null 2>&1 || exit 0
# The shell owns org.freedesktop.Notifications, and the crash most worth
# reporting is the one that took the shell with it. Waiting means that report
# arrives rather than vanishing into a bus nobody is serving.
for _ in $(seq 1 60); do
busctl --user status org.freedesktop.Notifications >/dev/null 2>&1 && break
sleep 1
done
declare -A reported=()
# -f from now, not from the boot: a session that starts after a crash should
# not open with a notification about something the user has already lived
# through and cannot act on.
journalctl --user -f -n 0 --output=json MESSAGE_ID="$COREDUMP_MESSAGE_ID" 2>/dev/null \
| while IFS= read -r line; do
[[ -n "$line" ]] || continue
uid="$(jq -r '.COREDUMP_UID // empty' <<<"$line" 2>/dev/null)"
exe="$(jq -r '.COREDUMP_EXE // empty' <<<"$line" 2>/dev/null)"
comm="$(jq -r '.COREDUMP_COMM // empty' <<<"$line" 2>/dev/null)"
# Another user's crash is not this session's business, and reporting it
# would leak what they are running.
[[ "$uid" == "$(id -u)" ]] || continue
[[ -n "$exe" || -n "$comm" ]] || continue
# The executable name first: COREDUMP_COMM is the kernel's comm field
# and is truncated to fifteen characters, so it reports
# "panama-test-cra" for a program called panama-test-crasher.
if [[ -n "$exe" ]]; then
program="$(basename "$exe")"
else
program="$comm"
fi
[[ -z "${reported[$program]:-}" ]] || continue
reported[$program]=1
notify-send --icon=dialog-error-symbolic --app-name=Panama \
"$program stopped unexpectedly" \
"It crashed and was not able to recover. System Health has the details." \
2>/dev/null || true
done
+68
View File
@@ -0,0 +1,68 @@
#!/usr/bin/env bash
# The pressure valve.
#
# panama-hook theme-set dark orchid
#
# Runs ~/.config/panama/hooks/<name> and everything executable in
# ~/.config/panama/hooks/<name>.d/, in sorted order, with the hook's arguments.
#
# This exists so "can Panama also do X when the theme changes" is a five-line
# file somebody drops in a directory rather than a fork, a feature request, or
# a patch that has to be rebased forever. docs/UPSTREAM-INSPIRATION.md defers a
# plugin host as premature and still should: this is the thirty-line version
# that covers most of what people actually want from one, and it has no API to
# keep stable beyond "we will run your script and tell you what happened".
#
# A failing hook is reported and stepped over. Somebody's broken script must
# never break a theme change, an upgrade, or a login -- which is exactly what
# would happen if this used `set -e` and the caller did too.
#
# Hooks run synchronously, so a slow one delays whatever called it. That is
# deliberate: the alternative is a hook whose output arrives after the thing it
# was reacting to has already finished, which is harder to reason about than a
# pause.
set -uo pipefail
HOOK_DIR="${PANAMA_HOOK_DIR:-${XDG_CONFIG_HOME:-$HOME/.config}/panama/hooks}"
name="${1:-}"
if [[ -z "$name" ]]; then
echo 'usage: panama-hook <name> [args...]' >&2
exit 2
fi
shift
# A hook name reaches the filesystem, so it cannot be allowed to leave the
# directory. Callers are all in-repo today, which is exactly when this is
# cheap to add and easy to forget.
if [[ ! "$name" =~ ^[a-z][a-z0-9-]*$ ]]; then
echo "panama-hook: refusing hook name: $name" >&2
exit 2
fi
run_one() {
local script="$1"
# Shifted off before the arguments are forwarded, or every hook receives
# its own path as $1 and the real arguments arrive one place late.
shift
[[ -f "$script" && -x "$script" ]] || return 0
if ! "$script" "$@"; then
printf 'panama-hook: %s failed (%s); continuing\n' \
"$(basename "$script")" "$name" >&2
fi
}
# The single file first, then the .d directory in sorted order. Both are
# optional and having neither is the normal case.
run_one "$HOOK_DIR/$name" "$@"
if [[ -d "$HOOK_DIR/$name.d" ]]; then
while IFS= read -r script; do
[[ -n "$script" ]] || continue
run_one "$script" "$@"
done < <(find "$HOOK_DIR/$name.d" -maxdepth 1 -type f | sort)
fi
exit 0
+3
View File
@@ -115,6 +115,9 @@ cmd_run() {
return 1
fi
ok "This machine now matches the checkout."
# Only after repairs actually ran: a hook that fires on every login when
# there was nothing to do is a hook people disable.
"$PANAMA_PATH/bin/panama-hook" post-migrate || true
}
# The check the login notifier runs. Exit 0 means work is waiting, so it reads