Coverage for little_loops / cli / sprint / show.py: 0%
141 statements
« prev ^ index » next coverage.py v7.12.0, created at 2026-03-14 23:20 -0500
« prev ^ index » next coverage.py v7.12.0, created at 2026-03-14 23:20 -0500
1"""ll-sprint show subcommand and dependency visualization renderers."""
3from __future__ import annotations
5import argparse
6from pathlib import Path
7from typing import TYPE_CHECKING, Any
9from little_loops.cli.output import terminal_width
10from little_loops.cli.sprint._helpers import (
11 _build_issue_contents,
12 _render_dependency_analysis,
13 _render_execution_plan,
14)
15from little_loops.dependency_graph import DependencyGraph, refine_waves_for_contention
16from little_loops.logger import Logger
18if TYPE_CHECKING:
19 from little_loops.dependency_graph import WaveContentionNote
20 from little_loops.sprint import SprintManager
23def _render_dependency_graph(
24 waves: list[list[Any]],
25 dep_graph: DependencyGraph,
26) -> str:
27 """Render ASCII dependency graph.
29 Args:
30 waves: List of execution waves
31 dep_graph: DependencyGraph for looking up relationships
33 Returns:
34 Formatted string showing dependency arrows
35 """
36 if not waves or len(waves) <= 1:
37 return ""
39 # Don't render graph if there are no actual dependency edges
40 # (waves > 1 can happen from file overlap splitting alone)
41 all_ids = {issue.issue_id for wave in waves for issue in wave}
42 has_edges = any(dep_graph.blocks.get(issue_id, set()) & all_ids for issue_id in all_ids)
43 if not has_edges:
44 return ""
46 width = terminal_width()
47 lines: list[str] = []
48 lines.append("")
49 lines.append("=" * width)
50 lines.append("DEPENDENCY GRAPH")
51 lines.append("=" * width)
52 lines.append("")
54 # Build chains: track which issues block what
55 # Show each independent chain on its own line
56 chains: list[str] = []
57 visited: set[str] = set()
59 def build_chain(issue_id: str) -> str:
60 """Recursively build chain string from issue."""
61 if issue_id in visited:
62 return issue_id
63 visited.add(issue_id)
65 blocked_issues = sorted(dep_graph.blocks.get(issue_id, set()))
66 if not blocked_issues:
67 return issue_id
69 if len(blocked_issues) == 1:
70 return f"{issue_id} \u2500\u2500\u2192 {build_chain(blocked_issues[0])}"
71 else:
72 # Multiple branches - show first inline, note others
73 result = f"{issue_id} \u2500\u2500\u2192 {build_chain(blocked_issues[0])}"
74 for other in blocked_issues[1:]:
75 if other not in visited:
76 chains.append(f" {issue_id} \u2500\u2500\u2192 {build_chain(other)}")
77 return result
79 # Find root issues structurally (not blocked by anything in this graph)
80 roots = [iid for iid in sorted(all_ids) if not (dep_graph.blocked_by.get(iid, set()) & all_ids)]
82 for root in roots:
83 if root not in visited:
84 chain = build_chain(root)
85 if chain and "──→" in chain:
86 chains.append(f" {chain}")
88 lines.extend(chains)
89 lines.append("")
90 lines.append("Legend: \u2500\u2500\u2192 blocks (must complete before)")
92 return "\n".join(lines)
95def _render_health_summary(
96 waves: list[list[Any]],
97 contention_notes: list[WaveContentionNote | None] | None,
98 has_cycles: bool,
99 invalid: set[str],
100 dep_report: Any | None = None,
101 issue_to_wave: dict[str, int] | None = None,
102) -> str:
103 """Render a one-line sprint health summary.
105 Returns:
106 Health summary string like "OK -- 5 issues in 1 wave, contention serialized"
107 """
108 total_issues = sum(len(w) for w in waves)
110 if has_cycles:
111 return "BLOCKED -- dependency cycles detected"
113 if invalid:
114 return f"WARNING -- {len(invalid)} issue(s) not found on disk"
116 # Check for novel (unsatisfied) high-confidence proposals
117 if dep_report and dep_report.proposals and issue_to_wave is not None:
118 novel_count = 0
119 for p in dep_report.proposals:
120 target_wave = issue_to_wave.get(p.target_id)
121 source_wave = issue_to_wave.get(p.source_id)
122 if target_wave is None or source_wave is None or target_wave >= source_wave:
123 if p.confidence >= 0.5:
124 novel_count += 1
125 if novel_count > 0:
126 return f"REVIEW -- {novel_count} potential dependency(ies) to review"
128 # Count logical waves (group contention sub-waves)
129 notes = contention_notes or [None] * len(waves)
130 logical_count = 0
131 has_contention = False
132 prev_parent: int | None = None
133 for idx in range(len(waves)):
134 note = notes[idx] if idx < len(notes) else None
135 if note is not None:
136 has_contention = True
137 if prev_parent is None or note.parent_wave_index != prev_parent:
138 logical_count += 1
139 prev_parent = note.parent_wave_index
140 else:
141 logical_count += 1
142 prev_parent = None
144 wave_word = "wave" if logical_count == 1 else "waves"
145 suffix = ", overlap serialized" if has_contention else ", all parallelizable"
146 if logical_count == 1 and total_issues == 1:
147 suffix = ""
149 return f"OK -- {total_issues} issues in {logical_count} {wave_word}{suffix}"
152def _cmd_sprint_show(args: argparse.Namespace, manager: SprintManager) -> int:
153 """Show sprint details with dependency visualization."""
154 logger = Logger()
155 sprint = manager.load(args.sprint)
156 if not sprint:
157 logger.error(f"Sprint not found: {args.sprint}")
158 return 1
160 # Validate issues
161 valid = manager.validate_issues(sprint.issues)
162 invalid = set(sprint.issues) - set(valid.keys())
164 # Load full IssueInfo objects for dependency analysis
165 issue_infos = manager.load_issue_infos(list(valid.keys()))
166 dep_graph: DependencyGraph | None = None
167 waves: list[list[Any]] = []
168 contention_notes: list[WaveContentionNote | None] | None = None
169 has_cycles = False
171 # Gather all issue IDs on disk to avoid false "nonexistent" warnings
172 from little_loops.dependency_mapper import gather_all_issue_ids
174 config = manager.config
175 issues_dir = config.project_root / config.issues.base_dir if config else Path(".issues")
176 all_known_ids = gather_all_issue_ids(issues_dir, config=config)
178 if issue_infos:
179 dep_graph = DependencyGraph.from_issues(issue_infos, all_known_ids=all_known_ids)
180 has_cycles = dep_graph.has_cycles()
182 if not has_cycles:
183 waves = dep_graph.get_execution_waves()
184 dep_config = config.dependency_mapping if config else None
185 waves, contention_notes = refine_waves_for_contention(waves, config=dep_config)
187 print(f"Sprint: {sprint.name}")
188 print(f"Description: {sprint.description or '(none)'}")
189 print(f"Created: {sprint.created}")
191 # Options on a single compact line right after metadata
192 if sprint.options:
193 opts = sprint.options
194 print(
195 f"Options: max_workers={opts.max_workers}, timeout={opts.timeout}s, max_iterations={opts.max_iterations}"
196 )
198 # Dependency analysis (ENH-301) - run before health summary so we can reference it
199 dep_report: Any = None
200 issue_to_wave: dict[str, int] = {}
201 if issue_infos and not args.skip_analysis:
202 from little_loops.dependency_mapper import analyze_dependencies
204 issue_contents = _build_issue_contents(issue_infos)
205 dep_report = analyze_dependencies(
206 issue_infos, issue_contents, all_known_ids=all_known_ids, config=dep_config
207 )
209 # Build wave ordering map so we can filter already-satisfied proposals
210 for wave_idx, wave in enumerate(waves):
211 for issue in wave:
212 issue_to_wave[issue.issue_id] = wave_idx
214 # Sprint health summary
215 if waves:
216 health = _render_health_summary(
217 waves,
218 contention_notes,
219 has_cycles,
220 invalid,
221 dep_report=dep_report,
222 issue_to_wave=issue_to_wave if issue_to_wave else None,
223 )
224 print(f"Sprint health: {health}")
226 # Show execution plan if we have dependency info and no cycles
227 if waves and dep_graph:
228 print(_render_execution_plan(waves, dep_graph, contention_notes))
229 print(_render_dependency_graph(waves, dep_graph))
230 else:
231 # Fallback to simple list if no valid issues or cycles
232 print(f"Issues ({len(sprint.issues)}):")
233 for issue_id in sprint.issues:
234 status = "valid" if issue_id in valid else "NOT FOUND"
235 print(f" - {issue_id} ({status})")
237 # Warn about cycles if detected
238 if has_cycles and dep_graph:
239 cycles = dep_graph.detect_cycles()
240 print("\nWarning: Dependency cycles detected:")
241 for cycle in cycles:
242 print(f" {' -> '.join(cycle)}")
244 # Render dependency analysis output
245 if dep_report is not None:
246 _render_dependency_analysis(
247 dep_report,
248 logger,
249 issue_to_wave=issue_to_wave if issue_to_wave else None,
250 config=dep_config,
251 )
253 if invalid:
254 print(f"\nWarning: {len(invalid)} issue(s) not found")
256 return 0