Skip to content

Agent bypasses the graph by Read-ing files: PreToolUse hook only catches Bash grep, not the Read tool (help wanted) #1114

Closed

Description

@safishamsi

Problem

graphify's core value prop is: once graphify-out/graph.json exists, the coding agent should answer codebase questions with graphify query / path / explain (a scoped subgraph) instead of grepping or reading raw files. Today that's enforced by a PreToolUse hook (graphify/__main__.py, _SETTINGS_HOOK) that fires before Bash search commands and nudges the agent toward graphify query:

case "$CMD" in
  *grep*|*rg\ *|*ripgrep*|*find\ *|*fd\ *|*ack\ *|*ag\ *)
    [ -f graphify-out/graph.json ] && echo '{...additionalContext: use `graphify query` instead...}' ;;
esac

The gap: it only matches Bash search commands. The most common way an agent actually avoids the graph is invisible to it:

  • The native Read tool — an agent that answers "how does X work?" by reading 20-30 files one at a time never runs a Bash command, so the hook never fires and the graph is bypassed entirely.
  • Bash content reads — cat, head, tail, sed -n, awk over many files dodge the search-tool match too.

So a graph can exist and still be ignored on exactly the questions it's meant to answer.

Ask (help wanted)

Extend the enforcement so the agent is nudged toward graphify query when a graph exists and it's about to browse raw files, not just when it greps:

  1. Add a PreToolUse hook on the Read tool (and Glob) that fires only when graphify-out/graph.json exists, injecting the same "query first" additionalContext.
  2. Broaden the Bash matcher to include multi-file content reads (cat/head/sed/awk) — but only when multiple paths / a glob are involved, so single-file reads don't trigger.

Design constraint: don't spam

Firing on every Read is noise that trains the agent to ignore the nudge. Scope it:

  • Gate on graphify-out/graph.json existing (already the pattern).
  • Prefer nudging when the Read/glob targets a source or doc file (not configs, not files already inside graphify-out/).
  • Keep it a nudge (additionalContext), never a block — legitimate reads must still go through.

Scope / why it's low-risk

  • This is hook configuration in graphify/__main__.py + tests — it touches no skill-file content, so it can't break the extraction pipeline.
  • Testable: assert the generated settings/hook JSON includes a Read-tool matcher and that the command emits the nudge only when graph.json exists.
  • Verify the equivalent for the non-Claude enforcement paths (the always-on AGENTS.md / Cursor-rules text already says "query first … instead of raw grep output" — extend that wording to cover reading files, not just grep).

Related

Activity

  1. safishamsi commented on Jun 2, 2026

    @safishamsi
    MemberAuthor

    Fixed and released in 0.8.30. A Read|Glob PreToolUse hook (5cc7ec8) now nudges toward graphify query when graphify-out/graph.json exists and the agent reads a source/doc file outside graphify-out/ (the Read-tool path the Bash search hook never saw). It never blocks and fails open. Closing note for the record.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions