Skip to content

Your lightweight open-source personal agent.

17.7 KB
Raw Blame
import json
from datetime import datetime, timedelta, timezone
from types import SimpleNamespace
from zoneinfo import ZoneInfo

import pytest

from nanobot.agent.tools.context import RequestContext, request_context
from nanobot.agent.tools.cron import CronTool
from nanobot.cron.service import CronService
from nanobot.cron.types import CronJob, CronJobState, CronPayload, CronSchedule


def _make_tool(tmp_path) -> CronTool:
    service = CronService(tmp_path / "cron" / "jobs.json")
    return CronTool(service)


def _make_tool_with_tz(tmp_path, tz: str) -> CronTool:
    service = CronService(tmp_path / "cron" / "jobs.json")
    return CronTool(service, default_timezone=tz)


def _bound_chat(chat_id: str = "chat-1") -> dict[str, str]:
    return {
        "session_key": f"websocket:{chat_id}",
        "origin_channel": "websocket",
        "origin_chat_id": chat_id,
    }


def test_format_timing_cron_with_tz(tmp_path) -> None:
    tool = _make_tool(tmp_path)
    s = CronSchedule(kind="cron", expr="0 9 * * 1-5", tz="America/Denver")
    assert tool._format_timing(s) == "cron: 0 9 * * 1-5 (America/Denver)"


def test_format_timing_cron_without_tz(tmp_path) -> None:
    tool = _make_tool(tmp_path)
    s = CronSchedule(kind="cron", expr="*/5 * * * *")
    assert tool._format_timing(s) == "cron: */5 * * * *"


@pytest.mark.parametrize(
    "expected, every_ms",
    [
        pytest.param("every 2h", 7200000, id="hours"),
        pytest.param("every 30m", 1800000, id="minutes"),
        pytest.param("every 30s", 30000, id="seconds"),
        pytest.param("every 90s", 90000, id="non_minute_seconds"),
        pytest.param("every 200ms", 200, id="milliseconds"),
    ],
)
def test_format_timing_every_interval(tmp_path, expected, every_ms) -> None:
    tool = _make_tool(tmp_path)
    s = CronSchedule(kind="every", every_ms=every_ms)
    assert tool._format_timing(s) == expected


def test_format_timing_at(tmp_path) -> None:
    tool = _make_tool_with_tz(tmp_path, "Asia/Shanghai")
    s = CronSchedule(kind="at", at_ms=1773684000000)
    result = tool._format_timing(s)
    assert "Asia/Shanghai" in result
    assert result.startswith("at 2026-")


def test_format_timing_fallback(tmp_path) -> None:
    tool = _make_tool(tmp_path)
    s = CronSchedule(kind="every")
    assert tool._format_timing(s) == "every"


def test_format_state_empty(tmp_path) -> None:
    tool = _make_tool(tmp_path)
    state = CronJobState()
    assert tool._format_state(state, CronSchedule(kind="every")) == []


def test_format_state_last_run_ok(tmp_path) -> None:
    tool = _make_tool(tmp_path)
    state = CronJobState(last_run_at_ms=1773673200000, last_status="ok")
    lines = tool._format_state(state, CronSchedule(kind="cron", expr="0 9 * * *", tz="UTC"))
    assert len(lines) == 1
    assert "Last run:" in lines[0]
    assert "ok" in lines[0]


def test_format_state_last_run_with_error(tmp_path) -> None:
    tool = _make_tool(tmp_path)
    state = CronJobState(last_run_at_ms=1773673200000, last_status="error", last_error="timeout")
    lines = tool._format_state(state, CronSchedule(kind="cron", expr="0 9 * * *", tz="UTC"))
    assert len(lines) == 1
    assert "error" in lines[0]
    assert "timeout" in lines[0]


def test_format_state_next_run_only(tmp_path) -> None:
    tool = _make_tool(tmp_path)
    state = CronJobState(next_run_at_ms=1773684000000)
    lines = tool._format_state(state, CronSchedule(kind="cron", expr="0 9 * * *", tz="UTC"))
    assert len(lines) == 1
    assert "Next run:" in lines[0]


def test_format_state_both(tmp_path) -> None:
    tool = _make_tool(tmp_path)
    state = CronJobState(
        last_run_at_ms=1773673200000, last_status="ok", next_run_at_ms=1773684000000
    )
    lines = tool._format_state(state, CronSchedule(kind="cron", expr="0 9 * * *", tz="UTC"))
    assert len(lines) == 2
    assert "Last run:" in lines[0]
    assert "Next run:" in lines[1]


def test_format_state_unknown_status(tmp_path) -> None:
    tool = _make_tool(tmp_path)
    state = CronJobState(last_run_at_ms=1773673200000, last_status=None)
    lines = tool._format_state(state, CronSchedule(kind="cron", expr="0 9 * * *", tz="UTC"))
    assert "unknown" in lines[0]


def test_list_empty(tmp_path) -> None:
    tool = _make_tool(tmp_path)
    assert tool._list_jobs() == "No scheduled jobs."


def test_list_cron_job_shows_expression_and_timezone(tmp_path) -> None:
    tool = _make_tool(tmp_path)
    tool._cron.add_job(
        name="Morning scan",
        schedule=CronSchedule(kind="cron", expr="0 9 * * 1-5", tz="America/Denver"),
        message="scan",
        **_bound_chat(),
    )
    result = tool._list_jobs()
    assert "cron: 0 9 * * 1-5 (America/Denver)" in result


@pytest.mark.parametrize(
    ("every_ms", "expected"),
    [
        pytest.param(1_800_000, "every 30m", id="minutes"),
        pytest.param(7_200_000, "every 2h", id="hours"),
        pytest.param(30_000, "every 30s", id="seconds"),
        pytest.param(90_000, "every 90s", id="non-minute-seconds"),
        pytest.param(200, "every 200ms", id="milliseconds"),
    ],
)
def test_list_every_job_shows_human_interval(tmp_path, every_ms, expected) -> None:
    tool = _make_tool(tmp_path)
    tool._cron.add_job(
        name="Frequent check",
        schedule=CronSchedule(kind="every", every_ms=every_ms),
        message="check",
        **_bound_chat(),
    )
    result = tool._list_jobs()
    assert expected in result


def test_list_at_job_shows_iso_timestamp(tmp_path) -> None:
    tool = _make_tool_with_tz(tmp_path, "Asia/Shanghai")
    tool._cron.add_job(
        name="One-shot",
        schedule=CronSchedule(kind="at", at_ms=1773684000000),
        message="fire",
        **_bound_chat(),
    )
    result = tool._list_jobs()
    assert "at 2026-" in result
    assert "Asia/Shanghai" in result


@pytest.mark.asyncio
async def test_list_shows_last_run_state(tmp_path) -> None:
    tool = _make_tool(tmp_path)
    tool._cron._running = True
    job = tool._cron.add_job(
        name="Stateful job",
        schedule=CronSchedule(kind="cron", expr="0 9 * * *", tz="UTC"),
        message="test",
        **_bound_chat(),
    )
    job.state.last_run_at_ms = 1773673200000
    job.state.last_status = "ok"
    tool._cron._save_store()

    result = tool._list_jobs()
    assert "Last run:" in result
    assert "ok" in result
    assert "(UTC)" in result

@pytest.mark.asyncio
async def test_list_shows_error_message(tmp_path) -> None:
    tool = _make_tool(tmp_path)
    tool._cron._running = True
    job = tool._cron.add_job(
        name="Failed job",
        schedule=CronSchedule(kind="cron", expr="0 9 * * *", tz="UTC"),
        message="test",
        **_bound_chat(),
    )
    job.state.last_run_at_ms = 1773673200000
    job.state.last_status = "error"
    job.state.last_error = "timeout"
    tool._cron._save_store()

    result = tool._list_jobs()
    assert "error" in result
    assert "timeout" in result


def test_list_shows_next_run(tmp_path) -> None:
    tool = _make_tool(tmp_path)
    tool._cron.add_job(
        name="Upcoming job",
        schedule=CronSchedule(kind="cron", expr="0 9 * * *", tz="UTC"),
        message="test",
        **_bound_chat(),
    )
    result = tool._list_jobs()
    assert "Next run:" in result
    assert "(UTC)" in result


def test_list_includes_protected_dream_system_job_with_memory_purpose(tmp_path) -> None:
    tool = _make_tool(tmp_path)
    tool._cron.register_system_job(CronJob(
        id="dream",
        name="dream",
        schedule=CronSchedule(kind="cron", expr="0 */2 * * *", tz="UTC"),
        payload=CronPayload(kind="system_event"),
    ))

    result = tool._list_jobs()

    assert "- dream (id: dream, cron: 0 */2 * * * (UTC))" in result
    assert "Dream memory consolidation for long-term memory." in result
    assert "cannot be removed" in result


def test_remove_protected_dream_job_returns_clear_feedback(tmp_path) -> None:
    tool = _make_tool(tmp_path)
    tool._cron.register_system_job(CronJob(
        id="dream",
        name="dream",
        schedule=CronSchedule(kind="cron", expr="0 */2 * * *", tz="UTC"),
        payload=CronPayload(kind="system_event"),
    ))

    result = tool._remove_job("dream")

    assert "Cannot remove job `dream`." in result
    assert "Dream memory consolidation job for long-term memory" in result
    assert "cannot be removed" in result
    assert tool._cron.get_job("dream") is not None


def test_add_cron_job_defaults_to_tool_timezone(tmp_path) -> None:
    tool = _make_tool_with_tz(tmp_path, "Asia/Shanghai")
    with request_context(
        RequestContext(channel="telegram", chat_id="chat-1", session_key="telegram:chat-1")
    ):
        result = tool._add_job(None, "Morning standup", None, "0 8 * * *", None, None)

    assert result.startswith("Created job")
    job = tool._cron.list_jobs()[0]
    assert job.schedule.tz == "Asia/Shanghai"


def test_add_at_job_uses_default_timezone_for_naive_datetime(tmp_path) -> None:
    tool = _make_tool_with_tz(tmp_path, "Asia/Shanghai")
    naive = (datetime.now(timezone.utc) + timedelta(days=1)).replace(tzinfo=None)
    with request_context(
        RequestContext(channel="telegram", chat_id="chat-1", session_key="telegram:chat-1")
    ):
        result = tool._add_job(None, "Morning reminder", None, None, None, naive.isoformat())

    assert result.startswith("Created job")
    job = tool._cron.list_jobs()[0]
    expected = int(naive.replace(tzinfo=ZoneInfo("Asia/Shanghai")).timestamp() * 1000)
    assert job.schedule.at_ms == expected

def test_add_at_job_rejects_past_datetime(tmp_path) -> None:
    tool = _make_tool_with_tz(tmp_path, "Asia/Shanghai")
    with request_context(
        RequestContext(channel="telegram", chat_id="chat-1", session_key="telegram:chat-1")
    ):
        result = tool._add_job(None, "Old reminder", None, None, None, "2020-01-01T09:00:00")

    assert "not in the future" in result
    assert "2020-01-01T09:00:00" in result
    assert tool._cron.list_jobs() == []

def test_add_at_job_rejects_datetime_equal_to_now(tmp_path, monkeypatch) -> None:
    tool = _make_tool(tmp_path)
    fixed_now = 1_900_000_000.0
    monkeypatch.setattr("nanobot.agent.tools.cron.time", SimpleNamespace(time=lambda: fixed_now))
    at = datetime.fromtimestamp(fixed_now, tz=timezone.utc).isoformat()
    with request_context(
        RequestContext(channel="telegram", chat_id="chat-1", session_key="telegram:chat-1")
    ):
        result = tool._add_job(None, "Deadline reminder", None, None, None, at)

    assert "not in the future" in result
    assert tool._cron.list_jobs() == []

def test_add_at_job_accepts_future_datetime(tmp_path) -> None:
    tool = _make_tool(tmp_path)
    future = (datetime.now(timezone.utc) + timedelta(hours=1)).isoformat()
    with request_context(
        RequestContext(channel="telegram", chat_id="chat-1", session_key="telegram:chat-1")
    ):
        result = tool._add_job(None, "Future reminder", None, None, None, future)

    assert result.startswith("Created job")
    job = tool._cron.list_jobs()[0]
    assert job.schedule.kind == "at"
    assert job.state.next_run_at_ms is not None


def test_add_job_rejects_multiple_schedule_fields(tmp_path) -> None:
    tool = _make_tool(tmp_path)
    with request_context(
        RequestContext(channel="telegram", chat_id="chat-1", session_key="telegram:chat-1")
    ):
        result = tool._add_job(None, "Morning standup", 60, "0 8 * * *", None, None)

    assert result == "Error: exactly one of every_seconds, cron_expr, or at is required"
    assert tool._cron.list_jobs() == []


@pytest.mark.parametrize("every_seconds", [0, -60])
def test_add_job_rejects_non_positive_interval(tmp_path, every_seconds: int) -> None:
    tool = _make_tool(tmp_path)
    with request_context(
        RequestContext(channel="telegram", chat_id="chat-1", session_key="telegram:chat-1")
    ):
        result = tool._add_job(None, "Morning standup", every_seconds, None, None, None)

    assert result == "Error: every_seconds must be a positive integer"
    assert tool._cron.list_jobs(include_disabled=True) == []


@pytest.mark.parametrize("every_seconds", [0, -60])
def test_validate_params_rejects_non_positive_interval(tmp_path, every_seconds: int) -> None:
    tool = _make_tool(tmp_path)

    errors = tool.validate_params(
        {"action": "add", "message": "Morning standup", "every_seconds": every_seconds}
    )

    assert any("every_seconds" in error for error in errors)


@pytest.mark.asyncio
async def test_legacy_zero_interval_job_can_be_listed_repaired_and_removed(tmp_path) -> None:
    tool = _make_tool(tmp_path)
    store_path = tool._cron.store_path
    store_path.parent.mkdir(parents=True)
    store_path.write_text(json.dumps({
        "version": 1,
        "jobs": [{
            "id": "legacy-zero",
            "name": "Legacy reminder",
            "enabled": True,
            "schedule": {"kind": "every", "everyMs": 0},
            "payload": {"kind": "agent_turn", "message": "hello", **_bound_chat()},
        }],
    }), encoding="utf-8")

    assert "Legacy reminder" in await tool.execute(action="list")
    renamed = tool._cron.update_job("legacy-zero", name="Repair me")
    assert isinstance(renamed, CronJob)
    assert renamed.schedule.every_ms == 0
    repaired = tool._cron.update_job(
        "legacy-zero", schedule=CronSchedule(kind="every", every_ms=60_000),
    )
    assert isinstance(repaired, CronJob)
    assert repaired.state.next_run_at_ms is not None

    reloaded = _make_tool(tmp_path)
    assert "Repair me" in await reloaded.execute(action="list")
    with request_context(RequestContext(
        channel="websocket", chat_id="chat-1", session_key="websocket:chat-1",
    )):
        await reloaded.execute(action="remove", job_id="legacy-zero")
    assert _make_tool(tmp_path)._cron.list_jobs(include_disabled=True) == []


def test_add_job_binds_current_session_key(tmp_path) -> None:
    tool = _make_tool(tmp_path)
    with request_context(
        RequestContext(channel="telegram", chat_id="chat-1", session_key="telegram:chat-1")
    ):
        result = tool._add_job(None, "Morning standup", 60, None, None, None)

    assert result.startswith("Created job")
    job = tool._cron.list_jobs()[0]
    assert job.payload.session_key == "telegram:chat-1"
    assert job.payload.origin_channel == "telegram"
    assert job.payload.origin_chat_id == "chat-1"
    assert job.payload.origin_metadata == {}
    assert job.payload.channel is None
    assert job.payload.to is None


def test_add_job_requires_session_key(tmp_path) -> None:
    tool = _make_tool(tmp_path)
    with request_context(RequestContext(channel="telegram", chat_id="chat-1")):
        result = tool._add_job(None, "Background refresh", 60, None, None, None)

    assert result == "Error: scheduled cron jobs must be created from a chat session"
    assert tool._cron.list_jobs() == []


def test_cron_schema_advertises_action_specific_requirements(tmp_path) -> None:
    tool = _make_tool(tmp_path)

    # Only ``action`` is required at the schema root — per-action requirements
    # are enforced at runtime via ``validate_params`` and surfaced to the LLM
    # through field descriptions. We intentionally do NOT set top-level
    # ``oneOf``/``anyOf``/``allOf``/``enum``/``not``: OpenAI Codex/Responses
    # reject those at the root of function parameters (#3265 regression).
    assert tool.parameters["required"] == ["action"]
    for disallowed in ("oneOf", "anyOf", "allOf", "not"):
        assert disallowed not in tool.parameters, (
            f"Top-level '{disallowed}' is rejected by OpenAI Codex/Responses tool schemas"
        )
    message_desc = tool.parameters["properties"]["message"]["description"]
    assert "REQUIRED" in message_desc and "action='add'" in message_desc
    job_id_desc = tool.parameters["properties"]["job_id"]["description"]
    assert "REQUIRED" in job_id_desc and "action='remove'" in job_id_desc


def test_validate_params_requires_message_only_for_add(tmp_path) -> None:
    tool = _make_tool(tmp_path)

    assert "message is required when action='add'" in tool.validate_params({"action": "add"})
    assert tool.validate_params({"action": "list"}) == []
    assert "job_id is required when action='remove'" in tool.validate_params({"action": "remove"})


def test_add_job_empty_message_returns_actionable_error(tmp_path) -> None:
    tool = _make_tool(tmp_path)
    with request_context(
        RequestContext(channel="telegram", chat_id="chat-1", session_key="telegram:chat-1")
    ):
        result = tool._add_job(None, "", 60, None, None, None)

    assert "action='add' requires a non-empty 'message'" in result
    assert "Retry including message=" in result


def test_add_job_captures_owner_and_origin_without_legacy_delivery_fields(tmp_path) -> None:
    tool = _make_tool(tmp_path)
    meta = {"slack": {"thread_ts": "111.222", "channel_type": "channel"}}
    with request_context(
        RequestContext(
            channel="slack",
            chat_id="C99",
            metadata=meta,
            session_key="slack:C99:111.222",
        )
    ):
        result = tool._add_job("test", "say hi", 60, None, None, None)
    assert "Created job" in result

    jobs = tool._cron.list_jobs()
    assert len(jobs) == 1
    assert jobs[0].payload.session_key == "slack:C99:111.222"
    assert jobs[0].payload.origin_channel == "slack"
    assert jobs[0].payload.origin_chat_id == "C99"
    assert jobs[0].payload.origin_metadata == meta
    assert jobs[0].payload.channel is None
    assert jobs[0].payload.to is None
    assert jobs[0].payload.channel_meta == {}


def test_list_excludes_disabled_jobs(tmp_path) -> None:
    tool = _make_tool(tmp_path)
    job = tool._cron.add_job(
        name="Paused job",
        schedule=CronSchedule(kind="cron", expr="0 9 * * *", tz="UTC"),
        message="test",
    )
    tool._cron.enable_job(job.id, enabled=False)

    result = tool._list_jobs()
    assert "Paused job" not in result
    assert result == "No scheduled jobs."