Repository navigation
Derive playbook create_node output ids from (rule, action, iteration path) - #2573
Conversation
…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]>
|
Addressed adversarial review feedback (2 blocking findings, both doc/test-only — the core mechanism was confirmed sound and unchanged):
Full gate re-run clean after both fixes: |
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 playbookcreate_nodeaction outputs.execute_create_nodepreviously assigned a random id viaNode::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 existingdeterministic_collection_id/deterministic_conflict_idpattern.iteration_path(playbook::types::IterationPath) starts as[trigger_node.id]— this already covers both thegraph_eventtrigger-node case and thescheduledtrigger's scanned-node case, since the engine hands the scanned node to the action executor astrigger_nodeeither way — and gains one real node id per nestedfor_eachlevel entered (never a positional/loop index).execute_create_nodechecks for an existing node at the derived id before inserting; if found, it's returned as-is (the intended convergence outcome), not an error.for_eachitem with no resolvable real node id (neither a bare string nor an object with a non-empty"id"field) fails the rule via a newActionError::IterationPathResolutionFailed, rather than silently falling back to a scan-order-dependent positional index.Design note:
rule_idwithout touchingengine.rsThis 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 inengine.rs, andParsedRulecarries no id/index of its own — so instead of threading a positionalrule_indexin as a new parameter (which needs anengine.rscall-site change),rule_id_for(play_id, actions)derives the rule's identity from data already available insideexecute_actions: the play id (already passed viaexecution_context.source_playbook_id) and the rule's own action list (theactions: &[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):
rule_id_forhashes the entire action list into onerule_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 shiftingaction_index), and it's this implementation's own consequence, not something the ADR already accepted. Documented in the module doc and therule_id_for_changes_when_the_rule_is_editedtest.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 onnode_created, rule B later onproperty_changedfor 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 newtwo_rules_with_identical_actions_silently_share_one_output_nodetest. Not fixed in this PR — tracked as a follow-up: a save-timevalidation.rscheck 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
Tests (all passing)
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— afor_eachover 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, 70test result: ok.blocks.bun run quality:fix— 0 warnings (cargo clippy --all-targets -- -D warnings -D clippy::unused-async, eslint, svelte-check all clean).cargo build --bin nodespaced+ test:e2e) passed in full before both pushes to this branch.Addresses #2565
🤖 Generated with Claude Code