Chat list: order chat_session_changed pushes by a registry revision
operator_sessions gains a revision bumped by a trigger on every update; the wire row carries it, and the app ignores a push (or a list read) older than the row it holds. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -41,6 +41,9 @@ type OperatorSession struct {
|
||||
// protocol.ChatColors by name, or "" for none. The name, not a shade, is
|
||||
// stored so every client renders its own theme-tuned colour.
|
||||
Color string
|
||||
// Revision increases with every change to the row (a trigger bumps it), so
|
||||
// a client receiving list metadata pushes out of order keeps the newest.
|
||||
Revision int64
|
||||
}
|
||||
|
||||
// ChatCreateBootstrap is the durable progress record for one chat_create
|
||||
@@ -175,7 +178,20 @@ func (s *Store) migrateOperatorSessions() error {
|
||||
// replaced_by names the native session a chat moved to when the operator
|
||||
// started a fresh session (/new). The replaced row is kept, unlisted, so the
|
||||
// old id still resolves; existing rows default to '' (current).
|
||||
return s.addColumnIfMissing("operator_sessions", "replaced_by", "TEXT NOT NULL DEFAULT ''")
|
||||
if err := s.addColumnIfMissing("operator_sessions", "replaced_by", "TEXT NOT NULL DEFAULT ''"); err != nil {
|
||||
return err
|
||||
}
|
||||
// revision orders a row's states: every update that does not set it bumps
|
||||
// it, so the chat_session_changed pushes (sent concurrently) can be ordered
|
||||
// by the client. SQLite does not re-fire a trigger from its own update.
|
||||
if err := s.addColumnIfMissing("operator_sessions", "revision", "INTEGER NOT NULL DEFAULT 0"); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := s.db.Exec(`CREATE TRIGGER IF NOT EXISTS operator_sessions_revision AFTER UPDATE ON operator_sessions
|
||||
FOR EACH ROW WHEN NEW.revision = OLD.revision BEGIN
|
||||
UPDATE operator_sessions SET revision = OLD.revision + 1 WHERE session_id = NEW.session_id;
|
||||
END`)
|
||||
return err
|
||||
}
|
||||
|
||||
// ErrOperatorSessionReplaced reports that a chat was already moved to another
|
||||
@@ -409,9 +425,9 @@ func (s *Store) SetOperatorSessionColor(ctx context.Context, sessionID, color st
|
||||
// returned). It is one indexed lookup and never mutates the row.
|
||||
func (s *Store) OperatorSession(ctx context.Context, sessionID string) (OperatorSession, bool, error) {
|
||||
var session OperatorSession
|
||||
row := s.db.QueryRowContext(ctx, `SELECT session_id,directory,title,tag,backend,created_at,last_used_at,closed_at,starred_at,color
|
||||
row := s.db.QueryRowContext(ctx, `SELECT session_id,directory,title,tag,backend,created_at,last_used_at,closed_at,starred_at,color,revision
|
||||
FROM operator_sessions WHERE session_id=? AND tag <> ''`, sessionID)
|
||||
if err := row.Scan(&session.ID, &session.Directory, &session.Title, &session.Tag, &session.Backend, &session.CreatedAt, &session.UpdatedAt, &session.ClosedAt, &session.StarredAt, &session.Color); err != nil {
|
||||
if err := row.Scan(&session.ID, &session.Directory, &session.Title, &session.Tag, &session.Backend, &session.CreatedAt, &session.UpdatedAt, &session.ClosedAt, &session.StarredAt, &session.Color, &session.Revision); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return OperatorSession{}, false, nil
|
||||
}
|
||||
@@ -430,7 +446,7 @@ FROM operator_sessions WHERE session_id=? AND tag <> ''`, sessionID)
|
||||
// operator can still see and reopen what they finished. 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,backend,created_at,last_used_at,closed_at,starred_at,color
|
||||
rows, err := s.db.QueryContext(ctx, `SELECT session_id,directory,title,tag,backend,created_at,last_used_at,closed_at,starred_at,color,revision
|
||||
FROM operator_sessions WHERE tag <> '' AND ephemeral = 0 AND replaced_by = '' ORDER BY last_used_at DESC, session_id LIMIT ?`, operatorSessionListLimit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -439,7 +455,7 @@ FROM operator_sessions WHERE tag <> '' AND ephemeral = 0 AND replaced_by = '' OR
|
||||
sessions := []OperatorSession{}
|
||||
for rows.Next() {
|
||||
var session OperatorSession
|
||||
if err := rows.Scan(&session.ID, &session.Directory, &session.Title, &session.Tag, &session.Backend, &session.CreatedAt, &session.UpdatedAt, &session.ClosedAt, &session.StarredAt, &session.Color); err != nil {
|
||||
if err := rows.Scan(&session.ID, &session.Directory, &session.Title, &session.Tag, &session.Backend, &session.CreatedAt, &session.UpdatedAt, &session.ClosedAt, &session.StarredAt, &session.Color, &session.Revision); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sessions = append(sessions, session)
|
||||
|
||||
@@ -320,3 +320,45 @@ func TestOperatorSessionsMigrationAddsEphemeral(t *testing.T) {
|
||||
t.Fatalf("verification session listed after migration: %+v err=%v", listed, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Every change to an operator session bumps its revision, so clients can order
|
||||
// concurrent chat_session_changed pushes.
|
||||
func TestOperatorSessionRevisionIncreasesOnEveryChange(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store, err := Open(filepath.Join(t.TempDir(), "revision.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
if err := store.RecordOperatorSession(ctx, "ses_a", "/work/a", "A", "operator"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
revision := func() int64 {
|
||||
t.Helper()
|
||||
session, found, err := store.OperatorSession(ctx, "ses_a")
|
||||
if err != nil || !found {
|
||||
t.Fatalf("read: %v %v", found, err)
|
||||
}
|
||||
return session.Revision
|
||||
}
|
||||
last := revision()
|
||||
for i, change := range []func() error{
|
||||
func() error { return store.SetOperatorSessionColor(ctx, "ses_a", "red") },
|
||||
func() error { return store.SetOperatorSessionStar(ctx, "ses_a", true) },
|
||||
func() error { return store.SetOperatorSessionTitle(ctx, "ses_a", "Renamed") },
|
||||
func() error { return store.SetOperatorSessionColor(ctx, "ses_a", "") },
|
||||
} {
|
||||
if err := change(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
next := revision()
|
||||
if next != last+1 {
|
||||
t.Fatalf("change %d: revision %d after %d", i, next, last)
|
||||
}
|
||||
last = next
|
||||
}
|
||||
sessions, err := store.OperatorSessions(ctx)
|
||||
if err != nil || len(sessions) != 1 || sessions[0].Revision != last {
|
||||
t.Fatalf("listed %+v %v, want revision %d", sessions, err, last)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -357,6 +357,10 @@ type ChatSession struct {
|
||||
Tag string `json:"tag,omitempty"`
|
||||
CreatedAt int64 `json:"created_at,omitempty"`
|
||||
UpdatedAt int64 `json:"updated_at,omitempty"`
|
||||
// Revision increases with every change to the chat's registry row. The
|
||||
// chat_session_changed pushes may arrive out of order; a client ignores one
|
||||
// older than the row it holds. 0 (older servers) is unordered.
|
||||
Revision int64 `json:"revision,omitempty"`
|
||||
// Busy is retained for wire compatibility only: it is never set by this
|
||||
// server. A session is never read-only, any number of clients may watch and
|
||||
// write it, and only a turn is serialised (a waiting prompt is acknowledged
|
||||
|
||||
@@ -156,6 +156,7 @@ func chatSessionFromOperator(session pipeline.OperatorSession) protocol.ChatSess
|
||||
CreatedAt: session.CreatedAt,
|
||||
UpdatedAt: session.UpdatedAt,
|
||||
Backend: session.Backend,
|
||||
Revision: session.Revision,
|
||||
}
|
||||
if session.ClosedAt > 0 {
|
||||
out.Closed = true
|
||||
|
||||
Reference in New Issue
Block a user