Repository navigation
Description
Overview
Playbook cycle detection compares PlaybookExecutionContext.depth against MAX_CHAIN_DEPTH (10), but that context lives only in the in-memory event envelope for the process that produced it — it is never written to the node the action creates or updates. A causal chain that hops from one device to another via sync therefore arrives with no memory of how deep it already is, so no single device can ever observe the depth limit for a chain that alternates devices, and the disable-the-playbook safety valve never trips.
Problem Statement
Verified against current code:
PlaybookExecutionContext(packages/core/src/db/events.rs, ~line 122) is{ originating_event_id, depth: u8, source_playbook_id }. It is constructed fresh inrule_processor_loop(packages/core/src/playbook/engine.rs, ~line 642-651) asdepth: depth + 1, attached to theEventEnvelopethe emitted action's domain event carries, and read back only by comparing againstwork_item.trigger_event.metadata.playbook_contexton the same process (~line 556-566).- Nothing in
execute_actions/execute_create_node(packages/core/src/playbook/actions.rs) writes this depth value onto the node's own persistedproperties. It exists purely as event metadata for the lifetime of the in-process channel send. - Sync transports the node's persisted state, not the transient
EventMetadatathat accompanied its local creation. A node produced by a chain at depth 6 on device A arrives on device B viaNodeCreated/sync-apply with no depth information at all — the receiving engine'srule_processor_loopcomputesdepth = 0(the.unwrap_or(0)default) for any rule the arriving node itself triggers. - Because the sync-service origin gate (ADR-073,
packages/core/src/playbook/engine.rslocal-origin check) currently excludes all sync-originated events from firing rules at all, this specific failure mode (an infinite chain sustained purely by hopping devices) cannot manifest under the gate as it exists today. It becomes reachable again as soon as reactive rules are allowed to fire on sync-applied events per ADR-060 §5's actual design (see Playbook execution under multi-device sync: derived identity, stable ordering, causal cycle depth, and catch-up suppression #1738) — at which point causal depth must already be in place, since retrofitting it after reactive-on-sync ships is the harder order.
Proposed Solution
Per ADR-060 §5 ("Causal cycle depth"): chain depth must travel with the event as causality — carried on the written node itself so it survives sync, and reconstructed by the receiving device from the node's own data rather than defaulting to zero.
- Add a namespaced, internal-only property to nodes created/updated by a playbook action (e.g. following the existing
properties["playbook_log"]nested-namespace precedent inpackages/core/src/playbook/logging.rs, or the_schemaVersion-style reserved-prefix precedent inpackages/core/src/models/task_node.rs) that records the chain depth at the moment the action produced this node. - When a rule fires against a node, the engine reads this persisted depth (if present) as the chain's current depth, instead of relying solely on the in-process
PlaybookExecutionContextfor locally-originated chains. execute_actions/execute_create_nodeinpackages/core/src/playbook/actions.rswrite the depth value into the new property when constructing the output node.rule_processor_loopinpackages/core/src/playbook/engine.rscontinues to enforceMAX_CHAIN_DEPTHagainst whichever depth value is authoritative for the firing (local in-process context, or the node-persisted value for a chain link that arrived via a device hop once Playbook execution under multi-device sync: derived identity, stable ordering, causal cycle depth, and catch-up suppression #1738's reactive-on-sync gating is relaxed).
Acceptance Criteria
- A node created or updated by a playbook action's chain persists its causal depth as a namespaced node property (not a bare top-level field, per project schema-namespace conventions)
- Test: a chain whose events are simulated as crossing a device hop (node received via sync-apply carrying the persisted depth property, then triggering a further rule) accumulates depth correctly rather than resetting to zero
- Test: the existing single-device cycle-depth behavior (
MAX_CHAIN_DEPTH= 10, disable-on-limit, log-node creation) is unchanged - No regression in the existing playbook test suite
- Tests pass (
bun run test:all) - Code passes
bun run quality:fix
Technical Specifications
Reference Files
- Execution context:
packages/core/src/db/events.rs—PlaybookExecutionContext(~line 122) - Depth check / cycle valve:
packages/core/src/playbook/engine.rs—rule_processor_loop(~lines 548-651),MAX_CHAIN_DEPTHinpackages/core/src/playbook/logging.rs(line 29) - Action execution / node writes:
packages/core/src/playbook/actions.rs—execute_actions,execute_create_node - Namespacing precedent:
packages/core/src/playbook/logging.rs(properties["playbook_log"]nesting),packages/core/src/models/task_node.rs(_schemaVersionreserved-prefix convention)
Implementation Details
- This issue only adds the persisted depth field and has the engine consult it; it does not itself change the ADR-073 local-origin gate that currently prevents reactive rules from firing on sync-applied events (that gating change, and the accompanying join-point suppression, is separate scope tracked on Playbook execution under multi-device sync: derived identity, stable ordering, causal cycle depth, and catch-up suppression #1738).
- Depth must be namespaced per the project's schema rules (see
nodespace-docs/decisions/063-namespace-prefixes-scope.md) rather than added as a bare top-level property, per Playbook execution under multi-device sync: derived identity, stable ordering, causal cycle depth, and catch-up suppression #1738's own implementation guidance.
Dependencies
- None — can be implemented independently of Playbook execution under multi-device sync: derived identity, stable ordering, causal cycle depth, and catch-up suppression #1738's remaining reactive-on-sync gating work, though it is only observably exercised once that gating changes
Related Issues
- Playbook execution under multi-device sync: derived identity, stable ordering, causal cycle depth, and catch-up suppression #1738 (parent — this closes the "causal cycle depth" acceptance criterion)
Activity
mstomar125 commentedon Sep 14, 2026
Frontend baseline: 240 test files passed, 4951 tests passed (105.07s).
mstomar125 commentedon Sep 14, 2026
Resolved via PR #2606 (#2606), not yet merged.
Summary:
PLAYBOOK_CHAIN_DEPTH_PROPERTY(_playbookChainDepth) +persisted_chain_depth()reader topackages/core/src/db/events.rs— a_-prefixed reserved node property, same internal-bookkeeping convention as_seed/_schemaVersion(not thecustom:/org:/plugin:ADR-063 namespace prefix, which protects a different, narrower hazard that doesn't apply to engine-internal bookkeeping on nodes of any type).execute_create_node/execute_update_nodeinpackages/core/src/playbook/actions.rsnow stamp this property with the execution context's current depth on every node a play action creates or updates (merged into existing properties on update, not a replace).rule_processor_loopinpackages/core/src/playbook/engine.rsnow computes the depth it checks againstMAX_CHAIN_DEPTHvia a neweffective_chain_depth()helper, which falls back to the node's persisted depth property when no in-processPlaybookExecutionContextis present (the shape of a node that arrived via sync).Tests added:
persisted_chain_depthunit tests (events.rs), integration tests confirming create/update actions persist depth correctly against a real NodeService (actions.rs), andeffective_chain_depthtests including a direct device-hop simulation showing depth continues from the persisted property instead of resetting to 0, plus a boundary test confirming a chain at MAX_CHAIN_DEPTH still trips the cycle limit right after a device hop (playbook/tests.rs). Existing single-device cycle-depth tests are unchanged and pass.bun run test:allandbun run quality:fixboth clean; pre-push gate (test:all + cargo build + test:e2e) passed on push.Persist playbook causal chain depth onto nodes for cross-device cycle…