Skip to content

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

Merged
mstomar125 merged 2 commits into
mainfrom
issue-2562-playbook-daemon-wiring
Sep 11, 2026
Merged

mstomar125 merged 2 commits into
mainfrom
issue-2562-playbook-daemon-wiring

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

Summary

Addresses #2562.

  • Instantiates PlaybookEngine per database in packages/daemon/src/services/assembly.rs::build_database_services, alongside NodeService construction — the same per-database background-task shape already used for the conflict-journal reconciliation sweep, with its own watch::channel shutdown signal stopped by DatabaseServices::shutdown. PlaybookEngine::start() subscribes to NodeService's real EventEnvelope broadcast channel (subscribe_to_events()) and spawns CronRunner's 60-second poll loop itself — nothing else needs to.
  • Adds the ADR-073 local-origin gate in packages/core/src/playbook/engine.rs: PlaybookEngine::handle_event now calls a new is_sync_originated check before trigger matching and skips evaluation entirely for any event whose EventEnvelope.metadata.source_client_id equals the new SYNC_SERVICE_CLIENT_ID reserved constant (added to packages/core/src/db/events.rs), following the existing NodeService::with_client / source_client_id origin-tagging convention from ADR-027 (the same pattern push_forward_allowed/push_excluded_origin already uses elsewhere in node_service/mod.rs) rather than inventing a new mechanism. Play lifecycle management (install/uninstall/enable-disable, schema-drift detection) is deliberately not gated — a Play node authored on another device is data like any other and must still install locally once synced. Only trigger evaluation against a mutation event is excluded for sync-applied writes. Cron triggers are unaffected by construction — they scan local graph state directly and never go through this event-driven path.
  • Adds packages/core/tests/playbook_engine_integration_test.rs: three integration tests driving a real, running PlaybookEngine::start() against a real NodeService/SQLite store (not mocked) — a local mutation firing end-to-end (trigger → condition → action, plus a condition-failing control), a sync-tagged mutation verifiably not reaching trigger evaluation (plus a same-shape local control proving the engine would have fired otherwise), and CronRunner's poll loop actually ticking (via a paused virtual clock + tokio::time::advance) and firing a scheduled play's action.
  • Adds unit coverage for the new is_sync_originated filter in packages/core/src/playbook/tests.rs.

Per ADR-073's Consequences section: this gate makes reactive rules local-device-only until ADR-060 lands, and does not make RuleClass::Invariant rules sync-safe — their fail-closed guarantee still depends on unbuilt ADR-060 §2/§7 mechanisms. Noted explicitly in the integration test file's module doc comment.

Scope

Deliberately did not touch packages/core/src/playbook/actions.rs or types.rs (off-limits per the issue — concurrent unrelated work), packages/agent/, any CLI/skill/MCP surface, or packages/desktop-app/.

Test plan

  • cargo test -p nodespace-core --lib playbook:: — 241 passed
  • cargo test -p nodespace-core --test playbook_engine_integration_test — 3 passed (play_fires_end_to_end_for_local_mutation, sync_originated_event_does_not_reach_trigger_evaluation, cron_runner_ticks_and_fires_a_scheduled_play)
  • cargo test -p nodespace-daemon --lib — 204 passed (including database_manager:: tests exercising DatabaseServices::shutdown)
  • Mutation-tested the gate: temporarily forced is_sync_originated to always return false, confirmed both the new unit test and the sync_originated_event_does_not_reach_trigger_evaluation integration test fail with a clear diagnostic; reverted and reconfirmed green.
  • bun run test:all — full local gate, passes
  • bun run quality:fix — passes (cargo fmt, cargo clippy -D warnings -D clippy::unused-async, eslint, svelte-check, tsc)
  • git push pre-push gate (test:all + cargo build --bin nodespaced + test:e2e) — passes

🤖 Generated with Claude Code

…events

PlaybookEngine::start() had zero call sites in the repo. Spawn it per
database in build_database_services (assembly.rs), alongside NodeService
construction — the same per-database background-task shape already used
for the conflict-journal sweep, with its own watch-channel shutdown signal
stopped by DatabaseServices::shutdown. start() subscribes to NodeService's
real EventEnvelope broadcast channel and spawns CronRunner's 60-second
poll loop itself.

Add the ADR-073 local-origin gate: the engine's event subscriber now skips
trigger evaluation for any event tagged with the reserved sync-service
source_client_id (SYNC_SERVICE_CLIENT_ID, added to db::events), following
the existing with_client/source_client_id origin-tagging convention from
ADR-027 rather than inventing a new mechanism. Play lifecycle management
(install/uninstall/schema-drift) stays ungated since a Play node is data
like any other; only rule-triggering evaluation is excluded for
sync-applied writes. Cron triggers are unaffected — they scan local graph
state directly.

Add packages/core/tests/playbook_engine_integration_test.rs: three
integration tests driving a real running engine against a real
NodeService/SQLite store — a local mutation firing end-to-end
(trigger -> condition -> action), a sync-tagged mutation verifiably not
reaching trigger evaluation, and CronRunner's poll loop actually ticking
(via a paused virtual clock) and firing a scheduled play. Add unit
coverage for the new is_sync_originated filter.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
…s it

SqliteStore::batch_create_nodes hardcoded source: None, playbook_context:
None for every node in the batch, discarding whatever client_id the
calling NodeService carried. This silently broke the ADR-073 local-origin
gate for the real sync catch-up path: nodespace-sync's reconnect/gap-catch-up
flow applies pulled backlog pages via NodeService::bulk_create on a
"sync-service"-tagged handle, so every NodeCreated event from that path
landed with source_client_id: None instead of Some("sync-service") --
indistinguishable from a genuine local mutation to is_sync_originated,
letting the play engine fire reactive rules against sync-replayed backlog
data. bulk_update and bulk_delete already threaded client_id through
correctly; only bulk_create's batch path was missing it.

Thread source/playbook_context through batch_create_nodes the same way
single-row create_node already does, and pass self.client_id.clone() /
self.execution_context.clone() from NodeService::bulk_create.

Add an integration test mirroring the real sync-apply shape through the
batched path: NodeService::with_client(SYNC_SERVICE_CLIENT_ID).bulk_create(..)
must not reach trigger evaluation, with a same-shape local bulk_create
control proving the engine would otherwise have fired. Mutation-tested by
reverting the fix locally and confirming the new test (and only that test)
fails with the exact backlog-replay symptom, then re-confirmed green.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
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.

1 participant