Keep Drive permission expiry across delivery retry

EZ
28m ago
+128-71

Keep Drive permission expiry across delivery retries

Description

When deliver_recording (src/recording/transfer.rs) shares a file through Google Drive, share_file grants a time-limited permission with expires_at = clock.now() + retention_seconds and stores drive_permission_id via set_drive_permission, while finish_delivery previously wrote recordings.expires_at only in the subsequent finish step. When finish_delivery failed and the delivery was retried after DELIVERY_BACKOFF_SECONDS, the next attempt computed a fresh, later expires_at from the clock, skipped the Drive share call because existing_permission was already set, and overwrote recordings.expires_at with the later timestamp—leaving the row and the status route (GET /api/recordings/{id}) reporting a deadline later than the expirationTime that Google Drive holds.

This change implements Option 1 from #665:

  • set_drive_permission (src/recording/transfer.rs) writes SET drive_permission_id = ?2, expires_at = ?3 atomically when the Drive permission is recorded, and finish_delivery transitions the row to state = 'ready' without overwriting expires_at on retry.
  • forget_drive_file (src/recording/transfer.rs) clears expires_at = NULL alongside drive_file_id = NULL and drive_permission_id = NULL when a lost race cleans up an uncompleted delivery.

Validation Plan

  1. Retried delivery expiry preservation (tests/recording.rs — transfer_retry_after_share_keeps_the_drive_expiry):
    • Run run_delivery_queue with a FakeDelivery double that records the expires_at passed to share, while a SQLite trigger aborts the finish_delivery update (WHEN NEW.state = 'ready') on the first attempt.
    • Remove the trigger, advance the clock by DELIVERY_BACKOFF_SECONDS[0] (60 seconds), and run run_delivery_queue a second time.
    • Assert that FakeDelivery received only one share call and that recordings.expires_at equals the original t0 + RETENTION_SECONDS from the Drive permission rather than t0 + 60 + RETENTION_SECONDS.
  2. Lost-race cleanup (tests/recording.rs — failure_drive_loser_stops_naming_the_file_it_deleted):
    • Verify that when a delivery loses the row race and forget_drive_file runs, (drive_file_id, drive_permission_id, expires_at) are all reset to (None, None, None).
  3. Repository gates & mutation coverage:
    • cargo fmt -- --check and cargo clippy --all-targets -- -D warnings
    • cargo test --test recording

Closes #665


Summary by cubic

Keeps the Google Drive permission's expiry across delivery retries. A retry after a failed finish_delivery used to recompute a later expires_at from the clock, skip re-sharing because the permission already existed, and overwrite the stored deadline; now the stored expiry survives, an expired grant blocks the retry and is cleared so the next attempt shares afresh. Closes #665.

Bug Fixes

  • set_drive_permission stores expires_at alongside drive_permission_id, and finish_delivery keeps it on retry.
  • finish_delivery refuses to mark the recording ready when the saved grant has expired or carries no recorded expiry, then revokes and clears the grant so the next retry shares afresh.
  • forget_drive_file clears expires_at with the Drive handles during lost-race cleanup.
  • due_for_deletion, the cleanup script, and the summary and replay views honor expires_at only for ready or operator-deleted rows, ignoring it for transferring and failed.