Skip to content

Keep Drive permission expiry across delivery retry - #753

Open
Ezechungi7 wants to merge 1 commit into
sysprog21:mainfrom
Ezechungi7:fix/retried-delivery-expiry
Open

Ezechungi7 wants to merge 1 commit into
sysprog21:mainfrom
Ezechungi7:fix/retried-delivery-expiry

Conversation

@Ezechungi7

@Ezechungi7 Ezechungi7 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

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 expires_at is written alongside drive_permission_id, survives the retry, and an expired or missing grant blocks ready until the permission is revoked and re-shared. Closes #665.

Bug Fixes

  • set_drive_permission stores expires_at with drive_permission_id; finish_delivery keeps it when marking ready.
  • finish_delivery refuses ready when the saved grant is expired or carries no recorded expiry, then revokes and clears the grant so the next retry shares afresh.
  • forget_drive_file clears expires_at along 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.

Written for commit a305429. Summary will update on new commits.

View guided diff

cubic-dev-ai[bot]

This comment was marked as resolved.

@Quinton1230 Quinton1230 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The normal retry test passes. A reduced SQLite probe at this head reproduced two edge cases: cleanup cuts the operator’s retry window short, and the queue can count an expired grant as delivered.

Comment thread src/recording/transfer.rs Resolved
Comment thread src/recording/transfer.rs Resolved
jserv

This comment was marked as resolved.

jserv

This comment was marked as outdated.

@Ezechungi7
Ezechungi7 force-pushed the fix/retried-delivery-expiry branch from b36184c to 2e8f7e4 Compare November 17, 2026 13:51
cubic-dev-ai[bot]

This comment was marked as resolved.

jserv

This comment was marked as resolved.

jserv

This comment was marked as resolved.

jserv

This comment was marked as outdated.

@onepch onepch left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I reviewed the changes at 2e8f7e4 and identified two opportunities to strengthen regression coverage for Drive permission expiry handling.

The existing recording tests passed (65 passed, 0 failed). I also verified both coverage gaps through mutation testing: the existing tests did not detect the injected regressions, while additional assertions caught them.

Comment thread tests/recording.rs Resolved
Comment thread tests/recording/after_the_end.rs Resolved
@Ezechungi7
Ezechungi7 force-pushed the fix/retried-delivery-expiry branch from 2e8f7e4 to 877dd26 Compare November 18, 2026 17:31
jserv

This comment was marked as resolved.

@Ezechungi7
Ezechungi7 force-pushed the fix/retried-delivery-expiry branch from 877dd26 to 2287c7a Compare November 19, 2026 07:34
jserv

This comment was marked as resolved.

@Ezechungi7
Ezechungi7 force-pushed the fix/retried-delivery-expiry branch from 2287c7a to ecbd56a Compare November 19, 2026 09:18
cubic-dev-ai[bot]

This comment was marked as resolved.

jserv

This comment was marked as resolved.

jserv

This comment was marked as resolved.

@Ezechungi7
Ezechungi7 force-pushed the fix/retried-delivery-expiry branch from ecbd56a to 3c735d8 Compare November 20, 2026 06:09
jserv

This comment was marked as outdated.

jserv

This comment was marked as resolved.

@Ezechungi7
Ezechungi7 force-pushed the fix/retried-delivery-expiry branch from 3c735d8 to dbd65e0 Compare November 20, 2026 08:26
@jserv
jserv dismissed their stale review November 20, 2026 08:33

No merge conflicts at the current head.

@Ezechungi7
Ezechungi7 force-pushed the fix/retried-delivery-expiry branch from dbd65e0 to 0e86922 Compare November 20, 2026 08:59
jserv

This comment was marked as resolved.

@Ezechungi7
Ezechungi7 force-pushed the fix/retried-delivery-expiry branch from 0e86922 to 83a7f62 Compare November 20, 2026 11:34
jserv

This comment was marked as resolved.

@Ezechungi7
Ezechungi7 force-pushed the fix/retried-delivery-expiry branch from 83a7f62 to 13256f2 Compare November 20, 2026 13:52
jserv

This comment was marked as resolved.

@Ezechungi7
Ezechungi7 force-pushed the fix/retried-delivery-expiry branch from 13256f2 to c407c3d Compare November 20, 2026 14:29
jserv

This comment was marked as resolved.

jserv

This comment was marked as resolved.

jserv

This comment was marked as resolved.

jserv

This comment was marked as resolved.

jserv

This comment was marked as resolved.

jserv

This comment was marked as resolved.

jserv

This comment was marked as resolved.

@jserv

jserv commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Some review comments on this pull request are not settled yet. Please refine the change per the review comments listed below, reply on each thread with what you changed or why you did not, and mark each thread resolved yourself once it is settled (Resolve conversation, at the bottom of the thread). The pull request stays blocked while review threads are open.

Not addressed at the current head:

Checked at c407c3d.

@Ezechungi7
Ezechungi7 force-pushed the fix/retried-delivery-expiry branch from c407c3d to 1e34250 Compare November 23, 2026 09:09
jserv

This comment was marked as resolved.

@Ezechungi7
Ezechungi7 force-pushed the fix/retried-delivery-expiry branch from 1e34250 to 9fd8e28 Compare November 23, 2026 11:46
Comment thread tests/recording.rs
refuse_revoke: Mutex<bool>,
refuse_object_delete: Mutex<bool>,
calls: Mutex<Vec<String>>,
pub(crate) shared_expires_at: Mutex<Vec<i64>>,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This file is still capped at the 3822 lines main has, and the changes to it here add one line net, so it is 3823 and scripts/check-rust-style.py --baseline origin/main fails with tests/recording.rs: ceiling: 3823 lines, over 3822, the gate #753 (comment) hit at an earlier head. Take one line back elsewhere in the file, or move a self-contained group out the way recording/after_the_end.rs is wired in with #[path].

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.

Trimmed retention_cleanup_script_lists_without_deleting in tests/recording.rs so the file is 3,821 lines (<= 3,822 on main) and verified with python3 scripts/check-rust-style.py --workspace --baseline upstream/main.

Comment thread tests/recording/after_the_end.rs Outdated
let (s, acc, _, c, r) = harness("shared-delivery");
enqueue_delivery(&acc, &transferring(&acc, &r).await.id, 1_000).unwrap();
let (db, d, mut out) = (s.open(), FakeDelivery::default(), Vec::new());
let f = ",ready_at=1;CREATE TRIGGER IF NOT EXISTS t BEFORE UPDATE ON recordings WHEN NEW.state='ready' AND OLD.ready_at=1 BEGIN SELECT RAISE(FAIL,'');END";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The trigger that keeps the first deliveries from reaching ready is named t again and aborts with an empty message, where #753 (comment) asked for fail_finish and 'finish', and the helper around it reads through s, acc, c, r, d, f, b, q, w, e, sh and g, a nested Step tuple and out.remove(5), so a failing test says neither which step nor which attempt went wrong. The use codetrial::recording::{SnapshotView::*, *} glob added above it also belongs with the named imports at the top of the file. Name the trigger, its message and the bindings by what they hold, and return a small struct per step instead of the tuple.

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.

Moved use codetrial::recording::{SnapshotView::Expired, recording_summary, replay_snapshot}; to the imports at the top of tests/recording/after_the_end.rs, restored the trigger name fail_finish and message 'finish', replaced the single-letter bindings and nested tuple with descriptive names (scratch, accounts, clock, recorder, db, drive, steps, sql, wait) and a Step { expiry, shared, expired } struct, and removed the 5-step dummy loop and out.remove(5).

Comment thread src/recording/transfer.rs Outdated
(recording_id, now),
)?;
Ok(changed > 0)
use {Finished::GrantExpired as G, Finished::*, rusqlite::OptionalExtension};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Finished::*, the G alias, q, s and (changed > 0).then_some(Ready) make the three outcomes the hardest thing here to read, and no ceiling forces it, since this file is 622 lines. Commit and return Finished::Ready when changed > 0, then run the SELECT with the variants spelled out and OptionalExtension imported at the top of the file. The doc comment the old function carried, saying what each outcome means, belongs on Finished.

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.

Imported rusqlite::OptionalExtension at the top of src/recording/transfer.rs, documented the outcomes on Finished, and rewrote finish_delivery to commit and return Finished::Ready when changed > 0 before running the fallback SELECT with Finished::GrantExpired and Finished::Lost spelled out.

Comment thread src/recording/transfer.rs Outdated
accounts: &Accounts,
recording_id: &str,
drive_file_id: &str,
[file, perm]: [Option<String>; 2],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Passing the file and the permission as one [Option<String>; 2] means a caller that builds it as [permission, file] still compiles and matches each id against the other column. Two named Option<String> parameters say which is which, and replace the [..].map(|s| s.map(Into::into)) in clean_up_after_losing with two plain conversions.

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.

Changed forget_drive_file (src/recording/transfer.rs) to take drive_file_id: Option<String> and permission_id: Option<String> as separate named parameters and replaced the array map in clean_up_after_losing with two direct map conversions (drive_file_id.map(str::to_string) and permission.map(|(_, id)| id.to_string())).

jserv

This comment was marked as outdated.

A delivery whose finish write failed after share_file had already
granted the Drive permission computed a fresh expires_at from the clock
on retry, skipped the second share call because existing_permission was
already set, and wrote the later deadline to recordings.expires_at.
Write expires_at alongside drive_permission_id in set_drive_permission,
leave it in place when finish_delivery marks the row ready, and clear
it with the handles when forget_drive_file cleans up a lost race.

Closes sysprog21#665

Change-Id: Ie38a8d5fe15f0ff0cc5af451dd9f43dd1d028829
@Ezechungi7
Ezechungi7 force-pushed the fix/retried-delivery-expiry branch from 9fd8e28 to a305429 Compare November 23, 2026 19:24
Comment thread src/recording/summary.rs
.query_row(
"
SELECT id, interview_id, state, error, created_at, ready_at, expires_at,
SELECT id, interview_id, state, error, created_at, ready_at, CASE WHEN state NOT IN ('transferring', 'failed') OR deleted_by = 'operator' THEN expires_at END,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This CASE WHEN state NOT IN ('transferring', 'failed') OR deleted_by = 'operator' THEN expires_at END is written again word for word in replay_view in src/recording/replay.rs, and due_for_deletion below states the same rule a third way (state = 'ready' OR deleted_by = 'operator'). The readers already drifted apart once on this PR (the thread on replay.rs about operator-expired failed rows), so the next change to which rows show a deadline has to find all three. One shared SQL constant used in both SELECTs, with due_for_deletion either using the same expression or carrying a comment that names where the rule lives, would keep them in step.

Comment thread tests/recording.rs
o.is_ok_and(|o| o.status.success() && (a.ends_with('1') || o.stdout == b"rec-1\n"))
};
assert!(run(q, "9") && !run(",state='failed'", "1000") && run("", "87400"));
assert!(!run("", "9 --expire x") && run("", "9 --expire rec-1") && run("", "9"));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The missing check this replaces asserted !missing.status.success(): --expire with an unknown id must exit non-zero. run is true only for a successful exit and, for arguments not ending in 1, rec-1\n on stdout, so !run("", "9 --expire x") also holds when the script exits 0 and prints nothing, and a script that accepted an unknown id would pass. Keep the exit status in the check, for example by having run return the ExitStatus and asserting !status.success() for this one, or keep the separate missing command.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Retried delivery records an expiry Drive does not hold

4 participants