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) {
|
Future<void> _openExistingSession(Session session, Project project) async {
|
||||||
Navigator.of(context).push(
|
try {
|
||||||
MaterialPageRoute(
|
await Navigator.of(context).push(
|
||||||
builder: (context) => TerminalPage(
|
MaterialPageRoute(
|
||||||
session: session,
|
builder: (context) => TerminalPage(
|
||||||
agentBaseUri: ref.read(agentBaseUriProvider),
|
session: session,
|
||||||
project: project,
|
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 {
|
Future<void> _deleteProject(Project project) async {
|
||||||
|
|||||||
@ -11,6 +11,7 @@ import '../../core/network/agent_error_formatter.dart';
|
|||||||
import '../projects/project.dart';
|
import '../projects/project.dart';
|
||||||
import '../sessions/session.dart';
|
import '../sessions/session.dart';
|
||||||
import 'terminal_diagnostic_log.dart';
|
import 'terminal_diagnostic_log.dart';
|
||||||
|
import 'history_window.dart';
|
||||||
import 'terminal_input_controller.dart';
|
import 'terminal_input_controller.dart';
|
||||||
import 'terminal_interaction_controller.dart';
|
import 'terminal_interaction_controller.dart';
|
||||||
import 'terminal_session_coordinator.dart';
|
import 'terminal_session_coordinator.dart';
|
||||||
@ -34,6 +35,7 @@ class TerminalPage extends ConsumerStatefulWidget {
|
|||||||
|
|
||||||
class _TerminalPageState extends ConsumerState<TerminalPage>
|
class _TerminalPageState extends ConsumerState<TerminalPage>
|
||||||
with WidgetsBindingObserver {
|
with WidgetsBindingObserver {
|
||||||
|
static const Duration _historySeedDelay = Duration(milliseconds: 120);
|
||||||
static const List<_QuickTerminalKey> _quickTerminalKeys = [
|
static const List<_QuickTerminalKey> _quickTerminalKeys = [
|
||||||
_QuickTerminalKey(keyId: 'esc', label: 'Esc', input: '\u001b'),
|
_QuickTerminalKey(keyId: 'esc', label: 'Esc', input: '\u001b'),
|
||||||
_QuickTerminalKey(keyId: 'tab', label: 'Tab', input: '\t'),
|
_QuickTerminalKey(keyId: 'tab', label: 'Tab', input: '\t'),
|
||||||
@ -55,6 +57,10 @@ class _TerminalPageState extends ConsumerState<TerminalPage>
|
|||||||
late final TerminalSessionCoordinator _coordinator;
|
late final TerminalSessionCoordinator _coordinator;
|
||||||
late final TerminalInputController _inputController;
|
late final TerminalInputController _inputController;
|
||||||
late final Listenable _pageStateListenable;
|
late final Listenable _pageStateListenable;
|
||||||
|
Timer? _historySeedTimer;
|
||||||
|
String? _pendingHistorySeed;
|
||||||
|
bool _receivedSocketFrame = false;
|
||||||
|
bool _historySeeded = false;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
@ -67,7 +73,8 @@ class _TerminalPageState extends ConsumerState<TerminalPage>
|
|||||||
sessionFactory: ref.read(terminalSocketSessionFactoryProvider).create,
|
sessionFactory: ref.read(terminalSocketSessionFactoryProvider).create,
|
||||||
baseUri: widget.agentBaseUri,
|
baseUri: widget.agentBaseUri,
|
||||||
diagnosticLog: _diagnosticLog,
|
diagnosticLog: _diagnosticLog,
|
||||||
onFrame: terminal.write,
|
onFrame: _handleTerminalFrame,
|
||||||
|
onHistoryLoaded: _handleHistoryLoaded,
|
||||||
viewportProvider: () => TerminalViewport(
|
viewportProvider: () => TerminalViewport(
|
||||||
columns: terminal.viewWidth,
|
columns: terminal.viewWidth,
|
||||||
rows: terminal.viewHeight,
|
rows: terminal.viewHeight,
|
||||||
@ -82,6 +89,7 @@ class _TerminalPageState extends ConsumerState<TerminalPage>
|
|||||||
_coordinator,
|
_coordinator,
|
||||||
_inputController,
|
_inputController,
|
||||||
]);
|
]);
|
||||||
|
_pageStateListenable.addListener(_handlePageStateChanged);
|
||||||
terminal.onResize = (width, height, _, _) {
|
terminal.onResize = (width, height, _, _) {
|
||||||
_coordinator.handleTerminalResize(width, height);
|
_coordinator.handleTerminalResize(width, height);
|
||||||
};
|
};
|
||||||
@ -96,6 +104,8 @@ class _TerminalPageState extends ConsumerState<TerminalPage>
|
|||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
WidgetsBinding.instance.removeObserver(this);
|
WidgetsBinding.instance.removeObserver(this);
|
||||||
|
_pageStateListenable.removeListener(_handlePageStateChanged);
|
||||||
|
_historySeedTimer?.cancel();
|
||||||
_terminalFocusNode.dispose();
|
_terminalFocusNode.dispose();
|
||||||
_inputController.dispose();
|
_inputController.dispose();
|
||||||
_terminalScrollController.dispose();
|
_terminalScrollController.dispose();
|
||||||
@ -239,6 +249,70 @@ class _TerminalPageState extends ConsumerState<TerminalPage>
|
|||||||
return input.replaceAll('\r', r'\r').replaceAll('\n', r'\n');
|
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() {
|
Future<void> _showDiagnostics() {
|
||||||
return showModalBottomSheet<void>(
|
return showModalBottomSheet<void>(
|
||||||
context: context,
|
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(
|
testWidgets(
|
||||||
'terminal attach replay keeps the cursor on the last restored line',
|
'terminal attach replay keeps the cursor on the last restored line',
|
||||||
(tester) async {
|
(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', (
|
testWidgets('session list deletes a session after confirmation', (
|
||||||
tester,
|
tester,
|
||||||
) async {
|
) async {
|
||||||
@ -930,12 +992,16 @@ class _FakeProjectRepository extends ProjectRepository {
|
|||||||
final List<Project> _projects;
|
final List<Project> _projects;
|
||||||
final Map<String, List<Session>> _recentSessionsByProject;
|
final Map<String, List<Session>> _recentSessionsByProject;
|
||||||
final List<String> deletedProjectIds = <String>[];
|
final List<String> deletedProjectIds = <String>[];
|
||||||
|
int detailRequestCount = 0;
|
||||||
|
|
||||||
|
Project get firstProject => _projects.first;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<List<Project>> listProjects() async => List<Project>.of(_projects);
|
Future<List<Project>> listProjects() async => List<Project>.of(_projects);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<ProjectDetail> getProjectDetail(String projectId) async {
|
Future<ProjectDetail> getProjectDetail(String projectId) async {
|
||||||
|
detailRequestCount += 1;
|
||||||
final project = _projects.singleWhere(
|
final project = _projects.singleWhere(
|
||||||
(entry) => entry.projectId == projectId,
|
(entry) => entry.projectId == projectId,
|
||||||
);
|
);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user