Skip to content

fix(scanner): scope file reconciliation to changed path - #741

Merged
Quick104 merged 2 commits into
mainfrom
t3code/investigate-database-load
Aug 24, 2026
Merged

Quick104 merged 2 commits into
mainfrom
t3code/investigate-database-load

Conversation

@Quick104

@Quick104 Quick104 commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Related issue: N/A — narrow fix

A file-scoped Autoscan request ended by calling syncPresentLibraryState, which reconciles every catalog membership in the media folder. On a large production library, the plans scanned most media_files rows and millions of episodes rows for each changed file. Bursts of subtitle sidecar updates therefore kept PostgreSQL busy with folder-wide work even though each request named one path.

Approach

Both scopes now share one implementation, syncPresentState(ctx, folderID, filePath *string): syncPresentLibraryState and syncPresentFileState are thin wrappers, and the exact-path predicate is appended in Go so the folder-wide plan is unchanged. The repair performs, in a single transaction:

  • clear dangling content links (episode links have an ON DELETE SET NULL FK, so that statement is defensive);
  • restore media-item membership;
  • restore episode membership, aggregating first_seen_at over all of the episode's active files rather than just the scanned path;
  • bump the parent series' latest_episode_added_at only for genuinely new links.

The transaction replaces four autocommit round-trips, so a crash mid-repair can no longer leave a row with its links cleared but its memberships unrestored.

The extras-conversion branch no longer calls the sync at all: the Upsert that converts a row into an extra already nulls its content/episode links, which made every statement of the sync a no-op there. The membership cleanup on that path comes from the unchanged folder-wide reconcileLibraryMemberships call. (An earlier revision of this PR kept a file-scoped sync call there and described it as retaining cleanup; review showed it was dead code.)

Validation

Passed:

  • go test ./internal/scanner ./internal/autoscan ./internal/libraryingest -count=1
  • go test ./internal/scanner -count=1 with SILO_TEST_DATABASE_URL against a local migrated Postgres — the DB-backed regression tests executed (not skipped), covering the file scope, the folder-wide scope through the same unified body, and dangling content-link repair in both scopes
  • go build ./...
  • go vet ./...
  • gofmt -l . (no output)
  • pnpm install --frozen-lockfile
  • pnpm run lint (0 errors; 155 existing warnings)
  • pnpm run format:check
  • pnpm run build (existing font and chunk-size warnings)
  • make test-web (283 files, 2,039 tests)
  • make verify-settings-bindings-all
  • make verify-playback-fixtures
  • make verify-local-paths

make test-go ran the full suite. All scanner tests passed, but the command failed on two unrelated macOS Jellycompat process-lock tests:

  • TestBeginWebOperationRecoversDeadProcessLock
  • TestBeginWebOperationRejectsLiveProcessLock

The same path was exercised on shared-dev through a real file scan: the scan completed in 2.07 seconds, and EXPLAIN ANALYZE used the exact-path and active-episode indexes with 3.19 ms execution time and no broad sequential scan. During CephFS batches of 33 and 37 file scans, PostgreSQL sampled at 0.67–2.23% CPU; health and readiness remained green.

golangci-lint run --new-from-merge-base="origin/main" ./... was not run because golangci-lint is not installed in this environment.

Risks

A single-file scan no longer repairs unrelated stale memberships in the same folder. Full folder scans retain that cleanup, and the extra-conversion path still runs reconcileLibraryMemberships. There are no migrations or API changes.

Pre-existing and unchanged by this PR: the plain single-file scan path never deletes a stale membership left behind when a file is re-identified from one content ID to another (neither sync variant issues DELETEs; that cleanup belongs to reconcileLibraryMemberships, which the plain path does not call). Worth a follow-up issue.

AI Disclosure

  • Tool(s): Codex; Claude Code
  • Model(s): gpt-5.6-sol; Claude Fable 5 (orchestration) with Opus 5 implementation subagents
  • Involvement: AI-assisted
  • Adversarial review: A multi-agent Claude review of the merge-base diff (8 finder angles, adversarial verification votes) found the dead extras-branch sync call, the duplicated SQL bodies, and the non-transactional repair sequence; this revision addresses those findings. The DB-backed regression tests were executed against a real migrated Postgres as part of validating the revision.

Checklist

  • I read and can explain the complete diff.
  • This pull request addresses one concern.

@coderabbitai

coderabbitai Bot commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 121a0ee6-103a-43f3-a6ae-db38a9462e5f

📥 Commits

Reviewing files that changed from the base of the PR and between 820eef7 and a7b59dc.

📒 Files selected for processing (2)
  • internal/scanner/present_file_state_test.go
  • internal/scanner/scanner.go

Included review availability: 5 reviews are currently available. Your included PR review attempts over the past 7 days set your current allowance at 6 reviews per hour.


📝 Walkthrough

Walkthrough

Single-file scans now repair catalog memberships for only the affected media file. The change also restores episode links and timestamps. A database-backed integration test verifies target restoration, unrelated-item isolation, and timestamp updates.

Changes

Present-file reconciliation

Layer / File(s) Summary
Targeted file-state synchronization
internal/scanner/scanner.go
syncPresentFileState clears dangling links, restores memberships for one file, and updates the parent series timestamp for newly restored episode memberships.
Single-file scan wiring and validation
internal/scanner/scanner.go, internal/scanner/present_file_state_test.go
Single-file extra and primary scan paths use targeted synchronization. The integration test verifies membership isolation and episode timestamp restoration.

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

Merge Risk: ⚪ Minimal · up to a7b59

The change limits file-scoped reconciliation to the changed path while preserving per-file repairs and existing folder-wide cleanup where required; no actionable merge-blocking risk remains after normal checks and review.

Suggested reviewers: rxwatcher

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed Docstring check was indeterminate for this PR — some files could not be analyzed in time. Not blocking.
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.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: limiting scanner reconciliation to the changed file path.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch t3code/investigate-database-load

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

Review follow-ups for the file-scoped reconciliation:

- Collapse syncPresentLibraryState and syncPresentFileState into one
  syncPresentState implementation with an optional exact-path scope, so the
  two variants cannot drift. The folder-wide entry point emits the same SQL
  as before; the episode-membership statement is unified on the sibling-join
  shape so first_seen_at always aggregates over all of an episode's active
  files.
- Run the four repair statements in a single transaction instead of four
  autocommit round-trips, so a crash mid-repair cannot leave a row with its
  links cleared but its memberships unrestored.
- Remove the syncPresentFileState call in the extras-conversion branch: the
  preceding Upsert already nulls the row's content/episode links, making
  every statement a no-op there. The membership cleanup at that site comes
  from the unchanged reconcileLibraryMemberships call.
- Drop the redundant COALESCE inside GREATEST in the series denorm bump.
- Extend the regression test to drive both scopes through the unified body,
  including dangling content link repair.

Co-Authored-By: Claude Fable 5 <[email protected]>
@Quick104
Quick104 merged commit 970ebf7 into main Aug 24, 2026
4 checks passed
@Quick104
Quick104 deleted the t3code/investigate-database-load branch August 24, 2026 15:02
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