Skip to content

fix(schedule): show runs missed while the PC was off or asleep - #5391

Merged
kovtcharov-amd merged 5 commits into
amd:mainfrom
kovtcharov:claude/rel-missed-schedule-runs
Oct 19, 2026
Merged

kovtcharov-amd merged 5 commits into
amd:mainfrom
kovtcharov:claude/rel-missed-schedule-runs

Conversation

@kovtcharov

@kovtcharov kovtcharov commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

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 show and list count 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 does tests/unit/test_schedule_missed_runs.py for gaia 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; for gaia 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 change
  • pytest 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 passed
  • python util/lint.py --black --isort --pylint --flake8 --test-lanes
  • Real CLI, with USERPROFILE/HOME/GAIA_HOME on a scratch folder (the schedule store follows USERPROFILE, not GAIA_HOME): gaia schedule add, then the daemon's scheduler started once after backdating the expected run 3 hours:
    next_fire:  2026-10-19T01:00:00-07:00
    missed:     4 runs in all while the PC was off or asleep, or the schedule daemon wasn't running (latest due 2026-10-19T00:00:00-07:00)
    brief  [enabled]  cron='0 * * * *'  sink=stdout  next=2026-10-19T01:00:00-07:00  missed=4
    
  • Two Agent UI schedulers on one scratch database, an every-10-minutes task 21 minutes overdue:
    runs: ['A']
    history: done
    history: Missed 2 runs due 2026-10-18 23:41 PDT to 2026-10-18 23:51 PDT while the PC was off or asleep, or GAIA was closed.
    
🔍 Technical details
  • Agent UI (ui/scheduler.py): _run_loop waits for the stored next_run_at in 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 with claim_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.
  • _plan works 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 within MISSED_RUN_GRACE_SECONDS (3600, the daemon's misfire grace).
  • Each pass (_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 requires status = 'active', so a pause on one server holds on the others; a server whose claim finds the task paused stops its timer.
  • Clock-face walks start at the floor, so due times before it are never walked or counted.
  • 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 stale next_run_at is not reported as missed.
  • An unreadable next_run_at fails at load, like a bad schedule_config.
  • gaia schedule (schedule/daemon.py, store.py): new fields missed_count, last_missed, resumed_at, missed_since. At start, catch_up_missed_runs counts cron fires from the expected next_run, never before creation, the last run, the last resume, or missed_since. While running, only EVENT_JOB_SUBMITTED is 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, inside store_lock (account_missed). Past 100,000 in one stretch, last_missed is the latest due time and show says "at least". show prints every time in local time.
  • gaia schedule run still moves next_run to now, so runs missed before a manual run are not counted.
  • The daemon's own job clock (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 of scheduler.py.
  • Not LLM-affecting.

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.
@github-actions github-actions Bot added documentation Documentation changes cli CLI changes tests Test changes labels Oct 19, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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 gaia schedule show / list run that shows the new missed: line. When the strix-halo lane runs, add a screenshot of a "Missed …" entry in the Agent UI run history.

Real-world evidence

N/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 gaia schedule CLI output and the Agent UI run history. I couldn't run the tests here because pytest isn't installed on this runner, and CI was still pending when I reviewed.

🔍 Technical details

Issues found

🟡 No real-surface evidence for the new CLI and UI output (src/gaia/cli.py:4339, src/gaia/cli.py:4352, src/gaia/ui/scheduler.py:1206)

  • The new missed=N suffix in list, the missed: line in show, and the "Missed N runs due …" history row are all user-visible.
  • A short shell transcript would cover the CLI: add a schedule, set its next_run in schedules.toml to a few fires ago, then run gaia schedule daemon and gaia schedule show <name>.
  • For the UI, the /api/schedules/{name}/results response showing the error row is enough until the screenshot can be captured.

🟢 A very fast job can hide folded runs (src/gaia/schedule/daemon.py:194)

  • _account_for_submission runs after submit_job has already handed the job to the executor.
  • If _job gets through mark_run before the listener takes store_lock, last_run moves past the folded due times. The fire > floor filter in _missed_runs then drops them, so they're never counted.
  • Real runs take seconds, so this is unlikely in practice. If you want it airtight, pass latest as an upper bound on the floor (ignore a last_run later than latest).

Strengths

  • Lateness is decided once, under store_lock, inside the submission listener (account_missed with a decide callback). This avoids double-counting against EVENT_JOB_MISSED, which fires from the executor thread.
  • Upgrades are handled: a store with no missed_since starts counting from its first check and doesn't trust a stale next_run. Pause and resume clear next_run and stamp resumed_at, so time spent paused is never reported as missed.
  • An unreadable next_run_at fails at load with a remedy that names the table and column, instead of killing the timer coroutine silently. The tests use a fake clock for the sleep case instead of real waits.

…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.
kovtcharov and others added 3 commits October 19, 2026 00:05
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.
@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Oct 19, 2026
Merged via the queue into amd:main with commit f4dcebe Oct 19, 2026
39 of 40 checks passed
@kovtcharov-amd
kovtcharov-amd deleted the claude/rel-missed-schedule-runs branch October 19, 2026 16:58
kovtcharov added a commit to kovtcharov/gaia that referenced this pull request Oct 21, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli CLI changes documentation Documentation changes tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants