Jido.AI.Reasoning.TRM.ACT (Jido AI v2.2.0)

Copy Markdown View Source

Adaptive Computational Time (ACT) module for TRM strategy.

Uses Jido.AI.Reasoning.TRM.Helpers.clamp/3 for value clamping.

This module implements early stopping logic based on confidence thresholds and convergence detection. It helps the TRM strategy decide when to stop iterating and return the best answer found.

Overview

ACT monitors the progression of answer quality across supervision steps and decides whether to continue iterating or halt early. Key mechanisms:

  1. Confidence Threshold: Stop when confidence exceeds a threshold
  2. Convergence Detection: Stop when improvements have plateaued
  3. Expected Improvement: Estimate whether continuing is worthwhile

Usage

# Calculate combined confidence
confidence = ACT.calculate_confidence(latent_state, quality_score)

# Check if should halt
ACT.should_halt?(confidence, threshold)
#=> true

# Detect convergence from history
ACT.detect_convergence([0.5, 0.6, 0.62, 0.63, 0.63])
#=> true

# Make a decision with full context
state = %{threshold: 0.9, current_confidence: 0.85, history: [0.5, 0.7, 0.85]}
ACT.make_decision(state, latent_state)
#=> {:continue, %{expected_improvement: 0.08}}

Summary

Functions

Calculates combined confidence from latent state and quality score.

Calculates the expected improvement from continuing.

Detects if improvements have plateaued based on confidence history.

Detects convergence with custom window and epsilon parameters.

Estimates the number of steps remaining to reach a target confidence.

Returns the reason for halting based on the ACT state.

Calculates the improvement rate from confidence history.

Makes a continue/halt decision based on ACT state and latent state.

Creates a new ACT state with the given threshold.

Checks if the current confidence exceeds the threshold for early stopping.

Calculates the total improvement from first to last confidence score.

Updates the ACT state with a new confidence value.

Updates the confidence history with a new value.

Types

act_state()

@type act_state() :: %{
  threshold: float(),
  current_confidence: float(),
  history: [float()]
}

decision()

@type decision() :: :continue | :halt

halt_reason()

@type halt_reason() ::
  :threshold_exceeded | :convergence_detected | :max_improvement_reached

Functions

calculate_confidence(latent_state, quality_score)

@spec calculate_confidence(map(), float()) :: float()

Calculates combined confidence from latent state and quality score.

Combines the reasoning confidence from latent state with the quality score from supervision feedback using weighted averaging.

Parameters

  • latent_state - The machine's latent state containing reasoning confidence
  • quality_score - Quality score from supervision (0.0-1.0)

Returns

Combined confidence score between 0.0 and 1.0.

Examples

iex> latent_state = %{confidence_score: 0.8}
iex> ACT.calculate_confidence(latent_state, 0.9)
0.86

calculate_expected_improvement(history)

@spec calculate_expected_improvement([float()]) :: float()

Calculates the expected improvement from continuing.

Uses the recent improvement trend to estimate how much additional improvement is likely from another iteration.

Parameters

  • history - List of confidence scores

Returns

Expected improvement as a float (0.0-1.0).

Examples

iex> ACT.calculate_expected_improvement([0.5, 0.6, 0.7, 0.8])
0.1

iex> ACT.calculate_expected_improvement([0.8, 0.82, 0.83])
0.02

detect_convergence(history)

@spec detect_convergence([float()]) :: boolean()

Detects if improvements have plateaued based on confidence history.

Analyzes the recent confidence scores to determine if the improvement rate has dropped below a meaningful threshold.

Parameters

  • history - List of confidence scores from oldest to newest

Returns

true if improvements have plateaued, false otherwise.

Examples

iex> ACT.detect_convergence([0.5, 0.6, 0.7, 0.8])
false

iex> ACT.detect_convergence([0.7, 0.72, 0.73, 0.73])
true

detect_convergence(history, window, epsilon)

@spec detect_convergence([float()], pos_integer(), float()) :: boolean()

Detects convergence with custom window and epsilon parameters.

Parameters

  • history - List of confidence scores
  • window - Number of recent scores to consider
  • epsilon - Maximum range for convergence

Examples

iex> ACT.detect_convergence([0.7, 0.71, 0.72], 3, 0.05)
true

estimated_steps_remaining(current, target, history)

@spec estimated_steps_remaining(float(), float(), [float()]) ::
  non_neg_integer() | :infinity

Estimates the number of steps remaining to reach a target confidence.

Parameters

  • current - Current confidence score
  • target - Target confidence to reach
  • history - Confidence history for trend estimation

Returns

Estimated steps remaining, or :infinity if improvement rate is too low.

Examples

iex> ACT.estimated_steps_remaining(0.7, 0.9, [0.5, 0.6, 0.7])
2

get_halt_reason(arg1)

@spec get_halt_reason(act_state()) :: halt_reason() | nil

Returns the reason for halting based on the ACT state.

Parameters

  • act_state - Current ACT state

Returns

A halt reason atom or nil if should continue.

Examples

iex> state = %{threshold: 0.9, current_confidence: 0.95, history: [0.9, 0.92, 0.95]}
iex> ACT.get_halt_reason(state)
:threshold_exceeded

improvement_rate(history)

@spec improvement_rate([float()]) :: float()

Calculates the improvement rate from confidence history.

Returns the average rate of improvement per step.

Parameters

  • history - List of confidence scores

Examples

iex> ACT.improvement_rate([0.5, 0.6, 0.7, 0.8])
0.1

make_decision(act_state, latent_state \\ %{})

@spec make_decision(act_state(), map()) :: {:continue, map()} | {:halt, halt_reason()}

Makes a continue/halt decision based on ACT state and latent state.

Evaluates multiple factors:

  1. Whether confidence exceeds threshold
  2. Whether improvements have converged
  3. Expected improvement from continuing

Parameters

  • act_state - Current ACT state with threshold and history
  • latent_state - Machine's latent state (optional)

Returns

A tuple of {:continue, metadata} or {:halt, reason}.

Examples

iex> state = %{threshold: 0.9, current_confidence: 0.95, history: [0.8, 0.9, 0.95]}
iex> ACT.make_decision(state, %{})
{:halt, :threshold_exceeded}

iex> state = %{threshold: 0.9, current_confidence: 0.7, history: [0.5, 0.6, 0.7]}
iex> ACT.make_decision(state, %{})
{:continue, %{expected_improvement: ...}}

new(threshold \\ 0.9)

@spec new(float()) :: act_state()

Creates a new ACT state with the given threshold.

Parameters

  • threshold - Confidence threshold for early stopping (0.0-1.0)

Examples

iex> ACT.new(0.9)
%{threshold: 0.9, current_confidence: 0.0, history: []}

should_halt?(confidence, threshold)

@spec should_halt?(float(), float()) :: boolean()

Checks if the current confidence exceeds the threshold for early stopping.

Parameters

  • confidence - Current confidence score
  • threshold - Threshold for early stopping

Returns

true if confidence >= threshold, false otherwise.

Examples

iex> ACT.should_halt?(0.95, 0.9)
true

iex> ACT.should_halt?(0.85, 0.9)
false

total_improvement(history)

@spec total_improvement([float()]) :: float()

Calculates the total improvement from first to last confidence score.

Parameters

  • history - List of confidence scores

Examples

iex> ACT.total_improvement([0.5, 0.6, 0.7, 0.8])
0.3

update(state, confidence)

@spec update(act_state(), float()) :: act_state()

Updates the ACT state with a new confidence value.

Adds the new confidence to the history and updates the current confidence.

Parameters

  • state - Current ACT state
  • confidence - New confidence value to record

Examples

iex> state = ACT.new(0.9)
iex> ACT.update(state, 0.7)
%{threshold: 0.9, current_confidence: 0.7, history: [0.7]}

update_confidence_history(history, new_confidence)

@spec update_confidence_history([float()], float()) :: [float()]

Updates the confidence history with a new value.

Maintains a rolling history of confidence scores for convergence detection.

Parameters

  • history - List of previous confidence scores
  • new_confidence - New confidence score to add

Returns

Updated history list.

Examples

iex> ACT.update_confidence_history([0.5, 0.6], 0.7)
[0.5, 0.6, 0.7]