Class: Legion::Extensions::Agentic::Self::Fingerprint::Helpers::CognitiveTrait

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/self/fingerprint/helpers/cognitive_trait.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(category:) ⇒ CognitiveTrait

Returns a new instance of CognitiveTrait.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
# File 'lib/legion/extensions/agentic/self/fingerprint/helpers/cognitive_trait.rb', line 14

def initialize(category:)
  raise ArgumentError, "unknown category: #{category}" unless Constants::TRAIT_CATEGORIES.include?(category)

  @id           = SecureRandom.uuid
  @category     = category
  @baseline     = 0.5
  @variance     = 0.0
  @sample_count = 0
  @last_updated = Time.now.utc
end

Instance Attribute Details

#baselineObject (readonly)

Returns the value of attribute baseline.



12
13
14
# File 'lib/legion/extensions/agentic/self/fingerprint/helpers/cognitive_trait.rb', line 12

def baseline
  @baseline
end

#categoryObject (readonly)

Returns the value of attribute category.



12
13
14
# File 'lib/legion/extensions/agentic/self/fingerprint/helpers/cognitive_trait.rb', line 12

def category
  @category
end

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/legion/extensions/agentic/self/fingerprint/helpers/cognitive_trait.rb', line 12

def id
  @id
end

#last_updatedObject (readonly)

Returns the value of attribute last_updated.



12
13
14
# File 'lib/legion/extensions/agentic/self/fingerprint/helpers/cognitive_trait.rb', line 12

def last_updated
  @last_updated
end

#sample_countObject (readonly)

Returns the value of attribute sample_count.



12
13
14
# File 'lib/legion/extensions/agentic/self/fingerprint/helpers/cognitive_trait.rb', line 12

def sample_count
  @sample_count
end

#varianceObject (readonly)

Returns the value of attribute variance.



12
13
14
# File 'lib/legion/extensions/agentic/self/fingerprint/helpers/cognitive_trait.rb', line 12

def variance
  @variance
end

Instance Method Details

#deviation_from(value) ⇒ Object



38
39
40
# File 'lib/legion/extensions/agentic/self/fingerprint/helpers/cognitive_trait.rb', line 38

def deviation_from(value)
  (value.clamp(0.0, 1.0) - @baseline).abs.round(10)
end

#record_sample!(value) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/legion/extensions/agentic/self/fingerprint/helpers/cognitive_trait.rb', line 25

def record_sample!(value)
  clamped = value.clamp(0.0, 1.0)
  old_baseline = @baseline
  @baseline = ((Constants::EMA_ALPHA * clamped) + ((1.0 - Constants::EMA_ALPHA) * old_baseline)).round(10)

  deviation = (clamped - @baseline).abs
  @variance = ((Constants::EMA_ALPHA * deviation) + ((1.0 - Constants::EMA_ALPHA) * @variance)).round(10)

  @sample_count = [@sample_count + 1, Constants::MAX_SAMPLES].min
  @last_updated = Time.now.utc
  self
end

#stable?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/legion/extensions/agentic/self/fingerprint/helpers/cognitive_trait.rb', line 42

def stable?
  @variance <= 0.1
end

#strength_labelObject



50
51
52
# File 'lib/legion/extensions/agentic/self/fingerprint/helpers/cognitive_trait.rb', line 50

def strength_label
  Constants.trait_strength_label_for(@baseline)
end

#to_hObject



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/legion/extensions/agentic/self/fingerprint/helpers/cognitive_trait.rb', line 54

def to_h
  {
    id:           @id,
    category:     @category,
    baseline:     @baseline,
    variance:     @variance,
    sample_count: @sample_count,
    stable:       stable?,
    volatile:     volatile?,
    strength:     strength_label,
    last_updated: @last_updated
  }
end

#volatile?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/legion/extensions/agentic/self/fingerprint/helpers/cognitive_trait.rb', line 46

def volatile?
  @variance >= 0.3
end