Ragex.Analysis.Duplication.AIAnalyzer (Ragex v0.14.0)

View Source

AI-powered semantic analysis for code duplication detection.

Uses AI to evaluate Type IV clones (different syntax, same semantics) by asking the AI to determine if code snippets are semantically equivalent and providing consolidation recommendations.

Features

  • Semantic equivalence detection for Type IV clones
  • False positive reduction for near-miss clones
  • Consolidation strategy recommendations
  • Confidence scoring for duplication claims
  • Batch processing for efficiency

Usage

alias Ragex.Analysis.Duplication.AIAnalyzer

# Analyze a single clone pair
clone_pair = %{
  type: :type_iv,
  snippets: [snippet1, snippet2],
  similarity: 0.65
}

{:ok, analysis} = AIAnalyzer.analyze_clone_pair(clone_pair)
# => %{
#   semantically_equivalent: true,
#   confidence: 0.85,
#   reasoning: "Both implement the same validation logic...",
#   consolidation_strategy: "Extract common validation function..."
# }

# Batch analyze multiple clone pairs
{:ok, analyzed} = AIAnalyzer.analyze_batch(clone_pairs)

Configuration

config :ragex, :ai_features,
  duplication_semantic_analysis: true

Summary

Functions

Analyze multiple clone pairs in batch.

Analyze whether a clone pair is semantically equivalent.

Clear the analysis cache.

Check if AI semantic analysis is currently enabled.

Types

analysis_result()

@type analysis_result() :: %{
  semantically_equivalent: boolean(),
  confidence: float(),
  reasoning: String.t(),
  consolidation_strategy: String.t() | nil,
  duplicate_lines: pos_integer() | nil
}

clone_pair()

@type clone_pair() :: map()

Functions

analyze_batch(clone_pairs, opts \\ [])

@spec analyze_batch(
  [clone_pair()],
  keyword()
) :: {:ok, [analysis_result()]} | {:error, term()}

Analyze multiple clone pairs in batch.

More efficient than calling analyze_clone_pair/2 multiple times.

Parameters

  • clone_pairs - List of clone detection results
  • opts - Options (same as analyze_clone_pair/2)

Returns

  • {:ok, analyzed_list} - List of analysis results

analyze_clone_pair(clone_pair, opts \\ [])

@spec analyze_clone_pair(
  clone_pair(),
  keyword()
) :: {:ok, analysis_result()} | {:error, term()}

Analyze whether a clone pair is semantically equivalent.

Uses AI to perform deep semantic analysis of code snippets to determine if they implement the same logic despite syntactic differences.

Parameters

  • clone_pair - Clone detection result with snippets
  • opts - Options:
    • :ai_analyze - Enable/disable AI (default: from config)
    • :min_confidence - Minimum confidence to report (default: 0.6)

Returns

  • {:ok, analysis_result} - Semantic analysis with recommendations
  • {:error, reason} - Error if analysis fails

Examples

clone_pair = %{
  type: :type_iv,
  snippets: [
    %{code: "if x > 0, do: x, else: 0", location: "lib/a.ex:10"},
    %{code: "max(x, 0)", location: "lib/b.ex:25"}
  ],
  similarity: 0.45
}

{:ok, analysis} = AIAnalyzer.analyze_clone_pair(clone_pair)
# => %{
#   semantically_equivalent: true,
#   confidence: 0.9,
#   reasoning: "Both compute max(x, 0)...",
#   consolidation_strategy: "Use Elixir's max/2 function consistently"
# }

clear_cache()

@spec clear_cache() :: :ok

Clear the analysis cache.

enabled?(opts \\ [])

@spec enabled?(keyword()) :: boolean()

Check if AI semantic analysis is currently enabled.