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

128 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2026-03-14 23:20 -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 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 _render_execution_plan( 

16 waves: list[list[Any]], 

17 dep_graph: DependencyGraph, 

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

19) -> str: 

20 """Render execution plan with wave groupings. 

21 

22 Args: 

23 waves: List of execution waves from get_execution_waves() 

24 dep_graph: DependencyGraph for looking up blockers 

25 contention_notes: Optional per-wave contention annotations from 

26 refine_waves_for_contention(). Same length as waves. 

27 

28 Returns: 

29 Formatted string showing wave structure 

30 """ 

31 if not waves: 

32 return "" 

33 

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

35 # parent_wave_index are grouped together. 

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

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

38 

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

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

41 if note is not None: 

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

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

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

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

46 logical_waves[-1].append(idx) 

47 continue 

48 logical_waves.append([idx]) 

49 else: 

50 logical_waves.append([idx]) 

51 

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

53 num_logical = len(logical_waves) 

54 lines: list[str] = [] 

55 

56 width = terminal_width() 

57 lines.append("") 

58 lines.append("=" * width) 

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

60 lines.append(f"EXECUTION PLAN ({total_issues} issues, {num_logical} {wave_word})") 

61 lines.append("=" * width) 

62 

63 for logical_idx, group in enumerate(logical_waves): 

64 lines.append("") 

65 logical_num = logical_idx + 1 

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

67 group_count = len(group_issues) 

68 is_contention = len(group) > 1 

69 

70 if is_contention: 

71 # Multiple sub-waves from overlap splitting 

72 lines.append( 

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

74 ) 

75 step = 0 

76 for widx in group: 

77 for issue in waves[widx]: 

78 step += 1 

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

80 

81 # Truncate title if too long 

82 title = issue.title 

83 if len(title) > 45: 

84 title = title[:42] + "..." 

85 

86 lines.append( 

87 f" \u2514\u2500\u2500 {issue.issue_id}: {title} ({issue.priority})" 

88 ) 

89 

90 # Show blockers for this issue 

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

92 if blockers: 

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

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

95 

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

97 first_note = notes[group[0]] 

98 if first_note: 

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

100 extra = len(first_note.contended_paths) - 2 

101 if extra > 0: 

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

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

104 else: 

105 # Single wave (no overlap splitting) 

106 widx = group[0] 

107 wave = waves[widx] 

108 

109 if logical_num == 1: 

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

111 else: 

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

113 if len(wave) > 1: 

114 parallel_note += " parallel" 

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

116 

117 for i, issue in enumerate(wave): 

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

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

120 

121 # Truncate title if too long 

122 title = issue.title 

123 if len(title) > 45: 

124 title = title[:42] + "..." 

125 

126 lines.append(f"{prefix}{issue.issue_id}: {title} ({issue.priority})") 

127 

128 # Show blockers for this issue 

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

130 if blockers: 

131 blocker_prefix = ( 

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

133 ) 

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

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

136 

137 return "\n".join(lines) 

138 

139 

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

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

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

143 

144 

145def _render_dependency_analysis( 

146 report: Any, 

147 logger: Logger, 

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

149 *, 

150 config: DependencyMappingConfig | None = None, 

151) -> None: 

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

153 

154 Args: 

155 report: DependencyReport from analyze_dependencies() 

156 logger: Logger instance 

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

158 provided, proposals where the target already runs before the 

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

160 config: Optional dependency mapping config for custom thresholds. 

161 """ 

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

163 return 

164 

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

166 

167 if report.proposals: 

168 # Partition proposals into novel vs already-satisfied 

169 novel: list[Any] = [] 

170 satisfied_count = 0 

171 for p in report.proposals: 

172 if issue_to_wave is not None: 

173 target_wave = issue_to_wave.get(p.target_id) 

174 source_wave = issue_to_wave.get(p.source_id) 

175 if ( 

176 target_wave is not None 

177 and source_wave is not None 

178 and target_wave < source_wave 

179 ): 

180 satisfied_count += 1 

181 continue 

182 novel.append(p) 

183 

184 if novel: 

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

186 high_threshold = config.high_conflict_threshold if config else 0.7 

187 conflict_threshold = config.conflict_threshold if config else 0.4 

188 for p in novel: 

189 if p.conflict_score >= high_threshold: 

190 conflict = "HIGH" 

191 elif p.conflict_score >= conflict_threshold: 

192 conflict = "MEDIUM" 

193 else: 

194 conflict = "LOW" 

195 logger.warning( 

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

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

198 ) 

199 if p.overlapping_files: 

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

201 if len(p.overlapping_files) > 3: 

202 files += " and more" 

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

204 

205 if satisfied_count > 0: 

206 total = len(report.proposals) 

207 if not novel: 

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

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

210 else: 

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

212 

213 if report.validation.has_issues: 

214 v = report.validation 

215 if v.broken_refs: 

216 for issue_id, ref_id in v.broken_refs: 

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

218 if v.stale_completed_refs: 

219 for issue_id, ref_id in v.stale_completed_refs: 

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

221 if v.missing_backlinks: 

222 for issue_id, ref_id in v.missing_backlinks: 

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

224 

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

226 print() # blank line separator