Reply shape survives a compaction (#7716); test review pass (#8249); detector fix (#8292) #239

Merged
bvandeusen merged 27 commits from dev into main 2026-12-02 13:09:44 -05:00
4 changed files with 54 additions and 40 deletions
Showing only changes of commit ea30e0bd75 - Show all commits
-14
View File
@@ -214,20 +214,6 @@ def test_a_missing_old_text_says_nothing(repo):
# ── 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():
"""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
+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 + "?")
def test_the_hook_consults_the_filter_before_spending_a_request() -> None:
"""Placement, not merely presence.
@pytest.mark.parametrize("prompt,asks", [(SYNTHETIC[0], False), (REAL[0], True)],
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
logged, so it has to sit ahead of the request that would log one.
"""
body = HOOK.read_text()
assert "grimoire_skip_prompt" in body, "the hook does not consult the filter"
assert body.index("grimoire_skip_prompt") < body.index("curl -fsS"), \
"the filter runs after the request it exists to prevent"
from tests.helpers import http_sink, need_tools
need_tools("bash", "curl", "awk")
with http_sink(by_path={}) as (port, seen):
out = subprocess.run(
["bash", str(HOOK)],
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"]
def test_the_hook_and_the_route_agree_on_the_ledgers_names():
"""A contract across the seam: a renamed field fails silently."""
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. 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 tests.helpers import route_query_args
hook = HOOK.read_text()
assert "shapes_seen" in hook and "'.shape_keys'" in hook
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
assert "shapes_seen" in route_query_args(routes.autoinject_retrieve)
assert '"shape_keys"' in inspect.getsource(routes.autoinject_retrieve)
+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"
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.
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
not repeat itself. Asserted on the FILENAME both scripts build, because
that is the shared thing; a copy of the path in each is how they drift.
tool arm, and the hint that fires most often is exactly the one that must
not repeat itself. So the write arm runs and is told it named rule 33; the
tool arm, next in the same session, must send rule 33 as already named.
"""
prior = Path("plugin/hooks/grimoire_prior_art.sh").read_text()
tool = Path("plugin/hooks/grimoire_tool_rules.sh").read_text()
import json
import os
import subprocess
for src, name in ((prior, "grimoire_prior_art.sh"), (tool, "grimoire_tool_rules.sh")):
assert '"${TMPDIR:-/tmp}/grimoire-priorart"' in src, f"{name}: state dir moved"
assert '.rules.ids' in src, f"{name}: rules ledger filename moved"
assert "exclude_rule_ids" in src, f"{name}: does not send the exclusion"
from tests.helpers import http_sink, need_tools
need_tools("bash", "curl", "awk")
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():