Repository navigation
Description
Overview
The playbook engine (packages/core/src/playbook/) is a complete, unit-tested Rust runtime — CEL condition evaluation, sequential action execution, cron scheduling, single-device cycle detection, save-time validation. PlaybookEngine::start() has zero call sites anywhere in the repo, including tests. It is never instantiated in packages/daemon/ or packages/desktop-app/src-tauri/.
Problem Statement
A playbook node can be authored today and passes validate_playbook_rules (the only production code path touching playbook nodes, in packages/core/src/services/node_service/crud.rs). But no rule in it will ever fire — there is no running engine subscribed to graph events to evaluate a trigger against. The playbook system is fully specified and coded as a library, but connected to nothing.
Proposed Solution
Instantiate and run the playbook engine at daemon startup, gated to locally-originated events only for this issue's scope.
Architecture Impact
This issue implements ../nodespace-docs/decisions/073-playbook-engine-local-only-until-sync-stable.md (ADR-073), which decided the playbook engine runs hard-gated to locally-originated events until ../nodespace-docs/decisions/060-playbook-execution-under-multi-device-sync.md (ADR-060)'s multi-device semantics are implemented. Full ADR-060 support (invariant-rule transactional execution under sync, derived identity, single-writer election, cross-device causal depth, join-point catch-up suppression) is explicitly out of scope here and tracked separately, per ADR-073's Follow-up section.
This issue does not reverse or supersede any ADR — it implements one already accepted.
Terminology note: this issue's description was written using current code identifiers ("playbook" node_type, validate_playbook_rules). #2569 renames these to "play"/validate_play_rules at the instance level (the engine itself stays named "Playbook" — see #2569 for the full explanation). #2569 should land first; read this issue's "playbook" node_type/function-name references as their post-#2569 renamed equivalents once that lands.
Implementation Approach
Engine wiring
- Instantiate
PlaybookEngineat daemon startup (packages/daemon/). - Subscribe it to the real domain-event broadcast channel owned by
packages/core. Do not model this onDomainEventForwarder— that component isdesktop-app-only per../nodespace-docs/architecture/crate-boundaries.md, andcoremust not depend on or be described in terms of it (this was a doc error inplaybook-engine.md, already corrected). Verify the actual broadcast mechanism directly inpackages/corebefore wiring. - Spawn
CronRunner's 60-second polling loop as a real tokio task at startup.
Local-origin gating (ADR-073)
- The engine's event subscriber must evaluate triggers only for locally-originated events. Events arriving via the sync-apply path must be structurally excluded from trigger evaluation before reaching rule matching — not merely documented as unsupported.
- Follow the existing
source_client_id-based origin-tagging convention used elsewhere in the system (see../nodespace-docs/decisions/027-local-first-sync-service.mdfor the established pattern of tagging sync-service writes and filtering on origin). - Scheduled (
cron) triggers are unaffected by this gating — they scan local graph state directly and carry no sync-origin ambiguity.
Explicitly out of scope
- Full ADR-060 mechanisms: invariant-rule in-transaction execution guarantees under sync, derived identity for reactive-rule outputs, single-writer election, cross-device causal cycle depth, join-point catch-up suppression.
RuleClass::Invariant/Reactiveand existing single-device cycle-depth tracking are unaffected — they already exist and remain as specified.- Per ADR-073's consequences: invariant rules are not sync-safe under this gating alone (their fail-closed guarantee depends on unbuilt ADR-060 §2/§7 mechanisms). Do not treat this issue's gating as making invariant rules safe under sync — note this explicitly in test coverage or follow-up documentation rather than assuming it silently.
Acceptance Criteria
-
PlaybookEngineis instantiated and running as part of normal daemon startup - A play (playbook node instance) fires end-to-end for a local mutation (integration test covering trigger → condition → action)
-
CronRunner's poll loop verifiably runs on a schedule (test or diagnostic confirms it is spawned and ticking) - An event tagged as sync-originated (via the existing
source_client_idconvention) verifiably does not reach trigger evaluation (integration test) - No regression to existing playbook validation tests (
bun run test:allpasses with no new failures) - Code passes
bun run quality:fix
Technical Specifications
Reference Files
packages/core/src/playbook/engine.rs—PlaybookEngine, itsstart()methodpackages/core/src/playbook/lifecycle.rs—PlaybookLifecycleManagerpackages/core/src/playbook/cron_runner.rs—CronRunner, the 60s poll looppackages/core/src/services/node_service/crud.rs(~line 1889) —validate_playbook_rules(renamed by Rename node_type "playbook" to "play" and instance-level identifiers in code #2569), the only current production code path touching playbook/play nodespackages/daemon/src/main.rs— daemon startup, where engine instantiation belongs../nodespace-docs/architecture/crate-boundaries.md—core/desktop-appboundary; confirmsDomainEventForwarderisdesktop-app-only../nodespace-docs/decisions/027-local-first-sync-service.md— existingsource_client_idorigin-tagging pattern to follow
Testing Requirements
- Integration test for local-mutation trigger firing
- Integration test proving sync-originated events are excluded from evaluation
- Existing playbook unit tests (
packages/core/src/playbook/tests.rs) must continue passing unmodified in behavior (only gaining real execution, not changing rule semantics)
Dependencies
- Rename node_type "playbook" to "play" and instance-level identifiers in code #2569 (renames
node_type: "playbook"→"play"and instance-level identifiers in code; should land first so this issue's implementation uses correct terminology from the start)
Related Issues
- Blocks a follow-up issue exposing playbook lifecycle +
get-workflow-statevia CLI and skill guidance (to be filed, depends on this issue)
Non-Goals
- Full ADR-060 multi-device semantics (tracked separately per ADR-073)
- Any CLI, skill, or MCP-adjacent external control surface for playbooks (separate issue)
Activity
mstomar125 commentedon Sep 11, 2026
Resolution summary
PR: #2575
Engine wiring:
PlaybookEngineis now instantiated per database inpackages/daemon/src/services/assembly.rs::build_database_services, right afterNodeServiceconstruction — mirroring the conflict-journal sweep's existing per-database background-task shape (ownwatch::channelshutdown signal, stopped byDatabaseServices::shutdown).PlaybookEngine::start()subscribes toNodeService's real domain-event broadcast channel viasubscribe_to_events()(atokio::sync::broadcast::Receiver<EventEnvelope>— verified directly inpackages/core/src/services/node_service/mod.rs, not modeled onDomainEventForwarder) and spawnsCronRunner's 60-second poll loop itself.Local-origin gating (ADR-073): Added
SYNC_SERVICE_CLIENT_IDas a reserved constant inpackages/core/src/db/events.rs, following the exact existingsource_client_id/with_clientorigin-tagging convention from ADR-027 (the same pattern already used bypush_forward_allowed/push_excluded_origininnode_service/mod.rs).PlaybookEngine::handle_eventnow checks a newis_sync_originatedhelper before anyTriggerKeylookup and returns early for sync-tagged events — structurally excluded, not just documented. Play lifecycle management (install/uninstall/schema-drift) stays ungated since Play nodes are data like any other; only rule-triggering evaluation of mutation events is excluded for sync-applied writes. Cron is unaffected by construction (scans local state directly).Testing:
packages/core/tests/playbook_engine_integration_test.rsadds three real integration tests (running engine, realNodeService/SQLite, no mocks):play_fires_end_to_end_for_local_mutation— trigger → condition → action, plus a condition-failing control node left untouched. Pass.sync_originated_event_does_not_reach_trigger_evaluation— a node created viaNodeService::with_client(SYNC_SERVICE_CLIENT_ID)stays untouched, while a same-shape local control node fires (ruling out "play never activated" as a false-negative explanation). Pass.cron_runner_ticks_and_fires_a_scheduled_play—#[tokio::test(start_paused = true)]+tokio::time::advanceto fast-forward past the real 60s poll interval; confirmsCronRunneractually ticks and fires. Pass.Mutation-tested the safety-critical gate: temporarily forced
is_sync_originatedto always returnfalse— both the unit test and the sync-integration test failed with a clear diagnostic (the sync-tagged node's property flipped exactly like the local one). Reverted, reconfirmed green.Full local gate:
bun run test:allandbun run quality:fixboth pass;git push's pre-push gate (fulltest:all+cargo build --bin nodespaced+test:e2e) also passed.Per ADR-073, this does not make
RuleClass::Invariantrules sync-safe — noted explicitly in the test file's module doc comment.