Class: Sxn::Core::SessionManager

Inherits:
Object
  • Object
show all
Defined in:
lib/sxn/core/session_manager.rb

Overview

Manages session lifecycle and operations

Instance Method Summary collapse

Constructor Details

#initialize(config_manager = nil) ⇒ SessionManager

Returns a new instance of SessionManager.



11
12
13
14
# File 'lib/sxn/core/session_manager.rb', line 11

def initialize(config_manager = nil)
  @config_manager = config_manager || ConfigManager.new
  @database = initialize_database
end

Instance Method Details

#activate_session(name) ⇒ Object



170
171
172
173
# File 'lib/sxn/core/session_manager.rb', line 170

def activate_session(name)
  update_session_status_by_name(name, "active")
  true
end

#add_worktree_to_session(session_name, project_name, worktree_path, branch) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/sxn/core/session_manager.rb', line 118

def add_worktree_to_session(session_name, project_name, worktree_path, branch)
  session = get_session(session_name)
  raise Sxn::SessionNotFoundError, "Session '#{session_name}' not found" unless session

  # Update session metadata
  session_data = @database.get_session_by_id(session[:id])
  worktrees = session_data[:worktrees] || {}
  worktrees[project_name] = {
    path: worktree_path,
    branch: branch,
    created_at: Time.now.iso8601
  }

  projects = session_data[:projects] || []
  projects << project_name unless projects.include?(project_name)

  @database.update_session(session[:id], {
                             worktrees: worktrees,
                             projects: projects.uniq
                           })
end

#archive_session(name) ⇒ Object



165
166
167
168
# File 'lib/sxn/core/session_manager.rb', line 165

def archive_session(name)
  update_session_status_by_name(name, "archived")
  true
end

#cleanup_old_sessions(days_old = 30) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/sxn/core/session_manager.rb', line 175

def cleanup_old_sessions(days_old = 30)
  cutoff_date = Time.now.utc - (days_old * 24 * 60 * 60)
  old_sessions = @database.list_sessions.select do |session|
    session_time = Time.parse(session[:updated_at]).utc
    session_time < cutoff_date
  rescue ArgumentError
    # If we can't parse the time, err on the side of caution and don't delete
    false
  end

  old_sessions.each do |session|
    remove_session(session[:name], force: true)
  end

  old_sessions.length
end

#create_session(name, description: nil, linear_task: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sxn/core/session_manager.rb', line 16

def create_session(name, description: nil, linear_task: nil)
  validate_session_name!(name)
  ensure_sessions_folder_exists!

  raise Sxn::SessionAlreadyExistsError, "Session '#{name}' already exists" if session_exists?(name)

  session_id = SecureRandom.uuid
  session_path = File.join(@config_manager.sessions_folder_path, name)

  # Create session directory
  FileUtils.mkdir_p(session_path)

  # Create session record
  session_data = {
    id: session_id,
    name: name,
    path: session_path,
    created_at: Time.now.iso8601,
    updated_at: Time.now.iso8601,
    status: "active",
    description: description,
    linear_task: linear_task,
    projects: [],
    worktrees: {}
  }

  @database.create_session(session_data)
  session_data
rescue Sxn::Database::DuplicateSessionError => e
  raise Sxn::SessionAlreadyExistsError, e.message
end

#current_sessionObject



107
108
109
110
111
112
# File 'lib/sxn/core/session_manager.rb', line 107

def current_session
  current_name = @config_manager.current_session
  return nil unless current_name

  get_session(current_name)
end

#get_session(name) ⇒ Object



88
89
90
91
92
93
# File 'lib/sxn/core/session_manager.rb', line 88

def get_session(name)
  session = @database.get_session_by_name(name)
  session ? format_session_data(session) : nil
rescue Sxn::Database::SessionNotFoundError
  nil
end

#get_session_worktrees(session_name) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/sxn/core/session_manager.rb', line 157

def get_session_worktrees(session_name)
  session = get_session(session_name)
  return {} unless session

  session_data = @database.get_session_by_id(session[:id])
  session_data[:worktrees] || {}
end

#list_sessions(status: nil, limit: 100, filters: nil, **options) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/sxn/core/session_manager.rb', line 76

def list_sessions(status: nil, limit: 100, filters: nil, **options)
  # Support both the filters parameter and individual status parameter
  filter_hash = filters || {}
  filter_hash[:status] = status if status

  # Merge any other options
  filter_hash.merge!(options) if options.any?

  sessions = @database.list_sessions(filters: filter_hash, limit: limit)
  sessions.map { |s| format_session_data(s) }
end

#remove_session(name, force: false) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/sxn/core/session_manager.rb', line 48

def remove_session(name, force: false)
  session = get_session(name)
  raise Sxn::SessionNotFoundError, "Session '#{name}' not found" unless session

  unless force
    # Check for uncommitted changes in worktrees
    uncommitted_worktrees = find_uncommitted_worktrees(session)
    unless uncommitted_worktrees.empty?
      raise Sxn::SessionHasChangesError,
            "Session has uncommitted changes in: #{uncommitted_worktrees.join(", ")}"
    end
  end

  # Remove worktrees first
  remove_session_worktrees(session)

  # Remove session directory
  FileUtils.rm_rf(session[:path])

  # Remove from database
  @database.delete_session(session[:id])

  # Clear current session if it was this one
  @config_manager.update_current_session(nil) if @config_manager.current_session == name

  true
end

#remove_worktree_from_session(session_name, project_name) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/sxn/core/session_manager.rb', line 140

def remove_worktree_from_session(session_name, project_name)
  session = get_session(session_name)
  raise Sxn::SessionNotFoundError, "Session '#{session_name}' not found" unless session

  session_data = @database.get_session_by_id(session[:id])
  worktrees = session_data[:worktrees] || {}
  worktrees.delete(project_name)

  projects = session_data[:projects] || []
  projects.delete(project_name)

  @database.update_session(session[:id], {
                             worktrees: worktrees,
                             projects: projects
                           })
end

#session_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/sxn/core/session_manager.rb', line 114

def session_exists?(name)
  !get_session(name).nil?
end

#use_session(name) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/sxn/core/session_manager.rb', line 95

def use_session(name)
  session = get_session(name)
  raise Sxn::SessionNotFoundError, "Session '#{name}' not found" unless session

  @config_manager.update_current_session(name)

  # Update session status to active
  update_session_status(session[:id], "active")

  session
end