test(8249): prompt filter, shared rule ledger and reply-shape guards run the hooks
CI & Build / Python lint (push) Successful in 2s
CI & Build / Plugin hooks (push) Successful in 13s
CI & Build / TypeScript typecheck (push) Successful in 32s
CI & Build / Python tests (push) Successful in 2m19s
CI & Build / integration (push) Successful in 2m33s
CI & Build / Build & push image (push) Successful in 18s

- test_prompt_boundary_filter: the prompt hook runs on a client-written turn
  and sends nothing, and on a typed one and asks; was "the filter call sits
  above the curl line" in the source.
- test_rule_usage_wiring: the write arm runs and is told it named rule 33,
  then the tool arm in the same session must send 33 as already named; was
  three string searches of each script for the ledger's path.
- test_reply_shape_turn: the hook half of the shape-ledger contract is the
  runtime test already there; the remaining test checks the route half from
  the route's AST.
- test_contract_around_the_change: its write-gate guard was a copy of
  test_hook_never_returns_a_permission_decision, which now checks the same
  two things.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 17:37:24 -05:00
co-authored by Claude Opus 5.5
parent 0681534257
commit ea30e0bd75
4 changed files with 54 additions and 40 deletions
-14
View File
@@ -214,20 +214,6 @@ def test_a_missing_old_text_says_nothing(repo):
# ── The hook that carries it ────────────────────────────────────────────── # ── The hook that carries it ──────────────────────────────────────────────
def test_the_write_hook_still_cannot_stop_a_write():
"""THE RECORDED DECISION THIS ARM MUST NOT ERODE. The operator's position
is that a recall aid may not stand in the way of a write, and this arm is
a nudge like the others — it names files, it does not gate. Asserted here
as well as in test_write_path_trigger.py because this is the change most
likely to tempt someone into making it a gate: it reports something that
may already be broken.
"""
code = [ln for ln in HOOK.read_text().splitlines()
if not ln.lstrip().startswith("#")]
assert not any("permissionDecision" in ln for ln in code)
assert not any("grimoire_json_deny" in ln for ln in code)
def test_the_hook_asks_the_contract_question_first(): def test_the_hook_asks_the_contract_question_first():
"""Order is the strength of the claim: this arm says something may already """Order is the strength of the claim: this arm says something may already
be BROKEN, the local arm says a copy exists, the recorded arms say be BROKEN, the local arm says a copy exists, the recorded arms say
+19 -9
View File
@@ -110,13 +110,23 @@ def test_the_filter_is_a_prefix_test_not_a_substring_test() -> None:
assert not _skip("what is " + tag + "?") assert not _skip("what is " + tag + "?")
def test_the_hook_consults_the_filter_before_spending_a_request() -> None: @pytest.mark.parametrize("prompt,asks", [(SYNTHETIC[0], False), (REAL[0], True)],
"""Placement, not merely presence. ids=["client-written", "typed"])
def test_the_hook_spends_no_request_on_a_client_written_turn(tmp_path, prompt, asks) -> None:
"""Placement, not merely presence: the point is that no retrieval row is
logged for a turn nobody typed, so the hook must not reach the server at all."""
import json
The guard is worthless below the `curl`: the point is that no row is from tests.helpers import http_sink, need_tools
logged, so it has to sit ahead of the request that would log one.
""" need_tools("bash", "curl", "awk")
body = HOOK.read_text() with http_sink(by_path={}) as (port, seen):
assert "grimoire_skip_prompt" in body, "the hook does not consult the filter" out = subprocess.run(
assert body.index("grimoire_skip_prompt") < body.index("curl -fsS"), \ ["bash", str(HOOK)],
"the filter runs after the request it exists to prevent" input=json.dumps({"session_id": "s-filter", "cwd": str(tmp_path), "prompt": prompt}),
capture_output=True, text=True, timeout=30,
env={"PATH": os.environ["PATH"], "GRIMOIRE_URL": f"http://127.0.0.1:{port}",
"GRIMOIRE_TOKEN": "t", "TMPDIR": str(tmp_path), "HOME": str(tmp_path)},
)
assert out.returncode == 0, out.stderr
assert any(e["_path"] == "/api/plugin/retrieve" for e in seen) is asks
+7 -7
View File
@@ -131,12 +131,12 @@ def test_the_core_goes_whole_once_then_as_a_reminder_then_whole_after_a_compacti
assert "Every reply" in json.loads(out)["hookSpecificOutput"]["additionalContext"] assert "Every reply" in json.loads(out)["hookSpecificOutput"]["additionalContext"]
def test_the_hook_and_the_route_agree_on_the_ledgers_names(): def test_the_route_reads_the_shape_ledger_the_hook_sends_and_returns_its_keys():
"""A contract across the seam: a renamed field fails silently.""" """A contract across the seam: a renamed field fails silently. The hook
half (it sends `shapes_seen` and keeps the `shape_keys` it is given) is
the runtime test above; this is the route half."""
from grimoire.routes import plugin as routes from grimoire.routes import plugin as routes
from tests.helpers import route_query_args
hook = HOOK.read_text() assert "shapes_seen" in route_query_args(routes.autoinject_retrieve)
assert "shapes_seen" in hook and "'.shape_keys'" in hook assert '"shape_keys"' in inspect.getsource(routes.autoinject_retrieve)
assert "grimoire_shapes_file" in hook and "grimoire_shapes_append" in hook
route = inspect.getsource(routes.autoinject_retrieve)
assert 'request.args.get("shapes_seen")' in route and '"shape_keys"' in route
+28 -10
View File
@@ -670,21 +670,39 @@ async def test_a_long_command_is_bounded_before_it_reaches_the_ranker():
assert sent.startswith("git tag v1"), "the head of the command is the signal" assert sent.startswith("git tag v1"), "the head of the command is the signal"
def test_the_two_pre_tool_arms_share_one_session_rule_ledger(): def test_the_two_pre_tool_arms_share_one_session_rule_ledger(tmp_path):
"""The integration point most worth guarding. """The integration point most worth guarding.
Two ledgers would mean a rule named by the write arm gets re-offered by the Two ledgers would mean a rule named by the write arm gets re-offered by the
tool arm — and the hint that fires most often is exactly the one that must tool arm, and the hint that fires most often is exactly the one that must
not repeat itself. Asserted on the FILENAME both scripts build, because not repeat itself. So the write arm runs and is told it named rule 33; the
that is the shared thing; a copy of the path in each is how they drift. tool arm, next in the same session, must send rule 33 as already named.
""" """
prior = Path("plugin/hooks/grimoire_prior_art.sh").read_text() import json
tool = Path("plugin/hooks/grimoire_tool_rules.sh").read_text() import os
import subprocess
for src, name in ((prior, "grimoire_prior_art.sh"), (tool, "grimoire_tool_rules.sh")): from tests.helpers import http_sink, need_tools
assert '"${TMPDIR:-/tmp}/grimoire-priorart"' in src, f"{name}: state dir moved"
assert '.rules.ids' in src, f"{name}: rules ledger filename moved" need_tools("bash", "curl", "awk")
assert "exclude_rule_ids" in src, f"{name}: does not send the exclusion" hooks = Path("plugin/hooks")
replies = {"/api/plugin/prior-art": b'{"context":"> rule 33","note_ids":[],"rule_ids":[33]}',
"/api/plugin/tool-rules": b'{"context":"","rule_ids":[]}'}
with http_sink(by_path=replies) as (port, seen):
env = {"PATH": os.environ["PATH"], "GRIMOIRE_URL": f"http://127.0.0.1:{port}",
"GRIMOIRE_TOKEN": "t", "TMPDIR": str(tmp_path), "HOME": str(tmp_path)}
for script, event in (
("grimoire_prior_art.sh", {"tool_name": "Write", "tool_input": {
"file_path": str(tmp_path / "a.py"), "content": "def debounce(fn):\n return fn"}}),
("grimoire_tool_rules.sh", {"tool_name": "Bash", "tool_input": {"command": "git push"}}),
):
out = subprocess.run(["bash", str(hooks / script)],
input=json.dumps({"session_id": "s-shared", "cwd": str(tmp_path),
**event}),
capture_output=True, text=True, env=env, timeout=30)
assert out.returncode == 0, out.stderr
tool_ask = [e for e in seen if e["_path"] == "/api/plugin/tool-rules"]
assert tool_ask and tool_ask[0]["exclude_rule_ids"] == ["33"]
def test_the_tool_arm_is_registered_on_bash(): def test_the_tool_arm_is_registered_on_bash():