|
| 1 | +"""Integration test: verify the agent core works WITHOUT the cybernetic layer. |
| 2 | +
|
| 3 | +This test proves that the cybernetic subsystem is a truly optional extension — |
| 4 | +the core agent path (entry → agent_loop → tools → session) must function |
| 5 | +correctly even when every cybernetic import fails. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import importlib |
| 11 | +import sys |
| 12 | +from pathlib import Path |
| 13 | +from unittest.mock import MagicMock |
| 14 | + |
| 15 | +import pytest |
| 16 | + |
| 17 | +ROOT = Path(__file__).resolve().parent.parent |
| 18 | + |
| 19 | + |
| 20 | +class _BlockCybernetic: |
| 21 | + """Context manager that makes all minicode.cybernetic_* modules unimportable.""" |
| 22 | + |
| 23 | + CYBERNETIC_PREFIXES = ( |
| 24 | + "minicode.cybernetic_", |
| 25 | + "minicode.feedback_controller", |
| 26 | + "minicode.feedforward_controller", |
| 27 | + "minicode.predictive_controller", |
| 28 | + "minicode.decoupling_controller", |
| 29 | + "minicode.adaptive_pid_tuner", |
| 30 | + "minicode.state_observer", |
| 31 | + "minicode.progress_controller", |
| 32 | + "minicode.stability_monitor", |
| 33 | + "minicode.self_healing_engine", |
| 34 | + "minicode.verification_controller", |
| 35 | + "minicode.decision_audit", |
| 36 | + ) |
| 37 | + |
| 38 | + # Non-cybernetic modules that lazy-import cybernetic — must be cleared too |
| 39 | + CASCADE_MODULES = ( |
| 40 | + "minicode.agent_loop", |
| 41 | + "minicode.tty_app", |
| 42 | + ) |
| 43 | + |
| 44 | + def __init__(self): |
| 45 | + self._blocked: dict[str, object] = {} |
| 46 | + |
| 47 | + def __enter__(self): |
| 48 | + # Remove cybernetic modules |
| 49 | + for key in list(sys.modules): |
| 50 | + for prefix in self.CYBERNETIC_PREFIXES: |
| 51 | + if key.startswith(prefix): |
| 52 | + self._blocked[key] = sys.modules.pop(key) |
| 53 | + # Remove cascading modules so they re-import cleanly |
| 54 | + for key in self.CASCADE_MODULES: |
| 55 | + if key in sys.modules: |
| 56 | + self._blocked[key] = sys.modules.pop(key) |
| 57 | + return self |
| 58 | + |
| 59 | + def __exit__(self, *args): |
| 60 | + # Restore blocked modules |
| 61 | + sys.modules.update(self._blocked) |
| 62 | + |
| 63 | + |
| 64 | +def test_core_agent_loop_imports_without_cybernetic(): |
| 65 | + """Agent loop must be importable even when cybernetic modules are absent.""" |
| 66 | + with _BlockCybernetic(): |
| 67 | + # Force re-import |
| 68 | + if "minicode.agent_loop" in sys.modules: |
| 69 | + del sys.modules["minicode.agent_loop"] |
| 70 | + # Should not raise |
| 71 | + from minicode.agent_loop import run_agent_turn # noqa: F401 |
| 72 | + assert True # reached = success |
| 73 | + |
| 74 | + |
| 75 | +def test_core_tooling_works_without_cybernetic(): |
| 76 | + """ToolResult must work without any cybernetic imports.""" |
| 77 | + from minicode.tooling import ToolResult |
| 78 | + result = ToolResult(ok=True, output="test ok") |
| 79 | + assert result.ok is True |
| 80 | + |
| 81 | + |
| 82 | +def test_core_session_works_without_cybernetic(tmp_path): |
| 83 | + """Session persistence must work without cybernetic modules.""" |
| 84 | + from minicode.session import SessionData, save_session |
| 85 | + sd = SessionData(session_id="test", created_at=0.0, updated_at=0.0, workspace=str(tmp_path)) |
| 86 | + save_session(sd) |
| 87 | + assert sd.session_id == "test" |
| 88 | + |
| 89 | + |
| 90 | +def test_core_context_manager_without_cybernetic(): |
| 91 | + """ContextManager + token estimation must work without cybernetic.""" |
| 92 | + from minicode.context_manager import estimate_message_tokens |
| 93 | + tokens = estimate_message_tokens({"role": "user", "content": "Hello world"}) |
| 94 | + assert isinstance(tokens, int) |
| 95 | + assert tokens > 0 |
| 96 | + |
| 97 | + |
| 98 | +def test_core_config_without_cybernetic(): |
| 99 | + """Config loading must work without cybernetic.""" |
| 100 | + from minicode.config import load_runtime_config |
| 101 | + config = load_runtime_config(".", trust_project_mcp=False) |
| 102 | + assert isinstance(config, dict) |
| 103 | + |
| 104 | + |
| 105 | +def test_core_memory_without_cybernetic(tmp_path): |
| 106 | + """MemoryManager must work without cybernetic.""" |
| 107 | + from minicode.memory import MemoryManager, MemoryScope |
| 108 | + mgr = MemoryManager(project_root=tmp_path) |
| 109 | + mgr.add_entry(MemoryScope.PROJECT, "test", "hello", ["tag"]) |
| 110 | + results = mgr.search("hello") |
| 111 | + assert len(results) >= 1 |
0 commit comments