Skip to content

Validate cron expressions at save time so invalid schedules fail loudly - #1558

Merged
malibio merged 3 commits into
mainfrom
worktree-issue-1508-cron-validation
Jul 6, 2026
Merged

malibio merged 3 commits into
mainfrom
worktree-issue-1508-cron-validation

Conversation

@malibio

@malibio malibio commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

Closes #1508

…ck (closes #1508)

Reject scheduled-trigger playbooks with an invalid or wrong-field-count
cron expression at save time via cron::Schedule::from_str, instead of
silently accepting a schedule that will never fire. Also recover the
cron poll loop's lifecycle lock read from a poisoned state instead of
panicking, which previously stopped the runner permanently.

Co-Authored-By: Claude <[email protected]>
@malibio

malibio commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator Author

Code Review Summary

This PR closes two independent bugs from #1508 with a small, well-scoped diff (2 files, +130/-2 net). Both fixes are correct, tested, and verified locally: cargo build, cargo fmt --check, cargo clippy, and the full playbook::validation::, playbook::cron_runner::, and sync_gate_tests:: suites (56 tests total) all pass with zero warnings. This is a net positive change that should be merged after one clarification (see Improvements) — nothing here blocks merge.

Findings

Critical Issues

None.

Suggested Improvements

  • packages/core/src/playbook/cron_runner.rs line ~81 vs. packages/core/src/playbook/engine.rs (13 other call sites): The fix recovers only the cron poller's lifecycle.read(). engine.rs has 13 other .expect("lifecycle lock poisoned") sites on the same Arc<RwLock<PlaybookLifecycleManager>> instance (confirmed via Arc::clone(&self.lifecycle) passed into cron_runner_loop). Practically, this means: if any of those 13 sites panics and poisons the lock, the cron poller now survives, but the very next graph-event trigger evaluation or lifecycle mutation (activate/deactivate/enable/disable) going through engine.rs will still panic and propagate — likely killing whatever task called into the engine (e.g., the event-subscriber loop), which is arguably just as bad as the bug this PR fixes, just moved one door down.
    I agree the issue text scopes the fix narrowly to "the cron poll loop," and a full audit of all 13 sites is legitimately a larger, separable refactor (some may run on request-handling paths where "propagate the panic" is a defensible choice, vs. background loops where it isn't). Given YAGNI/atomicity principles, keeping this PR scoped is reasonable — but the PR description and/or a tracking issue should say explicitly that the other 13 sites are a known, deliberately-deferred gap, not an oversight. Right now nothing in the codebase signals that the recovery pattern is incomplete, so a future reader may assume poisoning is fully handled for this lock. Suggest either a one-line doc comment where lifecycle is defined (engine.rs) noting "cron poller recovers from poisoned reads; other call sites still propagate — see #," or filing that follow-up issue now.

  • packages/core/src/playbook/cron_runner.rs line ~81, poisoned.into_inner(): Verified this is sound for this data structure. PlaybookLifecycleManager holds only a HashMap/Vec-based index (active_playbooks, trigger_index, cron_registry); poisoning happens because some other writer panicked while holding the guard, not because this reader's own operation is unsafe to resume. Since none of activate_playbook/deactivate_playbook/etc. leave those collections in a type-invalid state on panic (Rust's standard collections are panic-safe for the mutations used here — pure insert/remove/iterate, no unsafe or manual invariant spanning multiple fields), into_inner() recovering "last write before the panic" data is a legitimate and low-risk choice, not a correctness gamble. Worth calling out explicitly in the PR description since it's the kind of decision a future reviewer will want reasoning for without re-deriving it — the in-code comment added is good but doesn't mention why it's safe (i.e., no cross-field invariants), only what it does.

Nitpicks

  • Nit: packages/core/src/playbook/validation.rs — InvalidCronExpression.message stores e.to_string() from cron::error::Error. Worth double-checking (not required for this PR) that this message never leaks anything sensitive; in practice it's just a parse-position diagnostic over a string the same user just typed, so no actual concern — flagging only because external-crate error text is now surfaced directly in a user-facing validation message.
  • Nit: The three tests in validation.rs's integration module (test_invalid_cron_expression_rejected, test_wrong_field_count_cron_rejected, test_valid_cron_expression_passes) and the one in sync_gate_tests (test_invalid_cron_rejected_on_create) overlap somewhat — the latter is a superset (exercises the same rejection through the real create_node gate). That's intentional layering (unit-level validator test + full-stack gate test) and is good practice, not a real duplication concern — just noting it's by design.

Verification Performed

  • Confirmed validate_playbook's new cron check sits inside the per-rule loop, runs unconditionally for every ParsedTrigger::Scheduled rule, and is not short-circuited by earlier checks (errors are accumulated, not returned early).
  • Confirmed both save paths are gated: NodeService::create_node (crud.rs:159) and update_node (crud.rs:848) both call validate_playbook_rules → validate_playbook for node_type == "playbook". No playbook save path bypasses this.
  • Confirmed cron = "0.16" is a pre-existing dependency (not newly added) and its module doc already documents the 7-field (sec/min/hour/dom/month/dow/year) convention used elsewhere in cron_runner.rs, so rejecting standard 5-field cron strings via the same from_str call is consistent with existing runtime behavior, not a new/surprising constraint.
  • Ran cargo test --lib playbook::validation::, playbook::cron_runner::, and sync_gate_tests:: — 40 + 10 + 6 = 56 tests, all passing.
  • Ran cargo fmt -p nodespace-core -- --check and cargo clippy -p nodespace-core --lib — clean, no diffs or warnings.
  • Reviewed the poisoned-lock test: it poisons via catch_unwind in-process (not a separate thread), which is safe under Rust's default unwind panic strategy and won't abort the test binary; it then asserts is_poisoned() before exercising check_and_enqueue, and asserts the recovered path still enqueues the expected work item — this is a meaningful behavioral test, not just a structural/type check.

Acceptance Criteria Coverage

  • Invalid cron expressions are rejected at save time with a clear error — verified end-to-end via test_invalid_cron_rejected_on_create, which goes through the real NodeService::create_node gate and asserts on the surfaced error message.
  • A poisoned lock does not permanently stop the cron runner — verified via check_and_enqueue_recovers_from_poisoned_lock, which poisons the real lock and confirms check_and_enqueue still functions afterward.
  • Code passes bun run quality:fix equivalent (cargo fmt/clippy); tests pass — verified locally.

Recommendation: Approve. The scoping question (partial poisoned-lock coverage) is worth a follow-up issue or a one-line comment for future readers, but it does not regress anything relative to main and the two acceptance-criteria bugs are fixed correctly and tested meaningfully.

@malibio malibio left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review complete (see full report in PR comment above). Recommendation: Approve — both acceptance-criteria bugs fixed correctly and tested meaningfully; net positive change. Note: GitHub blocks self-approval since I'm the PR author (malibio), so this is posted as --comment rather than --approve; treat the recommendation above as the approval verdict.

…rred gap

Addresses code review feedback on #1508 / PR #1558: the cron poller's
lock read now recovers from poisoning, but engine.rs's other lock call
sites still propagate a panic. Note why that's scoped out rather than
an oversight.

Co-Authored-By: Claude <[email protected]>
@malibio

malibio commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator Author

Addressed the review's scoping note: added a doc comment on PlaybookEngine.lifecycle in engine.rs explaining that the cron poller recovers from a poisoned lock while the other 13 .expect("lifecycle lock poisoned") sites in engine.rs still propagate a panic — a deliberate, deferred scope decision (matches the issue's explicit scoping to "the cron poll loop"), not an oversight. No behavior change, cargo build/fmt/test:all all green.

Reviewer asked for the in-code comment to cover *why* recovering a
poisoned lock via into_inner() is safe (no cross-field invariants),
not just what the recovery does. Corrected the claim while at it:
activate_playbook does update trigger_index/cron_registry before
active_playbooks across a loop, so a panic mid-activation could in
principle leave a partial entry — that's a pre-existing risk on this
lock either way, not something the recovery path introduces.

Co-Authored-By: Claude <[email protected]>
@malibio

malibio commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator Author

Addressed Review Feedback

  • ✅ Addressed: Expanded the into_inner() comment in cron_runner.rs to explain why recovering from a poisoned lock is safe here (PlaybookLifecycleManager's fields are plain HashMap/Vec indexes), and corrected an overclaim from the earlier follow-up — activate_playbook does update trigger_index/cron_registry before active_playbooks across a loop, so a panic mid-activation could in principle leave a partial entry. That's a pre-existing risk on this lock regardless of this PR (any .expect(...) reader hitting it mid-activation would panic too) — not something the recovery path introduces.
  • ⏭️ Skipped (per reviewer's own notes): the InvalidCronExpression.message external-crate-text nitpick ("not required for this PR") and the test-overlap nitpick between validation.rs and sync_gate_tests ("not a real duplication concern... by design").

Build, cargo fmt/clippy, and all 50 relevant unit tests pass. Full test:all (including e2e) green on push.

Re-Review Decision

Decision: NO RE-REVIEW NEEDED

Rationale: This follow-up is a documentation-only change (comment text, no logic/behavior/test changes), verified against the actual PlaybookLifecycleManager structure. Combined with the prior review's Approve recommendation, the PR is ready for merge.

@malibio
malibio merged commit 9a50603 into main Jul 6, 2026
@malibio
malibio deleted the worktree-issue-1508-cron-validation branch July 6, 2026 22:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Validate cron expressions at save time so invalid schedules fail loudly

1 participant