feat(judgement): a target's owner is looked up from the connectors at decision time, and an unknown owner is a finding that sends the request to the default owner (#1991)

This commit is contained in:
sam committed 2026-11-16 09:49:33 +11:00
1 parent 4bc0d54341
commit a4a4e97f07
15 files changed
+498 -84

No files matched your search

+1 -1
View File
@@ -435,7 +435,7 @@ func mayAnswer(who governance.Verified, row store.Request) error {
return ErrNoAuthority
case row.Kind == KindRaise:
return nil
case !who.Approver:
case !who.Approver && !slices.Contains(askedOf(row), who.ID):
return ErrNotApprover
case row.Level == judgement.Escalate.String() && !who.Authority:
return ErrNeedsAuthority
+1
View File
@@ -45,6 +45,7 @@ func TestVerifyAcceptsAnAnswerOnlyFromAPersonWhoMayGiveIt(t *testing.T) {
{name: "an unmapped identity is refused", identity: "telegram:9999", row: action(propose, "owner"), wantErr: ErrUnverified},
{name: "an unmapped identity can't answer a raise", identity: "telegram:9999", row: store.Request{Kind: KindRaise}, wantErr: ErrUnverified},
{name: "a person who isn't an approver is refused an action", identity: "telegram:1003", row: action(propose, "owner"), wantErr: ErrNotApprover},
{name: "the target's owner answers when the request was put to them", identity: "telegram:1003", row: action(propose, "bystander")},
{name: "an approver without authority is refused a raise", identity: "telegram:1001", row: store.Request{Kind: KindRaise}, wantErr: ErrNoAuthority},
{name: "authority answers a raise", identity: "telegram:1002", row: store.Request{Kind: KindRaise}},
{name: "a person on the path is refused until the request reaches them", identity: "telegram:1004", row: action(propose, "owner"), wantErr: ErrNotAsked},
+32
View File
@@ -136,6 +136,8 @@ type Connector struct {
// agent.
deliveries map[string]*jsonschema.Resolved
listens bool
// facts says it answers TargetFacts.
facts bool
}
// open configures a connector and reads its declaration.
@@ -157,6 +159,7 @@ func open(ctx context.Context, name string, client pb.ConnectorServiceClient, se
lookups: map[string]lookup{},
deliveries: map[string]*jsonschema.Resolved{},
listens: desc.GetListens(),
facts: desc.GetTargetFacts(),
}
return c, c.load(desc)
}
@@ -246,6 +249,35 @@ func (c *Connector) Listen(ctx context.Context, cursor string, store func(events
}
}
// TargetFacts is what a connector's system of record knows about a target.
// A zero field is a fact it doesn't hold.
type TargetFacts struct {
// OwnerIdentity is who answers for the target, such as "servicenow:jdoe".
OwnerIdentity string
DataClass string
Count int
}
// TargetFacts asks the connector what it knows about t. A connector that
// doesn't serve target facts, or has no record of t, supplies none.
func (c *Connector) TargetFacts(ctx context.Context, t Target) (TargetFacts, error) {
if !c.facts {
return TargetFacts{}, nil
}
resp, err := c.client.TargetFacts(ctx, &pb.TargetFactsRequest{Target: &pb.Target{
Kind: t.Kind, Id: t.ID, Environment: t.Environment, Disposable: t.Disposable,
}})
if err != nil {
return TargetFacts{}, fmt.Errorf("connector %q: target facts: %w", c.name, err)
}
if !resp.GetFound() {
return TargetFacts{}, nil
}
return TargetFacts{
OwnerIdentity: resp.GetOwnerIdentity(), DataClass: resp.GetDataClass(), Count: int(resp.GetCount()),
}, nil
}
// Declarations lists the connector's lookups and actions in name order.
func (c *Connector) Declarations() []Declaration {
out := make([]Declaration, 0, len(c.lookups)+len(c.actions))
+25
View File
@@ -132,6 +132,31 @@ func (m *Module) Tool(name string) (Tool, bool) {
return s, ok
}
// TargetFacts asks every running connector that serves target facts what it
// knows about t, now, and keeps nothing. Connectors are asked in name order and
// the first to supply a fact wins it.
func (m *Module) TargetFacts(ctx context.Context, t Target) (TargetFacts, error) {
m.mu.RLock()
asked := make([]*Connector, 0, len(m.running))
for _, name := range slices.Sorted(maps.Keys(m.running)) {
if c := m.running[name]; c.facts {
asked = append(asked, c)
}
}
m.mu.RUnlock()
var out TargetFacts
for _, c := range asked {
got, err := c.TargetFacts(ctx, t)
if err != nil {
return TargetFacts{}, err
}
out.OwnerIdentity = cmp.Or(out.OwnerIdentity, got.OwnerIdentity)
out.DataClass = cmp.Or(out.DataClass, got.DataClass)
out.Count = cmp.Or(out.Count, got.Count)
}
return out, nil
}
// Guards lists the running guard extensions in name order.
func (m *Module) Guards() []*Guard {
m.mu.RLock()
+34 -13
View File
@@ -9,8 +9,9 @@ import (
)
// PathStep is one stop on the route an unanswered approval request takes: who
// it is passed to, and how long it waits there. The first step is the default
// owner, who may be nobody, and anyone with authority.
// it is passed to, and how long it waits there. The first step is the target's
// owner when there is one, otherwise the default owner, who may be nobody, and
// then anyone with authority.
type PathStep struct {
// Person is the id of the person asked at this step, or empty.
Person string `json:"person"`
@@ -21,11 +22,23 @@ type PathStep struct {
// Deadline is how long the request waits at this step.
func (p PathStep) Deadline() time.Duration { return time.Duration(p.DeadlineMS) * time.Millisecond }
// EscalationPath returns the route an approval request takes, with its first
// step the default owner and every later step a person on the escalation path.
// When the action needs authority, a step held by someone without it is left
// out, so the request never moves to a person who could not answer it.
func (m *Module) EscalationPath(ctx context.Context, needAuthority bool) ([]PathStep, error) {
// HasDefaultOwner reports whether the organisation has a default owner.
func (m *Module) HasDefaultOwner(ctx context.Context) (bool, error) {
set, err := m.Settings(ctx)
if err != nil {
return false, err
}
return set.DefaultOwner != "", nil
}
// EscalationPath returns the route an approval request takes. owner is the
// person who answers for the action's target, or empty when none is known. A
// known owner is asked first and the default owner is the next step; an unknown
// owner leaves the default owner first. Every later step is a person on the
// escalation path. When the action needs authority, a step held by someone
// without it is left out, so the request never moves to a person who could not
// answer it.
func (m *Module) EscalationPath(ctx context.Context, owner string, needAuthority bool) ([]PathStep, error) {
set, err := m.Settings(ctx)
if err != nil {
return nil, err
@@ -34,10 +47,10 @@ func (m *Module) EscalationPath(ctx context.Context, needAuthority bool) ([]Path
if err != nil {
return nil, err
}
return set.path(people, needAuthority), nil
return set.path(people, owner, needAuthority), nil
}
func (s Settings) path(people []store.Person, needAuthority bool) []PathStep {
func (s Settings) path(people []store.Person, owner string, needAuthority bool) []PathStep {
find := func(id string) (store.Person, bool) {
i := slices.IndexFunc(people, func(p store.Person) bool { return p.ID.String() == id })
if i < 0 {
@@ -45,14 +58,22 @@ func (s Settings) path(people []store.Person, needAuthority bool) []PathStep {
}
return people[i], true
}
first := PathStep{Person: s.DefaultOwner, DeadlineMS: s.ApprovalDeadline.Milliseconds()}
defaultOwner := PathStep{Person: s.DefaultOwner, DeadlineMS: s.ApprovalDeadline.Milliseconds()}
if p, ok := find(s.DefaultOwner); ok {
first.Name = p.Name
defaultOwner.Name = p.Name
}
var out []PathStep
if p, ok := find(owner); ok && owner != "" && (p.Authority || !needAuthority) {
out = append(out, PathStep{Person: owner, Name: p.Name, DeadlineMS: s.ApprovalDeadline.Milliseconds()})
}
// With no owner to ask, the default owner leads even when nobody is set,
// so the request still goes to anyone who may answer it.
if len(out) == 0 || (s.DefaultOwner != "" && s.DefaultOwner != owner) {
out = append(out, defaultOwner)
}
out := []PathStep{first}
for _, st := range s.Escalation {
p, ok := find(st.Person)
if !ok || (needAuthority && !p.Authority) {
if !ok || (needAuthority && !p.Authority) || slices.ContainsFunc(out, func(o PathStep) bool { return o.Person == st.Person }) {
continue
}
out = append(out, PathStep{Person: st.Person, Name: p.Name, DeadlineMS: st.Deadline.Milliseconds()})
+17 -2
View File
@@ -12,7 +12,8 @@ import (
// TestPathNeverDropsBelowTheAuthorityTheActionNeeds guards the escalation
// path: an action that needs authority is never passed to a person without
// it, however the path is set, and every step keeps its own deadline.
// it, however the path is set, every step keeps its own deadline, and a target's
// known owner is asked before the default owner, which an unknown owner leaves first.
func TestPathNeverDropsBelowTheAuthorityTheActionNeeds(t *testing.T) {
owner := store.Person{ID: uuid.New(), Name: "Owner"}
peer := store.Person{ID: uuid.New(), Name: "Peer"}
@@ -28,6 +29,8 @@ func TestPathNeverDropsBelowTheAuthorityTheActionNeeds(t *testing.T) {
tests := []struct {
name string
needAuthority bool
// target is the person who answers for the action's target.
target string
want []string
wantDeadlines []time.Duration
}{
@@ -39,12 +42,24 @@ func TestPathNeverDropsBelowTheAuthorityTheActionNeeds(t *testing.T) {
name: "an action that needs authority skips a step without it", needAuthority: true,
want: []string{"Owner", "Lead"}, wantDeadlines: []time.Duration{time.Hour, 3 * time.Hour},
},
{
name: "the target's owner is asked first, then the default owner and the path", target: peer.ID.String(),
want: []string{"Peer", "Owner", "Lead"}, wantDeadlines: []time.Duration{time.Hour, time.Hour, 3 * time.Hour},
},
{
name: "a target owner without the authority an action needs is not asked", target: peer.ID.String(), needAuthority: true,
want: []string{"Owner", "Lead"}, wantDeadlines: []time.Duration{time.Hour, 3 * time.Hour},
},
{
name: "a target owner nobody knows leaves the default owner first", target: uuid.NewString(),
want: []string{"Owner", "Peer", "Lead"}, wantDeadlines: []time.Duration{time.Hour, 2 * time.Hour, 3 * time.Hour},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var names []string
var deadlines []time.Duration
for _, st := range set.path(people, tt.needAuthority) {
for _, st := range set.path(people, tt.target, tt.needAuthority) {
names = append(names, st.Name)
deadlines = append(deadlines, st.Deadline())
}
+12 -1
View File
@@ -24,7 +24,8 @@ type Verified struct {
Authority bool
// Approver is whether the person may answer an approval for an action:
// they hold authority, are the default owner, or are on the escalation
// path.
// path. A person a request was put to as the target's owner may answer
// it without.
Approver bool
}
@@ -45,6 +46,16 @@ func (m *Module) Verify(ctx context.Context, identity string) (Verified, bool, e
return Verified{ID: id, Name: p.Name, Authority: p.Authority, Approver: approver}, true, nil
}
// PersonID returns the id of the person who holds identity; ok is false when
// nobody does.
func (m *Module) PersonID(ctx context.Context, identity string) (string, bool, error) {
p, ok, err := m.store.PersonByIdentity(ctx, m.orgID, identity)
if err != nil || !ok {
return "", false, err
}
return p.ID.String(), true, nil
}
// personRequest is a person as the API shows and sets them. An empty id is a
// new person.
type personRequest struct {
+47
View File
@@ -0,0 +1,47 @@
package judgement
import (
"context"
"fmt"
"github.com/samcharles93/archie-core/modules/connectors"
)
// FactsSource answers what the connectors' systems of record know about a
// target, at the moment it is asked.
type FactsSource interface {
TargetFacts(ctx context.Context, t connectors.Target) (connectors.TargetFacts, error)
}
// Identities says which person holds an identity, such as "servicenow:jdoe".
type Identities interface {
PersonID(ctx context.Context, identity string) (id string, ok bool, err error)
}
// RecordedFacts looks a target's facts up in the connectors each time and
// keeps none. The owner a connector names counts only when the identity is a
// person Archie knows, so the request can be put to them.
type RecordedFacts struct {
Source FactsSource
People Identities
}
// Facts asks the connectors about t and maps the owner they name to a person.
func (r RecordedFacts) Facts(ctx context.Context, t connectors.Target) (TargetFacts, error) {
got, err := r.Source.TargetFacts(ctx, t)
if err != nil {
return TargetFacts{}, fmt.Errorf("ask connectors: %w", err)
}
facts := TargetFacts{Count: got.Count, DataClass: got.DataClass, OwnerIdentity: got.OwnerIdentity}
if got.OwnerIdentity == "" {
return facts, nil
}
id, ok, err := r.People.PersonID(ctx, got.OwnerIdentity)
if err != nil {
return TargetFacts{}, fmt.Errorf("match owner %s: %w", got.OwnerIdentity, err)
}
if ok {
facts.Owner = id
}
return facts, nil
}
+48 -10
View File
@@ -15,8 +15,18 @@ type TargetFacts struct {
Count int
// DataClass names what the target holds, such as "temp" or "customer".
DataClass string
// Owner is who answers for the target.
// Owner is the id of the person who answers for the target, empty when the
// target has no recorded owner or the owner isn't a person Archie knows.
Owner string
// OwnerIdentity is the owner as the source recorded them, kept so a finding
// can say who couldn't be matched to a person.
OwnerIdentity string
}
// DefaultOwners says whether the organisation has a default owner, who is
// asked when a target's owner is unknown.
type DefaultOwners interface {
HasDefaultOwner(ctx context.Context) (bool, error)
}
// TargetLookup supplies the facts the target's own declaration lacks.
@@ -24,15 +34,6 @@ type TargetLookup interface {
Facts(ctx context.Context, t connectors.Target) (TargetFacts, error)
}
// NoTargetFacts knows nothing about any target, so Impact holds every changing
// action until a connector lookup supplies the data.
type NoTargetFacts struct{}
// Facts returns no facts.
func (NoTargetFacts) Facts(context.Context, connectors.Target) (TargetFacts, error) {
return TargetFacts{}, nil
}
const (
environmentProduction = "production"
dataTemp = "temp"
@@ -49,6 +50,9 @@ var lowRiskData = []string{dataTemp, dataCache, dataNone}
// them. A datum nobody supplied holds the action at propose and wait.
type Impact struct {
Lookup TargetLookup
// Defaults, when set, lets an unknown owner's finding say that no default
// owner is set to take the request.
Defaults DefaultOwners
}
// Name is the guard's name in verdicts.
@@ -66,9 +70,24 @@ func (i Impact) Judge(ctx context.Context, a Action) (Verdict, error) {
if err != nil {
return Verdict{}, fmt.Errorf("look up target facts: %w", err)
}
v, err := i.verdict(ctx, a, facts)
v.Owner = facts.Owner
return v, err
}
// verdict folds the facts into a ceiling.
func (i Impact) verdict(ctx context.Context, a Action, facts TargetFacts) (Verdict, error) {
if missing := missingImpactData(a.Target, facts); len(missing) > 0 {
v := Verdict{Ceiling: ProposeAndWait, BlindSpots: missing}
for _, m := range missing {
if m == "owner" {
text, err := i.unknownOwner(ctx, facts)
if err != nil {
return Verdict{}, err
}
v.Findings = append(v.Findings, Finding{Text: text})
continue
}
v.Findings = append(v.Findings, Finding{Text: "no " + m + " is known for the target"})
}
return v, nil
@@ -84,6 +103,25 @@ func (i Impact) Judge(ctx context.Context, a Action) (Verdict, error) {
return Verdict{Ceiling: ActQuietly}, nil
}
// unknownOwner words the finding for a target nobody is known to own, and says
// where the request goes instead.
func (i Impact) unknownOwner(ctx context.Context, f TargetFacts) (string, error) {
text := "unknown owner: no owner is recorded for the target"
if f.OwnerIdentity != "" {
text = fmt.Sprintf("unknown owner: %s isn't a person Archie knows", f.OwnerIdentity)
}
if i.Defaults != nil {
set, err := i.Defaults.HasDefaultOwner(ctx)
if err != nil {
return "", fmt.Errorf("read default owner: %w", err)
}
if !set {
return text + ", and no default owner is set", nil
}
}
return text + ", so the request goes to the default owner", nil
}
// missingImpactData names what Impact needs and doesn't have.
func missingImpactData(t connectors.Target, f TargetFacts) []string {
var missing []string
+15
View File
@@ -84,6 +84,9 @@ type Verdict struct {
// Failed says why the guard couldn't answer; its ceiling is then
// propose and wait.
Failed string `json:"failed,omitzero"`
// Owner is the id of the person the impact guard found answering for the
// target, empty when it found none.
Owner string `json:"owner,omitzero"`
}
// Guard judges one concrete action.
@@ -120,6 +123,18 @@ type Decision struct {
At time.Time `json:"at"`
}
// Owner is the id of the person who answers for the decision's target, or empty
// when the impact guard found none. Only the built-in guard's word counts, so
// an extension can't name who is asked.
func (d Decision) Owner() string {
for _, v := range d.Verdicts {
if v.Guard == (Impact{}).Name() && v.Owner != "" {
return v.Owner
}
}
return ""
}
// Request asks for one action to be judged. Key identifies the attempt to run
// it: the same key is the same decision, so a replayed step never judges
// twice.
+2 -2
View File
@@ -29,7 +29,7 @@ import (
// model it runs, and the escalation path an approval request takes.
type Settings interface {
AgentModel(ctx context.Context) (governance.Model, error)
EscalationPath(ctx context.Context, needAuthority bool) ([]governance.PathStep, error)
EscalationPath(ctx context.Context, owner string, needAuthority bool) ([]governance.PathStep, error)
RecurrenceThreshold(ctx context.Context) (int, error)
AttemptsPerRun(ctx context.Context) (int, error)
}
@@ -362,7 +362,7 @@ func settleOne(rc *Context, judge Judge, ask Approver, settings Settings, d judg
// the request as it stood when it settled, and the answer if there was one.
func askThroughPath(rc *Context, ask Approver, settings Settings, d judgement.Decision) (approvals.Request, approvals.Answer, error) {
path, err := rc.Step("path:"+d.Key, func(ctx context.Context, _ string) ([]governance.PathStep, error) {
return settings.EscalationPath(ctx, d.Level == judgement.Escalate)
return settings.EscalationPath(ctx, d.Owner(), d.Level == judgement.Escalate)
})
if err != nil {
return approvals.Request{}, approvals.Answer{}, err