Skip to content

Persist playbook causal chain depth onto nodes for cross-device cycle detection - #2606

Merged
mstomar125 merged 2 commits into
mainfrom
issue-2578-causal-depth-persistence
Sep 14, 2026
Merged

mstomar125 merged 2 commits into
mainfrom
issue-2578-causal-depth-persistence

Conversation

@mstomar125

@mstomar125 mstomar125 commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • PlaybookExecutionContext.depth (packages/core/src/db/events.rs) previously lived only in the in-process EventEnvelope metadata built fresh by rule_processor_loop each time — nothing wrote it onto the node a play action produced. A causal chain that hopped from device A (where it created/updated a node) to device B (where that node syncs in and a local rule fires on it) arrived with no depth information, so MAX_CHAIN_DEPTH could never see the chain's true depth once it crossed a device boundary.
  • Added PLAYBOOK_CHAIN_DEPTH_PROPERTY (_playbookChainDepth) and a persisted_chain_depth reader in db::events. This follows the existing _-prefixed internal-bookkeeping convention (_seed, _schemaVersion) rather than the custom:/org:/plugin: namespace-prefix convention from ADR-063 — that convention exists to prevent a user's property on a type they extend from colliding with a future core property, which doesn't apply here: this is internal engine bookkeeping that a play action can write onto a node of any type, not a user- or schema-authored field. The _ prefix keeps it at the top level of properties regardless of node_type (NodeService::normalize_flat_properties_to_namespace never nests _-prefixed keys under the node's type namespace) and keeps it out of CEL condition bindings (playbook::cel::node_to_cel_value filters _-prefixed keys in both the type-namespace branch and the top-level branch it actually lands in).
  • execute_create_node/execute_update_node (packages/core/src/playbook/actions.rs) now stamp this property with the execution context's depth on every node a play action creates or updates — including an update_node action that only changes content or lifecycle_status, since the classic runaway-chain shape is a node repeatedly re-triggering itself through any field, not just properties. The stamp merges into the node's existing properties rather than replacing them (NodeService deep-merges NodeUpdate.properties).
  • rule_processor_loop (packages/core/src/playbook/engine.rs) now computes the depth to check against MAX_CHAIN_DEPTH via a new effective_chain_depth(work_item) helper: it prefers the in-process PlaybookExecutionContext (unchanged single-device behavior), falling back to the depth persisted on the trigger node's own properties when that in-process context is absent — the shape a node takes once it has arrived via sync.

Scope note

This does not touch the ADR-073 local-origin gate that currently excludes sync-applied events from trigger evaluation entirely (tracked separately on #1738) — that gate means this fix is not yet observable end-to-end via live sync until it relaxes. It only ensures the persisted depth is correct and consulted, so the gate can relax later without also needing a depth-tracking retrofit.

Addresses part of #1738 — the "causal cycle depth" acceptance criterion of that still-open parent epic. #1738 itself should stay open; a separate comment there notes this criterion is done.

Review round 1 — addressed

Adversarial review flagged two issues, both fixed in a follow-up commit:

  1. Unchecked overflow + no validation on the externally-writable persisted depth (blocking). persisted_chain_depth accepted the full u8 range (0-255) from a node's properties — ordinary data any create_node/update_node caller can write, not something only this engine produces. A persisted value of exactly 255 passed through unbounded, and rule_processor_loop's unchecked depth + 1 > MAX_CHAIN_DEPTH then overflowed and silently wrapped to 0 in a release build (this repo's release profile leaves overflow-checks off by default) — the guard would read "not exceeded" and the chain's depth tracking would be reset. Fixed: persisted_chain_depth now takes max_depth and rejects anything above it as corrupt/absent rather than clamping; the two depth + 1 sites in engine.rs now go through a new exceeds_max_chain_depth() / saturating_add(1), defense-in-depth against an out-of-range depth reaching the arithmetic by any path. Added regression tests for the 255 case both at the persisted_chain_depth level and the arithmetic itself.
  2. Factually-wrong CEL claim (non-blocking, fixed anyway). The doc comment claimed _-prefixed properties are excluded from CEL condition bindings, but node_to_cel_value's top-level branch (the shape _playbookChainDepth actually takes, since it's never nested under the node's type namespace) had no such filter — only the type-namespace-unwrap branch did. Fixed the else-branch to apply the same filter, corrected the doc comments, added a regression test.

Not addressed (lower priority / different mechanism, per review): NodeService still doesn't restrict a non-engine caller from writing/deleting _playbookChainDepth directly, and graph_resolver::get_node_property's explicit dotted-path lookup wasn't audited for the same _-prefix gap. Tracked as a follow-up rather than folded into this PR.

Tests

  • packages/core/src/db/events.rs: unit tests for persisted_chain_depth (reads the reserved property, absent/empty/non-numeric handling, and bound-checking against max_depth — including the 255-does-not-pass-through regression and exact-boundary cases).
  • packages/core/src/playbook/actions.rs (chain_depth_persistence_integration, real NodeService): a create_node action persists the execution context's depth onto the output node; an update_node action persists depth while preserving the node's other existing properties; a re-stamp overwrites a stale prior value.
  • packages/core/src/playbook/tests.rs (effective_chain_depth_* / exceeds_max_chain_depth_*): the "device hop" scenario — a node built with only a persisted depth property and no in-process PlaybookExecutionContext (exactly the shape of a sync-arrived node) continues the chain from that persisted depth instead of resetting to 0; the in-process context wins when both are present (no change to single-device behavior); a chain already at MAX_CHAIN_DEPTH when it crosses a device hop still trips the cycle limit on the next local hop; a persisted 255 is treated as absent (falls back to 0), not as 255; exceeds_max_chain_depth doesn't wrap at u8::MAX.
  • packages/core/src/playbook/cel.rs: node_to_cel_value excludes _-prefixed keys at the top level, not just when nested under the type namespace.
  • Existing single-device cycle-depth/MAX_CHAIN_DEPTH tests are unchanged and still pass.

Test plan

  • bun run test:all — clean (240 frontend test files / 4951 tests, full Rust workspace including nodespace-core's 1350+ lib tests, test:scripts, test:skill)
  • bun run quality:fix — clean (cargo fmt + cargo clippy -D warnings -D clippy::unused-async)
  • Pre-push gate (scripts/test-gate.ts) passed on both pushes: test:all, cargo build --bin nodespaced, full test:e2e

Closes #2578

🤖 Generated with Claude Code

mstomar125 and others added 2 commits September 14, 2026 08:22
… detection

PlaybookExecutionContext.depth previously lived only in the in-process
EventEnvelope metadata, so a chain that hopped devices via sync arrived
with no depth information and reset to 0 on the receiving device --
MAX_CHAIN_DEPTH could never see a chain's true depth once it crossed a
device boundary.

Add PLAYBOOK_CHAIN_DEPTH_PROPERTY (db::events), a reserved `_`-prefixed
node property following the _seed/_schemaVersion internal-bookkeeping
convention: stored at the top level of `properties`, independent of
node_type, and stripped from CEL bindings and frontend property reads.
execute_create_node/execute_update_node (playbook::actions) stamp it
with the execution context's depth on every node a play action produces.
rule_processor_loop's cycle check (playbook::engine) now reads this via
a new effective_chain_depth helper, falling back to the persisted
property when no in-process PlaybookExecutionContext is present -- the
shape of a node that arrived through sync.

Does not touch the ADR-073 local-origin gate that currently excludes
sync-applied events from trigger evaluation (tracked separately on
#1738); this only ensures the persisted depth is correct and consulted
once that gate relaxes.

Closes #2578
Addresses part of #1738

Co-Authored-By: Claude Sonnet 5 <[email protected]>
…tic, close CEL leak

Review findings on PR #2606:

1. persisted_chain_depth() accepted the full u8 range (0-255) from a node's
   properties -- ordinary data any create_node/update_node caller can write,
   not something only this engine produces. A persisted value of 255 passed
   through unbounded; rule_processor_loop's unchecked `depth + 1 >
   MAX_CHAIN_DEPTH` then overflowed and silently wrapped to 0 in a release
   build (overflow-checks off by default), making the guard read "not
   exceeded" and resetting the chain's depth tracking. Fix: persisted_chain_depth
   now takes max_depth and rejects anything above it as corrupt/absent rather
   than clamping it; the two `depth + 1` sites (engine.rs) now go through a
   new exceeds_max_chain_depth()/saturating_add(1), defense-in-depth against
   an out-of-range depth reaching the arithmetic by any path, not solely
   reliant on the bound check holding. Added regression tests for the 255
   case at both the persisted_chain_depth level and the arithmetic itself.

2. The claim that `_`-prefixed properties are excluded from CEL condition
   bindings didn't hold for node_to_cel_value's top-level branch (playbook::cel)
   -- the shape _playbookChainDepth actually takes, since it's never nested
   under the node's type namespace. Only the type-namespace-unwrap branch
   filtered `_`-prefixed keys; the branch handling every other top-level key
   had no such filter and inserted them into the CEL map verbatim. Fixed the
   else-branch to apply the same filter, corrected the doc comments that made
   the now-accurate claim, and added a regression test.

Not addressed here (lower priority / different mechanism, per review):
NodeService itself still does not restrict a non-engine caller from writing
or deleting _playbookChainDepth; graph_resolver::get_node_property's explicit
dotted-path lookup was not audited for the same `_`-prefix gap. Tracked as a
follow-up rather than folded into this PR's scope.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@mstomar125
mstomar125 force-pushed the issue-2578-causal-depth-persistence branch from 9528880 to 9038d4f Compare September 14, 2026 06:33
@mstomar125
mstomar125 merged commit 76cfb1f into main Sep 14, 2026
@mstomar125
mstomar125 deleted the issue-2578-causal-depth-persistence branch September 14, 2026 06:46
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.

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

1 participant