1"""PreToolUse hook handler: learning-test discoverability gate (FEAT-1742).

2

3 Dispatches Write/Edit tool calls to the learning_tests_gate for import

4 detection and registry probing. All other tool calls pass through unchanged.

5

6 The gate is a no-op when ``learning_tests.enabled`` is false (default) or

7``learning_tests.discoverability.mode`` is ``"off"``. When enabled, it

8 emits a soft nudge (warn mode, exit 0) or blocks (block mode, exit 2) when

9 a file being written imports packages with no proven Learning Test record.

10

11 Claude Code wires this handler via

12``hooks/adapters/claude-code/pre-tool-use.sh`` for the ``"Write|Edit"``

13 matcher in ``hooks/hooks.json``. Codex and OpenCode users opt in separately

14— see ``hooks/adapters/codex/README.md`` and

15``hooks/adapters/opencode/README.md``.

16"""

17

18 from __future__ import annotations

19

20 from little_loops.hooks.types import LLHookEvent, LLHookResult

21

22

23 def handle(event: LLHookEvent) -> LLHookResult:

24 """Dispatch Write/Edit to discoverability gate; pass-through for other tools."""

25 tool_name = event.payload.get("tool_name", "")

26 if tool_name in {"Write", "Edit"}:

27 from little_loops.hooks.learning_tests_gate import gate

28

29 return gate(event)

30 return LLHookResult(exit_code=0)