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

1"""ll-sprint show subcommand and dependency visualization renderers.""" 

2 

3from __future__ import annotations 

4 

5import argparse 

6from pathlib import Path 

7from typing import TYPE_CHECKING, Any 

8 

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 

17 

18if TYPE_CHECKING: 

19 from little_loops.dependency_graph import WaveContentionNote 

20 from little_loops.sprint import SprintManager 

21 

22 

23def _render_dependency_graph( 

24 waves: list[list[Any]], 

25 dep_graph: DependencyGraph, 

26) -> str: 

27 """Render ASCII dependency graph. 

28 

29 Args: 

30 waves: List of execution waves 

31 dep_graph: DependencyGraph for looking up relationships 

32 

33 Returns: 

34 Formatted string showing dependency arrows 

35 """ 

36 if not waves or len(waves) <= 1: 

37 return "" 

38 

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 "" 

45 

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("") 

53 

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() 

58 

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) 

64 

65 blocked_issues = sorted(dep_graph.blocks.get(issue_id, set())) 

66 if not blocked_issues: 

67 return issue_id 

68 

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 

78 

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)] 

81 

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}") 

87 

88 lines.extend(chains) 

89 lines.append("") 

90 lines.append("Legend: \u2500\u2500\u2192 blocks (must complete before)") 

91 

92 return "\n".join(lines) 

93 

94 

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. 

104 

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) 

109 

110 if has_cycles: 

111 return "BLOCKED -- dependency cycles detected" 

112 

113 if invalid: 

114 return f"WARNING -- {len(invalid)} issue(s) not found on disk" 

115 

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" 

127 

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 

143 

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 = "" 

148 

149 return f"OK -- {total_issues} issues in {logical_count} {wave_word}{suffix}" 

150 

151 

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 

159 

160 # Validate issues 

161 valid = manager.validate_issues(sprint.issues) 

162 invalid = set(sprint.issues) - set(valid.keys()) 

163 

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 

170 

171 # Gather all issue IDs on disk to avoid false "nonexistent" warnings 

172 from little_loops.dependency_mapper import gather_all_issue_ids 

173 

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) 

177 

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() 

181 

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) 

186 

187 print(f"Sprint: {sprint.name}") 

188 print(f"Description: {sprint.description or '(none)'}") 

189 print(f"Created: {sprint.created}") 

190 

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 ) 

197 

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 

203 

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 ) 

208 

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 

213 

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}") 

225 

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})") 

236 

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)}") 

243 

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 ) 

252 

253 if invalid: 

254 print(f"\nWarning: {len(invalid)} issue(s) not found") 

255 

256 return 0