Coverage for little_loops / parallel / merge_coordinator.py: 83%

445 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2026-03-04 01:17 -0600

1"""Merge coordinator for sequential integration of parallel worker changes. 

2 

3Handles merging completed worker branches back to main with conflict detection 

4and automatic retry capability. 

5""" 

6 

7from __future__ import annotations 

8 

9import shutil 

10import subprocess 

11import threading 

12import time 

13from pathlib import Path 

14from queue import Empty, Queue 

15from typing import TYPE_CHECKING 

16 

17from little_loops.parallel.git_lock import GitLock 

18from little_loops.parallel.types import ( 

19 MergeRequest, 

20 MergeStatus, 

21 ParallelConfig, 

22 WorkerResult, 

23) 

24 

25if TYPE_CHECKING: 

26 from little_loops.logger import Logger 

27 

28 

29class MergeCoordinator: 

30 """Sequential merge queue with conflict handling. 

31 

32 Processes merge requests one at a time to avoid conflicts. Supports 

33 automatic rebase and retry on merge failures. Handles uncommitted local 

34 changes by stashing them before merge operations. 

35 

36 Example: 

37 >>> coordinator = MergeCoordinator(config, logger, repo_path) 

38 >>> coordinator.start() 

39 >>> coordinator.queue_merge(worker_result) 

40 >>> # ... later ... 

41 >>> coordinator.shutdown() 

42 """ 

43 

44 def __init__( 

45 self, 

46 config: ParallelConfig, 

47 logger: Logger, 

48 repo_path: Path | None = None, 

49 git_lock: GitLock | None = None, 

50 ) -> None: 

51 """Initialize the merge coordinator. 

52 

53 Args: 

54 config: Parallel processing configuration 

55 logger: Logger for merge output 

56 repo_path: Path to the git repository (default: current directory) 

57 git_lock: Shared lock for git operations (created if not provided) 

58 """ 

59 self.config = config 

60 self.logger = logger 

61 self.repo_path = repo_path or Path.cwd() 

62 self._git_lock = git_lock or GitLock(logger) 

63 self._queue: Queue[MergeRequest] = Queue() 

64 self._thread: threading.Thread | None = None 

65 self._shutdown_event = threading.Event() 

66 self._merged: list[str] = [] 

67 self._failed: dict[str, str] = {} 

68 self._lock = threading.Lock() 

69 self._stash_active = False # Track if we have an active stash 

70 self._consecutive_failures = 0 # Circuit breaker counter 

71 self._paused = False # Set when circuit breaker trips 

72 self._assume_unchanged_active = False # Track if state file is marked assume-unchanged 

73 self._stash_pop_failures: dict[str, str] = {} # issue_id -> failure message 

74 self._current_issue_id: str | None = ( 

75 None # Track current issue for stash failure attribution 

76 ) 

77 self._problematic_commits: set[str] = ( 

78 set() 

79 ) # Track commits causing repeated rebase conflicts 

80 

81 def start(self) -> None: 

82 """Start the merge coordinator background thread.""" 

83 if self._thread is not None and self._thread.is_alive(): 

84 return 

85 

86 self._shutdown_event.clear() 

87 self._thread = threading.Thread( 

88 target=self._merge_loop, 

89 name="merge-coordinator", 

90 daemon=True, 

91 ) 

92 self._thread.start() 

93 self.logger.info("Merge coordinator started") 

94 

95 def shutdown(self, wait: bool = True, timeout: float = 30.0) -> None: 

96 """Shutdown the merge coordinator. 

97 

98 Args: 

99 wait: Whether to wait for pending merges to complete 

100 timeout: Maximum time to wait for shutdown 

101 """ 

102 if self._thread is None: 

103 return 

104 

105 self._shutdown_event.set() 

106 

107 if wait and self._thread.is_alive(): 

108 self._thread.join(timeout=timeout) 

109 

110 self._thread = None 

111 self.logger.info("Merge coordinator stopped") 

112 

113 def queue_merge(self, worker_result: WorkerResult) -> None: 

114 """Queue a worker result for merging. 

115 

116 Args: 

117 worker_result: Result from a completed worker 

118 """ 

119 request = MergeRequest(worker_result=worker_result) 

120 self._queue.put(request) 

121 self.logger.info( 

122 f"Queued merge for {worker_result.issue_id} (branch: {worker_result.branch_name})" 

123 ) 

124 

125 def _stash_local_changes(self) -> bool: 

126 """Stash any uncommitted tracked changes in the main repo. 

127 

128 Only stashes tracked file modifications. Untracked files are not stashed 

129 because git stash pathspec exclusions don't work reliably with -u flag. 

130 Untracked file conflicts during merge are handled by _handle_untracked_conflict. 

131 

132 The following are explicitly excluded from stashing: 

133 1. State file - managed by orchestrator and continuously updated 

134 2. Lifecycle file moves - issue files being moved to completed/ directory 

135 3. Files in completed directory - lifecycle-managed files 

136 4. Claude Code context state file - managed by Claude Code externally 

137 

138 These exclusions prevent stash pop conflicts after merge, since the merge 

139 may change HEAD and create conflicts with stashed rename operations. 

140 

141 Returns: 

142 True if changes were stashed, False if working tree was clean 

143 """ 

144 state_file_path = Path(self.config.state_file) 

145 state_file_str = str(state_file_path) 

146 state_file_name = state_file_path.name 

147 

148 # Check if there are any tracked changes to stash. 

149 # We only look at tracked files (exclude untracked with grep -v '??') 

150 # since we can only reliably stash tracked changes. 

151 status_result = self._git_lock.run( 

152 ["status", "--porcelain"], 

153 cwd=self.repo_path, 

154 timeout=30, 

155 ) 

156 

157 # Filter to only tracked changes (lines not starting with ??) 

158 # and exclude orchestrator-managed files to prevent stash pop conflicts 

159 tracked_changes = [] 

160 files_to_stash = [] 

161 # Note: Don't use .strip() on the full output - it removes leading spaces 

162 # from the first line which are significant in git status porcelain format 

163 for line in status_result.stdout.splitlines(): 

164 if not line or line.startswith("??"): 

165 continue 

166 # Skip lifecycle file moves (issue files moved to completed/) 

167 # These are managed by orchestrator and cause stash pop conflicts 

168 if self._is_lifecycle_file_move(line): 

169 self.logger.debug(f"Skipping lifecycle file move from stash: {line}") 

170 continue 

171 # Extract file path from porcelain format (XY filename or XY -> filename for renames) 

172 # Format: XY filename or XY old -> new (XY is exactly 2 chars + 1 space) 

173 file_path = line[3:].split(" -> ")[-1].strip() 

174 if file_path == state_file_str or file_path.endswith(state_file_name): 

175 continue # Skip state file - orchestrator manages it independently 

176 # Skip files in completed/deferred directory - these are lifecycle-managed 

177 # Handle both .issues/completed/ (with dot) and issues/completed/ (without dot) 

178 if ( 

179 ".issues/completed/" in file_path 

180 or file_path.startswith(".issues/completed/") 

181 or "issues/completed/" in file_path 

182 or file_path.startswith("issues/completed/") 

183 or ".issues/deferred/" in file_path 

184 or file_path.startswith(".issues/deferred/") 

185 or "issues/deferred/" in file_path 

186 or file_path.startswith("issues/deferred/") 

187 ): 

188 self.logger.debug( 

189 f"Skipping completed/deferred directory file from stash: {file_path}" 

190 ) 

191 continue 

192 # Skip Claude Code context state file - managed externally 

193 if file_path.endswith("ll-context-state.json"): 

194 self.logger.debug(f"Skipping Claude context state file from stash: {file_path}") 

195 continue 

196 tracked_changes.append(line) 

197 files_to_stash.append(file_path) 

198 

199 if not files_to_stash: 

200 return False # No tracked changes to stash (excluding state file) 

201 

202 # Log files to be stashed for debugging 

203 self.logger.debug(f"Tracked files to stash: {tracked_changes[:10]}") 

204 

205 # Stash only specific files, explicitly excluding the state file. 

206 # Using explicit file list avoids race conditions where the orchestrator 

207 # modifies the state file between a checkout and stash-all operation. 

208 # Note: gitignored files are never stashed anyway. 

209 stash_result = self._git_lock.run( 

210 [ 

211 "stash", 

212 "push", 

213 "-m", 

214 "ll-parallel: auto-stash before merge", 

215 "--", 

216 *files_to_stash, 

217 ], 

218 cwd=self.repo_path, 

219 timeout=30, 

220 ) 

221 

222 if stash_result.returncode == 0: 

223 self._stash_active = True 

224 self.logger.info("Stashed local changes before merge") 

225 return True 

226 

227 self.logger.error(f"Failed to stash local changes: {stash_result.stderr}") 

228 return False 

229 

230 def _pop_stash(self) -> bool: 

231 """Restore stashed changes if any were stashed. 

232 

233 Important: This method preserves the merge even if stash pop fails. 

234 We never reset --hard HEAD here because that would undo a successful merge. 

235 

236 Returns: 

237 True if stash was successfully popped or no stash was active, 

238 False if pop failed (stash is left for manual recovery). 

239 """ 

240 if not self._stash_active: 

241 return True 

242 

243 pop_result = self._git_lock.run( 

244 ["stash", "pop"], 

245 cwd=self.repo_path, 

246 timeout=30, 

247 ) 

248 

249 if pop_result.returncode != 0: 

250 self.logger.warning(f"Failed to pop stash: {pop_result.stderr.strip()}") 

251 

252 # Check if it's a conflict issue - in that case, stash pop may have 

253 # partially applied. We need to clean up the index but preserve the merge. 

254 status_result = self._git_lock.run( 

255 ["status", "--porcelain"], 

256 cwd=self.repo_path, 

257 timeout=30, 

258 ) 

259 

260 # Check for unmerged entries from the stash pop attempt 

261 unmerged_prefixes = ("UU", "AA", "DD", "AU", "UA", "DU", "UD") 

262 has_unmerged = any( 

263 line[:2] in unmerged_prefixes 

264 for line in status_result.stdout.splitlines() 

265 if len(line) >= 2 

266 ) 

267 

268 if has_unmerged: 

269 # Clean up the conflicted stash pop without affecting the merge 

270 # Use checkout to restore conflicted files to their post-merge state 

271 self._git_lock.run( 

272 ["checkout", "--theirs", "."], 

273 cwd=self.repo_path, 

274 timeout=30, 

275 ) 

276 self._git_lock.run( 

277 ["reset", "HEAD"], 

278 cwd=self.repo_path, 

279 timeout=30, 

280 ) 

281 self.logger.info("Cleaned up conflicted stash pop, merge preserved") 

282 

283 # Leave the stash intact for manual recovery 

284 self._stash_active = False 

285 

286 # Record this failure for reporting in final summary 

287 if self._current_issue_id: 

288 with self._lock: 

289 self._stash_pop_failures[self._current_issue_id] = ( 

290 "Local changes could not be restored after merge. " 

291 "Run 'git stash list' and 'git stash pop' to recover manually." 

292 ) 

293 

294 self.logger.warning( 

295 "Stash could not be restored - your changes are saved in 'git stash list'. " 

296 "Run 'git stash show' to view and 'git stash pop' to retry manually." 

297 ) 

298 return False 

299 

300 self._stash_active = False 

301 self.logger.info("Restored stashed local changes") 

302 return True 

303 

304 def _mark_state_file_assume_unchanged(self) -> bool: 

305 """Mark the state file as assume-unchanged to prevent git from seeing modifications. 

306 

307 This allows git pull --rebase to proceed even when the state file is modified, 

308 since the orchestrator continuously updates it during processing. 

309 

310 Returns: 

311 True if successfully marked, False otherwise 

312 """ 

313 state_file = str(self.config.state_file) 

314 

315 # Check if file exists and is tracked 

316 ls_files = self._git_lock.run( 

317 ["ls-files", state_file], 

318 cwd=self.repo_path, 

319 timeout=10, 

320 ) 

321 

322 if not ls_files.stdout.strip(): 

323 # File not tracked, nothing to do 

324 return True 

325 

326 result = self._git_lock.run( 

327 ["update-index", "--assume-unchanged", state_file], 

328 cwd=self.repo_path, 

329 timeout=10, 

330 ) 

331 

332 if result.returncode == 0: 

333 self._assume_unchanged_active = True 

334 self.logger.debug(f"Marked {state_file} as assume-unchanged") 

335 return True 

336 

337 self.logger.warning(f"Failed to mark state file assume-unchanged: {result.stderr}") 

338 return False 

339 

340 def _restore_state_file_tracking(self) -> bool: 

341 """Restore normal tracking for the state file. 

342 

343 Returns: 

344 True if successfully restored, False otherwise 

345 """ 

346 if not self._assume_unchanged_active: 

347 return True 

348 

349 state_file = str(self.config.state_file) 

350 

351 result = self._git_lock.run( 

352 ["update-index", "--no-assume-unchanged", state_file], 

353 cwd=self.repo_path, 

354 timeout=10, 

355 ) 

356 

357 self._assume_unchanged_active = False 

358 

359 if result.returncode != 0: 

360 self.logger.warning(f"Failed to restore state file tracking: {result.stderr}") 

361 return False 

362 

363 self.logger.debug(f"Restored tracking for {state_file}") 

364 return True 

365 

366 def _is_lifecycle_file_move(self, porcelain_line: str) -> bool: 

367 """Check if a porcelain status line represents a lifecycle file move. 

368 

369 Lifecycle file moves are issue files being moved to completed/ directory. 

370 These are managed by the orchestrator and should not be stashed, as they 

371 will conflict with the merge when popping. 

372 

373 Args: 

374 porcelain_line: A line from `git status --porcelain` output 

375 

376 Returns: 

377 True if this is a lifecycle file move that should be excluded from stash 

378 """ 

379 # Rename entries have format: R old_path -> new_path 

380 if not porcelain_line.startswith("R"): 

381 return False 

382 

383 # Check if it's a move to completed/ directory 

384 if " -> " not in porcelain_line: 

385 return False 

386 

387 # Extract destination path (after " -> ") 

388 parts = porcelain_line[3:].split(" -> ") 

389 if len(parts) != 2: 

390 return False 

391 

392 dest_path = parts[1].strip() 

393 

394 # Check if destination is in completed or deferred directory (with or without dot prefix) 

395 return ( 

396 ".issues/completed/" in dest_path 

397 or dest_path.startswith(".issues/completed/") 

398 or "issues/completed/" in dest_path 

399 or dest_path.startswith("issues/completed/") 

400 or ".issues/deferred/" in dest_path 

401 or dest_path.startswith(".issues/deferred/") 

402 or "issues/deferred/" in dest_path 

403 or dest_path.startswith("issues/deferred/") 

404 ) 

405 

406 def _commit_pending_lifecycle_moves(self) -> bool: 

407 """Commit any uncommitted lifecycle file moves. 

408 

409 Lifecycle file moves (issue files moved to completed/) are excluded from 

410 stashing to prevent stash pop conflicts. However, if they remain uncommitted 

411 when a merge starts, they will block the merge. This method commits any such 

412 pending moves before the merge proceeds. 

413 

414 Returns: 

415 True if any lifecycle moves were committed or none existed, 

416 False if commit failed 

417 """ 

418 # Check for lifecycle file moves in git status 

419 status_result = self._git_lock.run( 

420 ["status", "--porcelain"], 

421 cwd=self.repo_path, 

422 timeout=30, 

423 ) 

424 

425 lifecycle_moves = [] 

426 for line in status_result.stdout.splitlines(): 

427 if self._is_lifecycle_file_move(line): 

428 lifecycle_moves.append(line) 

429 

430 if not lifecycle_moves: 

431 return True 

432 

433 self.logger.info( 

434 f"Found {len(lifecycle_moves)} uncommitted lifecycle file move(s), committing..." 

435 ) 

436 

437 # Stage all changes (lifecycle moves should already be staged from git mv, 

438 # but add -A ensures any associated content changes are included) 

439 self._git_lock.run( 

440 ["add", "-A"], 

441 cwd=self.repo_path, 

442 timeout=30, 

443 ) 

444 

445 # Commit the lifecycle moves 

446 commit_result = self._git_lock.run( 

447 [ 

448 "commit", 

449 "-m", 

450 "chore(issues): commit pending lifecycle file moves\n\n" 

451 "Auto-committed before merge to prevent conflicts.", 

452 ], 

453 cwd=self.repo_path, 

454 timeout=30, 

455 ) 

456 

457 if commit_result.returncode != 0: 

458 if "nothing to commit" in commit_result.stdout.lower(): 

459 # This shouldn't happen since we detected moves, but handle gracefully 

460 self.logger.debug("No changes to commit despite detecting lifecycle moves") 

461 return True 

462 self.logger.error(f"Failed to commit lifecycle moves: {commit_result.stderr}") 

463 return False 

464 

465 self.logger.info("Committed pending lifecycle file moves") 

466 return True 

467 

468 def _is_local_changes_error(self, error_output: str) -> bool: 

469 """Check if the error is due to uncommitted local changes. 

470 

471 Args: 

472 error_output: The stderr/stdout from the failed git command 

473 

474 Returns: 

475 True if the error indicates local changes would be overwritten 

476 """ 

477 indicators = [ 

478 "Your local changes to the following files would be overwritten", 

479 "Please commit your changes or stash them before you merge", 

480 "error: cannot pull with rebase: You have unstaged changes", 

481 ] 

482 return any(indicator in error_output for indicator in indicators) 

483 

484 def _is_untracked_files_error(self, error_output: str) -> bool: 

485 """Check if the error is due to untracked files blocking merge. 

486 

487 Args: 

488 error_output: The stderr/stdout from the failed git command 

489 

490 Returns: 

491 True if the error indicates untracked files would be overwritten 

492 """ 

493 indicators = [ 

494 "untracked working tree files would be overwritten by merge", 

495 "Please move or remove them before you merge", 

496 ] 

497 return any(indicator in error_output for indicator in indicators) 

498 

499 def _is_index_error(self, error_output: str) -> bool: 

500 """Check if the error is due to a corrupted git index. 

501 

502 Args: 

503 error_output: The stderr/stdout from the failed git command 

504 

505 Returns: 

506 True if the error indicates index problems 

507 """ 

508 indicators = [ 

509 "you need to resolve your current index first", 

510 "fatal: cannot do a partial commit during a merge", 

511 "error: you have not concluded your merge", 

512 ] 

513 return any(indicator in error_output for indicator in indicators) 

514 

515 def _is_rebase_in_progress(self) -> bool: 

516 """Check if a rebase is currently in progress. 

517 

518 Returns: 

519 True if rebase is in progress (rebase-merge or rebase-apply exists) 

520 """ 

521 rebase_merge = self.repo_path / ".git" / "rebase-merge" 

522 rebase_apply = self.repo_path / ".git" / "rebase-apply" 

523 return rebase_merge.exists() or rebase_apply.exists() 

524 

525 def _abort_rebase_if_in_progress(self) -> bool: 

526 """Abort any in-progress rebase operation. 

527 

528 Returns: 

529 True if rebase was aborted or none was in progress, 

530 False if abort failed 

531 """ 

532 if not self._is_rebase_in_progress(): 

533 return True 

534 

535 self.logger.warning("Detected rebase in progress, aborting...") 

536 abort_result = self._git_lock.run( 

537 ["rebase", "--abort"], 

538 cwd=self.repo_path, 

539 timeout=30, 

540 ) 

541 

542 if abort_result.returncode != 0: 

543 self.logger.error(f"Failed to abort rebase: {abort_result.stderr}") 

544 # Force hard reset as last resort 

545 return self._attempt_hard_reset() 

546 

547 self.logger.info("Aborted incomplete rebase from pull") 

548 return True 

549 

550 def _is_unmerged_files_error(self, error_output: str) -> bool: 

551 """Check if the error is due to pre-existing unmerged files. 

552 

553 Args: 

554 error_output: The stderr/stdout from the failed git command 

555 

556 Returns: 

557 True if the error indicates unmerged files blocking the operation 

558 """ 

559 indicators = [ 

560 "you have unmerged files", 

561 "Merging is not possible because you have unmerged files", 

562 "fix conflicts and then commit the result", 

563 ] 

564 return any(indicator in error_output for indicator in indicators) 

565 

566 def _detect_conflict_commit(self, error_output: str) -> str | None: 

567 """Extract commit hash from rebase conflict output. 

568 

569 Looks for patterns like: 

570 - "dropping ae3b85ec1cac501058f6e5da362be37be1c99801 feat(ai): add stall detectio" 

571 

572 Args: 

573 error_output: The stderr/stdout from the failed git pull --rebase 

574 

575 Returns: 

576 The 40-character commit hash if found, None otherwise 

577 """ 

578 import re 

579 

580 # Pattern: "dropping <40-char-hash>" followed by space and message 

581 # Match only full 40-char hashes to avoid false positives 

582 match = re.search(r"dropping\s+([a-f0-9]{40})\s+", error_output, re.IGNORECASE) 

583 return match.group(1) if match else None 

584 

585 def _check_and_recover_index(self) -> bool: 

586 """Check git index health and attempt recovery if needed. 

587 

588 Returns: 

589 True if index is healthy or was recovered, False if unrecoverable 

590 """ 

591 # Check if we're in the middle of a merge 

592 merge_head = self.repo_path / ".git" / "MERGE_HEAD" 

593 if merge_head.exists(): 

594 self.logger.warning("Detected incomplete merge, aborting...") 

595 abort_result = self._git_lock.run( 

596 ["merge", "--abort"], 

597 cwd=self.repo_path, 

598 timeout=30, 

599 ) 

600 if abort_result.returncode != 0: 

601 self.logger.error(f"Failed to abort merge: {abort_result.stderr}") 

602 return False 

603 self.logger.info("Aborted incomplete merge") 

604 

605 # Check if we're in the middle of a rebase 

606 rebase_dir = self.repo_path / ".git" / "rebase-merge" 

607 rebase_apply = self.repo_path / ".git" / "rebase-apply" 

608 if rebase_dir.exists() or rebase_apply.exists(): 

609 self.logger.warning("Detected incomplete rebase, aborting...") 

610 abort_result = self._git_lock.run( 

611 ["rebase", "--abort"], 

612 cwd=self.repo_path, 

613 timeout=30, 

614 ) 

615 if abort_result.returncode != 0: 

616 self.logger.error(f"Failed to abort rebase: {abort_result.stderr}") 

617 return False 

618 self.logger.info("Aborted incomplete rebase") 

619 # Force reset after rebase abort - the abort can leave index in dirty state 

620 # This is defensive since unmerged file detection below may not trigger 

621 if not self._attempt_hard_reset(): 

622 return False 

623 

624 # Check for unmerged files in the index (UU, AA, DD, AU, UA, DU, UD prefixes) 

625 # These can persist even after merge --abort in some edge cases 

626 status_result = self._git_lock.run( 

627 ["status", "--porcelain"], 

628 cwd=self.repo_path, 

629 timeout=30, 

630 ) 

631 

632 if status_result.returncode != 0: 

633 self.logger.error(f"git status failed: {status_result.stderr}") 

634 return self._attempt_hard_reset() 

635 

636 # Debug logging to diagnose unmerged detection issues 

637 if status_result.stdout.strip(): 

638 self.logger.debug(f"Git status output: {status_result.stdout[:500]}") 

639 

640 # Check for unmerged entries (first two chars indicate index/worktree status) 

641 # Unmerged states: UU (both modified), AA (both added), DD (both deleted), 

642 # AU/UA (added by us/them), DU/UD (deleted by us/them) 

643 unmerged_prefixes = ("UU", "AA", "DD", "AU", "UA", "DU", "UD") 

644 has_unmerged = any( 

645 line[:2] in unmerged_prefixes 

646 for line in status_result.stdout.splitlines() 

647 if len(line) >= 2 

648 ) 

649 

650 if has_unmerged: 

651 self.logger.warning("Detected unmerged files in index, resetting...") 

652 if not self._attempt_hard_reset(): 

653 return False 

654 self.logger.info("Cleared unmerged files from index") 

655 

656 # Final safety check - if MERGE_HEAD still exists, force reset 

657 # This catches edge cases where abort succeeded but state is still dirty 

658 if merge_head.exists(): 

659 self.logger.warning("MERGE_HEAD persists after recovery attempts, forcing reset") 

660 if not self._attempt_hard_reset(): 

661 return False 

662 

663 return True 

664 

665 def _attempt_hard_reset(self) -> bool: 

666 """Attempt a hard reset to recover from index issues. 

667 

668 Returns: 

669 True if reset succeeded, False otherwise 

670 """ 

671 self.logger.warning("Attempting hard reset to recover...") 

672 reset_result = self._git_lock.run( 

673 ["reset", "--hard", "HEAD"], 

674 cwd=self.repo_path, 

675 timeout=30, 

676 ) 

677 if reset_result.returncode != 0: 

678 self.logger.error("Hard reset failed - manual intervention required") 

679 return False 

680 self.logger.info("Hard reset successful") 

681 return True 

682 

683 def _merge_loop(self) -> None: 

684 """Background thread loop for processing merge requests.""" 

685 while not self._shutdown_event.is_set(): 

686 try: 

687 # Wait for next merge request with timeout 

688 try: 

689 request = self._queue.get(timeout=1.0) 

690 except Empty: 

691 continue 

692 

693 # Process the merge 

694 self._process_merge(request) 

695 

696 except Exception as e: 

697 self.logger.error(f"Merge loop error: {e}") 

698 time.sleep(1.0) 

699 

700 def _process_merge(self, request: MergeRequest) -> None: 

701 """Process a single merge request. 

702 

703 Stashes any uncommitted local changes before attempting the merge, 

704 and restores them afterward (regardless of success/failure). 

705 

706 Args: 

707 request: Merge request to process 

708 """ 

709 result = request.worker_result 

710 self._current_issue_id = result.issue_id 

711 self.logger.info(f"Processing merge for {result.issue_id}") 

712 

713 # Circuit breaker check 

714 if self._paused: 

715 self.logger.warning( 

716 f"Merge coordinator paused due to repeated failures. " 

717 f"Skipping {result.issue_id}. Manual intervention required." 

718 ) 

719 self._handle_failure(request, "Merge coordinator paused - circuit breaker tripped") 

720 return 

721 

722 request.status = MergeStatus.IN_PROGRESS 

723 

724 # Health check: ensure git index is clean before proceeding 

725 if not self._check_and_recover_index(): 

726 self._consecutive_failures += 1 

727 if self._consecutive_failures >= 3: 

728 self._paused = True 

729 self.logger.error( 

730 f"Circuit breaker tripped after {self._consecutive_failures} consecutive failures. " 

731 "Merge coordinator paused. Manual recovery required." 

732 ) 

733 self._handle_failure(request, "Git index recovery failed") 

734 return 

735 

736 # Mark state file as assume-unchanged to prevent pull --rebase conflicts 

737 # The orchestrator continuously updates the state file during processing 

738 self._mark_state_file_assume_unchanged() 

739 

740 # Commit any uncommitted lifecycle file moves before stash/merge 

741 # These are excluded from stash to prevent pop conflicts, so they must 

742 # be committed to avoid blocking the merge (BUG-018 fix) 

743 if not self._commit_pending_lifecycle_moves(): 

744 self._handle_failure(request, "Failed to commit pending lifecycle moves") 

745 return 

746 

747 # Stash any local changes before merge operations 

748 had_local_changes = self._stash_local_changes() 

749 

750 try: 

751 # Ensure we're on base branch in the main repo 

752 base = self.config.base_branch 

753 checkout_result = self._git_lock.run( 

754 ["checkout", base], 

755 cwd=self.repo_path, 

756 timeout=30, 

757 ) 

758 

759 if checkout_result.returncode != 0: 

760 error_output = checkout_result.stderr + checkout_result.stdout 

761 if self._is_local_changes_error(error_output): 

762 # This shouldn't happen since we stashed, but handle it anyway 

763 self.logger.warning( 

764 "Checkout failed due to local changes despite stash attempt" 

765 ) 

766 if self._is_index_error(error_output): 

767 # Try recovery 

768 if self._check_and_recover_index(): 

769 # Retry checkout 

770 checkout_result = self._git_lock.run( 

771 ["checkout", base], 

772 cwd=self.repo_path, 

773 timeout=30, 

774 ) 

775 if checkout_result.returncode == 0: 

776 self.logger.info("Recovered from index error, checkout succeeded") 

777 else: 

778 raise RuntimeError( 

779 f"Failed to checkout {base} after recovery: " 

780 f"{checkout_result.stderr}" 

781 ) 

782 else: 

783 raise RuntimeError(f"Failed to checkout {base}: {checkout_result.stderr}") 

784 else: 

785 raise RuntimeError(f"Failed to checkout {base}: {checkout_result.stderr}") 

786 

787 # Track if merge strategy was used during pull (for conflict handling) 

788 used_merge_strategy = False 

789 

790 # Pull latest changes 

791 pull_result = self._git_lock.run( 

792 ["pull", "--rebase", "origin", base], 

793 cwd=self.repo_path, 

794 timeout=60, 

795 ) 

796 

797 # Handle pull failures 

798 if pull_result.returncode != 0: 

799 error_output = pull_result.stderr + pull_result.stdout 

800 

801 # Check if rebase conflicted - must abort before continuing 

802 if self._is_rebase_in_progress(): 

803 conflict_commit = self._detect_conflict_commit(error_output) 

804 

805 if conflict_commit and conflict_commit in self._problematic_commits: 

806 # Known problematic commit - use merge strategy instead 

807 self.logger.info( 

808 f"Repeated rebase conflict with {conflict_commit[:8]}, " 

809 f"using merge strategy (git pull --no-rebase)" 

810 ) 

811 if not self._abort_rebase_if_in_progress(): 

812 raise RuntimeError("Failed to recover from rebase conflict during pull") 

813 

814 # Attempt merge strategy pull 

815 merge_pull_result = self._git_lock.run( 

816 ["pull", "--no-rebase", "origin", base], 

817 cwd=self.repo_path, 

818 timeout=60, 

819 ) 

820 

821 if merge_pull_result.returncode != 0: 

822 self.logger.warning( 

823 f"Merge strategy pull also failed: {merge_pull_result.stderr[:200]}" 

824 ) 

825 # Continue anyway - merge may still work or fail appropriately 

826 else: 

827 self.logger.info( 

828 f"Merge strategy pull succeeded for {conflict_commit[:8]}" 

829 ) 

830 used_merge_strategy = True 

831 

832 else: 

833 # First time seeing this conflict or couldn't extract commit 

834 if conflict_commit: 

835 self._problematic_commits.add(conflict_commit) 

836 self.logger.warning( 

837 f"New rebase conflict with {conflict_commit[:8]}, " 

838 f"tracking for future merges (will use merge strategy on repeat)" 

839 ) 

840 else: 

841 self.logger.warning( 

842 "Rebase conflict detected (could not extract commit hash), " 

843 "tracking for future merges" 

844 ) 

845 

846 self.logger.warning( 

847 f"Pull --rebase failed with conflicts: {error_output[:200]}" 

848 ) 

849 

850 if not self._abort_rebase_if_in_progress(): 

851 raise RuntimeError("Failed to recover from rebase conflict during pull") 

852 # After aborting rebase, we're back to pre-pull state 

853 # Continue without the pull - merge may still work or conflict 

854 self.logger.info("Continuing without pull after rebase abort") 

855 

856 elif self._is_local_changes_error(error_output): 

857 self.logger.warning( 

858 f"Pull failed due to local changes, attempting re-stash: {error_output[:200]}" 

859 ) 

860 # Re-stash any local changes that appeared during pull 

861 if self._stash_local_changes(): 

862 self.logger.info("Re-stashed local changes after pull conflict") 

863 had_local_changes = True 

864 # For other pull failures, continue - merge will handle or fail 

865 

866 # Safety check: ensure no unmerged files before merge attempt 

867 # This catches edge cases where previous operations left dirty state 

868 if not self._check_and_recover_index(): 

869 raise RuntimeError( 

870 "Git index has unresolved conflicts before merge - recovery failed" 

871 ) 

872 

873 # Attempt merge with no-ff 

874 merge_result = self._git_lock.run( 

875 [ 

876 "merge", 

877 result.branch_name, 

878 "--no-ff", 

879 "-m", 

880 f"feat: parallel merge {result.issue_id}\n\n" 

881 f"Automated merge from parallel issue processing.", 

882 ], 

883 cwd=self.repo_path, 

884 timeout=60, 

885 ) 

886 

887 if merge_result.returncode != 0: 

888 error_output = merge_result.stderr + merge_result.stdout 

889 

890 # Check for local changes error (shouldn't happen after stash) 

891 if self._is_local_changes_error(error_output): 

892 self.logger.warning( 

893 f"Merge blocked by local changes despite stash: {error_output[:200]}" 

894 ) 

895 raise RuntimeError(f"Merge failed due to local changes: {error_output[:200]}") 

896 

897 # Check for untracked files blocking merge 

898 if self._is_untracked_files_error(error_output): 

899 self._handle_untracked_conflict(request, error_output) 

900 return 

901 

902 # Check for merge conflict (including unmerged files from current merge) 

903 # Unmerged files at this point are genuine conflicts from the current merge 

904 # attempt, not leftover state from previous operations (those are cleaned up 

905 # by _check_and_recover_index() at the start of _process_merge) 

906 if self._is_unmerged_files_error(error_output) or "CONFLICT" in error_output: 

907 self._handle_conflict(request, used_merge_strategy) 

908 return 

909 else: 

910 raise RuntimeError(f"Merge failed: {merge_result.stderr}") 

911 

912 # Merge successful 

913 self._finalize_merge(request) 

914 

915 except Exception as e: 

916 self._handle_failure(request, str(e)) 

917 

918 finally: 

919 # Always restore stashed changes 

920 if had_local_changes: 

921 self._pop_stash() 

922 # Always restore state file tracking 

923 self._restore_state_file_tracking() 

924 # Clear current issue tracking 

925 self._current_issue_id = None 

926 

927 def _handle_conflict(self, request: MergeRequest, used_merge_strategy: bool = False) -> None: 

928 """Handle a merge conflict with retry logic. 

929 

930 Args: 

931 request: The merge request that conflicted 

932 used_merge_strategy: If True, merge strategy was used during pull and 

933 rebase retry should be skipped (rebase would fail on same conflicts) 

934 """ 

935 result = request.worker_result 

936 request.retry_count += 1 

937 

938 # Abort the failed merge 

939 self._git_lock.run( 

940 ["merge", "--abort"], 

941 cwd=self.repo_path, 

942 timeout=10, 

943 ) 

944 

945 # Skip rebase retry if merge strategy was used during pull (BUG-079) 

946 # Rebase would fail on the same commits that caused the initial conflict 

947 if used_merge_strategy: 

948 self.logger.warning( 

949 f"Merge conflict for {result.issue_id}, " 

950 f"skipping rebase retry (merge strategy was used during pull)" 

951 ) 

952 self._handle_failure( 

953 request, 

954 "Merge conflict - rebase not attempted (would fail on same conflicts " 

955 "that required merge strategy)", 

956 ) 

957 return 

958 

959 if request.retry_count <= self.config.max_merge_retries: 

960 # Attempt rebase in the worktree 

961 self.logger.warning( 

962 f"Merge conflict for {result.issue_id}, " 

963 f"attempting rebase (retry {request.retry_count}/{self.config.max_merge_retries})" 

964 ) 

965 

966 request.status = MergeStatus.RETRYING 

967 

968 # Check for and stash any unstaged changes in the worktree before rebase 

969 worktree_status = subprocess.run( 

970 ["git", "status", "--porcelain"], 

971 cwd=result.worktree_path, 

972 capture_output=True, 

973 text=True, 

974 timeout=30, 

975 ) 

976 worktree_has_changes = bool(worktree_status.stdout.strip()) 

977 

978 if worktree_has_changes: 

979 self.logger.debug( 

980 f"Stashing worktree changes before rebase: {worktree_status.stdout[:200]}" 

981 ) 

982 stash_result = subprocess.run( 

983 ["git", "stash", "push", "-m", "ll-parallel: auto-stash before rebase"], 

984 cwd=result.worktree_path, 

985 capture_output=True, 

986 text=True, 

987 timeout=30, 

988 ) 

989 if stash_result.returncode != 0: 

990 self.logger.warning(f"Failed to stash worktree changes: {stash_result.stderr}") 

991 

992 # Fetch latest base branch before rebase (BUG-180) 

993 # Use origin/base if fetch succeeds, fall back to base if no remote 

994 base = self.config.base_branch 

995 fetch_result = subprocess.run( 

996 ["git", "fetch", "origin", base], 

997 cwd=result.worktree_path, 

998 capture_output=True, 

999 text=True, 

1000 timeout=60, 

1001 ) 

1002 rebase_target = f"origin/{base}" if fetch_result.returncode == 0 else base 

1003 

1004 # Rebase the branch onto latest base branch (BUG-180) 

1005 rebase_result = subprocess.run( 

1006 ["git", "rebase", rebase_target], 

1007 cwd=result.worktree_path, 

1008 capture_output=True, 

1009 text=True, 

1010 timeout=120, 

1011 ) 

1012 

1013 if rebase_result.returncode == 0: 

1014 # Rebase succeeded, restore stash if we made one, then retry merge 

1015 if worktree_has_changes: 

1016 pop_result = subprocess.run( 

1017 ["git", "stash", "pop"], 

1018 cwd=result.worktree_path, 

1019 capture_output=True, 

1020 text=True, 

1021 timeout=30, 

1022 ) 

1023 if pop_result.returncode != 0: 

1024 self.logger.error( 

1025 f"Stash pop failed for {result.issue_id} after rebase: " 

1026 f"{pop_result.stderr.strip()}" 

1027 ) 

1028 self._handle_failure(request, "Stash pop conflict after rebase") 

1029 return 

1030 self._queue.put(request) 

1031 else: 

1032 # Rebase also failed - abort and restore stash 

1033 subprocess.run( 

1034 ["git", "rebase", "--abort"], 

1035 cwd=result.worktree_path, 

1036 capture_output=True, 

1037 timeout=10, 

1038 ) 

1039 if worktree_has_changes: 

1040 subprocess.run( 

1041 ["git", "stash", "pop"], 

1042 cwd=result.worktree_path, 

1043 capture_output=True, 

1044 timeout=30, 

1045 ) 

1046 self._handle_failure( 

1047 request, 

1048 f"Rebase failed after merge conflict: {rebase_result.stderr}", 

1049 ) 

1050 else: 

1051 self._handle_failure( 

1052 request, 

1053 f"Merge conflict after {request.retry_count} retries", 

1054 ) 

1055 

1056 def _handle_untracked_conflict(self, request: MergeRequest, error_output: str) -> None: 

1057 """Handle untracked files that would be overwritten by merge. 

1058 

1059 Backs up conflicting untracked files and retries the merge. 

1060 

1061 Args: 

1062 request: The merge request that failed 

1063 error_output: Git error message containing file list 

1064 """ 

1065 result = request.worker_result 

1066 request.retry_count += 1 

1067 

1068 if request.retry_count > self.config.max_merge_retries: 

1069 self._handle_failure( 

1070 request, 

1071 f"Untracked file conflict after {request.retry_count} retries", 

1072 ) 

1073 return 

1074 

1075 # Parse conflicting files from error message 

1076 # Format: "error: The following untracked working tree files would be overwritten..." 

1077 # followed by file paths, then "Please move or remove them..." 

1078 conflicting_files = [] 

1079 in_file_list = False 

1080 for line in error_output.splitlines(): 

1081 line = line.strip() 

1082 if "untracked working tree files would be overwritten" in line: 

1083 in_file_list = True 

1084 continue 

1085 if "Please move or remove them" in line: 

1086 in_file_list = False 

1087 continue 

1088 if in_file_list and line and not line.startswith("error:"): 

1089 conflicting_files.append(line) 

1090 

1091 if not conflicting_files: 

1092 self._handle_failure( 

1093 request, 

1094 f"Could not parse conflicting files from: {error_output[:200]}", 

1095 ) 

1096 return 

1097 

1098 # Create backup directory 

1099 backup_dir = self.repo_path / ".ll-backup" / result.issue_id 

1100 backup_dir.mkdir(parents=True, exist_ok=True) 

1101 

1102 # Move conflicting files to backup 

1103 moved_files = [] 

1104 for file_path in conflicting_files: 

1105 src = self.repo_path / file_path 

1106 if src.exists(): 

1107 dst = backup_dir / file_path 

1108 dst.parent.mkdir(parents=True, exist_ok=True) 

1109 shutil.move(str(src), str(dst)) 

1110 moved_files.append(file_path) 

1111 

1112 if moved_files: 

1113 self.logger.info( 

1114 f"Backed up {len(moved_files)} conflicting untracked file(s) to {backup_dir}" 

1115 ) 

1116 

1117 # Retry the merge 

1118 self.logger.warning( 

1119 f"Untracked files conflict for {result.issue_id}, " 

1120 f"retrying after backup (attempt {request.retry_count}/{self.config.max_merge_retries})" 

1121 ) 

1122 request.status = MergeStatus.RETRYING 

1123 self._queue.put(request) 

1124 

1125 def _finalize_merge(self, request: MergeRequest) -> None: 

1126 """Finalize a successful merge. 

1127 

1128 Args: 

1129 request: The completed merge request 

1130 """ 

1131 result = request.worker_result 

1132 request.status = MergeStatus.SUCCESS 

1133 

1134 # Reset circuit breaker on success 

1135 self._consecutive_failures = 0 

1136 

1137 with self._lock: 

1138 self._merged.append(result.issue_id) 

1139 

1140 # Cleanup worktree and branch 

1141 self._cleanup_worktree(result.worktree_path, result.branch_name) 

1142 

1143 self.logger.success(f"Merged {result.issue_id} successfully") 

1144 

1145 def _handle_failure(self, request: MergeRequest, error: str) -> None: 

1146 """Handle a merge failure. 

1147 

1148 Args: 

1149 request: The failed merge request 

1150 error: Error message describing the failure 

1151 """ 

1152 result = request.worker_result 

1153 request.status = MergeStatus.FAILED 

1154 request.error = error 

1155 

1156 with self._lock: 

1157 self._failed[result.issue_id] = error 

1158 

1159 self.logger.error(f"Merge failed for {result.issue_id}: {error}") 

1160 

1161 def _cleanup_worktree(self, worktree_path: Path, branch_name: str) -> None: 

1162 """Clean up a merged worktree and its branch. 

1163 

1164 Args: 

1165 worktree_path: Path to the worktree 

1166 branch_name: Name of the branch to delete 

1167 """ 

1168 if not worktree_path.exists(): 

1169 return 

1170 

1171 # Remove worktree 

1172 self._git_lock.run( 

1173 ["worktree", "remove", "--force", str(worktree_path)], 

1174 cwd=self.repo_path, 

1175 timeout=30, 

1176 ) 

1177 

1178 # Force delete directory if still exists 

1179 if worktree_path.exists(): 

1180 shutil.rmtree(worktree_path, ignore_errors=True) 

1181 

1182 # Delete the branch 

1183 if branch_name.startswith("parallel/"): 

1184 self._git_lock.run( 

1185 ["branch", "-D", branch_name], 

1186 cwd=self.repo_path, 

1187 timeout=10, 

1188 ) 

1189 

1190 @property 

1191 def merged_ids(self) -> list[str]: 

1192 """List of successfully merged issue IDs.""" 

1193 with self._lock: 

1194 return list(self._merged) 

1195 

1196 @property 

1197 def failed_merges(self) -> dict[str, str]: 

1198 """Mapping of failed issue IDs to error messages.""" 

1199 with self._lock: 

1200 return dict(self._failed) 

1201 

1202 @property 

1203 def pending_count(self) -> int: 

1204 """Number of pending merge requests.""" 

1205 return self._queue.qsize() 

1206 

1207 @property 

1208 def stash_pop_failures(self) -> dict[str, str]: 

1209 """Mapping of issue IDs to stash pop failure messages. 

1210 

1211 These represent issues where the merge succeeded but the user's 

1212 local changes could not be automatically restored and need manual 

1213 recovery via 'git stash pop'. 

1214 """ 

1215 with self._lock: 

1216 return dict(self._stash_pop_failures) 

1217 

1218 def wait_for_completion(self, timeout: float | None = None) -> bool: 

1219 """Wait for all pending merges to complete. 

1220 

1221 Waits until both: 

1222 1. The merge queue is empty (no pending requests) 

1223 2. No merge is actively being processed (_current_issue_id is None) 

1224 

1225 Args: 

1226 timeout: Maximum time to wait (None = forever) 

1227 

1228 Returns: 

1229 True if all merges completed, False if timeout 

1230 """ 

1231 start_time = time.time() 

1232 while not self._queue.empty() or self._current_issue_id: 

1233 if timeout and (time.time() - start_time) > timeout: 

1234 return False 

1235 time.sleep(0.5) 

1236 return True