package pipeline import ( "context" "time" ) // operatorSessionListLimit bounds one OperatorSessions read so a long-lived // database with unbounded operator-session history cannot make one chat_sessions // request scan it all. Rows are read most-recently-used first, so the bound only // ever drops the oldest sessions, never one the operator just used. It mirrors // the bounded newest-first convention of sessionOwnerScanLimit. const operatorSessionListLimit = 128 // OperatorSession is one recorded operator chat session: a chat the person // created through chat_create, tagged with its kind and carrying the directory // it lives in. CreatedAt is when the row was first recorded; UpdatedAt is the // last-used epoch-millisecond timestamp that drives most-recently-used ordering. type OperatorSession struct { ID string Directory string Title string Tag string CreatedAt int64 UpdatedAt int64 } // initOperatorSessions creates the operator-session registry. It is a table of // its own rather than a kind column on run_steps so a pipeline run's session // ownership (run_session_owners, SessionOwner) is untouched and the two // vocabularies cannot be confused. session_id is the primary key, so lookups and // the upsert use the index; operator_sessions_recent serves the recency order. func (s *Store) initOperatorSessions() error { _, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS operator_sessions ( session_id TEXT PRIMARY KEY, directory TEXT NOT NULL, title TEXT NOT NULL DEFAULT '', tag TEXT NOT NULL DEFAULT '', created_at INTEGER NOT NULL, last_used_at INTEGER NOT NULL); CREATE INDEX IF NOT EXISTS operator_sessions_recent ON operator_sessions(last_used_at DESC);`) return err } // RecordOperatorSession upserts one operator chat session in a single statement: // a new id is inserted with its created_at and last_used_at set to now, and a // repeated id updates directory/title/tag and moves last_used_at to now while // preserving the original created_at. CreatedAt is therefore stable across // re-records. func (s *Store) RecordOperatorSession(ctx context.Context, sessionID, directory, title, tag string) error { now := time.Now().UnixMilli() _, err := s.db.ExecContext(ctx, `INSERT INTO operator_sessions(session_id,directory,title,tag,created_at,last_used_at) VALUES(?,?,?,?,?,?) ON CONFLICT(session_id) DO UPDATE SET directory=excluded.directory,title=excluded.title,tag=excluded.tag,last_used_at=excluded.last_used_at`, sessionID, directory, title, tag, now, now) return err } // TouchOperatorSession records that an operator session was just used, in one // UPDATE. A session id that is not an operator session updates no row and is not // an error: the statement is itself the presence check, so no second read is // needed. func (s *Store) TouchOperatorSession(ctx context.Context, sessionID string) error { _, err := s.db.ExecContext(ctx, "UPDATE operator_sessions SET last_used_at=? WHERE session_id=?", time.Now().UnixMilli(), sessionID) return err } // OperatorSessions reports the operator's chat sessions, most-recently-used // first (ties broken by session id for a deterministic order), capped at // operatorSessionListLimit. Rows with no tag are omitted: a session with no // recorded tag is not an operator session and must never be listed. It is one // bounded statement; recency is never mutated here. func (s *Store) OperatorSessions(ctx context.Context) ([]OperatorSession, error) { rows, err := s.db.QueryContext(ctx, `SELECT session_id,directory,title,tag,created_at,last_used_at FROM operator_sessions WHERE tag <> '' ORDER BY last_used_at DESC, session_id LIMIT ?`, operatorSessionListLimit) if err != nil { return nil, err } defer rows.Close() sessions := []OperatorSession{} for rows.Next() { var session OperatorSession if err := rows.Scan(&session.ID, &session.Directory, &session.Title, &session.Tag, &session.CreatedAt, &session.UpdatedAt); err != nil { return nil, err } sessions = append(sessions, session) } return sessions, rows.Err() } // OperatorDirectories reports the distinct directories of the operator's // sessions, most-recently-used first, deduplicated in SQL and capped at limit. // It is one bounded statement, newest use per directory wins the ordering, and // it never mutates recency. A non-positive limit returns no directories. func (s *Store) OperatorDirectories(ctx context.Context, limit int) ([]string, error) { if limit <= 0 { return nil, nil } rows, err := s.db.QueryContext(ctx, `SELECT directory FROM operator_sessions WHERE tag <> '' GROUP BY directory ORDER BY MAX(last_used_at) DESC, directory LIMIT ?`, limit) if err != nil { return nil, err } defer rows.Close() directories := []string{} for rows.Next() { var directory string if err := rows.Scan(&directory); err != nil { return nil, err } directories = append(directories, directory) } return directories, rows.Err() }