Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

fix: only an explicit Take control press may claim a terminal from mobile

tony46ec31cfc56c
diff --git a/mobile/lib/phone/phone_status.dart b/mobile/lib/phone/phone_status.dart
index aeebdead5..1be665848 100644
--- a/mobile/lib/phone/phone_status.dart
+++ b/mobile/lib/phone/phone_status.dart
@@ -116,10 +116,16 @@ PhoneSummary phoneAgentSummary(MachineState machine, Agent agent) {
/// page opens before the pane has one.
/// [takerName]: who took the terminal, when known ([phoneTakerName]) — the
/// dot's tooltip then says "Taken over by Mac mini" rather than just that it was.
+/// [held]: nothing is attaching, on purpose — see `TerminalPane.heldForTakeControl`. "Attaching…"
+/// is what a null session normally means, and it would be a promise nothing is going to keep.
PhoneSummary phoneSessionSummary(
TerminalSession? session, {
String? takerName,
+ bool held = false,
}) {
+ if (held && session == null) {
+ return (label: 'Not connected', tone: PhoneTone.attention);
+ }
// ⚠️ Checked BEFORE the status, because a watcher's status is `controlling` — it holds a live
// stream and renders every byte; what it does not hold is the terminal. "Live" would promise a
// prompt that ignores typing. See [TerminalSession.watching].
@@ -158,10 +164,20 @@ String? phoneTakerName(
/// The one line under the header while another client drives this terminal —
/// the desktop's banner, at phone size. Null in every other state.
-String? phoneTakeoverNotice(TerminalSession? session, String? takerName) =>
- session?.status == TerminalSessionStatus.takenOver
- ? '${takerName ?? 'Another app'} took control of this terminal'
- : null;
+/// [held]: nothing has been opened here at all, so nobody "took" anything — the line says why the
+/// terminal is not on screen and what the button will do. See `TerminalPane.heldForTakeControl`.
+String? phoneTakeoverNotice(
+ TerminalSession? session,
+ String? takerName, {
+ bool held = false,
+}) {
+ if (held && session == null) {
+ return 'Opening this terminal would take it from another app';
+ }
+ return session?.status == TerminalSessionStatus.takenOver
+ ? '${takerName ?? 'Another app'} took control of this terminal'
+ : null;
+}
/// The way back into a session this device is not driving, or is no longer
/// driving — and what the button offers to do about it.
@@ -173,7 +189,15 @@ String? phoneTakeoverNotice(TerminalSession? session, String? takerName) =>
/// The desktop puts the same two words on the same two states, in the tile
/// header it draws (`widgets/terminal_panel.dart`); a phone hides that header
/// and draws its own, which is how the way out went missing here.
-PhoneSummary? phoneReclaimAction(TerminalSession? session) {
+/// [held]: the tile has no session because opening one would have taken the terminal — an older
+/// CLI, where every open is a takeover, so nothing but a press may open at all. See
+/// `TerminalPane.heldForTakeControl`.
+PhoneSummary? phoneReclaimAction(TerminalSession? session, {bool held = false}) {
+ // Before the status switch: there is no session to read one off, and "Attaching…" is exactly
+ // the wrong thing to say about a tile that is deliberately not attaching.
+ if (held && session == null) {
+ return (label: 'Take control', tone: PhoneTone.attention);
+ }
// A watcher is the ordinary way onto an agent another app is driving: output is already on
// screen, and this is the button that asks for the keyboard. See [TerminalSession.watching].
if (session != null && session.watching) {
diff --git a/mobile/lib/phone/terminal_page.dart b/mobile/lib/phone/terminal_page.dart
index 00eed7b22..eb7436abb 100644
--- a/mobile/lib/phone/terminal_page.dart
+++ b/mobile/lib/phone/terminal_page.dart
@@ -893,18 +893,27 @@ class _TerminalPageState extends State<TerminalPage>
machine.agentLoadStatus == AgentLoadStatus.loaded;
// Captured while the agent is still listed, for the sentence above.
if (agent != null) _cachedAgentName = agent.name;
- final reclaim = phoneReclaimAction(session);
+ // Deliberately not attaching, rather than still on its way: an older CLI cannot be asked for a
+ // terminal politely, so only a press opens one there. See [TerminalPane.heldForTakeControl].
+ final held = !agentGone && pane != null && pane.heldForTakeControl;
+ final reclaim = phoneReclaimAction(session, held: held);
// Read-only either way — the terminal was never this pane's (a watcher) or was taken from it.
// `_AgentGone` owns the page when the agent itself is missing, so this stays out of its way.
final blocked =
!agentGone &&
- session != null &&
- (session.watching || session.status == TerminalSessionStatus.takenOver);
+ (held ||
+ (session != null &&
+ (session.watching ||
+ session.status == TerminalSessionStatus.takenOver)));
final takerName = phoneTakerName(
session,
(id) => widget.notifier.stateOf(id)?.machine.displayName,
);
- final takeoverNotice = phoneTakeoverNotice(session, takerName);
+ final takeoverNotice = phoneTakeoverNotice(
+ session,
+ takerName,
+ held: held,
+ );
// Creating needs the machine to list its folders and name its engines,
// so one that is offline or still wants its password cannot host a new
// agent — the same gate the Agents tab puts on its fab.
@@ -965,6 +974,12 @@ class _TerminalPageState extends State<TerminalPage>
name: _cachedAgentName,
onPickAnother: _pickAnotherAgent,
)
+ // Nothing is attaching and nothing will until the band above is
+ // pressed, so the skeleton would be a promise with nobody behind
+ // it — sweeping once and then sitting still for good. An empty
+ // ground under the band instead. See [_ControlBanner].
+ : held
+ ? ColoredBox(color: AppPalette.windowBg)
: pane == null || session == null
? const _Attaching()
: TerminalPanel(
@@ -1154,6 +1169,7 @@ class _TerminalPageState extends State<TerminalPage>
status: phoneSessionSummary(
session,
takerName: takerName,
+ held: held,
),
trailing: [
// Read-only is a state to get OUT of, so its way
diff --git a/mobile/lib/state/app_state.dart b/mobile/lib/state/app_state.dart
index 917bed927..548e33140 100644
--- a/mobile/lib/state/app_state.dart
+++ b/mobile/lib/state/app_state.dart
@@ -5214,9 +5214,10 @@ class AppNotifier extends ChangeNotifier {
/// control" — see `phoneReclaimAction`. It is the one gesture that means the claim, rather than
/// merely arriving where the claim could be made.
///
- /// A machine whose CLI predates the key cannot be asked politely (it takes over regardless), so
- /// arriving at one of its agents takes the terminal the way it always did — see
- /// [MachineState.terminalNoTakeoverAvailable].
+ /// A machine whose CLI predates the key cannot be asked politely — it takes over regardless — so
+ /// on one of those NOTHING here opens a terminal except a press: arriving at an agent leaves the
+ /// tile empty rather than taking a terminal the desktop may be working in, and the button is
+ /// what opens it. See [_attachSession] and [MachineState.terminalNoTakeoverAvailable].
Future<void> selectAgent(
String machineId,
String agentId, {
@@ -5237,13 +5238,13 @@ class AppNotifier extends ChangeNotifier {
}
final terminal = existing.session;
if (terminal != null) {
- // A press STICKS, for every later open this session makes — its own reconnects included.
- // A person who took this terminal once keeps it through a tunnel dropping; asked politely
- // again on the way back, they would lose it to whoever picked it up meanwhile.
+ // A press arms the NEXT open, and only that one: the flag is lowered again the moment an
+ // open it armed is answered (see the `terminal_ready` branch of `TerminalSession`), so a
+ // claim cannot outlive the press that made it and come back as a reconnect hours later.
//
- // Otherwise the tile is merely being looked at, and asks politely from here on. Left alone
- // while the session already holds the terminal: the flag decides what its NEXT open asks
- // for, and lowering it mid-stream would mean losing the terminal to a reconnect.
+ // Arriving is never a claim, so anything else lowers it — but not out from under an open
+ // already in flight or a stream being held, where lowering it mid-stream would hand the
+ // terminal to a reconnect.
if (takeControl) {
terminal.takeover = true;
} else if (!terminal.holdsTerminal) {
@@ -5304,11 +5305,12 @@ class AppNotifier extends ChangeNotifier {
/// nothing retries, and landing on it changes nothing: only a person pressing
/// "Take control" claims a terminal somebody else holds.
///
- /// ⚠️ **Nothing is opened ahead of a person on an OLDER machine.** A CLI that
- /// predates the key ignores it and takes the terminal over like any other
- /// open — so on such a machine this guess would cost the desktop its terminal
- /// silently, which is the one thing it must never do. Landing on a page there
- /// still opens, because that is a person arriving, not a guess. See
+ /// ⚠️ **Nothing is opened at all on an OLDER machine.** A CLI that predates
+ /// the key ignores it and takes the terminal over like any other open — so on
+ /// such a machine this guess would cost the desktop its terminal silently,
+ /// which is the one thing it must never do. [_attachSession] refuses there
+ /// for every caller now, a press excepted; this returns first only to avoid
+ /// building a tile for a stream that would be refused a line later. See
/// [MachineState.terminalNoTakeoverAvailable].
Future<void> warmAgentPane(String machineId, String agentId) async {
if (_disposed) return;
@@ -5538,6 +5540,28 @@ class AppNotifier extends ChangeNotifier {
if (machine == null) return;
if (machine.nodeOnline == false) return;
if (!machine.terminalCapabilityAvailable) return;
+ // ⚠️ **On a machine that cannot be asked politely, only a press may open at all.**
+ //
+ // `takeover: false` is the whole of the phone's politeness, and a CLI that predates the key
+ // ignores it and takes the terminal over like any other open. So on such a machine EVERY open
+ // is a takeover, whatever this was called with — and the ones that are not a press (a tile
+ // restored at launch, a page swiped onto, a machine coming back online) would take the
+ // desktop's terminal silently, which is the one thing that must never happen.
+ //
+ // [warmAgentPane] already refused to guess here. This is the same refusal, moved to the one
+ // place every session is built, so no caller can reach a silent takeover by another route.
+ // A press still opens: that IS a person asking, and it is the only thing that is.
+ //
+ // Marked rather than merely refused, so the page can offer the press instead of sitting on
+ // "Attaching…" — a null session says nothing about WHY on its own. See
+ // [TerminalPane.heldForTakeControl].
+ if (!takeControl && !machine.terminalNoTakeoverAvailable) {
+ if (!pane.heldForTakeControl) {
+ pane.heldForTakeControl = true;
+ notifyListeners();
+ }
+ return;
+ }
Agent? agent;
for (final candidate in machine.agents) {
@@ -5547,6 +5571,11 @@ class AppNotifier extends ChangeNotifier {
}
}
if (agent == null || !agent.terminalAvailable) return;
+ // Cleared only once this is certain to build a session. Cleared any earlier, a press that
+ // fell out above (the agent withdrawn between the tap and here) would leave the tile with no
+ // session AND no mark — which the page reads as "attaching", forever, with the button that is
+ // its only way forward now gone.
+ pane.heldForTakeControl = false;
final terminal = TerminalSession(
machineId: pane.machineId,
diff --git a/mobile/lib/state/terminal_pane.dart b/mobile/lib/state/terminal_pane.dart
index 014393e7f..a5feb8a8e 100644
--- a/mobile/lib/state/terminal_pane.dart
+++ b/mobile/lib/state/terminal_pane.dart
@@ -87,6 +87,22 @@ class TerminalPane {
/// Also what keeps the closed-history clean: a warm tile closed by the pager
/// was never something the person had open, so it is not remembered as such.
bool warm = false;
+
+ /// Whether this tile has no session because opening one would have TAKEN the
+ /// terminal from whoever is driving it.
+ ///
+ /// Only ever set on a machine whose CLI predates `takeover: false` (see
+ /// `MachineState.terminalNoTakeoverAvailable`). There, every open is a
+ /// takeover, so `AppNotifier._attachSession` refuses each one that is not a
+ /// person pressing "Take control" — and a tile left with a null session is
+ /// otherwise indistinguishable from one still attaching, which would leave
+ /// the page on "Attaching…" for good with no way forward.
+ ///
+ /// This is what gives it the way forward: the page reads it as "another app
+ /// may be driving this — press to take it", the same offer a `takenOver`
+ /// session gets. Cleared by the press itself, and by anything else that
+ /// attaches the tile.
+ bool heldForTakeControl = false;
}
/// A tile as it survives a restart: intent only, never the session.
diff --git a/mobile/lib/terminal/terminal_session.dart b/mobile/lib/terminal/terminal_session.dart
index ad13c05e2..e073f28dc 100644
--- a/mobile/lib/terminal/terminal_session.dart
+++ b/mobile/lib/terminal/terminal_session.dart
@@ -188,6 +188,12 @@ class TerminalSession extends ChangeNotifier {
/// a refusal that arrives after a press asks again, properly — see the `terminal_error` branch of
/// [handleFrame].
///
+ /// ⚠️ **A press arms ONE open.** Lowered again as soon as an open it armed is answered (the
+ /// `terminal_ready` branch of [handleFrame]), because by then the claim has become a lease this
+ /// session holds and later opens rest on holding it. Left standing it would outlive the press:
+ /// a phone in a pocket reconnecting hours later would take the terminal off whoever had it by
+ /// then, which is the silent takeover a press is meant to be the only cause of.
+ ///
/// ⚠️ Only meaningful against a daemon that advertises `noTakeover`: an older one ignores the key
/// and takes over regardless. The caller checks — `MachineState.terminalNoTakeoverAvailable`.
bool takeover;
@@ -565,6 +571,19 @@ class TerminalSession extends ChangeNotifier {
// The daemon's answer to a polite open on a terminal somebody else holds: it opened, it
// renders, and it may not type. See [watching].
watching = payload['readOnly'] == true;
+ // ⚠️ **The press is spent here.** A takeover was asked for and ANSWERED, so the claim it
+ // was making is now a lease this session holds — and holding it is what later opens should
+ // rest on, not the press that won it.
+ //
+ // Left standing, the flag outlived the moment: an app backgrounded for hours would come
+ // back through a reconnect and take the terminal again, off whoever was working in it by
+ // then, with nobody having asked for it since the morning. That is exactly the silent
+ // takeover a press is supposed to be the only cause of. See [takeover].
+ //
+ // Nothing is lost by lowering it. A reconnect that finds the terminal still free opens
+ // normally; one that finds it taken is refused and lands on [takenOver], where the button
+ // is — a person asks again, which is the whole rule.
+ if (!watching) takeover = false;
_resyncTimer?.cancel();
_resyncTimer = null;
_heartbeat = Timer.periodic(