Search documentation of Ragex

# Ragex.Analysis.DeadCode.AIRefiner (Ragex v0.4.0)

View Source

AI-powered refinement of dead code confidence scores.

Uses semantic analysis to reduce false positives by evaluating whether "unused" functions are actually callback functions, hooks, or entry points that heuristics might miss.

## Features

- Semantic function name analysis
- Behavior pattern detection
- Documentation hint analysis
- Similar pattern matching from codebase
- Confidence score adjustment with reasoning

## Usage

    alias Ragex.Analysis.DeadCode.AIRefiner

    # Refine a single dead code result
    dead_func = %{
      function: {:function, MyModule, :handle_custom, 2},
      confidence: 0.7,
      reason: "No callers found"
    }

    {:ok, refined} = AIRefiner.refine_confidence(dead_func)
    # => %{
    #   confidence: 0.2,  # Lowered - likely a callback
    #   ai_reasoning: "Function name 'handle_custom' suggests...",
    #   original_confidence: 0.7
    # }

    # Refine multiple results
    {:ok, refined_list} = AIRefiner.refine_batch(dead_functions)

## Configuration

    config :ragex, :ai_features,
      dead_code_refinement: true

## Summary

### Types

dead_function()
refined_result()

### Functions

clear_cache()

Clear the refinement cache.

enabled?(opts \\ [])

Check if AI refinement is currently enabled.

refine_batch(dead_functions, opts \\ [])

Refine confidence scores for multiple dead code results in batch.

refine_confidence(dead_func, opts \\ [])

Refine confidence score for a dead code detection result.

## Types

# dead_function()

    @type dead_function() :: map()

# refined_result()

    @type refined_result() :: %{
      confidence: float(),
      ai_reasoning: String.t(),
      original_confidence: float(),
      adjustment: float()
    }

## Functions

# clear_cache()

    @spec clear_cache() :: :ok

Clear the refinement cache.

# enabled?(opts \\ [])

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

Check if AI refinement is currently enabled.

# refine_batch(dead_functions, opts \\ [])

    @spec refine_batch(
      [dead_function()],
      keyword()
    ) :: {:ok, [refined_result()]} | {:error, term()}

Refine confidence scores for multiple dead code results in batch.

More efficient than calling refine_confidence/2 multiple times.

## Parameters

- `dead_functions` - List of dead code results
- `opts` - Options (same as refine_confidence/2)

## Returns

- `{:ok, refined_list}` - List of refined results

# refine_confidence(dead_func, opts \\ [])

    @spec refine_confidence(
      dead_function(),
      keyword()
    ) :: {:ok, refined_result()} | {:error, term()}

Refine confidence score for a dead code detection result.

Uses AI to analyze whether the function is truly dead or likely a callback/hook/entry point that the heuristic missed.

## Parameters

- `dead_func` - Dead code detection result with function info
- `opts` - Options:
  - `:ai_refine` - Enable/disable AI (default: from config)
  - `:min_adjustment` - Minimum confidence change to report (default: 0.1)

## Returns

- `{:ok, refined_result}` - Updated confidence with reasoning
- `{:error, reason}` - Error if refinement fails

## Examples

    dead_func = %{
      function: {:function, MyModule, :init, 1},
      confidence: 0.8,
      reason: "No callers found",
      visibility: :public,
      module: MyModule
    }

    {:ok, refined} = AIRefiner.refine_confidence(dead_func)
    # => %{
    #   confidence: 0.1,
    #   ai_reasoning: "init/1 is a standard GenServer callback...",
    #   original_confidence: 0.8,
    #   adjustment: -0.7
    # }