CI & Build / TypeScript typecheck (push) Successful in 38s
CI & Build / Plugin hooks (push) Successful in 14s
CI & Build / Python lint (push) Successful in 3s
CI & Build / Python tests (push) Failing after 1m45s
CI & Build / integration (push) Successful in 2m10s
CI & Build / Build & push image (push) Skipped
- A rule set to enforce, reached by a mount or a trigger, refuses the tool call in its own words every time (`act_decision`, `/moment` returns `decision`, grimoire_moment.sh denies). Opening it does not lift it. - A rule set to hold denies the call once until read, under the act checkpoint's per-rule and per-session guard; file writes are never held. - The reply hold, the reply backstop and the act checkpoint hold only on rules declared hold or enforce (`rule_order.holds`). An inform rule is a line beside the work. - The prompt arm leaves rules that arrive at an act (mounted on a non-reply moment, or with a tool/path/command trigger) to that act. - The end-of-turn shape check no longer blocks: its note is kept for the next prompt, carried as `carry` into the one fitted payload, and the stop after that prompt records whether the lines were judged (noted / judged_after_note / left_after_note). - Rule editor and list copy say what hold and enforce now do. Plugin 2026.11.23.0005. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
240 lines
10 KiB
Python
240 lines
10 KiB
Python
"""The end-of-turn shape check: the write hooks note what each
|
|
write defined, the Stop hook asks the instance which of those nobody has
|
|
judged, and the answer rides the next prompt rather than holding the reply.
|
|
|
|
Runs the real shell against the shared HTTP sink. What it pins: the write
|
|
hooks' ledger (a Write of a new file adds a `file` line; a skipped path adds
|
|
nothing); silence with nothing written; the Stop hook never blocks; a note
|
|
only when the instance returned one, carried by the next prompt and then
|
|
gone; the stop after that prompt records the answer; a note no prompt carried
|
|
is asked again with the next turn's writes; another hook's block loop left
|
|
alone; the ledger kept when the instance could not be reached.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import shutil
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from tests.helpers import http_sink
|
|
|
|
HOOKS = Path(__file__).resolve().parents[1] / "plugin" / "hooks"
|
|
CHECK = HOOKS / "grimoire_shape_check.sh"
|
|
PRIOR_ART = HOOKS / "grimoire_prior_art.sh"
|
|
PROMPT = HOOKS / "grimoire_autoinject.sh"
|
|
NOTE = "SERVER NOTE: judge what you wrote"
|
|
|
|
|
|
def _env(tmp_path, url="http://127.0.0.1:9"):
|
|
for tool in ("curl", "bash", "git"):
|
|
if shutil.which(tool) is None:
|
|
pytest.skip(f"hook runtime tool {tool!r} not installed")
|
|
return {"PATH": os.environ["PATH"], "GRIMOIRE_URL": url, "GRIMOIRE_TOKEN": "t",
|
|
"TMPDIR": str(tmp_path), "HOME": str(tmp_path)}
|
|
|
|
|
|
def _ledger(tmp_path, session="s1"):
|
|
return tmp_path / "grimoire-priorart" / f"{session}.written.ids"
|
|
|
|
|
|
def _write_ledger(tmp_path, lines, session="s1"):
|
|
path = _ledger(tmp_path, session)
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text("".join(f"{line}\n" for line in lines))
|
|
return path
|
|
|
|
|
|
def _stop(env, cwd, active=False, session="s1"):
|
|
out = subprocess.run(
|
|
["bash", str(CHECK)],
|
|
input=json.dumps({"session_id": session, "transcript_path": "/nonexistent",
|
|
"cwd": str(cwd), "hook_event_name": "Stop",
|
|
"stop_hook_active": active}),
|
|
capture_output=True, text=True, env=env, timeout=30,
|
|
)
|
|
assert out.returncode == 0, out.stderr
|
|
return out.stdout.strip()
|
|
|
|
|
|
def _repo(tmp_path):
|
|
repo = tmp_path / "repo"
|
|
repo.mkdir()
|
|
subprocess.run(["git", "init", "-q", str(repo)], check=True)
|
|
subprocess.run(["git", "-C", str(repo), "remote", "add", "origin",
|
|
"https://git.example.com/alice/widget.git"], check=True)
|
|
return repo
|
|
|
|
|
|
# ── the write hooks keep the ledger ───────────────────────────────────────
|
|
|
|
def _pre_write(env, repo, rel, content, tool="Write"):
|
|
field = "content" if tool == "Write" else "new_string"
|
|
out = subprocess.run(
|
|
["bash", str(PRIOR_ART)],
|
|
input=json.dumps({"session_id": "s1", "cwd": str(repo), "tool_name": tool,
|
|
"tool_input": {"file_path": str(repo / rel), field: content}}),
|
|
capture_output=True, text=True, env=env, timeout=30,
|
|
)
|
|
assert out.returncode == 0, out.stderr
|
|
|
|
|
|
def test_a_write_notes_what_it_defined_and_a_new_file_is_a_candidate(tmp_path):
|
|
with http_sink(b'{"note_ids":[]}') as (port, _seen):
|
|
env = _env(tmp_path, f"http://127.0.0.1:{port}")
|
|
repo = _repo(tmp_path)
|
|
(repo / "web").mkdir()
|
|
_pre_write(env, repo, "web/StatusChip.svelte",
|
|
"<script>\nfunction tone(s) {\n return s;\n}\n</script>\n"
|
|
"<style>\n.chip {\n color: red;\n}\n</style>\n")
|
|
lines = _ledger(tmp_path).read_text().splitlines()
|
|
assert "web/StatusChip.svelte\tfile\tStatusChip" in lines
|
|
assert "web/StatusChip.svelte\tsym\ttone" in lines
|
|
assert "web/StatusChip.svelte\tcss\tchip" in lines
|
|
|
|
|
|
def test_an_edit_to_an_existing_file_adds_no_file_line(tmp_path):
|
|
with http_sink(b'{"note_ids":[]}') as (port, _seen):
|
|
env = _env(tmp_path, f"http://127.0.0.1:{port}")
|
|
repo = _repo(tmp_path)
|
|
(repo / "lib.py").write_text("x = 1\n")
|
|
_pre_write(env, repo, "lib.py", "def helper():\n return 1\n", tool="Edit")
|
|
lines = _ledger(tmp_path).read_text().splitlines()
|
|
assert lines == ["lib.py\tsym\thelper"]
|
|
|
|
|
|
def test_a_write_with_many_definitions_notes_every_one(tmp_path):
|
|
"""The ledger is not bounded by the twelve names the prior-art query sends:
|
|
a new test file with twenty functions is asked about in full."""
|
|
with http_sink(b'{"note_ids":[]}') as (port, _seen):
|
|
env = _env(tmp_path, f"http://127.0.0.1:{port}")
|
|
repo = _repo(tmp_path)
|
|
body = "".join(f"def test_case_{i:02d}():\n assert {i}\n\n" for i in range(20))
|
|
_pre_write(env, repo, "test_many.py", body)
|
|
lines = _ledger(tmp_path).read_text().splitlines()
|
|
assert len(lines) == 20
|
|
assert "test_many.py\tsym\ttest_case_19" in lines
|
|
|
|
|
|
def test_a_skipped_path_notes_nothing(tmp_path):
|
|
with http_sink(b'{"note_ids":[]}') as (port, _seen):
|
|
env = _env(tmp_path, f"http://127.0.0.1:{port}")
|
|
repo = _repo(tmp_path)
|
|
_pre_write(env, repo, "README.md", "# def helper():\n")
|
|
assert not _ledger(tmp_path).exists()
|
|
|
|
|
|
# ── the Stop hook asks ────────────────────────────────────────────────────
|
|
|
|
def test_nothing_written_is_silent_and_asks_nothing(tmp_path):
|
|
with http_sink(b'{"status":"ok","note":"x"}') as (port, seen):
|
|
env = _env(tmp_path, f"http://127.0.0.1:{port}")
|
|
assert _stop(env, _repo(tmp_path)) == ""
|
|
assert seen == []
|
|
|
|
|
|
def test_all_judged_passes_silently_and_empties_the_ledger(tmp_path):
|
|
with http_sink(b'{"status":"ok","outcome":"passed","unjudged":[]}') as (port, seen):
|
|
env = _env(tmp_path, f"http://127.0.0.1:{port}")
|
|
ledger = _write_ledger(tmp_path, ["a.py\tsym\thelper", "a.py\tsym\thelper"])
|
|
assert _stop(env, _repo(tmp_path)) == ""
|
|
assert seen[0]["phase"] == ["check"]
|
|
assert seen[0]["written"] == ["a.py\tsym\thelper"], "duplicates collapse"
|
|
assert seen[0]["repo"] == ["https://git.example.com/alice/widget.git"]
|
|
assert not ledger.exists()
|
|
|
|
|
|
def _note_file(tmp_path, session="s1"):
|
|
return tmp_path / "grimoire-shapecheck" / f"{session}.note"
|
|
|
|
|
|
def _asked_file(tmp_path, session="s1"):
|
|
return tmp_path / "grimoire-shapecheck" / f"{session}.asked"
|
|
|
|
|
|
def _prompt(env, cwd, text="carry on", session="s1"):
|
|
out = subprocess.run(
|
|
["bash", str(PROMPT)],
|
|
input=json.dumps({"session_id": session, "prompt": text, "cwd": str(cwd),
|
|
"transcript_path": "/nonexistent",
|
|
"hook_event_name": "UserPromptSubmit"}),
|
|
capture_output=True, text=True, env=env, timeout=30,
|
|
)
|
|
assert out.returncode == 0, out.stderr
|
|
return out.stdout.strip()
|
|
|
|
|
|
def test_unjudged_is_noted_for_the_next_prompt_and_never_holds_the_reply(tmp_path):
|
|
reply = json.dumps({"status": "ok", "outcome": "noted", "note": NOTE,
|
|
"context": "", "note_ids": []}).encode()
|
|
with http_sink(reply) as (port, seen):
|
|
env = _env(tmp_path, f"http://127.0.0.1:{port}")
|
|
repo = _repo(tmp_path)
|
|
ledger = _write_ledger(tmp_path, ["web/A.svelte\tfile\tA"])
|
|
assert _stop(env, repo) == "", "the reply goes out as written"
|
|
assert _note_file(tmp_path).read_text() == NOTE
|
|
assert _asked_file(tmp_path).read_text().splitlines() == ["web/A.svelte\tfile\tA"]
|
|
assert not ledger.exists()
|
|
|
|
# The next prompt carries the note, and it is gone once answered.
|
|
_prompt(env, repo)
|
|
assert seen[-1]["carry"] == [NOTE]
|
|
assert not _note_file(tmp_path).exists()
|
|
|
|
# The stop after it records whether the noted lines were judged.
|
|
assert _stop(env, repo) == ""
|
|
assert seen[-1]["phase"] == ["after"]
|
|
assert seen[-1]["written"] == ["web/A.svelte\tfile\tA"]
|
|
assert not _asked_file(tmp_path).exists()
|
|
before = len(seen)
|
|
assert _stop(env, repo) == ""
|
|
assert len(seen) == before, "asked once, recorded once"
|
|
|
|
|
|
def test_a_note_no_prompt_carried_is_asked_again_with_the_next_turns_writes(tmp_path):
|
|
reply = json.dumps({"status": "ok", "outcome": "noted", "note": NOTE}).encode()
|
|
with http_sink(reply) as (port, seen):
|
|
env = _env(tmp_path, f"http://127.0.0.1:{port}")
|
|
repo = _repo(tmp_path)
|
|
_write_ledger(tmp_path, ["a.py\tsym\tfirst"])
|
|
_stop(env, repo)
|
|
_write_ledger(tmp_path, ["b.py\tsym\tsecond"])
|
|
_stop(env, repo)
|
|
assert seen[-1]["phase"] == ["check"]
|
|
assert seen[-1]["written"] == ["b.py\tsym\tsecond\na.py\tsym\tfirst"]
|
|
assert [q["phase"] for q in seen] == [["check"], ["check"]], "no prompt read it: no answer to record"
|
|
|
|
|
|
def test_the_stop_hook_never_blocks(tmp_path):
|
|
"""Structural, and able to fail: no decision is ever printed."""
|
|
code = [ln for ln in CHECK.read_text().splitlines() if not ln.lstrip().startswith("#")]
|
|
assert not any('"decision"' in ln or "block" in ln for ln in code)
|
|
|
|
|
|
def test_no_note_without_one_from_the_instance(tmp_path):
|
|
with http_sink(b'{"status":"ok"}') as (port, _seen):
|
|
env = _env(tmp_path, f"http://127.0.0.1:{port}")
|
|
ledger = _write_ledger(tmp_path, ["a.py\tsym\thelper"])
|
|
assert _stop(env, _repo(tmp_path)) == ""
|
|
assert not _note_file(tmp_path).exists()
|
|
assert not ledger.exists()
|
|
|
|
|
|
def test_another_hooks_block_loop_is_left_alone(tmp_path):
|
|
with http_sink(b'{"status":"ok","note":"x"}') as (port, seen):
|
|
env = _env(tmp_path, f"http://127.0.0.1:{port}")
|
|
ledger = _write_ledger(tmp_path, ["a.py\tsym\thelper"])
|
|
assert _stop(env, _repo(tmp_path), active=True) == ""
|
|
assert seen == []
|
|
assert ledger.exists(), "this hook's own question is still owed"
|
|
|
|
|
|
def test_an_unreachable_instance_keeps_the_question_for_later(tmp_path):
|
|
env = _env(tmp_path) # port 9: nothing listens
|
|
ledger = _write_ledger(tmp_path, ["a.py\tsym\thelper"])
|
|
assert _stop(env, _repo(tmp_path)) == ""
|
|
assert ledger.exists()
|