Skip to content

fix(schedule): run jobs that fire late instead of silently skipping them - #4271

Merged
kovtcharov-amd merged 3 commits into
amd:mainfrom
kovtcharov:claude/schedule-misfire-grace
Sep 25, 2026
Merged

kovtcharov-amd merged 3 commits into
amd:mainfrom
kovtcharov:claude/schedule-misfire-grace

Conversation

@kovtcharov

Copy link
Copy Markdown
Contributor

A schedule that came due while the laptop was asleep or the daemon was busy for more than one second never ran. The daemon inherited APScheduler's 1-second misfire grace, and the skip only showed up as a generic APScheduler warning. Now a run that is up to an hour late still fires, several missed runs collapse into one, and every skipped run is logged at WARNING with the schedule name. That covers runs more than an hour late and runs that overlap the schedule's previous run, which is now an explicit max_instances=1 rather than an inherited default.

Test plan

  • ~/.cache/gaia-dev/venv/bin/python -m pytest tests/unit/test_schedule_daemon.py -q -k Misfire: 5 failed on origin/main, 5 pass with the fix. This includes an end-to-end check that a run due 60 s ago still fires.
  • ~/.cache/gaia-dev/venv/bin/python -m pytest tests/unit/test_schedule_daemon.py tests/unit/test_schedule_store.py tests/unit/test_schedule_sinks.py tests/unit/cli/test_cli_schedule.py -q: 106 passed
  • black, isort, and pylint (errors-only, CI flags) are clean on the changed files

Fixes #4207

Kalin Ovtcharov added 2 commits September 24, 2026 17:34
A schedule due while the laptop slept or the daemon was busy for more than one second was dropped, because the daemon inherited APScheduler's 1-second misfire grace. Late runs now fire if they are within an hour of the due time, several missed runs collapse into one, and every skipped run (too late, or overlapping the previous run) is logged with its schedule name.

Fixes amd#4207
@github-actions

Copy link
Copy Markdown
Contributor

Approve with suggestions — one doc sentence needs narrowing

Scheduled runs that came due a moment late used to vanish; this makes them fire for up to an hour and logs the ones it still has to skip, by schedule name. I ran the daemon in this environment and the behaviour holds: 30 minutes late fires, two hours late is skipped with a named WARNING, and a burst of missed minutes collapses to one run.

The one thing to fix before merge is the new CLI doc sentence. It promises that a run due while the machine was asleep fires "once the daemon catches up" — true when the process was merely suspended, but not when the daemon itself was stopped or the machine was off. In that case the missed run is dropped and no warning is logged, which is the opposite of what the paragraph tells the user to expect. Narrowing the wording (or saying plainly that a restart doesn't replay) is enough; actually replaying across downtime would be a bigger change and belongs in a follow-up.

Real-world evidence

No evidence-bundle.md was produced for this PR, so the evidence below is what I exercised directly during review. The changed surface is daemon timing, which needs no model inference — nothing here was deferred to the strix-halo lane.

Full tests/unit/test_schedule_daemon.py suite on the PR head:

28 passed, 1 warning in 1.35s

Live daemon probe (real BackgroundScheduler, runner.fire stubbed to record the call):

WARNING | apscheduler.executors.default.run_job | Run time of job "daily-late (... hour='3', minute='0' ...)" was missed by 2:00:00.000389
WARNING | gaia.schedule.daemon._log_skipped_run | schedule 'daily-late' missed its run due at 2026-09-24 22:49:25+00:00: the machine was asleep or busy for more than 3600s past that time
PROBE A (2h late, daily): ran? False
PROBE B (30m late, daily): ran? True
PROBE C (2h late, minutely): ran? True
PROBE D: now 00:49  next_run_time 2026-09-26 00:00:00+00:00

A–C confirm the fix end to end, including that the named WARNING really reaches the listener through a genuine misfire. D is the gap behind the doc finding: a daily 00:00 schedule armed at 00:49 — 49 minutes inside the one-hour grace — skips straight to tomorrow with no run and no warning.

🔍 Technical details

🟡 Important

Doc claim doesn't hold when the daemon itself restarts (docs/reference/cli.mdx:2489)

refresh_schedules arms jobs with scheduler.add_job(...) and no next_run_time, so APScheduler computes the next future fire time (daemon.py:116-123). A past occurrence is never in job.next_run_time, so it can't misfire, so no EVENT_JOB_MISSED fires and _log_skipped_run never runs. misfire_grace_time only helps while the scheduler process stays alive across the due time. PROBE D above is that case at 49 minutes late — inside the advertised hour, dropped silently.

A run that comes due while the daemon is running but the machine is asleep or busy still fires once the daemon catches up, as long as that is within an hour of the due time; several missed runs collapse into one. A run more than an hour late, or one that comes due while the same schedule's previous run is still going, is skipped and logged at WARNING with the schedule name. A run that came due while the daemon itself was not running is not replayed on restart.

Worth noting for the follow-up: the store already records next_run via mark_run, so a startup comparison against it is the natural place to close this properly.

🟢 Minor

Tests hardcode the grace value (tests/unit/test_schedule_daemon.py:95,124) — >= 3600 duplicates the constant it is asserting about; retuning MISFIRE_GRACE_SECONDS downward would leave these passing for the wrong reason.

                assert job.misfire_grace_time >= daemon.MISFIRE_GRACE_SECONDS

Logging tests drive a private APScheduler method (tests/unit/test_schedule_daemon.py:139,157) — scheduler._dispatch_event proves the listener is registered but not that APScheduler ever emits the event with those fields. A real misfire is easy to provoke and covers the whole wiring: a daily-cron job whose next_run_time is pushed past the grace window produces exactly the daily-late WARNING in the evidence above, and runner.fire is not called.

Overlap message says "due at" but formats a list (src/gaia/schedule/daemon.py:77-80) — scheduled_run_times is joined with ", ", which with coalesce=True is virtually always one timestamp. Harmless, but "due at 07:00, 08:00" reads oddly if it ever isn't.

Strengths

  • The misfire/overlap distinction is handled correctly at the API level: EVENT_JOB_MISSED carries a JobExecutionEvent and EVENT_JOB_MAX_INSTANCES a JobSubmissionEvent, and the isinstance branch picks the right attribute for each (scheduled_run_time vs scheduled_run_times). Getting that backwards would raise inside the listener thread and be swallowed.
  • job_defaults on the scheduler rather than per-add_job means schedules armed later by the one-second reload tick inherit the grace automatically — and there's a test that specifically covers the reload path, which is the easy thing to miss.
  • Pinning coalesce and max_instances explicitly even though they match APScheduler's defaults is the right call for behaviour this fix now depends on and documents.

The doc promised a run due while the machine slept "fires once the
daemon catches up" without qualifying that the daemon process itself
has to have stayed up -- a run that came due while the daemon wasn't
running is dropped silently, the opposite of what the paragraph told
users to expect. Also stops the grace-time test asserting against a
duplicated literal instead of the constant it's actually pinning.
@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Sep 25, 2026
Merged via the queue into amd:main with commit b95437d Sep 25, 2026
29 of 31 checks passed
kovtcharov pushed a commit to kovtcharov/gaia that referenced this pull request Sep 25, 2026
…amd#4370)

`main` has been red since 2026-09-25 18:29, so nothing can leave the
merge queue. Three separate regressions landed within minutes of each
other; this clears all three. The worst is a literal `SyntaxError` —
`ChatAgentConfig(...)` was passed `device=` twice, so the Agent UI
backend and everything importing it failed at module load, taking out
the Linux, Windows and macOS unit-test jobs. Nobody wrote that
duplicate: amd#3666 and amd#4275 each added the same kwarg a few lines apart,
neither diff conflicted textually, and the merge queue merged both
cleanly into a file that does not parse.

The other two are one-liners: amd#3402 re-imported `tool_cancelled` over a
name the module already imports (failing the pylint gate) and shipped
`wait_for_condition` with no UI label, and amd#4271 landed an over-long
assertion (failing the black gate).

**Worth knowing:** the ~13 "new pylint errors" that appear in the
failing job's log are not pylint errors. They are the first 20 lines of
the **non-blocking MyPy** output (1224 warnings, truncated for display)
and have never gated anything. Only one pylint hit was real. No
dependency or tool-version drift was involved — pylint 4.0.9 and black
26.5.1 both predate the last green run.

## Test plan

- [ ] `python util/lint.py --black` — clean (1180 files unchanged)
- [ ] `uvx pylint src/gaia --rcfile .pylintrc --disable
C0103,C0301,W0246,W0221,E1102,R0401,E0401,W0718,W0212` — clean on Linux
- [ ] `python -m compileall -q src/gaia tests` — no syntax errors
- [ ] `python -m pytest tests/unit/test_schedule_daemon.py -q` — 28
passed
- [ ] `python -m pytest tests/unit/chat/ui -q` — 1040 passed (was 17
collection errors)

<details>
<summary>🔍 Technical details</summary>

**`src/gaia/ui/agent_loop.py`** — the two `device=session.get("device")`
lines were textually identical, so deleting the later one restores
exactly the intended call. `git blame` attributes line 461 to
`1caad2ade5` (amd#4275) and line 465 to `7a8fea58b1` (amd#3666); amd#3666 is an
ancestor of the last green commit `69f76ab57`, so amd#4275 is the one that
created the duplicate.

**`src/gaia/agents/tools/shell_tools.py`** —
`gaia.agents.base.tools.tool_cancelled` is a pure re-export of
`gaia.tool_cancellation.tool_cancelled`, which the module already
imports at line 31. Verified the bindings are the same object, so
dropping the function-local import is a no-op rather than a behaviour
change.

**Follow-up worth a maintainer's eye, deliberately not bundled here:**
`wait_for_condition` executes arbitrary commands against the same
allowlist as `run_shell_command`, but unlike `run_shell_command` it is
not in `_GAIA_MUTATING_TOOLS`. By that set's own stated criterion ("or
execute code") it looks like it belongs there, and omitting it means the
tool skips the confirmation gating its sibling gets. That is a behaviour
change, not a CI fix, so it should be reviewed on its own rather than
smuggled into an unblock PR.

**Also noted:** `.pylintrc` uses `disable = all` plus an explicit
`enable` list that does not include `syntax-error`, which is why the
pylint gate walked past a file it could not parse. Enabling `E0001`
would have caught this at PR time.
</details>
kovtcharov-amd added a commit to kovtcharov/gaia that referenced this pull request Sep 26, 2026
…tes (amd#4272)

Stacked on amd#4271. Merge that first.

With two `gaia schedule daemon` processes running (for example a login
item plus a manual start), every schedule ran twice: two LLM runs and
two Telegram messages. Separately, a `gaia schedule add` that raced the
daemon's run bookkeeping could silently drop the new schedule. Now the
daemon holds `~/.gaia/schedules.lock` (next to whichever store it uses)
for its whole lifetime, so a second daemon exits at once with an error
that names the running pid. Each store read-modify-write also takes a
short lock, so concurrent changes are all kept. The flock/msvcrt calls
in `gaia.daemon.lock` became shared `try_lock`/`unlock` helpers rather
than a second copy.

## Test plan
- [x] `~/.cache/gaia-dev/venv/bin/python -m pytest
tests/unit/test_schedule_lock.py -q`: without the wiring, the
second-daemon and concurrent `add` + `mark_run` tests fail (2 failed,
and the race test loses schedule `b`). With the fix, 6 pass.
- [x] `gaia schedule daemon` twice against one `HOME`: the second exits
1 with `another gaia schedule daemon (pid N) is already running … Stop
the other daemon first`, and the first keeps running
- [x] `~/.cache/gaia-dev/venv/bin/python -m pytest
tests/unit/test_schedule_lock.py tests/unit/test_schedule_daemon.py
tests/unit/test_schedule_store.py tests/unit/test_schedule_sinks.py
tests/unit/cli/test_cli_schedule.py tests/unit/test_scheduler.py
tests/unit/test_daemon_client.py tests/unit/test_daemon_scheduler.py
tests/unit/test_daemon_cli_dev_anchor.py -q`: all pass
- [x] black, isort, and pylint (errors-only, CI flags) are clean on the
changed files
- [ ] Windows: a second daemon is refused (the pid is omitted there,
because Windows blocks reads of the locked byte)

Fixes amd#4208

---------

Co-authored-by: Kalin Ovtcharov <[email protected]>
Co-authored-by: Kalin Ovtcharov <[email protected]>
kovtcharov-amd added a commit to kovtcharov/gaia that referenced this pull request Sep 26, 2026
…md#4273)

Stacked on amd#4272 (itself stacked on amd#4271). Merge those first.

`gaia schedule add` used to accept sinks that could never deliver: a
misspelled sink name, `telegram` without `--to`, a bare `file` with no
path, or `notification` on Windows. Each of those schedules then failed
on every run, only after spending a full LLM call. Now `add` rejects
them with an actionable message and stores nothing. Each run also checks
its sink before calling the agent, so schedules stored before this
change fail fast too. The Telegram token is still checked only at
delivery, because the daemon reads it from its own environment, not the
shell that ran `add`.

## Test plan
- [x] `~/.cache/gaia-dev/venv/bin/python -m pytest
tests/unit/test_schedule_sinks.py tests/unit/cli/test_cli_schedule.py
tests/unit/test_schedule_daemon.py -q`: 18 failed before the fix, 86
pass after
- [x] `gaia schedule add --name t --cron "0 7 * * *" --prompt hi --sink
telegram` exits 1 with `telegram sink needs a recipient: pass --to`.
`--sink telegarm` and `--sink file` are rejected the same way, `--sink
telegram --to 123` is added, and `gaia schedule list` shows only that
schedule.
- [x] `~/.cache/gaia-dev/venv/bin/python -m pytest tests/unit/cli/ -q`:
all pass except the 4 `test_gaia_binary_on_path` cases, which fail
locally because the dev venv's binaries aren't on PATH (unrelated)
- [x] black, isort, and pylint (errors-only, CI flags) are clean on the
changed files

Fixes amd#4228

---------

Co-authored-by: Kalin Ovtcharov <[email protected]>
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

documentation Documentation changes tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(schedule): jobs that fire more than 1 second late are silently skipped

2 participants