Skip to content

fix: granite33 citation spans wrong for duplicate sentences (#851) - #872

Merged
planetf1 merged 1 commit into
generative-computing:mainfrom
planetf1:cs/issue-851
Apr 20, 2026
Merged

planetf1 merged 1 commit into
generative-computing:mainfrom
planetf1:cs/issue-851

Conversation

@planetf1

@planetf1 planetf1 commented Apr 16, 2026 •

Copy link
Copy Markdown
Contributor

fix: granite33 citation spans wrong for duplicate sentences

Type of PR

  • Bug Fix
  • New Feature
  • Documentation
  • Other

Description

_add_citation_response_spans used str.find(sentence) without an
offset, so it always matched the first occurrence of a sentence in
the response. When the same sentence appeared more than once — each
cited separately — every citation after the first ended up pointing at
the wrong span.

Example: a response containing "The sky is blue." twice, once
cited by document 1 and once by document 2:

Before fix — both citations point at the first occurrence:
  citation 0: response[0:16]  → "The sky is blue."  ✓
  citation 1: response[0:16]  → "The sky is blue."  ✗  (should be [17:33])

After fix:
  citation 0: response[0:16]  → "The sky is blue."  ✓
  citation 1: response[17:33] → "The sky is blue."  ✓

The fix introduces search_offset that advances past each consumed
sentence. If find() returns nothing after the offset the sentence
appears only once in the response (multiple citations on the same
sentence); the code falls back to searching from the start so those
citations correctly share the same span.

Note: the related response_end off-by-one (using the full response
length instead of the sentence length) was already fixed in #845.

Performance

No meaningful impact:

  • Common case is marginally faster — find(text, offset) scans from
    offset rather than from the start, so it examines less of the string
    than the original find(text).
  • Fallback case does two find() calls — when the offset overshoots
    (e.g. multiple citations on the same sentence) we do one extra call.
    This path is unusual and both calls operate on C-level Boyer-Moore-Horspool
    search over a few hundred characters at most.
  • Inputs are tiny — model responses are typically a few hundred
    characters; the citation count is small (single digits in practice).
  • The bottleneck is elsewhere — nltk.sent_tokenize dominates this
    function by orders of magnitude; the str.find() calls are negligible
    by comparison.

Testing

  • Tests added to the respective file if code was changed
  • New code has 100% coverage if code as added
  • Ensure existing tests and github automation passes (a maintainer will kick off the github automation when the rest of the PR is populated)

Attribution

  • AI coding assistants used

…ve-computing#851)

str.find() without an offset always returned the first occurrence, so
when the same sentence appeared more than once in the response, every
citation after the first got response_begin/response_end pointing at
the first occurrence.

Introduce search_offset that advances past each consumed sentence.
When find() misses after search_offset the sentence appears only once
(multiple citations on one sentence); fall back to searching from the
start so those citations share the correct span.

Adds two regression tests to TestAddCitationResponseSpans:
- test_duplicate_sentences_each_get_own_span
- test_multiple_citations_on_same_sentence_share_span

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>
@github-actions github-actions Bot added the bug Something isn't working label Apr 16, 2026
@github-actions

Copy link
Copy Markdown
Contributor

The PR description has been updated. Please fill out the template for your PR to be reviewed.

@planetf1
planetf1 marked this pull request as ready for review April 16, 2026 10:06
@planetf1
planetf1 requested a review from a team as a code owner April 16, 2026 10:06
@planetf1
planetf1 requested a review from frreiss April 16, 2026 10:07
@planetf1

Copy link
Copy Markdown
Contributor Author

fyi @frreiss @yannisk2

@planetf1
planetf1 enabled auto-merge April 16, 2026 11:29

@frreiss frreiss left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@planetf1
planetf1 added this pull request to the merge queue Apr 20, 2026
Merged via the queue into generative-computing:main with commit 0fd342e Apr 20, 2026
16 of 17 checks passed
@planetf1
planetf1 deleted the cs/issue-851 branch April 20, 2026 04:05
akihikokuroda pushed a commit to akihikokuroda/mellea that referenced this pull request May 27, 2026
…ve-computing#851) (generative-computing#872)

str.find() without an offset always returned the first occurrence, so
when the same sentence appeared more than once in the response, every
citation after the first got response_begin/response_end pointing at
the first occurrence.

Introduce search_offset that advances past each consumed sentence.
When find() misses after search_offset the sentence appears only once
(multiple citations on one sentence); fall back to searching from the
start so those citations share the correct span.

Adds two regression tests to TestAddCitationResponseSpans:
- test_duplicate_sentences_each_get_own_span
- test_multiple_citations_on_same_sentence_share_span

Assisted-by: Claude Code

Signed-off-by: Nigel Jones <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: granite33 citation spans wrong for duplicate sentences (find() first-occurrence)

2 participants