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,74 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/data/chat_repository.dart';
import 'package:pipelines_app/features/chat/ui/sessions/chat_sessions_controller.dart';
import 'chat_test_harness.dart';
import 'fake_chat_gateway.dart';
/// `/new` keeps a chat: its row changes id in place, here and on the other
/// devices that receive the move as `chat_session_changed` with `previous_id`.
void main() {
late FakeChatGateway gateway;
late ChatSessionsController controller;
setUp(() async {
gateway = FakeChatGateway();
gateway.sessionsHandler = () async => {
'chat_sessions': [
{'id': 's1', 'title': 'First', 'directory': '/w'},
{'id': 's2', 'title': 'Second', 'directory': '/w', 'color': 'teal'},
{'id': 's3', 'title': 'Third', 'directory': '/w'},
],
};
controller = ChatSessionsController(
ChatRepository(gateway),
localStore: MemoryChatSessionLocalStore(),
);
await controller.refresh();
});
tearDown(() => controller.dispose());
List<String> ids() => [for (final row in controller.sessions) '${row['id']}'];
test('a new session takes the row it replaces, in place', () {
controller.replaceSession('s2', {
'id': 'n2',
'title': 'Second',
'directory': '/w',
'color': 'teal',
});
expect(ids(), ['s1', 'n2', 's3']);
expect(controller.sessions[1]['color'], 'teal');
});
test('a replaced row this device never listed joins at the head', () {
controller.replaceSession('gone', {'id': 'n9', 'title': 'Elsewhere'});
expect(ids(), ['n9', 's1', 's2', 's3']);
});
test('another device\'s /new swaps the row live and is reported', () async {
final lists = gateway.count('chat_sessions');
final replaced = <(String, JsonMap)>[];
controller.onSessionReplaced = (previous, row) =>
replaced.add((previous, row));
gateway.pushed.add({
'type': 'chat_session_changed',
'chat_session': {
'id': 'n2',
'previous_id': 's2',
'title': 'Second',
'directory': '/w',
'color': 'teal',
'resume_command': 'cd /w && claude --resume n2',
},
});
await pumpEventQueue();
expect(ids(), ['s1', 'n2', 's3']);
expect(controller.sessions[1].containsKey('previous_id'), isFalse);
expect(controller.sessions[1]['resume_command'], contains('n2'));
expect(replaced.single.$1, 's2');
expect(replaced.single.$2['id'], 'n2');
expect(gateway.count('chat_sessions'), lists);
});
}