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(mobile): redial a dropped P2P channel within seconds

tony460ebd2e5047
diff --git a/mobile/lib/p2p/terminal_p2p_plugin.dart b/mobile/lib/p2p/terminal_p2p_plugin.dart
index 18475f536..6c2de2da1 100644
--- a/mobile/lib/p2p/terminal_p2p_plugin.dart
+++ b/mobile/lib/p2p/terminal_p2p_plugin.dart
@@ -28,6 +28,25 @@ const _retryDelay = Duration(seconds: 60);
const _retryWindow = Duration(hours: 1);
const _retryHourlyCap = 10;
+// ⚠️ **Deliberately not the CLI's rule (owner, 2026-10-14).** A channel that was carrying terminals
+// and went away is dialled again within seconds, then at the minute as before. Its going away is
+// rarely the network being unreachable, which is what the minute is for: on a Galaxy S23 the
+// channel closed within a second of its streams moving onto it (2026-10-14), and everything typed
+// for the next minute crossed the relay, a ~200ms round trip from the machine to the backend, so
+// every keystroke showed about 0.4s late. Each redial still spends the hourly budget.
+const _redialDelays = [
+ Duration(seconds: 1),
+ Duration(seconds: 5),
+ Duration(seconds: 15),
+];
+
+// How long a channel has to stay up before its dropping starts the redials over from the first:
+// one that keeps closing right after it opens is dialled three times quickly, not every second.
+const _redialStableAfter = Duration(minutes: 2);
+
+// How often, at most, typing over the relay brings a waiting retry forward (`_kickForTyping`).
+const _typingKickEvery = Duration(seconds: 10);
+
// How long a stream may sit mid-migration before the sweep gives up on it and the
// ordinary demote-on-mismatch rule applies to it again.
const _migrationTtl = Duration(seconds: 30);
@@ -148,6 +167,22 @@ class TerminalP2pPlugin implements TerminalTransportPlugin {
final _retryTimestamps = <DateTime>[];
Timer? _retryTimer;
+
+ /// Whether the waiting [_retryTimer] is the budget's deferral rather than a retry it paid for —
+ /// typing does not bring that one forward ([_kickForTyping]).
+ bool _retryDeferred = false;
+
+ /// When the live link opened, until it goes away — what a drop is measured against
+ /// ([_redialStableAfter]).
+ DateTime? _linkOpenedAt;
+
+ /// A channel that was up went away, and none has come up since: retries take [_redialDelays].
+ bool _redialing = false;
+
+ /// How many of [_redialDelays] have been used since a channel last stayed up.
+ int _redials = 0;
+
+ DateTime? _lastTypingKickAt;
late final Timer _sweep;
int _upgradeAttempts = 0;
@@ -260,6 +295,7 @@ class TerminalP2pPlugin implements TerminalTransportPlugin {
if (timer != null) {
timer.cancel();
_retryTimer = null;
+ _retryDeferred = false;
_log('retry kicked · app resumed');
if (_link == null) _startP2p();
return;
@@ -274,6 +310,33 @@ class TerminalP2pPlugin implements TerminalTransportPlugin {
}
}
+ /// Somebody is typing into a terminal the relay carries while a retry waits out its delay, after a
+ /// channel that was up went away: the retry runs now, as a resume's does ([kickRetry]). A round
+ /// trip to the backend on every keystroke is felt the moment somebody types, and what is typed
+ /// after a channel came back rides it.
+ ///
+ /// Only after a drop ([_redialing]): where no channel has come up, the network is the likelier
+ /// reason, and typing would spend the hourly budget in minutes on negotiations bound to fail. Not
+ /// more than once per [_typingKickEvery], each a full negotiation; never while one is under way,
+ /// and never for the budget's deferral, which stays the budget's.
+ void _kickForTyping() {
+ if (_disposed ||
+ !_redialing ||
+ _retryTimer == null ||
+ _retryDeferred ||
+ _link != null) {
+ return;
+ }
+ final now = _now();
+ final last = _lastTypingKickAt;
+ if (last != null && now.difference(last) < _typingKickEvery) return;
+ _lastTypingKickAt = now;
+ _retryTimer!.cancel();
+ _retryTimer = null;
+ _log('retry kicked · typing on the relay');
+ _startP2p();
+ }
+
// ── Negotiation ───────────────────────────────────────────────────────────
void _startP2p() {
@@ -292,6 +355,8 @@ class TerminalP2pPlugin implements TerminalTransportPlugin {
switch (state) {
case TerminalP2pLinkState.open:
wasDirect = true;
+ _linkOpenedAt = _now();
+ _redialing = false;
final relayed = link.transport == TerminalP2pTransport.relay;
_log(
'connected · via=${relayed ? 'turn' : 'direct'}'
@@ -349,7 +414,8 @@ class TerminalP2pPlugin implements TerminalTransportPlugin {
final windowStart = now.subtract(_retryWindow);
_retryTimestamps.removeWhere((at) => !at.isAfter(windowStart));
var delay = _retryDelay;
- if (_retryTimestamps.length >= _retryHourlyCap) {
+ _retryDeferred = _retryTimestamps.length >= _retryHourlyCap;
+ if (_retryDeferred) {
// The CLI stops here and leaves the streams on the relay until the socket
// itself redials. The window is rolling, though, and nothing else on a phone
// would ever ask again — so wait for the oldest attempt to age out instead.
@@ -361,14 +427,19 @@ class TerminalP2pPlugin implements TerminalTransportPlugin {
' last hour) — deferred ${delay.inSeconds}s until it frees',
);
} else {
+ // A channel that was up went away: its first retries come within seconds ([_redialDelays]).
+ final redial = _redialing && _redials < _redialDelays.length;
+ if (redial) delay = _redialDelays[_redials++];
_retryTimestamps.add(now);
_log(
'retry scheduled · in ${delay.inSeconds}s'
+ '${redial ? ' (redial $_redials/${_redialDelays.length})' : ''}'
' (${_retryTimestamps.length}/$_retryHourlyCap this hour)',
);
}
_retryTimer = Timer(delay, () {
_retryTimer = null;
+ _retryDeferred = false;
if (!_disposed && _link == null) _startP2p();
});
}
@@ -656,7 +727,14 @@ class TerminalP2pPlugin implements TerminalTransportPlugin {
Future<bool> sendBinary(Uint8List localFrame, Uint8List sealedFrame) async {
if (_disposed) return false;
final header = peekTerminalLocal(localFrame);
- if (header == null || !_p2pStreams.contains(header.streamId)) return false;
+ if (header == null) return false;
+ if (!_p2pStreams.contains(header.streamId)) {
+ if (header.kind == TerminalBinaryKind.input ||
+ header.kind == TerminalBinaryKind.paste) {
+ _kickForTyping();
+ }
+ return false;
+ }
final link = _link;
if (link == null) return false;
// A burst of large frames (a chunked upload) can push the send buffer over the
@@ -715,6 +793,14 @@ class TerminalP2pPlugin implements TerminalTransportPlugin {
}
if (streamIds.isNotEmpty) _reportP2pResult('dropped', reason: reason);
unawaited(link?.stop(reason: reason));
+ // A channel that was up is dialled again within seconds ([_redialDelays]), from the first delay
+ // again once it had stayed up a while.
+ final openedAt = _linkOpenedAt;
+ _linkOpenedAt = null;
+ if (openedAt != null) {
+ if (_now().difference(openedAt) >= _redialStableAfter) _redials = 0;
+ _redialing = true;
+ }
_scheduleRetry();
}
diff --git a/mobile/test/p2p/terminal_p2p_redial_test.dart b/mobile/test/p2p/terminal_p2p_redial_test.dart
new file mode 100644
index 000000000..19a1a48cb
--- /dev/null
+++ b/mobile/test/p2p/terminal_p2p_redial_test.dart
@@ -0,0 +1,254 @@
+import 'dart:convert';
+
+import 'package:fake_async/fake_async.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness_mobile/p2p/terminal_p2p_plugin.dart';
+import 'package:harness_mobile/terminal/terminal_binary.dart';
+
+import 'fakes.dart';
+
+void flush(FakeAsync async) => async.flushMicrotasks();
+
+Map<String, dynamic> ready(String streamId, String requestId) => {
+ 'type': 'terminal_ready',
+ 'payload': {'streamId': streamId, 'requestId': requestId, 'agentId': 'a'},
+};
+
+typedef Booted = ({
+ TerminalP2pPlugin plugin,
+ FakeHost host,
+ FakeLinkFactory links,
+});
+
+/// The first terminal starts the negotiation and rides the relay; nothing is up yet.
+Booted started(FakeAsync async) {
+ final b = bootPlugin(async: async);
+ b.host.sendTerminalFrame('terminal_open', {'requestId': 'r0'});
+ flush(async);
+ expect(b.links.created, hasLength(1));
+ return b;
+}
+
+/// The channel up, with [streamA] opened over it.
+Booted upWithStream(FakeAsync async) {
+ final b = started(async);
+ b.links.last.open();
+ flush(async);
+ b.host.sendTerminalFrame('terminal_open', {'requestId': 'r1'});
+ flush(async);
+ b.links.last.receive(jsonEncode(ready(streamA, 'r1')));
+ flush(async);
+ expect(b.plugin.linkModeFor(streamA), 'p2p');
+ return b;
+}
+
+/// The next link is dialled after exactly [delay], and not a moment before.
+void expectDialledAfter(FakeAsync async, Booted b, Duration delay) {
+ final before = b.links.created.length;
+ async.elapse(delay - const Duration(milliseconds: 1));
+ expect(b.links.created, hasLength(before), reason: 'not before $delay');
+ async.elapse(const Duration(milliseconds: 1));
+ expect(b.links.created, hasLength(before + 1), reason: 'at $delay');
+}
+
+/// A key typed into [streamId], as the session puts it on the wire.
+void type(FakeAsync async, Booted b, {String streamId = streamA}) {
+ b.host.sendTerminalBinary(htrl(TerminalBinaryKind.input, streamId));
+ flush(async);
+}
+
+/// A channel that was carrying terminals and went away is dialled again within seconds, not a
+/// minute later, and typing on the relay meanwhile brings the wait forward: on a Galaxy S23 the
+/// channel closed within a second of its streams moving onto it (2026-10-14), and everything typed
+/// for the next minute crossed the relay, every keystroke about 0.4s late.
+///
+/// Negotiation starts with the connection's first terminal (`prepareOpen`), so every plugin here is
+/// started by an open, as the app starts it.
+void main() {
+ group('a channel that went away', () {
+ test('is dialled again at 1s, 5s and 15s, then at the minute', () {
+ fakeAsync((async) {
+ final b = upWithStream(async);
+ b.links.last.drop('channel_closed');
+ flush(async);
+ expect(b.plugin.linkModeFor(streamA), 'relay');
+ expectDialledAfter(async, b, const Duration(seconds: 1));
+ // Each comes up and goes away again at once, as the S23's did.
+ for (final delay in const [
+ Duration(seconds: 5),
+ Duration(seconds: 15),
+ Duration(seconds: 60),
+ ]) {
+ b.links.last.open();
+ flush(async);
+ b.links.last.drop('channel_closed');
+ flush(async);
+ expectDialledAfter(async, b, delay);
+ }
+ b.plugin.dispose();
+ });
+ });
+
+ test('keeps the quick pace through redials that fail to connect', () {
+ fakeAsync((async) {
+ final b = upWithStream(async);
+ b.links.last.drop('peer_failed');
+ flush(async);
+ expectDialledAfter(async, b, const Duration(seconds: 1));
+ for (final delay in const [
+ Duration(seconds: 5),
+ Duration(seconds: 15),
+ Duration(seconds: 60),
+ ]) {
+ b.links.last.failNegotiation('negotiation_timeout');
+ flush(async);
+ expectDialledAfter(async, b, delay);
+ }
+ b.plugin.dispose();
+ });
+ });
+
+ test(
+ 'starts its redials over once a channel had stayed up two minutes',
+ () {
+ fakeAsync((async) {
+ final b = upWithStream(async);
+ b.links.last.drop('channel_closed');
+ flush(async);
+ expectDialledAfter(async, b, const Duration(seconds: 1));
+ b.links.last.open();
+ flush(async);
+ async.elapse(const Duration(minutes: 2));
+ b.links.last.drop('channel_closed');
+ flush(async);
+ // From the first again, not the second.
+ expectDialledAfter(async, b, const Duration(seconds: 1));
+ b.plugin.dispose();
+ });
+ },
+ );
+
+ test(
+ 'still spends the hourly budget, and waits for it once it is spent',
+ () {
+ fakeAsync((async) {
+ final b = upWithStream(async);
+ b.links.last.drop('channel_closed');
+ flush(async);
+ // Ten attempts within the hour: the drop's, then nine that never connect.
+ for (var attempt = 0; attempt < 9; attempt++) {
+ async.elapse(const Duration(minutes: 1));
+ b.links.last.failNegotiation('peer_failed');
+ flush(async);
+ }
+ async.elapse(const Duration(minutes: 1));
+ expect(b.links.created, hasLength(11));
+ b.links.last.failNegotiation('peer_failed');
+ flush(async);
+ async.elapse(const Duration(minutes: 30));
+ expect(b.links.created, hasLength(11), reason: 'budget spent');
+ b.plugin.dispose();
+ });
+ },
+ );
+ });
+
+ test('a negotiation that never connected still waits the minute', () {
+ fakeAsync((async) {
+ final b = started(async);
+ b.links.last.failNegotiation('negotiation_timeout');
+ flush(async);
+ expectDialledAfter(async, b, const Duration(seconds: 60));
+ b.plugin.dispose();
+ });
+ });
+
+ group('typing on the relay', () {
+ test('after a drop brings the waiting retry forward, once per 10s', () {
+ fakeAsync((async) {
+ final b = upWithStream(async);
+ b.links.last.drop('channel_closed');
+ flush(async);
+ // The 1s redial is waiting: a key typed now does not wait for it.
+ type(async, b);
+ expect(b.links.created, hasLength(2));
+ expect(
+ b.host.wsBinary,
+ hasLength(1),
+ reason: 'the key itself still goes, over the relay',
+ );
+ b.links.last.failNegotiation('negotiation_timeout');
+ flush(async);
+ // The next (5s) is waiting; another key within the 10s brings nothing forward.
+ async.elapse(const Duration(seconds: 2));
+ type(async, b);
+ expect(b.links.created, hasLength(2));
+ expectDialledAfter(async, b, const Duration(seconds: 3));
+ b.links.last.failNegotiation('negotiation_timeout');
+ flush(async);
+ // Ten seconds after the last kick, the 15s one is brought forward.
+ async.elapse(const Duration(seconds: 5));
+ type(async, b);
+ expect(b.links.created, hasLength(4));
+ b.plugin.dispose();
+ });
+ });
+
+ test('a paste brings it forward too', () {
+ fakeAsync((async) {
+ final b = upWithStream(async);
+ b.links.last.drop('channel_closed');
+ flush(async);
+ b.host.sendTerminalBinary(htrl(TerminalBinaryKind.paste, streamA));
+ flush(async);
+ expect(b.links.created, hasLength(2));
+ b.plugin.dispose();
+ });
+ });
+
+ test('brings nothing forward where no channel has come up', () {
+ fakeAsync((async) {
+ final b = started(async);
+ b.links.last.failNegotiation('peer_failed');
+ flush(async);
+ type(async, b);
+ expect(b.links.created, hasLength(1));
+ expectDialledAfter(async, b, const Duration(seconds: 60));
+ b.plugin.dispose();
+ });
+ });
+
+ test('brings nothing forward while a negotiation is under way', () {
+ fakeAsync((async) {
+ final b = upWithStream(async);
+ b.links.last.drop('channel_closed');
+ flush(async);
+ async.elapse(const Duration(seconds: 1));
+ expect(b.links.created, hasLength(2));
+ type(async, b);
+ expect(b.links.created, hasLength(2));
+ b.plugin.dispose();
+ });
+ });
+
+ test('leaves the budget\'s deferral to the budget', () {
+ fakeAsync((async) {
+ final b = upWithStream(async);
+ b.links.last.drop('channel_closed');
+ flush(async);
+ for (var attempt = 0; attempt < 9; attempt++) {
+ async.elapse(const Duration(minutes: 1));
+ b.links.last.failNegotiation('peer_failed');
+ flush(async);
+ }
+ async.elapse(const Duration(minutes: 1));
+ b.links.last.failNegotiation('peer_failed');
+ flush(async);
+ expect(b.links.created, hasLength(11));
+ type(async, b);
+ expect(b.links.created, hasLength(11));
+ b.plugin.dispose();
+ });
+ });
+ });
+}