Skip to content

fix(userstore): record a completion in the same second as a partial stop - #5403

Open
Rhainland wants to merge 1 commit into
Silo-Server:mainfrom
Rhainland:fix/5366-same-second-completion-history
Open

Rhainland wants to merge 1 commit into
Silo-Server:mainfrom
Rhainland:fix/5366-same-second-completion-history

Conversation

@Rhainland

@Rhainland Rhainland commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Closes #5366
Related issue: N/A
Validation tasks: none (related cases not yet run: #1238 C1 and C2, #1241 C3)

Other clients: N/A (no client-visible change)

When a webhook delivered a partial stop and then a played mark for the same item within one whole second, Silo marked the item played but recorded no completed play, so its play count was one short. Silo skips an imported watch when history already holds a play of that item in the same second, so a replayed import isn't counted twice. That check didn't look at completion, so the partial stop hid the completion. Webhook sync, watch-provider imports and history imports all go through this check.

This change lets a completion through when the only play in its second is partial. A replay of the same import is still skipped.

Approach

  • On both user stores (Postgres and SQLite), a partial play no longer counts as a duplicate of a completion in the same second. A completed play still matches either kind, and a partial import still matches any play in its second. The partial play stays in history next to the completion, the same way native playback keeps one row per stop.
  • The guard against provider copies of scrobbled plays ([bug] Watch-provider import counts each scrobbled local play twice #3433) works through scrobble sessions in the watch-provider import, so this change doesn't affect it.
  • A shared conformance test runs against both stores and is listed in scripts/ci/db-contracts.txt.

Validation

  • New conformance test fails on main and passes on this branch, on SQLite and Postgres.
  • go test for userdb, userstore, watchstate, webhooksync, watchsync, historyimport and notifications against a migrated Postgres: pass.
  • The DB contracts runner passes for the new contract.
  • go build ./..., gofmt, go mod tidy -diff, go vet, make lint-changed, make verify-local-paths, make verify-size-ratchet: clean.
  • make test-go: pass. Some backup, upload, playback and handler tests fail under a group-writable temp directory, which is a property of the test host. They pass with umask 077 and don't touch this change.
  • Local server on Postgres before and after, with a Jellyfin webhook connection and a synthetic movie: see Evidence.

Evidence

Local build on Postgres with a Jellyfin webhook connection mapped to the admin profile. A PlaybackStop at 120 s (timestamp …:SS.1Z) was sent, then a UserDataSaved TogglePlayed with played: true (timestamp …:SS.6Z). Then the item and its history rows were read.

Before, main at 0ad67a01e:

GET /api/v2/catalog/items/movie-tmdb-900001  -> {"played":true}
user_watch_history:
 2026-11-26 10:10:07+00 | completed f | import

After, this branch, same database:

GET /api/v2/catalog/items/movie-tmdb-900001  -> {"played":true}
user_watch_history:
 2026-11-26 10:20:09+00 | completed f | import
 2026-11-26 10:20:09+00 | completed t | import

Sending the same two deliveries again added no rows.

Risks

A partial and a completed play of one item can now share a whole second in history. Backup restore treats plays in the same second as one play and keeps the first it reads, so restoring such a pair can keep only the partial row. Native playback could already produce same-second rows, so restore already behaved this way; this change makes the case more common for imports. Restore is left unchanged here.

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 (via T3 Code); Codex CLI for code review
  • Tool(s): Claude Code, Codex CLI, GitHub CLI, curl, Docker, ffmpeg
  • Model(s): claude-opus-5-5; gpt-6.2-sol (code review)
  • Involvement: Fully AI-generated, human verified
  • Adversarial review: dual-code-review at medium effort (gpt-6.2-sol through Codex CLI, and a separate claude-opus-5-5 reviewer), on correctness, standards and spec. Opus found nothing. Sol raised two points. First, backup restore keeps only the first play in a second, so it can drop the new completed row. This was checked and is real, but the rule predates this change, so it is noted under Risks and not fixed here. Second, the partial-replay check ran only after a completed row existed. The test now replays the partial stop right after inserting it. Lint findings (a repeated string, a spelling) were fixed.

Note

Fix userstore to record completions in the same second as a partial stop

  • Changes the same-second duplicate predicate in userdb.AddHistoryIfMissing (progress.go) and pgstore.PostgresUserStore.AddHistoryIfMissing (progress.go) so an incoming completion is accepted when matching rows in that second are only partial.
  • Existing completed rows still block same-second imports, and incoming partial rows are still blocked by any matching row at that timestamp.
  • Adds a shared conformance test RunImportedCompletionAfterSameSecondPartial (imported_history.go) covering partial/completion ordering and duplicate-import rejection, run for both SQLite and Postgres stores.
  • Registers the Postgres test in the database contract manifest (db-contracts.txt).
  • Behavioral Change: same-second imports that were previously rejected as duplicates are now accepted when the stored row is partial and the import is a completion.

Macroscope summarized 4198870.

An imported watch is skipped when history already has a row for the item in
the same second, so a replay is not counted twice. That check ignored
completion, so a partial stop earlier in the second kept the completion out:
the item was marked played but history had no completed row.

A partial row no longer hides a completion in its second. A completed row
still hides either kind, and a replayed partial still matches any row. The
Postgres and SQLite stores share a conformance test for this.
@silo-kody

silo-kody Bot commented Nov 26, 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/M 50–199 added + deleted lines (all files); change volume, not priority. label Nov 26, 2026
@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Walkthrough

Same-second imported-history checks now allow a completion to be recorded after a partial row. Shared conformance tests verify both insertion orders and replay handling against SQLite and PostgreSQL.

Changes: Same-second imported history

Layer / File(s) Summary
Same-second duplicate matching
internal/userdb/progress.go, internal/userstore/pgstore/progress.go
Both stores allow an incoming completion when an incomplete history row exists at the same timestamp. Comments describe the exception.
Imported history conformance
internal/userstore/storetest/imported_history.go, internal/userdb/conformance_test.go, internal/userstore/pgstore/conformance_test.go, scripts/ci/db-contracts.txt
The shared test checks partial-then-completion insertion, rejects replays, and checks that a partial row is rejected when a completion already exists. SQLite and PostgreSQL run the test; the PostgreSQL contract list includes its wrapper.

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

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

Possibly related PRs

Suggested reviewers: zzebrahz, blurbery, quick104

Merge Risk: Moderate · up to 41988

Same-second partial and completed plays are now both recorded, which fixes the missing completed history row. Restoring a backup that contains both rows for the same second can keep only the partial one, so the completed play count can come out short. This is a narrow case that the author has acknowledged. Fix restore, or explicitly accept the gap, before merging.

Security architecture risk: Low · up to 41988

The fix records a completed play that was previously missed. A selective restore can still discard that completion and leave the restored play count one short. The risk is limited to the affected watch-history records; the change does not expand who can write them.

Retained concerns

  • Low · reliability · inferred: Selective PostgreSQL watch-history restore can silently lose a completion that this PR now persists. On an empty target, the decoder keeps the first record per item and second, so a partial ordered first displaces the completion. On a merge target, an existing partial also blocks the incoming completion. The restore logic is unchanged, but this import path can now produce the affected pair.
Security review details

Security Blast Radius

  • observed — The newly accepted insert remains within the selected user's store and the same profile and media item. It changes which row is recorded, not which user's history the import can reach.

Trust Boundaries and Controls

  • observed — The history-hide cutoff still applies before insertion. SQLite keeps the cutoff check, duplicate check, and insert in one write statement; PostgreSQL retains the transaction-scoped user/profile lock shared with history resets and restore.

Hardening Proposals

  • proposed — Align selective-restore decoding, preview, and merge with an explicit completion-preservation rule. Test both a round trip to an empty profile and a merge into a profile that already contains the partial, including replay of the restore.
Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: a completion recorded in the same second as a partial stop. It matches the summary of the changes in progress.go, the Postgres and SQLite stores, and the new conforman…
Description check Passed The description explains the bug, the cause (the same-second duplicate check ignored completion state), the fix for both user stores, the new conformance test, validation steps, and a restore risk. It…
Linked Issues check Passed Issue #5366 requires a completed history row when a partial stop and a completion for the same item and profile fall in the same whole second. The reviewed head changes the same-second duplicate check…
Out of Scope Changes check Passed All changed files serve the same-second import check that #5366 names. The changes are: the duplicate predicate in internal/userdb/progress.go and internal/userstore/pgstore/progress.go, with updated …
Full details: Title check

Explanation

The title names the main change: a completion recorded in the same second as a partial stop. It matches the summary of the changes in progress.go, the Postgres and SQLite stores, and the new conformance test. It uses the conventional fix(scope) prefix and is specific.

Full details: Description check

Explanation

The description explains the bug, the cause (the same-second duplicate check ignored completion state), the fix for both user stores, the new conformance test, validation steps, and a restore risk. It relates directly to the changeset.

Full details: Linked Issues check

Explanation

Issue #5366 requires a completed history row when a partial stop and a completion for the same item and profile fall in the same whole second. The reviewed head changes the same-second duplicate check in both stores. In internal/userdb/progress.go, AddHistoryIfMissing treats a same-timestamp row as a duplicate only when the existing row is completed or the incoming row is incomplete. In internal/userstore/pgstore/progress.go, the duplicate check ignores same-second incomplete rows when the incoming entry is completed. Under both rules, an incoming completion is inserted beside an existing partial row. A replayed partial or completion is still rejected. internal/userstore/storetest/imported_history.go adds RunImportedCompletionAfterSameSecondPartial. It asserts two retained rows for partial-then-completion. It also asserts that replays of either are rejected, and that a partial arriving after a completion in the same second is rejected. The Postgres and SQLite wrappers run this contract, and the contract is listed in scripts/ci/db-contracts.txt. The PR also reports a manual before/after run on Postgres through the webhook path. The Postgres result shows a completed row added and the replay adding no rows. This evidence comes from the PR description and the change summary. I did not re-run the tests. Scope limit: the restore behavior noted in Risks is outside #5366 and is not required here.

Full details: Out of Scope Changes check

Explanation

All changed files serve the same-second import check that #5366 names. The changes are: the duplicate predicate in internal/userdb/progress.go and internal/userstore/pgstore/progress.go, with updated comments and argument wiring; a new conformance test in each store's test file; the shared storetest contract; and one line in scripts/ci/db-contracts.txt that registers the new contract. These support the behavior change and its verification. The PR states that the scrobble-session guard for #3433 runs through a separate path in the watch-provider import and is not changed. The summary does not show that path, so that claim is not verified here. The backup restore same-second behavior is left unchanged and is documented as a risk. No unrelated behavior change appears in the summary.

Finishing Touches

Generate unit tests (beta)

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

@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 26, 2026

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

Actionable comments posted: 1

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: a9b2bfa5-26e2-4fc2-b0b4-38228294990f
Commits

Reviewing files that changed from the base of the PR and between 0ad67a0 and 4198870.

Files selected for processing: 6

  • internal/userdb/conformance_test.go
  • internal/userdb/progress.go
  • internal/userstore/pgstore/conformance_test.go
  • internal/userstore/pgstore/progress.go
  • internal/userstore/storetest/imported_history.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.

Comment thread internal/userdb/progress.go Resolved
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.

[bug] A completion in the same second as an earlier partial stop adds no completed history row

1 participant