Module: Legion::Extensions::Agentic::Memory::Trace::Runners::Traces

Includes:
Helpers::Lex
Included in:
Client
Defined in:
lib/legion/extensions/agentic/memory/trace/runners/traces.rb

Instance Method Summary collapse

Instance Method Details

#delete_trace(trace_id:, store: nil) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/legion/extensions/agentic/memory/trace/runners/traces.rb', line 69

def delete_trace(trace_id:, store: nil, **)
  store ||= default_store
  store.delete(trace_id)
  persist_store(store)
  log.debug("[memory] deleted trace #{trace_id[0..7]}")
  { deleted: true, trace_id: trace_id }
end

#get_trace(trace_id:, store: nil) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/legion/extensions/agentic/memory/trace/runners/traces.rb', line 19

def get_trace(trace_id:, store: nil, **)
  store ||= default_store
  trace = store.get(trace_id)
  log.debug("[memory] get trace #{trace_id[0..7]} found=#{!trace.nil?}")
  return { found: false } unless trace

  { found: true, trace: trace }
end

#retrieve_and_reinforce(limit: 10, store: nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/legion/extensions/agentic/memory/trace/runners/traces.rb', line 77

def retrieve_and_reinforce(limit: 10, store: nil, **)
  store ||= default_store
  all = store.all_traces(min_strength: 0.1)
  top = all.sort_by { |t| -t[:strength] }.first(limit)

  top.each do |trace|
    next if trace[:base_decay_rate].zero? # skip firmware

    trace[:reinforcement_count] += 1
    trace[:last_reinforced] = Time.now.utc
    store.store(trace)
  end

  persist_store(store) unless top.empty?

  log.debug("[memory] retrieve_and_reinforce: retrieved=#{top.size} from=#{all.size} total")
  { count: top.size, traces: top }
end

#retrieve_associated(trace_id:, store: nil, min_strength: 0.0, limit: 20) ⇒ Object



42
43
44
45
46
47
# File 'lib/legion/extensions/agentic/memory/trace/runners/traces.rb', line 42

def retrieve_associated(trace_id:, store: nil, min_strength: 0.0, limit: 20, **)
  store ||= default_store
  traces = store.retrieve_associated(trace_id, min_strength: min_strength, limit: limit)
  log.debug("[memory] retrieve_associated for #{trace_id[0..7]} count=#{traces.size}")
  { count: traces.size, traces: traces }
end

#retrieve_by_domain(domain_tag:, store: nil, min_strength: 0.0, limit: 100) ⇒ Object



35
36
37
38
39
40
# File 'lib/legion/extensions/agentic/memory/trace/runners/traces.rb', line 35

def retrieve_by_domain(domain_tag:, store: nil, min_strength: 0.0, limit: 100, **)
  store ||= default_store
  traces = store.retrieve_by_domain(domain_tag, min_strength: min_strength, limit: limit)
  log.debug("[memory] retrieve_by_domain=#{domain_tag} count=#{traces.size}")
  { count: traces.size, traces: traces }
end

#retrieve_by_type(type:, store: nil, min_strength: 0.0, limit: 100) ⇒ Object



28
29
30
31
32
33
# File 'lib/legion/extensions/agentic/memory/trace/runners/traces.rb', line 28

def retrieve_by_type(type:, store: nil, min_strength: 0.0, limit: 100, **)
  store ||= default_store
  traces = store.retrieve_by_type(type.to_sym, min_strength: min_strength, limit: limit)
  log.debug("[memory] retrieve_by_type=#{type} count=#{traces.size} min_strength=#{min_strength}")
  { count: traces.size, traces: traces }
end

#retrieve_ranked(trace_ids: [], store: nil, query_time: nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/legion/extensions/agentic/memory/trace/runners/traces.rb', line 49

def retrieve_ranked(trace_ids: [], store: nil, query_time: nil, **)
  store ||= default_store
  query_time ||= Time.now.utc
  associated_set = Set.new

  traces = trace_ids.filter_map { |id| store.get(id) }
  traces.each { |t| associated_set.merge(t[:associated_traces]) }

  scored = traces.map do |t|
    score = Helpers::Decay.compute_retrieval_score(
      trace: t, query_time: query_time, associated: associated_set.include?(t[:trace_id])
    )
    { trace: t, score: score }
  end

  result = scored.sort_by { |s| -s[:score] }
  log.debug("[memory] retrieve_ranked ids=#{trace_ids.size} scored=#{result.size}")
  { success: true, traces: result }
end

#store_trace(type:, content_payload:, store: nil) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/legion/extensions/agentic/memory/trace/runners/traces.rb', line 10

def store_trace(type:, content_payload:, store: nil, **)
  store ||= default_store
  trace = Helpers::Trace.new_trace(type: type.to_sym, content_payload: content_payload, **)
  store.store(trace)
  persist_store(store)
  log.debug("[memory] stored trace #{trace[:trace_id][0..7]} type=#{trace[:trace_type]} strength=#{trace[:strength].round(2)}")
  { trace_id: trace[:trace_id], trace_type: trace[:trace_type], strength: trace[:strength] }
end