CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 12s
CI & Build / TypeScript typecheck (push) Successful in 56s
CI & Build / integration (push) Successful in 1m4s
CI & Build / Python tests (push) Successful in 1m55s
CI & Build / Build & push image (push) Successful in 44s
Step 2 of the Scribe -> Grimoire rename. - src/scribe -> src/grimoire, and every import, patch target, module path, hypercorn entry point and `python -m grimoire.restore` with it; pyproject, uv.lock and the frontend package are named grimoire. - Image git.fabledsword.com/bvandeusen/fabledgrimoire; CI's Postgres service runs as grimoire / grimoire_test. - Compose files default the DB user, database and (prod) volume to grimoire, each overridable from .env, so an install created as Scribe keeps its data by setting POSTGRES_USER, POSTGRES_DB and PGDATA_VOLUME. - Backups are written as grimoire-<ts>.tar with format grimoire-backup-tar. Archives named scribe-* with the scribe-backup-tar manifest still restore and are still counted at the destination (folder and S3), with tests. - Exports, the MCP auth realm, the ASGI scope key and the persistent logger carry the new name. Kept on purpose: the secret-sealing key label stays "scribe.secrets.v1", because every stored secret was derived under it. Plugin identity (plugin name, hooks, env vars, .scribe marker, skills) is step 3. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
164 lines
6.5 KiB
Python
164 lines
6.5 KiB
Python
"""Systems become findable by meaning — the charter as an ANSWER.
|
|
|
|
A System's `description` is a charter: several hundred words saying what
|
|
belongs in that area and what does not. It is the answer to "where does this
|
|
go?", and there was no semantic path to it — `list_systems` enumerates and
|
|
`search(system_id=…)` uses a System as a FILTER over notes, so a System could
|
|
narrow a search and could never be the answer to one.
|
|
|
|
These pin the three halves: the document shape, the search's scoping and
|
|
best-chunk contract, and that a charter edited through any door is re-indexed.
|
|
"""
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
from grimoire.services import embeddings as emb
|
|
from grimoire.services import systems as systems_svc
|
|
from tests.helpers import make_mock_session
|
|
|
|
|
|
def _system(system_id=3, name="Retrieval & recall", description="the charter"):
|
|
s = MagicMock(id=system_id, project_id=2, description=description)
|
|
s.name = name # never via the constructor: `name` names the mock
|
|
return s
|
|
|
|
|
|
# --- the document shape ------------------------------------------------------
|
|
|
|
|
|
def test_the_document_is_the_name_and_the_charter():
|
|
assert emb.system_document("Retrieval", "what belongs here") == (
|
|
"Retrieval", "what belongs here"
|
|
)
|
|
|
|
|
|
def test_a_system_with_no_charter_yet_degrades_to_its_name():
|
|
"""It still embeds, just weakly. That is an argument for writing the
|
|
charter, not for padding the document with whatever is to hand."""
|
|
assert emb.system_document("Retrieval", None) == ("Retrieval", None)
|
|
assert emb.system_document("Retrieval", " ") == ("Retrieval", None)
|
|
assert emb.chunk_document(*emb.system_document("Retrieval", None)) == ["Retrieval"]
|
|
|
|
|
|
def test_an_empty_system_produces_no_document_at_all():
|
|
"""Callers gate on falsiness to clear vectors rather than embed nothing."""
|
|
assert emb.system_document("", "") == (None, None)
|
|
assert emb.chunk_document(*emb.system_document("", "")) == []
|
|
|
|
|
|
# --- the search --------------------------------------------------------------
|
|
|
|
|
|
def _rows_ctx(rows):
|
|
session = MagicMock()
|
|
result = MagicMock()
|
|
result.all.return_value = rows
|
|
session.execute = AsyncMock(return_value=result)
|
|
ctx = MagicMock()
|
|
ctx.__aenter__ = AsyncMock(return_value=session)
|
|
ctx.__aexit__ = AsyncMock(return_value=False)
|
|
return session, ctx
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_the_search_collapses_to_best_chunk_and_reports_which_one_won():
|
|
"""A charter is long, and a result shows its NAME — so the paragraph that
|
|
actually decides where a record belongs has to come back with it, or the
|
|
caller judges from two words that cannot say."""
|
|
a, b = _system(3), _system(4, name="Data Model")
|
|
rows = [
|
|
(a, 0.10, 2, "retrieval telemetry belongs here"),
|
|
(b, 0.22, 0, "the persistence layer"),
|
|
(a, 0.40, 0, "a weaker paragraph of the same charter"),
|
|
]
|
|
_session, ctx = _rows_ctx(rows)
|
|
report: dict = {}
|
|
with (
|
|
patch.object(emb, "async_session", return_value=ctx),
|
|
patch.object(emb, "get_embedding", AsyncMock(return_value=[0.0] * 384)),
|
|
patch.object(emb, "can_read_project", AsyncMock(return_value=True)),
|
|
):
|
|
out = await emb.semantic_search_systems(
|
|
7, "where does telemetry go?",
|
|
project_id=2, threshold=0.0, report=report,
|
|
)
|
|
|
|
assert [s.id for _score, s in out] == [3, 4]
|
|
assert out[0][0] == 1.0 - 0.10 # the BEST chunk's score
|
|
assert report["best_chunk"][3] == {
|
|
"index": 2, "text": "retrieval telemetry belongs here",
|
|
}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_a_project_the_caller_cannot_read_returns_nothing():
|
|
"""The charter of a project someone was not given is not an
|
|
answer to any question they are entitled to ask."""
|
|
_session, ctx = _rows_ctx([])
|
|
with (
|
|
patch.object(emb, "async_session", return_value=ctx),
|
|
patch.object(emb, "get_embedding", AsyncMock(return_value=[0.0] * 384)),
|
|
patch.object(emb, "can_read_project", AsyncMock(return_value=False)),
|
|
):
|
|
assert await emb.semantic_search_systems(7, "q", project_id=99) == []
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_an_empty_query_never_reaches_the_embedder():
|
|
with patch.object(emb, "get_embedding", AsyncMock()) as embed:
|
|
assert await emb.semantic_search_systems(7, " ") == []
|
|
embed.assert_not_awaited()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_a_search_that_fails_returns_nothing_rather_than_raising():
|
|
"""A recall aid must never break the call it serves."""
|
|
with patch.object(emb, "get_embedding", AsyncMock(side_effect=RuntimeError)):
|
|
assert await emb.semantic_search_systems(7, "q") == []
|
|
|
|
|
|
# --- staying current ---------------------------------------------------------
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_creating_a_system_indexes_its_charter():
|
|
session = make_mock_session()
|
|
with (
|
|
patch.object(systems_svc, "async_session", return_value=session),
|
|
patch.object(systems_svc.access, "can_write_project", AsyncMock(return_value=True)),
|
|
patch.object(systems_svc, "embed_system") as embed,
|
|
):
|
|
await systems_svc.create_system(7, 2, "Retrieval", description="the charter")
|
|
embed.assert_called_once()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_editing_a_charter_re_indexes_it():
|
|
"""A charter edited and not re-indexed stays findable by what it used to
|
|
say — the stale-vector case, and the one that matters most for a record
|
|
whose whole job is to say where things belong now."""
|
|
system = _system()
|
|
session = make_mock_session()
|
|
session.get = AsyncMock(return_value=system)
|
|
system.deleted_at = None
|
|
with (
|
|
patch.object(systems_svc, "async_session", return_value=session),
|
|
patch.object(systems_svc.access, "can_write_project", AsyncMock(return_value=True)),
|
|
patch.object(systems_svc, "embed_system") as embed,
|
|
):
|
|
await systems_svc.update_system(7, 3, description="a new charter")
|
|
embed.assert_called_once_with(system)
|
|
|
|
|
|
def test_embedding_a_system_never_breaks_the_write_that_saved_it():
|
|
"""A System that saved must not fail on its index refresh. ValueError
|
|
rather than RuntimeError deliberately: RuntimeError is also what "no
|
|
running loop" raises, which is an ordinary sync caller and has its own
|
|
branch — this has to land in the general swallow to prove it exists."""
|
|
with patch(
|
|
"grimoire.services.embeddings.upsert_system_embedding",
|
|
side_effect=ValueError("boom"),
|
|
):
|
|
systems_svc.embed_system(_system()) # returns quietly, does not raise
|