Merge branch 'worktree-agent-a2b0140029ece13b5'

This commit is contained in:
2026-11-10 00:46:16 +00:00
26 changed files with 1213 additions and 611 deletions
@@ -58,8 +58,10 @@ class SettingsTerminalSection extends StatelessWidget {
children: [
const Text(
'Saved on this device. Ctrl+` shows or hides the terminal; on '
'a narrow window it opens full screen. Shells keep running '
'while it is hidden.',
'a narrow window it opens full screen. A terminal starts at '
'the machine\'s login prompt: type that account\'s password '
'once, and it stays logged in while hidden. Terminals end '
'when the server restarts; run tmux inside one to keep work.',
),
if (layout.saveFailed)
Text(
+1 -4
View File
@@ -40,10 +40,7 @@ class TerminalBody extends StatelessWidget {
if (!tab.connected)
'${tab.machineName} is not connected; the terminal resumes when it is.'
else if (!tab.info.running)
tab.info.endedText
else if (!tab.info.persistent)
'This shell ends if the server on ${tab.machineName} restarts '
'(tmux is not installed there).',
tab.info.endedText,
?tab.error,
?hub.error,
];
+14 -3
View File
@@ -29,6 +29,14 @@ class TerminalHub extends ChangeNotifier {
/// The last refusal of an open or close, in the server's words.
String? error;
/// Whether this app run has had its first terminal: the first time the
/// terminal is shown it starts one (`openTerminalIfNone`), later shows
/// only show it. Opening one by hand counts.
bool firstOpenDone = false;
/// The first show's start, while it runs.
Future<void>? firstOpening;
List<TerminalTab> get tabs => List.unmodifiable(_tabs);
List<TerminalMachine> get connected => machines.value;
@@ -79,8 +87,10 @@ class TerminalHub extends ChangeNotifier {
TerminalMachine? machineFor(String? id) =>
connected.where((machine) => machine.id == id).firstOrNull;
/// Opens a shell on [machineId] (the first connected machine when null)
/// in [cwd] (home when empty) and selects its tab. Returns the refusal.
/// Opens a terminal on [machineId] (the first connected machine when
/// null) and selects its tab: it shows that machine's login prompt, and
/// after the password the shell starts in [cwd] (home when empty).
/// Returns the refusal.
Future<String?> open({String? machineId, String cwd = ''}) async {
activate();
final machine = machineId == null
@@ -96,6 +106,7 @@ class TerminalHub extends ChangeNotifier {
final record = reply['terminal'];
if (record is! Map) throw StateError('The server sent no terminal.');
final tab = _adopt(machine, TerminalInfo.fromJson(record));
firstOpenDone = true;
_selected = tab.key;
error = null;
_notify();
@@ -107,7 +118,7 @@ class TerminalHub extends ChangeNotifier {
}
}
/// Ends [tab]'s shell (and its tmux session) and removes the tab. A tab
/// Ends [tab]'s login or shell and removes the tab. A tab
/// whose machine is not connected cannot be closed yet.
Future<String?> close(TerminalTab tab) async {
if (!tab.connected) {
+4 -11
View File
@@ -1,7 +1,8 @@
import '../../core/api_gateway.dart';
/// One machine terminal as the server records it (`terminal_list`,
/// `terminal_event`): a login shell on that machine, running or ended.
/// `terminal_event`): a login on that machine (its login prompt, then the
/// account's shell), running or ended. It ends with the server process.
class TerminalInfo {
const TerminalInfo({
required this.id,
@@ -9,7 +10,6 @@ class TerminalInfo {
required this.status,
this.title = '',
this.shell = '',
this.tmux = '',
this.reason = '',
this.exitCode,
this.createdAt = 0,
@@ -25,7 +25,6 @@ class TerminalInfo {
status: '${json['status'] ?? ''}',
title: '${json['title'] ?? ''}',
shell: '${json['shell'] ?? ''}',
tmux: '${json['tmux'] ?? ''}',
reason: '${json['reason'] ?? ''}',
exitCode: (json['exit_code'] as num?)?.toInt(),
createdAt: (json['created_at'] as num?)?.toInt() ?? 0,
@@ -45,10 +44,6 @@ class TerminalInfo {
final String title;
final String shell;
/// The tmux session keeping the shell alive across server restarts;
/// empty when the shell ends with the server.
final String tmux;
/// Why an ended terminal ended, when it was not its own exit.
final String reason;
final int? exitCode;
@@ -56,8 +51,8 @@ class TerminalInfo {
final int cols;
final int rows;
/// Names the server process's output stream: a restarted server starts
/// a new one, whose byte offsets begin again at zero.
/// Names the server process's output stream; another stream's byte
/// offsets begin again at zero.
final String stream;
final int outputEnd;
@@ -68,7 +63,6 @@ class TerminalInfo {
status: status,
title: title,
shell: shell,
tmux: tmux,
reason: reason,
exitCode: exitCode,
createdAt: createdAt,
@@ -80,7 +74,6 @@ class TerminalInfo {
bool get running => status == 'running';
bool get closed => status == 'closed';
bool get persistent => tmux.isNotEmpty;
/// What an ended terminal shows instead of a prompt.
String get endedText {
@@ -7,12 +7,25 @@ import '../../ui/adaptive/segmented_bar_overflow.dart';
import 'terminal_hub.dart';
import 'terminal_scope.dart';
/// Opening the terminal with none to show starts one, as New terminal
/// does, once every machine's list has arrived.
Future<void> openTerminalIfNone(TerminalHub hub) async {
/// The first time the terminal is shown in an app run it starts one
/// terminal, as New terminal does (it shows the machine's login prompt),
/// unless one is already running once every machine's list has arrived.
/// Every later show only shows it: a hidden terminal stays logged in, and
/// a new one (each asking for the password) is opened by hand.
Future<void> openTerminalIfNone(TerminalHub hub) {
if (hub.firstOpenDone) return Future.value();
return hub.firstOpening ??= _openFirst(
hub,
).whenComplete(() => hub.firstOpening = null);
}
Future<void> _openFirst(TerminalHub hub) async {
hub.activate();
await hub.listed();
if (hub.tabs.isNotEmpty || hub.connected.isEmpty) return;
// With no machine connected yet, a later show tries again.
if (hub.firstOpenDone || hub.connected.isEmpty) return;
hub.firstOpenDone = true;
if (hub.tabs.any((tab) => tab.info.running)) return;
await hub.open();
}