diff --git a/deeptutor/capabilities/explore_context/explorer.py b/deeptutor/capabilities/explore_context/explorer.py index 18c6cadae..75eb55e4c 100644 --- a/deeptutor/capabilities/explore_context/explorer.py +++ b/deeptutor/capabilities/explore_context/explorer.py @@ -658,17 +658,10 @@ class ContextExplorer: mode: str, manifest: str, ) -> list[dict[str, Any]]: - """Lay the investigation out so the repeatable bytes come first. + """Keep preloaded source text in the shared prefix on follow-up turns. - Provider prompt caches match a *prefix*, so only the bytes before the - first per-turn difference can ever be reused. The original layout put - the user's question at the top of the first message, which meant the - longest shared prefix with the previous turn ended a few tokens in and - every byte the pre-pass re-read — including tens of thousands of tokens - of unchanged source text — was billed at the miss rate, every turn. - Here the question is the *last* message and everything ahead of it - (mode, manifest, attached sources) is byte-stable for as long as the - attachments are, so it becomes a cache hit. + The manifest replaces fresh previews with references on later turns, + so it belongs with the current mode and question after the source text. Returns ``[]`` when the localized split prompts are absent, leaving the caller on the original single-message layout. @@ -678,15 +671,15 @@ class ContextExplorer: if not preamble_template or not question_template: return [] selected = self._preload_selection(source_index) - sources_text = self._render_preloaded_sources(source_index, selected) + fields = { + "sources": self._render_preloaded_sources(source_index, selected) or "(none)", + "mode": mode, + "manifest": manifest, + "question": question, + } return [ - { - "role": "user", - "content": preamble_template.format( - mode=mode, manifest=manifest, sources=sources_text or "(none)" - ), - }, - {"role": "user", "content": question_template.format(question=question)}, + {"role": "user", "content": preamble_template.format(**fields)}, + {"role": "user", "content": question_template.format(**fields)}, ] def _preload_selection(self, source_index: dict[str, str]) -> list[str]: diff --git a/deeptutor/capabilities/explore_context/prompts/en/explore_context.yaml b/deeptutor/capabilities/explore_context/prompts/en/explore_context.yaml index 90a43ff88..e120bb530 100644 --- a/deeptutor/capabilities/explore_context/prompts/en/explore_context.yaml +++ b/deeptutor/capabilities/explore_context/prompts/en/explore_context.yaml @@ -64,25 +64,24 @@ loop: Begin investigating: call read_source as needed, then write your objective, thorough investigation. - # Cache-shaped split of the same message: everything that repeats across - # turns (mode, manifest, attached sources) comes first, and the only per-turn - # bytes — the user's request — come last, so the repeated prefix is a prompt - # cache hit. Falls back to the single-message user_template above when these - # keys are absent. + # Keep preloaded text ahead of the mode and manifest. A follow-up replaces + # the fresh upload preview in the manifest with a reference to the earlier turn. preamble_template: | - Current mode: {mode} - - Manifest of readable sources (use read_source to load full text by id): - {manifest} - - Full text of this turn's attached sources (already loaded for you — do not - call read_source for these again): + Preloaded source texts by ID: {sources} - Begin investigating: call read_source for any further sources you need, - then write your objective, thorough investigation. + The full text of every source above is already here. Use it directly and do + not call read_source for these IDs. question_template: | + Current mode: {mode} + + Manifest of readable sources: + {manifest} + + Call read_source by ID only for sources you need whose full text was not + preloaded above. Then write your objective, thorough investigation. + The user's current request: {question} diff --git a/deeptutor/capabilities/explore_context/prompts/zh/explore_context.yaml b/deeptutor/capabilities/explore_context/prompts/zh/explore_context.yaml index 76e4e4ca5..5fdce1fc1 100644 --- a/deeptutor/capabilities/explore_context/prompts/zh/explore_context.yaml +++ b/deeptutor/capabilities/explore_context/prompts/zh/explore_context.yaml @@ -47,23 +47,22 @@ loop: 现在开始调查:按需调用 read_source,然后写出你客观、详尽的调查结论。 - # 缓存友好版:把「每轮都一样」的内容(模式、清单、来源全文)放在前面, - # 把「每轮都变」的用户请求放在最后一条消息。前缀缓存只认公共前缀, - # 请求放在最前面会让后面所有稳定内容永远无法命中。 - # 缺少这两个键时回退到上面的 user_template 单条消息布局。 + # 预读全文放在模式和清单之前。上传时清单带预览,追问时会换成对先前回合的引用。 preamble_template: | - 当前模式:{mode} - - 可读来源清单(用 read_source 按 id 读取全文): - {manifest} - - 本回合附带来源的全文(已为你预先读取,不必再次调用 read_source): + 已预读的来源全文(按来源 ID 列出): {sources} - 现在开始调查:需要时继续按需调用 read_source 追读其它来源,然后写出你客观、 - 详尽的调查结论。 + 以上来源的全文已经完整给出,直接使用,不要再对这些 ID 调用 read_source。 question_template: | + 当前模式:{mode} + + 可读来源清单: + {manifest} + + 只有在需要的来源尚未预读全文时,才按来源 ID 调用 read_source。 + 根据读到的材料写出客观、详尽的调查结论。 + 用户当前的请求: {question} diff --git a/tests/capabilities/test_explore_context_capability.py b/tests/capabilities/test_explore_context_capability.py index c275a3ba2..c1733c9a8 100644 --- a/tests/capabilities/test_explore_context_capability.py +++ b/tests/capabilities/test_explore_context_capability.py @@ -9,6 +9,7 @@ import pytest from deeptutor.capabilities.explore_context import ExploreContextCapability from deeptutor.capabilities.explore_context import explorer as explorer_mod +from deeptutor.capabilities.explore_context.capability import _load_prompts from deeptutor.capabilities.protocol import PromptBlock from deeptutor.core.context import Attachment, UnifiedContext from deeptutor.core.stream import StreamEventType @@ -659,6 +660,33 @@ def test_explorer_kind_label_from_prefix() -> None: assert explorer._kind_label("zz-abc") == "Source" +@pytest.mark.parametrize("language", ["en", "zh"]) +def test_preloaded_source_text_stays_in_the_shared_prefix(language: str) -> None: + explorer = explorer_mod.ContextExplorer(language=language, prompts=_load_prompts(language)) + source_text = "Lecture notes on the sampling theorem." + sources = {"at-lecture": source_text} + fresh = "at-lecture: preview from the upload" + previous = "at-lecture: previously attached (turn 1)" + upload = explorer._cache_shaped_messages( + source_index=sources, mode="chat", manifest=fresh, question="What is the theorem?" + ) + followup = explorer._cache_shaped_messages( + source_index=sources, mode="deep_solve", manifest=previous, question="Can you prove it?" + ) + + assert upload[0] == followup[0] + assert source_text in upload[0]["content"] + # Without this the model re-reads the preloaded text with read_source on + # every turn, sending the whole lecture a second time. + assert "read_source" in upload[0]["content"] + for messages, mode, manifest, question in ( + (upload, "chat", fresh, "What is the theorem?"), + (followup, "deep_solve", previous, "Can you prove it?"), + ): + assert [m["role"] for m in messages] == ["user", "user"] + assert all(value in messages[1]["content"] for value in (mode, manifest, question)) + + def test_preload_never_exceeds_limit_even_for_the_first_source(): explorer = object.__new__(explorer_mod.ContextExplorer) sources = {"too-large": "x" * 120001, "b": "y" * 80000, "a": "z" * 40000, "extra": "q"}