Repository navigation
fix(schedule): reject undeliverable sinks when a schedule is added - #4273
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
`gaia schedule add` stored sinks that could never deliver (a misspelled name, `telegram` without `--to`, bare `file`, `notification` on Windows), so the job then failed on every run after a full LLM call. `add` now rejects them and stores nothing, and each run checks its sink before calling the agent so older stores fail fast too. The Telegram token stays a delivery-time check, since the daemon reads it from its own environment. Fixes amd#4228
Verdict: Approve with suggestionsThis makes three scheduling failures loud instead of silent: a sink that could never deliver is now rejected when the schedule is added (and re-checked before each run, so schedules already on disk stop burning an LLM call first), a run that fires late still runs instead of vanishing, and a second schedule daemon refuses to start instead of firing everything twice. The behaviour, the errors, and the CLI docs all line up, and the tests cover the failure modes rather than just the happy path. One thing worth a follow-up: the new lock can time out, and when it does on Note for the reviewer: this is the top of a stack, but the diff under review bundles all three commits (#4271 misfire handling, #4272 daemon/store locking, #4273 sink validation). The PR description's evidence covers only the sink change; the daemon-lock and misfire behaviour is evidenced on the lower PRs. Real-world evidenceN/A — no automated evidence bundle was produced for this run, so the verdict rests on static review plus the evidence the PR itself carries. The PR description does show the real CLI surface for the sink change: That matches the rubric's CLI-evidence bar for the change this PR owns. I could not run the unit suite here (no pytest in this environment), so the "18 failed before / 86 pass after" figure is the author's, not reproduced. The 🔍 Technical detailsIssues🟡 A store-lock timeout crashes the CLI with a traceback on every action except
from gaia.schedule.lock import ScheduleLockError
try:
store.add(schedule)
except ScheduleLockError as exc:
print(f"❌ {exc}", file=sys.stderr)
sys.exit(1)(The pre-existing duplicate-name 🟢
def daemon_lock_path(store_path: Path) -> Path:
store_path = Path(store_path)
return store_path.with_name(store_path.name + ".daemon.lock")Not filed as a one-click suggestion because the filename is documented in two places ( 🟢 The concurrency test can pass without exercising the race (
🟢 Generic file-lock primitives live under
Strengths
|
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
|
Partly unblocked, not fully — and the conflict with main is gone. #4271 is merged, so that half of the stack is resolved. It went in as a squash (b95437d), which is why this branch was showing #4272 is still open, so this PR is still genuinely blocked. I did not force it. Worth flagging: #4272 had moved on without this branch — it gained a Windows lock-path fix that was not here. I merged #4272's current head in as well, so the two are back in sync and this stops being a stale copy of its own prerequisite. Merge #4272 first, then this one. Nothing else is standing in the way. Test evidenceNo conflicts to resolve by hand — both merges were clean. Every changed Those 11 are the 🔍 Technical detailsBranch history vs
The review's 🟡 suggestion (a |
Stacked on #4272 (itself stacked on #4271). Merge those first.
gaia schedule addused to accept sinks that could never deliver: a misspelled sink name,telegramwithout--to, a barefilewith no path, ornotificationon Windows. Each of those schedules then failed on every run, only after spending a full LLM call. Nowaddrejects 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 ranadd.Test plan
~/.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 aftergaia schedule add --name t --cron "0 7 * * *" --prompt hi --sink telegramexits 1 withtelegram sink needs a recipient: pass --to.--sink telegarmand--sink fileare rejected the same way,--sink telegram --to 123is added, andgaia schedule listshows only that schedule.~/.cache/gaia-dev/venv/bin/python -m pytest tests/unit/cli/ -q: all pass except the 4test_gaia_binary_on_pathcases, which fail locally because the dev venv's binaries aren't on PATH (unrelated)Fixes #4228