Repository navigation
Persist playbook causal chain depth onto nodes for cross-device cycle detection - #2606
Merged
Merged
Conversation
… 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
force-pushed
the
issue-2578-causal-depth-persistence
branch
from
September 14, 2026 06:33
9528880 to
9038d4f
Compare
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
PlaybookExecutionContext.depth(packages/core/src/db/events.rs) previously lived only in the in-processEventEnvelopemetadata built fresh byrule_processor_loopeach 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, soMAX_CHAIN_DEPTHcould never see the chain's true depth once it crossed a device boundary.PLAYBOOK_CHAIN_DEPTH_PROPERTY(_playbookChainDepth) and apersisted_chain_depthreader indb::events. This follows the existing_-prefixed internal-bookkeeping convention (_seed,_schemaVersion) rather than thecustom:/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 ofpropertiesregardless ofnode_type(NodeService::normalize_flat_properties_to_namespacenever nests_-prefixed keys under the node's type namespace) and keeps it out of CEL condition bindings (playbook::cel::node_to_cel_valuefilters_-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 anupdate_nodeaction that only changescontentorlifecycle_status, since the classic runaway-chain shape is a node repeatedly re-triggering itself through any field, not justproperties. The stamp merges into the node's existing properties rather than replacing them (NodeServicedeep-mergesNodeUpdate.properties).rule_processor_loop(packages/core/src/playbook/engine.rs) now computes the depth to check againstMAX_CHAIN_DEPTHvia a neweffective_chain_depth(work_item)helper: it prefers the in-processPlaybookExecutionContext(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:
persisted_chain_depthaccepted the fullu8range (0-255) from a node'sproperties— ordinary data anycreate_node/update_nodecaller can write, not something only this engine produces. A persisted value of exactly 255 passed through unbounded, andrule_processor_loop's uncheckeddepth + 1 > MAX_CHAIN_DEPTHthen overflowed and silently wrapped to0in a release build (this repo's release profile leavesoverflow-checksoff by default) — the guard would read "not exceeded" and the chain's depth tracking would be reset. Fixed:persisted_chain_depthnow takesmax_depthand rejects anything above it as corrupt/absent rather than clamping; the twodepth + 1sites inengine.rsnow go through a newexceeds_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 thepersisted_chain_depthlevel and the arithmetic itself._-prefixed properties are excluded from CEL condition bindings, butnode_to_cel_value's top-level branch (the shape_playbookChainDepthactually 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):
NodeServicestill doesn't restrict a non-engine caller from writing/deleting_playbookChainDepthdirectly, andgraph_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 forpersisted_chain_depth(reads the reserved property, absent/empty/non-numeric handling, and bound-checking againstmax_depth— including the255-does-not-pass-through regression and exact-boundary cases).packages/core/src/playbook/actions.rs(chain_depth_persistence_integration, realNodeService): acreate_nodeaction persists the execution context's depth onto the output node; anupdate_nodeaction 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-processPlaybookExecutionContext(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 atMAX_CHAIN_DEPTHwhen it crosses a device hop still trips the cycle limit on the next local hop; a persisted255is treated as absent (falls back to 0), not as 255;exceeds_max_chain_depthdoesn't wrap atu8::MAX.packages/core/src/playbook/cel.rs:node_to_cel_valueexcludes_-prefixed keys at the top level, not just when nested under the type namespace.MAX_CHAIN_DEPTHtests are unchanged and still pass.Test plan
bun run test:all— clean (240 frontend test files / 4951 tests, full Rust workspace includingnodespace-core's 1350+ lib tests,test:scripts,test:skill)bun run quality:fix— clean (cargo fmt+cargo clippy -D warnings -D clippy::unused-async)scripts/test-gate.ts) passed on both pushes:test:all,cargo build --bin nodespaced, fulltest:e2eCloses #2578
🤖 Generated with Claude Code