Skip to content

feat(metadata): read metadata AI status and translate descriptions through v2 - #390

Merged
Quick104 merged 2 commits into
mainfrom
apiv2-stack/42-metadata-ai-v2
Sep 23, 2026
Merged

Quick104 merged 2 commits into
mainfrom
apiv2-stack/42-metadata-ai-v2

Conversation

@Quick104

Copy link
Copy Markdown
Contributor

The item-detail "Translate" affordance works on 1.0 servers again: the metadata AI capability read and the description translation now use v2.

Problem

The Apple clients still read the metadata AI capability from GET /api/v1/metadata/ai/status and queued description translations with POST /api/v1/items/{id}/translate-description. A 1.0 server answers both with 410, so the item-detail "Translate" affordance never appears. The v2 translate operation differs from v1: it answers 202 with a MetadataTranslationJob body, requires X-Profile-Id, and can return a recently failed job without starting new work.

Solution

  • SiloAI.metadataAIStatus() calls GET /api/v2/capabilities/metadata-ai (getMetadataAICapability) on the shared SiloAPI.shared.apiV2Client. The read needs a selected profile and sends X-Profile-Id. The feature is enabled only when allowed && state == "available". An unknown on_view value decodes to .off, and an empty revision is rejected.
  • SiloAI.translateDescription(contentId:targetLanguage:auth:) calls POST /api/v2/catalog/items/{id}/translate-description (translateCatalogItemDescription) with a catalogPathSegment-encoded id. The call runs inside withOwnerFence for the captured owner and accepts only 202. It rejects a job that names a different item or an unknown target_kind.
  • DescriptionTranslationCoordinator captures the owner before its first await and sends the POST for that owner. A returned failed or canceled job puts the coordinator in its failed state. Before each detail poll and before applying a result, it checks that the owner is still current. After an account or profile change, it stops with the failed state and applies nothing.
  • The metadata AI methods move to APIv2Client+MetadataAI.swift and their wire models move to APIv2MetadataAIModels.swift. The shared AI owner capture is renamed from subtitleCreateAuthority() to captureAIAuthority(); it had no app callers yet.
  • Deleted: both v1 paths, the v1 MetadataAIStatus decoder and its v1 wire test, and the v1 TranslateDescriptionBody. MetadataAIStatus is now a plain domain value. The AIModels.swift header no longer lists the metadata routes as v1 and points to APIv2MetadataAIModels.swift.

Failure outcomes for the translate POST. The contract marks it x-silo-retry-safety: coalescing, so the server reuses an active job for the same item and language:

Outcome Behavior
Definite success (202, matching job, not failed) Poll item detail on the existing bounded backoff (about 31 s) until pending_translation_language clears.
Definite failure (non-202, problem+json such as 409, mismatched job, reused failed or canceled job) Show the failed state. The Translate button stays available. The .auto latch prevents automatic re-firing.
Uncertain (sent, no response) Show the failed state. A manual retry is safe because the operation is coalescing. The app never replays it automatically.
Owner changed Before dispatch, send nothing. During the POST, the owner fence discards the answer and the coordinator shows the failed state. During polling, the coordinator stops with the failed state and applies nothing.

Validation

  • iOS, tvOS and macOS builds pass on this branch.
  • Focused tests pass on iOS and tvOS: MetadataAIV2Tests, APIv2ContractTests, AIModelDecodingTests and APIv2SubtitleTests, 61 tests each, 0 failures.
  • New MetadataAIV2Tests (StubURLProtocol) cover:
    • the capability path and X-Profile-Id
    • the allowed/state gate
    • a 503 problem and an empty revision both throwing, with no fallback to disabled
    • no request when no profile is selected
    • the encoded translate path and {"target_language"} body
    • a reused failed job
    • 200 and 409 rejected
    • a job for another item rejected
    • no dispatch after a profile change
  • scripts/ci/check-no-api-v1.sh passes. The withOwnerFence/sameCredentialIdentity count in iosApp/iosApp stays at 69.
  • The final stack passed the full suites (iOS 1981 tests, tvOS 1792 tests, 0 failures), the macOS build and the v1 path guard, and every branch in the stack builds on its own.

Risks and follow-ups

  • AICapabilities still treats a failed capability read as "feature unavailable" until the next refresh (app foreground, cold launch, sign-in or profile switch). That matches the current behavior, but plan invariant I9 asks for a retryable state. This PR does not add a retry path.
  • The capability read ignores ETag and If-None-Match, and the server's 304 is not used.
  • The coordinator's detail poll goes through SiloAPI.itemDetail, which captures the current owner itself. The coordinator's owner checks before and after the poll keep another owner's detail from being applied.
  • The coordinator has no tests and was not run in the app. Only the client calls have tests.

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 42 of 50 in the Apple API v2 stack. Base: apiv2-stack/41-requests-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: 4eef94a0-a0d9-4189-8a2d-650295ff38e2

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:24:19.087911Z ca67075 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: c54f7063 (apiv2-stack/42-metadata-ai-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: AIModels header still lists the metadata AI endpoints as /api/v1 routes
  • minor: PR body understates when a failed capability read is retried

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: c54f7063d1

ℹ️ 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 +86 to +88
auth = try await api.captureAuthority()
guard isCurrentRun(runID) else { return }
let job = try await api.translateDescription(contentId: contentId, targetLanguage: targetLanguage, auth: auth)

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 Bind translation to the displayed detail owner

The repository ownership contract says a displayed catalog read must authorize its later action rather than capturing whichever profile is current after asynchronous work begins (docs/native-api-v2.md:12-16). Here no owner is retained from the displayed detail, and captureAIAuthority() itself awaits gate() before reading TokenStore; if the identity changes while the task is queued or that gate is suspended and the view remains active, this captures the replacement profile and posts the old item's translation for it. isCurrentRun only checks cancellation/run identity, so pass the detail's captured owner into the coordinator (or otherwise snapshot it before the first suspension) and fence the POST against that owner.

Useful? React with 👍 / 👎.

@Quick104
Quick104 force-pushed the apiv2-stack/42-metadata-ai-v2 branch from c54f706 to ca67075 Compare September 23, 2026 13:19

@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: ca67075250

ℹ️ 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".

}
guard raw.statusCode == 202 else { throw APIv2Error.httpStatus(raw.statusCode) }
let job = try HTTPClient.makeJSONDecoder().decode(APIv2MetadataTranslationJob.self, from: raw.data)
guard !job.id.isEmpty, job.contentId == contentID, ["item", "season", "episode"].contains(job.targetKind) else {

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 Verify the translation job's target language

When the 202 response names the requested item but contains a different target_language, this guard accepts it even though the decoded field is the only evidence that the returned/coalesced job represents the requested translation. The coordinator then polls for the requested language while tracking an unrelated job, leaving the UI spinning until its roughly 31-second timeout or potentially applying an unintended translation; reject responses whose job.targetLanguage does not match language.

Useful? React with 👍 / 👎.

@Quick104
Quick104 force-pushed the apiv2-stack/41-requests-v2 branch from 40e5048 to 7330831 Compare September 23, 2026 13:44
Base automatically changed from apiv2-stack/41-requests-v2 to main September 23, 2026 13:44
Quick104 and others added 2 commits September 23, 2026 09:44
…rough v2

SiloAI still read the metadata AI capability from
/api/v1/metadata/ai/status and queued description translations at
/api/v1/items/{id}/translate-description. A 1.0 server answers both with
410, so the on-view translate affordance never appeared. The v2
translation answers 202 with a job body, needs a selected profile, and
can return a recently failed job without starting new work.

SiloAI now calls the shared APIv2Client for both. The capability read
sends X-Profile-Id and enables the feature only when the profile is
allowed and the state is available. DescriptionTranslationCoordinator
captures the owner before its first await, sends the POST for that
owner, shows the failed state for a reused failed or canceled job, and
stops polling without applying a detail read once the account or
profile changes. The POST is coalescing, so a failed attempt stays
retryable from the Translate button.

Move the metadata AI methods to APIv2Client+MetadataAI.swift and their
wire models to APIv2MetadataAIModels.swift, rename the shared AI owner
capture to captureAIAuthority, and delete the v1 MetadataAIStatus
decoder and its test.

Stack-PR: W5-S
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The AIModels.swift header still said every model rides /api/v1 and listed
the metadata AI status and translate-description routes, which this PR
moved to APIv2Client. Point the header at APIv2MetadataAIModels.swift for
the metadata wire shapes and keep only the subtitle AI routes in the v1
endpoint list.

Stack-PR: W5-S
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@Quick104
Quick104 force-pushed the apiv2-stack/42-metadata-ai-v2 branch from ca67075 to 827f7d3 Compare September 23, 2026 13:44
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

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