Skip to content

Run structural effects before dependent effects - #4878

Merged
calebporzio merged 7 commits into
mainfrom
agent/structural-effect-scheduling
Aug 13, 2026
Merged

calebporzio merged 7 commits into
mainfrom
agent/structural-effect-scheduling

Conversation

@calebporzio

@calebporzio calebporzio commented Aug 13, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #4843.

Problem

A keyed x-for can reuse a DOM node while its injected iteration scope still represents the node's previous loop position. If a child effect is notified before x-for, it combines new outer state with that old scope. Alpine's whole-flush dedupe then suppresses the corrective run requested when x-for refreshes the scope.

In the reported virtual-list case, the sequence 1–25 → 1–15 → 13–30 can therefore render 25, 26, 27 where 13, 14, 15 belong.

Approach

Before Alpine runs an ordinary effect, this change runs every pending structural effect parent-before-child. It keeps the existing invariant that a job runs at most once per flush.

  • x-for, x-if, and x-html opt their subtree-owning effects into structural scheduling.
  • Structural effects retain their owner element and a creation-order tie-breaker.
  • Only the unprocessed queue tail is reordered; completed jobs never requeue and ordinary jobs remain FIFO.
  • Parent ordering uses current logical-tree depth, including teleport-back links and shadow-root hosts.
  • Structural work added during a flush is promoted before the next ordinary job.
  • x-html destroys the Alpine child tree it replaces so obsolete descendant effects are cleaned up and dequeued.

This is an alternative to the completed-job requeueing proposed in #4851. Requeueing fixes the reported symptom, but changes Alpine globally into a convergence scheduler and reopens recursive-effect behavior. Structural ordering prevents the invalid intermediate observation without changing whole-flush dedupe.

Why the complete rule is needed

Each smaller version has a demonstrated counterexample:

  • Prioritizing only x-for lets a nested loop run before an invalidated x-if or x-html ancestor removes it.
  • A structural phase without parent ordering lets a nested loop read stale outer loop scope.
  • Static creation order fails when an older initialized node is moved beneath a newer removing ancestor.
  • Physical DOM depth fails for teleported descendants.
  • Allowing completed jobs to requeue changes dedupe and recursion semantics for every Alpine effect.

Validation

  • Exact keyed-range reproduction and nested keyed-scope variants.
  • Removing x-if and x-html ancestors, including a moved initialized subtree.
  • Teleport logical ancestry and shadow-root scheduler ordering.
  • Morph cloning and transaction batching.
  • Real Livewire request → transaction → morph → Alpine flush against this branch.
  • x-html lifecycle destruction exactly once.
  • Deterministic browser stress: 800 seeded mutation batches with a complete DOM oracle after every flush, covering reused keys, nested scopes, x-if, x-html, teleports, randomized assignment order, and transaction/non-transaction batching.
  • Random scheduler property test: 3,200 jobs covering logical depth, teleport backlinks, ordinary FIFO order, dedupe, and pending-job removal.
  • Mutation checks prove the stress tests are sensitive: removing priority fails browser seed 0x4851 immediately; replacing logical depth with creation order fails scheduler seed 1.
  • 185 Vitest tests passed, with 1 existing skip.
  • Complete Cypress suite and production build passed.

Performance

Average medians from three alternating main/PR browser runs:

Workload main PR
1,000 leaf effects 3.50 ms 3.40 ms
100 sibling loops / 1,000 leaves 8.93 ms 8.70 ms
100 nested outer loops / 500 inner leaves 3.63 ms 3.67 ms
100 x-if directives 1.07 ms 1.10 ms
100 replacing x-html directives 17.30 ms 1.80 ms

Ordinary and structural workloads are within run-to-run noise. The repeatable x-html improvement comes from destroying replaced descendant effects instead of leaving detached effects subscribed.

The minified CDN bundle grows by 860 bytes raw / 262 bytes gzip (47,465 → 48,325 raw; 17,034 → 17,296 gzip).

Intentional boundary

This guarantees ordering for work invalidated before a flush and structural work added while draining it. It does not make arbitrary user-effect graphs converge, nor does it change scheduler exception recovery. Those are separate scheduler policies and intentionally remain out of scope.

@calebporzio calebporzio changed the title Prioritize structural effects before dependent effects Run structural effects before dependent effects Aug 13, 2026
@calebporzio
calebporzio marked this pull request as ready for review August 13, 2026 16:04
@calebporzio
calebporzio merged commit aa4c297 into main Aug 13, 2026
2 checks passed
@calebporzio
calebporzio deleted the agent/structural-effect-scheduling branch August 13, 2026 17:11
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.

Keyed x-for with seems to not be robust against adding/removing elements

1 participant