Repository navigation
Description
Overview
OrderedRuleRef::cmp (packages/core/src/playbook/types.rs) orders matched rules across different plays by (play_created_at, rule_index). Play nodes are synced nodes created at wall-clock times on different devices, so clock skew or a different install order gives two devices different evaluation orders for the same event — and a chain of rules that depends on order can reach different end states on each device.
Problem Statement
Verified against current code:
OrderedRuleRef(packages/core/src/playbook/types.rs, struct at line 73) carriesplay_created_at: DateTime<Utc>and implementsOrd(lines 97-103) asself.play_created_at.cmp(&other.play_created_at).then_with(|| self.rule_index.cmp(&other.rule_index)).PlaybookLifecycleManager::activate_play(packages/core/src/playbook/lifecycle.rs, ~line 64-67) constructs eachOrderedRuleRefwithplay_created_at: node.created_at— the play node's wall-clock creation timestamp — and inserts it intotrigger_index/cron_registryentries that are kept sorted via.sort().lookup_rules(packages/core/src/playbook/lifecycle.rs, line 190) merges and re-sorts matched rules the same way before returning them to the engine for sequential execution.
Two devices that installed the same set of plays in different order, or whose clocks disagree, will therefore evaluate cross-play rules in a different order for the identical triggering event. ADR-060 §5 requires ordering to be a function of the data, not of clocks or install history.
Proposed Solution
Per ADR-060 §5 ("Stable ordering"): key cross-playbook ordering on the play node's id (a stable, content-derived identifier already present on every play node) instead of created_at. Two engines given the same play set in a different creation order must evaluate rules in the same order.
- Replace
play_created_at: DateTime<Utc>onOrderedRuleRefwith the play id as the primary sort key (the struct already carriesplay_id: String;Ordshould key on it directly instead of a separate timestamp field). - Keep
rule_indexas the tie-breaker for rules within the same play, unchanged. - Update
PlaybookLifecycleManager::activate_playto stop threadingnode.created_atinto the ordering key. docs/architecture/playbook-engine.mdinnodespace-docsalready documents the target(playbook_id, rule_index)ordering (per Playbook execution under multi-device sync: derived identity, stable ordering, causal cycle depth, and catch-up suppression #1738's own Architecture Impact section) — no further docs work should be needed, but confirm the doc matches the shipped key exactly once this lands.
Acceptance Criteria
- Cross-playbook rule ordering keys on the play id, not
created_at - Test: two
PlaybookLifecycleManagerinstances given the same play set activated in different order (simulating two devices with different install/sync order) produce rules in the same evaluation order for a shared trigger - Existing within-play rule ordering (by
rule_index) 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
- Ordering type:
packages/core/src/playbook/types.rs—OrderedRuleRefstruct and itsOrd/PartialOrdimpls (lines ~73-103) - Construction site:
packages/core/src/playbook/lifecycle.rs—activate_play(~line 64),lookup_rules(line 190)
Implementation Details
PartialEq/EqforOrderedRuleRefalready compares(play_id, rule_index)for identity (used by.sort().dedup()); only theOrdkey needs to change fromplay_created_attoplay_id.- String comparison of play ids is a stable total order regardless of what id format is later chosen for plays, so no new id scheme is required here.
Dependencies
- None — self-contained within
packages/core/src/playbook/
Related Issues
- Playbook execution under multi-device sync: derived identity, stable ordering, causal cycle depth, and catch-up suppression #1738 (parent — this closes the "stable ordering" acceptance criterion)
Activity
malibio commentedon Sep 13, 2026
@mstomar125 heads up: #2563's PR (#2600, adding
get-workflow-state) reads matched rules viaPlaybookLifecycleManager::lookup_rules, and its response'srules[]array is presented in whatever order that returns — i.e.OrderedRuleRef'sOrdorder. No code conflict (that PR never touchestypes.rsor constructsOrderedRuleRefitself), but once this issue lands,get-workflow-state's output order will shift fromplay_created_at-based toplay_id-based as a side effect. That's expected and harmless (both orders are equally valid, this issue makes it the correct/stable one) — just flagging so it isn't a surprise if you or a reviewer notices the ordering change while working on this.mstomar125 commentedon Sep 14, 2026
Frontend baseline: 4951 passed (240 test files), Duration 86.76s. Starting implementation.
mstomar125 commentedon Sep 14, 2026
Fixed in PR #2605: #2605
OrderedRuleRef::Ord now keys on play_id (already carried on the struct for identity) instead of the wall-clock play_created_at field, which has been removed. rule_index remains the within-play tiebreaker. PartialEq/Eq (used by lookup_rules' sort().dedup()) already keyed on (play_id, rule_index) and needed no change.
Tests added confirm: (1) cross-play rule order follows play_id even when it contradicts creation-time order, (2) two PlaybookLifecycleManager instances activating the same plays in opposite order (simulating devices with differing install order/clock skew) produce identical rule order, (3) within-play rule_index ordering is unchanged, and (4) sort().dedup() identity collapsing still works. An existing lifecycle test was also strengthened to prove play_id — not creation time — drives ordering rather than relying on the two coinciding.
bun run test:all and bun run quality:fix both clean; pre-push gate (including test:e2e and the Tauri-seam suite) passed in full.