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



194
195
196
197
# File 'lib/sxn/core/session_manager.rb', line 194

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



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/sxn/core/session_manager.rb', line 133

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



189
190
191
192
# File 'lib/sxn/core/session_manager.rb', line 189

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

#cleanup_old_sessions(days_old = 30) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/sxn/core/session_manager.rb', line 199

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, default_branch: nil, template_id: 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sxn/core/session_manager.rb', line 16

def create_session(name, description: nil, linear_task: nil, default_branch: nil, template_id: 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)
  branch = default_branch || name

  # Create session directory
  FileUtils.mkdir_p(session_path)

  # Create .sxnrc configuration file
  session_config = SessionConfig.new(session_path)
  session_config.create(
    parent_sxn_path: @config_manager.sxn_folder_path,
    default_branch: branch,
    session_name: name,
    template_id: template_id
  )

  # Create session record with template_id in metadata
   = {}
  ["template_id"] = template_id if template_id

  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,
    default_branch: branch,
    projects: [],
    worktrees: {},
    metadata: 
  }

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

#current_sessionObject



122
123
124
125
126
127
# File 'lib/sxn/core/session_manager.rb', line 122

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

  get_session(current_name)
end

#get_session(name) ⇒ Object



103
104
105
106
107
108
# File 'lib/sxn/core/session_manager.rb', line 103

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_default_branch(session_name) ⇒ Object



180
181
182
183
184
185
186
187
# File 'lib/sxn/core/session_manager.rb', line 180

def get_session_default_branch(session_name)
  session = get_session(session_name)
  return nil unless session

  # Read from .sxnrc file in session directory
  session_config = SessionConfig.new(session[:path])
  session_config.default_branch
end

#get_session_worktrees(session_name) ⇒ Object



172
173
174
175
176
177
178
# File 'lib/sxn/core/session_manager.rb', line 172

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



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/sxn/core/session_manager.rb', line 91

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sxn/core/session_manager.rb', line 63

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



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/sxn/core/session_manager.rb', line 155

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)


129
130
131
# File 'lib/sxn/core/session_manager.rb', line 129

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

#use_session(name) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/sxn/core/session_manager.rb', line 110

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