Repository navigation
fix(schedule): show runs missed while the PC was off or asleep - #5391
Conversation
Scheduled runs that came due while the PC was off or asleep, or while GAIA or the schedule daemon was closed, vanished: the Agent UI scheduler restarted every timer from scratch on each start (so a task restarted more often than its interval never ran), and `gaia schedule` armed its timers from "now" and let APScheduler fold a long sleep's runs into one without a trace. Agent UI tasks now keep their next run time across a restart. A run late by under an hour still runs; older ones go into the task's run history as "Missed ... while the PC was off or asleep, or GAIA was closed", and nothing is run twice to catch up. `gaia schedule` counts the runs it never made, at start and as a sleep skips them, and `show`/`list` print them; runs due while a schedule was paused are not counted.
|
Verdict: Approve with suggestions Scheduled runs that came due while the PC was off or asleep, or while GAIA or the schedule daemon was closed, used to vanish without a trace. Agent UI restarts also reset every task's timer. This PR fixes both. Agent UI tasks now keep their next run time across a restart, a run that's less than an hour late still runs once, and the other missed runs are recorded instead of dropped. I traced the counting paths: daemon start-up, sleeps that fold several runs into one, pause/resume, and stores saved before this change. I found no correctness bugs, and no run gets executed twice. The one thing to add: the user-visible output changed, but the PR shows only unit tests for it. Before merge, paste a real Real-world evidenceN/A: no evidence bundle was produced for this run, and the PR description has unit-test results only. This verdict rests on static review for the 🔍 Technical detailsIssues found 🟡 No real-surface evidence for the new CLI and UI output (
🟢 A very fast job can hide folded runs (
Strengths
|
…owlist The CLI reference now documents `gaia schedule show`, so the docs-drift test flagged its allowlist entry as stale and failed on every OS.
Two Agent UI servers on one chat database both ran a catch-up run and both wrote a "Missed" entry. Each due run is now claimed with a compare-and-set on the task's stored next run time, and only the server that wins runs it or records it. Past the 100,000-period walk cap the last walked time was taken for the latest due time, so a run due seconds ago was skipped and the message's end date was weeks early. Interval tasks are now counted by arithmetic; clock-face schedules find the latest due time from the last week and say "at least N". The first start after updating no longer blames an older build's stale next run time on the PC being off: a settings key records when this build started counting. `gaia schedule show` prints the latest missed time in local time, like next_fire.
…ervers An every-minute 9-to-5 task an older build left months back, met by this build's first start outside its window, took the cap path, which skipped the floor check and returned no first missed time; recording it raised AttributeError after the claim had moved the run on, so the schedule stayed dead until restart and a pre-build run got blamed. The walk now starts at the floor, so nothing before it is walked or counted, and a bad pass is logged and retried instead of ending the timer. A pause on one server was undone by another, because the claim also matched the NULL next run time a pause leaves; it now requires the task to be active, and a server whose claim finds it paused stops its timer. `gaia schedule`: past the count cap, last_missed is the latest due time and `show` says "at least"; `show` prints created_at in local time too.
Before: if the Agent UI backend restarted, crashed or shut down mid-answer, the question sat in the chat with no reply and no explanation, forever, and a document caught mid-index read "indexing" forever. After: the next start shows the reply as far as it got, which tools it had started, then "This answer was interrupted when GAIA closed. Send again to retry.", and the document shows **Failed** with **Retry**. A scheduled run cut short says the same in its chat. Nothing is re-run on its own, since a half-finished turn may already have run tools. Lands after v0.25.0. Reproduced by `tests/unit/chat/ui/test_restart_recovery.py`: on main, `test_app_start_runs_the_recovery` finds no reply after a restart and the document still "indexing"; the other cases fail for want of the turn marker. All pass here. ## Test plan - [x] `pytest tests/unit/chat/ui/test_restart_recovery.py` (22 cases): streamed, non-streamed and scheduled turns cut off; a shutdown mid-turn; a turn that ends on an error; a policy block saved mid-turn; two servers with turns in one chat; another server's reply not ending this turn; the 30 s sweep closing a turn once its server exits; tools named in the recovered reply; a clock step on Linux; indexing cut off - [x] The two-server case on the previous head (6940982), with the reviewer's `two_servers.py`: B's question got A's streamed text plus the notice. Here each turn keeps its own text. - [x] `pytest tests/unit/chat/ui tests/unit/test_chat_preflight.py tests/unit/test_ui_warmup.py tests/integration/test_chat_ui_integration.py tests/unit/test_scheduler.py tests/unit/test_scheduler_api.py tests/integration/test_scheduler_e2e.py` with an isolated `GAIA_HOME`: all pass except `test_issue_841_custom_python_agent_model_id_respected` and `test_custom_agent_model_id_honored_through_api` (they patch `HOME`, and fail the same way on main whenever `GAIA_HOME` is set), and three folder-indexing tests that fail on main too under `-n 6` and pass run on their own - [x] `python util/lint.py --black --isort --pylint --flake8 --test-lanes` - [x] Live, with Gemma 4 E4B on Lemonade and the built web UI against a fresh `GAIA_HOME`: force-killed the backend mid-answer (145 chunks in) and mid-index (6.2 MB file), restarted it, and reopened the chat. The reply shows its streamed text plus the interrupted notice, the chat is marked failed and unread, and the document shows Failed / Retry (screenshots in the PR comments). Not run through the desktop app's own Quit. <details> <summary>🔍 Technical details</summary> - `unfinished_turns`: one row per question (`question_id` primary key, cascading from the message), written with the question in one transaction (`begin_turn`). `TurnJournal` saves the cleaned streamed text and the tools started every 2 s into this process's own row. Saving the reply (`add_message`/`upsert_message(ends_turn=True)`) deletes the row in the same transaction, so a row left behind is always a turn that never saved a reply; there is no "is there a later assistant message" guess. - Every update and delete is scoped to the row's owner. Another server closes a row only once its owner has exited (`recover_interrupted_work`, at `create_app` and every 30 s). - An owner is `pid:start:token` (`gaia.utils.process_owner`, shared with amd#5390's session history; both PRs add the identical file). On Linux, *start* is the boot id plus the start tick from `/proc/<pid>/stat`, so an NTP step can't make a live server look gone; on Windows and macOS it is `create_time()`, which those keep as recorded. - A turn that ends without saving a reply is closed with what it showed: its final `answer` as is, or its text plus the last error it showed, or else "This answer ended before GAIA could save a reply." A non-streamed turn whose request is cancelled gets "This answer was interrupted before it finished." - Scheduled runs: `_execute_task` saves the prompt with `begin_turn` and the reply with `ends_turn=True`. This touches a different part of `scheduler.py` from amd#5391. - Builds on amd#5316: after a restart its re-attach gets a 404 and loads the saved chat, which now holds the interrupted reply. - Merging with amd#5337: - It moves the non-streamed question save ahead of the turn queue. Save it with `question_id = db.begin_turn(sid, request.message)` there, and on its "Cancelled." replies use `add_message(..., ends_turn=True)`. - Its `except BaseException` branch should `db.discard_turn(question_id)` before releasing the lock. - If `journal.observe` moves into its `_emit`, keep the `TurnJournal` per run. Its `_RunOutcome`/`TurnCancelled` path must not close the turn on a shutdown cancel, or the next start can't report it. - Main's chat-list state (`last_run_status`, unread) is set by recovery too: a recovered turn's chat shows failed and unread. A turn withdrawn from the turn queue, or stopped by the spending limit, saves its reply as the turn's end. - Docs: a "PC sleeps" row in the Close/Quit table belongs with amd#5387, which also edits `agent-ui.mdx`, so expect a textual conflict there. - Not LLM-affecting: no prompt, tool or model change. A recovered reply enters the model's history cleaned the same way a finished one is. </details> --------- Co-authored-by: Kalin Ovtcharov <[email protected]>
Before: a scheduled run that came due while the PC was off or asleep, or while GAIA or the schedule daemon was closed, vanished without a trace. In the Agent UI, every restart also reset each task's timer, so a daily task on a PC restarted daily could never run. After: Agent UI tasks keep their next run time across a restart. The latest run, if under an hour late, still runs once; every other one shows in the task's run history as "Missed 3 runs due … while the PC was off or asleep, or GAIA was closed".
gaia schedule showandlistcount the runs the daemon never made. Nothing is run twice, including with two GAIA servers on one chat database. Lands after v0.25.0.Reproduced by
tests/unit/test_scheduler_missed_runs.py: on main a task three runs overdue at startup gets no history entry, a run that just came due never fires, and a task restarted before its time loses it. On this PR's first commit two servers both run the catch-up, a stretch past the count cap skips the run due seconds ago, and an older build's stale time is blamed on the PC; on the second commit an old dense windowed task first met outside its window crashed the timer, and a pause on one server was undone by the other. All pass here, as doestests/unit/test_schedule_missed_runs.pyforgaia schedule.Test plan
pytest tests/unit/test_scheduler_missed_runs.py tests/unit/test_schedule_missed_runs.py: missed at startup; a sleep while waiting (fake clock); a late run that still runs; a restart before the due time; two servers on one database; a clock-face stretch past the cap ("at least 3", latest still runs); every-second for three days counted exactly; a task left by an older build; an old every-minute 9-to-5 task first met in the evening; a pass that raises is logged and retried; a pause on one server holds on the other; forgaia schedule, the latest due time past the cap, runs a sleep folded together, a run dropped as too late counted once, a paused schedule, a store saved before this changepytest tests/unit/test_scheduler*.py tests/unit/test_schedule_*.py tests/unit/cli/test_cli_schedule.py tests/unit/cli/test_cli_docs_drift.py tests/integration/test_scheduler_e2e.py tests/unit/chat/ui/test_server.py tests/unit/chat/ui/test_database.py: 529 passedpython util/lint.py --black --isort --pylint --flake8 --test-lanesUSERPROFILE/HOME/GAIA_HOMEon a scratch folder (the schedule store followsUSERPROFILE, notGAIA_HOME):gaia schedule add, then the daemon's scheduler started once after backdating the expected run 3 hours:🔍 Technical details
ui/scheduler.py):_run_loopwaits for the storednext_run_atin naps of at most 30 s, re-reading the wall clock, so a sleep is noticed within 30 s of waking. At the due time it claims the run withclaim_scheduled_run(UPDATE … WHERE id = ? AND next_run_at IS ?); a server that loses the claim re-reads the stored time and waits again. The winner alone records misses and runs._planworks out what came due. Interval tasks are counted exactly, by arithmetic. Clock-face schedules walk up to 100,000 due times; past that, the latest is found from the last 8 days and the count reads "at least N". The latest runs if it is withinMISSED_RUN_GRACE_SECONDS(3600, the daemon's misfire grace)._take_due_run) that raises is logged with the task and due time and retried after 30 s, so one bad pass can't end a schedule until restart. The claim requiresstatus = 'active', so a pause on one server holds on the others; a server whose claim finds the task paused stops its timer.scheduler_missed_runs_since(a settings key) records when this build first started. Due times before it, or before a task's creation, are never counted, so an older build's stalenext_run_atis not reported as missed.next_run_atfails at load, like a badschedule_config.gaia schedule(schedule/daemon.py,store.py): new fieldsmissed_count,last_missed,resumed_at,missed_since. At start,catch_up_missed_runscounts cron fires from the expectednext_run, never before creation, the last run, the last resume, ormissed_since. While running, onlyEVENT_JOB_SUBMITTEDis used: APScheduler drops the runs a sleep folded together without an event, and decides "too late" in another thread, so this listener counts the folded ones and decides lateness itself, once, insidestore_lock(account_missed). Past 100,000 in one stretch,last_missedis the latest due time andshowsays "at least".showprints every time in local time.gaia schedule runstill movesnext_runto now, so runs missed before a manual run are not counted.daemon/scheduler/) is untouched. fix(ui): say when a restart cut a reply or an index short #5386 marks scheduled runs as unfinished turns in_execute_task, a different part ofscheduler.py.