Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

fix(runtime): restore snapshot functions with live namespace globals (#2471)

Sebastian Müller8ee46be35b6f
diff --git a/prime-agent-runtime/src/rlm/repl.py b/prime-agent-runtime/src/rlm/repl.py
index a53f89f83f..555d63eb0d 100644
--- a/prime-agent-runtime/src/rlm/repl.py
+++ b/prime-agent-runtime/src/rlm/repl.py
@@ -869,6 +869,102 @@ def _snapshot_state(
return result
+def _revive_with_live_globals(
+ value: Any,
+ ns: dict[str, Any],
+ backfill: list[tuple[str, Any]] | None = None,
+ memo: dict[int, Any] | None = None,
+) -> Any:
+ """Rebind restored __main__ callables onto the live namespace, collecting
+ names their saved globals carry but ns lacks as backfill for the caller
+ to apply at commit (live ns values always win)."""
+ import functools
+
+ if memo is None:
+ memo = {}
+ if id(value) in memo:
+ return memo[id(value)]
+
+ def revive(dep: Any) -> Any:
+ return _revive_with_live_globals(dep, ns, backfill, memo)
+
+ if isinstance(value, functools.partial):
+ # No placeholder memo entry: a partial is immutable, so it could never be patched;
+ # every cycle passes through a function, which is memoized before recursing.
+ rebuilt = revive(value.func)
+ changed = rebuilt is not value.func
+ args = []
+ keywords = {}
+ for arg in value.args:
+ revived = revive(arg)
+ changed = changed or revived is not arg
+ args.append(revived)
+ for key, arg in value.keywords.items():
+ revived = revive(arg)
+ changed = changed or revived is not arg
+ keywords[key] = revived
+ if not changed:
+ return memo.setdefault(id(value), value)
+ rebuilt_partial = functools.partial(rebuilt, *args, **keywords)
+ rebuilt_partial.__dict__.update(value.__dict__)
+ return memo.setdefault(id(value), rebuilt_partial)
+ atoms = (int, float, str, bytes, bool, type(None))
+ # dill loads __main__.__dict__ by reference, so a saved globals() IS the live ns: never walk it.
+ if value is ns:
+ return value
+ if isinstance(value, (list, dict)):
+ # Memoized before recursing and revived in place: cycles and identity come for free.
+ # Skipping atoms keeps the walk over million-element containers near dill.loads cost.
+ memo[id(value)] = value
+ for key, item in enumerate(value) if isinstance(value, list) else value.items():
+ revived = item if type(item) in atoms else revive(item)
+ if revived is not item:
+ value[key] = revived
+ return value
+ if type(value) is tuple:
+ items = tuple(item if type(item) in atoms else revive(item) for item in value)
+ if all(new is old for new, old in zip(items, value)):
+ items = value
+ return memo.setdefault(id(value), items)
+ if not isinstance(value, types.FunctionType) or value.__module__ != "__main__":
+ return value
+ # Defaults and cell contents are revived only after the rebound function is memoized, so a
+ # function reachable from its own defaults or closure resolves to it. Cells are revived in
+ # place: holders this walk never sees (attribute-held siblings) must keep sharing them.
+ rebound = types.FunctionType(value.__code__, ns, value.__name__, None, value.__closure__)
+ memo[id(value)] = rebound
+ if backfill is not None:
+ for name, dep in value.__globals__.items():
+ # Snapshots never save _-prefixed or skip-listed names; backfill must not smuggle them past that policy.
+ if name in ns or name.startswith("_") or name in _ALWAYS_SKIP or name in _RESTORE_SKIP:
+ continue
+ backfill.append((name, revive(dep)))
+ if value.__defaults__:
+ rebound.__defaults__ = tuple(revive(dep) for dep in value.__defaults__)
+ if value.__kwdefaults__:
+ rebound.__kwdefaults__ = {key: revive(dep) for key, dep in value.__kwdefaults__.items()}
+ for cell in value.__closure__ or ():
+ if id(cell) in memo:
+ continue
+ memo[id(cell)] = cell
+ try:
+ contents = cell.cell_contents
+ except ValueError:
+ continue
+ cell.cell_contents = revive(contents)
+ rebound.__doc__ = value.__doc__
+ rebound.__dict__.update({key: revive(attr) for key, attr in value.__dict__.items()})
+ rebound.__annotations__ = value.__annotations__
+ rebound.__qualname__ = value.__qualname__
+ rebound.__module__ = value.__module__
+ # PEP 695 generics carry their type params here on 3.12+; plain 3.11
+ # functions lack the attribute entirely, hence the getattr guard.
+ params = getattr(value, "__type_params__", None)
+ if params is not None:
+ rebound.__type_params__ = params
+ return rebound
+
+
def _restore_state(
ns: dict[str, Any], path: str, committed: list[dict[str, Any]] | None = None
) -> dict[str, Any]:
@@ -900,12 +996,26 @@ def _restore_state(
staged[name] = dill.loads(blob)
except Exception as err: # noqa: BLE001 - revive every other name regardless
failed.append({"name": name, "reason": f"{type(err).__name__}: {_safe_str(err)[:200]}"})
- result = {"restored": sorted(staged), "failed": failed}
+ # Revive every staged name before parking: a failure must never abort the
+ # apply halfway and leave the namespace half old, half new.
+ prepared: dict[str, Any] = {}
+ backfill: list[tuple[str, Any]] = []
+ revive_failed: list[dict[str, str]] = []
+ for name, value in staged.items():
+ try:
+ prepared[name] = _revive_with_live_globals(value, ns, backfill)
+ except Exception as err: # noqa: BLE001 - one broken revival must not abort the restore
+ revive_failed.append({"name": name, "reason": f"{type(err).__name__}: {_safe_str(err)[:200]}"})
+ result = {"restored": sorted(prepared), "failed": failed + revive_failed}
# Park SIGINT across the whole apply so it is all-or-nothing; the parked interrupt is consumed by the commit (as in snapshot).
previous = signal.signal(signal.SIGINT, lambda signum, frame: None)
try:
- for name, value in staged.items():
+ for name, value in prepared.items():
ns[name] = value
+ for name, value in backfill:
+ # prepared names already sit in ns here: a restored value always beats backfill.
+ if name not in ns:
+ ns[name] = value
# Publish while still parked: a later KeyboardInterrupt into this task finds the committed result (see _handle_state).
if committed is not None:
committed.append(result)
diff --git a/prime-agent-runtime/test/test_repl.py b/prime-agent-runtime/test/test_repl.py
index 7e57221ab8..b46eb00911 100644
--- a/prime-agent-runtime/test/test_repl.py
+++ b/prime-agent-runtime/test/test_repl.py
@@ -660,6 +660,77 @@ class ReplTest(unittest.TestCase):
events = self.repl.execute("chk", "'In' in dir()")
self.assertEqual(one(events, "result")["text"], "False")
+ def test_restore_revives_functions_with_live_globals_pr2471(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ path = os.path.join(tmp, "state.dill")
+ self.repl.execute("fn1", "G = 1\ndef reader():\n return G\ndef prober():\n return late")
+ # priv.__globals__ is the private exec dict pn, not ns: drives backfill.
+ self.repl.execute("fn0", "exec('SECRET = 42\\ndef priv():\\n return SECRET', pn:={'__name__': '__main__'})\npriv = pn['priv']")
+ self.repl.send({"type": "snapshot", "id": "fn2", "path": path, "manifest_path": os.path.join(tmp, "state.json")})
+ self.repl.send({"type": "restore", "id": "fn3", "path": path})
+ self.assertEqual(one(self.repl.until_done("fn3"), "done")["status"], "ok")
+ events = self.repl.execute("fn4", "G = 2\nreader()")
+ self.assertEqual(one(events, "result")["text"], "2")
+ events = self.repl.execute("fn5", "late = 'live'\nprober()")
+ self.assertEqual(one(events, "result")["text"], "'live'")
+ events = self.repl.execute("fn6", "priv()")
+ self.assertEqual(one(events, "result")["text"], "42")
+
+ def test_restore_revives_partial_wrapped_functions_pr2471(self):
+ # dill revives a partial's wrapped __main__ function with frozen globals.
+ with tempfile.TemporaryDirectory() as tmp:
+ path = os.path.join(tmp, "state.dill")
+ self.repl.execute("pt1", "import functools\nG = 1\ndef base():\n return G\nwrapped = functools.partial(base)")
+ self.repl.send({"type": "snapshot", "id": "pt2", "path": path, "manifest_path": os.path.join(tmp, "state.json")})
+ self.repl.send({"type": "restore", "id": "pt3", "path": path})
+ self.assertEqual(one(self.repl.until_done("pt3"), "done")["status"], "ok")
+ events = self.repl.execute("pt4", "G = 2\nwrapped()")
+ self.assertEqual(one(events, "result")["text"], "2")
+
+ def _snapshot_restore(self, rid: str, code: str, tmp: str) -> None:
+ self.assertEqual(one(self.repl.execute(rid + "0", code), "done")["status"], "ok")
+ self.repl.send({"type": "snapshot", "id": rid + "1", "path": os.path.join(tmp, "s.dill"), "manifest_path": os.path.join(tmp, "s.json")})
+ self.repl.send({"type": "restore", "id": rid + "2", "path": os.path.join(tmp, "s.dill")})
+ self.assertEqual(one(self.repl.until_done(rid + "2"), "done")["status"], "ok")
+
+ def test_restore_rebuilt_partial_keeps_attributes_and_args_pr2471(self):
+ code = (
+ "import functools\nG = 1\ndef helper():\n return G\ndef apply(fn):\n return fn()\n"
+ "wrapped = functools.partial(apply, helper)\nwrapped.label = 'x'"
+ )
+ with tempfile.TemporaryDirectory() as tmp:
+ self._snapshot_restore("pa", code, tmp)
+ self.assertEqual(one(self.repl.execute("pa4", "wrapped.label"), "result")["text"], "'x'")
+ self.assertEqual(one(self.repl.execute("pa5", "G = 2\nwrapped()"), "result")["text"], "2")
+
+ def test_restore_backfill_skips_excluded_names_pr2471(self):
+ code = (
+ "exec('PUB = 1\\n_hidden = 2\\nIn = 3\\nOut = 4\\ndef user():\\n return (PUB, _hidden, In, Out)', pn:={'__name__': '__main__'})\n"
+ "user = pn['user']"
+ )
+ with tempfile.TemporaryDirectory() as tmp:
+ self._snapshot_restore("ex", code, tmp)
+ probe = "('PUB' in dir(), '_hidden' in dir(), 'In' in dir(), 'Out' in dir())"
+ self.assertEqual(one(self.repl.execute("ex4", probe), "result")["text"], "(True, False, False, False)")
+
+ def test_restore_revives_callables_in_defaults_and_closures_pr2471(self):
+ code = ("G = 1\ndef helper():\n return G\nrun = lambda fn=helper: fn()\nrun2 = lambda *, fn=helper: fn()\nclosed = (lambda fn: lambda: fn())(helper)\n"
+ "def make():\n n = 0\n def get():\n return n\n def set(v):\n nonlocal n\n n = v\n get.set = set\n return get\ncounter = make()\nrun.callback = helper")
+ with tempfile.TemporaryDirectory() as tmp:
+ self._snapshot_restore("dc", code, tmp)
+ self.assertEqual(one(self.repl.execute("dc4", "G = 2\ncounter.set(5)\n(run(), run2(), closed(), counter(), run.callback())"), "result")["text"], "(2, 2, 2, 5, 2)")
+
+ def test_restore_publishes_rebuilt_partial_in_backfill_cycle_pr2471(self):
+ code = ("exec('import functools\\nG = 1\\ndef base():\\n return G, wrapped\\nwrapped = functools.partial(base)\\n"
+ "def entry():\\n return wrapped()', pn:={'__name__': '__main__'})\nentry = pn['entry']")
+ self._snapshot_restore("bp", code, self.enterContext(tempfile.TemporaryDirectory()))
+ self.assertEqual(one(self.repl.execute("bp4", "G = 2\nentry()[0]"), "result")["text"], "2")
+
+ def test_restore_revives_functions_inside_containers_pr2471(self):
+ code = "G = 1\ndef reader():\n return G\ncallbacks = [reader]\nhandlers = {'read': reader}\npair = (reader,)"
+ self._snapshot_restore("ct", code, self.enterContext(tempfile.TemporaryDirectory()))
+ self.assertEqual(one(self.repl.execute("ct4", "G = 2\n(callbacks[0](), handlers['read'](), pair[0]())"), "result")["text"], "(2, 2, 2)")
+
def test_snapshot_prune_oversized(self):
with tempfile.TemporaryDirectory() as tmp:
path = os.path.join(tmp, "kernel-state.dill")
@@ -2061,6 +2132,14 @@ class RestoreApplyShieldTest(unittest.TestCase):
self.assertEqual(result["restored"], ["a", "b"])
self.assertEqual(dict(ns), {"a": 1, "b": 2})
+ def test_sigint_with_backfill_first_write_is_consumed(self):
+ from rlm.repl import _restore_state
+ exec("SECRET = 42\ndef reader():\n return SECRET", source := {"__name__": "__main__"})
+ with tempfile.TemporaryDirectory() as tmp:
+ ns = self.SigintOnNthSet(fire_on=1)
+ result = _restore_state(ns, self._write_snapshot(tmp, {"reader": source["reader"]}))
+ self.assertEqual((result["restored"], ns["SECRET"]), (["reader"], 42))
+
def _restore_with_sigint_at_unpark(self):
"""Real unparking swap, then the newly restored handler fires immediately."""
from rlm.repl import _restore_state