|
| 1 | +"""During daemon warmup the fallback must keep working WITH a note — the |
| 2 | +agent should know priming/recall results are provisional, and `_daemon.status` |
| 3 | +is the single source of that truth for every consumer.""" |
| 4 | + |
| 5 | +import json |
| 6 | +import os |
| 7 | +import socket |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from ultan import _daemon, _hooks |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture() |
| 16 | +def home(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path: |
| 17 | + h = tmp_path / "agent-mem" |
| 18 | + h.mkdir() |
| 19 | + monkeypatch.setenv("AGENT_MEM_HOME", str(h)) |
| 20 | + return h |
| 21 | + |
| 22 | + |
| 23 | +# ── _daemon.status() ────────────────────────────────────────────────── |
| 24 | + |
| 25 | + |
| 26 | +def test_status_down_with_no_signals(home: Path) -> None: |
| 27 | + assert _daemon.status() == "down" |
| 28 | + |
| 29 | + |
| 30 | +def test_status_warming_from_state_flag_with_live_pid(home: Path) -> None: |
| 31 | + (home / "daemon.state").write_text( |
| 32 | + json.dumps({"phase": "warming", "pid": os.getpid(), "since": "x"}), |
| 33 | + encoding="utf-8", |
| 34 | + ) |
| 35 | + assert _daemon.status() == "warming" |
| 36 | + |
| 37 | + |
| 38 | +def test_status_ignores_stale_flag_with_dead_pid(home: Path) -> None: |
| 39 | + (home / "daemon.state").write_text( |
| 40 | + json.dumps({"phase": "warming", "pid": 99999999, "since": "x"}), |
| 41 | + encoding="utf-8", |
| 42 | + ) |
| 43 | + assert _daemon.status() == "down" |
| 44 | + |
| 45 | + |
| 46 | +def test_status_warming_from_fresh_spawn_stamp(home: Path) -> None: |
| 47 | + (home / ".daemon-spawn-attempt").write_text("x", encoding="utf-8") |
| 48 | + assert _daemon.status() == "warming" |
| 49 | + |
| 50 | + |
| 51 | +def test_status_ready_when_socket_answers(monkeypatch: pytest.MonkeyPatch) -> None: |
| 52 | + # AF_UNIX paths cap at ~104 bytes on macOS; pytest tmp dirs are too deep, |
| 53 | + # so bind in a short mkdtemp under /tmp instead. |
| 54 | + import shutil |
| 55 | + import tempfile |
| 56 | + |
| 57 | + short_home = Path(tempfile.mkdtemp(prefix="ultan-t-", dir="/tmp")) |
| 58 | + monkeypatch.setenv("AGENT_MEM_HOME", str(short_home)) |
| 59 | + srv = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 60 | + try: |
| 61 | + srv.bind(str(short_home / "priming.sock")) |
| 62 | + srv.listen(1) |
| 63 | + assert _daemon.status() == "ready" |
| 64 | + finally: |
| 65 | + srv.close() |
| 66 | + shutil.rmtree(short_home, ignore_errors=True) |
| 67 | + |
| 68 | + |
| 69 | +# ── hook priming carries the warming note ───────────────────────────── |
| 70 | + |
| 71 | + |
| 72 | +def _dispatch_priming( |
| 73 | + monkeypatch: pytest.MonkeyPatch, |
| 74 | + capsys: pytest.CaptureFixture[str], |
| 75 | + status: str, |
| 76 | +) -> str: |
| 77 | + monkeypatch.setattr(_hooks._daemon, "ensure_running", lambda: True) |
| 78 | + monkeypatch.setattr(_hooks._daemon, "status", lambda: status) |
| 79 | + monkeypatch.setattr(_hooks._priming, "get_priming", lambda *a, **k: "## bullets\n") |
| 80 | + rc = _hooks.dispatch("user-prompt-submit", {"session_id": "s", "prompt": "q"}) |
| 81 | + assert rc == 0 |
| 82 | + out = capsys.readouterr().out |
| 83 | + data = json.loads(out) |
| 84 | + context: str = data["hookSpecificOutput"]["additionalContext"] |
| 85 | + return context |
| 86 | + |
| 87 | + |
| 88 | +def test_priming_notes_lexical_fallback_while_warming( |
| 89 | + home: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str] |
| 90 | +) -> None: |
| 91 | + context = _dispatch_priming(monkeypatch, capsys, "warming") |
| 92 | + assert "## bullets" in context # the fallback still delivers |
| 93 | + assert "warming up" in context # ...but says so |
| 94 | + |
| 95 | + |
| 96 | +def test_priming_carries_no_note_when_ready( |
| 97 | + home: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str] |
| 98 | +) -> None: |
| 99 | + context = _dispatch_priming(monkeypatch, capsys, "ready") |
| 100 | + assert "## bullets" in context |
| 101 | + assert "warming" not in context |
0 commit comments