Skip to content

feat: add Cursor hooks support to install command#196

Open
imkarrer wants to merge 2 commits intotirth8205:mainfrom
imkarrer:feature/cursor-hooks
Open

feat: add Cursor hooks support to install command#196
imkarrer wants to merge 2 commits intotirth8205:mainfrom
imkarrer:feature/cursor-hooks

Conversation

@imkarrer
Copy link
Copy Markdown

@imkarrer imkarrer commented Apr 9, 2026

Summary

  • Adds user-level Cursor hooks (~/.cursor/hooks.json + shell scripts in ~/.cursor/hooks/) that mirror all three Claude Code hook behaviors: auto-update graph after file edits, show graph status on session start, and detect changes before git commits
  • All hooks fail gracefully (exit 0) since they're user-level and run for all projects, including those without code-review-graph installed
  • Hooks are installed automatically when running install/init with --platform cursor or --platform all, unless --no-hooks is passed

Related Issues & PRs

Changes

code_review_graph/skills.py

  • generate_cursor_hooks_config() — returns Cursor hooks.json config dict (version 1) with hooks for afterFileEdit, sessionStart, and beforeShellExecution
  • _cursor_hook_scripts() — returns 3 shell script contents (crg-update.sh, crg-session-start.sh, crg-pre-commit.sh) that consume stdin per Cursor protocol and emit JSON on stdout
  • install_cursor_hooks() — writes ~/.cursor/hooks.json (merging with existing config), creates ~/.cursor/hooks/ with executable scripts

code_review_graph/cli.py

  • Calls install_cursor_hooks() in _handle_init() when target is all or cursor, gated by --no-hooks
  • Added logger module-level variable for consistent logging

tests/test_skills.py

  • 18 new tests across 3 test classes:
    • TestCursorHooksConfig (6 tests): config structure, event types, paths
    • TestCursorHookScripts (7 tests): script content, shebang, exit codes, commands
    • TestInstallCursorHooks (5 tests): file creation, executability, merge, idempotency, error handling

Hook Behaviors

Cursor Event Script Command
afterFileEdit crg-update.sh code-review-graph update --skip-flows
sessionStart crg-session-start.sh code-review-graph status
beforeShellExecution (matcher: ^git\s+commit) crg-pre-commit.sh code-review-graph detect-changes --brief

Testing

All 48 tests pass (30 existing + 18 new). Ruff and mypy clean.

Checklist (per CONTRIBUTING.md)

  • Feature branch: feature/cursor-hooks
  • Tests added for new functionality (18 new tests)
  • All tests pass: uv run pytest tests/test_skills.py (48/48)
  • Linting passes: ruff check with rules E, F, I, N, W
  • Type checking passes: mypy --ignore-missing-imports --no-strict-optional
  • Line length ≤ 100 characters
  • Python 3.10+ compatible

Isaac Karrer added 2 commits April 9, 2026 15:21
Install user-level Cursor hooks (~/.cursor/hooks.json + shell scripts)
that mirror the three Claude Code hook behaviors:

1. afterFileEdit: auto-update graph via code-review-graph update --skip-flows
2. sessionStart: show graph status via code-review-graph status
3. beforeShellExecution: detect changes before git commit

All hooks fail gracefully (exit 0) since they're user-level and will
run for all projects. Hooks are installed when running install/init
with --platform cursor or --platform all, unless --no-hooks is passed.

New functions in skills.py:
- generate_cursor_hooks_config(): returns hooks.json config dict
- _cursor_hook_scripts(): returns 3 shell script contents
- install_cursor_hooks(): writes config + scripts, merges with existing

18 new tests covering config generation, script content, installation,
merge behavior, idempotency, and error handling.
Match MCP behavior: only install Cursor hooks when ~/.cursor exists,
consistent with how PLATFORMS['cursor']['detect'] gates MCP config.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant