package runs import ( "context" "encoding/json/jsontext" "errors" "testing" "github.com/google/uuid" "github.com/samcharles93/ai-sdk/core" "github.com/samcharles93/archie-core/modules/approvals" "github.com/samcharles93/archie-core/modules/connectors" "github.com/samcharles93/archie-core/modules/judgement" "github.com/samcharles93/archie-core/modules/runs/internal/store" ) type countingJudge struct { level judgement.Level executed int } func (j *countingJudge) Assess(_ context.Context, req judgement.Request) (judgement.Decision, error) { return judgement.Decision{Key: req.Key, Level: j.level, Action: judgement.Action{Connector: req.Connector, Name: req.Action, Input: req.Input}}, nil } func (j *countingJudge) Execute(context.Context, judgement.Decision, jsontext.Value, string) (string, error) { j.executed++ return "done", nil } func (j *countingJudge) ExecuteApproved(context.Context, judgement.Decision, judgement.Approval, jsontext.Value, string) (string, error) { j.executed++ return "done", nil } func (j *countingJudge) ForRun(context.Context, uuid.UUID) ([]judgement.Decision, error) { return nil, nil } type stubBox struct{ tool connectors.Tool } func (stubBox) Declarations() []connectors.Declaration { return nil } func (b stubBox) Tool(string) (connectors.Tool, bool) { return b.tool, true } // TestActionStartedBeforeItsResultIsNeverRunAgain guards R15: a crash between // a connector's effect and its recorded result leaves a started marker and no // result, and the resumed run must report the outcome as unknown instead of // running the effect a second time. func TestActionStartedBeforeItsResultIsNeverRunAgain(t *testing.T) { runID := uuid.MustParse("6b1f6c2e-8d5a-4c43-9f0e-1d2a3b4c5d6e") decl := connectors.Declaration{Connector: "ops", Kind: connectors.KindAction, Name: "restart"} marker := func(key string) []store.Step { return []store.Step{{RunID: runID, Seq: markerBase, Name: startedPrefix + key, Outcome: store.OutcomeDone, Output: jsontext.Value("true")}} } agentKey := runID.String() + ":investigate:3" approvedKey := runID.String() + ":0" tests := []struct { name string steps []store.Step approved bool wantExecuted int wantUnknown bool }{ {name: "agent action with no marker runs", wantExecuted: 1}, {name: "agent action with a marker is unknown", steps: marker(agentKey), wantUnknown: true}, {name: "approved action with no marker runs", approved: true, wantExecuted: 1}, {name: "approved action with a marker is unknown", steps: marker(approvedKey), approved: true, wantUnknown: true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { work := &fakeWork{steps: tt.steps} run := store.Run{ID: runID, OrgID: "org"} rc := newContext(t.Context(), work, run, uuid.New(), tt.steps) judge := &countingJudge{level: judgement.ActAndTell} 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}) } else { ctx := core.WithIdempotencyKey(t.Context(), agentKey) _, err = call(ctx, stubBox{}, judge, rc, decl, jsontext.Value(`{}`)) } if got := errors.Is(err, ErrOutcomeUnknown) && errors.Is(err, core.ErrOutcomeUnknown); got != tt.wantUnknown { t.Fatalf("unknown outcome = %v (err %v), want %v", got, err, tt.wantUnknown) } if judge.executed != tt.wantExecuted { t.Fatalf("action executed %d times, want %d", judge.executed, tt.wantExecuted) } }) } }