Module: Legion::Extensions::Agentic::Social::TheoryOfMind::Runners::TheoryOfMind

Includes:
Helpers::Lex
Included in:
Client
Defined in:
lib/legion/extensions/agentic/social/theory_of_mind/runners/theory_of_mind.rb

Instance Method Summary collapse

Instance Method Details

#check_false_beliefs(agent_id:, known_truths:) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/legion/extensions/agentic/social/theory_of_mind/runners/theory_of_mind.rb', line 50

def check_false_beliefs(agent_id:, known_truths:, **)
  false_beliefs = tracker.false_belief_check(agent_id: agent_id, known_truths: known_truths)
  return { error: 'unknown agent' } unless false_beliefs

  log.debug "[tom] false beliefs for #{agent_id}: #{false_beliefs.size}"
  { agent_id: agent_id, false_beliefs: false_beliefs, count: false_beliefs.size }
end

#compare_agents(agent_ids:) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/legion/extensions/agentic/social/theory_of_mind/runners/theory_of_mind.rb', line 66

def compare_agents(agent_ids:, **)
  comparison = tracker.compare_agents(agent_ids: agent_ids)
  return { error: 'no matching agents' } unless comparison

  log.debug "[tom] comparing #{agent_ids.size} agents"
  comparison
end

#mental_state(agent_id:) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/legion/extensions/agentic/social/theory_of_mind/runners/theory_of_mind.rb', line 74

def mental_state(agent_id:, **)
  model = tracker.agent_models[agent_id]
  return { error: 'unknown agent' } unless model

  {
    agent_id:    agent_id,
    beliefs:     model.beliefs.transform_values { |b| { content: b[:content], confidence: b[:confidence].round(4) } },
    desires:     model.desires,
    intentions:  model.intentions,
    accuracy:    model.prediction_accuracy.round(4),
    perspective: model.perspective
  }
end

#observe_agent(agent_id:, observations: {}) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/legion/extensions/agentic/social/theory_of_mind/runners/theory_of_mind.rb', line 25

def observe_agent(agent_id:, observations: {}, **)
  apply_belief_observation(agent_id, observations)
  apply_desire_observation(agent_id, observations)
  apply_intention_observation(agent_id, observations)

  log.debug "[tom] observed agent=#{agent_id}"
  { success: true, model: tracker.model_for(agent_id).to_h }
end

#perspective_take(agent_id:) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/legion/extensions/agentic/social/theory_of_mind/runners/theory_of_mind.rb', line 58

def perspective_take(agent_id:, **)
  perspective = tracker.perspective_take(agent_id: agent_id)
  return { error: 'unknown agent' } unless perspective

  log.debug "[tom] perspective for #{agent_id}"
  { agent_id: agent_id, perspective: perspective }
end

#predict_behavior(agent_id:, context: {}) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/legion/extensions/agentic/social/theory_of_mind/runners/theory_of_mind.rb', line 34

def predict_behavior(agent_id:, context: {}, **)
  prediction = tracker.predict_behavior(agent_id: agent_id, context: context)
  return { error: 'unknown agent' } unless prediction

  log.debug "[tom] predicted action for #{agent_id}: #{prediction[:predicted_action]}"
  prediction
end

#record_outcome(agent_id:, outcome:) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/legion/extensions/agentic/social/theory_of_mind/runners/theory_of_mind.rb', line 42

def record_outcome(agent_id:, outcome:, **)
  result = tracker.record_prediction_outcome(agent_id: agent_id, outcome: outcome.to_sym)
  return { error: 'unknown agent' } unless result

  log.debug "[tom] outcome for #{agent_id}: #{outcome}"
  { success: true, accuracy: tracker.model_for(agent_id).prediction_accuracy.round(4) }
end

#tom_statsObject



88
89
90
91
92
93
# File 'lib/legion/extensions/agentic/social/theory_of_mind/runners/theory_of_mind.rb', line 88

def tom_stats(**)
  log.debug '[tom] stats'
  tracker.to_h.merge(
    models: tracker.agent_models.transform_values(&:to_h)
  )
end

#update_theory_of_mind(tick_results: {}, human_observations: []) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/legion/extensions/agentic/social/theory_of_mind/runners/theory_of_mind.rb', line 13

def update_theory_of_mind(tick_results: {}, human_observations: [], **)
  extract_social_observations(tick_results)
  extract_mesh_observations(tick_results)
  process_tom_human_observations(human_observations)
  tracker.decay_all

  log.debug "[tom] agents=#{tracker.agents_tracked} " \
            "beliefs=#{tracker.total_beliefs} accuracy=#{tracker.avg_prediction_accuracy}"

  tracker.to_h
end