fix: clear a stale relay banner once the connection is back

- resuming from the background can fail the first relay request (DNS not
  ready yet) while a later session read and the event stream work; the
  banner stayed because only the exact "Connection lost, reconnecting…"
  text was ever cleared, and a successful session read did not clear it
- transport errors (connect, session list/search/more, archive, active,
  permissions, chat settings, projects, fs home, models) are now marked as
  such and cleared by any successful read or by an event arriving on the
  restarted stream; a pin/archive action error is left for the next action
- the sidebar Retry now reconnects the stored connection when there is no
  client, otherwise re-reads the session list and clears the stale error
- version 0.6.4+604; tests cover the stale-error clearing and rollback
This commit is contained in:
blumia committed 2026-11-27 18:37:19 +08:00
1 parent ba3c820186
commit 9ae1e9b97a
6 files changed
+177 -6

No files matched your search

+1 -1
View File
@@ -131,7 +131,7 @@ class _SessionListPaneState extends State<SessionListPane> {
color: scheme.error, fontSize: 13)),
),
TextButton(
onPressed: _app.refreshSessions,
onPressed: _app.retryConnection,
child: const AppText('Retry')),
],
),
+56 -3
View File
@@ -90,6 +90,12 @@ class AppState extends ChangeNotifier {
List<Session> sessions = [];
bool sessionsLoading = false;
String? error;
/// True when [error] came from the transport (connect, session list or the
/// event stream) rather than one session action. A successful read clears
/// only this kind, so an action error is not wiped by a routine refresh.
bool _connectionError = false;
Set<String> active = {};
List<PermissionRequest> perms = [];
Map<String, String> modelNames = {};
@@ -210,6 +216,7 @@ class AppState extends ChangeNotifier {
settings['allowPromptingSubagentSessions'] == true;
showGitignored = settings['filesViewShowGitignored'] == true;
_applyToolSettings(settings);
_clearConnectionError();
notifyListeners();
} catch (_) {}
}
@@ -398,6 +405,7 @@ class AppState extends ChangeNotifier {
final message = friendlyConnectError(failure ?? tr('Pairing failed'));
debugPrint('pair: failed: $message');
error = message;
_connectionError = true;
_lastError = message;
notifyListeners();
return false;
@@ -447,6 +455,7 @@ class AppState extends ChangeNotifier {
if (busy) return;
busy = true;
error = null;
_connectionError = false;
notifyListeners();
final cl = _buildClient(c);
try {
@@ -481,6 +490,7 @@ class AppState extends ChangeNotifier {
} catch (e) {
busy = false;
error = friendlyConnectError(e);
_connectionError = true;
if (!silent) {
// Surface in the form: the UI reads error itself.
_lastError = error;
@@ -521,6 +531,31 @@ class AppState extends ChangeNotifier {
String? get lastError => _lastError;
void clearLastError() => _lastError = null;
/// Drops a transport error once a request or event proves the connection is
/// back. A session-action error (pin, archive) is left alone.
void _clearConnectionError() {
if (!_connectionError) return;
_connectionError = false;
error = null;
}
/// The sidebar banner's Retry: reconnect when the client is gone, otherwise
/// read the session list again. The successful read clears a stale
/// transport error; an action error is simply dismissed.
Future<void> retryConnection() async {
final current = conn;
if (client == null) {
if (current != null) await connect(current, silent: true);
return;
}
if (!_connectionError) {
error = null;
notifyListeners();
return;
}
await refreshSessions();
}
void _connected(Client cl, Connection c) {
client = cl;
conn = c;
@@ -546,6 +581,7 @@ class AppState extends ChangeNotifier {
refreshPerms();
refreshModels();
refreshChatSettings();
_clearConnectionError();
notifyListeners();
}
@@ -578,6 +614,7 @@ class AppState extends ChangeNotifier {
_sessionsNext = null;
perms = [];
error = null;
_connectionError = false;
notifyListeners();
}
@@ -602,9 +639,11 @@ class AppState extends ChangeNotifier {
sessions = page.data;
_sessionsNext = page.next;
sessionsLoading = false;
_clearConnectionError();
} catch (e) {
sessionsLoading = false;
error = e.toString();
_connectionError = true;
}
notifyListeners();
}
@@ -621,6 +660,7 @@ class AppState extends ChangeNotifier {
// sessions.
_sessionsNext = null;
sessionsLoading = false;
_clearConnectionError();
} catch (_) {
sessionsLoading = false;
}
@@ -682,6 +722,7 @@ class AppState extends ChangeNotifier {
project.withMeta(byPath[slashPath(project.canonical)] ??
byPath[slashPath(project.canonical).toLowerCase()]),
];
_clearConnectionError();
notifyListeners();
} catch (_) {}
}
@@ -736,8 +777,10 @@ class AppState extends ChangeNotifier {
...page.data.where((s) => !known.contains(s.id)),
];
_sessionsNext = page.next;
_clearConnectionError();
} catch (e) {
error = e.toString();
_connectionError = true;
} finally {
loadingMoreSessions = false;
notifyListeners();
@@ -753,8 +796,10 @@ class AppState extends ChangeNotifier {
final page = await cl.listSessions(archived: true, limit: 100);
archivedSessions = page.data;
archivedLoaded = true;
_clearConnectionError();
} catch (e) {
error = e.toString();
_connectionError = true;
} finally {
archivedLoading = false;
notifyListeners();
@@ -895,6 +940,7 @@ class AppState extends ChangeNotifier {
chatsRoot = info.chatsRoot.isNotEmpty ? info.chatsRoot : null;
chatRoots = roots.toList();
if (info.home.isNotEmpty) homeDirectory = info.home;
_clearConnectionError();
notifyListeners();
} catch (_) {}
}
@@ -952,6 +998,7 @@ class AppState extends ChangeNotifier {
_patchSession(session.id, (s) => s.copyWith(metadata: metadata));
} catch (e) {
error = e.toString();
_connectionError = false;
notifyListeners();
}
}
@@ -980,6 +1027,7 @@ class AppState extends ChangeNotifier {
notifyListeners();
} catch (e) {
error = e.toString();
_connectionError = false;
notifyListeners();
}
}
@@ -1127,6 +1175,7 @@ class AppState extends ChangeNotifier {
final ids = await cl.activeSessions();
if (cl != client) return false;
active = ids;
_clearConnectionError();
notifyListeners();
return true;
} catch (_) {
@@ -1139,6 +1188,7 @@ class AppState extends ChangeNotifier {
if (cl == null) return;
try {
perms = await cl.pendingPermissions(directory);
_clearConnectionError();
notifyListeners();
} catch (_) {}
}
@@ -1159,6 +1209,7 @@ class AppState extends ChangeNotifier {
modelNames = names;
modelLimits = limits;
modelList = models;
_clearConnectionError();
notifyListeners();
} catch (_) {}
try {
@@ -1194,9 +1245,10 @@ class AppState extends ChangeNotifier {
(ev) {
if (client != cl) return;
// Anything arriving means the stream is back (the server greets
// every connection with `server.connected`), so the "reconnecting"
// banner has done its job.
if (error == tr('Connection lost, reconnecting…')) error = null;
// every connection with `server.connected`), so a connection
// banner has done its job — including a relay error left over
// from a request that failed while the tunnel was reconnecting.
_clearConnectionError();
if (catchUp) {
catchUp = false;
_resyncActivity();
@@ -1215,6 +1267,7 @@ class AppState extends ChangeNotifier {
if (client != cl || gen != _streamGen) return;
afterDrop = true;
error = tr('Connection lost, reconnecting…');
_connectionError = true;
notifyListeners();
await Future.delayed(const Duration(seconds: 2));
}