Compute leaf-first processing order from a module tree. Returns a list of dicts with module path, name, leaf status, and component/children info.
(module_tree: Dict[str, Any], parent_path: List[str] | None = None)
| 20 | |
| 21 | # Cap on ID lists embedded in the MCP response. Full lists live in the |
| 22 | # workspace module_tree_validation.json file so stdio stays small. |
| 23 | _MAX_IDS_IN_RESPONSE = 20 |
| 24 | |
| 25 | |
| 26 | def _cap(ids: List[str]) -> Tuple[List[str], bool]: |
| 27 | """Return (ids capped to _MAX_IDS_IN_RESPONSE, was_truncated).""" |
| 28 | if len(ids) <= _MAX_IDS_IN_RESPONSE: |
| 29 | return ids, False |
| 30 | return ids[:_MAX_IDS_IN_RESPONSE], True |
| 31 | |
| 32 | |
| 33 | def _get_processing_order(module_tree: Dict[str, Any], parent_path: List[str] | None = None) -> List[Dict[str, Any]]: |
| 34 | """Compute leaf-first processing order from a module tree. |
| 35 | |
| 36 | Returns a list of dicts with module path, name, leaf status, and |
| 37 | component/children info. |
| 38 | """ |
| 39 | if parent_path is None: |
| 40 | parent_path = [] |
| 41 | order: List[Dict[str, Any]] = [] |
| 42 | |
| 43 | def _collect(tree: Dict[str, Any], path: List[str]) -> None: |
| 44 | for module_name, module_info in tree.items(): |
| 45 | current_path = path + [module_name] |
| 46 | children = module_info.get("children", {}) |
| 47 | has_children = isinstance(children, dict) and len(children) > 0 |
| 48 | |
| 49 | if has_children: |
| 50 | _collect(children, current_path) |
| 51 | order.append({ |
| 52 | "module": module_name, |
| 53 | "path": current_path, |
| 54 | "is_leaf": False, |
| 55 | "children": list(children.keys()), |
| 56 | "components": module_info.get("components", []), |
| 57 | }) |
| 58 | else: |
| 59 | order.append({ |
no test coverage detected