Coverage for little_loops / cli / sprint / show.py: 84%

139 statements  

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

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.sprint._helpers import ( 

10 _build_issue_contents, 

11 _render_dependency_analysis, 

12 _render_execution_plan, 

13) 

14from little_loops.dependency_graph import DependencyGraph, refine_waves_for_contention 

15from little_loops.logger import Logger 

16 

17if TYPE_CHECKING: 

18 from little_loops.dependency_graph import WaveContentionNote 

19 from little_loops.sprint import SprintManager 

20 

21 

22def _render_dependency_graph( 

23 waves: list[list[Any]], 

24 dep_graph: DependencyGraph, 

25) -> str: 

26 """Render ASCII dependency graph. 

27 

28 Args: 

29 waves: List of execution waves 

30 dep_graph: DependencyGraph for looking up relationships 

31 

32 Returns: 

33 Formatted string showing dependency arrows 

34 """ 

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

36 return "" 

37 

38 # Don't render graph if there are no actual dependency edges 

39 # (waves > 1 can happen from file overlap splitting alone) 

40 all_ids = {issue.issue_id for wave in waves for issue in wave} 

41 has_edges = any(dep_graph.blocks.get(issue_id, set()) & all_ids for issue_id in all_ids) 

42 if not has_edges: 

43 return "" 

44 

45 lines: list[str] = [] 

46 lines.append("") 

47 lines.append("=" * 70) 

48 lines.append("DEPENDENCY GRAPH") 

49 lines.append("=" * 70) 

50 lines.append("") 

51 

52 # Build chains: track which issues block what 

53 # Show each independent chain on its own line 

54 chains: list[str] = [] 

55 visited: set[str] = set() 

56 

57 def build_chain(issue_id: str) -> str: 

58 """Recursively build chain string from issue.""" 

59 if issue_id in visited: 

60 return issue_id 

61 visited.add(issue_id) 

62 

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

64 if not blocked_issues: 

65 return issue_id 

66 

67 if len(blocked_issues) == 1: 

68 return f"{issue_id} \u2500\u2500\u2192 {build_chain(blocked_issues[0])}" 

69 else: 

70 # Multiple branches - show first inline, note others 

71 result = f"{issue_id} \u2500\u2500\u2192 {build_chain(blocked_issues[0])}" 

72 for other in blocked_issues[1:]: 

73 if other not in visited: 

74 chains.append(f" {issue_id} \u2500\u2500\u2192 {build_chain(other)}") 

75 return result 

76 

77 # Find root issues structurally (not blocked by anything in this graph) 

78 roots = [iid for iid in sorted(all_ids) if not (dep_graph.blocked_by.get(iid, set()) & all_ids)] 

79 

80 for root in roots: 

81 if root not in visited: 

82 chain = build_chain(root) 

83 if chain and "──→" in chain: 

84 chains.append(f" {chain}") 

85 

86 lines.extend(chains) 

87 lines.append("") 

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

89 

90 return "\n".join(lines) 

91 

92 

93def _render_health_summary( 

94 waves: list[list[Any]], 

95 contention_notes: list[WaveContentionNote | None] | None, 

96 has_cycles: bool, 

97 invalid: set[str], 

98 dep_report: Any | None = None, 

99 issue_to_wave: dict[str, int] | None = None, 

100) -> str: 

101 """Render a one-line sprint health summary. 

102 

103 Returns: 

104 Health summary string like "OK -- 5 issues in 1 wave, contention serialized" 

105 """ 

106 total_issues = sum(len(w) for w in waves) 

107 

108 if has_cycles: 

109 return "BLOCKED -- dependency cycles detected" 

110 

111 if invalid: 

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

113 

114 # Check for novel (unsatisfied) high-confidence proposals 

115 if dep_report and dep_report.proposals and issue_to_wave is not None: 

116 novel_count = 0 

117 for p in dep_report.proposals: 

118 target_wave = issue_to_wave.get(p.target_id) 

119 source_wave = issue_to_wave.get(p.source_id) 

120 if target_wave is None or source_wave is None or target_wave >= source_wave: 

121 if p.confidence >= 0.5: 

122 novel_count += 1 

123 if novel_count > 0: 

124 return f"REVIEW -- {novel_count} potential dependency(ies) to review" 

125 

126 # Count logical waves (group contention sub-waves) 

127 notes = contention_notes or [None] * len(waves) 

128 logical_count = 0 

129 has_contention = False 

130 prev_parent: int | None = None 

131 for idx in range(len(waves)): 

132 note = notes[idx] if idx < len(notes) else None 

133 if note is not None: 

134 has_contention = True 

135 if prev_parent is None or note.parent_wave_index != prev_parent: 

136 logical_count += 1 

137 prev_parent = note.parent_wave_index 

138 else: 

139 logical_count += 1 

140 prev_parent = None 

141 

142 wave_word = "wave" if logical_count == 1 else "waves" 

143 suffix = ", overlap serialized" if has_contention else ", all parallelizable" 

144 if logical_count == 1 and total_issues == 1: 

145 suffix = "" 

146 

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

148 

149 

150def _cmd_sprint_show(args: argparse.Namespace, manager: SprintManager) -> int: 

151 """Show sprint details with dependency visualization.""" 

152 logger = Logger() 

153 sprint = manager.load(args.sprint) 

154 if not sprint: 

155 logger.error(f"Sprint not found: {args.sprint}") 

156 return 1 

157 

158 # Validate issues 

159 valid = manager.validate_issues(sprint.issues) 

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

161 

162 # Load full IssueInfo objects for dependency analysis 

163 issue_infos = manager.load_issue_infos(list(valid.keys())) 

164 dep_graph: DependencyGraph | None = None 

165 waves: list[list[Any]] = [] 

166 contention_notes: list[WaveContentionNote | None] | None = None 

167 has_cycles = False 

168 

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

170 from little_loops.dependency_mapper import gather_all_issue_ids 

171 

172 config = manager.config 

173 issues_dir = config.project_root / config.issues.base_dir if config else Path(".issues") 

174 all_known_ids = gather_all_issue_ids(issues_dir, config=config) 

175 

176 if issue_infos: 

177 dep_graph = DependencyGraph.from_issues(issue_infos, all_known_ids=all_known_ids) 

178 has_cycles = dep_graph.has_cycles() 

179 

180 if not has_cycles: 

181 waves = dep_graph.get_execution_waves() 

182 dep_config = config.dependency_mapping if config else None 

183 waves, contention_notes = refine_waves_for_contention(waves, config=dep_config) 

184 

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

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

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

188 

189 # Options on a single compact line right after metadata 

190 if sprint.options: 

191 opts = sprint.options 

192 print( 

193 f"Options: max_workers={opts.max_workers}, timeout={opts.timeout}s, max_iterations={opts.max_iterations}" 

194 ) 

195 

196 # Dependency analysis (ENH-301) - run before health summary so we can reference it 

197 dep_report: Any = None 

198 issue_to_wave: dict[str, int] = {} 

199 if issue_infos and not args.skip_analysis: 

200 from little_loops.dependency_mapper import analyze_dependencies 

201 

202 issue_contents = _build_issue_contents(issue_infos) 

203 dep_report = analyze_dependencies( 

204 issue_infos, issue_contents, all_known_ids=all_known_ids, config=dep_config 

205 ) 

206 

207 # Build wave ordering map so we can filter already-satisfied proposals 

208 for wave_idx, wave in enumerate(waves): 

209 for issue in wave: 

210 issue_to_wave[issue.issue_id] = wave_idx 

211 

212 # Sprint health summary 

213 if waves: 

214 health = _render_health_summary( 

215 waves, 

216 contention_notes, 

217 has_cycles, 

218 invalid, 

219 dep_report=dep_report, 

220 issue_to_wave=issue_to_wave if issue_to_wave else None, 

221 ) 

222 print(f"Sprint health: {health}") 

223 

224 # Show execution plan if we have dependency info and no cycles 

225 if waves and dep_graph: 

226 print(_render_execution_plan(waves, dep_graph, contention_notes)) 

227 print(_render_dependency_graph(waves, dep_graph)) 

228 else: 

229 # Fallback to simple list if no valid issues or cycles 

230 print(f"Issues ({len(sprint.issues)}):") 

231 for issue_id in sprint.issues: 

232 status = "valid" if issue_id in valid else "NOT FOUND" 

233 print(f" - {issue_id} ({status})") 

234 

235 # Warn about cycles if detected 

236 if has_cycles and dep_graph: 

237 cycles = dep_graph.detect_cycles() 

238 print("\nWarning: Dependency cycles detected:") 

239 for cycle in cycles: 

240 print(f" {' -> '.join(cycle)}") 

241 

242 # Render dependency analysis output 

243 if dep_report is not None: 

244 _render_dependency_analysis( 

245 dep_report, 

246 logger, 

247 issue_to_wave=issue_to_wave if issue_to_wave else None, 

248 config=dep_config, 

249 ) 

250 

251 if invalid: 

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

253 

254 return 0