Class: Llmemory::ForgetLog

Inherits:
Object
  • Object
show all
Defined in:
lib/llmemory/forget_log.rb

Overview

Append-only audit trail of forgotten memory entries. CoALA notes that modifying and deleting memory ("unlearning") are understudied; when an agent removes knowledge it should remain accountable for what was removed, when and why. ForgetLog records that trail, unified per user across memory types.

Backed by the same pluggable short-term stores as the rest of the session layer, under a per-user pseudo-session key.

Constant Summary collapse

SESSION_KEY =
"__forget_log__"

Instance Method Summary collapse

Constructor Details

#initialize(store: nil) ⇒ ForgetLog

Returns a new instance of ForgetLog.



17
18
19
# File 'lib/llmemory/forget_log.rb', line 17

def initialize(store: nil)
  @store = store || ShortTerm::Stores.build
end

Instance Method Details

#entries(user_id) ⇒ Object



40
41
42
43
44
45
# File 'lib/llmemory/forget_log.rb', line 40

def entries(user_id)
  state = @store.load(user_id, SESSION_KEY)
  return [] unless state.is_a?(Hash)
  list = state[:entries] || state["entries"]
  list.is_a?(Array) ? list.map { |e| symbolize(e) } : []
end

#record(user_id, memory_type:, ids:, reason: nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/llmemory/forget_log.rb', line 21

def record(user_id, memory_type:, ids:, reason: nil)
  ids = Array(ids).map(&:to_s)
  entry = {
    memory_type: memory_type.to_s,
    ids: ids,
    count: ids.size,
    reason: reason,
    at: Time.now.iso8601
  }
  @store.update(user_id, SESSION_KEY) do |state|
    state = symbolize(state || {})
    log = state[:entries]
    log = log.is_a?(Array) ? log.map { |e| symbolize(e) } : []
    log << entry
    state.merge(entries: log)
  end
  entry
end