Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,32 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.99] - 2026-01-08

### Added
- **AI Triage Logging & Verification** - Enhanced `ai-triage.py` with comprehensive logging and post-write verification
- Added detailed progress logging (input file, findings count, classification breakdown, confidence level)
- Added post-write verification to ensure `ai_triage` data persists correctly in JSON
- Added regression test (`test-ai-triage-simple.sh`) to verify AI triage functionality
- **Impact:** Easier debugging and guaranteed data integrity for AI triage operations
- **Affected File:** `dist/bin/ai-triage.py`
- **Test Status:** ✅ Verified with smoke test - 7 findings triaged successfully

### Changed
- **AI Triage Logging to stderr** - All `[AI Triage]` log messages now output to stderr instead of stdout
- **Rationale:** Prevents potential JSON output corruption when piping stdout (follows same pattern as main scanner)
- **Impact:** Safe to pipe stdout without mixing log messages with data output
- **Affected File:** `dist/bin/ai-triage.py` (all print statements now use `file=sys.stderr`)
- **Test Status:** ✅ Verified stdout is clean when stderr redirected to /dev/null
- **AI Triage Schema Consistency** - Duplicated `findings_reviewed` into `ai_triage.summary` for convenience
- **Rationale:** Prevents future schema mismatches (similar to bug fixed in 1.0.98); keeps all summary stats in one place
- **Schema:** Now stored in both `ai_triage.scope.findings_reviewed` (original) and `ai_triage.summary.findings_reviewed` (new)
- **HTML Generator:** Updated to read from summary first, with fallback to scope for backward compatibility
- **Impact:** More consistent schema, fewer future breakages when accessing summary statistics
- **Affected Files:** `dist/bin/ai-triage.py`, `dist/bin/json-to-html.py`
- **Test Status:** ✅ Verified both locations contain same value (5 findings reviewed)
- **Version:** Bumped to 1.0.99

## [1.0.98] - 2026-01-08

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion dist/PATTERN-LIBRARY.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version": "1.0.0",
"generated": "2026-01-08T02:52:09Z",
"generated": "2026-01-08T03:41:27Z",
"summary": {
"total_patterns": 26,
"enabled": 26,
Expand Down
4 changes: 2 additions & 2 deletions dist/PATTERN-LIBRARY.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Pattern Library Registry

**Auto-generated by Pattern Library Manager**
**Last Updated:** 2026-01-08 02:52:09 UTC
**Last Updated:** 2026-01-08 03:41:27 UTC

---

Expand Down Expand Up @@ -114,6 +114,6 @@

---

**Generated:** 2026-01-08 02:52:09 UTC
**Generated:** 2026-01-08 03:41:27 UTC
**Version:** 1.0.0
**Tool:** Pattern Library Manager
59 changes: 59 additions & 0 deletions dist/bin/ai-triage.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import argparse
import json
import sys
from collections import Counter, defaultdict
from datetime import datetime, timezone
from pathlib import Path
Expand Down Expand Up @@ -271,8 +272,11 @@ def main() -> int:
ap.add_argument('--max-findings', type=int, default=200, help='Max findings to triage (keeps report manageable).')
args = ap.parse_args()

print(f"[AI Triage] Reading JSON log: {args.json_path}", file=sys.stderr)
data = json.loads(args.json_path.read_text(encoding='utf-8'))
findings: List[Dict[str, Any]] = data.get('findings') or []
print(f"[AI Triage] Total findings in log: {len(findings)}", file=sys.stderr)
print(f"[AI Triage] Max findings to review: {args.max_findings}", file=sys.stderr)

triaged_items: List[Dict[str, Any]] = []
counts = Counter()
Expand Down Expand Up @@ -305,6 +309,8 @@ def main() -> int:
}
)

print(f"[AI Triage] Findings reviewed: {reviewed}", file=sys.stderr)

# Infer overall confidence from distribution.
overall_conf = 'medium'
if reviewed:
Expand All @@ -315,6 +321,12 @@ def main() -> int:
elif low_ratio >= 0.4:
overall_conf = 'low'

print(f"[AI Triage] Classification breakdown:", file=sys.stderr)
print(f" - Confirmed Issues: {counts.get('Confirmed', 0)}", file=sys.stderr)
print(f" - False Positives: {counts.get('False Positive', 0)}", file=sys.stderr)
print(f" - Needs Review: {counts.get('Needs Review', 0)}", file=sys.stderr)
print(f"[AI Triage] Overall confidence: {overall_conf}", file=sys.stderr)

# Minimal executive summary tailored to what we observed in the sample.
narrative_parts = []
narrative_parts.append(
Expand Down Expand Up @@ -344,6 +356,7 @@ def main() -> int:
'findings_reviewed': reviewed,
},
'summary': {
'findings_reviewed': reviewed, # Duplicated for convenience/back-compat
'confirmed_issues': counts.get('Confirmed', 0),
'false_positives': counts.get('False Positive', 0),
'needs_review': counts.get('Needs Review', 0),
Expand All @@ -354,7 +367,53 @@ def main() -> int:
'triaged_findings': triaged_items,
}

print(f"[AI Triage] Writing updated JSON to: {args.json_path}", file=sys.stderr)
args.json_path.write_text(json.dumps(data, indent=2, ensure_ascii=False) + '\n', encoding='utf-8')

# Verify write was successful
file_size = args.json_path.stat().st_size
print(f"[AI Triage] ✅ Successfully wrote {file_size:,} bytes", file=sys.stderr)
print(f"[AI Triage] Triage data injected with {len(triaged_items)} triaged findings", file=sys.stderr)

# Post-write verification: re-open and assert ai_triage exists
print(f"[AI Triage] Verifying write integrity...", file=sys.stderr)
try:
verification_data = json.loads(args.json_path.read_text(encoding='utf-8'))

# Check that ai_triage key exists
if 'ai_triage' not in verification_data:
print(f"[AI Triage] ❌ VERIFICATION FAILED: 'ai_triage' key not found in written JSON", file=sys.stderr)
return 1

# Check that ai_triage.performed is True
if not verification_data.get('ai_triage', {}).get('performed'):
print(f"[AI Triage] ❌ VERIFICATION FAILED: 'ai_triage.performed' is not True", file=sys.stderr)
return 1

# Check that triaged_findings count matches
written_count = len(verification_data.get('ai_triage', {}).get('triaged_findings', []))
if written_count != len(triaged_items):
print(f"[AI Triage] ❌ VERIFICATION FAILED: Expected {len(triaged_items)} triaged findings, found {written_count}", file=sys.stderr)
return 1

# Check that summary exists and has expected keys
summary = verification_data.get('ai_triage', {}).get('summary', {})
required_keys = ['findings_reviewed', 'confirmed_issues', 'false_positives', 'needs_review', 'confidence_level']
missing_keys = [k for k in required_keys if k not in summary]
if missing_keys:
print(f"[AI Triage] ❌ VERIFICATION FAILED: Missing summary keys: {missing_keys}", file=sys.stderr)
return 1

print(f"[AI Triage] ✅ Verification passed: ai_triage data is intact", file=sys.stderr)
print(f"[AI Triage] ✅ Confirmed {written_count} triaged findings persisted", file=sys.stderr)

except json.JSONDecodeError as e:
print(f"[AI Triage] ❌ VERIFICATION FAILED: Written JSON is invalid: {e}", file=sys.stderr)
return 1
except Exception as e:
print(f"[AI Triage] ❌ VERIFICATION FAILED: Unexpected error: {e}", file=sys.stderr)
return 1

return 0


Expand Down
2 changes: 1 addition & 1 deletion dist/bin/check-performance.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash
#
# WP Code Check by Hypercart - Performance Analysis Script
# Version: 1.0.94
# Version: 1.0.99
#
# Fast, zero-dependency WordPress performance analyzer
# Catches critical issues before they crash your site
Expand Down
9 changes: 6 additions & 3 deletions dist/bin/json-to-html.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,12 @@ def main():

if ai_triage_performed:
# Build summary stats
# Note: findings_reviewed is in ai_triage['scope'], not in summary
ai_triage_scope = ai_triage.get('scope', {})
findings_reviewed = ai_triage_scope.get('findings_reviewed', 0)
# Note: findings_reviewed is duplicated in both summary and scope for convenience
# Try summary first (new location), fall back to scope (old location) for back-compat
findings_reviewed = ai_triage_summary.get('findings_reviewed')
if findings_reviewed is None:
ai_triage_scope = ai_triage.get('scope', {})
findings_reviewed = ai_triage_scope.get('findings_reviewed', 0)
confirmed_issues = ai_triage_summary.get('confirmed_issues', 0)
false_positives = ai_triage_summary.get('false_positives', 0)
needs_review = ai_triage_summary.get('needs_review', 0)
Expand Down
86 changes: 86 additions & 0 deletions dist/tests/test-ai-triage-simple.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/usr/bin/env bash
#
# WP Code Check - AI Triage Simple Smoke Test
# Version: 1.0.0
#
# Simple regression test for ai-triage.py to ensure it writes ai_triage data.
#
# Usage:
# ./test-ai-triage-simple.sh

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BIN_DIR="$SCRIPT_DIR/../bin"
FIXTURES_DIR="$SCRIPT_DIR/fixtures"
TEMP_JSON=$(mktemp)

echo "================================================"
echo " WP Code Check - AI Triage Simple Smoke Test"
echo "================================================"
echo ""

# Step 1: Generate a scan log
echo "Step 1: Generating scan log..."
"$BIN_DIR/check-performance.sh" \
--paths "$FIXTURES_DIR/antipatterns.php" \
--format json \
--no-log 2>&1 | sed -n '/^{/,/^}$/p' > "$TEMP_JSON"

if [ ! -s "$TEMP_JSON" ]; then
echo "✗ FAIL: Could not generate JSON log"
rm -f "$TEMP_JSON"
exit 1
fi

echo "✓ Generated JSON log"

# Step 2: Run AI triage
echo "Step 2: Running AI triage..."
python3 "$BIN_DIR/ai-triage.py" "$TEMP_JSON" --max-findings 50 > /dev/null 2>&1

if [ $? -ne 0 ]; then
echo "✗ FAIL: ai-triage.py exited with non-zero code"
rm -f "$TEMP_JSON"
exit 1
fi

echo "✓ AI triage completed"

# Step 3: Verify ai_triage exists
echo "Step 3: Verifying ai_triage data..."

if command -v jq &> /dev/null; then
HAS_AI_TRIAGE=$(jq 'has("ai_triage")' "$TEMP_JSON" 2>/dev/null)
if [ "$HAS_AI_TRIAGE" != "true" ]; then
echo "✗ FAIL: ai_triage key not found in JSON"
rm -f "$TEMP_JSON"
exit 1
fi

PERFORMED=$(jq -r '.ai_triage.performed' "$TEMP_JSON" 2>/dev/null)
if [ "$PERFORMED" != "true" ]; then
echo "✗ FAIL: ai_triage.performed is not true"
rm -f "$TEMP_JSON"
exit 1
fi

TRIAGED_COUNT=$(jq '.ai_triage.triaged_findings | length' "$TEMP_JSON" 2>/dev/null)
echo "✓ ai_triage data verified ($TRIAGED_COUNT findings triaged)"
else
if grep -q '"ai_triage"' "$TEMP_JSON" 2>/dev/null && grep -q '"performed": true' "$TEMP_JSON" 2>/dev/null; then
echo "✓ ai_triage data verified (jq not available, used grep)"
else
echo "✗ FAIL: ai_triage data not found"
rm -f "$TEMP_JSON"
exit 1
fi
fi

# Cleanup
rm -f "$TEMP_JSON"

echo ""
echo "================================================"
echo "✓ All tests passed!"
echo "================================================"
exit 0

Loading
Loading