Files
FabledGrimoire/tests/test_record_read_hook.py
T
bvandeusenandClaude Opus 5.5 b0fab00fe2
CI & Build / Plugin hooks (push) Successful in 18s
CI & Build / Python lint (push) Successful in 2s
CI & Build / TypeScript typecheck (push) Successful in 38s
CI & Build / Python tests (push) Successful in 1m59s
CI & Build / integration (push) Successful in 2m6s
CI & Build / Build & push image (push) Successful in 32s
feat(7831): every record a session is shown, asks for or opens on its own is recorded per session, by kind
A ledger beside the rule deliveries (record_deliveries, migration 0136):
- every note, snippet, issue, lesson or process line a plugin door pushes
  is recorded against the session, only when the payload kept it;
- a search the session runs itself records its results as asked, matched
  to the server log of that call;
- an open is stamped on the delivery it followed, or recorded as direct
  when nothing showed the record first.

The plugin reports opens and searches from the tool calls
(grimoire_record_read.sh). The readout is in retrieval_telemetry
(record_reach), at /api/retrieval/record-reach, and in Settings > Moments
as "How records reach the work". Plugin 2026.11.24.1827.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 13:27:13 -05:00

60 lines
2.5 KiB
Python

"""`grimoire_record_read.sh`, run for real through bash with a stand-in `curl`:
an opened record and a search the session ran are reported to the server
with the session, and the hook is registered on exactly those tools."""
from __future__ import annotations
import json
import re
from tests.test_rule_moment_signal_hooks import HOOKS, _run, sandbox # noqa: F401 - fixture
ROOT = HOOKS.parents[1]
def _sent(tmp) -> tuple[str, dict | None]:
log = (tmp / "curl.log").read_text() if (tmp / "curl.log").exists() else ""
body = (tmp / "curl.body").read_text() if (tmp / "curl.body").exists() else ""
return log, (json.loads(body) if body else None)
def test_an_opened_record_is_reported_with_its_session(sandbox): # noqa: F811
tmp, env = sandbox
_run("grimoire_record_read.sh",
{"session_id": "s1", "tool_name": "mcp__grimoire__get_task", "tool_input": {"task_id": 42}},
env)
log, body = _sent(tmp)
assert "/api/plugin/record-opened" in log
assert body == {"session_id": "s1", "note_id": 42}
def test_a_search_is_reported_with_its_query(sandbox): # noqa: F811
tmp, env = sandbox
_run("grimoire_record_read.sh",
{"session_id": "s1", "tool_name": "mcp__plugin_grimoire_grimoire__search",
"tool_input": {"q": 'why does "the lock" stick', "content_type": "issue"}},
env)
log, body = _sent(tmp)
assert "/api/plugin/record-searched" in log
assert body == {"session_id": "s1", "q": 'why does "the lock" stick'}
def test_a_process_opened_by_name_is_not_reported(sandbox): # noqa: F811
tmp, env = sandbox
_run("grimoire_record_read.sh",
{"session_id": "s1", "tool_name": "mcp__grimoire__get_process",
"tool_input": {"name_or_id": "Drift Audit"}},
env)
assert _sent(tmp) == ("", None)
def test_the_hook_runs_after_every_record_getter_and_search_and_nothing_else():
hooks = json.loads((ROOT / "plugin" / "hooks" / "hooks.json").read_text())["hooks"]["PostToolUse"]
matchers = [e["matcher"] for e in hooks
if any("grimoire_record_read.sh" in h["command"] for h in e["hooks"])]
assert len(matchers) == 1
runs = re.compile(f"^(?:{matchers[0]})$")
for tool in ("get_note", "get_task", "get_snippet", "get_lesson", "get_process", "search"):
assert runs.match(f"mcp__plugin_grimoire_grimoire__{tool}"), tool
for tool in ("get_rule", "search_rules", "update_task", "get_milestone"):
assert not runs.match(f"mcp__plugin_grimoire_grimoire__{tool}"), tool