feat(runs): run an approved proposal's actions in a durable workflow step (#1995)
This commit is contained in:
5 files changed
+165
-9
No files matched your search
@@ -253,3 +253,7 @@ func (m *Module) beginConfirmation(ctx context.Context, in *confirmationInput) (
|
||||
}
|
||||
return httpapi.OK(Confirmation{Token: ceremony.Token, Options: jsontext.Value(ceremony.Options)}), nil
|
||||
}
|
||||
|
||||
// Irreversible reports whether approving the action needs a fresh passkey
|
||||
// assertion, as a held action does.
|
||||
func Irreversible(a judgement.Action) bool { return irreversible(a) }
|
||||
@@ -95,10 +95,13 @@ func stepBudget(ctx context.Context, settings Settings) (agentloop.Budget, error
|
||||
// the connectors and tools an agent may call, judgement, approvals, delivery
|
||||
// and memory.
|
||||
type Deps struct {
|
||||
Settings Settings
|
||||
Toolbox Toolbox
|
||||
Judge Judge
|
||||
Approver Approver
|
||||
Settings Settings
|
||||
Toolbox Toolbox
|
||||
Judge Judge
|
||||
Approver Approver
|
||||
// Proposals provides the actions of an approved proposal to the workflow
|
||||
// that runs them.
|
||||
Proposals Proposals
|
||||
Notifier Notifier
|
||||
Messenger Messenger
|
||||
Memory Memory
|
||||
@@ -665,7 +668,8 @@ func settleOne(rc *Context, judge Judge, ask Approver, settings Settings, d judg
|
||||
}); err != nil {
|
||||
return out, err
|
||||
}
|
||||
out.Result, out.Error, out.Unknown = runJudged(rc, judge, d, &answer)
|
||||
approval := answer.Approval(d)
|
||||
out.Result, out.Error, out.Unknown = runJudged(rc, judge, d, &approval)
|
||||
}
|
||||
out.State = approvals.StateApproved
|
||||
return out, nil
|
||||
@@ -711,7 +715,7 @@ func askThroughPath(rc *Context, ask Approver, settings Settings, d judgement.De
|
||||
// runJudged runs the action judged as d, on approval when there is one, and
|
||||
// returns what came of it. The action's own failure is part of what the answer
|
||||
// led to, and its step has recorded it, so the investigation still reports.
|
||||
func runJudged(rc *Context, judge Judge, d judgement.Decision, approval *approvals.Answer) (result, failure string, unknown bool) {
|
||||
func runJudged(rc *Context, judge Judge, d judgement.Decision, approval *judgement.Approval) (result, failure string, unknown bool) {
|
||||
var runErr error
|
||||
if approval == nil {
|
||||
result, runErr = runUnattended(rc, judge, d)
|
||||
@@ -820,12 +824,12 @@ func runUnattended(rc *Context, judge Judge, d judgement.Decision) (string, erro
|
||||
|
||||
// runApproved runs the action a person approved, once. judgement runs only the
|
||||
// action that was judged, whatever input comes with it.
|
||||
func runApproved(rc *Context, judge Judge, d judgement.Decision, answer approvals.Answer) (string, error) {
|
||||
func runApproved(rc *Context, judge Judge, d judgement.Decision, approval judgement.Approval) (string, error) {
|
||||
return rc.Step("run:"+d.Key, func(ctx context.Context, key string) (string, error) {
|
||||
if err := begun(ctx, rc, key); err != nil {
|
||||
return "", err
|
||||
}
|
||||
out, err := judge.ExecuteApproved(ctx, d, answer.Approval(d), d.Action.Input, key)
|
||||
out, err := judge.ExecuteApproved(ctx, d, approval, d.Action.Input, key)
|
||||
return out, unknownOutcome(err)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
package runs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/samcharles93/archie-core/modules/judgement"
|
||||
)
|
||||
|
||||
// ProposalWorkflow is the workflow that takes the actions of an approved
|
||||
// proposal. Its input is the proposal's id.
|
||||
const ProposalWorkflow = "run-proposal"
|
||||
|
||||
// proposalDocument runs an approved proposal's actions in order.
|
||||
const proposalDocument = `inputs:
|
||||
proposal_id:
|
||||
type: string
|
||||
required: true
|
||||
repository: none
|
||||
steps:
|
||||
- id: actions
|
||||
type: proposal.actions
|
||||
`
|
||||
|
||||
// ProposalDocument defines the workflow that ships with the binary to run an
|
||||
// approved proposal's actions.
|
||||
func ProposalDocument() string { return proposalDocument }
|
||||
|
||||
// The states an action of a proposal settles as.
|
||||
const (
|
||||
ActionRan = "ran"
|
||||
ActionFailed = "failed"
|
||||
ActionUnknown = "unknown"
|
||||
ActionRefused = "refused"
|
||||
ActionSkipped = "skipped"
|
||||
)
|
||||
|
||||
// ApprovedAction is one action of an approved proposal: the decision it was
|
||||
// judged under when proposed, and the approval the proposal's answer gave it.
|
||||
type ApprovedAction struct {
|
||||
Seq int
|
||||
Decision judgement.Decision
|
||||
Approval judgement.Approval
|
||||
}
|
||||
|
||||
// Proposals provides the actions of an approved proposal and records how each
|
||||
// one settled.
|
||||
type Proposals interface {
|
||||
// ApprovedActions returns the proposal's actions in order, only while the
|
||||
// proposal is approved and each approval covers exactly its action.
|
||||
ApprovedActions(ctx context.Context, proposalID uuid.UUID) ([]ApprovedAction, error)
|
||||
// SettleAction records how one action ended. Settling it again changes
|
||||
// nothing.
|
||||
SettleAction(ctx context.Context, proposalID uuid.UUID, seq int, state, outcome string) error
|
||||
}
|
||||
|
||||
type proposalActions struct{}
|
||||
|
||||
func (proposalActions) valid() error { return nil }
|
||||
|
||||
func (proposalActions) describe() string {
|
||||
return "Runs the actions of an approved proposal in order, each judged again first, and stops at the first that does not run"
|
||||
}
|
||||
|
||||
// runProposalActions takes a proposal's actions one at a time. Each is judged
|
||||
// again and runs only if that judgement still allows what the approval
|
||||
// covered. An action that does not run stops the rest, which settle as
|
||||
// skipped. Every settlement is its own step, so a resumed run repeats none.
|
||||
func runProposalActions(d Deps, rc *Context, p *progress, _ string, _ proposalActions) (StepResult, error) {
|
||||
if d.Proposals == nil {
|
||||
return StepResult{}, errors.New("proposals are not available")
|
||||
}
|
||||
id, err := uuid.Parse(fmt.Sprint(p.inputs["proposal_id"]))
|
||||
if err != nil {
|
||||
return StepResult{}, fmt.Errorf("proposal_id is not a proposal id: %w", err)
|
||||
}
|
||||
actions, err := rc.Step("proposal-actions", func(ctx context.Context, _ string) ([]ApprovedAction, error) {
|
||||
return d.Proposals.ApprovedActions(ctx, id)
|
||||
})
|
||||
if err != nil {
|
||||
return StepResult{}, err
|
||||
}
|
||||
stopped, ran := false, 0
|
||||
for _, a := range actions {
|
||||
state, outcome := ActionSkipped, "an earlier action did not run"
|
||||
if !stopped {
|
||||
out, err := takeProposed(rc, d.Judge, a)
|
||||
if err != nil {
|
||||
return StepResult{}, err
|
||||
}
|
||||
p.outcomes = append(p.outcomes, out)
|
||||
state, outcome = settlement(out)
|
||||
stopped = state != ActionRan
|
||||
}
|
||||
if state == ActionRan {
|
||||
ran++
|
||||
}
|
||||
if _, err := rc.Step(fmt.Sprintf("settle:%d", a.Seq), func(ctx context.Context, _ string) (bool, error) {
|
||||
return true, d.Proposals.SettleAction(ctx, id, a.Seq, state, outcome)
|
||||
}); err != nil {
|
||||
return StepResult{}, err
|
||||
}
|
||||
}
|
||||
return StepResult{Summary: fmt.Sprintf("%d of %d actions ran", ran, len(actions))}, nil
|
||||
}
|
||||
|
||||
// takeProposed judges one approved action again and runs it if the approval
|
||||
// still holds, as for an approval given during a run.
|
||||
func takeProposed(rc *Context, judge Judge, a ApprovedAction) (approvalOutcome, error) {
|
||||
d := a.Decision
|
||||
out := approvalOutcome{
|
||||
Action: d.Action.Connector + " " + d.Action.Name, Target: d.Action.Target.Kind + ":" + d.Action.Target.ID,
|
||||
State: "approved", digest: d.Action.Digest, By: a.Approval.By,
|
||||
}
|
||||
fresh, err := rc.Step(fmt.Sprintf("judge-again:%d", a.Seq), func(ctx context.Context, key string) (judgement.Decision, error) {
|
||||
return judge.Assess(ctx, rc.request(key, d.Action.Connector, d.Action.Name, d.Action.Input))
|
||||
})
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
switch next, why := afterApproval(d, fresh); next {
|
||||
case refuseAction, reaskPerson:
|
||||
out.State, out.Error = ActionRefused, why
|
||||
case runFresh:
|
||||
out.Result, out.Error, out.Unknown = runJudged(rc, judge, fresh, nil)
|
||||
default:
|
||||
out.Result, out.Error, out.Unknown = runJudged(rc, judge, d, &a.Approval)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// settlement is the state and text an action's outcome settles as.
|
||||
func settlement(o approvalOutcome) (state, text string) {
|
||||
switch {
|
||||
case o.State == ActionRefused:
|
||||
return ActionRefused, o.Error
|
||||
case o.Unknown:
|
||||
return ActionUnknown, o.Error
|
||||
case o.Error != "":
|
||||
return ActionFailed, o.Error
|
||||
}
|
||||
return ActionRan, o.Result
|
||||
}
|
||||
@@ -79,7 +79,7 @@ func TestActionStartedBeforeItsResultIsNeverRunAgain(t *testing.T) {
|
||||
var err error
|
||||
if tt.approved {
|
||||
d := judgement.Decision{Key: "k", Level: judgement.ProposeAndWait, Action: judgement.Action{Connector: "ops", Name: "restart"}}
|
||||
_, err = runApproved(rc, judge, d, approvals.Answer{Approved: true})
|
||||
_, err = runApproved(rc, judge, d, approvals.Answer{Approved: true}.Approval(d))
|
||||
} else {
|
||||
ctx := core.WithIdempotencyKey(t.Context(), agentKey)
|
||||
_, err = call(ctx, stubBox{}, judge, rc, decl, jsontext.Value(`{}`))
|
||||
|
||||
@@ -29,6 +29,7 @@ const (
|
||||
typeHandoff = "workflow.handoff"
|
||||
typeApprove = "workflow.approve"
|
||||
typeMessage = "message.send"
|
||||
typeProposal = "proposal.actions"
|
||||
)
|
||||
|
||||
// StepType defines a workflow step's settings and implementation.
|
||||
@@ -98,6 +99,7 @@ var stepTypes = vocabulary(
|
||||
define(typeHandoff, "Hand off", handoffSettings.valid, handoffSettings.describe, runHandoff),
|
||||
define(typeApprove, "Approval", approveSettings.valid, approveSettings.describe, runApprove),
|
||||
define(typeMessage, "Send message", messageSettings.valid, messageSettings.describe, runMessage),
|
||||
define(typeProposal, "Run a proposal's actions", proposalActions.valid, proposalActions.describe, runProposalActions),
|
||||
)
|
||||
|
||||
func vocabulary(types ...StepType) map[string]StepType {
|
||||
|
||||
Reference in new issue
Block a user