Skip to content

Persist causal chain depth on synced nodes so cycle detection survives a device hop (ADR-060 §5) #2578

Closed

Description

@mstomar125

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 in rule_processor_loop (packages/core/src/playbook/engine.rs, ~line 642-651) as depth: depth + 1, attached to the EventEnvelope the emitted action's domain event carries, and read back only by comparing against work_item.trigger_event.metadata.playbook_context on 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 persisted properties. 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 EventMetadata that accompanied its local creation. A node produced by a chain at depth 6 on device A arrives on device B via NodeCreated/sync-apply with no depth information at all — the receiving engine's rule_processor_loop computes depth = 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.rs local-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 in packages/core/src/playbook/logging.rs, or the _schemaVersion-style reserved-prefix precedent in packages/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 PlaybookExecutionContext for locally-originated chains.
  • execute_actions/execute_create_node in packages/core/src/playbook/actions.rs write the depth value into the new property when constructing the output node.
  • rule_processor_loop in packages/core/src/playbook/engine.rs continues to enforce MAX_CHAIN_DEPTH against 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_DEPTH in packages/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 (_schemaVersion reserved-prefix convention)

Implementation Details

Dependencies

Related Issues

Activity

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

    @mstomar125
    ContributorAuthor

    Frontend baseline: 240 test files passed, 4951 tests passed (105.07s).

  3. mstomar125 commented on Sep 14, 2026

    @mstomar125
    ContributorAuthor

    Resolved via PR #2606 (#2606), not yet merged.

    Summary:

    • Added PLAYBOOK_CHAIN_DEPTH_PROPERTY (_playbookChainDepth) + persisted_chain_depth() reader to packages/core/src/db/events.rs — a _-prefixed reserved node property, same internal-bookkeeping convention as _seed/_schemaVersion (not the custom:/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_node in packages/core/src/playbook/actions.rs now 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_loop in packages/core/src/playbook/engine.rs now computes the depth it checks against MAX_CHAIN_DEPTH via a new effective_chain_depth() helper, which falls back to the node's persisted depth property when no in-process PlaybookExecutionContext is present (the shape of a node that arrived via sync).
    • Does not touch the ADR-073 local-origin gate (tracked separately, still open on Playbook execution under multi-device sync: derived identity, stable ordering, causal cycle depth, and catch-up suppression #1738) — this fix is currently only observable in tests/local scenarios until that gate relaxes, exactly as scoped.

    Tests added: persisted_chain_depth unit tests (events.rs), integration tests confirming create/update actions persist depth correctly against a real NodeService (actions.rs), and effective_chain_depth tests 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:all and bun run quality:fix both clean; pre-push gate (test:all + cargo build + test:e2e) passed on push.

  4. added a commit that references this issue on Sep 14, 2026
    ee71795
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