Coverage for little_loops / config / core.py: 68%
140 statements
« prev ^ index » next coverage.py v7.12.0, created at 2026-04-02 13:03 -0500
« prev ^ index » next coverage.py v7.12.0, created at 2026-04-02 13:03 -0500
1"""Core configuration dataclasses and the root BRConfig aggregator.
3ProjectConfig holds project-level settings. BRConfig is the single entry
4point that loads ll-config.json and exposes all domain configs via properties.
5"""
7from __future__ import annotations
9import json
10from dataclasses import dataclass
11from pathlib import Path
12from typing import Any, cast
14from little_loops.config.automation import (
15 AutomationConfig,
16 CommandsConfig,
17 DependencyMappingConfig,
18 ParallelAutomationConfig,
19)
20from little_loops.config.cli import CliConfig, RefineStatusConfig
21from little_loops.config.features import (
22 IssuesConfig,
23 LoopsConfig,
24 ScanConfig,
25 SprintsConfig,
26 SyncConfig,
27)
28from little_loops.parallel.types import ParallelConfig
31@dataclass
32class ProjectConfig:
33 """Project-level configuration."""
35 name: str = ""
36 src_dir: str = "src/"
37 test_dir: str = "tests"
38 test_cmd: str = "pytest"
39 lint_cmd: str = "ruff check ."
40 type_cmd: str | None = "mypy"
41 format_cmd: str | None = "ruff format ."
42 build_cmd: str | None = None
43 run_cmd: str | None = None
45 @classmethod
46 def from_dict(cls, data: dict[str, Any]) -> ProjectConfig:
47 """Create ProjectConfig from dictionary."""
48 return cls(
49 name=data.get("name", ""),
50 src_dir=data.get("src_dir", "src/"),
51 test_dir=data.get("test_dir", "tests"),
52 test_cmd=data.get("test_cmd", "pytest"),
53 lint_cmd=data.get("lint_cmd", "ruff check ."),
54 type_cmd=data.get("type_cmd", "mypy"),
55 format_cmd=data.get("format_cmd", "ruff format ."),
56 build_cmd=data.get("build_cmd"),
57 run_cmd=data.get("run_cmd"),
58 )
61class BRConfig:
62 """Main configuration class for little-loops.
64 Loads configuration from .ll/ll-config.json and merges with defaults.
65 Provides convenient property access to all configuration values.
67 Example:
68 config = BRConfig(Path.cwd())
69 print(config.project.src_dir) # "src/"
70 print(config.issues.base_dir) # ".issues"
71 print(config.get_issue_dir("bugs")) # Path(".issues/bugs")
72 """
74 CONFIG_FILENAME = "ll-config.json"
75 CONFIG_DIR = ".ll"
77 def __init__(self, project_root: Path) -> None:
78 """Initialize configuration from project root.
80 Args:
81 project_root: Path to the project root directory
82 """
83 self.project_root = project_root.resolve()
84 self._raw_config = self._load_config()
85 self._parse_config()
87 def _load_config(self) -> dict[str, Any]:
88 """Load configuration from file."""
89 config_path = self.project_root / self.CONFIG_DIR / self.CONFIG_FILENAME
90 if config_path.exists():
91 with open(config_path, encoding="utf-8") as f:
92 return cast(dict[str, Any], json.load(f))
93 return {}
95 def _parse_config(self) -> None:
96 """Parse raw config into typed dataclasses."""
97 self._project = ProjectConfig.from_dict(self._raw_config.get("project", {}))
98 if not self._project.name:
99 self._project.name = self.project_root.name
101 self._issues = IssuesConfig.from_dict(self._raw_config.get("issues", {}))
102 self._automation = AutomationConfig.from_dict(self._raw_config.get("automation", {}))
103 self._parallel = ParallelAutomationConfig.from_dict(self._raw_config.get("parallel", {}))
104 self._commands = CommandsConfig.from_dict(self._raw_config.get("commands", {}))
105 self._scan = ScanConfig.from_dict(self._raw_config.get("scan", {}))
106 self._sprints = SprintsConfig.from_dict(self._raw_config.get("sprints", {}))
107 self._loops = LoopsConfig.from_dict(self._raw_config.get("loops", {}))
108 self._sync = SyncConfig.from_dict(self._raw_config.get("sync", {}))
109 self._dependency_mapping = DependencyMappingConfig.from_dict(
110 self._raw_config.get("dependency_mapping", {})
111 )
112 self._cli = CliConfig.from_dict(self._raw_config.get("cli", {}))
113 self._refine_status = RefineStatusConfig.from_dict(
114 self._raw_config.get("refine_status", {})
115 )
117 @property
118 def project(self) -> ProjectConfig:
119 """Get project configuration."""
120 return self._project
122 @property
123 def issues(self) -> IssuesConfig:
124 """Get issues configuration."""
125 return self._issues
127 @property
128 def automation(self) -> AutomationConfig:
129 """Get automation configuration."""
130 return self._automation
132 @property
133 def parallel(self) -> ParallelAutomationConfig:
134 """Get parallel automation configuration."""
135 return self._parallel
137 @property
138 def commands(self) -> CommandsConfig:
139 """Get commands configuration."""
140 return self._commands
142 @property
143 def scan(self) -> ScanConfig:
144 """Get scan configuration."""
145 return self._scan
147 @property
148 def sprints(self) -> SprintsConfig:
149 """Get sprints configuration."""
150 return self._sprints
152 @property
153 def loops(self) -> LoopsConfig:
154 """Get loops configuration."""
155 return self._loops
157 @property
158 def sync(self) -> SyncConfig:
159 """Get sync configuration."""
160 return self._sync
162 @property
163 def dependency_mapping(self) -> DependencyMappingConfig:
164 """Get dependency mapping configuration."""
165 return self._dependency_mapping
167 @property
168 def cli(self) -> CliConfig:
169 """Get CLI output configuration."""
170 return self._cli
172 @property
173 def refine_status(self) -> RefineStatusConfig:
174 """Get refine-status display configuration."""
175 return self._refine_status
177 @property
178 def repo_path(self) -> Path:
179 """Get the repository root path."""
180 return self.project_root
182 # Convenience methods for common operations
184 def get_issue_dir(self, category: str) -> Path:
185 """Get the directory path for an issue category.
187 Args:
188 category: Category key (e.g., "bugs", "features")
190 Returns:
191 Path to the issue category directory
192 """
193 if category in self._issues.categories:
194 dir_name = self._issues.categories[category].dir
195 else:
196 dir_name = category
197 return self.project_root / self._issues.base_dir / dir_name
199 def get_completed_dir(self) -> Path:
200 """Get the path to the completed issues directory."""
201 return self.project_root / self._issues.base_dir / self._issues.completed_dir
203 def get_deferred_dir(self) -> Path:
204 """Get the path to the deferred issues directory."""
205 return self.project_root / self._issues.base_dir / self._issues.deferred_dir
207 def get_issue_prefix(self, category: str) -> str:
208 """Get the issue ID prefix for a category.
210 Args:
211 category: Category key (e.g., "bugs", "features")
213 Returns:
214 Issue prefix (e.g., "BUG", "FEAT")
215 """
216 if category in self._issues.categories:
217 return self._issues.categories[category].prefix
218 return category.upper()[:3]
220 def get_category_action(self, category: str) -> str:
221 """Get the default action for a category.
223 Args:
224 category: Category key (e.g., "bugs", "features")
226 Returns:
227 Action verb (e.g., "fix", "implement")
228 """
229 if category in self._issues.categories:
230 return self._issues.categories[category].action
231 return "fix"
233 def get_loops_dir(self) -> Path:
234 """Get the loops directory path."""
235 return self.project_root / self._loops.loops_dir
237 def get_src_path(self) -> Path:
238 """Get the source directory path."""
239 return self.project_root / self._project.src_dir
241 def get_worktree_base(self) -> Path:
242 """Get the worktree base directory path."""
243 return self.project_root / self._automation.worktree_base
245 def get_state_file(self) -> Path:
246 """Get the state file path."""
247 return self.project_root / self._automation.state_file
249 def get_parallel_state_file(self) -> Path:
250 """Get the parallel state file path."""
251 return self.project_root / self._parallel.base.state_file
253 def create_parallel_config(
254 self,
255 *,
256 max_workers: int | None = None,
257 priority_filter: list[str] | None = None,
258 max_issues: int = 0,
259 dry_run: bool = False,
260 timeout_seconds: int | None = None,
261 idle_timeout_per_issue: int | None = None,
262 stream_output: bool | None = None,
263 show_model: bool | None = None,
264 only_ids: set[str] | None = None,
265 skip_ids: set[str] | None = None,
266 type_prefixes: set[str] | None = None,
267 merge_pending: bool = False,
268 clean_start: bool = False,
269 ignore_pending: bool = False,
270 overlap_detection: bool = False,
271 serialize_overlapping: bool = True,
272 base_branch: str = "main",
273 remote_name: str | None = None,
274 ) -> ParallelConfig:
275 """Create a ParallelConfig from BRConfig settings with optional overrides.
277 Args:
278 max_workers: Override max_workers (default: from config)
279 priority_filter: Override priority filter (default: from issues config)
280 max_issues: Maximum issues to process (default: 0 = unlimited)
281 dry_run: Preview mode (default: False)
282 timeout_seconds: Per-issue timeout (default: from config)
283 idle_timeout_per_issue: Kill worker if no output for N seconds (0 to disable, default: 0)
284 stream_output: Stream output (default: from config)
285 show_model: Make API call to verify model (default: False)
286 only_ids: If provided, only process these issue IDs
287 skip_ids: Issue IDs to skip (in addition to completed/failed)
288 merge_pending: Attempt to merge pending worktrees (default: False)
289 clean_start: Remove all worktrees without checking (default: False)
290 ignore_pending: Report pending work but continue (default: False)
291 overlap_detection: Enable pre-flight overlap detection (default: False)
292 serialize_overlapping: If True, defer overlapping issues; if False, just warn
294 Returns:
295 ParallelConfig configured from BRConfig
296 """
297 return ParallelConfig(
298 max_workers=max_workers or self._parallel.base.max_workers,
299 p0_sequential=self._parallel.p0_sequential,
300 worktree_base=Path(self._parallel.base.worktree_base),
301 state_file=Path(self._parallel.base.state_file),
302 max_merge_retries=self._parallel.max_merge_retries,
303 priority_filter=priority_filter or self._issues.priorities,
304 max_issues=max_issues,
305 dry_run=dry_run,
306 timeout_per_issue=timeout_seconds or self._parallel.base.timeout_seconds,
307 idle_timeout_per_issue=idle_timeout_per_issue
308 if idle_timeout_per_issue is not None
309 else 0,
310 stream_subprocess_output=(
311 stream_output if stream_output is not None else self._parallel.base.stream_output
312 ),
313 show_model=show_model if show_model is not None else False,
314 command_prefix=self._parallel.command_prefix,
315 ready_command=self._parallel.ready_command,
316 manage_command=self._parallel.manage_command,
317 only_ids=only_ids,
318 skip_ids=skip_ids,
319 type_prefixes=type_prefixes,
320 worktree_copy_files=self._parallel.worktree_copy_files,
321 require_code_changes=self._parallel.require_code_changes,
322 use_feature_branches=self._parallel.use_feature_branches,
323 merge_pending=merge_pending,
324 clean_start=clean_start,
325 ignore_pending=ignore_pending,
326 overlap_detection=overlap_detection,
327 serialize_overlapping=serialize_overlapping,
328 base_branch=base_branch,
329 remote_name=remote_name if remote_name is not None else self._parallel.remote_name,
330 )
332 @property
333 def issue_categories(self) -> list[str]:
334 """Get list of configured issue category names."""
335 return list(self._issues.categories.keys())
337 @property
338 def issue_priorities(self) -> list[str]:
339 """Get list of valid priority prefixes."""
340 return self._issues.priorities
342 def to_dict(self) -> dict[str, Any]:
343 """Convert configuration to dictionary.
345 Useful for variable substitution in command templates.
346 """
347 return {
348 "project": {
349 "name": self._project.name,
350 "src_dir": self._project.src_dir,
351 "test_dir": self._project.test_dir,
352 "test_cmd": self._project.test_cmd,
353 "lint_cmd": self._project.lint_cmd,
354 "type_cmd": self._project.type_cmd,
355 "format_cmd": self._project.format_cmd,
356 "build_cmd": self._project.build_cmd,
357 "run_cmd": self._project.run_cmd,
358 },
359 "issues": {
360 "base_dir": self._issues.base_dir,
361 "categories": {
362 k: {"prefix": v.prefix, "dir": v.dir, "action": v.action}
363 for k, v in self._issues.categories.items()
364 },
365 "completed_dir": self._issues.completed_dir,
366 "deferred_dir": self._issues.deferred_dir,
367 "priorities": self._issues.priorities,
368 "templates_dir": self._issues.templates_dir,
369 "capture_template": self._issues.capture_template,
370 },
371 "automation": {
372 "timeout_seconds": self._automation.timeout_seconds,
373 "idle_timeout_seconds": self._automation.idle_timeout_seconds,
374 "state_file": self._automation.state_file,
375 "worktree_base": self._automation.worktree_base,
376 "max_workers": self._automation.max_workers,
377 "stream_output": self._automation.stream_output,
378 "max_continuations": self._automation.max_continuations,
379 },
380 "parallel": {
381 "max_workers": self._parallel.base.max_workers,
382 "p0_sequential": self._parallel.p0_sequential,
383 "worktree_base": self._parallel.base.worktree_base,
384 "state_file": self._parallel.base.state_file,
385 "timeout_per_issue": self._parallel.base.timeout_seconds,
386 "max_merge_retries": self._parallel.max_merge_retries,
387 "stream_subprocess_output": self._parallel.base.stream_output,
388 "command_prefix": self._parallel.command_prefix,
389 "ready_command": self._parallel.ready_command,
390 "manage_command": self._parallel.manage_command,
391 "worktree_copy_files": self._parallel.worktree_copy_files,
392 "require_code_changes": self._parallel.require_code_changes,
393 "use_feature_branches": self._parallel.use_feature_branches,
394 "remote_name": self._parallel.remote_name,
395 },
396 "commands": {
397 "pre_implement": self._commands.pre_implement,
398 "post_implement": self._commands.post_implement,
399 "custom_verification": self._commands.custom_verification,
400 "confidence_gate": {
401 "enabled": self._commands.confidence_gate.enabled,
402 "readiness_threshold": self._commands.confidence_gate.readiness_threshold,
403 "outcome_threshold": self._commands.confidence_gate.outcome_threshold,
404 },
405 "tdd_mode": self._commands.tdd_mode,
406 },
407 "scan": {
408 "focus_dirs": self._scan.focus_dirs,
409 "exclude_patterns": self._scan.exclude_patterns,
410 "custom_agents": self._scan.custom_agents,
411 },
412 "sprints": {
413 "sprints_dir": self._sprints.sprints_dir,
414 "default_timeout": self._sprints.default_timeout,
415 "default_max_workers": self._sprints.default_max_workers,
416 },
417 "loops": {
418 "loops_dir": self._loops.loops_dir,
419 "glyphs": self._loops.glyphs.to_dict(),
420 },
421 "sync": {
422 "enabled": self._sync.enabled,
423 "provider": self._sync.provider,
424 "github": {
425 "repo": self._sync.github.repo,
426 "label_mapping": self._sync.github.label_mapping,
427 "priority_labels": self._sync.github.priority_labels,
428 "sync_completed": self._sync.github.sync_completed,
429 "state_file": self._sync.github.state_file,
430 "pull_template": self._sync.github.pull_template,
431 },
432 },
433 "dependency_mapping": {
434 "overlap_min_files": self._dependency_mapping.overlap_min_files,
435 "overlap_min_ratio": self._dependency_mapping.overlap_min_ratio,
436 "min_directory_depth": self._dependency_mapping.min_directory_depth,
437 "conflict_threshold": self._dependency_mapping.conflict_threshold,
438 "high_conflict_threshold": self._dependency_mapping.high_conflict_threshold,
439 "confidence_modifier": self._dependency_mapping.confidence_modifier,
440 "scoring_weights": {
441 "semantic": self._dependency_mapping.scoring_weights.semantic,
442 "section": self._dependency_mapping.scoring_weights.section,
443 "type": self._dependency_mapping.scoring_weights.type,
444 },
445 "exclude_common_files": self._dependency_mapping.exclude_common_files,
446 },
447 "cli": {
448 "color": self._cli.color,
449 "colors": {
450 "logger": {
451 "info": self._cli.colors.logger.info,
452 "success": self._cli.colors.logger.success,
453 "warning": self._cli.colors.logger.warning,
454 "error": self._cli.colors.logger.error,
455 },
456 "priority": {
457 "P0": self._cli.colors.priority.P0,
458 "P1": self._cli.colors.priority.P1,
459 "P2": self._cli.colors.priority.P2,
460 "P3": self._cli.colors.priority.P3,
461 "P4": self._cli.colors.priority.P4,
462 "P5": self._cli.colors.priority.P5,
463 },
464 "type": {
465 "BUG": self._cli.colors.type.BUG,
466 "FEAT": self._cli.colors.type.FEAT,
467 "ENH": self._cli.colors.type.ENH,
468 },
469 "fsm_active_state": self._cli.colors.fsm_active_state,
470 "fsm_edge_labels": {
471 "yes": self._cli.colors.fsm_edge_labels.yes,
472 "no": self._cli.colors.fsm_edge_labels.no,
473 "error": self._cli.colors.fsm_edge_labels.error,
474 "partial": self._cli.colors.fsm_edge_labels.partial,
475 "next": self._cli.colors.fsm_edge_labels.next,
476 "default": self._cli.colors.fsm_edge_labels.default,
477 "blocked": self._cli.colors.fsm_edge_labels.blocked,
478 "retry_exhausted": self._cli.colors.fsm_edge_labels.retry_exhausted,
479 },
480 },
481 },
482 }
484 def resolve_variable(self, var_path: str) -> str | None:
485 """Resolve a variable path like 'project.src_dir' to its value.
487 Args:
488 var_path: Dot-separated path to configuration value
490 Returns:
491 The resolved value as a string, or None if not found
492 """
493 parts = var_path.split(".")
494 value: Any = self.to_dict()
496 for part in parts:
497 if isinstance(value, dict) and part in value:
498 value = value[part]
499 else:
500 return None
502 if value is None:
503 return None
504 if isinstance(value, list):
505 return " ".join(str(v) for v in value)
506 return str(value)
509# Backwards compatibility alias
510CLConfig = BRConfig