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.

Web: Activity, Threads, Follow, per-chat notifications and Restore reach the backend (#2482)

0xken15a0ab584dc53f
diff --git a/desktop/lib/super_tabs/backend_transport.dart b/desktop/lib/super_tabs/backend_transport.dart
index fc49c8f91..b31f044c4 100644
--- a/desktop/lib/super_tabs/backend_transport.dart
+++ b/desktop/lib/super_tabs/backend_transport.dart
@@ -12,14 +12,16 @@ import 'controller.dart';
/// call (hosts, starts, GitHub tokens, deliveries, terminals) are refused
/// before anything is sent, so this transport never signs them.
const _tab =
- r'(?!(?:hosts|workspaces|grids|list|invitations|routing-policy|workspace-invitations)/)[A-Za-z0-9_-]+';
+ r'(?!(?:hosts|workspaces|grids|list|invitations|deleted|routing-policy|workspace-invitations)/)[A-Za-z0-9_-]+';
const _id = r'[A-Za-z0-9_-]+';
final _swarmRoute = RegExp(
'^/api/super-tabs(?:'
- '|/list|/invitations'
+ '|/list|/invitations|/deleted'
'|/$_tab/(?:parts|changes|commands|mentions|decisions|leaderboard|sharing'
'|routing-policy|groups|groups/$_id|join|join/preview|native'
- '|conversations/$_id/(?:messages|catch-up|read|threads/$_id/read'
+ '|activity|activity/read|threads|inbox'
+ '|conversations/$_id/(?:messages|catch-up|read|notifications'
+ '|threads/$_id/(?:read|follow)'
'|messages/$_id/attachments/$_id)'
'|projects/$_id/(?:github|github/approve'
'|instructions(?:/revisions(?:/[0-9]+)?)?)'
diff --git a/desktop/test/super_tab_backend_transport_test.dart b/desktop/test/super_tab_backend_transport_test.dart
index 0d0b67369..8095d935f 100644
--- a/desktop/test/super_tab_backend_transport_test.dart
+++ b/desktop/test/super_tab_backend_transport_test.dart
@@ -2,6 +2,7 @@
// browser asks the backend itself, with its own sign-in, instead of relaying
// through one of the person's machines. Run against a real local HTTP server,
// so the header, the body and the backend's envelope are the real ones.
+import 'dart:async';
import 'dart:convert';
import 'dart:io';
@@ -10,11 +11,15 @@ import 'package:harness/api/access_token_source.dart';
import 'package:harness/api/api_client.dart';
import 'package:harness/auth/auth_session.dart';
import 'package:harness/core/config.dart';
+import 'package:harness/super_tabs/activity.dart';
+import 'package:harness/super_tabs/attention.dart';
import 'package:harness/super_tabs/backend_transport.dart';
import 'package:harness/super_tabs/client.dart';
import 'package:harness/super_tabs/controller.dart';
import 'package:harness/super_tabs/model.dart';
+import 'super_tab_activity_test.dart' show reply;
+import 'super_tab_attention_test.dart' show projectServer, unreadThread;
import 'support/super_tab_fixture.dart';
import 'swarm_state_test.dart' show MemoryStore;
@@ -110,13 +115,13 @@ void main() {
});
tearDown(() => backend.server.close(force: true));
- Future<SuperTabFailure> refusal(Future<Object?> call) async {
+ Future<SuperTabFailure> refusal(Future<Object?> call, {String? what}) async {
try {
await call;
} on SuperTabFailure catch (failure) {
return failure;
}
- fail('expected a SuperTabFailure');
+ fail('expected a SuperTabFailure${what == null ? '' : ' for $what'}');
}
test(
@@ -260,8 +265,44 @@ void main() {
'GET',
'/api/super-tabs/tab1/conversations/c1/messages/m1/attachments/a1/x',
),
+ // Next to Activity, Threads, Follow, per-chat notifications and the
+ // deleted Swarms (#2434, #2453, #2457): nothing above, past or beside
+ // the exact routes, and never under a daemon's own prefix.
+ ('GET', '/api/super-tabs/hosts/activity'),
+ ('GET', '/api/super-tabs/hosts/m1/activity'),
+ ('POST', '/api/super-tabs/hosts/activity/read'),
+ ('GET', '/api/super-tabs/hosts/threads'),
+ ('GET', '/api/super-tabs/hosts/inbox'),
+ ('GET', '/api/super-tabs/hosts/m1/inbox'),
+ ('POST', '/api/super-tabs/hosts/conversations/c1/notifications'),
+ ('POST', '/api/super-tabs/hosts/conversations/c1/threads/m1/follow'),
+ ('GET', '/api/super-tabs/tab1/activity/x'),
+ ('POST', '/api/super-tabs/tab1/activity/read/x'),
+ ('GET', '/api/super-tabs/tab1/threads/m1'),
+ ('GET', '/api/super-tabs/tab1/inbox/x'),
+ ('POST', '/api/super-tabs/tab1/conversations/c1/notifications/x'),
+ ('POST', '/api/super-tabs/tab1/conversations/notifications'),
+ ('POST', '/api/super-tabs/tab1/notifications'),
+ ('POST', '/api/super-tabs/tab1/conversations/c1/threads/follow'),
+ ('POST', '/api/super-tabs/tab1/conversations/c1/threads/m1/follow/x'),
+ ('POST', '/api/super-tabs/tab1/conversations/c1/threads/m1/m2/follow'),
+ ('POST', '/api/super-tabs/tab1/conversations/c1/threads/../follow'),
+ ('POST', '/api/super-tabs/tab1/threads/m1/follow'),
+ ('GET', '/api/super-tabs/tab1/deleted'),
+ ('GET', '/api/super-tabs/deleted/x'),
+ ('GET', '/api/super-tabs/deleted/parts'),
+ ('GET', '/api/super-tabs/deleted/threads'),
+ ('GET', '/api/super-tabs/tab1/agents/a1/deliveries/d1'),
+ ('POST', '/api/super-tabs/tab1/agents/a1/deliveries/d1'),
+ ('POST', '/api/super-tabs/tab1/starts/s1'),
+ ('POST', '/api/super-tabs/hosts/m1/starts/tab1/s1'),
+ ('POST', '/api/super-tabs/hosts/m1/readings'),
+ ('GET', '/api/super-tabs/hosts/m1/assignments'),
]) {
- final failure = await refusal(transport(method, path));
+ final failure = await refusal(
+ transport(method, path),
+ what: '$method $path',
+ );
expect(failure.code, 'INVALID_REQUEST', reason: '$method $path');
}
expect(backend.requests, isEmpty);
@@ -303,6 +344,15 @@ void main() {
('POST', '/api/super-tabs/tab1/join'),
('GET', '/api/super-tabs/tab1/join/preview'),
('POST', '/api/super-tabs/tab1/native'),
+ // Activity, Threads, Follow, per-chat notifications (#2434, #2453) and
+ // the owner's deleted Swarms (#2457).
+ ('GET', '/api/super-tabs/tab1/activity'),
+ ('POST', '/api/super-tabs/tab1/activity/read'),
+ ('GET', '/api/super-tabs/tab1/threads'),
+ ('GET', '/api/super-tabs/tab1/inbox'),
+ ('POST', '/api/super-tabs/tab1/conversations/c1/threads/m1/follow'),
+ ('POST', '/api/super-tabs/tab1/conversations/c1/notifications'),
+ ('GET', '/api/super-tabs/deleted'),
];
for (final (method, path) in routes) {
await transport(method, path, body: method == 'POST' ? {} : null);
@@ -470,4 +520,571 @@ void main() {
'Bearer browser-token',
);
});
+
+ // Every Swarm path the app builds goes through this transport on the web,
+ // so a route added to the client and not here fails here, not in a browser
+ // (Activity, Threads, Follow, per-chat notifications and Restore said
+ // "Could not load" on the web for this, 11-01).
+ test(
+ 'every Swarm path the app\'s code builds is one the web may send',
+ () async {
+ final paths = swarmPathsInSource(
+ Directory('lib'),
+ // Uploads post a form straight through ApiClient, never this transport.
+ skip: const {
+ 'lib/super_tabs/attachment_upload.dart',
+ 'lib/super_tabs/avatar_upload.dart',
+ },
+ );
+ // The walk finds what it should, so a broken walk cannot pass.
+ expect(paths.length, greaterThan(40));
+ expect(
+ paths,
+ containsAll([
+ '/api/super-tabs/1/activity',
+ '/api/super-tabs/1/activity/read',
+ '/api/super-tabs/1/threads',
+ '/api/super-tabs/1/inbox',
+ '/api/super-tabs/1/conversations/1/threads/1/follow',
+ '/api/super-tabs/1/conversations/1/notifications',
+ '/api/super-tabs/deleted',
+ '/api/super-tabs/1/projects/1/instructions/revisions/1',
+ '/api/super-tabs/list?1',
+ '/api/super-tabs/1/projects/1/github?refresh=1',
+ ]),
+ );
+ final refused = <String>[];
+ for (final path in paths) {
+ try {
+ await transport('GET', path);
+ } on SuperTabFailure catch (failure) {
+ if (failure.code == 'INVALID_REQUEST') refused.add(path);
+ }
+ }
+ expect(refused, isEmpty, reason: 'refused before sending on the web');
+ for (final file in [
+ 'lib/super_tabs/attachment_upload.dart',
+ 'lib/super_tabs/avatar_upload.dart',
+ ]) {
+ final source = File(file).readAsStringSync();
+ expect(source, isNot(contains('request(')), reason: file);
+ expect(source, contains('postForm('), reason: file);
+ }
+ },
+ );
+
+ /// [server] behind the fake backend, answering as the backend does, and a
+ /// Swarm on the web read through this transport.
+ Future<SuperTabController> webSwarm(ConversationServer server) async {
+ backend.handle = (method, path, body) async {
+ try {
+ final data = await server.request(
+ method,
+ path,
+ body: body as Map<String, dynamic>?,
+ );
+ return (200, {'success': true, 'data': data});
+ } on SuperTabFailure catch (failure) {
+ return (
+ failure.statusCode == 0 ? 500 : failure.statusCode,
+ {
+ 'success': false,
+ 'error': {'code': failure.code, 'message': failure.message},
+ },
+ );
+ }
+ };
+ final c = SuperTabController(
+ tabId: 'tab',
+ accountId: 'owner',
+ request: (method, path, {body}) async =>
+ superTabMap(await transport(method, path, body: body)),
+ save: server.save,
+ pollInterval: Duration.zero,
+ );
+ addTearDown(() async {
+ await c.flush();
+ c.dispose();
+ });
+ await c.refresh();
+ return c;
+ }
+
+ group('on the web, through the backend', () {
+ test('Activity lists replies to the reader, and leaving it marks them '
+ 'read on the backend', () async {
+ final server = projectServer();
+ server.state['activityDigest'] = 'd1';
+ server.activity = {
+ 'items': [reply('r1')],
+ 'more': false,
+ };
+ final c = await webSwarm(server);
+ final activity = SuperTabActivity.of(c);
+ await settle();
+ expect(activity.error, isNull);
+ expect(activity.supported, isTrue);
+ expect(activity.items.map((i) => i.id), ['r1']);
+ activity.opened();
+ await settle();
+ activity.closed();
+ await settle();
+ expect(server.activityReadPosts, hasLength(1));
+ expect(
+ sent(),
+ containsAll([
+ 'GET /api/super-tabs/tab/activity',
+ 'POST /api/super-tabs/tab/activity/read',
+ ]),
+ );
+ });
+
+ test(
+ 'Activity that failed its first read recovers on the next change',
+ () async {
+ final server = projectServer();
+ server.state['activityDigest'] = 'd1';
+ server.activityStatus = 503;
+ server.activity = {
+ 'items': [reply('r1')],
+ 'more': false,
+ };
+ final c = await webSwarm(server);
+ final activity = SuperTabActivity.of(c);
+ await settle();
+ expect(activity.error, isNotNull);
+ expect(activity.items, isEmpty);
+ server.activityStatus = 0;
+ server.append('Something else changed');
+ await c.refresh();
+ await settle();
+ expect(activity.error, isNull);
+ expect(activity.items.map((i) => i.id), ['r1']);
+ },
+ );
+
+ test('Activity drops its rows when access goes, and an answer on its way '
+ 'shows nothing', () async {
+ final server = projectServer();
+ server.state['activityDigest'] = 'd1';
+ server.activity = {
+ 'items': [reply('r1')],
+ 'more': false,
+ };
+ final c = await webSwarm(server);
+ final activity = SuperTabActivity.of(c);
+ await settle();
+ expect(activity.items, hasLength(1));
+ server.holdActivity = Completer();
+ unawaited(activity.load());
+ await settle();
+ c.revokeAccess('Removed from this Swarm.');
+ expect(activity.items, isEmpty);
+ server.holdActivity!.complete();
+ await settle();
+ expect(activity.items, isEmpty);
+ expect(activity.count, 0);
+ });
+
+ test(
+ 'Threads lists followed threads, and Follow is kept on the backend',
+ () async {
+ final server = projectServer();
+ server.followedThreads = {
+ 'unread': [
+ unreadThread('message-1', 2),
+ // A chat this reader does not have: never shown on its word.
+ unreadThread('message-9', 3, conversationId: 'elsewhere'),
+ ],
+ 'read': [],
+ 'more': false,
+ };
+ final c = await webSwarm(server);
+ final attention = SuperTabAttention.of(c);
+ await attention.load();
+ expect(attention.error, isNull);
+ expect(attention.followSupported, isTrue);
+ expect(attention.unread.map((t) => t.rootId), ['message-1']);
+ expect(sent(), contains('GET /api/super-tabs/tab/threads'));
+ expect(sent(), isNot(contains('GET /api/super-tabs/tab/inbox')));
+ expect(
+ await attention.setFollow(attention.unread.single, false),
+ isTrue,
+ );
+ expect(server.followPosts, [
+ {'conversationId': 'lead', 'root': 'message-1', 'follow': false},
+ ]);
+ // Access gone: no Follow is sent at all. (A kept Follow reads the
+ // list again first.)
+ await settle();
+ final thread = attention.unread.single;
+ backend.requests.clear();
+ c.revokeAccess('Removed from this Swarm.');
+ expect(await attention.setFollow(thread, true), isFalse);
+ await settle();
+ expect(backend.requests, isEmpty);
+ },
+ );
+
+ test(
+ 'Threads on a backend before followed threads reads its inbox',
+ () async {
+ final server = projectServer();
+ server.followedThreads = null;
+ server.inbox = {
+ 'threads': [unreadThread('message-1', 2)],
+ 'latest': null,
+ 'more': false,
+ };
+ final c = await webSwarm(server);
+ final attention = SuperTabAttention.of(c);
+ await attention.load();
+ expect(attention.error, isNull);
+ expect(attention.followSupported, isFalse);
+ expect(attention.unread.map((t) => t.rootId), ['message-1']);
+ expect(sent(), contains('GET /api/super-tabs/tab/inbox'));
+ },
+ );
+
+ test('a chat\'s notifications are kept on the backend; a refused one goes '
+ 'back, and a chat this Swarm does not list sends nothing', () async {
+ final server = projectServer();
+ final c = await webSwarm(server);
+ expect(await c.setNotify('lead', SuperTabNotify.muted), isTrue);
+ expect(server.notifyPosts, [
+ {'conversationId': 'lead', 'notify': 'muted'},
+ ]);
+ expect(c.notifyOf('lead'), SuperTabNotify.muted);
+ server.failNotify = true;
+ expect(await c.setNotify('lead', SuperTabNotify.all), isFalse);
+ expect(c.notifyOf('lead'), SuperTabNotify.muted);
+ backend.requests.clear();
+ expect(
+ await c.setNotify('someone-else-buddy', SuperTabNotify.all),
+ isFalse,
+ );
+ expect(backend.requests, isEmpty);
+ });
+
+ test(
+ 'Restore reads the owner\'s deleted Swarms from the backend',
+ () async {
+ final deleted = {
+ 'swarms': [
+ {
+ 'id': 'tab9',
+ 'name': 'Old',
+ 'purgeAt': '2026-11-08T00:00:00.000Z',
+ },
+ ],
+ };
+ backend.answer = {'success': true, 'data': deleted};
+ expect(await transport('GET', '/api/super-tabs/deleted'), deleted);
+ expect(sent(), ['GET /api/super-tabs/deleted']);
+ expect(
+ backend.requests.single.headers['authorization'],
+ 'Bearer browser-token',
+ );
+ },
+ );
+ });
+}
+
+/// Every Swarm path the code under [root] builds, read from its source: each
+/// string that starts `/api/super-tabs`, joined with the literals beside it.
+/// A getter, function or local whose body is a literal (`_path`,
+/// `_messagesPath(id)`, `base`) is filled in from it, both sides of a
+/// `? :` are taken, and any other value reads `1`.
+Set<String> swarmPathsInSource(Directory root, {Set<String> skip = const {}}) {
+ final paths = <String>{};
+ for (final file in root.listSync(recursive: true).whereType<File>()) {
+ final name = file.path.replaceAll(r'\', '/');
+ if (!name.endsWith('.dart') || skip.contains(name)) continue;
+ final source = file.readAsStringSync();
+ final tokens = _DartStrings.scan(source);
+ final defs = <String, List<List<_Literal>>>{};
+ // Literals that only name a prefix (`_path`, `base`), interpolated into
+ // the paths this file sends: not a path on their own.
+ final prefixes = <int>{};
+ for (var i = 0; i + 1 < tokens.length; i++) {
+ final code = tokens[i];
+ if (code is! String || tokens[i + 1] is! _Literal) continue;
+ final def = RegExp(r'(\w+)\s*(?:\([^()]*\))?\s*(?:=>|=)\s*$')
+ .firstMatch(code);
+ if (def == null) continue;
+ final name = def.group(1)!;
+ (defs[name] ??= []).add(_DartStrings.adjacent(tokens, i + 1).$1);
+ if (RegExp('\\\$\\{?${RegExp.escape(name)}\\b').hasMatch(source)) {
+ prefixes.add(i + 1);
+ }
+ }
+ final expand = _Expander(defs);
+ for (var i = 0; i < tokens.length;) {
+ if (tokens[i] is! _Literal) {
+ i++;
+ continue;
+ }
+ final (group, end) = _DartStrings.adjacent(tokens, i);
+ final prefix = prefixes.contains(i);
+ i = end;
+ if (prefix) continue;
+ for (final text in expand.literals(group)) {
+ if (text.startsWith('/api/super-tabs')) paths.add(text);
+ }
+ }
+ }
+ return paths;
+}
+
+/// One string literal: its text pieces (String) and interpolations
+/// ([_Interpolation]).
+class _Literal {
+ _Literal(this.pieces);
+ final List<Object> pieces;
+}
+
+class _Interpolation {
+ _Interpolation(this.source);
+ final String source;
+}
+
+/// Splits Dart source into code (String) and string literals ([_Literal]),
+/// dropping comments.
+class _DartStrings {
+ _DartStrings(this.s);
+ final String s;
+ var i = 0;
+
+ static List<Object> scan(String source) => _DartStrings(source)._tokens();
+
+ /// The literals that start at [start] and sit side by side, only blank
+ /// code between them, and the index past the last.
+ static (List<_Literal>, int) adjacent(List<Object> tokens, int start) {
+ final group = <_Literal>[tokens[start] as _Literal];
+ var j = start + 1;
+ while (j < tokens.length) {
+ final token = tokens[j];
+ if (token is _Literal) {
+ group.add(token);
+ } else if (!(token is String &&
+ token.trim().isEmpty &&
+ j + 1 < tokens.length &&
+ tokens[j + 1] is _Literal)) {
+ break;
+ }
+ j++;
+ }
+ return (group, j);
+ }
+
+ List<Object> _tokens() {
+ final out = <Object>[];
+ final code = StringBuffer();
+ void flush() {
+ if (code.isNotEmpty) out.add(code.toString());
+ code.clear();
+ }
+
+ while (i < s.length) {
+ if (_comment()) continue;
+ final literal = _literal();
+ if (literal != null) {
+ flush();
+ out.add(literal);
+ continue;
+ }
+ code.write(s[i++]);
+ }
+ flush();
+ return out;
+ }
+
+ bool _comment() {
+ if (s.startsWith('//', i)) {
+ final end = s.indexOf('\n', i);
+ i = end < 0 ? s.length : end;
+ return true;
+ }
+ if (s.startsWith('/*', i)) {
+ final end = s.indexOf('*/', i + 2);
+ i = end < 0 ? s.length : end + 2;
+ return true;
+ }
+ return false;
+ }
+
+ _Literal? _literal() {
+ var j = i;
+ final raw = j < s.length && s[j] == 'r';
+ if (raw) j++;
+ if (j >= s.length || (s[j] != "'" && s[j] != '"')) return null;
+ // An identifier ending in r (`for'`) is not a raw string.
+ if (raw && i > 0 && RegExp(r'\w').hasMatch(s[i - 1])) return null;
+ final quote = s.startsWith("${s[j]}${s[j]}${s[j]}", j)
+ ? '${s[j]}${s[j]}${s[j]}'
+ : s[j];
+ i = j + quote.length;
+ final pieces = <Object>[];
+ final text = StringBuffer();
+ void flushText() {
+ if (text.isNotEmpty) pieces.add(text.toString());
+ text.clear();
+ }
+
+ while (i < s.length && !s.startsWith(quote, i)) {
+ final c = s[i];
+ if (!raw && c == r'\' && i + 1 < s.length) {
+ text.write(s[i + 1]);
+ i += 2;
+ } else if (!raw && c == r'$' && s.startsWith(r'${', i)) {
+ flushText();
+ final start = i + 2;
+ i = start;
+ var depth = 0;
+ while (i < s.length) {
+ if (_comment()) continue;
+ if (_literal() != null) continue;
+ if (s[i] == '{') depth++;
+ if (s[i] == '}') {
+ if (depth == 0) break;
+ depth--;
+ }
+ i++;
+ }
+ pieces.add(_Interpolation(s.substring(start, i)));
+ i++;
+ } else if (!raw && c == r'$') {
+ flushText();
+ final name = RegExp(r'[A-Za-z_]\w*').matchAsPrefix(s, i + 1);
+ pieces.add(_Interpolation(name?.group(0) ?? ''));
+ i = name?.end ?? i + 1;
+ } else {
+ text.write(c);
+ i++;
+ }
+ }
+ i += quote.length;
+ flushText();
+ return _Literal(pieces);
+ }
+}
+
+class _Expander {
+ _Expander(this.defs);
+ final Map<String, List<List<_Literal>>> defs;
+ var _depth = 0;
+
+ Set<String> literals(List<_Literal> group) {
+ var out = {''};
+ for (final literal in group) {
+ for (final piece in literal.pieces) {
+ final options = piece is String
+ ? {piece}
+ : expression((piece as _Interpolation).source);
+ out = {
+ for (final a in out)
+ // A value unknown here: an id, or right after a path's last
+ // segment a query string (`_query(id)`), or none.
+ for (final b
+ in options ??
+ (RegExp(r'[A-Za-z]$').hasMatch(a) ? {'', '?1'} : {'1'}))
+ a + b,
+ };
+ if (out.length > 256) out = out.take(256).toSet();
+ }
+ }
+ return out;
+ }
+
+ /// The values [source] can have, or null when they are not known here.
+ Set<String>? expression(String source) {
+ if (_depth > 8) return null;
+ _depth++;
+ try {
+ final tokens = _DartStrings.scan(source.trim());
+ final code = tokens.whereType<String>().join(' ');
+ if (tokens.isNotEmpty &&
+ tokens.every((t) => t is _Literal || (t as String).trim().isEmpty)) {
+ return literals(tokens.whereType<_Literal>().toList());
+ }
+ // cond ? a : b, both sides.
+ final question = _topLevel(tokens, '?');
+ if (question != null) {
+ final colon = _topLevel(tokens, ':', from: question);
+ if (colon != null) {
+ return {
+ ...(expression(_join(tokens, question, colon)) ?? {'1'}),
+ ...(expression(_join(tokens, colon, null)) ?? {'1'}),
+ };
+ }
+ }
+ final name = RegExp(r'^(\w+)\s*(?:\(.*\))?$').firstMatch(code.trim());
+ final bodies = name == null ? null : defs[name.group(1)!];
+ if (bodies != null) {
+ return {for (final body in bodies) ...literals(body)};
+ }
+ return null;
+ } finally {
+ _depth--;
+ }
+ }
+
+ /// Where a top-level [mark] (not `?.`, `??` or inside brackets) is, as
+ /// (token, offset).
+ (int, int)? _topLevel(List<Object> tokens, String mark, {(int, int)? from}) {
+ var depth = 0;
+ for (var t = from?.$1 ?? 0; t < tokens.length; t++) {
+ final code = tokens[t];
+ if (code is! String) continue;
+ final start = from != null && t == from.$1 ? from.$2 + 1 : 0;
+ for (var k = start; k < code.length; k++) {
+ final c = code[k];
+ if ('([{'.contains(c)) depth++;
+ if (')]}'.contains(c)) depth--;
+ if (depth != 0 || c != mark) continue;
+ if (mark == '?') {
+ final next = k + 1 < code.length ? code[k + 1] : '';
+ final prev = k > 0 ? code[k - 1] : '';
+ if (next == '.' || next == '?' || prev == '?') continue;
+ }
+ return (t, k);
+ }
+ }
+ return null;
+ }
+
+ /// The source between two marks, literals put back as code.
+ String _join(List<Object> tokens, (int, int) from, (int, int)? to) {
+ final out = StringBuffer();
+ for (var t = from.$1; t < (to?.$1 ?? tokens.length - 1) + 1; t++) {
+ final token = tokens[t];
+ if (token is String) {
+ final start = t == from.$1 ? from.$2 + 1 : 0;
+ final end = to != null && t == to.$1 ? to.$2 : token.length;
+ if (start < end) out.write(token.substring(start, end));
+ } else if (to == null || t < to.$1) {
+ if (t == from.$1) continue;
+ out.write(_source(token as _Literal));
+ }
+ }
+ return out.toString();
+ }
+
+ static String _source(_Literal literal) {
+ final out = StringBuffer("'");
+ for (final piece in literal.pieces) {
+ if (piece is String) {
+ out.write(
+ piece
+ .replaceAll(r'\', r'\\')
+ .replaceAll("'", r"\'")
+ .replaceAll(r'$', r'\$'),
+ );
+ } else {
+ out.write('\${${(piece as _Interpolation).source}}');
+ }
+ }
+ out.write("'");
+ return out.toString();
+ }
}