<img width="2318" height="1448" alt="image" src="https://github.com/user-attachments/assets/bca71d69-4972-420c-b14e-c8cf74a17de9" /> ## Summary Everything a knowledge base could not read, and the pieces it cut them into saying what they are. **Reading a PDF.** A PDF records glyphs at coordinates; whether they are a heading, a table cell or a running footer is inferred from how they look. There are now two readers. The geometric one infers structure from type size and position and needs nothing but the file. The other asks deepdoc's models, ported into `raven/knowledge/parser/deepdoc`: a layout model that says what each region is, a detector and recognizer that read a page carrying no text at all, and a table-structure model that rebuilds a grid from a picture of one. Detecting boxes around text the file already spells out exactly costs an inference pass per page to learn less, and recognising it costs accuracy. The layout graph answers with a shape its own postprocessing raises on, and its classes are DocLayout-YOLO's order rather than the list Both were established by running the model, and the label order is pinned by a test. **Tables go in as tables.** `<table>` is the only shape that keeps what a grid states -- which column a cell stood under, which cells are merged, which row is the header -- and it is the shape a model reads a table in, which is what these chunks are for. PDF, Word and PowerPoint all state their merges exactly, and they now survive as `colspan` and `rowspan`. The flat form this replaces had to repeat a header standing over two columns across both of them, because there was nowhere to record a span; it had no callers left afterwards and is gone. Two deviations from upstream, both about what the string is for: every cell's text is escaped, where upstream interpolates it raw and a cell holding a `<` produces markup that is no longer a table, and the span attributes are written only where there is a span, where upstream emits `<td >` for every ordinary cell. **A picture of where each chunk came from.** A chunk is a few hundred words with a page number on it, which is enough to cite and not enough to answer whether the parser read the page correctly. So every PDF chunk that knows where it sits carries one image: the regions it covers, cropped out of the rendered pages and stacked. here they are already structured in the parser's element spans, so the markers would only put text nobody wrote into the thing being embedded. The panel draws a chunk's tables as tables, parsed and rebuilt as elements rather than injected: a person can rewrite any piece from that same panel, so what comes back from the store is user input however it got there. **The page that reads them.** Upstream retired the knowledge page while this branch was out; it is rebuilt against the new architecture, and the half that never existed is the one a reader needs -- what a document actually became once it was cut and embedded. Documents sit in one-level folders and arrive by four routes: a file, a folder, a URL, a written note. Adding and indexing are two calls on the contract and nothing drains that queue on its own, so a page that stopped after the first left every upload saying Queued for good. Opening a file takes the whole page: the document on the left, the pieces the index holds of it on the right. The left half is the file drawn as its own pages rather than framed by the browser's PDF viewer, which reads where to open once and answers to nothing afterwards -- and which nothing can be drawn on. Pages are rendered once and kept, and the region a piece was cut from is washed over the page it came from, placed in fractions of the page so the picture's own scale never enters the arithmetic. Clicking a piece takes the file there; double-clicking opens it for rewriting. The pieces can be searched, filtered by state, turned on and off, deleted, rewritten and added to, one at a time or in a batch. Every one of those was already on the contract with no reader for it. A base's settings are back and fuller: how many pieces a search returns, how documents are cut, and how much prose comes with a table or a figure. The embedding model is a picker while the base holds no pieces and a stated fact once it does -- moving it then is the collection made again at the new width and every document re-cut, which is not a thing a panel does by being saved. **An agent can read them.** The retrieval engine was there and nothing could reach it: no agent had a tool that opened a library. A reader now points a conversation at bases from the composer, the way they pin a working directory, and the selection rides in the same place -- the session's metadata, bound around the turn. The tool takes a query and never a base. The scope is the reader's choice, so a model cannot reach material nobody offered it, and it is withheld where nothing was picked rather than offered and refused. A hit says which document and page it came from, because a hit a reader cannot place is a hit they cannot check -- and the passages are pressable in the transcript, walking into the base, the document, the folder it is filed under and then the piece. A citation a reader cannot follow is a claim they have to take on trust, and the whole point of searching their own material is that they can go and look. **The word segmenter and the resources.** `raven.core.Tokenizer` is Infinity's `rag_tokenizer`, ported: a forward and backward maximum match over a dictionary trie, with stemming and lemmatisation on the latin path. XGBoost is not carried over. None of the data lives in git -- the dictionary, the NLTK corpora and the ONNX graphs run to about a hundred megabytes, so `make fetch-resources` on an install and an optional Docker layer behind `RAVEN_RESOURCES` both point at one script. A checkout that could not reach the network still works, and every feature that reads this data says so when asked. ## Type - [ ] Fix - [x] Feature - [ ] Docs - [ ] CI / tooling - [ ] Refactor - [ ] Other ## Verification Run on macOS against Python 3.12, with the deepdoc weights fetched (the model tests skip without them): ``` uv run pytest 27450 passed, 111 skipped, 3 failed npm run test --prefix ui-web 3276 passed npm run test --prefix ui-tui 2106 passed npm run type-check --prefix ui-web clean (tsc) npm run lint --prefix ui-web clean (4 pre-existing hook warnings) make lint-python clean (ruff check + format) make lint-types clean (ty) make lint-imports 10 contracts kept, 0 broken make check-source-language clean make check-large-files clean make build-ui built, both boot snapshots match node ui-web/scripts/check-css.mjs clean node ui-web/scripts/check-class-namespace.mjs clean ``` The three Python failures are not this branch's and were each confirmed on the base alone: one asks a host without LibreOffice to refuse a render and this host has LibreOffice installed, and two simulation cases fail on an unmodified checkout of the base commit. The RPC contract was regenerated for both consumers and both match it (`npm run gen --prefix ui-web`, `npm run gen:rpc --prefix ui-tui`, then `gen:check`). The page's boot snapshots and region goldens were refreshed where the composer bar gained a button, and each records that button and nothing else. - [x] Relevant tests pass locally - [x] Relevant lint / type checks pass locally - [x] User-facing docs or screenshots are updated when needed ## Risk A PDF's tables, and a Word or PowerPoint file's, are now indexed as HTML rather than as header-prefixed lines. Chunk text changes shape for those documents, so a base indexed before this and searched after it holds both forms until its documents are reindexed; retrieval keeps working either way, because both forms are text. The same applies to how much prose a table or a figure carries with it: the setting now reaches as far as it says, where before it stopped at the nearest paragraph, so a chunk cut before this keeps the narrower context until its document is rebuilt. Rolling back is reverting the branch and reindexing. The deepdoc models are optional. An install that never ran `make fetch-resources` reads PDFs geometrically, exactly as before, and says so when the models are asked for by name. Nothing here reaches the network at request time. Chunk crops and rendered pages are written at index time and on first view, both under the state directory rather than in the repository, and both are deleted with their document. A page is immutable for the life of its document id, so the route serving them caches; the document route does not. All three routes take ids and no path, and the two image routes answer under the strictest sandbox in the tree. What an agent may search is the reader's choice and nothing else. The tool takes no base argument, so a model cannot name one, and it is absent from the schema entirely for a conversation that named none -- a lane nobody pointed at a base is unchanged by this branch. The bases a session carries are stored beside its working directory and travel no further. - [x] Security impact considered - [x] Backward compatibility considered - [x] Rollback path is clear for risky changes ## Related Issues N/A --------- Co-authored-by: Claude (claude-opus-5) <noreply@anthropic.com>
568 lines
20 KiB
Python
568 lines
20 KiB
Python
"""The tool that searches what a conversation was pointed at.
|
|
|
|
Two halves under test. The scope: which bases a turn may reach, which is the
|
|
reader's choice and never the model's. And the answer: what comes back, in
|
|
words a person could check.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
from raven.agent import knowledge_scope
|
|
from raven.agent.tools.knowledge import DEFAULT_LIMIT, MAX_CHARS, MAX_LIMIT, KnowledgeSearchTool
|
|
|
|
pytestmark = pytest.mark.asyncio
|
|
|
|
|
|
class _Chunk:
|
|
def __init__(self, text: str, page: int | None = None) -> None:
|
|
self.text = text
|
|
self.metadata = {"page_number": page} if page is not None else {}
|
|
|
|
|
|
class _Hit:
|
|
def __init__(self, document_id: str, text: str, page: int | None = None, score: float = 0.9) -> None:
|
|
self.document_id = document_id
|
|
self.chunk = _Chunk(text, page)
|
|
self.chunk_id = f"c-{document_id}"
|
|
self.score = score
|
|
self.retrieval = "vector"
|
|
|
|
|
|
class _Found:
|
|
def __init__(self, hits: list[_Hit], by_keyword: dict[str, str] | None = None) -> None:
|
|
self.hits = hits
|
|
self.by_keyword = by_keyword or {}
|
|
|
|
|
|
class _Record:
|
|
def __init__(self, source: str) -> None:
|
|
self.source = source
|
|
|
|
|
|
class _Library:
|
|
"""A manager that answers from memory and records what it was asked."""
|
|
|
|
def __init__(self, hits: list[_Hit] | None = None, by_keyword: dict[str, str] | None = None) -> None:
|
|
self._hits = hits or []
|
|
self._by_keyword = by_keyword or {}
|
|
self.asked: list[tuple[list[str], str, int | None]] = []
|
|
self.raises: Exception | None = None
|
|
|
|
async def search(self, base_ids: list[str], query: str, top_k: int | None = None) -> _Found:
|
|
self.asked.append((list(base_ids), query, top_k))
|
|
if self.raises is not None:
|
|
raise self.raises
|
|
return _Found(self._hits, self._by_keyword)
|
|
|
|
def get_document(self, document_id: str) -> Any:
|
|
return _Record(f"{document_id}.pdf")
|
|
|
|
|
|
async def test_the_tool_searches_what_the_session_was_pointed_at() -> None:
|
|
"""The bases come from the turn's binding, not from an argument: a model
|
|
that could name a base could read material nobody offered it."""
|
|
library = _Library([_Hit("d1", "the answer", page=4)])
|
|
tool = KnowledgeSearchTool(library)
|
|
|
|
with knowledge_scope.bind(("kb-a", "kb-b")):
|
|
out = await tool.execute(query="what is the rota")
|
|
|
|
assert library.asked == [(["kb-a", "kb-b"], "what is the rota", DEFAULT_LIMIT)]
|
|
assert "the answer" in out
|
|
|
|
|
|
async def test_a_base_cannot_be_named_by_the_caller() -> None:
|
|
"""There is no parameter for it, and passing one changes nothing."""
|
|
library = _Library([_Hit("d1", "the answer")])
|
|
tool = KnowledgeSearchTool(library)
|
|
|
|
assert "base" not in str(tool.parameters).lower()
|
|
|
|
with knowledge_scope.bind(("kb-a",)):
|
|
await tool.execute(query="q", base_ids=["kb-secret"], knowledge_bases=["kb-secret"])
|
|
|
|
assert library.asked[0][0] == ["kb-a"]
|
|
|
|
|
|
async def test_a_hit_says_which_document_and_page_it_came_from() -> None:
|
|
"""A hit a reader cannot place is a hit they cannot check."""
|
|
library = _Library([_Hit("handbook", "backups run nightly", page=12)])
|
|
tool = KnowledgeSearchTool(library)
|
|
|
|
with knowledge_scope.bind(("kb-a",)):
|
|
out = await tool.execute(query="backups")
|
|
|
|
assert "handbook.pdf" in out
|
|
assert "page 12" in out
|
|
|
|
|
|
async def test_a_format_with_no_pages_is_named_without_one() -> None:
|
|
"""Absent means the format has none, never page zero."""
|
|
library = _Library([_Hit("notes", "a line")])
|
|
tool = KnowledgeSearchTool(library)
|
|
|
|
with knowledge_scope.bind(("kb-a",)):
|
|
out = await tool.execute(query="anything")
|
|
|
|
assert "notes.pdf" in out
|
|
assert "page" not in out
|
|
|
|
|
|
async def test_nothing_selected_is_said_rather_than_searched() -> None:
|
|
"""Withheld from the schema, but a plugin or a subagent can still call it."""
|
|
library = _Library([_Hit("d1", "unreachable")])
|
|
tool = KnowledgeSearchTool(library)
|
|
|
|
out = await tool.execute(query="anything")
|
|
|
|
assert library.asked == []
|
|
assert "no knowledge bases" in out.lower()
|
|
|
|
|
|
async def test_an_empty_query_is_refused_before_the_engine_is_touched() -> None:
|
|
library = _Library()
|
|
tool = KnowledgeSearchTool(library)
|
|
|
|
with knowledge_scope.bind(("kb-a",)):
|
|
out = await tool.execute(query=" ")
|
|
|
|
assert library.asked == []
|
|
assert "needs a query" in out
|
|
|
|
|
|
async def test_the_limit_is_bounded_at_both_ends() -> None:
|
|
"""A model asking for a hundred pieces is asking for the whole base, and
|
|
the answer would fill the context it was trying to save."""
|
|
library = _Library()
|
|
tool = KnowledgeSearchTool(library)
|
|
|
|
with knowledge_scope.bind(("kb-a",)):
|
|
await tool.execute(query="q", limit=500)
|
|
await tool.execute(query="q", limit=0)
|
|
await tool.execute(query="q", limit="nine")
|
|
|
|
assert [asked[2] for asked in library.asked] == [MAX_LIMIT, 1, DEFAULT_LIMIT]
|
|
|
|
|
|
async def test_a_long_passage_is_cut_rather_than_sent_whole() -> None:
|
|
"""A base cut at eight thousand tokens has pieces that would swallow a turn."""
|
|
library = _Library([_Hit("d1", "x" * (MAX_CHARS + 500))])
|
|
tool = KnowledgeSearchTool(library)
|
|
|
|
with knowledge_scope.bind(("kb-a",)):
|
|
out = await tool.execute(query="q")
|
|
|
|
assert len(out) < MAX_CHARS + 200
|
|
assert out.rstrip().endswith("...")
|
|
|
|
|
|
async def test_finding_nothing_says_so_in_the_words_that_were_asked() -> None:
|
|
library = _Library([])
|
|
tool = KnowledgeSearchTool(library)
|
|
|
|
with knowledge_scope.bind(("kb-a",)):
|
|
out = await tool.execute(query="the rota")
|
|
|
|
assert "'the rota'" in out
|
|
|
|
|
|
async def test_a_keyword_fallback_is_reported_after_the_hits_not_before() -> None:
|
|
"""Those hits are in the list, so it is not an error and does not lead."""
|
|
library = _Library([_Hit("d1", "found by words")], by_keyword={"kb-a": "no endpoint"})
|
|
tool = KnowledgeSearchTool(library)
|
|
|
|
with knowledge_scope.bind(("kb-a",)):
|
|
out = await tool.execute(query="q")
|
|
|
|
assert out.index("found by words") < out.index("matching words")
|
|
|
|
|
|
async def test_a_search_that_fails_is_told_rather_than_raised() -> None:
|
|
"""The turn goes on: one unreachable base is not the end of the answer."""
|
|
library = _Library()
|
|
library.raises = RuntimeError("the collection is gone")
|
|
tool = KnowledgeSearchTool(library)
|
|
|
|
with knowledge_scope.bind(("kb-a",)):
|
|
out = await tool.execute(query="q")
|
|
|
|
assert "the collection is gone" in out
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# the scope itself
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class _Sessions:
|
|
"""Both halves of the manager's read contract.
|
|
|
|
`get_or_create` mints what it cannot find; `peek` does not. A seeded
|
|
double stands for a session that is already on disk, so peek answers from
|
|
the seed -- and answers without storing, which is the half that matters:
|
|
a read must leave the cache as it found it.
|
|
"""
|
|
|
|
def __init__(self, metadata: dict[str, Any] | None = None) -> None:
|
|
self._records: dict[str, Any] = {}
|
|
self._seed = metadata or {}
|
|
|
|
def _record(self) -> Any:
|
|
return type("_S", (), {"metadata": dict(self._seed)})()
|
|
|
|
def get_or_create(self, key: str) -> Any:
|
|
if key not in self._records:
|
|
self._records[key] = self._record()
|
|
return self._records[key]
|
|
|
|
def peek(self, key: str) -> Any:
|
|
if key in self._records:
|
|
return self._records[key]
|
|
return self._record() if self._seed else None
|
|
|
|
|
|
async def test_the_selection_is_read_off_the_session() -> None:
|
|
sessions = _Sessions({knowledge_scope.METADATA_KEY: ["kb-a", "kb-b"]})
|
|
|
|
assert knowledge_scope.read(sessions, "tui:1") == ("kb-a", "kb-b")
|
|
|
|
|
|
async def test_a_session_that_chose_nothing_reaches_nothing() -> None:
|
|
assert knowledge_scope.read(_Sessions(), "tui:1") == ()
|
|
assert knowledge_scope.read(None, "tui:1") == ()
|
|
assert knowledge_scope.read(_Sessions(), "") == ()
|
|
|
|
|
|
async def test_a_selection_keeps_its_order_and_drops_its_repeats() -> None:
|
|
"""The order is the only thing saying which base the reader reached for
|
|
first, and a list is not a set."""
|
|
sessions = _Sessions()
|
|
|
|
written = knowledge_scope.write(sessions, "tui:1", ["kb-b", "kb-a", "kb-b", "", " "])
|
|
|
|
assert written == ("kb-b", "kb-a")
|
|
assert knowledge_scope.read(sessions, "tui:1") == ("kb-b", "kb-a")
|
|
|
|
|
|
async def test_writing_replaces_rather_than_adds() -> None:
|
|
"""The picker sends what is ticked, and a call that added would have no way
|
|
to say that something was unticked."""
|
|
sessions = _Sessions()
|
|
knowledge_scope.write(sessions, "tui:1", ["kb-a", "kb-b"])
|
|
|
|
assert knowledge_scope.write(sessions, "tui:1", ["kb-c"]) == ("kb-c",)
|
|
|
|
|
|
async def test_a_binding_does_not_outlive_its_block() -> None:
|
|
"""One loop serves every session on the process."""
|
|
with knowledge_scope.bind(("kb-a",)):
|
|
assert knowledge_scope.selected() == ("kb-a",)
|
|
|
|
assert knowledge_scope.selected() == ()
|
|
|
|
|
|
async def test_a_session_record_that_cannot_be_read_selects_nothing() -> None:
|
|
class _Broken:
|
|
def get_or_create(self, key: str) -> Any:
|
|
raise OSError("the session file is gone")
|
|
|
|
assert knowledge_scope.read(_Broken(), "tui:1") == ()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# the seam: what a loop offers, and what a turn binds
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class _StubProvider:
|
|
"""Construction reads the default model; no turn is run here."""
|
|
|
|
def get_default_model(self) -> str:
|
|
return "stub-model"
|
|
|
|
async def chat_with_retry(self, **kwargs: Any) -> Any: # pragma: no cover - never invoked
|
|
raise NotImplementedError
|
|
|
|
|
|
def _offered(loop: Any) -> set[str]:
|
|
return {d["function"]["name"] for d in loop.tools.get_definitions()}
|
|
|
|
|
|
async def test_the_tool_is_withheld_where_nothing_was_selected(tmp_path: Any) -> None:
|
|
"""A conversation with no bases is the ordinary case, and a tool that is
|
|
always offered and always answers "you have not selected anything" spends a
|
|
slot in every schema to say so."""
|
|
from raven.agent.loop.main import AgentLoop
|
|
from tests._wiring import wire
|
|
|
|
loop = AgentLoop(provider=_StubProvider(), workspace=tmp_path / "ws", **wire())
|
|
|
|
assert "knowledge_search" not in _offered(loop)
|
|
|
|
|
|
async def test_the_tool_is_offered_once_a_base_is_attached(tmp_path: Any) -> None:
|
|
"""Both directions, mid-conversation: the withholding is read from the
|
|
turn's binding rather than from config, so attaching a base takes effect on
|
|
the next model call."""
|
|
from raven.agent.loop.main import AgentLoop
|
|
from tests._wiring import wire
|
|
|
|
loop = AgentLoop(provider=_StubProvider(), workspace=tmp_path / "ws", **wire())
|
|
|
|
with knowledge_scope.bind(("kb-a",)):
|
|
assert "knowledge_search" in _offered(loop)
|
|
|
|
assert "knowledge_search" not in _offered(loop)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# what the reader is shown: the citations, and whose turn they belong to
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class _Filed(_Library):
|
|
"""A manager whose documents know which base they are in."""
|
|
|
|
def get_document(self, document_id: str) -> Any:
|
|
record = _Record(f"{document_id}.pdf")
|
|
record.base_id = "kb-a"
|
|
return record
|
|
|
|
|
|
async def test_a_search_files_its_hits_for_the_reader_to_follow() -> None:
|
|
"""Text alone cannot be followed: the model gets prose naming the document,
|
|
and the reader gets the ids so the passage can be opened where it sits."""
|
|
tool = KnowledgeSearchTool(_Filed([_Hit("handbook", "backups run nightly", page=12)]))
|
|
|
|
with knowledge_scope.bind(("kb-a",), "tui:1"):
|
|
await tool.execute(query="backups")
|
|
filed = tool.take_metadata()
|
|
|
|
assert filed == {
|
|
"knowledge_hits": [
|
|
{
|
|
"base_id": "kb-a",
|
|
"document_id": "handbook",
|
|
"source": "handbook.pdf",
|
|
"page": 12,
|
|
"chunk_index": 0,
|
|
}
|
|
]
|
|
}
|
|
|
|
|
|
async def test_a_format_with_no_pages_files_no_page() -> None:
|
|
"""Absent means the format has none, never page zero."""
|
|
tool = KnowledgeSearchTool(_Filed([_Hit("notes", "a line")]))
|
|
|
|
with knowledge_scope.bind(("kb-a",), "tui:1"):
|
|
await tool.execute(query="q")
|
|
filed = tool.take_metadata()
|
|
|
|
assert filed is not None
|
|
assert filed["knowledge_hits"][0]["page"] is None
|
|
|
|
|
|
async def test_the_citations_are_taken_once() -> None:
|
|
"""The turn stream pops them: a second read is a second turn's, and
|
|
handing it the first turn's hits would file them under the wrong answer."""
|
|
tool = KnowledgeSearchTool(_Filed([_Hit("d1", "x")]))
|
|
|
|
with knowledge_scope.bind(("kb-a",), "tui:1"):
|
|
await tool.execute(query="q")
|
|
assert tool.take_metadata() is not None
|
|
assert tool.take_metadata() is None
|
|
|
|
|
|
async def test_one_conversation_cannot_take_another_s_citations() -> None:
|
|
"""Two turns run at once on one process, and the tool is one object."""
|
|
tool = KnowledgeSearchTool(_Filed([_Hit("d1", "x")]))
|
|
|
|
with knowledge_scope.bind(("kb-a",), "tui:1"):
|
|
await tool.execute(query="q")
|
|
|
|
with knowledge_scope.bind(("kb-a",), "tui:2"):
|
|
assert tool.take_metadata() is None
|
|
|
|
with knowledge_scope.bind(("kb-a",), "tui:1"):
|
|
assert tool.take_metadata() is not None
|
|
|
|
|
|
async def test_a_search_outside_a_turn_files_nothing() -> None:
|
|
"""A subagent on its own task or a plugin has nowhere to file it and no
|
|
reader waiting for it."""
|
|
tool = KnowledgeSearchTool(_Filed([_Hit("d1", "x")]))
|
|
|
|
with knowledge_scope.bind(("kb-a",)):
|
|
await tool.execute(query="q")
|
|
|
|
assert tool.take_metadata() is None
|
|
|
|
|
|
async def test_finding_nothing_files_nothing() -> None:
|
|
tool = KnowledgeSearchTool(_Filed([]))
|
|
|
|
with knowledge_scope.bind(("kb-a",), "tui:1"):
|
|
await tool.execute(query="q")
|
|
|
|
assert tool.take_metadata() is None
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# the whole chain: a session's selection reaching a turn's tool schema
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class _Watching:
|
|
"""A provider that records the tools it was offered, then says nothing.
|
|
|
|
The schema is assembled per model call, so what it saw is what the turn
|
|
decided -- which is the one thing no unit test above can show.
|
|
"""
|
|
|
|
def __init__(self) -> None:
|
|
self.offered: list[set[str]] = []
|
|
self.api_key = "test"
|
|
|
|
async def chat(self, messages: Any, tools: Any = None, **kwargs: Any) -> Any:
|
|
from raven.providers.base import LLMResponse
|
|
|
|
self.offered.append({d["function"]["name"] for d in (tools or [])})
|
|
return LLMResponse(content="done", tool_calls=[])
|
|
|
|
async def chat_with_retry(self, **kwargs: Any) -> Any:
|
|
return await self.chat(kwargs.get("messages"), kwargs.get("tools"))
|
|
|
|
def get_default_model(self) -> str:
|
|
return "stub"
|
|
|
|
def classify_error(self, exc: BaseException) -> Any: # pragma: no cover - never reached
|
|
raise exc
|
|
|
|
|
|
async def _turn(workspace: Any, picked: list[str]) -> _Watching:
|
|
"""One turn on a session pointed at ``picked``, and what it was offered."""
|
|
from raven.agent import knowledge_scope
|
|
from raven.agent.loop import AgentLoop
|
|
from raven.agent.loop.bundles import ToolWiring, TurnPolicy
|
|
from raven.spine.message import ChatType, Source
|
|
from raven.spine.turn import Origin, TurnRequest
|
|
|
|
provider = _Watching()
|
|
loop = AgentLoop(
|
|
provider=provider,
|
|
workspace=workspace,
|
|
model="stub",
|
|
policy=TurnPolicy(max_iterations=1),
|
|
tools=ToolWiring(restrict_to_workspace=True),
|
|
)
|
|
request = TurnRequest(
|
|
origin=Origin.USER,
|
|
source=Source(channel="tui", chat_id="chat1", sender_id="user", chat_type=ChatType.DM),
|
|
text="what do the documents say",
|
|
)
|
|
if picked:
|
|
knowledge_scope.write(loop.sessions, "tui:chat1", picked)
|
|
# Through `run_turn`, which is how a turn arrives -- the spine calls it and
|
|
# it is where the session's bindings are made. `_process_message` is the
|
|
# older direct path that makes none of them, so a test knocking on that
|
|
# door would report the feature dead however well it was wired.
|
|
await loop.run_turn(request, _nowhere, lambda: [], stream=False)
|
|
return provider
|
|
|
|
|
|
async def _nowhere(*_args: Any, **_kwargs: Any) -> None:
|
|
"""A turn's output, dropped: what it says is not what these are about."""
|
|
return None
|
|
|
|
|
|
async def test_a_turn_offers_the_search_when_its_session_named_a_base(tmp_path: Any) -> None:
|
|
"""The whole chain in one case: the record the picker wrote, the binding the
|
|
turn makes from it, the withholding that reads the binding, and the schema
|
|
the model is handed. Every link is one line, and any of them dropped leaves
|
|
the feature silently dead."""
|
|
provider = await _turn(tmp_path / "ws", ["kb-a"])
|
|
|
|
assert provider.offered, "the turn made no model call"
|
|
assert "knowledge_search" in provider.offered[0]
|
|
|
|
|
|
async def test_a_turn_whose_session_named_none_is_offered_none(tmp_path: Any) -> None:
|
|
provider = await _turn(tmp_path / "ws", [])
|
|
|
|
assert provider.offered, "the turn made no model call"
|
|
assert "knowledge_search" not in provider.offered[0]
|
|
|
|
|
|
# -- which library the tool searches -------------------------------
|
|
|
|
|
|
async def test_the_tool_and_the_gateway_share_one_manager(monkeypatch, tmp_path) -> None:
|
|
"""Two managers over the same directory is two `records.json` snapshots.
|
|
|
|
A `RecordStore` reads the file once, when it is constructed, so the second
|
|
one holds the moment it was first used: a base the reader creates on the
|
|
page afterwards does not exist for the tool, and attaching it and asking
|
|
answers that nothing in the attached material does.
|
|
"""
|
|
from raven.config import paths
|
|
from raven.rpc.methods import knowledge as rpc
|
|
|
|
monkeypatch.setattr(paths, "get_runtime_subdir", lambda name: tmp_path / name)
|
|
monkeypatch.setattr("raven.knowledge._library._manager", None)
|
|
|
|
assert KnowledgeSearchTool()._library() is rpc.knowledge_manager()
|
|
|
|
|
|
async def test_a_base_created_after_the_tool_has_searched_is_still_found(monkeypatch, tmp_path) -> None:
|
|
"""The failure as the reader meets it: search once, make a base, search
|
|
again. The second search found nothing, because the tool was holding the
|
|
library as it stood before the base existed."""
|
|
from raven.config import paths
|
|
from raven.rpc.methods import knowledge as rpc
|
|
|
|
monkeypatch.setattr(paths, "get_runtime_subdir", lambda name: tmp_path / name)
|
|
monkeypatch.setattr("raven.knowledge._library._manager", None)
|
|
|
|
tool = KnowledgeSearchTool()
|
|
tool._library() # the tool's first use, before the base exists
|
|
|
|
made = await rpc.knowledge_manager().create_base(name="notes", embedding=False)
|
|
|
|
assert tool._library().get_base(made.id) is not None
|
|
|
|
|
|
async def test_a_library_handed_in_still_wins(monkeypatch, tmp_path) -> None:
|
|
"""Which is how a case gives this tool a library of its own."""
|
|
from raven.config import paths
|
|
|
|
monkeypatch.setattr(paths, "get_runtime_subdir", lambda name: tmp_path / name)
|
|
monkeypatch.setattr("raven.knowledge._library._manager", None)
|
|
own = _Library([])
|
|
|
|
assert KnowledgeSearchTool(own)._library() is own
|
|
|
|
|
|
async def test_the_manager_is_asked_for_on_every_search(monkeypatch, tmp_path) -> None:
|
|
"""Not cached on the instance: the process's manager can be replaced --
|
|
a test does exactly that -- under a tool that outlives it."""
|
|
from raven.config import paths
|
|
from raven.knowledge import set_manager_for_tests
|
|
|
|
monkeypatch.setattr(paths, "get_runtime_subdir", lambda name: tmp_path / name)
|
|
monkeypatch.setattr("raven.knowledge._library._manager", None)
|
|
tool = KnowledgeSearchTool()
|
|
first = tool._library()
|
|
|
|
replacement = _Library([])
|
|
set_manager_for_tests(replacement)
|
|
try:
|
|
assert tool._library() is replacement
|
|
assert tool._library() is not first
|
|
finally:
|
|
set_manager_for_tests(None)
|