Class: DecisionAgent::ABTesting::ABTestingAgent

Inherits:
Object
  • Object
show all
Defined in:
lib/decision_agent/ab_testing/ab_testing_agent.rb

Overview

Agent wrapper that adds A/B testing capabilities to the standard Agent Automatically handles variant assignment and decision tracking

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ab_test_manager:, version_manager: nil, evaluators: [], scoring_strategy: nil, audit_adapter: nil, cache_agents: true) ⇒ ABTestingAgent

Returns a new instance of ABTestingAgent.

Parameters:

  • ab_test_manager (ABTestManager)

    The A/B test manager

  • version_manager (Versioning::VersionManager) (defaults to: nil)

    Version manager for rules

  • evaluators (Array) (defaults to: [])

    Base evaluators (can be overridden by versioned rules)

  • scoring_strategy (Scoring::Base) (defaults to: nil)

    Scoring strategy

  • audit_adapter (Audit::Adapter) (defaults to: nil)

    Audit adapter

  • cache_agents (Boolean) (defaults to: true)

    Whether to cache agents by version_id (default: true)



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/decision_agent/ab_testing/ab_testing_agent.rb', line 16

def initialize(
  ab_test_manager:,
  version_manager: nil,
  evaluators: [],
  scoring_strategy: nil,
  audit_adapter: nil,
  cache_agents: true
)
  @ab_test_manager = ab_test_manager
  @version_manager = version_manager || ab_test_manager.version_manager
  @base_evaluators = evaluators
  @scoring_strategy = scoring_strategy
  @audit_adapter = audit_adapter
  @cache_agents = cache_agents
  @agent_cache = {} # Cache agents by version_id
  @agent_cache_mutex = Mutex.new
end

Instance Attribute Details

#ab_test_managerObject (readonly)

Returns the value of attribute ab_test_manager.



8
9
10
# File 'lib/decision_agent/ab_testing/ab_testing_agent.rb', line 8

def ab_test_manager
  @ab_test_manager
end

#version_managerObject (readonly)

Returns the value of attribute version_manager.



8
9
10
# File 'lib/decision_agent/ab_testing/ab_testing_agent.rb', line 8

def version_manager
  @version_manager
end

Instance Method Details

#active_testsArray<ABTest>

List active A/B tests

Returns:

  • (Array<ABTest>)

    Active tests



70
71
72
# File 'lib/decision_agent/ab_testing/ab_testing_agent.rb', line 70

def active_tests
  @ab_test_manager.active_tests
end

#cache_statsObject

Get cache statistics



80
81
82
83
84
85
86
87
# File 'lib/decision_agent/ab_testing/ab_testing_agent.rb', line 80

def cache_stats
  @agent_cache_mutex.synchronize do
    {
      cached_agents: @agent_cache.size,
      version_ids: @agent_cache.keys
    }
  end
end

#clear_agent_cache!Object

Clear the agent cache (useful for testing or when versions are updated)



75
76
77
# File 'lib/decision_agent/ab_testing/ab_testing_agent.rb', line 75

def clear_agent_cache!
  @agent_cache_mutex.synchronize { @agent_cache.clear }
end

#decide(context:, feedback: {}, ab_test_id: nil, user_id: nil) ⇒ Hash

Make a decision with A/B testing support

Parameters:

  • context (Hash, Context)

    The decision context

  • feedback (Hash) (defaults to: {})

    Optional feedback

  • ab_test_id (String, Integer, nil) (defaults to: nil)

    Optional A/B test ID

  • user_id (String, nil) (defaults to: nil)

    Optional user ID for consistent assignment

Returns:

  • (Hash)

    Decision result with A/B test metadata



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/decision_agent/ab_testing/ab_testing_agent.rb', line 40

def decide(context:, feedback: {}, ab_test_id: nil, user_id: nil)
  ctx = context.is_a?(Context) ? context : Context.new(context)

  # If A/B test is specified, use variant assignment
  if ab_test_id
    decide_with_ab_test(ctx, feedback, ab_test_id, user_id)
  else
    # Standard decision without A/B testing
    agent = build_agent(@base_evaluators)
    decision = agent.decide(context: ctx, feedback: feedback)

    {
      decision: decision.decision,
      confidence: decision.confidence,
      explanations: decision.explanations,
      evaluations: decision.evaluations,
      ab_test: nil
    }
  end
end

#get_test_results(test_id) ⇒ Hash

Get A/B test results

Parameters:

  • test_id (String, Integer)

    The test ID

Returns:

  • (Hash)

    Test results and statistics



64
65
66
# File 'lib/decision_agent/ab_testing/ab_testing_agent.rb', line 64

def get_test_results(test_id)
  @ab_test_manager.get_results(test_id)
end