fix(judgement,runs,approvals): record immutable identity IDs for attribution (#2122)

This commit is contained in:
sam committed 2026-11-24 02:23:23 +11:00
1 parent eb4136ffe6
commit 4a9f30ef04
10 files changed
+38 -28

No files matched your search

+1 -1
View File
@@ -141,7 +141,7 @@ func (o *operations) Wait() {
// installStarter puts the workflow that ships with the binary in place. It
// belongs to the organisation, so it waits until setup has created one.
func installStarter(ctx context.Context, engine *runs.Engine) error {
if _, err := engine.Install(ctx, orgID, "investigate", runs.InvestigateDocument(), "starter"); err != nil {
if _, err := engine.Install(ctx, orgID, "investigate", runs.InvestigateDocument(), string(governance.SystemID)); err != nil {
return fmt.Errorf("install the investigate workflow: %w", err)
}
return nil
+5 -2
View File
@@ -176,7 +176,7 @@ type Delivery interface {
// level; approvals count towards a proposal to raise it, which a person must
// approve separately.
type ActionTypes interface {
Approved(ctx context.Context, decisionID uuid.UUID, by string, need int, deadline time.Time) (*judgement.AutonomyProposal, error)
Approved(ctx context.Context, decisionID uuid.UUID, who judgement.Answerer, need int, deadline time.Time) (*judgement.AutonomyProposal, error)
Denied(ctx context.Context, decisionID uuid.UUID) error
AnswerProposal(ctx context.Context, id uuid.UUID, who judgement.Answerer, approve bool) (bool, error)
CancelProposal(ctx context.Context, id uuid.UUID) error
@@ -311,7 +311,7 @@ func (m *Module) learn(ctx context.Context, row store.Request, a Answer) error {
if err != nil {
return fmt.Errorf("read approval deadline: %w", err)
}
proposal, err := m.actionTypes.Approved(ctx, row.DecisionID, a.By, need, a.At.Add(wait))
proposal, err := m.actionTypes.Approved(ctx, row.DecisionID, judgement.Answerer{ID: a.ByID, Name: a.By}, need, a.At.Add(wait))
if err != nil || proposal == nil {
return err
}
@@ -539,6 +539,9 @@ func (m *Module) Outcomes(ctx context.Context, ids []uuid.UUID) (map[uuid.UUID]j
if row.AnsweredBy.Valid {
o.By = row.AnsweredBy.String
}
if row.AnsweredByID.Valid {
o.ByID = row.AnsweredByID.String
}
if row.AnsweredAt.Valid {
o.At = row.AnsweredAt.Time
}
+1 -1
View File
@@ -399,7 +399,7 @@ func (m *Module) publish(ctx context.Context, in *idInput) (*httpapi.Reply[View]
return nil, huma.Error409Conflict("a workflow named " + d.Name + " already exists; rename the draft")
}
}
by := "draft:" + d.Name
by := governance.CallerID(ctx)
if strings.TrimSpace(d.Policy) != "" {
if _, err := m.policies.Append(ctx, d.PolicyKind, d.Policy, by); err != nil {
return nil, m.fail(ctx, "add the policy", err)
+9
View File
@@ -480,3 +480,12 @@ func PrincipalFromContext(ctx context.Context) (Principal, bool) {
p, ok := ctx.Value(principalContextKey{}).(Principal)
return p, ok
}
// CallerID returns the identity ID of the principal in ctx, or SystemID when
// no principal is attached.
func CallerID(ctx context.Context) string {
if p, ok := PrincipalFromContext(ctx); ok && p.IdentityID != "" {
return string(p.IdentityID)
}
return string(SystemID)
}
+7 -8
View File
@@ -37,6 +37,7 @@ func DefaultLevel(readOnly bool) Level {
type Outcome struct {
State string
By string
ByID string
At time.Time
}
@@ -115,9 +116,10 @@ type HistoryEntry struct {
Refused string `json:"refused_by,omitzero"`
// Approval is the state of the request for approval: pending, approved,
// denied or expired. It is empty when none was asked.
Approval string `json:"approval,omitzero"`
AnsweredBy string `json:"answered_by,omitzero"`
At time.Time `json:"at"`
Approval string `json:"approval,omitzero"`
AnsweredBy string `json:"answered_by,omitzero"`
AnsweredByID string `json:"answered_by_id,omitzero"`
At time.Time `json:"at"`
}
// ResetView is one return of an action type to its default.
@@ -258,7 +260,7 @@ func historyOf(decisions []store.Decision, got map[uuid.UUID]Outcome) ([]History
o := got[d.ID]
out = append(out, HistoryEntry{
DecisionID: d.ID, RunID: d.RunID, Target: body.Action.Target.ID, Level: level,
Refused: body.RefusedBy, Approval: o.State, AnsweredBy: o.By, At: d.CreatedAt,
Refused: body.RefusedBy, Approval: o.State, AnsweredBy: o.By, AnsweredByID: o.ByID, At: d.CreatedAt,
})
}
return out, nil
@@ -319,9 +321,6 @@ func (c *ActionTypes) Reset(ctx context.Context, id uuid.UUID, by string) (Actio
return viewOf(row)
}
// bootstrapAdmin names the identity the bootstrap admin token stands for.
const bootstrapAdmin = "admin"
// Register adds the action types API.
func (c *ActionTypes) Register(api huma.API, outcomes Outcomes) {
tags := []string{"Controls"}
@@ -371,7 +370,7 @@ func (c *ActionTypes) reset(ctx context.Context, in *actionTypeInput) (*httpapi.
if err != nil {
return nil, err
}
out, err := c.Reset(ctx, id, bootstrapAdmin)
out, err := c.Reset(ctx, id, governance.CallerID(ctx))
if err != nil {
return nil, c.notFound(ctx, err)
}
+4 -3
View File
@@ -91,13 +91,14 @@ type AutonomyProposal struct {
type ProposalEvidence struct {
DecisionID uuid.UUID `json:"decision_id"`
By string `json:"by"`
ByID string `json:"by_id,omitzero"`
At time.Time `json:"at"`
}
// Approved counts a person's approval of the held action decisionID, and
// returns the autonomy proposal it earns, if it does: need approvals in a row since the
// action type was last tightened. The autonomy proposal waits until deadline for an answer.
func (c *ActionTypes) Approved(ctx context.Context, decisionID uuid.UUID, by string, need int, deadline time.Time) (*AutonomyProposal, error) {
func (c *ActionTypes) Approved(ctx context.Context, decisionID uuid.UUID, who Answerer, need int, deadline time.Time) (*AutonomyProposal, error) {
row, err := c.store.ActionTypeOfDecision(ctx, c.orgID, decisionID)
if errors.Is(err, store.ErrNoActionType) {
return nil, nil
@@ -107,7 +108,7 @@ func (c *ActionTypes) Approved(ctx context.Context, decisionID uuid.UUID, by str
}
var out *AutonomyProposal
err = c.store.InActionType(ctx, c.orgID, row.ID, c.now().UTC(), func(t *store.ActionTypeTx) error {
if err := t.AddApproval(ctx, decisionID, by); err != nil {
if err := t.AddApproval(ctx, decisionID, who.Name, who.ID); err != nil {
return err
}
out, err = c.apply(ctx, t, approved, need, deadline, cause{})
@@ -289,7 +290,7 @@ func (c *ActionTypes) propose(ctx context.Context, t *store.ActionTypeTx, from,
}
shown := make([]ProposalEvidence, len(evidence))
for i, a := range evidence {
shown[i] = ProposalEvidence{DecisionID: a.DecisionID, By: a.By, At: a.At}
shown[i] = ProposalEvidence{DecisionID: a.DecisionID, By: a.By, ByID: a.ByID, At: a.At}
}
body, err := json.Marshal(shown)
if err != nil {
+6 -5
View File
@@ -167,11 +167,11 @@ func (t *ActionTypeTx) Count(ctx context.Context) (int, *AutonomyProposal, error
}
// AddApproval counts one approved decision. A decision counts once.
func (t *ActionTypeTx) AddApproval(ctx context.Context, decisionID uuid.UUID, by string) error {
func (t *ActionTypeTx) AddApproval(ctx context.Context, decisionID uuid.UUID, by, byID string) error {
if _, err := t.tx.ExecContext(ctx, `
INSERT INTO judgement.action_type_approvals (id, org_id, action_type_id, decision_id, by, at)
VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (decision_id) DO NOTHING`,
uuid.New(), t.ActionType.OrgID, t.ActionType.ID, decisionID, by, t.now); err != nil {
INSERT INTO judgement.action_type_approvals (id, org_id, action_type_id, decision_id, by, by_id, at)
VALUES ($1, $2, $3, $4, $5, $6, $7) ON CONFLICT (decision_id) DO NOTHING`,
uuid.New(), t.ActionType.OrgID, t.ActionType.ID, decisionID, by, byID, t.now); err != nil {
return fmt.Errorf("record approval: %w", err)
}
return nil
@@ -181,6 +181,7 @@ func (t *ActionTypeTx) AddApproval(ctx context.Context, decisionID uuid.UUID, by
type Approval struct {
DecisionID uuid.UUID `db:"decision_id" json:"decision_id"`
By string `db:"by" json:"by"`
ByID string `db:"by_id" json:"by_id,omitzero"`
At time.Time `db:"at" json:"at"`
}
@@ -188,7 +189,7 @@ type Approval struct {
func (t *ActionTypeTx) Approvals(ctx context.Context) ([]Approval, error) {
var out []Approval
if err := t.tx.SelectContext(ctx, &out, `
SELECT decision_id, by, at FROM judgement.action_type_approvals WHERE action_type_id = $1 AND counted ORDER BY at, id`, t.ActionType.ID); err != nil {
SELECT decision_id, by, by_id, at FROM judgement.action_type_approvals WHERE action_type_id = $1 AND counted ORDER BY at, id`, t.ActionType.ID); err != nil {
return nil, fmt.Errorf("list approvals: %w", err)
}
return out, nil
+2 -2
View File
@@ -79,7 +79,7 @@ func (p *Policies) current(ctx context.Context, kind string) (store.PolicySet, e
if !errors.Is(err, store.ErrNoPolicySet) {
return set, err
}
if _, err := p.store.AddPolicy(ctx, store.PolicySet{ID: uuid.New(), OrgID: p.orgID, Kind: kind, Body: starters[kind], CreatedBy: "starter"}); err != nil {
if _, err := p.store.AddPolicy(ctx, store.PolicySet{ID: uuid.New(), OrgID: p.orgID, Kind: kind, Body: starters[kind], CreatedBy: string(governance.SystemID)}); err != nil {
// Another writer seeded it first.
if again, rerr := p.store.Policy(ctx, p.orgID, kind, 0); rerr == nil {
return again, nil
@@ -427,7 +427,7 @@ type policyInput struct {
func (p *Policies) put(kind string) func(context.Context, *policyInput) (*httpapi.Reply[PolicySetView], error) {
return func(ctx context.Context, in *policyInput) (*httpapi.Reply[PolicySetView], error) {
out, err := p.Put(ctx, kind, in.Body.Body, bootstrapAdmin)
out, err := p.Put(ctx, kind, in.Body.Body, governance.CallerID(ctx))
switch {
case errors.Is(err, ErrInvalidPolicy):
return nil, huma.Error400BadRequest(err.Error())
+2 -5
View File
@@ -17,9 +17,6 @@ import (
"github.com/samcharles93/archie-core/modules/runs/internal/store"
)
// bootstrapAdmin is who a change made with the admin token is recorded as.
const bootstrapAdmin = "admin"
// WorkflowView is one stored version of a workflow.
type WorkflowView struct {
Name string `json:"name"`
@@ -204,7 +201,7 @@ func (a *workflowAPI) put(ctx context.Context, in *putWorkflowInput) (*httpapi.R
if !stableid.Valid(in.Name) {
return nil, huma.Error400BadRequest("a workflow name is lowercase letters and digits, joined by dots or hyphens")
}
version, err := a.e.Publish(ctx, a.orgID, in.Name, in.Body.Body, bootstrapAdmin, in.Body.ExpectedVersion)
version, err := a.e.Publish(ctx, a.orgID, in.Name, in.Body.Body, governance.CallerID(ctx), in.Body.ExpectedVersion)
if err != nil {
if errors.Is(err, ErrVersionConflict) {
return nil, huma.Error409Conflict("the workflow has another version than the one this was edited from; reload it before publishing")
@@ -340,7 +337,7 @@ func (a *workflowAPI) putDraft(ctx context.Context, in *putDraftInput) (*httpapi
if !stableid.Valid(in.Name) {
return nil, huma.Error400BadRequest("a workflow name is lowercase letters and digits, joined by dots or hyphens")
}
d, err := a.e.db.PutDraft(ctx, a.orgID, in.Name, in.Body.Body, bootstrapAdmin, in.Body.Revision)
d, err := a.e.db.PutDraft(ctx, a.orgID, in.Name, in.Body.Body, governance.CallerID(ctx), in.Body.Revision)
switch {
case errors.Is(err, store.ErrDraftConflict):
return nil, huma.Error409Conflict("the draft was changed elsewhere; load it before saving")
+1 -1
View File
@@ -1333,7 +1333,7 @@ export interface ActionType {
}
export interface ActionTypeDetail extends ActionType {
history?: { at: string; level: string; run_id?: string; target?: string; approval?: string; answered_by?: string; refused_by?: string }[];
history?: { at: string; level: string; run_id?: string; target?: string; approval?: string; answered_by?: string; answered_by_id?: string; refused_by?: string }[];
autonomy_proposals?: {
id: string;
from: string;