From 41199b1bc401afe17a77ae402c7964815b7649cf Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sat, 17 Oct 2026 10:03:12 +1100 Subject: [PATCH] fix(daemon): open model egress only for workflows with a model step --- internal/daemon/kit_task.go | 6 ++- internal/domain/workflow/definition.go | 41 +++++++++++++++++++++ internal/domain/workflow/uses_model_test.go | 27 ++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 internal/domain/workflow/uses_model_test.go diff --git a/internal/daemon/kit_task.go b/internal/daemon/kit_task.go index fd52f4ee8..4e0fd691b 100644 --- a/internal/daemon/kit_task.go +++ b/internal/daemon/kit_task.go @@ -149,8 +149,12 @@ type ModelSession struct { } // openModelEgress opens task's model session, or refuses: provider keys -// never reach a container, so without the proxy no model call can run. +// never reach a container, so without the proxy no model call can run. A +// workflow with no model step needs no session and no provider credential. func (d *Daemon) openModelEgress(ctx context.Context, task *workflow.Task, credential string) (ModelSession, func(), error) { + if !workflow.UsesModel(task.WorkflowDefinitionYAML) { + return ModelSession{}, func() {}, nil + } cfg, err := d.taskConfig(ctx, task) if err != nil { return ModelSession{}, nil, err diff --git a/internal/domain/workflow/definition.go b/internal/domain/workflow/definition.go index 15db664e1..f6b832a6a 100644 --- a/internal/domain/workflow/definition.go +++ b/internal/domain/workflow/definition.go @@ -294,3 +294,44 @@ func checkStepAliases(workflowID string, steps []StepRecord, aliases map[string] } return nil } + +// modelFreeStepTypes are the step types that never call a model. Any other +// type counts as model use, so an unknown or new step keeps the credential +// boundary. +var modelFreeStepTypes = map[string]bool{ + FinishStepName: true, HandoffStepName: true, ApproveStepName: true, + CloseIssueStepName: true, CommentStepName: true, CommandRunStepName: true, + WorkflowCallStepName: true, RepoPrepareStepName: true, RepoCommitStepName: true, + RepoOpenPRStepName: true, GateRepositoryName: true, GateDiffSizeStepName: true, DiffRulesStepName: true, +} + +// UsesModel reports whether a pinned definition has any step that may call a +// model. A missing or undecodable definition counts as using one. +func UsesModel(src string) bool { + var definition YAMLDefinition + if err := yaml.Unmarshal([]byte(src), &definition); err != nil || len(definition.Steps) == 0 { + return true + } + return stepsUseModel(definition.Steps) +} + +func stepsUseModel(steps []StepRecord) bool { + for _, step := range steps { + for _, branch := range step.Parallel { + if stepsUseModel(branch) { + return true + } + } + if step.Switch != nil { + for _, branch := range step.Switch.Cases { + if stepsUseModel(branch) { + return true + } + } + } + if step.Parallel == nil && step.Switch == nil && !modelFreeStepTypes[step.Type] { + return true + } + } + return false +} diff --git a/internal/domain/workflow/uses_model_test.go b/internal/domain/workflow/uses_model_test.go new file mode 100644 index 000000000..2de55876d --- /dev/null +++ b/internal/domain/workflow/uses_model_test.go @@ -0,0 +1,27 @@ +package workflow + +import "testing" + +func TestUsesModel(t *testing.T) { + cases := []struct { + name string + src string + want bool + }{ + {"finish only", "id: w\nsteps:\n - type: workflow.finish\n", false}, + {"repo and gates", "id: w\nsteps:\n - type: repo.prepare\n - type: gate.repository\n - type: repo.commit\n", false}, + {"agent step", "id: w\nsteps:\n - type: agent.run\n", true}, + {"agent inside parallel", "id: w\nsteps:\n - parallel:\n a:\n - type: workflow.finish\n b:\n - type: agent.run\n", true}, + {"agent inside switch", "id: w\nsteps:\n - switch:\n on: inputs.x\n cases:\n default:\n - type: agent.run\n", true}, + {"unknown step", "id: w\nsteps:\n - type: something.new\n", true}, + {"no definition", "", true}, + {"undecodable", "steps: [", true}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + if got := UsesModel(tc.src); got != tc.want { + t.Fatalf("UsesModel = %v, want %v", got, tc.want) + } + }) + } +}