#4112·LightRAG

[Bug]: F/R source-span provenance is lost for consecutive token windows on emoji-heavy documents (up to 55% of the text at default chunk settings)

Author: passionworkeerCreated Sep 27, 2026Updated Sep 28, 2026

I have searched the existing issues and this bug is not already filed. I believe this is a legitimate bug, not just a question or feature request.

Describe the bug

The F (fixed-token) chunker attaches a private _source_span to each chunk, which lightrag.sidecar.backfill.backfill_chunk_sidecars() turns into per-chunk sidecar provenance. When a token-window boundary splits a multi-byte character, the window's own decoded content carries a U+FFFD replacement character and _token_window_source_span() cannot locate it (content.find(window, ...) searches for a string that does not occur in the source), so the window's span is None.

backfill_chunk_sidecars() handles that case deliberately — _is_unlocatable() skips the chunk with a warning instead of failing the document. The module docstring and the _REPLACEMENT_CHAR comment both describe the impact as "degrade to no-sidecar for that single chunk".

On emoji-containing documents the impact is not a single chunk. At the default chunk parameters the boundary condition repeats, so several consecutive windows lose their spans at once and the source spans of the surviving windows do not reach across the gap:

# PARA_EMOJI * 120 (13560 chars / 5881 tokens), chunk_token_size=1200, chunk_overlap_token_size=100
idx= 0 tokens=1200 span=(0, 2780)      fffd=0
idx= 1 tokens=1200 span=(2553, 5299)   fffd=0
idx= 2 tokens=1200 span=None           fffd=1
idx= 3 tokens=1200 span=None           fffd=2
idx= 4 tokens=1200 span=None           fffd=1
idx= 5 tokens= 381 span=(12696, 13559) fffd=0
uncovered runs: [(5299, 12696, 7397), (13559, 13560, 1)]   # the shorter run is the trailing space
7398 / 13560 = 54.6% of the document is covered by no chunk's _source_span

Half of the chunks are emitted with no provenance and 54.6% of the document text is covered by no chunk's _source_span (7397 characters in one contiguous run, plus the trailing whitespace character). On the same input with 1200/100 the test_token_window_source_span.py cases pass, because every case there is ASCII or uses a synthetic tokenizer where decode(encode(x)) == x.

Two smaller discrepancies in the same code path:

  1. The comment on _REPLACEMENT_CHAR attributes the case to "4-byte supplementary-plane chars: emoji, rare CJK extensions", but 3-byte BMP characters trigger it as well — a plain-CJK document loses 4 of 31 chunks and 175 of 1650 characters at chunk_token_size=60, chunk_overlap_token_size=10. At the same settings a symbol-heavy ASCII document loses 12 of 22 chunks.
  2. Because _token_window_source_span() returns None for these windows, _is_unlocatable(body) is the only thing keeping the document from failing. The check is on the chunk body's content, so the two code paths agree today — but nothing ties them together.

Steps to reproduce

python
from lightrag.chunker.token_size import chunking_by_token_size
from lightrag.utils import TiktokenTokenizer

tok = TiktokenTokenizer()
para = (
    "Release notes:  new chunker ✅ tested  shipped. "
    "Emoji families   and flags    appear in user text. "
)
text = para * 120

chunks = chunking_by_token_size(
    tok, text, chunk_overlap_token_size=100, chunk_token_size=1200,
    _emit_source_span=True,
)
for c in chunks:
    print(c["chunk_order_index"], c.get("_source_span"), c["content"].count("�"))

covered = bytearray(len(text))
for c in chunks:
    s = c.get("_source_span")
    if s:
        for j in range(s["start"], s["end"]):
            covered[j] = 1
print("uncovered chars:", len(covered) - sum(covered))
print("uncovered ratio:", (len(covered) - sum(covered)) / len(text))

Expected Behavior

Either the whole document keeps provenance, or the loss stays proportionate to what the code documents. The window's source position is recoverable: the window differs from the source slice only by the leading/trailing U+FFFD that replaced a partial multi-byte character, so trimming those characters before the find probe would relocate the span (the span would then differ from the chunk content by the replacement character, which backfill's whitespace-normalized comparison does not accept today — so accepting it would require a matching decision there).

Alternatively, if skipping is the intended end state, the comments and the _is_unlocatable docstring should state the realistic magnitude (consecutive windows, a majority of the text on emoji-heavy input) instead of "that single chunk".

Logs and screenshots

[sidecar-backfill] chunk #2 contains replacement characters from a multi-byte token-boundary split; skipping provenance for it
[sidecar-backfill] chunk #3 contains replacement characters from a multi-byte token-boundary split; skipping provenance for it
[sidecar-backfill] chunk #4 contains replacement characters from a multi-byte token-boundary split; skipping provenance for it

Additional Information

  • LightRAG Version: main @ 6702eea912b12df5ba831f58609470ca9da34cfc
  • Operating System: macOS 15 (Darwin 25.6.0), arm64
  • Python Version: 3.12.14
  • tiktoken: 0.14.0
  • Related Issues: none found (see collision check above)
  • Files involved: lightrag/chunker/token_size.py (_token_window_source_span), lightrag/sidecar/backfill.py (_is_unlocatable, backfill_chunk_sidecars), tests/chunker/test_token_window_source_span.py
  • No credentials or external services are needed to reproduce; the snippet above is self-contained.