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:
1 parent
9564019a91
commit
00564fe94b
5 files changed
+139
-10
No files matched your search
+67
-8
@@ -984,13 +984,21 @@ class AppState extends ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> refreshActive() async {
|
||||
/// Reads the sessions the server says are executing. False when it could
|
||||
/// not tell: the caller then keeps its own view instead of reading an empty
|
||||
/// answer as "everything is idle".
|
||||
Future<bool> refreshActive() async {
|
||||
final cl = client;
|
||||
if (cl == null) return;
|
||||
if (cl == null) return false;
|
||||
try {
|
||||
active = await cl.activeSessions();
|
||||
final ids = await cl.activeSessions();
|
||||
if (cl != client) return false;
|
||||
active = ids;
|
||||
notifyListeners();
|
||||
} catch (_) {}
|
||||
return true;
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> refreshPerms() async {
|
||||
@@ -1041,9 +1049,14 @@ class AppState extends ChangeNotifier {
|
||||
if (cl == null) return;
|
||||
final gen = ++_streamGen;
|
||||
_events?.cancel().ignore();
|
||||
var afterDrop = false;
|
||||
Future<void> loop() async {
|
||||
while (client == cl && gen == _streamGen) {
|
||||
final done = Completer<void>();
|
||||
// Events sent while the stream was down are gone; the first one
|
||||
// after a drop means the server is back, so catch up on the state
|
||||
// those events would have changed.
|
||||
var catchUp = afterDrop;
|
||||
_events = cl.streamEvents(types: _eventTypes).listen(
|
||||
(ev) {
|
||||
if (client != cl) return;
|
||||
@@ -1051,6 +1064,10 @@ class AppState extends ChangeNotifier {
|
||||
// every connection with `server.connected`), so the "reconnecting"
|
||||
// banner has done its job.
|
||||
if (error == '连接中断,正在重连…') error = null;
|
||||
if (catchUp) {
|
||||
catchUp = false;
|
||||
_resyncActivity();
|
||||
}
|
||||
_applyEvent(ev);
|
||||
},
|
||||
onError: (Object _) {
|
||||
@@ -1063,6 +1080,7 @@ class AppState extends ChangeNotifier {
|
||||
);
|
||||
await done.future;
|
||||
if (client != cl || gen != _streamGen) return;
|
||||
afterDrop = true;
|
||||
error = '连接中断,正在重连…';
|
||||
notifyListeners();
|
||||
await Future.delayed(const Duration(seconds: 2));
|
||||
@@ -1079,15 +1097,30 @@ class AppState extends ChangeNotifier {
|
||||
_startStream();
|
||||
refreshFollowUpBehavior();
|
||||
await refreshSessions();
|
||||
if (chat != null) {
|
||||
await chat!.reloadTail();
|
||||
await chat!.reloadInbox();
|
||||
}
|
||||
await _resyncActivity();
|
||||
if (chat != null) await chat!.reloadInbox();
|
||||
for (final workspace in terminals.values) {
|
||||
await workspace.reconnectIfNeeded();
|
||||
}
|
||||
}
|
||||
|
||||
/// Reconciles what the server says is executing with what the UI shows.
|
||||
/// A turn that started or ended while the app was in the background (or
|
||||
/// while the event stream was down) leaves no event behind, so the strips
|
||||
/// would keep their old state; the server's active set and the transcript
|
||||
/// tail say where things actually stand.
|
||||
Future<void> _resyncActivity() async {
|
||||
final cl = client;
|
||||
if (cl == null) return;
|
||||
final ch = chat;
|
||||
final known = await refreshActive();
|
||||
if (client != cl) return;
|
||||
if (ch == null) return;
|
||||
await ch.reloadTail();
|
||||
if (client != cl || chat != ch) return;
|
||||
ch.reconcileRunning(active, known: known);
|
||||
}
|
||||
|
||||
void _applyEvent(Map<String, dynamic> ev) {
|
||||
final type = ev['type'] as String? ?? '';
|
||||
final data = ev['data'] as Map<String, dynamic>? ?? {};
|
||||
@@ -1251,7 +1284,14 @@ class AppState extends ChangeNotifier {
|
||||
// the session's subagents in parallel fills the nested rows under it.
|
||||
notifyListeners();
|
||||
unawaited(loadChildren(s));
|
||||
// Opening a chat whose turn is already under way: the events that started
|
||||
// it came while the chat was closed, so ask the server once.
|
||||
final actives = refreshActive();
|
||||
await chat!.load();
|
||||
final known = await actives;
|
||||
if (chat != null && chat!.session.id == s.id) {
|
||||
chat!.reconcileRunning(active, known: known);
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@@ -1362,6 +1402,25 @@ class ChatState extends ChangeNotifier {
|
||||
/// Public way to signal UI changes.
|
||||
void touch() => notifyListeners();
|
||||
|
||||
/// Takes the server's word on whether a turn is still running — the events
|
||||
/// that started or ended it can arrive while the chat is closed, or never
|
||||
/// arrive because the stream was down. Without an answer from the server,
|
||||
/// the tail decides: an assistant message that has not been marked complete
|
||||
/// is a live turn.
|
||||
void reconcileRunning(Set<String> activeIds, {required bool known}) {
|
||||
final next = known ? activeIds.contains(id) : _tailRunning;
|
||||
if (next == running) return;
|
||||
running = next;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
bool get _tailRunning {
|
||||
for (var i = msgs.length - 1; i >= 0; i--) {
|
||||
if (msgs[i].type == 'assistant') return msgs[i].completed == 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
String get id => session.id;
|
||||
String get dir => session.directory;
|
||||
|
||||
|
||||
Reference in new issue
Block a user