Skip to content

feat(hooks): block spurious new example .ino creation - #2537

Merged
zackees merged 4 commits into
masterfrom
feat/hook-block-new-example-ino
May 29, 2026
Merged

zackees merged 4 commits into
masterfrom
feat/hook-block-new-example-ino

Conversation

@zackees

@zackees zackees commented May 29, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Add ci/hooks/protect_example_ino.py, a PreToolUse Write hook that blocks creating a new .ino file anywhere under examples/. Editing an existing .ino is always allowed.
  • Wire it into .claude/settings.json under the Write matcher (same pyproject-root/conflict-marker wrapper as the other hooks).
  • Document the policy in CLAUDE.md: keep examples/ minimal; test new functionality in examples/AutoResearch/AutoResearch.ino.

Why

Agents tend to spin up one-off .ino sketches 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 by protect_platform_headers.py and check_forbidden_commands.py. To force creation of a genuinely-new example:

  • Prepend a comment containing FL_AGENT_ALLOW_NEW_EXAMPLE to the file content (e.g. // FL_AGENT_ALLOW_NEW_EXAMPLE), or
  • Launch with env var FL_AGENT_ALLOW_NEW_EXAMPLE=1.

Test plan

  • New .ino under examples/, no directive → blocked (exit 2) with guidance
  • New .ino with in-content directive → allowed
  • New .ino with env-var override → allowed
  • Overwrite of existing .ino (e.g. AutoResearch) → allowed
  • New non-.ino file under examples/ → allowed
  • .ino outside examples/ → allowed
  • bash lint clean; .claude/settings.json valid JSON with hook wired

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Documentation

    • Added an examples management policy describing a minimal canonical examples set and rules for editing examples.
  • Chores

    • Added a pre-write validation hook (configured in settings) that blocks creation of new example files under examples/ unless explicitly overridden via an environment flag or in-file directive; edits to existing examples remain allowed.

Review Change Stack

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]>
@coderabbitai

coderabbitai Bot commented May 29, 2026 •

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: dc7ba6e0-9ddd-4b36-8dd2-cf6f213751b5

📥 Commits

Reviewing files that changed from the base of the PR and between eeb7c98 and 40f11af.

📒 Files selected for processing (1)
  • CLAUDE.md
✅ Files skipped from review due to trivial changes (1)
  • CLAUDE.md

📝 Walkthrough

Walkthrough

This PR enforces an Examples Policy by adding a PreToolUse hook that blocks creation of new .ino files under examples/. The hook is configured in .claude/settings.json, documented in CLAUDE.md, and implemented as a Python script that parses Write-tool input, allows overrides via environment variable or directive, and permits edits to existing files.

Changes

Examples Policy and Hook Protection

Layer / File(s) Summary
Examples Policy and Hook Configuration
CLAUDE.md, .claude/settings.json
Defines the Examples Policy (minimize examples, use canonical scratch target, block new .ino creation via hook, allow override via FL_AGENT_ALLOW_NEW_EXAMPLE) and configures the PreToolUse Write hook with a pyproject.toml presence guard and 10-second timeout.
Hook Implementation – Input Parsing, Overrides, and File Protection
ci/hooks/protect_example_ino.py
Parses stdin JSON from Write tool calls, checks environment variable and in-content directive overrides, normalizes paths and scopes enforcement to examples/*.ino, checks filesystem to allow edits but block new files, and reports blocking errors to stderr with exit code 2.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • FastLED/FastLED#2295: The PreToolUse hook relies on the same pyproject.toml presence + non-blocking skip pattern referenced in that PR.

Poem

🐰 I nibble on code and hop through the tree,
A gentle guard keeps wild examples tidy for me.
New .ino sprouts are paused at the gate,
Whisper the secret flag or directive to create.
Hop, guard, and hum — neat examples we see.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main feature: blocking the creation of spurious new example .ino files, which is the primary objective of this PR.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/hook-block-new-example-ino

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 5ba7e6b and e0db31f.

📒 Files selected for processing (3)
  • .claude/settings.json
  • CLAUDE.md
  • ci/hooks/protect_example_ino.py

Comment thread ci/hooks/protect_example_ino.py Resolved
Comment thread ci/hooks/protect_example_ino.py Resolved
zackees and others added 2 commits May 29, 2026 13:03
…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]>
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