Skip to content
Notifications
Clear all

Walkthrough: implementing a pre-commit hook that checks AI suggestions against your standards

3 Posts
3 Users
0 Reactions
3 Views
(@benchmark_bob_42)
Reputable Member
Joined: 3 months ago
Posts: 151
Topic starter   [#2872]

In our ongoing efforts to establish reproducible and high-quality code generation workflows, I've observed a critical gap: the lack of automated, pre-merge validation for AI-generated code suggestions. While we meticulously benchmark database throughput and query latency, we often treat AI-assisted code as a black box, accepting it on faith. This is antithetical to our principles of measurement and standardization.

I propose a concrete, implementable pre-commit hook that acts as a "linting suite" for AI-suggested code blocks. The goal is not to reject all AI code, but to ensure it adheres to our team's established coding standards, security baselines, and dependency policies *before* it enters the codebase. This transforms a subjective review process into a reproducible, automated check.

The core of the hook is a Python script that leverages existing linting and analysis tools, triggered specifically on files or diffs containing markers of AI origin (e.g., specific tags in comments). Below is a foundational implementation.

```python
#!/usr/bin/env python3
"""
pre-commit-ai-standards.py
A pre-commit hook to run standards checks on AI-suggested code.
Detects files with common AI attribution comments and runs a targeted battery of linters.
"""

import subprocess
import sys
import os

# Patterns in source that suggest AI-origin (customize this list)
AI_INDICATOR_PATTERNS = [
"Generated by",
"AI-assisted",
"@assist",
"# AI_SUGGESTION"
]

def file_contains_ai_indicator(filepath):
"""Heuristic check if file contains markers of AI-generated code."""
try:
with open(filepath, 'r') as f:
content = f.read()
for pattern in AI_INDICATOR_PATTERNS:
if pattern in content:
return True
except Exception:
pass
return False

def run_linter(command, filepath):
"""Run a linter command and return success status."""
try:
result = subprocess.run(
command + [filepath],
capture_output=True,
text=True
)
if result.returncode != 0:
print(f"FAIL: {command[0]} on {filepath}")
print(result.stdout)
print(result.stderr)
return False
return True
except FileNotFoundError:
print(f"WARN: Linter {command[0]} not installed.")
return True # Skip if linter missing

def main():
staged_files = sys.argv[1:] # Files passed by pre-commit
all_passed = True

for file in staged_files:
if not file_contains_ai_indicator(file):
continue # Skip files without AI markers

print(f"n🔍 AI-assisted code detected in {file}. Running standards checks...")

# Define your linter battery. These must be installed in your environment.
linters = [
["black", "--check"], # Code formatting
["ruff", "check"], # Python style & errors
["bandit", "-ll"], # Security linting
["vulture"], # Dead code detection (useful for hallucinated functions)
]

for linter in linters:
if not run_linter(linter, file):
all_passed = False

if not all_passed:
print("n❌ AI standards check failed. Please address linter warnings before committing.")
sys.exit(1)
else:
print("n✅ All AI standards checks passed.")
sys.exit(0)

if __name__ == "__main__":
main()
```

To integrate this into your pre-commit framework (`.pre-commit-config.yaml`), add the following entry:

```yaml
repos:
- repo: local
hooks:
- id: ai-standards-check
name: AI-Assisted Code Standards
entry: python3 ./scripts/pre-commit-ai-standards.py
language: system
types: [python] # Extend to [python, javascript, ...] as needed
args: [--files]
```

Key considerations and benchmark-like metrics to track:

* **False Positive Rate:** Tune your `AI_INDICATOR_PATTERNS` to minimize human-written code being flagged. Start broad and narrow down.
* **Check Latency:** The hook should complete in under 2-3 seconds to not disrupt workflow. Profile your linter battery.
* **Critical Rules:** The hook should enforce non-negotiable standards (e.g., no hardcoded credentials, approved license headers). These are "fail fast" conditions.
* **Advisory Rules:** Warnings for style deviations can be logged but should not necessarily block commits, depending on team policy.

This approach provides a measurable, repeatable process. The next step is to gather empirical data on its efficacy: how many potential issues are caught pre-merge versus discovered in later review or, worse, in production. I am currently running a longitudinal study on our team's repositories and will share the latency and catch-rate statistics in a follow-up post.

-- bb42


-- bb42


   
Quote
(@oliviar)
New Member
Joined: 1 week ago
Posts: 2
 

This is exactly the kind of thinking we need to get ahead of the curve. You're right to focus on reproducible automation over subjective review. The script framework you've started is the right first step.

One nuance I'd add from a security and compliance angle: the "markers of AI origin" detection. If we're relying on a comment tag like `# AI-SUGGESTION`, that creates an obvious workaround - someone could just strip the tag to bypass the check. The hook should be part of a broader policy that treats *any* code without a clear, auditable origin (like a known library or a manual write) as suspect. The real enforcement needs to be cultural and backed by policy, with the script as a technical control, not the sole gatekeeper.

Also, consider having the hook check for patterns that are *impossible* for a human to have authored in a reasonable timeframe, like a 50-line function added in a single commit with perfect syntax but no prior traces in the user's git history. That's a signal worth flagging.



   
ReplyQuote
(@ninap)
New Member
Joined: 1 week ago
Posts: 2
 

The impulse to automate validation is a good one, but you're starting at the wrong end of the pipeline. A pre-commit hook that relies on a voluntary "AI-SUGGESTION" comment tag is fundamentally a policy of trust, not verification. It assumes good faith from the developer, which is fine until you're dealing with a junior under pressure or a contractor optimizing for speed.

You're also adding a new layer of process to police a symptom, not the cause. The real question isn't how to lint AI code, it's why your team feels compelled to accept unvetted AI blocks as if they were library imports. The correct first step is a procurement and training decision that says "we don't paste from the AI unless it's been through the same rigor as a pull request from an intern."

If you must have a technical control, it belongs in the CI/CD system, not a local hook. Let the build break when a commit introduces new dependencies or security anti-patterns flagged by static analysis on the *entire* diff, regardless of its claimed origin. Otherwise you're just building a polite suggestion system that can be bypassed by deleting a comment.



   
ReplyQuote