CI & Build / TypeScript typecheck (push) Successful in 41s
CI & Build / Plugin hooks (push) Successful in 17s
CI & Build / Python lint (push) Successful in 3s
CI & Build / Python tests (push) Failing after 1m41s
CI & Build / integration (push) Successful in 1m59s
CI & Build / Build & push image (push) Skipped
Every rule arm now ranks what it delivers with one function (services/rule_order.py): enforcement, then salience, then how narrowly the rule was reached, then first mention before reminder, then the rule's own open rate at that moment, then similarity. A crowded moment drops the same rules whichever arm brought them, and a rule that holds is never cut for a line beside it. The open rate comes from a new table, rule_deliveries (migration 0129): one row per rule line a plugin session is shown, full or repeat, with the open stamped on the latest delivery it followed. The plugin routes bind the session per request; the prompt, pre-tool and write-path hooks now send it, and the opened hook reports every open, with its acts only when no hook had just named the rule. Rows are pruned daily after 180 days. Also fixes the TypeScript check: the store's list row now carries the new enforcement field. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
214 lines
8.6 KiB
Python
214 lines
8.6 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_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)
|