Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(chat): prevent PDF attachment tool hallucinations

XZH6b96364d83cb
diff --git a/deeptutor/capabilities/explore_context/prompts/en/explore_context.yaml b/deeptutor/capabilities/explore_context/prompts/en/explore_context.yaml
index 9c189079a..65f910718 100644
--- a/deeptutor/capabilities/explore_context/prompts/en/explore_context.yaml
+++ b/deeptutor/capabilities/explore_context/prompts/en/explore_context.yaml
@@ -106,9 +106,10 @@ briefing_header: |
[Context Investigation]
An investigator examined the materials the user attached to this turn,
focused on your current task. The following is an objective, third-person
- account for your grounding. Any conversation transcripts referenced below are
- the user's exchanges with OTHER agents — not your own work. Do not adopt
- their voice or claim their actions.
+ account for your grounding. Do not call a file, PDF, or source-loading tool to
+ reopen these materials. Any conversation transcripts referenced below are the
+ user's exchanges with OTHER agents — not your own work. Do not adopt their
+ voice or claim their actions.
status:
exploring: "Context exploration"
diff --git a/deeptutor/capabilities/explore_context/prompts/zh/explore_context.yaml b/deeptutor/capabilities/explore_context/prompts/zh/explore_context.yaml
index 14fe89f13..e6fb85ab7 100644
--- a/deeptutor/capabilities/explore_context/prompts/zh/explore_context.yaml
+++ b/deeptutor/capabilities/explore_context/prompts/zh/explore_context.yaml
@@ -81,8 +81,9 @@ user_template: |
briefing_header: |
[上下文调查]
调查员围绕你的当前任务,调查了用户在本回合附带的材料,以下是供你作答依据的客观、
- 第三人称结论。下面提到的任何对话记录,都是用户与「其他 agent」的往来——不是你自己
- 做的事。不要沿用其口吻,也不要把其中的动作说成是你做的。
+ 第三人称结论。不要调用文件、PDF 或来源读取工具重新打开这些材料。下面提到的任何
+ 对话记录,都是用户与「其他 agent」的往来——不是你自己做的事。不要沿用其口吻,也不
+ 要把其中的动作说成是你做的。
status:
exploring: "上下文调查"
diff --git a/deeptutor/services/session/source_inventory.py b/deeptutor/services/session/source_inventory.py
index 1da3a0385..8d0492e1b 100644
--- a/deeptutor/services/session/source_inventory.py
+++ b/deeptutor/services/session/source_inventory.py
@@ -1,9 +1,9 @@
"""Branch-isolated, cumulative source inventory for the chat capability.
-The chat pipeline shows the LLM an "Attached Sources" manifest each turn so
-it can decide whether to call ``read_source(id)`` for full text. Historically
-this manifest only listed sources the user attached in the *current* turn —
-so the model forgot anything uploaded in earlier turns unless re-attached.
+The chat pipeline shows the LLM an "Attached Sources" manifest each turn.
+Historically this manifest only listed sources the user attached in the
+*current* turn — so the model forgot anything uploaded in earlier turns unless
+re-attached.
This module materialises the manifest as a **session-cumulative inventory**:
@@ -11,8 +11,7 @@ This module materialises the manifest as a **session-cumulative inventory**:
in the manifest, just like before.
* Sources attached in *prior* turns on the active branch's ancestor chain
(the "historical" set) get a compact one-line row: id, name, kind, size,
- and the turn ordinal where they first appeared. The LLM can call
- ``read_source(id)`` to load full text when the question warrants it.
+ and the turn ordinal where they first appeared.
Both sets dedupe by source id; fresh always wins on collision. Branch
isolation is enforced by walking ``parent_message_id`` from the active
@@ -23,8 +22,9 @@ The output is decoupled from the rest of ``turn_runtime``:
inventory = await build_inventory(store, ..., fresh_*=...)
manifest_text, source_index = render_manifest(inventory)
-``source_index`` is the per-turn ``{source_id: full_text}`` map handed to
-``ReadSourceTool`` via tool-call kwargs injection.
+``source_index`` is the per-turn ``{source_id: full_text}`` map consumed by the
+Context Investigator. The answer loop receives that investigation and does not
+mount ``ReadSourceTool`` itself.
"""
from __future__ import annotations
@@ -41,8 +41,8 @@ logger = logging.getLogger(__name__)
# Per-source text-preview caps. Fresh sources get a meaningful preview so
# the model can answer simple "is this the right one?" questions without
-# read_source. Historical sources surface only their identity — the model
-# pays the read_source cost only when it actually needs them.
+# reading the full source. Historical sources surface only their identity —
+# the investigator pays the full-text cost only when it needs a source.
MANIFEST_PREVIEW_CHARS_FRESH = 2000
# Image attachments flow through the multimodal block path; never list them.
_IMAGE_MIME_PREFIX = "image/"
@@ -176,10 +176,10 @@ async def build_inventory(
def render_manifest(inv: SourceInventory) -> tuple[str, dict[str, str]]:
"""Render the inventory into (manifest_text, source_index).
- ``manifest_text`` is the human/LLM-readable block injected at the tail
- of the chat system prompt. ``source_index`` maps each source id to its
- full extracted text and is handed to ``ReadSourceTool`` so the LLM can
- read on demand.
+ ``manifest_text`` is the human/LLM-readable block injected into both the
+ Context Investigator and the chat prompt. ``source_index`` maps each source
+ id to its full extracted text and is consumed by the investigator's
+ ``ReadSourceTool``.
"""
if inv.is_empty():
return "", {}
@@ -194,8 +194,9 @@ def render_manifest(inv: SourceInventory) -> tuple[str, dict[str, str]]:
"An index of the sources the user has attached in this conversation. "
"Rows with a `preview` field were attached **this turn**; rows marked "
"`previously attached (turn N)` were uploaded in earlier turns and show "
- "only their identity. Their full text can be loaded on demand when a "
- "source is relevant. Refer to sources by name; never invent source ids."
+ "only their identity. Relevant full text is examined by the Context "
+ "Investigator. Refer to sources by name; never invent source ids or "
+ "call a file/PDF tool to reopen them."
)
return header + "\n\n" + "\n\n".join(rendered_rows), source_index
diff --git a/tests/capabilities/test_explore_context_capability.py b/tests/capabilities/test_explore_context_capability.py
index 8d76acef2..8510ff63d 100644
--- a/tests/capabilities/test_explore_context_capability.py
+++ b/tests/capabilities/test_explore_context_capability.py
@@ -101,6 +101,7 @@ async def test_single_pass_returns_investigation_block(
assert block.name == "explore_context"
# Header framing + the model's third-person account are both present.
assert "Context Investigation" in block.content
+ assert "Do not call a file, PDF, or source-loading tool" in block.content
assert "the other agent updated the nav." in block.content
diff --git a/tests/services/session/test_source_inventory.py b/tests/services/session/test_source_inventory.py
index 4eedafcd7..741be032d 100644
--- a/tests/services/session/test_source_inventory.py
+++ b/tests/services/session/test_source_inventory.py
@@ -161,6 +161,24 @@ def test_render_manifest_distinguishes_fresh_vs_historical() -> None:
assert idx["at-old"] == "x" * 5000
+def test_render_manifest_does_not_advertise_answer_loop_source_tools() -> None:
+ inv = SourceInventory()
+ inv.add(
+ SourceEntry(
+ sid="at-old",
+ kind="attachment",
+ name="homework.pdf",
+ full_text="PDF body",
+ fresh=False,
+ first_seen_turn=1,
+ )
+ )
+ text, _ = render_manifest(inv)
+ assert "Context Investigator" in text
+ assert "call a file/PDF tool" in text
+ assert "loaded on demand" not in text
+
+
# ---------------------------------------------------------------------------
# build_inventory
# ---------------------------------------------------------------------------