Coverage for little_loops / cli / parallel.py: 100%
54 statements
« prev ^ index » next coverage.py v7.12.0, created at 2026-03-04 01:17 -0600
« prev ^ index » next coverage.py v7.12.0, created at 2026-03-04 01:17 -0600
1"""ll-parallel: Process issues concurrently using isolated git worktrees."""
3from __future__ import annotations
5import argparse
6import subprocess
7from pathlib import Path
9from little_loops.cli_args import (
10 add_dry_run_arg,
11 add_max_issues_arg,
12 add_only_arg,
13 add_quiet_arg,
14 add_resume_arg,
15 add_skip_arg,
16 add_timeout_arg,
17 add_type_arg,
18 parse_issue_ids,
19 parse_issue_types,
20)
21from little_loops.config import BRConfig
22from little_loops.logger import Logger
25def main_parallel() -> int:
26 """Entry point for ll-parallel command.
28 Process issues concurrently using isolated git worktrees.
30 Returns:
31 Exit code (0 = success)
32 """
33 parser = argparse.ArgumentParser(
34 description="Process issues concurrently using isolated git worktrees",
35 formatter_class=argparse.RawDescriptionHelpFormatter,
36 epilog="""
37Examples:
38 %(prog)s # Process with default workers
39 %(prog)s --workers 3 # Use 3 parallel workers
40 %(prog)s --dry-run # Preview what would be processed
41 %(prog)s --priority P1,P2 # Only process P1 and P2 issues
42 %(prog)s --cleanup # Clean up worktrees and exit
43 %(prog)s --stream-output # Stream Claude CLI output in real-time
44 %(prog)s --only BUG-001,BUG-002 # Process only specific issues
45 %(prog)s --skip BUG-003 # Skip specific issues
46 %(prog)s --type BUG # Process only bugs
47 %(prog)s --type BUG,ENH # Process bugs and enhancements
48""",
49 )
51 # Parallel-specific arguments (--workers, not --max-workers)
52 parser.add_argument(
53 "--workers",
54 "-w",
55 type=int,
56 default=None,
57 help="Number of parallel workers (default: from config or 2)",
58 )
59 parser.add_argument(
60 "--priority",
61 "-p",
62 type=str,
63 default=None,
64 help="Comma-separated priorities to process (default: all)",
65 )
66 parser.add_argument(
67 "--worktree-base",
68 type=Path,
69 default=None,
70 help="Base directory for git worktrees",
71 )
72 parser.add_argument(
73 "--cleanup",
74 "-c",
75 action="store_true",
76 help="Clean up all worktrees and exit",
77 )
78 parser.add_argument(
79 "--merge-pending",
80 action="store_true",
81 help="Attempt to merge pending work from previous interrupted runs",
82 )
83 parser.add_argument(
84 "--clean-start",
85 action="store_true",
86 help="Remove all worktrees and start fresh (skip pending work check)",
87 )
88 parser.add_argument(
89 "--ignore-pending",
90 action="store_true",
91 help="Report pending work but continue without merging",
92 )
93 parser.add_argument(
94 "--stream-output",
95 action="store_true",
96 help="Stream Claude CLI subprocess output to console",
97 )
98 parser.add_argument(
99 "--show-model",
100 action="store_true",
101 help="Make API call to verify and display model on worktree setup",
102 )
103 parser.add_argument(
104 "--overlap-detection",
105 action="store_true",
106 help="Enable pre-flight overlap detection to reduce merge conflicts (ENH-143)",
107 )
108 parser.add_argument(
109 "--warn-only",
110 action="store_true",
111 help="With --overlap-detection, warn about overlaps instead of serializing",
112 )
114 # Add common arguments from shared module
115 add_dry_run_arg(parser)
116 add_resume_arg(parser)
117 add_timeout_arg(parser)
118 add_quiet_arg(parser)
119 add_only_arg(parser)
120 add_skip_arg(parser)
121 add_type_arg(parser)
123 # Add max-issues and config individually (different help text needed)
124 add_max_issues_arg(parser)
125 parser.add_argument(
126 "--config",
127 type=Path,
128 default=None,
129 help="Path to project root",
130 )
132 args = parser.parse_args()
134 project_root = args.config or Path.cwd()
135 config = BRConfig(project_root)
137 logger = Logger(verbose=not args.quiet)
139 # Handle cleanup mode
140 if args.cleanup:
141 from little_loops.parallel import WorkerPool
143 parallel_config = config.create_parallel_config()
144 pool = WorkerPool(parallel_config, config, logger, project_root)
145 pool.cleanup_all_worktrees()
146 logger.success("Cleanup complete")
147 return 0
149 # Build priority filter
150 priority_filter = (
151 [p.strip().upper() for p in args.priority.split(",")] if args.priority else None
152 )
154 # Parse issue ID filters
155 only_ids = parse_issue_ids(args.only)
156 skip_ids = parse_issue_ids(args.skip)
157 type_prefixes = parse_issue_types(args.type)
159 # Detect current branch for rebase/merge operations (BUG-439)
160 _branch_result = subprocess.run(
161 ["git", "rev-parse", "--abbrev-ref", "HEAD"],
162 capture_output=True,
163 text=True,
164 cwd=project_root,
165 )
166 _base_branch = _branch_result.stdout.strip() if _branch_result.returncode == 0 else "main"
168 # Create parallel config with CLI overrides
169 parallel_config = config.create_parallel_config(
170 max_workers=args.workers,
171 priority_filter=priority_filter,
172 max_issues=args.max_issues,
173 dry_run=args.dry_run,
174 timeout_seconds=args.timeout,
175 stream_output=args.stream_output if args.stream_output else None,
176 show_model=args.show_model if args.show_model else None,
177 only_ids=only_ids,
178 skip_ids=skip_ids,
179 type_prefixes=type_prefixes,
180 merge_pending=args.merge_pending,
181 clean_start=args.clean_start,
182 ignore_pending=args.ignore_pending,
183 overlap_detection=args.overlap_detection,
184 serialize_overlapping=not args.warn_only,
185 base_branch=_base_branch,
186 )
188 # Delete state file if not resuming
189 if not args.resume:
190 state_file = config.get_parallel_state_file()
191 if state_file.exists():
192 state_file.unlink()
194 # Create and run orchestrator
195 from little_loops.parallel import ParallelOrchestrator
197 orchestrator = ParallelOrchestrator(
198 parallel_config=parallel_config,
199 br_config=config,
200 repo_path=project_root,
201 verbose=not args.quiet,
202 )
204 return orchestrator.run()