Fix terminal session restore fallback
This commit is contained in:
parent
dd211e0949
commit
e1d01d539c
@ -142,16 +142,35 @@ class _ProjectDetailPageState extends ConsumerState<ProjectDetailPage> {
|
||||
}
|
||||
}
|
||||
|
||||
void _openExistingSession(Session session, Project project) {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) => TerminalPage(
|
||||
session: session,
|
||||
agentBaseUri: ref.read(agentBaseUriProvider),
|
||||
project: project,
|
||||
Future<void> _openExistingSession(Session session, Project project) async {
|
||||
try {
|
||||
await Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) => TerminalPage(
|
||||
session: session,
|
||||
agentBaseUri: ref.read(agentBaseUriProvider),
|
||||
project: project,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
);
|
||||
if (!mounted) {
|
||||
return;
|
||||
}
|
||||
|
||||
await _refreshDetail();
|
||||
} catch (error) {
|
||||
if (!mounted) {
|
||||
return;
|
||||
}
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
formatAgentError(error, fallback: 'Failed to refresh project.'),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _deleteProject(Project project) async {
|
||||
|
||||
@ -11,6 +11,7 @@ import '../../core/network/agent_error_formatter.dart';
|
||||
import '../projects/project.dart';
|
||||
import '../sessions/session.dart';
|
||||
import 'terminal_diagnostic_log.dart';
|
||||
import 'history_window.dart';
|
||||
import 'terminal_input_controller.dart';
|
||||
import 'terminal_interaction_controller.dart';
|
||||
import 'terminal_session_coordinator.dart';
|
||||
@ -34,6 +35,7 @@ class TerminalPage extends ConsumerStatefulWidget {
|
||||
|
||||
class _TerminalPageState extends ConsumerState<TerminalPage>
|
||||
with WidgetsBindingObserver {
|
||||
static const Duration _historySeedDelay = Duration(milliseconds: 120);
|
||||
static const List<_QuickTerminalKey> _quickTerminalKeys = [
|
||||
_QuickTerminalKey(keyId: 'esc', label: 'Esc', input: '\u001b'),
|
||||
_QuickTerminalKey(keyId: 'tab', label: 'Tab', input: '\t'),
|
||||
@ -55,6 +57,10 @@ class _TerminalPageState extends ConsumerState<TerminalPage>
|
||||
late final TerminalSessionCoordinator _coordinator;
|
||||
late final TerminalInputController _inputController;
|
||||
late final Listenable _pageStateListenable;
|
||||
Timer? _historySeedTimer;
|
||||
String? _pendingHistorySeed;
|
||||
bool _receivedSocketFrame = false;
|
||||
bool _historySeeded = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@ -67,7 +73,8 @@ class _TerminalPageState extends ConsumerState<TerminalPage>
|
||||
sessionFactory: ref.read(terminalSocketSessionFactoryProvider).create,
|
||||
baseUri: widget.agentBaseUri,
|
||||
diagnosticLog: _diagnosticLog,
|
||||
onFrame: terminal.write,
|
||||
onFrame: _handleTerminalFrame,
|
||||
onHistoryLoaded: _handleHistoryLoaded,
|
||||
viewportProvider: () => TerminalViewport(
|
||||
columns: terminal.viewWidth,
|
||||
rows: terminal.viewHeight,
|
||||
@ -82,6 +89,7 @@ class _TerminalPageState extends ConsumerState<TerminalPage>
|
||||
_coordinator,
|
||||
_inputController,
|
||||
]);
|
||||
_pageStateListenable.addListener(_handlePageStateChanged);
|
||||
terminal.onResize = (width, height, _, _) {
|
||||
_coordinator.handleTerminalResize(width, height);
|
||||
};
|
||||
@ -96,6 +104,8 @@ class _TerminalPageState extends ConsumerState<TerminalPage>
|
||||
@override
|
||||
void dispose() {
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
_pageStateListenable.removeListener(_handlePageStateChanged);
|
||||
_historySeedTimer?.cancel();
|
||||
_terminalFocusNode.dispose();
|
||||
_inputController.dispose();
|
||||
_terminalScrollController.dispose();
|
||||
@ -239,6 +249,70 @@ class _TerminalPageState extends ConsumerState<TerminalPage>
|
||||
return input.replaceAll('\r', r'\r').replaceAll('\n', r'\n');
|
||||
}
|
||||
|
||||
void _handleTerminalFrame(String frame) {
|
||||
_receivedSocketFrame = true;
|
||||
_cancelHistorySeedTimer();
|
||||
terminal.write(frame);
|
||||
}
|
||||
|
||||
void _handleHistoryLoaded(HistoryWindow history) {
|
||||
if (history.lines.isEmpty) {
|
||||
_pendingHistorySeed = null;
|
||||
return;
|
||||
}
|
||||
|
||||
_pendingHistorySeed = history.lines.join('\r\n');
|
||||
_scheduleHistorySeedIfNeeded();
|
||||
}
|
||||
|
||||
void _handlePageStateChanged() {
|
||||
_scheduleHistorySeedIfNeeded();
|
||||
}
|
||||
|
||||
void _scheduleHistorySeedIfNeeded() {
|
||||
if (_historySeeded ||
|
||||
_receivedSocketFrame ||
|
||||
_connectionState != TerminalConnectionState.connected) {
|
||||
_cancelHistorySeedTimer();
|
||||
return;
|
||||
}
|
||||
|
||||
final pendingHistorySeed = _pendingHistorySeed;
|
||||
if (pendingHistorySeed == null ||
|
||||
pendingHistorySeed.isEmpty ||
|
||||
_terminalHasVisibleContent) {
|
||||
_historySeeded = _terminalHasVisibleContent;
|
||||
_cancelHistorySeedTimer();
|
||||
return;
|
||||
}
|
||||
|
||||
_historySeedTimer ??= Timer(_historySeedDelay, () {
|
||||
_historySeedTimer = null;
|
||||
if (!mounted ||
|
||||
_historySeeded ||
|
||||
_receivedSocketFrame ||
|
||||
_connectionState != TerminalConnectionState.connected ||
|
||||
_terminalHasVisibleContent) {
|
||||
return;
|
||||
}
|
||||
|
||||
final historySeed = _pendingHistorySeed;
|
||||
if (historySeed == null || historySeed.isEmpty) {
|
||||
return;
|
||||
}
|
||||
|
||||
terminal.write(historySeed);
|
||||
_historySeeded = true;
|
||||
});
|
||||
}
|
||||
|
||||
void _cancelHistorySeedTimer() {
|
||||
_historySeedTimer?.cancel();
|
||||
_historySeedTimer = null;
|
||||
}
|
||||
|
||||
bool get _terminalHasVisibleContent => terminal.buffer.getText().trim().isNotEmpty;
|
||||
|
||||
Future<void> _showDiagnostics() {
|
||||
return showModalBottomSheet<void>(
|
||||
context: context,
|
||||
|
||||
@ -675,6 +675,29 @@ void main() {
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'terminal seeds the main buffer from loaded history when attach replay is absent',
|
||||
(tester) async {
|
||||
final transportFactory = _QueuedTerminalSocketTransportFactory();
|
||||
|
||||
await _pumpTerminalPage(
|
||||
tester,
|
||||
session: _session('session-1', 'codex-main'),
|
||||
apiClient: _FakeAgentApiClient(),
|
||||
socketFactory: TerminalSocketSessionFactory(
|
||||
transportFactory: transportFactory.create,
|
||||
),
|
||||
);
|
||||
|
||||
final terminal = tester
|
||||
.widget<TerminalView>(find.byType(TerminalView))
|
||||
.terminal;
|
||||
|
||||
expect(transportFactory.createCount, 1);
|
||||
expect(terminal.buffer.getText(), contains('one\ntwo'));
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'terminal attach replay keeps the cursor on the last restored line',
|
||||
(tester) async {
|
||||
@ -783,6 +806,45 @@ void main() {
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'project detail refreshes after returning from an existing session',
|
||||
(tester) async {
|
||||
final projectRepository = _FakeProjectRepository.withRecentSessions({
|
||||
'project-1': [_session('session-1', 'alpha')],
|
||||
});
|
||||
|
||||
await tester.pumpWidget(
|
||||
ProviderScope(
|
||||
overrides: [
|
||||
agentApiClientProvider.overrideWithValue(_FakeAgentApiClient()),
|
||||
projectRepositoryProvider.overrideWithValue(projectRepository),
|
||||
sessionRepositoryProvider.overrideWithValue(_FakeSessionRepository()),
|
||||
terminalSocketSessionFactoryProvider.overrideWithValue(
|
||||
TerminalSocketSessionFactory(
|
||||
transportFactory: (_) =>
|
||||
_FakeTerminalSocketTransport(autoAttach: true),
|
||||
),
|
||||
),
|
||||
],
|
||||
child: MaterialApp(
|
||||
home: ProjectDetailPage(project: projectRepository.firstProject),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(projectRepository.detailRequestCount, 1);
|
||||
|
||||
await tester.tap(find.text('alpha'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
await tester.pageBack();
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(projectRepository.detailRequestCount, 2);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('session list deletes a session after confirmation', (
|
||||
tester,
|
||||
) async {
|
||||
@ -930,12 +992,16 @@ class _FakeProjectRepository extends ProjectRepository {
|
||||
final List<Project> _projects;
|
||||
final Map<String, List<Session>> _recentSessionsByProject;
|
||||
final List<String> deletedProjectIds = <String>[];
|
||||
int detailRequestCount = 0;
|
||||
|
||||
Project get firstProject => _projects.first;
|
||||
|
||||
@override
|
||||
Future<List<Project>> listProjects() async => List<Project>.of(_projects);
|
||||
|
||||
@override
|
||||
Future<ProjectDetail> getProjectDetail(String projectId) async {
|
||||
detailRequestCount += 1;
|
||||
final project = _projects.singleWhere(
|
||||
(entry) => entry.projectId == projectId,
|
||||
);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user