lex-agentic-memory 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c20af8021bf914fea9be62a7cbfc8ebbf4f71c0a4ceb49defac2867fa2469b12
4
- data.tar.gz: e4d005aa924f4007f1f8ea91a81311a8ada4e7c9501bab4d7f72559e31416c1c
3
+ metadata.gz: ab87360ac0b2afc81f1868d934c29c851acdbb9db7b3864bc41437a20d959850
4
+ data.tar.gz: 922800b83b2c11ae1d525d23c9da664fbe6098bab81d4c0f32b45d96cc80d111
5
5
  SHA512:
6
- metadata.gz: 2128a3c7c86aee317d516df8a04b1849b7e886c9e309f76c32d63c9dbc67df6eaa34172d87614778c5670396ca6bcc9d31ca1049ebfd7c2a0260112f66d5b157
7
- data.tar.gz: 34730c557e9c0438fa2093748f7878aa0104b53d81d51dd89a5ca29c2ff196410c315f6ea103ae3ddb20ac4b41341f854de847706575b2ce13a4835bf4b9537d
6
+ metadata.gz: 2b2d45540a99c67dae5a5b27b49f75b27c1baa3beb56db00f1b7f4aef671577172b6a9a3a71077e535c7d9bcff0dc8d0690eb3f8a1af818c9bea5b0ac7d04d2c
7
+ data.tar.gz: 212f0679f2e1248209ad7a4a41a92694348080f1d71d24bbd6baa2eb7fff4389d591bad3f931fd8c1d7fc48298fb361f64cb9c5d08053785f59c3ec82d5be2e1
data/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.1.6] - 2026-03-23
4
+
5
+ ### Fixed
6
+ - Fix trace migration path registration (`'memory/local_migrations'` -> `'trace/local_migrations'`)
7
+ - Refactor CacheStore from single-blob storage to per-trace individual cache keys with index, preventing OOM on flush
8
+
9
+ ### Changed
10
+ - CacheStore now uses `legion:memory:trace:<uuid>` keys instead of serializing all traces into one key
11
+ - Dirty set tracking replaces dirty flag for more efficient selective flushing
12
+ - Batch flush in groups of 500 traces
13
+
3
14
  ## [0.1.5] - 2026-03-22
4
15
 
5
16
  ### Changed
@@ -7,27 +7,32 @@ module Legion
7
7
  module Trace
8
8
  module Helpers
9
9
  # Cache-backed store using Legion::Cache (Memcached/Redis).
10
- # Keeps a local copy in memory, syncs to/from cache on load/flush.
11
- # Call `flush` after a batch of writes, or it auto-flushes when dirty.
12
- # Call `reload` to pull latest state from cache (e.g. after another process wrote).
10
+ # Each trace is stored as an individual cache key for scalability.
11
+ # An index key tracks all known trace IDs.
12
+ # Keeps a local in-memory copy for fast reads; syncs to cache on flush.
13
13
  class CacheStore
14
- TRACES_KEY = 'legion:memory:traces'
15
- ASSOC_KEY = 'legion:memory:associations'
16
- TTL = 86_400 # 24 hours
14
+ TRACE_PREFIX = 'legion:memory:trace:'
15
+ INDEX_KEY = 'legion:memory:trace_index'
16
+ ASSOC_KEY = 'legion:memory:associations'
17
+ TTL = 86_400 # 24 hours
18
+ FLUSH_BATCH = 500 # traces per flush batch
17
19
 
18
20
  attr_reader :traces, :associations
19
21
 
20
22
  def initialize
21
- Legion::Logging.info '[memory] CacheStore initialized (memcached-backed)'
22
- @traces = Legion::Cache.get(TRACES_KEY) || {}
23
- @associations = Legion::Cache.get(ASSOC_KEY) || {}
24
- @dirty = false
23
+ Legion::Logging.info '[memory] CacheStore initialized (memcached-backed, per-key)'
24
+ @traces = {}
25
+ @associations = {}
26
+ @dirty_ids = Set.new
27
+ @deleted_ids = Set.new
28
+ @assoc_dirty = false
29
+ load_index
25
30
  Legion::Logging.info "[memory] CacheStore loaded #{@traces.size} traces from cache"
26
31
  end
27
32
 
28
33
  def store(trace)
29
34
  @traces[trace[:trace_id]] = trace
30
- @dirty = true
35
+ @dirty_ids << trace[:trace_id]
31
36
  trace[:trace_id]
32
37
  end
33
38
 
@@ -39,7 +44,9 @@ module Legion
39
44
  @traces.delete(trace_id)
40
45
  @associations.delete(trace_id)
41
46
  @associations.each_value { |links| links.delete(trace_id) }
42
- @dirty = true
47
+ @dirty_ids.delete(trace_id)
48
+ @deleted_ids << trace_id
49
+ @assoc_dirty = true
43
50
  end
44
51
 
45
52
  def retrieve_by_type(type, min_strength: 0.0, limit: 100)
@@ -77,7 +84,7 @@ module Legion
77
84
 
78
85
  threshold = Helpers::Trace::COACTIVATION_THRESHOLD
79
86
  link_traces(trace_id_a, trace_id_b) if @associations[trace_id_a][trace_id_b] >= threshold
80
- @dirty = true
87
+ @assoc_dirty = true
81
88
  end
82
89
 
83
90
  def all_traces(min_strength: 0.0)
@@ -121,26 +128,81 @@ module Legion
121
128
  results
122
129
  end
123
130
 
124
- # Write local state to cache
131
+ # Write dirty traces to cache as individual keys
125
132
  def flush
126
- return unless @dirty
127
-
128
- Legion::Cache.set(TRACES_KEY, @traces, TTL)
129
- Legion::Cache.set(ASSOC_KEY, strip_default_procs(@associations), TTL)
130
- @dirty = false
131
- Legion::Logging.debug "[memory] CacheStore flushed #{@traces.size} traces to cache"
133
+ flush_deleted
134
+ flush_traces
135
+ flush_associations
136
+ flush_index
137
+ Legion::Logging.debug "[memory] CacheStore flushed #{@dirty_ids.size} dirty traces (#{@traces.size} total)"
138
+ @dirty_ids.clear
139
+ @deleted_ids.clear
132
140
  end
133
141
 
134
- # Pull latest state from cache (after another process wrote)
142
+ # Pull latest state from cache
135
143
  def reload
136
- @traces = Legion::Cache.get(TRACES_KEY) || {}
137
- @associations = Legion::Cache.get(ASSOC_KEY) || {}
138
- @dirty = false
144
+ @traces.clear
145
+ @associations = {}
146
+ @dirty_ids.clear
147
+ @deleted_ids.clear
148
+ @assoc_dirty = false
149
+ load_index
139
150
  Legion::Logging.debug "[memory] CacheStore reloaded #{@traces.size} traces from cache"
140
151
  end
141
152
 
142
153
  private
143
154
 
155
+ def trace_key(trace_id)
156
+ "#{TRACE_PREFIX}#{trace_id}"
157
+ end
158
+
159
+ def load_index
160
+ index = Legion::Cache.get(INDEX_KEY)
161
+ return unless index.is_a?(Array)
162
+
163
+ loaded = 0
164
+ index.each do |id|
165
+ trace = Legion::Cache.get(trace_key(id))
166
+ if trace
167
+ @traces[id] = trace
168
+ loaded += 1
169
+ end
170
+ end
171
+ Legion::Logging.debug "[memory] CacheStore loaded #{loaded}/#{index.size} traces from index" if defined?(Legion::Logging)
172
+ rescue StandardError => e
173
+ Legion::Logging.warn "[memory] CacheStore load_index failed: #{e.message}" if defined?(Legion::Logging)
174
+ end
175
+
176
+ def flush_traces
177
+ return if @dirty_ids.empty?
178
+
179
+ @dirty_ids.each_slice(FLUSH_BATCH) do |batch|
180
+ batch.each do |id|
181
+ trace = @traces[id]
182
+ Legion::Cache.set(trace_key(id), trace, TTL) if trace
183
+ end
184
+ end
185
+ end
186
+
187
+ def flush_deleted
188
+ @deleted_ids.each do |id|
189
+ Legion::Cache.delete(trace_key(id))
190
+ end
191
+ end
192
+
193
+ def flush_associations
194
+ return unless @assoc_dirty
195
+
196
+ Legion::Cache.set(ASSOC_KEY, strip_default_procs(@associations), TTL)
197
+ @assoc_dirty = false
198
+ end
199
+
200
+ def flush_index
201
+ return if @dirty_ids.empty? && @deleted_ids.empty?
202
+
203
+ Legion::Cache.set(INDEX_KEY, @traces.keys, TTL)
204
+ end
205
+
144
206
  def strip_default_procs(hash)
145
207
  hash.each_with_object({}) do |(k, v), plain|
146
208
  plain[k] = v.is_a?(Hash) ? {}.merge(v) : v
@@ -47,7 +47,7 @@ module Legion
47
47
  if defined?(Legion::Data::Local)
48
48
  Legion::Data::Local.register_migrations(
49
49
  name: :memory,
50
- path: File.join(__dir__, 'memory', 'local_migrations')
50
+ path: File.join(__dir__, 'trace', 'local_migrations')
51
51
  )
52
52
  end
53
53
  end
@@ -4,7 +4,7 @@ module Legion
4
4
  module Extensions
5
5
  module Agentic
6
6
  module Memory
7
- VERSION = '0.1.5'
7
+ VERSION = '0.1.6'
8
8
  end
9
9
  end
10
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lex-agentic-memory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Esity