Class: Legion::Extensions::Agentic::Social::TheoryOfMind::Helpers::MentalStateTracker
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Social::TheoryOfMind::Helpers::MentalStateTracker
- Defined in:
- lib/legion/extensions/agentic/social/theory_of_mind/helpers/mental_state_tracker.rb
Instance Attribute Summary collapse
-
#agent_models ⇒ Object
readonly
Returns the value of attribute agent_models.
-
#prediction_log ⇒ Object
readonly
Returns the value of attribute prediction_log.
Instance Method Summary collapse
- #agents_tracked ⇒ Object
- #avg_prediction_accuracy ⇒ Object
- #compare_agents(agent_ids:) ⇒ Object
- #decay_all ⇒ Object
- #dirty? ⇒ Boolean
- #false_belief_check(agent_id:, known_truths:) ⇒ Object
- #from_apollo(store:) ⇒ Object
- #infer_intention(agent_id:, action:, confidence: :possible) ⇒ Object
-
#initialize ⇒ MentalStateTracker
constructor
A new instance of MentalStateTracker.
- #mark_clean! ⇒ Object
- #model_for(agent_id) ⇒ Object
- #pending_prediction(agent_id:) ⇒ Object
- #perspective_take(agent_id:) ⇒ Object
- #predict_behavior(agent_id:, context: {}) ⇒ Object
- #record_prediction_outcome(agent_id:, outcome:) ⇒ Object
- #to_apollo_entries ⇒ Object
- #to_h ⇒ Object
- #total_beliefs ⇒ Object
- #update_belief(agent_id:, domain:, content:, confidence:, source: :inference) ⇒ Object
- #update_desire(agent_id:, goal:, priority: :medium) ⇒ Object
Constructor Details
#initialize ⇒ MentalStateTracker
Returns a new instance of MentalStateTracker.
12 13 14 15 16 |
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/mental_state_tracker.rb', line 12 def initialize @agent_models = {} @prediction_log = [] @dirty = false end |
Instance Attribute Details
#agent_models ⇒ Object (readonly)
Returns the value of attribute agent_models.
10 11 12 |
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/mental_state_tracker.rb', line 10 def agent_models @agent_models end |
#prediction_log ⇒ Object (readonly)
Returns the value of attribute prediction_log.
10 11 12 |
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/mental_state_tracker.rb', line 10 def prediction_log @prediction_log end |
Instance Method Details
#agents_tracked ⇒ Object
140 141 142 |
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/mental_state_tracker.rb', line 140 def agents_tracked @agent_models.size end |
#avg_prediction_accuracy ⇒ Object
148 149 150 151 152 153 |
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/mental_state_tracker.rb', line 148 def avg_prediction_accuracy return 0.0 if @agent_models.empty? total = @agent_models.values.sum(&:prediction_accuracy) (total / @agent_models.size).round(4) end |
#compare_agents(agent_ids:) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/mental_state_tracker.rb', line 119 def compare_agents(agent_ids:) models = agent_ids.filter_map { |id| @agent_models[id] } return nil if models.empty? { agents: models.map(&:to_h), shared_beliefs: find_shared_beliefs(models), conflicting_goals: find_conflicting_goals(models), interaction_gap: interaction_gap(models) } end |
#decay_all ⇒ Object
135 136 137 138 |
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/mental_state_tracker.rb', line 135 def decay_all @agent_models.each_value(&:decay_beliefs) @agent_models.reject! { |_, m| m.beliefs.empty? && m.desires.empty? && m.intentions.empty? } end |
#dirty? ⇒ Boolean
18 19 20 |
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/mental_state_tracker.rb', line 18 def dirty? @dirty end |
#false_belief_check(agent_id:, known_truths:) ⇒ Object
105 106 107 108 109 110 |
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/mental_state_tracker.rb', line 105 def false_belief_check(agent_id:, known_truths:) model = @agent_models[agent_id] return nil unless model model.false_beliefs(known_truths) end |
#from_apollo(store:) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/mental_state_tracker.rb', line 40 def from_apollo(store:) result = store.query(text: 'theory_of_mind agent_model', tags: %w[theory_of_mind agent_model]) return false unless result[:success] && result[:results]&.any? result[:results].each { |entry| restore_from_entry(entry) } true rescue StandardError => e Legion::Logging.warn("[mental_state_tracker] from_apollo error: #{e.}") false end |
#infer_intention(agent_id:, action:, confidence: :possible) ⇒ Object
69 70 71 72 73 |
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/mental_state_tracker.rb', line 69 def infer_intention(agent_id:, action:, confidence: :possible) model = model_for(agent_id) model.update_intention(action: action, confidence: confidence) @dirty = true end |
#mark_clean! ⇒ Object
22 23 24 25 |
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/mental_state_tracker.rb', line 22 def mark_clean! @dirty = false self end |
#model_for(agent_id) ⇒ Object
51 52 53 54 55 |
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/mental_state_tracker.rb', line 51 def model_for(agent_id) @agent_models[agent_id] ||= AgentModel.new(agent_id) trim_models @agent_models[agent_id] end |
#pending_prediction(agent_id:) ⇒ Object
131 132 133 |
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/mental_state_tracker.rb', line 131 def pending_prediction(agent_id:) @prediction_log.reverse.find { |p| p[:agent_id] == agent_id } end |
#perspective_take(agent_id:) ⇒ Object
112 113 114 115 116 117 |
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/mental_state_tracker.rb', line 112 def perspective_take(agent_id:) model = @agent_models[agent_id] return nil unless model model.perspective end |
#predict_behavior(agent_id:, context: {}) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/mental_state_tracker.rb', line 75 def predict_behavior(agent_id:, context: {}) model = @agent_models[agent_id] return nil unless model intention = model.most_likely_intention desire = model.strongest_desire prediction = { agent_id: agent_id, predicted_action: intention&.dig(:action), action_confidence: intention&.dig(:confidence), underlying_desire: desire&.dig(:goal), context_considered: context.keys, model_accuracy: model.prediction_accuracy.round(4), interactions_seen: model.interaction_count } @prediction_log << prediction.merge(predicted_at: Time.now.utc) trim_prediction_log prediction end |
#record_prediction_outcome(agent_id:, outcome:) ⇒ Object
98 99 100 101 102 103 |
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/mental_state_tracker.rb', line 98 def record_prediction_outcome(agent_id:, outcome:) model = @agent_models[agent_id] return nil unless model model.update_prediction_accuracy(outcome) end |
#to_apollo_entries ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/mental_state_tracker.rb', line 27 def to_apollo_entries @agent_models.map do |agent_id, model| = (agent_id) content = Legion::JSON.dump({ agent_id: agent_id.to_s, beliefs: serialize_beliefs(model.beliefs), desires: model.desires, intentions: model.intentions }) { content: content, tags: } end end |
#to_h ⇒ Object
155 156 157 158 159 160 161 162 |
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/mental_state_tracker.rb', line 155 def to_h { agents_tracked: agents_tracked, total_beliefs: total_beliefs, avg_prediction_accuracy: avg_prediction_accuracy, prediction_log_size: @prediction_log.size } end |
#total_beliefs ⇒ Object
144 145 146 |
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/mental_state_tracker.rb', line 144 def total_beliefs @agent_models.values.sum { |m| m.beliefs.size } end |
#update_belief(agent_id:, domain:, content:, confidence:, source: :inference) ⇒ Object
57 58 59 60 61 |
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/mental_state_tracker.rb', line 57 def update_belief(agent_id:, domain:, content:, confidence:, source: :inference) model = model_for(agent_id) model.update_belief(domain: domain, content: content, confidence: confidence, source: source) @dirty = true end |
#update_desire(agent_id:, goal:, priority: :medium) ⇒ Object
63 64 65 66 67 |
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/mental_state_tracker.rb', line 63 def update_desire(agent_id:, goal:, priority: :medium) model = model_for(agent_id) model.update_desire(goal: goal, priority: priority) @dirty = true end |