Files
archie/internal/infrastructure/postgres/task_alerts.go
T
sam caf560bdfa Feat/operator alerts (#1635)
* feat(state): durable operator alerts owned by the State Store

Alerts dedup on org+kind+subject, resolve themselves when a condition
clears, and are raised, resolved, dismissed and listed through the state
contract. Task transitions raise needs-human and failed alerts in the same
transaction; the store checks service health and prunes closed alerts.
Each raise and resolve queues a webhook delivery whose attempts are kept.

* feat(notify): deliver operator alerts to the webhook; raise config and release alerts

notify.webhook now receives raised and resolved alerts above a severity
threshold with HMAC-signed payloads, bounded retry and every attempt recorded
on the alert. archied raises model alias, provider and credential-binding
alerts from the effective configuration, an update alert from the release
channel, and a credential-rejected alert from the egress resolver. The
dashboard API lists and dismisses the caller's org's alerts.

* feat(ui): alert bell in the top bar and webhook alert settings

The bell lists the org's open alerts by severity with dismiss and a link per
alert, and refreshes from the event stream. The task settings page gains the
webhook severity threshold and signing secret. ui/dist rebuilt.
2026-10-12 03:30:46 +11:00

92 lines
3.2 KiB
Go

package postgres
import (
"context"
"errors"
"fmt"
"github.com/jackc/pgx/v5"
"github.com/samcharles93/archie-core/internal/domain/alert"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
"github.com/samcharles93/archie-core/internal/taskstate"
)
// systemResolver is recorded as the resolver of an alert the store clears
// itself because the task moved on.
const systemResolver = "system"
// recordTransition writes a transition's audit row and the alerts that
// transition raises or clears, in the same transaction. Every task status
// move goes through it, so the alerts follow the state without a poller.
func recordTransition(ctx context.Context, q *postgresdb.Queries, p postgresdb.InsertTransitionParams) error {
if err := q.InsertTransition(ctx, p); err != nil {
return err
}
return syncTaskAlerts(ctx, q, p)
}
// needsHuman reports whether a task in status, parked with class, is waiting
// on an operator's decision.
func needsHuman(status, class string) bool {
return status == workflow.StatusWaitingHuman || (status == workflow.StatusParked && class == taskstate.ParkNeedsHuman)
}
// failed reports whether a task in status, parked with class, will not
// succeed without an operator starting it over.
func failed(status, class string) bool {
return status == workflow.StatusDead || (status == workflow.StatusParked && class == taskstate.ParkTerminal)
}
func syncTaskAlerts(ctx context.Context, q *postgresdb.Queries, p postgresdb.InsertTransitionParams) error {
leaving := p.FromStatus == workflow.StatusWaitingHuman || p.FromStatus == workflow.StatusParked
if !leaving && p.ToStatus != workflow.StatusWaitingHuman && p.ToStatus != workflow.StatusParked && p.ToStatus != workflow.StatusDead {
return nil
}
info, err := q.TaskAlertInfo(ctx, p.TaskID)
if errors.Is(err, pgx.ErrNoRows) {
return nil
}
if err != nil {
return err
}
subject := fmt.Sprintf("task:%d", p.TaskID)
link := fmt.Sprintf("/tasks/%d", p.TaskID)
name := fmt.Sprintf("Task #%d", p.TaskID)
described := name
if info.Title != "" {
described = fmt.Sprintf("%s %q", name, info.Title)
}
key := func(kind string) alert.Key {
return alert.Key{Org: org.OrgID(info.OrgID), Kind: kind, Subject: subject}
}
if needsHuman(p.ToStatus, info.ParkClass) {
body := described + " is waiting for your approval or review."
if p.ToStatus == workflow.StatusParked {
body = described + " is parked and needs a decision."
}
_, err := raiseAlert(ctx, q, alert.Input{
Key: key(alert.KindTaskNeedsHuman), Severity: alert.SeverityAction, Condition: true,
Title: name + " needs you", Body: body, Link: link,
})
if err != nil {
return err
}
} else if leaving {
if err := resolveAlert(ctx, q, key(alert.KindTaskNeedsHuman), systemResolver); err != nil {
return err
}
}
if failed(p.ToStatus, info.ParkClass) {
_, err := raiseAlert(ctx, q, alert.Input{
Key: key(alert.KindTaskFailed), Severity: alert.SeverityWarning,
Title: name + " failed", Body: described + " failed and will not run again until you retry it.", Link: link,
})
return err
}
return nil
}