fix: number redaction tokens in document order#165
Merged
Conversation
redact() assigned per-type counters during the right-to-left span replacement loop, so with multiple entities of the same type the last occurrence received _1 (e.g. [EMAIL_2] ... [EMAIL_1]). Replacements are now precomputed in ascending document order and applied right-to-left, so the first occurrence of each type always gets _1 for the token and pseudonymize strategies. Also: RedactResult.entities is now ordered by document position (ascending), and mapping originals are read from the immutable input text instead of the partially-replaced string, preventing token leakage into mapping values on overlapping spans.
- redact() now suppresses overlapping/duplicate entity spans before applying replacements: longest span wins, ties broken by entity type priority then confidence (same rule as the regex scan pipeline) - mask strategy mapping is keyed by per-type indexed keys ([EMAIL_MASK_1]) instead of the mask string, which collided for distinct same-length values and silently dropped originals
Merged
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
redact()numbered per-type tokens in reverse document order: counters incremented during the right-to-left span-replacement loop, so the last occurrence of each entity type got_1(e.g. two emails redacted as[EMAIL_2] ... [EMAIL_1]). Replacements (and themapping) are now precomputed in ascending document order, then spans are applied right-to-left so offsets stay valid — the first occurrence always gets_1for thetokenandpseudonymizestrategies.RedactResult.entitiesis now ordered by document position (ascending, was descending). All in-repo consumers (agent.py, LiteLLM guardrail, Claude Code hook) are order-independent.[Unreleased]in CHANGELOG.MD, including the pre-existingmaskmapping-collision limitation (being addressed separately).Note: the datafog-lp in-browser demo intentionally still mirrors the old numbering; it gets flipped (plus parity fixtures regenerated) once this ships in a release.
Test plan
test_redact_token_numbering_follows_document_orderandtest_redact_pseudonymize_numbering_follows_document_order(written first, failed with the reversed output before the fix)tests/test_engine_api.py— 16 passeddev(CLI smoke test, missing spaCy model)