Skip to content

fix: cap citable FAQ text and verify streamed citations against the live endpoint - #388

Merged
neuromechanist merged 1 commit into
feature/issue-360-epic-anthropic-platformfrom
fix/issue-383-review-findings
Sep 17, 2026
Merged

neuromechanist merged 1 commit into
feature/issue-360-epic-anthropic-platformfrom
fix/issue-383-review-findings

Conversation

@neuromechanist

Copy link
Copy Markdown
Member

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 that src/knowledge/search.py has already truncated, but a FAQ answer comes straight from the database, capped only at ingest (5000 chars, src/knowledge/db.py). 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 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.

TestCitablePayloadsAreBounded covers 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 an on_chat_model_stream chunk 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_response against 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 each citation event 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 required rather than reaching the model: _resolve_provider only falls back to the platform key for an authorized origin, and the generator was called with origin=None. The test now takes an origin from HED's own cors_origins rather 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

uv run pytest tests/ -q -m "not llm and not integration and not network and not slow"
2085 passed, 13 skipped, 1 failed

The failure is the pre-existing test_version_shows_version (Rich ANSI codes), which fails on the base branch too. ruff check and ruff format --check clean. The new llm-marked streaming test was run against the live endpoint and passes.

…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
neuromechanist merged commit 26712ee into feature/issue-360-epic-anthropic-platform Sep 17, 2026
3 of 4 checks passed
@neuromechanist
neuromechanist deleted the fix/issue-383-review-findings branch September 17, 2026 16:12
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