Skip to content

Derive playbook create_node output ids from (rule, action, iteration path) - #2573

Merged
mstomar125 merged 2 commits into
mainfrom
issue-2565-derived-identity
Sep 11, 2026
Merged

mstomar125 merged 2 commits into
mainfrom
issue-2565-derived-identity

Conversation

@mstomar125

@mstomar125 mstomar125 commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Implements ADR-060 §3 (derived identity for reactive-rule outputs) and its ADR-074 generalization (scan-based/scheduled triggers and nested for_each) for playbook create_node action outputs. execute_create_node previously assigned a random id via Node::new(...), so N devices independently executing the same rule against the same trigger each created a distinct node and sync merged them into N duplicates instead of one.

  • deterministic_action_output_id(rule_id, action_index, iteration_path) derives a stable id (UUIDv5 over a fixed namespace), following the existing deterministic_collection_id/deterministic_conflict_id pattern.
  • iteration_path (playbook::types::IterationPath) starts as [trigger_node.id] — this already covers both the graph_event trigger-node case and the scheduled trigger's scanned-node case, since the engine hands the scanned node to the action executor as trigger_node either way — and gains one real node id per nested for_each level entered (never a positional/loop index).
  • execute_create_node checks for an existing node at the derived id before inserting; if found, it's returned as-is (the intended convergence outcome), not an error.
  • A for_each item with no resolvable real node id (neither a bare string nor an object with a non-empty "id" field) fails the rule via a new ActionError::IterationPathResolutionFailed, rather than silently falling back to a scan-order-dependent positional index.

Design note: rule_id without touching engine.rs

This task's scope boundary kept engine.rs/lifecycle.rs/cron_runner.rs/packages/daemon/ off-limits (concurrent work in progress there on daemon-wiring, #2562). execute_actions's only call site is in engine.rs, and ParsedRule carries no id/index of its own — so instead of threading a positional rule_index in as a new parameter (which needs an engine.rs call-site change), rule_id_for(play_id, actions) derives the rule's identity from data already available inside execute_actions: the play id (already passed via execution_context.source_playbook_id) and the rule's own action list (the actions: &[ParsedAction] parameter already is one rule's actions).

Trade-offs of this choice (reworded after review to state them accurately, rather than attributing them to ADR-060 §3, which doesn't describe either one):

  • Editing any action in a rule changes every action's derived id in that rule. rule_id_for hashes the entire action list into one rule_id, so editing one action's params — content, order, or nesting, anywhere in the rule — changes the derived id of every action in that rule, including untouched siblings. This is broader than what ADR-060 §3 itself names (reordering shifting action_index), and it's this implementation's own consequence, not something the ADR already accepted. Documented in the module doc and the rule_id_for_changes_when_the_rule_is_edited test.
  • Two different rules in the same play with byte-identical action lists collide onto the same rule_id. Not a narrow edge case: the realistic trigger is copy-paste rule authoring (duplicate a rule, change its trigger/condition, leave the action list untouched), and the two rules don't even need to fire from the same event — only to eventually act on the same node (e.g. rule A on node_created, rule B later on property_changed for that same node). The failure mode is a silent incorrect merge, not a duplicate or an error: execute_create_node's existing-node check treats rule B's output as "already converged" and quietly returns rule A's node. Made explicit (not just described) by the new two_rules_with_identical_actions_silently_share_one_output_node test. Not fixed in this PR — tracked as a follow-up: a save-time validation.rs check that rejects/warns on two same-play rules with byte-identical action lists (Reject or warn at save time on two same-play rules with byte-identical action lists #2574).

Formula

derived_id = deterministic_id(rule_id, action_index, iteration_path)

Tests (all passing)

  • Pure unit tests for deterministic_action_output_id / rule_id_for / resolve_iteration_path_item_id (stability, distinctness by action_index/path-content/path-order/rule_id).
  • graph_event_depth_1_two_executions_converge_to_one_node — two independent executions of the same graph_event rule against the same trigger converge to one node.
  • scheduled_for_each_produces_one_distinct_id_per_item — a for_each over 3 scanned items produces 3 distinct ids, not 1 collapsed id.
  • nested_for_each_incorporates_both_iteration_levels — the same leaf item id under two different outer scan nodes derives two different ids, proving both levels feed the hash.
  • scan_order_does_not_affect_the_resulting_id_set — two independent "device" stores scanning the same set in different orders derive the same set of ids.
  • for_each_item_without_resolvable_id_fails_the_rule — hard error instead of a scan-order-dependent fallback.
  • rule_id_for_changes_when_the_rule_is_edited — a rule edit changes derived identity from that point on (doc corrected to describe this implementation's actual, broader trade-off, not ADR-060 §3's narrower one).
  • two_rules_with_identical_actions_silently_share_one_output_node — new: makes the byte-identical-action-list collision's real behavior (silent merge, no error) explicit and observable.

Also documented (module doc, not solved here): ADR-060 failure mode 5 (divergent scan results across devices mid-catch-up) remains a separate, unclosed gap — derived identity guarantees convergence of whatever outputs are produced, not that the same number of outputs are produced by every device.

Non-goals (unchanged)

Test plan

  • bun run test:all — full pass (frontend + scripts + skill + Rust workspace/core/libsql-linked/app-units), 0 failures, 70 test result: ok. blocks.
  • bun run quality:fix — 0 warnings (cargo clippy --all-targets -- -D warnings -D clippy::unused-async, eslint, svelte-check all clean).
  • Pre-push gate (test-gate.ts: test:all + cargo build --bin nodespaced + test:e2e) passed in full before both pushes to this branch.

Addresses #2565

🤖 Generated with Claude Code

…path)

execute_create_node assigned reactive-rule action outputs a random id via
Node::new(...), so N devices independently executing the same rule against
the same trigger each created a distinct node and sync merged them into N
duplicates instead of one.

Add deterministic_action_output_id(rule_id, action_index, iteration_path),
following the existing deterministic_collection_id/deterministic_conflict_id
UUIDv5-over-a-stable-namespace pattern, and wire it into execute_create_node
in place of the random id. iteration_path (playbook::types::IterationPath)
starts as [trigger_node.id] -- serving both the graph_event trigger-node case
and the scheduled trigger's scanned-node case, since the engine already hands
the scanned node in as trigger_node for a scheduled work item -- and gains one
more real node id per nested for_each level entered via execute_actions'
existing for_each loop, never a positional/loop index.

rule_id is derived by rule_id_for(play_id, actions) from the play id (already
available via PlaybookExecutionContext::source_playbook_id, no new threading
needed) and the rule's own parsed action list, rather than a positional
rule_index: ParsedRule carries no id of its own, and execute_actions' only
call site is in engine.rs, which was off-limits to concurrent daemon-wiring
work in progress on that file. This keeps the whole change scoped to
actions.rs/types.rs. Trade-offs are documented in the module doc: editing a
rule's actions changes rule_id (same trade-off ADR-060 already names for a
positional index), and two different rules in one play with byte-identical
action lists would collide onto the same rule_id (an accepted, narrow gap).

execute_create_node now checks for an existing node at the derived id before
inserting and returns it instead of erroring: a second firing computing the
same id is the intended convergence outcome (ADR-060 §3), not a failure, and
letting the insert fail would flow into ActionResult::Failed, which disables
the whole play.

A for_each item with no resolvable real node id (bare string, or an object's
"id" field) can't produce a valid iteration_path element and now fails with a
new ActionError::IterationPathResolutionFailed rather than silently falling
back to a positional index, which would make the derived id depend on scan
order.

Implements ADR-060 §3 and ADR-074 (Derived Identity for Scan-Based and
Nested-Iteration Playbook Actions).

Co-Authored-By: Claude Sonnet 5 <[email protected]>
…ollision test

ADR-060 §3 only says reordering actions shifts action_index; it says nothing
about a positional rule_index. rule_id_for's actual trade-off is broader and
is this implementation's own choice, not something the ADR already accepted:
hashing the whole action list into one rule_id means editing any single
action changes the derived id of every action in the rule, including
untouched siblings. Reword the module doc and the rule_id_for_changes test
docstring to say that plainly instead of misattributing it to the ADR.

Add two_rules_with_identical_actions_silently_share_one_output_node,
demonstrating that two different rules in the same play with byte-identical
action lists don't just share a rule_id in the abstract -- when they later
act on the same real node (e.g. rule A on node_created, rule B on
property_changed for that same node), execute_create_node's existing-node
convergence check can't tell them apart from a legitimate re-fire of the same
rule, so rule B's action silently returns rule A's node instead of erroring
or duplicating. Point the module doc at this test and note a save-time
validation.rs guard is tracked as a separate follow-up, without naming the
issue number in code (house style keeps issue numbers out of comments).

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@mstomar125

Copy link
Copy Markdown
Contributor Author

Addressed adversarial review feedback (2 blocking findings, both doc/test-only — the core mechanism was confirmed sound and unchanged):

  1. Misattribution to ADR-060 §3: the module doc, the rule_id_for_changes_when_the_rule_is_edited test docstring, and this PR's design note claimed the "editing a rule changes its derived id" trade-off was "the same trade-off ADR-060 §3 already accepts for a positional rule_index." ADR-060 §3 only says reordering shifts action_index; it never mentions a positional rule_index. Worse, the actual trade-off here is broader: rule_id_for hashes the entire action list, so editing any one action changes the derived id of every action in that rule, including untouched siblings — not something the ADR describes or blessed. Reworded to state this as this implementation's own consequence.

  2. Byte-identical-action-list collision undersold, zero coverage: added two_rules_with_identical_actions_silently_share_one_output_node, which proves the real failure mode — two different rules in one play with the same action list, later acting on the same node, don't error or duplicate; execute_create_node's convergence check silently hands rule B's execution rule A's node. Filed Reject or warn at save time on two same-play rules with byte-identical action lists #2574 to track a save-time validation.rs guard for this (not fixed in this PR).

Full gate re-run clean after both fixes: bun run test:all (0 failures, 70 test result: ok. blocks) and bun run quality:fix (0 warnings), plus the pre-push gate (test:all + cargo build --bin nodespaced + test:e2e) on push.

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.

1 participant