Files
loki/scripts/ci/runtime-conformance-audit.sh
CatoandClaude Opus 5.5 f8da012c95 feat(notify): Telegram only with a one-tap action — one gateway for every bot (#1540)
* fix(alerts): stop paging on transient systemd-run units; send digests silently

The 5-minute host sweep announced every failed one-off systemd-run unit
(run-p<pid>-i<id>) as its own incident. Each has a fresh name, so no
per-unit cooldown held and the phone rang every five minutes.

The sweep now resets those units and never announces them, and the
STILL FAILING / RECOVERED digests are delivered with disable_notification.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtzUbLhg3oqt4X1NaN

* fix(alerts): quiet by default, ring only for down/intrusion; stop deploy and cron echoes

- lib-alert: all box alerts deliver silently except DOWN/deploy failure/
  intrusion-class messages (_alert_silent)
- host sweep: failed appcron oneshots are the OnFailure notifier's, not repeated
- security check: files a deploy rewrites by design are journal-only

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtzUbLhg3oqt4X1NaN

* ci(box): install the alerting scripts on the box when they change

The alert fixes only take effect once install-host-alerts.sh and
install-security-watch.sh run on the box, and nothing ran them.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtzUbLhg3oqt4X1NaN

* feat(notify): one tap or not at all — every bot reaches the owner through one gateway

The owner's Telegram held ~1,200 unread messages and rang every five minutes
with lines he could do nothing with ("STILL FAILING: run-p…", "SENSITIVE
FILE(S) CHANGED … if this was a deploy, ignore", "built nothing"). New rule,
enforced in code: a message reaches the phone only with a button that does
the thing; everything else is recorded on /today or /system.

- telegram-send: refuses a message without a URL button (NOT_ACTIONABLE);
  adds silent delivery and sendTelegramVoice.
- lib/notify: one gateway (deliverNotice) with the policy — at most 3
  buttons, one incident one message with backed-off reminders, 3/source/hour
  and 12/day caps, sound only for "now" and never 22:00–07:00 Zurich. Voice
  notes for paid plans; a monthly "voice notes come with Personal" notice
  with its pay button for free accounts.
- Routes: /api/notify (token holders, `loki.sh notify`), /api/notify/box
  (the box's lib-alert), /api/orangecat/notify (OrangeCat's bots, signed).
- Box: lib-alert posts every line to Loki's list instead of Telegram; only an
  intrusion goes straight to the phone when Loki is unreachable, with a Box
  doctor button. ivy-health pages only "down and restart failed" with
  Redeploy; clean deploy rollbacks are not paged; fleet-uptime pages only for
  Loki or 3+ apps down; sleeping apps are no longer conformance findings.
- App senders: each kept one now carries its button (Triage it, Review it,
  Watch the fix, Write to them, Restart the gateway, Decide on Control …);
  telemetry/runner/model-id pings and run successes leave Telegram.
- Day plan: the morning brief's prompt is in the repo
  (scripts/openclaw/day-plan) and installed on merge, which also switches off
  jobs that only post swap/memory warnings.
- Workflows install the box alerting, ivy-health, the day plan and OpenClaw's
  photo/video understanding on merge instead of by hand.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtzUbLhg3oqt4X1NaN

* feat(telegram): every sender is silent between 22:00 and 07:00 unless it says otherwise

Feedback, studio briefs and the other direct senders skipped the notify
gateway's quiet hours. A send that does not ask for sound now arrives
silently at night in the owner's zone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtzUbLhg3oqt4X1NaN

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-02 07:04:45 +01:00

136 lines
6.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# runtime-conformance-audit.sh — check the RUNNING system, not the source.
#
# WHY THIS EXISTS. bitbaum/fleet already has hosted-supabase-audit.sh, whose
# stated job is "does any repo still point at a Supabase we retired?". It ran on
# 2026-08-28 and reported SUCCESS while printcraft's production API returned 500,
# because it pointed at a hosted project whose DNS no longer resolved.
#
# It missed because it runs `git grep` over repo checkouts, and the reference
# lived in /opt/printcraft/shared/.env — not in git, deliberately, because the
# box is the environment SSOT. The gate was scoped to the wrong substrate: it
# checked the artifact while the configuration lived in the running system.
#
# So the rule this file implements: SOURCE audits run in CI against git;
# RUNTIME audits run here against what is actually deployed. The fleet had a
# good collection of the first kind and none of the second.
#
# Read-only. Prints one line per finding, exits 1 if any. Never prints a secret.
set -u
export LC_ALL=C
BOX_NODE="$(node --version 2>/dev/null | tr -d 'v')"
BOX_NODE_MAJOR="${BOX_NODE%%.*}"
findings=0
unreadable=0
note() { printf '%s\n' "$*"; }
finding() { findings=$((findings+1)); printf 'FINDING|%s|%s\n' "$1" "$2"; }
cannot() { unreadable=$((unreadable+1)); printf 'UNREADABLE|%s|%s\n' "$1" "$2"; }
# Hosts we have deliberately left. A managed service we no longer pay for is
# not a dependency we still want discovered by an outage.
RETIRED_RE='supabase\.co|neon\.tech|planetscale\.com|railway\.app|render\.com|\.vercel-storage\.com'
for dir in /opt/*/; do
app="$(basename "$dir")"
case "$app" in _appcron|backups|monitoring|supabase) continue ;; esac
env=""
for cand in "$dir/shared/.env" "$dir/app/.env"; do
[ -f "$cand" ] && { env="$cand"; break; }
done
[ -n "$env" ] || continue
[ -r "$env" ] || { cannot "$app" "env not readable: $env"; continue; }
# ---- 1. a retired managed host still referenced -------------------------
hits="$(grep -oE "https?://[A-Za-z0-9._-]*($RETIRED_RE)" "$env" 2>/dev/null | sort -u | tr '\n' ' ')"
if [ -n "${hits// /}" ]; then
finding "$app" "env still points at a retired managed host: $hits"
fi
# ---- 2. every configured host actually resolves -------------------------
# A URL that no longer resolves is the shape printcraft was in for weeks
# while its pages still returned 200.
while read -r host; do
[ -z "$host" ] && continue
case "$host" in localhost|127.0.0.1|0.0.0.0) continue ;; esac
if ! getent hosts "$host" >/dev/null 2>&1; then
finding "$app" "configured host does not resolve: $host"
fi
done < <(grep -oE '^[A-Z_]+=[^#]*https?://[A-Za-z0-9._-]+' "$env" 2>/dev/null \
| grep -oE 'https?://[A-Za-z0-9._-]+' | sed 's#https\?://##' | sort -u)
# ---- 3. the running process vs the file that is supposed to produce it ---
# Config can be broken on disk for hours while the process still holds the
# old values in memory. That is a PENDING outage, and a restart detonates it.
unit="${app}-app.service"
if systemctl cat "$unit" >/dev/null 2>&1; then
pid="$(systemctl show "$unit" -p MainPID --value 2>/dev/null)"
if [ -n "$pid" ] && [ "$pid" != "0" ] && [ -r "/proc/$pid/environ" ]; then
disk="$(grep -oE '^[A-Z][A-Z0-9_]*=' "$env" | sed 's/=$//' | sort -u)"
proc="$(tr '\0' '\n' < "/proc/$pid/environ" | grep -oE '^[A-Z][A-Z0-9_]*=' | sed 's/=$//' \
| grep -vE '^(PATH|HOME|USER|LOGNAME|SHELL|PWD|OLDPWD|LANG|LC_ALL|TERM|SHLVL|_|HOSTNAME|PORT|NODE_ENV|NODE_VERSION|INVOCATION_ID|JOURNAL_STREAM|SYSTEMD_EXEC_PID|MEMORY_PRESSURE_WATCH|MEMORY_PRESSURE_WRITE|NOTIFY_SOCKET|MANAGERPID)$' \
| sort -u)"
lost="$(comm -13 <(printf '%s\n' "$disk") <(printf '%s\n' "$proc") | tr '\n' ' ')"
if [ -n "${lost// /}" ]; then
finding "$app" "running with keys its .env can no longer provide (lost on next restart): $lost"
fi
fi
fi
# ---- 4. declared node engine vs the node that will run it ---------------
for pj in "$dir/app/package.json" "$dir/package.json"; do
[ -f "$pj" ] || continue
want="$(grep -oE '"node"[[:space:]]*:[[:space:]]*"[^"]+"' "$pj" 2>/dev/null | head -1 | grep -oE '[0-9]+' | head -1)"
if [ -n "$want" ] && [ -n "$BOX_NODE_MAJOR" ] && [ "$want" -gt "$BOX_NODE_MAJOR" ]; then
finding "$app" "declares node >=$want but this box runs $BOX_NODE"
fi
break
done
done
# ---- 5. every app unit MEANT to run is running, and still has its guards ----
# Enabled-but-not-active, not merely not-active. aoz-demo is stopped AND
# disabled with nothing routed to its port — deliberately retired, and flagging
# it as a fault is how a check earns the reputation that gets it ignored.
# "Is it supposed to be running?" is a different question from "is it running?".
for u in /etc/systemd/system/*-app.service; do
[ -e "$u" ] || continue
unit="$(basename "$u")"; app="${unit%-app.service}"
enabled="$(systemctl is-enabled "$unit" 2>/dev/null)"
case "$enabled" in
enabled|enabled-runtime|static) ;;
*) continue ;;
esac
state="$(systemctl is-active "$unit" 2>/dev/null)"
# An app that sleeps when idle (lib.sh sleep-when-idle) is inactive BY DESIGN
# while its wake socket listens: the first request starts it. On 2026-11-02
# at 03:02 this line sent fifteen "🧩 <app>: unit is enabled but inactive"
# messages at once — every sleeping app, none of them broken.
if [ "$state" != "active" ] && systemctl is-active --quiet "${app}-wake.socket" 2>/dev/null; then
state="active"
fi
[ "$state" = "active" ] || finding "$app" "unit is enabled but $state"
# A rename moves the unit file; it does NOT move the .service.d/ drop-ins
# beside it. evig-app ran for two days after such a rename with no memory
# cap on a shared box and no OnFailure, because the drop-ins were still
# sitting in the OLD unit's directory. Nothing in git describes them — they
# are box state — so the running system is the only thing that can be asked,
# and every other app unit here carries both, which is what makes an
# exception a finding rather than a preference.
[ "$(systemctl show "$unit" -p MemoryMax --value 2>/dev/null)" = "infinity" ] \
&& finding "$app" "unit has no memory cap (MemoryMax=infinity) on a shared box"
[ -n "$(systemctl show "$unit" -p OnFailure --value 2>/dev/null)" ] \
|| finding "$app" "unit has no OnFailure= — it can die without alerting anyone"
done
note "checked $(ls -d /opt/*/ 2>/dev/null | wc -l) app dirs on node $BOX_NODE"
note "findings=$findings unreadable=$unreadable"
# Absence of a finding across zero readable apps is not a pass.
if [ "$unreadable" -gt 0 ] && [ "$findings" -eq 0 ]; then
note "NOTE: $unreadable app(s) could not be read — clean is not proven"
fi
[ "$findings" -eq 0 ]