Class: Rubino::Memory::Flusher

Inherits:
Object
  • Object
show all
Defined in:
lib/rubino/memory/flusher.rb

Overview

Flushes working memory to persistent storage before compaction. Ensures no important information is lost when context is compressed.

Instance Method Summary collapse

Constructor Details

#initialize(backend: nil, config: nil) ⇒ Flusher

Returns a new instance of Flusher.



8
9
10
11
# File 'lib/rubino/memory/flusher.rb', line 8

def initialize(backend: nil, config: nil)
  @backend = backend
  @config = config
end

Instance Method Details

#flush_before_compaction!(session_id) ⇒ Object

Flushes all pending memories for a session before compaction. Routes through the configured backend’s extract path so compaction mines facts with the same backend the rest of the gem uses.



16
17
18
19
20
21
22
23
# File 'lib/rubino/memory/flusher.rb', line 16

def flush_before_compaction!(session_id)
  extracted = backend.extract(session_id)

  {
    flushed_count: extracted.size,
    session_id: session_id
  }
end

#flush_on_session_end!(session_id) ⇒ Object

Mines any un-extracted turns when a session closes (#554). The turn-based auto-extract gate (memory.auto_extract_interval, default 10) only fires when the turn counter LANDS on the interval, so a session that ends with fewer turns than the interval — and no compaction — never extracted: the “tell a fact one session, recall it the next” workflow silently dropped short chats. This is the end-of-session catch-all, mirroring Hermes’ MemoryProvider#on_session_end. Idempotent and bounded by the SAME per-session extraction watermark (sessions.memory_extracted_msg_id) the backend already uses, so it only mines turns not yet extracted — a second flush (or a flush after a 10-turn-interval extract already ran) mines nothing new. Respects the memory config gates: a no-op when memory is disabled or auto_extract is off. Best-effort: never breaks the exit path.



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rubino/memory/flusher.rb', line 37

def flush_on_session_end!(session_id)
  return { flushed_count: 0, session_id: session_id } unless extract_enabled?

  extracted = backend.extract(session_id)

  {
    flushed_count: extracted.size,
    session_id: session_id
  }
rescue StandardError
  { flushed_count: 0, session_id: session_id }
end