Skip to content

fix(schedule): reject undeliverable sinks when a schedule is added - #4273

Merged
kovtcharov-amd merged 8 commits into
amd:mainfrom
kovtcharov:claude/schedule-validate-sink
Sep 26, 2026
Merged

kovtcharov-amd merged 8 commits into
amd:mainfrom
kovtcharov:claude/schedule-validate-sink

Conversation

@kovtcharov

Copy link
Copy Markdown
Contributor

Stacked on #4272 (itself stacked on #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

  • ~/.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
  • 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.
  • ~/.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)
  • black, isort, and pylint (errors-only, CI flags) are clean on the changed files

Fixes #4228

Kalin Ovtcharov added 4 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
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
@github-actions github-actions Bot added documentation Documentation changes cli CLI changes tests Test changes daemon Daemon supervisor / sidecar control plane labels Sep 25, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Approve with suggestions

This 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 add, remove, pause, resume, or run, the user gets a Python traceback instead of the clean one-line error this same PR gives the daemon command. Everything else is minor.

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 evidence

N/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:

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 rejected the same way; --sink telegram --to 123 is added
 and gaia schedule list shows only that schedule)

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 gaia schedule daemon second-instance message and the late-fire behaviour have no CLI output in this description — they belong to the stacked PRs, so I'm nudging rather than blocking.

🔍 Technical details

Issues

🟡 A store-lock timeout crashes the CLI with a traceback on every action except daemon (src/gaia/cli.py:3394, :3424, :3430, and the run path)

store.add / remove / set_enabled / mark_run now acquire store_lock, which raises ScheduleLockError after 10s. The daemon action catches its lock error and prints ❌ … + exit 1 (cli.py:3446-3452), but the store-mutating actions don't, so a contended store gives the user a raw traceback — and the actionable text you wrote in lock.py gets buried in it. Cheapest fix is one wrapper around the mutating actions:

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 ValueError and missing-name KeyError escape the same way, so a single try around the whole action dispatch would close all of them at once.)

🟢 daemon_lock_path can resolve to the same file as store_lock_path (src/gaia/schedule/lock.py:35-41)

with_suffix(".lock") only differs from store_lock_path's append-style name when the store path has a suffix. TomlScheduleStore(path) and run_daemon(store_path) both accept an arbitrary path, so a suffix-less store (~/.gaia/schedules) makes both helpers return schedules.lock — the running daemon then holds the store lock for its whole lifetime and every gaia schedule add times out after 10s with a misleading "hung gaia schedule command" message. Not reachable from today's CLI (no --store flag), so it's a latent trap rather than a bug. Deriving both by append removes the class of problem:

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 (lock.py's module docstring and docs/reference/cli.mdx) and both would need the new name.

🟢 The concurrency test can pass without exercising the race (tests/unit/test_schedule_lock.py:98)

adder.join(0.5) is a fixed sleep hoping the adder thread reaches store_lock inside mark_run's window. If it doesn't get there in time the test still passes, having proven nothing — and it costs 0.5s every run. Having the adder signal an Event just before cli_store.add(...) and waiting on that instead makes it deterministic.

🟢 Generic file-lock primitives live under gaia/daemon/ (src/gaia/schedule/lock.py:271 importing gaia.daemon.lock)

try_lock / unlock are OS primitives with nothing daemon-specific about them, but the extraction leaves gaia/schedule/ depending on gaia/daemon/ to get at them. A neutral home (gaia/utils/filelock.py, say) would let both packages import down instead of sideways. Worth a thought, not a merge blocker — gaia.daemon's eager imports are cheap, so there's no import-cost consequence today.

Strengths

  • Extracting try_lock/unlock instead of copying the platform branch into a second file, and getting the Windows detail right — msvcrt.locking unlocks the byte at the current offset, so _release seeks to 0 first, with a comment saying why (schedule/lock.py:295-302). That's exactly the kind of non-obvious WHY comment CLAUDE.md asks for.
  • Every new error names what failed, what to do, and where to look, per the no-silent-fallbacks rule — including the nice touch of reporting the holding daemon's pid, with the Windows locked-byte read failure handled instead of crashing the error path.
  • Validating in runner.fire as well as at add is the right call, not redundancy: it's what makes a store written before this change fail before the LLM call rather than after it, and the test asserts the agent is never constructed.
  • Docs moved with the behaviour — the misfire window, the single-daemon rule, the lock file, and the sink rejection rules are all in docs/reference/cli.mdx in the same change.

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
@github-actions github-actions Bot added the devops DevOps/infrastructure changes label Sep 26, 2026
@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

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 CONFLICTING: it still carried its own copy of the same commit. Merging main in resolved that, and the PR now reports MERGEABLE.

#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 evidence

No conflicts to resolve by hand — both merges were clean. Every changed .py file AST-parses, and the tree has no stray conflict markers.

tests/unit/test_schedule_{sinks,daemon,lock,store}.py + cli/test_cli_schedule.py
  → 139 passed, 1 skipped

tests/unit/cli/ (full)
  → 296 passed, 11 failed

Those 11 are the test_cli_smoke.py PATH checks (Binary 'gaia-cli' not found on PATH) — the same environmental failures this PR's description already calls out, unrelated to scheduling. black + isort clean.

🔍 Technical details

Branch history vs main after the restack:

8c6b545e7 Merge main into schedule-validate-sink
e771efe76 Merge #4272 (schedule-single-daemon) into schedule-validate-sink
cacfb4592 Merge origin/main into work-4272
39dd30205 test(schedule): run the Windows lock path, and fix what running it found   ← was missing here
ef5bf2b21 fix(schedule): reject undeliverable sinks when a schedule is added          ← this PR
e23c47d2f fix(schedule): allow one schedule daemon per store and lock store writes    ← #4272

git merge-base --is-ancestor ba67a76f2 origin/main returns false, confirming #4271 was squashed rather than merged — the duplicated content was the entire source of the DIRTY state.

The review's 🟡 suggestion (a ScheduleLockError timeout giving a traceback instead of a clean ❌ … exit 1 on add/remove/pause/resume/run) is untouched here — it was raised as a follow-up against an Approve verdict, and this pass was scoped to unblocking. Still worth doing, either before merge or as its own PR.

@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Sep 26, 2026
Merged via the queue into amd:main with commit 73b24d5 Sep 26, 2026
55 of 58 checks passed
@kovtcharov-amd
kovtcharov-amd deleted the claude/schedule-validate-sink branch September 26, 2026 05:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli CLI changes daemon Daemon supervisor / sidecar control plane devops DevOps/infrastructure changes documentation Documentation changes tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(schedule): gaia schedule add accepts sinks it can't deliver to

2 participants