Class: Aidp::Watch::WorktreeCleanupJob

Inherits:
Object
  • Object
show all
Defined in:
lib/aidp/watch/worktree_cleanup_job.rb

Overview

Handles automatic cleanup of worktrees whose branches have been merged into main. Runs periodically during watch mode according to configured frequency.

Requirements (from issue #367):

  • Skip worktrees that are not "clean" (have uncommitted changes)
  • Run silently with log entries
  • Skip failed worktrees and retry later
  • Default to weekly cleanup frequency

Constant Summary collapse

SECONDS_PER_DAY =
86_400
SECONDS_PER_WEEK =
604_800

Instance Method Summary collapse

Constructor Details

#initialize(project_dir:, config: {}) ⇒ WorktreeCleanupJob

Returns a new instance of WorktreeCleanupJob.

Parameters:

  • project_dir (String)

    Project root directory

  • config (Hash) (defaults to: {})

    Cleanup configuration from aidp.yml



22
23
24
25
26
27
28
# File 'lib/aidp/watch/worktree_cleanup_job.rb', line 22

def initialize(project_dir:, config: {})
  @project_dir = project_dir
  @config = normalize_config(config)
  Aidp.log_debug("worktree_cleanup_job", "initialized",
    project_dir: project_dir,
    config: @config)
end

Instance Method Details

#cleanup_due?(last_cleanup_at) ⇒ Boolean

Check if cleanup is due based on last cleanup time and configured frequency

Parameters:

  • last_cleanup_at (Time, nil)

    Time of last cleanup run

Returns:

  • (Boolean)

    True if cleanup should run



34
35
36
37
38
39
40
# File 'lib/aidp/watch/worktree_cleanup_job.rb', line 34

def cleanup_due?(last_cleanup_at)
  return true unless enabled?
  return true if last_cleanup_at.nil?

  elapsed = Time.now - last_cleanup_at
  elapsed >= cleanup_interval_seconds
end

#cleanup_interval_secondsInteger

Get the configured cleanup interval in seconds

Returns:

  • (Integer)


89
90
91
92
93
94
95
96
97
98
# File 'lib/aidp/watch/worktree_cleanup_job.rb', line 89

def cleanup_interval_seconds
  case @config[:frequency]
  when "daily"
    SECONDS_PER_DAY
  when "weekly"
    SECONDS_PER_WEEK
  else
    SECONDS_PER_WEEK
  end
end

#enabled?Boolean

Check if cleanup is enabled in configuration

Returns:

  • (Boolean)


82
83
84
# File 'lib/aidp/watch/worktree_cleanup_job.rb', line 82

def enabled?
  @config[:enabled]
end

#executeHash

Execute cleanup of merged worktrees

Returns:

  • (Hash)

    Result containing cleaned count, skipped count, and errors



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/aidp/watch/worktree_cleanup_job.rb', line 45

def execute
  Aidp.log_info("worktree_cleanup_job", "cleanup_started",
    base_branch: base_branch,
    delete_branch: delete_branch?)

  return {cleaned: 0, skipped: 0, errors: []} unless enabled?

  worktrees = list_worktrees
  Aidp.log_debug("worktree_cleanup_job", "worktrees_found", count: worktrees.size)

  cleaned = 0
  skipped = 0
  errors = []

  worktrees.each do |worktree|
    result = process_worktree(worktree)
    case result[:status]
    when :cleaned
      cleaned += 1
    when :skipped
      skipped += 1
    when :error
      errors << {slug: worktree[:slug], error: result[:error]}
    end
  end

  Aidp.log_info("worktree_cleanup_job", "cleanup_completed",
    cleaned: cleaned,
    skipped: skipped,
    errors_count: errors.size)

  {cleaned: cleaned, skipped: skipped, errors: errors}
end