Repository navigation
fix(schedule): allow one schedule daemon per store and lock store writes - #4272
Conversation
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
Two `gaia schedule daemon` processes on one machine fired every schedule twice, and a `gaia schedule add` racing the daemon's run bookkeeping could drop the added schedule. The daemon now holds `schedules.lock` for its lifetime, so a second one exits at once with an error naming the running pid, and each store read-modify-write takes a short lock so concurrent changes are all kept. The flock/msvcrt calls in `gaia.daemon.lock` are lifted into `try_lock`/`unlock` so both daemons share one implementation. Fixes amd#4208
|
Verdict: Approve with suggestions This stops two The one thing worth a maintainer's attention before merge: the Windows behaviour is unverified. Windows file locking works differently enough from Linux that the new lock has a separate code path there, and the repo's unit tests only run on Linux — so the platform most GAIA users are on is the one nobody has run this against. The author's own test plan leaves that box unchecked. Given the bug being fixed (a schedule firing twice) is most likely to hit someone with a login-item daemon on Windows, it's worth ten minutes on a real machine rather than trusting the reasoning. Smaller things, all optional: a lock-file naming edge case that would make the daemon lock and the store lock collide for an unusual store location, a stray-bytes case in reading the holder's pid that would replace the friendly error with a crash, and the fact that only the Real-world evidenceNo The gap is platform, not surface: that run was on Linux/macOS, the Windows box in the test plan is unchecked, and 🔍 Technical details🟡 ImportantWindows lock path is unexercised by CI and unverified by hand (
Suggest running the existing 🟢 Minor1.
That keeps 2. If the lock file ever holds non-UTF-8 bytes — a pre-existing file at that path, a partial write — 3. Only
4.
Strengths
Note on scopeThis diff includes the misfire/grace-time work from the stacked PR ( |
The single-daemon guard takes msvcrt.locking on Windows and fcntl.flock everywhere else, but tests/unit/ runs on ubuntu-latest only — so the branch that matters to the users most likely to hit the bug (a login-item daemon plus a manual one, firing every schedule twice) had never been executed. Running the existing suite on Windows fails: msvcrt refuses another handle's read of the locked byte, so no pid is available and test_error_names_the_running_daemon_pid cannot pass there. The pid assertion is now POSIX-only, with a Windows counterpart asserting the documented degradation — no pid claimed, message still actionable. TestWindowsLockingPrimitive pins the three msvcrt differences the code depends on: the owner may truncate and write the byte it locked, unlocking needs the seek back to 0 that _release does (otherwise only the close releases it, because unlock() swallows the error), and a second handle's read raises. test_gaia_cli_windows.yml runs the file so this stays true. Two real bugs the new coverage caught: - daemon_lock_path and store_lock_path both returned `<name>.lock` for a suffix-less store, so the daemon would have held the STORE lock for its whole life and every write, including its own mark_run, would have timed out. with_suffix alone does not fix it — `schedules` has no suffix to replace — so the collision is detected and sidestepped. - _read_pid decoded strictly inside an OSError-only guard, so a lock file holding non-UTF-8 bytes replaced the actionable refusal with a UnicodeDecodeError traceback on POSIX.
# Conflicts: # docs/reference/cli.mdx # tests/unit/test_schedule_daemon.py
|
Ran the suite on a real Windows box, which is what the open checkbox was for — and it does not pass there. The Windows path also had two bugs that only running it would surface.
Two real bugs it caught:
🔍 Technical detailsWindows behaviour, measured on this host rather than inferred — all three of the review's load-bearing assumptions hold:
The seek matters more than it looks: Fix 1 — lock-path collision ( Fix 2 — CI.
Merged Not changed: only the |
|
CI note: the one red unit-test job is not from this branch. |
…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]>
Stacked on #4271. Merge that first.
With two
gaia schedule daemonprocesses running (for example a login item plus a manual start), every schedule ran twice: two LLM runs and two Telegram messages. Separately, agaia schedule addthat 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 ingaia.daemon.lockbecame sharedtry_lock/unlockhelpers rather than a second copy.Test plan
~/.cache/gaia-dev/venv/bin/python -m pytest tests/unit/test_schedule_lock.py -q: without the wiring, the second-daemon and concurrentadd+mark_runtests fail (2 failed, and the race test loses scheduleb). With the fix, 6 pass.gaia schedule daemontwice against oneHOME: the second exits 1 withanother gaia schedule daemon (pid N) is already running … Stop the other daemon first, and the first keeps running~/.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 passFixes #4208