Skip to content

fix(jellycompat): stop UserData duplicating watch history - #2892

Merged
Quick104 merged 2 commits into
Silo-Server:mainfrom
7487:fix/issue-2886-bug-jellyfin-compat-post-userdata-with-p
Oct 24, 2026
Merged

Quick104 merged 2 commits into
Silo-Server:mainfrom
7487:fix/issue-2886-bug-jellyfin-compat-post-userdata-with-p

Conversation

@7487

@7487 7487 commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Closes #2886
Related issue: N/A
Validation tasks: none

Some Jellyfin-compatible clients, CrossWatch among them, mark a movie or episode played and then save the same state through UserData. Silo treated every leaf UserData write with Played: true as a new play, so one play could appear twice in watch history, and a repeated UserData post added another row each time. Watched state looked right, but anything counting history rows (statistics, watch-provider export) counted those plays twice.

Approach

A leaf UserData played mark now follows the rule the mark-played route already has: a mark is idempotent per leaf. A leaf that is already completed gets no new history row, and an unmark resets it so the next mark records a play.

RecordJellycompatProgress still passes the history entry for Played: true. ApplyJellycompatProgress in both stores adds it through markWatchedBatchTx, the conditional progress write that series and season marks use, inside the same transaction and before the explicit position, date, and favorite are written. The decision is made atomically in the database, so two concurrent posts record one play. A client that marks played only through UserData still records the play, so history views and watch-provider export keep seeing it. Explicit Played: false and the completion notification behave as before.

Validation

  • TestPlayedMutationsReturnCurrentDTOAndDate replays the [bug] Jellyfin-compat POST UserData with Played: true adds a watch-history row every time, even when the item is already played #2886 sequence (UserPlayedItems, then UserData with Played: true and the same date) through the compatibility handlers. It fails on main with two rows and passes with one.
  • TestUserDataZeroDateUsesCurrentTime checks that a UserData-only mark records one play with the progress date. It failed against an earlier revision of this branch that dropped UserData history altogether.
  • The shared store contract RunAtomicJellycompatProgress checks that a repeated played edit adds no row, that a failed played edit rolls back its history insert, and that a play after unmark records again. It runs on SQLite (internal/userdb) and, through the new TestPostgresAtomicJellycompatProgress, on a migrated Postgres 18 database; it fails on main's store code for both and passes with the fix. The Postgres test is listed in scripts/ci/db-contracts.txt.
  • Focused packages pass: internal/jellycompat, internal/notifications, internal/userdb, internal/userstore/..., internal/watchstate. make lint-changed reports no issues.
  • Not run: a live sandbox check over HTTP. The test host had no free container network for a new sandbox.

Evidence

Surface: Jellyfin-compatible movie and episode UserData persistence. No Silo UI or client code changes. The v1 GET /api/v1/history response already collapses repeated rows for one item, so it's the same before and after. What changes is the persisted history that row-count and export consumers read.

The #2886 sequence (POST /UserPlayedItems/{id}?datePlayed=2025-02-03T04:05:06Z, then POST /UserItems/{id}/UserData with {"Played":true,"PlaybackPositionTicks":0,"LastPlayedDate":"2025-02-03T04:05:06Z"}) through the compatibility handlers, on main 1ec456e:

--- FAIL: TestPlayedMutationsReturnCurrentDTOAndDate (0.01s)
    viewer_state_test.go:130: user data duplicated played history: status=200 history=[{... MediaItemID:movie-1 WatchedAt:2025-02-03T04:05:06Z ... Source:jellycompat ...} {... MediaItemID:movie-1 WatchedAt:2025-02-03T04:05:06Z ... Source:jellycompat ...}] error=<nil>

A UserData-only played mark, on this branch's first revision 3f7f831:

--- FAIL: TestUserDataZeroDateUsesCurrentTime (0.00s)
    viewer_state_test.go:450: history and progress dates disagree: [] <nil>

The store contract on Postgres 18 with main's store code:

--- FAIL: TestPostgresAtomicJellycompatProgress (0.28s)
    conformance_test.go:81: history [{... MediaItemID:atomic-item WatchedAt:2024-01-02T03:04:05Z ...} {...}]

At 27924ac, all three pass:

ok  github.com/Silo-Server/silo-server/internal/jellycompat
ok  github.com/Silo-Server/silo-server/internal/userdb
--- PASS: TestPostgresAtomicJellycompatProgress (0.10s)

Risks

  • Existing duplicate history rows are not rewritten; no migration is required.
  • Series and season UserData keep their existing batch-mark behavior.
  • No authentication, authorization, schema, or API contract changes.

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 says there is none.

AI Disclosure

  • Harness: Codex desktop (original change); Claude Code in T3 Code (maintainer review fixes)
  • Tool(s): Codex, Claude Code, Go toolchain, pnpm, GitHub CLI
  • Model(s): GPT-5 (original change); claude-opus-5-5 (review fixes); gpt-6.1-sol (cross-provider review)
  • Involvement: Fully AI-generated, pending human and maintainer review
  • Adversarial review: an independent Codex reviewer inspected the original diff and its callers; two empty rollback assertions it found were fixed. A maintainer review then found that the original approach dropped history for clients that mark played only through UserData, and replaced it with the idempotent store-level mark described above, after a cross-provider review (gpt-6.1-sol) confirmed the finding and the repair.

🤖 Generated with Claude Code

@silo-kody

silo-kody Bot commented Oct 22, 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 the size/S 0–49 added + deleted lines (all files); change volume, not priority. label Oct 22, 2026
@coderabbitai

coderabbitai Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

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: e42c7d06-05f0-480b-bcfc-b808a0e4ed8f
Commits

Reviewing files that changed from the base of the PR and between 3f7f831 and 27924ac.

Files selected for processing: 7
  • internal/userdb/jellycompat_progress.go
  • internal/userstore/jellycompat_progress.go
  • internal/userstore/pgstore/conformance_test.go
  • internal/userstore/pgstore/jellycompat_progress.go
  • internal/userstore/storetest/jellycompat_progress.go
  • internal/watchstate/service.go
  • scripts/ci/db-contracts.txt
Files skipped from review as they are similar to previous changes: 1
  • internal/watchstate/service.go

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

Walkthrough

Jellycompat progress updates now route history entries through the watched-marking path. Tests verify that repeated played edits do not add history, date-only edits leave history unchanged, and failed edits preserve prior progress and history.

Changes: Jellycompat progress updates

Layer / File(s) Summary
Route played history through watched marking
internal/userstore/jellycompat_progress.go, internal/watchstate/service.go, internal/userdb/jellycompat_progress.go, internal/userstore/pgstore/jellycompat_progress.go
Both storage implementations pass history entries and watched targets to markWatchedBatchTx. Comments describe history creation for explicit played marks on leaves that are not already completed.
Verify progress and history behavior
internal/userstore/storetest/jellycompat_progress.go, internal/jellycompat/viewer_state_test.go, internal/userstore/pgstore/conformance_test.go, scripts/ci/db-contracts.txt
Tests check repeated played edits, date-only and unplayed edits, and rollback after failed writes. A Postgres conformance test injects a progress-write failure and is added to the database-contract test list.

Priority: Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

Suggested reviewers: quick104

Merge Risk: Minimal · up to 27924

First-time UserData plays retain their history entry, while repeated played updates do not duplicate it. No issue remains that requires a fix before merge.

Security architecture risk: Minimal · up to 27924

The change prevents duplicate history by reusing existing watched-state transactions. The reviewed changes preserve account and profile scoping, atomic updates, and notification ordering without adding access or permissions.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The changed mutation remains scoped to the selected account, profile, and media item. The inspected change affects history eligibility and associated progress or favorite state, rather than expanding authority to other accounts or services.

Trust Boundaries and Controls

  • observed — The newly reused database helpers gate history insertion on identifiers returned by conditional progress updates, within the same transaction. This avoids a separate application-level eligibility check followed by an unconditional history write.

Resilience and Maintainability Implications

  • observed — Errors before commit roll back the leaf mutation. Completion callbacks and interest-tracking updates occur only after successful writer return. Repeated successful completion writes can still notify downstream observers even when no history is added; that notification behavior predates this PR.
Pre-merge checks: Passed 3 · Failed 2

Failed Failed checks: 2 warnings

Check name Status Explanation Resolution
Linked Issues check Warning Issue #2886 explicitly requires a UserData post to set user data without recording a new play. The reviewed store code routes edit.History through markWatchedBatchTx in both SQLite and Postgres.… Change the Jellyfin-compatible UserData path so Played: true updates progress without inserting history. Keep history creation on the explicit UserPlayedItems mark-played path. Update the UserData-only and atomic store tests to verify n…
Docstring Coverage Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 9 functions across 10 files. (1 skipped: … Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 3 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the primary change: preventing duplicate watch-history entries from Jellyfin-compatible UserData writes.
Description check Passed The description directly explains the duplicate-history problem, the idempotent store-level fix, affected behavior, validation, risks, and test coverage.
Out of Scope Changes check Passed The changes stay on the Jellyfin-compatible progress surface. The SQLite and Postgres store changes implement idempotent played-state handling and transaction rollback. The added tests and CI contract…
Full details: Out of Scope Changes check

Explanation

The changes stay on the Jellyfin-compatible progress surface. The SQLite and Postgres store changes implement idempotent played-state handling and transaction rollback. The added tests and CI contract registration verify this behavior. The supplied change summary shows no unrelated UI, client, authentication, schema, or API changes.

Full details: Linked Issues check

Explanation

Issue #2886 explicitly requires a UserData post to set user data without recording a new play. The reviewed store code routes edit.History through markWatchedBatchTx in both SQLite and Postgres. The supplied PR evidence states that RecordJellycompatProgress still supplies this history entry for Played: true. Therefore, a first-time UserData-only played mark records a history row. The repeated mark is idempotent, and the CrossWatch sequence produces one row, but the first-time UserData behavior does not meet the issue's stated requirement.

Resolution

Change the Jellyfin-compatible UserData path so Played: true updates progress without inserting history. Keep history creation on the explicit UserPlayedItems mark-played path. Update the UserData-only and atomic store tests to verify no history row from UserData, while retaining the duplicate-prevention regression test.

Full details: Docstring Coverage

Explanation

Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 9 functions across 10 files. (1 skipped: 1 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@silo-triage silo-triage Bot added bug Something isn't working evidence-required Needs before-and-after evidence before the Evidence check passes go Pull requests that update go code impact: compat Breaks a supported client or server path priority: P2 Limited scope, workaround exists, or polish labels Oct 22, 2026
@silo-triage

silo-triage Bot commented Oct 22, 2026

Copy link
Copy Markdown

The Evidence table describes the change from two history rows to one, but includes no captures showing it. Please add before-and-after captures of the affected client's watch history using the same data, and identify the client and build or commit for each capture. The handler-test summary alone does not show the visible history change required by CONTRIBUTING.md.

@macroscopeapp

macroscopeapp Bot commented Oct 22, 2026

Copy link
Copy Markdown

Jellycompat UserData no longer adds a duplicate movie or episode card to the v1 GET /history response after UserPlayedItems (internal/watchstate/service.go); the Evidence table reports stored-row counts but lacks before-and-after response excerpts for the same request. Please add both response excerpts; the surface and base/head commits are already named.

Automated check: Macroscope check run agent (gpt-6-luna). Evidence was not reviewed for correctness.

Posted via Macroscope — Visible change evidence

@7487

7487 commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Updated the Evidence section with same-data captures from the CrossWatch 0.13.6 request sequence on base 3bc9e58c and head 3f7f831: persisted history changes from two rows to one. I also included the actual v1 GET /api/v1/history?limit=10 response from both commits. It is unchanged at one card because that renderer already collapses repeated rows for a display item; the affected observable is persisted row counts, matching #2886.

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

Review of 3f7f831. One finding below, which I'm fixing in a commit on this branch, so there's no need to change it yourself. The branch also conflicts with main after #3013 renamed ClearHistory to ClearWatched; I'll rebase it as part of the fix.

Comment thread internal/watchstate/service.go Resolved
7487 and others added 2 commits October 24, 2026 04:14
Instead of dropping history for every leaf UserData played mark, add it
only when the leaf is not already completed, the rule the mark-played
path follows. The store decides inside the transaction with the same
conditional progress write markWatchedBatchTx uses. A client that marks
played only through UserData still records the play, and a repeat after
UserPlayedItems or another UserData post adds no row.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Quick104
Quick104 force-pushed the fix/issue-2886-bug-jellyfin-compat-post-userdata-with-p branch from 3f7f831 to 27924ac Compare October 24, 2026 08:22
@silo-triage silo-triage Bot added size/M 50–199 added + deleted lines (all files); change volume, not priority. and removed size/S 0–49 added + deleted lines (all files); change volume, not priority. labels Oct 24, 2026
@silo-kody

silo-kody Bot commented Oct 24, 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

Maintainer review summary

Reviewed head: 27924ac (rebased onto main; the branch history was rewritten).

Addressed

Remaining: nothing.

Tests that failed on the base

  • TestPlayedMutationsReturnCurrentDTOAndDate fails on main (two rows).
  • TestUserDataZeroDateUsesCurrentTime fails on 3f7f831 (no row from a UserData-only mark).
  • RunAtomicJellycompatProgress fails on main's store code for SQLite and for Postgres (the new TestPostgresAtomicJellycompatProgress, listed in scripts/ci/db-contracts.txt).

No live HTTP check on a sandbox. The handler test replays the client's request sequence, and the store contract covers both databases.

Validated features: no passed case is in reach. The Jellyfin compatibility task (#1246) has no case run yet.

User manual: unaffected. The watch-history page doesn't describe how compatibility clients' played marks are counted.

Apple/Android parity: not applicable. This is a server-only fix to the Jellyfin-compatible surface, with no native API change.

Cross-provider review: one round (gpt-6.1-sol) confirmed the finding and suggested deciding inside the store transaction, which the fix does.

Fit: 9/10 · Readiness: 9/10 — ready (assuming CI is green)

  • Fit: it fixes double-counted plays from a common sync pattern on the Jellyfin-compatible surface, and it reuses the existing mark rule instead of adding a second one.
  • Readiness: the fix is small and atomic in both stores, and every behavior it changes has a test that failed before it.

@macroscopeapp

macroscopeapp Bot commented Oct 24, 2026

Copy link
Copy Markdown

Idempotent writes in internal/userdb/jellycompat_progress.go change the raw history rows consumed by paginated v2 history (internal/apiv2/history.go). That endpoint bounds pages by raw rows before filtering duplicate display cards, so removing rows can shift a same-limit page’s items or cursor. The current Evidence section says no user-visible change and provides no v2 comparison; please add before-and-after GET /api/v2/history response excerpts for the same data and query, identifying the base and head commits.

Suggested fix: No diff.

Automated check: Macroscope check run agent (gpt-6-luna). Evidence was not reviewed for correctness.

Posted via Macroscope — Visible change evidence

@macroscopeapp

This comment has been minimized.

@macroscopeapp

This comment has been minimized.

@Quick104
Quick104 merged commit dfc6503 into Silo-Server:main Oct 24, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working evidence-required Needs before-and-after evidence before the Evidence check passes go Pull requests that update go code impact: compat Breaks a supported client or server path 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.

[bug] Jellyfin-compat POST UserData with Played: true adds a watch-history row every time, even when the item is already played

2 participants