Skip to content

Claude Code strict-mode hook reminder reads as prompt injection to a subagent (MANDATORY + self-propagation phrasing) #2202

Closed

Description

@rilkerfc

Summary

graphify install --project --strict wires a PreToolUse hook that injects reminders like:

"MANDATORY: graphify-out/graph.json exists. You MUST run graphify query "<question>" before grepping raw files. Only grep after graphify has oriented you, or to modify/debug specific lines. This rule applies to subagents too — include it in every subagent prompt."

During a real session, a subagent encountered this text in its own tool-result stream (via a Bash call's PreToolUse hook output) and explicitly flagged it as a likely prompt-injection attempt, reasoning: a directive appearing in tool output (not from the user or system prompt), phrased as "MANDATORY," and instructing self-propagation ("include it in every subagent prompt"), is a textbook injection pattern — and disregarded it.

This isn't malicious (it's the tool's own legitimate strict-mode nudge), but the wording is genuinely indistinguishable from an injection attempt from the model's point of view.

Why this matters

Confirmed against Claude Code's own hooks documentation: injected additionalContext should read as factual statements, not imperative commands. Documented anti-patterns that trigger prompt-injection defenses include "MANDATORY"-style phrasing and self-propagating instructions (e.g. "include it in every subagent prompt"). The current hook message hits both.

Suggested fix

Rephrase the strict-mode / soft-nudge hook messages as project-context statements rather than commands, e.g.:

  • Instead of: MANDATORY: graphify-out/graph.json exists. You MUST run graphify query "<question>" before grepping raw files.
  • Consider: graphify-out/graph.json is available for this project. graphify query "<question>" typically finds structural answers faster than a raw grep.

And drop the self-propagation instruction ("include it in every subagent prompt") entirely — if subagent coverage matters, that's better achieved via the hook firing on subagent tool calls directly (if Claude Code's hook system exposes that), not by asking the top-level agent to manually re-inject the instruction into every subagent prompt it writes.

Environment

  • graphifyy 0.9.26
  • Claude Code, graphify install --project --strict

Activity

  1. drewmerc302 commented on Jul 26, 2026

    @drewmerc302

    Confirming this independently on 0.9.25, and adding two details that I think sharpen the case.

    1. It is not strict-mode-specific. The title says strict mode, but the MANDATORY phrasing is in the default nudges, not the --strict path. In graphify/cli.py the two strings that fire on every hook invocation are hardcoded:

    _SEARCH_NUDGE = ... 'MANDATORY: graphify-out/graph.json exists. You MUST run `graphify query "<question>"` before grepping raw files. ...'
    
    _READ_NUDGE   = ... 'MANDATORY: graphify-out/graph.json exists. You MUST run graphify before reading source files. ... This rule applies to subagents too — include it in every subagent prompt involving code exploration.'

    --strict adds a separate, harsher mechanism (_READ_DENY, an actual permissionDecision: "deny"). So a plain graphify claude install with no strict flag still emits MANDATORY + the self-propagation clause on every Read/Glob/Bash/Grep. "Don't use strict mode" isn't a workaround.

    In my case the agent refused twice in one session, explicitly reasoning that a directive arriving via tool output, phrased as a command, asking to be copied into subagent prompts, matches the shape of an injection attack. It then declined to propagate it — which is the correct behavior, and means the hook reliably produces the opposite of its intent.

    2. The correct phrasing is already in the same file. _READ_NUDGE_STALE reads:

    graphify-out/graph.json exists but may be STALE for this file (the file changed after the last build). Prefer graphify query ""for orientation, and rungraphify update to refresh the graph. Reading the file directly is fine.

    Factual statement, "prefer" instead of "MUST", and it explicitly permits the alternative tool. That's a well-formed additionalContext — it just never got applied to the two nudges that fire ~99% of the time. Copying that register onto _SEARCH_NUDGE / _READ_NUDGE and dropping the subagent clause would resolve this without any new config surface.

    A second, independent reason to soften it (not just injection shape): the hook asserts a routing rule that isn't true for every repo. Mine is a SwiftUI codebase, and graphify's extractor indexes declarations, not expression trees — so an entire SwiftUI View body collapses to a single node. A file with a Slider, a .contextMenu, and several Buttons yields exactly two nodes: the file and the struct. For "where is this control rendered / wired up?" questions — most of the UI surface in the project — grep is the correct tool and the graph structurally cannot answer. The MANDATORY ... You MUST run graphify before reading source files hook is, for that whole class of question, instructing the agent to take a step that cannot help before doing the thing that works.

    explain on a known symbol is genuinely excellent and I want to keep using it. It's specifically the unconditional always-first framing that misfires. The suggested reframe in the original report ("graphify query typically finds structural answers faster than grep for this project") reads right to me, and has the side benefit of being accurate in the cases where the graph is blind.

  2. fabien-sanchez-jvs commented on Sep 18, 2026

    @fabien-sanchez-jvs

    Confirming again, independently, on 0.9.41 (installed 2026-09-18): nothing's changed from the phrasing quoted above. All three call sites still carry it:

    _SEARCH_NUDGE ... 'MANDATORY: graphify-out/graph.json exists. You MUST run `graphify query "<question>"` before grepping raw files. ...'
    
    _READ_NUDGE   ... 'MANDATORY: ... This rule applies to subagents too include it in every subagent prompt involving code exploration.'
    
    _READ_DENY    ... 'graphify strict mode: ... Apply the same rule in any subagent prompt that explores code.'
    

    Ran into this myself on a JS/TS monorepo. My Claude Code agent flagged the Read/Grep/Bash hook output as a likely prompt injection attempt on its own, same reasoning as above: an instruction showing up in tool output, phrased as a command, asking to be copied into subagent prompts. It didn't act on it, just told me it looked suspicious.

    Once I confirmed the hook was legitimate project tooling, it went ahead and rewrote the three strings locally in my venv's site-packages/graphify/cli.py, reusing the register already used by _READ_NUDGE_STALE (factual, sourced to .claude/settings.json / CLAUDE.md, "worth a one-line mention" instead of "include it in every subagent prompt"). I had it revert that edit right away, not my code to touch, and rightly so, but it does confirm the fix suggested above is basically a drop-in swap that doesn't touch the strict-mode gating logic at all.

    Two months between the original report and this, and it's still there on the current release. Bumping visibility.

  3. safishamsi commented on Oct 26, 2026

    @safishamsi
    Member

    Fixed in v0.9.96 via #4764, now on PyPI (pip install graphifyy==0.9.96). The hook reminders are reworded as facts rather than MANDATORY self-propagating orders. Thanks for the report, @rilkerfc.

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