Skip to content

Commit 1c2a292

Browse files
committed
fix: alias context to latest persistent input
1 parent 854e688 commit 1c2a292

4 files changed

Lines changed: 29 additions & 16 deletions

File tree

‎rlm/environments/base_env.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ class SupportsPersistence(Protocol):
298298
299299
ALIASING BEHAVIOR:
300300
The unversioned names always point to index 0:
301-
- context -> context_0 (first context)
301+
- context -> latest numbered context
302302
- history -> history_0 (first history)
303303
304304
EXAMPLE IMPLEMENTATION:
@@ -335,7 +335,7 @@ def add_context(
335335
Storage:
336336
Must store so executed code can access:
337337
- context_0, context_1, etc. (versioned)
338-
- context (alias to context_0)
338+
- context (alias to latest numbered context)
339339
340340
Args:
341341
context_payload: The context data (string, dict, or list).

‎rlm/environments/docker_repl.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,10 @@ def SHOW_VARS():
387387
388388
# Restore scaffold aliases if overwritten by executed code
389389
if "context_0" in _locals:
390-
_locals["context"] = _locals["context_0"]
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+
)]
391394
{history_alias}
392395
393396
save_state(_locals)
@@ -590,7 +593,7 @@ def add_context(
590593
) -> int:
591594
"""Add a context as ``context_N`` (auto-incrementing unless given).
592595
593-
``context`` aliases ``context_0`` for backward compatibility, matching
596+
``context`` aliases the latest numbered context, matching
594597
the versioning behavior of the local environment.
595598
"""
596599
if context_index is None:
@@ -611,8 +614,7 @@ def add_context(
611614
f"with open('/workspace/{fname}', 'r') as _f:\n"
612615
f" {var_name} = json.load(_f)"
613616
)
614-
if context_index == 0:
615-
code += "\ncontext = context_0"
617+
code += f"\ncontext = {var_name}"
616618
self.execute_code(code)
617619

618620
self._context_count = max(self._context_count, context_index + 1)

‎rlm/environments/ipython_repl.py‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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"\ncontext = {var_name}"
1014+
code += f"\ncontext = {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

‎rlm/environments/local_repl.py‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,8 @@ def add_context(
431431
f"import json\nwith open(r'{context_path}', 'r') as f:\n {var_name} = json.load(f)"
432432
)
433433

434-
# Alias context_0 as 'context' for backward compatibility
435-
if context_index == 0:
436-
self.execute_code(f"context = {var_name}")
434+
# Alias the latest context as 'context'.
435+
self.execute_code(f"context = {var_name}")
437436

438437
self._context_count = max(self._context_count, context_index + 1)
439438
return context_index
@@ -538,7 +537,16 @@ def _restore_scaffold(self) -> None:
538537
self._last_final_answer = str(current.get("content", ""))
539538
self.locals["answer"] = replacement
540539
elif name == "context" and "context_0" in self.locals:
541-
self.locals["context"] = self.locals["context_0"]
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+
]
542550
elif name == "history" and "history_0" in self.locals and not self.compaction:
543551
self.locals["history"] = self.locals["history_0"]
544552
elif name == "history" and self.compaction:

0 commit comments

Comments
 (0)