Class: Smith::Trace::Memory

Inherits:
Object
  • Object
show all
Defined in:
lib/smith/trace/memory.rb

Constant Summary collapse

CONFIG_MAP =
{
  transition: :trace_transitions,
  tool_call: :trace_tool_calls,
  token_usage: :trace_token_usage,
  provider_call: :trace_provider_calls,
  cost: :trace_cost,
  normalizer_decision: :trace_normalizer
}.freeze
CONTENT_KEYS =
%i[content prompt response args result].freeze
DEFAULT_LIMIT =

Generous enough that test and development runs never hit it; a bound exists at all so a long-lived process with parallel branches cannot grow this adapter without limit.

10_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(limit: DEFAULT_LIMIT) ⇒ Memory

Returns a new instance of Memory.



24
25
26
27
28
29
30
31
32
33
# File 'lib/smith/trace/memory.rb', line 24

def initialize(limit: DEFAULT_LIMIT)
  unless limit.is_a?(Integer) && limit.positive?
    raise ArgumentError, "Smith::Trace::Memory limit must be a positive integer, got #{limit.inspect}"
  end

  @limit = limit
  @traces = []
  @dropped_count = 0
  @mutex = Mutex.new
end

Instance Attribute Details

#limitObject (readonly)

Returns the value of attribute limit.



22
23
24
# File 'lib/smith/trace/memory.rb', line 22

def limit
  @limit
end

#tracesObject (readonly)

Returns the value of attribute traces.



22
23
24
# File 'lib/smith/trace/memory.rb', line 22

def traces
  @traces
end

Instance Method Details

#clear!Object



61
62
63
64
65
66
# File 'lib/smith/trace/memory.rb', line 61

def clear!
  @mutex.synchronize do
    @traces = []
    @dropped_count = 0
  end
end

#dropped_countObject

Entries rejected because the adapter was full. Zero in any healthy test run; a growing value means the limit needs raising or the process needs a clear!.



51
52
53
# File 'lib/smith/trace/memory.rb', line 51

def dropped_count
  @mutex.synchronize { @dropped_count }
end

#record(type:, data:) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/smith/trace/memory.rb', line 35

def record(type:, data:)
  return unless type_enabled?(type)

  entry = { type: type, data: filter_content(data) }
  @mutex.synchronize do
    if @traces.length >= @limit
      @dropped_count += 1
    else
      @traces << entry
    end
  end
end

#snapshotObject

A consistent copy for readers that may race concurrent recording; #traces stays the live array for compatibility.



57
58
59
# File 'lib/smith/trace/memory.rb', line 57

def snapshot
  @mutex.synchronize { @traces.dup }
end