Coverage for little_loops / cli / sprint / _helpers.py: 74%

156 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2026-05-22 16:19 -0500

1"""Shared helpers for ll-sprint CLI subcommands.""" 

2 

3from __future__ import annotations 

4 

5from typing import TYPE_CHECKING, Any 

6 

7from little_loops.cli.output import PRIORITY_COLOR, TYPE_COLOR, colorize, terminal_width 

8 

9if TYPE_CHECKING: 

10 from little_loops.config import DependencyMappingConfig 

11 from little_loops.dependency_graph import DependencyGraph, WaveContentionNote 

12 from little_loops.logger import Logger 

13 

14 

15def _score_suffix(issue: Any) -> str: 

16 """Build an inline score suffix like ' [ready: 85, conf: 72]'.""" 

17 cs = getattr(issue, "confidence_score", None) 

18 oc = getattr(issue, "outcome_confidence", None) 

19 if cs is None and oc is None: 

20 return "" 

21 parts: list[str] = [] 

22 if cs is not None: 

23 parts.append(f"ready: {cs}") 

24 if oc is not None: 

25 parts.append(f"conf: {oc}") 

26 return f" [{', '.join(parts)}]" 

27 

28 

29def _render_execution_plan( 

30 waves: list[list[Any]], 

31 dep_graph: DependencyGraph, 

32 contention_notes: list[WaveContentionNote | None] | None = None, 

33 *, 

34 config: DependencyMappingConfig | None = None, 

35) -> str: 

36 """Render execution plan with wave groupings. 

37 

38 Args: 

39 waves: List of execution waves from get_execution_waves() 

40 dep_graph: DependencyGraph for looking up blockers 

41 contention_notes: Optional per-wave contention annotations from 

42 refine_waves_for_contention(). Same length as waves. 

43 config: Optional dependency mapping config for surfacing effective thresholds. 

44 

45 Returns: 

46 Formatted string showing wave structure 

47 """ 

48 if not waves: 

49 return "" 

50 

51 # Build logical wave groups: consecutive sub-waves from the same 

52 # parent_wave_index are grouped together. 

53 logical_waves: list[list[int]] = [] # each entry is list of indices into waves 

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

55 

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

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

58 if note is not None: 

59 # Check if this belongs to the same parent as the previous group 

60 if logical_waves and notes[logical_waves[-1][0]] is not None: 

61 prev_note = notes[logical_waves[-1][0]] 

62 if prev_note and prev_note.parent_wave_index == note.parent_wave_index: 

63 logical_waves[-1].append(idx) 

64 continue 

65 logical_waves.append([idx]) 

66 else: 

67 logical_waves.append([idx]) 

68 

69 total_issues = sum(len(wave) for wave in waves) 

70 num_logical = len(logical_waves) 

71 lines: list[str] = [] 

72 

73 width = terminal_width() 

74 wave_word = "wave" if num_logical == 1 else "waves" 

75 header_text = f"Execution Plan ({total_issues} issues, {num_logical} {wave_word})" 

76 fill = "\u2500" * max(0, width - len(header_text) - 4) 

77 lines.append("") 

78 lines.append(f"\u2500\u2500 {header_text} {fill}") 

79 

80 max_title = max(45, width - 30) 

81 

82 for logical_idx, group in enumerate(logical_waves): 

83 lines.append("") 

84 logical_num = logical_idx + 1 

85 group_issues = [issue for widx in group for issue in waves[widx]] 

86 group_count = len(group_issues) 

87 is_contention = len(group) > 1 

88 

89 if is_contention: 

90 # Multiple sub-waves from overlap splitting 

91 threshold_suffix = ( 

92 f" [min_files={config.overlap_min_files}, ratio={config.overlap_min_ratio}]" 

93 if config 

94 else "" 

95 ) 

96 lines.append( 

97 f"Wave {logical_num} ({group_count} issues, serialized \u2014 file overlap{threshold_suffix}):" 

98 ) 

99 step = 0 

100 for widx in group: 

101 for issue in waves[widx]: 

102 step += 1 

103 lines.append(f" Step {step}/{group_count}:") 

104 

105 # Truncate title if too long 

106 title = issue.title 

107 if len(title) > max_title: 

108 title = title[: max_title - 3] + "..." 

109 

110 issue_type = issue.issue_id.split("-", 1)[0] 

111 colored_id = colorize(issue.issue_id, TYPE_COLOR.get(issue_type, "0")) 

112 colored_priority = colorize( 

113 issue.priority, PRIORITY_COLOR.get(issue.priority, "0") 

114 ) 

115 score_suffix = _score_suffix(issue) 

116 lines.append( 

117 f" \u2514\u2500\u2500 {colored_id}: {title} ({colored_priority}){score_suffix}" 

118 ) 

119 if hasattr(issue, "path") and issue.path: 

120 lines.append(f" {issue.path}") 

121 

122 # Show blockers for this issue 

123 blockers = dep_graph.blocked_by.get(issue.issue_id, set()) 

124 if blockers: 

125 blockers_str = ", ".join(sorted(blockers)) 

126 lines.append(f" blocked by: {blockers_str}") 

127 

128 # Show contended files once at the end of the group 

129 first_note = notes[group[0]] 

130 if first_note: 

131 paths_str = ", ".join(first_note.contended_paths[:2]) 

132 extra = len(first_note.contended_paths) - 2 

133 if extra > 0: 

134 paths_str += f" +{extra} more" 

135 lines.append(f" Contended files: {paths_str}") 

136 if config is not None: 

137 lines.append( 

138 " Tune: dependency_mapping.overlap_min_files / overlap_min_ratio in ll-config.json" 

139 ) 

140 else: 

141 # Single wave (no overlap splitting) 

142 widx = group[0] 

143 wave = waves[widx] 

144 

145 if logical_num == 1: 

146 parallel_note = "(parallel)" if len(wave) > 1 else "" 

147 else: 

148 parallel_note = f"(after Wave {logical_num - 1})" 

149 if len(wave) > 1: 

150 parallel_note += " parallel" 

151 lines.append(f"Wave {logical_num} {parallel_note}:".strip()) 

152 

153 for i, issue in enumerate(wave): 

154 is_last = i == len(wave) - 1 

155 prefix = " \u2514\u2500\u2500 " if is_last else " \u251c\u2500\u2500 " 

156 

157 # Truncate title if too long 

158 title = issue.title 

159 if len(title) > max_title: 

160 title = title[: max_title - 3] + "..." 

161 

162 issue_type = issue.issue_id.split("-", 1)[0] 

163 colored_id = colorize(issue.issue_id, TYPE_COLOR.get(issue_type, "0")) 

164 colored_priority = colorize(issue.priority, PRIORITY_COLOR.get(issue.priority, "0")) 

165 score_suffix = _score_suffix(issue) 

166 lines.append(f"{prefix}{colored_id}: {title} ({colored_priority}){score_suffix}") 

167 

168 # Show file path 

169 path_indent = " " if is_last else " \u2502 " 

170 if hasattr(issue, "path") and issue.path: 

171 lines.append(f"{path_indent}{issue.path}") 

172 

173 # Show blockers for this issue 

174 blockers = dep_graph.blocked_by.get(issue.issue_id, set()) 

175 if blockers: 

176 blocker_prefix = ( 

177 " \u2514\u2500\u2500 " if is_last else " \u2502 \u2514\u2500\u2500 " 

178 ) 

179 blockers_str = ", ".join(sorted(blockers)) 

180 lines.append(f"{blocker_prefix}blocked by: {blockers_str}") 

181 

182 return "\n".join(lines) 

183 

184 

185def _build_issue_contents(issue_infos: list) -> dict[str, str]: 

186 """Build issue_id -> file content mapping for dependency analysis.""" 

187 return {info.issue_id: info.path.read_text() for info in issue_infos if info.path.exists()} 

188 

189 

190def _render_dependency_analysis( 

191 report: Any, 

192 logger: Logger, 

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

194 *, 

195 config: DependencyMappingConfig | None = None, 

196) -> None: 

197 """Display dependency analysis results in CLI format. 

198 

199 Args: 

200 report: DependencyReport from analyze_dependencies() 

201 logger: Logger instance 

202 issue_to_wave: Optional mapping of issue_id -> wave index. When 

203 provided, proposals where the target already runs before the 

204 source in wave ordering are counted as "already handled". 

205 config: Optional dependency mapping config for custom thresholds. 

206 """ 

207 if not report.proposals and not report.validation.has_issues: 

208 return 

209 

210 logger.header("Dependency Analysis", char="-", width=60) 

211 

212 if report.proposals: 

213 # Partition proposals into novel vs already-satisfied 

214 novel: list[Any] = [] 

215 satisfied_count = 0 

216 for p in report.proposals: 

217 if issue_to_wave is not None: 

218 target_wave = issue_to_wave.get(p.target_id) 

219 source_wave = issue_to_wave.get(p.source_id) 

220 if ( 

221 target_wave is not None 

222 and source_wave is not None 

223 and target_wave < source_wave 

224 ): 

225 satisfied_count += 1 

226 continue 

227 novel.append(p) 

228 

229 if novel: 

230 logger.warning(f"Found {len(novel)} potential missing dependency(ies):") 

231 high_threshold = config.high_conflict_threshold if config else 0.7 

232 conflict_threshold = config.conflict_threshold if config else 0.4 

233 for p in novel: 

234 if p.conflict_score >= high_threshold: 

235 conflict = "HIGH" 

236 elif p.conflict_score >= conflict_threshold: 

237 conflict = "MEDIUM" 

238 else: 

239 conflict = "LOW" 

240 logger.warning( 

241 f" {p.source_id} may depend on {p.target_id} " 

242 f"({conflict} conflict, {p.confidence:.0%} confidence)" 

243 ) 

244 if p.overlapping_files: 

245 files = ", ".join(p.overlapping_files[:3]) 

246 if len(p.overlapping_files) > 3: 

247 files += " and more" 

248 logger.info(f" Shared files: {files}") 

249 

250 if satisfied_count > 0: 

251 total = len(report.proposals) 

252 if not novel: 

253 dep_word = "dependency" if total == 1 else "dependencies" 

254 logger.info(f"All {total} potential {dep_word} already handled by wave ordering.") 

255 else: 

256 logger.info(f"({satisfied_count} additional already handled by wave ordering)") 

257 

258 if report.validation.has_issues: 

259 v = report.validation 

260 if v.broken_refs: 

261 for issue_id, ref_id in v.broken_refs: 

262 logger.warning(f" {issue_id}: references nonexistent {ref_id}") 

263 if v.stale_completed_refs: 

264 for issue_id, ref_id in v.stale_completed_refs: 

265 logger.warning(f" {issue_id}: blocked by {ref_id} (completed)") 

266 if v.missing_backlinks: 

267 for issue_id, ref_id in v.missing_backlinks: 

268 logger.warning(f" {issue_id} blocked by {ref_id}, but {ref_id} missing backlink") 

269 

270 logger.info("Run /ll:map-dependencies to apply discovered dependencies") 

271 print() # blank line separator