diff --git a/internal/app/archie/run.go b/internal/app/archie/run.go index fdb4c7aef..b9d783f9b 100644 --- a/internal/app/archie/run.go +++ b/internal/app/archie/run.go @@ -181,6 +181,8 @@ func wire(log *slog.Logger, configPath string, cfg Config, db *sqlx.DB) (*binary tell := deliveries.NewModule(db, log.With("module", "deliveries"), orgID, live, conns) engine := runs.New(db, log.With("module", "runs"), runs.Options{Owner: owner()}) + // A model alias a saved workflow names can't be deleted out from under it. + settings.UseWorkflowAliases(engine) talk := chat.NewModule(db, log.With("module", "chat"), orgID, live) // A workflow takes its context from the run handle, as a handler takes it // from the request. diff --git a/modules/governance/governance.go b/modules/governance/governance.go index 0f614acba..ebd49143b 100644 --- a/modules/governance/governance.go +++ b/modules/governance/governance.go @@ -14,6 +14,7 @@ import ( "net/http" "net/url" "regexp" + "strings" "sync" "sync/atomic" "time" @@ -275,6 +276,12 @@ func (m *Module) provider(ctx context.Context, model Model) (chat.Provider, chat return provider, info, nil } +// WorkflowAliases names the saved workflows that name a model alias, so a +// setting that would break them is refused while they still rely on it. +type WorkflowAliases interface { + WorkflowsUsingModel(ctx context.Context, orgID, alias string) ([]string, error) +} + // Module serves one organisation's settings. type Module struct { orgID string @@ -285,6 +292,8 @@ type Module struct { oidc *oidcLogin setup *SetupSignal channels ChannelLinks + // workflows reports the saved workflows that name a model alias. + workflows WorkflowAliases // The published model catalogue, read once, and the file it is cached in. cataloguePath string @@ -673,6 +682,16 @@ func (m *Module) deleteAgentModel(ctx context.Context, in *agentModelAlias) (*st if err := ValidateAgentModelAlias(in.Alias); err != nil { return nil, huma.NewError(http.StatusBadRequest, err.Error()) } + if m.workflows != nil { + using, err := m.workflows.WorkflowsUsingModel(ctx, m.orgID, in.Alias) + if err != nil { + m.log.ErrorContext(ctx, "read the workflows that name the model", "error", err) + return nil, huma.NewError(http.StatusInternalServerError, "couldn't check which workflows use the model") + } + if len(using) > 0 { + return nil, huma.NewError(http.StatusConflict, fmt.Sprintf("Saved workflows still name this model: %s. Point them at another model first.", strings.Join(using, ", "))) + } + } found, err := m.store.DeleteModelRole(ctx, m.orgID, agentModelRole(in.Alias)) if err != nil { m.log.ErrorContext(ctx, "delete agent model", "error", err) diff --git a/modules/governance/links.go b/modules/governance/links.go index 565c8eff2..4bea3205f 100644 --- a/modules/governance/links.go +++ b/modules/governance/links.go @@ -45,6 +45,10 @@ type ChannelLinks interface { // UseChannels gives the module the connectors an account links channels on. func (m *Module) UseChannels(c ChannelLinks) { m.channels = c } +// UseWorkflowAliases gives the module the saved workflows that name a model +// alias, so deleting one still in use is refused. +func (m *Module) UseWorkflowAliases(w WorkflowAliases) { m.workflows = w } + // linkCode is a stored link code, as redeeming judges it. type linkCode struct { account string diff --git a/modules/governance/models_api_test.go b/modules/governance/models_api_test.go index 045ccce53..96c87dac2 100644 --- a/modules/governance/models_api_test.go +++ b/modules/governance/models_api_test.go @@ -5,6 +5,7 @@ import ( "errors" "log/slog" "net/http" + "strings" "testing" "github.com/danielgtaylor/huma/v2" @@ -141,3 +142,50 @@ func TestModelProviderCredentialBoundary(t *testing.T) { } }) } + +// aliasUsers is the runs module as the models routes see it: the saved +// workflows that name an alias. +type aliasUsers struct { + asked string + workflows []string + err error +} + +func (a *aliasUsers) WorkflowsUsingModel(_ context.Context, _, alias string) ([]string, error) { + a.asked = alias + return a.workflows, a.err +} + +// TestDeletingAnAliasAWorkflowUsesIsRefused guards #2097: the delete refused +// only a missing alias, so removing the default model, or one a saved workflow +// still names, succeeded and those workflows failed at their agent step. +func TestDeletingAnAliasAWorkflowUsesIsRefused(t *testing.T) { + users := &aliasUsers{workflows: []string{"remediate", "triage"}} + m := NewModule(nil, slog.New(slog.DiscardHandler), "org-sys") + m.UseWorkflowAliases(users) + _, err := m.deleteAgentModel(t.Context(), &agentModelAlias{Alias: "fast"}) + if got := statusCode(t, err); got != http.StatusConflict { + t.Fatalf("status = %d (%v), want a conflict", got, err) + } + for _, name := range users.workflows { + if !strings.Contains(err.Error(), name) { + t.Fatalf("refusal %q does not name %q", err, name) + } + } + if users.asked != "fast" { + t.Fatalf("asked about alias %q, want fast", users.asked) + } +} + +// statusCode reads the HTTP status a handler's error carries. +func statusCode(t *testing.T, err error) int { + t.Helper() + if err == nil { + return http.StatusOK + } + var se huma.StatusError + if !errors.As(err, &se) { + t.Fatalf("error %v carries no status", err) + } + return se.GetStatus() +} diff --git a/modules/runs/models.go b/modules/runs/models.go new file mode 100644 index 000000000..7db2cf6a5 --- /dev/null +++ b/modules/runs/models.go @@ -0,0 +1,67 @@ +package runs + +import ( + "context" + "slices" + + "gopkg.in/yaml.v3" +) + +// modelReference is the part of a workflow body that names a model: an agent +// step's settings. +type modelReference struct { + Steps []struct { + Type string `yaml:"type"` + Settings struct { + Model string `yaml:"model"` + } `yaml:"settings"` + } `yaml:"steps"` +} + +// defaultModelAlias is what a step that names no model uses. It is also the +// name the operator sees for it, so the two spell the same model. +const defaultModelAlias = "default" + +// sameModelAlias reports whether two names mean one model. +func sameModelAlias(a, b string) bool { + norm := func(s string) string { + if s == "" { + return defaultModelAlias + } + return s + } + return norm(a) == norm(b) +} + +// namesModel reports whether a workflow body gives an agent step the named +// model. A body that isn't a workflow names nothing. +func namesModel(body, alias string) bool { + var doc modelReference + if err := yaml.Unmarshal([]byte(body), &doc); err != nil { + return false + } + for _, step := range doc.Steps { + if step.Type == typeAgent && sameModelAlias(step.Settings.Model, alias) { + return true + } + } + return false +} + +// WorkflowsUsingModel returns the names of the saved workflows that give an +// agent step the named model, in name order. The caller is a setting that +// would otherwise make them fail at their agent step. +func (e *Engine) WorkflowsUsingModel(ctx context.Context, orgID, alias string) ([]string, error) { + values, err := e.db.Workflows(ctx, orgID) + if err != nil { + return nil, err + } + var out []string + for _, w := range values { + if namesModel(w.Body, alias) { + out = append(out, w.Name) + } + } + slices.Sort(out) + return out, nil +} diff --git a/modules/runs/models_test.go b/modules/runs/models_test.go new file mode 100644 index 000000000..306f3c2f2 --- /dev/null +++ b/modules/runs/models_test.go @@ -0,0 +1,61 @@ +package runs + +import "testing" + +// TestWorkflowsNamingAModel guards #2097's half of the rule: the alias a +// workflow relies on is the one its agent steps name, and an agent step that +// names none relies on the default. A check that matched the wrong text would +// either refuse a delete nothing depends on, or allow one that breaks a run. +func TestWorkflowsNamingAModel(t *testing.T) { + for _, tc := range []struct { + name string + body string + alias string + want bool + }{ + { + name: "an agent step naming the alias", + body: "steps:\n - id: assess\n type: agent.run\n settings: {mission: m, model: fast}\n", + alias: "fast", + want: true, + }, + { + name: "an agent step naming another alias", + body: "steps:\n - type: agent.run\n settings: {mission: m, model: careful}\n", + alias: "fast", + }, + { + name: "an agent step naming no model relies on the default", + body: "steps:\n - type: agent.run\n settings: {mission: m}\n", + alias: "", + want: true, + }, + { + name: "the default named outright is the same model", + body: "steps:\n - type: agent.run\n settings: {mission: m, model: default}\n", + alias: "", + want: true, + }, + { + name: "a workflow with no agent step relies on no model", + body: "steps:\n - type: notify\n settings: {title: t}\n", + alias: "", + }, + { + name: "an alias that only shares a prefix is another model", + body: "steps:\n - type: agent.run\n settings: {mission: m, model: faster}\n", + alias: "fast", + }, + { + name: "a body that isn't a workflow names nothing", + body: "not: a workflow\n", + alias: "fast", + }, + } { + t.Run(tc.name, func(t *testing.T) { + if got := namesModel(tc.body, tc.alias); got != tc.want { + t.Fatalf("namesModel(%q, %q) = %v, want %v", tc.body, tc.alias, got, tc.want) + } + }) + } +}