active_harness 0.2.12 → 0.2.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f4f069d1d894324475b0a5395d5728fa77626698255bc3400428ef66a2228ac4
4
- data.tar.gz: 1cbfee779b26e122d0e01e3fa1e4a80907398eec5f0895b28bc1dcb19484d8a4
3
+ metadata.gz: 6a42e03d329e638e3119d0a259a2286f05a0ec1603b8c2c81636e4ac041ebf9d
4
+ data.tar.gz: 074cf7e1558d9efee20cd17c3405665be00ef29c30574289b54d6e2251ebeacc
5
5
  SHA512:
6
- metadata.gz: c2293baad25b739468f7fb3306583f2e475409edc7b1533a03e83da9e28776ba17a27ae6a9aebf47fb18d4a1953957240d2615a277443e39781876a685d7da1e
7
- data.tar.gz: 6e78901ee75204d853b53df08baef9215f7f225f15065da771262ee084af17243a796ab132526701b016e4d0066762338e01fd01fd36f5e316e1045599cc8c3b
6
+ metadata.gz: 3397bd1901b3062030b3a41dc327ed1fd7053ef5d90e8e53d0ebb820375054f112cc80e70e528282703277e90137310318d026c456950f86ae755a09aca4e473
7
+ data.tar.gz: 7137703c7469a5f1cd8adfb121fd5047231387890756325f7e035955c7a23dab3bd0dfaf22c223343a9fb006279b6b352c01f1cf443da6cfcf60c3e26227cac3
@@ -0,0 +1,28 @@
1
+ module ActiveHarness
2
+ class Agent
3
+ private
4
+
5
+ # Calculates the monetary cost of a single request based on token usage
6
+ # and pricing data from ActiveHarness::Costs.
7
+ #
8
+ # Returns a hash { input_cost:, output_cost:, total_cost: } in USD,
9
+ # or nil if usage is absent or the model is not found in the pricing registry.
10
+ def calculate_cost(model_id, usage)
11
+ return nil if model_id.nil? || usage.nil?
12
+
13
+ pricing = ActiveHarness::Costs.find(model_id.to_s)
14
+ return nil unless pricing&.input_per_million && pricing&.output_per_million
15
+
16
+ input_cost = (usage[:input_tokens].to_f / 1_000_000) * pricing.input_per_million
17
+ output_cost = (usage[:output_tokens].to_f / 1_000_000) * pricing.output_per_million
18
+
19
+ {
20
+ input_cost: input_cost.round(8),
21
+ output_cost: output_cost.round(8),
22
+ total_cost: (input_cost + output_cost).round(8)
23
+ }
24
+ rescue StandardError
25
+ nil
26
+ end
27
+ end
28
+ end
@@ -113,6 +113,7 @@ module ActiveHarness
113
113
  def build_result(response, entry, attempts, elapsed)
114
114
  raw = response[:content]
115
115
  parsed = parse_output(raw)
116
+ usage = response[:usage]
116
117
 
117
118
  Result.new(
118
119
  input: @input,
@@ -125,7 +126,8 @@ module ActiveHarness
125
126
  model_list: model_list,
126
127
  attempts: attempts,
127
128
  execution_time: elapsed,
128
- usage: response[:usage]
129
+ usage: usage,
130
+ cost: calculate_cost(entry[:model], usage)
129
131
  )
130
132
  end
131
133
 
@@ -152,4 +154,5 @@ require_relative "agent/models"
152
154
  require_relative "agent/providers"
153
155
  require_relative "agent/output_parser"
154
156
  require_relative "agent/ruby_llm_backend"
157
+ require_relative "agent/cost"
155
158
 
@@ -183,10 +183,21 @@ module ActiveHarness
183
183
  end
184
184
 
185
185
  def registry
186
- @registry ||= begin
187
- source = File.exist?(cache_file) ? cache_file : BUNDLED_DATA_FILE
188
- JSON.parse(File.read(source), symbolize_names: true)
186
+ @registry ||= load_registry
187
+ end
188
+
189
+ def load_registry
190
+ if File.exist?(cache_file)
191
+ begin
192
+ data = JSON.parse(File.read(cache_file), symbolize_names: true)
193
+ return data if data.is_a?(Array)
194
+ rescue JSON::ParserError
195
+ # Cache file corrupted — fall through to bundled data
196
+ end
189
197
  end
198
+ JSON.parse(File.read(BUNDLED_DATA_FILE), symbolize_names: true)
199
+ rescue JSON::ParserError, Errno::ENOENT
200
+ []
190
201
  end
191
202
 
192
203
  def fetch_models_dev
@@ -6,5 +6,6 @@ module ActiveHarness
6
6
  # output — raw string from the provider
7
7
  # parsed — for format :json: a Ruby Hash/Array; for format :text: same as output
8
8
  # usage — token counts: { input_tokens:, output_tokens:, total_tokens: } or nil for streaming
9
- Result = Struct.new(:input, :output, :parsed, :system_prompt, :provider, :model, :temperature, :model_list, :attempts, :execution_time, :usage, keyword_init: true)
9
+ # cost — { input_cost:, output_cost:, total_cost: } in USD, or nil if pricing unavailable
10
+ Result = Struct.new(:input, :output, :parsed, :system_prompt, :provider, :model, :temperature, :model_list, :attempts, :execution_time, :usage, :cost, keyword_init: true)
10
11
  end
@@ -0,0 +1,21 @@
1
+ module ActiveHarness
2
+ class Tribunal
3
+ class << self
4
+ # Declare agents at the class level.
5
+ #
6
+ # agents PolitenessAgent, ConstructivenessAgent
7
+ # agents [PolitenessAgent, ConstructivenessAgent]
8
+ def agents(*list)
9
+ tribunal_config[:agents] = list.flatten
10
+ end
11
+
12
+ # Class-level process block — defines how to compute the verdict from results.
13
+ #
14
+ # process { |results| results.all? { |r| r.parsed["result"] == true } }
15
+ # process { |results| results.count { |r| r.parsed["result"] == true } >= 2 }
16
+ def process(&block)
17
+ tribunal_config[:process] = block
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,87 @@
1
+ module ActiveHarness
2
+ class Tribunal
3
+ VALID_HOOKS = %i[
4
+ before_call
5
+ before_agent
6
+ after_agent
7
+ agent_error
8
+ after_call
9
+ before_verdict
10
+ after_verdict
11
+ ].freeze
12
+
13
+ class << self
14
+ # Class-level hook registration.
15
+ #
16
+ # on :before_call do ... end
17
+ # on :before_agent do |agent| ... end
18
+ # on :after_agent do |result| ... end
19
+ # on :agent_error do |name, error| ... end
20
+ # on :after_call do |results, errors| ... end
21
+ # on :before_verdict do |results| results end # transform hook
22
+ # on :after_verdict do |verdict| ... end
23
+ def on(event, &block)
24
+ unless VALID_HOOKS.include?(event)
25
+ raise ArgumentError,
26
+ "Unknown Tribunal hook :#{event}. Valid hooks: #{VALID_HOOKS.map { |h| ":#{h}" }.join(", ")}"
27
+ end
28
+
29
+ tribunal_config[:hooks][event] = block
30
+ end
31
+
32
+ # Rails-style aliases for +on+:
33
+ #
34
+ # before :call do ... end # → on :before_call
35
+ # before :agent do |agent| end # → on :before_agent
36
+ # before :verdict do |results| end # → on :before_verdict (transform)
37
+ # after :call do |r, e| end # → on :after_call
38
+ # after :agent do |result| end # → on :after_agent
39
+ # after :verdict do |verdict| end # → on :after_verdict
40
+ # callback :agent_error do |name, e| end # → on :agent_error
41
+ def before(event, &block)
42
+ on(:"before_#{event}", &block)
43
+ end
44
+
45
+ def after(event, &block)
46
+ on(:"after_#{event}", &block)
47
+ end
48
+
49
+ def callback(event, &block)
50
+ on(event, &block)
51
+ end
52
+ end
53
+
54
+ # Instance-level hook registration — overrides class-level hooks for this instance.
55
+ # :before_verdict is a transform hook: its return value replaces the results array
56
+ # passed to the process block.
57
+ def on(event, &block)
58
+ unless VALID_HOOKS.include?(event)
59
+ raise ArgumentError,
60
+ "Unknown Tribunal hook :#{event}. Valid hooks: #{VALID_HOOKS.map { |h| ":#{h}" }.join(", ")}"
61
+ end
62
+
63
+ @hooks[event] = block
64
+ self
65
+ end
66
+
67
+ private
68
+
69
+ def run_hook(event, *args)
70
+ return unless @hooks[event]
71
+
72
+ if args.any?
73
+ instance_exec(*args, &@hooks[event])
74
+ else
75
+ instance_eval(&@hooks[event])
76
+ end
77
+ end
78
+
79
+ # Like run_hook but uses the return value to replace the passed value.
80
+ # Used by :before_verdict to allow results transformation before verdict computation.
81
+ def transform_hook(event, value)
82
+ return value unless @hooks[event]
83
+
84
+ instance_exec(value, &@hooks[event])
85
+ end
86
+ end
87
+ end
@@ -29,62 +29,11 @@ module ActiveHarness
29
29
  # ContentQualityTribunal.new(input: "...").call
30
30
  #
31
31
  class Tribunal
32
- VALID_HOOKS = %i[
33
- before_call
34
- after_agent
35
- agent_error
36
- after_call
37
- before_verdict
38
- after_verdict
39
- ].freeze
40
-
41
32
  # -------------------------------------------------------------------------
42
- # Class-level DSL — used when subclassing ActiveHarness::Tribunal
33
+ # Class-level DSL — core
43
34
  # -------------------------------------------------------------------------
44
35
  class << self
45
- # Declare agents at the class level.
46
- # agents PolitenessAgent, ConstructivenessAgent
47
- def agents(*list)
48
- tribunal_config[:agents] = list.flatten
49
- end
50
-
51
- # Class-level hook registration.
52
- # on(:after_agent) { |result| puts result.model }
53
- def on(event, &block)
54
- unless VALID_HOOKS.include?(event)
55
- raise ArgumentError, "Unknown Tribunal hook :#{event}. Valid hooks: #{VALID_HOOKS.join(", ")}"
56
- end
57
-
58
- tribunal_config[:hooks][event] = block
59
- end
60
-
61
- # Rails-style aliases for +on+:
62
- #
63
- # before :call do ... end # → on :before_call
64
- # before :agent do ... end # → on :before_agent (not used yet)
65
- # before :verdict do |r| end # → on :before_verdict
66
- # after :call do ... end # → on :after_call
67
- # after :agent do |r| end # → on :after_agent
68
- # after :verdict do |v| end # → on :after_verdict
69
- # callback :agent_error do |n,e| end # → on :agent_error
70
- def before(event, &block)
71
- on(:"before_#{event}", &block)
72
- end
73
-
74
- def after(event, &block)
75
- on(:"after_#{event}", &block)
76
- end
77
-
78
- def callback(event, &block)
79
- on(event, &block)
80
- end
81
-
82
- # Class-level process block.
83
- # process { |results| results.all? { |r| r.parsed["result"] == true } }
84
- def process(&block)
85
- tribunal_config[:process] = block
86
- end
87
-
36
+ # Each subclass gets its own isolated config hash.
88
37
  def tribunal_config
89
38
  @tribunal_config ||= { agents: [], hooks: {} }
90
39
  end
@@ -94,36 +43,32 @@ module ActiveHarness
94
43
  end
95
44
  end
96
45
 
97
- attr_accessor :input, :context
46
+ # -------------------------------------------------------------------------
47
+ # Instance API
48
+ # -------------------------------------------------------------------------
49
+ attr_accessor :input, :context, :stream, :agent_event_stream, :tribunal_event_stream
98
50
  attr_reader :results, :errors, :verdict, :execution_time, :agent_execution_times
99
51
 
100
- def initialize(input: nil, context: {}, agents: nil, timeout: 7)
52
+ def initialize(input: nil, context: {}, agents: nil, timeout: 7,
53
+ stream: nil, agent_event_stream: nil, tribunal_event_stream: nil)
101
54
  config = self.class.tribunal_config
102
55
 
103
- @input = input
104
- @context = context
105
- @agents = agents || config[:agents]
106
- @timeout = timeout
107
- @process_block = config[:process]
108
- @hooks = config[:hooks].dup
109
- @results = []
110
- @errors = []
111
- @verdict = nil
112
- @execution_time = nil
56
+ @input = input
57
+ @context = context
58
+ @agents = agents || config[:agents]
59
+ @timeout = timeout
60
+ @process_block = config[:process]
61
+ @hooks = config[:hooks].dup
62
+ @stream = stream
63
+ @agent_event_stream = agent_event_stream
64
+ @tribunal_event_stream = tribunal_event_stream
65
+ @results = []
66
+ @errors = []
67
+ @verdict = nil
68
+ @execution_time = nil
113
69
  @agent_execution_times = []
114
70
  end
115
71
 
116
- # Instance-level hook registration — overrides class-level hooks.
117
- # :before_verdict is a transform hook: its return value replaces the results array.
118
- def on(event, &block)
119
- unless VALID_HOOKS.include?(event)
120
- raise ArgumentError, "Unknown Tribunal hook :#{event}. Valid hooks: #{VALID_HOOKS.join(", ")}"
121
- end
122
-
123
- @hooks[event] = block
124
- self
125
- end
126
-
127
72
  # Instance-level process block — overrides class-level block.
128
73
  def process(&block)
129
74
  @process_block = block
@@ -143,14 +88,15 @@ module ActiveHarness
143
88
 
144
89
  started_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
145
90
 
146
- futures = agents.map do |agent|
91
+ futures = agents.each_with_index.map do |agent, index|
92
+ run_hook(:before_agent, agent, index)
147
93
  t0 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
148
94
  future = Concurrent::Future.execute { agent.call }
149
95
  [future, t0]
150
96
  end
151
97
 
152
- @results = []
153
- @errors = []
98
+ @results = []
99
+ @errors = []
154
100
  @agent_execution_times = []
155
101
 
156
102
  futures.each_with_index do |(future, t0), index|
@@ -159,19 +105,19 @@ module ActiveHarness
159
105
  @agent_execution_times << { agent: agents[index].class.name, time: elapsed }
160
106
 
161
107
  if future.fulfilled?
162
- value = future.value
108
+ value = future.value
163
109
  result = value.is_a?(ActiveHarness::Agent) ? value.result : value
164
110
  @results << result
165
- run_hook(:after_agent, result)
111
+ run_hook(:after_agent, result, index)
166
112
  elsif future.incomplete?
167
113
  error = Errors::TimeoutError.new(
168
114
  "Agent #{agents[index].class.name} timed out after #{@timeout}s"
169
115
  )
170
116
  @errors << { agent: agents[index].class.name, error: error }
171
- run_hook(:agent_error, agents[index].class.name, error)
117
+ run_hook(:agent_error, agents[index].class.name, error, index)
172
118
  else
173
119
  @errors << { agent: agents[index].class.name, error: future.reason }
174
- run_hook(:agent_error, agents[index].class.name, future.reason)
120
+ run_hook(:agent_error, agents[index].class.name, future.reason, index)
175
121
  end
176
122
  end
177
123
 
@@ -193,26 +139,25 @@ module ActiveHarness
193
139
 
194
140
  private
195
141
 
196
- def run_hook(event, *args)
197
- @hooks[event]&.call(*args)
198
- end
199
-
200
- # Like run_hook but uses the return value to replace the passed value.
201
- def transform_hook(event, value)
202
- return value unless @hooks[event]
203
-
204
- @hooks[event].call(value)
205
- end
206
-
207
142
  def resolve_agents
208
143
  @agents.map do |agent|
209
144
  if agent.is_a?(Class)
210
- agent.new(input: @input, context: @context.dup)
145
+ agent.new(
146
+ input: @input,
147
+ context: @context.dup,
148
+ stream: @stream,
149
+ event_stream: @agent_event_stream
150
+ )
211
151
  else
212
- agent.input = @input if @input
152
+ agent.input = @input if @input
153
+ agent.stream = @stream if @stream
154
+ agent.event_stream = @agent_event_stream if @agent_event_stream
213
155
  agent
214
156
  end
215
157
  end
216
158
  end
217
159
  end
218
160
  end
161
+
162
+ require_relative "tribunal/hooks"
163
+ require_relative "tribunal/dsl"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_harness
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.12
4
+ version: 0.2.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - the-teacher
@@ -33,6 +33,7 @@ extra_rdoc_files: []
33
33
  files:
34
34
  - lib/active_harness.rb
35
35
  - lib/active_harness/agent.rb
36
+ - lib/active_harness/agent/cost.rb
36
37
  - lib/active_harness/agent/hooks.rb
37
38
  - lib/active_harness/agent/models.rb
38
39
  - lib/active_harness/agent/output_parser.rb
@@ -71,6 +72,8 @@ files:
71
72
  - lib/active_harness/railtie.rb
72
73
  - lib/active_harness/result.rb
73
74
  - lib/active_harness/tribunal.rb
75
+ - lib/active_harness/tribunal/dsl.rb
76
+ - lib/active_harness/tribunal/hooks.rb
74
77
  - lib/generators/active_harness/agent/agent_generator.rb
75
78
  - lib/generators/active_harness/agent/templates/agent.rb.tt
76
79
  - lib/generators/active_harness/install/install_generator.rb