Files
DeepTutor/tests/multi_user/test_kb_manifest_access.py
Bingxi Zhao (Frank) a43f9fdf2a feat(knowledge): answer "how many files" from an inventory, not retrieval
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.
2026-07-26 22:02:54 +08:00

71 lines
2.7 KiB
Python

"""``resolve_kb_manifest`` is the one seam that reads a KB's document list.
Both consumers (the chat system-prompt inventory and the ``kb_files`` tool) go
through it, so per-user visibility has to hold here: a user's own KBs resolve,
an admin KB resolves only while it is granted, and anything else yields
``None`` rather than a listing.
"""
from __future__ import annotations
from pathlib import Path
from deeptutor.knowledge.manager import KnowledgeBaseManager
from deeptutor.multi_user.knowledge_access import resolve_kb_manifest
def _make_kb(manager: KnowledgeBaseManager, name: str, *files: str) -> None:
"""Register a KB and stage documents into it, as an upload would."""
raw = Path(manager.base_dir) / name / "raw"
raw.mkdir(parents=True, exist_ok=True)
for filename in files:
(raw / filename).write_bytes(b"x" * 512)
manager.register_knowledge_base(name, description=f"test KB {name}")
def test_user_sees_their_own_kb(mu_isolated_root, as_user) -> None:
from deeptutor.multi_user.knowledge_access import current_kb_manager
with as_user("u_alice", role="user"):
_make_kb(current_kb_manager(), "alice-kb", "a.pdf", "b.pdf")
manifest = resolve_kb_manifest("alice-kb")
assert manifest is not None
assert manifest.total == 2
assert [document.name for document in manifest.documents] == ["a.pdf", "b.pdf"]
def test_pattern_and_limit_reach_the_filesystem(mu_isolated_root, as_user) -> None:
from deeptutor.multi_user.knowledge_access import current_kb_manager
with as_user("u_alice", role="user"):
_make_kb(current_kb_manager(), "alice-kb", "a.pdf", "b.pdf", "notes.md")
manifest = resolve_kb_manifest("alice-kb", pattern="*.pdf", limit=1)
assert manifest is not None
assert (manifest.total, manifest.matched, manifest.omitted) == (3, 2, 1)
def test_unknown_kb_yields_no_manifest(mu_isolated_root, as_user) -> None:
with as_user("u_alice", role="user"):
assert resolve_kb_manifest("does-not-exist") is None
def test_ungranted_admin_kb_yields_no_manifest(mu_isolated_root, as_user) -> None:
"""Naming an admin KB directly must not leak its file list (403 → None)."""
from deeptutor.multi_user.knowledge_access import admin_kb_manager
with as_user("u_admin", role="admin"):
_make_kb(admin_kb_manager(), "admin-kb", "secret.pdf")
with as_user("u_alice", role="user"):
assert resolve_kb_manifest("admin:kb:admin-kb") is None
def test_empty_reference_yields_no_manifest(mu_isolated_root, as_user) -> None:
with as_user("u_alice", role="user"):
assert resolve_kb_manifest("") is None
assert resolve_kb_manifest(None) is None