Cluster the potential core components into modules. Args: completer: optional ``(prompt: str) -> str`` callable. When provided, clustering calls go through this completer instead of the legacy ``call_llm``. This is how the LLMBackend abstraction injects
(
leaf_nodes: List[str],
components: Dict[str, Node],
config: Config,
current_module_tree: dict[str, Any] = {},
current_module_name: str = None,
current_module_path: List[str] = [],
completer: Optional[Completer] = None,
)
| 54 | else: |
| 55 | logger.warning(f"Skipping invalid leaf node '{leaf_node}' - not found in components") |
| 56 | |
| 57 | # group leaf nodes by file |
| 58 | leaf_nodes_by_file = defaultdict(list) |
| 59 | for leaf_node in valid_leaf_nodes: |
| 60 | leaf_nodes_by_file[components[leaf_node].relative_path].append(leaf_node) |
| 61 | |
| 62 | potential_core_components = "" |
| 63 | potential_core_components_with_code = "" |
| 64 | for file, file_nodes in dict(sorted(leaf_nodes_by_file.items())).items(): |
| 65 | header = f"# {file}" |
| 66 | if all(components[n].component_type == "artifact" for n in file_nodes): |
| 67 | header += f" (artifact: {components[file_nodes[0]].artifact_class or 'config'})" |
| 68 | potential_core_components += f"{header}\n" |
| 69 | potential_core_components_with_code += f"{header}\n" |
| 70 | for leaf_node in file_nodes: |
| 71 | potential_core_components += f"\t{leaf_node}\n" |
| 72 | potential_core_components_with_code += f"\t{leaf_node}\n" |
| 73 | potential_core_components_with_code += f"{components[leaf_node].source_code}\n" |
| 74 | |
| 75 | return potential_core_components, potential_core_components_with_code |
| 76 | |
| 77 | |
| 78 | def get_clustering_input_token_count(leaf_nodes: list[str], components: dict[str, Node]) -> int: |
| 79 | """Count leaf prompt tokens for diagnostics, never for the split decision.""" |
| 80 | _, potential_core_components_with_code = format_potential_core_components( |
| 81 | leaf_nodes, components |
| 82 | ) |
| 83 | return count_tokens(potential_core_components_with_code) |
| 84 | |
| 85 | |
| 86 | def _cluster_batch_fits(node_ids: list[str], config: Config) -> bool: |
| 87 | """Whether a single LLM clustering call can handle these nodes. |
| 88 | |
| 89 | The clustering response must re-emit every component ID verbatim, so the |
| 90 | joined ID list is a direct proxy for output size; keep 2x headroom under |
| 91 | max_tokens for dict syntax, module names/paths, and preamble. |
| 92 | """ |
| 93 | max_nodes = getattr(config, "max_leaf_nodes_per_cluster", DEFAULT_MAX_LEAF_NODES_PER_CLUSTER) |
| 94 | if len(node_ids) > max_nodes: |
| 95 | return False |
| 96 | output_budget = max(2048, config.max_tokens // 2) |
| 97 | return count_tokens("\n".join(node_ids)) <= output_budget |
| 98 | |
| 99 | |
| 100 | def partition_leaf_nodes_by_structure( |
| 101 | leaf_nodes: list[str], |
| 102 | components: dict[str, Node], |
| 103 | fits: Callable[[list[str]], bool], |
| 104 | ) -> list[list[str]]: |
| 105 | """Partition leaf nodes into batches that each satisfy ``fits``. |
| 106 | |
| 107 | Splits along the directory structure of the nodes' relative paths, then |
| 108 | greedily coalesces path-adjacent small groups so root-level files and tiny |
| 109 | directories don't become their own batches. Deterministic: nodes are |
| 110 | processed in (relative_path, id) order. Returns the input as a single |
| 111 | batch when it already fits. |
| 112 | """ |
| 113 | valid = [] |
no test coverage detected