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): a notification body is never cut through the middle of an emoji

dee6f0607796460
diff --git a/mobile/lib/notify/agent_announcer.dart b/mobile/lib/notify/agent_announcer.dart
index 09aba5971..9a125cc46 100644
--- a/mobile/lib/notify/agent_announcer.dart
+++ b/mobile/lib/notify/agent_announcer.dart
@@ -168,5 +168,10 @@ String noticeBody(String text, {int limit = 180}) {
final last = paragraphs.isEmpty ? text : paragraphs.last;
final line = last.replaceAll(RegExp(r'\s+'), ' ').trim();
if (line.length <= limit) return line;
- return '${line.substring(0, limit - 1).trimRight()}…';
+ // Back off one unit rather than cut an emoji — or any character past the
+ // BMP — in half: a lone surrogate reaches the lock screen as `�`, since the
+ // platform channel encodes the body as UTF-8.
+ var end = limit - 1;
+ if (end > 0 && (line.codeUnitAt(end - 1) & 0xFC00) == 0xD800) end--;
+ return '${line.substring(0, end).trimRight()}…';
}
diff --git a/mobile/test/notify/agent_announcer_test.dart b/mobile/test/notify/agent_announcer_test.dart
index 5bd2f149e..0e0a642ba 100644
--- a/mobile/test/notify/agent_announcer_test.dart
+++ b/mobile/test/notify/agent_announcer_test.dart
@@ -236,5 +236,30 @@ void main() {
expect(body.length, 20);
expect(body.endsWith('…'), isTrue);
});
+
+ test('never cuts an emoji in half', () {
+ // 🎉 is two UTF-16 units. Cut between them, the lock screen got a lone
+ // surrogate — which the platform channel's UTF-8 encoding turns into
+ // U+FFFD, a `�` in front of the ellipsis.
+ final body = noticeBody('${'x' * 18}🎉 and the rest', limit: 20);
+ expect(body, '${'x' * 18}…');
+ expect(
+ body.codeUnits.any((unit) => unit >= 0xD800 && unit <= 0xDFFF),
+ isFalse,
+ );
+ // An emoji that fits whole is kept whole.
+ expect(noticeBody('${'x' * 17}🎉 and more', limit: 20), '${'x' * 17}🎉…');
+ });
+
+ test('a reply that is only whitespace says nothing', () {
+ expect(noticeBody(' \n\n \t'), '');
+ });
+
+ test('a question is one line already, and is said as asked', () {
+ expect(
+ noticeBody('Approve Bash command: psql -f migrate.sql'),
+ 'Approve Bash command: psql -f migrate.sql',
+ );
+ });
});
}