Skip to content

Commit 0f7ea00

Browse files
cdeustclaude
andcommitted
fix(hooks): source the envelope-length constant and split test_entry.py under the craftsmanship cap (#608)
CI's craftsmanship gate on e527fce flagged two new violations neither of which existed at review time locally, because both were introduced by edits made after my last local run of check_craftsmanship.py: the ruff PLR2004 fix that named _MIN_ENVELOPE_LINES had no # source: comment, and ruff format's reflow of test_entry.py (plus the apply_patch end-to-end tests appended after that point) pushed the file to 302 lines, over the 300-line cap. Gives _MIN_ENVELOPE_LINES its source (zetetic-team-subagents hooks/lib/host_events.py, commit 4c2bc9b, PR #139, the same three-line envelope floor). Splits test_entry.py along its seam: PR 1's launcher parity and environment-resolution tests stay in test_entry.py (159 lines); the new apply_patch end-to-end tests move to tests_py/hooks/test_entry_apply_patch.py (165 lines), importing REPO_ROOT/_isolated_env from test_entry.py rather than duplicating them. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01Cz7A6goxVzfKL9NT7k4zDH
1 parent ff70044 commit 0f7ea00

3 files changed

Lines changed: 169 additions & 144 deletions

File tree

‎mcp_server/hooks/host_patch.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@
3737

3838
_ADD, _DELETE, _UPDATE = "Add File", "Delete File", "Update File"
3939
_MOVE_PREFIX = "*** Move to: "
40-
# An envelope needs at least its Begin/End markers plus one operation header.
40+
# source: zetetic-team-subagents hooks/lib/host_events.py, commit 4c2bc9b,
41+
# PR #139 -- the same three-line floor `_patch_events` checks there
42+
# (`len(lines) < 3`): an envelope needs at least its Begin/End markers
43+
# plus one operation header.
4144
_MIN_ENVELOPE_LINES = 3
4245

4346

‎tests_py/hooks/test_entry.py‎

Lines changed: 0 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -157,146 +157,3 @@ def test_pyproject_declares_the_console_script() -> None:
157157
data = tomllib.loads((REPO_ROOT / "pyproject.toml").read_text(encoding="utf-8"))
158158
scripts = data["project"]["scripts"]
159159
assert scripts["hypermnesia-mcp-hook"] == "mcp_server.hooks.entry:main"
160-
161-
162-
def _run_entry(
163-
module: str, event: str, env: dict[str, str]
164-
) -> subprocess.CompletedProcess[str]:
165-
return subprocess.run(
166-
[sys.executable, "-m", "mcp_server.hooks.entry", module],
167-
input=event,
168-
capture_output=True,
169-
text=True,
170-
env=env,
171-
cwd=REPO_ROOT,
172-
)
173-
174-
175-
def _update_patch_event(tmp_path: Path, old: str, new: str) -> str:
176-
target = tmp_path / "edited.py"
177-
target.write_text(old)
178-
old_line, new_line = old.rstrip("\n"), new.rstrip("\n")
179-
body = (
180-
f"*** Begin Patch\n*** Update File: edited.py\n@@\n"
181-
f"-{old_line}\n+{new_line}\n*** End Patch"
182-
)
183-
return json.dumps(
184-
{
185-
"hook_event_name": "PostToolUse",
186-
"cwd": str(tmp_path),
187-
"tool_name": "apply_patch",
188-
"tool_input": {"command": body},
189-
"tool_response": "",
190-
}
191-
)
192-
193-
194-
def _equivalent_edit_event(tmp_path: Path, old: str, new: str) -> str:
195-
target = tmp_path / "edited.py"
196-
return json.dumps(
197-
{
198-
"hook_event_name": "PostToolUse",
199-
"cwd": str(tmp_path),
200-
"tool_name": "Edit",
201-
"tool_input": {
202-
"file_path": str(target),
203-
"old_string": old,
204-
"new_string": new,
205-
},
206-
"tool_response": "",
207-
}
208-
)
209-
210-
211-
@pytest.mark.parametrize("module", ["preemptive_context", "pipeline_impact_bump"])
212-
def test_apply_patch_posttooluse_matches_equivalent_edit_stdout(
213-
module: str, tmp_path: Path
214-
) -> None:
215-
env = _isolated_env(tmp_path)
216-
patch_result = _run_entry(
217-
module, _update_patch_event(tmp_path, "before\n", "after\n"), env
218-
)
219-
edit_result = _run_entry(
220-
module, _equivalent_edit_event(tmp_path, "before\n", "after\n"), env
221-
)
222-
223-
assert patch_result.returncode == edit_result.returncode
224-
assert patch_result.stdout == edit_result.stdout
225-
226-
227-
_LONG_COMMENT_BLOCK = "\n".join(
228-
f"# reason line {n} for this design choice" for n in range(1, 9)
229-
)
230-
231-
232-
def _add_file_patch_event(tmp_path: Path, filename: str) -> str:
233-
lines = "\n".join(f"+{line}" for line in _LONG_COMMENT_BLOCK.splitlines())
234-
body = f"*** Begin Patch\n*** Add File: {filename}\n{lines}\n+x = 1\n*** End Patch"
235-
return json.dumps(
236-
{
237-
"hook_event_name": "PreToolUse",
238-
"cwd": str(tmp_path),
239-
"tool_name": "apply_patch",
240-
"tool_input": {"command": body},
241-
}
242-
)
243-
244-
245-
def _equivalent_write_event(tmp_path: Path, filename: str) -> str:
246-
content = _LONG_COMMENT_BLOCK + "\nx = 1\n"
247-
return json.dumps(
248-
{
249-
"hook_event_name": "PreToolUse",
250-
"cwd": str(tmp_path),
251-
"tool_name": "Write",
252-
"tool_input": {"file_path": str(tmp_path / filename), "content": content},
253-
}
254-
)
255-
256-
257-
def test_apply_patch_pretooluse_blocks_like_equivalent_write(tmp_path: Path) -> None:
258-
env = _isolated_env(tmp_path)
259-
patch_result = _run_entry(
260-
"decision_gate", _add_file_patch_event(tmp_path, "new_a.py"), env
261-
)
262-
write_result = _run_entry(
263-
"decision_gate", _equivalent_write_event(tmp_path, "new_b.py"), env
264-
)
265-
266-
assert patch_result.returncode == 2
267-
assert write_result.returncode == 2
268-
assert patch_result.returncode == write_result.returncode
269-
assert "[decision-gate] BLOCKED" in patch_result.stderr
270-
assert "[decision-gate] BLOCKED" in write_result.stderr
271-
272-
273-
def _malformed_patch_event(tmp_path: Path, hook_event_name: str) -> str:
274-
body = "*** Begin Patch\n*** Update File: missing.py\n@@\n-x\n+y\n*** End Patch"
275-
return json.dumps(
276-
{
277-
"hook_event_name": hook_event_name,
278-
"cwd": str(tmp_path),
279-
"tool_name": "apply_patch",
280-
"tool_input": {"command": body},
281-
}
282-
)
283-
284-
285-
def test_malformed_patch_on_pretooluse_exits_2(tmp_path: Path) -> None:
286-
env = _isolated_env(tmp_path)
287-
result = _run_entry(
288-
"decision_gate", _malformed_patch_event(tmp_path, "PreToolUse"), env
289-
)
290-
assert result.returncode == 2
291-
assert result.stdout == ""
292-
assert "decision_gate" in result.stderr
293-
294-
295-
def test_malformed_patch_on_posttooluse_exits_1_with_stderr(tmp_path: Path) -> None:
296-
env = _isolated_env(tmp_path)
297-
result = _run_entry(
298-
"post_tool_capture", _malformed_patch_event(tmp_path, "PostToolUse"), env
299-
)
300-
assert result.returncode == 1
301-
assert result.stdout == ""
302-
assert "post_tool_capture" in result.stderr
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
"""End-to-end tests for a Codex ``apply_patch`` event through the console
2+
entry point: it must reach each hook module in the same shape, and produce
3+
the same outcome, as the equivalent Claude Edit/Write event would.
4+
5+
Split out of ``test_entry.py`` (which covers PR 1's launcher parity and
6+
environment-resolution surface) along the seam between "does the entry
7+
point dispatch a benign Claude event correctly" and "does it normalize and
8+
dispatch a Codex ``apply_patch`` event correctly" -- two different
9+
questions about two different inputs, kept in one file only because they
10+
shared helpers before this split grew past the 300-line cap.
11+
"""
12+
13+
from __future__ import annotations
14+
15+
import json
16+
import subprocess
17+
import sys
18+
from pathlib import Path
19+
20+
import pytest
21+
22+
from tests_py.hooks.test_entry import REPO_ROOT, _isolated_env
23+
24+
25+
def _run_entry(
26+
module: str, event: str, env: dict[str, str]
27+
) -> subprocess.CompletedProcess[str]:
28+
return subprocess.run(
29+
[sys.executable, "-m", "mcp_server.hooks.entry", module],
30+
input=event,
31+
capture_output=True,
32+
text=True,
33+
env=env,
34+
cwd=REPO_ROOT,
35+
)
36+
37+
38+
def _update_patch_event(tmp_path: Path, old: str, new: str) -> str:
39+
target = tmp_path / "edited.py"
40+
target.write_text(old)
41+
old_line, new_line = old.rstrip("\n"), new.rstrip("\n")
42+
body = (
43+
f"*** Begin Patch\n*** Update File: edited.py\n@@\n"
44+
f"-{old_line}\n+{new_line}\n*** End Patch"
45+
)
46+
return json.dumps(
47+
{
48+
"hook_event_name": "PostToolUse",
49+
"cwd": str(tmp_path),
50+
"tool_name": "apply_patch",
51+
"tool_input": {"command": body},
52+
"tool_response": "",
53+
}
54+
)
55+
56+
57+
def _equivalent_edit_event(tmp_path: Path, old: str, new: str) -> str:
58+
target = tmp_path / "edited.py"
59+
return json.dumps(
60+
{
61+
"hook_event_name": "PostToolUse",
62+
"cwd": str(tmp_path),
63+
"tool_name": "Edit",
64+
"tool_input": {
65+
"file_path": str(target),
66+
"old_string": old,
67+
"new_string": new,
68+
},
69+
"tool_response": "",
70+
}
71+
)
72+
73+
74+
@pytest.mark.parametrize("module", ["preemptive_context", "pipeline_impact_bump"])
75+
def test_apply_patch_posttooluse_matches_equivalent_edit_stdout(
76+
module: str, tmp_path: Path
77+
) -> None:
78+
env = _isolated_env(tmp_path)
79+
patch_result = _run_entry(
80+
module, _update_patch_event(tmp_path, "before\n", "after\n"), env
81+
)
82+
edit_result = _run_entry(
83+
module, _equivalent_edit_event(tmp_path, "before\n", "after\n"), env
84+
)
85+
86+
assert patch_result.returncode == edit_result.returncode
87+
assert patch_result.stdout == edit_result.stdout
88+
89+
90+
_LONG_COMMENT_BLOCK = "\n".join(
91+
f"# reason line {n} for this design choice" for n in range(1, 9)
92+
)
93+
94+
95+
def _add_file_patch_event(tmp_path: Path, filename: str) -> str:
96+
lines = "\n".join(f"+{line}" for line in _LONG_COMMENT_BLOCK.splitlines())
97+
body = f"*** Begin Patch\n*** Add File: {filename}\n{lines}\n+x = 1\n*** End Patch"
98+
return json.dumps(
99+
{
100+
"hook_event_name": "PreToolUse",
101+
"cwd": str(tmp_path),
102+
"tool_name": "apply_patch",
103+
"tool_input": {"command": body},
104+
}
105+
)
106+
107+
108+
def _equivalent_write_event(tmp_path: Path, filename: str) -> str:
109+
content = _LONG_COMMENT_BLOCK + "\nx = 1\n"
110+
return json.dumps(
111+
{
112+
"hook_event_name": "PreToolUse",
113+
"cwd": str(tmp_path),
114+
"tool_name": "Write",
115+
"tool_input": {"file_path": str(tmp_path / filename), "content": content},
116+
}
117+
)
118+
119+
120+
def test_apply_patch_pretooluse_blocks_like_equivalent_write(tmp_path: Path) -> None:
121+
env = _isolated_env(tmp_path)
122+
patch_result = _run_entry(
123+
"decision_gate", _add_file_patch_event(tmp_path, "new_a.py"), env
124+
)
125+
write_result = _run_entry(
126+
"decision_gate", _equivalent_write_event(tmp_path, "new_b.py"), env
127+
)
128+
129+
assert patch_result.returncode == 2
130+
assert write_result.returncode == 2
131+
assert patch_result.returncode == write_result.returncode
132+
assert "[decision-gate] BLOCKED" in patch_result.stderr
133+
assert "[decision-gate] BLOCKED" in write_result.stderr
134+
135+
136+
def _malformed_patch_event(tmp_path: Path, hook_event_name: str) -> str:
137+
body = "*** Begin Patch\n*** Update File: missing.py\n@@\n-x\n+y\n*** End Patch"
138+
return json.dumps(
139+
{
140+
"hook_event_name": hook_event_name,
141+
"cwd": str(tmp_path),
142+
"tool_name": "apply_patch",
143+
"tool_input": {"command": body},
144+
}
145+
)
146+
147+
148+
def test_malformed_patch_on_pretooluse_exits_2(tmp_path: Path) -> None:
149+
env = _isolated_env(tmp_path)
150+
result = _run_entry(
151+
"decision_gate", _malformed_patch_event(tmp_path, "PreToolUse"), env
152+
)
153+
assert result.returncode == 2
154+
assert result.stdout == ""
155+
assert "decision_gate" in result.stderr
156+
157+
158+
def test_malformed_patch_on_posttooluse_exits_1_with_stderr(tmp_path: Path) -> None:
159+
env = _isolated_env(tmp_path)
160+
result = _run_entry(
161+
"post_tool_capture", _malformed_patch_event(tmp_path, "PostToolUse"), env
162+
)
163+
assert result.returncode == 1
164+
assert result.stdout == ""
165+
assert "post_tool_capture" in result.stderr

0 commit comments

Comments
 (0)