fix(#943,#952): act on the Gemini gate findings

Two of five findings were real.

Log the downgrade refusal BEFORE the path lookup and the throw. onUpgrade runs
inside drift's beforeOpen, so it fires on whichever isolate issued the first
query, and whoever catches the exception decides what the user sees. A
downgrade that only stopped background work, with nothing in the log, would be
the quiet failure this branch exists to replace. The log line now lands first,
so the record exists however the exception is handled.

Strengthen the recovery bench. It asserted row COUNTS, so a converter
producing the right number of rows with wrong contents would have passed.
It now also checks that every identified blob record has a row carrying its
identity, and that no payload is empty, both derived from the raw blob rather
than from the converter under test so the check is not circular. Records with
no messageId are not identity-checked, because reproducing the `x:` fallback
key here would make it circular again; the count assertion still covers them.

Verified on a copy of the owner's database with the trap reconstructed (the
6,308-message channel removed from the marker plus one planted row the blob
does not contain): the key migrates, and all 6,308 identities are present.
Then verified the new assertion can FAIL, by planting an empty payload the
migration does not overwrite:

  Expected: empty
    Actual: WhereIterable<String>:['{}']

Three findings were rejected, with reasons in the PR body: the duplicate
merge-key collapse is pre-existing and unchanged by this diff, the claimed
migration/blob-save race cannot occur because of the re-check inside the
per-key lock, and the path_provider call is bounded and already proven.

Task: meshcore-open-lkp
Agent Mail: TealGlen

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
fix/943-952-storage-migration
diffwireauto 4 weeks ago
parent c55649cb4f
commit fa8c2658d9

@ -377,8 +377,23 @@ class OffbandDatabase extends _$OffbandDatabase {
MigrationStrategy get migration => MigrationStrategy(
onUpgrade: (m, from, to) async {
if (from > to) {
// Best-effort: path_provider has no web implementation, and a message
// without the path still beats the silent rewrite it replaces.
// Logged BEFORE the path lookup and before the throw, deliberately.
// This runs inside drift's beforeOpen, so it fires on whichever
// isolate issued the first query, and whoever catches the exception
// decides what the user sees. A downgrade that only stopped
// background work, with nothing in the log, would be the quiet
// failure this branch exists to replace (Gemini review).
appLogger.error(
'Refusing to open the database: it is at schema $from and this '
'build understands $to. The upgrade to a newer schema is one-way '
'(#943).',
tag: 'Storage',
);
// Best-effort: path_provider has no web implementation, and on the
// platforms where a downgrade is reachable this reads environment
// variables rather than a platform channel, and has already resolved
// once to open this database. A message without the path still beats
// the silent version rewrite it replaces.
String? path;
try {
path = await pinnedDatabasePath();

@ -26,6 +26,19 @@ import 'package:shared_preferences/shared_preferences.dart';
const String _benchDb = String.fromEnvironment('BENCH_DB');
/// Non-empty `messageId`s in a history blob, read straight from the JSON.
///
/// Deliberately not via the converter: this is used to check the converter's
/// output, so sharing its code would make the check circular.
List<String> _messageIdsIn(String? blob) {
if (blob == null || blob.isEmpty) return const [];
return (jsonDecode(blob) as List<dynamic>)
.map((e) => (e as Map<String, dynamic>)['messageId'] as String?)
.whereType<String>()
.where((id) => id.isNotEmpty)
.toList();
}
void main() {
setUp(() async {
SharedPreferences.setMockInitialValues({});
@ -115,14 +128,40 @@ void main() {
(ok ? recovered : refused).add(key);
if (ok) {
// Every blob record must be readable as a row, and the extra rows
// that caused the old refusal must still be there.
expect(
rowsAfter,
greaterThanOrEqualTo(records),
reason: '$key lost records while migrating',
);
expect(await blobs.isOnMessageRows(key), isTrue);
// Counts alone would pass against a converter that produced the
// right NUMBER of rows with the wrong contents (Gemini review).
// Identity and payloads are checked too, derived from the raw blob
// rather than from the converter under test, so this is not
// circular.
//
// Only records carrying a messageId are identity-checked: the `x:`
// fallback merge key would mean reimplementing the converter here,
// which would make the check circular again. The count assertion
// above still covers those.
final ids = _messageIdsIn(blob);
final stored = await blobs.messageMergeKeys(key);
final absent = ids.where((id) => !stored.contains('id:$id')).toList();
expect(
absent,
isEmpty,
reason:
'$key: ${absent.length} of ${ids.length} identified messages '
'in the blob have no row carrying their identity',
);
final payloads = await blobs.messagePayloads(key);
expect(
payloads.where((p) => p.trim().length <= 2),
isEmpty,
reason: '$key stored empty payloads',
);
}
}

Loading…
Cancel
Save

Powered by TurnKey Linux.