Skip to content

Wire playbook engine into daemon startup, hard-gated to local-only events #2562

Closed

Description

@malibio

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 PlaybookEngine at daemon startup (packages/daemon/).
  • Subscribe it to the real domain-event broadcast channel owned by packages/core. Do not model this on DomainEventForwarder — that component is desktop-app-only per ../nodespace-docs/architecture/crate-boundaries.md, and core must not depend on or be described in terms of it (this was a doc error in playbook-engine.md, already corrected). Verify the actual broadcast mechanism directly in packages/core before 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.md for 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/Reactive and 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

  • PlaybookEngine is 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_id convention) verifiably does not reach trigger evaluation (integration test)
  • No regression to existing playbook validation tests (bun run test:all passes with no new failures)
  • Code passes bun run quality:fix

Technical Specifications

Reference Files

  • packages/core/src/playbook/engine.rs — PlaybookEngine, its start() method
  • packages/core/src/playbook/lifecycle.rs — PlaybookLifecycleManager
  • packages/core/src/playbook/cron_runner.rs — CronRunner, the 60s poll loop
  • packages/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 nodes
  • packages/daemon/src/main.rs — daemon startup, where engine instantiation belongs
  • ../nodespace-docs/architecture/crate-boundaries.md — core/desktop-app boundary; confirms DomainEventForwarder is desktop-app-only
  • ../nodespace-docs/decisions/027-local-first-sync-service.md — existing source_client_id origin-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

Related Issues

  • Blocks a follow-up issue exposing playbook lifecycle + get-workflow-state via 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

  1. self-assigned this
    on Sep 11, 2026
  2. mstomar125 commented on Sep 11, 2026

    @mstomar125
    Contributor

    Resolution summary

    PR: #2575

    Engine wiring: PlaybookEngine is now instantiated per database in packages/daemon/src/services/assembly.rs::build_database_services, right after NodeService construction — mirroring the conflict-journal sweep's existing per-database background-task shape (own watch::channel shutdown signal, stopped by DatabaseServices::shutdown). PlaybookEngine::start() subscribes to NodeService's real domain-event broadcast channel via subscribe_to_events() (a tokio::sync::broadcast::Receiver<EventEnvelope> — verified directly in packages/core/src/services/node_service/mod.rs, not modeled on DomainEventForwarder) and spawns CronRunner's 60-second poll loop itself.

    Local-origin gating (ADR-073): Added SYNC_SERVICE_CLIENT_ID as a reserved constant in packages/core/src/db/events.rs, following the exact existing source_client_id/with_client origin-tagging convention from ADR-027 (the same pattern already used by push_forward_allowed/push_excluded_origin in node_service/mod.rs). PlaybookEngine::handle_event now checks a new is_sync_originated helper before any TriggerKey lookup 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.rs adds three real integration tests (running engine, real NodeService/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 via NodeService::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::advance to fast-forward past the real 60s poll interval; confirms CronRunner actually ticks and fires. Pass.

    Mutation-tested the safety-critical gate: temporarily forced is_sync_originated to always return false — 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:all and bun run quality:fix both pass; git push's pre-push gate (full test:all + cargo build --bin nodespaced + test:e2e) also passed.

    Per ADR-073, this does not make RuleClass::Invariant rules sync-safe — noted explicitly in the test file's module doc comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions