Skip to content

Commit b069014

Browse files
committed
refactor: share latest context lookup across REPL backends
1 parent 1c2a292 commit b069014

4 files changed

Lines changed: 20 additions & 20 deletions

File tree

‎rlm/environments/base_env.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@
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
2840
class ToolInfo:
2941
"""Parsed information about a custom tool."""

‎rlm/environments/docker_repl.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import base64
2121
import copy
22+
import inspect
2223
import json
2324
import os
2425
import shutil
@@ -37,6 +38,7 @@
3738
from 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(
281283
except ImportError:
282284
import pickle as dill
283285
286+
{inspect.getsource(get_latest_context)}
284287
PROXY = "http://host.docker.internal:{proxy_port}"
285288
STATE = "/workspace/state.dill"
286289
DEPTH = {depth}
@@ -387,10 +390,7 @@ def SHOW_VARS():
387390
388391
# Restore scaffold aliases if overwritten by executed code
389392
if "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
396396
save_state(_locals)

‎rlm/environments/ipython_repl.py‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
RESERVED_TOOL_NAMES,
5151
NonIsolatedEnv,
5252
extract_tool_value,
53+
get_latest_context,
5354
validate_custom_tools,
5455
)
5556
from 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

‎rlm/environments/local_repl.py‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
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:

0 commit comments

Comments
 (0)