Files
archie/internal/infrastructure/postgres/task_alerts.go
T
sam c265b07ec7 refactor(workflow): stop assuming a human decides
An approval, ask or park can be resolved by an operator, a rule, a
classifier or automatically, so the vocabulary stops naming a person:

- step type human.approve -> workflow.approve
- park class needs_human -> needs_decision (ParkNeedsDecision)
- event kinds human_* -> operator_*
- alert kind task.needs_human -> task.needs_decision
- status grouping "needs you" -> "needs attention"

0061 rewrites the stored values, including the approval step type in the
workflow-definitions resource and each task's pinned definition, with the
digest recomputed so resumed tasks still match.
2026-10-25 17:31:16 +11:00

101 lines
3.3 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)
}
// needsDecision reports whether a task in status, parked with class, is waiting
// on an operator's decision.
func needsDecision(status, class string) bool {
return status == workflow.StatusWaitingApproval || (status == workflow.StatusParked && class == taskstate.ParkNeedsDecision)
}
// 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.StatusWaitingApproval || p.FromStatus == workflow.StatusParked
if !leaving && p.ToStatus != workflow.StatusWaitingApproval && 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)
title := name
if info.Title != "" {
title = truncate(info.Title, 80)
}
key := func(kind string) alert.Key {
return alert.Key{Org: org.OrgID(info.OrgID), Kind: kind, Subject: subject}
}
if needsDecision(p.ToStatus, info.ParkClass) {
body := name + " is waiting for approval or review."
if p.ToStatus == workflow.StatusParked {
body = name + " is parked and needs a decision."
}
_, err := raiseAlert(ctx, q, alert.Input{
Key: key(alert.KindTaskNeedsDecision), Severity: alert.SeverityAction, Condition: true,
Title: title, Body: body, Link: link,
})
if err != nil {
return err
}
} else if leaving {
if err := resolveAlert(ctx, q, key(alert.KindTaskNeedsDecision), 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: title, Body: name + " failed.", Link: link,
})
return err
}
return nil
}
// truncate shortens s to at most n runes, marking the cut with an ellipsis.
func truncate(s string, n int) string {
r := []rune(s)
if len(r) <= n {
return s
}
return string(r[:n-1]) + "…"
}