Repository navigation
Wire playbook engine into daemon startup, hard-gated to local-only events - #2575
Merged
Merged
Conversation
…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]>
6 tasks
…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]>
10 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Addresses #2562.
PlaybookEngineper database inpackages/daemon/src/services/assembly.rs::build_database_services, alongsideNodeServiceconstruction — the same per-database background-task shape already used for the conflict-journal reconciliation sweep, with its ownwatch::channelshutdown signal stopped byDatabaseServices::shutdown.PlaybookEngine::start()subscribes toNodeService's realEventEnvelopebroadcast channel (subscribe_to_events()) and spawnsCronRunner's 60-second poll loop itself — nothing else needs to.packages/core/src/playbook/engine.rs:PlaybookEngine::handle_eventnow calls a newis_sync_originatedcheck before trigger matching and skips evaluation entirely for any event whoseEventEnvelope.metadata.source_client_idequals the newSYNC_SERVICE_CLIENT_IDreserved constant (added topackages/core/src/db/events.rs), following the existingNodeService::with_client/source_client_idorigin-tagging convention from ADR-027 (the same patternpush_forward_allowed/push_excluded_originalready uses elsewhere innode_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.packages/core/tests/playbook_engine_integration_test.rs: three integration tests driving a real, runningPlaybookEngine::start()against a realNodeService/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), andCronRunner's poll loop actually ticking (via a paused virtual clock +tokio::time::advance) and firing a scheduled play's action.is_sync_originatedfilter inpackages/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::Invariantrules 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.rsortypes.rs(off-limits per the issue — concurrent unrelated work),packages/agent/, any CLI/skill/MCP surface, orpackages/desktop-app/.Test plan
cargo test -p nodespace-core --lib playbook::— 241 passedcargo 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 (includingdatabase_manager::tests exercisingDatabaseServices::shutdown)is_sync_originatedto always returnfalse, confirmed both the new unit test and thesync_originated_event_does_not_reach_trigger_evaluationintegration test fail with a clear diagnostic; reverted and reconfirmed green.bun run test:all— full local gate, passesbun run quality:fix— passes (cargo fmt,cargo clippy -D warnings -D clippy::unused-async, eslint, svelte-check, tsc)git pushpre-push gate (test:all+cargo build --bin nodespaced+test:e2e) — passes🤖 Generated with Claude Code