Skip to content

Commit 5843380

Browse files
CyrusNuevoDiaclaude
andcommitted
Address final Monty REPL review comments
Fix cleanup() to clear stderr_parts and reset counters, guard state restoration to avoid silently setting variables to None, remove dead final_var()/show_vars() instance methods, and add execution-level stderr capture test. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent bd730e5 commit 5843380

2 files changed

Lines changed: 10 additions & 27 deletions

File tree

‎rlm/environments/monty_repl.py‎

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ def build_wrapper_script(self, user_code: str) -> str:
267267
if name.isidentifier() and not name.startswith("_") and name not in RESERVED_NAMES
268268
}
269269
for name in sorted(persisted_names):
270-
lines.append(f"{name} = __rlm_state.get({name!r})")
270+
lines.append(f"if {name!r} in __rlm_state:")
271+
lines.append(f" {name} = __rlm_state[{name!r}]")
271272

272273
lines.append("def FINAL_VAR(variable_name):")
273274
lines.append(' variable_name = variable_name.strip().strip("\\"\'")')
@@ -386,32 +387,6 @@ def get_history_count(self) -> int:
386387
"""Return the number of conversation histories stored."""
387388
return self._history_count
388389

389-
def final_var(self, variable_name: str) -> str:
390-
"""Return the value of a variable as a final answer."""
391-
variable_name = variable_name.strip().strip("\"'")
392-
if variable_name in self.locals:
393-
return str(self.locals[variable_name])
394-
395-
available = [k for k in self.locals.keys() if not k.startswith("_")]
396-
if available:
397-
return (
398-
f"Error: Variable '{variable_name}' not found. "
399-
f"Available variables: {available}. "
400-
"You must create and assign a variable BEFORE calling FINAL_VAR on it."
401-
)
402-
return (
403-
f"Error: Variable '{variable_name}' not found. "
404-
"No variables have been created yet. "
405-
"You must create and assign a variable in a REPL block BEFORE calling FINAL_VAR on it."
406-
)
407-
408-
def show_vars(self) -> str:
409-
"""Show all available variables in the REPL environment."""
410-
available = {k: type(v).__name__ for k, v in self.locals.items() if not k.startswith("_")}
411-
if not available:
412-
return "No variables created yet. Use ```repl``` blocks to create variables."
413-
return f"Available variables: {available}"
414-
415390
def llm_query(self, prompt: str, model: str | None = None) -> str:
416391
"""Query the LM via socket connection to the handler."""
417392
if not self.lm_handler_address:
@@ -455,6 +430,9 @@ def cleanup(self) -> None:
455430
self.locals.clear()
456431
self.pending_llm_calls.clear()
457432
self.stdout_parts.clear()
433+
self.stderr_parts.clear()
434+
self._context_count = 0
435+
self._history_count = 0
458436

459437
def __enter__(self) -> MontyREPL:
460438
return self

‎tests/test_monty_repl.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ def test_stderr_capture(self):
3939
repl.handle_print_callback("stderr", "oops")
4040
assert "oops" in repl.stderr_parts
4141

42+
def test_stderr_from_execution(self):
43+
repl = MontyREPL()
44+
result = repl.execute_code("1 / 0")
45+
assert "ZeroDivisionError" in result.stderr
46+
4247
def test_context_and_history_counts(self):
4348
repl = MontyREPL()
4449
assert repl.get_context_count() == 0

0 commit comments

Comments
 (0)