@@ -1009,10 +1009,9 @@ def add_context(
10091009 f" { var_name } = _rlm_json.load(_rlm_f)"
10101010 )
10111011
1012- # Fold the ``context = context_0`` alias into the same cell to
1012+ # Fold the latest context alias into the same cell to
10131013 # save a kernel round-trip in subprocess mode.
1014- if context_index == 0 :
1015- code += f"\n context = { var_name } "
1014+ code += f"\n context = { var_name } "
10161015
10171016 result = self .execute_code (code )
10181017 if result .stderr :
@@ -1021,8 +1020,7 @@ def add_context(
10211020 # Shadow for subprocess mode so self.locals can report context_N.
10221021 if self .kernel_mode == "subprocess" :
10231022 self ._subprocess_shadow [var_name ] = copy .deepcopy (context_payload )
1024- if context_index == 0 :
1025- self ._subprocess_shadow ["context" ] = self ._subprocess_shadow [var_name ]
1023+ self ._subprocess_shadow ["context" ] = self ._subprocess_shadow [var_name ]
10261024
10271025 self ._context_count = max (self ._context_count , context_index + 1 )
10281026 return context_index
@@ -1400,7 +1398,12 @@ def _restore_scaffold_in_process(self) -> None:
14001398 self ._last_final_answer = str (current .get ("content" , "" ))
14011399 ns ["answer" ] = replacement
14021400 if "context_0" in ns :
1403- ns ["context" ] = ns ["context_0" ]
1401+ ns ["context" ] = ns [
1402+ max (
1403+ (key for key in ns if key .startswith ("context_" ) and key [8 :].isdigit ()),
1404+ key = lambda key : int (key [8 :]),
1405+ )
1406+ ]
14041407 if "history_0" in ns :
14051408 ns ["history" ] = ns ["history_0" ]
14061409 # Re-inject custom tools if overwritten
0 commit comments