Files
pipelines/internal/pipeline/run_notifications.go
T
alan b3c9575fde Runs park on usage limits; failure classes, harness error policy, run budgets
- A stage refused for a Claude usage limit no longer fails its run: the
  runner tries the chats' Claude account auto-switch (per-home setting) and
  reruns the visit, else parks the run in the new waiting_quota status
  (persisted, with reset/resume time in run summaries, details and events)
  and resumes the same visit at reset + 1 min, also after a restart.
  resume_run / `pipelines resume` resumes now; cancel works on parked runs.
  The rerun visit gets a "# Previous attempt" interruption note. Subscription
  limits no longer say "top up the provider account".
- run/retry into an account known to be limited are deferred (quota_policy
  defer/refuse/now; CLI refuses under --json/--yes unless --defer or --now).
- failure_class on runs (quota, loop_exhausted, timeout, permission_denied,
  harness_error, blocked, environment, canceled, interrupted, budget,
  check_failed, other) in RunSummary/RunDetail/events, CLI inspect/runs/wait/
  stats and the app failure line.
- Claude permission denials (permission_denials / refused tool results) fail
  a blocked visit with permission_denied naming the tool use.
- Harness failures are classified transient vs deterministic; deterministic
  ones (and a repeated empty readback) skip error self-loops; transient
  self-loops back off 15 s, 1 min, 4 min.
- Whole-run USD budget: run --budget, server default (run-budget /
  get/set_run_settings); 80% warning notification, stop with class budget.
- App: waiting_quota status/filter with resume time, Resume now and Cancel,
  failure class wording. Docs and skill references updated.

Merges main (check stage, delivery gate, Claude stage restart survival,
schedule changes).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 14:34:53 +00:00

208 lines
8.8 KiB
Go

package pipeline
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"strings"
"time"
"unicode/utf8"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// RunNotification is durable publisher evidence, not proof of device reception.
// A sending lease permits restart recovery; a crash after publish but before
// acknowledgement can repeat a message. Attempts are bounded, not guaranteed.
type RunNotification struct {
RunID int64
Sequence int
Event, Channel, Title, Body, Click, Status, Diagnostic, MessageID string
Attempts int
}
// enqueueRunTransitionNotification must share the state mutation's transaction.
// Read the committed-to-be state, not a runner's proposed outcome or identity.
func enqueueRunTransitionNotification(ctx context.Context, tx *sql.Tx, id int64) error {
var mode, status, name, stage, diagnostic, failureKind, graphJSON string
var sequence int
if err := tx.QueryRowContext(ctx, `SELECT r.notify_mode,r.status,p.name,r.current_stage,r.diagnostic,r.failure_kind,
COALESCE((SELECT MAX(sequence) FROM run_steps WHERE run_id=r.id),0),v.graph_json
FROM pipeline_runs r JOIN pipelines p ON p.id=r.pipeline_id
JOIN pipeline_versions v ON v.pipeline_id=r.pipeline_id AND v.version=r.version WHERE r.id=?`, id).Scan(&mode, &status, &name, &stage, &diagnostic, &failureKind, &sequence, &graphJSON); err != nil {
return err
}
if mode == "off" {
return nil
}
var title, body string
switch status {
case "waiting":
title = "Run needs your decision"
body = "waiting for a human decision"
case protocol.RunStatusWaitingQuota:
title = "Run paused for a usage limit"
body = "waiting for the usage limit to reset"
case "failed", "interrupted", "succeeded":
if status == "failed" && failureKind == "expected" && mode != "on" {
settings, err := readNotificationSettings(ctx, tx)
if err != nil {
return err
}
if !settings.RunFinished {
return nil
}
}
// Successful runs notify only for runs marked "on", unless the operator
// opted into every run end via run_finished, read in this transaction.
if status == "succeeded" && mode != "on" {
settings, err := readNotificationSettings(ctx, tx)
if err != nil {
return err
}
if !settings.RunFinished {
return nil
}
}
title = "Run " + status
body = "run " + status
default:
return nil
}
body = fmt.Sprintf("Run #%d · %s · %s", id, name, body)
if stage != "" {
body += " at stage " + stage
}
if diagnostic != "" {
body += ": " + diagnostic
}
body = strings.NewReplacer("\r", " ", "\n", " ", "\x00", " ").Replace(body)
if len(body) > 2000 {
body = body[:2000]
for !utf8.ValidString(body) {
body = body[:len(body)-1]
}
}
// Give a declared stage hook first opportunity to send its artifacts. Its
// initial claim bypasses this delay. If the process dies before that callback,
// the generic durable alert becomes eligible after one minute, not lost.
var graph protocol.Graph
if err := json.Unmarshal([]byte(graphJSON), &graph); err != nil {
return err
}
stageEvent := status
if status == "succeeded" {
stageEvent = "success"
}
if status == "failed" {
stageEvent = "failure"
}
var nextAttempt int64
for _, definition := range graph.Stages {
if definition.Key != stage || definition.Notify == nil {
continue
}
for _, event := range definition.Notify.On {
if event == stageEvent {
nextAttempt = time.Now().Add(time.Minute).Unix()
}
}
}
_, err := tx.ExecContext(ctx, `INSERT INTO run_notifications(run_id,sequence,event,channel,title,body,click,next_attempt)
VALUES (?,?,?,'system',?,?,?,?) ON CONFLICT(run_id,sequence,event,channel) DO NOTHING`, id, sequence, status, title, body, fmt.Sprintf("pipelines://run/%d", id), nextAttempt)
return err
}
// ClaimStageRunNotification arbitrates a transition hook against the outbox worker.
// With no transactional intent (legacy/direct stage callers), it leaves the
// stage's existing publication path alone. Otherwise the same lease protects
// both publishers, so only one may publish this event.
func (s *Store) ClaimStageRunNotification(ctx context.Context, id int64, sequence int, event string, now int64) (*RunNotification, bool, error) {
n := &RunNotification{RunID: id, Sequence: sequence, Event: event, Channel: "system"}
err := s.db.QueryRowContext(ctx, `UPDATE run_notifications SET status='sending',attempts=attempts+1,next_attempt=?
WHERE run_id=? AND sequence=? AND event=? AND channel='system' AND status='pending' AND attempts<3 AND (attempts=0 OR next_attempt<=?)
RETURNING attempts`, now+60, id, sequence, event, now).Scan(&n.Attempts)
if err == nil {
return n, true, nil
}
if !errors.Is(err, sql.ErrNoRows) {
return nil, false, err
}
var exists bool
err = s.db.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM run_notifications WHERE run_id=? AND sequence=? AND event=? AND channel='system')`, id, sequence, event).Scan(&exists)
return nil, !exists, err
}
func (s *Store) initRunNotifications() error {
_, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS run_notifications (
run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, event TEXT NOT NULL, channel TEXT NOT NULL,
title TEXT NOT NULL, body TEXT NOT NULL, click TEXT NOT NULL,
status TEXT NOT NULL DEFAULT 'pending', attempts INTEGER NOT NULL DEFAULT 0,
next_attempt INTEGER NOT NULL DEFAULT 0, diagnostic TEXT NOT NULL DEFAULT '', message_id TEXT NOT NULL DEFAULT '',
PRIMARY KEY(run_id,sequence,event,channel));
CREATE INDEX IF NOT EXISTS run_notifications_due ON run_notifications(status,next_attempt);`)
return err
}
func (s *Store) EnqueueRunNotification(ctx context.Context, n RunNotification) error {
_, err := s.db.ExecContext(ctx, `INSERT OR IGNORE INTO run_notifications
(run_id,sequence,event,channel,title,body,click) VALUES (?,?,?,?,?,?,?)`, n.RunID, n.Sequence, n.Event, n.Channel, n.Title, n.Body, n.Click)
return err
}
// ClaimRunNotification atomically leases one due attempt for 60 seconds. Three
// attempts total bound both transient failures and crash/restart loops.
func (s *Store) ClaimRunNotification(ctx context.Context, now int64) (*RunNotification, error) {
if _, err := s.db.ExecContext(ctx, `UPDATE run_notifications SET status='failed',diagnostic='publisher acknowledgement unknown after final lease expired' WHERE status='sending' AND attempts>=3 AND next_attempt<=?`, now); err != nil {
return nil, err
}
n := new(RunNotification)
err := s.db.QueryRowContext(ctx, `UPDATE run_notifications SET status='sending', attempts=attempts+1, next_attempt=?
WHERE rowid=(SELECT rowid FROM run_notifications WHERE status IN ('pending','sending') AND attempts<3 AND next_attempt<=? ORDER BY next_attempt,rowid LIMIT 1)
RETURNING run_id,sequence,event,channel,title,body,click,status,attempts`, now+60, now).Scan(&n.RunID, &n.Sequence, &n.Event, &n.Channel, &n.Title, &n.Body, &n.Click, &n.Status, &n.Attempts)
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
return n, err
}
// CoverRunNotification records a successful stage publication covering the same
// run event, without publishing a second final alert.
func (s *Store) CoverRunNotification(ctx context.Context, id int64, sequence int, event string) error {
_, err := s.db.ExecContext(ctx, `INSERT INTO run_notifications(run_id,sequence,event,channel,title,body,click,status) VALUES (?,?,?,'system','','','','covered')
ON CONFLICT(run_id,sequence,event,channel) DO UPDATE SET status='covered',diagnostic='' WHERE run_notifications.status IN ('pending','sending')`, id, sequence, event)
return err
}
func (s *Store) FinishRunNotification(ctx context.Context, n RunNotification, now int64, messageID string, failure error) error {
status, diagnostic := "sent", ""
if failure != nil {
status, diagnostic = "pending", failure.Error()
if n.Attempts >= 3 {
status = "failed"
}
}
_, err := s.db.ExecContext(ctx, `UPDATE run_notifications SET status=?,diagnostic=?,message_id=?,next_attempt=?,click=?
WHERE run_id=? AND sequence=? AND event=? AND channel=? AND status='sending' AND attempts=?`, status, diagnostic, messageID, now+int64(n.Attempts*5), n.Click, n.RunID, n.Sequence, n.Event, n.Channel, n.Attempts)
return err
}
func (s *Store) RunNotifications(ctx context.Context, id int64) ([]RunNotification, error) {
rows, err := s.db.QueryContext(ctx, `SELECT run_id,sequence,event,channel,status,attempts,diagnostic,message_id FROM run_notifications WHERE run_id=? ORDER BY sequence,event,channel`, id)
if err != nil {
return nil, err
}
defer rows.Close()
result := []RunNotification{}
for rows.Next() {
var n RunNotification
if err := rows.Scan(&n.RunID, &n.Sequence, &n.Event, &n.Channel, &n.Status, &n.Attempts, &n.Diagnostic, &n.MessageID); err != nil {
return nil, err
}
result = append(result, n)
}
return result, rows.Err()
}