Skip to content

fix(app): keep live phone-mic audio until the transcript confirms it - #20387

Open
Itaxh1 wants to merge 6 commits into
BasedHardware:mainfrom
Itaxh1:fix/phone-mic-keep-backup
Open

Itaxh1 wants to merge 6 commits into
BasedHardware:mainfrom
Itaxh1:fix/phone-mic-keep-backup

Conversation

@Itaxh1

@Itaxh1 Itaxh1 commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

TL;DR: Live phone-mic audio was marked as synced the moment it was sent over the socket, so the app kept no backup of it. If the server never saved the text, the recording was gone (#20366). Last week 495 phone-mic recordings sent audio and never got text back: 101 of them were longer than 30 seconds, about 12 hours of audio. Now phone frames stay in the WAL until the transcript confirms them, as pendant frames have since #19199, and audio the server didn't transcribe is uploaded for repair. The capture screen still shows only audio at risk, so a healthy recording doesn't read as "Uploading audio". App only, no backend change.

What changed and why

Closes #20366

  • Frames are kept. One per-frame bit used to do two jobs. A socket send set it, and a chunk whose frames were all set was never stored (or was born synced). That is how sent phone audio lost its backup.
    • The bit now only records that a live socket took the frame: markFrameSynced is renamed markFrameStreamed.
    • LocalWalSyncImpl._chunk and finalizeCurrentSession keep every live chunk as a miss until the transcript confirmation the pendant uses releases it. A send can no longer drop or confirm audio.
  • The capture screen still shows only audio at risk (found by cubic's review). The three phone send paths mark frames streamed again. The in-flight count and the session window (getSessionWals, getSessionUnsyncedWals) leave out a copy the socket took in full (syncedFrameOffset == totalFrames), so a healthy recording shows no "Uploading audio" or "Audio saved locally" line, and no pending count. Audio captured while the socket was down still shows.
  • Stamping works after the user stops. A phone recording is usually stopped before the server closes its conversation. By then activeRecordingId is null, so the recording-id check that fix(sync): bind safety WALs to live recording intervals #19424 added treated every WAL as foreign, left it unstamped, and it would have been uploaded again without a conversation. This went unnoticed only because phone recordings had almost no WALs.
    • With no active id, stampConversationId now uses the recording the WAL store was bound to when the window began: the store records each binding and its time. That holds even if a newer recording binds before the close, and even if the stopped recording's own copies have already uploaded (both found by cubic's review). Rebinding the same recording doesn't push that binding out of the history, and logout clears the history (also from cubic). The controller's stamp code is unchanged from main.
    • An active recording id keeps priority, so another recording's WALs are never claimed.
  • The pendant has shown that indicator for all of its live audio since fix: make live capture uploads durable and self-healing #19199, because its frames are never marked. Marking them streamed is now safe and would be a one-line follow-up.

Costs and known limits

  • Storage: phone audio is PCM16 at 16 kHz, about 1.9 MB a minute (about 115 MB per recorded hour). It stays on disk until the conversation is confirmed, then it's deleted. The existing 720-WAL cap still applies: in a long outage the oldest copies are evicted, so the changelog promises a kept copy, not that nothing can be lost.
  • Repair uploads:
    • When the server returns no text, the whole recording is uploaded for repair.
    • When the connection was interrupted, the existing repair path uploads the whole session, as it does for the pendant.
    • These uploads are raw PCM, about 8 times the pendant's Opus. PostHog can't size the interrupted case yet, because Recording Completed.socket_error_count is 0 on every recording. Uploading only the uncovered audio after an interruption, or encoding the backup as Opus, would cut this and could be a follow-up.
  • Partial transcripts: with this PR alone, a phone conversation that gets any text releases its whole backup, as the pendant does today. Flutter transcription: a partial transcript deletes the pendant's whole audio backup #20364 (fix(app): release the pendant backup only for audio the saved transcript covers #20376) limits that release to the audio the transcript covers. It also releases a covered copy that was stamped by recording id but starts before the window, which main's confirmation leaves behind. Land it first, as the merge order says.

Product invariants affected

none

How it was verified

  • flutter test test/services/capture/capture_phone_mic_backup_test.dart: 5 passed. On main without the fix:
    • the never-transcribed and empty-transcript scenarios fail, because no WAL exists;
    • the full-transcript scenario passes on both, as it should.
  • The capture-screen scenario fails on this PR's first commit: the streamed copy showed up as audio at risk.
  • The newer-recording scenario fails on the commit that fell back to the WAL store's last-bound id: the stopped recording's copies stayed unstamped.
  • The stopped-recording unit test fails on the commit that took the id from the window's earliest unstamped WAL: a newer recording's copy was stamped with the older conversation. The rebinding and logout tests fail on the previous commit.
  • The chunk-join unit tests fail on the previous code, which counted only the new chunk's streamed run.
  • The scenarios wait for the expected WAL state, in memory and in the saved index, and fail with the WAL state if it never arrives. They don't treat a quiet moment as done.
  • With frames kept but without the stamp fallback, the empty-transcript and full-transcript scenarios failed: the WALs were left unstamped after stop. That's how the stop-then-close problem was found.
  • Capture, WAL, sync, spine and capture-provider suites (test/unit/*wal*|*capture*|*sync*|*transcri*|*recording*|*segment*, test/services/capture, test/services/wals, test/spine, test/providers/capture_provider_test.dart): 955 passed. This includes the existing network-loss replay, which still uploads the recording exactly once.
  • bash app/test.sh: 3,910 passed, 5 skipped, 0 failed.
  • Combined with Flutter transcription: a partial transcript deletes the pendant's whole audio backup #20364 (fix(app): release the pendant backup only for audio the saved transcript covers #20376) and Flutter transcription: later conversations on one pendant connection never get their backup tagged, so they are re-uploaded as duplicates #20365 (fix(app): stamp and release every conversation's pendant backup on one socket #20380) on a local merge, the three branches merge cleanly. On that merge, the related suites pass (987 tests), and bash app/test.sh reports 3,942 passed, 5 skipped, 0 failed.
  • flutter analyze on the changed files: no new issues. The touched debug line also drops its unnecessary interpolation braces.
  • Not run on a physical device.

Tests

app/test/services/capture/capture_phone_mic_backup_test.dart (new) uses the replay world: the real CaptureController, native mic service, LocalWalSyncImpl and RecordingTransferCoordinator, with a scripted socket. Each scenario records 130 s with the socket connected and checks that every frame reached the server.

  • The server never transcribes it: the audio stays on the phone, and a recovery pass uploads it.
  • The server closes it with no saved text: it's uploaded under that conversation.
  • The server's transcript covers it: the copy is released and nothing is uploaded.
  • A newer recording binds to the WAL store after the stop and before the close. The stopped recording's copies are still stamped with its conversation.
  • The capture screen counts only audio the socket did not take. After 110 s online, a streamed copy is on disk, yet nothing is shown as at risk and the in-flight count is 0. After a 20 s network drop, the in-flight count is 20 s.

app/test/unit/wal_recovery_test.dart and app/test/unit/local_wal_sync_test.dart:

  • A fully streamed session is still stored, as a miss with no retention clock, even with unlimited local storage on.
  • A fully streamed copy is left out of the session window; a copy with any audio the socket didn't take is in it.
  • The in-flight count covers only frames no socket took.
  • When a chunk joins an existing WAL, the streamed run carries over only if the socket took every earlier frame. A missed frame ends it, and an earlier break doesn't resume.
  • A stamp with no active recording takes the one bound when its window began. A newer recording's miss copy is left alone even after the stopped recording's own copy has uploaded. After 20 rebinds of the same recording, its window still finds it. After logout, an old recording never matches.

Failure class (fixes)

Failure-Class: none

Review in cubic

Phone-mic frames were marked synced as soon as they were sent, so nothing was
written to the WAL while the socket was connected, and audio the server lost
could not be recovered. Keep them until transcript confirmation, as pendant
frames already are.

A stopped phone recording has no active recording id by the time the server
closes its conversation, so the stamp found every WAL foreign and left it
unstamped, to be uploaded again without a conversation. The stamp now falls
back to the recording id the WAL store last bound.

Closes BasedHardware#20366
@Itaxh1

Itaxh1 commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review

@cubic-dev-ai

cubic-dev-ai Bot commented Oct 2, 2026

Copy link
Copy Markdown

@cubic-dev-ai review

@Itaxh1 I have started the AI code review. It will take a few minutes to complete.

@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.

All reported issues were addressed across 4 files

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

Re-trigger cubic

Itaxh1 added 2 commits October 2, 2026 16:59
Writing, stamping and releasing copies touch real files outside the
virtual scheduler, so the scenarios wait for the WAL state they assert.
With phone frames kept until transcript confirmation, the frame bit that
once released a sent frame also fed the capture screen, so a healthy
recording read as "Uploading audio" with a growing "saved locally" total.

The bit now only records that a live socket took the frame
(markFrameStreamed). It no longer decides whether a chunk is stored or
born synced: every live chunk is kept as a miss until a transcript
confirms it. The in-flight count and the session window leave out
streamed audio, so the indicator and its pending count show only audio
at risk.

The changelog no longer promises that recordings cannot be lost, and the
scenario waits now also require the saved index to match.
@Itaxh1

Itaxh1 commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review

@cubic-dev-ai

cubic-dev-ai Bot commented Oct 2, 2026

Copy link
Copy Markdown

@cubic-dev-ai review

@Itaxh1 I have started the AI code review. It will take a few minutes to complete.

@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.

All reported issues were addressed across 9 files

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

Re-trigger cubic

The stamp fell back to the recording id the WAL store last bound, which
is stale once a newer recording binds before the server closes the
stopped one's conversation: its WALs then read as foreign and stayed
unstamped. The stamp now takes the id on the session's earliest
unstamped WAL, so the controller is back to main's stamp code.

A chunk that joins an existing WAL now carries the streamed run only
when the socket took every earlier frame, so a fully streamed copy is
not shown as at risk.

The changelog leads with the benefit.
@Itaxh1

Itaxh1 commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review

@cubic-dev-ai

cubic-dev-ai Bot commented Oct 2, 2026

Copy link
Copy Markdown

@cubic-dev-ai review

@Itaxh1 I have started the AI code review. It will take a few minutes to complete.

@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.

All reported issues were addressed across 9 files

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

Re-trigger cubic

…its window began under

With no active recording id, the stamp took the id on the window's earliest unstamped
WAL. Once the stopped recording's own copies had uploaded, that could be a newer
recording's WAL, which then got stamped with the older conversation. The store now
records when it binds each recording, and the stamp uses the binding at the window's
start.
@Itaxh1

Itaxh1 commented Oct 3, 2026

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review

@cubic-dev-ai

cubic-dev-ai Bot commented Oct 3, 2026

Copy link
Copy Markdown

@cubic-dev-ai review

@Itaxh1 I have started the AI code review. It will take a few minutes to complete.

@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.

All reported issues were addressed across 9 files

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

Re-trigger cubic

… them on logout

Rebinding the same recording no longer pushes the binding a window started under
out of the history, and logout clears the history with the account's WALs.
@Itaxh1

Itaxh1 commented Oct 3, 2026

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review

@cubic-dev-ai

cubic-dev-ai Bot commented Oct 3, 2026

Copy link
Copy Markdown

@cubic-dev-ai review

@Itaxh1 I have started the AI code review. It will take a few minutes to complete.

@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.

No issues found across 9 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Re-trigger cubic

@Itaxh1
Itaxh1 marked this pull request as ready for review October 3, 2026 06:47
@Itaxh1
Itaxh1 requested a review from mdmohsin7 as a code owner October 3, 2026 06:47

@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.

No issues found across 9 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Re-trigger cubic

@Git-on-my-level

Copy link
Copy Markdown
Member

Reviewed the full diff on 293423b — this is a well-built fix for a real data-loss bug (#20366), and the design matches the contract pendant audio already has since #19199. Per-file notes:

  • local_wal_sync.dart — the core change is sound. The per-frame bit now only records that a live socket took a frame (markFrameStreamed), and _chunk() / finalizeCurrentSession() keep every live chunk as WalStatus.miss with syncedAt = 0, so a socket send can no longer drop or confirm audio. _leadingStreamedFrames plus the syncedFrameOffset carry-over on chunk append (contiguous vs non-contiguous evidence) is handled correctly, and I verified there is no remaining markFrameSynced reference anywhere under app/.
  • capture_controller.dart — all three phone send paths mark frames streamed again, and unsyncedSessionWals / sessionTranscriptionBacklogCounts correctly consume the new getSessionWals semantics, so a healthy recording no longer reads as "Uploading audio".
  • wal_interfaces.dart — the rewritten doc comment ("a send is not a save") is accurate and matches the code.
  • phone_mic_source.dart — comment-only rename; fine.
  • capture_phone_mic_backup_test.dart — strong addition: six replay scenarios including kept-until-confirmed, repair upload on empty transcript, post-stop binding, release without re-upload, and at-risk accounting.
  • local_wal_sync_test.dart, wal_recovery_test.dart, capture_coordinator_test.dart — expectations consistently rewritten for the new contract; the fully-streammed case now asserts a kept miss copy with syncedFrameOffset == totalFrames.
  • 20261002-phone-mic-keep-until-saved.json — accurate user-facing changelog wording.

One awareness point for the maintainer sign-off: streamed phone-mic audio now persists on disk until the transcript confirms it, and unconfirmed copies are deliberately exempt from the auto-remove retention sweep (syncedAt == 0 never sweeps) — they leave only via transcript confirmation or repair upload. That's the right trade for not losing recordings, but it's a retention behavior change worth a human decision for low-storage devices.

Also noting the Dart/mobile suites were skipped in CI on this head, so the (good-looking) tests haven't machine-run here; they were reviewed statically.

Thanks @Itaxh1 — the diagnosis (495 recordings / ~12h of audio lost last week) and the fix quality are excellent.


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 flutter flutter work labels Oct 3, 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.

Confirmed bug (phone-mic backup loss, closes #20366) and well-tested, but mergeStateStatus is DIRTY (real conflict) and diff is 803 lines — approve-only, not merging; author/sibling-PR stack needs to resolve conflicts (policy forbids rebasing the branch ourselves).

@aryanorastar

Copy link
Copy Markdown
Collaborator

Tested this end to end on the iOS Simulator against the local backend, with a small proxy that accepts /v4/listen audio and never returns a transcript (the #20366 case).

  • Base 2e878f6d: 119 s phone-mic recording, 3,584,000 bytes taken by the socket, 0 transcripts. After Finish, wals.json was empty and no audio file remained, so the recording was unrecoverable.
  • This branch 293423be: 126 s recording, 4,025,280 bytes taken, 0 transcripts. After Finish, 3 WALs remained as miss on disk, ready for repair upload. Every frame was kept: the three files hold 4,075,596 bytes, exactly the 4,025,280 bytes streamed plus the 4-byte length prefix on each of the 12,579 frames.
  • With the base's local_wal_sync.dart, wal_interfaces.dart, phone_mic_source.dart and capture_controller.dart restored, 4 of the 5 tests in capture_phone_mic_backup_test.dart fail; on this branch the four touched suites pass (157 tests).

The branch now conflicts with main. Screen recordings of both runs are available if useful.

@undivisible

Copy link
Copy Markdown
Collaborator

@Itaxh1 this conflicts with main now. Please rebase and push; it still looks valid.

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

flutter flutter work positive-signal Automation verified a genuine fix/quality contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flutter transcription: phone-mic audio has no backup once it is sent

5 participants