Class: DecisionAgent::Simulation::ReplayEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/decision_agent/simulation/replay_engine.rb

Overview

Engine for replaying historical decisions and backtesting rule changes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent:, version_manager: nil) ⇒ ReplayEngine

Returns a new instance of ReplayEngine.



20
21
22
23
# File 'lib/decision_agent/simulation/replay_engine.rb', line 20

def initialize(agent:, version_manager: nil)
  @agent = agent
  @version_manager = version_manager || Versioning::VersionManager.new
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



18
19
20
# File 'lib/decision_agent/simulation/replay_engine.rb', line 18

def agent
  @agent
end

#version_managerObject (readonly)

Returns the value of attribute version_manager.



18
19
20
# File 'lib/decision_agent/simulation/replay_engine.rb', line 18

def version_manager
  @version_manager
end

Instance Method Details

#backtest(historical_data:, proposed_version:, baseline_version: nil, options: {}) ⇒ Hash

Backtest a rule change against historical data

Parameters:

  • historical_data (String, Array<Hash>, Hash)

    Historical context data (file path, array, or database config)

  • proposed_version (String, Integer, Hash)

    Proposed rule version

  • baseline_version (String, Integer, Hash, nil) (defaults to: nil)

    Baseline version (default: active version)

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

    Execution options

Returns:

  • (Hash)

    Backtest results with impact analysis



64
65
66
67
68
69
70
71
72
# File 'lib/decision_agent/simulation/replay_engine.rb', line 64

def backtest(historical_data:, proposed_version:, baseline_version: nil, options: {})
  baseline_version ||= get_active_version_for_rule(proposed_version)
  replay(
    historical_data: historical_data,
    rule_version: proposed_version,
    compare_with: baseline_version,
    options: options
  )
end

#replay(historical_data:, rule_version: nil, compare_with: nil, options: {}) ⇒ Hash

Replay historical decisions with a specific rule version

Parameters:

  • historical_data (String, Array<Hash>, Hash)

    Path to CSV/JSON file, array of context hashes, or database query config Database config format: { database: { connection: ..., query: "SELECT ..." } } or { database: { connection: ..., table: "table_name", where: ... } }

  • rule_version (String, Integer, Hash, nil) (defaults to: nil)

    Version ID, version hash, or nil to use current agent

  • compare_with (String, Integer, Hash, nil) (defaults to: nil)

    Optional baseline version to compare against

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

    Execution options

    • :parallel [Boolean] Use parallel execution (default: true)
    • :thread_count [Integer] Number of threads (default: 4)
    • :progress_callback [Proc] Progress callback

Returns:

  • (Hash)

    Replay results with comparison data



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/decision_agent/simulation/replay_engine.rb', line 36

def replay(historical_data:, rule_version: nil, compare_with: nil, options: {})
  contexts = load_historical_data(historical_data)
  options = {
    parallel: true,
    thread_count: 4,
    progress_callback: nil
  }.merge(options)

  # Build agent with specified version
  replay_agent = build_agent_from_version(rule_version) if rule_version
  replay_agent ||= @agent

  # Build baseline agent if comparison requested
  baseline_agent = build_agent_from_version(compare_with) if compare_with

  # Execute replay
  results = execute_replay(contexts, replay_agent, baseline_agent, options)

  # Build comparison report
  build_comparison_report(results, baseline_agent)
end