File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2424)
2525
2626
27+ def get_latest_context (namespace : dict ) -> object :
28+ """Return the highest-numbered context stored in a REPL namespace."""
29+ prefix = "context_"
30+ names = (
31+ name
32+ for name in namespace
33+ if name .startswith (prefix ) and name .removeprefix (prefix ).isdigit ()
34+ )
35+ latest = max (names , key = lambda name : int (name .removeprefix (prefix )))
36+ return namespace [latest ]
37+
38+
2739@dataclass
2840class ToolInfo :
2941 """Parsed information about a custom tool."""
Original file line number Diff line number Diff line change 1919
2020import base64
2121import copy
22+ import inspect
2223import json
2324import os
2425import shutil
3738from rlm .environments .base_env import (
3839 NonIsolatedEnv ,
3940 extract_tool_value ,
41+ get_latest_context ,
4042 validate_custom_tools ,
4143)
4244
@@ -281,6 +283,7 @@ def _build_exec_script(
281283except ImportError:
282284 import pickle as dill
283285
286+ { inspect .getsource (get_latest_context )}
284287PROXY = "http://host.docker.internal:{ proxy_port } "
285288STATE = "/workspace/state.dill"
286289DEPTH = { depth }
@@ -387,10 +390,7 @@ def SHOW_VARS():
387390
388391# Restore scaffold aliases if overwritten by executed code
389392if "context_0" in _locals:
390- _locals["context"] = _locals[max(
391- (key for key in _locals if key.startswith("context_") and key[8:].isdigit()),
392- key=lambda key: int(key[8:]),
393- )]
393+ _locals["context"] = get_latest_context(_locals)
394394{ history_alias }
395395
396396save_state(_locals)
Original file line number Diff line number Diff line change 5050 RESERVED_TOOL_NAMES ,
5151 NonIsolatedEnv ,
5252 extract_tool_value ,
53+ get_latest_context ,
5354 validate_custom_tools ,
5455)
5556from rlm .environments .local_repl import _AnswerDict
@@ -1398,12 +1399,7 @@ def _restore_scaffold_in_process(self) -> None:
13981399 self ._last_final_answer = str (current .get ("content" , "" ))
13991400 ns ["answer" ] = replacement
14001401 if "context_0" in ns :
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- ]
1402+ ns ["context" ] = get_latest_context (ns )
14071403 if "history_0" in ns :
14081404 ns ["history" ] = ns ["history_0" ]
14091405 # Re-inject custom tools if overwritten
Original file line number Diff line number Diff line change 1919 RESERVED_TOOL_NAMES ,
2020 NonIsolatedEnv ,
2121 extract_tool_value ,
22+ get_latest_context ,
2223 validate_custom_tools ,
2324)
2425
@@ -537,16 +538,7 @@ def _restore_scaffold(self) -> None:
537538 self ._last_final_answer = str (current .get ("content" , "" ))
538539 self .locals ["answer" ] = replacement
539540 elif name == "context" and "context_0" in self .locals :
540- self .locals ["context" ] = self .locals [
541- max (
542- (
543- key
544- for key in self .locals
545- if key .startswith ("context_" ) and key [8 :].isdigit ()
546- ),
547- key = lambda key : int (key [8 :]),
548- )
549- ]
541+ self .locals ["context" ] = get_latest_context (self .locals )
550542 elif name == "history" and "history_0" in self .locals and not self .compaction :
551543 self .locals ["history" ] = self .locals ["history_0" ]
552544 elif name == "history" and self .compaction :
You can’t perform that action at this time.
0 commit comments