fix: auto-invalidate location cache after document mutations - #420
Draft
toddr-bot wants to merge 2 commits into
Draft
fix: auto-invalidate location cache after document mutations#420toddr-bot wants to merge 2 commits into
toddr-bot wants to merge 2 commits into
Conversation
…ions Locations cached by index_locations() become stale after mutations (insert_before, insert_after, remove, replace, add_element, prune) but are not automatically invalidated. Users must manually call flush_locations(), which is error-prone and undocumented on mutation methods. These tests verify that location() returns correct values after each type of mutation. All mutation tests are marked $TODO and fail, demonstrating the bug described in GitHub issue #91. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
When a PPI document is modified via insert_before, insert_after, remove, replace, add_element, or prune, cached token locations become stale. Previously users had to manually call flush_locations() after every mutation, which was error-prone and undocumented on the mutation methods. The fix adds a _locations_dirty flag to PPI::Document that is set by a __notify_locations_dirty helper called from all Node mutation methods (add_element, remove_child, __insert_before_child, __insert_after_child, __replace_child). When location() is subsequently called via _ensure_location_present, it detects the dirty flag and triggers flush_locations + index_locations to recalculate all positions. The internal __add_element (used only during initial parsing) is not modified, avoiding any performance impact on document construction. Fixes #91 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This was referenced Apr 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a PPI document is modified via
insert_before,insert_after,remove,replace,add_element, orprune, cached token locations become stale but are not automatically invalidated. Users had to manually callflush_locations()after every mutation — error-prone and undocumented on the mutation methods themselves.This PR adds a
_locations_dirtyflag that is set on the owningPPI::Documentwhenever a mutation occurs. Whenlocation()is next called, it detects the dirty flag and transparently flushes + re-indexes all positions. The internal__add_element(used only during initial parsing) is unchanged, avoiding any performance impact on document construction.Fixes #91
Changes
PPI::Node: Added__notify_locations_dirtyhelper; called fromadd_element,remove_child,__insert_before_child,__insert_after_child, and__replace_childPPI::Document:index_locationsflushes stale caches when_locations_dirtyflag is setPPI::Element:_ensure_location_presentchecks the dirty flag before returning cached locationst/ppi_location_dirty.t: New test file covering all 6 mutation pathst/ppi_element_flush.t: Updated existing assertions that previously expected stale locationsTest plan
t/ppi_location_dirty.tverifies auto-invalidation for:insert_before,insert_after,remove,replace,add_element,prunet/ppi_element_flush.tto expect fresh (auto-refreshed) locations instead of stale onesGenerated by Kōan /fix
Quality Report
Changes: 5 files changed, 153 insertions(+), 28 deletions(-)
Code scan: clean
Tests: skipped
Branch hygiene: clean
Generated by Kōan post-mission quality pipeline