mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 22:08:18 +01:00
* 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>
202 lines
10 KiB
Bash
202 lines
10 KiB
Bash
#!/usr/bin/env bash
|
|
# loki.sh — Loki API wrapper for the chat agent's `loki` skill.
|
|
#
|
|
# Installed onto bitbaum at
|
|
# /home/openclaw/.openclaw/workspace/skills/loki/scripts/loki.sh
|
|
# by scripts/openclaw/install-loki-skill.sh.
|
|
#
|
|
# TWO THINGS WENT WRONG HERE, AND BOTH ARE WHY THIS FILE IS NOW IN THE REPO.
|
|
#
|
|
# It lived ONLY on the box, so the seam between the operator's chat and this
|
|
# app's action queue was unreviewable and undiffable. And on 2026-09-14 the
|
|
# product was renamed to Loki — the env file's keys were renamed with it, but
|
|
# this script was not, because nothing that runs in CI could see it. Every
|
|
# command in it had been aborting on a missing variable ever since:
|
|
#
|
|
# line 14: FLEE…_AGENT_TOKEN: …_AGENT_TOKEN missing in calendar-drain.env
|
|
#
|
|
# So from Telegram the operator could not list the approval queue, could not
|
|
# approve anything, and could not dispatch — silently, because a skill that
|
|
# errors just makes the agent talk about something else. The only surviving
|
|
# route to a decision was the website, which is exactly what they complained
|
|
# about. A file no gate can reach is a file that rots where nobody is looking.
|
|
#
|
|
# Auth: the box's existing agent token (SSOT: calendar-drain.env — the same
|
|
# ck_* token the calendar drain uses). No token is ever printed.
|
|
set -euo pipefail
|
|
|
|
ENV_FILE="/home/openclaw/.openclaw/calendar-drain.env"
|
|
[ -f "$ENV_FILE" ] || { echo "error: $ENV_FILE not found (token source)"; exit 1; }
|
|
# shellcheck disable=SC1090
|
|
set -a; source "$ENV_FILE"; set +a
|
|
BASE="${LOKI_API_URL:-http://127.0.0.1:4002}"
|
|
TOKEN="${LOKI_AGENT_TOKEN:?LOKI_AGENT_TOKEN missing in $ENV_FILE}"
|
|
|
|
api() { # api <method> <path> [json-body]
|
|
local method=$1 path=$2 body=${3:-}
|
|
local args=( -sS -m 30 -X "$method" "$BASE$path"
|
|
-H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" )
|
|
[ -n "$body" ] && args+=( -d "$body" )
|
|
local out http
|
|
out=$(curl "${args[@]}" -w $'\n%{http_code}')
|
|
http=${out##*$'\n'}
|
|
out=${out%$'\n'*}
|
|
if [ "${http:0:1}" != "2" ]; then
|
|
# Error pages can be full HTML — keep chat-sized. Prefer the JSON error field.
|
|
local msg
|
|
msg=$(echo "$out" | jq -r '.error // empty' 2>/dev/null || true)
|
|
[ -n "$msg" ] || msg=$(echo "$out" | head -c 200)
|
|
echo "API error (HTTP $http): $msg" >&2
|
|
return 1
|
|
fi
|
|
echo "$out"
|
|
}
|
|
|
|
jesc() { # JSON-escape a string via jq
|
|
jq -Rn --arg v "$1" '$v'
|
|
}
|
|
|
|
resolve_id() { # resolve_id <prefix> — full id from pending list, fail on 0 or >1
|
|
local prefix=$1 matches
|
|
matches=$(api GET /api/actions/pending | jq -r --arg p "$prefix" '.pending[].id | select(startswith($p))')
|
|
local n
|
|
n=$(echo "$matches" | grep -c . || true)
|
|
if [ "$n" -eq 0 ]; then echo "error: no pending action matches '$prefix'" >&2; return 1; fi
|
|
if [ "$n" -gt 1 ]; then echo "error: '$prefix' is ambiguous ($n matches) — use more characters" >&2; return 1; fi
|
|
echo "$matches"
|
|
}
|
|
|
|
cmd=${1:-help}
|
|
case "$cmd" in
|
|
dispatch)
|
|
project=${2:?usage: loki.sh dispatch <project> "<task>"}
|
|
task=${3:?usage: loki.sh dispatch <project> "<task>"}
|
|
api POST /api/inject "{\"tab\":$(jesc "$project"),\"customPrompt\":$(jesc "$task"),\"notifyOnClose\":true}" | jq .
|
|
;;
|
|
hosted)
|
|
project=${2:?usage: loki.sh hosted <project> "<task>"}
|
|
task=${3:?usage: loki.sh hosted <project> "<task>"}
|
|
api POST /api/hermes/dispatch "{\"projectKey\":$(jesc "$project"),\"task\":$(jesc "$task")}" | jq .
|
|
;;
|
|
book)
|
|
# Put an appointment in the operator's calendar.
|
|
#
|
|
# WHY THIS COMMAND EXISTS. `gog calendar create` is blocked on this box by
|
|
# the safety shim (/usr/local/bin/gog), because an agent that can write to
|
|
# the real calendar unattended — on a heartbeat, from a cron — is not a
|
|
# thing anyone signed up for. But until now the skill had no OTHER way to
|
|
# book either, so a plain "put the dentist in my calendar" hit the shim and
|
|
# dead-ended in advice to go and use a website. The shim was doing its job;
|
|
# there was simply no sanctioned road.
|
|
#
|
|
# This is that road. It proposes a STRUCTURED event (real start/end fields,
|
|
# not a date buried in prose) to the action queue. What happens next is the
|
|
# operator's own standing decision, made in advance at /approvals:
|
|
# - covered by a standing approval → approved and booked by the calendar
|
|
# drain within about fifteen seconds, and they get a Telegram
|
|
# confirmation once it is genuinely in the calendar;
|
|
# - not covered → it waits, and they get one-tap Approve/Reject buttons.
|
|
# (Below, an event the operator asked for in their own words and that has no
|
|
# guests is approved straight away — see the comment at the decision call.)
|
|
# Either way the answer comes back HERE, in `status`, so the agent can tell
|
|
# them what actually happened instead of guessing.
|
|
#
|
|
# Times must be absolute, with an offset (the API will not invent a
|
|
# timezone): 2026-09-19T14:00:00+02:00. A bare YYYY-MM-DD means all-day.
|
|
title=${2:?usage: loki.sh book "<title>" <start> [end] [location]}
|
|
start=${3:?usage: loki.sh book "<title>" <start> [end] [location]}
|
|
end=${4:-}
|
|
location=${5:-}
|
|
|
|
if [[ "$start" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]; then
|
|
when="\"eventDate\":$(jesc "$start"),\"allDay\":true"
|
|
else
|
|
when="\"eventStart\":$(jesc "$start")"
|
|
[ -n "$end" ] && when="$when,\"eventEnd\":$(jesc "$end")"
|
|
fi
|
|
[ -n "$location" ] && when="$when,\"eventLocation\":$(jesc "$location")"
|
|
|
|
out=$(api POST /api/actions/propose \
|
|
"{\"type\":\"create_event\",\"title\":$(jesc "$title"),\"operatorRequested\":true,\"payload\":{\"eventTitle\":$(jesc "$title"),$when}}")
|
|
# The operator just asked for this event in words. `book` takes no guests, so
|
|
# there is no invitation to send and nothing a second "yes" would protect:
|
|
# asking them to tap Approve for the thing they typed is the friction that
|
|
# made chat feel worse than the website. Decide it ourselves, and say so.
|
|
# An event that needs a real decision (a date that did not resolve) still
|
|
# waits — the API reports that as awaiting-approval with a reason.
|
|
status=$(echo "$out" | jq -r '.status // empty')
|
|
id=$(echo "$out" | jq -r '.action.id // empty')
|
|
reason=$(echo "$out" | jq -r '.reason // empty')
|
|
if [ "$status" = "awaiting-approval" ] && [ -n "$id" ] && [ -z "${LOKI_BOOK_ASK:-}" ] \
|
|
&& [[ ! "$reason" =~ (guest|date|resolve) ]]; then
|
|
api POST "/api/actions/$id/decision" '{"decision":"approve"}' >/dev/null \
|
|
&& out=$(echo "$out" | jq '.status = "approved-by-request" | .note = "approved because the operator asked for it in chat"')
|
|
fi
|
|
echo "$out" | jq '{status, note, reason, executed, deferred, id: .action.id, title: .action.title, deduped}'
|
|
;;
|
|
remind|todo)
|
|
# A commitment: something the operator owes or wants to remember. Internal and
|
|
# reversible (Loki's own table), so a chat request is approved on the spot.
|
|
text=${2:?usage: loki.sh remind "<what>" [YYYY-MM-DD]}
|
|
due=${3:-}
|
|
pl="{\"commitment\":$(jesc "$text")"
|
|
[ -n "$due" ] && pl="$pl,\"dueDate\":$(jesc "$due")"
|
|
pl="$pl}"
|
|
out=$(api POST /api/actions/propose \
|
|
"{\"type\":\"create_commitment\",\"title\":$(jesc "$text"),\"operatorRequested\":true,\"payload\":$pl}")
|
|
id=$(echo "$out" | jq -r '.action.id // empty')
|
|
status=$(echo "$out" | jq -r '.status // empty')
|
|
if [ "$status" = "awaiting-approval" ] && [ -n "$id" ]; then
|
|
api POST "/api/actions/$id/decision" '{"decision":"approve"}' | jq '{ok, executed, error}'
|
|
else
|
|
echo "$out" | jq '{status, executed, id: .action.id, deduped}'
|
|
fi
|
|
;;
|
|
note)
|
|
# A sticky note on the Today page. For "remember that…", links, ideas.
|
|
text=${2:?usage: loki.sh note "<text>"}
|
|
api POST /api/captures "{\"body\":$(jesc "$text")}" | jq '{ok, id: .capture.id}'
|
|
;;
|
|
agenda)
|
|
# Read-only: what is in the calendar. `gog calendar list` reads; only writes are shimmed.
|
|
days=${2:-7}
|
|
case "$days" in ''|*[!0-9]*) days=7 ;; esac
|
|
gog calendar list --json --days "$days" 2>/dev/null \
|
|
| jq -r 'if type=="array" then . else (.events // []) end
|
|
| map("\(.start.dateTime // .start.date // .start // "?") \(.summary // .title // "(no title)")")
|
|
| .[]' 2>/dev/null \
|
|
|| echo "calendar unavailable right now"
|
|
;;
|
|
pending)
|
|
api GET /api/actions/pending | jq '.pending'
|
|
;;
|
|
notify)
|
|
# notify "<title>" "<button label>" "<https url>" [body] [now|today]
|
|
# The one way a job or agent reaches George's phone. No button, no message:
|
|
# Loki's gateway refuses it and keeps it on /today instead.
|
|
title=${2:?usage: loki.sh notify "<title>" "<button>" "<https-url>" [body] [now|today]}
|
|
label=${3:?usage: loki.sh notify "<title>" "<button>" "<https-url>" [body] [now|today]}
|
|
url=${4:?usage: loki.sh notify "<title>" "<button>" "<https-url>" [body] [now|today]}
|
|
body=${5:-}; urgency=${6:-today}
|
|
payload=$(jq -n --arg t "$title" --arg l "$label" --arg u "$url" --arg b "$body" --arg g "$urgency" \
|
|
'{source:"loki", title:$t, actions:[{label:$l, url:$u}], urgency:$g} + (if $b == "" then {} else {body:$b} end)')
|
|
api POST /api/notify "$payload" | jq '{sent, rang, why}'
|
|
;;
|
|
decide)
|
|
prefix=${2:?usage: loki.sh decide <id-or-prefix> approve|reject}
|
|
decision=${3:?usage: loki.sh decide <id-or-prefix> approve|reject}
|
|
case "$decision" in approve|reject) ;; *) echo "error: decision must be approve or reject" >&2; exit 1 ;; esac
|
|
id=$(resolve_id "$prefix")
|
|
api POST "/api/actions/$id/decision" "{\"decision\":\"$decision\"}" | jq .
|
|
;;
|
|
help|*)
|
|
echo "usage: loki.sh dispatch <project> \"<task>\" | hosted <project> \"<task>\""
|
|
echo " loki.sh book \"<title>\" <start> [end] [location]"
|
|
echo " loki.sh remind \"<what>\" [YYYY-MM-DD] (a commitment / to-do)"
|
|
echo " loki.sh note \"<text>\" (a sticky note on Today)"
|
|
echo " loki.sh agenda [days] (read the calendar)"
|
|
echo " loki.sh pending | decide <id> approve|reject"
|
|
echo " loki.sh notify \"<title>\" \"<button>\" \"<https-url>\" [body] [now|today]"
|
|
;;
|
|
esac
|