Class: Smith::Workflow::UsageEntry

Inherits:
Struct
  • Object
show all
Defined in:
lib/smith/workflow/usage_entry.rb

Overview

One row per provider RESPONSE within an agent provider attempt. usage_id is a UUID generated at recording time and stable across persist/restore so hosts can use it as an idempotency anchor.

transition, branch_key, round, and workflow come from the ambient Smith::Attribution at recording time, so hosts can attribute usage to the exact step, fan-out branch, optimizer round, and (for nested workflows) the exact graph that spent it. attempt_id is shared by every entry the same provider attempt produced (an attempt with an N-round tool loop records N entries): join on it against the :provider_call trace event for the attempt's single measured duration; never sum a duration across entries. Every attempt emits its :provider_call (an attempt aborted by a non-provider error emits with outcome: :aborted before re-raising), so prefix-accounted entries always have their join target. All optional members are nil on entries restored from checkpoints written by earlier Smith versions, and nil attribution members are omitted from serialization so those documents re-serialize byte-identically. rubocop:disable Style/RedundantStructKeywordInit

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**attributes) ⇒ UsageEntry

Returns a new instance of UsageEntry.



41
42
43
44
45
46
47
# File 'lib/smith/workflow/usage_entry.rb', line 41

def initialize(**attributes)
  attributes = attributes.transform_values do |value|
    value.is_a?(String) ? value.dup.freeze : value
  end
  super(**attributes) # rubocop:disable Style/SuperArguments
  freeze
end

Instance Attribute Details

#agent_nameObject

Returns the value of attribute agent_name

Returns:

  • (Object)

    the current value of agent_name



24
25
26
# File 'lib/smith/workflow/usage_entry.rb', line 24

def agent_name
  @agent_name
end

#attempt_idObject

Returns the value of attribute attempt_id

Returns:

  • (Object)

    the current value of attempt_id



24
25
26
# File 'lib/smith/workflow/usage_entry.rb', line 24

def attempt_id
  @attempt_id
end

#attempt_kindObject

Returns the value of attribute attempt_kind

Returns:

  • (Object)

    the current value of attempt_kind



24
25
26
# File 'lib/smith/workflow/usage_entry.rb', line 24

def attempt_kind
  @attempt_kind
end

#branch_keyObject

Returns the value of attribute branch_key

Returns:

  • (Object)

    the current value of branch_key



24
25
26
# File 'lib/smith/workflow/usage_entry.rb', line 24

def branch_key
  @branch_key
end

#costObject

Returns the value of attribute cost

Returns:

  • (Object)

    the current value of cost



24
25
26
# File 'lib/smith/workflow/usage_entry.rb', line 24

def cost
  @cost
end

#input_tokensObject

Returns the value of attribute input_tokens

Returns:

  • (Object)

    the current value of input_tokens



24
25
26
# File 'lib/smith/workflow/usage_entry.rb', line 24

def input_tokens
  @input_tokens
end

#modelObject

Returns the value of attribute model

Returns:

  • (Object)

    the current value of model



24
25
26
# File 'lib/smith/workflow/usage_entry.rb', line 24

def model
  @model
end

#output_tokensObject

Returns the value of attribute output_tokens

Returns:

  • (Object)

    the current value of output_tokens



24
25
26
# File 'lib/smith/workflow/usage_entry.rb', line 24

def output_tokens
  @output_tokens
end

#providerObject

Returns the value of attribute provider

Returns:

  • (Object)

    the current value of provider



24
25
26
# File 'lib/smith/workflow/usage_entry.rb', line 24

def provider
  @provider
end

#recorded_atObject

Returns the value of attribute recorded_at

Returns:

  • (Object)

    the current value of recorded_at



24
25
26
# File 'lib/smith/workflow/usage_entry.rb', line 24

def recorded_at
  @recorded_at
end

#roundObject

Returns the value of attribute round

Returns:

  • (Object)

    the current value of round



24
25
26
# File 'lib/smith/workflow/usage_entry.rb', line 24

def round
  @round
end

#transitionObject

Returns the value of attribute transition

Returns:

  • (Object)

    the current value of transition



24
25
26
# File 'lib/smith/workflow/usage_entry.rb', line 24

def transition
  @transition
end

#usage_idObject

Returns the value of attribute usage_id

Returns:

  • (Object)

    the current value of usage_id



24
25
26
# File 'lib/smith/workflow/usage_entry.rb', line 24

def usage_id
  @usage_id
end

#workflowObject

Returns the value of attribute workflow

Returns:

  • (Object)

    the current value of workflow



24
25
26
# File 'lib/smith/workflow/usage_entry.rb', line 24

def workflow
  @workflow
end

Class Method Details

.from_h(hash) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/smith/workflow/usage_entry.rb', line 57

def self.from_h(hash)
  sym = hash.transform_keys(&:to_sym)
  filtered = sym.slice(*members)
  %i[agent_name provider attempt_kind transition branch_key].each do |attribute|
    filtered[attribute] = filtered[attribute].to_sym if filtered[attribute].is_a?(String)
  end
  new(**filtered)
end

Instance Method Details

#to_hObject

Serialization is additive only when present: nil attribution members are omitted so an entry restored from a pre-attribution checkpoint re-serializes byte-identically. Hosts that digest whole persisted documents (exact-mutation proofs) depend on this stability.



53
54
55
# File 'lib/smith/workflow/usage_entry.rb', line 53

def to_h
  super.reject { |key, value| value.nil? && %i[transition branch_key round attempt_id workflow].include?(key) }
end