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
@@ -13,20 +13,25 @@ import 'chat_composer_bar_fixtures.dart';
/// on-screen keyboard's Return breaks the line instead.
final _desktop = TargetPlatformVariant.only(TargetPlatform.linux);
/// Answers the session commands' create, close and rename requests.
/// Answers the session commands' new-session, close and rename requests.
class _SessionGateway extends ComposerBarGateway {
String? refuse;
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
if (type == 'chat_create' ||
if (type == 'chat_new_session' ||
type == 'chat_close' ||
type == 'chat_rename') {
calls.add((type: type, fields: fields));
if (refuse != null) throw ProtocolError(refuse!);
if (type == 'chat_create') {
if (type == 'chat_new_session') {
return {
'chat_session': {'id': 'n1', 'title': 'New chat', 'directory': '/w'},
'chat_session': {
'id': 'n1',
'title': 'Old',
'directory': '/w',
'previous_id': 's',
},
};
}
return <String, dynamic>{};
@@ -44,6 +49,7 @@ void main() {
late _SessionGateway gateway;
JsonMap? created;
String? createdNotice;
String? replacedId;
String? renamed;
var closed = 0;
@@ -51,6 +57,7 @@ void main() {
gateway = _SessionGateway();
created = null;
createdNotice = null;
replacedId = null;
renamed = null;
closed = 0;
});
@@ -71,7 +78,8 @@ void main() {
drafts: MemoryChatDraftStore(),
transcripts: transcripts,
newOperationId: () => 'op-1',
onCreatedSession: (session, notice) {
onNewSession: (previousId, session, notice) {
replacedId = previousId;
created = session;
createdNotice = notice;
},
@@ -95,32 +103,37 @@ void main() {
await tester.pumpAndSettle();
}
testWidgets('/new creates in this directory and hands the row over', (
testWidgets('/new keeps this chat on a fresh session and hands it over', (
tester,
) async {
await pump(tester);
await run(tester, '/new');
final body = gateway.sent('chat_create').single['chat_create'] as JsonMap;
expect(body['title'], 'New chat');
expect(body['directory'], '/w');
expect(body['operation_id'], 'op-1');
expect(gateway.sent('chat_create'), isEmpty);
final body =
gateway.sent('chat_new_session').single['chat_new_session'] as JsonMap;
expect(body['session_id'], 's');
expect(body['instructions'], isNotEmpty);
expect(replacedId, 's');
expect(created?['id'], 'n1');
expect(createdNotice, 'Started a new session “New chat” in /w.');
expect(created?['title'], 'Old');
expect(created?.containsKey('previous_id'), isFalse);
expect(createdNotice, startsWith('New session started.'));
expect(gateway.promptBodies, isEmpty);
}, variant: _desktop);
testWidgets('/new without a directory sends nothing and reports it', (
testWidgets('/new refused keeps the reason here and hands nothing over', (
tester,
) async {
await pump(tester, directory: '');
gateway.refuse = 'a turn is still running in this chat';
await pump(tester);
await run(tester, '/new');
expect(gateway.sent('chat_create'), isEmpty);
expect(gateway.sent('chat_new_session'), hasLength(1));
expect(created, isNull);
expect(
find.text(
"Can't start a new session here: the server reported no directory "
'for this chat.',
"Couldn't start a new session: a turn is still running in this chat",
),
findsOneWidget,
);