mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-05 17:39:45 +08:00
Asked how many files a knowledge base holds, or whether some document is in it, the model had only `rag` — and retrieval cannot answer either question: the passages it returns say nothing about the size of the collection they came from, so the model guessed from whichever chunks the query happened to match. PageIndex KBs already answered correctly (their doc list is injected into the system prompt) and Obsidian vaults have `list_notes`, which makes this a wiring gap in the default engines rather than a limit of retrieval. `deeptutor/knowledge/manifest.py` now owns a KB's document inventory as facts read off disk, with two consumers that cannot disagree: - the chat system prompt carries a per-KB inventory (count plus a truncated name list), so a count costs no tool round-trip — the failure mode where a weaker model never emits the call at all (cf. #532). Built once per turn on a worker thread, keeping the prompt byte-stable for the whole turn; - a new `kb_files` tool enumerates on demand with glob / substring filtering, mounted under `rag`'s gate (a KB is attached) and deniable per partner the same way. The inventory reports the document set and the KB's index status, and stops there. It deliberately does not report how many documents reached the index: every per-file record available is unreliable — LlamaIndex's docstore.json counts nodes (chunk-level, so it drifts with the chunking config, and costs a multi-MB parse to read), metadata.json's file_hashes is only written on the incremental-add path, and last_indexed_count is the last batch's size rather than a total. A confidently wrong count is worse than none. The prompt block also states the rule that retrieved passages must never be used to infer how many documents a KB holds or whether one exists in it. Connected KBs degrade honestly instead of reporting zero: linked folders and Obsidian vaults are enumerated in place, while a remote LightRAG server and a connected subagent are declared unlistable. Hidden OS/editor entries (.DS_Store, .obsidian/) stop counting as documents, and get_info's raw_documents reads the same definition, so the KB page and the chat answer can no longer disagree about the same KB. Deep Research is untouched: its block loop narrows tools to the four that produce block-level evidence, and a file list is not evidence for a subtopic.
240 lines
9.7 KiB
Python
240 lines
9.7 KiB
Python
"""Tests for the Obsidian knowledge capability: vault ops, hooks, tools, exclusivity."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from deeptutor.agents._shared.tool_composition import ToolMountFlags, compose_enabled_tools
|
|
from deeptutor.capabilities import any_exclusive_capability_active
|
|
from deeptutor.capabilities.obsidian import OBSIDIAN_TOOL_NAMES, ObsidianCapability
|
|
from deeptutor.capabilities.obsidian import binding as obsidian_binding
|
|
from deeptutor.capabilities.obsidian import vault as V
|
|
from deeptutor.capabilities.obsidian.tools import (
|
|
ObsidianAppendTool,
|
|
ObsidianBacklinksTool,
|
|
ObsidianReadTool,
|
|
ObsidianSearchTool,
|
|
)
|
|
from deeptutor.core.context import UnifiedContext
|
|
from deeptutor.runtime.registry.tool_registry import get_tool_registry
|
|
|
|
|
|
def _seed_vault(root: Path) -> None:
|
|
(root / "notes").mkdir(parents=True, exist_ok=True)
|
|
(root / ".obsidian").mkdir(exist_ok=True)
|
|
(root / ".obsidian" / "app.json").write_text("{}", encoding="utf-8")
|
|
(root / "notes" / "Photosynthesis.md").write_text(
|
|
"---\ntags: [biology]\nstatus: draft\n---\n"
|
|
"# Photosynthesis\nConverts light. See [[Chlorophyll]] and #biology.\n",
|
|
encoding="utf-8",
|
|
)
|
|
(root / "Chlorophyll.md").write_text("# Chlorophyll\nGreen pigment.\n", encoding="utf-8")
|
|
(root / "Index.md").write_text("Map: [[Photosynthesis]] is key.\n", encoding="utf-8")
|
|
|
|
|
|
# ---- vault operations (pure) -------------------------------------------------
|
|
|
|
|
|
def test_vault_read_parses_frontmatter_and_body(tmp_path: Path) -> None:
|
|
_seed_vault(tmp_path)
|
|
note = V.read_note(tmp_path, "Photosynthesis")
|
|
assert note["frontmatter"] == {"tags": ["biology"], "status": "draft"}
|
|
assert note["body"].startswith("# Photosynthesis")
|
|
assert note["path"] == "notes/Photosynthesis.md"
|
|
|
|
|
|
def test_vault_search_and_list_skip_internal_dirs(tmp_path: Path) -> None:
|
|
_seed_vault(tmp_path)
|
|
assert [h["path"] for h in V.search_notes(tmp_path, "light")] == ["notes/Photosynthesis.md"]
|
|
assert all(".obsidian" not in p for p in V.list_notes(tmp_path))
|
|
|
|
|
|
def test_vault_links_and_backlinks_follow_wikilinks(tmp_path: Path) -> None:
|
|
_seed_vault(tmp_path)
|
|
assert V.outgoing_links(tmp_path, "Photosynthesis") == ["Chlorophyll"]
|
|
assert [b["path"] for b in V.backlinks(tmp_path, "Photosynthesis")] == ["Index.md"]
|
|
|
|
|
|
def test_vault_tags_ranked_by_count(tmp_path: Path) -> None:
|
|
_seed_vault(tmp_path)
|
|
tags = {row["tag"]: row["count"] for row in V.collect_tags(tmp_path)}
|
|
assert tags["biology"] == 2 # frontmatter list + inline #biology
|
|
|
|
|
|
def test_vault_writes_are_additive(tmp_path: Path) -> None:
|
|
_seed_vault(tmp_path)
|
|
created = V.create_note(
|
|
tmp_path, "Summaries/Light.md", "body [[Photosynthesis]]", {"tags": ["s"]}
|
|
)
|
|
assert created == "Summaries/Light.md"
|
|
with pytest.raises(V.VaultError):
|
|
V.create_note(tmp_path, "Summaries/Light.md", "dup") # no overwrite
|
|
V.append_note(tmp_path, "Chlorophyll", "extra line")
|
|
assert "extra line" in (tmp_path / "Chlorophyll.md").read_text(encoding="utf-8")
|
|
V.set_property(tmp_path, "Chlorophyll", "reviewed", "yes")
|
|
assert V.read_note(tmp_path, "Chlorophyll")["frontmatter"]["reviewed"] == "yes"
|
|
|
|
|
|
def test_vault_refuses_path_traversal(tmp_path: Path) -> None:
|
|
_seed_vault(tmp_path)
|
|
with pytest.raises(V.VaultError):
|
|
V.create_note(tmp_path, "../escape.md", "x")
|
|
|
|
|
|
# ---- capability hooks --------------------------------------------------------
|
|
|
|
|
|
def _bind(monkeypatch, vault_path: str, name: str = "myvault") -> None:
|
|
"""Make ``resolve_kb_metadata`` report ``name`` as an Obsidian vault."""
|
|
monkeypatch.setattr(
|
|
"deeptutor.multi_user.knowledge_access.resolve_kb_metadata",
|
|
lambda ref: (
|
|
{"name": ref, "type": "obsidian", "vault_path": vault_path}
|
|
if ref == name
|
|
else {"name": ref, "type": None}
|
|
),
|
|
)
|
|
|
|
|
|
def test_capability_inactive_without_obsidian_kb(monkeypatch, tmp_path: Path) -> None:
|
|
_bind(monkeypatch, str(tmp_path))
|
|
cap = ObsidianCapability()
|
|
ctx = UnifiedContext(user_message="hi", knowledge_bases=["plain-kb"])
|
|
assert cap.is_active(ctx) is False
|
|
assert cap.system_block(ctx, language="en", prompts={}) is None
|
|
|
|
|
|
def test_capability_active_injects_vault_path(monkeypatch, tmp_path: Path) -> None:
|
|
_bind(monkeypatch, str(tmp_path))
|
|
cap = ObsidianCapability()
|
|
ctx = UnifiedContext(user_message="hi", knowledge_bases=["myvault"])
|
|
assert cap.is_active(ctx) is True
|
|
assert tuple(cap.owned_tools) == OBSIDIAN_TOOL_NAMES
|
|
# vault path injected for obsidian tools, even overwriting a forged value...
|
|
assert cap.augment_kwargs("obsidian_read", {}, ctx)["_vault_path"] == str(tmp_path)
|
|
assert cap.augment_kwargs("obsidian_read", {"_vault_path": "/etc"}, ctx)["_vault_path"] == str(
|
|
tmp_path
|
|
)
|
|
# ...but never for a non-obsidian tool.
|
|
assert "_vault_path" not in cap.augment_kwargs("rag", {}, ctx)
|
|
block = cap.system_block(ctx, language="en", prompts={})
|
|
assert block is not None and "myvault" in block.content
|
|
|
|
|
|
def test_binding_resolved_once_and_cached(monkeypatch, tmp_path: Path) -> None:
|
|
calls = {"n": 0}
|
|
|
|
def fake(ref):
|
|
calls["n"] += 1
|
|
return {"name": ref, "type": "obsidian", "vault_path": str(tmp_path)}
|
|
|
|
monkeypatch.setattr("deeptutor.multi_user.knowledge_access.resolve_kb_metadata", fake)
|
|
ctx = UnifiedContext(user_message="hi", knowledge_bases=["v"])
|
|
obsidian_binding.vault_for_turn(ctx)
|
|
obsidian_binding.vault_for_turn(ctx)
|
|
assert calls["n"] == 1 # second call hits the per-turn cache
|
|
|
|
|
|
# ---- tools -------------------------------------------------------------------
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_tools_fail_without_vault_path() -> None:
|
|
res = await ObsidianSearchTool().execute(query="x")
|
|
assert res.success is False and "vault" in res.content.lower()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_tools_round_trip_against_vault(tmp_path: Path) -> None:
|
|
_seed_vault(tmp_path)
|
|
vp = str(tmp_path)
|
|
hits = json.loads((await ObsidianSearchTool().execute(query="light", _vault_path=vp)).content)
|
|
assert hits["count"] == 1
|
|
read = json.loads(
|
|
(await ObsidianReadTool().execute(note="Photosynthesis", _vault_path=vp)).content
|
|
)
|
|
assert read["frontmatter"]["status"] == "draft"
|
|
back = json.loads(
|
|
(await ObsidianBacklinksTool().execute(note="Photosynthesis", _vault_path=vp)).content
|
|
)
|
|
assert back["backlinks"][0]["path"] == "Index.md"
|
|
appended = await ObsidianAppendTool().execute(
|
|
note="Chlorophyll", content="line", _vault_path=vp
|
|
)
|
|
assert appended.success and json.loads(appended.content)["status"] == "appended"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_read_missing_note_is_graceful(tmp_path: Path) -> None:
|
|
res = await ObsidianReadTool().execute(note="Nope", _vault_path=str(tmp_path))
|
|
assert res.success is False # VaultError surfaced as a clean failure
|
|
|
|
|
|
# ---- exclusivity & registry --------------------------------------------------
|
|
|
|
|
|
def test_exclusive_compose_drops_builtins_but_keeps_coexisting_rag() -> None:
|
|
# Issue #650: an exclusive capability drops chat built-ins / composer
|
|
# toggles, but the KB built-ins coexist when has_kb is set — co-selected
|
|
# LlamaIndex KBs the capability does not own stay both searchable (rag) and
|
|
# enumerable (kb_files). Other flags (code/memory) stay dropped.
|
|
composed = compose_enabled_tools(
|
|
registry=get_tool_registry(),
|
|
requested_tools=["web_search", "reason"],
|
|
optional_whitelist=["web_search", "reason"],
|
|
mount_flags=ToolMountFlags(has_kb=True, has_code=True, has_memory=True),
|
|
capability_owned=["obsidian_search", "obsidian_read"],
|
|
exclusive=True,
|
|
)
|
|
assert set(composed) == {
|
|
"obsidian_search",
|
|
"obsidian_read",
|
|
"rag",
|
|
"kb_files",
|
|
"ask_user",
|
|
}
|
|
|
|
|
|
def test_exclusive_compose_pure_vault_mounts_no_rag() -> None:
|
|
# No coexisting KBs (has_kb=False) → pure vault turn, rag stays off.
|
|
composed = compose_enabled_tools(
|
|
registry=get_tool_registry(),
|
|
requested_tools=["web_search"],
|
|
optional_whitelist=["web_search"],
|
|
mount_flags=ToolMountFlags(has_kb=False),
|
|
capability_owned=["obsidian_search", "obsidian_read"],
|
|
exclusive=True,
|
|
)
|
|
assert set(composed) == {"obsidian_search", "obsidian_read", "ask_user"}
|
|
|
|
|
|
def test_registry_flags_obsidian_turn_as_exclusive(monkeypatch, tmp_path: Path) -> None:
|
|
_bind(monkeypatch, str(tmp_path))
|
|
obsidian_turn = UnifiedContext(user_message="hi", knowledge_bases=["myvault"])
|
|
plain_turn = UnifiedContext(user_message="hi", knowledge_bases=["plain-kb"])
|
|
assert any_exclusive_capability_active(obsidian_turn) is True
|
|
assert any_exclusive_capability_active(plain_turn) is False
|
|
|
|
|
|
def test_owned_kbs_reports_only_vault_refs(monkeypatch, tmp_path: Path) -> None:
|
|
# Issue #650: the capability owns only the vault; a co-selected LlamaIndex KB
|
|
# must NOT be reported as owned (so it keeps its rag surface).
|
|
_bind(monkeypatch, str(tmp_path)) # only "myvault" resolves as obsidian
|
|
cap = ObsidianCapability()
|
|
ctx = UnifiedContext(user_message="hi", knowledge_bases=["myvault", "kb-plain"])
|
|
assert cap.owned_kbs(ctx) == {"myvault"}
|
|
|
|
|
|
def test_obsidian_vault_refs_enumerates_every_selected_vault(monkeypatch, tmp_path: Path) -> None:
|
|
def fake(ref):
|
|
if ref in {"vaultA", "vaultB"}:
|
|
return {"name": ref, "type": "obsidian", "vault_path": str(tmp_path)}
|
|
return {"name": ref, "type": None}
|
|
|
|
monkeypatch.setattr("deeptutor.multi_user.knowledge_access.resolve_kb_metadata", fake)
|
|
ctx = UnifiedContext(user_message="hi", knowledge_bases=["vaultA", "kb1", "vaultB"])
|
|
assert obsidian_binding.obsidian_vault_refs(ctx) == {"vaultA", "vaultB"}
|