WeKnora icon indicating copy to clipboard operation
WeKnora copied to clipboard

[Bug]: Session titles can leak the answer — title generation has no post-validation when a model answers instead of titling

Open vicfei opened this issue 2 months ago • 2 comments

Affected Component

Backend Service & API / session title generation (internal/application/service/session.go GenerateTitle)

Bug Description

Auto-generated session titles have no validation beyond length truncation. sanitizeGeneratedTitle only strips a <think> prefix and truncates to 100 runes — whatever the model returns becomes the sidebar title verbatim.

The prompt (config/prompt_templates/generate_session_title.yaml) does say "Only extract the intent of the user's question, don't answer about it", but nothing enforces it. With instruction-weak models (small/local/OpenAI-compatible endpoints users configure freely), the model answers the question instead of titling it, and the answer lands in the sidebar before the conversation is even opened.

Observed while testing a deployment with a DeepSeek model configured as the workspace KnowledgeQA model — titles for short factual questions came back as:

  • 42 (question: "What is 6 multiplied by 7?")
  • Yes, they were both American. (a yes/no question)

5 of 6 sampled titles were direct answers rather than titles. Well-behaved models (qwen3.8-max produced "六乘以七等于多少" / "法国首都城市查询" in the same test) follow the instruction, so the leak is model-dependent — but the pipeline accepts whatever shape arrives.

Why this matters

  • The title is persisted and shown in the session sidebar; a leaked answer spoils the reply and looks broken.
  • Title generation runs in parallel with the RAG answer, so the leaked title often appears before the answer streams.
  • Users pick arbitrary OpenAI-compatible models, so the system cannot rely on prompt compliance alone.

Expected Behavior

A generated title that reads like the answer (bare number, yes/no sentence, equation) should be rejected and replaced by a derived-from-question fallback, so the sidebar never shows a leaked answer.

Suggested fix (happy to implement)

  1. Post-generation validation: conservative heuristics detect answer-shaped titles — bare numbers with optional short unit ("42", "8848米", "98%"), equations ("6×7=42"), yes/no openers ("Yes, they were both American.", "是的"). On match, fall back to a rune-truncated first line of the user question (always a valid title).
  2. Prompt hardening: add explicit "NEVER answer the question" wording plus few-shot examples for short-factual questions to generate_session_title.yaml.

Both are model-agnostic: well-behaved models are unaffected, weak models get a guaranteed-shaped title.

WeKnora Version

upstream main @ 647848f3 (behavior present since title generation exists)

Confirmation

  • [x] I have searched existing issues and found none covering title-answer leakage or title post-validation.

vicfei avatar Sep 08 '26 07:09 vicfei

Transparency update from further testing, so the record is complete:

Could not reproduce the answer-leak on stock main with fresh samples: 9 short factual questions (mix of arithmetic / yes-no / single-fact) through a deepseek-v4-flash KnowledgeQA model and 4 more through qwen3.8-max — every generated title was a proper question-topic title ("六乘以七的结果", "确认巴黎是否法国首都", …). The leak observations in the issue body came from one of our other deployment environments with a differently-configured DeepSeek model, so the leak is model/config-dependent rather than universal.

The code-level gap stands regardless: nothing validates title shape beyond length truncation, so compliance rests entirely on the model following the prompt. Users configure arbitrary OpenAI-compatible endpoints, which is why I'd still argue for the (cheap, conservative) post-generation fallback + prompt few-shot hardening described above — defense in depth, not a fix for a universal failure.

The extra sampling did catch one first-hand cosmetic defect though: models sometimes return the title wrapped in literal quotes — we got “询问一加一等于多少” persisted verbatim. Quote-stripping belongs in sanitizeGeneratedTitle alongside the whitespace trim.

vicfei avatar Sep 08 '26 08:09 vicfei

The fix is up as #3176: a conservative leak heuristic + question-derived fallback + never-answer few-shot prompt hardening, plus quote stripping in the sanitizer for the wrapping-quote defect observed first-hand on stock main. Since the leak itself is model/config dependent, this treats validation as defense-in-depth rather than relying on prompt compliance alone.

vicfei avatar Sep 10 '26 15:09 vicfei