Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(imports): break safety config cycle

Ruben Lütke8f4798c58751
diff --git a/jarvis/brain/__init__.py b/jarvis/brain/__init__.py
index 9cb95efc8f..52e64819f5 100644
--- a/jarvis/brain/__init__.py
+++ b/jarvis/brain/__init__.py
@@ -1,13 +1,14 @@
-"""Brain-Layer: Provider-Manager, Dispatcher, Tool-Use-Loop."""
+"""Lazy public exports for the brain layer.
+
+Configuration imports the lightweight ``brain.ack_brain.config`` module while
+the safety layer may still be initializing. Eagerly importing the dispatcher,
+manager, and tool loop here creates a safety/config/brain cycle, so public
+objects are resolved only when callers request them.
+"""
from __future__ import annotations
-from .cache_heartbeat import CacheHeartbeat
-from .dispatcher import BrainDispatcher
-from .iteration_budget import IterationBudget
-from .manager import BrainManager
-from .provider_registry import BrainProviderRegistry
-from .streaming import StreamingAggregate, aggregate, tee_text
-from .tool_use_loop import ToolUseLoop
+from importlib import import_module
+from typing import Any
__all__ = [
"BrainDispatcher",
@@ -20,3 +21,25 @@ __all__ = [
"aggregate",
"tee_text",
]
+
+_EXPORTS: dict[str, tuple[str, str]] = {
+ "BrainDispatcher": (".dispatcher", "BrainDispatcher"),
+ "BrainManager": (".manager", "BrainManager"),
+ "BrainProviderRegistry": (".provider_registry", "BrainProviderRegistry"),
+ "CacheHeartbeat": (".cache_heartbeat", "CacheHeartbeat"),
+ "IterationBudget": (".iteration_budget", "IterationBudget"),
+ "StreamingAggregate": (".streaming", "StreamingAggregate"),
+ "ToolUseLoop": (".tool_use_loop", "ToolUseLoop"),
+ "aggregate": (".streaming", "aggregate"),
+ "tee_text": (".streaming", "tee_text"),
+}
+
+
+def __getattr__(name: str) -> Any:
+ target = _EXPORTS.get(name)
+ if target is None:
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
+ module_name, attribute = target
+ value = getattr(import_module(module_name, __name__), attribute)
+ globals()[name] = value
+ return value
diff --git a/jarvis/safety/__init__.py b/jarvis/safety/__init__.py
index 02d66aa8be..0c563e366b 100644
--- a/jarvis/safety/__init__.py
+++ b/jarvis/safety/__init__.py
@@ -1,9 +1,18 @@
-"""Safety-Layer: Risk-Tier-Eval, Approval-Workflow, Tool-Executor."""
+"""Lazy public exports for the safety layer.
+
+Keeping the package initializer import-free prevents an import-order cycle:
+``approval -> safety.__init__ -> risk_tier -> config -> brain -> dispatcher ->
+tool_executor -> risk_tier``. Concrete modules load only when their public name
+is requested, while ``from jarvis.safety import ToolExecutor`` remains stable.
+"""
from __future__ import annotations
-from .approval import ApprovalWorkflow
-from .risk_tier import ActionBlocked, RiskTierEvaluator, TierDecision
-from .tool_executor import ToolExecutor
+from typing import TYPE_CHECKING, Any
+
+if TYPE_CHECKING:
+ from .approval import ApprovalWorkflow
+ from .risk_tier import ActionBlocked, RiskTierEvaluator, TierDecision
+ from .tool_executor import ToolExecutor
__all__ = [
"ActionBlocked",
@@ -12,3 +21,23 @@ __all__ = [
"TierDecision",
"ToolExecutor",
]
+
+
+def __getattr__(name: str) -> Any:
+ if name == "ApprovalWorkflow":
+ from .approval import ApprovalWorkflow
+
+ return ApprovalWorkflow
+ if name in {"ActionBlocked", "RiskTierEvaluator", "TierDecision"}:
+ from .risk_tier import ActionBlocked, RiskTierEvaluator, TierDecision
+
+ return {
+ "ActionBlocked": ActionBlocked,
+ "RiskTierEvaluator": RiskTierEvaluator,
+ "TierDecision": TierDecision,
+ }[name]
+ if name == "ToolExecutor":
+ from .tool_executor import ToolExecutor
+
+ return ToolExecutor
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
diff --git a/tests/unit/safety/test_import_order.py b/tests/unit/safety/test_import_order.py
new file mode 100644
index 0000000000..38f3e197e8
--- /dev/null
+++ b/tests/unit/safety/test_import_order.py
@@ -0,0 +1,33 @@
+"""Safety modules remain importable in fresh processes in any public order."""
+from __future__ import annotations
+
+import subprocess
+import sys
+
+import pytest
+
+from jarvis.core.process_utils import NO_WINDOW_CREATIONFLAGS
+
+
+ "statement",
+ (
+ "import jarvis.safety.approval; import jarvis.safety.tool_executor",
+ "import jarvis.core.config; from jarvis.safety import ToolExecutor",
+ "from jarvis.safety import ApprovalWorkflow; import jarvis.core.config",
+ "from jarvis.safety import ActionBlocked, RiskTierEvaluator, TierDecision",
+ ),
+)
+def test_safety_public_imports_are_order_independent(statement: str) -> None:
+ completed = subprocess.run(
+ [sys.executable, "-X", "utf8", "-c", statement],
+ capture_output=True,
+ text=True,
+ encoding="utf-8",
+ errors="replace",
+ timeout=20,
+ check=False,
+ creationflags=NO_WINDOW_CREATIONFLAGS,
+ )
+
+ assert completed.returncode == 0, completed.stderr