fix(425): address adversarial review — hide owned artifacts from schedule picker
Build ErsatzTV Image / CI image pin matches docker/ci (pull_request) Successful in 6s
Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 10s
Build ErsatzTV Image / decisions.md append-only (pull_request) Successful in 21s
Build ErsatzTV Image / API docs in sync (OpenAPI + endpoint index) (pull_request) Successful in 13m34s
Build ErsatzTV Image / Functional E2E (curl contracts) (pull_request) Successful in 15m21s
Build ErsatzTV Image / Formatting (changed .cs conform to .editorconfig) (pull_request) Successful in 16m19s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 19m36s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 19m37s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped

Independent review found a blocker + two mediums:
- BLOCKER: SearchSmartCollections/SearchMultiCollections (the scheduling
  collection picker) didn't filter OwnedByChannelId, so a user could select a
  system-owned auto-tune artifact into their own schedule item — a later channel
  delete would then cascade-delete that schedule item (ProgramScheduleItem->
  MultiCollection is OnDelete(Cascade)). Both handlers now exclude owned rows;
  regression test added (CollectionPickerHidesOwnedTests).
- MEDIUM: TryStampOwnership now runs on CancellationToken.None (post-commit
  convention #254) and stamps the MC + member SmartCollections in one
  transaction, so a late cancel or mid-way failure can't leave a permanent
  orphan / list leak.
- Documented the movie-remainder weight clamp bound (>1000 un-touched movies
  can't express the exact ratio — the 1..1000 weight-column bound from #70).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-18 03:52:06 +02:00
co-authored by Claude Opus 4.8
parent e364b338e6
commit 3bca000e27
4 changed files with 83 additions and 6 deletions
@@ -328,6 +328,11 @@ public class CreateAutoTunedChannelsHandler(
}
}
// Remainder weight = the un-touched base movie count, so a weighted movie airs N× *each* remainder
// movie (the fake path already pools movies uniformly, so this is equivalent to materializing each).
// Clamped to MultiCollectionItemWeight.Maximum (1000): a genre with >1000 un-touched movies can't
// express the exact ratio (the weighted movie then airs slightly more than intended) — the same
// 1..1000 bound #70's weight column imposes everywhere. Realistic only at very large scale.
int remainderCount = baseIds.Count - subtractedBase;
var remainder = new WeightedMember(
AutoTuneAxisMap.GenerateRemainderQuery(selection.Axis, selection.Value, subtracted),
@@ -456,7 +461,7 @@ public class CreateAutoTunedChannelsHandler(
// Stamp ownership so the artifacts are hidden from user collection lists and cleaned up on channel
// delete. Best-effort: an unstamped artifact is a cosmetic/cleanup issue, never a failed channel.
await TryStampOwnership(multiCollectionId, smartCollectionIds, channelId, cancellationToken);
await TryStampOwnership(multiCollectionId, smartCollectionIds, channelId);
return new AutoTuneChannelOutcome(name, AutoTuneOutcomeStatus.Created, channelId, null);
}
@@ -464,18 +469,24 @@ public class CreateAutoTunedChannelsHandler(
private async Task TryStampOwnership(
int multiCollectionId,
List<int> smartCollectionIds,
int channelId,
CancellationToken cancellationToken)
int channelId)
{
try
{
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
// Post-commit side effect: runs on CancellationToken.None so a late request cancellation can't
// abort it after the channel-create commit landed (#254) — an un-stamped artifact would be a
// permanent orphan (never cleaned on delete, and visible in the user collection lists). The MC +
// its member smart collections are stamped in one transaction so a mid-way failure can't leave the
// MC owned while the smart collections stay orphaned.
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(CancellationToken.None);
await using var transaction = await dbContext.Database.BeginTransactionAsync(CancellationToken.None);
await dbContext.MultiCollections
.Where(mc => mc.Id == multiCollectionId)
.ExecuteUpdateAsync(s => s.SetProperty(mc => mc.OwnedByChannelId, channelId), cancellationToken);
.ExecuteUpdateAsync(s => s.SetProperty(mc => mc.OwnedByChannelId, channelId), CancellationToken.None);
await dbContext.SmartCollections
.Where(sc => smartCollectionIds.Contains(sc.Id))
.ExecuteUpdateAsync(s => s.SetProperty(sc => sc.OwnedByChannelId, channelId), cancellationToken);
.ExecuteUpdateAsync(s => s.SetProperty(sc => sc.OwnedByChannelId, channelId), CancellationToken.None);
await transaction.CommitAsync(CancellationToken.None);
}
catch (Exception)
{
@@ -15,6 +15,9 @@ public class SearchMultiCollectionsHandler(IDbContextFactory<TvContext> dbContex
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
return await dbContext.MultiCollections
.AsNoTracking()
// Hide system-owned auto-tune weighted artifacts (#425) from the scheduling picker: selecting one
// into a user schedule would let a later channel delete cascade away that schedule item.
.Where(mc => mc.OwnedByChannelId == null)
.Where(mc => EF.Functions.Like(mc.Name, $"%{request.Query}%"))
.OrderBy(mc => mc.Name)
.Take(10)
@@ -15,6 +15,9 @@ public class SearchSmartCollectionsHandler(IDbContextFactory<TvContext> dbContex
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
return await dbContext.SmartCollections
.AsNoTracking()
// Hide system-owned auto-tune weighted artifacts (#425) from the scheduling picker: selecting one
// into a user schedule would let a later channel delete cascade away that schedule item.
.Where(sc => sc.OwnedByChannelId == null)
.Where(sc => EF.Functions.Like(sc.Name, $"%{request.Query}%"))
.OrderBy(sc => sc.Name)
.Take(10)
@@ -0,0 +1,60 @@
using System.Threading;
using System.Threading.Tasks;
using ErsatzTV.Application.MediaCollections;
using ErsatzTV.Application.Search;
using ErsatzTV.Core.Domain;
using ErsatzTV.Infrastructure.Data;
using ErsatzTV.Tests.Support;
using NUnit.Framework;
using Shouldly;
namespace ErsatzTV.Tests.Application.Search;
// #425: the scheduling collection picker (search-smart/multi-collections) must NOT surface the
// system-owned weighted-auto-tune artifacts — selecting one into a user schedule would let a later
// channel delete cascade away that schedule item.
[TestFixture]
public class CollectionPickerHidesOwnedTests
{
private InMemoryTvContext _db = null!;
[SetUp]
public async Task SetUp() => _db = await InMemoryTvContext.CreateAsync();
[TearDown]
public async Task TearDown() => await _db.DisposeAsync();
[Test]
public async Task SearchSmartCollections_Excludes_Owned()
{
await using (TvContext ctx = _db.CreateContext())
{
ctx.SmartCollections.Add(new SmartCollection { Name = "at:tok:0", Query = "q", OwnedByChannelId = 5 });
ctx.SmartCollections.Add(new SmartCollection { Name = "at:user:sc", Query = "q" });
await ctx.SaveChangesAsync();
}
var result = await new SearchSmartCollectionsHandler(_db.Factory)
.Handle(new SearchSmartCollections("at"), CancellationToken.None);
result.ShouldContain(vm => vm.Name == "at:user:sc");
result.ShouldNotContain(vm => vm.Name == "at:tok:0");
}
[Test]
public async Task SearchMultiCollections_Excludes_Owned()
{
await using (TvContext ctx = _db.CreateContext())
{
ctx.MultiCollections.Add(new MultiCollection { Name = "at-mc:tok", OwnedByChannelId = 5 });
ctx.MultiCollections.Add(new MultiCollection { Name = "at-mc:user" });
await ctx.SaveChangesAsync();
}
var result = await new SearchMultiCollectionsHandler(_db.Factory)
.Handle(new SearchMultiCollections("at"), CancellationToken.None);
result.ShouldContain(vm => vm.Name == "at-mc:user");
result.ShouldNotContain(vm => vm.Name == "at-mc:tok");
}
}