@@ -49,7 +49,18 @@ func ResolveSessionID(ctx context.Context, store Store, ref string) (string, err
4949 if ! isRelative {
5050 return ref , nil
5151 }
52- return store .GetSessionByOffset (ctx , offset )
52+
53+ summaries , err := store .GetSessionSummaries (ctx )
54+ if err != nil {
55+ return "" , fmt .Errorf ("getting session summaries: %w" , err )
56+ }
57+
58+ index := offset - 1
59+ if index >= len (summaries ) {
60+ return "" , fmt .Errorf ("session offset %d out of range (have %d sessions)" , offset , len (summaries ))
61+ }
62+
63+ return summaries [index ].ID , nil
5364}
5465
5566// Summary contains lightweight session metadata for listing purposes.
@@ -72,12 +83,6 @@ type Store interface {
7283 DeleteSession (ctx context.Context , id string ) error
7384 UpdateSession (ctx context.Context , session * Session ) error // Updates metadata only (not messages/items)
7485 SetSessionStarred (ctx context.Context , id string , starred bool ) error
75- BranchSession (ctx context.Context , parentSessionID string , branchAtPosition int ) (* Session , error )
76-
77- // GetSessionByOffset returns the session ID at the given offset from the most recent.
78- // Offset 1 returns the most recent session, 2 returns the second most recent, etc.
79- // Only root sessions are considered (sub-sessions are excluded).
80- GetSessionByOffset (ctx context.Context , offset int ) (string , error )
8186
8287 // === Granular item operations ===
8388
@@ -147,6 +152,9 @@ func (s *InMemorySessionStore) GetSessions(_ context.Context) ([]*Session, error
147152func (s * InMemorySessionStore ) GetSessionSummaries (_ context.Context ) ([]Summary , error ) {
148153 summaries := make ([]Summary , 0 , s .sessions .Length ())
149154 s .sessions .Range (func (_ string , value * Session ) bool {
155+ if value .ParentID != "" {
156+ return true
157+ }
150158 summaries = append (summaries , Summary {
151159 ID : value .ID ,
152160 Title : value .Title ,
@@ -156,6 +164,9 @@ func (s *InMemorySessionStore) GetSessionSummaries(_ context.Context) ([]Summary
156164 })
157165 return true
158166 })
167+ sort .Slice (summaries , func (i , j int ) bool {
168+ return summaries [i ].CreatedAt .After (summaries [j ].CreatedAt )
169+ })
159170 return summaries , nil
160171}
161172
@@ -206,25 +217,6 @@ func (s *InMemorySessionStore) SetSessionStarred(_ context.Context, id string, s
206217 return nil
207218}
208219
209- // BranchSession creates a new session branched from the parent at the given position.
210- func (s * InMemorySessionStore ) BranchSession (_ context.Context , parentSessionID string , branchAtPosition int ) (* Session , error ) {
211- if parentSessionID == "" {
212- return nil , ErrEmptyID
213- }
214- parent , exists := s .sessions .Load (parentSessionID )
215- if ! exists {
216- return nil , ErrNotFound
217- }
218-
219- branched , err := buildBranchedSession (parent , branchAtPosition )
220- if err != nil {
221- return nil , err
222- }
223-
224- s .sessions .Store (branched .ID , branched )
225- return branched , nil
226- }
227-
228220// AddMessage adds a message to a session at the next position.
229221// Returns the ID of the created message (for in-memory, this is a simple counter).
230222func (s * InMemorySessionStore ) AddMessage (_ context.Context , sessionID string , msg * Message ) (int64 , error ) {
@@ -358,34 +350,6 @@ func (s *InMemorySessionStore) UpdateSessionTitle(_ context.Context, sessionID,
358350 return nil
359351}
360352
361- // GetSessionByOffset returns the session ID at the given offset from the most recent.
362- func (s * InMemorySessionStore ) GetSessionByOffset (_ context.Context , offset int ) (string , error ) {
363- if offset < 1 {
364- return "" , fmt .Errorf ("offset must be >= 1, got %d" , offset )
365- }
366-
367- // Collect and sort sessions by creation time (newest first)
368- var sessions []* Session
369- s .sessions .Range (func (_ string , value * Session ) bool {
370- // Only include root sessions (not sub-sessions)
371- if value .ParentID == "" {
372- sessions = append (sessions , value )
373- }
374- return true
375- })
376-
377- sort .Slice (sessions , func (i , j int ) bool {
378- return sessions [i ].CreatedAt .After (sessions [j ].CreatedAt )
379- })
380-
381- index := offset - 1 // offset 1 means index 0 (most recent session)
382- if index >= len (sessions ) {
383- return "" , fmt .Errorf ("session offset %d out of range (have %d sessions)" , offset , len (sessions ))
384- }
385-
386- return sessions [index ].ID , nil
387- }
388-
389353// NewSQLiteSessionStore creates a new SQLite session store
390354func NewSQLiteSessionStore (path string ) (Store , error ) {
391355 store , err := openAndMigrateSQLiteStore (path )
@@ -1091,37 +1055,6 @@ func (s *SQLiteSessionStore) SetSessionStarred(ctx context.Context, id string, s
10911055 return nil
10921056}
10931057
1094- // BranchSession creates a new session branched from the parent at the given position.
1095- func (s * SQLiteSessionStore ) BranchSession (ctx context.Context , parentSessionID string , branchAtPosition int ) (* Session , error ) {
1096- if parentSessionID == "" {
1097- return nil , ErrEmptyID
1098- }
1099-
1100- parent , err := s .GetSession (ctx , parentSessionID )
1101- if err != nil {
1102- return nil , err
1103- }
1104-
1105- branched , err := buildBranchedSession (parent , branchAtPosition )
1106- if err != nil {
1107- return nil , err
1108- }
1109-
1110- if err := s .AddSession (ctx , branched ); err != nil {
1111- return nil , err
1112- }
1113-
1114- ids := make (map [string ]struct {})
1115- collectSessionIDs (branched , ids )
1116- for id := range ids {
1117- if err := s .syncMessagesColumn (ctx , id ); err != nil {
1118- slog .Warn ("[STORE] Failed to sync messages column after branch" , "session_id" , id , "error" , err )
1119- }
1120- }
1121-
1122- return branched , nil
1123- }
1124-
11251058// Close closes the database connection
11261059func (s * SQLiteSessionStore ) Close () error {
11271060 return s .db .Close ()
@@ -1400,28 +1333,3 @@ func (s *SQLiteSessionStore) UpdateSessionTitle(ctx context.Context, sessionID,
14001333 title , sessionID )
14011334 return err
14021335}
1403-
1404- // GetSessionByOffset returns the session ID at the given offset from the most recent.
1405- func (s * SQLiteSessionStore ) GetSessionByOffset (ctx context.Context , offset int ) (string , error ) {
1406- if offset < 1 {
1407- return "" , fmt .Errorf ("offset must be >= 1, got %d" , offset )
1408- }
1409-
1410- // Query sessions ordered by creation time (newest first), limited to offset
1411- // Only include root sessions (not sub-sessions)
1412- var sessionID string
1413- err := s .db .QueryRowContext (ctx ,
1414- `SELECT id FROM sessions
1415- WHERE parent_id IS NULL OR parent_id = ''
1416- ORDER BY created_at DESC
1417- LIMIT 1 OFFSET ?` ,
1418- offset - 1 ).Scan (& sessionID )
1419- if err != nil {
1420- if errors .Is (err , sql .ErrNoRows ) {
1421- return "" , fmt .Errorf ("session offset %d out of range" , offset )
1422- }
1423- return "" , err
1424- }
1425-
1426- return sessionID , nil
1427- }
0 commit comments