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.

feat(swipe): attach neighbour agents ahead of the swipe

tony00de41747e58
diff --git a/mobile/lib/phone/agent_home.dart b/mobile/lib/phone/agent_home.dart
index 6892c49ae..7ef18d661 100644
--- a/mobile/lib/phone/agent_home.dart
+++ b/mobile/lib/phone/agent_home.dart
@@ -695,7 +695,7 @@ class _AgentHomeState extends State<AgentHome> {
});
}
- /// Attaches the agent a new pager opened on, then closes every other pane.
+ /// Attaches the agent a new pager opened on, then closes every other pane no pager is holding.
///
/// The close is what `openAgent` used to do beside its push (`keepOthers: false`), and it matters
/// more now that agents are switched IN PLACE: the pager being replaced keeps the pane of the
@@ -703,6 +703,10 @@ class _AgentHomeState extends State<AgentHome> {
/// ever show that pane again. Without it every agent picked from search would leave a live remote
/// stream behind.
///
+ /// ⚠️ **Not the panes the new pager holds.** It attaches the pages beside its own ahead of the
+ /// first swipe (see [agentPaneHeldByPager]), and closing those here would undo that a frame after
+ /// it started; they are that pager's to close, and it does, a beat after each swipe.
+ ///
/// Skipped if the screen has moved on to another pager by the time the attach lands.
Future<void> _attachOnly(({String machineId, String agentId}) agent) async {
final notifier = widget.notifier;
@@ -712,6 +716,14 @@ class _AgentHomeState extends State<AgentHome> {
if (pane.machineId == agent.machineId && pane.agentId == agent.agentId) {
continue;
}
+ final paneAgentId = pane.agentId;
+ if (paneAgentId != null &&
+ agentPaneHeldByPager((
+ machineId: pane.machineId,
+ agentId: paneAgentId,
+ ))) {
+ continue;
+ }
await notifier.closePane(pane.id);
}
}
diff --git a/mobile/lib/phone/agent_pane_prune.dart b/mobile/lib/phone/agent_pane_prune.dart
index 9a67ccc7d..87ebeef4e 100644
--- a/mobile/lib/phone/agent_pane_prune.dart
+++ b/mobile/lib/phone/agent_pane_prune.dart
@@ -19,26 +19,28 @@ void releaseAgentPanes(
if (pane == null) continue;
// Not awaited: `closePane` detaches the session and tells the daemon on its own, and every
// caller here is a callback that cannot wait for it.
- unawaited(notifier.closePane(pane.id));
+ //
+ // A warm tile was never in the saved layout (see `TerminalPane.warm`), so there is nothing to
+ // rewrite for it — and a prune runs after every swipe, which is too often to encode the whole
+ // desk for nothing.
+ unawaited(notifier.closePane(pane.id, persist: !pane.warm));
}
}
-/// Keeps the phone down to ONE open agent — the one on screen — by closing the rest a beat after
-/// each swipe.
+/// Keeps the phone down to a HANDFUL of open agents — the one on screen, the ones a swipe away, and
+/// the ones just left — by closing the rest a beat after each swipe.
///
-/// ⚠️ **Why an agent nobody is looking at must not stay open.** The daemon keeps a single controller
-/// per agent, so an open stream is a CLAIM on that agent's terminal: every agent the pager had been
-/// swiped to held one until the pager was thrown away, which on the phone is a whole session. A lap
-/// of the list left the desktop locked out of every agent on it. Closing hands each one straight
-/// back.
+/// ⚠️ **Why the rest must not stay open.** The daemon keeps a single controller per agent, so an
+/// open stream is a CLAIM on that agent's terminal, and every stream is a 10,000-line scrollback
+/// and a heartbeat on the phone: every agent the pager had been swiped to held one until the pager
+/// was thrown away, which on the phone is a whole session. A lap of a hundred agents would hold a
+/// hundred. The pager decides which few are worth holding — see `AgentSwipeHost` — and this closes
+/// everything outside that set.
///
/// ⚠️ **A beat after the swipe, not on it.** `onPageChanged` fires at the halfway point of the
/// settle, with the page being left still sliding off screen — closing its stream there empties the
/// terminal the swipe is still showing, and it finishes the animation as an "Attaching…" skeleton.
/// The beat also folds a fling across several pages into one prune, at the page it lands on.
-///
-/// The cost is that swiping back re-attaches rather than arriving on output already there. That is
-/// the trade this class exists to make: one remote stream per phone, not one per agent visited.
class AgentPanePruner {
AgentPanePruner({
required this.notifier,
@@ -59,20 +61,19 @@ class AgentPanePruner {
Timer? _timer;
- /// Arms the prune for [current], the agent now on screen. Re-arming replaces the pending one, so
- /// a flick through five pages prunes once.
- void keepOnly(AgentRef current) {
+ /// Arms the prune: everything on [attached] outside [keeping] is closed once [delay] has passed.
+ /// Re-arming replaces the pending one, so a flick through five pages prunes once, for the set the
+ /// page it lands on wants — every page change arms it again with that page's own set.
+ void keep(Set<AgentRef> keeping) {
_timer?.cancel();
_timer = Timer(delay, () {
_timer = null;
final leaving = [
for (final agent in attached)
- if (agent != current) agent,
+ if (!keeping.contains(agent)) agent,
];
if (leaving.isEmpty) return;
- attached
- ..clear()
- ..add(current);
+ attached.removeAll(leaving);
releaseAgentPanes(notifier, leaving, heldElsewhere: heldElsewhere);
});
}
diff --git a/mobile/lib/phone/agent_swipe.dart b/mobile/lib/phone/agent_swipe.dart
index 30d91bf83..ffce3de91 100644
--- a/mobile/lib/phone/agent_swipe.dart
+++ b/mobile/lib/phone/agent_swipe.dart
@@ -1,6 +1,9 @@
import 'dart:async';
import 'package:flutter/material.dart';
+// `ScrollCacheExtent` lives in the rendering layer and is not re-exported by
+// the widgets barrel, even though [PageView.scrollCacheExtent] takes it.
+import 'package:flutter/rendering.dart' show ScrollCacheExtent;
import 'package:harness_mobile/state/app_state.dart';
@@ -78,8 +81,9 @@ class _AgentSwipeHostState extends State<AgentSwipeHost> {
agentId: widget.agentId,
);
- /// Every agent this pager attached and has not closed yet — in practice the one on screen, and
- /// the one behind it until [_pruner]'s beat has passed.
+ /// Every agent this pager attached and has not closed yet — the one on screen, the ones attached
+ /// ahead of a swipe ([_prefetchAround]), and the ones just left, until [_pruner]'s beat has
+ /// passed.
///
/// ⚠️ **Without this the pager leaks terminals.** The phone's old rule was one pane, enforced by
/// closing every other one on the way in; a pager attaches its own pages, so nothing else would
@@ -90,16 +94,57 @@ class _AgentSwipeHostState extends State<AgentSwipeHost> {
/// Recorded rather than recomputed: by the time this page is disposed the list may have moved on,
/// and the panes to close are the ones actually opened, not the ones a fresh list would name.
///
- /// ⚠️ **Only agents actually SWIPED TO go in here.** The pager used to open the agent one swipe
- /// either side as well, so a page slid in already showing output instead of "Attaching…" — which
- /// took the terminal of the two agents beside the one being read away from the desktop, and they
- /// were agents nobody had asked for.
+ /// ⚠️ **Attaching ahead of the swipe takes those agents' terminals away from the desktop**, and
+ /// they are agents nobody has asked for yet. That was once the reason not to; the phone is the
+ /// primary now, and the desktop wins them back the moment it opens one (see
+ /// `AppNotifier.warmAgentPane` for what a taken-over warm page does — nothing). What bounds it
+ /// is [_keepSet]: a few agents around the one on screen, never a lap of the list.
final Set<AgentRef> _attached = {};
- /// Closes every agent but the one on screen, a beat after each swipe. Null for a passthrough page,
+ /// Closes every agent outside [_keepSet], a beat after each swipe. Null for a passthrough page,
/// which has no swipe to prune after. See [AgentPanePruner].
AgentPanePruner? _pruner;
+ /// How many pages either side of the one on screen are mounted ahead of time — and attached
+ /// ahead of time too, see [_prefetchAround]. Read by [build] for the pager's cache extent.
+ static const _reach = 2;
+
+ /// How many of the agents just swiped away from stay open, so going back is instant.
+ static const _keepRecent = 2;
+
+ /// The most streams this pager holds open at once, whatever [_reach] and [_keepRecent] add up
+ /// to: each is a scrollback and a heartbeat on the phone, and a claim on the far machine.
+ static const _maxOpen = 8;
+
+ /// How long after landing on a page its neighbours are attached. A fling through several pages
+ /// re-arms this on each one, so only the page it stops on attaches anything.
+ static const _prefetchDebounce = Duration(milliseconds: 250);
+
+ /// How long a ring of pages may take to render before the next ring is attached anyway — a page
+ /// that never renders (its machine offline, say) must not hold the others back for good.
+ static const _renderWait = Duration(milliseconds: 1500);
+
+ /// The same wait for the page on SCREEN, before anything else is attached at all. Longer, because
+ /// that page is the one somebody is looking at: measured, a first keyframe takes 2.5–3s to land,
+ /// and neighbours opening at 1.5s would compete with it for the connection for the rest of that.
+ static const _currentRenderWait = Duration(seconds: 4);
+
+ /// The agents most recently swiped AWAY from, newest first, at most [_keepRecent]. Never holds
+ /// [_current]: an agent on screen is kept for being on screen.
+ final List<AgentRef> _recent = [];
+
+ Timer? _prefetchTimer;
+
+ /// Bumped by every page change and by dispose, so a prefetch still in flight for a page the
+ /// pager has since left finds itself stale and stops between rings. See [_prefetchAround].
+ int _prefetchRun = 0;
+
+ /// The [_awaitRendered] calls still waiting, so [dispose] can let them go. Each holds a deadline
+ /// timer and a listener on the notifier; left to themselves they would clear up on the next
+ /// notifier tick, but a pager is disposed on the way to another screen, and nothing should be
+ /// left ticking behind it.
+ final Set<Completer<void>> _waits = {};
+
/// Whether [agent] belongs to a pager OTHER than this one, which is then the one to close it.
///
/// `pager != this` matters only while this pager is live — [_detachAll] is out of [_livePagers]
@@ -150,10 +195,19 @@ class _AgentSwipeHostState extends State<AgentSwipeHost> {
attached: _attached,
heldElsewhere: _heldByAnotherPager,
);
+ // The opening page's neighbours too, not only a swiped-to page's: the first swipe out of a
+ // freshly opened pager is the one most people make. The page itself is attached by whoever
+ // opened the pager (see [AgentHome]'s `_attachOnly`), and this waits for it to render first.
+ _armPrefetch();
}
@override
void dispose() {
+ _prefetchTimer?.cancel();
+ _prefetchRun++;
+ for (final wait in _waits.toList()) {
+ if (!wait.isCompleted) wait.complete();
+ }
_pruner?.dispose();
_voice.dispose();
_controller?.dispose();
@@ -248,6 +302,14 @@ class _AgentSwipeHostState extends State<AgentSwipeHost> {
// pushed without that gesture now (see `phoneRoute`'s `swipeToGoBack`), so going back is the
// header's back band, or Android's back button.
physics: const PageScrollPhysics(),
+ // ⚠️ **[_reach] pages either side are built and laid out AHEAD of the swipe.** Three things
+ // follow, and each was a cost paid on the swipe itself before: the page is not built in the
+ // first frame of the drag; its [TerminalPanel] measures the viewport while it is still off
+ // screen, so its `terminal_open` goes out at the right size with no 2s wait and no second
+ // keyframe on arrival; and that keyframe — up to 500 lines parsed on this thread — lands
+ // while nobody is looking at it. `allowImplicitScrolling` is what the cache extent requires.
+ allowImplicitScrolling: true,
+ scrollCacheExtent: const ScrollCacheExtent.viewport(_reach * 1.0),
// No count is what makes it endless: the builder answers for any page, and the modulo below
// wraps it back onto the list. A one-agent list keeps its single page instead — see
// [AgentSwipeList.wraps].
@@ -255,7 +317,12 @@ class _AgentSwipeHostState extends State<AgentSwipeHost> {
onPageChanged: _onPageChanged,
itemBuilder: (context, i) {
final entry = neighbours.entries[i % neighbours.entries.length];
- return TerminalPage(
+ // ⚠️ Tickers off for every page but the one on screen. Four pages are mounted beside it
+ // now (see `scrollCacheExtent` above), and each has things that tick — the cursor blink,
+ // the skeleton's sweep and pulse — which would otherwise run at frame rate for screens
+ // nobody can see. [TerminalPanel] reads this for its blink; the skeleton reads it to draw
+ // itself still rather than not at all.
+ return TickerMode(
// ⚠️ Keyed by PAGE, not by agent, and the difference only shows once the pager wraps: an
// endless run holds several pages for the same agent — the lap before and the lap after —
// and an agent-shaped key would make Flutter treat two live pages as one widget, which
@@ -263,12 +330,15 @@ class _AgentSwipeHostState extends State<AgentSwipeHost> {
// `_hadPane`, which is what that flag wants anyway: it is about this page's attach, not
// about the agent.
key: ValueKey(i),
- notifier: widget.notifier,
- machineId: entry.machineId,
- agentId: entry.agent.id,
- voice: _voice,
- // Exactly one mounted page, by page number — see [_page].
- isActive: i == _page,
+ enabled: i == _page,
+ child: TerminalPage(
+ notifier: widget.notifier,
+ machineId: entry.machineId,
+ agentId: entry.agent.id,
+ voice: _voice,
+ // Exactly one mounted page, by page number — see [_page].
+ isActive: i == _page,
+ ),
);
},
),
@@ -295,17 +365,159 @@ class _AgentSwipeHostState extends State<AgentSwipeHost> {
// Before the setState, so a host that rebuilds this pager in response already names the agent
// swiped to — told afterwards, it would rebuild still pointing at the previous one.
widget.onAgentChanged?.call(arrived);
+ final leaving = _current;
setState(() {
_page = index;
_current = arrived;
});
- // The agents behind this page go back to whoever else wants them, once the swipe has settled —
- // see [AgentPanePruner].
- _pruner?.keepOnly(arrived);
+ _rememberRecent(leaving, arrived);
+ // The agents outside this page's keep-set go back to whoever else wants them, once the swipe
+ // has settled — see [AgentPanePruner].
+ _pruner?.keep(_keepSet());
// Attaching is what makes the terminal live, and it only happens once the page has SETTLED —
// `onPageChanged` fires at the halfway point of a settled swipe, not on every dragged pixel, so
// flicking across five agents attaches the ones passed through rather than all of them at once.
- // `selectAgent` reuses a pane that is already there, so coming back costs nothing.
+ // `selectAgent` reuses a pane that is already there — which, with the neighbours attached ahead
+ // of time, is nearly always the case — so landing costs a focus and nothing else.
unawaited(widget.notifier.selectAgent(entry.machineId, entry.agent.id));
+ _armPrefetch();
}
+
+ /// Records the agent just swiped away from as the newest of [_recent], and takes the one arrived
+ /// at off it — that one is [_current] now, and is kept for being current.
+ void _rememberRecent(AgentRef leaving, AgentRef arrived) {
+ _recent.remove(arrived);
+ if (leaving != arrived) {
+ _recent
+ ..remove(leaving)
+ ..insert(0, leaving);
+ }
+ if (_recent.length > _keepRecent) {
+ _recent.removeRange(_keepRecent, _recent.length);
+ }
+ }
+
+ /// The agents worth holding open for the page on screen: the page itself, then the ones within
+ /// [_reach] swipes of it nearest first, then the ones just left. That order is the priority, and
+ /// [_maxOpen] cuts from the end of it.
+ Set<AgentRef> _keepSet() {
+ final ordered = <AgentRef>{_current};
+ for (final ring in _rings()) {
+ ordered.addAll(ring);
+ }
+ ordered.addAll(_recent);
+ return ordered.take(_maxOpen).toSet();
+ }
+
+ /// The agents on the pages within [_reach] of [_page], one ring per distance: `[N−1, N+1]`, then
+ /// `[N−2, N+2]`. An agent under several of those pages — a short list, wrapped — is named once,
+ /// in the nearest ring, and [_current] not at all; a ring can therefore be short, or empty.
+ List<List<AgentRef>> _rings() {
+ final neighbours = widget.neighbours;
+ if (neighbours == null || neighbours.isEmpty) return const [];
+ final entries = neighbours.entries;
+ final count = entries.length;
+ final seen = <AgentRef>{_current};
+ final rings = <List<AgentRef>>[];
+ for (var distance = 1; distance <= _reach; distance++) {
+ final ring = <AgentRef>[];
+ for (final step in [-distance, distance]) {
+ final entry = entries[((_page + step) % count + count) % count];
+ final agent = (machineId: entry.machineId, agentId: entry.agent.id);
+ if (seen.add(agent)) ring.add(agent);
+ }
+ rings.add(ring);
+ }
+ return rings;
+ }
+
+ /// Attaches the pages around the one on screen, a beat after landing on it — see
+ /// [_prefetchAround]. Re-armed by every page change, so a fling attaches only where it stops.
+ void _armPrefetch() {
+ _prefetchTimer?.cancel();
+ final run = ++_prefetchRun;
+ _prefetchTimer = Timer(_prefetchDebounce, () {
+ _prefetchTimer = null;
+ unawaited(_prefetchAround(run));
+ });
+ }
+
+ /// Opens the streams of the agents within [_reach] of the page on screen, so the next swipe —
+ /// either way — lands on output rather than on "Attaching…".
+ ///
+ /// The pages themselves are already mounted: [build] keeps [_reach] pages either side alive,
+ /// which is what lets each one measure its own viewport and take its keyframe while nobody is
+ /// looking. This is the other half — asking the machine for the stream.
+ ///
+ /// ⚠️ **In rings, and each ring only once the one before it has rendered.** The page on screen
+ /// first, then N±1, then N±2: a stream is a keyframe of up to 500 lines and then whatever the
+ /// agent prints, and four opening at once would compete with the one being read for the same
+ /// connection. [_renderWait] bounds each wait, so a page that never renders does not hold the
+ /// rest back for good.
+ ///
+ /// [run] is the page change this was armed for. Another since makes it stale, and it stops
+ /// between agents rather than opening streams around a page the pager has already left.
+ Future<void> _prefetchAround(int run) async {
+ final notifier = widget.notifier;
+ bool stale() => !mounted || run != _prefetchRun;
+ await _awaitRendered(_current, run, limit: _currentRenderWait);
+ if (stale()) return;
+ for (final ring in _rings()) {
+ for (final agent in ring) {
+ if (stale()) return;
+ final open = notifier.paneOfAgent(agent.machineId, agent.agentId);
+ if (open == null && _openPanes() >= _maxOpen) return;
+ // Recorded BEFORE the attach, for the same reason [_onPageChanged] records the page it
+ // lands on: a pane always has an owner to close it.
+ _attached.add(agent);
+ unawaited(notifier.warmAgentPane(agent.machineId, agent.agentId));
+ }
+ await Future.wait([for (final agent in ring) _awaitRendered(agent, run)]);
+ }
+ }
+
+ /// Completes once [agent]'s session has drawn its first keyframe — or after [limit], or as soon
+ /// as [run] is stale, whichever comes first.
+ Future<void> _awaitRendered(
+ AgentRef agent,
+ int run, {
+ Duration limit = _renderWait,
+ }) async {
+ final notifier = widget.notifier;
+ bool rendered() =>
+ notifier
+ .paneOfAgent(agent.machineId, agent.agentId)
+ ?.session
+ ?.hasRenderedFrame ??
+ false;
+ if (rendered()) return;
+ final done = Completer<void>();
+ void check() {
+ if (done.isCompleted) return;
+ if (rendered() || !mounted || run != _prefetchRun) done.complete();
+ }
+
+ _waits.add(done);
+ notifier.addListener(check);
+ final deadline = Timer(limit, () {
+ if (!done.isCompleted) done.complete();
+ });
+ try {
+ await done.future;
+ } finally {
+ deadline.cancel();
+ notifier.removeListener(check);
+ _waits.remove(done);
+ }
+ }
+
+ /// How many agents the phone has streams for right now — every pane on the phone is a pager's.
+ int _openPanes() =>
+ widget.notifier.panes.where((pane) => pane.agentId != null).length;
}
+
+/// Whether a pager currently mounted is holding [agent] open — on screen, a swipe away, or just
+/// left. What [AgentHome] asks before closing a pane, so tidying up after a replaced pager does not
+/// undo the new pager's prefetch a frame after it started.
+bool agentPaneHeldByPager(AgentRef agent) => _AgentSwipeHostState._livePagers
+ .any((pager) => pager._attached.contains(agent));
diff --git a/mobile/lib/phone/terminal_page.dart b/mobile/lib/phone/terminal_page.dart
index 211df9c06..7ac1cb443 100644
--- a/mobile/lib/phone/terminal_page.dart
+++ b/mobile/lib/phone/terminal_page.dart
@@ -10,7 +10,6 @@ import 'package:lucide_icons_flutter/lucide_icons.dart';
import 'package:harness_mobile/core/models.dart' show AgentProject;
import 'package:harness_mobile/shared/theme/app_theme.dart';
import 'package:harness_mobile/shared/widgets/app_icon_button.dart';
-import 'package:harness_mobile/shared/widgets/pulse.dart';
import 'package:harness_mobile/shared/widgets/skeleton.dart';
import 'package:harness_mobile/state/app_state.dart';
import 'package:harness_mobile/terminal/image_transcode.dart';
@@ -150,6 +149,26 @@ void dismissKeyboardForSwipe() {
FocusManager.instance.primaryFocus?.unfocus();
}
+/// What this page's build reads from [AppNotifier], as one value that can be
+/// compared — see [_TerminalPageState._onNotifier]. A record, so two readings
+/// are equal when every field is: the session by identity, the project by value.
+typedef _PageFacts = ({
+ int? paneId,
+ TerminalSession? session,
+ TerminalSessionStatus? status,
+ bool rendered,
+ String? agentName,
+ String? agentEngine,
+ AgentProject? agentProject,
+ bool agentPresent,
+ bool agentsFromCache,
+ AgentLoadStatus? agentLoadStatus,
+ bool machinePresent,
+ PhoneMachineStatus? machineStatus,
+ bool imagePaste,
+ String? machineName,
+});
+
class _TerminalPageState extends State<TerminalPage>
with WidgetsBindingObserver, TickerProviderStateMixin {
/// Whether this page's pane ever existed.
@@ -357,10 +376,64 @@ class _TerminalPageState extends State<TerminalPage>
setState(() => _keyboardSettling = false);
}
+ /// What the last build read from the notifier — the baseline [_onNotifier]
+ /// compares the next tick against.
+ _PageFacts? _facts;
+
+ _PageFacts _readFacts() {
+ final notifier = widget.notifier;
+ final pane = notifier.panes
+ .where(
+ (p) => p.machineId == widget.machineId && p.agentId == widget.agentId,
+ )
+ .firstOrNull;
+ final session = pane?.session;
+ final machine = notifier.stateOf(widget.machineId);
+ final agent = machine?.agents
+ .where((a) => a.id == widget.agentId)
+ .firstOrNull;
+ return (
+ paneId: pane?.id,
+ session: session,
+ status: session?.status,
+ rendered: session?.hasRenderedFrame ?? false,
+ agentName: agent?.name,
+ agentEngine: agent?.engine,
+ agentProject: agent?.project,
+ agentPresent: agent != null,
+ agentsFromCache: machine?.agentsFromCache ?? true,
+ agentLoadStatus: machine?.agentLoadStatus,
+ machinePresent: machine != null,
+ machineStatus: machine == null ? null : phoneMachineStatusOf(machine),
+ imagePaste: machine?.terminalImagePasteAvailable ?? false,
+ machineName: machine?.machine.displayName,
+ );
+ }
+
+ /// The notifier moved. Rebuilds only if something this page draws from it
+ /// has changed.
+ ///
+ /// ⚠️ **This is what a [ListenableBuilder] on the notifier used to be, and
+ /// the difference is the point.** `AppNotifier.notifyListeners()` fires for
+ /// everything on the account — an agent list landing, one agent's row
+ /// syncing, a session anywhere changing state — and the pager keeps five of
+ /// these pages mounted now. Rebuilding all five, header and chrome and all,
+ /// on every tick was a burst of work on exactly the thread the swipe animates
+ /// on. Terminal output never came through here in the first place: xterm
+ /// listens to its own `Terminal`, and the dock and the mic listen to the
+ /// session for themselves.
+ void _onNotifier() {
+ if (!mounted) return;
+ final facts = _readFacts();
+ if (facts == _facts) return;
+ setState(() => _facts = facts);
+ }
+
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
+ widget.notifier.addListener(_onNotifier);
}
/// Releases the resize hold when this page is parked mid-animation.
@@ -370,6 +443,10 @@ class _TerminalPageState extends State<TerminalPage>
@override
void didUpdateWidget(TerminalPage oldWidget) {
super.didUpdateWidget(oldWidget);
+ if (!identical(oldWidget.notifier, widget.notifier)) {
+ oldWidget.notifier.removeListener(_onNotifier);
+ widget.notifier.addListener(_onNotifier);
+ }
if (oldWidget.isActive && !widget.isActive) {
// ⚠️ Parked pages come back with their chrome shown. A page swiped away
// with its header hidden would arrive that way the next time it is swiped
@@ -386,6 +463,7 @@ class _TerminalPageState extends State<TerminalPage>
@override
void dispose() {
+ widget.notifier.removeListener(_onNotifier);
_cancelSettle();
_searchHoldTimer?.cancel();
_chrome.dispose();
@@ -728,360 +806,357 @@ class _TerminalPageState extends State<TerminalPage>
///
@override
Widget build(BuildContext context) {
- return ListenableBuilder(
- // Not the voice controller: what the mic hears repaints the floating mic
- // and its pill, which listen for themselves, and never rebuilds the
- // terminal under them.
- listenable: widget.notifier,
- builder: (context, _) {
- AppTheme.watch(context);
- // Before anything is laid out: if the key bar is about to open or shut,
- // the hold has to already be on. See [_watchKeyBar].
- _watchKeyBar();
- final pane = widget.notifier.panes
- .where(
- (p) =>
- p.machineId == widget.machineId &&
- p.agentId == widget.agentId,
- )
- .firstOrNull;
- if (pane != null) {
- _hadPane = true;
- } else if (!widget.isActive) {
- // ⚠️ **A parked page whose pane went away has to FORGET it ever had one.** It is the
- // ordinary case now: the pager closes the agent behind it a beat after each swipe, so the
- // phone holds only the one on screen (see [AgentPanePruner]). The flag means "this page's
- // stream is live", and a parked page's is not — swiping back is what attaches it again.
- // Left set, the page would land saying the agent had GONE for any frame that beats the
- // attach to it, and the branch below takes the whole pager down for that.
- _hadPane = false;
- } else if (_hadPane) {
- // The pane this page was showing is gone while it was the one being READ — another tab
- // opened a different agent, or the agent was deleted. Leave rather than spin: there is
- // nothing here to come back.
- //
- // ⚠️ Only the ACTIVE page may leave, and only it ever should. A page parked beside the one
- // being read shares the route, so popping from there would take the whole pager down —
- // including the terminal actually on screen.
- _leave();
- }
- final session = pane?.session;
- final machine = widget.notifier.stateOf(widget.machineId);
- final agent = machine?.agents
- .where((a) => a.id == widget.agentId)
- .firstOrNull;
- // ⚠️ **The agent this page opened on is not in the machine's own list.**
- // Only ever true of a page opened from the cache (see [_AgentGone]): the
- // list is the machine's — `agentsFromCache` is false, so a real
- // `agents_list` has landed — it has been LOADED rather than merely
- // attempted, and the agent it named is not in it.
- //
- // Every one of those conditions matters. Without `loaded` this would
- // fire during the window the cache exists to cover; without
- // `!agentsFromCache` it would fire against the cached list itself, which
- // is where the agent came from; and a machine that errored or went
- // offline has not disowned anything — it simply has not answered, and
- // its pane keeps the terminal's own reconnect behaviour.
- //
- // Note this deliberately ignores `_hadPane`/`_leave` above: that path
- // handles a pane REMOVED while the page was live. This one is about a
- // page that should never have been built, answered before its pane
- // could be.
- final agentGone =
- agent == null &&
- machine != null &&
- !machine.agentsFromCache &&
- 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);
- // 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.
- final canCreate =
- machine != null &&
- phoneMachineStatusOf(machine) == PhoneMachineStatus.ready;
- return Scaffold(
- backgroundColor: AppPalette.windowBg,
- // ⚠️ No fab. New agent is the `+` in the header — see the note there.
- // A Scaffold fab floats over the body, and the body here is the
- // terminal: it covered the newest line of output, which on a page
- // that streams is the line being read.
- // ⚠️ Plain `SafeArea`. A `bottom: !keyboardUp` toggle was here,
- // computed from `MediaQuery.viewInsetsOf(context).bottom > 0` — and
- // that value is pinned at ZERO inside this page (see
- // [didChangeMetrics]). The toggle therefore never toggled.
- body: SafeArea(
- child: Stack(
- children: [
- // ⚠️ The terminal is FADED, never unbuilt, while search is
- // open. Taking it down would detach the pane and drop the
- // scrollback; cancelling has to hand back the same screen that
- // was there, mid-stream.
- //
- // ⚠️ **The header is NOT inside the fade, on purpose.** The
- // overlay's bar sits exactly on top of this one and has to read
- // as the same object growing — fading this one out underneath
- // it made the row flicker on every open, which is the opposite
- // of what the expansion is for.
- // ⚠️ **Held at its height while search is open.** The search
- // field's keyboard shrinks this page like any other; followed,
- // the terminal shrank under the search and grew back through it
- // as it faded out — the flash seen on every close. Nothing here
- // is on screen while search covers it, so nothing here moves.
- HeldHeight(
- hold: _heldForSearch,
+ // Whatever brought this build about, the baseline the notifier is compared
+ // against is what THIS build saw — see [_onNotifier]. Not the voice
+ // controller: what the mic hears repaints the floating mic and its pill,
+ // which listen for themselves, and never rebuilds the terminal under them.
+ _facts = _readFacts();
+ AppTheme.watch(context);
+ // Before anything is laid out: if the key bar is about to open or shut,
+ // the hold has to already be on. See [_watchKeyBar].
+ _watchKeyBar();
+ final pane = widget.notifier.panes
+ .where(
+ (p) =>
+ p.machineId == widget.machineId &&
+ p.agentId == widget.agentId,
+ )
+ .firstOrNull;
+ if (pane != null) {
+ _hadPane = true;
+ } else if (!widget.isActive) {
+ // ⚠️ **A parked page whose pane went away has to FORGET it ever had one.** It is the
+ // ordinary case now: the pager closes the agent behind it a beat after each swipe, so the
+ // phone holds only the one on screen (see [AgentPanePruner]). The flag means "this page's
+ // stream is live", and a parked page's is not — swiping back is what attaches it again.
+ // Left set, the page would land saying the agent had GONE for any frame that beats the
+ // attach to it, and the branch below takes the whole pager down for that.
+ _hadPane = false;
+ } else if (_hadPane) {
+ // The pane this page was showing is gone while it was the one being READ — another tab
+ // opened a different agent, or the agent was deleted. Leave rather than spin: there is
+ // nothing here to come back.
+ //
+ // ⚠️ Only the ACTIVE page may leave, and only it ever should. A page parked beside the one
+ // being read shares the route, so popping from there would take the whole pager down —
+ // including the terminal actually on screen.
+ _leave();
+ }
+ final session = pane?.session;
+ final machine = widget.notifier.stateOf(widget.machineId);
+ final agent = machine?.agents
+ .where((a) => a.id == widget.agentId)
+ .firstOrNull;
+ // ⚠️ **The agent this page opened on is not in the machine's own list.**
+ // Only ever true of a page opened from the cache (see [_AgentGone]): the
+ // list is the machine's — `agentsFromCache` is false, so a real
+ // `agents_list` has landed — it has been LOADED rather than merely
+ // attempted, and the agent it named is not in it.
+ //
+ // Every one of those conditions matters. Without `loaded` this would
+ // fire during the window the cache exists to cover; without
+ // `!agentsFromCache` it would fire against the cached list itself, which
+ // is where the agent came from; and a machine that errored or went
+ // offline has not disowned anything — it simply has not answered, and
+ // its pane keeps the terminal's own reconnect behaviour.
+ //
+ // Note this deliberately ignores `_hadPane`/`_leave` above: that path
+ // handles a pane REMOVED while the page was live. This one is about a
+ // page that should never have been built, answered before its pane
+ // could be.
+ final agentGone =
+ agent == null &&
+ machine != null &&
+ !machine.agentsFromCache &&
+ 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);
+ // 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.
+ final canCreate =
+ machine != null &&
+ phoneMachineStatusOf(machine) == PhoneMachineStatus.ready;
+ return Scaffold(
+ backgroundColor: AppPalette.windowBg,
+ // ⚠️ No fab. New agent is the `+` in the header — see the note there.
+ // A Scaffold fab floats over the body, and the body here is the
+ // terminal: it covered the newest line of output, which on a page
+ // that streams is the line being read.
+ // ⚠️ Plain `SafeArea`. A `bottom: !keyboardUp` toggle was here,
+ // computed from `MediaQuery.viewInsetsOf(context).bottom > 0` — and
+ // that value is pinned at ZERO inside this page (see
+ // [didChangeMetrics]). The toggle therefore never toggled.
+ body: SafeArea(
+ child: Stack(
+ children: [
+ // ⚠️ The terminal is FADED, never unbuilt, while search is
+ // open. Taking it down would detach the pane and drop the
+ // scrollback; cancelling has to hand back the same screen that
+ // was there, mid-stream.
+ //
+ // ⚠️ **The header is NOT inside the fade, on purpose.** The
+ // overlay's bar sits exactly on top of this one and has to read
+ // as the same object growing — fading this one out underneath
+ // it made the row flicker on every open, which is the opposite
+ // of what the expansion is for.
+ // ⚠️ **Held at its height while search is open.** The search
+ // field's keyboard shrinks this page like any other; followed,
+ // the terminal shrank under the search and grew back through it
+ // as it faded out — the flash seen on every close. Nothing here
+ // is on screen while search covers it, so nothing here moves.
+ HeldHeight(
+ hold: _heldForSearch,
+ child: Column(
+ children: [
+ Expanded(
+ // ⚠️ The mic floats INSIDE this box, over the terminal
+ // — not over the whole page. Stacked any higher it
+ // would hang over the key bar while the keyboard is
+ // up, which is the one row the thumb is working.
+ child: Stack(
+ children: [
+ Positioned.fill(
+ // ⚠️ The chrome is driven from OUT HERE, not
+ // from inside the panel. xterm's own
+ // [Scrollable] is several widgets down and is
+ // remounted whenever the agent changes;
+ // listening for its notifications as they
+ // bubble past is what survives that, and costs
+ // the panel no knowledge of the page's chrome.
+ child: NotificationListener<ScrollNotification>(
+ onNotification: _chrome.onNotification,
+ child: agentGone
+ ? _AgentGone(
+ name: _cachedAgentName,
+ onPickAnother: _pickAnotherAgent,
+ )
+ : pane == null || session == null
+ ? const _Attaching()
+ : TerminalPanel(
+ key: ValueKey(pane.id),
+ notifier: widget.notifier,
+ session: session,
+ // Only the page on screen takes the keyboard — see
+ // [TerminalPage.isActive]. `visible` is the same answer for the
+ // panel's other half: a page parked beside this one releases
+ // focus, stops rendering and stops resizing its remote shell.
+ //
+ // Whether it also HOLDS one that is already
+ // up is a separate question, and the pager asks
+ // it on every swipe — see [_shouldFocus].
+ focused: _shouldFocus,
+ visible: widget.isActive,
+ // Hold the remote resize while the keyboard
+ // slides. Separate from `visible` because this
+ // must NOT release focus — the animation being
+ // waited on is the one that focus started.
+ //
+ // The keyboard is the only thing left that
+ // moves this pane's height: the header slides
+ // OVER the terminal rather than out of its
+ // column — see [_SlideAway].
+ //
+ // ⚠️ Held for as long as search is open, too:
+ // its keyboard is typing a query over a faded
+ // terminal, and resizing the agent's shell for
+ // it redrew the whole TUI on the way in and
+ // again on the way out.
+ settling:
+ _keyboardSettling || _heldForSearch,
+ // ⚠️ The tap is taken in the panel, not by a
+ // `Listener` over it. xterm's own `_onTapDown`
+ // calls `requestKeyboard()`, so anything that
+ // merely ALSO reacted to the tap would raise
+ // the keyboard before the words said were typed
+ // into the prompt — and a re-armed claim on top
+ // of it was measured asking Android twice per
+ // tap, which answers a show mid-animation by
+ // cancelling and restarting it. Null while the
+ // keyboard is up or coming, so the tap is
+ // xterm's and the keyboard stays.
+ onInputTap: _shouldFocus
+ ? null
+ : () => unawaited(
+ _raiseKeyboard(session),
+ ),
+ showHeader: false,
+ // No composer, and so no grip above it: the
+ // page hands the pane its full height and the
+ // software keyboard drives the terminal
+ // directly. The mic's send is what kept the
+ // composer's batched turn.
+ ),
+ ),
+ ),
+ // ⚠️ **The skeleton is laid OVER the live panel, not
+ // swapped in for it, and that is not a stylistic
+ // choice.** [TerminalPanel.initState] calls
+ // `session.attachViewport`, which is how the machine
+ // learns how many rows and columns to draw; a page
+ // that showed a skeleton INSTEAD would leave the
+ // session with no viewport, and the first keyframe
+ // would arrive sized for nothing.
+ //
+ // So the panel mounts, measures and resizes as always,
+ // and this covers the empty emulator buffer it paints
+ // meanwhile. `session == null` upstream keeps its own
+ // branch for the frames before a session exists at all.
+ //
+ // ⚠️ **This is also the bug that made the skeleton
+ // invisible.** The only gate used to be `session ==
+ // null`, and `selectAgent` creates the pane and the
+ // session in one frame — so the branch above was
+ // essentially never taken, and what a person actually
+ // waited in front of was a mounted terminal with an
+ // empty buffer: a black rectangle, for as long as the
+ // keyframe took.
+ if (session != null && !session.hasRenderedFrame)
+ const Positioned.fill(child: _Attaching()),
+ // The mic, Search and New agent, floating in the
+ // terminal's bottom-right corner — see
+ // [TerminalActionColumn].
+ //
+ // ⚠️ Hidden while this page owns the keyboard.
+ // Typing is the other way of saying what the mic
+ // says, the key bar is already under the thumb, and
+ // a column floating over the prompt being typed into
+ // would be in the way of both.
+ if (!_ownsInput)
+ Positioned(
+ right: TerminalActionColumn.inset,
+ bottom: TerminalActionColumn.bottomInset,
+ child: TerminalActionColumn(
+ voice: widget.voice,
+ session: session,
+ onSearch: _openSearch,
+ onNewAgent: canCreate
+ ? () => unawaited(_newAgent())
+ : null,
+ ),
+ ),
+ ],
+ ),
+ ),
+ // The bottom of this page IS just above the keyboard:
+ // `PhoneShell`'s Scaffold has already resized for it —
+ // the same resize that empties this page's MediaQuery
+ // insets (see [didChangeMetrics]).
+ if (session != null)
+ TerminalInputDock(
+ session: session,
+ keyboardUp: _keyBarUp,
+ onDismiss: _dismissInput,
+ // Only where the far side can actually take one: an
+ // older CLI never advertises the binary kind, so the
+ // upload would go nowhere silently. Null leaves the
+ // buttons undrawn rather than drawn dead.
+ onPickImage:
+ machine?.terminalImagePasteAvailable == true
+ ? () => unawaited(
+ _sendImage(session, ImageSource.gallery),
+ )
+ : null,
+ onTakePhoto:
+ machine?.terminalImagePasteAvailable == true
+ ? () => unawaited(
+ _sendImage(session, ImageSource.camera),
+ )
+ : null,
+ ),
+ ],
+ ),
+ ),
+ // The header, laid OVER the terminal rather than above it in the
+ // column.
+ //
+ // ⚠️ **Over, not beside, and that is what keeps scrolling smooth.**
+ // It used to shrink out of the column and hand its height to the
+ // terminal. Every fold then changed the terminal's row count: a
+ // `terminal_resize` and a real SIGWINCH on the far machine, a
+ // keyframe back carrying up to 500 lines of history to re-parse on
+ // this thread, and the agent's whole screen redrawn — once as the
+ // header left, once as it came back, on every change of scroll
+ // direction. The renderer was held frozen for each slide on top of
+ // that, so a fling stopped dead for a third of a second.
+ //
+ // Laid over, the terminal keeps one height whatever the header
+ // does. The rows under it are the oldest on the screen, and the
+ // first push of a scroll is what slides it away from them.
+ //
+ // Nothing stands in for it while it is gone: `⋯` comes back with
+ // the header on the first scroll the other way.
+ Positioned(
+ top: 0,
+ left: 0,
+ right: 0,
+ child: _SlideAway(
+ progress: _chrome.header,
+ // Opaque: the terminal is underneath now, and the row has to
+ // read as the same bar it was when the two were stacked.
+ child: ColoredBox(
+ color: AppPalette.windowBg,
child: Column(
+ mainAxisSize: MainAxisSize.min,
children: [
- Expanded(
- // ⚠️ The mic floats INSIDE this box, over the terminal
- // — not over the whole page. Stacked any higher it
- // would hang over the key bar while the keyboard is
- // up, which is the one row the thumb is working.
- child: Stack(
- children: [
- Positioned.fill(
- // ⚠️ The chrome is driven from OUT HERE, not
- // from inside the panel. xterm's own
- // [Scrollable] is several widgets down and is
- // remounted whenever the agent changes;
- // listening for its notifications as they
- // bubble past is what survives that, and costs
- // the panel no knowledge of the page's chrome.
- child: NotificationListener<ScrollNotification>(
- onNotification: _chrome.onNotification,
- child: agentGone
- ? _AgentGone(
- name: _cachedAgentName,
- onPickAnother: _pickAnotherAgent,
- )
- : pane == null || session == null
- ? const _Attaching()
- : TerminalPanel(
- key: ValueKey(pane.id),
- notifier: widget.notifier,
- session: session,
- // Only the page on screen takes the keyboard — see
- // [TerminalPage.isActive]. `visible` is the same answer for the
- // panel's other half: a page parked beside this one releases
- // focus, stops rendering and stops resizing its remote shell.
- //
- // Whether it also HOLDS one that is already
- // up is a separate question, and the pager asks
- // it on every swipe — see [_shouldFocus].
- focused: _shouldFocus,
- visible: widget.isActive,
- // Hold the remote resize while the keyboard
- // slides. Separate from `visible` because this
- // must NOT release focus — the animation being
- // waited on is the one that focus started.
- //
- // The keyboard is the only thing left that
- // moves this pane's height: the header slides
- // OVER the terminal rather than out of its
- // column — see [_SlideAway].
- //
- // ⚠️ Held for as long as search is open, too:
- // its keyboard is typing a query over a faded
- // terminal, and resizing the agent's shell for
- // it redrew the whole TUI on the way in and
- // again on the way out.
- settling:
- _keyboardSettling || _heldForSearch,
- // ⚠️ The tap is taken in the panel, not by a
- // `Listener` over it. xterm's own `_onTapDown`
- // calls `requestKeyboard()`, so anything that
- // merely ALSO reacted to the tap would raise
- // the keyboard before the words said were typed
- // into the prompt — and a re-armed claim on top
- // of it was measured asking Android twice per
- // tap, which answers a show mid-animation by
- // cancelling and restarting it. Null while the
- // keyboard is up or coming, so the tap is
- // xterm's and the keyboard stays.
- onInputTap: _shouldFocus
- ? null
- : () => unawaited(
- _raiseKeyboard(session),
- ),
- showHeader: false,
- // No composer, and so no grip above it: the
- // page hands the pane its full height and the
- // software keyboard drives the terminal
- // directly. The mic's send is what kept the
- // composer's batched turn.
- ),
+ TerminalHeader(
+ agent: agent,
+ status: phoneSessionSummary(session),
+ trailing: [
+ // Read-only is a state to get OUT of, so its way
+ // out is a labelled button in the header rather
+ // than a line in the actions sheet: the sheet is
+ // where you go having decided to do something,
+ // and this is the thing telling you that typing
+ // will go nowhere until you do.
+ if (reclaim != null)
+ _ReclaimButton(
+ action: reclaim,
+ onPressed: () => widget.notifier.selectAgent(
+ widget.machineId,
+ widget.agentId,
),
),
- // ⚠️ **The skeleton is laid OVER the live panel, not
- // swapped in for it, and that is not a stylistic
- // choice.** [TerminalPanel.initState] calls
- // `session.attachViewport`, which is how the machine
- // learns how many rows and columns to draw; a page
- // that showed a skeleton INSTEAD would leave the
- // session with no viewport, and the first keyframe
- // would arrive sized for nothing.
- //
- // So the panel mounts, measures and resizes as always,
- // and this covers the empty emulator buffer it paints
- // meanwhile. `session == null` upstream keeps its own
- // branch for the frames before a session exists at all.
- //
- // ⚠️ **This is also the bug that made the skeleton
- // invisible.** The only gate used to be `session ==
- // null`, and `selectAgent` creates the pane and the
- // session in one frame — so the branch above was
- // essentially never taken, and what a person actually
- // waited in front of was a mounted terminal with an
- // empty buffer: a black rectangle, for as long as the
- // keyframe took.
- if (session != null && !session.hasRenderedFrame)
- const Positioned.fill(child: _Attaching()),
- // The mic, Search and New agent, floating in the
- // terminal's bottom-right corner — see
- // [TerminalActionColumn].
- //
- // ⚠️ Hidden while this page owns the keyboard.
- // Typing is the other way of saying what the mic
- // says, the key bar is already under the thumb, and
- // a column floating over the prompt being typed into
- // would be in the way of both.
- if (!_ownsInput)
- Positioned(
- right: TerminalActionColumn.inset,
- bottom: TerminalActionColumn.bottomInset,
- child: TerminalActionColumn(
- voice: widget.voice,
- session: session,
- onSearch: _openSearch,
- onNewAgent: canCreate
- ? () => unawaited(_newAgent())
- : null,
- ),
+ // Null while the agent is not loaded: there is
+ // nothing to act on yet, and a menu of actions
+ // that all fail is worse than no menu.
+ if (agent != null)
+ _HeaderAction(
+ icon: LucideIcons.ellipsis300,
+ size: 21,
+ tooltip: 'Agent actions',
+ // Last in the row, so its padding stops at
+ // the header's own right inset.
+ last: true,
+ onPressed: () => _showActions(
+ machineName:
+ machine?.machine.displayName ?? '',
+ agentName: agent.name,
+ project: agent.project,
),
- ],
- ),
- ),
- // The bottom of this page IS just above the keyboard:
- // `PhoneShell`'s Scaffold has already resized for it —
- // the same resize that empties this page's MediaQuery
- // insets (see [didChangeMetrics]).
- if (session != null)
- TerminalInputDock(
- session: session,
- keyboardUp: _keyBarUp,
- onDismiss: _dismissInput,
- // Only where the far side can actually take one: an
- // older CLI never advertises the binary kind, so the
- // upload would go nowhere silently. Null leaves the
- // buttons undrawn rather than drawn dead.
- onPickImage:
- machine?.terminalImagePasteAvailable == true
- ? () => unawaited(
- _sendImage(session, ImageSource.gallery),
- )
- : null,
- onTakePhoto:
- machine?.terminalImagePasteAvailable == true
- ? () => unawaited(
- _sendImage(session, ImageSource.camera),
- )
- : null,
- ),
- ],
- ),
- ),
- // The header, laid OVER the terminal rather than above it in the
- // column.
- //
- // ⚠️ **Over, not beside, and that is what keeps scrolling smooth.**
- // It used to shrink out of the column and hand its height to the
- // terminal. Every fold then changed the terminal's row count: a
- // `terminal_resize` and a real SIGWINCH on the far machine, a
- // keyframe back carrying up to 500 lines of history to re-parse on
- // this thread, and the agent's whole screen redrawn — once as the
- // header left, once as it came back, on every change of scroll
- // direction. The renderer was held frozen for each slide on top of
- // that, so a fling stopped dead for a third of a second.
- //
- // Laid over, the terminal keeps one height whatever the header
- // does. The rows under it are the oldest on the screen, and the
- // first push of a scroll is what slides it away from them.
- //
- // Nothing stands in for it while it is gone: `⋯` comes back with
- // the header on the first scroll the other way.
- Positioned(
- top: 0,
- left: 0,
- right: 0,
- child: _SlideAway(
- progress: _chrome.header,
- // Opaque: the terminal is underneath now, and the row has to
- // read as the same bar it was when the two were stacked.
- child: ColoredBox(
- color: AppPalette.windowBg,
- child: Column(
- mainAxisSize: MainAxisSize.min,
- children: [
- TerminalHeader(
- agent: agent,
- status: phoneSessionSummary(session),
- trailing: [
- // Read-only is a state to get OUT of, so its way
- // out is a labelled button in the header rather
- // than a line in the actions sheet: the sheet is
- // where you go having decided to do something,
- // and this is the thing telling you that typing
- // will go nowhere until you do.
- if (reclaim != null)
- _ReclaimButton(
- action: reclaim,
- onPressed: () => widget.notifier.selectAgent(
- widget.machineId,
- widget.agentId,
- ),
- ),
- // Null while the agent is not loaded: there is
- // nothing to act on yet, and a menu of actions
- // that all fail is worse than no menu.
- if (agent != null)
- _HeaderAction(
- icon: LucideIcons.ellipsis300,
- size: 21,
- tooltip: 'Agent actions',
- // Last in the row, so its padding stops at
- // the header's own right inset.
- last: true,
- onPressed: () => _showActions(
- machineName:
- machine?.machine.displayName ?? '',
- agentName: agent.name,
- project: agent.project,
- ),
- ),
- ],
- ),
- Divider(height: 1, color: AppGlass.hair),
+ ),
],
),
- ),
+ Divider(height: 1, color: AppGlass.hair),
+ ],
),
),
- // Search, faded up over the whole page. Only built while it is
- // on its way in, up, or on its way out — see [_searching].
- if (_searching)
- Positioned.fill(
- child: TerminalSearchOverlay(
- notifier: widget.notifier,
- animation: _searchCurve,
- onClose: _closeSearch,
- ),
- ),
- ],
+ ),
),
- ),
- );
- },
+ // Search, faded up over the whole page. Only built while it is
+ // on its way in, up, or on its way out — see [_searching].
+ if (_searching)
+ Positioned.fill(
+ child: TerminalSearchOverlay(
+ notifier: widget.notifier,
+ animation: _searchCurve,
+ onClose: _closeSearch,
+ ),
+ ),
+ ],
+ ),
+ ),
);
}
@@ -1380,8 +1455,7 @@ class _Attaching extends StatefulWidget {
State<_Attaching> createState() => _AttachingState();
}
-class _AttachingState extends State<_Attaching>
- with SingleTickerProviderStateMixin {
+class _AttachingState extends State<_Attaching> with TickerProviderStateMixin {
/// How long the block takes to arrive, top edge to bottom.
///
/// ⚠️ **Matched to the wait it covers, not chosen for its own sake.** Measured
@@ -1413,20 +1487,76 @@ class _AttachingState extends State<_Attaching>
/// the pulse underneath keeps the block alive.
///
/// 0.84 leaves roughly the last 9% of the pane below the soft edge — about one
- /// turn on a phone. Worked out against [_feather] rather than guessed: the
- /// edge travels `t * (1 + 2f) - f`, so it reaches the bottom at `t = 1`, and
- /// values much above this finish the sweep after all.
+ /// turn on a phone. Worked out against [_SkeletonPainter._feather] rather than
+ /// guessed: the edge travels `t * (1 + 2f) - f`, so it reaches the bottom at
+ /// `t = 1`, and values much above this finish the sweep after all.
static const _sweepExtent = 0.84;
+ /// One half of the breath — rest to peak; the whole breath is twice this. The
+ /// 1100ms `shared/widgets/skeleton.dart` sets for a placeholder: slower than
+ /// the status LED's blink, because the two are different instruments.
+ static const _pulseDuration = Duration(milliseconds: 1100);
+
late final AnimationController _reveal = AnimationController(
vsync: this,
duration: _revealDuration,
upperBound: _sweepExtent,
- )..forward();
+ );
+
+ /// The bars' breath — one controller for the whole block, so the rows breathe
+ /// together rather than shimmering independently.
+ ///
+ /// Owned here rather than borrowed from `Pulse`: that widget rebuilds its
+ /// subtree on every frame of the breath, which for this block was seventy-odd
+ /// widgets per frame — and the whole reason [_SkeletonPainter] exists is to
+ /// spend a repaint on it instead.
+ late final AnimationController _pulse = AnimationController(
+ vsync: this,
+ duration: _pulseDuration,
+ );
+
+ late final Animation<double> _breath = CurvedAnimation(
+ parent: _pulse,
+ curve: Curves.easeInOut,
+ );
+
+ /// Whether the sweep and the breath are running. False with the page parked
+ /// beside the one on screen, or with animations turned off — see
+ /// [didChangeDependencies] for what is drawn then.
+ bool _animating = false;
+
+ @override
+ void didChangeDependencies() {
+ super.didChangeDependencies();
+ // ⚠️ **Still, not blank, when it cannot animate.** The pager keeps this page
+ // mounted beside the one on screen with its tickers off (see
+ // `AgentSwipeHost`), and a sweep that never started would leave the reveal
+ // covering everything: the page would slide in as bare ground. So with no
+ // ticker the sweep is jumped to where it would end and the breath held at
+ // rest, and the block is simply there. The same answer for a person who has
+ // turned animations off, which `Pulse` gives too.
+ final animate =
+ TickerMode.valuesOf(context).enabled &&
+ !MediaQuery.disableAnimationsOf(context);
+ if (animate == _animating) return;
+ _animating = animate;
+ if (animate) {
+ if (!_reveal.isCompleted) _reveal.forward();
+ _pulse.repeat(reverse: true);
+ } else {
+ _reveal
+ ..stop()
+ ..value = _sweepExtent;
+ _pulse
+ ..stop()
+ ..value = 0;
+ }
+ }
@override
void dispose() {
_reveal.dispose();
+ _pulse.dispose();
super.dispose();
}
@@ -1435,7 +1565,7 @@ class _AttachingState extends State<_Attaching>
/// ⚠️ **A fixed list of turns cannot fill a screen.** Four of them left the
/// top half of a tall phone empty, which reads as a page that finished loading
/// with almost nothing on it — the opposite of what a skeleton is for. The
- /// build measures the pane and takes as many of these as it needs, so the
+ /// painter measures the pane and takes as many of these as it needs, so the
/// block reaches the top edge on any device at any terminal font size.
///
/// Five, and a prime count, so cycling does not line the same shape up under
@@ -1483,267 +1613,102 @@ class _AttachingState extends State<_Attaching>
(light ? Colors.black : Colors.white).withValues(alpha: 0.17),
ground,
);
+ // The prompt's banner sits on its own ground, a step above the terminal's —
+ // the band an agent paints behind the line it is answering, and the one
+ // element that makes this block read as a transcript rather than as a list
+ // of lines.
+ //
+ // 0.08, not the 0.05 this started at: the banner has to be legible as a BAND
+ // at arm's length on a phone, and at 0.05 over `#181818` it was a shade
+ // nobody would notice. Still well under the bars themselves (0.10–0.17), so
+ // it reads as the ground behind them rather than as another bar.
+ final banner = Color.lerp(
+ ground,
+ light ? Colors.black : Colors.white,
+ 0.08,
+ )!;
+ final bar = (fontSize * 0.62).clamp(5.0, 12.0).toDouble();
return SkeletonBlock(
semanticsLabel: 'Attaching to the agent',
- child: _RevealDown(
- listenable: _reveal,
- child: ColoredBox(
- // Opaque, because this covers a live [TerminalPanel] — see the note at
- // the call site. Without it the empty emulator buffer shows between the
- // bars and the two grounds differ by a hair, which reads as a smudge.
- color: ground,
- child: Padding(
- // ⚠️ The terminal view's OWN padding (`TerminalPanel` passes
- // `EdgeInsets.all(10)` to the xterm view), so a bar starts on the
- // column the first character will occupy. A different gutter here
- // would shift every line sideways the moment the keyframe lands.
- padding: const EdgeInsets.all(10),
- child: Pulse(
- // The breath `shared/widgets/skeleton.dart` sets for a placeholder;
- // one controller for the whole block rather than one per bar, so
- // the rows pulse together instead of shimmering independently.
- duration: const Duration(milliseconds: 1100),
- builder: (context, t, _) {
- final fill = Color.lerp(rest, peak, t)!;
- // The prompt's banner sits on its own ground, a step above the
- // terminal's — the band an agent paints behind the line it is
- // answering, and the one element that makes this block read as a
- // transcript rather than as a list of lines.
- //
- // 0.08, not the 0.05 this started at: the banner has to be legible
- // as a BAND at arm's length on a phone, and at 0.05 over `#181818`
- // it was a shade nobody would notice. Still well under the bars
- // themselves (0.10–0.17), so it reads as the ground behind them
- // rather than as another bar.
- final banner = Color.lerp(
- ground,
- light ? Colors.black : Colors.white,
- 0.08,
- )!;
- final bar = (fontSize * 0.62).clamp(5.0, 12.0).toDouble();
- return LayoutBuilder(
- builder: (context, constraints) {
- // ⚠️ **As many turns as the pane is tall, not a fixed four.**
- // A fixed list left the top half of a tall phone empty, which
- // reads as a page that has finished loading and has almost
- // nothing on it. One extra turn is added beyond the measured
- // fit so the topmost one is genuinely cut by the edge rather
- // than ending a few pixels short of it.
- final height = constraints.maxHeight;
- final perTurn = _turnHeight(
- _shapes[0],
- lineHeight: lineHeight,
- );
- final count = height.isFinite && perTurn > 0
- ? (height / perTurn).ceil() + 1
- : _shapes.length;
- final turns = [
- for (var i = 0; i < count; i++)
- // Cycled from the back, so the LAST shape is always the
- // one at the bottom edge: it is the only one written
- // without a closing meta line, which is what an in-progress
- // turn looks like.
- _shapes[(_shapes.length - 1 - i % _shapes.length)],
- ].reversed.toList();
- final blocks = <Widget>[
- for (var i = 0; i < turns.length; i++)
- Opacity(
- // Oldest at 0.4, newest at full: the block nearest the
- // top edge is the one about to be clipped by it, so it
- // fades INTO that edge rather than ending against it.
- opacity: turns.length == 1
- ? 1
- : 0.4 + (i / (turns.length - 1)) * 0.6,
- child: _turn(
- turns[i],
- lineHeight: lineHeight,
- bar: bar,
- fill: fill,
- banner: banner,
- // The newest turn ends at the bottom edge, as the live
- // screen's does; only the ones above it are separated.
- last: i == turns.length - 1,
- ),
- ),
- ];
- // ⚠️ Aligned to the BOTTOM and clipped at the top, which is how
- // a terminal itself sits: the newest output is at the bottom
- // edge and history runs off the top. `OverflowBox` lets the
- // column exceed the pane — the extra turn above guarantees it
- // does — and `ClipRect` cuts what runs past, instead of Flutter
- // painting an overflow stripe over the whole thing.
- return ClipRect(
- child: OverflowBox(
- alignment: Alignment.bottomLeft,
- minHeight: 0,
- maxHeight: double.infinity,
- // ⚠️ The pane's own width is pinned back on, because
- // `OverflowBox` relaxes BOTH axes and the rows inside are
- // `FractionallySizedBox` — against an unbounded width they
- // would have no fraction to take and the layout would throw.
- minWidth: constraints.maxWidth,
- maxWidth: constraints.maxWidth,
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.stretch,
- mainAxisSize: MainAxisSize.min,
- children: blocks,
- ),
- ),
- );
- },
- );
- },
- ),
+ // ⚠️ **One painter, repainted, in a layer of its own** — where this was a
+ // column of seventy-odd widgets rebuilt on every frame of the breath, under
+ // a [ShaderMask] that pushed the whole pane through an offscreen buffer on
+ // every frame of the sweep. Both animations are a repaint of one render
+ // object now, and the [RepaintBoundary] keeps that repaint from reaching
+ // the live panel underneath or the chrome above.
+ child: RepaintBoundary(
+ child: CustomPaint(
+ painter: _SkeletonPainter(
+ breath: _breath,
+ reveal: _reveal,
+ lineHeight: lineHeight,
+ bar: bar,
+ ground: ground,
+ rest: rest,
+ peak: peak,
+ banner: banner,
),
+ child: const SizedBox.expand(),
),
),
);
}
- /// One turn: the prompt banner, the bulleted answer under it, the meta line.
- ///
- /// Every row is exactly one terminal line box tall, so the whole block
- /// occupies a whole number of rows and the keyframe replaces it without the
- /// page growing or shrinking by a fraction of a line.
- /// What one turn occupies, so the build can work out how many fit.
+ /// What one turn occupies, so the painter can work out how many fit.
///
- /// ⚠️ **Must stay in step with [_turn] below**, which is why the terms are
- /// written in the same order as the widgets they measure: banner, gap, answer
- /// lines, gap and meta line, trailing gap. A drift here does not break the
- /// layout — the column is clipped either way — it just means a turn too few
- /// (a band of empty ground at the top) or one too many (wasted build).
+ /// ⚠️ **Must stay in step with [_SkeletonPainter._turn]**, which is why the
+ /// terms are written in the same order as the rows it draws: banner, gap,
+ /// answer lines, gap and meta line, trailing gap. A drift here does not break
+ /// the picture — the block is clipped either way — it just means a turn too
+ /// few (a band of empty ground at the top) or one too many (wasted paint).
static double _turnHeight(_SkeletonTurn turn, {required double lineHeight}) =>
lineHeight + // the prompt banner
lineHeight * 0.35 + // the gap under it
lineHeight * turn.answer.length +
(turn.meta == null ? 0 : lineHeight * 0.35 + lineHeight) +
lineHeight * 0.9; // the gap to the next turn
-
- static Widget _turn(
- _SkeletonTurn turn, {
- required double lineHeight,
- required double bar,
- required Color fill,
- required Color banner,
- bool last = false,
- }) {
- Widget line(double factor, {double indent = 0, double? height}) => SizedBox(
- height: lineHeight,
- child: Align(
- alignment: Alignment.centerLeft,
- child: Padding(
- padding: EdgeInsets.only(left: indent),
- child: FractionallySizedBox(
- alignment: Alignment.centerLeft,
- widthFactor: factor,
- child: Container(
- height: height ?? bar,
- decoration: BoxDecoration(
- color: fill,
- borderRadius: BorderRadius.circular(3),
- ),
- ),
- ),
- ),
- ),
- );
-
- return Column(
- crossAxisAlignment: CrossAxisAlignment.stretch,
- mainAxisSize: MainAxisSize.min,
- children: [
- // The prompt, on its own full-width ground — the one element that makes
- // this read as a transcript rather than as a list of lines.
- Container(
- height: lineHeight,
- color: banner,
- child: Row(
- children: [
- // The `›` gutter the real prompt keeps.
- SizedBox(width: bar * 0.9),
- Expanded(
- child: FractionallySizedBox(
- alignment: Alignment.centerLeft,
- widthFactor: turn.prompt,
- child: Container(
- height: bar,
- decoration: BoxDecoration(
- color: fill,
- borderRadius: BorderRadius.circular(3),
- ),
- ),
- ),
- ),
- ],
- ),
- ),
- SizedBox(height: lineHeight * 0.35),
- // The answer: a bullet on the first row, the rest indented under it.
- for (var i = 0; i < turn.answer.length; i++)
- if (i == 0)
- SizedBox(
- height: lineHeight,
- child: Row(
- crossAxisAlignment: CrossAxisAlignment.center,
- children: [
- Container(
- width: bar * 0.55,
- height: bar * 0.55,
- decoration: BoxDecoration(
- color: fill,
- shape: BoxShape.circle,
- ),
- ),
- SizedBox(width: bar * 0.7),
- Expanded(
- child: FractionallySizedBox(
- alignment: Alignment.centerLeft,
- widthFactor: turn.answer[i],
- child: Container(
- height: bar,
- decoration: BoxDecoration(
- color: fill,
- borderRadius: BorderRadius.circular(3),
- ),
- ),
- ),
- ),
- ],
- ),
- )
- else
- line(turn.answer[i], indent: bar * 1.25),
- if (turn.meta != null) ...[
- SizedBox(height: lineHeight * 0.35),
- // The `✳ Crunched for 3s · done 10:36` line that closes a turn: always
- // shorter, and thinner than a line of body text.
- line(turn.meta!, height: bar * 0.7),
- ],
- if (!last) SizedBox(height: lineHeight * 0.9),
- ],
- );
- }
}
-/// The shape of one transcript turn, for [_Attaching].
-/// Wipes [child] in from the top edge to the bottom over [listenable]'s 0→1.
-///
-/// ⚠️ **A wipe, not a fade or a slide.** The skeleton stands in for a terminal
-/// whose rows must not move — every bar is already at the pixel its text will
-/// occupy (see [_Attaching]) — so the reveal cannot translate anything. A plain
-/// fade would have the whole block appear at once, which says nothing about the
-/// direction output arrives from. Masking by height instead lets the rows arrive
-/// the way a screen paints: from the top, downward, each one landing where it
-/// will stay.
+/// Paints [_Attaching]: the turns of a transcript, bottom-aligned and clipped at
+/// the top the way a terminal sits, breathing with [breath] and wiped in from
+/// the top by [reveal].
///
-/// The leading edge is a short gradient rather than a hard line, so what crosses
-/// a row is a brightening rather than a switch — at 60fps a hard edge stepping
-/// down the screen reads as a tear.
-class _RevealDown extends StatelessWidget {
- const _RevealDown({required this.listenable, required this.child});
-
- final Animation<double> listenable;
- final Widget child;
+/// Everything the widget tree this replaced did, as drawing — each piece keeps
+/// the note that explained it there.
+class _SkeletonPainter extends CustomPainter {
+ _SkeletonPainter({
+ required this.breath,
+ required this.reveal,
+ required this.lineHeight,
+ required this.bar,
+ required this.ground,
+ required this.rest,
+ required this.peak,
+ required this.banner,
+ }) : super(repaint: Listenable.merge([breath, reveal]));
+
+ /// 0 at rest, 1 at the peak of the breath.
+ final Animation<double> breath;
+
+ /// The sweep's progress, 0→1; where the edge is for a value is worked out in
+ /// [_revealFrom].
+ final Animation<double> reveal;
+
+ final double lineHeight;
+
+ /// A bar's thickness — the height of a line of text, roughly.
+ final double bar;
+
+ final Color ground;
+ final Color rest;
+ final Color peak;
+ final Color banner;
+
+ /// ⚠️ The terminal view's OWN padding (`TerminalPanel` passes
+ /// `EdgeInsets.all(10)` to the xterm view), so a bar starts on the column the
+ /// first character will occupy. A different gutter here would shift every
+ /// line sideways the moment the keyframe lands.
+ static const _padding = 10.0;
/// The soft edge's depth, as a fraction of the pane.
///
@@ -1754,45 +1719,219 @@ class _RevealDown extends StatelessWidget {
static const _feather = 0.10;
@override
- Widget build(BuildContext context) => AnimatedBuilder(
- animation: listenable,
- // The subtree does not depend on `t`, so it is built once and reused across
- // every frame of the sweep — the mask is the only thing that changes.
- child: child,
- builder: (context, built) {
- // ⚠️ **Linear, and that is the point for a sweep this long.** Every eased
- // curve front-loads the travel: `easeOutQuad` has the edge 84% of the way
- // down at the halfway mark, so over a 2.6-second reveal the last row would
- // take more than a second to gain its final sliver — a sweep that visibly
- // stalls just before it arrives, which is the thing this pacing exists to
- // avoid. A constant rate reads as steady progress, which is what it is
- // standing in for.
- final t = listenable.value;
- // Kept even though the controller stops short of 1 (see `_sweepExtent`),
- // because this widget must also be correct for a caller that runs it to
- // completion — and a mask that covers nothing is pure cost per frame.
- if (t >= 1) return built!;
- // Travels from just above the top edge towards just past the bottom, so
- // the first row is fully revealed rather than starting at half brightness.
- // It does not arrive: the controller is bounded below the value that would
- // take it there.
- final edge = t * (1 + _feather * 2) - _feather;
- return ShaderMask(
- blendMode: BlendMode.dstIn,
- shaderCallback: (bounds) => LinearGradient(
- begin: Alignment.topCenter,
- end: Alignment.bottomCenter,
- colors: const [Colors.white, Colors.white, Colors.transparent],
- // Clamped because a stop list must be non-decreasing and within 0..1;
- // at the extremes of the travel above, both terms run outside it.
- stops: [0, (edge - _feather).clamp(0.0, 1.0), edge.clamp(0.0, 1.0)],
- ).createShader(bounds),
- child: built,
+ void paint(Canvas canvas, Size size) {
+ final full = Offset.zero & size;
+ // Opaque, because this covers a live [TerminalPanel] — see the note at the
+ // call site. Without it the empty emulator buffer shows between the bars
+ // and the two grounds differ by a hair, which reads as a smudge.
+ canvas.drawRect(full, Paint()..color = ground);
+ final inner = full.deflate(_padding);
+ if (inner.width <= 0 || inner.height <= 0) return;
+ // ⚠️ **As many turns as the pane is tall, not a fixed four.** A fixed list
+ // left the top half of a tall phone empty, which reads as a page that has
+ // finished loading and has almost nothing on it. One extra turn is added
+ // beyond the measured fit so the topmost one is genuinely cut by the edge
+ // rather than ending a few pixels short of it.
+ final shapes = _AttachingState._shapes;
+ final perTurn = _AttachingState._turnHeight(
+ shapes[0],
+ lineHeight: lineHeight,
+ );
+ final count = perTurn > 0
+ ? (inner.height / perTurn).ceil() + 1
+ : shapes.length;
+ final turns = [
+ for (var i = 0; i < count; i++)
+ // Cycled from the back, so the LAST shape is always the one at the
+ // bottom edge: it is the only one written without a closing meta line,
+ // which is what an in-progress turn looks like.
+ shapes[shapes.length - 1 - i % shapes.length],
+ ].reversed.toList();
+ // ⚠️ Aligned to the BOTTOM and clipped at the top, which is how a terminal
+ // itself sits: the newest output is at the bottom edge and history runs off
+ // the top. The extra turn above guarantees the column exceeds the pane; the
+ // clip cuts what runs past.
+ var total = 0.0;
+ for (final turn in turns) {
+ total += _AttachingState._turnHeight(turn, lineHeight: lineHeight);
+ }
+ // The newest turn ends at the bottom edge, as the live screen's does — no
+ // gap after it. See `last` in [_turn].
+ total -= lineHeight * 0.9;
+ final fill = Color.lerp(rest, peak, breath.value)!;
+ canvas.save();
+ canvas.clipRect(full);
+ var top = inner.bottom - total;
+ for (var i = 0; i < turns.length; i++) {
+ // Oldest at 0.4, newest at full: the block nearest the top edge is the one
+ // about to be clipped by it, so it fades INTO that edge rather than ending
+ // against it. Blended against the ground rather than composited through
+ // an opacity layer — the ground is opaque, so the picture is the same.
+ final opacity = turns.length == 1
+ ? 1.0
+ : 0.4 + (i / (turns.length - 1)) * 0.6;
+ top = _turn(
+ canvas,
+ turns[i],
+ inner: inner,
+ top: top,
+ fill: Color.lerp(ground, fill, opacity)!,
+ banner: Color.lerp(ground, banner, opacity)!,
+ last: i == turns.length - 1,
);
- },
- );
+ }
+ _revealFrom(canvas, full);
+ canvas.restore();
+ }
+
+ /// One turn: the prompt banner, the bulleted answer under it, the meta line —
+ /// drawn from [top] down, answering with where the next turn starts.
+ ///
+ /// Every row is exactly one terminal line box tall, so the whole block
+ /// occupies a whole number of rows and the keyframe replaces it without the
+ /// page growing or shrinking by a fraction of a line.
+ ///
+ /// ⚠️ **Must stay in step with [_AttachingState._turnHeight]**: the terms
+ /// there are written in the order of the rows here.
+ double _turn(
+ Canvas canvas,
+ _SkeletonTurn turn, {
+ required Rect inner,
+ required double top,
+ required Color fill,
+ required Color banner,
+ required bool last,
+ }) {
+ final x = inner.left;
+ final width = inner.width;
+ final paint = Paint()..color = fill;
+ var y = top;
+ // The prompt, on its own full-width ground — the one element that makes
+ // this read as a transcript rather than as a list of lines.
+ canvas.drawRect(
+ Rect.fromLTWH(x, y, width, lineHeight),
+ Paint()..color = banner,
+ );
+ // The `›` gutter the real prompt keeps.
+ final gutter = bar * 0.9;
+ _bar(canvas, x + gutter, y, turn.prompt * (width - gutter), paint);
+ y += lineHeight + lineHeight * 0.35;
+ // The answer: a bullet on the first row, the rest indented under it.
+ for (var i = 0; i < turn.answer.length; i++) {
+ if (i == 0) {
+ final dot = bar * 0.55;
+ canvas.drawCircle(
+ Offset(x + dot / 2, y + lineHeight / 2),
+ dot / 2,
+ paint,
+ );
+ final indent = dot + bar * 0.7;
+ _bar(canvas, x + indent, y, turn.answer[i] * (width - indent), paint);
+ } else {
+ final indent = bar * 1.25;
+ _bar(canvas, x + indent, y, turn.answer[i] * (width - indent), paint);
+ }
+ y += lineHeight;
+ }
+ final meta = turn.meta;
+ if (meta != null) {
+ y += lineHeight * 0.35;
+ // The `✳ Crunched for 3s · done 10:36` line that closes a turn: always
+ // shorter, and thinner than a line of body text.
+ _bar(canvas, x, y, meta * width, paint, height: bar * 0.7);
+ y += lineHeight;
+ }
+ if (!last) y += lineHeight * 0.9;
+ return y;
+ }
+
+ /// A bar of text, centred in the line box that starts at [top].
+ void _bar(
+ Canvas canvas,
+ double left,
+ double top,
+ double width,
+ Paint paint, {
+ double? height,
+ }) {
+ final thickness = height ?? bar;
+ canvas.drawRRect(
+ RRect.fromRectAndRadius(
+ Rect.fromLTWH(
+ left,
+ top + (lineHeight - thickness) / 2,
+ width,
+ thickness,
+ ),
+ const Radius.circular(3),
+ ),
+ paint,
+ );
+ }
+
+ /// Wipes the block in from the top edge to the bottom over [reveal]'s 0→1.
+ ///
+ /// ⚠️ **A wipe, not a fade or a slide.** The skeleton stands in for a terminal
+ /// whose rows must not move — every bar is already at the pixel its text will
+ /// occupy — so the reveal cannot translate anything. A plain fade would have
+ /// the whole block appear at once, which says nothing about the direction
+ /// output arrives from. Covering by height instead lets the rows arrive the
+ /// way a screen paints: from the top, downward, each one landing where it
+ /// will stay.
+ ///
+ /// The leading edge is a short gradient rather than a hard line, so what
+ /// crosses a row is a brightening rather than a switch — at 60fps a hard edge
+ /// stepping down the screen reads as a tear.
+ ///
+ /// ⚠️ **Linear, and that is the point for a sweep this long.** Every eased
+ /// curve front-loads the travel: `easeOutQuad` has the edge 84% of the way
+ /// down at the halfway mark, so over a 2.6-second reveal the last row would
+ /// take more than a second to gain its final sliver — a sweep that visibly
+ /// stalls just before it arrives. A constant rate reads as steady progress,
+ /// which is what it is standing in for.
+ void _revealFrom(Canvas canvas, Rect full) {
+ final t = reveal.value;
+ // Kept even though the controller stops short of 1 (see
+ // [_AttachingState._sweepExtent]): a cover that hides nothing is pure cost
+ // per frame.
+ if (t >= 1) return;
+ // Travels from just above the top edge towards just past the bottom, so the
+ // first row is fully revealed rather than starting at half brightness. It
+ // does not arrive: the controller is bounded below the value that would
+ // take it there.
+ final edge = t * (1 + _feather * 2) - _feather;
+ // Ground laid OVER the bars from the edge down — the same picture the
+ // `dstIn` mask this replaced gave, without the offscreen layer a mask needs:
+ // the ground is opaque, so covering is as good as cutting.
+ final cover = Paint()
+ ..shader = LinearGradient(
+ begin: Alignment.topCenter,
+ end: Alignment.bottomCenter,
+ colors: [
+ ground.withValues(alpha: 0),
+ ground.withValues(alpha: 0),
+ ground,
+ ],
+ // Clamped because a stop list must be non-decreasing and within 0..1;
+ // at the extremes of the travel above, both terms run outside it.
+ stops: [0, (edge - _feather).clamp(0.0, 1.0), edge.clamp(0.0, 1.0)],
+ ).createShader(full);
+ canvas.drawRect(full, cover);
+ }
+
+ @override
+ bool shouldRepaint(_SkeletonPainter old) =>
+ old.lineHeight != lineHeight ||
+ old.bar != bar ||
+ old.ground != ground ||
+ old.rest != rest ||
+ old.peak != peak ||
+ old.banner != banner ||
+ !identical(old.breath, breath) ||
+ !identical(old.reveal, reveal);
}
+/// The shape of one transcript turn, for [_Attaching].
class _SkeletonTurn {
const _SkeletonTurn({
required this.prompt,
diff --git a/mobile/lib/state/app_state.dart b/mobile/lib/state/app_state.dart
index 16297e585..ed0e8e9e2 100644
--- a/mobile/lib/state/app_state.dart
+++ b/mobile/lib/state/app_state.dart
@@ -5117,6 +5117,14 @@ class AppNotifier extends ChangeNotifier {
selectedMachineId = machineId;
machineStates[machineId]?.activeAgentId = agentId;
focusPane(existing.id);
+ // A tile the pager opened ahead of time is being looked at now, so it
+ // joins the saved layout like any tile a person chose. Left warm, a
+ // relaunch would restore the tile before it AND reopen this agent from
+ // `lastOpenedAgent` — two streams for one screen. See [TerminalPane.warm].
+ if (existing.warm) {
+ existing.warm = false;
+ _persistLayout();
+ }
final terminal = existing.session;
if (terminal == null) {
// The pane wanted this agent before `_attachSession` could actually attach it (the agent's
@@ -5136,6 +5144,42 @@ class AppNotifier extends ChangeNotifier {
await addAgentToSwarm(machineId, agentId);
}
+ /// Opens [agentId]'s stream for a tile nobody is looking at yet — the phone's
+ /// pager attaching the agents either side of the one on screen, so the next
+ /// swipe lands on output instead of on "Attaching…".
+ ///
+ /// Everything [selectAgent] does BESIDES attaching is deliberately left out:
+ /// no focus, no `selectedMachineId`, no `activeAgentId`, no announcement to
+ /// the daemon, no layout write. The tile is a guess about where the thumb
+ /// goes next, and none of those should move for a guess. [TerminalPane.warm]
+ /// is the one thing that marks it, and [selectAgent] is what happens when the
+ /// guess comes true.
+ ///
+ /// A tile already there is left alone — except one holding a dead stream,
+ /// which is reopened the way `_attachPendingPanes` would. A stream someone
+ /// else took over is NOT reopened from here: that is the tug-of-war
+ /// `_paneNeedsAttach` exists to avoid, and a page nobody is looking at has no
+ /// business starting it.
+ Future<void> warmAgentPane(String machineId, String agentId) async {
+ if (_disposed) return;
+ final existing = paneOfAgent(machineId, agentId);
+ if (existing != null) {
+ if (_paneNeedsAttach(existing)) await _reattachPane(existing);
+ return;
+ }
+ if (!canAddPane || !_canAttachAgent(machineId, agentId)) return;
+ final pane = TerminalPane(
+ id: _nextPaneId++,
+ machineId: machineId,
+ agentId: agentId,
+ )..warm = true;
+ panes.add(pane);
+ // Told, so the parked page for this agent builds its panel — which is what
+ // measures the viewport `_attachSession` is about to wait for.
+ notifyListeners();
+ await _attachSession(pane);
+ }
+
/// Show a MACHINE in the grid, for the states that belong to the machine
/// rather than to any agent on it.
///
@@ -5718,7 +5762,9 @@ class AppNotifier extends ChangeNotifier {
Future<void> closePane(int paneId, {bool persist = true}) async {
final pane = panes.where((p) => p.id == paneId).firstOrNull;
if (pane == null) return;
- if (pane.agentId != null) {
+ // A warm tile was never something the person had open — see
+ // [TerminalPane.warm] — so closing it is not something to offer undoing.
+ if (pane.agentId != null && !pane.warm) {
final machine = stateOf(pane.machineId);
final agent = machine?.agents
.where((a) => a.id == pane.agentId)
diff --git a/mobile/lib/state/swarm.dart b/mobile/lib/state/swarm.dart
index 13d536162..b47a838ef 100644
--- a/mobile/lib/state/swarm.dart
+++ b/mobile/lib/state/swarm.dart
@@ -68,8 +68,11 @@ class Swarm {
}
Map<String, Object?> toJson() {
+ // Warm tiles are the pager's guesses, not the person's layout — see
+ // [TerminalPane.warm]. Saved, a relaunch would open every one of them as a
+ // stream before the agent the person was actually reading.
final agents = panes
- .where((p) => p.agentId != null)
+ .where((p) => p.agentId != null && !p.warm)
.toList(growable: false);
return {
'id': id,
diff --git a/mobile/lib/state/terminal_pane.dart b/mobile/lib/state/terminal_pane.dart
index ceab4b648..014393e7f 100644
--- a/mobile/lib/state/terminal_pane.dart
+++ b/mobile/lib/state/terminal_pane.dart
@@ -73,6 +73,20 @@ class TerminalPane {
/// pane pays a network round trip per keystroke. Off by default, and remembered, so the choice
/// survives a restart the way the rest of the layout does.
bool composerVisible = false;
+
+ /// Whether this tile was opened AHEAD of anyone looking at it — the phone's
+ /// pager attaching the agents beside the one on screen, so a swipe lands on
+ /// output rather than on "Attaching…". See `AppNotifier.warmAgentPane`.
+ ///
+ /// ⚠️ **Not persisted, and that is the point of the flag.** A warm tile is a
+ /// guess about where the thumb goes next; restored at launch it would open
+ /// every guess as a stream before the one agent the person actually left on.
+ /// `Swarm.toJson` leaves it out, and `selectAgent` clears it the moment the
+ /// tile is looked at — from then on it is an ordinary tile.
+ ///
+ /// 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;
}
/// A tile as it survives a restart: intent only, never the session.
diff --git a/mobile/lib/widgets/terminal_panel.dart b/mobile/lib/widgets/terminal_panel.dart
index c799a7fc1..dc2e66faa 100644
--- a/mobile/lib/widgets/terminal_panel.dart
+++ b/mobile/lib/widgets/terminal_panel.dart
@@ -725,6 +725,34 @@ class _TerminalPanelState extends State<TerminalPanel>
}
}
+ /// The one thing a pane nobody is looking at contributes to its session: how
+ /// big it is, before the stream opens. True when there is nothing (more) to
+ /// do; false when the view has not laid out yet and this should be asked
+ /// again next frame.
+ ///
+ /// ⚠️ **Only until `streamId` is set.** The phone's pager mounts the pages
+ /// either side of the one on screen and attaches them ahead of a swipe (see
+ /// `AgentSwipeHost`); their `open(waitForViewportSize: true)` would otherwise
+ /// wait two seconds for a measurement that never came, fall back to 80×24,
+ /// and pay a resize and a second keyframe on arrival. Once the stream is open
+ /// a parked pane goes back to saying nothing: a resize from a page nobody is
+ /// looking at is a SIGWINCH and a full TUI redraw on the far machine, which
+ /// is what gating everything else on `visible` is for.
+ bool _reportInitialViewport() {
+ if (widget.session.streamId != null) return true;
+ final view = _laidOutTerminalView();
+ if (view == null) return false;
+ final renderTerminal = view.renderTerminal;
+ final cellSize = renderTerminal.cellSize;
+ final renderSize = renderTerminal.size;
+ if (cellSize.width <= 0 || cellSize.height <= 0) return false;
+ widget.session.reportViewport(
+ renderSize.width ~/ cellSize.width,
+ renderSize.height ~/ cellSize.height,
+ );
+ return true;
+ }
+
void _afterTerminalMounted({
bool clearSelection = false,
bool scrollToEnd = true,
@@ -739,7 +767,21 @@ class _TerminalPanelState extends State<TerminalPanel>
_laidOutTerminalView()?.scrollToBottom();
}
WidgetsBinding.instance.addPostFrameCallback((_) {
- if (!mounted || !widget.visible) return;
+ if (!mounted) return;
+ if (!widget.visible) {
+ // A parked pane does one thing, and only until its stream opens — see
+ // [_reportInitialViewport]. Retried like the visible path below: the
+ // view may not have laid out yet on the frame this was asked in.
+ if (!_reportInitialViewport() && retries > 0) {
+ _afterTerminalMounted(
+ clearSelection: clearSelection,
+ scrollToEnd: scrollToEnd,
+ claimFocus: claimFocus,
+ retries: retries - 1,
+ );
+ }
+ return;
+ }
if (clearSelection) _controller.clearSelection();
final view = _laidOutTerminalView();
if (view == null) {