Skip to main content

Defect Pattern Analysis

AI-generated code introduces specific, repeatable defect patterns that differ from those found in human-written code. By cataloging these patterns, understanding their root causes, and building detection mechanisms, you can shift from reactive defect fixing to proactive defect prevention. This section provides a comprehensive catalog of AI-specific defect categories, root cause analysis techniques, and strategies for systematic quality improvement.

AI-Specific Defect Categories

Category 1: Logic Correctness Defects

These are the most common and most dangerous AI-specific defects because they are the hardest to detect -- the code compiles, runs, and produces plausible but incorrect results.

PatternDescriptionExampleDetection Method
Off-by-one errorsAI frequently miscalculates loop boundaries, array indices, and range endpointsfor (i = 0; i <= arr.length; i++) instead of < arr.lengthBoundary value testing, property-based testing
Inverted conditionsBoolean conditions that are reversed, especially with negationif (!user.isActive) when the intent was if (user.isActive)Specification-based testing; test both branches
Incorrect operator precedenceMath or logic operations that evaluate in the wrong ordera + b * c when (a + b) * c was intendedUnit tests with known expected values
Stale pattern applicationCorrect pattern applied to wrong context, producing subtle errorsRepository pattern for a query that needs raw SQL for performanceArchitecture review, performance testing
Missing state transitionsState machine that handles some transitions but misses othersOrder goes from "shipped" to "cancelled" without "return initiated"State machine testing, specification-based testing
Truncation errorsFloating-point arithmetic that loses precision, especially in financial calculationsCurrency calculation using float instead of DecimalProperty-based testing with precision assertions

Category 2: Security Vulnerability Defects

AI-generated code has a 2.74x higher vulnerability rate. These are the most common security defect patterns per PRD-STD-005:

PatternCWEDescriptionDetection Method
SQL injectionCWE-89String concatenation in database queriesSAST rules, parameterized query enforcement
Cross-site scripting (XSS)CWE-79User input rendered without sanitizationSAST + DAST, output encoding enforcement
Broken authenticationCWE-287Missing or incorrect authentication checksIntegration tests for every endpoint, DAST
Broken access controlCWE-862Missing authorization after authenticationRole-based integration tests, DAST
Hardcoded credentialsCWE-798Secrets in source codeSecret scanning (pre-commit and CI)
Insecure deserializationCWE-502Deserializing untrusted data without validationSAST, manual review of deserialization code
SSRFCWE-918Server making requests to user-controlled URLsSAST, input validation testing
Path traversalCWE-22File access without path sanitizationSAST, fuzzing with ../ patterns

Category 3: Structural Defects

AI-generated code often has structural issues that do not cause immediate bugs but degrade maintainability and create future defect risk:

PatternDescriptionImpactDetection Method
Dead codeFunctions, variables, or branches that are never executedMaintenance burden, confusionStatic analysis, coverage analysis
Duplicate logicSame logic implemented differently in multiple placesInconsistent behavior when one copy is updatedDuplication analysis tools
Inappropriate couplingModules with hidden dependencies on each otherFragile changes, cascading failuresArchitecture testing, dependency analysis
Resource leaksConnections, file handles, or memory not properly releasedGradual performance degradation, crashesSAST, load testing, memory profiling
Race conditionsConcurrent access to shared state without synchronizationIntermittent failures, data corruptionConcurrency testing, stress testing, code review
N+1 queriesDatabase query inside a loop instead of batch queryPerformance degradation under loadQuery logging, performance testing

Category 4: Integration Defects

AI generates code in isolation and may produce components that work individually but fail when integrated:

PatternDescriptionDetection Method
API contract mismatchGenerated client does not match actual API specificationContract testing, integration tests
Data format assumptionsCode assumes date format, number format, or encoding that differs from actual dataIntegration tests with real data samples
Error contract mismatchError handling expects different error format than what upstream producesIntegration tests with error injection
Timeout/retry mismatchTimeout values that conflict with upstream service SLAsConfiguration review, chaos testing

Root Cause Analysis (RCA) Techniques

Technique 1: AI-Specific 5 Whys

Adapt the traditional 5 Whys technique with AI-specific follow-up questions:

  1. Why did the defect occur? (e.g., "The function does not handle null input")
  2. Why was the null handling missing? (e.g., "The AI-generated code did not include null checks")
  3. Why did the AI omit null checks? (e.g., "The prompt did not specify null handling as a constraint")
  4. Why was the prompt incomplete? (e.g., "The developer did not follow the prompt engineering guidelines")
  5. Why were the guidelines not followed? (e.g., "The developer was not trained on the new prompt template")

AI-specific root cause categories:

  • Prompt deficiency: The prompt did not specify the constraint
  • Review gap: The reviewer did not catch the issue
  • Test gap: No test covered this scenario
  • Tool limitation: The AI tool consistently fails at this pattern
  • Training gap: The developer lacked awareness of this risk

Technique 2: Defect Clustering Analysis

Group defects by their AI root cause to identify systemic issues:

ClusterIf FrequentAction
Same defect type, multiple developersTool limitation or team knowledge gapTraining session or tool evaluation
Same defect type, one developerIndividual skill gapTargeted coaching per Skill Development
Same defect type, one code areaCodebase area unsuitable for AIAdd to "low AI suitability" classification
Many defect types, one developerInsufficient review rigorProcess intervention per Performance Management
Security defects increasingSecurity awareness gapSecurity training refresh per Security Awareness

Technique 3: Before-After Comparison

Compare defect patterns before and after AI adoption to isolate AI-specific contributions:

MetricPre-AI BaselineCurrentDeltaInterpretation
Defects per sprint[X][Y][Y-X]Net defect change
Defects per KLOC[X][Y][Y-X]Normalized for code volume
Security findings per sprint[X][Y][Y-X]AI security impact
Defect severity distribution[%Critical/%High/%Med/%Low][current][shift]Severity pattern change
Time to detect[X days][Y days][Y-X]Detection efficiency

Building a Defect Pattern Database

Maintain an organizational defect pattern database that captures AI-specific patterns:

Database Schema

FieldDescriptionExample
Pattern IDUnique identifierADP-042
CategoryLogic / Security / Structural / IntegrationSecurity
Pattern nameDescriptive nameHardcoded API Key in AI-Generated Service
DescriptionDetailed description of the patternAI generates placeholder API keys that resemble real keys
Detection methodHow to find this patternSecret scanning with regex for sk-, pk_, AKIA patterns
Prevention methodHow to prevent this patternInclude "no hardcoded secrets" in all prompts; pre-commit hooks
First observedWhen this pattern was first seen2025-03-15
FrequencyHow often this pattern appears3-4 times per month
AI toolWhich AI tool generates this pattern[Tool name]
SeverityImpact severityHigh

Using the Database

  1. Onboarding. New developers review the top 20 defect patterns as part of onboarding.
  2. Code review. Reviewers reference the database when reviewing AI-generated code.
  3. Testing. Test suites include test cases specifically designed to catch cataloged patterns.
  4. Tool evaluation. When evaluating new AI tools per Tooling Decisions, test against the known defect pattern database.
  5. Trend analysis. Track pattern frequency over time. Declining frequency indicates effective prevention.

Reporting Defect Analysis

To Development Managers

Monthly report including:

  • Defect count by category (AI-specific vs. general)
  • Top 5 most frequent defect patterns this month
  • Trend comparison (this month vs. last 3 months)
  • Recommended actions (training, tool changes, process adjustments)

To CTO

Quarterly report including:

  • Security vulnerability trend with AI overlay
  • Defect density trend normalized by code volume
  • Defect pattern database highlights (new patterns, declining patterns)
  • Risk assessment update per Technical Risk Management

To Executive

Quarterly summary for Board-Ready Metrics:

  • Escaped defect count and severity
  • Security posture indicator (green/yellow/red)
  • Quality trend narrative
info

Defect pattern analysis is most valuable when it drives prevention, not just detection. Every pattern in your database should have both a detection method and a prevention method. The goal is to shrink the database over time as prevention mechanisms take effect.