fix: reconcile the running state when the app returns

A turn that starts or ends while the app is in the background leaves no
event behind, so the activity strip kept its old state (stayed visible
after the turn ended) and the sidebar's running marks went stale too.

- on return, read the server's active set and the transcript tail once
  and settle both the open chat's strip and the sidebar from them
- do the same on the first event after the stream reconnects, since a
  silent drop loses the same events without leaving the app
- opening a chat whose turn is already under way asks once, so the strip
  is right from the first frame
- when the server cannot answer, the tail decides (an assistant message
  without a completion time is a live turn); a failed request is never
  read as "everything is idle"
This commit is contained in:
blumia committed 2026-11-25 00:23:50 +08:00
1 parent 9564019a91
commit 00564fe94b
5 files changed
+139 -10

No files matched your search

+55
View File
@@ -0,0 +1,55 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:openchamber/api/models.dart';
import 'package:openchamber/state.dart';
void main() {
// The app can be in the background while a turn starts or ends; the events
// that said so never arrive, so the chat's running flag has to be taken
// from the server again when the app comes back.
group('reconciling a turn on return', () {
late AppState app;
late ChatState chat;
setUp(() {
app = AppState();
chat = ChatState(app, Session(id: 'ses_a', title: '会话'));
});
tearDown(() {
chat.dispose();
app.dispose();
});
test('the server saying idle ends a turn that was left running', () {
chat.running = true;
chat.reconcileRunning({'ses_other'}, known: true);
expect(chat.running, isFalse);
});
test('the server saying running keeps the strip, even without events', () {
chat.running = false;
chat.reconcileRunning({'ses_a'}, known: true);
expect(chat.running, isTrue);
});
test('the server not answering falls back on the transcript tail', () {
chat.running = false;
chat.msgs = [
Message(id: 'u1', type: 'user'),
Message(id: 'a1', type: 'assistant', completed: 100),
Message(id: 'u2', type: 'user'),
Message(id: 'a2', type: 'assistant'),
];
chat.reconcileRunning(const {}, known: false);
expect(chat.running, isTrue);
chat.running = true;
chat.msgs = [
Message(id: 'u1', type: 'user'),
Message(id: 'a1', type: 'assistant', completed: 100),
];
chat.reconcileRunning(const {}, known: false);
expect(chat.running, isFalse);
});
});
}