Class: Legion::Extensions::Agentic::Self::Identity::Helpers::Fingerprint
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Self::Identity::Helpers::Fingerprint
- Includes:
- Data::Helper
- Defined in:
- lib/legion/extensions/agentic/self/identity/helpers/fingerprint.rb
Instance Attribute Summary collapse
-
#entropy_history ⇒ Object
readonly
Returns the value of attribute entropy_history.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#observation_count ⇒ Object
readonly
Returns the value of attribute observation_count.
Instance Method Summary collapse
- #current_entropy(observations = {}) ⇒ Object
- #entropy_trend(window: 10) ⇒ Object
-
#initialize ⇒ Fingerprint
constructor
A new instance of Fingerprint.
- #load_from_local ⇒ Object
- #maturity ⇒ Object
- #observe(dimension, value) ⇒ Object
- #observe_all(observations) ⇒ Object
- #save_to_local ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize ⇒ Fingerprint
Returns a new instance of Fingerprint.
17 18 19 20 21 22 23 |
# File 'lib/legion/extensions/agentic/self/identity/helpers/fingerprint.rb', line 17 def initialize @model = Dimensions.new_identity_model @observation_count = 0 @entropy_history = [] @local_previously_available = false load_from_local end |
Instance Attribute Details
#entropy_history ⇒ Object (readonly)
Returns the value of attribute entropy_history.
15 16 17 |
# File 'lib/legion/extensions/agentic/self/identity/helpers/fingerprint.rb', line 15 def entropy_history @entropy_history end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
15 16 17 |
# File 'lib/legion/extensions/agentic/self/identity/helpers/fingerprint.rb', line 15 def model @model end |
#observation_count ⇒ Object (readonly)
Returns the value of attribute observation_count.
15 16 17 |
# File 'lib/legion/extensions/agentic/self/identity/helpers/fingerprint.rb', line 15 def observation_count @observation_count end |
Instance Method Details
#current_entropy(observations = {}) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/legion/extensions/agentic/self/identity/helpers/fingerprint.rb', line 44 def current_entropy(observations = {}) entropy = Dimensions.compute_entropy(observations, @model) @entropy_history << { entropy: entropy, at: Time.now.utc } @entropy_history.shift while @entropy_history.size > 200 entropy end |
#entropy_trend(window: 10) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/legion/extensions/agentic/self/identity/helpers/fingerprint.rb', line 51 def entropy_trend(window: 10) recent = @entropy_history.last(window) return :stable if recent.size < 2 values = recent.map { |e| e[:entropy] } first_half = values[0...(values.size / 2)] second_half = values[(values.size / 2)..] diff = (second_half.sum / second_half.size) - (first_half.sum / first_half.size) if diff > 0.1 :rising elsif diff < -0.1 :falling else :stable end end |
#load_from_local ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/legion/extensions/agentic/self/identity/helpers/fingerprint.rb', line 138 def load_from_local return unless local_available? db = local_data_connection db[:identity_fingerprint].each do |row| dim = row[:dimension].to_sym next unless @model.key?(dim) @model[dim][:mean] = row[:mean].to_f @model[dim][:variance] = row[:variance].to_f @model[dim][:observations] = row[:observations].to_i @model[dim][:last_observed] = row[:last_observed] end = db[:identity_meta].first if @observation_count = [:observation_count].to_i raw = [:entropy_history] if raw && !raw.empty? parsed = ::JSON.parse(raw) @entropy_history = parsed.map { |e| { entropy: e['entropy'].to_f, at: Time.parse(e['at']) } } end end true rescue StandardError => e Legion::Logging.warn "lex-identity: load_from_local failed: #{e.}" false end |
#maturity ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/legion/extensions/agentic/self/identity/helpers/fingerprint.rb', line 69 def maturity if @observation_count < 10 :nascent elsif @observation_count < 100 :developing elsif @observation_count < 1000 :established else :mature end end |
#observe(dimension, value) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/legion/extensions/agentic/self/identity/helpers/fingerprint.rb', line 25 def observe(dimension, value) return unless Dimensions::IDENTITY_DIMENSIONS.include?(dimension) dim = @model[dimension] dim[:observations] += 1 @observation_count += 1 alpha = Dimensions::OBSERVATION_ALPHA old_mean = dim[:mean] dim[:mean] = (alpha * value) + ((1.0 - alpha) * old_mean) deviation = (value - dim[:mean]).abs dim[:variance] = (alpha * deviation) + ((1.0 - alpha) * dim[:variance]) dim[:last_observed] = Time.now.utc end |
#observe_all(observations) ⇒ Object
40 41 42 |
# File 'lib/legion/extensions/agentic/self/identity/helpers/fingerprint.rb', line 40 def observe_all(observations) observations.each { |dim, value| observe(dim, value) } end |
#save_to_local ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/legion/extensions/agentic/self/identity/helpers/fingerprint.rb', line 90 def save_to_local unless local_available? if @local_previously_available Legion::Logging.warn 'lex-identity: local persistence unavailable after being available' return false end return nil end db = local_data_connection @model.each do |dimension, data| existing = db[:identity_fingerprint].where(dimension: dimension.to_s).first row = { dimension: dimension.to_s, mean: data[:mean], variance: data[:variance], observations: data[:observations], last_observed: data[:last_observed] } if existing db[:identity_fingerprint].where(dimension: dimension.to_s).update(row) else db[:identity_fingerprint].insert(row) end end history_json = ::JSON.generate(@entropy_history.map { |e| { entropy: e[:entropy], at: e[:at].iso8601 } }) = db[:identity_meta].first if db[:identity_meta].where(id: [:id]).update( observation_count: @observation_count, entropy_history: history_json ) else db[:identity_meta].insert( observation_count: @observation_count, entropy_history: history_json ) end true rescue StandardError => e Legion::Logging.warn "lex-identity: save_to_local failed: #{e.}" false end |
#to_h ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/legion/extensions/agentic/self/identity/helpers/fingerprint.rb', line 81 def to_h { model: @model, observation_count: @observation_count, maturity: maturity, entropy_history_size: @entropy_history.size } end |