Shell-agnostic PreToolUse guard (#522). Reads the tool-call JSON from stdin and, when a fresh in-project knowledge graph exists, nudges the agent to use graphify instead of grepping/reading raw files. Replaces the old inline bash hooks that failed to parse on Windows. Fails open ev
(kind: str, strict: bool = False)
| 1622 | |
| 1623 | |
| 1624 | def _run_hook_guard(kind: str, strict: bool = False) -> None: |
| 1625 | """Shell-agnostic PreToolUse guard (#522). |
| 1626 | |
| 1627 | Reads the tool-call JSON from stdin and, when a fresh in-project knowledge graph |
| 1628 | exists, nudges the agent to use graphify instead of grepping/reading raw files. |
| 1629 | Replaces the old inline bash hooks that failed to parse on Windows. |
| 1630 | |
| 1631 | Fails open everywhere: any error, or a non-matching tool call, prints nothing |
| 1632 | and the caller exits 0, so a legitimate tool call is never blocked by a bug. |
| 1633 | |
| 1634 | In strict mode (opt-in), the FIRST raw Read of indexed, fresh code or dedicated |
| 1635 | Grep in the project is DENIED with a redirect to `graphify query`, then |
| 1636 | downgrades to the soft nudge. The two tools share one block per session. |
| 1637 | Bash and Glob stay nudge-only: a compound shell command has no single |
| 1638 | parseable target and blocking file listing would strand navigation. A Bash |
| 1639 | command that prints an in-project code file (cat/head/tail/sed -n/nl/less/ |
| 1640 | more/bat, see _bash_read_target) gets the Read nudge for that file, through |
| 1641 | the same budget, and is never denied. |
| 1642 | Out-of-project and graph-output targets are ignored; stale graphs never block. |
| 1643 | """ |
| 1644 | from graphify.paths import out_path |
| 1645 | # Gemini's BeforeTool hook takes no stdin and must ALWAYS return a decision so |
| 1646 | # the tool is never blocked; the graph nudge is appended only when a graph |
| 1647 | # exists. Handled before the stdin read below (which the search/read guards need). |
| 1648 | if kind == "gemini": |
| 1649 | payload = {"decision": "allow"} |
| 1650 | try: |
| 1651 | if out_path("graph.json").is_file(): |
| 1652 | payload["additionalContext"] = _GEMINI_NUDGE_TEXT |
| 1653 | except Exception: |
| 1654 | pass |
| 1655 | sys.stdout.write(json.dumps(payload, ensure_ascii=False, separators=(",", ":"))) |
| 1656 | return |
| 1657 | try: |
| 1658 | d = json.loads(sys.stdin.buffer.read().decode("utf-8", "replace")) |
| 1659 | except Exception: |
| 1660 | return |
| 1661 | if not isinstance(d, dict): |
| 1662 | return |
| 1663 | # Claude Code SessionStart with matcher "compact": reset this session's |
| 1664 | # nudge budget and restate the graph. Other sources (startup, resume, |
| 1665 | # clear, fork) keep their counters. |
| 1666 | if kind == "compact": |
| 1667 | try: |
| 1668 | if d.get("hook_event_name", "SessionStart") != "SessionStart" \ |
| 1669 | or d.get("source") != "compact": |
| 1670 | return |
| 1671 | _reset_hook_session(str(d.get("session_id") or "")) |
| 1672 | if out_path("graph.json").is_file(): |
| 1673 | sys.stdout.write(_nudge_for_out(_COMPACT_CONTEXT)) |
| 1674 | except Exception: |
| 1675 | pass |
| 1676 | return |
| 1677 | t = d.get("tool_input", d) |
| 1678 | if not isinstance(t, dict): |
| 1679 | return |
| 1680 | try: |
| 1681 | # A Bash file read is a Read by another name: route it through the read |