Class: Legion::Extensions::Agentic::Social::Trust::Helpers::TrustMap

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/social/trust/helpers/trust_map.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTrustMap

Returns a new instance of TrustMap.



12
13
14
15
# File 'lib/legion/extensions/agentic/social/trust/helpers/trust_map.rb', line 12

def initialize
  @entries = {} # key: "agent_id:domain"
  @dirty = false
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



10
11
12
# File 'lib/legion/extensions/agentic/social/trust/helpers/trust_map.rb', line 10

def entries
  @entries
end

Instance Method Details

#countObject



89
90
91
# File 'lib/legion/extensions/agentic/social/trust/helpers/trust_map.rb', line 89

def count
  @entries.size
end

#decay_allObject



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/legion/extensions/agentic/social/trust/helpers/trust_map.rb', line 65

def decay_all
  decayed = 0
  @entries.each_value do |entry|
    TrustModel::TRUST_DIMENSIONS.each do |dim|
      old = entry[:dimensions][dim]
      entry[:dimensions][dim] = TrustModel.clamp(old - TrustModel::TRUST_DECAY_RATE)
    end
    entry[:composite] = TrustModel.composite_score(entry[:dimensions])
    decayed += 1
  end
  @dirty = true if decayed.positive?
  decayed
end

#delegatable_agents(domain: :general) ⇒ Object



85
86
87
# File 'lib/legion/extensions/agentic/social/trust/helpers/trust_map.rb', line 85

def delegatable_agents(domain: :general)
  trusted_agents(domain: domain, min_trust: TrustModel::TRUST_DELEGATE_THRESHOLD)
end

#dirty?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/legion/extensions/agentic/social/trust/helpers/trust_map.rb', line 17

def dirty?
  @dirty
end

#from_apollo(store:) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/legion/extensions/agentic/social/trust/helpers/trust_map.rb', line 111

def from_apollo(store:)
  result = store.query(text: 'trust trust_entry', tags: %w[trust trust_entry])
  return false unless result[:success] && result[:results]&.any?

  result[:results].each { |entry| restore_from_entry(entry) }
  true
rescue StandardError => e
  Legion::Logging.warn("[trust_map] from_apollo error: #{e.message}")
  false
end

#get(agent_id, domain: :general) ⇒ Object



26
27
28
# File 'lib/legion/extensions/agentic/social/trust/helpers/trust_map.rb', line 26

def get(agent_id, domain: :general)
  @entries[key(agent_id, domain)]
end

#get_or_create(agent_id, domain: :general) ⇒ Object



30
31
32
# File 'lib/legion/extensions/agentic/social/trust/helpers/trust_map.rb', line 30

def get_or_create(agent_id, domain: :general)
  @entries[key(agent_id, domain)] ||= TrustModel.new_trust_entry(agent_id: agent_id, domain: domain)
end

#mark_clean!Object



21
22
23
24
# File 'lib/legion/extensions/agentic/social/trust/helpers/trust_map.rb', line 21

def mark_clean!
  @dirty = false
  self
end

#record_interaction(agent_id, positive:, domain: :general) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/legion/extensions/agentic/social/trust/helpers/trust_map.rb', line 34

def record_interaction(agent_id, positive:, domain: :general)
  entry = get_or_create(agent_id, domain: domain)
  entry[:interaction_count] += 1
  entry[:last_interaction] = Time.now.utc

  if positive
    entry[:positive_count] += 1
    TrustModel::TRUST_DIMENSIONS.each do |dim|
      entry[:dimensions][dim] = TrustModel.clamp(entry[:dimensions][dim] + TrustModel::TRUST_REINFORCEMENT)
    end
  else
    entry[:negative_count] += 1
    TrustModel::TRUST_DIMENSIONS.each do |dim|
      entry[:dimensions][dim] = TrustModel.clamp(entry[:dimensions][dim] - TrustModel::TRUST_PENALTY)
    end
  end

  entry[:composite] = TrustModel.composite_score(entry[:dimensions])
  @dirty = true
  entry
end

#reinforce_dimension(agent_id, dimension:, domain: :general, amount: TrustModel::TRUST_REINFORCEMENT) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/legion/extensions/agentic/social/trust/helpers/trust_map.rb', line 56

def reinforce_dimension(agent_id, dimension:, domain: :general, amount: TrustModel::TRUST_REINFORCEMENT)
  entry = get_or_create(agent_id, domain: domain)
  return unless TrustModel::TRUST_DIMENSIONS.include?(dimension)

  entry[:dimensions][dimension] = TrustModel.clamp(entry[:dimensions][dimension] + amount)
  entry[:composite] = TrustModel.composite_score(entry[:dimensions])
  @dirty = true
end

#to_apollo_entriesObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/legion/extensions/agentic/social/trust/helpers/trust_map.rb', line 93

def to_apollo_entries
  @entries.map do |_key, entry|
    tags = build_apollo_tags(entry[:agent_id], entry[:domain])
    content = Legion::JSON.dump({
                                  agent_id:          entry[:agent_id].to_s,
                                  domain:            entry[:domain].to_s,
                                  dimensions:        entry[:dimensions],
                                  composite:         entry[:composite],
                                  interaction_count: entry[:interaction_count],
                                  positive_count:    entry[:positive_count],
                                  negative_count:    entry[:negative_count],
                                  last_interaction:  entry[:last_interaction]&.iso8601,
                                  created_at:        entry[:created_at]&.iso8601
                                })
    { content: content, tags: tags }
  end
end

#trusted_agents(domain: :general, min_trust: TrustModel::TRUST_CONSIDER_THRESHOLD) ⇒ Object



79
80
81
82
83
# File 'lib/legion/extensions/agentic/social/trust/helpers/trust_map.rb', line 79

def trusted_agents(domain: :general, min_trust: TrustModel::TRUST_CONSIDER_THRESHOLD)
  @entries.values
          .select { |e| e[:domain] == domain && e[:composite] >= min_trust }
          .sort_by { |e| -e[:composite] }
end