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.

feat(deploy): systemd --user units, timers, tmux config, failure notifier

smixsa07bfc0e8cc1
diff --git a/deploy/d-brain-bot.service b/deploy/d-brain-bot.service
deleted file mode 100644
index 81d8c3d..0000000
--- a/deploy/d-brain-bot.service
+++ /dev/null
@@ -1,15 +0,0 @@
-[Unit]
-Description=d-brain Telegram Bot
-After=network.target
-
-[Service]
-Type=simple
-User=shima
-WorkingDirectory=/home/shima/projects/dbrain
-ExecStart=/home/shima/.local/bin/uv run python -m d_brain
-Restart=always
-RestartSec=10
-Environment=PYTHONUNBUFFERED=1
-
-[Install]
-WantedBy=multi-user.target
diff --git a/deploy/d-brain-process.service b/deploy/d-brain-process.service
deleted file mode 100644
index 6840b33..0000000
--- a/deploy/d-brain-process.service
+++ /dev/null
@@ -1,9 +0,0 @@
-[Unit]
-Description=d-brain Daily Processing
-
-[Service]
-Type=oneshot
-User=shima
-WorkingDirectory=/home/shima/projects/agent-second-brain
-ExecStart=/home/shima/projects/agent-second-brain/scripts/process.sh
-Environment=PYTHONUNBUFFERED=1
diff --git a/deploy/d-brain-weekly.service b/deploy/d-brain-weekly.service
deleted file mode 100644
index d0baf90..0000000
--- a/deploy/d-brain-weekly.service
+++ /dev/null
@@ -1,12 +0,0 @@
-[Unit]
-Description=d-brain Weekly Digest
-After=network.target
-
-[Service]
-Type=oneshot
-User=shima
-WorkingDirectory=/home/shima/projects/agent-second-brain
-ExecStart=/home/shima/.local/bin/uv run python scripts/weekly.py
-EnvironmentFile=/home/shima/projects/agent-second-brain/.env
-StandardOutput=journal
-StandardError=journal
diff --git a/deploy/d-brain-weekly.timer b/deploy/d-brain-weekly.timer
deleted file mode 100644
index 8b2c30f..0000000
--- a/deploy/d-brain-weekly.timer
+++ /dev/null
@@ -1,9 +0,0 @@
-[Unit]
-Description=Run d-brain weekly digest Friday morning
-
-[Timer]
-OnCalendar=Fri 06:00
-Persistent=true
-
-[Install]
-WantedBy=timers.target
diff --git a/deploy/dbrain-bot.service b/deploy/dbrain-bot.service
new file mode 100644
index 0000000..2b97354
--- /dev/null
+++ b/deploy/dbrain-bot.service
@@ -0,0 +1,21 @@
+[Unit]
+Description=d-brain Telegram bot (drives the persistent Claude session)
+After=network-online.target
+Wants=network-online.target
+StartLimitIntervalSec=300
+StartLimitBurst=5
+OnFailure=dbrain-notify@%n.service
+
+[Service]
+Type=notify
+WatchdogSec=180
+WorkingDirectory=%h/projects/dbrain
+ExecStart=%h/.local/bin/uv run python -m d_brain
+Restart=always
+RestartSec=10
+Environment=PYTHONUNBUFFERED=1
+Environment=CLAUDE_CONFIG_DIR=%h/.claude
+Environment=PATH=%h/.local/bin:/usr/local/bin:/usr/bin:/bin
+
+[Install]
+WantedBy=default.target
diff --git a/deploy/dbrain-doctor.service b/deploy/dbrain-doctor.service
new file mode 100644
index 0000000..2622ea4
--- /dev/null
+++ b/deploy/dbrain-doctor.service
@@ -0,0 +1,10 @@
+[Unit]
+Description=d-brain daily self-diagnostic
+OnFailure=dbrain-notify@%n.service
+
+[Service]
+Type=oneshot
+WorkingDirectory=%h/projects/dbrain
+ExecStart=%h/.local/bin/uv run python -m d_brain.services.doctor
+Environment=PATH=%h/.local/bin:/usr/local/bin:/usr/bin:/bin
+Environment=CLAUDE_CONFIG_DIR=%h/.claude
diff --git a/deploy/dbrain-doctor.timer b/deploy/dbrain-doctor.timer
new file mode 100644
index 0000000..99ab394
--- /dev/null
+++ b/deploy/dbrain-doctor.timer
@@ -0,0 +1,9 @@
+[Unit]
+Description=d-brain daily self-diagnostic (08:00 local)
+
+[Timer]
+OnCalendar=*-*-* 08:00:00
+Persistent=true
+
+[Install]
+WantedBy=timers.target
diff --git a/deploy/[email protected] b/deploy/[email protected]
new file mode 100644
index 0000000..41a2675
--- /dev/null
+++ b/deploy/[email protected]
@@ -0,0 +1,9 @@
+[Unit]
+Description=d-brain failure alert for %i
+
+[Service]
+Type=oneshot
+WorkingDirectory=%h/projects/dbrain
+# Independent of the bot/watchdog: only needs curl + the token from .env, so
+# it still fires when those services are crash-looping.
+ExecStart=%h/projects/dbrain/scripts/notify.sh "⚠️ d-brain: сбой юнита %i"
diff --git a/deploy/dbrain-process.service b/deploy/dbrain-process.service
new file mode 100644
index 0000000..b9b2522
--- /dev/null
+++ b/deploy/dbrain-process.service
@@ -0,0 +1,10 @@
+[Unit]
+Description=d-brain daily processing
+OnFailure=dbrain-notify@%n.service
+
+[Service]
+Type=oneshot
+WorkingDirectory=%h/projects/dbrain
+ExecStart=%h/projects/dbrain/scripts/process.sh
+Environment=PATH=%h/.local/bin:/usr/local/bin:/usr/bin:/bin
+Environment=CLAUDE_CONFIG_DIR=%h/.claude
diff --git a/deploy/d-brain-process.timer b/deploy/dbrain-process.timer
similarity index 64%
rename from deploy/d-brain-process.timer
rename to deploy/dbrain-process.timer
index 8802982..d8c4ddb 100644
--- a/deploy/d-brain-process.timer
+++ b/deploy/dbrain-process.timer
@@ -1,5 +1,5 @@
[Unit]
-Description=Run d-brain processing daily at 21:00
+Description=d-brain daily processing (21:00 local)
[Timer]
OnCalendar=*-*-* 21:00:00
diff --git a/deploy/dbrain-watchdog.service b/deploy/dbrain-watchdog.service
new file mode 100644
index 0000000..b30e630
--- /dev/null
+++ b/deploy/dbrain-watchdog.service
@@ -0,0 +1,22 @@
+[Unit]
+Description=d-brain watchdog (recovers a dead/hung Claude session)
+After=network-online.target
+StartLimitIntervalSec=300
+StartLimitBurst=5
+OnFailure=dbrain-notify@%n.service
+
+[Service]
+Type=notify
+WatchdogSec=60
+# Separate slice so an OOM kill of the brain doesn't take the watchdog down.
+Slice=dbrain-watchdog.slice
+WorkingDirectory=%h/projects/dbrain
+ExecStart=%h/.local/bin/uv run python -m d_brain.services.watchdog
+Restart=always
+RestartSec=10
+Environment=PYTHONUNBUFFERED=1
+Environment=CLAUDE_CONFIG_DIR=%h/.claude
+Environment=PATH=%h/.local/bin:/usr/local/bin:/usr/bin:/bin
+
+[Install]
+WantedBy=default.target
diff --git a/deploy/dbrain-weekly.service b/deploy/dbrain-weekly.service
new file mode 100644
index 0000000..8e05d95
--- /dev/null
+++ b/deploy/dbrain-weekly.service
@@ -0,0 +1,10 @@
+[Unit]
+Description=d-brain weekly digest
+OnFailure=dbrain-notify@%n.service
+
+[Service]
+Type=oneshot
+WorkingDirectory=%h/projects/dbrain
+ExecStart=%h/.local/bin/uv run python scripts/weekly.py
+Environment=PATH=%h/.local/bin:/usr/local/bin:/usr/bin:/bin
+Environment=CLAUDE_CONFIG_DIR=%h/.claude
diff --git a/deploy/dbrain-weekly.timer b/deploy/dbrain-weekly.timer
new file mode 100644
index 0000000..9ca8f7c
--- /dev/null
+++ b/deploy/dbrain-weekly.timer
@@ -0,0 +1,9 @@
+[Unit]
+Description=d-brain weekly digest (Friday 06:00 local)
+
+[Timer]
+OnCalendar=Fri *-*-* 06:00:00
+Persistent=true
+
+[Install]
+WantedBy=timers.target
diff --git a/deploy/tmux.conf b/deploy/tmux.conf
new file mode 100644
index 0000000..f9fa798
--- /dev/null
+++ b/deploy/tmux.conf
@@ -0,0 +1,10 @@
+# Reference tmux settings for the d-brain brain session.
+#
+# ClaudeSession applies the critical one (history-limit) via set-option after
+# new-session, so this file is informational / for `dbrain attach`. To use it
+# globally: cp deploy/tmux.conf ~/.tmux.conf
+#
+# Larger scrollback so long replies stay capturable.
+set -g history-limit 50000
+# Don't keep dead panes around — the watchdog recreates the session cleanly.
+set -g remain-on-exit off
diff --git a/scripts/notify.sh b/scripts/notify.sh
new file mode 100755
index 0000000..ac8a318
--- /dev/null
+++ b/scripts/notify.sh
@@ -0,0 +1,21 @@
+#!/bin/bash
+# Send a one-line alert to the admin Telegram chat. Standalone (only needs
+# curl + .env), so it works as a systemd OnFailure handler even when the bot
+# and watchdog are down. Usage: notify.sh "message"
+set -euo pipefail
+
+PROJECT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
+ENV_FILE="$PROJECT_DIR/.env"
+[ -f "$ENV_FILE" ] && export $(grep -v '^#' "$ENV_FILE" | xargs) || true
+
+MSG="${1:-d-brain alert}"
+CHAT_ID="${ALLOWED_USER_IDS//[\[\]]/}"
+CHAT_ID="${CHAT_ID%%,*}" # first id only
+
+if [ -z "${TELEGRAM_BOT_TOKEN:-}" ] || [ -z "$CHAT_ID" ]; then
+ echo "notify: missing token or chat id" >&2
+ exit 0
+fi
+
+curl -s -X POST "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage" \
+ -d "chat_id=$CHAT_ID" -d "text=$MSG" >/dev/null || true
diff --git a/src/d_brain/services/claude_session.py b/src/d_brain/services/claude_session.py
index 22b5896..14bdc5f 100644
--- a/src/d_brain/services/claude_session.py
+++ b/src/d_brain/services/claude_session.py
@@ -204,6 +204,8 @@ class ClaudeSession:
_PANE_HEIGHT,
self._start_command(),
)
+ # Bigger scrollback so long replies stay within capture range.
+ self._tmux("set-option", "-t", self.session_name, "history-limit", "50000")
self._tmux(
"pipe-pane",
"-t",