Skip to content

An always-on second brain you talk to. Voice notes in Telegram → typed, linked knowledge in your Obsidian vault. Runs 24/7 on the Claude subscription you already have.

fix(deploy): keep tmux brain alive across unit restarts; silence alert spam

smixsac147dfec911
diff --git a/deploy/dbrain-bot.service b/deploy/dbrain-bot.service
index 2b97354..e012fde 100644
--- a/deploy/dbrain-bot.service
+++ b/deploy/dbrain-bot.service
@@ -7,8 +7,14 @@ StartLimitBurst=5
OnFailure=dbrain-notify@%n.service
[Service]
-Type=notify
-WatchdogSec=180
+Type=simple
+# KillMode=process: stopping/restarting the bot must NOT kill the detached
+# tmux brain that lives in this unit's cgroup (the default control-group would
+# take it down). The brain outlives bot restarts; ensure_session re-attaches.
+KillMode=process
+# A clean stop/restart exits 143 (SIGTERM); that is success, not a failure, so
+# it must not trigger the OnFailure= alert.
+SuccessExitStatus=143 SIGTERM
WorkingDirectory=%h/projects/dbrain
ExecStart=%h/.local/bin/uv run python -m d_brain
Restart=always
diff --git a/deploy/dbrain-watchdog.service b/deploy/dbrain-watchdog.service
index b30e630..3ccafa0 100644
--- a/deploy/dbrain-watchdog.service
+++ b/deploy/dbrain-watchdog.service
@@ -6,8 +6,16 @@ StartLimitBurst=5
OnFailure=dbrain-notify@%n.service
[Service]
-Type=notify
-WatchdogSec=60
+Type=simple
+# KillMode=process: a watchdog restart must NOT kill the tmux brain it spawned
+# via force_recover (the brain lives in this cgroup). Previously the default
+# control-group + WatchdogSec SIGABRT loop killed the brain every minute.
+KillMode=process
+# Type=simple (not notify): the loop blocks during force_recover longer than a
+# short WatchdogSec, so a systemd WatchdogSec would SIGABRT it mid-recovery and
+# take the brain with it. Restart=always covers a crashed watchdog instead.
+# A clean stop exits 143 (SIGTERM) — success, must not fire OnFailure=.
+SuccessExitStatus=143 SIGTERM
# Separate slice so an OOM kill of the brain doesn't take the watchdog down.
Slice=dbrain-watchdog.slice
WorkingDirectory=%h/projects/dbrain
diff --git a/scripts/notify.sh b/scripts/notify.sh
index ac8a318..59a140c 100755
--- a/scripts/notify.sh
+++ b/scripts/notify.sh
@@ -17,5 +17,20 @@ if [ -z "${TELEGRAM_BOT_TOKEN:-}" ] || [ -z "$CHAT_ID" ]; then
exit 0
fi
+# Debounce identical alerts: a unit crash-loop must not spam Telegram. Transient
+# faults self-heal silently; we only want a rare, meaningful signal. Keyed by
+# message text so distinct faults still alert independently.
+RUNTIME_DIR="${DBRAIN_RUNTIME_DIR:-$HOME/.dbrain}"
+COOLDOWN="${DBRAIN_NOTIFY_COOLDOWN:-1800}" # seconds (30 min)
+mkdir -p "$RUNTIME_DIR" 2>/dev/null || true
+STAMP="$RUNTIME_DIR/notify.$(printf '%s' "$MSG" | cksum | cut -d' ' -f1).stamp"
+NOW=$(date +%s)
+LAST=$(cat "$STAMP" 2>/dev/null || echo 0)
+case "$LAST" in *[!0-9]*) LAST=0 ;; esac
+if [ "$((NOW - LAST))" -lt "$COOLDOWN" ]; then
+ exit 0 # within cooldown — stay silent
+fi
+echo "$NOW" >"$STAMP" 2>/dev/null || true
+
curl -s -X POST "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage" \
-d "chat_id=$CHAT_ID" -d "text=$MSG" >/dev/null || true