Class: SmartBrain::MemoryStore::InMemory

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_brain/memory_store/in_memory.rb

Constant Summary collapse

OVERWRITE_TYPES =
%w[preferences goals tasks].freeze

Instance Method Summary collapse

Constructor Details

#initializeInMemory

Returns a new instance of InMemory.



12
13
14
15
16
17
18
19
# File 'lib/smart_brain/memory_store/in_memory.rb', line 12

def initialize
  @by_scope = Hash.new { |h, k| h[k] = [] }
  @entities_index = Hash.new { |h, k| h[k] = [] }
  @summaries = {}
  @events = []
  @edges = []
  @exact = Retrievers::ExactRetriever.new
end

Instance Method Details

#active_items(session_id: nil, scope_ids: nil) ⇒ Object



55
56
57
58
59
# File 'lib/smart_brain/memory_store/in_memory.rb', line 55

def active_items(session_id: nil, scope_ids: nil)
  selected_scope_ids = normalize_scope_ids(session_id, scope_ids)
  selected_scope_ids.flat_map { |scope_id| by_scope[scope_id] }
                    .select { |item| item[:status] == 'active' && included_lifecycle?(item) }
end

#add_edge(session_id:, edge:, scope_id: nil, scope: nil, source_session_id: nil) ⇒ Object

--- knowledge graph -----------------------------------------------------



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/smart_brain/memory_store/in_memory.rb', line 145

def add_edge(session_id:, edge:, scope_id: nil, scope: nil, source_session_id: nil)
  resolved_scope_id = scope_id || edge[:scope_id] || legacy_scope_id(session_id)
  record = {
    id: SecureRandom.uuid,
    session_id: session_id,
    source_session_id: source_session_id || edge[:source_session_id] || session_id,
    scope_id: resolved_scope_id,
    scope: scope || edge[:scope] || { type: 'session', id: session_id },
    subject: edge[:subject].to_s,
    predicate: edge[:predicate].to_s,
    object: edge[:object].to_s,
    subject_entity_id: resolve_entity_id(nil, edge[:subject], scope_ids: [resolved_scope_id]),
    object_entity_id: resolve_entity_id(nil, edge[:object], scope_ids: [resolved_scope_id]),
    valid_from: edge[:valid_from] || Time.now.utc.iso8601,
    valid_to: edge[:valid_to],
    source_turn_id: edge[:source_turn_id],
    source_memory_item_id: edge[:source_memory_item_id],
    confidence: edge[:confidence] || 0.6,
    status: edge[:status] || 'active',
    meta: edge[:meta] || {},
    created_at: Time.now.utc.iso8601
  }
  edges << record
  record
end

#all_summariesObject



82
83
84
# File 'lib/smart_brain/memory_store/in_memory.rb', line 82

def all_summaries
  summaries.each_with_object({}) { |(sid, s), h| h[sid] = s }
end

#create_item(session_id:, item:, scope_id: nil, scope: nil) ⇒ Object

--- knowledge lifecycle -------------------------------------------------



87
88
89
90
91
92
93
94
95
96
# File 'lib/smart_brain/memory_store/in_memory.rb', line 87

def create_item(session_id:, item:, scope_id: nil, scope: nil)
  resolved_scope_id = scope_id || item[:scope_id] || legacy_scope_id(session_id)
  record = item.merge(
    id: SecureRandom.uuid, session_id: session_id, source_session_id: item[:source_session_id] || session_id,
    scope_id: resolved_scope_id, scope: scope || item[:scope] || { type: 'session', id: session_id }
  )
  by_scope[resolved_scope_id] << record
  update_entities(scope_id: resolved_scope_id, record: record)
  record
end

#edge_stats(session_id: nil, scope_ids: nil) ⇒ Object



202
203
204
205
206
207
208
# File 'lib/smart_brain/memory_store/in_memory.rb', line 202

def edge_stats(session_id: nil, scope_ids: nil)
  selected_scope_ids = normalize_scope_ids(session_id, scope_ids)
  scoped = edges.select { |e| selected_scope_ids.include?(e[:scope_id]) }
  { total: scoped.size,
    active: scoped.count { |e| e[:status] == 'active' },
    invalidated: scoped.count { |e| e[:status] == 'invalidated' } }
end

#edges_for_subject(session_id: nil, scope_ids: nil, subject:) ⇒ Object



184
185
186
# File 'lib/smart_brain/memory_store/in_memory.rb', line 184

def edges_for_subject(session_id: nil, scope_ids: nil, subject:)
  query_edges(session_id: session_id, scope_ids: scope_ids, subject: subject, include_invalid: true)
end

#entities(session_id: nil, scope_ids: nil) ⇒ Object



61
62
63
# File 'lib/smart_brain/memory_store/in_memory.rb', line 61

def entities(session_id: nil, scope_ids: nil)
  normalize_scope_ids(session_id, scope_ids).flat_map { |scope_id| entities_index[scope_id] }
end

#events_for(memory_item_id:) ⇒ Object



140
141
142
# File 'lib/smart_brain/memory_store/in_memory.rb', line 140

def events_for(memory_item_id:)
  events.select { |e| e[:memory_item_id] == memory_item_id }
end

#find_edge(id:) ⇒ Object



198
199
200
# File 'lib/smart_brain/memory_store/in_memory.rb', line 198

def find_edge(id:)
  edges.find { |edge| edge[:id] == id }
end

#find_item(id:) ⇒ Object



98
99
100
# File 'lib/smart_brain/memory_store/in_memory.rb', line 98

def find_item(id:)
  by_scope.values.flatten.find { |i| i[:id] == id }
end

#invalidate_edge(id:, reason: nil) ⇒ Object



188
189
190
191
192
193
194
195
196
# File 'lib/smart_brain/memory_store/in_memory.rb', line 188

def invalidate_edge(id:, reason: nil)
  edge = edges.find { |e| e[:id] == id }
  return nil unless edge

  edge[:valid_to] = Time.now.utc.iso8601
  edge[:status] = 'invalidated'
  edge[:meta] = (edge[:meta] || {}).merge(invalidated_reason: reason)
  edge
end

#latest_summary(session_id:) ⇒ Object



78
79
80
# File 'lib/smart_brain/memory_store/in_memory.rb', line 78

def latest_summary(session_id:)
  summaries[session_id]
end

#query_edges(session_id: nil, scope_ids: nil, subject: nil, predicate: nil, object: nil, include_invalid: false) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/smart_brain/memory_store/in_memory.rb', line 171

def query_edges(session_id: nil, scope_ids: nil, subject: nil, predicate: nil, object: nil, include_invalid: false)
  selected_scope_ids = normalize_scope_ids(session_id, scope_ids)
  edges.select do |e|
    next false unless selected_scope_ids.include?(e[:scope_id])
    next false if !include_invalid && e[:status] != 'active'
    next false if subject && e[:subject].downcase != subject.to_s.downcase
    next false if predicate && e[:predicate].downcase != predicate.to_s.downcase
    next false if object && e[:object].downcase != object.to_s.downcase

    true
  end.sort_by { |e| e[:valid_from].to_s }
end

#record_event(memory_item_id:, event_type:, from_lifecycle: nil, to_lifecycle: nil, reason: nil, reason_type: nil, reviewer: nil, evidence_refs: []) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/smart_brain/memory_store/in_memory.rb', line 122

def record_event(memory_item_id:, event_type:, from_lifecycle: nil, to_lifecycle: nil,
                 reason: nil, reason_type: nil, reviewer: nil, evidence_refs: [])
  event = {
    id: SecureRandom.uuid,
    memory_item_id: memory_item_id,
    event_type: event_type,
    from_lifecycle: from_lifecycle,
    to_lifecycle: to_lifecycle,
    reason: reason,
    reason_type: reason_type,
    reviewer: reviewer,
    evidence_refs: Array(evidence_refs),
    created_at: Time.now.utc.iso8601
  }
  events << event
  event
end

#resolve_entity_id(session_id, name, scope_ids: nil) ⇒ Object



210
211
212
213
214
215
216
217
218
# File 'lib/smart_brain/memory_store/in_memory.rb', line 210

def resolve_entity_id(session_id, name, scope_ids: nil)
  return nil if name.nil? || name.to_s.empty?

  match = entities(session_id: session_id, scope_ids: scope_ids).find do |e|
    e[:canonical].to_s.downcase == name.to_s.downcase ||
      e[:name].to_s.downcase == name.to_s.downcase
  end
  match && match[:id]
end

#save_summary(session_id:, summary:) ⇒ Object



73
74
75
76
# File 'lib/smart_brain/memory_store/in_memory.rb', line 73

def save_summary(session_id:, summary:)
  summaries[session_id] = summary
  summary
end

#search_memory(query:, session_id: nil, scope_ids: nil, limit:) ⇒ Object

Substring/exact search over active memory_items. Mirrors the FTS path of MemoryStore::Postgres#search_memory so MemoryRetriever is backend-agnostic.



67
68
69
70
71
# File 'lib/smart_brain/memory_store/in_memory.rb', line 67

def search_memory(query:, session_id: nil, scope_ids: nil, limit:)
  @exact.retrieve(
    query: query, memory_items: active_items(session_id: session_id, scope_ids: scope_ids), recent_turns: [], limit: limit
  )
end

#set_lifecycle(id:, lifecycle_status:, merge_value: nil) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/smart_brain/memory_store/in_memory.rb', line 102

def set_lifecycle(id:, lifecycle_status:, merge_value: nil)
  item = find_item(id: id)
  return nil unless item

  item[:lifecycle_status] = lifecycle_status
  item[:value_json] = (item[:value_json] || {}).merge(merge_value || {})
  item[:updated_at] = Time.now.utc.iso8601
  item
end

#set_status(id:, status:, merge_value: nil) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'lib/smart_brain/memory_store/in_memory.rb', line 112

def set_status(id:, status:, merge_value: nil)
  item = find_item(id: id)
  return nil unless item

  item[:status] = status
  item[:value_json] = (item[:value_json] || {}).merge(merge_value || {})
  item[:updated_at] = Time.now.utc.iso8601
  item
end

#upsert(extracted) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/smart_brain/memory_store/in_memory.rb', line 21

def upsert(extracted)
  session_id = extracted.fetch(:session_id)
  items = extracted.fetch(:items, [])
  written = []
  conflicts = []

  items.each do |item|
    scope_id = item[:scope_id] || legacy_scope_id(session_id)
    scope = item[:scope] || { type: 'session', id: session_id }
    existing = active_item(scope_id: scope_id, type: item[:type], key: item[:key])

    if existing && item[:status] == 'retracted'
      existing[:status] = 'retracted'
      conflicts << { type: 'retract', key: item[:key], previous_memory_item_id: existing[:id] }
      next
    end

    if existing && OVERWRITE_TYPES.include?(item[:type])
      existing[:status] = 'superseded'
      conflicts << { type: 'overwrite', key: item[:key], previous_memory_item_id: existing[:id] }
    end

    record = item.merge(
      id: SecureRandom.uuid, status: item[:status] || 'active', session_id: session_id,
      source_session_id: item[:source_session_id] || session_id, scope_id: scope_id, scope: scope
    )
    by_scope[scope_id] << record
    update_entities(scope_id: scope_id, record: record)
    written << record.slice(:id, :type, :key, :status, :confidence, :scope_id, :scope, :source_session_id)
  end

  { count: written.size, items: written, conflicts: conflicts }
end