Class: Agentic::Learning::ExecutionHistoryStore

Inherits:
Object
  • Object
show all
Defined in:
lib/agentic/learning/execution_history_store.rb

Overview

ExecutionHistoryStore is responsible for capturing, storing, and retrieving execution metrics and performance data for agent tasks and plans.

Examples:

Recording a task execution

history_store = Agentic::Learning::ExecutionHistoryStore.new
history_store.record_execution(
  task_id: task.id,
  agent_type: "research_agent",
  duration_ms: 1200,
  success: true,
  metrics: { tokens_used: 1500, prompt_tokens: 500 }
)

Retrieving execution history for a specific agent type

records = history_store.get_history(agent_type: "research_agent")

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ExecutionHistoryStore

Initialize a new ExecutionHistoryStore with the given options

Parameters:

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

    Configuration options

Options Hash (options):

  • :logger (Logger)

    Custom logger (defaults to Agentic.logger)

  • :storage_path (String)

    Directory to store execution history (defaults to ~/.agentic/history)

  • :retention_days (Integer)

    Number of days to retain history (defaults to 30)

  • :anonymize (Boolean)

    Whether to anonymize data (defaults to true)



33
34
35
36
37
38
39
40
41
42
# File 'lib/agentic/learning/execution_history_store.rb', line 33

def initialize(options = {})
  @logger = options[:logger] || Agentic.logger
  @storage_path = options[:storage_path] || default_storage_path
  @retention_days = options[:retention_days] || 30
  @anonymize = options.fetch(:anonymize, true)
  @memory_cache = []
  @cache_size_limit = options[:cache_size_limit] || 1000

  ensure_storage_path_exists
end

Instance Method Details

#cleanup_old_recordsInteger

Delete all history older than retention_days

Returns:

  • (Integer)

    Number of records deleted



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/agentic/learning/execution_history_store.rb', line 118

def cleanup_old_records
  cutoff_date = Date.today - @retention_days
  count = 0

  Dir.glob(File.join(@storage_path, "**/*.json")).each do |file|
    date_str = File.basename(file, ".json")
    date = begin
      Date.parse(date_str)
    rescue
      nil
    end
    if date && date < cutoff_date
      File.delete(file)
      count += 1
    end
  end

  @logger.info("Cleaned up #{count} old history records")
  count
end

#get_history(filters = {}) ⇒ Array<Hash>

Retrieve execution history based on filter criteria

Parameters:

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

    Filter criteria

Options Hash (filters):

  • :task_id (String)

    Filter by task ID

  • :plan_id (String)

    Filter by plan ID

  • :agent_type (String)

    Filter by agent type

  • :success (Boolean)

    Filter by success status

  • :start_time (Time)

    Filter by start time

  • :end_time (Time)

    Filter by end time

  • :limit (Integer)

    Maximum number of records to return

Returns:

  • (Array<Hash>)

    Array of matching history records



85
86
87
88
89
# File 'lib/agentic/learning/execution_history_store.rb', line 85

def get_history(filters = {})
  records = load_records(filters)
  limit = filters[:limit] || records.size
  records.first(limit)
end

#get_metric(metric_name, filters = {}, aggregation = :avg) ⇒ Float, Integer

Retrieve aggregated metrics from execution history

Parameters:

  • metric_name (Symbol)

    Name of the metric to aggregate

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

    Filter criteria

  • aggregation (Symbol) (defaults to: :avg)

    Aggregation method (:avg, :sum, :min, :max)

Options Hash (filters):

  • :agent_type (String)

    Filter by agent type

  • :success (Boolean)

    Filter by success status

  • :start_time (Time)

    Filter by start time

  • :end_time (Time)

    Filter by end time

Returns:

  • (Float, Integer)

    Aggregated metric value



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/agentic/learning/execution_history_store.rb', line 101

def get_metric(metric_name, filters = {}, aggregation = :avg)
  records = get_history(filters)
  values = records.map { |r| r.dig(:metrics, metric_name.to_s) }.compact

  return nil if values.empty?

  case aggregation
  when :avg then values.sum / values.size.to_f
  when :sum then values.sum
  when :min then values.min
  when :max then values.max
  end
end

#record_execution(execution_data) ⇒ Boolean

Record a new execution in the history store

Parameters:

  • execution_data (Hash)

    Execution data to record

Options Hash (execution_data):

  • :task_id (String)

    ID of the executed task

  • :plan_id (String)

    ID of the plan (optional)

  • :agent_type (String)

    Type of agent used

  • :duration_ms (Integer)

    Execution time in milliseconds

  • :success (Boolean)

    Whether execution succeeded

  • :metrics (Hash)

    Additional metrics

  • :context (Hash)

    Additional context information

Returns:

  • (Boolean)

    true if successfully recorded



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/agentic/learning/execution_history_store.rb', line 55

def record_execution(execution_data)
  record = build_record(execution_data)

  # Add to memory cache
  @memory_cache << record
  @memory_cache.shift if @memory_cache.size > @cache_size_limit

  # Persist to storage
  save_record(record)

  # Clean up old records periodically
  cleanup_old_records if rand < 0.05 # 5% chance to trigger cleanup

  true
rescue => e
  @logger.error("Failed to record execution history: #{e.message}")
  false
end