Skip to content

Commit 05b7ee2

Browse files
hah23255claude
andcommitted
Initial commit: Silent Alarm Detector v1.0.0
🚨 LLM Alarm-Silencing Pattern Detector Features: - 8 pattern detectors with 60+ indicators - Impact scoring (Performance/Security/Maintainability) - Claude Code hook integration - Pre-Tool-Use blocking of critical issues - Comprehensive documentation (9,000+ words) - CI/CD workflows with automated testing - Research-backed detection (2025 studies) Detects: - Silent exceptions (except: pass) - Warning suppression - Missing validation - Code duplication - Performance degradation (O(n²)) - Security shortcuts (SQL injection, eval()) - Error masking - Test avoidance Based on research showing: - 19% developer productivity decrease - 73% AI-startup failure rate - 8x increase in code duplication - 40% AI suggestions contain vulnerabilities Prevents 'minor' issues from becoming major disasters. Co-Authored-By: Claude <noreply@anthropic.com>
0 parents  commit 05b7ee2

19 files changed

+4520
-0
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Silent Alarm Detector Hook (Pre-Tool-Use)
4+
Detects when LLM is about to silence alarms or bypass "minor" issues.
5+
Integrates with Claude Code hooks system.
6+
"""
7+
8+
import sys
9+
import json
10+
import os
11+
from pathlib import Path
12+
from datetime import datetime
13+
14+
# Add analyzers to path
15+
hook_dir = Path(__file__).parent.parent.parent
16+
sys.path.insert(0, str(hook_dir))
17+
18+
try:
19+
from analyzers.pattern_detector import SilentAlarmDetector, Severity
20+
from analyzers.impact_assessor import ImpactAssessor
21+
except ImportError as e:
22+
print(f"⚠️ Silent Alarm Detector: Module import failed: {e}", file=sys.stderr)
23+
sys.exit(0) # Allow on import error
24+
25+
26+
def extract_code_from_tool_input(tool_input: dict) -> str:
27+
"""Extract code content from tool input."""
28+
code_content = ""
29+
30+
# Check for code in various tool input fields
31+
if 'content' in tool_input:
32+
code_content = tool_input['content']
33+
elif 'new_string' in tool_input: # Edit tool
34+
code_content = tool_input['new_string']
35+
elif 'command' in tool_input: # Bash tool
36+
code_content = tool_input['command']
37+
38+
return code_content
39+
40+
41+
def should_analyze(tool_name: str, tool_input: dict) -> bool:
42+
"""Determine if we should analyze this tool use."""
43+
# Analyze Write, Edit, and potentially Bash tools
44+
code_tools = ['Write', 'Edit', 'Bash']
45+
46+
if tool_name not in code_tools:
47+
return False
48+
49+
# For Bash, only analyze if it contains code patterns
50+
if tool_name == 'Bash':
51+
command = tool_input.get('command', '')
52+
# Only analyze if bash contains Python/code patterns
53+
if not any(keyword in command for keyword in ['python', 'def ', 'class ', 'import ']):
54+
return False
55+
56+
return True
57+
58+
59+
def log_detection(detections: list, impact_score: dict):
60+
"""Log detection to history file."""
61+
log_file = hook_dir / 'data' / 'detection_history.jsonl'
62+
log_file.parent.mkdir(exist_ok=True)
63+
64+
log_entry = {
65+
'timestamp': datetime.now().isoformat(),
66+
'num_detections': len(detections),
67+
'impact_score': impact_score,
68+
'detections': [
69+
{
70+
'pattern': d.pattern_type.value,
71+
'severity': d.severity.value,
72+
'line': d.line_number,
73+
'description': d.description
74+
}
75+
for d in detections
76+
]
77+
}
78+
79+
with open(log_file, 'a') as f:
80+
f.write(json.dumps(log_entry) + '\n')
81+
82+
83+
def main():
84+
"""Main hook entry point."""
85+
try:
86+
# Read hook input from stdin
87+
hook_input = json.load(sys.stdin)
88+
tool_name = hook_input.get('tool_name', '')
89+
tool_input = hook_input.get('tool_input', {})
90+
91+
# Check if we should analyze this tool use
92+
if not should_analyze(tool_name, tool_input):
93+
sys.exit(0) # Pass through
94+
95+
# Extract code
96+
code = extract_code_from_tool_input(tool_input)
97+
if not code or len(code) < 20: # Skip trivial code
98+
sys.exit(0)
99+
100+
# Analyze for alarm-silencing patterns
101+
detector = SilentAlarmDetector()
102+
detections = detector.analyze_code(code)
103+
104+
if not detections:
105+
sys.exit(0) # No issues found, pass through
106+
107+
# Assess impact
108+
assessor = ImpactAssessor()
109+
impact = assessor.assess_impact(detections)
110+
111+
# Log detection
112+
log_detection(detections, {
113+
'total_score': impact.total_score,
114+
'risk_level': impact.risk_level,
115+
'performance_cost': impact.performance_cost,
116+
'security_risk': impact.security_risk
117+
})
118+
119+
# Determine if we should block or warn
120+
critical_count = sum(1 for d in detections if d.severity == Severity.CRITICAL)
121+
122+
if critical_count > 0 or impact.risk_level == "CRITICAL":
123+
# BLOCK: Critical alarm-silencing detected
124+
print("🚨 CRITICAL ALARM-SILENCING DETECTED!", file=sys.stderr)
125+
print("", file=sys.stderr)
126+
print(f"Found {len(detections)} pattern(s) where LLM is silencing alarms:", file=sys.stderr)
127+
print("", file=sys.stderr)
128+
129+
for d in detections:
130+
if d.severity == Severity.CRITICAL:
131+
print(f"❌ Line {d.line_number}: {d.description}", file=sys.stderr)
132+
print(f" Impact: {d.impact}", file=sys.stderr)
133+
print(f" Fix: {d.recommendation}", file=sys.stderr)
134+
print("", file=sys.stderr)
135+
136+
print(f"🎯 Risk Level: {impact.risk_level}", file=sys.stderr)
137+
print(f"📊 Impact Score: {impact.total_score}/100", file=sys.stderr)
138+
print("", file=sys.stderr)
139+
print("⚠️ These 'minor' issues will have CRUSHING impact on production!", file=sys.stderr)
140+
print(" Please fix before proceeding.", file=sys.stderr)
141+
142+
sys.exit(2) # Block the tool use
143+
144+
elif impact.risk_level in ["HIGH", "MEDIUM"]:
145+
# WARN: Non-critical but significant issues
146+
print(f"⚠️ Alarm-Silencing Warning ({len(detections)} patterns)", file=sys.stderr)
147+
print(f" Risk: {impact.risk_level} | Impact: {impact.total_score}/100", file=sys.stderr)
148+
149+
# Show top 2 issues
150+
sorted_detections = sorted(detections, key=lambda d: d.severity.value, reverse=True)
151+
for d in sorted_detections[:2]:
152+
print(f" • Line {d.line_number}: {d.description}", file=sys.stderr)
153+
154+
print(f" ⏱️ Est. debug time if these cause issues: {impact.estimated_debug_hours}h", file=sys.stderr)
155+
156+
sys.exit(0) # Warn but allow
157+
158+
else:
159+
# INFO: Minor issues
160+
print(f"💡 {len(detections)} minor alarm-silencing pattern(s) detected", file=sys.stderr)
161+
sys.exit(0) # Allow
162+
163+
except Exception as e:
164+
# Never block on hook errors
165+
print(f"⚠️ Silent Alarm Detector error: {e}", file=sys.stderr)
166+
sys.exit(0)
167+
168+
169+
if __name__ == "__main__":
170+
main()
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
name: Bug Report
3+
about: Report a bug or issue with Silent Alarm Detector
4+
title: '[BUG] '
5+
labels: bug
6+
assignees: ''
7+
---
8+
9+
## 🐛 Bug Description
10+
11+
A clear and concise description of what the bug is.
12+
13+
## 📋 Steps to Reproduce
14+
15+
1. Install hook with...
16+
2. Run Claude Code...
17+
3. Execute command...
18+
4. See error...
19+
20+
## ✅ Expected Behavior
21+
22+
What you expected to happen.
23+
24+
## ❌ Actual Behavior
25+
26+
What actually happened.
27+
28+
## 📊 Environment
29+
30+
- **Python Version:** (run `python3 --version`)
31+
- **OS:** (e.g., Ubuntu 22.04, macOS 13, Windows 11)
32+
- **Claude Code Version:** (if known)
33+
- **Hook Version:** (from CHANGELOG.md or git tag)
34+
35+
## 📝 Logs
36+
37+
```
38+
Paste relevant logs here from:
39+
- ~/.claude/hooks/silent-alarm-detector/data/detection_history.jsonl
40+
- ~/.claude/debug/*.txt
41+
- Error messages from terminal
42+
```
43+
44+
## 💻 Code Sample
45+
46+
```python
47+
# Minimal code that reproduces the issue
48+
```
49+
50+
## 🔍 Additional Context
51+
52+
Add any other context about the problem here:
53+
- Screenshots
54+
- Related issues
55+
- Workarounds tried
56+
- Configuration changes
57+
58+
## ✨ Possible Solution
59+
60+
(Optional) If you have ideas on how to fix this.
61+
62+
---
63+
64+
**Checklist:**
65+
- [ ] I searched existing issues before creating this one
66+
- [ ] I tested with the latest version
67+
- [ ] I provided all requested information above
68+
- [ ] I included logs and code samples
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
name: Feature Request
3+
about: Suggest a new feature or enhancement
4+
title: '[FEATURE] '
5+
labels: enhancement
6+
assignees: ''
7+
---
8+
9+
## 💡 Feature Description
10+
11+
A clear and concise description of the feature you'd like to see.
12+
13+
## 🎯 Problem Statement
14+
15+
What problem does this feature solve? What pain point does it address?
16+
17+
**Example:**
18+
"Currently when X happens, I have to manually Y, which is tedious because..."
19+
20+
## ✨ Proposed Solution
21+
22+
Describe how you envision this feature working.
23+
24+
**Example:**
25+
"I'd like the hook to automatically detect Z pattern and suggest..."
26+
27+
## 🔄 Alternatives Considered
28+
29+
Have you considered any alternative solutions or features?
30+
31+
## 📊 Use Cases
32+
33+
Describe specific scenarios where this feature would be useful:
34+
35+
1. **Use Case 1:** ...
36+
2. **Use Case 2:** ...
37+
3. **Use Case 3:** ...
38+
39+
## 🎨 Mockups / Examples
40+
41+
(Optional) Provide mockups, code examples, or screenshots showing how this might look:
42+
43+
```python
44+
# Example of how the feature might work
45+
```
46+
47+
## 🚀 Priority
48+
49+
How important is this feature to you?
50+
51+
- [ ] Critical - Blocking my workflow
52+
- [ ] High - Would significantly improve my experience
53+
- [ ] Medium - Nice to have
54+
- [ ] Low - Just an idea
55+
56+
## 🤝 Contribution
57+
58+
Would you be willing to help implement this feature?
59+
60+
- [ ] Yes, I can submit a PR
61+
- [ ] Yes, I can help with testing
62+
- [ ] Yes, I can help with documentation
63+
- [ ] No, but I'd be happy to provide feedback
64+
65+
## 📚 Additional Context
66+
67+
Add any other context, links to research, or related features from other tools.
68+
69+
---
70+
71+
**Checklist:**
72+
- [ ] I searched existing issues/PRs for similar requests
73+
- [ ] I provided clear use cases
74+
- [ ] I explained why this would be valuable
75+
- [ ] I considered implementation complexity

0 commit comments

Comments
 (0)