test(8249): moment and checkpoint hook guards run the hooks

- test_moment_delivery: the route/hook name check runs the hook with the
  rule, opened and shape ledgers filled and checks every key it sends is read
  by the route; "leaves Grimoire's own tools alone" runs it with a listed
  Grimoire tool and sees no ask.
- test_gate_stop_hooks: the action hook run twice on one checkpoint rule
  holds once, and books the rule on the session ledger after the stop. This
  replaces two line-window checks in test_pre_act_checkpoint.
- The write hook never stopping a write: the two string guards (one per test
  file) are one, which now also forbids the shared deny helper, the call the
  old permissionDecision check could not see.
- route_query_args (tests/helpers.py) reads the query keys a route takes from
  its AST, shared by both route/hook contract tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 17:29:56 -05:00
co-authored by Claude Opus 5.5
parent 4321b05387
commit e92f129d4f
5 changed files with 81 additions and 100 deletions
+35 -25
View File
@@ -30,7 +30,7 @@ from grimoire.services import moment_actions
from grimoire.services import moment_delivery as md
from grimoire.services import retrieval_pipeline as rp
from grimoire.services import rule_usage, rulebooks
from tests.helpers import fake_rule, http_sink, need_tools
from tests.helpers import fake_rule, http_sink, need_tools, route_query_args
# Bound before conftest's autouse stub replaces the module attribute.
_REAL_DELIVER = md.deliver_for_act
@@ -274,30 +274,6 @@ def test_the_hook_is_registered_on_every_tool():
assert any("grimoire_moment.sh" in c for c in entries.get("*", []))
def test_the_hook_and_the_routes_agree_on_every_name():
"""Parity across the shell/Python seam: a renamed arg fails silently."""
src = HOOK.read_text()
assert "/api/plugin/moment-tools" in src and "/api/plugin/moment" in src
assert "exclude_rule_ids" in src and "grimoire_held_query" in src
assert "grimoire_rules_live" in src and "grimoire_rules_append" in src
# The SHARED ledger, not one of its own.
assert '"${TMPDIR:-/tmp}/grimoire-priorart"' in src and ".rules.ids" in src
for field in (".tools", ".rule_ids", ".context", ".shape_keys"):
assert f"'{field}'" in src
assert "shapes_seen" in src and "grimoire_shapes_append" in src
from grimoire.routes import plugin as routes
route = inspect.getsource(routes.moment)
for name in ("exclude_rule_ids", "held_rule_ids", "tool_name", "tool_input",
"shapes_seen", "shape_keys"):
assert name in route
def test_the_hook_leaves_grimoires_own_tools_to_their_responses():
assert "mcp__*grimoire*__*) exit 0" in HOOK.read_text()
def test_the_hook_holds_only_behind_the_session_guard_and_refuses_only_on_enforce():
"""Structural, and able to fail: every deny the hook emits is either the
`enforce` branch or sits behind `grimoire_checkpoint_allowed`. A hold
@@ -355,6 +331,40 @@ def test_an_unlisted_tool_never_leaves_the_machine(tmp_path):
assert [e["_path"] for e in seen] == ["/api/plugin/moment-tools"]
def test_grimoires_own_tools_are_left_to_their_responses(tmp_path):
"""A Grimoire tool attaches its own moment rules in its response, so the
hook never asks about one, even when the tool is listed."""
with http_sink(by_path={"/api/plugin/moment-tools": b'{"tools":["update_task"]}'}) \
as (port, seen):
assert _hook(tmp_path, port, tool="mcp__plugin_grimoire_grimoire__update_task") == ""
assert [e["_path"] for e in seen] == ["/api/plugin/moment-tools"]
def test_the_route_reads_every_name_the_hook_sends(tmp_path):
"""Parity across the shell/Python seam, a renamed key fails silently. The
hook runs with the shared rule ledger, the opened ledger and the shape
ledger all holding something; every query key it sends must be one the
route reads, and the body carries the event the route reads."""
from grimoire.routes import plugin as routes
state = tmp_path / "grimoire-priorart"
state.mkdir()
(state / "s-moment.opened.ids").write_text("7\n")
replies = dict(TOOLS)
replies["/api/plugin/moment"] = json.dumps(
{"context": "a rule", "rule_ids": [5], "moments": ["work.deliver"],
"shape_keys": ["asks"]}).encode()
with http_sink(by_path=replies) as (port, seen):
_hook(tmp_path, port)
_hook(tmp_path, port)
last = [e for e in seen if e["_path"] == "/api/plugin/moment"][-1]
sent = {k for k in last if not k.startswith("_")}
assert {"exclude_rule_ids", "held_rule_ids", "shapes_seen"} <= sent, sent
read = route_query_args(routes.moment, routes._project_scope)
assert sent - {"session_id"} <= read, f"the route ignores {sorted(sent - read)}"
assert {"tool_name", "tool_input"} <= set(json.loads(last["_body"]))
def test_an_install_with_nothing_mounted_sends_one_request_per_window(tmp_path):
with http_sink(by_path={"/api/plugin/moment-tools": b'{"tools":[]}'}) as (port, seen):
for _ in range(3):