-
Notifications
You must be signed in to change notification settings - Fork 1
[Hook architecture](15) Put the categorical-scope-guard hook onto the shared hook code #730
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mergify
merged 14 commits into
main
from
plan/hook-architecture-15-put-the-categorical-scope-guard-hook-onto-the-shared-hook-code
Sep 17, 2026
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
027cc35
invoker: wf-1789406883560-32/implement-hook-build-the-lever — Put the…
bbebd6e
invoker: wf-1789406883560-32/verify-hook-build-the-lever — Run the de…
5c0096b
invoker: wf-1789406883560-32/scrub-handoff-artifacts — Terminal read-…
eccbe83
Merge experiment/wf-1789406883560-32/scrub-handoff-artifacts/g1.t5.a-…
EdbertChan 7757396
invoker: wf-1789406886887-33/implement-hook-cat-mode-default — Put th…
13ec88c
invoker: wf-1789406886887-33/verify-hook-cat-mode-default — Run the d…
163d9e8
invoker: wf-1789406886887-33/scrub-handoff-artifacts — Terminal read-…
87798db
Merge experiment/wf-1789406886887-33/scrub-handoff-artifacts/g1.t5.a-…
EdbertChan 49b348f
invoker: wf-1789406891093-34/implement-hook-categorical-scope-guard —…
fcfbe21
invoker: wf-1789406891093-34/verify-hook-categorical-scope-guard — Ru…
0cc8306
invoker: wf-1789406891093-34/scrub-handoff-artifacts — Terminal read-…
2b76f0a
Merge experiment/wf-1789406891093-34/scrub-handoff-artifacts/g1.t5.a-…
EdbertChan bd8430d
Merge remote-tracking branch 'origin/main' into plan/hook-architectur…
EdbertChan 5bcd8b5
categorical-scope-guard: log the detector failure before returning UN…
EdbertChan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| #!/usr/bin/env python3 | ||
| """Codex PreToolUse entrypoint for categorical-scope-guard.""" | ||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import sys | ||
|
|
||
| sys.path.insert(0, os.path.join( | ||
| os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "_sdk")) | ||
|
|
||
| from detect import detect # noqa: E402 | ||
| from runtime import run_hook # noqa: E402 | ||
|
|
||
|
|
||
| def main() -> None: | ||
| run_hook("categorical-scope-guard", "codex", detect, "PreToolUse") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| #!/usr/bin/env python3 | ||
| """Cursor PreToolUse entrypoint for categorical-scope-guard.""" | ||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import sys | ||
|
|
||
| sys.path.insert(0, os.path.join( | ||
| os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "_sdk")) | ||
|
|
||
| from detect import detect # noqa: E402 | ||
| from runtime import run_hook # noqa: E402 | ||
|
|
||
|
|
||
| def main() -> None: | ||
| run_hook("categorical-scope-guard", "cursor", detect, "PreToolUse") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
engine/hooks/categorical-scope-guard/tests/test_hooks_sdk_mode.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import os | ||
| import subprocess | ||
| import sys | ||
| import tempfile | ||
| import unittest | ||
| from pathlib import Path | ||
|
|
||
| HERE = os.path.dirname(os.path.abspath(__file__)) | ||
| HOOK_DIR = os.path.dirname(HERE) | ||
| FIXTURES = os.path.join(HERE, "fixtures") | ||
| ENTRYPOINT = os.path.join(HOOK_DIR, "claude_pretooluse.py") | ||
|
|
||
|
|
||
| def fixture(name: str) -> str: | ||
| with open(os.path.join(FIXTURES, name), encoding="utf-8") as handle: | ||
| return handle.read() | ||
|
|
||
|
|
||
| def human(text: str) -> dict[str, object]: | ||
| return {"type": "user", "message": {"role": "user", "content": text}} | ||
|
|
||
|
|
||
| def write_transcript(entries: list[dict[str, object]]) -> str: | ||
| handle = tempfile.NamedTemporaryFile("w", suffix=".jsonl", delete=False, encoding="utf-8") | ||
| with handle: | ||
| for entry in entries: | ||
| handle.write(json.dumps(entry) + "\n") | ||
| return handle.name | ||
|
|
||
|
|
||
| def run_entrypoint(payload: dict[str, object], env: dict[str, str]) -> subprocess.CompletedProcess[str]: | ||
| merged_env = os.environ.copy() | ||
| merged_env.update(env) | ||
| return subprocess.run( | ||
| [sys.executable, ENTRYPOINT], | ||
| input=json.dumps(payload), | ||
| capture_output=True, | ||
| text=True, | ||
| env=merged_env, | ||
| ) | ||
|
|
||
|
|
||
| class SdkModeTest(unittest.TestCase): | ||
| def test_mode_override_warn_changes_block_to_warning(self) -> None: | ||
| path = write_transcript([human("can you make all tasks use claude and local executor")]) | ||
| try: | ||
| result = run_entrypoint( | ||
| { | ||
| "tool_name": "Bash", | ||
| "hook_event_name": "PreToolUse", | ||
| "transcript_path": path, | ||
| "tool_input": {"command": fixture("update_tasks_status_in_pending_queued.txt")}, | ||
| }, | ||
| {"CATSTACK_HOOK_MODE_CATEGORICAL_SCOPE_GUARD": "warn"}, | ||
| ) | ||
| finally: | ||
| os.unlink(path) | ||
|
|
||
| self.assertEqual(0, result.returncode, result.stderr) | ||
| self.assertEqual("", result.stderr) | ||
| rendered = json.loads(result.stdout) | ||
| self.assertIn( | ||
| "categorical-scope-guard", | ||
| rendered["hookSpecificOutput"]["additionalContext"], | ||
| ) | ||
|
|
||
| def test_each_finding_writes_one_event_row_with_rule_id(self) -> None: | ||
| path = write_transcript([human("can you make all tasks use claude and local executor")]) | ||
| try: | ||
| with tempfile.TemporaryDirectory() as tmp: | ||
| result = run_entrypoint( | ||
| { | ||
| "tool_name": "Bash", | ||
| "hook_event_name": "PreToolUse", | ||
| "session_id": "categorical-scope-guard-sdk-mode", | ||
| "transcript_path": path, | ||
| "tool_input": {"command": fixture("update_tasks_status_in_pending_queued.txt")}, | ||
| }, | ||
| { | ||
| "CATSTACK_HOOK_METRICS_DIR": tmp, | ||
| "CATSTACK_HOOK_MODE_CATEGORICAL_SCOPE_GUARD": "warn", | ||
| }, | ||
| ) | ||
| rows = [ | ||
| json.loads(line) | ||
| for file in Path(tmp).glob("events-*.jsonl") | ||
| for line in file.read_text(encoding="utf-8").splitlines() | ||
| ] | ||
| finally: | ||
| os.unlink(path) | ||
|
|
||
| self.assertEqual(0, result.returncode, result.stderr) | ||
| self.assertEqual(1, len(rows)) | ||
| self.assertEqual("categorical-scope-guard", rows[0]["hook"]) | ||
| self.assertEqual("categorical-scope-guard.partial-status-filter", rows[0]["rule_id"]) | ||
| self.assertEqual("warn", rows[0]["mode"]) | ||
| self.assertEqual("override", rows[0]["mode_source"]) | ||
| self.assertEqual("warned", rows[0]["action"]) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Detector prints crash line with finding
Low Severity
When
decide_payloadraises,detectwrites acatstack-hook-errorline to stderr and still returns an unchecked finding. Shared runtime already records and renders that finding, so the extra line prefixes the Claude block reason and makes the runner treat a warn-mode result ascaught_error.Reviewed by Cursor Bugbot for commit 5bcd8b5. Configure here.