fix(hooks): deny a single-file Grep only when the graph indexes it

A Grep pinned to one unindexed file (pyproject.toml, a config) spent the session's single strict deny on a target the graph cannot answer. Apply Read's indexed-file check to single-file targets; directory searches are unchanged.

Co-Authored-By: Claude <noreply@anthropic.com>
(cherry picked from commit 1b89dfa50c)
This commit is contained in:
Andre Brait authored and safishamsi committed 2026-11-03 21:37:25 +00:00
1 parent 2a5d442632
commit 7433c0a805
3 files changed
+14 -2

No files matched your search

+1 -1
View File
@@ -253,7 +253,7 @@ Codex users also need `multi_agent = true` under `[features]` in `~/.codex/confi
`--platform agents` (alias `--platform skills`) targets the generic cross-framework [Agent-Skills](https://github.com/anthropics/skills) locations: the spec's user-global `~/.agents/skills/` (read by `npx skills` and spec-compliant frameworks) for a global install, and `./.agents/skills/` for a project (`--project`) install. The bare `graphify install` stays single-platform (Claude Code) by design: use the named `agents` platform when you want the skill discoverable by any framework that reads `.agents/skills`.
Claude Code's `graphify install --project --strict` makes Read and dedicated Grep share one denial per session unless a recent graph query has oriented the agent (default 30 minutes). Grep may target a directory; it does not need an indexed-file match. External and graph-output Grep targets produce neither a deny nor a nudge, in soft mode as well as strict mode, matching Read's scope. A graph marked `needs_update` never triggers a Grep denial. Bash searches and Glob stay advisory. `GRAPHIFY_HOOK_STRICT=1` enables the policy without reinstalling; `GRAPHIFY_HOOK_STRICT=0` disables it even when installed with `--strict`. Existing strict installs need a reinstall to add the search hook's `--strict` flag, or the environment override.
Claude Code's `graphify install --project --strict` makes Read and dedicated Grep share one denial per session unless a recent graph query has oriented the agent (default 30 minutes). A Grep over a directory needs no indexed-file match; a Grep pinned to a single file is denied only when the graph indexes that file, as with Read. External and graph-output Grep targets produce neither a deny nor a nudge, in soft mode as well as strict mode, matching Read's scope. A graph marked `needs_update` never triggers a Grep denial. Bash searches and Glob stay advisory. `GRAPHIFY_HOOK_STRICT=1` enables the policy without reinstalling; `GRAPHIFY_HOOK_STRICT=0` disables it even when installed with `--strict`. Existing strict installs need a reinstall to add the search hook's `--strict` flag, or the environment override.
Pi has no `PreToolUse` hook, so `graphify pi guard install` registers a native Pi extension (via Pi's own `pi install <package directory>`) that runs the installed `graphify hook-guard` before Pi's `read`, `grep`, `find` and search-style `bash` calls. Fresh/stale graph decisions and the `GRAPHIFY_HOOK_STRICT=1` once-per-session denial shared by indexed Read and dedicated Grep come from that same policy, so Pi matches Claude Code's hook behavior; Bash and file listing remain advisory. Guidance arrives as a `<system-reminder source="graphify">` steering message. It needs Pi 0.58.4 or newer: earlier versions let a steering message skip the rest of a parallel tool-call batch. The package ships inside the Graphify installation, so install Graphify persistently (`uv tool install graphifyy` or `pipx install graphifyy`, not `uvx`), restart Pi after installing or upgrading, and remove it with `graphify pi guard uninstall`. Only local file targets are inspected; the hook needs `graphify` on an absolute `PATH` entry outside the project, is bounded to a two-second, 256 KiB-in/64 KiB-out call, and fails open. The same package also loads in [Oh My Pi](https://github.com/can1357/oh-my-pi): `omp plugin install "$(graphify pi guard path)"`. The registered path contains the Python version and venv, so re-run `graphify pi guard install` after changing either, and remove the old entry with `pi remove <old path>`.
+4 -1
View File
@@ -1248,7 +1248,10 @@ def _run_hook_guard(kind: str, strict: bool = False) -> None:
output = root / output
if target.is_relative_to(output.resolve()):
return
if _hook_strict_enabled(strict) and not out_path("needs_update").exists() \
# A single-file target needs the same index evidence as Read; a
# directory is the code the graph covers, so it needs none.
indexed = not target.is_file() or _target_is_indexed(str(target), root)
if indexed and _hook_strict_enabled(strict) and not out_path("needs_update").exists() \
and not _query_stamp_fresh() \
and _mark_session_denied(str(d.get("session_id") or "")):
sys.stdout.write(_with_hint(_SEARCH_DENY, terms=_search_hint_terms(t)))
+9
View File
@@ -78,6 +78,15 @@ def test_soft_grep_suppresses_irrelevant_path_nudges(tmp_path, monkeypatch, targ
assert "knowledge graph at" in _invoke("search", _grep("src"), tmp_path, monkeypatch, strict=False)
def test_grep_on_one_unindexed_file_keeps_the_deny(tmp_path, monkeypatch):
_fixture(tmp_path) # manifest indexes src/mod.py only
(tmp_path / "pyproject.toml").write_text("[project]\n", encoding="utf-8")
out = _invoke("search", _grep("pyproject.toml"), tmp_path, monkeypatch, strict=True)
assert not _is_deny(out) and "knowledge graph at" in out
# The session's one deny is still available for an indexed target.
assert _is_deny(_invoke("search", _grep("src/mod.py"), tmp_path, monkeypatch, strict=True))
def test_grep_custom_output_bypasses(tmp_path, monkeypatch):
from graphify import paths
_fixture(tmp_path)