diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index fb7b91e5a..6b747ab92 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -1556,6 +1556,11 @@ func (d *Daemon) teardownStorage(ctx context.Context, task *workflow.Task, repo func (d *Daemon) taskWorkspace(ctx context.Context, task *workflow.Task) (*worktree.Manager, config.Repo, error) { if task.HasRepository() && d.ResolveRepository != nil { resolved, err := d.Repository(ctx, task, config.OperationClone) + if err == nil && !task.IsTestRun() && workflow.DefinitionPushes(task.WorkflowDefinitionYAML) { + // Checked before the run rather than at its commit step, so a + // missing permit parks in seconds instead of after the agent's work. + _, err = d.Repository(ctx, task, config.OperationPush) + } return resolved.Trees, resolved.Repository, err } trees := d.treesFor(task) @@ -2073,6 +2078,9 @@ func parkBlocker(err error) taskstate.Blocker { if b, ok := errors.AsType[blockedPark](err); ok { return b.blocker } + if failure, ok := errors.AsType[*forge.AccessError](err); ok && failure.Code == "operation_denied" { + return taskstate.BlockerConnectionPermit + } return "" } diff --git a/internal/domain/workflow/definition.go b/internal/domain/workflow/definition.go index 4894ec084..25cb1d64b 100644 --- a/internal/domain/workflow/definition.go +++ b/internal/domain/workflow/definition.go @@ -9,6 +9,7 @@ import ( "fmt" "io/fs" "path" + "slices" "strings" "gopkg.in/yaml.v3" @@ -333,3 +334,37 @@ func stepsUseModel(steps []StepRecord) bool { } return false } + +// pushStepTypes are the steps that publish to the repository's forge. +var pushStepTypes = []string{RepoCommitStepName, RepoOpenPRStepName} + +// DefinitionPushes reports whether a workflow definition has a step, at any +// depth, that pushes to the repository. +func DefinitionPushes(src string) bool { + var doc any + if yaml.Unmarshal([]byte(src), &doc) != nil { + return false + } + return hasStepType(doc, pushStepTypes) +} + +func hasStepType(node any, types []string) bool { + switch n := node.(type) { + case map[string]any: + if t, ok := n["type"].(string); ok && slices.Contains(types, t) { + return true + } + for _, v := range n { + if hasStepType(v, types) { + return true + } + } + case []any: + for _, v := range n { + if hasStepType(v, types) { + return true + } + } + } + return false +} diff --git a/internal/taskstate/taskstate.go b/internal/taskstate/taskstate.go index 2cb1ba1a2..8e99288e0 100644 --- a/internal/taskstate/taskstate.go +++ b/internal/taskstate/taskstate.go @@ -264,6 +264,9 @@ const ( BlockerAgentAssignment Blocker = "agent_assignment" // BlockerPolicyDenied: an access policy refuses the run. BlockerPolicyDenied Blocker = "policy_denied" + // BlockerConnectionPermit: the run's identity may not use the + // repository's connection for an operation the workflow needs. + BlockerConnectionPermit Blocker = "connection_permit" ) // Blocker is the persisted park blocker string. diff --git a/internal/webui/task_recovery.go b/internal/webui/task_recovery.go index 12c0a4b37..19c889464 100644 --- a/internal/webui/task_recovery.go +++ b/internal/webui/task_recovery.go @@ -33,6 +33,8 @@ func blockerRoute(t *task.Task) (recoveryRoute, bool) { return recoveryRoute{label: "Set execution identity", page: workflowPage, method: http.MethodPost, api: "/api/control-plane/resources/workflow-definitions/commands/apply"}, true case taskstate.BlockerAgentAssignment: return recoveryRoute{label: "Assign agent", page: "/org/agents", method: http.MethodPut, api: "/api/orgs/" + url.PathEscape(t.OrgOrDefault()) + "/agents/_", instance: true}, true + case taskstate.BlockerConnectionPermit: + return recoveryRoute{label: "Edit connection permissions", page: "/org/connections", method: http.MethodPost, api: "/api/control-plane/resources/forge-connections/commands/replace"}, true case taskstate.BlockerPolicyDenied: return recoveryRoute{label: "Edit policies", page: "/org/policies", method: http.MethodPut, api: "/api/access/policies"}, true }