Class: Rubino::Session::SummaryStore

Inherits:
Object
  • Object
show all
Defined in:
lib/rubino/session/summary_store.rb

Overview

Single owner of the ‘session_summaries` table.

Compaction summaries used to be read and written from three places (Context::Compressor, Context::SummaryBuilder, Context::PromptAssembler) with near-identical Sequel blocks that DIVERGED: the compressor stamped parent_summary_id to chain lineage, the builder’s own save! did not — so whether a summary linked to its parent depended on which code path happened to write it. Centralising here means the row shape and the parent lineage live in exactly one place.

“latest” is defined as the most recent created_at for a session (iso8601, ordered desc) — the same ordering every former caller used.

Instance Method Summary collapse

Constructor Details

#initialize(db: nil) ⇒ SummaryStore

Returns a new instance of SummaryStore.



20
21
22
# File 'lib/rubino/session/summary_store.rb', line 20

def initialize(db: nil)
  @db = db || Rubino.database.db
end

Instance Method Details

#insert(session_id:, content:) ⇒ Object

Inserts a new summary, chaining parent_summary_id to the current latest so lineage is always recorded regardless of caller. Returns the new id.



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rubino/session/summary_store.rb', line 43

def insert(session_id:, content:)
  id = SecureRandom.uuid
  @db[:session_summaries].insert(
    id: id,
    session_id: session_id,
    parent_summary_id: latest_id(session_id),
    content: content,
    token_count: (content.length / 4.0).ceil,
    created_at: Time.now.utc.iso8601
  )
  id
end

#latest(session_id) ⇒ Object

Most recent summary record for a session (or nil).



25
26
27
# File 'lib/rubino/session/summary_store.rb', line 25

def latest(session_id)
  dataset(session_id).first
end

#latest_content(session_id) ⇒ Object

Content of the most recent summary (or nil) — the read path used when only the text is needed (prompt assembly, previous-summary carry-over).



31
32
33
# File 'lib/rubino/session/summary_store.rb', line 31

def latest_content(session_id)
  latest(session_id)&.dig(:content)
end

#latest_id(session_id) ⇒ Object

Id of the most recent summary (or nil) — used as the parent link when recording compaction lineage.



37
38
39
# File 'lib/rubino/session/summary_store.rb', line 37

def latest_id(session_id)
  latest(session_id)&.dig(:id)
end