WeKnora icon indicating copy to clipboard operation
WeKnora copied to clipboard

[Bug]: res:// orphan resource aliases leak into streaming responses, causing broken images in chat

Open lisheguo opened this issue 4 months ago • 2 comments

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:

  1. llmresource.Registry - compacts resource:// handles into short aliases res://XXXX (e.g. res://0001) before sending to the LLM, and restores them via StreamDecoder in the response.
  2. llmreference.Registry - handles <ref/> citation tags and expands resource:// handles to accessible /files/xxx URLs.

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

  1. Deploy WeKnora v0.7.0 with local storage (STORAGE_TYPE=local)
  2. Upload a PDF document containing images to a knowledge base
  3. Ask a question that causes the model to reference document images in the answer
  4. Observe that some images in the response are broken
  5. 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

  1. Add OrphanAliases check to the streaming path: After flushDecoders and each ResponseTypeAnswer chunk, detect and strip orphan aliases.
  2. Strip in DecodeText / StreamDecoder.Feed: Replace unresolvable res://XXXX tokens with empty string or placeholder text instead of passing them through.
  3. 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

lisheguo avatar Jul 24 '26 09:07 lisheguo

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-112 decodes both the answer and thinking channels through modelContext.StreamDecoder(). Its last stage (orphanResourceStreamFilter, internal/modelcontext/stream.go) removes any handle-shaped res://<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-71 runs the same composed decoder per chunk; the OrphanResourceHandles warning further down is observability only.
  • Non-streaming — Registry.DecodeOutputText (internal/modelcontext/registry.go:329) calls StripOrphanHandles after DecodeText, and it is applied at registry.go:177-178 and session_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.

vicfei avatar Sep 18 '26 02:09 vicfei

Fixed by #2330: unresolved res:// handles are now stripped from the stream (internal/modelcontext/stream.go) in both the chat pipeline and the agent.

lyingbug avatar Sep 25 '26 06:09 lyingbug