Class: Legion::Extensions::Agentic::Inference::Intuition::Helpers::Pattern

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/inference/intuition/helpers/pattern.rb

Constant Summary

Constants included from Constants

Constants::CONFIDENCE_CEILING, Constants::CONFIDENCE_FLOOR, Constants::CONFIDENCE_LABELS, Constants::DECAY_RATE, Constants::DEFAULT_CONFIDENCE, Constants::HEURISTIC_TYPES, Constants::INTUITION_MODES, Constants::MAX_HEURISTICS, Constants::MAX_HISTORY, Constants::MAX_PATTERNS, Constants::PATTERN_STATES, Constants::RECOGNITION_THRESHOLD, Constants::REINFORCEMENT_RATE, Constants::SPEED_MULTIPLIER, Constants::STATE_THRESHOLDS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, cue:, response:, domain: :general, strength: DEFAULT_CONFIDENCE) ⇒ Pattern

Returns a new instance of Pattern.



15
16
17
18
19
20
21
22
23
24
# File 'lib/legion/extensions/agentic/inference/intuition/helpers/pattern.rb', line 15

def initialize(id:, cue:, response:, domain: :general, strength: DEFAULT_CONFIDENCE)
  @id         = id
  @cue        = cue
  @response   = response
  @domain     = domain
  @strength   = strength.to_f.clamp(CONFIDENCE_FLOOR, CONFIDENCE_CEILING)
  @encounters = 0
  @successes  = 0
  @state      = compute_state
end

Instance Attribute Details

#cueObject (readonly)

Returns the value of attribute cue.



12
13
14
# File 'lib/legion/extensions/agentic/inference/intuition/helpers/pattern.rb', line 12

def cue
  @cue
end

#domainObject (readonly)

Returns the value of attribute domain.



12
13
14
# File 'lib/legion/extensions/agentic/inference/intuition/helpers/pattern.rb', line 12

def domain
  @domain
end

#encountersObject (readonly)

Returns the value of attribute encounters.



12
13
14
# File 'lib/legion/extensions/agentic/inference/intuition/helpers/pattern.rb', line 12

def encounters
  @encounters
end

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/legion/extensions/agentic/inference/intuition/helpers/pattern.rb', line 12

def id
  @id
end

#responseObject (readonly)

Returns the value of attribute response.



12
13
14
# File 'lib/legion/extensions/agentic/inference/intuition/helpers/pattern.rb', line 12

def response
  @response
end

#stateObject (readonly)

Returns the value of attribute state.



12
13
14
# File 'lib/legion/extensions/agentic/inference/intuition/helpers/pattern.rb', line 12

def state
  @state
end

#strengthObject (readonly)

Returns the value of attribute strength.



12
13
14
# File 'lib/legion/extensions/agentic/inference/intuition/helpers/pattern.rb', line 12

def strength
  @strength
end

#successesObject (readonly)

Returns the value of attribute successes.



12
13
14
# File 'lib/legion/extensions/agentic/inference/intuition/helpers/pattern.rb', line 12

def successes
  @successes
end

Instance Method Details

#confidence_labelObject



68
69
70
71
# File 'lib/legion/extensions/agentic/inference/intuition/helpers/pattern.rb', line 68

def confidence_label
  CONFIDENCE_LABELS.each { |range, lbl| return lbl if range.cover?(@strength) }
  :noise
end

#decayObject



49
50
51
52
# File 'lib/legion/extensions/agentic/inference/intuition/helpers/pattern.rb', line 49

def decay
  @strength = (@strength - DECAY_RATE).clamp(CONFIDENCE_FLOOR, CONFIDENCE_CEILING)
  @state = compute_state
end

#expert?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/legion/extensions/agentic/inference/intuition/helpers/pattern.rb', line 64

def expert?
  @strength >= STATE_THRESHOLDS[:expert] && @encounters >= 10
end

#match_score(input_cue) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/legion/extensions/agentic/inference/intuition/helpers/pattern.rb', line 26

def match_score(input_cue)
  return 0.0 unless input_cue.is_a?(Hash) && @cue.is_a?(Hash)

  shared = @cue.keys & input_cue.keys
  return 0.0 if shared.empty?

  matches = shared.count { |k| @cue[k] == input_cue[k] }
  matches.to_f / [@cue.size, input_cue.size].max
end

#recognized?(input_cue) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/legion/extensions/agentic/inference/intuition/helpers/pattern.rb', line 36

def recognized?(input_cue)
  match_score(input_cue) >= RECOGNITION_THRESHOLD
end

#reinforce(success:) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/legion/extensions/agentic/inference/intuition/helpers/pattern.rb', line 40

def reinforce(success:)
  @encounters += 1
  @successes += 1 if success
  shift = success ? REINFORCEMENT_RATE : -REINFORCEMENT_RATE
  @strength = (@strength + shift).clamp(CONFIDENCE_FLOOR, CONFIDENCE_CEILING)
  @state = compute_state
  @strength
end

#reliable?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/legion/extensions/agentic/inference/intuition/helpers/pattern.rb', line 60

def reliable?
  @strength >= STATE_THRESHOLDS[:reliable] && @encounters >= 3
end

#success_rateObject



54
55
56
57
58
# File 'lib/legion/extensions/agentic/inference/intuition/helpers/pattern.rb', line 54

def success_rate
  return 0.0 if @encounters.zero?

  @successes.to_f / @encounters
end

#to_hObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/legion/extensions/agentic/inference/intuition/helpers/pattern.rb', line 73

def to_h
  {
    id:               @id,
    cue:              @cue,
    response:         @response,
    domain:           @domain,
    strength:         @strength.round(4),
    encounters:       @encounters,
    successes:        @successes,
    success_rate:     success_rate.round(4),
    state:            @state,
    confidence_label: confidence_label,
    reliable:         reliable?,
    expert:           expert?
  }
end