Skip to content

fix(engine): allocate tokens in document order - #31

Merged
woladi merged 1 commit into
masterfrom
fix/token-numbering-document-order
Sep 4, 2026
Merged

woladi merged 1 commit into
masterfrom
fix/token-numbering-document-order

Conversation

@woladi

@woladi woladi commented Sep 4, 2026

Copy link
Copy Markdown
Owner

Regex substitutions are applied right-to-left so that earlier offsets stay valid — but store.add() was called inside that same reversed loop, so the [TAG:N] counters ran backwards too. In a text with two IBANs, the second one became [IBAN:1].

Unmasking was always correct; this only affects what a human (or the downstream LLM) reads in the masked text — and reversed numbering is quietly confusing there.

Fix: allocate tokens in document order first, then substitute right-to-left.

Tests: +3 in tests/regex-regression.test.ts — appearance order, independent per-type counters, and a full round-trip back to the original.

tsc --noEmit clean, eslint clean, full suite 380/380 green. Changeset (patch) included.

Regex substitutions are applied right-to-left so that earlier offsets stay
valid, but store.add() was called in that same reverse order, so [TAG:N]
counters ran backwards: the second IBAN in a text was tokenised [IBAN:1].

Tokens are now allocated in document order first, then substituted
right-to-left. Unmasking was always correct; this only fixes what a human
or the downstream LLM reads in the masked text.
@woladi
woladi merged commit 97a8893 into master Sep 4, 2026
1 check passed
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