Skip to content

"Vibe-Trading: Your Personal Trading Agent"

18.5 KB
Raw Blame
"""Durable task records and the restart reconciliation that must never re-run."""

from __future__ import annotations

import json
import os
import stat
import time
from pathlib import Path
from types import SimpleNamespace

import pytest

from src.tools import background_store as store_mod
from src.tools.background_store import (
    SCHEMA_VERSION,
    BackgroundStoreError,
    BackgroundTaskStore,
    TaskRecord,
    record_from_task,
    reconcile_after_restart,
)


def make(**overrides) -> TaskRecord:
    values = dict(
        schema_version=SCHEMA_VERSION,
        task_id="deadbeef",
        owner_id="session:s1",
        status="completed",
        command_preview="echo hi",
        exit_code=0,
        result_preview="hi",
    )
    values.update(overrides)
    return TaskRecord(**values)


class TestRecoveryIsConservative:
    @pytest.fixture(autouse=True)
    def forbid_side_effects(self, monkeypatch):
        """Recovery must not spawn, signal, fetch or call a model."""

        def explode(*args, **kwargs):
            raise AssertionError(f"recovery must not call {kwargs or args}")

        from tests.module_os_helpers import patch_module_os

        signals = {name: explode for name in ("kill", "killpg") if hasattr(os, name)}
        patch_module_os(monkeypatch, store_mod, **signals)
        monkeypatch.setattr(store_mod, "time", SimpleNamespace(time=time.time, sleep=explode))

    def test_running_becomes_interrupted_without_rerun(self):
        recovered, unchanged = reconcile_after_restart([make(status="running")])
        assert unchanged == []
        assert recovered[0].status == "interrupted"
        assert recovered[0].recovery_reason == "service_restart"
        assert recovered[0].command_preview == "echo hi"

    def test_cancelling_stays_outcome_unknown(self):
        recovered, _ = reconcile_after_restart([make(status="cancelling")])
        assert recovered[0].status == "outcome_unknown"
        assert recovered[0].recovery_reason == "service_restart_before_reap_completed"

    @pytest.mark.parametrize("status", ["completed", "error", "timeout", "cancelled"])
    def test_terminal_outcomes_are_kept_queryable(self, status):
        recovered, unchanged = reconcile_after_restart([make(status=status, exit_code=3)])
        assert recovered == []
        assert unchanged[0].status == status
        assert unchanged[0].exit_code == 3

    def test_recovery_is_idempotent(self):
        once, _ = reconcile_after_restart([make(status="running")])
        twice, unchanged = reconcile_after_restart(once)
        assert twice == []
        assert unchanged[0].status == "interrupted"

    def test_stored_pid_is_descriptive_only(self):
        record = make(status="running", pid=os.getpid())
        recovered, _ = reconcile_after_restart([record])
        assert recovered[0].pid == os.getpid()
        # No signal path reads it; nothing above touches os.kill.


class TestStore:
    def test_round_trip_and_layout_is_stable_per_owner(self, tmp_path):
        store = BackgroundTaskStore(tmp_path)
        record = make()
        path = store.save(record)
        assert path.parent == tmp_path / "background-tasks"
        assert path.name == path.name  # deterministic
        assert store.save(record).name == path.name
        records, unreadable = store.load_all()
        assert unreadable == []
        assert records == [record]

    def test_owner_id_is_not_a_filename(self, tmp_path):
        store = BackgroundTaskStore(tmp_path)
        path = store.save(make(owner_id="session:../escape"))
        assert ".." not in path.name
        assert path.parent == tmp_path / "background-tasks"

    def test_unsafe_task_id_is_rejected(self, tmp_path):
        store = BackgroundTaskStore(tmp_path)
        with pytest.raises(BackgroundStoreError):
            store.save(make(task_id="../../etc/passwd"))

    def test_private_permissions(self, tmp_path):
        store = BackgroundTaskStore(tmp_path)
        path = store.save(make())
        assert stat.S_IMODE(path.stat().st_mode) == 0o600
        assert stat.S_IMODE(path.parent.stat().st_mode) == 0o700

    def test_unknown_version_is_reported_not_overwritten(self, tmp_path):
        store = BackgroundTaskStore(tmp_path)
        store.save(make())
        for path in (tmp_path / "background-tasks").glob("*.json"):
            payload = json.loads(path.read_text())
            payload["schema_version"] = SCHEMA_VERSION + 1
            path.write_text(json.dumps(payload))
        records, unreadable = store.load_all()
        assert records == []
        assert len(unreadable) == 1
        assert json.loads(Path(unreadable[0]).read_text())["schema_version"] == SCHEMA_VERSION + 1

    def test_half_written_file_is_reported_not_invented(self, tmp_path):
        store = BackgroundTaskStore(tmp_path)
        directory = tmp_path / "background-tasks"
        directory.mkdir()
        (directory / "deadbeef.json").write_text('{"schema_version": 1, "task_id"')
        records, unreadable = store.load_all()
        assert records == []
        assert len(unreadable) == 1

    def test_symlink_is_refused(self, tmp_path):
        store = BackgroundTaskStore(tmp_path)
        directory = tmp_path / "background-tasks"
        directory.mkdir()
        outside = tmp_path / "outside.json"
        outside.write_text(json.dumps(__import__("dataclasses").asdict(make(task_id="deadbeef"))))
        link = store._path_for(make(task_id="deadbeef"))
        link.symlink_to(outside)
        _records, unreadable = store.load_all()
        assert len(unreadable) == 1

    def test_previews_are_bounded_and_redacted(self, tmp_path):
        record = make(
            command_preview=store_mod._preview(" ".join(["x" * 100] * 50)),
            result_preview=store_mod._preview(f"token sk-{'a' * 40} end"),
        )
        assert len(record.command_preview) <= store_mod.MAX_PREVIEW_CHARS
        assert len(record.result_preview) <= store_mod.MAX_PREVIEW_CHARS
        assert f"sk-{'a' * 40}" not in record.result_preview

    def test_two_local_processes_can_share_the_directory(self, tmp_path):
        """One process's records stay readable after another writes beside them."""
        first = BackgroundTaskStore(tmp_path)
        second = BackgroundTaskStore(tmp_path)
        first.save(make(task_id="aaaaaaaa", owner_id="session:a"))
        second.save(make(task_id="bbbbbbbb", owner_id="session:b"))
        records, unreadable = first.load_all()
        assert unreadable == []
        assert {r.task_id for r in records} == {"aaaaaaaa", "bbbbbbbb"}

    def test_recovery_survives_a_process_that_never_wrote(self, tmp_path):
        """A crash before the first save leaves no record, and none is invented."""
        store = BackgroundTaskStore(tmp_path)
        assert store.load_all() == ([], [])
        recovered, unchanged = reconcile_after_restart([])
        assert recovered == [] and unchanged == []

    def test_record_from_task_projects_the_live_shape(self):
        task = {
            "status": "running",
            "owner_id": "session:s1",
            "attempt_id": "a1",
            "command": "echo hi",
            "result": None,
            "exit_code": None,
            "started_at": 10.0,
            "duration_seconds": 2.5,
        }
        record = record_from_task("deadbeef", task)
        assert record.owner_id == "session:s1"
        assert record.attempt_id == "a1"
        assert record.status == "running"
        assert record.started_at == 10.0
        assert record.duration_seconds == 2.5
        assert record.is_live and not record.is_terminal


class TestManagerWiring:
    def test_a_completed_run_leaves_a_queryable_record(self, tmp_path, monkeypatch):
        import json as _json
        import shlex
        import sys as _sys

        from src.tools import background_tools as bg

        monkeypatch.setattr(bg, "WORKDIR", tmp_path)
        manager = bg.BackgroundManager(bg.BackgroundScope("session:s1"), store=BackgroundTaskStore(tmp_path))
        command = shlex.join([_sys.executable, "-c", "print('stored')"])
        task_id = _json.loads(manager.run(command))["task_id"]
        deadline = time.monotonic() + 15
        while time.monotonic() < deadline and _json.loads(manager.check(task_id))["status"] == "running":
            time.sleep(0.01)
        # The in-memory status flips inside the lock while the best-effort write
        # happens after it, so reading the store the moment check() moves on
        # races the persist. Wait for the record to land.
        records: list = []
        unreadable: list = []
        while time.monotonic() < deadline:
            records, unreadable = manager._store.load_all()
            if records and records[0].status != "running":
                break
            time.sleep(0.01)
        assert unreadable == []
        assert records[0].task_id == task_id
        assert records[0].status == "completed"
        assert records[0].attempt_id is None
        assert records[0].command_preview
        assert records[0].notification_pending is True

        assert [item["task_id"] for item in manager.drain_notifications()] == [task_id]
        records, unreadable = manager._store.load_all()
        assert unreadable == []
        assert records[0].notification_pending is False

    def test_restart_recovery_leaves_this_processes_live_tasks_alone(self, tmp_path):
        from src.tools import background_tools as bg

        store = BackgroundTaskStore(tmp_path)
        live = make(task_id="aaaaaaaa", status="running", owner_id="session:live")
        stale = make(task_id="bbbbbbbb", status="running", owner_id="session:stale")
        store.save(live)
        store.save(stale)
        bg._LIVE_TASK_IDS.add("aaaaaaaa")
        try:
            first = BackgroundTaskStore(tmp_path)
            manager = bg.BackgroundManager(bg.BackgroundScope("session:x"), store=first)
            summary = manager.recover_on_start()
        finally:
            bg._LIVE_TASK_IDS.discard("aaaaaaaa")
        assert summary["recovered"] == 1
        records, _ = first.load_all()
        by_id = {record.task_id: record for record in records}
        assert by_id["aaaaaaaa"].status == "running"
        assert by_id["bbbbbbbb"].status == "interrupted"

    def test_recovery_does_not_rerun_a_previous_commands(self, tmp_path, monkeypatch):
        from src.tools import background_tools as bg

        monkeypatch.setattr(bg, "WORKDIR", tmp_path)
        store = BackgroundTaskStore(tmp_path)
        store.save(make(task_id="cccccccc", status="running", command_preview="touch should-not-exist"))
        manager = bg.BackgroundManager(bg.BackgroundScope("session:x"), store=store)
        assert manager.recover_on_start()["recovered"] == 1
        assert not (tmp_path / "should-not-exist").exists()



def test_recovered_task_is_queryable_by_owner_not_other_sessions(tmp_path):
    from src.tools import background_tools as bg
    store = BackgroundTaskStore(tmp_path)
    store.save(make(task_id="abcdef12", owner_id="session:a", status="running"))
    manager = bg.BackgroundManager(bg.BackgroundScope("session:a"), store=store)
    manager.recover_on_start()
    result = json.loads(manager.check("abcdef12"))
    assert result["status"] == "interrupted"
    assert result["recovery_reason"] == "service_restart"
    assert "abcdef12" in manager.check()
    sibling = manager.for_scope(bg.BackgroundScope("session:b"))
    assert json.loads(sibling.check("abcdef12"))["status"] == "error"


def test_runtime_roots_keep_their_records_and_tables_separate(tmp_path):
    from src.tools import background_tools as bg
    first = bg.get_background_manager(tmp_path / "a").for_scope(bg.BackgroundScope("session:a"))
    second = bg.get_background_manager(tmp_path / "b").for_scope(bg.BackgroundScope("session:a"))
    first.tasks["deadbeef"] = {"owner_id": "session:a", "status": "completed", "command": "echo done", "result": "done"}
    first._persist("deadbeef")
    assert json.loads(second.check("deadbeef"))["status"] == "error"
    assert second._store.load_all() == ([], [])


def test_cancel_intent_is_persisted_before_reaping(tmp_path, monkeypatch):
    from src.tools import background_tools as bg
    store = BackgroundTaskStore(tmp_path)
    manager = bg.BackgroundManager(bg.BackgroundScope("session:a"), store=store)
    manager.tasks["deadbeef"] = {"owner_id": "session:a", "status": "running", "command": "echo done", "process": None}
    manager.cancel("deadbeef")
    records, unreadable = store.load_all()
    assert not unreadable
    assert records[0].status == "cancelling"
    recovered, _ = reconcile_after_restart(records)
    assert recovered[0].status == "outcome_unknown"


def test_pending_terminal_notification_is_restored_once_and_acknowledged(tmp_path):
    from src.tools import background_tools as bg

    store = BackgroundTaskStore(tmp_path)
    store.save(make(task_id="eeeeeeee", owner_id="session:a", status="completed",
                    result_preview="finished", notification_pending=True))
    first = bg.BackgroundManager(bg.BackgroundScope("session:a"), store=store)
    first.recover_on_start()

    other = first.for_scope(bg.BackgroundScope("session:b"))
    assert other.drain_notifications() == []
    assert first.drain_notifications() == [{
        "task_id": "eeeeeeee", "status": "completed", "command": "echo hi",
        "result": "finished", "exit_code": 0,
    }]
    records, unreadable = store.load_all()
    assert unreadable == [] and records[0].notification_pending is False

    restarted = bg.BackgroundManager(bg.BackgroundScope("session:a"), store=store)
    restarted.recover_on_start()
    assert restarted.drain_notifications() == []


def test_interrupted_task_produces_a_recovered_notification(tmp_path):
    from src.tools import background_tools as bg

    store = BackgroundTaskStore(tmp_path)
    store.save(make(task_id="ffffffff", owner_id="session:a", status="running",
                    notification_pending=True))
    manager = bg.BackgroundManager(bg.BackgroundScope("session:a"), store=store)
    manager.recover_on_start()

    notices = manager.drain_notifications()
    assert len(notices) == 1
    assert notices[0]["task_id"] == "ffffffff"
    assert notices[0]["status"] == "interrupted"


def test_failed_delivery_ack_is_retried_after_another_restart(tmp_path):
    from src.tools import background_tools as bg

    class FailingAckStore(BackgroundTaskStore):
        def save(self, record):
            if record.notification_pending is False:
                raise OSError("disk full during delivery acknowledgement")
            return super().save(record)

    store = FailingAckStore(tmp_path)
    store.save(make(task_id="aaaaaaa1", owner_id="session:a", status="completed",
                    result_preview="deliver me", notification_pending=True))
    first = bg.BackgroundManager(bg.BackgroundScope("session:a"), store=store)
    first.recover_on_start()
    assert len(first.drain_notifications()) == 1

    restarted = bg.BackgroundManager(bg.BackgroundScope("session:a"), store=store)
    restarted.recover_on_start()
    assert [item["result"] for item in restarted.drain_notifications()] == ["deliver me"]


def test_v1_terminal_record_does_not_invent_a_duplicate_notification(tmp_path):
    from dataclasses import asdict
    from src.tools import background_tools as bg

    store = BackgroundTaskStore(tmp_path)
    legacy = make(task_id="bbbbbbb1", owner_id="session:a", status="completed")
    payload = asdict(legacy)
    payload["schema_version"] = 1
    payload.pop("notification_pending", None)
    store._write_json_atomically(store._path_for(legacy), payload)

    manager = bg.BackgroundManager(bg.BackgroundScope("session:a"), store=store)
    summary = manager.recover_on_start()
    assert summary["unchanged"] == 1
    assert json.loads(manager.check("bbbbbbb1"))["status"] == "completed"
    assert manager.drain_notifications() == []


def test_recovered_notifications_precede_new_work_and_recovery_is_idempotent(tmp_path):
    from src.tools import background_tools as bg

    store = BackgroundTaskStore(tmp_path)
    store.save(make(task_id="ccccccc1", owner_id="session:a", status="completed",
                    notification_pending=True, ended_at=1.0))
    manager = bg.BackgroundManager(bg.BackgroundScope("session:a"), store=store)
    manager._notifications.append(("session:a", {
        "task_id": "new-work", "status": "completed", "command": "new",
        "result": "new result", "exit_code": 0,
    }))

    manager.recover_on_start()
    manager.recover_on_start()

    notices = manager.drain_notifications()
    assert [item["task_id"] for item in notices] == ["ccccccc1", "new-work"]


def test_v1_live_record_is_migrated_when_recovered(tmp_path):
    from dataclasses import asdict
    from src.tools import background_tools as bg

    store = BackgroundTaskStore(tmp_path)
    legacy = make(task_id="ddddddd1", owner_id="session:a", status="running")
    payload = asdict(legacy)
    payload["schema_version"] = 1
    payload.pop("notification_pending", None)
    store._write_json_atomically(store._path_for(legacy), payload)

    manager = bg.BackgroundManager(bg.BackgroundScope("session:a"), store=store)
    manager.recover_on_start()

    records, unreadable = store.load_all()
    assert unreadable == []
    assert records[0].schema_version == SCHEMA_VERSION
    assert records[0].status == "interrupted"
    assert records[0].notification_pending is True
    assert len(manager.drain_notifications()) == 1


def test_recovered_notification_preserves_live_payload_limits(tmp_path):
    from src.tools import background_tools as bg

    store = BackgroundTaskStore(tmp_path)
    store.save(make(task_id="eeeeeee1", owner_id="session:a", status="completed",
                    command_preview="c" * 200, result_preview="r" * 2000,
                    notification_pending=True))
    manager = bg.BackgroundManager(bg.BackgroundScope("session:a"), store=store)
    manager.recover_on_start()

    notice, = manager.drain_notifications()
    assert notice["command"] == "c" * 80
    assert notice["result"] == "r" * 500


@pytest.mark.parametrize("version", [[], {}, True, None, "2"])
def test_corrupt_version_does_not_abort_other_task_recovery(tmp_path, version):
    store = BackgroundTaskStore(tmp_path)
    store.save(make(task_id="aaaaaaaa", notification_pending=True))
    broken = store._directory / "broken.json"
    broken.write_text(json.dumps({"schema_version": version}))
    from src.tools.background_tools import BackgroundManager, BackgroundScope

    manager = BackgroundManager(BackgroundScope("session:s1"), store=store)
    report = manager.recover_on_start()
    assert report["unreadable"] == [str(broken)]
    assert [notice["task_id"] for notice in manager.drain_notifications()] == ["aaaaaaaa"]