package webui import ( "net/http" "net/url" "github.com/samcharles93/archie-core/internal/domain/access" "github.com/samcharles93/archie-core/internal/domain/workflow/task" "github.com/samcharles93/archie-core/internal/taskstate" ) // taskRecoveryView is the fix for a parked task's blocker: what the task is // missing, in words, and the dashboard page that supplies it. The park reason // keeps the raw error. type taskRecoveryView struct { Blocker taskstate.Blocker `json:"blocker"` Summary string `json:"summary"` Label string `json:"label"` Link string `json:"link"` } // recoveryRoute is where a blocker is fixed: the page, and the API write that // page makes, which decides whether the viewer may act there. type recoveryRoute struct { summary, label, page, method, api string instance bool } func blockerRoute(t *task.Task) (recoveryRoute, bool) { workflowPage := "/workflows?workflow=" + url.QueryEscape(t.Workflow) connections := recoveryRoute{page: "/org/connections", method: http.MethodPost, api: "/api/control-plane/resources/forge-connections/commands/replace"} models := recoveryRoute{page: "/org/models", method: http.MethodPost, api: "/api/control-plane/resources/provider-settings/commands/replace"} switch t.ParkBlocker { case taskstate.BlockerWorkflowDisabled: return recoveryRoute{summary: "Workflow " + t.Workflow + " is disabled", label: "Enable workflow", page: workflowPage, method: http.MethodPut, api: "/api/workflows/" + url.PathEscape(t.Workflow) + "/enabled"}, true case taskstate.BlockerWorkflowIdentity: return recoveryRoute{summary: "Workflow has no execution identity", label: "Set identity", page: workflowPage, method: http.MethodPost, api: "/api/control-plane/resources/workflow-definitions/commands/apply"}, true case taskstate.BlockerAgentAssignment: return recoveryRoute{summary: "Agent is not assigned to this organisation", label: "Assign agent", page: "/org/agents", method: http.MethodPut, api: "/api/orgs/" + url.PathEscape(t.OrgOrDefault()) + "/agents/_", instance: true}, true case taskstate.BlockerConnectionPermit: connections.summary, connections.label = "Agent may not use this connection", "Edit permissions" return connections, true case taskstate.BlockerRepository: return recoveryRoute{summary: "Repository is not set up", label: "Set up repository", page: "/org/repositories", method: http.MethodPost, api: "/api/control-plane/resources/repository-policies/commands/replace"}, true case taskstate.BlockerConnectionCredential: connections.summary, connections.label = "Connection credential is unavailable", "Fix credential" return connections, true case taskstate.BlockerForgeExtension: return recoveryRoute{summary: "Forge extension is not running", label: "Enable extension", page: "/settings/extensions", method: http.MethodPut, api: "/api/extensions/_/enabled", instance: true}, true case taskstate.BlockerModelCredential: models.summary, models.label = "Model provider rejected its credential", "Set up model" return models, true case taskstate.BlockerModelCredit: models.summary, models.label = "Model provider is out of credit", "Check provider" return models, true case taskstate.BlockerPolicyDenied: return recoveryRoute{summary: "An access policy refuses the run", label: "Edit policies", page: "/org/policies", method: http.MethodPut, api: "/api/access/policies"}, true } return recoveryRoute{}, false } // taskRecovery returns the recovery link for a parked task, or nil when the // park names no blocker or the viewer may not make the fixing write. func (s *Server) taskRecovery(r *http.Request, t *task.Task) *taskRecoveryView { if t.Status != task.StatusParked || s.Access == nil { return nil } route, ok := blockerRoute(t) if !ok || (route.instance && !instanceViewer(r)) { return nil } if !s.mayCall(r, route.method, route.api) { return nil } return &taskRecoveryView{Blocker: t.ParkBlocker, Summary: route.summary, Label: route.label, Link: route.page} } // mayCall reports whether the request's principal may make the API call, the // same decision the access middleware takes for it. func (s *Server) mayCall(r *http.Request, method, path string) bool { principal, ok := access.PrincipalFromContext(r.Context()) if !ok || s.Access == nil { return false } action, kind, id := accessRequest(&http.Request{Method: method, URL: &url.URL{Path: path}}) resource, err := s.recordResource(r.Context(), kind, id) return err == nil && s.Access.Authorize(r.Context(), principal, action, resource, access.Context{}).Allowed }