[Bug]: res:// orphan resource aliases leak into streaming responses, causing broken images in chat
Affected Component
Backend (Go), Frontend
Bug Description
In knowledge-base Q&A, the streaming response (SSE) sometimes contains undecoded res://XXXX resource aliases (e.g. res://0007), causing broken images in the chat UI. The browser cannot resolve the res:// protocol, so affected images render as broken.
Some images load correctly (those referenced as images/imageFileXXXX.png or properly expanded /files/xxx paths), while others fail (referenced as raw res://XXXX aliases that survived decoding).
Root Cause
WeKnora uses a two-layer registry system for resource references:
llmresource.Registry- compactsresource://handles into short aliasesres://XXXX(e.g.res://0001) before sending to the LLM, and restores them viaStreamDecoderin the response.llmreference.Registry- handles<ref/>citation tags and expandsresource://handles to accessible/files/xxxURLs.
The pipeline in chat_completion_stream.go:
chatMessages, sourceRefs := prepareMessagesWithReferences(ctx, chatManage)
resourceRefs := llmresource.NewRegistry()
chatMessages = sourceRefs.EncodeMessages(chatMessages)
chatMessages = resourceRefs.EncodeMessages(chatMessages)
// ...
answerDecoder := llmresource.NewStreamDecoder(resourceRefs)
answerRefExpander := llmreference.NewStreamExpander(sourceRefs)
response.Content = answerRefExpander.Feed(answerDecoder.Feed(response.Content))
The bug: The LLM sometimes emits res://XXXX aliases that do not exist in the request-local registry (hallucination). StreamDecoder.DecodeText() cannot map them back to resource:// handles, so these orphan aliases pass through as literal text into the SSE stream. The browser cannot resolve res:// protocol -> broken images.
Key Code Locations
Non-streaming path (chat_completion.go:74):
if orphans := resourceRefs.OrphanAliases(chatResponse.Content); len(orphans) > 0 {
pipelineWarn(ctx, "Completion", "orphan_resource_aliases", ...)
}
Only logs a warning - does not strip orphan aliases from the content.
Streaming path (chat_completion_stream.go:114-222):
answerDecoder := llmresource.NewStreamDecoder(resourceRefs)
answerRefExpander := llmreference.NewStreamExpander(sourceRefs)
No OrphanAliases check at all - orphan aliases flow directly into SSE events.
Agent path (agent/think.go:119):
if orphans := e.resourceRefs.OrphanAliases(result.Content); len(orphans) > 0 {
logger.Warnf(ctx, "[Agent][Stream] Model emitted %d unresolvable resource alias(es): %v", ...)
}
Also only logs a warning, does not strip.
Reproduction
- Deploy WeKnora v0.7.0 with local storage (
STORAGE_TYPE=local) - Upload a PDF document containing images to a knowledge base
- Ask a question that causes the model to reference document images in the answer
- Observe that some images in the response are broken
- Inspect the DOM - broken images have
src="res://0007"(or similar) instead of a proper URL
Reproduction Rate
Approximately 30-50% of image-containing responses. Longer context and more resource references increase the likelihood.
Expected Behavior
Orphan res://XXXX aliases should be stripped from the response content before sending to the frontend, or at minimum replaced with a placeholder. The streaming path should have the same OrphanAliases guard as the non-streaming path.
Suggested Fixes
- Add
OrphanAliasescheck to the streaming path: AfterflushDecodersand eachResponseTypeAnswerchunk, detect and strip orphan aliases. - Strip in
DecodeText/StreamDecoder.Feed: Replace unresolvableres://XXXXtokens with empty string or placeholder text instead of passing them through. - Frontend fallback: Markdown renderer should gracefully degrade
res://URLs (hide image or show "image load failed").
Environment
- WeKnora v0.7.0
- Docker deployment (5 containers: frontend/app/postgres/redis/docreader)
- Storage type: local
- LLM: doubao-seed-2.0-lite (Volcengine Ark)
Related
- PR #2075 introduced the resource registry and alias system
- PR #2278 (
fix(agent): resolve request-local source handles) addresses a related but different alias resolution issue in the Agent path - Issue #2179 (
think tags leak into chat responses) is a similar pattern - LLM output not being sanitized in the streaming pipeline
Verified fixed on current main (checked at c8ebb9ed, 2026-09-18): orphan res:// aliases no longer reach the SSE stream on any of the three paths identified in this report.
The two-registry system (llmresource/llmreference) has since been consolidated into internal/modelcontext, and the composed Registry.StreamDecoder() now runs an orphan filter as its final stage. The strip landed on 2026-07-27, three days after this report, via the modelcontext registry commits (5e413c64 / 66632685 / 69b79239) — for context, 38c594ae (07-15) was the earlier detection/logging-only pass this issue was filed against.
- Streaming chat —
chat_pipeline/chat_completion_stream.go:111-112decodes both the answer and thinking channels throughmodelContext.StreamDecoder(). Its last stage (orphanResourceStreamFilter,internal/modelcontext/stream.go) removes any handle-shapedres://<digits>token left after known-handle restoration, and holds partial suffixes ("re", "res://99…") across provider chunk boundaries so a split orphan can't leak either. - Streaming agent —
agent/think.go:70-71runs the same composed decoder per chunk; theOrphanResourceHandleswarning further down is observability only. - Non-streaming —
Registry.DecodeOutputText(internal/modelcontext/registry.go:329) callsStripOrphanHandlesafterDecodeText, and it is applied atregistry.go:177-178andsession_knowledge_qa.go:1231.
Unit coverage exists on the new path (TestOrphanAliasesReportsUnresolvableTokens, TestStripOrphanAliasesRemovesIncompleteNumericHandle, plus the split-suffix stream decoder tests in internal/modelcontext/resources_test.go).
One residual worth noting for anyone still seeing broken images: stripping happens at emission time, so messages persisted before 07-27 can still contain literal res://… aliases and will keep rendering broken in old sessions — there is no backfill. Fresh answers on current main should be clean.
Given all three paths now strip orphans, this looks safe to close as fixed; happy to dig in further if anyone reproduces it on a build that contains the 07-27 commits.
Fixed by #2330: unresolved res:// handles are now stripped from the stream (internal/modelcontext/stream.go) in both the chat pipeline and the agent.