Persist the IDE agent's clustering result as the module tree.
(
arguments: Dict[str, Any],
store: SessionStore,
)
| 57 | |
| 58 | |
| 59 | def handle_save_module_tree( |
| 60 | arguments: Dict[str, Any], |
| 61 | store: SessionStore, |
| 62 | ) -> str: |
| 63 | """Persist the IDE agent's clustering result as the module tree.""" |
| 64 | session_id = arguments["session_id"] |
| 65 | session = store.get(session_id) |
| 66 | if session is None: |
| 67 | return json.dumps({"error": f"Session {session_id} not found or expired."}) |
| 68 | |
| 69 | module_tree = arguments["module_tree"] |
| 70 | output_dir = session.output_dir |
| 71 | |
| 72 | # Save both immutable snapshot and mutable working copy |
| 73 | first_path = os.path.join(output_dir, FIRST_MODULE_TREE_FILENAME) |
| 74 | working_path = os.path.join(output_dir, MODULE_TREE_FILENAME) |
| 75 | |
| 76 | os.makedirs(output_dir, exist_ok=True) |
| 77 | |
| 78 | with open(first_path, "w", encoding="utf-8") as f: |
| 79 | json.dump(module_tree, f, indent=2, ensure_ascii=False) |
| 80 | with open(working_path, "w", encoding="utf-8") as f: |
| 81 | json.dump(module_tree, f, indent=2, ensure_ascii=False) |
| 82 | |
| 83 | # Cache in session |
| 84 | session.module_tree = module_tree |
| 85 | |
| 86 | # Compute processing order and write to workspace file |
| 87 | order = _get_processing_order(module_tree) |
| 88 | order_file = None |
| 89 | if session.workspace is not None: |
| 90 | order_path = session.workspace.write_json("processing_order.json", order) |
| 91 | order_file = str(order_path) |
| 92 | |
| 93 | result = { |
| 94 | "status": "saved", |
| 95 | "module_count": len(module_tree), |
| 96 | "tree_path": working_path, |
| 97 | "first_tree_path": first_path, |
| 98 | "processing_order_file": order_file, |
| 99 | "hint": ( |
| 100 | "Read the processing_order.json file for the leaf-first generation order. " |
| 101 | "Process leaf modules first (is_leaf=true), then parent modules. " |
| 102 | "For each leaf module: get_prompt('system_leaf') + read_code_components + write_doc_file. " |
| 103 | "For each parent module: get_prompt('overview_module') + write_doc_file." |
| 104 | ), |
| 105 | } |
| 106 | return json.dumps(result, indent=2, ensure_ascii=False) |
| 107 | |
| 108 | |
| 109 | def handle_get_processing_order( |
nothing calls this directly
no test coverage detected