Repository navigation
feat(hooks): block spurious new example .ino creation - #2537
Conversation
Add a PreToolUse Write hook that prevents agents from creating new one-off .ino sketches under examples/, keeping the examples tree minimal. New functionality should be tested in the canonical scratch sketch examples/AutoResearch/AutoResearch.ino instead. Editing existing .ino files is always allowed. Creation can be forced with the FL_AGENT_ALLOW_NEW_EXAMPLE directive (in-content comment or env var), matching the override convention of the other FastLED hooks. Co-Authored-By: Claude Opus 4.7 <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
✅ Files skipped from review due to trivial changes (1)
📝 WalkthroughWalkthroughThis PR enforces an Examples Policy by adding a PreToolUse hook that blocks creation of new ChangesExamples Policy and Hook Protection
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@ci/hooks/protect_example_ino.py`:
- Around line 53-55: The current check returns 0 if OVERRIDE_TOKEN appears
anywhere in content; change it so the override only applies when the token is
present as a leading comment (first non-empty line) or via env var.
Specifically, in the code path using tool_input and content, replace the
substring check (if OVERRIDE_TOKEN in content) with a check that the first
non-blank line startswith OVERRIDE_TOKEN (e.g., inspect content.splitlines() and
strip the first non-empty line or use a regex like r'^\s*' +
re.escape(OVERRIDE_TOKEN)). Also ensure the existing environment-based override
(FL_AGENT_ALLOW_NEW_EXAMPLE) is still respected by checking
os.environ.get("FL_AGENT_ALLOW_NEW_EXAMPLE") == "1".
- Around line 34-37: The try/except around json.load(sys.stdin) must explicitly
handle KeyboardInterrupt first and must fail fast on JSON decode errors instead
of returning 0; update the block that sets input_data to first except
KeyboardInterrupt and re-raise or call sys.exit(1) to abort cleanly, then catch
json.JSONDecodeError and call sys.exit(1) (or raise SystemExit) with a
descriptive stderr message so the enforcement hook cannot be bypassed; reference
the input_data assignment, json.load(sys.stdin), and the existing try/except
block when making this change.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 69149ae9-c137-4902-b87c-12a3b8b270b3
📒 Files selected for processing (3)
.claude/settings.jsonCLAUDE.mdci/hooks/protect_example_ino.py
…sistency Match the standardized constant name used by protect_platform_headers.py and check_forbidden_commands.py. No behavior change. Co-Authored-By: Claude Opus 4.7 <[email protected]>
- Fail fast (exit 2) with descriptive stderr on invalid hook payload instead of silently allowing the write; handle KeyboardInterrupt first per repo Python exception-handling standards. - Restrict in-content override to a leading comment containing the FL_AGENT_ALLOW_NEW_EXAMPLE token (first non-empty line must start with //), closing an accidental-bypass path from any substring match. Co-Authored-By: Claude Opus 4.7 <[email protected]>
Summary
ci/hooks/protect_example_ino.py, aPreToolUseWrite hook that blocks creating a new.inofile anywhere underexamples/. Editing an existing.inois always allowed..claude/settings.jsonunder theWritematcher (same pyproject-root/conflict-marker wrapper as the other hooks).CLAUDE.md: keepexamples/minimal; test new functionality inexamples/AutoResearch/AutoResearch.ino.Why
Agents tend to spin up one-off
.inosketches to try things out, bloating the examples tree. This adds friction so new functionality is exercised in the canonical scratch sketch instead.Override (FL_* directive)
Follows the existing
FL_AGENT_ALLOW_*convention used byprotect_platform_headers.pyandcheck_forbidden_commands.py. To force creation of a genuinely-new example:FL_AGENT_ALLOW_NEW_EXAMPLEto the file content (e.g.// FL_AGENT_ALLOW_NEW_EXAMPLE), orFL_AGENT_ALLOW_NEW_EXAMPLE=1.Test plan
.inounderexamples/, no directive → blocked (exit 2) with guidance.inowith in-content directive → allowed.inowith env-var override → allowed.ino(e.g. AutoResearch) → allowed.inofile underexamples/→ allowed.inooutsideexamples/→ allowedbash lintclean;.claude/settings.jsonvalid JSON with hook wired🤖 Generated with Claude Code
Summary by CodeRabbit
Documentation
Chores