File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -241,11 +241,15 @@ def _run(self, rounds: int) -> str | None:
241241 if self ._is_cancelled ():
242242 return None
243243 self ._inject_pending_prompts ()
244- self ._update_context_ratio ()
245- if self ._need_compaction ():
246- session .log (f"compacting context { session .context_ratio :.1%} " )
247- if self .compact ():
248- continue
244+ if self .top_level :
245+ # sub-agents must not touch the shared context accounting:
246+ # their payload (fresh context) is structurally different,
247+ # so their ratio/usage would skew the parent's
248+ self ._update_context_ratio ()
249+ if self ._need_compaction ():
250+ session .log (f"compacting context { session .context_ratio :.1%} " )
251+ if self .compact ():
252+ continue
249253
250254 def safe_delta (text : str ) -> None :
251255 if not self ._is_cancelled () and session .on_delta is not None :
@@ -280,8 +284,8 @@ def safe_delta(text: str) -> None:
280284 if not assistant .tool_calls and self .top_level :
281285 session .last_messages = list (self .messages )
282286
283- session .calibrator .update (usage .input_tokens )
284287 if self .top_level :
288+ session .calibrator .update (usage .input_tokens )
285289 session .remember_user_text (self .messages )
286290 session .auto_save (self .messages , self .system )
287291
Original file line number Diff line number Diff line change @@ -153,6 +153,17 @@ def test_subagent_never_inherits_parent_prompt(self):
153153 self .assertNotIn ("Task Completion Rules" , sp )
154154 self .assertNotIn ("PARENT PROMPT" , sp )
155155
156+ def test_subagent_does_not_touch_shared_context_accounting (self ):
157+ """The sub-agent's rounds must not update the shared context
158+ ratio or calibration factor: its payload (fresh context) is
159+ structurally different, so its usage would skew the parent's
160+ compaction decisions."""
161+ s = make_session (RecClient ([]))
162+ s .run_subagent ("subagent" , "explore" , "find stuff" )
163+ self .assertIsNone (s .context_ratio )
164+ self .assertEqual (s .calibrator .factor , 1.0 )
165+ self .assertIsNone (s .calibrator .last_raw_estimate )
166+
156167
157168if __name__ == "__main__" :
158169 unittest .main ()
You can’t perform that action at this time.
0 commit comments