Skip to content

feat(settings): store subtitle and audio track preferences through v2 - #367

Merged
Quick104 merged 2 commits into
mainfrom
apiv2-stack/19-track-preferences-v2
Sep 23, 2026
Merged

Quick104 merged 2 commits into
mainfrom
apiv2-stack/19-track-preferences-v2

Conversation

@Quick104

Copy link
Copy Markdown
Contributor

Audio and subtitle picks are now saved through /api/v2/{audio,subtitle}-prefs/{series_id} for the owner captured when the write is dispatched. Writes for the same track kind and key go out in the order the user picked them.

Problem

The detail pages (iOS and tvOS) and the player save explicit audio and subtitle picks so they stay selected the next time the item plays. They wrote them to /api/v1/audio-prefs/{series_id} and /api/v1/subtitle-prefs/{series_id} through the v1 transport, which used whatever session was current when the request left. If the profile changed in between, the pick could be saved to the wrong profile. These were the last v1 calls for track preferences (inventory rows 38-41).

Solution

  • TrackSelectionPersistence captures the signed-in owner (captureOrdinaryRequestAuth) when a write is dispatched and sends it through the shared SiloAPI.shared.apiV2Client to PUT / DELETE /api/v2/{audio,subtitle}-prefs/{series_id} (contract operations updateAudioPreference, deleteAudioPreference, updateSubtitlePreference and deleteSubtitlePreference). Call sites in ItemDetailView, TVItemDetailView and PlayerViewModel are unchanged.
  • APIv2Client.writeTrackPreference / deleteTrackPreference already existed and had no callers. They now take a typed TrackPreferenceKind instead of a free string. They send only while the captured owner is current (currentOrdinaryRequestAuth(matchingIdentityOf:) plus withOwnerFence), send X-Profile-Id, percent-encode the key, require exactly 204 and map problem+json responses to APIv2Error.problem.
  • The request bodies match the closed AudioPreferenceUpdate / SubtitlePreferenceUpdate schemas: snake_case, nil members omitted, so an unset show_forced_subtitles keeps the stored override. The audio signature still sends default and embedded_title.
  • The server's v2 delete answers 204 whether or not a preference existed, so the old 404 special case is gone.
  • Writes for the same kind and key now run one at a time, in the order the user picked them. On v1, each write was an independent task, so a pick followed quickly by "Auto" could land out of order and leave a stale override. A failed or unanswered write does not block the next one. The owner is still captured at dispatch, so a write waiting behind another is dropped, not sent for a new profile, if the profile changed meanwhile. Writes for other keys or the other kind are not blocked.
  • Deleted SiloAPI.setSubtitlePref, deleteSubtitlePref, setAudioPref and deleteAudioPref.

Failure outcomes

All four operations are natural_idempotent. The definite-failure and uncertain rows deliberately deviate from the plan §9 failure model; see Risks.

Outcome Behavior
Definite success (204) Nothing else to do; the server applies the preference to the next detail and watch read.
Definite failure (any other status, problem+json, owner changed before send, no signed-in owner) §9 deviation: logged once under TrackPrefs with the real error and dropped. Not shown to the user. Playback is not affected.
Uncertain (sent, no response) §9 deviation: logged and dropped. Not held, and there is no "Discard held change" action. No automatic replay; the next explicit pick sends a fresh write. Later writes for the same key still go out in order.

Validation

  • iOS, tvOS and macOS builds passed.
  • iOS tests (SiloTests/TrackPreferencesV2Tests SiloTests/TrackSelectionPersistenceTests) passed (17 tests). The same classes under SiloTVTests passed (17 tests).
  • scripts/ci/check-no-api-v1.sh passed.
  • New TrackPreferencesV2Tests (on StubURLProtocol) check the exact PUT body members for audio and subtitle, the -1/off subtitle-off body with no signature and no forced override, DELETE paths with a percent-encoded key, that nothing is sent when the owner changed after capture, and that a 200 or a 422 problem is a failure with no retry. Two more tests hold an unanswered subtitle pick and check that the "Auto" DELETE for that key waits for it while an audio write for the same key goes out, and that a write lost to a transport error does not block the next write for its key. The ordering test fails when the chaining is removed.
  • The completed stack passed the full iOS suite (1981 tests) and tvOS suite (1792 tests) with 0 failures, built for macOS and passed the v1 path guard. Every branch in the stack builds on its own.
  • Not run: the manual check (pick a track, replay, confirm it is remembered) against a live server.

Risks and follow-ups

  • The owner is captured when the fire-and-forget task starts, not when the detail or watch data was loaded. A pick made on a screen that outlived a profile switch would be saved for the new profile. That is the same authority boundary as before, and switching profile rebuilds these screens. Passing the detail's load-time auth through ItemDetailViewModel and PlayerViewModel is a possible follow-up.
  • Deliberate deviation from plan §9, for reviewers to accept or reject: track picks are best-effort. A definite failure is logged and not shown to the user, and an uncertain outcome is not held and has no "Discard held change" action. A failed write costs a remembered preference, never playback, and v1 behaved the same way. A §9 hold with a discard action would need new UI on the detail pages and the player, which is out of proportion for this PR.
  • Writes are ordered but not coalesced: every pick is sent, in order. Skipping a superseded PUT is not safe in general, because a PUT that omits show_forced_subtitles keeps the stored value, so a skipped earlier PUT could drop a forced-subtitle change.
  • Android has no subtitle-prefs or audio-prefs calls to compare against. The web client uses the same v2 operations.
  • DiagnosticsPathTemplateTests still uses /api/v1/subtitle-prefs as a path-template vector. Z1 owns that allowlist entry.

AI disclosure

Code and description written by Claude Opus 5.5 (claude-opus-5-5) agents in Claude Code (T3 Code), orchestrated by a Claude Code workflow.


Part 19 of 50 in the Apple API v2 stack. Base: apiv2-stack/18-settings-writes-v2. Merge in order from the bottom of the stack. The plan and full index are in PR 01.

🤖 Generated with Claude Code

@coderabbitai

coderabbitai Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 33a30f00-09b3-4c41-b0b3-776ae5c781c9

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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

Quick104 has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-23T13:41:01.900576Z a4c472a New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Quick104

Copy link
Copy Markdown
Contributor Author

Code review summary

Reviewed head: b0f05493 (apiv2-stack/19-track-preferences-v2).

Process: an adversarial review of this PR's diff with each candidate finding independently verified before any fix, a simplify pass for medium and larger PRs, and a whole-stack review across contract conformance, authority and failure handling, data integrity, user-visible behavior and cross-PR integration.

Verified findings, all fixed on this branch:

  • minor: Write failures are logged and dropped, which departs from the plan §9 failure model without saying so

Validation: this branch builds on its own for iOS, tvOS and macOS, and its focused tests passed. The completed stack passed the full iOS suite (1981 tests) and tvOS suite (1792 tests) with 0 failures, the macOS build and the v1 path guard. The final stack was also exercised on iOS and tvOS simulators against a v1/v2 bridge server: sign-in (password and QR), restore, browse, search, detail, favorites and watchlist, playback (direct play, transcode, subtitles, stop), audiobooks, downloads with offline playback, settings, requests, profiles, push registration and diagnostics. Server logs showed no /api/v1 request from the new builds except the retained health probe.

Remaining: no open verified findings. Merge in stack order.

Mergeability: 8/10 — conditional. The approach holds up and every verified finding is fixed. Conditions: this PR's CI must pass (it was pending at review time), and the owner still plans a physical-device check of the completed stack.

Review by Claude Opus 5.5 (claude-opus-5-5) agents in Claude Code (T3 Code).

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b0f05493cd

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +216 to +218
writeOrder.enqueue("\(kind.rawValue)/\(prefKey)") { previous in
let auth = await tokenStore.captureOrdinaryRequestAuth()
await previous?.value

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Capture the owner before scheduling the write

If a profile switch occurs after saveAudio/saveSubtitle returns but before this new task gets time on TokenStore, the capture observes the replacement profile rather than the profile that made the selection. Every subsequent fence then succeeds for that replacement owner, so the old profile’s track choice can be stored under the new profile—the exact cross-profile write this change is intended to prevent. Capture or pass the originating authority before enqueueing the fire-and-forget work.

Useful? React with 👍 / 👎.

@Quick104
Quick104 force-pushed the apiv2-stack/18-settings-writes-v2 branch from bfb4131 to b46bf1c Compare September 23, 2026 13:19
@Quick104
Quick104 force-pushed the apiv2-stack/19-track-preferences-v2 branch from b0f0549 to a678385 Compare September 23, 2026 13:19
@Quick104
Quick104 force-pushed the apiv2-stack/18-settings-writes-v2 branch from b46bf1c to ffba129 Compare September 23, 2026 13:37
Base automatically changed from apiv2-stack/18-settings-writes-v2 to main September 23, 2026 13:37
Quick104 and others added 2 commits September 23, 2026 09:37
The detail pages and the player remembered explicit audio and subtitle
picks by writing /api/v1/audio-prefs and /api/v1/subtitle-prefs with
whatever session was current when the request left, so a pick could land
on a profile other than the one that made it.

TrackSelectionPersistence now captures the signed-in owner when a write
is dispatched and sends it through the shared APIv2Client to
PUT/DELETE /api/v2/{audio,subtitle}-prefs/{series_id}. The client sends
only while that owner is current, percent-encodes the key, requires 204
and maps problem responses to typed errors. The preference kind is a
typed enum instead of a free string. The v2 delete answers 204 for an
absent preference, so the old 404 swallow goes away. The four v1
SiloAPI methods are deleted.

The writes are natural_idempotent, but they stay best-effort: a failed
or unanswered write is logged and dropped, and the next explicit pick
sends a fresh one.

Stack-PR: W4-C
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Each audio or subtitle preference write ran as an independent task, so a
pick followed quickly by "Auto" could reach the server out of order and
leave a stale override. The v2 path adds actor hops before dispatch, which
widens that window.

Writes for the same kind and key now chain: each starts only after the one
dispatched before it has finished, whatever its outcome. The owner is still
captured at dispatch, so a profile switch while a write waits drops it
instead of sending it for the new profile. Writes for other keys or the
other kind are not blocked. Failures stay best-effort (logged, not held),
which the PR body records as a deliberate deviation from the plan's
failure model.

Stack-PR: W4-C
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@Quick104
Quick104 force-pushed the apiv2-stack/19-track-preferences-v2 branch from a678385 to a4c472a Compare September 23, 2026 13:37
@Quick104
Quick104 merged commit 997d706 into main Sep 23, 2026
3 of 6 checks passed
@Quick104
Quick104 deleted the apiv2-stack/19-track-preferences-v2 branch September 23, 2026 13:37
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