6b40d5b2ec
Co-authored-by: Carlos Florencio <carlosmflorencio@gmail.com>
3049 lines
109 KiB
Go
3049 lines
109 KiB
Go
//revive:disable:file-length-limit // Legacy task operations remain cohesive; workspace-source additions live in focused files.
|
|
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
runtimeapi "github.com/kandev/kandev/internal/agent/runtime"
|
|
v1 "github.com/kandev/kandev/pkg/api/v1"
|
|
"go.uber.org/zap"
|
|
|
|
"github.com/kandev/kandev/internal/events"
|
|
"github.com/kandev/kandev/internal/task/models"
|
|
taskrepo "github.com/kandev/kandev/internal/task/repository"
|
|
"github.com/kandev/kandev/internal/task/repository/repoerrors"
|
|
wfmodels "github.com/kandev/kandev/internal/workflow/models"
|
|
"github.com/kandev/kandev/internal/worktree"
|
|
)
|
|
|
|
// defaultPriority is the default value for the task priority column.
|
|
// Used when a caller omits priority so the DB CHECK constraint is satisfied.
|
|
const defaultPriority = "medium"
|
|
|
|
const (
|
|
providerAzureDevOps = "azure_devops"
|
|
providerGitHub = "github"
|
|
providerGitLab = "gitlab"
|
|
protocolHTTP = "http"
|
|
protocolHTTPS = "https"
|
|
)
|
|
|
|
const defaultKandevTaskWorktreePathSegment = "/.kandev/tasks/"
|
|
|
|
const (
|
|
titleNotPendingReason = "title_not_pending"
|
|
titleNotOwnerReason = "title_not_owner"
|
|
)
|
|
|
|
// ErrSubtaskDepthExceeded is returned when a caller tries to create a
|
|
// subtask of a kanban subtask (nesting depth > 1). Office task trees are
|
|
// intentionally exempt.
|
|
var ErrSubtaskDepthExceeded = fmt.Errorf("cannot create a subtask of a subtask — maximum nesting depth is 1 for kanban tasks. Create a sibling task under the same parent or a top-level task instead")
|
|
|
|
// ErrInvalidTaskWorkflow identifies task creation requests whose explicit
|
|
// workflow or workflow step relationship is inconsistent.
|
|
var ErrInvalidTaskWorkflow = errors.New("invalid task workflow")
|
|
|
|
// ErrTaskAlreadyArchived is returned by ArchiveTask when the target task
|
|
// already has archived_at set. Sentinel so cascade callers (e.g.
|
|
// DeleteWorkflow) can treat a concurrent archive as a no-op instead of
|
|
// aborting the whole operation.
|
|
var ErrTaskAlreadyArchived = errors.New("task is already archived")
|
|
|
|
// ErrAutoTitlePromptRequired is returned when prompt-first creation has no
|
|
// prompt from which to derive a provisional title.
|
|
var ErrAutoTitlePromptRequired = errors.New("description is required when auto_title is enabled")
|
|
|
|
// ErrAutoTitleUnsupportedForOffice is returned when prompt-first title
|
|
// generation is requested for an Office task. Office agents use a restricted
|
|
// MCP surface that does not expose the one-shot title tool.
|
|
var ErrAutoTitleUnsupportedForOffice = errors.New("auto_title is not supported for Office tasks")
|
|
|
|
type pendingTaskTitleSetter interface {
|
|
SetTaskTitleIfPending(ctx context.Context, taskID, sessionID, title string) (bool, error)
|
|
}
|
|
|
|
type taskStopTarget struct {
|
|
sessionID string
|
|
executionID string
|
|
// terminal indicates the session is already in a terminal state (CANCELLED,
|
|
// COMPLETED, FAILED, IDLE). Stop failures for terminal sessions are expected
|
|
// and must not block environment cleanup — the agent is already gone.
|
|
terminal bool
|
|
}
|
|
|
|
type taskEnvironmentCleanup struct {
|
|
env *models.TaskEnvironment
|
|
deleteRow bool
|
|
}
|
|
|
|
type taskEnvironmentSessionUsageChecker interface {
|
|
HasActiveTaskSessionsByTaskEnvironmentExcludingTask(ctx context.Context, taskEnvironmentID, taskID string) (bool, error)
|
|
}
|
|
|
|
type taskEnvironmentSessionBorrowerFinder interface {
|
|
FindActiveTaskSessionTaskIDByTaskEnvironmentExcludingTask(ctx context.Context, taskEnvironmentID, taskID string) (string, error)
|
|
}
|
|
|
|
type taskEnvironmentOwnerTransferer interface {
|
|
TransferTaskEnvironmentToTask(ctx context.Context, envID, taskID string) error
|
|
}
|
|
|
|
type workflowStepCapacityTaskCreator interface {
|
|
CreateTaskIfWorkflowStepHasCapacity(ctx context.Context, task *models.Task, targetStepID string, limit int) error
|
|
}
|
|
|
|
type workflowStepAdmissionTaskCreator interface {
|
|
CreateTaskWithWorkflowStepAdmission(ctx context.Context, task *models.Task, targetStepID string, targetLimit int, feederStepID string, feederLimit int) error
|
|
}
|
|
|
|
// Task operations
|
|
|
|
// isOfficeRequest returns true if the request should create an office task.
|
|
func isOfficeRequest(req *CreateTaskRequest) bool {
|
|
return req.ProjectID != "" ||
|
|
req.Origin == models.TaskOriginAgentCreated ||
|
|
req.Origin == models.TaskOriginRoutine ||
|
|
req.Origin == models.TaskOriginOnboarding
|
|
}
|
|
|
|
// CreateTask creates a new task and publishes a task.created event.
|
|
// WorkflowID is required for non-ephemeral kanban tasks.
|
|
// Office tasks (project_id set, or origin is agent_created/routine)
|
|
// auto-resolve to the workspace's office workflow.
|
|
// Ephemeral tasks (quick chat, config chat) must NOT have a workflow.
|
|
func (s *Service) CreateTask(ctx context.Context, req *CreateTaskRequest) (*models.Task, error) {
|
|
if err := s.authorizeWorkspaceID(ctx, req.WorkspaceID); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := prepareAutoTitle(req); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := s.validateCreateTaskRequest(req); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := s.validateSubtaskDepth(ctx, req); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Subtasks created without explicit repositories inherit the parent's, so
|
|
// an inherit_parent subtask resolves a repo at launch and can reuse the
|
|
// parent's worktree (the UI omits repositories expecting this). Mirrors the
|
|
// MCP create_task path so UI- and agent-created subtasks behave identically.
|
|
if err := s.inheritParentRepositories(ctx, req); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// For office tasks, resolve workflow from workspace
|
|
if isOfficeRequest(req) && req.WorkflowID == "" {
|
|
if err := s.resolveOfficeWorkflow(ctx, req); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if err := s.validateTaskWorkflow(ctx, req); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
workflowStepID := s.resolveWorkflowStep(ctx, req)
|
|
task := s.buildTask(req, workflowStepID)
|
|
|
|
// Auto-assign identifier for office tasks
|
|
if isOfficeRequest(req) {
|
|
if err := s.assignIdentifier(ctx, task); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
if err := s.createTaskWithCapacity(ctx, task); err != nil {
|
|
s.logger.Error("failed to create task", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
// Create blocker relationships if specified.
|
|
for _, blockerID := range req.BlockedBy {
|
|
if err := s.AddBlocker(ctx, task.ID, blockerID); err != nil {
|
|
return nil, fmt.Errorf("add blocker %s: %w", blockerID, err)
|
|
}
|
|
}
|
|
|
|
if err := s.createTaskRepositories(ctx, task.ID, req.WorkspaceID, req.Repositories); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Load repositories into task for response
|
|
repos, err := s.taskRepos.ListTaskRepositories(ctx, task.ID)
|
|
if err != nil {
|
|
s.logger.Error("failed to list task repositories", zap.Error(err))
|
|
} else {
|
|
task.Repositories = repos
|
|
}
|
|
|
|
s.publishTaskEvent(ctx, events.TaskCreated, task, nil)
|
|
s.pullTasksFromNewFeederWork(ctx, task.WorkflowID, task.WorkflowStepID)
|
|
if refreshed, err := s.tasks.GetTask(ctx, task.ID); err != nil {
|
|
s.logger.Warn("failed to refresh task after feeder pull", zap.String("task_id", task.ID), zap.Error(err))
|
|
} else if refreshed != nil {
|
|
refreshed.Repositories = task.Repositories
|
|
task = refreshed
|
|
}
|
|
s.logger.Info("task created", zap.String("task_id", task.ID), zap.String("title", task.Title))
|
|
|
|
return task, nil
|
|
}
|
|
|
|
func deriveProvisionalTaskTitle(description string) (string, error) {
|
|
words := strings.Fields(description)
|
|
if len(words) == 0 {
|
|
return "", ErrAutoTitlePromptRequired
|
|
}
|
|
if len(words) > 6 {
|
|
words = words[:6]
|
|
}
|
|
title := strings.Join(words, " ")
|
|
title = TruncateTaskTitle(title)
|
|
return title, nil
|
|
}
|
|
|
|
func prepareAutoTitle(req *CreateTaskRequest) error {
|
|
if !req.AutoTitle {
|
|
return nil
|
|
}
|
|
if isOfficeRequest(req) {
|
|
return ErrAutoTitleUnsupportedForOffice
|
|
}
|
|
title, err := deriveProvisionalTaskTitle(req.Description)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Title = title
|
|
if req.Metadata == nil {
|
|
req.Metadata = make(map[string]interface{})
|
|
}
|
|
req.Metadata[models.MetaKeyAgentTitlePending] = true
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) pullTasksFromNewFeederWork(ctx context.Context, workflowID, feederStepID string) {
|
|
stepLister, ok := s.workflowStepGetter.(workflowStepLister)
|
|
if !ok || workflowID == "" || feederStepID == "" {
|
|
return
|
|
}
|
|
steps, err := stepLister.ListStepsByWorkflow(ctx, workflowID)
|
|
if err != nil {
|
|
s.logger.Warn("failed to list workflow steps after feeder task creation",
|
|
zap.String("workflow_id", workflowID), zap.Error(err))
|
|
return
|
|
}
|
|
for _, step := range steps {
|
|
if step != nil && step.PullFromStepID == feederStepID {
|
|
s.pullNextTaskOnVacate(ctx, step.ID, "")
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *Service) createTaskWithCapacity(ctx context.Context, task *models.Task) error {
|
|
if task.IsEphemeral || task.WorkflowStepID == "" {
|
|
return s.tasks.CreateTask(ctx, task)
|
|
}
|
|
if s.workflowStepGetter == nil {
|
|
return s.tasks.CreateTask(ctx, task)
|
|
}
|
|
step, err := s.workflowStepGetter.GetStep(ctx, task.WorkflowStepID)
|
|
if err != nil {
|
|
return fmt.Errorf("load workflow step %s for task creation: %w", task.WorkflowStepID, err)
|
|
}
|
|
if step == nil {
|
|
return fmt.Errorf("%w: workflow step not found: %s", ErrInvalidTaskWorkflow, task.WorkflowStepID)
|
|
}
|
|
if step.WIPLimit <= 0 {
|
|
return s.tasks.CreateTask(ctx, task)
|
|
}
|
|
if admissionCreator, ok := s.tasks.(workflowStepAdmissionTaskCreator); ok {
|
|
feederStepID, feederLimit, err := s.resolveAdmissionFeeder(ctx, step)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return admissionCreator.CreateTaskWithWorkflowStepAdmission(ctx, task, step.ID, step.WIPLimit, feederStepID, feederLimit)
|
|
}
|
|
creator, ok := s.tasks.(workflowStepCapacityTaskCreator)
|
|
if !ok {
|
|
return fmt.Errorf("workflow step %s has WIP limit %d but capacity-aware task persistence is unavailable", step.ID, step.WIPLimit)
|
|
}
|
|
return creator.CreateTaskIfWorkflowStepHasCapacity(ctx, task, step.ID, step.WIPLimit)
|
|
}
|
|
|
|
func (s *Service) resolveAdmissionFeeder(ctx context.Context, step *wfmodels.WorkflowStep) (string, int, error) {
|
|
if step.PullFromStepID == "" {
|
|
return "", 0, nil
|
|
}
|
|
if s.workflowStepGetter == nil {
|
|
return "", 0, fmt.Errorf("workflow step %s configures feeder %s but workflow step lookup is unavailable", step.ID, step.PullFromStepID)
|
|
}
|
|
feeder, err := s.workflowStepGetter.GetStep(ctx, step.PullFromStepID)
|
|
if err != nil {
|
|
return "", 0, fmt.Errorf("load feeder workflow step %s: %w", step.PullFromStepID, err)
|
|
}
|
|
if feeder == nil || feeder.WorkflowID != step.WorkflowID {
|
|
return "", 0, fmt.Errorf("workflow step %s has invalid feeder %s", step.ID, step.PullFromStepID)
|
|
}
|
|
return feeder.ID, feeder.WIPLimit, nil
|
|
}
|
|
|
|
// inheritParentRepositories fills req.Repositories from the parent task when a
|
|
// subtask is created without explicit repositories. This applies to any
|
|
// repo-less subtask (not only inherit_parent ones), matching the MCP
|
|
// create_task path (mcp/handlers.inheritedRepoInputs) so UI- and agent-created
|
|
// subtasks behave identically — the UI's new_workspace mode always sends repos,
|
|
// so in practice only inherit_parent reaches here empty. RepositoryID and
|
|
// BaseBranch carry over; CheckoutBranch is dropped on purpose because two
|
|
// worktrees can't share a working branch, so the subtask branches off the same
|
|
// base as the parent.
|
|
//
|
|
// A lookup failure is returned rather than swallowed: a subtask silently
|
|
// created with no repositories can't establish a worktree, which would
|
|
// reintroduce the exact fresh-worktree bug this inheritance is meant to fix —
|
|
// failing fast surfaces the problem at creation time instead.
|
|
func (s *Service) inheritParentRepositories(ctx context.Context, req *CreateTaskRequest) error {
|
|
if req.ParentID == "" || len(req.Repositories) > 0 {
|
|
return nil
|
|
}
|
|
parentRepos, err := s.taskRepos.ListTaskRepositories(ctx, req.ParentID)
|
|
if err != nil {
|
|
return fmt.Errorf("list parent repositories for subtask inheritance: %w", err)
|
|
}
|
|
inherited := make([]TaskRepositoryInput, 0, len(parentRepos))
|
|
for _, r := range parentRepos {
|
|
if r == nil || r.RepositoryID == "" {
|
|
continue
|
|
}
|
|
inherited = append(inherited, TaskRepositoryInput{
|
|
RepositoryID: r.RepositoryID,
|
|
BaseBranch: r.BaseBranch,
|
|
})
|
|
}
|
|
if len(inherited) > 0 {
|
|
req.Repositories = inherited
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// validateCreateTaskRequest validates constraints for task creation.
|
|
func (s *Service) validateCreateTaskRequest(req *CreateTaskRequest) error {
|
|
if err := validateTaskTitle(req.Title); err != nil {
|
|
return err
|
|
}
|
|
isOffice := isOfficeRequest(req)
|
|
// Automation runs never land on a board, so they need no workflow — the
|
|
// trigger is the start signal, not a column. They are still ordinary,
|
|
// persistent tasks; only their origin keeps them out of board reads.
|
|
isAutomationRun := req.Origin == models.TaskOriginAutomationRun
|
|
if !req.IsEphemeral && !isOffice && !isAutomationRun && req.WorkflowID == "" {
|
|
return fmt.Errorf("workflow_id is required for non-ephemeral tasks")
|
|
}
|
|
if req.IsEphemeral && req.WorkflowID != "" {
|
|
return fmt.Errorf("workflow_id must be empty for ephemeral tasks")
|
|
}
|
|
if err := validateTaskRepositoryBranches(req.Repositories); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) validateTaskWorkflow(ctx context.Context, req *CreateTaskRequest) error {
|
|
if req.WorkflowID == "" {
|
|
if req.WorkflowStepID != "" {
|
|
return fmt.Errorf("%w: workflow_step_id requires workflow_id", ErrInvalidTaskWorkflow)
|
|
}
|
|
return nil
|
|
}
|
|
workflow, err := s.workflows.GetWorkflow(ctx, req.WorkflowID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if workflow == nil {
|
|
return fmt.Errorf("%w: workflow not found", ErrInvalidTaskWorkflow)
|
|
}
|
|
if workflow.WorkspaceID != req.WorkspaceID {
|
|
return repoerrors.ErrWorkspaceNotFound
|
|
}
|
|
if req.WorkflowStepID == "" {
|
|
return nil
|
|
}
|
|
if s.workflowStepGetter == nil {
|
|
return fmt.Errorf("%w: workflow step validation unavailable", ErrInvalidTaskWorkflow)
|
|
}
|
|
step, err := s.workflowStepGetter.GetStep(ctx, req.WorkflowStepID)
|
|
if err != nil {
|
|
return fmt.Errorf("%w: workflow_step_id: %w", ErrInvalidTaskWorkflow, err)
|
|
}
|
|
if step == nil || step.WorkflowID != req.WorkflowID {
|
|
return fmt.Errorf("%w: workflow step not found: %s", ErrInvalidTaskWorkflow, req.WorkflowStepID)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// validateSubtaskDepth prevents nesting deeper than one level for kanban
|
|
// (non-office) tasks. Office task trees intentionally allow arbitrary depth.
|
|
func (s *Service) validateSubtaskDepth(ctx context.Context, req *CreateTaskRequest) error {
|
|
if req.ParentID == "" {
|
|
return nil
|
|
}
|
|
parent, err := s.tasks.GetTask(ctx, req.ParentID)
|
|
if err != nil {
|
|
return fmt.Errorf("invalid parent_id: %w", err)
|
|
}
|
|
if parent.ParentID != "" && !parent.IsFromOffice {
|
|
return ErrSubtaskDepthExceeded
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// resolveOfficeWorkflow sets WorkflowID on the request from the workspace's office workflow.
|
|
func (s *Service) resolveOfficeWorkflow(ctx context.Context, req *CreateTaskRequest) error {
|
|
_, orchWorkflowID, err := s.tasks.GetWorkspaceTaskPrefix(ctx, req.WorkspaceID)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get office workflow for workspace: %w", err)
|
|
}
|
|
if orchWorkflowID == "" {
|
|
return fmt.Errorf("workspace %s has no office workflow configured", req.WorkspaceID)
|
|
}
|
|
req.WorkflowID = orchWorkflowID
|
|
return nil
|
|
}
|
|
|
|
// resolveWorkflowStep resolves the starting workflow step for a new task.
|
|
func (s *Service) resolveWorkflowStep(ctx context.Context, req *CreateTaskRequest) string {
|
|
workflowStepID := req.WorkflowStepID
|
|
if workflowStepID == "" && req.WorkflowID != "" && s.startStepResolver != nil {
|
|
var resolvedID string
|
|
var err error
|
|
if req.PlanMode {
|
|
resolvedID, err = s.startStepResolver.ResolveFirstStep(ctx, req.WorkflowID)
|
|
} else {
|
|
resolvedID, err = s.startStepResolver.ResolveStartStep(ctx, req.WorkflowID)
|
|
}
|
|
if err != nil {
|
|
s.logger.Warn("failed to resolve start step, using empty",
|
|
zap.String("workflow_id", req.WorkflowID),
|
|
zap.Error(err))
|
|
} else {
|
|
workflowStepID = resolvedID
|
|
}
|
|
}
|
|
return workflowStepID
|
|
}
|
|
|
|
// buildTask constructs a Task model from the CreateTaskRequest.
|
|
func (s *Service) buildTask(req *CreateTaskRequest, workflowStepID string) *models.Task {
|
|
state := v1.TaskStateCreated
|
|
if req.State != nil {
|
|
state = *req.State
|
|
}
|
|
origin := req.Origin
|
|
if origin == "" {
|
|
origin = models.TaskOriginManual
|
|
}
|
|
labels := req.Labels
|
|
if labels == "" {
|
|
labels = "[]"
|
|
}
|
|
priority := req.Priority
|
|
if priority == "" {
|
|
// Office tasks have a TEXT priority column with a CHECK constraint
|
|
// against the canonical four-value enum; default empty to defaultPriority
|
|
// so callers (e.g. onboarding) can omit it.
|
|
priority = defaultPriority
|
|
}
|
|
metadata := req.Metadata
|
|
if req.DeferredLaunch != nil {
|
|
if metadata == nil {
|
|
metadata = make(map[string]interface{})
|
|
}
|
|
metadata[models.MetaKeyDeferredLaunch] = req.DeferredLaunch
|
|
}
|
|
if wsPath := strings.TrimSpace(req.WorkspacePath); wsPath != "" {
|
|
if metadata == nil {
|
|
metadata = make(map[string]interface{})
|
|
}
|
|
metadata[models.MetaKeyWorkspacePath] = wsPath
|
|
}
|
|
return &models.Task{
|
|
ID: uuid.New().String(),
|
|
WorkspaceID: req.WorkspaceID,
|
|
WorkflowID: req.WorkflowID,
|
|
WorkflowStepID: workflowStepID,
|
|
Title: req.Title,
|
|
Description: req.Description,
|
|
State: state,
|
|
Priority: priority,
|
|
Position: req.Position,
|
|
Metadata: metadata,
|
|
IsEphemeral: req.IsEphemeral,
|
|
ParentID: req.ParentID,
|
|
AssigneeAgentProfileID: req.AssigneeAgentProfileID,
|
|
Origin: origin,
|
|
ProjectID: req.ProjectID,
|
|
Labels: labels,
|
|
}
|
|
}
|
|
|
|
// assignIdentifier generates a sequential identifier (e.g. "KAN-1") for the task.
|
|
func (s *Service) assignIdentifier(ctx context.Context, task *models.Task) error {
|
|
prefix, _, err := s.tasks.GetWorkspaceTaskPrefix(ctx, task.WorkspaceID)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get task prefix: %w", err)
|
|
}
|
|
seq, err := s.tasks.IncrementTaskSequence(ctx, task.WorkspaceID)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to increment task sequence: %w", err)
|
|
}
|
|
task.Identifier = fmt.Sprintf("%s-%d", prefix, seq)
|
|
return nil
|
|
}
|
|
|
|
// createTaskRepositories creates task-repository associations, resolving local paths to repository IDs.
|
|
func (s *Service) createTaskRepositories(ctx context.Context, taskID, workspaceID string, repositories []TaskRepositoryInput) error {
|
|
var repoByPath map[string]*models.Repository
|
|
for _, repoInput := range repositories {
|
|
if repoInput.RepositoryID == "" && repoInput.LocalPath != "" {
|
|
repos, err := s.repoEntities.ListRepositories(ctx, workspaceID)
|
|
if err != nil {
|
|
s.logger.Error("failed to list repositories", zap.Error(err))
|
|
return err
|
|
}
|
|
repoByPath = make(map[string]*models.Repository, len(repos))
|
|
for _, repo := range repos {
|
|
if repo.LocalPath == "" {
|
|
continue
|
|
}
|
|
repoByPath[repo.LocalPath] = repo
|
|
}
|
|
break
|
|
}
|
|
}
|
|
|
|
seen := make(map[string]bool, len(repositories))
|
|
for i, repoInput := range repositories {
|
|
if repoInput.RemoteContribution != nil {
|
|
if err := repoInput.RemoteContribution.Validate(); err != nil {
|
|
return fmt.Errorf("invalid remote contribution: %w", err)
|
|
}
|
|
if repoInput.CheckoutBranch == "" {
|
|
repoInput.CheckoutBranch = repoInput.RemoteContribution.HeadBranch
|
|
}
|
|
if repoInput.BaseBranch == "" {
|
|
repoInput.BaseBranch = repoInput.RemoteContribution.BaseBranch
|
|
}
|
|
if repoInput.CheckoutBranch != repoInput.RemoteContribution.HeadBranch ||
|
|
repoInput.BaseBranch != repoInput.RemoteContribution.BaseBranch {
|
|
return fmt.Errorf("remote contribution branches do not match the resolved binding")
|
|
}
|
|
}
|
|
repositoryID, baseBranch, _, err := s.resolveRepoInput(ctx, workspaceID, repoInput, repoByPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if repositoryID == "" {
|
|
return fmt.Errorf("repository_id is required")
|
|
}
|
|
// Multi-branch validation: the same repository may appear multiple
|
|
// times in a task on different branches. Identity is
|
|
// (repository_id, base_branch, checkout_branch) — base_branch matters
|
|
// because the worktree executor anchors the branch there while
|
|
// checkout_branch stays empty, and the local-executor flow puts the
|
|
// branch in checkout_branch with base_branch anchored to default_branch.
|
|
// Both shapes must dedup; matching DB key is UNIQUE(task_id,
|
|
// repository_id, base_branch, checkout_branch).
|
|
dedupKey := repositoryID + "\x00" + baseBranch + "\x00" + repoInput.CheckoutBranch
|
|
if seen[dedupKey] {
|
|
label := s.repoDisplayLabel(ctx, repoInput, repositoryID)
|
|
branchLabel := repoInput.CheckoutBranch
|
|
if branchLabel == "" {
|
|
branchLabel = baseBranch
|
|
}
|
|
if branchLabel == "" {
|
|
return fmt.Errorf("repository %q is listed more than once for this task", label)
|
|
}
|
|
return fmt.Errorf("repository %q on branch %q is listed more than once for this task", label, branchLabel)
|
|
}
|
|
seen[dedupKey] = true
|
|
metadata := make(map[string]interface{})
|
|
if prNum := resolvePRNumber(repoInput); prNum > 0 {
|
|
metadata["pr_number"] = prNum
|
|
}
|
|
if repoInput.RemoteContribution != nil {
|
|
if err := models.PutRemoteContribution(metadata, repoInput.RemoteContribution); err != nil {
|
|
return fmt.Errorf("persist remote contribution: %w", err)
|
|
}
|
|
}
|
|
taskRepo := &models.TaskRepository{
|
|
TaskID: taskID,
|
|
RepositoryID: repositoryID,
|
|
BaseBranch: baseBranch,
|
|
CheckoutBranch: repoInput.CheckoutBranch,
|
|
Position: i,
|
|
Metadata: metadata,
|
|
}
|
|
if err := s.taskRepos.CreateTaskRepository(ctx, taskRepo); err != nil {
|
|
s.logger.Error("failed to create task repository", zap.Error(err))
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// repoDisplayLabel returns a human-readable label for a repository to surface
|
|
// in the duplicate-repository error. It prefers owner/name parsed from the
|
|
// input's GitHub URL, then the resolved repo entity's owner/name (or bare
|
|
// name), and finally falls back to the repositoryID so the message is never
|
|
// empty. Best-effort: lookup failures degrade to the next fallback.
|
|
func (s *Service) repoDisplayLabel(ctx context.Context, repoInput TaskRepositoryInput, repositoryID string) string {
|
|
if remoteURL := effectiveRemoteURL(repoInput); remoteURL != "" {
|
|
if _, owner, name, _, err := parseRemoteRepositoryURL(remoteURL, repoInput.Provider); err == nil {
|
|
return owner + "/" + name
|
|
}
|
|
}
|
|
if repo, err := s.repoEntities.GetRepository(ctx, repositoryID); err == nil && repo != nil {
|
|
if repo.ProviderOwner != "" && repo.ProviderName != "" {
|
|
return repo.ProviderOwner + "/" + repo.ProviderName
|
|
}
|
|
if repo.Name != "" {
|
|
return repo.Name
|
|
}
|
|
}
|
|
return repositoryID
|
|
}
|
|
|
|
// ResolveRepositoryRef resolves a single TaskRepositoryInput to a
|
|
// (repositoryID, baseBranch) pair within the given workspace, creating the
|
|
// repository if necessary. Mirrors the resolution used during task creation
|
|
// (`createTaskRepositories`), but builds the local-path lookup map on demand
|
|
// so callers that only resolve one input (e.g. add_branch) don't need to
|
|
// thread the map themselves.
|
|
//
|
|
// Accepts inputs identified by RepositoryID, GitHubURL, or LocalPath. Returns
|
|
// an empty repositoryID with no error when none of those are set, letting
|
|
// callers decide whether to fall back to other defaults.
|
|
func (s *Service) ResolveRepositoryRef(ctx context.Context, workspaceID string, repoInput TaskRepositoryInput) (repositoryID, baseBranch string, created bool, err error) {
|
|
var repoByPath map[string]*models.Repository
|
|
if repoInput.RepositoryID == "" && repoInput.LocalPath != "" {
|
|
repos, listErr := s.repoEntities.ListRepositories(ctx, workspaceID)
|
|
if listErr != nil {
|
|
return "", "", false, listErr
|
|
}
|
|
repoByPath = make(map[string]*models.Repository, len(repos))
|
|
for _, repo := range repos {
|
|
if repo.LocalPath == "" {
|
|
continue
|
|
}
|
|
repoByPath[repo.LocalPath] = repo
|
|
}
|
|
}
|
|
return s.resolveRepoInput(ctx, workspaceID, repoInput, repoByPath)
|
|
}
|
|
|
|
// resolveRepoInput resolves a RepositoryInput to a repositoryID and baseBranch,
|
|
// creating the repository if it doesn't exist yet. Returns created=true only
|
|
// when this call inserted a new Repository row (GitHub-URL miss → CreateRepository
|
|
// or LocalPath miss → CreateRepository); callers that want to roll back a fresh
|
|
// row on a later failure key off this flag.
|
|
func (s *Service) resolveRepoInput(ctx context.Context, workspaceID string, repoInput TaskRepositoryInput, repoByPath map[string]*models.Repository) (repositoryID, baseBranch string, created bool, err error) {
|
|
repositoryID = repoInput.RepositoryID
|
|
baseBranch = repoInput.BaseBranch
|
|
if repositoryID != "" {
|
|
return s.resolveRepoInputID(ctx, workspaceID, repositoryID, baseBranch)
|
|
}
|
|
|
|
if effectiveRemoteURL(repoInput) != "" {
|
|
return s.resolveRepoInputRemote(ctx, workspaceID, repoInput, baseBranch)
|
|
}
|
|
|
|
if repoInput.LocalPath == "" {
|
|
return repositoryID, baseBranch, false, nil
|
|
}
|
|
return s.resolveRepoInputLocal(ctx, workspaceID, repoInput, repoByPath, baseBranch)
|
|
}
|
|
|
|
func (s *Service) resolveRepoInputID(ctx context.Context, workspaceID, repositoryID, baseBranch string) (string, string, bool, error) {
|
|
// Verify the repository belongs to the target workspace. Without this
|
|
// check, an agent that knows a repository UUID from another workspace
|
|
// could associate it with a task in this workspace via the MCP tool's
|
|
// repository_id fast path (the github_url and local_path branches both
|
|
// scope through FindOrCreateRepository, which is workspace-bound).
|
|
repo, lookupErr := s.repoEntities.GetRepository(ctx, repositoryID)
|
|
if lookupErr != nil {
|
|
return "", "", false, fmt.Errorf("looking up repository %q: %w", repositoryID, lookupErr)
|
|
}
|
|
if repo == nil || repo.WorkspaceID != workspaceID {
|
|
return "", "", false, fmt.Errorf("repository %q does not belong to workspace %q", repositoryID, workspaceID)
|
|
}
|
|
replacementID, replacementCreated, replacementErr := s.safeRepositoryIDForTaskWorktree(ctx, workspaceID, repo)
|
|
if replacementErr != nil {
|
|
return "", "", false, replacementErr
|
|
}
|
|
if replacementID != "" {
|
|
return replacementID, baseBranch, replacementCreated, nil
|
|
}
|
|
return repositoryID, baseBranch, false, nil
|
|
}
|
|
|
|
func (s *Service) safeRepositoryIDForTaskWorktree(ctx context.Context, workspaceID string, repo *models.Repository) (string, bool, error) {
|
|
if !s.isKandevTaskWorktreeRepository(repo) {
|
|
return "", false, nil
|
|
}
|
|
if repo.Provider == "" || repo.ProviderOwner == "" || repo.ProviderName == "" {
|
|
return "", false, fmt.Errorf("repository %q points at a Kandev task worktree; use the source repository or GitHub URL", repo.ID)
|
|
}
|
|
existing, err := s.findSafeReplacementRepository(ctx, workspaceID, repo)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
if existing != nil {
|
|
return existing.ID, false, nil
|
|
}
|
|
created, createErr := s.CreateRepository(ctx, &CreateRepositoryRequest{
|
|
WorkspaceID: workspaceID,
|
|
Name: repo.ProviderOwner + "/" + repo.ProviderName,
|
|
SourceType: sourceTypeProvider,
|
|
Provider: repo.Provider,
|
|
ProviderRepoID: repo.ProviderRepoID,
|
|
ProviderHost: repo.ProviderHost,
|
|
ProviderOwner: repo.ProviderOwner,
|
|
ProviderName: repo.ProviderName,
|
|
DefaultBranch: repo.DefaultBranch,
|
|
})
|
|
if createErr != nil {
|
|
return "", false, fmt.Errorf("create provider repository for task worktree %q: %w", repo.ID, createErr)
|
|
}
|
|
return created.ID, true, nil
|
|
}
|
|
|
|
func (s *Service) replaceTaskWorktreeRepositoryMatch(ctx context.Context, workspaceID string, repo *models.Repository) (*models.Repository, bool, error) {
|
|
replacementID, replacementCreated, err := s.safeRepositoryIDForTaskWorktree(ctx, workspaceID, repo)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
if replacementID == "" {
|
|
return repo, false, nil
|
|
}
|
|
replacement, lookupErr := s.repoEntities.GetRepository(ctx, replacementID)
|
|
if lookupErr != nil {
|
|
return nil, false, fmt.Errorf("looking up repository %q: %w", replacementID, lookupErr)
|
|
}
|
|
if replacement == nil {
|
|
return nil, false, fmt.Errorf("replacement repository %q no longer exists", replacementID)
|
|
}
|
|
return replacement, replacementCreated, nil
|
|
}
|
|
|
|
// findSafeReplacementRepository prefers an existing safe local clone over a
|
|
// provider row so private/offline repositories can reuse the user's checkout.
|
|
func (s *Service) findSafeReplacementRepository(ctx context.Context, workspaceID string, repo *models.Repository) (*models.Repository, error) {
|
|
repos, err := s.repoEntities.ListRepositories(ctx, workspaceID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("list repositories for task worktree replacement: %w", err)
|
|
}
|
|
var localClone *models.Repository
|
|
var providerRepo *models.Repository
|
|
for _, candidate := range repos {
|
|
if candidate == nil || candidate.ID == repo.ID {
|
|
continue
|
|
}
|
|
if !sameProviderIdentity(repo, candidate) {
|
|
continue
|
|
}
|
|
if s.isKandevTaskWorktreeRepository(candidate) {
|
|
continue
|
|
}
|
|
if candidate.SourceType == sourceTypeLocal && candidate.LocalPath != "" {
|
|
if localClone == nil {
|
|
localClone = candidate
|
|
}
|
|
continue
|
|
}
|
|
if candidate.SourceType == sourceTypeProvider && providerRepo == nil {
|
|
providerRepo = candidate
|
|
}
|
|
}
|
|
if localClone != nil {
|
|
return localClone, nil
|
|
}
|
|
if providerRepo != nil {
|
|
return providerRepo, nil
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func sameProviderIdentity(left, right *models.Repository) bool {
|
|
return left.Provider == right.Provider &&
|
|
normalizeProviderHost(left.Provider, left.ProviderHost) ==
|
|
normalizeProviderHost(right.Provider, right.ProviderHost) &&
|
|
left.ProviderOwner == right.ProviderOwner &&
|
|
left.ProviderName == right.ProviderName
|
|
}
|
|
|
|
func (s *Service) isKandevTaskWorktreeRepository(repo *models.Repository) bool {
|
|
return repo != nil && isKandevTaskWorktreePath(repo.LocalPath, s.discoveryConfig.TaskWorktreeRoots)
|
|
}
|
|
|
|
func isKandevTaskWorktreePath(path string, taskWorktreeRoots []string) bool {
|
|
normalized := normalizeTaskWorktreePath(path)
|
|
if normalized == "" {
|
|
return false
|
|
}
|
|
for _, root := range taskWorktreeRoots {
|
|
if pathAtOrInsideRoot(normalized, normalizeTaskWorktreePath(root)) {
|
|
return true
|
|
}
|
|
}
|
|
return strings.Contains(normalized, defaultKandevTaskWorktreePathSegment) ||
|
|
strings.HasSuffix(normalized, strings.TrimSuffix(defaultKandevTaskWorktreePathSegment, "/"))
|
|
}
|
|
|
|
func normalizeTaskWorktreePath(path string) string {
|
|
normalized := filepath.ToSlash(filepath.Clean(strings.TrimSpace(path)))
|
|
if normalized == "." || normalized == "" {
|
|
return ""
|
|
}
|
|
return normalized
|
|
}
|
|
|
|
func pathAtOrInsideRoot(path, root string) bool {
|
|
if path == "" || root == "" {
|
|
return false
|
|
}
|
|
if root != "/" {
|
|
root = strings.TrimRight(root, "/")
|
|
}
|
|
return path == root || strings.HasPrefix(path, root+"/")
|
|
}
|
|
|
|
// resolveRepoInputLocal handles the LocalPath branch of resolveRepoInput.
|
|
// Looks the path up in the workspace snapshot first; on miss, delegates to
|
|
// FindOrCreateRepositoryByLocalPath, which re-checks the canonical path
|
|
// against the database immediately before inserting (see repoResolveMu) so a
|
|
// second resolver racing this one onto the same on-disk repo reuses its row
|
|
// instead of creating a sibling duplicate. Extracted to keep resolveRepoInput
|
|
// inside the cyclomatic-complexity budget.
|
|
func (s *Service) resolveRepoInputLocal(
|
|
ctx context.Context, workspaceID string, repoInput TaskRepositoryInput,
|
|
repoByPath map[string]*models.Repository, baseBranch string,
|
|
) (string, string, bool, error) {
|
|
lookupPath := repoInput.LocalPath
|
|
canonicalPath, probedBranch, pathErr := resolveExplicitLocalRepositoryPath(repoInput.LocalPath)
|
|
if pathErr == nil {
|
|
lookupPath = canonicalPath
|
|
}
|
|
repo := repoByPath[lookupPath]
|
|
created := false
|
|
if repo == nil {
|
|
if isKandevTaskWorktreePath(lookupPath, s.discoveryConfig.TaskWorktreeRoots) {
|
|
return "", "", false, fmt.Errorf("local path %q points at a Kandev task worktree; use the source repository or GitHub URL", repoInput.LocalPath)
|
|
}
|
|
name := strings.TrimSpace(repoInput.Name)
|
|
if name == "" {
|
|
name = filepath.Base(repoInput.LocalPath)
|
|
}
|
|
// Resolve default_branch by probing the repo on disk so it's anchored
|
|
// to the integration branch (origin/HEAD or main/master) rather than
|
|
// whatever feature branch the user happens to have checked out. The
|
|
// frontend's `default_branch` hint wins when set; otherwise we probe
|
|
// directly. Falling back to repoInput.BaseBranch is wrong because in
|
|
// the local-executor flow that field carries the user's working
|
|
// branch, which would permanently pin repositories.default_branch to
|
|
// a feature branch and break every downstream merge-base lookup.
|
|
defaultBranch := repoInput.DefaultBranch
|
|
if defaultBranch == "" && pathErr == nil {
|
|
// A manually supplied path is an explicit read-only probe. Canonical
|
|
// repository validation protects the filesystem read; discovery roots
|
|
// only constrain automatic scans.
|
|
if probedBranch != "" {
|
|
defaultBranch = probedBranch
|
|
}
|
|
}
|
|
// identityPath is left empty when canonicalization failed: there is no
|
|
// reliable identity to dedupe against in that case, so
|
|
// FindOrCreateRepositoryByLocalPath always creates — same as prior
|
|
// behavior for that edge case.
|
|
var identityPath string
|
|
if pathErr == nil {
|
|
identityPath = canonicalPath
|
|
}
|
|
resolved, wasCreated, resolveErr := s.FindOrCreateRepositoryByLocalPath(ctx, workspaceID, identityPath, &CreateRepositoryRequest{
|
|
WorkspaceID: workspaceID,
|
|
Name: name,
|
|
SourceType: "local",
|
|
LocalPath: repoInput.LocalPath,
|
|
DefaultBranch: defaultBranch,
|
|
})
|
|
if resolveErr != nil {
|
|
return "", "", false, resolveErr
|
|
}
|
|
repo = resolved
|
|
if repoByPath != nil {
|
|
repoByPath[repoInput.LocalPath] = repo
|
|
repoByPath[repo.LocalPath] = repo
|
|
}
|
|
created = wasCreated
|
|
} else {
|
|
replacement, replacementCreated, replaceErr := s.replaceTaskWorktreeRepositoryMatch(ctx, workspaceID, repo)
|
|
if replaceErr != nil {
|
|
return "", "", false, replaceErr
|
|
}
|
|
repo = replacement
|
|
created = replacementCreated
|
|
}
|
|
if baseBranch == "" {
|
|
baseBranch = repo.DefaultBranch
|
|
}
|
|
return repo.ID, baseBranch, created, nil
|
|
}
|
|
|
|
// resolveRepoInputGitHub handles the GitHub-URL branch of resolveRepoInput:
|
|
// parse owner/name, optionally probe the provider for default_branch, then
|
|
// FindOrCreateRepository. Extracted so resolveRepoInput stays under the
|
|
// cognitive-complexity budget after adding the probe-skip and probe-error
|
|
// arms.
|
|
func (s *Service) resolveRepoInputRemote(
|
|
ctx context.Context, workspaceID string, repoInput TaskRepositoryInput, baseBranch string,
|
|
) (string, string, bool, error) {
|
|
provider, owner, name, canonicalURL, parseErr := parseRemoteRepositoryURL(
|
|
effectiveRemoteURL(repoInput), repoInput.Provider,
|
|
)
|
|
if parseErr != nil {
|
|
return "", "", false, parseErr
|
|
}
|
|
if repoInput.Provider != "" && !strings.EqualFold(repoInput.Provider, provider) {
|
|
return "", "", false, fmt.Errorf("remote_url provider %q does not match provider %q", provider, repoInput.Provider)
|
|
}
|
|
owner, metadataErr := validateRemoteRepositoryMetadata(repoInput, provider, owner, name)
|
|
if metadataErr != nil {
|
|
return "", "", false, metadataErr
|
|
}
|
|
defaultBranch := firstNonEmpty(repoInput.DefaultBranch, repoInput.BaseBranch)
|
|
if defaultBranch == "" && repoInput.ResolveProviderDefaults && s.providerProber != nil && provider == providerGitHub {
|
|
defaultBranch = s.probeProviderDefaultBranchIfMissing(ctx, workspaceID, provider, owner, name)
|
|
}
|
|
providerHost := remoteProviderHost(provider, canonicalURL)
|
|
if repoInput.ProviderHost != "" && !strings.EqualFold(strings.TrimRight(repoInput.ProviderHost, "/"), providerHost) {
|
|
return "", "", false, fmt.Errorf("remote_url provider host %q does not match %q", providerHost, repoInput.ProviderHost)
|
|
}
|
|
if provider == providerGitLab && providerHost != "https://gitlab.com" && !repoInput.TrustedRemote {
|
|
return "", "", false, fmt.Errorf("untrusted GitLab origin %q", providerHost)
|
|
}
|
|
repo, repoCreated, createErr := s.FindOrCreateRepository(ctx, &FindOrCreateRepositoryRequest{
|
|
WorkspaceID: workspaceID,
|
|
Provider: provider,
|
|
ProviderRepoID: repoInput.ProviderRepoID,
|
|
ProviderHost: providerHost,
|
|
ProviderOwner: owner,
|
|
ProviderName: name,
|
|
RemoteURL: canonicalURL,
|
|
DefaultBranch: defaultBranch,
|
|
})
|
|
if createErr != nil {
|
|
return "", "", false, createErr
|
|
}
|
|
if baseBranch == "" {
|
|
baseBranch = repo.DefaultBranch
|
|
}
|
|
return repo.ID, baseBranch, repoCreated, nil
|
|
}
|
|
|
|
func validateRemoteRepositoryMetadata(
|
|
input TaskRepositoryInput, provider, owner, name string,
|
|
) (string, error) {
|
|
if input.ProviderOwner != "" && provider != providerAzureDevOps && input.ProviderOwner != owner {
|
|
return "", fmt.Errorf("remote_url owner %q does not match provider_owner %q", owner, input.ProviderOwner)
|
|
}
|
|
if input.ProviderName != "" && input.ProviderName != name {
|
|
return "", fmt.Errorf("remote_url repository %q does not match provider_name %q", name, input.ProviderName)
|
|
}
|
|
if input.ProviderOwner != "" {
|
|
return input.ProviderOwner, nil
|
|
}
|
|
return owner, nil
|
|
}
|
|
|
|
func firstNonEmpty(values ...string) string {
|
|
for _, value := range values {
|
|
if value != "" {
|
|
return value
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func effectiveRemoteURL(input TaskRepositoryInput) string {
|
|
if strings.TrimSpace(input.RemoteURL) != "" {
|
|
return strings.TrimSpace(input.RemoteURL)
|
|
}
|
|
return strings.TrimSpace(input.GitHubURL)
|
|
}
|
|
|
|
func parseRemoteRepositoryURL(raw, providerHint string) (provider, owner, name, canonical string, err error) {
|
|
parsed, sshStyle, parseErr := normalizeRemoteRepositoryURL(raw)
|
|
if parseErr != nil {
|
|
return "", "", "", "", parseErr
|
|
}
|
|
host := strings.ToLower(parsed.Hostname())
|
|
originHost := strings.ToLower(parsed.Host)
|
|
parts := strings.Split(strings.Trim(strings.TrimSuffix(parsed.Path, ".git"), "/"), "/")
|
|
switch host {
|
|
case "github.com", "www.github.com":
|
|
return parseGitHubRemote(host, parts, sshStyle)
|
|
case "gitlab.com", "www.gitlab.com":
|
|
return parseGitLabRemote(parsed.Scheme, strings.TrimPrefix(originHost, "www."), parts, sshStyle)
|
|
case "dev.azure.com":
|
|
return parseAzureHTTPSRemote(parts)
|
|
case "ssh.dev.azure.com":
|
|
return parseAzureSSHRemote(parts)
|
|
default:
|
|
if strings.EqualFold(providerHint, providerGitLab) &&
|
|
(parsed.Scheme == protocolHTTP || parsed.Scheme == protocolHTTPS) {
|
|
return parseGitLabRemote(parsed.Scheme, originHost, parts, false)
|
|
}
|
|
return "", "", "", "", fmt.Errorf("unsupported remote repository host: %s", host)
|
|
}
|
|
}
|
|
|
|
func normalizeRemoteRepositoryURL(raw string) (*url.URL, bool, error) {
|
|
raw = strings.TrimSpace(raw)
|
|
if raw == "" {
|
|
return nil, false, fmt.Errorf("empty remote repository URL")
|
|
}
|
|
sshStyle := strings.HasPrefix(raw, "git@") && strings.Contains(raw, ":")
|
|
if sshStyle {
|
|
hostAndPath := strings.TrimPrefix(raw, "git@")
|
|
separator := strings.Index(hostAndPath, ":")
|
|
raw = "ssh://git@" + hostAndPath[:separator] + "/" + hostAndPath[separator+1:]
|
|
} else if !strings.Contains(raw, "://") {
|
|
raw = "https://" + raw
|
|
}
|
|
parsed, parseErr := url.Parse(raw)
|
|
if parseErr != nil || parsed.Host == "" {
|
|
return nil, false, fmt.Errorf("invalid remote repository URL")
|
|
}
|
|
return parsed, sshStyle, nil
|
|
}
|
|
|
|
func parseGitHubRemote(host string, parts []string, sshStyle bool) (string, string, string, string, error) {
|
|
if len(parts) < 2 || parts[0] == "" || parts[1] == "" {
|
|
return "", "", "", "", fmt.Errorf("invalid GitHub repository URL: expected github.com/owner/repo")
|
|
}
|
|
owner, name := parts[0], parts[1]
|
|
canonical := fmt.Sprintf("https://github.com/%s/%s.git", owner, name)
|
|
if sshStyle {
|
|
canonical = fmt.Sprintf("git@%s:%s/%s.git", host, owner, name)
|
|
}
|
|
return providerGitHub, owner, name, canonical, nil
|
|
}
|
|
|
|
func parseGitLabRemote(scheme, host string, parts []string, sshStyle bool) (string, string, string, string, error) {
|
|
for index, part := range parts {
|
|
if part == "-" {
|
|
parts = parts[:index]
|
|
break
|
|
}
|
|
}
|
|
if len(parts) < 2 {
|
|
return "", "", "", "", fmt.Errorf("invalid GitLab repository URL: expected gitlab.com/namespace/repo")
|
|
}
|
|
owner, name := strings.Join(parts[:len(parts)-1], "/"), parts[len(parts)-1]
|
|
if owner == "" || name == "" {
|
|
return "", "", "", "", fmt.Errorf("remote repository owner and name are required")
|
|
}
|
|
if scheme != protocolHTTP && scheme != protocolHTTPS {
|
|
scheme = protocolHTTPS
|
|
}
|
|
canonical := fmt.Sprintf("%s://%s/%s/%s.git", scheme, host, owner, name)
|
|
if sshStyle {
|
|
canonical = fmt.Sprintf("git@%s:%s/%s.git", host, owner, name)
|
|
}
|
|
return providerGitLab, owner, name, canonical, nil
|
|
}
|
|
|
|
func remoteProviderHost(provider, remoteURL string) string {
|
|
if provider == providerGitHub {
|
|
return githubProviderHost
|
|
}
|
|
if !strings.EqualFold(provider, providerGitLab) {
|
|
return ""
|
|
}
|
|
origin, _ := ParseGitRemoteIdentity(remoteURL)
|
|
if strings.HasPrefix(origin, protocolHTTP+"://") || strings.HasPrefix(origin, protocolHTTPS+"://") {
|
|
return origin
|
|
}
|
|
if sshHost := strings.TrimPrefix(origin, "ssh://"); sshHost != origin && sshHost != "" {
|
|
return protocolHTTPS + "://" + sshHost
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func parseAzureHTTPSRemote(parts []string) (string, string, string, string, error) {
|
|
if len(parts) != 4 || parts[2] != "_git" || parts[0] == "" || parts[1] == "" || parts[3] == "" {
|
|
return "", "", "", "", fmt.Errorf("invalid Azure DevOps repository URL: expected dev.azure.com/org/project/_git/repo")
|
|
}
|
|
owner, name := parts[1], parts[3]
|
|
canonical := fmt.Sprintf("https://dev.azure.com/%s/%s/_git/%s", parts[0], owner, name)
|
|
return providerAzureDevOps, owner, name, canonical, nil
|
|
}
|
|
|
|
func parseAzureSSHRemote(parts []string) (string, string, string, string, error) {
|
|
if len(parts) != 4 || parts[0] != "v3" || parts[1] == "" || parts[2] == "" || parts[3] == "" {
|
|
return "", "", "", "", fmt.Errorf("invalid Azure DevOps SSH URL: expected git@ssh.dev.azure.com:v3/org/project/repo")
|
|
}
|
|
owner, name := parts[2], parts[3]
|
|
canonical := fmt.Sprintf("git@ssh.dev.azure.com:v3/%s/%s/%s", parts[1], owner, name)
|
|
return providerAzureDevOps, owner, name, canonical, nil
|
|
}
|
|
|
|
// probeProviderDefaultBranchIfMissing returns a default_branch resolved via
|
|
// the provider prober, but only when the workspace doesn't already hold the
|
|
// repo with a non-empty default_branch (the existing value wins downstream,
|
|
// so the remote round-trip would be pure waste). A DB lookup error skips
|
|
// the probe entirely — FindOrCreateRepository will hit the same DB and
|
|
// surface the real cause; we log the lookup failure for observability.
|
|
// Probe errors fall through to "" so the AddBranchToTask gate surfaces an
|
|
// actionable validation rejection rather than a silent orphan.
|
|
func (s *Service) probeProviderDefaultBranchIfMissing(
|
|
ctx context.Context, workspaceID, provider, owner, name string,
|
|
) string {
|
|
existing, lookupErr := s.repoEntities.GetRepositoryByProviderInfo(ctx, workspaceID, provider, githubProviderHost, owner, name)
|
|
if lookupErr != nil {
|
|
s.logger.Warn("resolveRepoInput: failed to look up existing repo before probe",
|
|
zap.String("provider", provider),
|
|
zap.String("owner", owner),
|
|
zap.String("name", name),
|
|
zap.Error(lookupErr))
|
|
return ""
|
|
}
|
|
if existing != nil && existing.DefaultBranch != "" {
|
|
return ""
|
|
}
|
|
probed, probeErr := s.providerProber.ProbeDefaultBranch(ctx, provider, owner, name)
|
|
if probeErr != nil {
|
|
return ""
|
|
}
|
|
return probed
|
|
}
|
|
|
|
// resolvePRNumber returns the GitHub PR number for a repository input. Prefers
|
|
// the explicit PRNumber field; falls back to parsing a /pull/<N> path out of
|
|
// GitHubURL when present. Returns 0 when no PR is identified.
|
|
//
|
|
// The PR number is needed at worktree-creation time so fork PRs (whose head
|
|
// branch only exists on the contributor's fork) can be materialized via the
|
|
// refs/pull/<N>/head refspec on the base repo instead of a branch-name fetch
|
|
// that would 404 against origin.
|
|
func resolvePRNumber(input TaskRepositoryInput) int {
|
|
if input.PRNumber > 0 {
|
|
return input.PRNumber
|
|
}
|
|
rawURL := effectiveRemoteURL(input)
|
|
idx := strings.Index(rawURL, "/pull/")
|
|
if idx < 0 {
|
|
return 0
|
|
}
|
|
numStr := rawURL[idx+len("/pull/"):]
|
|
if i := strings.IndexAny(numStr, "/?#"); i >= 0 {
|
|
numStr = numStr[:i]
|
|
}
|
|
n, err := strconv.Atoi(numStr)
|
|
if err != nil || n <= 0 {
|
|
return 0
|
|
}
|
|
return n
|
|
}
|
|
|
|
// ReplaceTaskRepositories deletes all existing task-repository associations
|
|
// and recreates them. Exported for callers that mutate repository inputs
|
|
// (e.g. the fresh-branch flow rewriting BaseBranch) after CreateTask has
|
|
// already persisted the original set.
|
|
func (s *Service) ReplaceTaskRepositories(ctx context.Context, taskID, workspaceID string, repositories []TaskRepositoryInput) error {
|
|
return s.replaceTaskRepositories(ctx, taskID, workspaceID, repositories)
|
|
}
|
|
|
|
// replaceTaskRepositories deletes all existing task-repository associations and recreates them.
|
|
func (s *Service) replaceTaskRepositories(ctx context.Context, taskID, workspaceID string, repositories []TaskRepositoryInput) error {
|
|
if err := s.taskRepos.DeleteTaskRepositoriesByTask(ctx, taskID); err != nil {
|
|
s.logger.Error("failed to delete task repositories", zap.Error(err))
|
|
return err
|
|
}
|
|
return s.createTaskRepositories(ctx, taskID, workspaceID, repositories)
|
|
}
|
|
|
|
// GetTask retrieves a task by ID and populates repositories
|
|
func (s *Service) GetTask(ctx context.Context, id string) (*models.Task, error) {
|
|
if err := s.authorizeTaskID(ctx, id); err != nil {
|
|
return nil, err
|
|
}
|
|
task, err := s.tasks.GetTask(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Load task repositories
|
|
repos, err := s.taskRepos.ListTaskRepositories(ctx, id)
|
|
if err != nil {
|
|
s.logger.Error("failed to list task repositories", zap.Error(err))
|
|
} else {
|
|
task.Repositories = repos
|
|
}
|
|
s.hydrateTaskWorkspaceFolders(ctx, task)
|
|
|
|
return task, nil
|
|
}
|
|
|
|
// UpdateTask updates an existing task and publishes a task.updated event
|
|
func (s *Service) UpdateTask(ctx context.Context, id string, req *UpdateTaskRequest) (*models.Task, error) {
|
|
if err := s.authorizeTaskID(ctx, id); err != nil {
|
|
return nil, err
|
|
}
|
|
if req.Title != nil {
|
|
if err := validateTaskTitle(*req.Title); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
task, err := s.tasks.GetTask(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var oldState *v1.TaskState
|
|
stateChanged := false
|
|
|
|
if req.Description != nil {
|
|
task.Description = *req.Description
|
|
}
|
|
if req.Priority != nil {
|
|
task.Priority = *req.Priority
|
|
}
|
|
if req.State != nil && task.State != *req.State {
|
|
current := task.State
|
|
oldState = ¤t
|
|
task.State = *req.State
|
|
stateChanged = true
|
|
}
|
|
if req.WorkflowStepID != nil {
|
|
task.WorkflowStepID = *req.WorkflowStepID
|
|
}
|
|
if req.Position != nil {
|
|
task.Position = *req.Position
|
|
}
|
|
if req.Metadata != nil {
|
|
task.Metadata = req.Metadata
|
|
}
|
|
if req.Title != nil {
|
|
task.Title = *req.Title
|
|
if task.Metadata != nil {
|
|
delete(task.Metadata, models.MetaKeyAgentTitlePending)
|
|
delete(task.Metadata, models.MetaKeyAgentTitleOwnerSessionID)
|
|
}
|
|
}
|
|
parentCleared := false
|
|
if req.ParentID != nil && *req.ParentID != task.ParentID {
|
|
if err := s.resolveParentID(ctx, task, *req.ParentID); err != nil {
|
|
return nil, err
|
|
}
|
|
parentCleared = *req.ParentID == ""
|
|
task.ParentID = *req.ParentID
|
|
}
|
|
task.UpdatedAt = time.Now().UTC()
|
|
|
|
if err := s.tasks.UpdateTask(ctx, task); err != nil {
|
|
s.logger.Error("failed to update task", zap.String("task_id", id), zap.Error(err))
|
|
return nil, err
|
|
}
|
|
// UpdateTask may have applied a conditional title/metadata patch because
|
|
// this snapshot was stale. Publish and return the row that actually won so
|
|
// callers never receive the provisional title or pending marker again.
|
|
task = s.reloadTaskAfterMutation(ctx, id, task, "update")
|
|
|
|
// Update task repositories if provided
|
|
if req.Repositories != nil {
|
|
if err := s.replaceTaskRepositories(ctx, task.ID, task.WorkspaceID, req.Repositories); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
// Load repositories into task for response
|
|
repos, err := s.taskRepos.ListTaskRepositories(ctx, task.ID)
|
|
if err != nil {
|
|
s.logger.Error("failed to list task repositories", zap.Error(err))
|
|
} else {
|
|
task.Repositories = repos
|
|
}
|
|
|
|
if stateChanged && oldState != nil {
|
|
s.publishTaskEvent(ctx, events.TaskStateChanged, task, oldState)
|
|
}
|
|
if parentCleared {
|
|
// Explicitly signal the un-nest with parent_id: nil so clients can
|
|
// distinguish "parent removed" from "parent unchanged" — matching the
|
|
// detach path's event contract. publishTaskEvent otherwise omits an
|
|
// empty parent_id entirely.
|
|
s.publishTaskEventWithExtra(ctx, events.TaskUpdated, task, nil, map[string]interface{}{parentIDEventField: nil})
|
|
} else {
|
|
s.publishTaskEvent(ctx, events.TaskUpdated, task, nil)
|
|
}
|
|
s.logger.Info("task updated", zap.String("task_id", task.ID))
|
|
|
|
return task, nil
|
|
}
|
|
|
|
func (s *Service) reloadTaskAfterMutation(ctx context.Context, id string, fallback *models.Task, operation string) *models.Task {
|
|
current, err := s.tasks.GetTask(ctx, id)
|
|
if err != nil {
|
|
s.logger.Warn("failed to reload task after mutation",
|
|
zap.String("task_id", id), zap.String("operation", operation), zap.Error(err))
|
|
return fallback
|
|
}
|
|
if current != nil {
|
|
return current
|
|
}
|
|
return fallback
|
|
}
|
|
|
|
// SetPendingAgentTitle replaces a prompt-first provisional title exactly once.
|
|
// Only the atomically claimed owner session may resolve it. A missing pending
|
|
// marker is an idempotent no-op so a human rename or an earlier agent call
|
|
// always wins a late request.
|
|
func (s *Service) SetPendingAgentTitle(ctx context.Context, id, sessionID, title string) (*models.Task, bool, string, error) {
|
|
if err := s.authorizeTaskID(ctx, id); err != nil {
|
|
return nil, false, "", err
|
|
}
|
|
task, err := s.tasks.GetTask(ctx, id)
|
|
if err != nil {
|
|
return nil, false, "", err
|
|
}
|
|
if !models.IsAgentTitlePending(task.Metadata) {
|
|
return task, false, titleNotPendingReason, nil
|
|
}
|
|
if !models.IsAgentTitleOwner(task.Metadata, sessionID) {
|
|
return task, false, titleNotOwnerReason, nil
|
|
}
|
|
title = strings.TrimSpace(title)
|
|
if title == "" {
|
|
return nil, false, "", errors.New("title is required")
|
|
}
|
|
if err := validateTaskTitle(title); err != nil {
|
|
return nil, false, "", err
|
|
}
|
|
if setter, ok := s.tasks.(pendingTaskTitleSetter); ok {
|
|
accepted, err := setter.SetTaskTitleIfPending(ctx, id, sessionID, title)
|
|
if err != nil {
|
|
s.logger.Error("failed to set pending agent title", zap.String("task_id", id), zap.Error(err))
|
|
return nil, false, "", err
|
|
}
|
|
if !accepted {
|
|
current, getErr := s.tasks.GetTask(ctx, id)
|
|
if getErr != nil {
|
|
return nil, false, "", getErr
|
|
}
|
|
if !models.IsAgentTitlePending(current.Metadata) {
|
|
return current, false, titleNotPendingReason, nil
|
|
}
|
|
return current, false, titleNotOwnerReason, nil
|
|
}
|
|
// Reload the winning row so the response/event includes any ordinary
|
|
// task update that raced the conditional title write.
|
|
task = s.reloadTaskAfterMutation(ctx, id, task, "set pending agent title")
|
|
s.publishTaskEvent(ctx, events.TaskUpdated, task, nil)
|
|
return task, true, "", nil
|
|
}
|
|
task.Title = title
|
|
delete(task.Metadata, models.MetaKeyAgentTitlePending)
|
|
delete(task.Metadata, models.MetaKeyAgentTitleOwnerSessionID)
|
|
task.UpdatedAt = time.Now().UTC()
|
|
if err := s.tasks.UpdateTask(ctx, task); err != nil {
|
|
s.logger.Error("failed to set pending agent title", zap.String("task_id", id), zap.Error(err))
|
|
return nil, false, "", err
|
|
}
|
|
s.publishTaskEvent(ctx, events.TaskUpdated, task, nil)
|
|
return task, true, "", nil
|
|
}
|
|
|
|
// parentChainWalkLimit bounds the ancestor walk in resolveParentID so a
|
|
// corrupted parent chain can never spin forever. Real subtask trees are
|
|
// nowhere near this depth.
|
|
const parentChainWalkLimit = 1000
|
|
|
|
// parentIDEventField is the task-event payload key carrying a task's parent.
|
|
// Emitting it explicitly (as nil) on un-nest lets clients tell "parent
|
|
// removed" apart from "parent unchanged".
|
|
const parentIDEventField = "parent_id"
|
|
|
|
// ErrInvalidParent wraps every rejection from resolveParentID so HTTP/WS
|
|
// handlers can classify a bad re-parent request as a client error (400)
|
|
// rather than an internal error (500).
|
|
var ErrInvalidParent = errors.New("invalid parent")
|
|
|
|
// resolveParentID validates a proposed parent assignment for task. An empty
|
|
// parentID (un-nest) is always allowed. A non-empty parentID must reference a
|
|
// different, existing, non-archived task in the same workspace, and must not
|
|
// introduce a cycle (nesting a task under one of its own descendants).
|
|
func (s *Service) resolveParentID(ctx context.Context, task *models.Task, parentID string) error {
|
|
if parentID == "" {
|
|
return nil
|
|
}
|
|
if parentID == task.ID {
|
|
return fmt.Errorf("%w: a task cannot be its own parent", ErrInvalidParent)
|
|
}
|
|
parent, err := s.tasks.GetTask(ctx, parentID)
|
|
if err != nil {
|
|
return fmt.Errorf("%w: parent task not found: %s", ErrInvalidParent, parentID)
|
|
}
|
|
if parent.WorkspaceID != task.WorkspaceID {
|
|
return fmt.Errorf("%w: parent task must belong to the same workspace", ErrInvalidParent)
|
|
}
|
|
if parent.ArchivedAt != nil {
|
|
return fmt.Errorf("%w: parent task is archived", ErrInvalidParent)
|
|
}
|
|
// Cycle detection runs before the depth guard so a self-referential
|
|
// re-parent reports the more specific "cycle" error rather than a depth
|
|
// violation.
|
|
if err := s.checkParentCycle(ctx, task, parent); err != nil {
|
|
return err
|
|
}
|
|
return s.validateReparentDepth(ctx, task, parent)
|
|
}
|
|
|
|
// checkParentCycle walks up the parent's ancestor chain. Reaching task.ID means
|
|
// the new edge would close a cycle (task -> ... -> parent -> task).
|
|
func (s *Service) checkParentCycle(ctx context.Context, task, parent *models.Task) error {
|
|
current := parent
|
|
for i := 0; i < parentChainWalkLimit; i++ {
|
|
if current.ID == task.ID {
|
|
return fmt.Errorf("%w: nesting would create a cycle", ErrInvalidParent)
|
|
}
|
|
if current.ParentID == "" {
|
|
return nil
|
|
}
|
|
ancestor, err := s.tasks.GetTask(ctx, current.ParentID)
|
|
if err != nil {
|
|
// Broken chain — treat the missing ancestor as a root so we don't
|
|
// block a legitimate re-parent on inconsistent data.
|
|
return nil
|
|
}
|
|
current = ancestor
|
|
}
|
|
return fmt.Errorf("%w: parent chain too deep", ErrInvalidParent)
|
|
}
|
|
|
|
// validateReparentDepth enforces the one-level subtask limit for kanban
|
|
// (non-office) tasks on the re-parent path, mirroring validateSubtaskDepth on
|
|
// the create path. Office task trees intentionally allow arbitrary depth, so
|
|
// the guard is skipped when either endpoint is an Office task. The returned
|
|
// error wraps both ErrInvalidParent (so handlers map it to HTTP 400) and
|
|
// ErrSubtaskDepthExceeded (so callers can still classify the depth violation).
|
|
func (s *Service) validateReparentDepth(ctx context.Context, task, parent *models.Task) error {
|
|
if task.IsFromOffice || parent.IsFromOffice {
|
|
return nil
|
|
}
|
|
// Nesting under a task that is itself a subtask would create a grandchild.
|
|
if parent.ParentID != "" {
|
|
return fmt.Errorf("%w: %w", ErrInvalidParent, ErrSubtaskDepthExceeded)
|
|
}
|
|
// Moving a task that already has children would push those children to
|
|
// depth 2 under the new parent.
|
|
children, err := s.tasks.ListChildren(ctx, task.ID)
|
|
if err != nil {
|
|
return fmt.Errorf("%w: failed to check existing subtasks: %v", ErrInvalidParent, err)
|
|
}
|
|
if len(children) > 0 {
|
|
return fmt.Errorf("%w: %w", ErrInvalidParent, ErrSubtaskDepthExceeded)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type taskMessageRollbackRepository interface {
|
|
RestoreTaskMessageRollbackIfSessionState(
|
|
ctx context.Context,
|
|
task *models.Task,
|
|
sessionID string,
|
|
expectedSessionState models.TaskSessionState,
|
|
) (bool, error)
|
|
}
|
|
|
|
// RestoreTaskMessageRollback restores message_task's task-state/workflow-step
|
|
// snapshot only while ownerSessionID still has expectedSessionState. It is a
|
|
// narrow compensation API: the repository predicate and both task-field
|
|
// writes share one SQL statement, so coordinator cancellation cannot be
|
|
// overwritten between a state check and the rollback write.
|
|
func (s *Service) RestoreTaskMessageRollback(
|
|
ctx context.Context,
|
|
taskID, ownerSessionID string,
|
|
expectedSessionState models.TaskSessionState,
|
|
state v1.TaskState,
|
|
workflowStepID string,
|
|
) (*models.Task, bool, error) {
|
|
repo, ok := s.tasks.(taskMessageRollbackRepository)
|
|
if !ok {
|
|
return nil, false, errors.New("task repository does not support guarded message rollback")
|
|
}
|
|
task, err := s.tasks.GetTask(ctx, taskID)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
oldState := task.State
|
|
restoredTask := *task
|
|
restoredTask.State = state
|
|
restoredTask.WorkflowStepID = workflowStepID
|
|
updated, err := repo.RestoreTaskMessageRollbackIfSessionState(
|
|
ctx,
|
|
&restoredTask,
|
|
ownerSessionID,
|
|
expectedSessionState,
|
|
)
|
|
if err != nil || !updated {
|
|
return task, updated, err
|
|
}
|
|
|
|
if restoredTask.State != oldState {
|
|
s.publishTaskEvent(ctx, events.TaskStateChanged, &restoredTask, &oldState)
|
|
}
|
|
s.publishTaskEvent(ctx, events.TaskUpdated, &restoredTask, nil)
|
|
return &restoredTask, true, nil
|
|
}
|
|
|
|
// ArchiveTask archives a task by setting its archived_at timestamp.
|
|
// The task remains in the DB but is excluded from active board views.
|
|
// Active agent sessions are stopped and worktrees cleaned up in background.
|
|
func (s *Service) ArchiveTask(ctx context.Context, id string) error {
|
|
start := time.Now()
|
|
|
|
if err := s.authorizeTaskID(ctx, id); err != nil {
|
|
return err
|
|
}
|
|
// 1. Get task and verify it exists
|
|
task, err := s.tasks.GetTask(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if task.ArchivedAt != nil {
|
|
return fmt.Errorf("%w: %s", ErrTaskAlreadyArchived, id)
|
|
}
|
|
|
|
// 2. Gather data needed for cleanup BEFORE archive
|
|
var stopTargets []taskStopTarget
|
|
activeSessions, err := s.sessions.ListActiveTaskSessionsByTaskID(ctx, id)
|
|
if err != nil {
|
|
return fmt.Errorf("list active task sessions for archive: %w", err)
|
|
}
|
|
if s.executionStopper != nil {
|
|
stopTargets, err = s.buildStopTargets(ctx, id, activeSessions)
|
|
if err != nil {
|
|
return fmt.Errorf("list runtime cleanup inventory: %w", err)
|
|
}
|
|
}
|
|
|
|
// 2b. Capture git archive snapshot for active sessions BEFORE stopping agents
|
|
// Use a bounded timeout to prevent blocking the archive operation if agentctl is stuck.
|
|
if s.gitArchiveCapture != nil && len(activeSessions) > 0 {
|
|
for _, sess := range activeSessions {
|
|
if sess == nil || sess.ID == "" {
|
|
continue
|
|
}
|
|
snapCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
err := s.gitArchiveCapture.CaptureArchiveSnapshot(snapCtx, sess.ID)
|
|
cancel()
|
|
if err != nil {
|
|
s.logger.Warn("failed to capture git archive snapshot",
|
|
zap.String("task_id", id),
|
|
zap.String("session_id", sess.ID),
|
|
zap.Error(err))
|
|
}
|
|
}
|
|
}
|
|
|
|
sessions, err := s.sessions.ListTaskSessions(ctx, id)
|
|
if err != nil {
|
|
return fmt.Errorf("list task sessions for archive: %w", err)
|
|
}
|
|
|
|
worktrees, err := s.gatherWorktreesForDelete(ctx, id)
|
|
if err != nil {
|
|
return fmt.Errorf("list worktrees for archive: %w", err)
|
|
}
|
|
taskEnv, err := s.gatherTaskEnvironmentForCleanup(ctx, id)
|
|
if err != nil {
|
|
return fmt.Errorf("lookup task environment for archive: %w", err)
|
|
}
|
|
envCleanup := taskEnvironmentCleanup{env: taskEnv, deleteRow: true}
|
|
cleanupJob, err := s.persistTaskResourceCleanup(
|
|
ctx, id, models.TaskResourceCleanupTriggerArchive, "",
|
|
sessions, worktrees, stopTargets, envCleanup, true,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 3. Set archived_at in DB
|
|
if err := s.tasks.ArchiveTask(ctx, id); err != nil {
|
|
s.resolveTaskResourceCleanupAfterMutationError(ctx, cleanupJob)
|
|
return err
|
|
}
|
|
|
|
// Register the exact inventory before CANCELLED becomes visible. A launch
|
|
// persistence loser can then distinguish these owned executions from one
|
|
// that raced in after the snapshot and must clean itself up.
|
|
s.registerTaskRuntimeStopOwners(stopTargets, true)
|
|
|
|
// archived_at has committed above: the rest of this function only
|
|
// reports on that already-durable state (cancelling sessions, re-reading
|
|
// the task, publishing task.updated) or kicks off cleanup that already
|
|
// runs detached from ctx. A client disconnecting right after the write
|
|
// above must not stop any of that from finishing and reaching
|
|
// event-driven clients that don't share the archiving caller's
|
|
// connection.
|
|
finalizeCtx := context.WithoutCancel(ctx)
|
|
|
|
// 3b. Finalize active sessions in the DB and publish their cancellation
|
|
// events. See finalizeCancelledSessions for the detailed rationale.
|
|
s.finalizeCancelledSessions(finalizeCtx, id, activeSessions)
|
|
|
|
// 4. Re-read task for updated archived_at field
|
|
task, err = s.tasks.GetTask(finalizeCtx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 5. Publish task.updated event so frontend removes from board
|
|
s.publishTaskEvent(finalizeCtx, events.TaskUpdated, task, nil)
|
|
s.pullNextTaskOnVacate(finalizeCtx, task.WorkflowStepID, task.ID)
|
|
s.logger.Info("task archived",
|
|
zap.String("task_id", id),
|
|
zap.Duration("duration", time.Since(start)))
|
|
|
|
// 6. Background: Stop agents and cleanup worktrees
|
|
if cleanupJob != nil {
|
|
if err := s.StartPreparedTaskResourceCleanup(finalizeCtx, cleanupJob.OperationID); err != nil {
|
|
s.logger.Warn("start committed archive resource cleanup",
|
|
zap.String("job_id", cleanupJob.ID), zap.String("task_id", id), zap.Error(err))
|
|
}
|
|
} else if len(stopTargets) > 0 || s.worktreeCleanup != nil || len(sessions) > 0 || taskEnv != nil {
|
|
s.runAsyncTaskCleanup(id, sessions, worktrees, stopTargets, envCleanup,
|
|
"task archived", "failed to stop session on task archive", "task archive cleanup completed")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// finalizeCancelledSessions finalizes an archived task's active sessions in
|
|
// the DB and publishes a session.state_changed event for each one actually
|
|
// cancelled. The async cleanup that follows tears down the agent processes;
|
|
// this records the terminal session state, which process teardown does not
|
|
// persist on its own. The DB write alone is invisible to any client cache
|
|
// (e.g. an Office task list's "is running" indicator) that's kept fresh
|
|
// exclusively by that event, and would otherwise show a live spinner
|
|
// forever after archive.
|
|
//
|
|
// CancelActiveTaskSessionsByTaskID is bounded by its own internal 10s
|
|
// timeout, so a single attempt can time out under SQLite writer contention
|
|
// alone — with archived_at already committed by the caller, that would
|
|
// silently leave this task's sessions stuck in an active DB state forever,
|
|
// since nothing else ever re-attempts the cancellation and no
|
|
// session.state_changed event is delivered to clear a client's running
|
|
// indicator. Retrying a small, fixed number of times — each attempt getting
|
|
// its own fresh 10s budget from the repository — meaningfully shrinks that
|
|
// window without making it unbounded. This is deliberately NOT a background
|
|
// reconciliation/sweep system, just closing the immediate race; if every
|
|
// attempt still fails, ArchiveTask has already committed archived_at, so
|
|
// this remains best-effort cleanup and is not a reason to fail the archive.
|
|
func (s *Service) finalizeCancelledSessions(ctx context.Context, taskID string, activeSessions []*models.TaskSession) {
|
|
const maxCancelAttempts = 3
|
|
const cancelRetryBackoff = 250 * time.Millisecond
|
|
|
|
var cancelledSessions []*models.TaskSession
|
|
var cancelErr error
|
|
for attempt := 1; attempt <= maxCancelAttempts; attempt++ {
|
|
cancelledSessions, cancelErr = s.sessions.CancelActiveTaskSessionsByTaskID(ctx, taskID, models.SessionArchiveCancelReason)
|
|
if cancelErr == nil {
|
|
break
|
|
}
|
|
if attempt < maxCancelAttempts {
|
|
time.Sleep(cancelRetryBackoff)
|
|
}
|
|
}
|
|
if cancelErr != nil {
|
|
s.logger.Error("failed to reap active sessions on archive after retries",
|
|
zap.String("task_id", taskID),
|
|
zap.Int("attempts", maxCancelAttempts),
|
|
zap.Error(cancelErr))
|
|
return
|
|
}
|
|
if len(cancelledSessions) == 0 {
|
|
return
|
|
}
|
|
s.logger.Info("reaped active sessions on archive",
|
|
zap.String("task_id", taskID),
|
|
zap.Int("count", len(cancelledSessions)))
|
|
// Detach from ctx via WithoutCancel: the DB write above already
|
|
// committed on a detached context, so a client disconnect here must
|
|
// not also suppress the event publish below — event-driven clients
|
|
// need session.state_changed regardless of whether the archiving
|
|
// caller is still connected.
|
|
// Deliberately left unbounded (no timeout) here: publishSessionsCancelled
|
|
// gives each session in cancelledSessions its own independent 10s
|
|
// deadline around its Publish call, so one slow synchronous subscriber
|
|
// can no longer consume a shared batch-wide budget and starve the
|
|
// events for sessions later in the loop.
|
|
detachedCtx := context.WithoutCancel(ctx)
|
|
s.publishSessionsCancelled(detachedCtx, taskID, activeSessions, cancelledSessions, models.SessionArchiveCancelReason)
|
|
}
|
|
|
|
func (s *Service) registerTaskRuntimeStopOwners(stopTargets []taskStopTarget, force bool) {
|
|
if s.executionStopper == nil {
|
|
return
|
|
}
|
|
for _, target := range stopTargets {
|
|
if target.sessionID == "" || target.executionID == "" {
|
|
continue
|
|
}
|
|
s.executionStopper.RegisterExecutionStopOwner(
|
|
target.sessionID,
|
|
target.executionID,
|
|
force,
|
|
)
|
|
}
|
|
}
|
|
|
|
// DeleteTask deletes a task and publishes a task.deleted event.
|
|
// For fast UI response, the DB delete and event publish happen synchronously,
|
|
// while agent stopping and worktree cleanup happen asynchronously.
|
|
func (s *Service) DeleteTask(ctx context.Context, id string) error {
|
|
return s.deleteTaskWithReason(ctx, id, "")
|
|
}
|
|
|
|
// DeleteTaskWithReason behaves like DeleteTask but attaches a machine-readable
|
|
// reason (e.g. "pr_approved_by_user") to the task.deleted event so the frontend
|
|
// can explain why a focused task vanished.
|
|
func (s *Service) DeleteTaskWithReason(ctx context.Context, id, reason string) error {
|
|
return s.deleteTaskWithReason(ctx, id, reason)
|
|
}
|
|
|
|
func (s *Service) deleteTaskWithReason(ctx context.Context, id, reason string) error {
|
|
if err := s.authorizeTaskID(ctx, id); err != nil {
|
|
return err
|
|
}
|
|
_, err := s.deleteTaskWithReasonAndDBDelete(ctx, id, reason, models.TaskResourceCleanupTriggerDelete, func(ctx context.Context, id string) (bool, error) {
|
|
if err := s.tasks.DeleteTask(ctx, id); err != nil {
|
|
return false, err
|
|
}
|
|
return true, nil
|
|
})
|
|
return err
|
|
}
|
|
|
|
func (s *Service) deleteExpiredQuickChatTask(ctx context.Context, id string, cutoff time.Time) (bool, error) {
|
|
deleted, err := s.deleteTaskWithReasonAndDBDelete(ctx, id, "", models.TaskResourceCleanupTriggerQuickChatExpire, func(ctx context.Context, id string) (bool, error) {
|
|
return s.tasks.DeleteExpiredQuickChatTask(ctx, id, cutoff)
|
|
})
|
|
if errors.Is(err, taskrepo.ErrTaskNotFound) {
|
|
return false, nil
|
|
}
|
|
return deleted, err
|
|
}
|
|
|
|
func (s *Service) deleteTaskWithReasonAndDBDelete(
|
|
ctx context.Context,
|
|
id string,
|
|
reason string,
|
|
trigger models.TaskResourceCleanupTrigger,
|
|
deleteFromDB func(context.Context, string) (bool, error),
|
|
) (bool, error) {
|
|
start := time.Now()
|
|
|
|
// 1. Get task (sync, fast)
|
|
task, err := s.tasks.GetTask(ctx, id)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
// 2. Gather data needed for cleanup BEFORE delete (sync, fast)
|
|
sessions, err := s.sessions.ListTaskSessions(ctx, id)
|
|
if err != nil {
|
|
return false, fmt.Errorf("list task sessions for delete: %w", err)
|
|
}
|
|
|
|
worktrees, err := s.gatherWorktreesForDelete(ctx, id)
|
|
if err != nil {
|
|
return false, fmt.Errorf("list worktrees for delete: %w", err)
|
|
}
|
|
taskEnv, err := s.gatherTaskEnvironmentForCleanup(ctx, id)
|
|
if err != nil {
|
|
return false, fmt.Errorf("lookup task environment for delete: %w", err)
|
|
}
|
|
stopTargets, err := s.deleteTaskStopTargets(ctx, id)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if preserved, err := s.preserveTaskEnvironmentForActiveBorrower(ctx, id, taskEnv); err != nil {
|
|
return false, err
|
|
} else if preserved {
|
|
s.logger.Info("transferred borrowed task environment before task delete",
|
|
zap.String("task_id", id),
|
|
zap.String("env_id", taskEnvironmentID(taskEnv)),
|
|
zap.String("new_owner_task_id", taskEnv.TaskID))
|
|
}
|
|
|
|
envCleanup := taskEnvironmentCleanup{env: taskEnv, deleteRow: false}
|
|
cleanupJob, err := s.persistTaskResourceCleanup(
|
|
ctx, id, trigger, "", sessions, worktrees, stopTargets, envCleanup, true,
|
|
)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
// 4. Delete from DB (sync, fast)
|
|
deleted, err := deleteFromDB(ctx, id)
|
|
if err != nil {
|
|
s.resolveTaskResourceCleanupAfterMutationError(ctx, cleanupJob)
|
|
s.logger.Error("failed to delete task", zap.String("task_id", id), zap.Error(err))
|
|
return false, err
|
|
}
|
|
if !deleted {
|
|
s.resolveTaskResourceCleanupAfterMutationError(ctx, cleanupJob)
|
|
return false, nil
|
|
}
|
|
if s.attachmentSvc != nil {
|
|
if err := s.attachmentSvc.DeleteByTask(context.WithoutCancel(ctx), id); err != nil {
|
|
s.logger.Warn("failed to remove task attachment bytes",
|
|
zap.String("task_id", id), zap.Error(err))
|
|
}
|
|
}
|
|
|
|
// 5. Publish event (sync, fast) - frontend removes task immediately
|
|
var extra map[string]interface{}
|
|
if reason != "" {
|
|
extra = map[string]interface{}{"reason": reason}
|
|
}
|
|
s.publishTaskEventWithExtra(ctx, events.TaskDeleted, task, nil, extra)
|
|
s.pullNextTaskOnVacate(ctx, task.WorkflowStepID, task.ID)
|
|
s.forgetTaskActivity(id)
|
|
s.logger.Info("task deleted",
|
|
zap.String("task_id", id),
|
|
zap.Duration("duration", time.Since(start)))
|
|
|
|
// 6. Stop agents and cleanup worktrees in the background. Carry the
|
|
// envCleanup struct so the task environment row is reset alongside
|
|
// the worktrees (an extra task.taskEnv != nil branch keeps the
|
|
// cleanup running when only the env needs reclaiming).
|
|
hasCleanup := len(stopTargets) > 0 || s.worktreeCleanup != nil || len(sessions) > 0 || task.IsEphemeral || taskEnv != nil
|
|
if cleanupJob != nil {
|
|
if err := s.StartPreparedTaskResourceCleanup(ctx, cleanupJob.OperationID); err != nil {
|
|
s.logger.Warn("start committed delete resource cleanup",
|
|
zap.String("job_id", cleanupJob.ID), zap.String("task_id", id), zap.Error(err))
|
|
}
|
|
} else if hasCleanup {
|
|
s.runAsyncTaskCleanup(id, sessions, worktrees, stopTargets, envCleanup,
|
|
"task deleted", "failed to stop session on task delete", "task cleanup completed")
|
|
}
|
|
|
|
return true, nil
|
|
}
|
|
|
|
func (s *Service) deleteTaskStopTargets(ctx context.Context, id string) ([]taskStopTarget, error) {
|
|
// Must query before delete since DB records will be gone.
|
|
if s.executionStopper == nil {
|
|
return nil, nil
|
|
}
|
|
activeSessions, err := s.sessions.ListActiveTaskSessionsByTaskID(ctx, id)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("list active sessions for delete: %w", err)
|
|
}
|
|
stopTargets, err := s.buildStopTargets(ctx, id, activeSessions)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("list runtime cleanup inventory: %w", err)
|
|
}
|
|
return stopTargets, nil
|
|
}
|
|
|
|
// CleanupTaskResources tears down a task's runtime resources (container,
|
|
// sandbox, worktree, executor_running rows, quick-chat dir, task_environment
|
|
// row) AFTER the task row has been archived or deleted by another path.
|
|
//
|
|
// Used by HandoffService.ArchiveTaskTree / DeleteTaskTree, which bypass
|
|
// Service.ArchiveTask / Service.DeleteTask and therefore miss the runtime
|
|
// teardown those wrappers run via runAsyncTaskCleanup. Without this call the
|
|
// agent gets stopped but its container/sandbox leaks indefinitely.
|
|
//
|
|
// The caller may already have cancelled active runs separately (cascade does
|
|
// this via runCanceller before invoking us), but terminal sessions can still
|
|
// have executors_running rows. We still derive runtime stop targets from
|
|
// executors_running here so cascade cleanup does not drop durable handles.
|
|
// deleteEnvRow controls whether the task_environment row is removed (true
|
|
// for delete cascade, false for archive — archive preserves the row). Runtime
|
|
// inventory failures abort cleanup so durable stop handles remain retryable.
|
|
func (s *Service) CleanupTaskResources(ctx context.Context, taskID string, deleteEnvRow bool) {
|
|
if deleteEnvRow && s.attachmentSvc != nil {
|
|
if err := s.attachmentSvc.DeleteByTask(context.WithoutCancel(ctx), taskID); err != nil {
|
|
s.logger.Warn("failed to remove task attachment bytes during resource cleanup",
|
|
zap.String("task_id", taskID), zap.Error(err))
|
|
}
|
|
}
|
|
sessions, err := s.sessions.ListTaskSessions(ctx, taskID)
|
|
if err != nil {
|
|
s.logger.Warn("failed to list sessions for cascade cleanup",
|
|
zap.String("task_id", taskID),
|
|
zap.Error(err))
|
|
}
|
|
var stopTargets []taskStopTarget
|
|
if s.executionStopper != nil {
|
|
stopTargets, err = s.buildStopTargets(ctx, taskID, sessions)
|
|
if err != nil {
|
|
s.logger.Warn("skipping cascade cleanup because runtime inventory failed",
|
|
zap.String("task_id", taskID),
|
|
zap.Error(err))
|
|
return
|
|
}
|
|
}
|
|
worktrees, err := s.gatherWorktreesForDelete(ctx, taskID)
|
|
if err != nil {
|
|
s.logger.Warn("skipping cascade cleanup because worktree inventory failed",
|
|
zap.String("task_id", taskID), zap.Error(err))
|
|
return
|
|
}
|
|
taskEnv, err := s.gatherTaskEnvironmentForCleanup(ctx, taskID)
|
|
if err != nil {
|
|
s.logger.Warn("skipping cascade cleanup because task environment inventory failed",
|
|
zap.String("task_id", taskID), zap.Error(err))
|
|
return
|
|
}
|
|
if deleteEnvRow {
|
|
preserved, err := s.preserveTaskEnvironmentForActiveBorrower(ctx, taskID, taskEnv)
|
|
if err != nil {
|
|
s.logger.Warn("skipping cascade cleanup because task environment could not be preserved for borrower",
|
|
zap.String("task_id", taskID),
|
|
zap.String("env_id", taskEnvironmentID(taskEnv)),
|
|
zap.Error(err))
|
|
return
|
|
}
|
|
if preserved {
|
|
deleteEnvRow = false
|
|
s.logger.Info("transferred borrowed task environment before cascade delete",
|
|
zap.String("task_id", taskID),
|
|
zap.String("env_id", taskEnvironmentID(taskEnv)),
|
|
zap.String("new_owner_task_id", taskEnv.TaskID))
|
|
}
|
|
}
|
|
envCleanup := taskEnvironmentCleanup{env: taskEnv, deleteRow: deleteEnvRow}
|
|
if len(sessions) == 0 && len(worktrees) == 0 && len(stopTargets) == 0 && taskEnv == nil {
|
|
return
|
|
}
|
|
reason := "cascade archive"
|
|
if deleteEnvRow {
|
|
reason = "cascade delete"
|
|
}
|
|
s.runAsyncTaskCleanup(taskID, sessions, worktrees, stopTargets, envCleanup,
|
|
reason, "failed to stop session on cascade cleanup", "cascade cleanup completed")
|
|
}
|
|
|
|
// gatherWorktreesForDelete collects worktrees for a task before it is deleted.
|
|
// For legacy WorktreeCleanup implementations that do not implement WorktreeProvider,
|
|
// it triggers cleanup immediately and returns nil.
|
|
func (s *Service) gatherWorktreesForDelete(ctx context.Context, taskID string) ([]*worktree.Worktree, error) {
|
|
if s.worktreeCleanup == nil {
|
|
return nil, nil
|
|
}
|
|
provider, ok := s.worktreeCleanup.(WorktreeProvider)
|
|
if !ok {
|
|
// Durable cleanup must persist its intent before invoking a destructive
|
|
// legacy callback. Non-durable test/legacy wiring keeps the old behavior.
|
|
if s.resourceCleanups == nil {
|
|
if err := s.worktreeCleanup.OnTaskDeleted(ctx, taskID); err != nil {
|
|
s.logger.Warn("failed to cleanup worktree on task deletion",
|
|
zap.String("task_id", taskID), zap.Error(err))
|
|
}
|
|
}
|
|
return nil, nil
|
|
}
|
|
worktrees, err := provider.GetAllByTaskID(ctx, taskID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return worktrees, nil
|
|
}
|
|
|
|
func (s *Service) gatherTaskEnvironmentForCleanup(ctx context.Context, taskID string) (*models.TaskEnvironment, error) {
|
|
if s.taskEnvironments == nil {
|
|
return nil, nil
|
|
}
|
|
env, err := s.taskEnvironments.GetTaskEnvironmentByTaskID(ctx, taskID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return env, nil
|
|
}
|
|
|
|
func (s *Service) runAsyncTaskCleanup(
|
|
id string,
|
|
sessions []*models.TaskSession,
|
|
worktrees []*worktree.Worktree,
|
|
stopTargets []taskStopTarget,
|
|
envCleanup taskEnvironmentCleanup,
|
|
stopReason, stopFailMsg, cleanupMsg string,
|
|
) {
|
|
go s.runTaskCleanup(id, sessions, worktrees, stopTargets, envCleanup, stopReason, stopFailMsg, cleanupMsg)
|
|
}
|
|
|
|
func (s *Service) runTaskCleanup(
|
|
id string,
|
|
sessions []*models.TaskSession,
|
|
worktrees []*worktree.Worktree,
|
|
stopTargets []taskStopTarget,
|
|
envCleanup taskEnvironmentCleanup,
|
|
stopReason, stopFailMsg, cleanupMsg string,
|
|
) {
|
|
cleanupStart := time.Now()
|
|
cleanupCtx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
defer cancel()
|
|
refreshedTargets, err := s.refreshTaskRuntimeStopTargets(cleanupCtx, id, stopTargets)
|
|
if err != nil {
|
|
s.logger.Warn(cleanupMsg+" deferred because runtime inventory refresh failed",
|
|
zap.String("task_id", id),
|
|
zap.Error(err))
|
|
s.signalCleanupDoneForTest()
|
|
return
|
|
}
|
|
stopTargets = refreshedTargets
|
|
s.registerTaskRuntimeStopOwners(stopTargets, true)
|
|
|
|
stopOutcome := s.stopTaskRuntimeTargets(cleanupCtx, id, stopTargets, stopReason, stopFailMsg)
|
|
|
|
cleanupErrors := s.performTaskCleanup(cleanupCtx, id, sessions, worktrees, stopTargets, envCleanup,
|
|
taskCleanupPreserveRows(stopOutcome))
|
|
|
|
if len(cleanupErrors) > 0 {
|
|
s.logger.Warn(cleanupMsg+" with errors",
|
|
zap.String("task_id", id),
|
|
zap.Int("error_count", len(cleanupErrors)),
|
|
zap.Duration("duration", time.Since(cleanupStart)))
|
|
} else {
|
|
s.logger.Info(cleanupMsg,
|
|
zap.String("task_id", id),
|
|
zap.Duration("duration", time.Since(cleanupStart)))
|
|
}
|
|
s.signalCleanupDoneForTest()
|
|
}
|
|
|
|
// isCleanableSessionState reports whether a session has no running agent process
|
|
// and stop failures are therefore expected. Unlike the orchestrator's
|
|
// isTerminalSessionState (which excludes IDLE), this helper is used only during
|
|
// task cleanup to decide whether a stop failure should block environment teardown.
|
|
// IDLE is included because an idle session has already released its execution slot
|
|
// and will return ErrExecutionNotFound just like CANCELLED/COMPLETED/FAILED.
|
|
func isCleanableSessionState(state models.TaskSessionState) bool {
|
|
switch state {
|
|
case models.TaskSessionStateCancelled,
|
|
models.TaskSessionStateCompleted,
|
|
models.TaskSessionStateFailed,
|
|
models.TaskSessionStateIdle:
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (s *Service) buildStopTargets(ctx context.Context, taskID string, activeSessions []*models.TaskSession) ([]taskStopTarget, error) {
|
|
targets := make([]taskStopTarget, 0, len(activeSessions))
|
|
seen := make(map[string]struct{})
|
|
// Index session states so executor_running rows can be marked terminal.
|
|
sessionStates := make(map[string]models.TaskSessionState, len(activeSessions))
|
|
for _, sess := range activeSessions {
|
|
if sess != nil {
|
|
sessionStates[sess.ID] = sess.State
|
|
}
|
|
}
|
|
if s.executors != nil {
|
|
runningRows, err := s.executors.ListExecutorsRunningByTaskID(ctx, taskID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, running := range runningRows {
|
|
if running == nil || running.SessionID == "" {
|
|
continue
|
|
}
|
|
target := taskStopTarget{
|
|
sessionID: running.SessionID,
|
|
executionID: strings.TrimSpace(running.AgentExecutionID),
|
|
terminal: isCleanableSessionState(sessionStates[running.SessionID]),
|
|
}
|
|
targets = append(targets, target)
|
|
seen[target.sessionID] = struct{}{}
|
|
}
|
|
}
|
|
for _, sess := range activeSessions {
|
|
if sess == nil || sess.ID == "" {
|
|
continue
|
|
}
|
|
if _, ok := seen[sess.ID]; ok {
|
|
continue
|
|
}
|
|
// Sessions without an executor_running row that are already in a terminal
|
|
// state have no running process; skip creating a stop target.
|
|
if isCleanableSessionState(sess.State) {
|
|
continue
|
|
}
|
|
target := taskStopTarget{
|
|
sessionID: sess.ID,
|
|
executionID: strings.TrimSpace(sess.AgentExecutionID),
|
|
}
|
|
if target.executionID == "" && s.executors != nil {
|
|
running, err := s.executors.GetExecutorRunningBySessionID(ctx, sess.ID)
|
|
if err == nil && running != nil {
|
|
target.executionID = strings.TrimSpace(running.AgentExecutionID)
|
|
}
|
|
}
|
|
targets = append(targets, target)
|
|
}
|
|
s.logger.Debug("prepared task cleanup stop targets",
|
|
zap.String("task_id", taskID),
|
|
zap.Int("count", len(targets)))
|
|
return targets, nil
|
|
}
|
|
|
|
// refreshTaskRuntimeStopTargets merges the durable/pre-mutation snapshot with
|
|
// the live runtime inventory immediately before teardown. A launch may rotate
|
|
// an execution after the snapshot but before archive/delete becomes visible;
|
|
// keeping every exact ID makes cleanup safe across that race and worker
|
|
// restarts. A session-only fallback is retained only when no exact ID exists
|
|
// for that session; otherwise it would observe absence after the exact stop and
|
|
// keep a durable cleanup retrying forever.
|
|
func (s *Service) refreshTaskRuntimeStopTargets(
|
|
ctx context.Context,
|
|
taskID string,
|
|
snapshot []taskStopTarget,
|
|
) ([]taskStopTarget, error) {
|
|
if s.executionStopper == nil || s.executors == nil {
|
|
return snapshot, nil
|
|
}
|
|
runningRows, err := s.executors.ListExecutorsRunningByTaskID(ctx, taskID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return mergeTaskStopTargets(taskStopTargetsFromRunningRows(runningRows), snapshot), nil
|
|
}
|
|
|
|
func taskStopTargetsFromRunningRows(runningRows []*models.ExecutorRunning) []taskStopTarget {
|
|
targets := make([]taskStopTarget, 0, len(runningRows))
|
|
for _, running := range runningRows {
|
|
if running == nil {
|
|
continue
|
|
}
|
|
targets = append(targets, taskStopTarget{
|
|
sessionID: strings.TrimSpace(running.SessionID),
|
|
executionID: strings.TrimSpace(running.AgentExecutionID),
|
|
})
|
|
}
|
|
return targets
|
|
}
|
|
|
|
func mergeTaskStopTargets(live, snapshot []taskStopTarget) []taskStopTarget {
|
|
sessionsWithExactTarget := exactTaskStopTargetSessions(live, snapshot)
|
|
targets := make([]taskStopTarget, 0, len(live)+len(snapshot))
|
|
seen := make(map[string]int, len(live)+len(snapshot))
|
|
appendTarget := func(target taskStopTarget) {
|
|
target.sessionID = strings.TrimSpace(target.sessionID)
|
|
target.executionID = strings.TrimSpace(target.executionID)
|
|
if target.sessionID == "" {
|
|
return
|
|
}
|
|
if target.executionID == "" {
|
|
if _, hasExact := sessionsWithExactTarget[target.sessionID]; hasExact {
|
|
return
|
|
}
|
|
}
|
|
key := target.sessionID + "\x00" + target.executionID
|
|
if index, exists := seen[key]; exists {
|
|
targets[index].terminal = targets[index].terminal || target.terminal
|
|
return
|
|
}
|
|
seen[key] = len(targets)
|
|
targets = append(targets, target)
|
|
}
|
|
for _, target := range live {
|
|
appendTarget(target)
|
|
}
|
|
for _, target := range snapshot {
|
|
appendTarget(target)
|
|
}
|
|
return targets
|
|
}
|
|
|
|
func exactTaskStopTargetSessions(targetSets ...[]taskStopTarget) map[string]struct{} {
|
|
exact := make(map[string]struct{})
|
|
for _, targets := range targetSets {
|
|
for _, target := range targets {
|
|
sessionID := strings.TrimSpace(target.sessionID)
|
|
if sessionID != "" && strings.TrimSpace(target.executionID) != "" {
|
|
exact[sessionID] = struct{}{}
|
|
}
|
|
}
|
|
}
|
|
return exact
|
|
}
|
|
|
|
// taskRuntimeStopOutcome separates the two independent effects a stop pass has on
|
|
// downstream cleanup. failed drives durable-job retry accounting AND row
|
|
// preservation (a still-uncertain stop must be retried and its row kept).
|
|
// preserve keeps a row (and its worktree) from being torn down WITHOUT counting
|
|
// as a retryable failure — used for a confirmed-dead but resume-safe row that was
|
|
// repaired in place under the resume-safety invariant.
|
|
type taskRuntimeStopOutcome struct {
|
|
failed map[string]struct{}
|
|
preserve map[string]struct{}
|
|
}
|
|
|
|
// taskCleanupPreserveRows is the set of sessions whose executor row and worktree
|
|
// must survive this cleanup pass: every failed (retryable) stop plus every
|
|
// resume-safe row repaired in place. performTaskCleanup treats membership as
|
|
// "do not delete this row/worktree".
|
|
func taskCleanupPreserveRows(outcome taskRuntimeStopOutcome) map[string]struct{} {
|
|
if len(outcome.preserve) == 0 {
|
|
return outcome.failed
|
|
}
|
|
preserve := make(map[string]struct{}, len(outcome.failed)+len(outcome.preserve))
|
|
for id := range outcome.failed {
|
|
preserve[id] = struct{}{}
|
|
}
|
|
for id := range outcome.preserve {
|
|
preserve[id] = struct{}{}
|
|
}
|
|
return preserve
|
|
}
|
|
|
|
func (s *Service) stopTaskRuntimeTargets(ctx context.Context, taskID string, stopTargets []taskStopTarget, stopReason, stopFailMsg string) taskRuntimeStopOutcome {
|
|
outcome := taskRuntimeStopOutcome{
|
|
failed: make(map[string]struct{}),
|
|
preserve: make(map[string]struct{}),
|
|
}
|
|
if s.executionStopper == nil || len(stopTargets) == 0 {
|
|
return outcome
|
|
}
|
|
for _, target := range stopTargets {
|
|
if context.Cause(ctx) != nil {
|
|
return outcome
|
|
}
|
|
if target.executionID != "" {
|
|
if err := s.executionStopper.StopExecution(ctx, target.executionID, stopReason, true); err != nil {
|
|
if runtimeStopAlreadyComplete(err) {
|
|
continue
|
|
}
|
|
if target.terminal {
|
|
s.logger.Debug("stop failed for terminal session execution (expected), proceeding with cleanup",
|
|
zap.String("task_id", taskID),
|
|
zap.String("session_id", target.sessionID),
|
|
zap.Error(err))
|
|
continue
|
|
}
|
|
outcome.failed[target.sessionID] = struct{}{}
|
|
s.logger.Warn(stopFailMsg,
|
|
zap.String("task_id", taskID),
|
|
zap.String("session_id", target.sessionID),
|
|
zap.String("execution_id", target.executionID),
|
|
zap.Error(err))
|
|
}
|
|
continue
|
|
}
|
|
if err := s.executionStopper.StopSession(ctx, target.sessionID, stopReason, true); err != nil {
|
|
if target.terminal {
|
|
s.logger.Debug("stop failed for terminal session (expected), proceeding with cleanup",
|
|
zap.String("task_id", taskID),
|
|
zap.String("session_id", target.sessionID),
|
|
zap.Error(err))
|
|
continue
|
|
}
|
|
// A session-level not-found is retryable by default (the execution may
|
|
// simply not be registered yet). But when the owned row is a
|
|
// confirmed-dead LOCAL runtime, the runtime really is gone — treat the
|
|
// stop as already complete so the durable cleanup job stops retrying a
|
|
// runtime that will never come back.
|
|
if runtimeStopAlreadyComplete(err) {
|
|
if running := s.confirmedDeadLocalRow(ctx, target.sessionID); running != nil {
|
|
s.reconcileConfirmedDeadRow(ctx, taskID, target.sessionID, running, &outcome)
|
|
continue
|
|
}
|
|
}
|
|
outcome.failed[target.sessionID] = struct{}{}
|
|
s.logger.Warn(stopFailMsg,
|
|
zap.String("task_id", taskID),
|
|
zap.String("session_id", target.sessionID),
|
|
zap.Error(err))
|
|
}
|
|
}
|
|
return outcome
|
|
}
|
|
|
|
// reconcileConfirmedDeadRow applies the resume-safety deletion invariant to a
|
|
// confirmed-dead local row whose stop reported not-found. A row holding a
|
|
// resume_token or backing a resumable session is repaired in place (never
|
|
// deleted) and marked preserve-without-retry; a row that is neither is left for
|
|
// the normal cleanup path to prune. Neither outcome is a failed stop, so the
|
|
// durable cleanup job does not retry solely because the runtime was absent.
|
|
func (s *Service) reconcileConfirmedDeadRow(
|
|
ctx context.Context,
|
|
taskID, sessionID string,
|
|
running *models.ExecutorRunning,
|
|
outcome *taskRuntimeStopOutcome,
|
|
) {
|
|
if models.RowMustBePreserved(running, s.sessionStateForCleanup(ctx, sessionID)) {
|
|
outcome.preserve[sessionID] = struct{}{}
|
|
if err := s.executors.RepairExecutorRunningDead(ctx, sessionID); err != nil {
|
|
s.logger.Warn("failed to repair confirmed-dead resume-safe runtime row",
|
|
zap.String("task_id", taskID),
|
|
zap.String("session_id", sessionID),
|
|
zap.Error(err))
|
|
}
|
|
s.logger.Info("session runtime confirmed dead and resume-safe; repairing row in place",
|
|
zap.String("task_id", taskID),
|
|
zap.String("session_id", sessionID))
|
|
return
|
|
}
|
|
s.logger.Info("session runtime confirmed dead and already gone; treating stop as complete",
|
|
zap.String("task_id", taskID),
|
|
zap.String("session_id", sessionID))
|
|
}
|
|
|
|
// sessionStateForCleanup best-effort reads a session's state for the
|
|
// resume-safety check. A missing session is treated as an empty (non-resumable)
|
|
// state so a row's preservation then hinges on its resume_token alone.
|
|
func (s *Service) sessionStateForCleanup(ctx context.Context, sessionID string) models.TaskSessionState {
|
|
if s.sessions == nil {
|
|
return ""
|
|
}
|
|
session, err := s.sessions.GetTaskSession(ctx, sessionID)
|
|
if err != nil || session == nil {
|
|
return ""
|
|
}
|
|
return session.State
|
|
}
|
|
|
|
func runtimeStopAlreadyComplete(err error) bool {
|
|
return errors.Is(err, runtimeapi.ErrNotFound)
|
|
}
|
|
|
|
// confirmedDeadLocalRow returns the executors_running row backing sessionID when
|
|
// it is a confirmed-dead LOCAL runtime — a local process handle that no longer
|
|
// exists on this host — and nil otherwise. It returns nil when no prober is
|
|
// wired, the row is missing/unreadable, or the runtime-aware liveness is
|
|
// anything other than Dead, so an alive, unknown, or remote row is never treated
|
|
// as absent.
|
|
func (s *Service) confirmedDeadLocalRow(ctx context.Context, sessionID string) *models.ExecutorRunning {
|
|
if s.rowLivenessProber == nil || s.executors == nil {
|
|
return nil
|
|
}
|
|
running, err := s.executors.GetExecutorRunningBySessionID(ctx, sessionID)
|
|
if err != nil || running == nil {
|
|
return nil
|
|
}
|
|
if s.rowLivenessProber.RowLiveness(running) != models.ProcessLivenessDead {
|
|
return nil
|
|
}
|
|
return running
|
|
}
|
|
|
|
// performTaskCleanup handles post-deletion cleanup operations.
|
|
// Handles worktree cleanup, executor_running records, and quick-chat workspace directories.
|
|
// Agent stopping is handled separately in the DeleteTask background goroutine.
|
|
// Returns a slice of errors encountered (empty if all succeeded).
|
|
func (s *Service) performTaskCleanup(
|
|
ctx context.Context,
|
|
taskID string,
|
|
sessions []*models.TaskSession,
|
|
worktrees []*worktree.Worktree,
|
|
stopTargets []taskStopTarget,
|
|
envCleanup taskEnvironmentCleanup,
|
|
preserveExecutorRows map[string]struct{},
|
|
) []error {
|
|
var errs []error
|
|
if cause := context.Cause(ctx); cause != nil {
|
|
return []error{cause}
|
|
}
|
|
hasPreservedRuntimes := len(preserveExecutorRows) > 0
|
|
|
|
if hasPreservedRuntimes {
|
|
s.logger.Warn("skipping shared environment cleanup after failed runtime stop",
|
|
zap.String("task_id", taskID),
|
|
zap.Int("preserved_runtime_count", len(preserveExecutorRows)))
|
|
}
|
|
errs = append(errs, s.cleanupDestructiveTaskResources(ctx, taskID, sessions, worktrees, envCleanup, preserveExecutorRows)...)
|
|
if cause := context.Cause(ctx); cause != nil {
|
|
return append(errs, cause)
|
|
}
|
|
|
|
sessionIDs := cleanupSessionIDs(sessions, stopTargets)
|
|
for _, sessionID := range sessionIDs {
|
|
if _, preserve := preserveExecutorRows[sessionID]; preserve {
|
|
s.logger.Warn("preserving executor runtime row after failed stop",
|
|
zap.String("task_id", taskID),
|
|
zap.String("session_id", sessionID))
|
|
continue
|
|
}
|
|
if cause := context.Cause(ctx); cause != nil {
|
|
return append(errs, cause)
|
|
}
|
|
if err := s.executors.DeleteExecutorRunningBySessionID(ctx, sessionID); err != nil {
|
|
s.logger.Debug("failed to delete executor runtime for session",
|
|
zap.String("task_id", taskID),
|
|
zap.String("session_id", sessionID),
|
|
zap.Error(err))
|
|
// Don't add to errs - this is a debug-level issue
|
|
}
|
|
}
|
|
|
|
// Cleanup quick-chat workspace directories for all tasks (not just ephemeral).
|
|
// Non-ephemeral office tasks also get quick-chat dirs allocated by manager_launch.go;
|
|
// both cases must be cleaned up to avoid a disk leak.
|
|
if cause := context.Cause(ctx); cause != nil {
|
|
return append(errs, cause)
|
|
}
|
|
errs = append(errs, s.cleanupQuickChatDirs(ctx, taskID, sessionIDs, preserveExecutorRows)...)
|
|
|
|
return errs
|
|
}
|
|
|
|
func (s *Service) signalCleanupDoneForTest() {
|
|
if s.cleanupDoneForTest == nil {
|
|
return
|
|
}
|
|
select {
|
|
case s.cleanupDoneForTest <- struct{}{}:
|
|
default:
|
|
}
|
|
}
|
|
|
|
func cleanupSessionIDs(sessions []*models.TaskSession, stopTargets []taskStopTarget) []string {
|
|
sessionIDs := make([]string, 0, len(sessions)+len(stopTargets))
|
|
seen := make(map[string]struct{})
|
|
for _, session := range sessions {
|
|
if session == nil {
|
|
continue
|
|
}
|
|
sessionIDs = appendUniqueSessionID(sessionIDs, seen, session.ID)
|
|
}
|
|
for _, target := range stopTargets {
|
|
sessionIDs = appendUniqueSessionID(sessionIDs, seen, target.sessionID)
|
|
}
|
|
return sessionIDs
|
|
}
|
|
|
|
func appendUniqueSessionID(sessionIDs []string, seen map[string]struct{}, sessionID string) []string {
|
|
if sessionID == "" {
|
|
return sessionIDs
|
|
}
|
|
if _, ok := seen[sessionID]; ok {
|
|
return sessionIDs
|
|
}
|
|
seen[sessionID] = struct{}{}
|
|
return append(sessionIDs, sessionID)
|
|
}
|
|
|
|
func (s *Service) cleanupQuickChatDirs(
|
|
ctx context.Context,
|
|
taskID string,
|
|
sessionIDs []string,
|
|
preserveExecutorRows map[string]struct{},
|
|
) []error {
|
|
if s.quickChatDir == "" {
|
|
return nil
|
|
}
|
|
var errs []error
|
|
for _, sessionID := range sessionIDs {
|
|
if _, preserve := preserveExecutorRows[sessionID]; preserve {
|
|
continue
|
|
}
|
|
sessionDir := filepath.Join(s.quickChatDir, sessionID)
|
|
if _, statErr := os.Stat(sessionDir); statErr != nil {
|
|
// Directory does not exist — nothing to remove.
|
|
continue
|
|
}
|
|
if cause := context.Cause(ctx); cause != nil {
|
|
return append(errs, cause)
|
|
}
|
|
if err := os.RemoveAll(sessionDir); err != nil {
|
|
s.logger.Warn("failed to cleanup quick-chat workspace directory",
|
|
zap.String("task_id", taskID),
|
|
zap.String("session_id", sessionID),
|
|
zap.String("path", sessionDir),
|
|
zap.Error(err))
|
|
errs = append(errs, fmt.Errorf("cleanup quick-chat dir %s: %w", sessionID, err))
|
|
continue
|
|
}
|
|
s.logger.Debug("cleaned up quick-chat workspace directory",
|
|
zap.String("task_id", taskID),
|
|
zap.String("session_id", sessionID),
|
|
zap.String("path", sessionDir))
|
|
}
|
|
return errs
|
|
}
|
|
|
|
func (s *Service) cleanupDestructiveTaskResources(
|
|
ctx context.Context,
|
|
taskID string,
|
|
sessions []*models.TaskSession,
|
|
worktrees []*worktree.Worktree,
|
|
envCleanup taskEnvironmentCleanup,
|
|
preserveExecutorRows map[string]struct{},
|
|
) []error {
|
|
var errs []error
|
|
if cause := context.Cause(ctx); cause != nil {
|
|
return []error{cause}
|
|
}
|
|
skipOwnedEnvironment, err := s.hasActiveOtherTaskSessionsForEnvironment(ctx, taskID, envCleanup.env)
|
|
if err != nil {
|
|
s.logger.Warn("skipping task environment cleanup after shared-environment ownership check failed",
|
|
zap.String("task_id", taskID),
|
|
zap.String("env_id", taskEnvironmentID(envCleanup.env)),
|
|
zap.Error(err))
|
|
errs = append(errs, fmt.Errorf("check task environment ownership %s: %w", taskEnvironmentID(envCleanup.env), err))
|
|
skipOwnedEnvironment = true
|
|
}
|
|
if skipOwnedEnvironment {
|
|
s.logger.Info("skipping task environment cleanup while another task still uses it",
|
|
zap.String("task_id", taskID),
|
|
zap.String("env_id", taskEnvironmentID(envCleanup.env)))
|
|
}
|
|
if cause := context.Cause(ctx); cause != nil {
|
|
return append(errs, cause)
|
|
}
|
|
if len(preserveExecutorRows) == 0 && !skipOwnedEnvironment {
|
|
errs = append(errs, s.cleanupTaskEnvironment(ctx, taskID, envCleanup)...)
|
|
if cause := context.Cause(ctx); cause != nil {
|
|
return append(errs, cause)
|
|
}
|
|
}
|
|
originalWorktreeCount := len(worktrees)
|
|
worktrees = s.filterOwnedWorktreesForTaskCleanup(ctx, taskID, sessions, worktrees, envCleanup.env, skipOwnedEnvironment)
|
|
worktrees = cleanupEligibleWorktrees(worktrees, envCleanup.env, preserveExecutorRows)
|
|
var referenceErrs []error
|
|
worktrees, referenceErrs = s.filterSharedWorktreesForTaskCleanup(ctx, taskID, sessions, worktrees)
|
|
errs = append(errs, referenceErrs...)
|
|
if len(worktrees) == 0 {
|
|
if originalWorktreeCount > 0 {
|
|
s.logger.Debug("no task worktrees eligible for cleanup",
|
|
zap.String("task_id", taskID),
|
|
zap.Int("input_count", originalWorktreeCount),
|
|
zap.Int("preserved_runtime_count", len(preserveExecutorRows)))
|
|
}
|
|
return errs
|
|
}
|
|
cleaner, ok := s.worktreeCleanup.(WorktreeBatchCleaner)
|
|
if !ok {
|
|
return errs
|
|
}
|
|
if cause := context.Cause(ctx); cause != nil {
|
|
return append(errs, cause)
|
|
}
|
|
if err := cleaner.CleanupWorktrees(ctx, worktrees); err != nil {
|
|
s.logger.Warn("failed to cleanup worktrees after delete",
|
|
zap.String("task_id", taskID),
|
|
zap.Error(err))
|
|
errs = append(errs, fmt.Errorf("cleanup worktrees: %w", err))
|
|
}
|
|
return errs
|
|
}
|
|
|
|
func (s *Service) filterSharedWorktreesForTaskCleanup(
|
|
ctx context.Context,
|
|
taskID string,
|
|
sessions []*models.TaskSession,
|
|
worktrees []*worktree.Worktree,
|
|
) ([]*worktree.Worktree, []error) {
|
|
guard, ok := s.worktreeCleanup.(worktreeReferenceGuard)
|
|
if !ok || len(worktrees) == 0 {
|
|
return worktrees, nil
|
|
}
|
|
excludedSessionIDs := taskSessionIDs(sessions)
|
|
filtered := worktrees[:0]
|
|
var errs []error
|
|
for _, wt := range worktrees {
|
|
count, err := guard.CountActiveWorktreeReferences(ctx, wt.ID, excludedSessionIDs)
|
|
if err != nil {
|
|
errs = append(errs, fmt.Errorf("count active references for worktree %s: %w", wt.ID, err))
|
|
continue
|
|
}
|
|
if count == 0 {
|
|
filtered = append(filtered, wt)
|
|
continue
|
|
}
|
|
if err := guard.ReleaseWorktreeReference(ctx, wt); err != nil {
|
|
errs = append(errs, fmt.Errorf("release shared worktree reference %s: %w", wt.ID, err))
|
|
continue
|
|
}
|
|
s.logger.Info("preserving worktree still referenced by another active task",
|
|
zap.String("task_id", taskID),
|
|
zap.String("worktree_id", wt.ID),
|
|
zap.Int("active_references", count))
|
|
}
|
|
return filtered, errs
|
|
}
|
|
|
|
func taskSessionIDs(sessions []*models.TaskSession) []string {
|
|
ids := make([]string, 0, len(sessions))
|
|
for _, session := range sessions {
|
|
if session != nil && session.ID != "" {
|
|
ids = append(ids, session.ID)
|
|
}
|
|
}
|
|
return ids
|
|
}
|
|
|
|
func taskEnvironmentID(env *models.TaskEnvironment) string {
|
|
if env == nil {
|
|
return ""
|
|
}
|
|
return env.ID
|
|
}
|
|
|
|
func (s *Service) hasActiveOtherTaskSessionsForEnvironment(ctx context.Context, taskID string, env *models.TaskEnvironment) (bool, error) {
|
|
if env == nil || env.ID == "" || s.sessions == nil {
|
|
return false, nil
|
|
}
|
|
checker, ok := s.sessions.(taskEnvironmentSessionUsageChecker)
|
|
if !ok {
|
|
return false, nil
|
|
}
|
|
return checker.HasActiveTaskSessionsByTaskEnvironmentExcludingTask(ctx, env.ID, taskID)
|
|
}
|
|
|
|
func (s *Service) preserveTaskEnvironmentForActiveBorrower(ctx context.Context, taskID string, env *models.TaskEnvironment) (bool, error) {
|
|
if env == nil || env.ID == "" || s.sessions == nil {
|
|
return false, nil
|
|
}
|
|
finder, ok := s.sessions.(taskEnvironmentSessionBorrowerFinder)
|
|
if !ok {
|
|
return false, nil
|
|
}
|
|
borrowerTaskID, err := finder.FindActiveTaskSessionTaskIDByTaskEnvironmentExcludingTask(ctx, env.ID, taskID)
|
|
if err != nil {
|
|
return false, fmt.Errorf("find task environment borrower %s: %w", env.ID, err)
|
|
}
|
|
if borrowerTaskID == "" {
|
|
return false, nil
|
|
}
|
|
ownerTransfer, ok := s.taskEnvironments.(taskEnvironmentOwnerTransferer)
|
|
if !ok {
|
|
return false, fmt.Errorf("task environment repository cannot transfer borrowed environment %s", env.ID)
|
|
}
|
|
if err := ownerTransfer.TransferTaskEnvironmentToTask(ctx, env.ID, borrowerTaskID); err != nil {
|
|
return false, fmt.Errorf("transfer task environment %s to %s: %w", env.ID, borrowerTaskID, err)
|
|
}
|
|
env.TaskID = borrowerTaskID
|
|
return true, nil
|
|
}
|
|
|
|
func (s *Service) filterOwnedWorktreesForTaskCleanup(
|
|
ctx context.Context,
|
|
taskID string,
|
|
sessions []*models.TaskSession,
|
|
worktrees []*worktree.Worktree,
|
|
ownedEnv *models.TaskEnvironment,
|
|
skipOwnedEnvironment bool,
|
|
) []*worktree.Worktree {
|
|
if len(worktrees) == 0 {
|
|
return worktrees
|
|
}
|
|
bySession := make(map[string]*models.TaskSession, len(sessions))
|
|
for _, sess := range sessions {
|
|
if sess != nil && sess.ID != "" {
|
|
bySession[sess.ID] = sess
|
|
}
|
|
}
|
|
envCache := map[string]*models.TaskEnvironment{}
|
|
filtered := worktrees[:0]
|
|
for _, wt := range worktrees {
|
|
if wt == nil {
|
|
continue
|
|
}
|
|
sess, sessionLoaded := bySession[wt.SessionID]
|
|
if s.taskOwnsSessionWorktree(ctx, taskID, wt.SessionID, sess, sessionLoaded, ownedEnv, skipOwnedEnvironment, envCache) {
|
|
filtered = append(filtered, wt)
|
|
}
|
|
}
|
|
return filtered
|
|
}
|
|
|
|
func (s *Service) taskOwnsSessionWorktree(
|
|
ctx context.Context,
|
|
taskID string,
|
|
sessionID string,
|
|
session *models.TaskSession,
|
|
sessionLoaded bool,
|
|
ownedEnv *models.TaskEnvironment,
|
|
skipOwnedEnvironment bool,
|
|
envCache map[string]*models.TaskEnvironment,
|
|
) bool {
|
|
if session == nil {
|
|
if sessionID == "" {
|
|
return true
|
|
}
|
|
if !sessionLoaded {
|
|
s.logger.Warn("skipping task worktree cleanup because session ownership cannot be checked",
|
|
zap.String("task_id", taskID),
|
|
zap.String("session_id", sessionID))
|
|
}
|
|
return false
|
|
}
|
|
if session.TaskEnvironmentID == "" {
|
|
return true
|
|
}
|
|
if ownedEnv != nil && session.TaskEnvironmentID == ownedEnv.ID {
|
|
return !skipOwnedEnvironment
|
|
}
|
|
if s.taskEnvironments == nil {
|
|
s.logger.Warn("skipping task worktree cleanup because task environment ownership cannot be checked",
|
|
zap.String("task_id", taskID),
|
|
zap.String("session_id", session.ID),
|
|
zap.String("task_environment_id", session.TaskEnvironmentID))
|
|
return false
|
|
}
|
|
env, ok := envCache[session.TaskEnvironmentID]
|
|
if !ok {
|
|
var err error
|
|
env, err = s.taskEnvironments.GetTaskEnvironment(ctx, session.TaskEnvironmentID)
|
|
if err != nil {
|
|
s.logger.Warn("skipping task worktree cleanup because task environment lookup failed",
|
|
zap.String("task_id", taskID),
|
|
zap.String("session_id", session.ID),
|
|
zap.String("task_environment_id", session.TaskEnvironmentID),
|
|
zap.Error(err))
|
|
return false
|
|
}
|
|
envCache[session.TaskEnvironmentID] = env
|
|
}
|
|
if env == nil || env.TaskID != taskID {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func cleanupEligibleWorktrees(worktrees []*worktree.Worktree, env *models.TaskEnvironment, preserveExecutorRows map[string]struct{}) []*worktree.Worktree {
|
|
worktrees = excludeEnvironmentWorktree(worktrees, env)
|
|
if len(preserveExecutorRows) == 0 || len(worktrees) == 0 {
|
|
return worktrees
|
|
}
|
|
filtered := worktrees[:0]
|
|
for _, wt := range worktrees {
|
|
if wt == nil {
|
|
continue
|
|
}
|
|
if _, preserve := preserveExecutorRows[wt.SessionID]; preserve {
|
|
continue
|
|
}
|
|
filtered = append(filtered, wt)
|
|
}
|
|
return filtered
|
|
}
|
|
|
|
func (s *Service) cleanupTaskEnvironment(
|
|
ctx context.Context,
|
|
taskID string,
|
|
cleanup taskEnvironmentCleanup,
|
|
) []error {
|
|
if cleanup.env == nil {
|
|
return nil
|
|
}
|
|
if cause := context.Cause(ctx); cause != nil {
|
|
return []error{cause}
|
|
}
|
|
if err := s.teardownEnvironmentResources(ctx, cleanup.env); err != nil {
|
|
s.logger.Warn("failed to teardown task environment during task cleanup",
|
|
zap.String("task_id", taskID),
|
|
zap.String("env_id", cleanup.env.ID),
|
|
zap.Error(err))
|
|
return []error{fmt.Errorf("teardown task environment %s: %w", cleanup.env.ID, err)}
|
|
}
|
|
if cleanup.deleteRow {
|
|
if cause := context.Cause(ctx); cause != nil {
|
|
return []error{cause}
|
|
}
|
|
if err := s.taskEnvironments.DeleteTaskEnvironment(ctx, cleanup.env.ID); err != nil &&
|
|
!errors.Is(err, taskrepo.ErrTaskEnvironmentNotFound) {
|
|
s.logger.Warn("failed to delete task environment row during task cleanup",
|
|
zap.String("task_id", taskID),
|
|
zap.String("env_id", cleanup.env.ID),
|
|
zap.Error(err))
|
|
return []error{fmt.Errorf("delete task environment row %s: %w", cleanup.env.ID, err)}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func excludeEnvironmentWorktree(worktrees []*worktree.Worktree, env *models.TaskEnvironment) []*worktree.Worktree {
|
|
if env == nil || env.WorktreeID == "" || len(worktrees) == 0 {
|
|
return worktrees
|
|
}
|
|
filtered := worktrees[:0]
|
|
for _, wt := range worktrees {
|
|
if wt == nil || wt.ID == env.WorktreeID {
|
|
continue
|
|
}
|
|
filtered = append(filtered, wt)
|
|
}
|
|
return filtered
|
|
}
|
|
|
|
// ListTasks returns all tasks for a workflow
|
|
func (s *Service) ListTasks(ctx context.Context, workflowID string) ([]*models.Task, error) {
|
|
if err := s.authorizeWorkflowID(ctx, workflowID); err != nil {
|
|
return nil, err
|
|
}
|
|
workflow, err := s.workflows.GetWorkflow(ctx, workflowID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if workflow == nil {
|
|
return nil, fmt.Errorf("workflow not found: %s", workflowID)
|
|
}
|
|
tasks, err := s.tasks.ListTasks(ctx, workflowID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
tasks = filterTasksByWorkspace(tasks, workflow.WorkspaceID)
|
|
|
|
if err := s.loadTaskRepositoriesBatch(ctx, tasks); err != nil {
|
|
s.logger.Error("failed to batch-load task repositories", zap.Error(err))
|
|
}
|
|
s.hydrateTaskWorkspaceFoldersBatch(ctx, tasks)
|
|
|
|
return tasks, nil
|
|
}
|
|
|
|
func filterTasksByWorkspace(tasks []*models.Task, workspaceID string) []*models.Task {
|
|
filtered := tasks[:0]
|
|
for _, task := range tasks {
|
|
if task != nil && task.WorkspaceID == workspaceID {
|
|
filtered = append(filtered, task)
|
|
}
|
|
}
|
|
return filtered
|
|
}
|
|
|
|
type workspaceArchiveModeLister interface {
|
|
ListTasksByWorkspaceWithArchiveMode(ctx context.Context, workspaceID, workflowID, repositoryID, query string, page, pageSize int, sort string, includeArchived, includeEphemeral, onlyEphemeral, excludeConfig, onlyArchived bool) ([]*models.Task, int, error)
|
|
}
|
|
|
|
func listTasksByWorkspaceWithArchiveMode(repo taskrepo.TaskRepository, ctx context.Context, workspaceID, workflowID, repositoryID, query string, page, pageSize int, sort string, includeArchived, includeEphemeral, onlyEphemeral, excludeConfig, onlyArchived bool) ([]*models.Task, int, error) {
|
|
if lister, ok := repo.(workspaceArchiveModeLister); ok {
|
|
return lister.ListTasksByWorkspaceWithArchiveMode(ctx, workspaceID, workflowID, repositoryID, query, page, pageSize, sort, includeArchived, includeEphemeral, onlyEphemeral, excludeConfig, onlyArchived)
|
|
}
|
|
if onlyArchived {
|
|
return nil, 0, fmt.Errorf("archived-only workspace task listing is unavailable")
|
|
}
|
|
return repo.ListTasksByWorkspace(ctx, workspaceID, workflowID, repositoryID, query, page, pageSize, sort, includeArchived, includeEphemeral, onlyEphemeral, excludeConfig)
|
|
}
|
|
|
|
// ListTasksByWorkspace returns paginated tasks for a workspace with task repositories loaded.
|
|
// If query is non-empty, filters by task title, description, repository name, or repository path.
|
|
// workflowID and repositoryID, when non-empty, further restrict results to that workflow/repository.
|
|
func (s *Service) ListTasksByWorkspace(ctx context.Context, workspaceID, workflowID, repositoryID, query string, page, pageSize int, sort string, includeArchived, includeEphemeral, onlyEphemeral, excludeConfig bool) ([]*models.Task, int, error) {
|
|
return s.ListTasksByWorkspaceWithArchiveMode(ctx, workspaceID, workflowID, repositoryID, query, page, pageSize, sort, includeArchived, includeEphemeral, onlyEphemeral, excludeConfig, false)
|
|
}
|
|
|
|
// ListTasksByWorkspaceWithArchiveMode is the additive workspace-list contract
|
|
// used by the sidebar archive view. onlyArchived takes precedence over
|
|
// includeArchived when both are true.
|
|
func (s *Service) ListTasksByWorkspaceWithArchiveMode(ctx context.Context, workspaceID, workflowID, repositoryID, query string, page, pageSize int, sort string, includeArchived, includeEphemeral, onlyEphemeral, excludeConfig, onlyArchived bool) ([]*models.Task, int, error) {
|
|
if err := s.authorizeWorkspaceID(ctx, workspaceID); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
tasks, total, err := listTasksByWorkspaceWithArchiveMode(s.tasks, ctx, workspaceID, workflowID, repositoryID, query, page, pageSize, sort, includeArchived, includeEphemeral, onlyEphemeral, excludeConfig, onlyArchived)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
tasks, total = s.augmentWithPRMatches(ctx, tasks, total, prSearchOptions{
|
|
workspaceID: workspaceID,
|
|
workflowID: workflowID,
|
|
repositoryID: repositoryID,
|
|
query: query,
|
|
page: page,
|
|
pageSize: pageSize,
|
|
includeArchived: includeArchived,
|
|
onlyArchived: onlyArchived,
|
|
includeEphemeral: includeEphemeral,
|
|
onlyEphemeral: onlyEphemeral,
|
|
excludeConfig: excludeConfig,
|
|
})
|
|
|
|
if err := s.loadTaskRepositoriesBatch(ctx, tasks); err != nil {
|
|
s.logger.Error("failed to batch-load task repositories", zap.Error(err))
|
|
}
|
|
s.hydrateTaskWorkspaceFoldersBatch(ctx, tasks)
|
|
|
|
return tasks, total, nil
|
|
}
|
|
|
|
type prSearchOptions struct {
|
|
workspaceID string
|
|
workflowID string
|
|
repositoryID string
|
|
query string
|
|
page int
|
|
pageSize int
|
|
includeArchived bool
|
|
onlyArchived bool
|
|
includeEphemeral bool
|
|
onlyEphemeral bool
|
|
excludeConfig bool
|
|
}
|
|
|
|
// parsePRQuery extracts a positive PR number from a search query, accepting an
|
|
// optional leading '#'. Returns (0, false) when the query is not a PR number.
|
|
func parsePRQuery(query string) (int, bool) {
|
|
trimmed := strings.TrimPrefix(strings.TrimSpace(query), "#")
|
|
if trimmed == "" {
|
|
return 0, false
|
|
}
|
|
n, err := strconv.Atoi(trimmed)
|
|
if err != nil || n <= 0 {
|
|
return 0, false
|
|
}
|
|
return n, true
|
|
}
|
|
|
|
// isConfigTask reports whether a task is a config-mode task (mirrors the SQL
|
|
// `json_extract(metadata, '$.config_mode') IS NOT 1` filter). JSON-decoded
|
|
// numbers arrive as float64, so accept both numeric 1 and bool true.
|
|
func isConfigTask(task *models.Task) bool {
|
|
switch v := task.Metadata["config_mode"].(type) {
|
|
case float64:
|
|
return v == 1
|
|
case int:
|
|
return v == 1
|
|
case bool:
|
|
return v
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
// augmentWithPRMatches surfaces tasks associated with a PR number when the
|
|
// search query looks like one. PR matches are prepended (most relevant) and
|
|
// deduped against the existing results; `total` grows by the net-new count.
|
|
// Best-effort: a missing resolver or a lookup error leaves results unchanged.
|
|
//
|
|
// Augmentation only applies to the first page of an unscoped search. It is
|
|
// skipped for page > 1 (the prepend+truncate only makes sense against page 1,
|
|
// otherwise a PR match would re-appear on every page and push out a real
|
|
// result) and when a workflow or repository filter is set (a PR-matched task
|
|
// isn't guaranteed to satisfy those filters, and the only caller that searches
|
|
// by PR number — the Cmd+K command panel — sets neither).
|
|
func (s *Service) augmentWithPRMatches(ctx context.Context, tasks []*models.Task, total int, opts prSearchOptions) ([]*models.Task, int) {
|
|
if opts.page > 1 || opts.workflowID != "" || opts.repositoryID != "" {
|
|
return tasks, total
|
|
}
|
|
prNum, ok := parsePRQuery(opts.query)
|
|
if !ok || s.prTaskResolver == nil {
|
|
return tasks, total
|
|
}
|
|
ids, err := s.prTaskResolver.FindTaskIDsByPRNumber(ctx, opts.workspaceID, prNum)
|
|
if err != nil {
|
|
s.logger.Warn("PR-number task lookup failed", zap.Int("pr_number", prNum), zap.Error(err))
|
|
return tasks, total
|
|
}
|
|
|
|
matched := s.fetchPRMatchedTasks(ctx, ids, tasks, opts)
|
|
if len(matched) == 0 {
|
|
return tasks, total
|
|
}
|
|
|
|
merged := make([]*models.Task, 0, len(matched)+len(tasks))
|
|
merged = append(merged, matched...)
|
|
merged = append(merged, tasks...)
|
|
total += len(matched)
|
|
if len(merged) > opts.pageSize {
|
|
merged = merged[:opts.pageSize]
|
|
}
|
|
return merged, total
|
|
}
|
|
|
|
// fetchPRMatchedTasks batch-loads the resolver's task IDs that aren't already in
|
|
// `existing`, applies the same visibility filters as the repository search, and
|
|
// returns the survivors in resolver order. The resolver returns distinct IDs,
|
|
// so excluding the already-present ones is enough to keep the result deduped.
|
|
func (s *Service) fetchPRMatchedTasks(ctx context.Context, ids []string, existing []*models.Task, opts prSearchOptions) []*models.Task {
|
|
seen := make(map[string]struct{}, len(existing))
|
|
for _, t := range existing {
|
|
seen[t.ID] = struct{}{}
|
|
}
|
|
var fetchIDs []string
|
|
for _, id := range ids {
|
|
if _, ok := seen[id]; !ok {
|
|
fetchIDs = append(fetchIDs, id)
|
|
}
|
|
}
|
|
if len(fetchIDs) == 0 {
|
|
return nil
|
|
}
|
|
fetched, err := s.tasks.GetTasksByIDs(ctx, fetchIDs)
|
|
if err != nil {
|
|
s.logger.Warn("PR-match task fetch failed", zap.Error(err))
|
|
return nil
|
|
}
|
|
byID := make(map[string]*models.Task, len(fetched))
|
|
for _, t := range fetched {
|
|
byID[t.ID] = t
|
|
}
|
|
var matched []*models.Task
|
|
for _, id := range fetchIDs {
|
|
task := byID[id]
|
|
if task == nil || s.prMatchFilteredOut(task, opts) {
|
|
continue
|
|
}
|
|
matched = append(matched, task)
|
|
}
|
|
return matched
|
|
}
|
|
|
|
// prMatchFilteredOut applies the same visibility filters the repository search
|
|
// uses, so a PR-matched task respects includeArchived / ephemeral / config flags.
|
|
func (s *Service) prMatchFilteredOut(task *models.Task, opts prSearchOptions) bool {
|
|
if opts.onlyArchived {
|
|
if task.ArchivedAt == nil {
|
|
return true
|
|
}
|
|
} else if !opts.includeArchived && task.ArchivedAt != nil {
|
|
return true
|
|
}
|
|
if opts.onlyEphemeral && !task.IsEphemeral {
|
|
return true
|
|
}
|
|
if !opts.includeEphemeral && !opts.onlyEphemeral && task.IsEphemeral {
|
|
return true
|
|
}
|
|
if opts.excludeConfig && isConfigTask(task) {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// loadTaskRepositoriesBatch loads repositories for multiple tasks in a single query.
|
|
func (s *Service) loadTaskRepositoriesBatch(ctx context.Context, tasks []*models.Task) error {
|
|
if len(tasks) == 0 {
|
|
return nil
|
|
}
|
|
taskIDs := make([]string, len(tasks))
|
|
for i, t := range tasks {
|
|
taskIDs[i] = t.ID
|
|
}
|
|
repoMap, err := s.taskRepos.ListTaskRepositoriesByTaskIDs(ctx, taskIDs)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, task := range tasks {
|
|
task.Repositories = repoMap[task.ID]
|
|
}
|
|
return nil
|
|
}
|