[FEATURE] Workspace-conditional / lazy hook loading (analog to MCP on-demand discovery)

Resolved 💬 3 comments Opened May 19, 2026 by Luiz-Frias Closed May 23, 2026

Preflight Checklist

  • [x] I have searched existing requests and this feature hasn't been requested yet
  • [x] This is a single feature request (not multiple features)

Problem Statement

Claude Code hooks currently fire on every PreToolUse / PostToolUse event regardless of workspace context. In multi-language or multi-project setups, this means:

  • Rust-specific hooks (cargo clippy, cargo deny, cargo audit) fire even in pure-Python repos
  • Python-specific hooks (ruff format --check, pytest, pip-audit) fire even in pure-Rust repos
  • Every hook pays the cost of running its workspace-detection guard ([ -f Cargo.toml ] && ... || exit 0) for every event

For polyglot users juggling N projects across Rust/Python/TS, this creates either (a) unnecessary hook execution latency, or (b) requires building a "router hook" that introspects workspace + delegates. The router pattern works but pushes the burden to each user.

Proposed Solution

Extend settings.json hook config with a condition field (or workspace matcher):

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "condition": {
          "workspace_has": ["Cargo.toml"]
        },
        "hooks": [{ "type": "command", "command": "cargo clippy" }]
      },
      {
        "matcher": "Edit|Write",
        "condition": {
          "workspace_has": ["pyproject.toml"]
        },
        "hooks": [{ "type": "command", "command": "ruff format --check ." }]
      }
    ]
  }
}

The condition.workspace_has (or equivalent) accepts file globs / paths relative to the workspace root. Claude Code evaluates the condition once at workspace-bind time (or with a short TTL cache), not on every event.

Alternative Solutions

  • Router hook: single hook that introspects + delegates. Works today but pushes complexity to user.
  • Per-repo .claude/hooks/: already supported, but doesn't help when ONE repo is polyglot.
  • Skill-based manual loading: requires explicit invocation; defeats the "automatic guard" purpose of hooks.

Priority

Low - Nice to have

Feature Category

Configuration and settings

Use Case Example

I run Claude Code across ~6 active repos: 2 Rust (orchestration framework + benchmark harness), 3 Python (research/ML/CLI tools), 1 polyglot. I want to define hook bundles ONCE in settings:

  • hooks.rust.* — fire only when Cargo.toml is present in the workspace
  • hooks.python.* — fire only when pyproject.toml is present
  • hooks.always.* — fire regardless

…and have Claude Code dispatch the right bundle per workspace, with the same on-demand loading semantics that MCP servers already enjoy.

Additional Context

_No response_

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗