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
@@ -0,0 +1,49 @@
// `/new` for the `?chats=N` fixture chats (tool/perf/perf_fixture.dart).
//
// A `chat_new_session` moves chat i to a fresh native id, as the server
// re-keys the registry row: the chat keeps its index (title, directory,
// colour, star) and its new id opens an empty transcript.
import 'package:pipelines_app/core/api_gateway.dart';
final Map<int, int> _generation = {};
/// The current native id of fixture chat [index].
String perfChatId(int index) {
final generation = _generation[index] ?? 0;
return generation == 0
? 'perf-chat-$index'
: 'perf-chat-$index-new$generation';
}
/// The fixture chat index an id (current or replaced) belongs to.
int perfChatIndex(String id) =>
int.tryParse(id.replaceFirst('perf-chat-', '').split('-').first) ?? 0;
/// Whether [id] is a fresh session `/new` started, whose transcript is empty.
bool perfChatIsFresh(String id) => id.contains('-new');
/// Answers `chat_new_session` with [row] (the chat under its new id) built
/// after the move, or an error for a chat id that is not current.
JsonMap perfChatNewSessionReply(
JsonMap request,
JsonMap Function(int index) row,
) {
final body = request['chat_new_session'];
final previous = body is Map ? '${body['session_id'] ?? ''}' : '';
final index = perfChatIndex(previous);
if (previous != perfChatId(index)) {
return {
'id': request['id'],
'type': 'error',
'error': 'this chat has already started a new session',
};
}
_generation[index] = (_generation[index] ?? 0) + 1;
final session = {...row(index), 'previous_id': previous};
return {
'id': request['id'],
'type': 'chat_new_session',
'chat_session': session,
'chat_sessions': [session],
};
}
+19 -4
View File
@@ -12,8 +12,11 @@
// them, and keeps colours picked with chat_color;
// `?msgs=`, `?rich=1`, `?older=1`, `?live=N` vary the chat transcripts for
// scroll captures (tool/perf/chat_scroll_fixture.dart).
// `/new` in a fixture chat moves it to a fresh, empty session
// (tool/perf/chat_new_session_fixture.dart).
import 'package:pipelines_app/core/api_gateway.dart';
import 'chat_new_session_fixture.dart';
import 'chat_scroll_fixture.dart';
Map<String, String> _params = const {};
@@ -45,7 +48,7 @@ const _colorNames = ['teal', 'purple', null, 'amber', 'red', null, 'blue'];
final Map<String, String?> _pickedColors = {};
JsonMap _chatRow(int i) {
final id = 'perf-chat-$i';
final id = perfChatId(i);
if (!_colors) {
return {
'id': id,
@@ -199,8 +202,7 @@ JsonMap? perfChatReply(JsonMap request) {
final session = query is Map ? '${query['session_id'] ?? ''}' : '';
final color = query is Map ? query['color'] as String? : null;
_pickedColors[session] = color;
final index = int.tryParse(session.replaceFirst('perf-chat-', '')) ?? 0;
final row = _chatRow(index);
final row = _chatRow(perfChatIndex(session));
return {
'id': id,
'type': 'chat_color',
@@ -222,6 +224,8 @@ JsonMap? perfChatReply(JsonMap request) {
'content': json ? '{"messages":[]}\n' : '# Fixture chat\n',
},
};
case 'chat_new_session':
return perfChatNewSessionReply(request, _chatRow);
case 'chat_subscribe':
return {'id': id, 'type': 'chat_subscribed'};
case 'chat_shells':
@@ -229,13 +233,24 @@ JsonMap? perfChatReply(JsonMap request) {
case 'chat_messages':
final query = request['chat_query'];
final session = query is Map ? '${query['session_id'] ?? ''}' : '';
if (perfChatIsFresh(session)) {
return {
'id': id,
'type': 'chat_messages',
'chat_history': {
'messages': const [],
'next_cursor': '',
'has_more': false,
},
};
}
if (_scrollVariety) {
return {
'id': id,
'type': 'chat_messages',
'chat_history': chatScrollHistory(
session,
int.tryParse(session.replaceFirst('perf-chat-', '')) ?? 0,
perfChatIndex(session),
query is Map ? '${query['cursor'] ?? ''}' : '',
),
};