Module: Legion::Extensions::Agentic::Memory::Trace::Helpers::Trace

Defined in:
lib/legion/extensions/agentic/memory/trace/helpers/trace.rb

Constant Summary collapse

TRACE_TYPES =
%i[firmware identity procedural trust semantic episodic sensory].freeze
ORIGINS =
%i[firmware direct_experience mesh_transfer imprint].freeze
STORAGE_TIERS =
%i[hot warm cold erased].freeze
BASE_DECAY_RATES =
{
  firmware:   0.000,
  identity:   0.001,
  procedural: 0.005,
  trust:      0.008,
  semantic:   0.010,
  episodic:   0.020,
  sensory:    0.100
}.freeze
STARTING_STRENGTHS =
{
  firmware:   1.000,
  identity:   1.000,
  procedural: 0.400,
  trust:      0.300,
  semantic:   0.500,
  episodic:   0.600,
  sensory:    0.400
}.freeze
E_WEIGHT =

Tuning constants from spec Section 4.5

0.3
R_AMOUNT =

emotional intensity weight on decay

0.10
IMPRINT_MULTIPLIER =

base reinforcement amount

3.0
AUTO_FIRE_THRESHOLD =

reinforcement boost during imprint window

0.85
ARCHIVE_THRESHOLD =

procedural auto-fire strength threshold

0.05
PRUNE_THRESHOLD =

below this, trace moves to cold storage

0.01
HOT_TIER_WINDOW =

below this, trace eligible for removal

86_400
WARM_TIER_WINDOW =

24 hours in seconds

7_776_000
RETRIEVAL_RECENCY_HALF =

90 days in seconds

3600
ASSOCIATION_BONUS =

half-life for recency scoring (1 hour)

0.15
MAX_ASSOCIATIONS =

bonus for Hebbian-associated traces

20
COACTIVATION_THRESHOLD =

max Hebbian links per trace

3

Class Method Summary collapse

Class Method Details

.default_partition_idObject



101
102
103
104
105
# File 'lib/legion/extensions/agentic/memory/trace/helpers/trace.rb', line 101

def default_partition_id
  Legion::Settings.dig(:agent, :id) || 'default'
rescue StandardError => _e
  'default'
end

.new_trace(type:, content_payload:, content_embedding: nil, emotional_valence: 0.0, emotional_intensity: 0.0, domain_tags: [], origin: :direct_experience, source_agent_id: nil, partition_id: nil, imprint_active: false, unresolved: false, consolidation_candidate: false, confidence: nil) ⇒ Object

rubocop:disable Metrics/ParameterLists

Raises:

  • (ArgumentError)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/legion/extensions/agentic/memory/trace/helpers/trace.rb', line 54

def new_trace(type:, content_payload:, content_embedding: nil, emotional_valence: 0.0, # rubocop:disable Metrics/ParameterLists
              emotional_intensity: 0.0, domain_tags: [], origin: :direct_experience,
              source_agent_id: nil, partition_id: nil, imprint_active: false,
              unresolved: false, consolidation_candidate: false, confidence: nil, **)
  raise ArgumentError, "invalid trace type: #{type}" unless TRACE_TYPES.include?(type)
  raise ArgumentError, "invalid origin: #{origin}" unless ORIGINS.include?(origin)

  now = Time.now.utc

  {
    trace_id:                SecureRandom.uuid,
    trace_type:              type,
    content_embedding:       content_embedding,
    content_payload:         content_payload,
    strength:                STARTING_STRENGTHS[type],
    peak_strength:           STARTING_STRENGTHS[type],
    base_decay_rate:         BASE_DECAY_RATES[type],
    emotional_valence:       emotional_valence.clamp(-1.0, 1.0),
    emotional_intensity:     emotional_intensity.clamp(0.0, 1.0),
    domain_tags:             Array(domain_tags),
    origin:                  origin,
    source_agent_id:         source_agent_id,
    created_at:              now,
    last_reinforced:         now,
    last_decayed:            now,
    reinforcement_count:     imprint_active ? 1 : 0,
    confidence:              confidence || (type == :firmware ? 1.0 : 0.5),
    storage_tier:            :hot,
    partition_id:            partition_id || default_partition_id,
    encryption_key_id:       nil,
    associated_traces:       [],
    parent_trace_id:         nil,
    child_trace_ids:         [],
    unresolved:              unresolved,
    consolidation_candidate: consolidation_candidate
  }
end

.valid_trace?(trace) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
98
99
# File 'lib/legion/extensions/agentic/memory/trace/helpers/trace.rb', line 92

def valid_trace?(trace)
  return false unless trace.is_a?(Hash)
  return false unless TRACE_TYPES.include?(trace[:trace_type])
  return false unless trace[:strength].is_a?(Numeric)
  return false unless trace[:strength].between?(0.0, 1.0)

  true
end