Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
Add daily provider health check as a native launchd job
Jack Rudenko1fee72d294d4
diff --git a/scripts/com.10xlabs.claudish-healthcheck.plist b/scripts/com.10xlabs.claudish-healthcheck.plistnew file mode 100644index 00000000..d2aa22f0--- /dev/null+++ b/scripts/com.10xlabs.claudish-healthcheck.plist@@ -0,0 +1,34 @@+<?xml version="1.0" encoding="UTF-8"?>+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">+<plist version="1.0">+<dict>+ <key>Label</key>+ <string>com.10xlabs.claudish-healthcheck</string>++ <key>ProgramArguments</key>+ <array>+ <string>/Users/jack/mag/claudish/scripts/healthcheck-launchd.sh</string>+ </array>++ <!-- 07:00 local time, every day. launchd tracks local time, so this follows+ the AEST/AEDT changeover on its own - no cron to re-point in October. -->+ <key>StartCalendarInterval</key>+ <dict>+ <key>Hour</key><integer>7</integer>+ <key>Minute</key><integer>0</integer>+ </dict>++ <key>WorkingDirectory</key>+ <string>/Users/jack/mag/claudish</string>++ <key>StandardOutPath</key>+ <string>/Users/jack/mag/claudish/logs/health/launchd.stdout.log</string>+ <key>StandardErrorPath</key>+ <string>/Users/jack/mag/claudish/logs/health/launchd.stderr.log</string>++ <key>RunAtLoad</key>+ <false/>+ <key>ProcessType</key>+ <string>Background</string>+</dict>+</plist>diff --git a/scripts/healthcheck-launchd.sh b/scripts/healthcheck-launchd.shnew file mode 100755index 00000000..eb180193--- /dev/null+++ b/scripts/healthcheck-launchd.sh@@ -0,0 +1,45 @@+#!/bin/bash+# Wrapper for the launchd LaunchAgent that runs the daily provider health check.+#+# launchd gives a job almost no environment — not the PATH from your shell rc —+# so everything the check needs is spelled out here rather than inherited.+#+# This runs as a LaunchAgent (not a LaunchDaemon) on purpose: an Agent runs+# inside Jack's logged-in GUI session, which is what makes the macOS Keychain+# credential path work without a prompt. A Daemon would run outside the session+# and every keychain read would fail.+#+# If the Mac is asleep at the scheduled time, launchd runs the job on the next+# wake rather than skipping the day.+set -u++export PATH="/opt/homebrew/bin:/usr/local/bin:$HOME/.bun/bin:/usr/bin:/bin:/usr/sbin:/sbin"+REPO="$HOME/mag/claudish"+LOG_DIR="$REPO/logs/health"+mkdir -p "$LOG_DIR"+LOG="$LOG_DIR/launchd.log"++{+ echo "=== $(date -u +%Y-%m-%dT%H:%M:%SZ) starting daily provider health check ==="+ cd "$REPO" || { echo "repo missing at $REPO"; exit 2; }++ if ! command -v bun >/dev/null 2>&1; then echo "bun not on PATH"; exit 2; fi+ if ! command -v claudish >/dev/null 2>&1; then echo "claudish not on PATH"; exit 2; fi++ bun scripts/daily-provider-healthcheck.ts --batch 5 --probe-timeout 20+ CODE=$?+ echo "=== exit $CODE ==="++ # Alerting is done here, on this machine, so the whole job survives Cowork+ # being unreachable. healthcheck-notify.sh reads its verdict out of the+ # result.json this run just wrote, and pulls the Slack webhook and SMTP+ # password from the login keychain. A missing credential skips that channel+ # rather than failing the run.+ RUN_DIR=$(ls -1d "$LOG_DIR"/*/ 2>/dev/null | tail -1)+ if [ -n "$RUN_DIR" ]; then+ "$REPO/scripts/healthcheck-notify.sh" "${RUN_DIR%/}" "$CODE" || echo "notify step failed (non-fatal)"+ else+ echo "no run directory produced - nothing to notify about"+ fi+ exit $CODE+} >> "$LOG" 2>&1diff --git a/scripts/healthcheck-notify.sh b/scripts/healthcheck-notify.shnew file mode 100755index 00000000..f0a92fad--- /dev/null+++ b/scripts/healthcheck-notify.sh@@ -0,0 +1,125 @@+#!/bin/bash+# healthcheck-notify.sh — send the health-check verdict to Jack, from the Mac itself.+#+# scripts/healthcheck-notify.sh <run-dir> <exit-code>+# scripts/healthcheck-notify.sh --test # send a fake alert to prove wiring+#+# Deliberately has NO dependency on Cowork, Claude, or any cloud service: the+# whole point of moving this job to launchd was that it keeps working when none+# of that is reachable. Everything here is curl and the macOS Keychain.+#+# CREDENTIALS come from the login keychain, never from a file in the repo and+# never from the environment (launchd jobs have no useful environment anyway):+#+# Slack incoming webhook:+# security add-generic-password -a "$USER" -s claudish-health-slack -w 'https://hooks.slack.com/services/...'+#+# Gmail app password, for the email copy (optional):+# security add-generic-password -a "$USER" -s claudish-health-smtp -w 'your-16-char-app-password'+#+# Either credential being absent is not an error — that channel is simply+# skipped and noted in the log. A health check that fails to alert should still+# leave a report behind, so nothing here is allowed to abort the run.+set -u++REPO="$HOME/mag/claudish"+EMAIL_TO="[email protected]"+EMAIL_FROM="[email protected]"+SMTP_URL="smtps://smtp.gmail.com:465"++kc() { security find-generic-password -a "$USER" -s "$1" -w 2>/dev/null; }++notify_mac() {+ # Always available, needs no credential, and is the one channel that works+ # even with no network. Fire it first so something lands regardless.+ osascript -e "display notification \"$2\" with title \"$1\"" >/dev/null 2>&1+}++post_slack() {+ local text="$1" hook+ hook="$(kc claudish-health-slack)"+ if [ -z "$hook" ]; then echo "[notify] no Slack webhook in keychain (claudish-health-slack) — skipped"; return 0; fi+ # --data-binary with a heredoc-built JSON file avoids every quoting trap that+ # comes with embedding a multi-line report in an inline -d argument.+ local payload; payload="$(mktemp)"+ python3 - "$text" > "$payload" <<'PY'+import json,sys+print(json.dumps({"text": sys.argv[1]}))+PY+ local code+ code=$(curl -sS -o /dev/null -w '%{http_code}' -X POST -H 'Content-Type: application/json' \+ --data-binary @"$payload" --max-time 25 "$hook" 2>&1)+ rm -f "$payload"+ echo "[notify] slack http $code"+}++send_email() {+ local subject="$1" body="$2" pass+ pass="$(kc claudish-health-smtp)"+ if [ -z "$pass" ]; then echo "[notify] no SMTP app password in keychain (claudish-health-smtp) — skipped"; return 0; fi+ local msg; msg="$(mktemp)"+ {+ echo "From: $EMAIL_FROM"+ echo "To: $EMAIL_TO"+ echo "Subject: $subject"+ echo "Content-Type: text/plain; charset=utf-8"+ echo+ echo "$body"+ } > "$msg"+ curl -sS --url "$SMTP_URL" --ssl-reqd \+ --mail-from "$EMAIL_FROM" --mail-rcpt "$EMAIL_TO" \+ --upload-file "$msg" --user "$EMAIL_FROM:$pass" --max-time 40 \+ && echo "[notify] email sent" || echo "[notify] email FAILED"+ rm -f "$msg"+}++if [ "${1:-}" = "--test" ]; then+ notify_mac "claudish health TEST" "Alert wiring test - not a real failure."+ post_slack ":test_tube: *claudish health check - wiring test*+This is not a real failure. If you can read this, the Slack path works."+ send_email "claudish health: wiring test" "Not a real failure. If you can read this, the email path works."+ exit 0+fi++RUN_DIR="${1:-}"+CODE="${2:-1}"+[ -d "$RUN_DIR" ] || { echo "[notify] no run dir '$RUN_DIR'"; exit 1; }++# Everything below is read out of result.json rather than recomputed, so the+# alert can never disagree with the report sitting next to it.+read -r BROKEN NEWIDS NEWPROV HEADLINE <<EOF2+$(python3 - "$RUN_DIR/result.json" <<'PY'+import json,sys+d=json.load(open(sys.argv[1]))+broken=d.get("brokenCount",0)+newids=",".join(d.get("newlyBrokenIds") or []) or "-"+newprov=",".join(d.get("newlyBrokenProviders") or []) or "-"+print(broken, newids, newprov, d.get("modelCount",0))+PY+)+EOF2++if [ "$CODE" -eq 0 ]; then+ echo "[notify] clean run - staying quiet by design"+ exit 0+fi++DETAIL="$(sed -n '/## Failure detail/,$p' "$RUN_DIR/report.md" | head -60)"+SUMMARY="$(head -8 "$RUN_DIR/report.md")"++notify_mac "claudish health check FAILED" "$BROKEN of $HEADLINE models failed. New: $NEWIDS"++post_slack ":rotating_light: *claudish provider health - $BROKEN of $HEADLINE models failed*+Newly broken models: \`$NEWIDS\`+Newly broken providers: \`$NEWPROV\`++\`\`\`+$DETAIL+\`\`\`+Report: \`$RUN_DIR/report.md\`"++send_email "claudish health: $BROKEN model(s) failed" "$SUMMARY++$DETAIL++Full report: $RUN_DIR/report.md"