llmemory 0.1.16 → 0.2.0

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.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +172 -0
  3. data/lib/llmemory/actions/reason.rb +49 -0
  4. data/lib/llmemory/actions.rb +8 -0
  5. data/lib/llmemory/configuration.rb +2 -0
  6. data/lib/llmemory/forget_log.rb +50 -0
  7. data/lib/llmemory/long_term/episodic/episode.rb +94 -0
  8. data/lib/llmemory/long_term/episodic/memory.rb +120 -0
  9. data/lib/llmemory/long_term/episodic/storage.rb +31 -0
  10. data/lib/llmemory/long_term/episodic/storages/base.rb +44 -0
  11. data/lib/llmemory/long_term/episodic/storages/file_storage.rb +126 -0
  12. data/lib/llmemory/long_term/episodic/storages/memory_storage.rb +74 -0
  13. data/lib/llmemory/long_term/episodic.rb +12 -0
  14. data/lib/llmemory/long_term/file_based/memory.rb +46 -0
  15. data/lib/llmemory/long_term/graph_based/memory.rb +30 -3
  16. data/lib/llmemory/long_term/procedural/memory.rb +116 -0
  17. data/lib/llmemory/long_term/procedural/skill.rb +93 -0
  18. data/lib/llmemory/long_term/procedural/storage.rb +31 -0
  19. data/lib/llmemory/long_term/procedural/storages/base.rb +53 -0
  20. data/lib/llmemory/long_term/procedural/storages/file_storage.rb +136 -0
  21. data/lib/llmemory/long_term/procedural/storages/memory_storage.rb +80 -0
  22. data/lib/llmemory/long_term/procedural.rb +12 -0
  23. data/lib/llmemory/long_term.rb +3 -0
  24. data/lib/llmemory/memory.rb +9 -1
  25. data/lib/llmemory/memory_module.rb +55 -0
  26. data/lib/llmemory/reflection/reflector.rb +116 -0
  27. data/lib/llmemory/reflection.rb +8 -0
  28. data/lib/llmemory/retrieval/engine.rb +115 -6
  29. data/lib/llmemory/retrieval/feedback_store.rb +50 -0
  30. data/lib/llmemory/short_term/checkpoint.rb +2 -14
  31. data/lib/llmemory/short_term/session_lifecycle.rb +3 -10
  32. data/lib/llmemory/short_term/stores.rb +27 -0
  33. data/lib/llmemory/version.rb +1 -1
  34. data/lib/llmemory/working_memory.rb +83 -0
  35. data/lib/llmemory.rb +5 -0
  36. metadata +24 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 135c27c05f80b660972a5aa8df5ba315cbdd8235ac68c662ddcfe9249ca79109
4
- data.tar.gz: 2e43bacc332ddb44613c79ebd7e4e832091fc68c451a75073860502962d496d9
3
+ metadata.gz: c302656888e6373faedb5732525f76d118fd98fb67ece22278d886faec5ba3cd
4
+ data.tar.gz: ec0897226ec378e51e86e01cb66e938fbeea3db7e4c49911d7e3d08a08959d07
5
5
  SHA512:
6
- metadata.gz: 9b0d7d67c2647ec0392f993ed31aedc5d51fdc3dadf40bd1a511b90a55b24ce415a9b9a420cc6e676d5a3ba8eb780048e047eff9d0800618dc10e99c4c779a1f
7
- data.tar.gz: 10184e66c86c94976d2c164f8717633fbb438e0e95cfb0dbffed4ce52f8bbf8c4266c0b3e48879c792250da15a385464983a886408c69598044c0f60c13f27e7
6
+ metadata.gz: 6cee9a244e42f198b9fd491d164d85d2524a30d6cf9ca0b4a4da3f1a012f7e2b4643ba1e4dd6838ba7ce32517d5425b77f62df1cb1334ac34d162a273cd6400f
7
+ data.tar.gz: 6d46031eda16a52b7c8b42b364dc5351a0eb18f0eeaa135271a3976a0ebba39db439e74fafa981b9cea062d70e2bd006b4576ccaccfff29bfd6ec6fe8011a411
data/README.md CHANGED
@@ -70,6 +70,10 @@ Llmemory.configure do |config|
70
70
  config.prune_after_days = 90
71
71
  config.compact_max_bytes = 8192 # max bytes before compact! triggers
72
72
 
73
+ # Retrieval ranking signals (see "Cognitive Memory (CoALA)")
74
+ config.importance_weight = 1.0 # how strongly importance multiplies the score (0 = ignore)
75
+ config.retrieval_feedback_weight = 0.5 # how strongly useful/harmful feedback shifts ranking (0 = ignore)
76
+
73
77
  # Pre-compaction memory flush (prevents knowledge loss when compacting)
74
78
  config.memory_flush_enabled = true
75
79
  config.memory_flush_threshold_tokens = 4000
@@ -195,6 +199,174 @@ candidates = memory.search_candidates("job", top_k: 20)
195
199
 
196
200
  **Graph storage:** `:memory` (in-memory) or `:active_record` (Rails). For ActiveRecord, run `rails g llmemory:install` and migrate; the migration creates `llmemory_nodes`, `llmemory_edges`, and `llmemory_embeddings` (pgvector). Enable the `vector` extension in PostgreSQL for embeddings.
197
201
 
202
+ ## Cognitive Memory (CoALA)
203
+
204
+ llmemory implements the memory and internal-action concepts from [CoALA — Cognitive Architectures for Language Agents](https://arxiv.org/abs/2309.02427) (Sumers et al., 2024), so a framework can build agents with episodic/semantic/procedural memory, structured working memory, and reasoning/retrieval/learning actions.
205
+
206
+ | CoALA concept | llmemory |
207
+ |---|---|
208
+ | Working memory | `Llmemory::WorkingMemory` |
209
+ | Episodic memory | `Llmemory::LongTerm::Episodic::Memory` |
210
+ | Semantic memory | `FileBased::Memory` / `GraphBased::Memory` |
211
+ | Procedural memory | `Llmemory::LongTerm::Procedural::Memory` |
212
+ | Reasoning action | `Llmemory::Actions::Reason` |
213
+ | Retrieval action | `Retrieval::Engine` (+ feedback, iterative) |
214
+ | Learning action | `memorize` / `record_episode` / `register_skill` / reflection |
215
+ | Uniform interface | `Llmemory::MemoryModule` (`read`/`write`/`list`/`stats`/`forget`) |
216
+
217
+ All three long-term memories below are **additive** — episodic and procedural coexist with semantic memory rather than replacing it. Episodic/procedural ship with `:memory` and `:file` backends (SQL/ActiveRecord and vector search are roadmap items); retrieval there is keyword-based.
218
+
219
+ ### Working memory (structured, persists across LLM calls)
220
+
221
+ A symbolic scratch space for the current session, distinct from the raw message buffer. Backed by the same pluggable short-term stores, under a namespaced session key so it never collides with messages.
222
+
223
+ ```ruby
224
+ wm = Llmemory::WorkingMemory.new(user_id: "u1", session_id: "s1")
225
+ # or, from the unified API: memory.working_memory
226
+
227
+ wm.goals = ["plan a trip to Lisbon"]
228
+ wm.current_task = "find flights"
229
+ wm.set(:budget, 1000) # arbitrary custom slot
230
+
231
+ wm.goals # => ["plan a trip to Lisbon"]
232
+ wm.custom_slots # => { budget: 1000 }
233
+ wm.update(last_observation: "no direct flights", scratchpad: "try connections")
234
+ wm.to_h # full state; wm.clear! to reset
235
+ ```
236
+
237
+ Predefined slots: `goals`, `current_task`, `retrieved_context`, `scratchpad`, `last_observation`, `intermediate_reasoning`.
238
+
239
+ ### Reasoning action
240
+
241
+ Read working memory, call the LLM, write the result back — CoALA's reasoning action. Composable (reason → retrieve → reason); it does not touch long-term memory.
242
+
243
+ ```ruby
244
+ Llmemory::Actions::Reason.call(
245
+ working_memory: wm,
246
+ template: "Goal: {{goals}}. Observation: {{last_observation}}. What is the next step?",
247
+ into: :intermediate_reasoning # slot to write to (nil to not write)
248
+ )
249
+ wm.intermediate_reasoning # => the LLM's answer
250
+
251
+ # A callable template gets the working memory; `parse` transforms the output before storing:
252
+ Llmemory::Actions::Reason.call(
253
+ working_memory: wm,
254
+ template: ->(w) { "List 3 options for #{w.current_task}" },
255
+ parse: ->(out) { out.split("\n") },
256
+ into: :scratchpad
257
+ )
258
+ ```
259
+
260
+ ### Episodic memory (trajectories of experience)
261
+
262
+ Records what happened — ordered steps `(observation → action → result)` plus a summary, outcome and importance — so experiences can be retrieved as examples or distilled into knowledge by reflection.
263
+
264
+ ```ruby
265
+ episodic = Llmemory::LongTerm::Episodic::Memory.new(user_id: "u1")
266
+
267
+ id = episodic.record_episode(
268
+ steps: [{ observation: "deploy failed", action: "rolled back", result: "service restored" }],
269
+ outcome: "recovered",
270
+ importance: 0.8
271
+ )
272
+
273
+ episodic.recent_episodes(limit: 5) # newest first
274
+ episodic.search_candidates("rolled back") # retrieval-compatible candidates
275
+ ```
276
+
277
+ ### Reflection (episodic → semantic)
278
+
279
+ Distills durable, higher-order insights from recent episodes and writes them to semantic memory with provenance back to the source episodes (the Reflexion / Generative Agents pattern).
280
+
281
+ ```ruby
282
+ semantic = Llmemory::LongTerm::FileBased::Memory.new(user_id: "u1")
283
+ reflector = Llmemory::Reflection::Reflector.new(episodic: episodic, semantic: semantic)
284
+
285
+ reflector.reflect(window: 10) # reads recent episodes -> LLM -> writes insights
286
+ # Each insight is stored with provenance { method: "reflection", sources: [{ type: "episode", id: ... }] }
287
+ ```
288
+
289
+ `semantic` must respond to `remember_fact(content:, category:, importance:, provenance:)` (file-based does; graph-based is a roadmap target).
290
+
291
+ ### Procedural memory (skill library)
292
+
293
+ A Voyager-style library of reusable skills (prompts, templates, code). Skills track success/failure, and their success rate is surfaced as `importance` so proven skills rank higher in retrieval.
294
+
295
+ ```ruby
296
+ skills = Llmemory::LongTerm::Procedural::Memory.new(user_id: "u1")
297
+
298
+ id = skills.register_skill(
299
+ name: "rollback", description: "revert a bad deploy",
300
+ body: "kubectl rollout undo deployment/$1", kind: "code" # kind: prompt | template | code
301
+ )
302
+ skills.register_skill(name: "rollback", body: "...newer...") # same name -> version auto-increments
303
+
304
+ skills.find_skill("revert deploy") # best match (a Skill)
305
+ skills.report_outcome(id, success: true) # feeds ranking + adaptive retrieval
306
+ ```
307
+
308
+ ### Uniform interface (MemoryModule)
309
+
310
+ The queryable long-term memories (file, graph, episodic, procedural) share one agent-facing contract, so a framework can treat them polymorphically:
311
+
312
+ ```ruby
313
+ memory.read(query, limit: 10) # retrieve relevant entries (delegates to search_candidates)
314
+ memory.write(...) # ingest (memorize / record_episode / register_skill)
315
+ memory.list(limit: 50) # enumerate stored entries
316
+ memory.stats # counts, e.g. { items: 12 } / { episodes: 4 } / { skills: 7 }
317
+ memory.forget(ids:, reason:) # see "Forgetting" below
318
+ ```
319
+
320
+ ### Provenance (lineage of every semantic datum)
321
+
322
+ Facts (items), graph nodes/edges and reflection insights carry provenance — where they came from, how they were produced, with what confidence — so a conclusion can be traced back to its source.
323
+
324
+ ```ruby
325
+ item = storage.get_all_items("u1").first
326
+ item[:provenance]
327
+ # => { sources: [{ type: "resource", id: "res_3" }], method: "fact_extraction", confidence: 0.9, created_at: "..." }
328
+ ```
329
+
330
+ Graph nodes/edges record a SHA-256 fingerprint of the ingested text (lineage without persisting the raw document). Build provenance directly with `Llmemory::Provenance.build(method:, sources:, confidence:)`.
331
+
332
+ ### Adaptive retrieval (feedback loop)
333
+
334
+ Tell the retrieval engine which retrieved items were useful or noisy; repeatedly-useful items rank higher in future retrievals, noise is dampened. Item ids come from the candidates returned by `read` / `search_candidates`.
335
+
336
+ ```ruby
337
+ engine = Retrieval::Engine.new(memory)
338
+ results = memory.read("deployment incidents") # candidates carry :id
339
+
340
+ engine.report_feedback(useful_ids: [results.first[:id]], harmful_ids: [])
341
+ # Next retrievals reweight accordingly. Set config.retrieval_feedback_weight = 0 to disable.
342
+ ```
343
+
344
+ ### Iterative retrieval (multi-hop)
345
+
346
+ Retrieve, reason about what is still missing, then retrieve again — for multi-hop questions a single pass would miss.
347
+
348
+ ```ruby
349
+ engine.iterative_retrieve(
350
+ "What is the capital of France and its population?",
351
+ max_hops: 3
352
+ )
353
+ # After each hop an LLM proposes a follow-up query (or "DONE"). Pass a custom
354
+ # `reasoner: ->(question, accumulated, hop) { ... }` to drive the loop yourself.
355
+ ```
356
+
357
+ ### Forgetting (unlearning with audit)
358
+
359
+ Remove entries by id, with an audit trail of what was forgotten, when and why.
360
+
361
+ ```ruby
362
+ removed = memory.forget(ids: [item_id], reason: "user requested deletion") # => count removed
363
+
364
+ Llmemory::ForgetLog.new.entries("u1")
365
+ # => [{ memory_type: "file_based", ids: ["item_7"], count: 1, reason: "user requested deletion", at: "..." }]
366
+ ```
367
+
368
+ Supported for file-based, episodic and procedural memory (hard delete by id). Graph forgetting (edge/node lifecycle with orphan handling) is a roadmap item.
369
+
198
370
  ## Advanced Memory Management
199
371
 
200
372
  These features improve robustness and efficiency, inspired by OpenClaw's memory system.
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Llmemory
4
+ module Actions
5
+ # CoALA's "reasoning" action: read working memory, call the LLM, write the
6
+ # result back to working memory. Unlike retrieval (long-term -> working) or
7
+ # learning (working -> long-term), reasoning reads from and writes to working
8
+ # memory, producing new information for the current decision.
9
+ #
10
+ # It is a small, composable primitive — agents can chain reason -> retrieve
11
+ # -> reason — and deliberately does NOT touch long-term memory.
12
+ #
13
+ # Llmemory::Actions::Reason.call(
14
+ # working_memory: wm,
15
+ # template: "Given goals {{goals}}, what is the next step?",
16
+ # into: :intermediate_reasoning
17
+ # )
18
+ #
19
+ # `template` is either a String (with {{slot}} placeholders filled from
20
+ # working memory) or a callable that receives the WorkingMemory and returns
21
+ # the prompt. `parse` optionally transforms the raw LLM output before it is
22
+ # stored. `into` is the slot to write to (nil to reason without writing).
23
+ # Returns the parsed result.
24
+ class Reason
25
+ DEFAULT_SLOT = :intermediate_reasoning
26
+
27
+ def self.call(working_memory:, template:, into: DEFAULT_SLOT, parse: nil, llm: nil)
28
+ client = llm || Llmemory::LLM.client
29
+ prompt = render(template, working_memory)
30
+ output = client.invoke(prompt).to_s
31
+ result = parse ? parse.call(output) : output
32
+ working_memory.set(into, result) unless into.nil?
33
+ result
34
+ end
35
+
36
+ def self.render(template, working_memory)
37
+ return template.call(working_memory).to_s if template.respond_to?(:call)
38
+ interpolate(template.to_s, working_memory.to_h)
39
+ end
40
+
41
+ def self.interpolate(text, slots)
42
+ text.gsub(/\{\{(\w+)\}\}/) do
43
+ key = Regexp.last_match(1).to_sym
44
+ slots.key?(key) ? slots[key].to_s : ""
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "actions/reason"
4
+
5
+ module Llmemory
6
+ module Actions
7
+ end
8
+ end
@@ -15,6 +15,7 @@ module Llmemory
15
15
  :vector_store,
16
16
  :time_decay_half_life_days,
17
17
  :importance_weight,
18
+ :retrieval_feedback_weight,
18
19
  :max_retrieval_tokens,
19
20
  :prune_after_days,
20
21
  :compact_max_bytes,
@@ -58,6 +59,7 @@ module Llmemory
58
59
  @vector_store = nil
59
60
  @time_decay_half_life_days = 30
60
61
  @importance_weight = 1.0
62
+ @retrieval_feedback_weight = 0.5
61
63
  @max_retrieval_tokens = 2000
62
64
  @prune_after_days = 90
63
65
  @compact_max_bytes = 8192
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "time"
4
+ require_relative "short_term/stores"
5
+
6
+ module Llmemory
7
+ # Append-only audit trail of forgotten memory entries. CoALA notes that
8
+ # modifying and deleting memory ("unlearning") are understudied; when an agent
9
+ # removes knowledge it should remain accountable for what was removed, when and
10
+ # why. ForgetLog records that trail, unified per user across memory types.
11
+ #
12
+ # Backed by the same pluggable short-term stores as the rest of the session
13
+ # layer, under a per-user pseudo-session key.
14
+ class ForgetLog
15
+ SESSION_KEY = "__forget_log__"
16
+
17
+ def initialize(store: nil)
18
+ @store = store || ShortTerm::Stores.build
19
+ end
20
+
21
+ def record(user_id, memory_type:, ids:, reason: nil)
22
+ ids = Array(ids).map(&:to_s)
23
+ entry = {
24
+ memory_type: memory_type.to_s,
25
+ ids: ids,
26
+ count: ids.size,
27
+ reason: reason,
28
+ at: Time.now.iso8601
29
+ }
30
+ log = entries(user_id)
31
+ log << entry
32
+ @store.save(user_id, SESSION_KEY, { "entries" => log })
33
+ entry
34
+ end
35
+
36
+ def entries(user_id)
37
+ state = @store.load(user_id, SESSION_KEY)
38
+ return [] unless state.is_a?(Hash)
39
+ list = state[:entries] || state["entries"]
40
+ list.is_a?(Array) ? list.map { |e| symbolize(e) } : []
41
+ end
42
+
43
+ private
44
+
45
+ def symbolize(entry)
46
+ return entry unless entry.is_a?(Hash)
47
+ entry.each_with_object({}) { |(k, v), acc| acc[k.to_sym] = v }
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "time"
4
+
5
+ module Llmemory
6
+ module LongTerm
7
+ module Episodic
8
+ # An Episode is a trajectory of an agent's experience: an ordered list of
9
+ # steps (observation -> action -> result) plus a summary, an outcome label
10
+ # and an importance score. This is CoALA's "episodic memory" — distinct
11
+ # from semantic memory (facts), it stores what happened so it can later be
12
+ # retrieved as examples or distilled into semantic knowledge (see P2,
13
+ # reflection).
14
+ class Episode
15
+ attr_reader :id, :user_id, :steps, :summary, :outcome, :importance, :provenance, :created_at
16
+
17
+ STEP_KEYS = %i[observation action result timestamp].freeze
18
+
19
+ def initialize(id:, user_id:, steps: [], summary: nil, outcome: nil, importance: 0.5, provenance: nil, created_at: nil)
20
+ @id = id
21
+ @user_id = user_id
22
+ @steps = self.class.normalize_steps(steps)
23
+ @summary = summary
24
+ @outcome = outcome
25
+ @importance = importance.nil? ? 0.5 : importance.to_f
26
+ @provenance = provenance
27
+ @created_at = created_at || Time.now
28
+ end
29
+
30
+ # Flat, searchable representation used for keyword retrieval and, in the
31
+ # future, embedding. Combines summary, outcome and every step field.
32
+ def searchable_text
33
+ parts = [summary, outcome]
34
+ steps.each do |s|
35
+ parts << s[:observation]
36
+ parts << s[:action]
37
+ parts << s[:result]
38
+ end
39
+ parts.compact.map(&:to_s).reject(&:empty?).join("\n")
40
+ end
41
+
42
+ def self.normalize_steps(steps)
43
+ Array(steps).filter_map do |step|
44
+ next nil unless step.is_a?(Hash)
45
+ {
46
+ observation: step[:observation] || step["observation"],
47
+ action: step[:action] || step["action"],
48
+ result: step[:result] || step["result"],
49
+ timestamp: normalize_time(step[:timestamp] || step["timestamp"])
50
+ }
51
+ end
52
+ end
53
+
54
+ def self.normalize_time(value)
55
+ return nil if value.nil?
56
+ value.respond_to?(:iso8601) ? value.iso8601 : value.to_s
57
+ end
58
+
59
+ def self.from_h(hash)
60
+ new(
61
+ id: hash[:id] || hash["id"],
62
+ user_id: hash[:user_id] || hash["user_id"],
63
+ steps: hash[:steps] || hash["steps"] || [],
64
+ summary: hash[:summary] || hash["summary"],
65
+ outcome: hash[:outcome] || hash["outcome"],
66
+ importance: hash[:importance] || hash["importance"] || 0.5,
67
+ provenance: hash[:provenance] || hash["provenance"],
68
+ created_at: parse_created_at(hash[:created_at] || hash["created_at"])
69
+ )
70
+ end
71
+
72
+ def self.parse_created_at(value)
73
+ return value if value.nil? || value.is_a?(Time)
74
+ Time.parse(value.to_s)
75
+ rescue ArgumentError
76
+ nil
77
+ end
78
+
79
+ def to_h
80
+ {
81
+ id: id,
82
+ user_id: user_id,
83
+ steps: steps,
84
+ summary: summary,
85
+ outcome: outcome,
86
+ importance: importance,
87
+ provenance: provenance,
88
+ created_at: created_at.respond_to?(:iso8601) ? created_at.iso8601(6) : created_at
89
+ }
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,120 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "episode"
4
+ require_relative "storage"
5
+ require_relative "../../memory_module"
6
+
7
+ module Llmemory
8
+ module LongTerm
9
+ module Episodic
10
+ # Episodic long-term memory: records agent trajectories and retrieves them
11
+ # by recency, importance and relevance. Designed to coexist with semantic
12
+ # memory (file/graph), not replace it, and to feed reflection (P2), which
13
+ # distills episodes into semantic knowledge.
14
+ #
15
+ # Deliberately LLM-free: recording and retrieval are deterministic. Higher
16
+ # order summarization belongs to reflection.
17
+ class Memory
18
+ include Llmemory::MemoryModule
19
+
20
+ attr_reader :user_id, :storage
21
+
22
+ def initialize(user_id:, storage: nil)
23
+ @user_id = user_id
24
+ @storage = storage || Storages.build
25
+ end
26
+
27
+ # Records a trajectory. `steps` is an array of hashes with any of
28
+ # :observation, :action, :result, :timestamp. Returns the episode id.
29
+ def record_episode(steps:, summary: nil, outcome: nil, importance: 0.5)
30
+ episode = Episode.new(
31
+ id: nil,
32
+ user_id: @user_id,
33
+ steps: steps,
34
+ summary: summary || derive_summary(steps),
35
+ outcome: outcome,
36
+ importance: importance
37
+ )
38
+ provenance = Llmemory::Provenance.from_text_fingerprint(
39
+ episode.searchable_text, method: "episode_recording", confidence: episode.importance
40
+ )
41
+ record = episode.to_h.merge(provenance: provenance)
42
+ @storage.save_episode(@user_id, record)
43
+ end
44
+
45
+ def recent_episodes(limit: 10)
46
+ @storage.list_episodes(@user_id, limit: limit).map { |e| Episode.from_h(e) }
47
+ end
48
+
49
+ def episodes(limit: nil)
50
+ @storage.list_episodes(@user_id, limit: limit).map { |e| Episode.from_h(e) }
51
+ end
52
+
53
+ def find_episode(id)
54
+ raw = @storage.get_episode(@user_id, id)
55
+ raw && Episode.from_h(raw)
56
+ end
57
+
58
+ def count
59
+ @storage.count_episodes(@user_id)
60
+ end
61
+
62
+ # Retrieval Engine integration. Returns candidates shaped like the other
63
+ # long-term memories so the Engine can rank episodes by relevance,
64
+ # recency (temporal decay) and importance (P3), with provenance (P10).
65
+ def search_candidates(query, user_id: nil, top_k: 20)
66
+ uid = user_id || @user_id
67
+ return [] unless uid == @user_id
68
+
69
+ @storage.search_episodes(uid, query).first(top_k).map do |e|
70
+ episode = Episode.from_h(e)
71
+ {
72
+ id: episode.id,
73
+ text: episode.summary.to_s.empty? ? episode.searchable_text : episode.summary,
74
+ timestamp: episode.created_at,
75
+ score: 1.0,
76
+ importance: episode.importance,
77
+ evergreen: false,
78
+ provenance: e[:provenance] || e["provenance"]
79
+ }
80
+ end
81
+ end
82
+
83
+ # --- MemoryModule uniform interface ---
84
+
85
+ def write(steps:, summary: nil, outcome: nil, importance: 0.5, **_meta)
86
+ record_episode(steps: steps, summary: summary, outcome: outcome, importance: importance)
87
+ end
88
+
89
+ def list(user_id: nil, limit: nil)
90
+ episodes(limit: limit)
91
+ end
92
+
93
+ def stats(user_id: nil)
94
+ { episodes: count }
95
+ end
96
+
97
+ def forget(ids:, reason: nil)
98
+ requested = Array(ids).map(&:to_s)
99
+ existing = @storage.list_episodes(@user_id).map { |e| (e[:id] || e["id"]).to_s }
100
+ removed = requested & existing
101
+ @storage.delete_episodes(@user_id, removed)
102
+ forget_log.record(@user_id, memory_type: "episodic", ids: removed, reason: reason)
103
+ removed.size
104
+ end
105
+
106
+ private
107
+
108
+ # Cheap, deterministic summary when the caller does not provide one.
109
+ # LLM-based summarization is reflection's job (P2).
110
+ def derive_summary(steps)
111
+ normalized = Episode.normalize_steps(steps)
112
+ return nil if normalized.empty?
113
+ actions = normalized.filter_map { |s| s[:action] }.reject { |a| a.to_s.strip.empty? }
114
+ return nil if actions.empty?
115
+ "Episode with #{normalized.size} step(s): #{actions.join(' -> ')}"
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "storages/base"
4
+ require_relative "storages/memory_storage"
5
+ require_relative "storages/file_storage"
6
+
7
+ module Llmemory
8
+ module LongTerm
9
+ module Episodic
10
+ # Backward compatibility: Storage points to the in-memory backend.
11
+ Storage = Storages::MemoryStorage
12
+
13
+ module Storages
14
+ def self.build(store: nil, base_path: nil)
15
+ case (store || Llmemory.configuration.long_term_store).to_s.to_sym
16
+ when :memory
17
+ MemoryStorage.new
18
+ when :file
19
+ FileStorage.new(base_path: base_path || Llmemory.configuration.long_term_storage_path)
20
+ when :postgres, :database, :active_record, :activerecord
21
+ raise NotImplementedError,
22
+ "Episodic SQL/ActiveRecord storage is not implemented yet; use :memory or :file " \
23
+ "(or pass an explicit storage instance)."
24
+ else
25
+ MemoryStorage.new
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Llmemory
4
+ module LongTerm
5
+ module Episodic
6
+ module Storages
7
+ # Storage contract for episodic memory. Implementations persist Episode
8
+ # hashes and expose recency-ordered listing plus keyword search so the
9
+ # retrieval Engine can rank episodes alongside other memory types.
10
+ class Base
11
+ def save_episode(user_id, episode)
12
+ raise NotImplementedError, "#{self.class}#save_episode must be implemented"
13
+ end
14
+
15
+ def get_episode(user_id, id)
16
+ raise NotImplementedError, "#{self.class}#get_episode must be implemented"
17
+ end
18
+
19
+ # Newest first. Optionally capped by limit.
20
+ def list_episodes(user_id, limit: nil)
21
+ raise NotImplementedError, "#{self.class}#list_episodes must be implemented"
22
+ end
23
+
24
+ def search_episodes(user_id, query)
25
+ raise NotImplementedError, "#{self.class}#search_episodes must be implemented"
26
+ end
27
+
28
+ def count_episodes(user_id)
29
+ raise NotImplementedError, "#{self.class}#count_episodes must be implemented"
30
+ end
31
+
32
+ # Deletes episodes by id. Returns the number actually removed.
33
+ def delete_episodes(user_id, ids)
34
+ raise NotImplementedError, "#{self.class}#delete_episodes must be implemented"
35
+ end
36
+
37
+ def list_users
38
+ raise NotImplementedError, "#{self.class}#list_users must be implemented"
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end