WeKnora/internal/handler/session
lyingbug 2be7bd4063
fix(agent): search bound knowledge bases before answering, and pass suggested-question origins (#3400)
* fix(agent): search bound knowledge bases before answering, and pass suggested-question origins

A user picked the suggested question "为什么在比较图形时需要谨慎处理?"
and the agent answered in round 1 with no tool call, claiming it had
searched the knowledge base and found nothing, then explained misleading
statistical charts. The question had been generated from "Corners Don't
Look Like That: Regarding Screenspace Ambient Occlusion" in a bound base,
where "comparing graphs" means SSAO falloff curves.

Two things let that happen: the grounding prompt said "Stable general
explanations need no lookup", and nothing told the agent where the
question came from - the base in question had no profile, only two
recent titles in the directory, and the click sent the text alone.

Prompt
- With knowledge tools available, search bound bases before answering a
  question about a topic, even one that reads like general knowledge;
  directory profiles and recent titles cannot prove a base lacks it.
- The general-explanation exemption applies only without bound bases.
- Say you searched or found nothing only when a search tool ran.

Question origin
- Suggested questions from FAQ/document chunks also return knowledge_id.
- Picking one on the home or chat page sends question_origin
  {knowledge_base_id, knowledge_id} with that question only.
- The service keeps the origin only when its base is inside the turn's
  resolved scope and the document belongs to that base; it never
  changes the scope (unlike knowledge_base_ids, which narrows it).
- The engine renders it in runtime_context as <question_origin> with the
  base and document handles and a note to search it before answering.

* fix(agent): keep question origins across file/tag scopes and bind them to the send

Review follow-up for the suggested-question origin.

- Scope check: the origin was kept only when its base was in the resolved
  KB list, which is empty for file-only and tag-only scopes even though
  their bases are searched. It is now checked against SearchTargets after
  they are built; the document is kept only when a target for that base
  covers it (unfiltered base, or a document scope listing it).
  resolveQuestionOriginInfo re-checks SearchTargets, looks up the name of
  a base that is not in kbInfos, and the renderer omits an unknown name.
- Frontend: the origin travelled through module-level "pending" state, so
  a send that returned early (agent not ready, upload in progress,
  steering) left it behind and a second pick overwrote it. It now rides
  with the one send: triggerSend(text, options) -> createSession ->
  send-msg -> sendMsg / changeFirstQuery.
- RetrieveKBOnlyWhenMentioned: suggestions are still generated from the
  agent's bases, but picking one retrieved nothing. The origin now
  selects its base, as an @mention would, when the agent may read it.
- API sync: question_origin in docs/api/chat.md and swagger;
  QuestionOrigin on the Go SDK's KnowledgeQARequest/AgentQARequest and
  knowledge_id on SuggestedQuestion.
2026-09-18 17:00:47 +08:00
..
agent_stream_handler.go fix(agent): report failed tool executions as tool_result, not error 2026-09-18 11:11:21 +08:00
agent_stream_handler_test.go fix(agent): report failed tool executions as tool_result, not error 2026-09-18 11:11:21 +08:00
artifact_completion_test.go feat(session): fork conversations from a historical user message (#3304) 2026-09-17 14:20:18 +08:00
artifact_download.go refactor(resource-access): unify scoped authorization and transfer recovery (#3097) 2026-09-08 15:36:33 +08:00
artifact_download_test.go refactor(resource-access): unify scoped authorization and transfer recovery (#3097) 2026-09-08 15:36:33 +08:00
artifact_library.go feat(artifacts): artifacts library page backed by a message_artifacts table (#3384) 2026-09-18 12:10:19 +08:00
artifact_reference.go feat(session): fork conversations from a historical user message (#3304) 2026-09-17 14:20:18 +08:00
artifact_reference_test.go feat(session): fork conversations from a historical user message (#3304) 2026-09-17 14:20:18 +08:00
attachment_processor.go feat(docparser): 引入 anydoc 引擎,在 Go 进程内解析 office 文档 2026-08-16 11:37:27 +08:00
browserskill.go feat(browser): connect local Chrome with task controls and display modes (#3180) 2026-09-11 17:59:27 +08:00
browserskill_test.go feat(browser): connect local Chrome with task controls and display modes (#3180) 2026-09-11 17:59:27 +08:00
command_output_test.go feat(session): fork conversations from a historical user message (#3304) 2026-09-17 14:20:18 +08:00
continue_stream_resource_urls_test.go feat(chat): append messages to a running agent turn (#3123) 2026-09-09 16:43:33 +08:00
fork.go feat(session): fork conversations from a historical user message (#3304) 2026-09-17 14:20:18 +08:00
fork_test.go feat(session): fork conversations from a historical user message (#3304) 2026-09-17 14:20:18 +08:00
handler.go feat(session): fork conversations from a historical user message (#3304) 2026-09-17 14:20:18 +08:00
helpers.go refactor: remove unused private Go helpers 2026-09-17 15:51:51 +08:00
helpers_test.go fix(chat): align live timestamps with persisted created_at 2026-08-24 17:04:48 +08:00
image_upload.go feat(storage): add multi-instance storage backends per workspace 2026-07-15 14:54:39 +08:00
qa.go fix(agent): search bound knowledge bases before answering, and pass suggested-question origins (#3400) 2026-09-18 17:00:47 +08:00
qa_attachment_test.go feat(sandbox) (#2301) 2026-08-10 21:28:20 +08:00
quick_answer_timeline.go fix(chat): 持久化快速问答的检索步骤,刷新后不再丢失时间轴 2026-08-13 21:34:21 +08:00
quick_answer_timeline_test.go fix(chat): 持久化快速问答的检索步骤,刷新后不再丢失时间轴 2026-08-13 21:34:21 +08:00
resolve_agent_test.go fix(agent): PUT no longer erases the avatar, and driver errors stay server-side (#3230) 2026-09-14 20:50:15 +08:00
resource_urls.go fix(api): close the resource_urls=public gaps found in review 2026-08-05 14:42:51 +08:00
resource_urls_test.go fix(api): close the resource_urls=public gaps found in review 2026-08-05 14:42:51 +08:00
sandbox_desktop_rfb.go feat(sandbox): add a graphical desktop to session sandboxes (#3245) 2026-09-14 20:37:37 +08:00
sandbox_desktop_rfb_test.go feat(sandbox): add a graphical desktop to session sandboxes (#3245) 2026-09-14 20:37:37 +08:00
sandbox_desktop_ws.go feat(sandbox): add a graphical desktop to session sandboxes (#3245) 2026-09-14 20:37:37 +08:00
sandbox_desktop_ws_test.go feat(sandbox): add a graphical desktop to session sandboxes (#3245) 2026-09-14 20:37:37 +08:00
sandbox_terminal_bridge.go feat(sandbox): add an interactive terminal and files tab beside chat (#3110) 2026-09-08 17:43:15 +08:00
sandbox_terminal_bridge_test.go feat(sandbox): add an interactive terminal and files tab beside chat (#3110) 2026-09-08 17:43:15 +08:00
sandbox_terminal_idle_test.go feat(sandbox): unify the interactive PTY prompt and reconnect running sandboxes (#3128) 2026-09-09 18:17:10 +08:00
sandbox_terminal_ws.go feat(sandbox): unify the interactive PTY prompt and reconnect running sandboxes (#3128) 2026-09-09 18:17:10 +08:00
search_knowledge_resource_urls_test.go fix(api): address resource_urls review follow-ups 2026-08-05 14:42:51 +08:00
steer.go fix(chat): preserve steering continuity and composer focus 2026-09-09 20:15:23 +08:00
steer_delivery_test.go feat(chat): append messages to a running agent turn (#3123) 2026-09-09 16:43:33 +08:00
steer_live_run_test.go fix(chat): preserve steering continuity and composer focus 2026-09-09 20:15:23 +08:00
steer_wiring_test.go feat(chat): append messages to a running agent turn (#3123) 2026-09-09 16:43:33 +08:00
stream.go fix(api): close the resource_urls=public gaps found in review 2026-08-05 14:42:51 +08:00
stream_usage_test.go feat(agent): report and attribute per-turn LLM token usage 2026-08-24 10:36:19 +08:00
temporary_attachment_persist_test.go feat(attachments): implement normalization and limits for temporary attachment IDs 2026-07-16 17:41:27 +08:00
temporary_document.go refactor(resource-access): unify scoped authorization and transfer recovery (#3097) 2026-09-08 15:36:33 +08:00
title.go feat(session): implement strict owner scope for API-key sessions 2026-07-20 19:31:02 +08:00
types.go fix(agent): search bound knowledge bases before answering, and pass suggested-question origins (#3400) 2026-09-18 17:00:47 +08:00
wiki_fixer_scope.go refactor(resource-access): unify scoped authorization and transfer recovery (#3097) 2026-09-08 15:36:33 +08:00
wiki_fixer_scope_test.go fix(session): scope wiki fixer to shared KB tenant 2026-05-21 11:19:19 +08:00
workspace_checkpoint_completion_test.go feat(session): fork conversations from a historical user message (#3304) 2026-09-17 14:20:18 +08:00