9b82595cfd
Users sometimes need a clean slate — broken packages, stale caches, a container that drifted into a bad state. Reset Environment is the explicit escape hatch: tear down the recorded container, sandbox, and/or worktree for a task and delete the TaskEnvironment row so the next session starts fresh. - service.ResetTaskEnvironment(taskID, ResetOptions) is the entry point. ResetOptions.PushBranch optionally git-pushes the workspace branch before teardown so committed work isn't dropped on the floor; a failed push aborts the reset and leaves the environment intact. - A new EnvironmentDestroyer interface in the task service hides the per-resource teardown behind a single seam, implemented in cmd/kandev by delegating to lifecycle.Manager (containers, sprites) and worktree.Manager (worktrees, branch preserved). - lifecycle.Manager.DestroyContainer / DestroySandbox dispatch to the Docker and Sprites executor backends without leaking model types into the lifecycle package. - Reset is blocked while any session on the task still has an ExecutorRunning row (returns ErrSessionRunning → HTTP 409). Per-resource teardown is best-effort but the DB row is only removed once everything is confirmed gone or already missing, so a partial failure is retryable by clicking Reset again. - Endpoint: POST /api/v1/tasks/:id/environment/reset.
189 lines
7.0 KiB
Go
189 lines
7.0 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"github.com/kandev/kandev/internal/task/models"
|
|
)
|
|
|
|
// EnvironmentDestroyer tears down the runtime resources recorded on a TaskEnvironment.
|
|
// Implemented by an adapter over the lifecycle.Manager and worktree.Manager.
|
|
// Worktree destruction preserves the underlying branch — user data that hasn't been
|
|
// pushed is never deleted by reset.
|
|
type EnvironmentDestroyer interface {
|
|
DestroyContainer(ctx context.Context, containerID string) error
|
|
DestroySandbox(ctx context.Context, sandboxID, executionID string) error
|
|
DestroyWorktree(ctx context.Context, worktreeID string) error
|
|
// PushEnvironmentBranch best-effort pushes the current branch of the environment's
|
|
// workspace to its upstream. Returns an error if the push fails; callers can decide
|
|
// whether to abort the reset on failure.
|
|
PushEnvironmentBranch(ctx context.Context, env *models.TaskEnvironment) error
|
|
}
|
|
|
|
// ResetOptions controls destructive behavior of ResetTaskEnvironment.
|
|
type ResetOptions struct {
|
|
// PushBranch runs `git push` on the environment's current branch before teardown.
|
|
// If the push fails, ResetTaskEnvironment aborts and the environment stays intact.
|
|
PushBranch bool
|
|
}
|
|
|
|
// SessionRunningChecker reports whether a session has a live executor running.
|
|
// Used by ResetTaskEnvironment to block resets when the user still has an agent
|
|
// attached to the environment.
|
|
type SessionRunningChecker interface {
|
|
IsAnySessionRunningForTask(ctx context.Context, taskID string) (bool, error)
|
|
}
|
|
|
|
// ErrSessionRunning is returned by ResetTaskEnvironment when at least one session
|
|
// on the task is still actively running and the caller must stop it first.
|
|
var ErrSessionRunning = errors.New("active session is running on this task; stop it before resetting the environment")
|
|
|
|
// ErrNoEnvironment is returned when the task has no TaskEnvironment to reset.
|
|
var ErrNoEnvironment = errors.New("no environment exists for this task")
|
|
|
|
// SetEnvironmentDestroyer wires the runtime-resource destroyer used by ResetTaskEnvironment.
|
|
func (s *Service) SetEnvironmentDestroyer(d EnvironmentDestroyer) {
|
|
s.envDestroyer = d
|
|
}
|
|
|
|
// SetSessionRunningChecker wires a custom running-session guard. When unset, the
|
|
// service falls back to a default implementation over the existing session repo.
|
|
func (s *Service) SetSessionRunningChecker(c SessionRunningChecker) {
|
|
s.sessionRunningChecker = c
|
|
}
|
|
|
|
// checkAnySessionRunning delegates to the configured checker or falls back to
|
|
// the default (session has an ExecutorRunning row).
|
|
func (s *Service) checkAnySessionRunning(ctx context.Context, taskID string) (bool, error) {
|
|
if s.sessionRunningChecker != nil {
|
|
return s.sessionRunningChecker.IsAnySessionRunningForTask(ctx, taskID)
|
|
}
|
|
return s.isAnySessionRunning(ctx, taskID)
|
|
}
|
|
|
|
// isAnySessionRunning is the default SessionRunningChecker: a session is running
|
|
// if it has an ExecutorRunning row in the repo.
|
|
func (s *Service) isAnySessionRunning(ctx context.Context, taskID string) (bool, error) {
|
|
sessions, err := s.sessions.ListTaskSessions(ctx, taskID)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
for _, sess := range sessions {
|
|
if sess == nil {
|
|
continue
|
|
}
|
|
running, err := s.executors.GetExecutorRunningBySessionID(ctx, sess.ID)
|
|
if err != nil {
|
|
if errors.Is(err, models.ErrExecutorRunningNotFound) {
|
|
continue
|
|
}
|
|
return false, err
|
|
}
|
|
if running != nil {
|
|
return true, nil
|
|
}
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
// GetTaskEnvironmentByTaskID returns the active task environment for a task.
|
|
// Returns nil if no environment exists yet.
|
|
func (s *Service) GetTaskEnvironmentByTaskID(ctx context.Context, taskID string) (*models.TaskEnvironment, error) {
|
|
return s.taskEnvironments.GetTaskEnvironmentByTaskID(ctx, taskID)
|
|
}
|
|
|
|
// ResetTaskEnvironment tears down the task's current environment (container/sandbox/worktree)
|
|
// and deletes the TaskEnvironment row, so the next session launch starts fresh.
|
|
//
|
|
// Guards:
|
|
// - No environment → ErrNoEnvironment
|
|
// - Any session on the task is actively running → ErrSessionRunning
|
|
//
|
|
// Cleanup is best-effort per resource. If any resource fails to destroy, the
|
|
// TaskEnvironment row is preserved so the user can retry. Success deletes the row.
|
|
//
|
|
// If opts.PushBranch is set, the branch is pushed before teardown; a failed push
|
|
// aborts the reset and leaves the environment intact so the user can investigate.
|
|
func (s *Service) ResetTaskEnvironment(ctx context.Context, taskID string, opts ResetOptions) error {
|
|
env, err := s.taskEnvironments.GetTaskEnvironmentByTaskID(ctx, taskID)
|
|
if err != nil {
|
|
return fmt.Errorf("lookup environment: %w", err)
|
|
}
|
|
if env == nil {
|
|
return ErrNoEnvironment
|
|
}
|
|
|
|
running, err := s.checkAnySessionRunning(ctx, taskID)
|
|
if err != nil {
|
|
s.logger.Warn("failed to check running sessions before reset, continuing",
|
|
zap.String("task_id", taskID),
|
|
zap.Error(err))
|
|
} else if running {
|
|
return ErrSessionRunning
|
|
}
|
|
|
|
s.logger.Info("resetting task environment",
|
|
zap.String("task_id", taskID),
|
|
zap.String("env_id", env.ID),
|
|
zap.String("executor_type", env.ExecutorType),
|
|
zap.String("container_id", env.ContainerID),
|
|
zap.String("sandbox_id", env.SandboxID),
|
|
zap.String("worktree_id", env.WorktreeID),
|
|
zap.Bool("push_branch", opts.PushBranch))
|
|
|
|
if opts.PushBranch {
|
|
if s.envDestroyer == nil {
|
|
return fmt.Errorf("environment destroyer not configured; cannot push branch")
|
|
}
|
|
if err := s.envDestroyer.PushEnvironmentBranch(ctx, env); err != nil {
|
|
return fmt.Errorf("push branch before reset: %w", err)
|
|
}
|
|
}
|
|
|
|
if err := s.teardownEnvironmentResources(ctx, env); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := s.taskEnvironments.DeleteTaskEnvironment(ctx, env.ID); err != nil {
|
|
return fmt.Errorf("delete task environment row: %w", err)
|
|
}
|
|
s.logger.Info("task environment reset complete",
|
|
zap.String("task_id", taskID),
|
|
zap.String("env_id", env.ID))
|
|
return nil
|
|
}
|
|
|
|
// teardownEnvironmentResources destroys runtime resources (container/sandbox/worktree)
|
|
// recorded on a TaskEnvironment. On any failure, the caller should preserve the row.
|
|
func (s *Service) teardownEnvironmentResources(ctx context.Context, env *models.TaskEnvironment) error {
|
|
if env.ContainerID != "" {
|
|
if s.envDestroyer == nil {
|
|
return fmt.Errorf("environment destroyer not configured; cannot remove container")
|
|
}
|
|
if err := s.envDestroyer.DestroyContainer(ctx, env.ContainerID); err != nil {
|
|
return fmt.Errorf("destroy container %s: %w", env.ContainerID, err)
|
|
}
|
|
}
|
|
if env.SandboxID != "" {
|
|
if s.envDestroyer == nil {
|
|
return fmt.Errorf("environment destroyer not configured; cannot destroy sandbox")
|
|
}
|
|
if err := s.envDestroyer.DestroySandbox(ctx, env.SandboxID, env.AgentExecutionID); err != nil {
|
|
return fmt.Errorf("destroy sandbox %s: %w", env.SandboxID, err)
|
|
}
|
|
}
|
|
if env.WorktreeID != "" {
|
|
if s.envDestroyer == nil {
|
|
return fmt.Errorf("environment destroyer not configured; cannot remove worktree")
|
|
}
|
|
if err := s.envDestroyer.DestroyWorktree(ctx, env.WorktreeID); err != nil {
|
|
return fmt.Errorf("destroy worktree %s: %w", env.WorktreeID, err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|