Files
buzz/mobile/lib/shared/mentions/mention_bindings.dart
Matt Toohey 2cbdabdae5 fix(mobile): bind picked mentions by position and never show a key in the composer
A picked chip is now bound to its recipient by its place in the draft, not
by its label. The editor carries each binding across every edit with a
selection-aware diff, so deleting the first of two identical chips removes
that recipient, text typed before a chip moves it, and a chip stays bound
until an edit reaches into it or a letter or digit is typed directly against
it. Punctuation, straight and curly apostrophes, a hyphen or a slash after a
chip keep it and its recipient (`@Bob's idea`, `@Bob-bot`, `@Bob/Alice`),
the same boundary rule buzz-app uses (recommendation 3).

Namesakes no longer put a public key in the composer. A picked chip whose
name another candidate shares reads the chooser's short label, `@Sam · a1b2`,
while the draft text stays `@Sam`; both Sams can be picked and each chip
keeps its own key. The sent content qualifies namesakes of different keys
as `@Sam (<key>)` so bubbles still tell them apart, except inside code,
and each picked Sam gets its own p tag (recommendation 4). The ambiguity
refusal for a typed, unpicked `@sam` is unchanged.

Drafts persist their bindings as positions (`mentions`), with the saved
label-keyed `mention_keys` still read and an unreadable record kept as a
tombstone that refuses to send while an `@` remains. The agent prefill and
failed-send recovery restore text and bindings in one update.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Signed-off-by: Matt Toohey <contact@matttoohey.com>
2026-11-19 15:06:59 +11:00

70 lines
2.9 KiB
Dart

/// Longest literal ranges win, including labels containing another @ sign.
/// Recognition precedes eligibility: ambiguous labels still block shorter ones.
/// A label opens where a query does (portable mention rules, section 2): at
/// the start, after whitespace, `(`, `[` or `{`, or after a Markdown
/// delimiter, so a mention picked after a bracket keeps its recipient.
List<({int start, int end, String label})> mentionOccurrences(
String text,
Iterable<String> labels,
) {
final matches = <int, ({int start, int end, String label})>{};
for (final label in labels) {
if (label.isEmpty) continue;
// A qualifier and reservation suffix belong to the literal even when no
// candidate binds it. Never fall back to a shorter, different recipient.
final suffix =
RegExp(r' \([0-9a-f]{64}\)$', caseSensitive: false).hasMatch(label)
? r'(?! (?:[2-9]|[1-9][0-9]+)(?=[\s,;.!?:)\]}*_]|$))'
: '';
final pattern = RegExp(
'(?:^|[\\s(\\[{]|[*_]{1,3}|\\|\\|)(@${RegExp.escape(label)})(?! \\([0-9a-f]{64}\\))$suffix(?=\\|\\||[\\s,;.!?:)\\]}*_]|\$)',
caseSensitive: false,
);
for (final match in pattern.allMatches(text)) {
final start = match.end - match.group(1)!.length;
if (matches[start] == null || matches[start]!.end < match.end) {
matches[start] = (start: start, end: match.end, label: label);
}
}
}
final result = <({int start, int end, String label})>[];
for (final match
in matches.values.toList()..sort((a, b) => a.start.compareTo(b.start))) {
if (result.isEmpty || match.start >= result.last.end) result.add(match);
}
return result;
}
/// Bind tagged qualifiers and ordinary aliases, never historical namesakes.
Map<String, Set<String>> renderedMentionBindings(
String content,
Map<String, String> names, [
Iterable<String> signedPubkeys = const [],
]) {
final bindings = <String, Set<String>>{};
void add(String label, String key) =>
(bindings[label.toLowerCase()] ??= {}).add(key.toLowerCase());
for (final entry in names.entries) {
add(entry.value, entry.key);
add(entry.value.split(RegExp(r'\s+')).first, entry.key);
}
final keys = signedPubkeys.map((key) => key.toLowerCase()).toSet();
for (final match in RegExp(
r'@([^@\r\n]+) \(([0-9a-f]{64})\)(?: ((?:[1-9][0-9]+|[2-9])))?',
caseSensitive: false,
).allMatches(content)) {
final key = match.group(2)!.toLowerCase();
final label = match.group(0)!.substring(1).toLowerCase();
if (!mentionOccurrences(content, [
label,
]).any((range) => range.start == match.start)) {
continue;
}
// Text can deny historical aliases without a profile, never authorize keys.
bindings[label] = keys.contains(key) ? {key} : <String>{};
// Recipient tags/current names cannot establish the historical plain owner.
bindings[match.group(1)!.toLowerCase()] = <String>{};
}
return bindings;
}