feat(judgement,approvals,governance,ui): a class earns a raise after N approvals and drops on denial or failure; a freeze holds changing actions (#2004)
This commit is contained in:
17 files changed
+1185
-58
No files matched your search
+126
-12
@@ -47,9 +47,17 @@ var (
|
||||
ErrExpired = errors.New("that request has expired")
|
||||
)
|
||||
|
||||
// The kinds of request. A raise asks whether an action class may act with one
|
||||
// level more autonomy; no run waits on it.
|
||||
const (
|
||||
KindAction = "action"
|
||||
KindRaise = "raise"
|
||||
)
|
||||
|
||||
// Request is one judged action waiting for a person's answer.
|
||||
type Request struct {
|
||||
ID uuid.UUID
|
||||
Kind string
|
||||
RunID uuid.UUID
|
||||
// Key is the run's wait key, stored so an answer knows what to wake.
|
||||
Key string
|
||||
@@ -86,6 +94,7 @@ func (a Answer) Approval(d judgement.Decision) judgement.Approval {
|
||||
// View is a request as the API shows it.
|
||||
type View struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Kind string `json:"kind"`
|
||||
RunID uuid.UUID `json:"run_id"`
|
||||
Action string `json:"action"`
|
||||
Target string `json:"target"`
|
||||
@@ -104,7 +113,7 @@ type View struct {
|
||||
// View is r as the API shows it.
|
||||
func (r Request) View() View {
|
||||
return View{
|
||||
ID: r.ID, RunID: r.RunID, Action: r.Connector + " " + r.Action,
|
||||
ID: r.ID, Kind: r.Kind, RunID: r.RunID, Action: r.Connector + " " + r.Action,
|
||||
Target: r.TargetKind + ":" + r.TargetID, Level: r.Level,
|
||||
Summary: r.Summary, State: r.State, Deadline: r.Deadline,
|
||||
DecisionID: r.DecisionID, RequestedAt: r.RequestedAt,
|
||||
@@ -124,20 +133,39 @@ type Delivery interface {
|
||||
Notify(ctx context.Context, n deliveries.Notification) error
|
||||
}
|
||||
|
||||
// Classes is where answers become autonomy: an approval counts toward raising
|
||||
// an action class, a denial tightens it, and a raise a person approves
|
||||
// loosens it.
|
||||
type Classes interface {
|
||||
Approved(ctx context.Context, decisionID uuid.UUID, by string, need int, deadline time.Time) (*judgement.Raise, error)
|
||||
Denied(ctx context.Context, decisionID uuid.UUID) error
|
||||
AnswerRaise(ctx context.Context, id uuid.UUID, by string, approve bool) (bool, error)
|
||||
CancelRaise(ctx context.Context, id uuid.UUID) error
|
||||
}
|
||||
|
||||
// Policy is what the organisation decided about approvals.
|
||||
type Policy interface {
|
||||
ApprovalDeadline(ctx context.Context) (time.Duration, error)
|
||||
RaiseAfter(ctx context.Context) (int, error)
|
||||
}
|
||||
|
||||
// Module owns one organisation's approval requests.
|
||||
type Module struct {
|
||||
orgID string
|
||||
store *store.Store
|
||||
tell Delivery
|
||||
wake Signaller
|
||||
log *slog.Logger
|
||||
now func() time.Time
|
||||
orgID string
|
||||
store *store.Store
|
||||
tell Delivery
|
||||
wake Signaller
|
||||
classes Classes
|
||||
policy Policy
|
||||
log *slog.Logger
|
||||
now func() time.Time
|
||||
}
|
||||
|
||||
// NewModule returns the approvals module for orgID. Answers are delivered by
|
||||
// tell and wake the run through wake.
|
||||
func NewModule(db *sqlx.DB, log *slog.Logger, orgID string, tell Delivery, wake Signaller) *Module {
|
||||
return &Module{orgID: orgID, store: store.New(db), tell: tell, wake: wake, log: log, now: time.Now}
|
||||
// tell and wake the run through wake, and are counted into classes under
|
||||
// policy.
|
||||
func NewModule(db *sqlx.DB, log *slog.Logger, orgID string, tell Delivery, wake Signaller, classes Classes, policy Policy) *Module {
|
||||
return &Module{orgID: orgID, store: store.New(db), tell: tell, wake: wake, classes: classes, policy: policy, log: log, now: time.Now}
|
||||
}
|
||||
|
||||
// Ask records a request for d and tells people about it. Asking again for the
|
||||
@@ -146,7 +174,7 @@ func NewModule(db *sqlx.DB, log *slog.Logger, orgID string, tell Delivery, wake
|
||||
func (m *Module) Ask(ctx context.Context, runID uuid.UUID, key string, d judgement.Decision, deadline time.Time) (Request, error) {
|
||||
row, err := m.store.Put(ctx, store.Request{
|
||||
ID: uuid.New(), OrgID: m.orgID, RunID: runID, Key: key,
|
||||
DecisionID: d.ID, Digest: d.Action.Digest, Level: d.Level.String(),
|
||||
DecisionID: d.ID, Kind: KindAction, Digest: d.Action.Digest, Level: d.Level.String(),
|
||||
Connector: d.Action.Connector, Action: d.Action.Name,
|
||||
TargetKind: d.Action.Target.Kind, TargetID: d.Action.Target.ID,
|
||||
Summary: Summary(d), State: StatePending, Deadline: deadline.UTC(),
|
||||
@@ -188,12 +216,98 @@ func (m *Module) Answer(ctx context.Context, id uuid.UUID, by, via string, appro
|
||||
return Request{}, fmt.Errorf("%w: %s", ErrAnswered, row.State)
|
||||
}
|
||||
answer := Answer{RequestID: row.ID, Approved: approved, By: by, Via: via, At: now}
|
||||
if row.Kind == KindRaise {
|
||||
if err := m.answerRaise(ctx, row, answer); err != nil {
|
||||
return Request{}, err
|
||||
}
|
||||
return requestOf(row, judgement.Decision{}), nil
|
||||
}
|
||||
if err := m.wakeWith(ctx, row, answer); err != nil {
|
||||
return Request{}, err
|
||||
}
|
||||
// The run has what it waited for; the answer's bearing on the class comes
|
||||
// after, so a failure here is reported without holding the run.
|
||||
if err := m.learn(ctx, row, answer); err != nil {
|
||||
return Request{}, err
|
||||
}
|
||||
return requestOf(row, judgement.Decision{}), nil
|
||||
}
|
||||
|
||||
// learn counts an answer to an action into its class. An approval that
|
||||
// completes a run of them earns a raise, which is put to a person.
|
||||
func (m *Module) learn(ctx context.Context, row store.Request, a Answer) error {
|
||||
if !a.Approved {
|
||||
return m.classes.Denied(ctx, row.DecisionID)
|
||||
}
|
||||
need, err := m.policy.RaiseAfter(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read raise count: %w", err)
|
||||
}
|
||||
wait, err := m.policy.ApprovalDeadline(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read approval deadline: %w", err)
|
||||
}
|
||||
raise, err := m.classes.Approved(ctx, row.DecisionID, a.By, need, a.At.Add(wait))
|
||||
if err != nil || raise == nil {
|
||||
return err
|
||||
}
|
||||
return m.askRaise(ctx, raise)
|
||||
}
|
||||
|
||||
// askRaise puts a raise to a person on every channel. If it can't be asked the
|
||||
// raise is withdrawn, so the class proposes it again at its next approval.
|
||||
func (m *Module) askRaise(ctx context.Context, r *judgement.Raise) error {
|
||||
class := r.Class
|
||||
row, err := m.store.Put(ctx, store.Request{
|
||||
ID: uuid.New(), OrgID: m.orgID, Key: "raise:" + r.ID.String(),
|
||||
DecisionID: r.ID, Kind: KindRaise, Level: r.From.String(),
|
||||
Connector: class.Connector, Action: class.Action, TargetKind: class.TargetKind, TargetID: class.Environment,
|
||||
Summary: raiseSummary(r), State: StatePending, Deadline: r.Deadline,
|
||||
})
|
||||
if err == nil {
|
||||
err = m.tell.Ask(ctx, deliveries.ApprovalRequest{
|
||||
RequestID: row.ID, Key: row.Key + ":ask",
|
||||
Title: "Raise autonomy: " + class.Connector + " " + class.Action,
|
||||
Body: row.Summary, ExpiresAt: row.Deadline,
|
||||
})
|
||||
}
|
||||
if err != nil {
|
||||
if cerr := m.classes.CancelRaise(ctx, r.ID); cerr != nil {
|
||||
err = errors.Join(err, cerr)
|
||||
}
|
||||
return fmt.Errorf("ask to raise a class: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// answerRaise applies a person's answer to a raise. No run waits on it.
|
||||
func (m *Module) answerRaise(ctx context.Context, row store.Request, a Answer) error {
|
||||
raised, err := m.classes.AnswerRaise(ctx, row.DecisionID, a.By, a.Approved)
|
||||
if err != nil {
|
||||
return fmt.Errorf("answer raise: %w", err)
|
||||
}
|
||||
text := answerText(a)
|
||||
if a.Approved && !raised {
|
||||
text += " The class had changed since, so its level stays."
|
||||
}
|
||||
return m.tell.Told(ctx, deliveries.ApprovalRequest{
|
||||
RequestID: row.ID, Key: row.Key + ":ask", Title: "Raise answered",
|
||||
}, text)
|
||||
}
|
||||
|
||||
// raiseSummary is what a person is shown about a raise: the class, the change
|
||||
// and the approvals that earned it.
|
||||
func raiseSummary(r *judgement.Raise) string {
|
||||
c := r.Class
|
||||
var s strings.Builder
|
||||
fmt.Fprintf(&s, "%s: %s on %s in %s. Raise from %s to %s.\nEarned by %d approvals in a row:",
|
||||
c.Connector, c.Action, c.TargetKind, c.Environment, r.From, r.To, len(r.Evidence))
|
||||
for _, e := range r.Evidence {
|
||||
fmt.Fprintf(&s, "\n- %s, %s", e.By, e.At.UTC().Format("2006-01-02 15:04 UTC"))
|
||||
}
|
||||
return s.String()
|
||||
}
|
||||
|
||||
// Settle answers a request from an answer a channel delivered as an event. An
|
||||
// answer that can't settle it, because it was already answered, has expired
|
||||
// or names no request, reports false rather than an error: retrying the event
|
||||
@@ -439,7 +553,7 @@ func inputLine(input jsontext.Value) string {
|
||||
// requestOf is a stored row as the module's Request.
|
||||
func requestOf(row store.Request, d judgement.Decision) Request {
|
||||
r := Request{
|
||||
ID: row.ID, RunID: row.RunID, Key: row.Key, Decision: d, DecisionID: row.DecisionID, RequestedAt: row.CreatedAt,
|
||||
ID: row.ID, Kind: row.Kind, RunID: row.RunID, Key: row.Key, Decision: d, DecisionID: row.DecisionID, RequestedAt: row.CreatedAt,
|
||||
Connector: row.Connector, Action: row.Action, TargetKind: row.TargetKind, TargetID: row.TargetID,
|
||||
Level: row.Level, Summary: row.Summary, State: row.State, Deadline: row.Deadline,
|
||||
}
|
||||
|
||||
@@ -15,11 +15,14 @@ import (
|
||||
|
||||
// Request is one judged action waiting for a person's answer.
|
||||
type Request struct {
|
||||
ID uuid.UUID `db:"id"`
|
||||
OrgID string `db:"org_id"`
|
||||
RunID uuid.UUID `db:"run_id"`
|
||||
Key string `db:"key"`
|
||||
ID uuid.UUID `db:"id"`
|
||||
OrgID string `db:"org_id"`
|
||||
RunID uuid.UUID `db:"run_id"`
|
||||
Key string `db:"key"`
|
||||
// DecisionID is the judged action the request is about, or for a raise
|
||||
// request the raise.
|
||||
DecisionID uuid.UUID `db:"decision_id"`
|
||||
Kind string `db:"kind"`
|
||||
Digest string `db:"digest"`
|
||||
// The action's own names, kept so a request reads back without its
|
||||
// decision.
|
||||
@@ -45,7 +48,7 @@ type Store struct {
|
||||
// New returns a store on db.
|
||||
func New(db *sqlx.DB) *Store { return &Store{db: db} }
|
||||
|
||||
const requestColumns = `id, org_id, run_id, key, decision_id, digest, connector, action, target_kind,
|
||||
const requestColumns = `id, org_id, run_id, key, decision_id, kind, digest, connector, action, target_kind,
|
||||
target_id, level, summary, state, deadline, created_at, answered_at, answered_by, answered_via`
|
||||
|
||||
// Put records r unless a request already exists for its run and key, and
|
||||
@@ -53,12 +56,12 @@ const requestColumns = `id, org_id, run_id, key, decision_id, digest, connector,
|
||||
func (s *Store) Put(ctx context.Context, r Request) (Request, error) {
|
||||
var out Request
|
||||
err := s.db.GetContext(ctx, &out, `
|
||||
INSERT INTO approvals.requests (id, org_id, run_id, key, decision_id, digest, connector, action,
|
||||
INSERT INTO approvals.requests (id, org_id, run_id, key, decision_id, kind, digest, connector, action,
|
||||
target_kind, target_id, level, summary, state, deadline)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15)
|
||||
ON CONFLICT (org_id, run_id, key) DO NOTHING
|
||||
RETURNING `+requestColumns,
|
||||
r.ID, r.OrgID, r.RunID, r.Key, r.DecisionID, r.Digest, r.Connector, r.Action, r.TargetKind,
|
||||
r.ID, r.OrgID, r.RunID, r.Key, r.DecisionID, r.Kind, r.Digest, r.Connector, r.Action, r.TargetKind,
|
||||
r.TargetID, r.Level, r.Summary, r.State, r.Deadline)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return s.Get(ctx, r.OrgID, r.RunID, r.Key)
|
||||
|
||||
Reference in new issue
Block a user