Servers: name a machine after its hostname once it connects

A saved server still named by the app ('My server', 'This server',
'Original legacy server' or its address host/IP) takes the hostname the
machine reports in get_status the first time the app connects; typed
names are kept. New servers (legacy migration, Settings with a blank
name, onboarding) start with the address host. A browser on its page
origin with no saved profile shows the reported hostname too. No server
change: get_status already reports the hostname.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-02 15:58:28 +00:00
parent 3e817a189b
commit c264f865ff
16 changed files with 654 additions and 24 deletions
+3 -3
View File
@@ -21,7 +21,7 @@ class AppTerminals {
required bool Function() connected,
required ApiGateway gateway,
required String machineId,
required String machineName,
required String Function() machineName,
required this.layout,
WindowManager? windows,
}) : machines = pool != null
@@ -29,9 +29,9 @@ class AppTerminals {
: SingleTerminalMachines(
connection: connection,
connected: connected,
machine: TerminalMachine(
machine: () => TerminalMachine(
id: machineId,
name: machineName,
name: machineName(),
gateway: gateway,
),
) {
+36 -5
View File
@@ -58,6 +58,7 @@ import '../features/terminal/terminal_layout.dart';
import 'app_route.dart';
import 'app_router.dart';
import 'app_session.dart';
import '../core/servers/server_naming.dart';
import 'chat_destination.dart';
import 'pipelines_destination.dart';
import 'runs_destination.dart';
@@ -357,16 +358,15 @@ class _PipelinesAppState extends State<PipelinesApp>
_session.addListener(_chatSyncChanged);
widget.chatPool?.addListener(_chatSyncChanged);
_chatSyncChanged();
_session.addListener(_learnServerHostname);
_learnServerHostname();
_terminals = AppTerminals(
pool: widget.chatPool,
connection: _session,
connection: Listenable.merge([_session, ?widget.servers]),
connected: () => _session.connected,
gateway: _session.gateway,
machineId: widget.serverId ?? 'server',
machineName:
widget.servers?.catalog.active?.name ??
widget.server.uri?.host ??
'This server',
machineName: _serverName,
layout: widget.terminalLayout ?? TerminalLayout.instance,
windows: _windows,
);
@@ -605,6 +605,7 @@ class _PipelinesAppState extends State<PipelinesApp>
}
_terminals.dispose();
_session.removeListener(_chatSyncChanged);
_session.removeListener(_learnServerHostname);
widget.chatPool?.removeListener(_chatSyncChanged);
_chatWindows.dispose();
_diffWindows.dispose();
@@ -935,6 +936,36 @@ class _PipelinesAppState extends State<PipelinesApp>
}
/// A replaced socket makes open chats re-subscribe, also in windows.
/// This server's name: its saved name, the hostname a browser's unsaved
/// page origin reported, else its address.
String _serverName() {
final servers = widget.servers;
final saved = servers?.catalog.servers
.where((server) => server.id == widget.serverId)
.firstOrNull;
final host = widget.server.uri?.host ?? '';
return saved?.name ??
servers?.originHostname ??
(host.isEmpty ? 'This server' : host);
}
bool _hostnameAsked = false;
/// Asks this server once for its hostname, which names a server the app
/// named by its address. The chat pool already asks every saved machine.
void _learnServerHostname() {
final servers = widget.servers;
if (_hostnameAsked || servers == null || !_session.connected) return;
if (widget.chatPool != null && widget.serverId != null) return;
_hostnameAsked = true;
final serverId = widget.serverId;
unawaited(
fetchServerHostname(_session.gateway).then((hostname) {
if (mounted) unawaited(servers.learnHostname(serverId, hostname));
}),
);
}
void _chatSyncChanged() => _chatRuntime.syncToken.value =
widget.chatPool?.revision ?? _session.connectionEpoch;
@@ -10,6 +10,7 @@ import '../../transport/pipelines_client.dart';
import '../../../features/settings/server_connections.dart';
import '../machine_access.dart';
import '../server_catalog.dart';
import '../server_naming.dart';
import 'browser_peer.dart';
import 'chat_server_gateway.dart';
import 'federated_chat_gateway.dart';
@@ -27,6 +28,9 @@ class ChatMachineStatus {
/// Why the machine is not connected, once classified; null while unknown.
MachineAccessResult? access;
/// The machine reported its hostname to this pool; asked once per pool.
bool hostnameKnown = false;
}
/// Probes one saved machine with its own kept sign-in (see
@@ -180,6 +184,7 @@ class ChatServerPool extends ChangeNotifier implements MachineAuthenticator {
if (item.connected && !was) {
revision++;
unawaited(ackSync.connected());
unawaited(_learnHostname(item, generation));
}
if (!item.connected && !item.connecting && item.access == null) {
unawaited(_classify(item, generation));
@@ -189,6 +194,7 @@ class ChatServerPool extends ChangeNotifier implements MachineAuthenticator {
revision++;
if (item.connected) {
unawaited(ackSync.connected());
unawaited(_learnHostname(item, generation));
} else {
unawaited(_classify(item, generation));
}
@@ -207,6 +213,21 @@ class ChatServerPool extends ChangeNotifier implements MachineAuthenticator {
if (!_disposed) notifyListeners();
}
/// Names a machine the app named by its address after the hostname it
/// reports (see [ServerConnections.learnHostname]); typed names are kept.
Future<void> _learnHostname(ChatMachineStatus item, int generation) async {
final adapter = item.adapter;
if (item.hostnameKnown || adapter == null) return;
item.hostnameKnown = true;
final hostname = await fetchServerHostname(adapter.gateway);
if (_disposed || generation != item.generation) return;
if (hostname.isEmpty) {
item.hostnameKnown = false;
return;
}
await servers.learnHostname(item.server.id, hostname);
}
/// A native socket that did not open says nothing about why. Ask the web
/// login with this machine's own kept sign-in: a form means it wants its
/// password, a redirect means the sign-in is fine and the socket failed.
@@ -4,6 +4,7 @@ import 'dart:math';
import 'package:shared_preferences/shared_preferences.dart';
import 'server_catalog.dart';
import 'server_naming.dart';
import '../transport/server_url.dart';
const String serverCatalogKey = 'pipelines.next.serverCatalog.v1';
@@ -27,7 +28,8 @@ ServerCatalog _migrate(String legacyEndpoint, [List<String>? binding]) {
servers: [
SavedServer.validated(
id: id,
name: 'My server',
// Its address names it until the machine reports its hostname.
name: temporaryServerName(legacyEndpoint),
endpoints: [legacyEndpoint],
legacyEndpoint: canMigrate ? legacyEndpoint : null,
),
+3
View File
@@ -15,7 +15,10 @@ class ServerContext extends InheritedNotifier<ServerConnections> {
static ServerContext? maybeOf(BuildContext context) =>
context.dependOnInheritedWidgetOfExactType<ServerContext>();
/// The machine's name; a browser's unsaved page origin is named by the
/// hostname it reported.
String? get name {
if (serverId == null) return notifier!.originHostname;
for (final server in notifier!.catalog.servers) {
if (server.id == serverId) return server.name;
}
+62
View File
@@ -0,0 +1,62 @@
import '../api_gateway.dart';
import 'server_catalog.dart';
/// Names the app itself gave a machine before it knew better. Older builds
/// saved these; a machine named one of them takes its own hostname instead.
const Set<String> placeholderServerNames = {
'My server',
'This server',
'Original legacy server',
};
/// The temporary name of a machine reached at [url]: its address's host.
String temporaryServerName(String url) {
final host = Uri.tryParse(url.trim())?.host ?? '';
return host.isEmpty ? url.trim() : host;
}
/// Whether [server] still carries a name the app chose: a placeholder, or
/// exactly the host (or IP) of one of its addresses. A name the operator
/// typed is anything else and is never replaced.
bool hasPlaceholderName(SavedServer server) {
final name = server.name.trim();
if (placeholderServerNames.contains(name)) return true;
final lower = name.toLowerCase();
return server.endpoints.any(
(url) => temporaryServerName(url).toLowerCase() == lower,
);
}
/// [server] renamed to the [hostname] its machine reports, or null when it
/// keeps its name: the name is the operator's, or the hostname is empty or
/// already the name.
SavedServer? renamedToHostname(SavedServer server, String hostname) {
final name = hostname.trim();
if (name.isEmpty || name == server.name || !hasPlaceholderName(server)) {
return null;
}
return SavedServer.validated(
id: server.id,
name: name,
endpoints: server.endpoints,
legacyEndpoint: server.legacyEndpoint,
);
}
/// The machine's own hostname from a `get_status` reply, or '' when absent.
String hostnameFromStatus(JsonMap reply) {
final status = reply['status'];
if (status is! Map) return '';
final hostname = status['hostname'];
return hostname is String ? hostname.trim() : '';
}
/// Asks the machine behind [gateway] for its hostname; '' when it cannot say
/// (an older server, a refused request or a dropped connection).
Future<String> fetchServerHostname(ApiGateway gateway) async {
try {
return hostnameFromStatus(await gateway.call('get_status'));
} catch (_) {
return '';
}
}
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import '../../core/servers/machine_access.dart';
import '../../core/servers/server_catalog.dart';
import '../../core/servers/server_naming.dart';
import '../../ui/adaptive/segmented_bar.dart';
import '../onboarding/discovery/discovered_server.dart';
import '../onboarding/discovery/discovery_panel.dart';
@@ -72,13 +73,19 @@ class _ServerEditorState extends State<_ServerEditor> {
Future<void> _save() async {
if (_saving) return;
try {
final endpoints = _addresses.text
.split('\n')
.where((line) => line.trim().isNotEmpty)
.toList();
// A blank name is the address's host until the machine reports its
// hostname, which then names it.
final server = SavedServer.validated(
id: _id,
name: _name.text,
name: _name.text.trim().isEmpty && endpoints.isNotEmpty
? temporaryServerName(endpoints.first)
: _name.text,
legacyEndpoint: widget.server?.legacyEndpoint,
endpoints: _addresses.text
.split('\n')
.where((line) => line.trim().isNotEmpty),
endpoints: endpoints,
);
setState(() {
_saving = true;
@@ -173,6 +180,10 @@ class _ServerEditorState extends State<_ServerEditor> {
const SizedBox(height: 16),
],
_field(_name, 'Server name', key: 'server-profile-name'),
const SizedBox(height: 8),
const Text(
'Leave blank to use the machine\'s own name once it connects.',
),
const SizedBox(height: 16),
_field(
_addresses,
@@ -1,8 +1,11 @@
import 'dart:async';
import 'package:flutter/foundation.dart';
import '../../core/servers/machine_access.dart';
import '../../core/servers/server_catalog.dart';
import '../../core/servers/server_catalog_store.dart';
import '../../core/servers/server_naming.dart';
/// Saved configurations are committed before switching the app's connection.
/// A failed save leaves the old selection and its live socket unchanged.
@@ -79,6 +82,47 @@ class ServerConnections extends ChangeNotifier {
bool get busy => _busy;
String? get error => _error;
String? _originHostname;
/// The hostname of the page-origin server a browser uses with no saved
/// profile for it; it names that server, which has no catalog entry.
String? get originHostname => _originHostname;
final _learned = <String, String>{};
/// Records the [hostname] the machine [id] reported. A saved machine still
/// named by the app (see [hasPlaceholderName]) is renamed and saved; a name
/// the operator typed is kept. A null [id] is the unsaved page origin.
/// A change already in progress defers the rename until it finishes.
Future<bool> learnHostname(String? id, String hostname) async {
final name = hostname.trim();
if (_disposed || name.isEmpty) return false;
if (id == null) {
if (_originHostname == name) return false;
_originHostname = name;
_notify();
return true;
}
_learned[id] = name;
return _applyLearnedNames();
}
Future<bool> _applyLearnedNames() async {
if (_busy || _disposed || _learned.isEmpty) return false;
final learned = Map.of(_learned);
_learned.clear();
var next = _catalog;
for (final server in _catalog.servers) {
final hostname = learned[server.id];
final renamed = hostname == null
? null
: renamedToHostname(server, hostname);
if (renamed != null) next = next.upsert(renamed);
}
if (identical(next, _catalog)) return false;
return _change(() => next, activate: false);
}
/// Browsers always connect to their page origin, even after Back or a failed
/// cross-origin navigation. Do not label another saved profile as active.
void alignBrowserOrigin(Uri endpoint) {
@@ -181,6 +225,7 @@ class ServerConnections extends ChangeNotifier {
} finally {
_busy = false;
_notify();
if (_learned.isNotEmpty) scheduleMicrotask(_applyLearnedNames);
}
}
@@ -65,11 +65,13 @@ class SingleTerminalMachines extends ValueNotifier<List<TerminalMachine>> {
}
final Listenable connection;
final TerminalMachine machine;
/// The machine as named now; [connection] also notifies a rename.
final TerminalMachine Function() machine;
final bool Function() connected;
void _update() {
final next = connected() ? [machine] : const <TerminalMachine>[];
final next = connected() ? [machine()] : const <TerminalMachine>[];
if (!_same(next, value)) value = next;
}