Skip to content

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

Rust

preserve the RLM namespace after importing the package

Sebastian Müller9083398b3a13
diff --git a/.changes/rlm-import-surface.md b/.changes/rlm-import-surface.md
new file mode 100644
index 0000000000..c516048162
--- /dev/null
+++ b/.changes/rlm-import-surface.md
@@ -0,0 +1 @@
+- Kernel: `rlm.factory.run(...)`, `rlm.factory.help()`, and every other `rlm` namespace call keep working after a cell runs `import rlm`, and `dir(rlm.harness)` now lists the harness methods.
diff --git a/prime-agent-runtime/src/rlm/__init__.py b/prime-agent-runtime/src/rlm/__init__.py
index 4a40f277e4..82f1da339b 100644
--- a/prime-agent-runtime/src/rlm/__init__.py
+++ b/prime-agent-runtime/src/rlm/__init__.py
@@ -559,6 +559,9 @@ class _HarnessProxy:
def __getattr__(self, name: str) -> Any:
return getattr(self._resolve(), name)
+ def __dir__(self) -> list[str]:
+ return sorted({*super().__dir__(), *(name for name in dir(self._resolve()) if not name.startswith("_"))})
+
def __repr__(self) -> str:
return repr(self._resolve())
@@ -689,6 +692,7 @@ class _RLMNamespace:
rlm = _RLMNamespace()
harness = _harness_state
+factory = rlm.factory
class _NotCallableModule(types.ModuleType):
@@ -740,6 +744,4 @@ def __getattr__(name: str) -> Any: # noqa: D401 - module-level lazy attr hook
from . import mcp_base
return getattr(mcp_base, name)
- if name == "run":
- raise AttributeError(_RENAMED_RUN_MESSAGE)
- raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
+ return getattr(rlm, name)
diff --git a/prime-agent-runtime/src/rlm/repl.py b/prime-agent-runtime/src/rlm/repl.py
index 7af6d3dcc6..8dbc102dc0 100644
--- a/prime-agent-runtime/src/rlm/repl.py
+++ b/prime-agent-runtime/src/rlm/repl.py
@@ -32,7 +32,7 @@ import uuid
from collections.abc import Awaitable, Callable
from typing import Any
-from . import factory as factory_module
+from .factory import schedule_activity
from .bash import _kill_live_handles, _rehydrate_persistent_shells
PROTOCOL_VERSION = 3
@@ -1582,7 +1582,7 @@ def _handle_request_line(raw: bytes, queue: asyncio.Queue[dict[str, Any]]) -> No
# Like bash_activity, this bypasses the cell FIFO: the factory view
# must answer while a cell runs. The handler schedules the async
# activity on this loop and replies when it settles.
- _loop.call_soon_threadsafe(factory_module.schedule_activity, req)
+ _loop.call_soon_threadsafe(schedule_activity, req)
return
if rtype in ("execute", "snapshot", "restore"):
with _interrupt_lock:
diff --git a/prime-agent-runtime/test/test_factory.py b/prime-agent-runtime/test/test_factory.py
index 65894467d2..d05fcf755a 100644
--- a/prime-agent-runtime/test/test_factory.py
+++ b/prime-agent-runtime/test/test_factory.py
@@ -50,6 +50,7 @@ from __future__ import annotations
import asyncio
import hashlib
+import importlib
import json
import os
import re
@@ -63,7 +64,6 @@ from typing import Any
from unittest.mock import patch
import rlm as rlm_module
-from rlm import factory as factory_module
from rlm.factory import (
ANSWER_BINDING_CAP,
ANSWER_CAPTURE_CAP,
@@ -104,6 +104,8 @@ from rlm.factory import (
)
from rlm.harness import HarnessState
+factory_module = importlib.import_module("rlm.factory")
+
# ---------------------------------------------------------------------------
# Spec fixtures
@@ -8953,6 +8955,7 @@ class FactoryRunFromLibraryTest(unittest.TestCase):
_INSTALLED_LIBRARY_RUNNER = r"""
import asyncio
+import importlib
import json
@@ -9013,9 +9016,10 @@ class ScriptedHost:
async def main():
import rlm as rlm_module
- import rlm.factory as factory_module
from rlm.factory import FactoryExecutor
+ factory_module = importlib.import_module("rlm.factory")
+
rlm_module.host_request = ScriptedHost()
async def instant_sleep(_seconds):
diff --git a/prime-agent-runtime/test/test_harness.py b/prime-agent-runtime/test/test_harness.py
index c2694edfd4..0aae510ca9 100644
--- a/prime-agent-runtime/test/test_harness.py
+++ b/prime-agent-runtime/test/test_harness.py
@@ -1342,6 +1342,9 @@ class HarnessStateTest(unittest.TestCase):
reloaded = HarnessState(Path(temp_dir) / "harness_state.json")
self.assertEqual(reloaded.get("memory", "session_note").content, "persisted")
+ def test_dir_of_module_harness_lists_state_methods(self) -> None:
+ self.assertIn("delete_factory", dir(package_harness))
+
def test_module_harness_without_env_raises_on_local_writes_and_reads_work(self) -> None:
previous_local = os.environ.get("RLM_HARNESS_STATE_DIR")
previous_session = os.environ.get("RLM_SESSION_DIR")
@@ -1689,7 +1692,9 @@ class HarnessRefinementsSettingTests(unittest.TestCase):
def test_disabled_refinements_refuse_factory_runs_before_launch(self):
import asyncio
- from rlm import factory
+ import importlib
+
+ factory = importlib.import_module("rlm.factory")
with tempfile.TemporaryDirectory() as tmp, patch.dict(os.environ, {"PRIME_AGENT_CODING_AGENT_DIR": tmp}):
settings = Path(tmp) / "settings.json"
diff --git a/prime-agent-runtime/test/test_subagent_registry.py b/prime-agent-runtime/test/test_subagent_registry.py
index cd399a0d97..01561c6a4e 100644
--- a/prime-agent-runtime/test/test_subagent_registry.py
+++ b/prime-agent-runtime/test/test_subagent_registry.py
@@ -2,6 +2,7 @@ from __future__ import annotations
import asyncio
import importlib
+import inspect
import unittest
from pathlib import Path
from unittest.mock import AsyncMock, patch
@@ -110,6 +111,14 @@ class RlmSubagentRegistryTest(unittest.TestCase):
target.run
self.assertFalse(hasattr(target, "run"))
+ def test_package_module_resolves_namespace_attributes(self) -> None:
+ self.assertIs(rlm_module.factory, rlm_module.rlm.factory)
+ self.assertTrue(inspect.iscoroutinefunction(rlm_module.factory.run))
+ self.assertEqual(rlm_module.factory.help(), importlib.import_module("rlm.factory").FACTORY_HELP)
+ session = object()
+ with patch.object(type(rlm_module.rlm), "session", session, create=True):
+ self.assertIs(rlm_module.session, session)
+
def test_finds_authenticated_models_through_host(self) -> None:
host_request = AsyncMock(
return_value={