Address message filter review findings

False positives
- Same-sender repeats need at least 3 words and 10 characters, so short
  replies ("ok", "test") repeated within 10 minutes stay visible.
- Repeated characters needs a run of 20+ of one non-digit character;
  "Noooooooo!", separators, a row of emoji and long numbers are chat.

Correctness
- Rules apply to stored messages: adding a rule hides matching messages
  already in the chat, and removing or disabling one shows again what only
  it was hiding. Other filter reasons are left alone. Also applied when
  history is loaded.
- The filter runs after mesh dedup, inside _addChannelMessage, so each
  message is evaluated and counted by the duplicate tracker once.
- Channels excluded from filtering no longer feed implicit clean examples
  to the classifier.
- Unread counting and the unread anchor treat filtered messages as normal
  when filtering is off for the channel.

Performance
- Classifier training runs in a background isolate (injectable trainer for
  widget tests); app start-up no longer waits for it.
- Reply previews look up the quoted message through an id index instead
  of scanning the channel per bubble.
- The latest-messages channel sort computes each channel's latest visible
  message once.

Cleanup
- Use MessageFilterText.isStructuredPayload instead of a screen copy.

Docs: message-filter.md explains how each check works, including the
learned classifier, and drops the outdated "new messages only" limit.
This commit is contained in:
zjs81 committed 2026-11-06 00:54:09 -07:00
1 parent 4862966e47
commit a9d369f42f
50 files changed
+703 -181

No files matched your search

@@ -371,4 +371,64 @@ void main() {
addTearDown(() => _filter.setChannelExcluded(channelId, false));
expect(_connector.latestVisibleChannelMessage(channel)?.text, text);
});
test('rule changes apply to messages already in the chat', () async {
await _connect();
const text = 'Cheap antennas at the swap meet, ask me';
_connector.handleFrameForTest(
ReviewFrames.channelMessage(
channelIndex: 2,
sender: 'Vendor',
message: text,
timestampSecs: _nowSecs(),
),
);
await _waitFor(() => _find(2, text) != null);
expect(_find(2, text)!.isFiltered, isFalse);
// A new rule hides the message that already arrived...
await _filter.addRule(const FilterRule(id: 'retro', pattern: 'antennas'));
expect(
_find(2, text)!.filterReason,
filterReasonToJson(FilterReason.textRule),
);
expect(_find(2, text)!.filterDetail, 'antennas');
// ...and deleting it shows the message again.
await _filter.removeRule('retro');
expect(_find(2, text)!.isFiltered, isFalse);
// Other filter reasons are not touched by rule changes.
await _connector.markChannelMessageSpam(_channelAt(2), _find(2, text)!);
await _filter.addRule(const FilterRule(id: 'retro2', pattern: 'swap'));
await _filter.removeRule('retro2');
expect(_find(2, text)!.filterReason, 'manual');
});
test('mesh copies of one message are stored and filtered once', () async {
await _connect();
const text = 'Weather net starts at seven tonight on the hill';
final ts = _nowSecs();
final cleanBefore = _filter.recentCleanCount;
_connector.handleFrameForTest(
ReviewFrames.channelMessage(
channelIndex: 2,
sender: 'Relay',
message: text,
timestampSecs: ts,
),
);
_connector.handleFrameForTest(_logRxGroupText(_psk(2), 'Relay: $text', ts));
_connector.handleFrameForTest(_logRxGroupText(_psk(2), 'Relay: $text', ts));
await _waitFor(() => _find(2, text) != null);
await Future<void>.delayed(const Duration(milliseconds: 100));
final copies = _connector
.getChannelMessages(_channelAt(2))
.where((m) => m.text == text)
.toList();
expect(copies, hasLength(1));
// Not mistaken for the sender repeating themselves.
expect(copies.single.isFiltered, isFalse);
expect(_filter.recentCleanCount, cleanBefore + 1);
});
}
@@ -20,7 +20,10 @@ void main() {
SharedPreferences.setMockInitialValues({});
PrefsManager.reset();
await PrefsManager.initialize();
service = MessageFilterService(store: MessageFilterStore());
service = MessageFilterService(
store: MessageFilterStore(),
trainer: MessageFilterService.trainInline,
);
await service.load();
connector = MeshCoreConnector();
});
@@ -8,20 +8,44 @@ void main() {
test('same sender repeat within 10 minutes is a duplicate', () {
final tracker = DuplicateTracker();
expect(
tracker.check(channelId: 'c', sender: 'Bob', text: 'Hello', at: t0),
tracker.check(
channelId: 'c',
sender: 'Bob',
text: 'Anyone on the hill repeater?',
at: t0,
),
isNull,
);
expect(
tracker.check(
channelId: 'c',
sender: 'Bob',
text: 'hello!',
text: 'anyone on the hill repeater!',
at: t0.add(const Duration(minutes: 9)),
),
FilterReason.duplicate,
);
});
test('short replies repeated by the same sender are not duplicates', () {
final tracker = DuplicateTracker();
for (final text in ['ok', 'test', 'yes sir', 'ok']) {
tracker.check(channelId: 'c', sender: 'Alice', text: text, at: t0);
}
for (final text in ['@[Bob] ok', 'test', 'yes sir']) {
expect(
tracker.check(
channelId: 'c',
sender: 'Alice',
text: text,
at: t0.add(const Duration(minutes: 5)),
),
isNull,
reason: text,
);
}
});
test('same sender repeat after the window is not a duplicate', () {
final tracker = DuplicateTracker();
tracker.check(channelId: 'c', sender: 'Bob', text: 'Hello', at: t0);
@@ -141,13 +165,14 @@ void main() {
test('filtered texts are still recorded so floods keep being caught', () {
final tracker = DuplicateTracker();
tracker.check(channelId: 'c', sender: 'Bob', text: 'buy now', at: t0);
const text = 'buy cheap radios now';
tracker.check(channelId: 'c', sender: 'Bob', text: text, at: t0);
for (var i = 1; i <= 5; i++) {
expect(
tracker.check(
channelId: 'c',
sender: 'Bob',
text: 'buy now',
text: text,
at: t0.add(Duration(minutes: 8 * i)),
),
FilterReason.duplicate,
@@ -64,6 +64,48 @@ void main() {
expect(eval('Bob', 'hello there'), isNull);
});
test('no implicit clean examples from excluded channels', () async {
await service.setChannelExcluded('ads', true);
final before = service.recentCleanCount;
service.recordClean(
channelId: 'ads',
sender: 'Bot',
text: 'Buy cheap radios today at the store',
);
expect(service.recentCleanCount, before);
service.recordClean(
channelId: _channel,
sender: 'Sam',
text: 'Anyone on the hill repeater tonight?',
);
expect(service.recentCleanCount, before + 1);
});
test('ruleVerdict matches rules without recording duplicates', () async {
await service.addRule(const FilterRule(id: 'r', pattern: 'meshos'));
expect(
service
.ruleVerdict(channelId: _channel, sender: 'a', text: 'get meshos')
?.reason,
FilterReason.textRule,
);
expect(
service.ruleVerdict(channelId: _channel, sender: 'a', text: 'hello'),
isNull,
);
await service.setChannelExcluded('off', true);
expect(
service.ruleVerdict(channelId: 'off', sender: 'a', text: 'get meshos'),
isNull,
);
// Nothing was recorded: the first real arrival is not a repeat.
const text = 'meet at the old fire tower at noon';
for (var i = 0; i < 3; i++) {
service.ruleVerdict(channelId: _channel, sender: 'a', text: text);
}
expect(eval('a', text), isNull);
});
test('skips outgoing, reactions, payloads, disabled and excluded', () async {
await service.addRule(
FilterRule(id: MessageFilterService.newRuleId(), pattern: 'bad'),
@@ -135,11 +177,12 @@ void main() {
);
test('repeated characters, togglable', () async {
expect(eval('a', 'nooooooooo')?.reason, FilterReason.repeatedCharacters);
expect(eval('a', 'n${'o' * 20}')?.reason, FilterReason.repeatedCharacters);
expect(eval('a', 'nooooooooo'), isNull);
await service.updateSettings(
service.settings.copyWith(repeatedCharacters: false),
);
expect(eval('b', 'yesssssssss'), isNull);
expect(eval('b', 'ye${'s' * 20}'), isNull);
});
test('duplicates: same sender and cross sender', () {
@@ -169,9 +212,15 @@ void main() {
test('rule-filtered texts are still recorded for duplicates', () async {
await service.addRule(const FilterRule(id: 'x', pattern: 'buy'));
expect(eval('a', 'buy stuff')?.reason, FilterReason.textRule);
expect(
eval('a', 'buy cheap radios here now')?.reason,
FilterReason.textRule,
);
await service.removeRule('x');
expect(eval('a', 'buy stuff')?.reason, FilterReason.duplicate);
expect(
eval('a', 'buy cheap radios here now')?.reason,
FilterReason.duplicate,
);
});
test('not spam exempts duplicates in that channel', () async {
@@ -187,7 +236,7 @@ void main() {
test('learning needs two spam taps, then catches similar promos', () async {
for (var i = 0; i < _chat.length; i++) {
service.recordClean(sender: 'User$i', text: _chat[i]);
service.recordClean(channelId: 'chat', sender: 'User$i', text: _chat[i]);
}
await service.markSpam(
sender: 'PromoBot',
@@ -259,7 +308,9 @@ void main() {
await service.addRule(const FilterRule(id: 'n', pattern: 'x'));
await service.markSpam(sender: 'a', text: 'b');
await service.setChannelExcluded('c', true);
expect(count, 3);
// addRule, markSpam (plus once more when its background training lands)
// and setChannelExcluded.
expect(count, 4);
});
test('addRule with index restores a deleted rule in place', () async {
@@ -297,9 +348,14 @@ void main() {
// Ordinary chat then arrives; no further taps.
for (var i = 0; i < _chat.length; i++) {
service.recordClean(sender: 'User$i', text: _chat[i]);
service.recordClean(
channelId: 'chat',
sender: 'User$i',
text: _chat[i],
);
}
await Future<void>.delayed(Duration.zero); // let the queued retrain run
await Future<void>.delayed(Duration.zero); // let the queued retrain start
await service.trainingSettled;
expect(eval('B', probe)?.reason, FilterReason.learned);
expect(eval('C', 'Anyone on the hill repeater tonight?'), isNull);
},
@@ -136,11 +136,21 @@ void main() {
});
test('repeated characters', () {
expect(MessageFilterText.hasRepeatedCharacters('aaaaaaaa'), isTrue);
expect(MessageFilterText.hasRepeatedCharacters('wow!!!!!!!!'), isTrue);
expect(MessageFilterText.hasRepeatedCharacters('AAAAaaaa'), isTrue);
expect(MessageFilterText.hasRepeatedCharacters('😀' * 8), isTrue);
expect(MessageFilterText.hasRepeatedCharacters('aaaaaaa'), isFalse);
expect(MessageFilterText.hasRepeatedCharacters('a' * 20), isTrue);
expect(MessageFilterText.hasRepeatedCharacters('wow${'!' * 20}'), isTrue);
expect(
MessageFilterText.hasRepeatedCharacters('AAAAAAAAAA${'a' * 10}'),
isTrue,
);
expect(MessageFilterText.hasRepeatedCharacters('😀' * 20), isTrue);
expect(MessageFilterText.hasRepeatedCharacters('a' * 19), isFalse);
// Ordinary chat that the old 8-character threshold caught.
expect(MessageFilterText.hasRepeatedCharacters('Noooooooo!'), isFalse);
expect(MessageFilterText.hasRepeatedCharacters('-' * 12), isFalse);
expect(MessageFilterText.hasRepeatedCharacters('!!!!!!!!'), isFalse);
expect(MessageFilterText.hasRepeatedCharacters('😀' * 8), isFalse);
// Digit runs never count (frequencies, coordinates, ids).
expect(MessageFilterText.hasRepeatedCharacters('0' * 30), isFalse);
expect(
MessageFilterText.hasRepeatedCharacters('hello there'),
isFalse,