Documentation
¶
Overview ¶
Package alerts decides when the owner's phone hears about a session: at once when it shows a permission dialog, and a minute after it finishes a turn with nothing following. Pure rules over the event stream, with no clock or network of its own; the daemon feeds it and sends what it returns (ADR-036).
Index ¶
- Constants
- func CheckMessage(link string) string
- func Header(d Details) string
- func Mark(agent string) string
- func Message(a Alert, d Details) string
- func ReplyBody(reply string, code bool, limit int) string
- func TurnNeed(ev event.Event) string
- type Alert
- type Details
- type Kind
- type Notification
- type Rules
Constants ¶
const ( // FinishedAfter is how long a Stop must stand before it is "finished": an // agent pausing between turns, or an owner replying at once, pages nobody. FinishedAfter = time.Minute // Cooldown is the quietest a session may be between two alerts of one kind. // A run of permission dialogs a few seconds apart is one interruption, and // whoever answered the first is already looking. Cooldown = 3 * time.Minute // HourlyCap bounds every alert together, whatever the sessions do. HourlyCap = 20 )
Defaults, chosen so that someone at the keyboard is never paged and someone away from it hears once.
const ParseMode = "HTML"
ParseMode is the Telegram parse mode Message and CheckMessage are written for. Everything that comes from a session is escaped by html.
Variables ¶
This section is empty.
Functions ¶
func CheckMessage ¶
CheckMessage is what "Send a test alert" sends: the same shape as a real one, saying it is a test, so the person setting it up sees what will arrive.
func Header ¶ added in v0.101.0
Header is the line that says which session a message is from, in HTML: the agent's mark and name, the project and the title — "🟠 Claude Code · caprock · Fix the cache test". A part not known is left out.
func Mark ¶ added in v0.101.0
Mark is an agent's sign at the head of a message: a coloured shape a glance tells apart in a list of them, in the colours the dashboard draws the agents in.
func Message ¶
Message renders an alert as one Telegram message in HTML.
The first line says which session it is from — the agent's mark and name, the project and the session's title: "🟠 Claude Code · caprock · Fix the cache test" — because the owner runs several at once and a message that does not say whose it is cannot be acted on (ADR-047). The second says what happened: "✅ Finished", "⏳ Needs approval". Then the folder and branch, and for a finished run what it took and changed and its reply; for a dialog, the tool and the command or file it asks about. What this sends to Telegram is set out in ADR-036 and ADR-047.
func ReplyBody ¶ added in v0.101.0
ReplyBody renders an agent's reply for Telegram's HTML: the text escaped, `inline code` as code, and each fenced block either kept as a <pre> block (code true) or replaced by a one-line note saying how long it was. Cut at limit runes of the source, on a rune boundary.
Types ¶
type Alert ¶
type Alert struct {
Kind Kind
SessionID string
// Question says the dialog is AskUserQuestion's: waiting for an answer
// rather than an approval.
Question bool
// LastThisHour says this alert used the hour's final slot, so the message
// can say the rest are held back rather than leaving a silence to explain.
LastThisHour bool
// Queued is the queued message whose turn this finished alert is about
// (the Queue view); "" for an ordinary finished turn.
Queued string
// Trigger is the event the alert is about: the permission.prompt, or the
// Stop (or StopFailure, recorded as throttle) that ended the turn. The
// message reads what it asks about or how the turn ended from it.
Trigger event.Event
}
Alert is one message to send.
func (Alert) Need ¶ added in v0.146.0
Need is what a finished turn asks of its owner, read from its trigger the way the dashboard reads it (narrate, § Needs you and Done): narrate.NeedError for a StopFailure, narrate.NeedAsks when the Stop's last message asks something, narrate.NeedDone otherwise. "" for an approval.
type Details ¶ added in v0.74.0
type Details struct {
// Title is the session's name as Now shows it: the agent's own title, or
// the first prompt that says something.
Title string
// Cwd is the session's directory; Home is the owner's home directory, so
// the folder reads ~/dev/caprock rather than one of several "caprock"s.
Cwd, Home string
Branch string
// Agent is the agent's display name; AgentKey its id ("claude",
// "codex", …), which picks the mark the header opens with.
Agent string
AgentKey string
// Project is the session's project as the dashboard names it; the app's
// notification leads with it.
Project string
// Link is the session's page at an address the phone can open; empty
// when phone access is off, and then the line is left out rather than
// offering a loopback address that opens nothing.
Link string
// The run a finished alert reports: since the owner's last prompt.
Run time.Duration
CostUSD float64
Tools int
// Files are the paths the run changed, first changed first.
Files []string
// Reply is the agent's final reply; empty when the owner switched the
// line off or there is none.
Reply string
// LongReply sends the reply whole, up to longReplyMaxRunes, rather than
// its first line: a session the owner talks to from Telegram (ADR-047).
// Code says its code blocks go too; without it each is a one-line
// placeholder.
LongReply, Code bool
}
Details is what the daemon knows about the session an alert is about. Any field may be empty; the message leaves out what it does not know.
type Notification ¶ added in v0.76.0
type Notification struct {
// ID names this alert and nothing else, so a frame replayed after a
// reconnect is recognised as one already shown.
ID string `json:"id"`
Kind string `json:"kind"`
SessionID string `json:"session_id"`
Project string `json:"project,omitempty"`
// Title is what a lock screen shows first: what happened and where,
// "Needs approval · caprock".
Title string `json:"title"`
// Body is the session, then what it asks about or what the run did.
Body string `json:"body"`
// PromptID is the permission prompt the session waits on (ADR-035) when
// Caprock can answer it; Actions are the answers offered for it.
PromptID string `json:"prompt_id,omitempty"`
Actions []string `json:"actions,omitempty"`
// Route is the dashboard route a click opens when the notification is
// about no session: "#/x/<id>" for a private module's (ADR-076).
Route string `json:"route,omitempty"`
}
Notification is an alert as the app shows it: the `notify` frame on /v1/live (.ai/03-contracts.md § Notify frame). The decision is the one Telegram reads; only the rendering differs — plain text, and it never leaves the machine.
func Notify ¶ added in v0.76.0
func Notify(a Alert, d Details, promptID string) Notification
Notify renders an alert for the app. promptID is the owned session's waiting prompt, empty when there is none Caprock can answer; with one, the notification offers Deny, and Approve too when it shows the whole request.
type Rules ¶
type Rules struct {
// Enabled says whether a kind is switched on right now. A switched-off
// kind is still tracked, so turning it on later starts from the truth.
Enabled func(Kind) bool
// EnabledFor, when set, decides instead of Enabled, with what a finished
// turn asks (Alert.Need): a Done turn may be wanted by one sender and
// not another.
EnabledFor func(k Kind, need string) bool
// contains filtered or unexported fields
}
Rules holds what the alerts depend on: which sessions are waiting or stopped, and what was sent when. Not safe for concurrent use.
func (*Rules) Due ¶
Due returns the finished alerts whose minute has passed, and forgets sessions with nothing left to decide.
func (*Rules) Observe ¶
Observe takes one stored event and returns the alerts it causes now: an approval alert for a permission dialog, at most one per dialog.
func (*Rules) QueuedDone ¶ added in v0.110.0
QueuedDone is the end of a turn a queued message started: a finished alert at once rather than after a quiet minute, since the next message goes in straight after it and the minute never comes. Under the same switch, per-session cooldown and hourly cap as every alert, so a queue of short turns is one message every few minutes and the minute alert for the last one is not sent twice.