A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
never reopen pickled file handles on kernel snapshot restore (#3749)
kt63bdb5ad0366
diff --git a/prime-agent-runtime/src/rlm/repl.md b/prime-agent-runtime/src/rlm/repl.mdindex a4484484eb..4da6c0b48d 100644--- a/prime-agent-runtime/src/rlm/repl.md+++ b/prime-agent-runtime/src/rlm/repl.md@@ -176,7 +176,11 @@ snapshot (and nothing is pruned).`restore` loads the payload and revives each name independently; a missingfile yields an ok empty restore with `reason:"snapshot not found"`, a corruptfile fails with a `reason`, and per-name failures are listed in `failed`.-Names `In`, `Out`, and `get_ipython` in a payload are never restored. `dill` is imported lazily; when unavailable, snapshot and restore+Names `In`, `Out`, and `get_ipython` in a payload are never restored. A pickled file+handle is never reopened: one saved by path revives as a closed stub that+keeps its `name` and `mode` and raises `ValueError` on I/O like any closed+file (reopening with `"w"`/`"w+"` would truncate the file, and `"x"` would+fail on an existing one), and one saved as a raw fd number fails that name. `dill` is imported lazily; when unavailable, snapshot and restorefail with `status:"error"` and a `reason`.`list_names` replies with `done` carrying `names`: the sorted user-defineddiff --git a/prime-agent-runtime/src/rlm/repl.py b/prime-agent-runtime/src/rlm/repl.pyindex eeb7d91228..7af6d3dcc6 100644--- a/prime-agent-runtime/src/rlm/repl.py+++ b/prime-agent-runtime/src/rlm/repl.py@@ -1067,27 +1067,54 @@ def _revive_with_live_globals(return rebound-def _snapshot_unpickler(dill: Any) -> type:- """Unpickler that refuses to reopen a pickled raw fd number in this kernel.+class _RestoredFileStub(io.IOBase):+ """Closed placeholder for a file handle that a snapshot restore revived.++ Keeps the saved name and mode for inspection; read, write and the other+ I/O calls raise ValueError("I/O operation on closed file.") like any+ closed file. It never touches the path it names.+ """++ def __init__(self, name: Any, mode: Any) -> None:+ super().__init__()+ self.name = name+ self.mode = mode+ self.close()++ def read(self, size: int = -1) -> Any:+ self._checkClosed()- dill serializes a pipe/socket-backed file as its fd NUMBER; restoring a- saved closed one reopens that number here and closes it again, killing- whatever owns the fd now (e.g. the event loop's self-pipe). The refusal- fails just that record; every other name still restores.+ def write(self, data: Any) -> Any:+ self._checkClosed()++ def __repr__(self) -> str:+ return f"<closed file {self.name!r} mode={self.mode!r} (restored from snapshot)>"+++def _snapshot_unpickler(dill: Any) -> type:+ """Unpickler that never reopens a pickled file handle in this kernel.++ dill saves a file object as _create_filehandle(name, mode, ...), and its+ revival reopens name with the saved mode: "w"/"w+" truncate the user's+ file (even for a handle that was closed when saved), "x" fails the name+ when the file exists and creates it when it does not, and a pipe/socket-backed+ file comes back as its raw fd NUMBER, which reopening and closing kills+ for whatever owns that fd now (e.g. the event loop's self-pipe). Raw fd+ records fail just that name; path records revive as a closed stub."""create_filehandle = dill._dill._create_filehandle- def refuse_raw_fd(name: Any, *args: Any) -> Any:+ def stub_filehandle(name: Any, mode: Any, *args: Any) -> Any:if isinstance(name, int):raise pickle.UnpicklingError(f"refusing to reopen raw file descriptor {name} from a snapshot")- return create_filehandle(name, *args)+ return _RestoredFileStub(name, mode)class GuardedUnpickler(dill.Unpickler):def find_class(self, module: str, name: str) -> Any:target = super().find_class(module, name)- return refuse_raw_fd if target is create_filehandle else target+ return stub_filehandle if target is create_filehandle else targetreturn GuardedUnpicklerdiff --git a/prime-agent-runtime/test/test_repl.py b/prime-agent-runtime/test/test_repl.pyindex 81d6bd0288..e76e38ecfb 100644--- a/prime-agent-runtime/test/test_repl.py+++ b/prime-agent-runtime/test/test_repl.py@@ -820,6 +820,46 @@ class ReplTest(unittest.TestCase):self.assertEqual(one(events, "done")["status"], "ok")self.assertEqual(one(events, "result")["text"], "'woken'")+ def test_restore_never_reopens_pickled_file_handles(self):+ # dill saves a file object as (name, mode) and its revival reopens the+ # path with that mode: "w" truncated the user's file on every restore+ # even when the handle was closed (discussion #3085).+ sys.path.insert(0, SRC)+ self.addCleanup(sys.path.remove, SRC)+ from rlm import repl as repl_module++ with tempfile.TemporaryDirectory() as tmp:+ target = os.path.join(tmp, "notes.txt")+ handle = open(target, "w")+ handle.close()+ with open(target, "w") as fh:+ fh.write("precious")+ # A handle nested in a container takes the same reconstructor.+ ns = {"handle": handle, "nested": {"inner": [handle]}}+ path = os.path.join(tmp, "state", "kernel-state.dill")+ manifest_path = os.path.join(tmp, "state", "kernel-state.json")+ result = repl_module._snapshot_state(+ ns,+ path,+ manifest_path,+ repl_module.DEFAULT_SNAPSHOT_MAX_BYTES,+ repl_module.DEFAULT_SNAPSHOT_MAX_VARIABLE_BYTES,+ False,+ )+ self.assertEqual(result["saved"], ["handle", "nested"])++ restored_ns = {}+ result = repl_module._restore_state(restored_ns, path)+ self.assertEqual((result["restored"], result["failed"]), (["handle", "nested"], []))+ with open(target) as fh:+ self.assertEqual(fh.read(), "precious")+ restored = restored_ns["handle"]+ self.assertEqual((restored.name, restored.mode, restored.closed), (target, "w", True))+ self.assertIsInstance(restored_ns["nested"]["inner"][0], repl_module._RestoredFileStub)+ for call in (lambda: restored.write("x"), restored.read, restored.readline):+ with self.assertRaisesRegex(ValueError, "closed file"):+ call()+def test_stdin_eof_flushes_final_snapshot(self):# Host death (EOF, no shutdown request) must persist the namespace# tail that postdates the last explicit snapshot.