1212 format_system_prompt ,
1313 format_user_prompt ,
1414)
15- from codewiki .src .be .utils import is_complex_module , count_tokens
16- from codewiki .src .be .cluster_modules import format_potential_core_components
15+ from codewiki .src .be .utils import is_complex_module
16+ from codewiki .src .be .module_scope import (
17+ component_files ,
18+ get_module_token_count ,
19+ prepare_submodule_partition ,
20+ )
1721
1822import logging
1923from codewiki .src .be .api_usage import usage_target
@@ -38,6 +42,8 @@ async def generate_sub_module_documentation(
3842
3943 deps = ctx .deps
4044 previous_module_name = deps .current_module_name
45+ if deps .current_depth >= deps .max_depth :
46+ return "Maximum delegation depth reached; write this module directly."
4147
4248 # Create fallback models from config
4349 fallback_models = create_fallback_models (deps .config )
@@ -64,10 +70,20 @@ async def generate_sub_module_documentation(
6470 if requested_name in name_map
6571 }
6672
67- # add the sub-module to the module tree
6873 value = deps .module_tree
6974 for key in deps .path_to_current_module :
7075 value = value [key ]["children" ]
76+ final_specs = prepare_submodule_partition (
77+ final_specs , deps .module_component_ids (), deps .components , value
78+ )
79+ for requested , final_name in list (name_map .items ()):
80+ if final_name not in final_specs :
81+ plan .skipped [requested ] = "no unassigned files in the current module scope"
82+ del name_map [requested ]
83+ if not final_specs :
84+ return skipped_report (plan .skipped , parent_doc )
85+
86+ # Reserve every child's complete file scope before running any agent.
7187 for sub_module_name , core_component_ids in final_specs .items ():
7288 value [sub_module_name ] = {"components" : core_component_ids , "children" : {}}
7389
@@ -78,14 +94,15 @@ async def generate_sub_module_documentation(
7894
7995 logger .info (f"{ indent } { arrow } Generating documentation for sub-module: { sub_module_name } " )
8096
81- num_tokens = count_tokens (
82- format_potential_core_components (core_component_ids , ctx . deps .components )[ - 1 ]
97+ num_tokens = get_module_token_count (
98+ deps . components , component_files (core_component_ids , deps .components )
8399 )
100+ child_depth = deps .current_depth + 1
84101
85102 if (
86103 is_complex_module (ctx .deps .components , core_component_ids )
87- and ctx . deps . current_depth < ctx .deps .max_depth
88- and num_tokens >= ctx .deps .config .max_token_per_leaf_module * ctx . deps . current_depth
104+ and child_depth < ctx .deps .max_depth
105+ and num_tokens >= ctx .deps .config .max_token_per_leaf_module * child_depth
89106 ):
90107 sub_agent = Agent (
91108 model = fallback_models ,
@@ -119,6 +136,8 @@ async def generate_sub_module_documentation(
119136 tools = [read_code_components_tool , str_replace_editor_tool ],
120137 )
121138
139+ previous_scope = deps .scope_component_ids
140+ deps .scope_component_ids = core_component_ids
122141 deps .current_module_name = sub_module_name
123142 deps .path_to_current_module .append (sub_module_name )
124143 deps .current_depth += 1
@@ -143,6 +162,7 @@ async def generate_sub_module_documentation(
143162 deps .path_to_current_module .pop ()
144163 deps .current_depth -= 1
145164 deps .current_module_name = previous_module_name
165+ deps .scope_component_ids = previous_scope
146166
147167 # restore the previous module name
148168 deps .current_module_name = previous_module_name
0 commit comments