Skip to content

fix(runtime): locate history excerpts by original byte offsets under Unicode lowercasing (F24) - #82

Merged
lg2m merged 1 commit into
mainfrom
fix/eng-805-f24-history-offsets
Sep 20, 2026
Merged

lg2m merged 1 commit into
mainfrom
fix/eng-805-f24-history-offsets

Conversation

@lg2m

@lg2m lg2m commented Sep 20, 2026

Copy link
Copy Markdown
Collaborator

fix(runtime): locate history excerpts by original byte offsets under Unicode lowercasing (F24)

Linear: ENG-805 · audit F24 (P2 · S). No protocol/schema change.

Bug

search_history found the query in text.to_lowercase() and used that byte offset to slice text. İ lowers from 2 bytes to 3, ẞ from 3 to 2, so with enough of them before a match the offset drifts — the excerpt drifts off its match, and past the excerpt half-width loses it entirely.

Fix

find_case_insensitive(haystack, needle) -> Option<(start, end)> lowers the haystack char by char, tracking original char boundaries, so the span always indexes the original and covers the full match. A needle that would end mid-lowering ("i" against "İ" → "i̇") does not match half a char. ASCII haystacks — the common case, and what the F06 bench measures — keep the byte-for-byte path.

excerpt_around drops its lowered parameter; the one caller in transcript.rs no longer allocates a lowercased copy per text.

Verification

  • New: matches_after_length_changing_lowercase_are_located_in_the_original (İ/ẞ prefixes; a 600-char drift beyond the half-width), case_insensitive_search_spans_original_bytes_and_respects_char_lowering.
  • cargo test -p qq-core 674 pass; fmt; clippy -D warnings.
  • context_assembly bench: search 27 / 35 ms at 1k / 10k archived runs vs 26 / 30 ms before — within run-to-run noise (fixture is ASCII → fast path).

…Unicode lowercasing (F24)

search_history found the needle in a lowercased copy of each text and used
that byte offset in the original. Lowercasing changes byte length for some
characters (İ 2→3 bytes, ẞ 3→2), so after enough of them the offset drifted:
excerpts could miss their match or, with enough drift, snap to the wrong
region entirely.

find_case_insensitive lowercases the haystack one char at a time and keeps
the original char boundaries, so the returned span always indexes the
original text and covers the whole match. A needle that would end inside a
multi-char lowering does not match half a character. ASCII haystacks keep
the byte-for-byte fast path (to_ascii_lowercase + memchr-backed find).
excerpt_around takes the haystack and needle only.

Tests: İ/ẞ prefixes of several shapes, and a 600-char İ prefix whose drift
exceeds the excerpt half-width; span assertions for growing and shrinking
lowerings. context_assembly bench search timings unchanged within noise.

Closes ENG-805.
@lg2m
lg2m merged commit 51ccf13 into main Sep 20, 2026
4 checks passed
@lg2m
lg2m deleted the fix/eng-805-f24-history-offsets branch September 20, 2026 06:27
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