Files
FabledGrimoire/tests/test_rule_moment_signal_hooks.py
T
bvandeusenandClaude Opus 5.5 b4bdd7798c
CI & Build / Python lint (push) Successful in 2s
CI & Build / Plugin hooks (push) Successful in 16s
CI & Build / TypeScript typecheck (push) Successful in 36s
CI & Build / Python tests (push) Failing after 1m48s
CI & Build / integration (push) Successful in 2m26s
CI & Build / Build & push image (push) Skipped
feat(7733): adherence is measured per rule and per moment, and shown
A rule is in force at a moment of a session when it was delivered there.
Each (session, rule, place) now carries at most one verdict (rule_adherence,
migration 0134): followed, departed, broken or not applicable, with where it
came from:

- an enforce rule refusing the act: followed, recorded by the moment arm, and
  never replaced;
- the session's own rule_outcome (applied, departed) and rule_misfired calls,
  which the plugin's outcome hook now also posts with the session id
  (/api/plugin/rule-verdict) onto the delivery being answered;
- a transcript review: adherence_to_judge lists what has no verdict yet, by
  session, and judge_adherence records verdicts with their evidence.

rule_adherence (MCP) and /api/retrieval/adherence give the readout: sessions
in force, opened and each verdict per rule and per place. Settings > Moments
shows it in a "Were they followed" panel, beside each moment's delivered and
opened counts, and per rule inside each moment.

Verdicts are pruned with their deliveries and left out of backups like them.
Plugin 2026.11.23.2227.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 17:28:23 -05:00

257 lines
11 KiB
Python

"""The plugin half of the rule↔moment signal, run for real.
`grimoire_moment.sh` writes every call to an acts ledger; `grimoire_record_opened.sh`
sends the last few minutes of it with the rule a session opened; the server
answers with a proposal line when one is due. These drive both scripts through
bash with a stand-in `curl` on PATH that records what it was sent, so what is
pinned is what the scripts actually do — the window, the body's shape, the
reduction of a large event, the line coming back out — not what their source
looks like. `grimoire_session_end.sh` removing the session's files is pinned the
same way.
"""
from __future__ import annotations
import inspect
import json
import os
import subprocess
import time
from pathlib import Path
import pytest
from grimoire.routes import plugin as plugin_routes
from grimoire.services import rule_moment_judgments as judgments_svc
from tests.helpers import need_tools
ROOT = Path(__file__).resolve().parents[1]
HOOKS = ROOT / "plugin" / "hooks"
FAKE_CURL = """#!/usr/bin/env bash
data=""
for a in "$@"; do
if [ "$a" = "@-" ]; then data=$(cat); fi
done
printf '%s\\n' "$*" >> "$FAKE_CURL_LOG"
if [ -n "$data" ]; then printf '%s' "$data" > "$FAKE_CURL_BODY"; fi
if [ -n "${FAKE_CURL_REPLY:-}" ]; then printf '%s' "$FAKE_CURL_REPLY"; else printf '{}'; fi
"""
@pytest.fixture
def sandbox(tmp_path):
need_tools("bash", "awk", "paste", "tail")
bin_dir = tmp_path / "bin"
bin_dir.mkdir()
curl = bin_dir / "curl"
curl.write_text(FAKE_CURL)
curl.chmod(0o755)
env = {
"PATH": f"{bin_dir}{os.pathsep}{os.environ['PATH']}",
"HOME": str(tmp_path), "TMPDIR": str(tmp_path),
"GRIMOIRE_URL": "http://grimoire.invalid", "GRIMOIRE_TOKEN": "t",
"FAKE_CURL_LOG": str(tmp_path / "curl.log"),
"FAKE_CURL_BODY": str(tmp_path / "curl.body"),
"CLAUDE_PROJECT_DIR": str(tmp_path),
}
return tmp_path, env
def _run(script: str, event: dict, env: dict, **extra) -> subprocess.CompletedProcess:
out = subprocess.run(
["bash", str(HOOKS / script)], input=json.dumps(event),
capture_output=True, text=True, env={**env, **extra}, timeout=30,
)
assert out.returncode == 0, out.stderr
return out
def _acts(tmp: Path, sid: str = "s1") -> list[tuple[int, dict]]:
path = tmp / "grimoire-moment" / f"{sid}.acts"
rows = []
for line in path.read_text().splitlines():
stamp, _, act = line.partition("\t")
rows.append((int(stamp), json.loads(act)))
return rows
def test_every_call_is_written_to_the_acts_ledger_grimoires_own_included(sandbox):
tmp, env = sandbox
push = {"session_id": "s1", "tool_name": "Bash",
"tool_input": {"command": "git push origin dev"}}
close = {"session_id": "s1", "tool_name": "mcp__plugin_grimoire_grimoire__update_task",
"tool_input": {"task_id": 4, "status": "done"}}
_run("grimoire_moment.sh", push, env, FAKE_CURL_REPLY='{"tools":[]}')
_run("grimoire_moment.sh", close, env)
acts = _acts(tmp)
assert [a for _, a in acts] == [push, close]
assert all(abs(stamp - time.time()) < 60 for stamp, _ in acts)
def test_the_open_itself_is_not_an_act(sandbox):
tmp, env = sandbox
_run("grimoire_moment.sh", {"session_id": "s1", "tool_name": "Bash",
"tool_input": {"command": "ls"}}, env,
FAKE_CURL_REPLY='{"tools":[]}')
_run("grimoire_moment.sh", {"session_id": "s1", "tool_name": "mcp__grimoire__get_rule",
"tool_input": {"rule_id": 11}}, env)
assert [a["tool_name"] for _, a in _acts(tmp)] == ["Bash"]
def test_a_large_event_is_kept_as_its_tool_name(sandbox):
tmp, env = sandbox
big = {"session_id": "s1", "tool_name": "Write",
"tool_input": {"file_path": "x", "content": "y" * 40000}}
_run("grimoire_moment.sh", big, env, FAKE_CURL_REPLY='{"tools":[]}')
[(_, act)] = _acts(tmp)
assert act == {"tool_name": "Write", "tool_input": {}}
def test_an_open_sends_the_window_and_prints_the_answer(sandbox):
tmp, env = sandbox
ledger = tmp / "grimoire-moment"
ledger.mkdir()
now = int(time.time())
old = {"tool_name": "Bash", "tool_input": {"command": "make old"}}
recent = {"tool_name": "Bash", "tool_input": {"command": "git push"}}
(ledger / "s1.acts").write_text(
f"{now - 1000}\t{json.dumps(old)}\n{now - 5}\t{json.dumps(recent)}\n"
)
line = "> Rule #11 has been opened just after `work.deliver` in 3 distinct sessions"
out = _run("grimoire_record_opened.sh",
{"session_id": "s1", "tool_name": "mcp__grimoire__get_rule",
"tool_input": {"rule_id": 11}}, env,
FAKE_CURL_REPLY=json.dumps({"moments": ["work.deliver"], "context": line}))
body = json.loads((tmp / "curl.body").read_text())
assert body == {"rule_id": 11, "session_id": "s1", "acts": [recent]}
assert "/api/plugin/rule-opened" in (tmp / "curl.log").read_text()
printed = json.loads(out.stdout)["hookSpecificOutput"]
assert printed["hookEventName"] == "PostToolUse"
assert printed["additionalContext"] == line
# The open is still recorded in its own ledger, as before.
assert (tmp / "grimoire-priorart" / "s1.opened.ids").read_text().startswith("11\t")
def test_every_request_names_the_plugin_version_that_sent_it(sandbox):
# The server counts a delivery toward a rule's open rate only when its
# plugin sent its version, since that is the plugin that reports opens.
tmp, env = sandbox
version = json.loads((ROOT / "plugin" / ".claude-plugin" / "plugin.json").read_text())["version"]
_run("grimoire_moment.sh", {"session_id": "s1", "tool_name": "Bash",
"tool_input": {"command": "ls"}}, env,
FAKE_CURL_REPLY='{"tools":["bash"]}')
_run("grimoire_record_opened.sh",
{"session_id": "s1", "tool_name": "mcp__grimoire__get_rule",
"tool_input": {"rule_id": 11}}, env)
calls = (tmp / "curl.log").read_text().splitlines()
assert len(calls) >= 2
assert all(f"X-Grimoire-Plugin: {version}" in call for call in calls), calls
def test_the_window_is_the_last_three_acts(sandbox):
tmp, env = sandbox
ledger = tmp / "grimoire-moment"
ledger.mkdir()
now = int(time.time())
acts = [{"tool_name": "Bash", "tool_input": {"command": f"step {i}"}} for i in range(5)]
(ledger / "s1.acts").write_text(
"".join(f"{now - 10 + i}\t{json.dumps(a)}\n" for i, a in enumerate(acts))
)
_run("grimoire_record_opened.sh",
{"session_id": "s1", "tool_name": "mcp__grimoire__get_rule",
"tool_input": {"rule_id": 11}}, env)
body = json.loads((tmp / "curl.body").read_text())
assert body["acts"] == acts[-3:]
def test_an_open_of_a_rule_a_hook_just_named_is_not_reported(sandbox):
"""A ranked arm or a moment line explains that open; the moment beside it
does not, so it is no evidence for a mount."""
tmp, env = sandbox
ledger = tmp / "grimoire-moment"
ledger.mkdir()
now = int(time.time())
(ledger / "s1.acts").write_text(
f"{now - 5}\t{json.dumps({'tool_name': 'Bash', 'tool_input': {'command': 'git push'}})}\n"
)
named = tmp / "grimoire-priorart"
named.mkdir()
(named / "s1.rules.ids").write_text(f"7\t{now - 30}\n11\t{now - 20}\n")
out = _run("grimoire_record_opened.sh",
{"session_id": "s1", "tool_name": "mcp__grimoire__get_rule",
"tool_input": {"rule_id": 11}}, env)
assert out.stdout == ""
# The open is reported for the delivery record, with no acts to count.
assert json.loads((tmp / "curl.body").read_text()) == {
"rule_id": 11, "session_id": "s1", "acts": []}
# The open itself is still recorded.
assert (named / "s1.opened.ids").read_text().startswith("11\t")
# A rule no hook named is still reported.
_run("grimoire_record_opened.sh",
{"session_id": "s1", "tool_name": "mcp__grimoire__get_rule",
"tool_input": {"rule_id": 1}}, env)
assert json.loads((tmp / "curl.body").read_text())["rule_id"] == 1
def test_an_open_with_nothing_in_the_window_sends_only_the_open(sandbox):
tmp, env = sandbox
ledger = tmp / "grimoire-moment"
ledger.mkdir()
(ledger / "s1.acts").write_text(
f"{int(time.time()) - 1000}\t{json.dumps({'tool_name': 'Bash', 'tool_input': {}})}\n"
)
out = _run("grimoire_record_opened.sh",
{"session_id": "s1", "tool_name": "mcp__grimoire__get_rule",
"tool_input": {"rule_id": 11}}, env, FAKE_CURL_REPLY='{"moments":[],"context":""}')
assert out.stdout == ""
assert json.loads((tmp / "curl.body").read_text())["acts"] == []
def test_session_end_removes_the_sessions_moment_files_and_no_others(sandbox):
tmp, env = sandbox
ledger = tmp / "grimoire-moment"
ledger.mkdir()
for name in ("s1.acts", "s1.tools", "s2.acts"):
(ledger / name).write_text("x")
_run("grimoire_session_end.sh", {"session_id": "s1", "reason": "clear"}, env)
assert sorted(p.name for p in ledger.iterdir()) == ["s2.acts"]
def test_the_hook_and_the_route_agree_on_the_body():
"""A contract across the shell/Python seam: a renamed field fails silently."""
src = (HOOKS / "grimoire_record_opened.sh").read_text()
route = inspect.getsource(plugin_routes.rule_opened)
assert "/api/plugin/rule-opened" in src
for field in ("rule_id", "session_id", "acts"):
assert f'"{field}"' in src and f'"{field}"' in route
assert "'.context'" in src
assert '"context"' in inspect.getsource(judgments_svc.opened_after)
def test_an_outcome_is_sent_as_a_verdict_with_the_session(sandbox):
tmp, env = sandbox
_run("grimoire_record_outcome.sh",
{"session_id": "s1", "tool_name": "mcp__grimoire__rule_outcome",
"tool_input": {"rule_id": 11, "outcome": "departed", "why": 'the operator said "ship"'}},
env)
assert json.loads((tmp / "curl.body").read_text()) == {
"rule_id": 11, "session_id": "s1", "outcome": "departed", "moment": "",
"why": 'the operator said "ship"',
}
assert "/api/plugin/rule-verdict" in (tmp / "curl.log").read_text()
assert (tmp / "grimoire-priorart" / "s1.acted.ids").exists()
def test_a_misfire_is_sent_as_a_verdict_at_the_moment_it_names(sandbox):
tmp, env = sandbox
_run("grimoire_record_outcome.sh",
{"session_id": "s1", "tool_name": "mcp__grimoire__rule_misfired",
"tool_input": {"rule_id": 4, "moment": "work.deliver", "why": "a redeploy, not a release"}},
env)
assert json.loads((tmp / "curl.body").read_text()) == {
"rule_id": 4, "session_id": "s1", "outcome": "", "moment": "work.deliver",
"why": "a redeploy, not a release",
}