From 71bea8b4d61c9912c2fefcca38069fe28cf7b9cc Mon Sep 17 00:00:00 2001 From: sudacode Date: Fri, 27 Nov 2026 17:32:58 -0800 Subject: [PATCH] fix(subsync): drop stale results when the media file is replaced mid-run - Check the media file signature again after embedded extraction and drop candidates extracted from a file that was replaced - Skip publishing the manual picker and auto-sync results if the local file changed during analysis - Make the tests stricter about stale picker payloads and about ffmpeg calls for cached tracks --- src/core/services/subsync.test.ts | 28 ++++++++++----- src/core/services/subsync.ts | 60 +++++++++++++++++++++---------- 2 files changed, 61 insertions(+), 27 deletions(-) diff --git a/src/core/services/subsync.test.ts b/src/core/services/subsync.test.ts index 5d2c42fd..46277f7c 100644 --- a/src/core/services/subsync.test.ts +++ b/src/core/services/subsync.test.ts @@ -4657,7 +4657,13 @@ exit 1 }, }); - await openSubsyncManualPicker(deps); + const openedFirst = await openSubsyncManualPicker(deps); + assert.equal(openedFirst, false); + assert.equal( + payloads.length, + 0, + 'stale candidates from superseded media version must not be published', + ); assert.equal(fs.existsSync(flagPath), true); assert.equal( getCachedEmbeddedSubtitles(videoPath), @@ -4665,9 +4671,10 @@ exit 1 'stale retry cues must not be cached under the replacement file signature', ); - await openSubsyncManualPicker(deps); - assert.equal(payloads.length, 2); - const track3AfterReplacement = (payloads[1]!.referenceCandidates ?? []).find( + const openedSecond = await openSubsyncManualPicker(deps); + assert.equal(openedSecond, true); + assert.equal(payloads.length, 1); + const track3AfterReplacement = (payloads[0]!.referenceCandidates ?? []).find( (c) => c.key === 'track:3', ); assert.equal(track3AfterReplacement?.audioStatus, 'aligned'); @@ -4811,13 +4818,16 @@ exit 1 const secondRunCalls = callsAfterSecond.slice(callsAfterFirst.length); // Run 2: batch retries missing 0:2 (fails), audio hits cache, discovery retries 0:2 individually, // and 0:3 reuses cached entry without spawning ffmpeg for 0:3 again. - assert.ok( - secondRunCalls.every((line) => !line.includes('0:3')), - `expected run 2 to reuse cached 0:3 without invoking ffmpeg for it, got: ${secondRunCalls.join(' | ')}`, + assert.equal( + secondRunCalls.length, + 2, + `expected 2 calls on run 2 (failed combined pass + individual retry for 0:2), got: ${secondRunCalls.join(' | ')}`, ); assert.ok( - secondRunCalls.some((line) => line.startsWith('PIPE=0') && line.includes('0:2')), - `expected run 2 to still retry missing 0:2 individually, got: ${secondRunCalls.join(' | ')}`, + secondRunCalls.every( + (line) => line.startsWith('PIPE=0') && line.includes('0:2') && !line.includes('0:3'), + ), + `expected both run 2 calls to map only 0:2 without 0:3, got: ${secondRunCalls.join(' | ')}`, ); assert.equal( (payloads[1]!.referenceCandidates ?? []).find((c) => c.key === 'track:3')?.audioStatus, diff --git a/src/core/services/subsync.ts b/src/core/services/subsync.ts index 24a10c0a..9dc9ecfb 100644 --- a/src/core/services/subsync.ts +++ b/src/core/services/subsync.ts @@ -868,19 +868,24 @@ async function discoverAndEvaluateSubtitleCandidates( options.fetchImpl, ); const parsed = readAndParseSubtitleFile(extraction.path); - if ( - isEmbedded && - !isRemoteMediaPath(context.videoPath) && - typeof track['ff-index'] === 'number' && - parsed.events.length > 0 && - preExtractSignature !== null - ) { - cacheEmbeddedSubtitleTrack( - context.videoPath, - track['ff-index'], - parsed, - preExtractSignature, - ); + if (isEmbedded && !isRemoteMediaPath(context.videoPath)) { + const postExtractSignature = getMediaFileSignature(context.videoPath); + if (postExtractSignature !== preExtractSignature) { + results.push(evaluateParsedCandidate(baseMeta, null)); + continue; + } + if ( + typeof track['ff-index'] === 'number' && + parsed.events.length > 0 && + preExtractSignature !== null + ) { + cacheEmbeddedSubtitleTrack( + context.videoPath, + track['ff-index'], + parsed, + preExtractSignature, + ); + } } results.push(evaluateParsedCandidate(baseMeta, parsed)); } catch { @@ -1664,6 +1669,7 @@ export async function openSubsyncManualPicker( let audioSpeechAvailable: boolean | undefined; if (!options.skipAudioAnalysis) { + const initialMediaSignature = !isRemote ? getMediaFileSignature(context.videoPath) : null; const prepared = await prepareRipCandidatesAndAudio( context, context.primaryTrack, @@ -1690,7 +1696,10 @@ export async function openSubsyncManualPicker( fetchImpl: deps.fetchImpl, }, ); - if (!(await isPlaybackSessionUnchanged(client, context, context.primaryTrack, 'primary'))) { + if ( + !(await isPlaybackSessionUnchanged(client, context, context.primaryTrack, 'primary')) || + (!isRemote && getMediaFileSignature(context.videoPath) !== initialMediaSignature) + ) { return false; } } @@ -1709,6 +1718,9 @@ async function runAutoSubsync(deps: TriggerSubsyncFromConfigDeps): Promise const context = await gatherSubsyncContext(client); const resolved = deps.getResolvedConfig(); const isRemote = isRemoteMediaPath(context.videoPath); + const initialMediaSignature = !isRemote ? getMediaFileSignature(context.videoPath) : null; + const isMediaFileUnchanged = (): boolean => + isRemote || getMediaFileSignature(context.videoPath) === initialMediaSignature; const targetTrack = context.primaryTrack; const targetSlot = resolveTargetSlot(targetTrack, context); @@ -1725,7 +1737,10 @@ async function runAutoSubsync(deps: TriggerSubsyncFromConfigDeps): Promise deps.fetchImpl, ); } catch (extractError) { - if (!(await isPlaybackSessionUnchanged(client, context, targetTrack, targetSlot))) { + if ( + !(await isPlaybackSessionUnchanged(client, context, targetTrack, targetSlot)) || + !isMediaFileUnchanged() + ) { return; } const payload = buildManualPayload(context, resolved, deps, { @@ -1739,7 +1754,10 @@ async function runAutoSubsync(deps: TriggerSubsyncFromConfigDeps): Promise const targetInfo = extractSpansFromParsedSubtitle(parsedTarget, { stripSigns: true }); if (parsedTarget.events.length === 0 || targetInfo.spans.length === 0) { - if (!(await isPlaybackSessionUnchanged(client, context, targetTrack, targetSlot))) { + if ( + !(await isPlaybackSessionUnchanged(client, context, targetTrack, targetSlot)) || + !isMediaFileUnchanged() + ) { return; } const payload = buildManualPayload(context, resolved, deps, { @@ -1783,7 +1801,10 @@ async function runAutoSubsync(deps: TriggerSubsyncFromConfigDeps): Promise const activeDelay = alreadyInSync ? parseOptionalNumber(await client.requestProperty(delayProperty).catch(() => null)) : null; - if (!(await isPlaybackSessionUnchanged(client, context, targetTrack, targetSlot))) { + if ( + !(await isPlaybackSessionUnchanged(client, context, targetTrack, targetSlot)) || + !isMediaFileUnchanged() + ) { return; } if (alreadyInSync) { @@ -2160,7 +2181,10 @@ async function runAutoSubsync(deps: TriggerSubsyncFromConfigDeps): Promise } reasons.push('Pick a reference, browse a subtitle file, or adjust sync options below.'); - if (!(await isPlaybackSessionUnchanged(client, context, targetTrack, targetSlot))) { + if ( + !(await isPlaybackSessionUnchanged(client, context, targetTrack, targetSlot)) || + !isMediaFileUnchanged() + ) { return; }