Skip to content

fix(firmware): preserve SD read cursor across BLE disconnects (fixes #13100) - #19327

Draft
TheRWX wants to merge 1 commit into
BasedHardware:mainfrom
TheRWX:fix/cv1-sd-read-cursor-advance-13100
Draft

TheRWX wants to merge 1 commit into
BasedHardware:mainfrom
TheRWX:fix/cv1-sd-read-cursor-advance-13100

Conversation

@TheRWX

@TheRWX TheRWX commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #13100.

CV1 firmware previously advanced and persisted the SD ring read pointer (read_seq) from the bt_gatt_notify_cb transmission completion callback every 2 seconds and upon BLE disconnection (introduced in #9954). That callback only indicates radio transmission of the ATT packet and link-layer ACK; it does not guarantee that the mobile OS delivered the packet to the client app or that the app durably flushed Opus frames to disk.

When a transfer disconnected mid-sync, the firmware advanced read_seq 20–22 records (~10 KB) ahead of the client's locally saved prefix. On reconnect, CMD_RING_INFO reported the advanced cursor, permanently discarding uncommitted audio packets from the SD ring.

This PR decouples radio flow control from storage durability:

  • In storage.c, removed sync_checkpoint_advance(), sync_confirmed_bytes, and periodic auto-advance timers.
  • storage_data_tx_done now strictly performs flow control via transport_bulk_tx_release().
  • On link drop (conn == NULL) and NOTIFY_DONE, the transfer stops without modifying read_seq.
  • sd_ring_advance() is restored strictly to client CMD_RING_ADVANCE (0x12).
  • Removed unused sd_ring_advance_async in sd_card.h and sd_card.c.

Verification

  • Standalone C reproduction harness verified baseline failure: unpatched code prematurely jumped read_seq from 100 to 122 on disconnect and on NOTIFY_DONE, reproducing the exact 22-record (9,768-byte) permanent audio loss gap.
  • Harness verified patched pass: read_seq remains firmly locked at 100 across disconnects and completion, advancing strictly upon client CMD_RING_ADVANCE(122).
  • clang-format --dry-run --Werror passed across all touched firmware files.
  • scripts/test-pre-push-firmware-format-prerequisite.sh passed.
  • PRE_PUSH_BASE_REMOTE=upstream ./scripts/pre-push passed all 12 manifest checks and policy guards.

Failure-Class: FC-split-mutation-authority

PayPal: [email protected]

Review in cubic

…asedHardware#13100)

CV1 firmware previously advanced and persisted the SD ring read pointer
(`read_seq`) from the `bt_gatt_notify_cb` transmission completion callback
every 2 seconds and upon BLE disconnection (introduced in BasedHardware#9954). That callback
only indicates radio transmission of the ATT packet and link-layer ACK;
it does not guarantee that the mobile OS delivered the packet to the client
app or that the app durably flushed Opus frames to disk.

When a transfer disconnected mid-sync, the firmware advanced `read_seq`
20–22 records (~10 KB) ahead of the client's locally saved prefix. On
reconnect, `CMD_RING_INFO` reported the advanced cursor, permanently
discarding the uncommitted audio packets from the SD ring.

This patch decouples radio flow control from storage durability:
- In `storage.c`, removed `sync_checkpoint_advance()`, `sync_confirmed_bytes`,
  and periodic auto-advance timers.
- `storage_data_tx_done` now strictly performs flow control via
  `transport_bulk_tx_release()`.
- On link drop (`conn == NULL`) and `NOTIFY_DONE`, the transfer stops
  without modifying `read_seq`.
- `sd_ring_advance()` is restored strictly to client `CMD_RING_ADVANCE` (0x12).
- Removed unused `sd_ring_advance_async` in `sd_card.h` and `sd_card.c`.

Verified:
- Reproduces 22-record (9,768-byte) permanent loss gap on unpatched baseline.
- Preserves `read_seq` at initial uncommitted sequence across link drops and
  NOTIFY_DONE in patched test harness.
- Durably advances cursor strictly upon client `CMD_RING_ADVANCE`.
- Passed `clang-format --dry-run --Werror` on touched firmware files.
- Passed `PRE_PUSH_BASE_REMOTE=upstream ./scripts/pre-push`.

Failure-Class: FC-split-mutation-authority
PayPal: [email protected]
@TheRWX
TheRWX requested a review from TuEmb as a code owner September 27, 2026 04:31
@Git-on-my-level

Copy link
Copy Markdown
Member

Thanks @TheRWX — verified this against main and issue #13100; the diagnosis and fix direction check out.

What I verified:

  • omi/firmware/omi/src/lib/core/storage.c — clean removal of the speculative auto-advance: STORAGE_ADVANCE_CHECKPOINT_MS, sync_confirmed_bytes, sync_confirmed_seq(), sync_checkpoint_advance(), and sync_status_account_freed() are all deleted with no dangling references left in the tree. storage_data_tx_done() is reduced to pure flow control (transport_bulk_tx_release()), and neither the link-drop path (conn == NULL) nor the post-send_done path touches read_seq anymore; sd_ring_advance() now runs only from the CMD_RING_ADVANCE handler (advance_request_pending in storage_write()).
  • omi/firmware/omi/src/sd_card.c — sd_ring_advance_async() removed, and REQ_ADVANCE_READ in sd_worker_thread() restored to the synchronous resp-gated form, so the NULL-resp fire-and-forget path no longer exists. Tree search on the PR head confirms no remaining callers of the async variant.
  • omi/firmware/omi/src/lib/core/sd_card.h — the declaration removal matches the .c removal.

Client compatibility: on current main, app/lib/services/wals/ring_storage_sync.dart (data-safety invariant at the top of the file, and the completion path around line 619) already enforces exactly this contract — CMD_RING_ADVANCE is sent only after NOTIFY_DONE with a good status and a successful local flush, and any cancel/drop leaves the ring untouched. So restoring client-authoritative advance matches the shipped app behavior; the #9954 TX-completion-based auto-advance was the divergence. #10654 had already identified the same boundary ("notification completion is flow control, not durability").

One gap against the repo's Definition of Done: this is a behavior change with no regression test landed in-repo — the reproduction harness described in the PR body was standalone and isn't included. The firmware tree currently has no wired test lane for this path, so at minimum please attach the harness (or a link to it) so the interrupted-transfer scenario is reproducible by maintainers.

Firmware data-durability behavior on the primary capture loop needs maintainer sign-off before a firmware release — leaving for human maintainer review.


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 firmware firmware work positive-signal Automation verified a genuine fix/quality contribution needs-maintainer-review Needs a human maintainer to sign off before merge needs-tests PR introduces logic that should be covered by tests labels Sep 27, 2026
@Git-on-my-level
Git-on-my-level marked this pull request as draft September 28, 2026 09:07
@Git-on-my-level

Copy link
Copy Markdown
Member

Reviewed the full diff against main (head 2575ce8). Treating GATT TX completion as flow control only, with ring advancement left exclusively to the client's CMD_RING_ADVANCE, matches the storage-authoritative direction and closes the data-loss window described in #13100. Main still contains the checkpoint code, so this is not superseded.

Per-file notes:

  • omi/firmware/omi/src/lib/core/storage.c - clean removal of the checkpoint machinery (STORAGE_ADVANCE_CHECKPOINT_MS, sync_confirmed_bytes, sync_checkpoint_advance, sync_status_account_freed). storage_data_tx_done() is back to pure flow control via transport_bulk_tx_release(), and storage_notify_data() no longer smuggles a byte count through user_data. The conn == NULL path in storage_write() now calls storage_stop_transfer() without touching read_seq, and the NOTIFY_DONE path resets transfer state without a forced advance. That is exactly the invariant the issue asks for.
  • omi/firmware/omi/src/sd_card.c - REQ_ADVANCE_READ worker handling reverted to synchronous-only (advance runs only when resp is set). Consistent because sd_ring_advance_async(), the only fire-and-forget NULL-resp producer, is deleted in the same diff; no orphan producers remain.
  • omi/firmware/omi/src/lib/core/sd_card.h - removal of the sd_ring_advance_async() declaration matches the implementation; no remaining callers found.

Two things worth a maintainer's eye before this leaves draft:

  1. Client-protocol compatibility: the ring read pointer now advances only when the phone sends CMD_RING_ADVANCE (0x12). If any released app version relied on the firmware auto-advance introduced in feat: add auto-save for SD card every 2s in sync mode #9954 and does not send 0x12 itself, pairing that app with this firmware would stop freeing the ring (recording would eventually stall or wrap). Worth confirming shipped clients always send 0x12 after a completed or aborted transfer.
  2. Loss of live free-space accounting during sync (sync_status_account_freed removed): used/free/unread stats now refresh only on the periodic SD status refresh and the final client advance. Minor, but noticeable in the app during long syncs.

Static review only - the firmware CI jobs are path-filtered/skipped on this PR, so the standalone harness verification described in the PR body is the main functional evidence; the needs-tests label stays appropriate. When you consider it ready, mark it Ready for review.

Firmware data-loss semantics and the client-protocol compatibility question above need a firmware maintainer's sign-off before merge.

Automated maintainer review on behalf of the Omi maintainers; feedback is AI-generated.


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

This branch has not been deployed

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

Labels

firmware firmware work needs-maintainer-review Needs a human maintainer to sign off before merge needs-tests PR introduces logic that should be covered by tests positive-signal Automation verified a genuine fix/quality contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CV1 firmware advances SD read cursor before the client safely stores audio

2 participants