package runs import ( "context" "fmt" "log/slog" "slices" "strings" "testing" "github.com/samcharles93/archie-core/modules/governance" ) // policyModels resolves aliases as the workflow model policy does: an alias // outside allowed is refused, a nil allowed list lets every alias through, and // an alias the policy allows resolves. With noDefault set, the organisation has // not chosen a default agent model yet. type policyModels struct { onePersonPath allowed []string noDefault bool } func (p policyModels) WorkflowModelForAlias(_ context.Context, alias string) (governance.Model, error) { name := alias if name == "" { name = "default" } if p.allowed != nil && !slices.Contains(p.allowed, name) { return governance.Model{}, fmt.Errorf("model alias %q: %w", name, governance.ErrModelNotAllowed) } if alias == "" && p.noDefault { return governance.Model{}, governance.ErrNoAgentModel } if alias != "" && alias != "fast" { return governance.Model{}, fmt.Errorf("agent model alias %q: %w", alias, governance.ErrNoAgentModel) } return governance.Model{Model: name}, nil } // TestSavingChecksAgentModelAgainstPolicy guards the workflow step contract at // save time: an agent step whose model the workflow model policy refuses, the // default included, is refused when the workflow is saved, not when its run // starts. func TestSavingChecksAgentModelAgainstPolicy(t *testing.T) { tests := []struct { name string model string allowed []string // noDefault means no default agent model is configured yet. noDefault bool want string }{ {name: "default is refused when the policy leaves it out", model: "model: default, ", allowed: []string{"fast"}, want: `steps[0].settings.model: model alias "default"`}, {name: "no model is checked as the default", model: "", allowed: []string{"fast"}, want: `steps[0].settings.model: model alias "default"`}, {name: "default is accepted when the policy allows it", model: "model: default, ", allowed: []string{"default"}}, {name: "a named alias the policy allows is accepted", model: "model: fast, ", allowed: []string{"fast"}}, {name: "no model saves while no default model is configured", model: "", noDefault: true}, {name: "default saves while no default model is configured", model: "model: default, ", allowed: []string{"default"}, noDefault: true}, {name: "default is still refused by the policy while no default model is configured", model: "model: default, ", allowed: []string{"fast"}, noDefault: true, want: `steps[0].settings.model: model alias "default"`}, {name: "an unknown named alias is refused", model: "model: ghost, ", want: `steps[0].settings.model: unknown agent model alias "ghost"`}, {name: "a named alias the policy leaves out is refused", model: "model: slow, ", allowed: []string{"fast"}, want: `steps[0].settings.model: model alias "slow"`}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { engine := newEngine(nil, &fakeWork{}, slog.New(slog.DiscardHandler), Options{}) engine.Use(Deps{Settings: policyModels{allowed: tt.allowed, noDefault: tt.noDefault}}) step := "{type: agent.run, settings: {" + tt.model + "mission: look}}" _, err := engine.validateDocument(t.Context(), "steps:\n - "+step+"\n") switch { case tt.want == "" && err != nil: t.Fatalf("validateDocument: %v", err) case tt.want != "" && (err == nil || !strings.Contains(err.Error(), tt.want)): t.Fatalf("validateDocument error = %v, want %q", err, tt.want) } }) } } // TestStarterWorkflowSavesWithoutModels guards a fresh install: the workflow // that ships with the binary is saved at start, before an operator has // configured any model, so saving it must not need one. func TestStarterWorkflowSavesWithoutModels(t *testing.T) { engine := newEngine(nil, &fakeWork{}, slog.New(slog.DiscardHandler), Options{}) engine.Use(Deps{Settings: policyModels{noDefault: true}}) if _, err := engine.validateDocument(t.Context(), InvestigateDocument()); err != nil { t.Fatalf("validateDocument: %v", err) } }