Skip to content

fix(listen): harden v3 rotation failure paths before the canary - #25731

Merged
kodjima33 merged 2 commits into
mainfrom
fix/v3-rotation-hardening
Nov 18, 2026
Merged

kodjima33 merged 2 commits into
mainfrom
fix/v3-rotation-hardening

Conversation

@kodjima33

@kodjima33 kodjima33 commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Follow-up to #25726 (merged as ae68d3176d, rotation off). The round-4 review found these failure paths in _rotate_v3_recording and the supersede step. They have to land before the one-owner canary.

Change

  1. Pointer move failure (routers/listen/conversations.py, supersede block in create_new_in_progress_conversation). The supersede_live_continuation call runs after the switch. It is now wrapped in try/except: a failure is logged and counted as pointer_failed, and the create always continues to its tail (capture timeline, admission attach, _refresh_speakers, send_conversation_session). Test: test_a_failed_pointer_move_still_finishes_the_successor_and_counts_it, using the real create path in CaptureHarness. The successor is fully bound, the old row is sealed, the phone gets the session event, and the Redis id names the successor.
  2. Redis restore can't mask the error. The restore moved into _clear_failed_successor, where any failure is caught and logged. A WebSocketException from the create is still re-raised. Test: test_a_failing_redis_restore_never_masks_ownership_loss.
  3. Empty successor after a bound-then-failed create.
    • _clear_failed_successor first publishes the successor's close barrier (close_server_batch_audio, with its producer token if it has one), then calls delete_empty_recording_conversation(..., allow_empty_server_batch=True) and checks the result.
    • The barrier is required because the delete only accepts a closed, never-received batch row (database/recording_sessions.py:294-310).
    • A row the delete keeps (it has audio) is counted as successor_kept and logged. It is in_progress and not current, so stale recovery seals or deletes it.
    • Tests use the real tombstone_and_delete_empty_conversation on _FakeFirestore:
      • test_a_failed_create_publishes_the_barrier_then_deletes_its_empty_successor: the unclosed owned successor is deleted. With the barrier call removed, this test fails, because the delete is a no-op.
      • test_a_failed_create_keeps_a_successor_that_has_audio: the successor is kept when it has has_content, server_batch_audio_received or server_batch_audio_start/end.
    • The earlier test that monkeypatched the delete is removed.
    • Residual label window: between the start of the create and the restore, host.recording_session_id already names the successor's session while frames still go to the old row. That label feeds telemetry (receiver.py:886) and the recording-session lease renewal, never audio binding: the archive binds by current_conversation_id, which never names a failed successor.
    • test_frames_during_a_failed_create_never_bind_to_its_successor runs 40 frames through a real LiveAudioArchive while the create fails, with the session label observed as session-2. Every frame lands in the old row's window, the successor is deleted, and nothing stays uncommitted.
    • After an ownership loss (WebSocketException), the Redis restore is skipped, because another socket owns the recording. Test: test_ownership_loss_skips_the_redis_restore.
  4. skipped_producer outcome. It is counted once per row (the set keeps the last 64 row ids; test_producer_skips_stay_bounded) when the reconnect producer protocol blocks rotation, and only for owners rotation admits. pointer_failed, skipped_producer and successor_kept are added to RecordingRotationOutcome. Test: test_a_reconnect_protocol_skip_is_counted_once_per_row.

Rotation stays off in every environment (CONVERSATIONS_V3_RECORDING_ROTATION=off). No flag, chart or runtime env value changes.

Before widening (design only, not in this PR)

A crashed v3 socket leaves its recording in_progress, whether or not rotation ran. Only listen stale recovery seals it: recover_stale_in_progress (routers/listen/conversations.py), on the owner's next connection, after an hour of quiet. The durable quiet check armed at creation refuses it. In recovery_admission_refusal (database/conversation_finalization_jobs.py:380), if conversation.get(TERMINAL_NO_DERIVED_EFFECTS_FIELD): return 'terminal_no_derived' matches every v3 recording, because utils/conversations/v3/triggers.py:61 (mark_new) stamps terminal_no_derived_effects=True at creation. The quiet check reaches it at database/conversation_quiet_checks.py:212.

Smallest fix: exempt only v3 recordings with server-batch audio from that refusal:

if conversation.get(TERMINAL_NO_DERIVED_EFFECTS_FIELD) and not (
    conversation.get('recording') is True and server_batch.has_audio(conversation)
):
    return 'terminal_no_derived'

For a recording the marker means it never takes derived effects. It does not mean the row is already finalized. Processing still goes through recording_short_path (utils/conversations/v3/service.py:54): no model, apps, memories or webhooks, then lifecycle.complete plus the v3 builder trigger.

The refusal is shared with database/stuck_backfill.py:59, so the fix needs one test per caller:

  • the quiet check admits a v3 recording with audio, after its lease expires;
  • the admitted job completes through recording_short_path with no derived effects;
  • stuck backfill and legacy terminal rows are unchanged.

A separate sweeper would duplicate the quiet check's lease and fence logic, so I'd not build one. Ownership: lane/conversations-v3. Until this lands, widening past a canary leaves any crash-orphaned v3 recording invisible until the owner reconnects.

Verification

  • Unit: test_listen_batch_speech_quiet.py, test_listen_v3_rotation_custody.py, test_listen_v3_rotation_continuation.py and test_reason_label_enums.py all pass. Listen, v3, quiet-finalization, desktop-batch, recording-session and query-shape suites: 2572 passed, 1 skipped.
  • backend/scripts/typecheck.sh: 0 errors.
  • scripts/pr-preflight --pr-body-file.
  • No device or prod run. Rotation is off.

Line-Count-Exception: backend/routers/listen/conversations.py | 1458 -> 1519 | rotation failure handling (pointer-move guard, failed-successor cleanup, producer-skip count) stays beside the create path it guards

Product invariants affected

none

Failure class (fixes)

Failure-Class: FC-transport-liveness-read-as-speech-activity

🤖 Generated with Claude Code

@kodjima33
kodjima33 merged commit 75cc459 into main Nov 18, 2026
65 checks passed
@kodjima33
kodjima33 deleted the fix/v3-rotation-hardening branch November 18, 2026 23:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant