Repository navigation
feat(citation): cite a source span, not the whole retrieved chunk - #162
Merged
Merged
Conversation
…n seams Introduce the contracts v1.5 builds on, with no user-visible behavior change. - Retriever.search() takes a RetrievalRequest and returns a RetrievalResult carrying a RetrievalTrace (strategy, scope, topK, threshold, durationMs). KnowledgeService gains retrieve() and keeps search()'s legacy shape as a delegate over it. - A requested source filter is refused until #94 implements pre-KNN filtering, instead of being silently ignored. Remove the dead, never-read VectorStore.QueryOptions.filter, which declared a capability that did nothing. - Declare CitationCandidate for #155: the cited span exists before the answer, so no post-hoc (and non-deterministic) semantic alignment is needed. - Declare ingestion run/stage and the source/index state split for #95. Verified: npm run typecheck; npm test (345 pass); the RAG eval harness reproduces docs/eval/baseline-v1.4.json byte-for-byte.
…splay id The candidate's identity in the answer is the same [n] the model writes and Citation.index carries; a string id like 'S2' would put a UI format into the domain model. blockId is optional because not every source has block structure.
…no blocks A chunk indexed before block provenance, or from a source that produces no blocks, has no span to point at. The fallback keeps the whole-chunk quote, so the offsets must be optional rather than a fabricated 0..0.
A retrieval chunk exists to make a passage findable; a citation must point at the passage that supports the claim. Build the spans before the answer and let the model choose among them, instead of trying to align a citation to a claim after the fact (that is semantic entailment, which no deterministic rule answers). - buildCitationCandidates() splits each retrieved chunk into sentence-sized candidate spans (short neighbours merge, at most 3 sentences) and numbers them 1..M. That number is the `[n]` the model writes and `Citation.index` carries, so the user-visible marker protocol is unchanged. - buildRAGContext() presents candidates grouped by source/chunk with globally continuous markers, and builds each Citation from its candidate: quote, page, blockId and startOffset/endOffset now describe the supporting passage. - A chunk with no block provenance falls back to one whole-chunk candidate with no span, so a pre-#66 index is never worse than before. - Text/HTML reader highlights the exact span; the PDF reader highlights the paragraph that contains the cited sentence (previously the chunk's first block). Sub-sentence highlight in the PDF viewer needs text-layer range mapping and is a follow-up. Verified: npm run typecheck; npm test (352 pass); npm run build; the RAG eval harness output is byte-identical to docs/eval/baseline-v1.4.json. Not verified: model behaviour against a live endpoint — the prompt contract changed, so the citation-marking rate should be watched on a real model before v1.5 ships.
This was referenced Sep 28, 2026
This was referenced Sep 28, 2026
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.
What does this PR do?
Makes a citation point at the span that supports the claim, not at the whole retrieved chunk.
Why?
Today a citation's evidence is the entire chunk:
citationFromSearchResult()setquote: result.contentandstartOffset/endOffsetto the union of the blocks the chunk covered. Clicking[3]quoted ~500 characters and highlighted every block the chunk touched.Aligning a citation to a claim after the answer is not solvable deterministically — deciding that "Transformer 不需要 RNN 结构" is supported by "Transformers eliminate recurrent connections" is semantic entailment (NLI), not substring/token/embedding matching. So the spans are produced before the answer, and the model selects among them by index.
Related issue
Fixes #155
Related to #154
What changed
buildCitationCandidates()(src/main/services/citationCandidates.ts) turns each retrieved chunk into sentence-sized candidate spans:3.14,U.S.,Fig. 3,et al.do not split);1..Mcontinuously across chunks — this number is the[n]the model writes andCitation.indexcarries, so the user-visible marker protocol is unchanged;buildRAGContext()presents candidates grouped by source/chunk with globally continuous markers, and builds eachCitationfrom its candidate, soquote/page/blockId/startOffset/endOffsetdescribe the supporting passage.chatHandlerstakescitationContextsfrombuildRAGContextinstead of re-deriving the span from the chunk.What is unchanged by design:
[n],citationMarkers,resolveCitations,citationToSourceAnchor,Citation.index— the[n] → citation → readerchain is the same; only what[n]resolves to is finer.How was this tested?
npm run typecheck— passes.npm test— 352 pass, 0 fail (newtest/citationCandidates.test.ts, rewrittentest/citations.test.ts).quoteis a verbatim slice of the source at its offsets (what the reader highlights);A causes B. So the result is C.stays one candidate);npm run build— passes.docs/eval/baseline-v1.4.json(retrieval is untouched by this change).Not verified / scoped out
TextSourceReaderhighlightsstartOffset..endOffsetexactly;PdfSourceReaderhighlights the containing paragraph (previously the chunk's first block). Mapping the parser's offsets onto the pdfjs text layer is a separate change.Checklist
npm run typecheckpasses.npm run buildpasses.Desktop / build changes