Skip to content

Order cross-playbook rules by playbook id instead of created_at (ADR-060 §5) #2577

Closed

Description

@mstomar125

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) carries play_created_at: DateTime<Utc> and implements Ord (lines 97-103) as self.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 each OrderedRuleRef with play_created_at: node.created_at — the play node's wall-clock creation timestamp — and inserts it into trigger_index / cron_registry entries 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> on OrderedRuleRef with the play id as the primary sort key (the struct already carries play_id: String; Ord should key on it directly instead of a separate timestamp field).
  • Keep rule_index as the tie-breaker for rules within the same play, unchanged.
  • Update PlaybookLifecycleManager::activate_play to stop threading node.created_at into the ordering key.
  • docs/architecture/playbook-engine.md in nodespace-docs already 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 PlaybookLifecycleManager instances 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 — OrderedRuleRef struct and its Ord/PartialOrd impls (lines ~73-103)
  • Construction site: packages/core/src/playbook/lifecycle.rs — activate_play (~line 64), lookup_rules (line 190)

Implementation Details

  • PartialEq/Eq for OrderedRuleRef already compares (play_id, rule_index) for identity (used by .sort().dedup()); only the Ord key needs to change from play_created_at to play_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

Activity

  1. self-assigned this
    on Sep 12, 2026
  2. malibio commented on Sep 13, 2026

    @malibio
    Collaborator

    @mstomar125 heads up: #2563's PR (#2600, adding get-workflow-state) reads matched rules via PlaybookLifecycleManager::lookup_rules, and its response's rules[] array is presented in whatever order that returns — i.e. OrderedRuleRef's Ord order. No code conflict (that PR never touches types.rs or constructs OrderedRuleRef itself), but once this issue lands, get-workflow-state's output order will shift from play_created_at-based to play_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.

  3. mstomar125 commented on Sep 14, 2026

    @mstomar125
    ContributorAuthor

    Frontend baseline: 4951 passed (240 test files), Duration 86.76s. Starting implementation.

  4. mstomar125 commented on Sep 14, 2026

    @mstomar125
    ContributorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions