Class: OllamaAgent::Runtime::Compactor

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama_agent/runtime/compactor.rb

Overview

Prunes terminal kernel state, cold-archives old WAL events, and garbage-collects blob files.

Constant Summary collapse

EVENT_ARCHIVE_BASENAME =
"event_store_archive.db"
EVENT_EPOCH_SQL =

Logical epoch extracted from logical_stamp (epoch:seq).

"(CASE WHEN instr(logical_stamp, ':') > 0 THEN " \
"CAST(substr(logical_stamp, 1, instr(logical_stamp, ':') - 1) AS INTEGER) " \
"ELSE 0 END)"
STANDALONE_EVENTS_DDL =
<<~SQL
  CREATE TABLE IF NOT EXISTS events (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    manifest_id TEXT NOT NULL,
    logical_stamp TEXT NOT NULL,
    kind TEXT NOT NULL,
    payload BLOB NOT NULL,
    intent_hash TEXT,
    created_at TEXT NOT NULL
  );
SQL

Instance Method Summary collapse

Constructor Details

#initialize(db_registry:, blob_store:, retention_epochs: 100_000) ⇒ Compactor

Returns a new instance of Compactor.



33
34
35
36
37
# File 'lib/ollama_agent/runtime/compactor.rb', line 33

def initialize(db_registry:, blob_store:, retention_epochs: 100_000)
  @db_registry = db_registry
  @blob_store = blob_store
  @retention = retention_epochs.to_i
end

Instance Method Details

#compact(current_epoch:) ⇒ Hash

rubocop:disable Metrics/AbcSize, Metrics/MethodLength – single orchestration entry for operators

Returns:

  • (Hash)

    per-field prune/archive counts



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ollama_agent/runtime/compactor.rb', line 41

def compact(current_epoch:)
  epoch = current_epoch.to_i
  cutoff = epoch - @retention
  ref_shas = Set.new
  sagas_pruned = 0
  transitions_pruned = 0
  recovery_leases_purged = 0
  intent_reservations_purged = 0

  @db_registry.runtime.transaction(:immediate) do
    ref_shas = referenced_blob_shas
    sagas_pruned, transitions_pruned = prune_terminal_sagas_and_transitions(cutoff)
    recovery_leases_purged = purge_recovery_leases(epoch)
    intent_reservations_purged = purge_intent_reservations(cutoff)
  end

  events_archived = archive_events(cutoff: cutoff)
  blobs_collected = prune_unreferenced_blobs(ref_shas)

  counts = {
    sagas_pruned: sagas_pruned,
    transitions_pruned: transitions_pruned,
    events_archived: events_archived,
    blobs_collected: blobs_collected,
    recovery_leases_purged: recovery_leases_purged,
    intent_reservations_purged: intent_reservations_purged
  }
  log_compaction(epoch, counts)
  counts
end