Files
nanobot/tests/cron/test_cron_active_rearm.py
T
chengyongruandXiaoyu-hub 4a14faa47e fix(webui): keep saved topics bound to their project (#6509)
* fix(webui): keep saved topics bound to their project

Lock the project as soon as the session exists, independently of loaded history. Validate both explicit updates and message admissions against the persisted directory while retaining same-project permission changes.

Related to #6505.

Co-authored-by: Xiaoyu-hub <17346801517@163.com>

* test(agent): retain goal shutdown failure stacks

Use the existing stop_loop diagnostic without changing the five-second teardown deadline or the goal assertions. The Windows failure on #6509 reached session shutdown but did not capture its worker stacks.

* test(cron): await rearm tick completion instead of disk timing

The Windows run completed the first and last jobs correctly, but rescheduling took over three seconds inside a two-second timeout. Await the original timer task, detect an early exit before callback entry, and preserve job-order, cancellation, persistence and rearming assertions. Keep real disk writes and all parameterized cases.

Also place the goal-shutdown diagnostic import in the existing first-party group.

---------

Co-authored-by: Xiaoyu-hub <17346801517@163.com>
2026-10-28 21:37:23 +08:00

176 lines
6.1 KiB
Python

"""Job-store edits must not cancel the timer task executing an agent turn."""
import asyncio
from pathlib import Path
import pytest
from nanobot.cron.service import CronService
from nanobot.cron.types import CronJob, CronSchedule
async def _wait_for_job_entry(timer: asyncio.Task[None], entered: asyncio.Event) -> None:
entry = asyncio.create_task(entered.wait())
try:
await asyncio.wait((timer, entry), return_when=asyncio.FIRST_COMPLETED)
if timer.done():
await timer
assert entered.is_set(), "timer finished without starting the due job"
finally:
entry.cancel()
await asyncio.gather(entry, return_exceptions=True)
@pytest.mark.asyncio
@pytest.mark.parametrize("action", ["add", "update", "remove", "disable"])
@pytest.mark.parametrize("edit_from_callback", [True, False])
async def test_edit_during_timer_execution_preserves_turn(
tmp_path: Path, action: str, edit_from_callback: bool,
) -> None:
entered = asyncio.Event()
release = asyncio.Event()
outcomes: list[str] = []
def add_job(name: str) -> CronJob:
return service.add_job(
name=name,
schedule=CronSchedule(kind="every", every_ms=3_600_000),
message=name,
session_key=f"websocket:{name}",
origin_channel="websocket",
origin_chat_id=name,
)
def edit_store() -> None:
if action == "add":
add_job("new-job")
elif action == "update":
service.update_job(other.id, message="updated")
elif action == "remove":
service.remove_job(other.id)
else:
service.enable_job(other.id, False)
async def on_job(job: CronJob) -> None:
entered.set()
try:
if edit_from_callback:
edit_store()
await release.wait()
outcomes.append("completed")
except asyncio.CancelledError:
outcomes.append("cancelled")
raise
service = CronService(tmp_path / "cron" / "jobs.json", on_job=on_job)
await service.start()
try:
other = add_job("other")
active = add_job("active")
active.state.next_run_at_ms = 1
service._save_store()
service._arm_timer()
timer = service._timer_task
assert timer is not None
await _wait_for_job_entry(timer, entered)
if not edit_from_callback:
edit_store()
release.set()
# Rearming cancels the finished tick; the job outcome must still be successful.
await asyncio.wait((timer,))
if not timer.cancelled():
timer.result()
assert outcomes == ["completed"]
assert service._active_executions == 0
restored = CronService(service.store_path).get_job(active.id)
assert restored is not None
assert restored.state.last_status == "ok"
assert len(restored.state.run_history) == 1
assert restored.state.next_run_at_ms is not None
assert restored.state.last_run_at_ms is not None
assert restored.state.next_run_at_ms > restored.state.last_run_at_ms
assert service._timer_task is not None
assert not service._timer_task.done()
finally:
timer = service._timer_task
service.stop()
if timer is not None:
await asyncio.gather(timer, return_exceptions=True)
@pytest.mark.asyncio
@pytest.mark.parametrize("action", ["remove", "disable", "reschedule"])
async def test_due_job_edited_while_waiting_is_skipped(tmp_path: Path, action: str) -> None:
entered = asyncio.Event()
release = asyncio.Event()
calls: list[str] = []
async def on_job(job: CronJob) -> None:
calls.append(job.name)
if job.name == "first":
entered.set()
await release.wait()
service = CronService(tmp_path / "cron" / "jobs.json", on_job=on_job)
await service.start()
try:
for name in ("first", "waiting", "last"):
service.add_job(
name=name,
schedule=CronSchedule(kind="every", every_ms=3_600_000),
message=name,
session_key=f"websocket:{name}",
origin_channel="websocket",
origin_chat_id=name,
)
jobs = service.list_jobs()
waiting = next(job for job in jobs if job.name == "waiting")
for job in jobs:
job.state.next_run_at_ms = 1
service._save_store()
service._arm_timer()
timer = service._timer_task
assert timer is not None
await _wait_for_job_entry(timer, entered)
if action == "remove":
assert service.remove_job(waiting.id) == "removed"
elif action == "disable":
service.enable_job(waiting.id, False)
else:
service.update_job(
waiting.id, schedule=CronSchedule(kind="every", every_ms=7_200_000),
)
edited = service.get_job(waiting.id)
next_run = edited.state.next_run_at_ms if edited is not None else None
release.set()
await asyncio.wait((timer,))
if not timer.cancelled():
timer.result()
assert service._active_executions == 0
assert calls == ["first", "last"]
restored = CronService(service.store_path)
for job in jobs:
if job.id == waiting.id:
continue
completed = restored.get_job(job.id)
assert completed is not None
assert completed.state.last_status == "ok"
assert len(completed.state.run_history) == 1
skipped = restored.get_job(waiting.id)
if action == "remove":
assert skipped is None
else:
assert skipped is not None
assert skipped.state.run_history == []
assert skipped.state.next_run_at_ms == next_run
assert skipped.enabled is (action == "reschedule")
assert service._timer_task is not None
assert not service._timer_task.done()
finally:
timer = service._timer_task
service.stop()
if timer is not None:
await asyncio.gather(timer, return_exceptions=True)