Skip to content

Commit 86eeaf8

Browse files
cdeustclaude
andcommitted
fix(hooks): entry.py wires the composition root, fix tomllib on 3.10 (#605)
CI on PR #605 found two gaps the parity-only test subset masked. First, tests_py/scripts/test_composition_root_wiring_coverage.py:: test_every_hook_with_a_main_block_wires_the_composition_root failed: scripts/launcher.py calls wire_composition_root() before run_module (issue number 560), and entry.py skipped that call, relying only on each hook's own __main__ block to re-wire, which the static guard does not accept as equivalent. entry.py:main now wires the composition root itself between prepare_environment and runpy.run_module, the same seam scripts/launcher.py uses. Second, tests_py/hooks/test_entry.py imported tomllib unconditionally, which does not exist on Python 3.10 (requires-python is >=3.10); the test now falls back to the tomli backport on 3.10, the same package build/hatchling already pull in for that Python version per uv.lock. Also corrected "ten hook modules" to "eleven" in entry.py, the test docstring and the CHANGELOG entry; the allowlist has eleven names. Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Cz7A6goxVzfKL9NT7k4zDH
1 parent 1a7e334 commit 86eeaf8

3 files changed

Lines changed: 35 additions & 15 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ adheres to [Semantic Versioning](https://semver.org/).
1313
reach this repository's `scripts/launcher.py` the way the Claude Code
1414
plugin does. `hypermnesia-mcp-hook`, a new `[project.scripts]` entry
1515
backed by `mcp_server/hooks/entry.py`, validates its module argument
16-
against the ten hook names `.claude-plugin/plugin.json` wires, resolves
17-
the storage backend the same way the server does, and dispatches to the
18-
named hook's own `__main__` unchanged. A Codex plugin can then call
16+
against the eleven hook names `.claude-plugin/plugin.json` wires,
17+
resolves the storage backend the same way the server does, wires the
18+
composition root the same way `scripts/launcher.py` does, and dispatches
19+
to the named hook's own `__main__` unchanged. A Codex plugin can then call
1920
`uvx --from "hypermnesia-mcp[postgresql,sqlite]" hypermnesia-mcp-hook
2021
<module>` directly. This changes nothing for the Claude Code plugin,
2122
which keeps using `scripts/launcher.py`.

‎mcp_server/hooks/entry.py‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
``uvx --from "hypermnesia-mcp[postgresql,sqlite]" hypermnesia-mcp-hook
88
<module>``. ``main()`` validates ``<module>`` against the allowlist below,
99
resolves the storage backend the way ``mcp_server/__main__.py`` does for
10-
the server, and dispatches to the named hook's own ``__main__`` block via
11-
``runpy`` -- unchanged, so its headless guard and composition-root wiring
12-
still run exactly as they do under ``scripts/launcher.py``.
10+
the server, wires the composition root the way ``scripts/launcher.py``
11+
does before dispatching (issue number 560), and dispatches to the named
12+
hook's own ``__main__`` block via ``runpy`` -- unchanged, so its headless
13+
guard and its own re-wiring still run exactly as they do under
14+
``scripts/launcher.py``.
1315
1416
No ``chdir`` (uvx does not need one), no dependency installation (uvx owns
1517
dependencies), no capture preflight (``post_tool_capture.py`` already
@@ -26,8 +28,9 @@
2628

2729
from mcp_server.infrastructure.backend_marker import apply_backend_resolution
2830

29-
# The ten hook modules the Claude Code plugin manifest wires (.claude-plugin/
30-
# plugin.json), verified against that file. Anything else is refused.
31+
# The eleven hook modules the Claude Code plugin manifest wires
32+
# (.claude-plugin/plugin.json), verified against that file. Anything else
33+
# is refused.
3134
HOOK_MODULES = frozenset(
3235
{
3336
"session_start",
@@ -81,10 +84,14 @@ def main() -> None:
8184
8285
Precondition: none (argv may be missing its module argument).
8386
Postcondition: exits 2 with nothing on stdout when argv[1] is absent or
84-
not in HOOK_MODULES; otherwise runs ``mcp_server.hooks.<module>``
85-
as ``__main__`` and exits with that hook's own exit code, or 1 on any
86-
exception the hook itself did not turn into a SystemExit (mirrors
87-
``scripts/launcher.py`` lines 159-167).
87+
not in HOOK_MODULES; otherwise wires every core/ injection seam
88+
(issue number 560, mirroring ``scripts/launcher.py`` lines 150-155 --
89+
the hook's own ``__main__`` re-wires too, but a hook body can run
90+
before that block returns control here), then runs
91+
``mcp_server.hooks.<module>`` as ``__main__`` and exits with that
92+
hook's own exit code, or 1 on any exception the hook itself did not
93+
turn into a SystemExit (mirrors ``scripts/launcher.py`` lines
94+
159-167).
8895
"""
8996
name = sys.argv[1] if len(sys.argv) > 1 else None
9097
if name not in HOOK_MODULES:
@@ -93,6 +100,10 @@ def main() -> None:
93100

94101
prepare_environment(os.environ)
95102

103+
from mcp_server.hooks.wiring import wire_composition_root # noqa: PLC0415 - issue number 560, same seam as scripts/launcher.py:153
104+
105+
wire_composition_root()
106+
96107
module = f"mcp_server.hooks.{name}"
97108
sys.argv = [module]
98109
try:

‎tests_py/hooks/test_entry.py‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
Source: this PR's specification (Codex plugin wave, PR 1). A Codex plugin
55
ships only its own directory and calls
66
``uvx --from "hypermnesia-mcp[postgresql,sqlite]" hypermnesia-mcp-hook
7-
<module>``, so the ten hook modules the Claude Code plugin manifest wires
8-
(.claude-plugin/plugin.json) must be runnable without scripts/launcher.py.
7+
<module>``, so the eleven hook modules the Claude Code plugin manifest
8+
wires (.claude-plugin/plugin.json) must be runnable without
9+
scripts/launcher.py.
910
1011
Parity: mcp_server.hooks.entry must behave exactly like scripts/launcher.py
1112
for every allowlisted module on a benign event, because both are meant to
@@ -19,11 +20,18 @@
1920
import os
2021
import subprocess
2122
import sys
22-
import tomllib
2323
from pathlib import Path
2424

2525
import pytest
2626

27+
# tomllib is 3.11+; requires-python is >=3.10 (pyproject.toml), so the
28+
# fallback backport is used on 3.10, exactly like build/hatchling's own
29+
# dependency on it (uv.lock: tomli, marker python_full_version < "3.11").
30+
if sys.version_info >= (3, 11):
31+
import tomllib
32+
else:
33+
import tomli as tomllib
34+
2735
REPO_ROOT = Path(__file__).resolve().parents[2]
2836
LAUNCHER_PATH = REPO_ROOT / "scripts" / "launcher.py"
2937

0 commit comments

Comments
 (0)