Class: Aidp::PrWorktreeManager

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

Overview

Manages worktrees specifically for Pull Request branches

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_repo_path: nil, project_dir: nil, worktree_registry_path: nil) ⇒ PrWorktreeManager

Returns a new instance of PrWorktreeManager.



8
9
10
11
12
13
14
15
16
17
# File 'lib/aidp/pr_worktree_manager.rb', line 8

def initialize(base_repo_path: nil, project_dir: nil, worktree_registry_path: nil)
  @base_repo_path = base_repo_path || project_dir || Dir.pwd
  @project_dir = project_dir
  @worktree_registry_path = worktree_registry_path || File.join(
    project_dir || File.expand_path("~/.aidp"),
    "pr_worktrees.json"
  )
  FileUtils.mkdir_p(File.dirname(@worktree_registry_path))
  @worktrees = load_registry
end

Instance Attribute Details

#worktree_registry_pathObject (readonly)

Returns the value of attribute worktree_registry_path.



19
20
21
# File 'lib/aidp/pr_worktree_manager.rb', line 19

def worktree_registry_path
  @worktree_registry_path
end

Instance Method Details

#apply_worktree_changes(pr_number, changes) ⇒ Object

Enhanced method to apply changes to the worktree with robust handling



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/aidp/pr_worktree_manager.rb', line 312

def apply_worktree_changes(pr_number, changes)
  Aidp.log_debug(
    "pr_worktree_manager", "applying_worktree_changes",
    pr_number: pr_number,
    changes: changes
  )

  # Find the worktree for the PR
  worktree_path = find_worktree(pr_number)
  raise "No worktree found for PR #{pr_number}" unless worktree_path

  # Track successful and failed file modifications
  successful_files = []
  failed_files = []

  Dir.chdir(worktree_path) do
    changes.fetch(:files, []).each_with_index do |file, index|
      operation = changes.fetch(:operations, [])[index] || :modify
      file_path = File.join(worktree_path, file)

      # Enhanced file manipulation with operation-specific handling
      begin
        # Ensure safe file path (prevent directory traversal)
        canonical_path = File.expand_path(file_path)
        raise SecurityError, "Unsafe file path" unless canonical_path.start_with?(worktree_path)

        # Ensure directory exists for file creation
        FileUtils.mkdir_p(File.dirname(file_path)) unless File.exist?(File.dirname(file_path))

        case operation
        when :create, :modify
          File.write(file_path, "# File #{(operation == :create) ? "added" : "modified"} by AIDP request-changes workflow\n")
        when :delete
          FileUtils.rm_f(file_path)
        else
          Aidp.log_warn(
            "pr_worktree_manager", "unknown_file_operation",
            file: file,
            operation: operation
          )
          next
        end

        successful_files << file
        Aidp.log_debug(
          "pr_worktree_manager", "file_changed",
          file: file,
          action: operation
        )
      rescue SecurityError => e
        Aidp.log_error(
          "pr_worktree_manager", "file_path_security_error",
          file: file,
          error: e.message
        )
        failed_files << file
      rescue => e
        Aidp.log_error(
          "pr_worktree_manager", "file_change_error",
          file: file,
          operation: operation,
          error: e.message
        )
        failed_files << file
      end
    end

    # Stage only successfully modified files
    unless successful_files.empty?
      system("git add #{successful_files.map { |f| Shellwords.escape(f) }.join(" ")}")
    end
  end

  Aidp.log_debug(
    "pr_worktree_manager", "worktree_changes_summary",
    pr_number: pr_number,
    successful_files_count: successful_files.size,
    failed_files_count: failed_files.size,
    total_files: changes.fetch(:files, []).size
  )

  {
    success: successful_files.size == changes.fetch(:files, []).size,
    successful_files: successful_files,
    failed_files: failed_files
  }
end

#cleanup_stale_worktrees(days_threshold = 30) ⇒ Object

Cleanup old/stale worktrees (more than 30 days old)



516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/aidp/pr_worktree_manager.rb', line 516

def cleanup_stale_worktrees(days_threshold = 30)
  Aidp.log_debug("pr_worktree_manager", "cleaning_stale_worktrees", threshold_days: days_threshold)

  stale_worktrees = @worktrees.select do |_, details|
    created_at = Time.at(details["created_at"])
    (Time.now - created_at) > (days_threshold * 24 * 60 * 60)
  end

  stale_worktrees.each_key { |pr_number| remove_worktree(pr_number) }

  Aidp.log_debug(
    "pr_worktree_manager", "stale_worktrees_cleaned",
    count: stale_worktrees.size
  )
end

#create_worktree(pr_number, base_branch, head_branch, allow_duplicate: true, max_diff_size: nil) ⇒ Object

Create a new worktree for a PR

Raises:

  • (ArgumentError)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/aidp/pr_worktree_manager.rb', line 94

def create_worktree(pr_number, base_branch, head_branch, allow_duplicate: true, max_diff_size: nil)
  # Log only the required attributes without max_diff_size
  Aidp.log_debug(
    "pr_worktree_manager", "creating_worktree",
    pr_number: pr_number,
    base_branch: base_branch,
    head_branch: head_branch
  )

  # Validate inputs
  raise ArgumentError, "PR number must be a positive integer" unless pr_number.to_i > 0
  raise ArgumentError, "Base branch cannot be empty" if base_branch.nil? || base_branch.empty?
  raise ArgumentError, "Head branch cannot be empty" if head_branch.nil? || head_branch.empty?

  # Advanced max diff size handling
  if max_diff_size
    Aidp.log_debug(
      "pr_worktree_manager", "diff_size_check",
      method: "worktree_based_workflow"
    )
  end

  # Check for existing worktrees if duplicates are not allowed
  if !allow_duplicate
    existing_worktrees = @worktrees.values.select do |details|
      details["base_branch"] == base_branch && details["head_branch"] == head_branch
    end
    return existing_worktrees.first["path"] unless existing_worktrees.empty?
  end

  # Check if a worktree for this PR already exists
  existing_path = find_worktree(pr_number)
  return existing_path if existing_path

  # Generate a unique slug for the worktree
  slug = "pr-#{pr_number}-#{Time.now.to_i}"

  # Determine the base directory for worktrees
  base_worktree_dir = @project_dir || File.expand_path("~/.aidp/worktrees")
  worktree_path = File.join(base_worktree_dir, slug)

  # Ensure base repo path is an actual git repository
  raise "Not a git repository: #{@base_repo_path}" unless File.exist?(File.join(@base_repo_path, ".git"))

  # Create the worktree directory if it doesn't exist
  FileUtils.mkdir_p(base_worktree_dir)

  # Verify base branch exists
  Dir.chdir(@base_repo_path) do
    # List all remote and local branches
    branch_list_output = `git branch -a`.split("\n").map(&:strip)

    # More robust branch existence check with expanded match criteria
    base_branch_exists = branch_list_output.any? do |branch|
      branch.end_with?("/#{base_branch}", "remotes/origin/#{base_branch}") ||
        branch == base_branch ||
        branch == "* #{base_branch}"
    end

    # Enhance branch tracking and fetching
    unless base_branch_exists
      # Try multiple fetch strategies
      fetch_commands = [
        "git fetch origin #{base_branch}:#{base_branch} 2>/dev/null",
        "git fetch origin 2>/dev/null",
        "git fetch --all 2>/dev/null"
      ]

      fetch_commands.each do |fetch_cmd|
        system(fetch_cmd)
        branch_list_output = `git branch -a`.split("\n").map(&:strip)
        base_branch_exists = branch_list_output.any? do |branch|
          branch.end_with?("/#{base_branch}", "remotes/origin/#{base_branch}") ||
            branch == base_branch ||
            branch == "* #{base_branch}"
        end
        break if base_branch_exists
      end
    end

    raise ArgumentError, "Base branch '#{base_branch}' does not exist in the repository" unless base_branch_exists

    # Robust worktree creation with enhanced error handling and logging
    worktree_create_command = "git worktree add #{Shellwords.escape(worktree_path)} -b #{Shellwords.escape(head_branch)} #{Shellwords.escape(base_branch)}"
    unless system(worktree_create_command)
      error_details = {
        pr_number: pr_number,
        base_branch: base_branch,
        head_branch: head_branch,
        command: worktree_create_command
      }
      Aidp.log_error(
        "pr_worktree_manager", "worktree_creation_failed",
        error_details
      )

      # Attempt to diagnose the failure
      git_status = `git status`
      Aidp.log_debug(
        "pr_worktree_manager", "git_status_on_failure",
        status: git_status
      )

      raise "Failed to create worktree for PR #{pr_number}"
    end
  end

  # Extended validation of worktree creation
  unless File.exist?(worktree_path) && File.directory?(worktree_path)
    error_details = {
      pr_number: pr_number,
      base_branch: base_branch,
      head_branch: head_branch,
      expected_path: worktree_path
    }
    Aidp.log_error(
      "pr_worktree_manager", "worktree_path_validation_failed",
      error_details
    )
    raise "Failed to validate worktree path for PR #{pr_number}"
  end

  # Prepare registry entry with additional metadata
  registry_entry = {
    "path" => worktree_path,
    "base_branch" => base_branch,
    "head_branch" => head_branch,
    "created_at" => Time.now.to_i,
    "slug" => slug,
    "source" => "label_workflow"  # Add custom source tracking
  }

  # Conditionally add max_diff_size only if it's provided
  registry_entry["max_diff_size"] = max_diff_size if max_diff_size

  # Store in registry
  @worktrees[pr_number.to_s] = registry_entry
  save_registry

  Aidp.log_debug(
    "pr_worktree_manager", "worktree_created",
    path: worktree_path,
    pr_number: pr_number
  )

  worktree_path
end

#extract_pr_changes(changes_description) ⇒ Object

Enhanced method to extract changes from PR comments/reviews



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/aidp/pr_worktree_manager.rb', line 243

def extract_pr_changes(changes_description)
  Aidp.log_debug(
    "pr_worktree_manager", "extracting_pr_changes",
    description_length: changes_description&.length
  )

  return nil if changes_description.nil? || changes_description.empty?

  # Sophisticated change extraction with multiple parsing strategies
  parsed_changes = {
    files: [],
    operations: [],
    comments: [],
    metadata: {}
  }

  # Advanced change detection patterns
  file_patterns = [
    /(?:modify|update|add|delete)\s+file:\s*([^\n]+)/i,
    /\[(\w+)\]\s*([^\n]+)/,           # GitHub-style change indicators
    /(?:Action:\s*(\w+))\s*File:\s*([^\n]+)/i
  ]

  # Operation mapping
  operation_map = {
    "add" => :create,
    "create" => :create,
    "update" => :modify,
    "modify" => :modify,
    "delete" => :delete,
    "remove" => :delete
  }

  # Parse changes using multiple strategies
  file_patterns.each do |pattern|
    changes_description.scan(pattern) do |match|
      operation = (match.size == 2) ? (match[0].downcase) : nil
      file = (match.size == 2) ? (match[1].strip) : match[0].strip

      parsed_changes[:files] << file
      if operation && operation_map.key?(operation)
        parsed_changes[:operations] << operation_map[operation]
      end
    end
  end

  # Extract potential comments or annotations
  comment_pattern = /(?:comment|note):\s*(.+)/i
  changes_description.scan(comment_pattern) do |match|
    parsed_changes[:comments] << match[0].strip
  end

  # Additional metadata extraction
  parsed_changes[:metadata] = {
    source: "pr_comments",
    timestamp: Time.now.to_i
  }

  Aidp.log_debug(
    "pr_worktree_manager", "pr_changes_extracted",
    files_count: parsed_changes[:files].size,
    operations_count: parsed_changes[:operations].size,
    comments_count: parsed_changes[:comments].size
  )

  parsed_changes
end

#find_worktree(pr_number = nil, branch: nil) ⇒ Object

Find an existing worktree for a given PR number or branch

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/aidp/pr_worktree_manager.rb', line 22

def find_worktree(pr_number = nil, branch: nil)
  Aidp.log_debug(
    "pr_worktree_manager",
    "finding_worktree",
    pr_number: pr_number,
    branch: branch
  )

  # Validate input
  raise ArgumentError, "Must provide either pr_number or branch" if pr_number.nil? && branch.nil?

  # First, check for exact PR match if PR number is provided
  existing_worktree = pr_number ? @worktrees[pr_number.to_s] : nil
  return validate_worktree_path(existing_worktree) if existing_worktree

  # If no PR number, search by branch in all worktrees
  matching_worktrees = @worktrees.values.select do |details|
    # Check for exact branch match or remote branch match with advanced checks
    details.values_at("base_branch", "head_branch").any? do |branch_name|
      branch_name.end_with?("/#{branch}", "remotes/origin/#{branch}") ||
        branch_name == branch
    end
  end

  # If multiple matching worktrees, prefer most recently created
  # Use min_by to efficiently find the most recently created worktree
  matching_worktree = matching_worktrees.min_by { |details| [-details["created_at"].to_i, details["path"]] }

  validate_worktree_path(matching_worktree)
end

#list_worktreesObject

List all active worktrees



509
510
511
512
513
# File 'lib/aidp/pr_worktree_manager.rb', line 509

def list_worktrees
  # Include all known metadata keys from stored details
   = ["path", "base_branch", "head_branch", "created_at", "max_diff_size"]
  @worktrees.transform_values { |details| details.slice(*) }
end

#push_worktree_changes(pr_number, branch: nil) ⇒ Object

Push changes back to the PR branch with enhanced error handling



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/aidp/pr_worktree_manager.rb', line 401

def push_worktree_changes(pr_number, branch: nil)
  Aidp.log_debug(
    "pr_worktree_manager", "pushing_worktree_changes",
    pr_number: pr_number,
    branch: branch
  )

  # Find the worktree and its head branch
  worktree_path = find_worktree(pr_number)
  raise "No worktree found for PR #{pr_number}" unless worktree_path

  # Retrieve the head branch from registry if not provided
  head_branch = branch || @worktrees[pr_number.to_s]["head_branch"]
  raise "No head branch found for PR #{pr_number}" unless head_branch

  # Comprehensive error tracking
  push_result = {
    success: false,
    git_actions: {
      staged_changes: false,
      committed: false,
      pushed: false
    },
    errors: [],
    changed_files: []
  }

  Dir.chdir(worktree_path) do
    # Check staged changes with more robust capture
    staged_changes_output = `git diff --staged --name-only`.strip

    if !staged_changes_output.empty?
      push_result[:git_actions][:staged_changes] = true
      push_result[:changed_files] = staged_changes_output.split("\n")

      # More robust commit command with additional logging
      commit_message = "Changes applied via AIDP request-changes workflow for PR ##{pr_number}"
      commit_command = "git commit -m '#{commit_message}' 2>&1"
      commit_output = `#{commit_command}`.strip

      if $?.success?
        push_result[:git_actions][:committed] = true

        # Enhanced push with verbose tracking
        push_command = "git push origin #{head_branch} 2>&1"
        push_output = `#{push_command}`.strip

        if $?.success?
          push_result[:git_actions][:pushed] = true
          push_result[:success] = true

          Aidp.log_debug(
            "pr_worktree_manager", "changes_pushed_successfully",
            pr_number: pr_number,
            branch: head_branch,
            changed_files_count: push_result[:changed_files].size
          )
        else
          # Detailed push error logging
          push_result[:errors] << "Push failed: #{push_output}"
          Aidp.log_error(
            "pr_worktree_manager", "push_changes_failed",
            pr_number: pr_number,
            branch: head_branch,
            error_details: push_output
          )
        end
      else
        # Detailed commit error logging
        push_result[:errors] << "Commit failed: #{commit_output}"
        Aidp.log_error(
          "pr_worktree_manager", "commit_changes_failed",
          pr_number: pr_number,
          branch: head_branch,
          error_details: commit_output
        )
      end
    else
      # No changes to commit
      push_result[:success] = true
      Aidp.log_debug(
        "pr_worktree_manager", "no_changes_to_push",
        pr_number: pr_number
      )
    end
  end

  push_result
end

#remove_worktree(pr_number) ⇒ Object

Remove a specific worktree



492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/aidp/pr_worktree_manager.rb', line 492

def remove_worktree(pr_number)
  Aidp.log_debug("pr_worktree_manager", "removing_worktree", pr_number: pr_number)

  existing_worktree = @worktrees[pr_number.to_s]
  return false unless existing_worktree

  # Remove git worktree
  system("git worktree remove #{existing_worktree["path"]}") if File.exist?(existing_worktree["path"])

  # Remove from registry and save
  @worktrees.delete(pr_number.to_s)
  save_registry

  true
end

#valid_worktree_repository?(worktree_path) ⇒ Boolean

Verify the integrity of the git worktree repository

Returns:

  • (Boolean)


81
82
83
84
85
86
87
88
89
90
91
# File 'lib/aidp/pr_worktree_manager.rb', line 81

def valid_worktree_repository?(worktree_path)
  return false unless File.directory?(worktree_path)

  # Check for .git directory or .git file (for submodules)
  git_dir = File.join(worktree_path, ".git")
  return false unless File.exist?(git_dir) || File.file?(git_dir)

  true
rescue
  false
end

#validate_worktree_path(worktree_details) ⇒ Object

Helper method to validate worktree path and provide consistent logging



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/aidp/pr_worktree_manager.rb', line 54

def validate_worktree_path(worktree_details)
  return nil unless worktree_details

  # Validate the worktree's integrity
  if File.exist?(worktree_details["path"])
    # Check if the worktree has the correct git repository
    if valid_worktree_repository?(worktree_details["path"])
      Aidp.log_debug("pr_worktree_manager", "found_existing_worktree",
        path: worktree_details["path"],
        base_branch: worktree_details["base_branch"],
        head_branch: worktree_details["head_branch"])
      return worktree_details["path"]
    else
      Aidp.log_warn("pr_worktree_manager", "corrupted_worktree",
        pr_number: worktree_details["pr_number"] || "unknown",
        path: worktree_details["path"])
    end
  else
    Aidp.log_warn("pr_worktree_manager", "worktree_path_missing",
      pr_number: worktree_details["pr_number"] || "unknown",
      expected_path: worktree_details["path"])
  end

  nil
end