Repository navigation
fix: cap citable FAQ text and verify streamed citations against the live endpoint - #388
Merged
neuromechanist merged 1 commit intoSep 17, 2026
Conversation
…tions live Review findings on PR #383. search_faq's citation path sent the stored answer uncapped while its string path truncated to 500 chars. The other two citable tools pass search snippets that src/knowledge/search.py has already truncated, but a FAQ answer comes straight from the database, capped only at ingest (5000 chars). Reproduced: at the default limit=5, one search carried 25,000 chars on the Anthropic path against 3,476 on the OpenRouter path for the same query, a cost difference visible only to whichever provider was paying. Capped at 2000 chars, more generous than the display truncation because a citation points at an exact span and a span cut mid-sentence is worse than no citation. TestCitablePayloadsAreBounded covers per-block and whole-call size in both modes, plus that an ordinary short answer is left alone. Verified both size tests fail when the cap is reverted (5000 == 2000 + 3, and 25000 chars). The streaming citation path had no live verification, unlike the non-streaming one, so it rested on a hand-built chunk shape: the assumption that a citation arrives as its own delta on an on_chat_model_stream chunk rather than only on the final aggregated message. Added an llm-marked test that drives the router's own SSE generator against the real endpoint and asserts the accumulated content ends with the [n] marker at the moment each citation event fires, which is what makes a citation inline rather than a trailing bundle of links. Ran it: passes. It needs an Origin, taken from HED's cors_origins rather than hardcoded, because the platform key is only offered to an authorized origin and the citations flag follows the resolved provider. extract_text lost its last production caller when the API switched to citation-aware assembly. Kept as the text-side counterpart of extract_citations, with a docstring note saying it has no production caller and why an answer must not be assembled through it.
neuromechanist
merged commit Sep 17, 2026
26712ee
into
feature/issue-360-epic-anthropic-platform
3 of 4 checks passed
This was referenced Sep 17, 2026
Merged
5 tasks done
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.
Follow-up to #383, which was already merged when its review came back. Three findings from that review, plus one accepted as a documented limitation.
FAQ citation payload was uncapped
search_faq's citation path sent the stored answer whole; its string path truncates to 500 chars. The other two citable tools pass search snippets thatsrc/knowledge/search.pyhas already truncated, but a FAQ answer comes straight from the database, capped only at ingest (5000 chars,src/knowledge/db.py). Reproduced at the defaultlimit=5: one search carried 25,000 chars on the Anthropic path against 3,476 on the OpenRouter path for the same query, a size and cost difference visible only to whichever provider happened to be paying.Capped at 2000 chars per answer. More generous than the 500-char display truncation on purpose: a citation points at an exact span, and a span cut mid-sentence is worse than no citation.
TestCitablePayloadsAreBoundedcovers per-block size, whole-call size in both modes, and that an ordinary short answer is left alone. Verified load-bearing: reverting the cap fails two of the three with exactly the reproduced numbers (5000 == 2000 + 3, and 25,000 chars against 3,476).Streamed citations had no live verification
The non-streaming path had an
llm-marked test against the real endpoint; the streaming path was tested only against a hand-built chunk shape, which is how Phase 2's cache-token bug shipped. The untested assumption was that a citation arrives as its own delta on anon_chat_model_streamchunk rather than only on the final aggregated message. If that were wrong, the unit tests would stay green while inline markers silently vanished from every streamed answer.Added a test that drives the router's own
_stream_ask_responseagainst the real endpoint, and ran it: it passes, so the assumption holds on the wire. It asserts more than presence: it records the accumulated content at the moment eachcitationevent fires and asserts that content ends with that marker's[n], which is what makes a citation inline rather than a trailing bundle of links.The first run came back
403 API key requiredrather than reaching the model:_resolve_provideronly falls back to the platform key for an authorized origin, and the generator was called withorigin=None. The test now takes an origin from HED's owncors_originsrather than hardcoding one, so origin, key source, and the citations flag hang together the way production wires them.extract_text has no production caller
Correct, since the API switched to citation-aware assembly. Kept as the text-side counterpart of
extract_citations, with a docstring note saying so and explaining that an answer must not be assembled through it, because it would carry citations no client could place.Accepted as a documented limitation, not fixed
The widget linkifies any bare
[n]matching a known marker, so prose that itself contains a bracketed number colliding with a live marker index would render a link to the wrong source. Plain[n]was chosen so the marker stays readable in a client that renders nothing special; switching to a distinctive delimiter would change the API surface, the widget, and the streamed content these tests pin. The consequence is one wrong link, not lost data. Filed as a follow-up alongside the preloaded-documents limitation from #364.Tests
The failure is the pre-existing
test_version_shows_version(Rich ANSI codes), which fails on the base branch too.ruff checkandruff format --checkclean. The newllm-marked streaming test was run against the live endpoint and passes.