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"
  load_from_local
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



77
78
79
# File 'lib/legion/extensions/agentic/social/trust/helpers/trust_map.rb', line 77

def count
  @entries.size
end

#decay_allObject



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

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
  decayed
end

#delegatable_agents(domain: :general) ⇒ Object



73
74
75
# File 'lib/legion/extensions/agentic/social/trust/helpers/trust_map.rb', line 73

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

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



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

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

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



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

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

#load_from_localObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/legion/extensions/agentic/social/trust/helpers/trust_map.rb', line 120

def load_from_local
  return unless defined?(Legion::Data::Local) && Legion::Data::Local.connected?

  Legion::Data::Local.connection[:trust_entries].each do |row|
    agent_id   = row[:agent_id]
    domain_str = row[:domain]
    domain_val = domain_str.to_sym
    entry_key  = "#{agent_id}:#{domain_str}"
    @entries[entry_key] = {
      agent_id:          agent_id,
      domain:            domain_val,
      dimensions:        {
        reliability: row[:reliability].to_f,
        competence:  row[:competence].to_f,
        integrity:   row[:integrity].to_f,
        benevolence: row[:benevolence].to_f
      },
      composite:         row[:composite].to_f,
      interaction_count: row[:interaction_count].to_i,
      positive_count:    row[:positive_count].to_i,
      negative_count:    row[:negative_count].to_i,
      last_interaction:  row[:last_interaction],
      created_at:        row[:created_at]
    }
  end
rescue StandardError => e
  Legion::Logging.warn "[trust] load_from_local failed: #{e.message}"
end

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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/legion/extensions/agentic/social/trust/helpers/trust_map.rb', line 25

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])
  entry
end

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



46
47
48
49
50
51
52
# File 'lib/legion/extensions/agentic/social/trust/helpers/trust_map.rb', line 46

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])
end

#save_to_localObject



81
82
83
84
85
86
87
88
89
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
# File 'lib/legion/extensions/agentic/social/trust/helpers/trust_map.rb', line 81

def save_to_local
  return unless defined?(Legion::Data::Local) && Legion::Data::Local.connected?

  dataset = Legion::Data::Local.connection[:trust_entries]

  @entries.each_value do |entry|
    row = {
      agent_id:          entry[:agent_id].to_s,
      domain:            entry[:domain].to_s,
      reliability:       entry[:dimensions][:reliability],
      competence:        entry[:dimensions][:competence],
      integrity:         entry[:dimensions][:integrity],
      benevolence:       entry[:dimensions][:benevolence],
      composite:         entry[:composite],
      interaction_count: entry[:interaction_count],
      positive_count:    entry[:positive_count],
      negative_count:    entry[:negative_count],
      last_interaction:  entry[:last_interaction],
      created_at:        entry[:created_at]
    }
    existing = dataset.where(agent_id: row[:agent_id], domain: row[:domain]).first
    if existing
      dataset.where(agent_id: row[:agent_id], domain: row[:domain])
             .update(row.except(:agent_id, :domain))
    else
      dataset.insert(row)
    end
  end

  # Remove DB rows for entries no longer in memory
  memory_pairs = @entries.values.map { |e| [e[:agent_id].to_s, e[:domain].to_s] }
  dataset.each do |row|
    pair = [row[:agent_id], row[:domain]]
    dataset.where(agent_id: pair[0], domain: pair[1]).delete unless memory_pairs.include?(pair)
  end
rescue StandardError => e
  Legion::Logging.warn "[trust] save_to_local failed: #{e.message}"
end

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



67
68
69
70
71
# File 'lib/legion/extensions/agentic/social/trust/helpers/trust_map.rb', line 67

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