Files
sam 90637aa457 fix(runs): settle held actions a skipped or failed approval would leave
The end-of-run decision read the document's last approval step alone, so a conditional approval that was skipped, or one that continued past a failure, let a run finish without ever putting its held actions to people. Those approvals no longer count as settling. Resume still replays the agent and the request exactly once.

Triggers and dry runs now refuse a document that names an identity or profile, dry runs check required inputs, and step ids cannot contain dots that references read as separators.

The shipped investigate document gains a test that holds an action, suspends, resumes through the journal and reports one approved action.

Refs #2039
2026-11-21 01:20:21 +11:00

118 lines
3.9 KiB
Go

package runs
import (
"context"
"encoding/json/jsontext"
"log/slog"
"strings"
"testing"
"github.com/google/uuid"
"github.com/samcharles93/archie-core/modules/connectors"
"github.com/samcharles93/archie-core/modules/judgement"
"github.com/samcharles93/archie-core/modules/runs/internal/store"
)
type decidingJudge struct {
decision judgement.Decision
executed int
}
func (j *decidingJudge) Assess(context.Context, judgement.Request) (judgement.Decision, error) {
return j.decision, nil
}
func (j *decidingJudge) Execute(context.Context, judgement.Decision, jsontext.Value, string) (string, error) {
j.executed++
return "done", nil
}
func (j *decidingJudge) ExecuteApproved(context.Context, judgement.Decision, judgement.Approval, jsontext.Value, string) (string, error) {
j.executed++
return "done", nil
}
func (j *decidingJudge) ForRun(context.Context, uuid.UUID) ([]judgement.Decision, error) {
return nil, nil
}
type anyBox struct{}
func (anyBox) Declarations() []connectors.Declaration { return nil }
func (anyBox) Tool(string) (connectors.Tool, bool) { return nil, true }
// TestStartDryRefusesWhatCannotStart guards the dry-run entry point against
// testing a document a live run could not start: a declared identity or
// profile, or a missing required input. The document is refused before a run
// is queued.
func TestStartDryRefusesWhatCannotStart(t *testing.T) {
engine := newEngine(nil, &fakeWork{}, slog.New(slog.DiscardHandler), Options{})
tests := []struct {
name string
doc string
want string
}{
{
name: "identity",
doc: "repository: none\nidentity: archie\nsteps:\n - {type: run.notify}\n",
want: "acts as identity",
},
{
name: "profile",
doc: "repository: none\nprofile: build\nsteps:\n - {type: run.notify}\n",
want: "runs under profile",
},
{
name: "required input",
doc: "inputs: {event: {type: object, required: true}}\nrepository: none\nsteps:\n - {type: run.notify}\n",
want: `input "event" is required`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := engine.StartDry(context.Background(), DryRequest{
OrgID: "org", Workflow: "draft", Document: tt.doc,
})
if err == nil || !strings.Contains(err.Error(), tt.want) {
t.Fatalf("StartDry error = %v, want %q", err, tt.want)
}
})
}
}
// TestDryRunNeverExecutesAChangingAction guards the dry run's promise: an
// action that changes something is judged and never executed, however freely
// judgement would let it run, while one that only reads runs for real.
func TestDryRunNeverExecutesAChangingAction(t *testing.T) {
decided := func(level judgement.Level, readOnly bool) judgement.Decision {
d := judgement.Decision{Level: level}
d.Action.Effect.ReadOnly = readOnly
return d
}
tests := []struct {
name string
dry bool
decision judgement.Decision
wantExecuted int
}{
{name: "dry: a changing action at act quietly is not executed", dry: true, decision: decided(judgement.ActQuietly, false)},
{name: "dry: a changing action at act and tell is not executed", dry: true, decision: decided(judgement.ActAndTell, false)},
{name: "dry: a read-only action runs for real", dry: true, decision: decided(judgement.ActQuietly, true), wantExecuted: 1},
{name: "live: a changing action at act quietly runs", decision: decided(judgement.ActQuietly, false), wantExecuted: 1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
judge := &decidingJudge{decision: tt.decision}
rc := newContext(t.Context(), &fakeWork{}, store.Run{ID: uuid.New(), Dry: tt.dry}, uuid.New(), nil)
decl := connectors.Declaration{Connector: "c", Kind: connectors.KindAction, Name: "restart"}
if _, err := call(t.Context(), anyBox{}, judge, rc, decl, jsontext.Value(`{}`)); err != nil {
t.Fatalf("call: %v", err)
}
if judge.executed != tt.wantExecuted {
t.Fatalf("executed %d times, want %d", judge.executed, tt.wantExecuted)
}
})
}
}