From 172c771d978d7be5ac8700420dc1d0f0348e4b92 Mon Sep 17 00:00:00 2001 From: sudacode Date: Mon, 23 Nov 2026 02:03:11 -0800 Subject: [PATCH] fix(jellyfin): keep the episode playlist window at least 3 long With a 2-episode window the episode just left falls outside the next window, so a queue run could remove it while the user stepped back to it. mpv's IPC cannot check-and-remove atomically; a window of 3 or more always keeps both immediate neighbors, so removals stay two steps from the playing entry. maxEpisodes now clamps to 3..25. --- config.example.jsonc | 2 +- docs-site/configuration.md | 2 +- docs-site/public/config.example.jsonc | 2 +- src/config/definitions/options-integrations.ts | 2 +- src/config/resolve/integrations.ts | 4 ++-- src/config/resolve/jellyfin.test.ts | 10 ++++++++++ .../runtime/jellyfin-episode-playlist.test.ts | 14 ++++++++++++++ src/main/runtime/jellyfin-episode-playlist.ts | 15 +++++++++++++-- 8 files changed, 43 insertions(+), 8 deletions(-) diff --git a/config.example.jsonc b/config.example.jsonc index 84ca8916..581ec69f 100644 --- a/config.example.jsonc +++ b/config.example.jsonc @@ -1114,7 +1114,7 @@ "transcodeVideoCodec": "h264", // Preferred transcode video codec when direct play is unavailable. "episodePlaylist": { "enabled": true, // Add the playing episode's neighbors from its Jellyfin series to the mpv playlist, so mpv's playlist controls and end-of-file advance move between episodes. Values: true | false - "maxEpisodes": 5 // Most episodes kept in the mpv playlist, the playing one included, centered on it (2-25). + "maxEpisodes": 5 // Most episodes kept in the mpv playlist, the playing one included, centered on it (3-25). } // Episode playlist setting. }, // Optional Jellyfin integration for auth, browsing, and playback launch. diff --git a/docs-site/configuration.md b/docs-site/configuration.md index 68325d9a..4c4d7d89 100644 --- a/docs-site/configuration.md +++ b/docs-site/configuration.md @@ -572,7 +572,7 @@ Log in to a Jellyfin server, browse libraries, and play or cast to SubMiner. Log | `jellyfin.directPlayPreferred` | `true` | Try direct play before transcoding | | `jellyfin.transcodeVideoCodec` | `"h264"` | Codec requested when transcoding | | `jellyfin.episodePlaylist.enabled` | `true` | Queue the episodes around the playing one in mpv's playlist ([episode playlist](/jellyfin-integration#episode-playlist)) | -| `jellyfin.episodePlaylist.maxEpisodes` | `5` | Most episodes in that playlist, the playing one included (2-25) | +| `jellyfin.episodePlaylist.maxEpisodes` | `5` | Most episodes in that playlist, the playing one included (3-25) | ### Discord rich presence diff --git a/docs-site/public/config.example.jsonc b/docs-site/public/config.example.jsonc index 84ca8916..581ec69f 100644 --- a/docs-site/public/config.example.jsonc +++ b/docs-site/public/config.example.jsonc @@ -1114,7 +1114,7 @@ "transcodeVideoCodec": "h264", // Preferred transcode video codec when direct play is unavailable. "episodePlaylist": { "enabled": true, // Add the playing episode's neighbors from its Jellyfin series to the mpv playlist, so mpv's playlist controls and end-of-file advance move between episodes. Values: true | false - "maxEpisodes": 5 // Most episodes kept in the mpv playlist, the playing one included, centered on it (2-25). + "maxEpisodes": 5 // Most episodes kept in the mpv playlist, the playing one included, centered on it (3-25). } // Episode playlist setting. }, // Optional Jellyfin integration for auth, browsing, and playback launch. diff --git a/src/config/definitions/options-integrations.ts b/src/config/definitions/options-integrations.ts index bbde8cba..c1b58e7b 100644 --- a/src/config/definitions/options-integrations.ts +++ b/src/config/definitions/options-integrations.ts @@ -716,7 +716,7 @@ export function buildIntegrationConfigOptionRegistry( kind: 'number', defaultValue: defaultConfig.jellyfin.episodePlaylist.maxEpisodes, description: - 'Most episodes kept in the mpv playlist, the playing one included, centered on it (2-25).', + 'Most episodes kept in the mpv playlist, the playing one included, centered on it (3-25).', }, { path: 'discordPresence.enabled', diff --git a/src/config/resolve/integrations.ts b/src/config/resolve/integrations.ts index 425cbfec..fc455f00 100644 --- a/src/config/resolve/integrations.ts +++ b/src/config/resolve/integrations.ts @@ -388,13 +388,13 @@ export function applyIntegrationConfig(context: ResolveContext): void { const maxEpisodes = asNumber(episodePlaylist.maxEpisodes); if (maxEpisodes !== undefined) { - const normalized = Math.min(25, Math.max(2, Math.floor(maxEpisodes))); + const normalized = Math.min(25, Math.max(3, Math.floor(maxEpisodes))); if (normalized !== maxEpisodes) { warn( 'jellyfin.episodePlaylist.maxEpisodes', episodePlaylist.maxEpisodes, normalized, - 'Out of range; clamped to 2..25.', + 'Out of range; clamped to 3..25.', ); } resolved.jellyfin.episodePlaylist.maxEpisodes = normalized; diff --git a/src/config/resolve/jellyfin.test.ts b/src/config/resolve/jellyfin.test.ts index 93a29d6f..37d5bf30 100644 --- a/src/config/resolve/jellyfin.test.ts +++ b/src/config/resolve/jellyfin.test.ts @@ -178,3 +178,13 @@ test('jellyfin episodePlaylist resolves the toggle and clamps the episode cap', assert.deepEqual(context.resolved.jellyfin.episodePlaylist, { enabled: false, maxEpisodes: 25 }); assert.ok(warnings.some((warning) => warning.path === 'jellyfin.episodePlaylist.maxEpisodes')); }); + +test('jellyfin episodePlaylist raises an episode cap below 3', () => { + const { context } = createResolveContext({ + jellyfin: { episodePlaylist: { maxEpisodes: 2 } }, + }); + + applyIntegrationConfig(context); + + assert.equal(context.resolved.jellyfin.episodePlaylist.maxEpisodes, 3); +}); diff --git a/src/main/runtime/jellyfin-episode-playlist.test.ts b/src/main/runtime/jellyfin-episode-playlist.test.ts index f16bbf40..f4041141 100644 --- a/src/main/runtime/jellyfin-episode-playlist.test.ts +++ b/src/main/runtime/jellyfin-episode-playlist.test.ts @@ -172,6 +172,20 @@ test('queueNeighbors never undoes a navigation that lands after its playlist rea assert.deepEqual(harness.filenames(), [url('e3'), url('e4'), url('e5')]); }); +test('queueNeighbors keeps the episode just left even with the smallest configured window', async () => { + const harness = makeHarness({ enabled: true, maxEpisodes: 2 }); + harness.playing('e3'); + await harness.queue.queueNeighbors(null, entry('e3')); + harness.playing('e4'); + // The read sees e4 playing; the user steps back to e3 before the edits arrive. + harness.setAfterRead(() => harness.playing('e3')); + + await harness.queue.queueNeighbors(null, entry('e4')); + + assert.equal(harness.playlist.find((item) => item.current)?.filename, url('e3')); + assert.ok(harness.filenames().includes(url('e3'))); +}); + test('queueNeighbors leaves the playlist alone once mpv has moved past the item', async () => { const harness = makeHarness(); harness.playing('e3'); diff --git a/src/main/runtime/jellyfin-episode-playlist.ts b/src/main/runtime/jellyfin-episode-playlist.ts index dffae999..dec764b5 100644 --- a/src/main/runtime/jellyfin-episode-playlist.ts +++ b/src/main/runtime/jellyfin-episode-playlist.ts @@ -13,10 +13,17 @@ export type JellyfinPlaylistEntry = { export type JellyfinEpisodePlaylistSettings = { enabled: boolean; - /** Most entries kept in mpv's playlist, the current one included. */ + /** Most entries kept in mpv's playlist, the current one included. Raised to at least 3. */ maxEpisodes: number; }; +/** + * A window of 3 or more always holds the current episode's immediate neighbors (it only shifts + * at a series edge, where that neighbor does not exist). Removals then stay at least two steps + * from the playing entry, so a queue run cannot remove the episode a user just stepped back to. + */ +const MIN_WINDOW_EPISODES = 3; + export type JellyfinQueuedEpisode = { context: Context; entry: Entry; @@ -110,7 +117,11 @@ export function createJellyfinEpisodePlaylist