Chat: /new continues the same chat on a fresh session

The composer's /new used to create a separate chat. It now sends
chat_new_session. The server starts a fresh native session on the chat's
own harness (OpenCode, Codex or Claude Code) and re-keys the
operator_sessions row in one transaction. The chat keeps its title,
directory, backend, colour, star, model and mode. The old row is kept
unlisted with replaced_by, and its native session stays resumable.

The reply and the chat_session_changed push carry previous_id, so every
client swaps the row in place and an open conversation follows it to the
fresh, empty transcript with a notice.
This commit is contained in:
2026-11-03 14:06:44 +00:00
parent 251cc4f274
commit ad4233f7b8
21 changed files with 910 additions and 131 deletions
+50 -2
View File
@@ -169,7 +169,55 @@ func (s *Store) migrateOperatorSessions() error {
// ephemeral marks a chat created for automated verification. It is hidden from
// OperatorSessions but still a real row, so the session stays usable and
// closable. Existing rows default to 0 (a normal, listed chat).
return s.addColumnIfMissing("operator_sessions", "ephemeral", "INTEGER NOT NULL DEFAULT 0")
if err := s.addColumnIfMissing("operator_sessions", "ephemeral", "INTEGER NOT NULL DEFAULT 0"); err != nil {
return err
}
// 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 ''")
}
// ErrOperatorSessionReplaced reports that a chat was already moved to another
// native session, so a second, concurrent /new cannot fork it.
var ErrOperatorSessionReplaced = errors.New("this chat has already started a new session")
// ReplaceOperatorSession re-keys one operator chat onto a fresh native session
// in one transaction: a row for newID takes every shared field of oldID (title,
// directory, tag, backend, colour, star, created_at, model and mode) and is
// marked used now and open, and the old row records replaced_by so it is no
// longer listed. The old row is kept, not deleted, so its id still resolves
// and its native session stays resumable. A row already replaced, or an id
// that is not a recorded operator chat, changes nothing.
func (s *Store) ReplaceOperatorSession(ctx context.Context, oldID, newID string) error {
if oldID == "" || newID == "" || oldID == newID {
return errors.New("a distinct current and new session id are required")
}
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
var replacedBy string
err = tx.QueryRowContext(ctx, `SELECT replaced_by FROM operator_sessions WHERE session_id=? AND tag<>''`, oldID).Scan(&replacedBy)
if errors.Is(err, sql.ErrNoRows) {
return errors.New("session " + oldID + " is not an operator chat session")
}
if err != nil {
return err
}
if replacedBy != "" {
return ErrOperatorSessionReplaced
}
now := time.Now().UnixMilli()
if _, err := tx.ExecContext(ctx, `INSERT INTO operator_sessions(session_id,directory,title,tag,created_at,last_used_at,closed_at,starred_at,color,backend,ephemeral,chat_model,chat_model_provider,chat_mode)
SELECT ?,directory,title,tag,created_at,?,0,starred_at,color,backend,ephemeral,chat_model,chat_model_provider,chat_mode FROM operator_sessions WHERE session_id=?`, newID, now, oldID); err != nil {
return err
}
if _, err := tx.ExecContext(ctx, `UPDATE operator_sessions SET replaced_by=? WHERE session_id=?`, newID, oldID); err != nil {
return err
}
return tx.Commit()
}
// SetChatModelSelection saves a Pipelines next-turn override, not a remote
@@ -354,7 +402,7 @@ FROM operator_sessions WHERE session_id=? AND tag <> ''`, sessionID)
// 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
FROM operator_sessions WHERE tag <> '' AND ephemeral = 0 ORDER BY last_used_at DESC, session_id LIMIT ?`, operatorSessionListLimit)
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
}