Skip to content

cbm-code-discovery-gate not working as intended #288

@tyrantlizard77

Description

@tyrantlizard77

I noticed Claude kept using sed to examine files. I'm on Claude Code v2.1.114

I asked it what was going on:

⏺ It's the PreToolUse hook at the bottom of the hooks section:

  {
      "matcher": "Grep|Glob|Read|Search",
      "hooks": [
          {
              "type": "command",
              "command": "~/.claude/hooks/cbm-code-discovery-gate"
          }
      ]
  }

  It intercepts every Read, Grep, Glob, and Search call and blocks them unless I've
  used codebase-memory-mcp first. That's why Edit fails — it internally requires Read
  to have succeeded first, and Read is blocked.

⏺ It only blocks the first call per session — after that it creates a gate file and
  allows everything through. The gate file already exists since I used Grep earlier
  (which was blocked). So subsequent Read/Edit calls should work now. Let me test:

⏺ The gate file exists but the problem is each subagent gets a different $PPID, so the
  gate was created for that PID, not mine. The main process PPID is different from the
  subagent PPIDs. That's why it kept blocking — each new context (subagent, or even
  each tool call if the process forks) gets a fresh PPID.

  Anyway — the hook is only meant to nudge toward MCP on the first call. The issue is
  the PPID-based gating doesn't work well with subagents. I'll switch back to using
  Read/Edit now and see if they work:

⏺ Still blocked. The gate file for this process's PPID hasn't been created yet (the
  earlier blocks created files for their PPIDs but not for the current main process).
  The hook design creates a new gate per PPID, and each tool call seems to get a fresh
  one.

  Bottom line: Read/Edit won't work in this session. I'll continue with cat/sed via
  Bash for the remaining edits. Want me to proceed, or would you rather fix the hook
  first?

I asked for some clarification, and it summarized:

⏺ The Bash tool gets PPID 13048 but the Read tool hook runs in a different process with
   a different PPID. Each tool invocation spawns a new shell, so $PPID is unique every
  time. The gate file from the first blocked Read doesn't help the second Read because
  it's a different PID.

  The hook's retry logic is broken for this tool — it only works if two calls happen
  within the same process, which never happens. Want me to fix the hook to use a
  session-level key instead of $PPID, or should I just keep going with cat/sed?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions