Skip to content

fix(metadata): release a kept rebind source's episode external IDs - #5631

Open
rangoDJ wants to merge 3 commits into
Silo-Server:mainfrom
rangoDJ:fix/4613-rebind-kept-source-episode-ids
Open

rangoDJ wants to merge 3 commits into
Silo-Server:mainfrom
rangoDJ:fix/4613-rebind-kept-source-episode-ids

Conversation

@rangoDJ

@rangoDJ rangoDJ commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Depends on: none. Merge order: none.

Problem

Part of #4613 (its path 1, and the first bullet of its Expected behavior). This doesn't close the issue.
Validation tasks: none

Other clients: Apple, Android and Roku get real episode titles for the affected show after its next refresh; no client change is needed. Jellyfin-compat N/A.

When a refresh finds that an item's provider IDs belong to another confirmed item, rebindItemOnto (internal/metadata/service.go) moves the source's library memberships, file links, provider IDs and user state onto the target. Then it deletes the source only if its status is deletable. From the Process path, a source with durable provider IDs reaches the rebind only while it is provisional, and the delete keeps it only if its status has turned matched since. A kept source has no library, no files and no provider ID rows. Its episodes keep their tmdb_id, tvdb_id and imdb_id, which are unique across all episodes (idx_episodes_tmdb_id and the like). So the target's episode upserts fail on those IDs, and the real episodes keep their scanner placeholders (Episode 1…). The issue saw 47 such items, one holding 13 episodes' TMDB IDs for ten days.

Approach

  • One new step in rebindItemOnto, after the source delete: it sets tmdb_id, tvdb_id and imdb_id to '' (the columns' empty default) on the source's episodes that still hold any. A deleted source took its episodes with it (ON DELETE CASCADE), so the step only affects a kept source. The target's next refresh can then store the IDs.
  • It runs in the rebind's transaction, after the user-state reattribution, which maps episodes by season and episode number and doesn't read these IDs.
  • Not in this PR, from the same issue:
    • a periodic cleanup of series items with no membership, files or user state, whatever their status;
    • path 2: items held during the file-removal grace keep their IDs.
    • Both need their own design, so the issue stays open for them.

Validation

  • New TestRebindKeptSourceReleasesEpisodeIDsPostgres sets up two matched series, the source with an episode holding TMDB, TVDB and IMDb IDs. It rebinds the source onto the target the way Process does, with a matched source not deletable. It checks that the source is kept, that its episode's IDs are released, and that the target can then store an episode with the same IDs. Added to scripts/ci/db-contracts.txt.
  • With main's service.go, the kept source still holds the IDs (output under Evidence).
  • With this change: go test -count=1 ./internal/metadata/ passes against a local Postgres 18. lint-changed: 0 issues.
    • In ./internal/catalog/, TestEpisodeCatalogRefreshSerializesFileChangesPostgres fails here with a deadlock, on this branch and on clean main alike (two runs each). CI runs it in its own environment.
  • Not tested: a live rebind across two libraries, as in the issue.

Evidence

No screen changes until the target's next refresh, which then shows its episodes' real titles. Results of go test -count=1 -v -run TestRebindKeptSourceReleasesEpisodeIDsPostgres ./internal/metadata/ against a local Postgres 18.

Before (service.go from main bc17dc0):

    rebind_kept_source_episode_ids_db_test.go:54: kept source episode IDs = "tmdb-a3296133-3a10-4e1d-b728-30906e7dd952" "tvdb-a3296133-3a10-4e1d-b728-30906e7dd952" "tta3296133-3a10-4e1d-b728-30906e7dd952", want them released
--- FAIL: TestRebindKeptSourceReleasesEpisodeIDsPostgres
FAIL
FAIL	github.com/Silo-Server/silo-server/internal/metadata

After (this branch, d3fdeff):

--- PASS: TestRebindKeptSourceReleasesEpisodeIDsPostgres
ok  	github.com/Silo-Server/silo-server/internal/metadata

Risks

  • The kept source's episodes lose their external IDs. The source has no library, files or provider IDs, so nothing shows or plays it. Items already left like this before the change keep their IDs until another rebind or the cleanup the issue asks for.
  • No API, schema or config change.

Checklist

  • I read and can explain the complete diff.
  • This pull request addresses one concern.
  • Each test this pull request adds meets "Decide what to test" in CONTRIBUTING.md.
  • The Evidence section shows every change a user can see, or how I checked a change nobody sees.

AI Disclosure

  • Harness: Claude Code in the Claude desktop app
  • Tool(s): Claude Code
  • Model(s): claude-opus-5-5
  • Involvement: Fully AI-generated, not yet human verified
  • Adversarial review: n/a

🤖 Generated with Claude Code

Note

Release kept rebind source's episode external IDs in rebindItemOnto

When a rebind keeps the source series (allowMatchedSource=false), its episodes retain TMDB, TVDB, and IMDb IDs. This blocks other episodes from storing those IDs due to uniqueness constraints. MetadataService.rebindItemOnto now clears those provider IDs and bumps updated_at for remaining source episodes in a new transaction step, added in service.go. Also adds a PostgreSQL integration test, TestRebindKeptSourceReleasesEpisodeIDsPostgres, in rebind_kept_source_episode_ids_db_test.go, and registers it in db-contracts.txt.

  • Behavioral Change: kept source episodes lose their TMDB, TVDB, and IMDb IDs after a rebind

Macroscope summarized 51d45fc.

A rebind that keeps its source (a matched item with durable provider
IDs) moved the source's memberships, files and provider IDs, but its
episodes kept their TMDB, TVDB and IMDb IDs. Those are unique across
episodes, so the target's episodes could not store them and stayed
placeholders. The rebind now clears them on the source's episodes.

Part of Silo-Server#4613

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

When a rebind keeps the source series, the operation clears TMDB, TVDB, and IMDb IDs from its episodes. A PostgreSQL integration test checks that a target episode can then use those IDs.

Changes: Kept source rebind

Layer / File(s) Summary
Clear and verify episode IDs
internal/metadata/service.go, internal/metadata/rebind_kept_source_episode_ids_db_test.go, scripts/ci/db-contracts.txt
After attempting to delete the source item, rebindItemOnto clears external IDs from episodes still associated with the source. The PostgreSQL test checks that the source remains, its episode IDs are cleared, and a target episode can be inserted with those IDs. The CI database-contract list includes the test.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

  • Issue 4613 — The change clears episode IDs that block target episode upserts when a rebind keeps its source. It does not address orphan cleanup or the grace-period hold path.

Suggested reviewers: quick104, rhainland

Merge Risk: Minimal · up to 51d45

When a rebind keeps the source series, its episodes now release their TMDB, TVDB, and IMDb IDs so the target can reuse them. The change is small and covered by a database test. It does not clean up existing orphans, which the linked issue still tracks. No merge-blocking risk was found.

Security architecture risk: Minimal · up to 51d45

The change releases episode identifiers from the old series as part of the same operation that moves its library links and user state. The destination can then claim those identifiers on its next refresh. The reviewed change did not establish a new security issue or weaken ownership controls.

Security review details

Security Blast Radius

  • observed — The write can affect every episode with a nonempty external ID on the retained source series, not just episodes present on the destination. Its direct database scope is one source series per rebind, while the existing uniqueness rules apply across all episodes.

Trust Boundaries and Controls

  • observed — The new database write inherits the existing rebind authority. Explicit curation checks the items through the curation guard before mutation; automatic metadata processing continues to use its existing path without that explicit-curation check.

Resilience and Maintainability Implications

  • inferred — The database enforces at most one holder of each nonempty provider ID, but the clear does not reserve IDs for the destination. If an existing source writer can repopulate an ID before the destination refresh writes it, the previous conflict can recur. That writer ordering was not established end to end.
Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: a fix in the metadata package that clears external IDs (TMDB, TVDB, IMDb) from a kept rebind source's episodes. The raw summary shows the new step in rebindItemOnto th…
Description check Passed The description states the problem, the approach, the validation steps, and the evidence. It names the changed function (rebindItemOnto), the new test, and the db-contracts.txt entry. These match the …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: a fix in the metadata package that clears external IDs (TMDB, TVDB, IMDb) from a kept rebind source's episodes. The raw summary shows the new step in rebindItemOnto that does this, and the objectives describe the same change. The title is specific and concise.

Full details: Description check

Explanation

The description states the problem, the approach, the validation steps, and the evidence. It names the changed function (rebindItemOnto), the new test, and the db-contracts.txt entry. These match the raw summary and the objectives. It is not off-topic.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5651e13a-7a18-44b8-bb64-64dc980c5f59
Commits

Reviewing files that changed from the base of the PR and between dc85466 and 51d45fc.

Files selected for processing: 1

  • scripts/ci/db-contracts.txt

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 2 remain after this review.

@silo-triage silo-triage Bot added the size/M 50–199 added + deleted lines (all files); change volume, not priority. label Nov 30, 2026
@silo-kody

silo-kody Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Silo Kody — review complete

Review finished. Check the inline comments for findings and verify each suggestion against the code and tests.

Reviewing changes in Silo
  • Include the related issue, expected behavior, and validation steps in the PR description.
  • For API changes, describe the effect on Apple and Android clients and Jellyfin compatibility.
  • For plugin changes, identify the affected SDK contract, plugin, and catalog entry.
  • Follow this repository's AGENTS.md and CONTRIBUTING.md.
  • Request another review with @kody start-review in a PR comment.
  • React with 👍 or 👎 to give feedback on individual suggestions.
Review settings
Review Options

The following review options are enabled or disabled:

Options Enabled
Bug ✅
Performance ✅
Security ✅
Business Logic ❌

@silo-triage silo-triage Bot added bug Something isn't working go Pull requests that update go code priority: P2 Limited scope, workaround exists, or polish labels Nov 30, 2026
Process rebinds a source with durable provider IDs only while it reads
the source as provisional, so the rebind keeps a matched source only when
its status turned matched since. The comments described a steady state.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@Quick104 Quick104 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.

🤖 Claude Code (claude-opus-5-5)

Review of d3fdeff. One finding: the SQL step is correct, but the description of when it runs doesn't match the code. Details are inline. The summary comment covers verification and the open question.

Comment thread internal/metadata/service.go Outdated
args: []any{fromContentID, rebindDeletableStatuses(allowMatchedSource)},
},
{
// A source the delete keeps (a matched item with durable IDs) has

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.

Medium: the trigger this comment and the PR description give doesn't happen on a steady state.

Process only rebinds when isProvisionalOwnershipStatus(existingItem.Status) || len(durableIDs) == 0 (service.go:2898), and it passes allowMatchedSource = len(durableIDs) == 0:

  • A source with no durable IDs gets true, so a matched source is deleted.
  • A source with durable IDs reaches the rebind only if it was pending, unmatched or ambiguous when Process read it (service.go:2862). Those statuses are deletable.
  • The other callers (service.go:3557, :3678 and :9688, merge_items.go:102, canonicalize.go:112) all pass true.

So the delete keeps a matched source only when the source's status turned matched after Process read it. One way that can happen is a second pass on the same item under a different provider-ID dedup key: the dedup lock is in-memory and keyed by provider IDs. A matched item with durable IDs never enters the rebind from Process.

Impact: the step is still correct whenever the delete keeps a source, and the new test fails on the base for the stated reason. But the story in the comment and the PR body isn't how a deployment reaches this state, and #4613's 47 orphans may have come another way.

Repair: I'm rewording this comment and the test's comment to describe the real condition in a commit on this branch, so there's no need to change it yourself. Whether this explains #4613's orphans is left for the maintainers.

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.

🤖 Claude Code (claude-opus-5-5)

Reworded in dc85466, comments only. The step's comment and the test's comment now say a source is kept when its status turned matched after Process read it as provisional. go build, go vet and lint-changed are clean. I'm leaving this thread open: whether this race explains #4613's orphans is still for the maintainers to decide.

@silo-kody

silo-kody Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Silo Kody — review complete

Review finished. Check the inline comments for findings and verify each suggestion against the code and tests.

Reviewing changes in Silo
  • Include the related issue, expected behavior, and validation steps in the PR description.
  • For API changes, describe the effect on Apple and Android clients and Jellyfin compatibility.
  • For plugin changes, identify the affected SDK contract, plugin, and catalog entry.
  • Follow this repository's AGENTS.md and CONTRIBUTING.md.
  • Request another review with @kody start-review in a PR comment.
  • React with 👍 or 👎 to give feedback on individual suggestions.
Review settings
Review Options

The following review options are enabled or disabled:

Options Enabled
Bug ✅
Performance ✅
Security ✅
Business Logic ❌

@Quick104

Copy link
Copy Markdown
Contributor

🤖 Claude Code (claude-opus-5-5)

Review summary

Reviewed head: dc85466 (the PR's d3fdeff plus one comment-only commit from this review).

The new step is correct and safe. When the rebind keeps its source, it frees the IDs that the target's episodes need, and when the source is deleted it does nothing. What doesn't match the code is the trigger the PR described. Process keeps a matched source only when that source's status turns matched after Process read it as provisional, so the state comes from a race, not a steady path. The 47 orphans in #4613 may have come from that race or from another path; this review couldn't tell which.

Addressed

  • The step's code comment, the test's comment and the PR body's Problem section now describe the real condition (dc85466, comments and description only).

Left for the maintainers

Verification

  • TestRebindKeptSourceReleasesEpisodeIDsPostgres failed on the base (bc17dc0) for the reported reason: the kept source's episode still held all three IDs. It passes on the head. Both runs used a throwaway Postgres 18.
  • Not reproduced live. The kept-source state needs two concurrent passes on one item, which a sandbox can't stage on demand.
  • Considered and not counted against this PR:

Evidence: https://evidence.siloserver.org/r/silo-server/pr-5631-review/

Validated features: no passed case on the v1.0 board changes. The Identify and duplicate-series cases (#1210 and #1211, C1 and C2) reach rebindItemOnto, but none of them checks the kept source's episode IDs. Validation tasks: none is correct.

User manual: unaffected; this is internal catalog repair.
Other clients: N/A. Clients see the real episode titles after the target's next refresh, and none of them needs a change. Jellyfin-compat needs no change either.

Council (round 1, on d3fdeff):

Member Round 1
Sol (gpt-6.2-sol, medium) 4/10
Opus (claude-opus-5-5, medium) 7/10
Gemini (gemini-4-neon, medium) 8/10

All three members independently flagged the trigger mismatch, and that is now fixed in the comments and body. Sol's low score rested mainly on that mismatch and on the two races above. The follow-up commit changes only comments, so no second round ran.

Fit: 9/10 · Readiness: 7/10 — conditional

@coderabbitai coderabbitai Bot 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.

Architecture findings (1)

Advisory architecture findings (1)

  • Trivial — The rebind commits the release of every set provider ID on the retained source's episodes without reserving those IDs for the target. If processing stops after commit, or the target episode write fails, the release is not rolled back. A later successful refresh can repopulate IDs it receives, but the rebind itself does not ensure that recovery.
Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f522b5e2-1fa5-4f88-a7a5-0140d819a784
Commits

Reviewing files that changed from the base of the PR and between bc17dc0 and dc85466.

Files selected for processing: 3

  • internal/metadata/rebind_kept_source_episode_ids_db_test.go
  • internal/metadata/service.go
  • scripts/ci/db-contracts.txt

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 0 remain after this review.

…source-episode-ids

# Conflicts:
#	scripts/ci/db-contracts.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working go Pull requests that update go code priority: P2 Limited scope, workaround exists, or polish size/M 50–199 added + deleted lines (all files); change volume, not priority.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants