Class: Legion::Extensions::Agentic::Executive::GoalManagement::Helpers::GoalPersistence

Inherits:
Object
  • Object
show all
Includes:
Cache::Helper, JSON::Helper
Defined in:
lib/legion/extensions/agentic/executive/goal_management/helpers/goal_persistence.rb

Constant Summary collapse

GOAL_TTL =
86_400
INDEX_KEY_SUFFIX =
':index'
BRIDGE_KEY_SUFFIX =
':bridge'

Instance Method Summary collapse

Constructor Details

#initialize(namespace: 'legion_goals') ⇒ GoalPersistence

Returns a new instance of GoalPersistence.



17
18
19
# File 'lib/legion/extensions/agentic/executive/goal_management/helpers/goal_persistence.rb', line 17

def initialize(namespace: 'legion_goals')
  @namespace = namespace
end

Instance Method Details

#delete_goal(id) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/legion/extensions/agentic/executive/goal_management/helpers/goal_persistence.rb', line 44

def delete_goal(id)
  return false unless persistence_available?

  cache_delete(goal_key(id))
  update_index(id, :remove)
  true
rescue StandardError => e
  log.error "[goal_persistence] delete_goal failed: #{e.message}"
  false
end

#load_allObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/legion/extensions/agentic/executive/goal_management/helpers/goal_persistence.rb', line 65

def load_all
  return {} unless persistence_available?

  ids = load_index
  return {} if ids.empty?

  goals = ids.each_with_object({}) do |id, result|
    goal = load_goal(id)
    result[id] = goal if goal
  end
  log.info "[goal_persistence] rehydrated #{goals.size} goals from cache"
  goals
rescue StandardError => e
  log.error "[goal_persistence] load_all failed: #{e.message}"
  {}
end

#load_bridge_stateObject



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/legion/extensions/agentic/executive/goal_management/helpers/goal_persistence.rb', line 92

def load_bridge_state
  return {} unless persistence_available?

  raw = cache_get(bridge_key)
  return {} unless raw

  json_load(raw)
rescue StandardError => e
  log.error "[goal_persistence] load_bridge_state failed: #{e.message}"
  {}
end

#load_goal(id) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/legion/extensions/agentic/executive/goal_management/helpers/goal_persistence.rb', line 32

def load_goal(id)
  return nil unless persistence_available?

  raw = cache_get(goal_key(id))
  return nil unless raw

  json_load(raw)
rescue StandardError => e
  log.error "[goal_persistence] load_goal failed: #{e.message}"
  nil
end

#save_all(goals_hash) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/legion/extensions/agentic/executive/goal_management/helpers/goal_persistence.rb', line 55

def save_all(goals_hash)
  return false unless persistence_available?

  goals_hash.each_value { |g| save_goal(g.is_a?(Hash) ? g : g.to_h) }
  true
rescue StandardError => e
  log.error "[goal_persistence] save_all failed: #{e.message}"
  false
end

#save_bridge_state(bridge_hash) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/legion/extensions/agentic/executive/goal_management/helpers/goal_persistence.rb', line 82

def save_bridge_state(bridge_hash)
  return false unless persistence_available?

  cache_set(bridge_key, json_dump(bridge_hash), ttl: GOAL_TTL)
  true
rescue StandardError => e
  log.error "[goal_persistence] save_bridge_state failed: #{e.message}"
  false
end

#save_goal(goal_hash) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/legion/extensions/agentic/executive/goal_management/helpers/goal_persistence.rb', line 21

def save_goal(goal_hash)
  return false unless persistence_available?

  cache_set(goal_key(goal_hash[:id]), json_dump(goal_hash), ttl: GOAL_TTL)
  update_index(goal_hash[:id], :add)
  true
rescue StandardError => e
  log.error "[goal_persistence] save_goal failed: #{e.message}"
  false
end