Skip to content

fix(workflows): preserve intra-overlay order for multiple insert_after edits#3662

Open
jawwad-ali wants to merge 1 commit into
github:mainfrom
jawwad-ali:fix/overlay-merge-insert-after-order
Open

fix(workflows): preserve intra-overlay order for multiple insert_after edits#3662
jawwad-ali wants to merge 1 commit into
github:mainfrom
jawwad-ali:fix/overlay-merge-insert-after-order

Conversation

@jawwad-ali

Copy link
Copy Markdown
Contributor

What

In _traverse_and_apply (workflows/overlays/merge.py), the insert_after loop iterated reversed(edits) over the flat per-anchor edit list. That reversal exists to place a higher-priority overlay closest to the anchor (mirroring insert_before's winner-closest behaviour) — but reversing the flat list also flips the declared order of multiple insert_after edits authored within a single overlay.

Concretely, one overlay with [insert_after a→x, insert_after a→y] produced [a, y, x, b] instead of [a, x, y, b]. The sibling insert_before loop directly above iterates forward and preserves order, so the two operations were asymmetric.

Fix

Group contiguous same-layer edits and reverse the group order only, keeping each overlay's own inserts in declared order. Cross-overlay priority is unchanged — a higher-priority overlay's group still lands closest to the anchor.

Tests

tests/workflows/test_overlay_merge.py::TestMergeSteps::test_merge_steps_multiple_insert_after_same_overlay_preserves_order — one overlay, two insert_after on the same anchor → asserts [a, x, y, b]. Fails before the fix ([a, y, x, b]); passes after. The existing test_merge_steps_higher_priority_wins (cross-overlay ordering) still passes. ruff clean; all 43 overlay merge + composer tests pass.


AI-assisted: authored with Claude Code. Verified the asymmetry against the forward insert_before loop and confirmed cross-overlay priority is preserved.

…r edits

_traverse_and_apply's insert_after loop iterated reversed(edits) over the
flat per-anchor edit list. The reversal is only meant to place a
higher-priority OVERLAY closer to the anchor (mirroring insert_before's
winner-closest behaviour), but reversing the flat list also flipped the
declared order of multiple insert_after edits authored within a SINGLE
overlay: [insert_after a->x, insert_after a->y] produced [a, y, x, b]
instead of [a, x, y, b]. insert_before (a forward loop) already preserves
order, so the two operations were asymmetric.

Group contiguous same-layer edits and reverse the GROUP order only, keeping
each overlay's own inserts in declared order. Cross-overlay priority is
unchanged (higher-priority overlay still lands closest to the anchor).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@jawwad-ali
jawwad-ali requested a review from mnriem as a code owner July 22, 2026 17:26
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