Skip to content

fix(app): stop deleting a recording from clearing the device's SD card - #13510

Merged
kodjima33 merged 2 commits into
BasedHardware:mainfrom
singhharsh1708:fix/app-wal-delete-cascade
Sep 12, 2026
Merged

kodjima33 merged 2 commits into
BasedHardware:mainfrom
singhharsh1708:fix/app-wal-delete-cascade

Conversation

@singhharsh1708

@singhharsh1708 singhharsh1708 commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

WalSyncs.deleteWal sends every recording delete to all five sub-syncs, whichever one owns it. The ring sync already checks membership first (the guard added in #7218, whose comment calls out exactly this cascade), but the SD card, storage and flash page syncs don't. So deleting any recording from the sync page, the auto-sync page or a recording's detail page while a device is connected also runs their device-side action.

For the SD card sync that is writeToStorage(fileNum, 1, 0). The DevKit firmware handles command 1 as DELETE_COMMAND and runs clear_audio_file(1) and save_offset(0) whatever file number was sent (omi/firmware/devkit/src/storage.c). Phone recordings default to fileNum = 1, so deleting a phone recording with a DevKit connected wipes the audio still waiting on its SD card. The storage sync sends [0x12, fileIndex]; the current Omi firmware reads 0x12 as ring advance and rejects a 2-byte write, so CV1 on the ring firmware isn't hit by either path. The flash page sync acknowledges the Limitless pendant up to wal.storageTotalBytes for any synced wal. Phone recordings carry 0 there, so I couldn't show that ack frees anything, but it shouldn't run for a recording that sync doesn't own.

All three now return early unless the wal is one of their own device recordings (the storage type they manage and listed in _wals), the same way the ring sync does. The storage type matters because a chunk copied to the phone keeps the original device and timerStart, and Wal.id is '${device}_$timerStart', so an id-only check would still clear the card when that phone copy is deleted. The device command also only goes out when wal.device is the connected device: the storage sync keeps its list across a device switch, so a stale recording from the previous device would otherwise be deleted by file number on the new one. The stale entry is still dropped from the list. Bulk deletes aren't affected because those only walk each sync's own list.

test/unit/wal_delete_cascade_test.dart builds each sync with a connected device and one of its own recordings, then deletes a phone recording, the phone copy of a device recording, a device recording it doesn't list, and a recording from a previous device. These classes can't take a fake connection, so reaching the device shows up in the test as ServiceManager throwing "Service manager is not initiated"; a control per sync deletes its own recording and expects exactly that. Against main with only the @visibleForTesting setters added, 9 of the 12 fail (every case where the sync doesn't own the recording) and the three controls pass. With the fix all 12 pass.

cd app
flutter test test/unit/wal_delete_cascade_test.dart   # +12: All tests passed!
flutter test test/unit/*wal*_test.dart test/unit/sdcard*_test.dart test/providers/*wal*_test.dart \
  test/widgets/sync_row_recovery_window_test.dart test/unit/sync_manifest_claim_drain_test.dart   # +173: All tests passed!
bash test.sh                          # +1913 ~5: All tests passed!
bash scripts/analyze_ratchet.sh       # Analyzer ratchet passed.

No existing class covers a fan-out receiver acting on a target it doesn't own, so this adds FC-fanout-delete-skips-ownership-check with #7218 as the earlier instance.

Failure-Class: new

WalSyncs.deleteWal fans every delete out to all sub-syncs. The ring sync
already ignores wals it does not own (BasedHardware#7218), but the SD card and
storage syncs sent their device delete for any wal. With a DevKit
connected, deleting a phone recording sent DELETE_COMMAND, which clears
the SD card audio file regardless of the file number. Both now return
early unless the wal is one of their own SD recordings.

Failure-Class: new

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 issues found across 4 files

Confidence score: 2/5

  • app/lib/services/wals/storage_sync.dart can accept a stale WAL from the previous device and delete its fileNum on the newly connected device, risking deletion of unrelated device data — require wal.id to match the current device ID before deleting.
  • app/lib/services/wals/sdcard_wal_sync.dart leaves flash_page_wal_sync.deleteWal outside the new ownership guard, so deleting a synced phone recording while a Limitless/Rayban is connected may still delete the wrong device’s WAL — apply the guard to that fifth fan-out path as well.
  • app/test/unit/wal_delete_cascade_test.dart relies on the internal “Service manager is not initiated” exception text to verify a device write, making tests brittle if that implementation message changes — assert the write attempt through a stable mock or explicit interaction instead.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="app/test/unit/wal_delete_cascade_test.dart">

<violation number="1" location="app/test/unit/wal_delete_cascade_test.dart:43">
P3: The 'still sends the delete to the device' tests assert that the device write is attempted by matching the internal exception text 'Service manager is not initiated' thrown by an uninitialized ServiceManager. This couples the tests to both the exception message and the test environment never initializing ServiceManager; if either changes, these tests break or silently pass for the wrong reason, and they never verify the actual deletion outcome (the wal removed from _wals, or a write sent). Inject/assert observable behavior instead, e.g. keep the wal list as the assertion after forcing the device call, or stub the connection write.</violation>
</file>

<file name="app/lib/services/wals/storage_sync.dart">

<violation number="1" location="app/lib/services/wals/storage_sync.dart:226">
P1: When the device changes before `_wals` refreshes, this membership check accepts a stale WAL from the previous device and deletes its `fileNum` on the new device. Require `wal.id` to match the current device ID and reject a null device before issuing the delete.</violation>
</file>

<file name="app/lib/services/wals/sdcard_wal_sync.dart">

<violation number="1" location="app/lib/services/wals/sdcard_wal_sync.dart:116">
P2: The ownership guard added here is not applied to flash_page_wal_sync.deleteWal, the fifth sub-sync in the same fan-out. When a synced phone recording is deleted while a Limitless/Rayban is connected, WalSyncs.deleteWal fans the delete to FlashPageWalSyncImpl.deleteWal, which still runs `_acknowledgeProcessedData` (for `wal.status == synced`) and clears the pendant's flash. This is the same cascade/data-loss class the SD-card/storage guards and the new failure class describe. Apply the same ownership guard (storage == flashPage and id present in `_wals`) to flash_page_wal_sync.deleteWal, or note explicitly why that receiver is exempt.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic


@override
Future deleteWal(Wal wal) async {
if (wal.storage != WalStorage.sdcard || !_wals.any((w) => w.id == wal.id)) return;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: When the device changes before _wals refreshes, this membership check accepts a stale WAL from the previous device and deletes its fileNum on the new device. Require wal.id to match the current device ID and reject a null device before issuing the delete.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At app/lib/services/wals/storage_sync.dart, line 226:

<comment>When the device changes before `_wals` refreshes, this membership check accepts a stale WAL from the previous device and deletes its `fileNum` on the new device. Require `wal.id` to match the current device ID and reject a null device before issuing the delete.</comment>

<file context>
@@ -219,6 +223,7 @@ class StorageSyncImpl implements StorageSync {
 
   @override
   Future deleteWal(Wal wal) async {
+    if (wal.storage != WalStorage.sdcard || !_wals.any((w) => w.id == wal.id)) return;
     await _deleteWalsOnDevice([wal]);
     _wals = _wals.where((w) => w.id != wal.id).toList();
</file context>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 3d4e247. The device command now only goes out when wal.device is the connected device, so a stale recording from the previous device is dropped from the list without deleting anything on the new one. Same check in the SD card and flash page syncs, with a test per sync that fails on main.


@override
Future deleteWal(Wal wal) async {
if (wal.storage != WalStorage.sdcard || !_wals.any((w) => w.id == wal.id)) return;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: The ownership guard added here is not applied to flash_page_wal_sync.deleteWal, the fifth sub-sync in the same fan-out. When a synced phone recording is deleted while a Limitless/Rayban is connected, WalSyncs.deleteWal fans the delete to FlashPageWalSyncImpl.deleteWal, which still runs _acknowledgeProcessedData (for wal.status == synced) and clears the pendant's flash. This is the same cascade/data-loss class the SD-card/storage guards and the new failure class describe. Apply the same ownership guard (storage == flashPage and id present in _wals) to flash_page_wal_sync.deleteWal, or note explicitly why that receiver is exempt.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At app/lib/services/wals/sdcard_wal_sync.dart, line 116:

<comment>The ownership guard added here is not applied to flash_page_wal_sync.deleteWal, the fifth sub-sync in the same fan-out. When a synced phone recording is deleted while a Limitless/Rayban is connected, WalSyncs.deleteWal fans the delete to FlashPageWalSyncImpl.deleteWal, which still runs `_acknowledgeProcessedData` (for `wal.status == synced`) and clears the pendant's flash. This is the same cascade/data-loss class the SD-card/storage guards and the new failure class describe. Apply the same ownership guard (storage == flashPage and id present in `_wals`) to flash_page_wal_sync.deleteWal, or note explicitly why that receiver is exempt.</comment>

<file context>
@@ -106,6 +113,7 @@ class SDCardWalSyncImpl implements SDCardWalSync {
 
   @override
   Future deleteWal(Wal wal) async {
+    if (wal.storage != WalStorage.sdcard || !_wals.any((w) => w.id == wal.id)) return;
     _wals.removeWhere((w) => w.id == wal.id);
 
</file context>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same cascade, agreed. Guarded in 3d4e247 with storage == flashPage plus membership, and the ack only goes to the connected device. Phone recordings carry storageTotalBytes 0, so today that path acks index 0 and I couldn't show it frees anything on the pendant, but it shouldn't run for a wal the flash page sync doesn't own.

originalStorage: WalStorage.sdcard,
);

final _reachedDevice = throwsA(predicate((e) => '$e'.contains('Service manager is not initiated')));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: The 'still sends the delete to the device' tests assert that the device write is attempted by matching the internal exception text 'Service manager is not initiated' thrown by an uninitialized ServiceManager. This couples the tests to both the exception message and the test environment never initializing ServiceManager; if either changes, these tests break or silently pass for the wrong reason, and they never verify the actual deletion outcome (the wal removed from _wals, or a write sent). Inject/assert observable behavior instead, e.g. keep the wal list as the assertion after forcing the device call, or stub the connection write.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At app/test/unit/wal_delete_cascade_test.dart, line 43:

<comment>The 'still sends the delete to the device' tests assert that the device write is attempted by matching the internal exception text 'Service manager is not initiated' thrown by an uninitialized ServiceManager. This couples the tests to both the exception message and the test environment never initializing ServiceManager; if either changes, these tests break or silently pass for the wrong reason, and they never verify the actual deletion outcome (the wal removed from _wals, or a write sent). Inject/assert observable behavior instead, e.g. keep the wal list as the assertion after forcing the device call, or stub the connection write.</comment>

<file context>
@@ -0,0 +1,90 @@
+      originalStorage: WalStorage.sdcard,
+    );
+
+final _reachedDevice = throwsA(predicate((e) => '$e'.contains('Service manager is not initiated')));
+
+void main() {
</file context>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no injection point here: ServiceManager builds a bare DeviceService and these syncs only reach the device through ServiceManager.instance(), so the uninitialized-manager error is the narrowest signal that the device path ran. The negative tests also assert the sync's own list is untouched, and a control per sync pins the positive path.

…connected device

The flash page sync had the same cascade: it acknowledged the Limitless
pendant for any synced wal. It now gets the same ownership guard. The
storage sync also keeps its list across a device switch, so a stale
recording from the previous device could be deleted by file number on
the new one. All three syncs now only send the device command when
wal.device is the connected device, and still drop the stale entry.

Failure-Class: new
@Git-on-my-level

Copy link
Copy Markdown
Member

Verified the full chain behind this one — the reported root cause and the fix both check out end to end.

Root cause, confirmed on main: WalSyncs.deleteWal fans every delete out to all five sub-syncs, and only the ring sync guarded membership (the #7218 guard). So deleting a phone recording (storage: disk, fileNum defaulting to 1) with a device connected reached:

  • sdcard_wal_sync.dart — _writeToStorage(deviceId, fileNum, 1, 0). DevKit firmware treats command 1 as DELETE and runs clear_audio_file(1) + save_offset(0) regardless of the file number sent (omi/firmware/devkit/src/storage.c), so the card gets wiped. Real data loss.
  • storage_sync.dart — deleteStorageFile(fileNum) sends a 2-byte 0x12 write, which omi firmware rejects as a malformed ring-advance (len != 9): no damage today, but a junk write in the wrong protocol.
  • flash_page_wal_sync.dart — _acknowledgeProcessedData(deviceId, storageTotalBytes) ran for any synced wal, acknowledging the pendant for a recording that sync doesn't own.

The fix mirrors the ring guard in all three receivers, and the two-condition form is the right one: the storage check matters because a phone copy keeps the original device and timerStart (Wal.id is '${device}_$timerStart', so an id-only check would still fire), and the wal.device == _device.id check in storage_sync.dart covers the stale-list window across a device switch, since its setDevice doesn't rebuild _wals (unlike the sdcard sync's). Dropping the stale entry from the list while skipping the device write is the correct split. Bulk paths (deleteAllSyncedWals/deleteAllPendingWals) only ever walk each sync's own list, so they're unaffected — confirmed.

The tests in wal_delete_cascade_test.dart isolate each clause: phone-recording, phone-copy, not-listed, and previous-device cases per sync with a positive control each, so the membership clause is decided by a same-storage unlisted wal — exactly the regression an id-only guard would reintroduce. Keying the positive controls on the uninitialized-ServiceManager error is a reasonable proxy given there's no injection point around ServiceManager.instance(). Two minor notes, both non-blocking: the flash-page negative test passes implicitly (no throw where the ack path would throw) — a one-line comment would make that contract explicit for future readers; and the @visibleForTesting setters slightly widen the public surface of the three impls, fine for now and worth revisiting if these classes ever grow constructor-injectable state.

FC-fanout-delete-skips-ownership-check.json matches the existing failure-class schema and every claim in it checked out against the code and firmware above — good documentation of the invariant.

Human maintainer sign-off wanted before merge — not out of doubt about the change, but because it alters destructive device-side delete/ack semantics on the recording pipeline, and that class of change keeps a human in the loop.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@Git-on-my-level Git-on-my-level added positive-signal Automation verified a genuine fix/quality contribution needs-maintainer-review Needs a human maintainer to sign off before merge flutter flutter work labels Sep 12, 2026

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug-fix, confidence 5/5: real ownership bug (SD-card/storage/flash-page syncs missing the membership guard #7218 already has for ring sync), scoped diff (182 lines, 4 files), test demonstrates 9/12 failing on main and passing after fix, CI green. Not already fixed on main (verified).

@kodjima33
kodjima33 merged commit c831ba0 into BasedHardware:main Sep 12, 2026
36 of 37 checks passed
@huiyuansun28-cmyk

Copy link
Copy Markdown
Contributor

PR: FAILED

AUTO (AI Agent). Authorship disclosed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flutter flutter work needs-maintainer-review Needs a human maintainer to sign off before merge positive-signal Automation verified a genuine fix/quality contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants