test(cli): lock disabled dream cursor advancement

This commit is contained in:
Xubin Ren 2026-06-25 16:30:34 +08:00
parent f7b027a295
commit 34f776b48b
2 changed files with 21 additions and 3 deletions

View File

@ -154,6 +154,12 @@ def _install_gateway_shutdown_handlers(
return restore return restore
def _advance_dream_cursor_if_behind(memory: Any) -> None:
latest = memory.get_latest_cursor()
if memory.get_last_dream_cursor() < latest:
memory.set_last_dream_cursor(latest)
class SafeFileHistory(FileHistory): class SafeFileHistory(FileHistory):
"""FileHistory subclass that sanitizes surrogate characters on write. """FileHistory subclass that sanitizes surrogate characters on write.
@ -1165,9 +1171,7 @@ def _run_gateway(
console.print(f"[green]✓[/green] Dream: {dream_cfg.describe_schedule()}") console.print(f"[green]✓[/green] Dream: {dream_cfg.describe_schedule()}")
else: else:
console.print("[yellow]○[/yellow] Dream: disabled") console.print("[yellow]○[/yellow] Dream: disabled")
latest = agent.context.memory.get_latest_cursor() _advance_dream_cursor_if_behind(agent.context.memory)
if agent.context.memory.get_last_dream_cursor() < latest:
agent.context.memory.set_last_dream_cursor(latest)
# Register Heartbeat system job (idempotent on restart) # Register Heartbeat system job (idempotent on restart)
if hb_cfg.enabled: if hb_cfg.enabled:

View File

@ -10,6 +10,7 @@ from unittest.mock import AsyncMock, MagicMock, patch
import pytest import pytest
from typer.testing import CliRunner from typer.testing import CliRunner
from nanobot.agent.memory import MemoryStore
from nanobot.bus.events import InboundMessage, OutboundMessage from nanobot.bus.events import InboundMessage, OutboundMessage
from nanobot.cli import commands as cli_commands from nanobot.cli import commands as cli_commands
from nanobot.cli.commands import app from nanobot.cli.commands import app
@ -140,6 +141,19 @@ def test_gateway_tty_signal_mode_restores_ctrl_c(monkeypatch) -> None:
os.close(slave_fd) os.close(slave_fd)
def test_disabled_dream_cursor_only_advances_when_behind(tmp_path) -> None:
store = MemoryStore(tmp_path)
store.append_history("first")
store.append_history("second")
cli_commands._advance_dream_cursor_if_behind(store)
assert store.get_last_dream_cursor() == 2
store.set_last_dream_cursor(10)
cli_commands._advance_dream_cursor_if_behind(store)
assert store.get_last_dream_cursor() == 10
@pytest.fixture @pytest.fixture
def mock_paths(): def mock_paths():
"""Mock config/workspace paths for test isolation.""" """Mock config/workspace paths for test isolation."""