Class: EnhanceSwarm::SessionManager
- Inherits:
-
Object
- Object
- EnhanceSwarm::SessionManager
- Defined in:
- lib/enhance_swarm/session_manager.rb
Constant Summary collapse
- SESSION_DIR =
'.enhance_swarm'- SESSION_FILE =
'session.json'
Instance Method Summary collapse
- #add_agent(role, pid, worktree_path, task = nil) ⇒ Object
- #check_agent_processes ⇒ Object
- #cleanup_session ⇒ Object
- #close_session ⇒ Object
- #create_session(task_description = nil) ⇒ Object
- #get_active_agents ⇒ Object
- #get_all_agents ⇒ Object
-
#initialize ⇒ SessionManager
constructor
A new instance of SessionManager.
- #read_session ⇒ Object
- #remove_agent(pid) ⇒ Object
- #session_exists? ⇒ Boolean
- #session_status ⇒ Object
- #update_agent_status(pid, status, completion_time = nil) ⇒ Object
Constructor Details
#initialize ⇒ SessionManager
Returns a new instance of SessionManager.
13 14 15 16 |
# File 'lib/enhance_swarm/session_manager.rb', line 13 def initialize @session_path = File.join(Dir.pwd, SESSION_DIR, SESSION_FILE) ensure_session_directory end |
Instance Method Details
#add_agent(role, pid, worktree_path, task = nil) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/enhance_swarm/session_manager.rb', line 32 def add_agent(role, pid, worktree_path, task = nil) session = read_session return false unless session agent_data = { role: role, pid: pid, worktree_path: worktree_path, task: task, start_time: Time.now.iso8601, status: 'running' } session[:agents] << agent_data write_session(session) Logger.info("Added agent to session: #{role} (PID: #{pid})") true end |
#check_agent_processes ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/enhance_swarm/session_manager.rb', line 157 def check_agent_processes session = read_session return [] unless session updated_agents = [] session_changed = false session[:agents].each do |agent| next unless agent[:status] == 'running' # Check if process is still running if process_running?(agent[:pid]) updated_agents << agent else # Process is no longer running, update status agent[:status] = 'stopped' agent[:completion_time] = Time.now.iso8601 session_changed = true Logger.info("Agent process stopped: #{agent[:role]} (PID: #{agent[:pid]})") end end write_session(session) if session_changed updated_agents end |
#cleanup_session ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/enhance_swarm/session_manager.rb', line 144 def cleanup_session return false unless session_exists? # Archive the session before removing if archive_session File.delete(@session_path) Logger.info("Cleaned up session file") true else false end end |
#close_session ⇒ Object
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/enhance_swarm/session_manager.rb', line 133 def close_session session = read_session return false unless session session[:status] = 'completed' session[:end_time] = Time.now.iso8601 write_session(session) Logger.info("Closed session: #{session[:session_id]}") true end |
#create_session(task_description = nil) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/enhance_swarm/session_manager.rb', line 18 def create_session(task_description = nil) session_data = { session_id: generate_session_id, start_time: Time.now.iso8601, task_description: task_description, agents: [], status: 'active' } write_session(session_data) Logger.info("Created new session: #{session_data[:session_id]}") session_data end |
#get_active_agents ⇒ Object
82 83 84 85 86 87 |
# File 'lib/enhance_swarm/session_manager.rb', line 82 def get_active_agents session = read_session return [] unless session session[:agents].select { |a| a[:status] == 'running' } end |
#get_all_agents ⇒ Object
89 90 91 92 93 94 |
# File 'lib/enhance_swarm/session_manager.rb', line 89 def get_all_agents session = read_session return [] unless session session[:agents] || [] end |
#read_session ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/enhance_swarm/session_manager.rb', line 100 def read_session return nil unless session_exists? begin content = File.read(@session_path) JSON.parse(content, symbolize_names: true) rescue JSON::ParserError, StandardError => e Logger.error("Failed to read session file: #{e.}") nil end end |
#remove_agent(pid) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/enhance_swarm/session_manager.rb', line 66 def remove_agent(pid) session = read_session return false unless session initial_count = session[:agents].length session[:agents].reject! { |a| a[:pid] == pid } if session[:agents].length < initial_count write_session(session) Logger.info("Removed agent from session: PID #{pid}") true else false end end |
#session_exists? ⇒ Boolean
96 97 98 |
# File 'lib/enhance_swarm/session_manager.rb', line 96 def session_exists? File.exist?(@session_path) end |
#session_status ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/enhance_swarm/session_manager.rb', line 112 def session_status session = read_session return { exists: false } unless session active_agents = get_active_agents all_agents = get_all_agents { exists: true, session_id: session[:session_id], start_time: session[:start_time], task_description: session[:task_description], status: session[:status], total_agents: all_agents.length, active_agents: active_agents.length, completed_agents: all_agents.count { |a| a[:status] == 'completed' }, failed_agents: all_agents.count { |a| a[:status] == 'failed' }, agents: all_agents } end |
#update_agent_status(pid, status, completion_time = nil) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/enhance_swarm/session_manager.rb', line 51 def update_agent_status(pid, status, completion_time = nil) session = read_session return false unless session agent = session[:agents].find { |a| a[:pid] == pid } return false unless agent agent[:status] = status agent[:completion_time] = completion_time if completion_time write_session(session) Logger.info("Updated agent status: PID #{pid} -> #{status}") true end |