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
This commit is contained in:
sudacode committed 2026-11-27 17:32:58 -08:00
1 parent 71d8d9148c
commit 71bea8b4d6
2 files changed
+61 -27

No files matched your search

+19 -9
View File
@@ -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(fs.existsSync(flagPath), true);
assert.equal( assert.equal(
getCachedEmbeddedSubtitles(videoPath), getCachedEmbeddedSubtitles(videoPath),
@@ -4665,9 +4671,10 @@ exit 1
'stale retry cues must not be cached under the replacement file signature', 'stale retry cues must not be cached under the replacement file signature',
); );
await openSubsyncManualPicker(deps); const openedSecond = await openSubsyncManualPicker(deps);
assert.equal(payloads.length, 2); assert.equal(openedSecond, true);
const track3AfterReplacement = (payloads[1]!.referenceCandidates ?? []).find( assert.equal(payloads.length, 1);
const track3AfterReplacement = (payloads[0]!.referenceCandidates ?? []).find(
(c) => c.key === 'track:3', (c) => c.key === 'track:3',
); );
assert.equal(track3AfterReplacement?.audioStatus, 'aligned'); assert.equal(track3AfterReplacement?.audioStatus, 'aligned');
@@ -4811,13 +4818,16 @@ exit 1
const secondRunCalls = callsAfterSecond.slice(callsAfterFirst.length); const secondRunCalls = callsAfterSecond.slice(callsAfterFirst.length);
// Run 2: batch retries missing 0:2 (fails), audio hits cache, discovery retries 0:2 individually, // 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. // and 0:3 reuses cached entry without spawning ffmpeg for 0:3 again.
assert.ok( assert.equal(
secondRunCalls.every((line) => !line.includes('0:3')), secondRunCalls.length,
`expected run 2 to reuse cached 0:3 without invoking ffmpeg for it, got: ${secondRunCalls.join(' | ')}`, 2,
`expected 2 calls on run 2 (failed combined pass + individual retry for 0:2), got: ${secondRunCalls.join(' | ')}`,
); );
assert.ok( assert.ok(
secondRunCalls.some((line) => line.startsWith('PIPE=0') && line.includes('0:2')), secondRunCalls.every(
`expected run 2 to still retry missing 0:2 individually, got: ${secondRunCalls.join(' | ')}`, (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( assert.equal(
(payloads[1]!.referenceCandidates ?? []).find((c) => c.key === 'track:3')?.audioStatus, (payloads[1]!.referenceCandidates ?? []).find((c) => c.key === 'track:3')?.audioStatus,
+42 -18
View File
@@ -868,19 +868,24 @@ async function discoverAndEvaluateSubtitleCandidates(
options.fetchImpl, options.fetchImpl,
); );
const parsed = readAndParseSubtitleFile(extraction.path); const parsed = readAndParseSubtitleFile(extraction.path);
if ( if (isEmbedded && !isRemoteMediaPath(context.videoPath)) {
isEmbedded && const postExtractSignature = getMediaFileSignature(context.videoPath);
!isRemoteMediaPath(context.videoPath) && if (postExtractSignature !== preExtractSignature) {
typeof track['ff-index'] === 'number' && results.push(evaluateParsedCandidate(baseMeta, null));
parsed.events.length > 0 && continue;
preExtractSignature !== null }
) { if (
cacheEmbeddedSubtitleTrack( typeof track['ff-index'] === 'number' &&
context.videoPath, parsed.events.length > 0 &&
track['ff-index'], preExtractSignature !== null
parsed, ) {
preExtractSignature, cacheEmbeddedSubtitleTrack(
); context.videoPath,
track['ff-index'],
parsed,
preExtractSignature,
);
}
} }
results.push(evaluateParsedCandidate(baseMeta, parsed)); results.push(evaluateParsedCandidate(baseMeta, parsed));
} catch { } catch {
@@ -1664,6 +1669,7 @@ export async function openSubsyncManualPicker(
let audioSpeechAvailable: boolean | undefined; let audioSpeechAvailable: boolean | undefined;
if (!options.skipAudioAnalysis) { if (!options.skipAudioAnalysis) {
const initialMediaSignature = !isRemote ? getMediaFileSignature(context.videoPath) : null;
const prepared = await prepareRipCandidatesAndAudio( const prepared = await prepareRipCandidatesAndAudio(
context, context,
context.primaryTrack, context.primaryTrack,
@@ -1690,7 +1696,10 @@ export async function openSubsyncManualPicker(
fetchImpl: deps.fetchImpl, 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; return false;
} }
} }
@@ -1709,6 +1718,9 @@ async function runAutoSubsync(deps: TriggerSubsyncFromConfigDeps): Promise<void>
const context = await gatherSubsyncContext(client); const context = await gatherSubsyncContext(client);
const resolved = deps.getResolvedConfig(); const resolved = deps.getResolvedConfig();
const isRemote = isRemoteMediaPath(context.videoPath); 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 targetTrack = context.primaryTrack;
const targetSlot = resolveTargetSlot(targetTrack, context); const targetSlot = resolveTargetSlot(targetTrack, context);
@@ -1725,7 +1737,10 @@ async function runAutoSubsync(deps: TriggerSubsyncFromConfigDeps): Promise<void>
deps.fetchImpl, deps.fetchImpl,
); );
} catch (extractError) { } catch (extractError) {
if (!(await isPlaybackSessionUnchanged(client, context, targetTrack, targetSlot))) { if (
!(await isPlaybackSessionUnchanged(client, context, targetTrack, targetSlot)) ||
!isMediaFileUnchanged()
) {
return; return;
} }
const payload = buildManualPayload(context, resolved, deps, { const payload = buildManualPayload(context, resolved, deps, {
@@ -1739,7 +1754,10 @@ async function runAutoSubsync(deps: TriggerSubsyncFromConfigDeps): Promise<void>
const targetInfo = extractSpansFromParsedSubtitle(parsedTarget, { stripSigns: true }); const targetInfo = extractSpansFromParsedSubtitle(parsedTarget, { stripSigns: true });
if (parsedTarget.events.length === 0 || targetInfo.spans.length === 0) { 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; return;
} }
const payload = buildManualPayload(context, resolved, deps, { const payload = buildManualPayload(context, resolved, deps, {
@@ -1783,7 +1801,10 @@ async function runAutoSubsync(deps: TriggerSubsyncFromConfigDeps): Promise<void>
const activeDelay = alreadyInSync const activeDelay = alreadyInSync
? parseOptionalNumber(await client.requestProperty(delayProperty).catch(() => null)) ? parseOptionalNumber(await client.requestProperty(delayProperty).catch(() => null))
: null; : null;
if (!(await isPlaybackSessionUnchanged(client, context, targetTrack, targetSlot))) { if (
!(await isPlaybackSessionUnchanged(client, context, targetTrack, targetSlot)) ||
!isMediaFileUnchanged()
) {
return; return;
} }
if (alreadyInSync) { if (alreadyInSync) {
@@ -2160,7 +2181,10 @@ async function runAutoSubsync(deps: TriggerSubsyncFromConfigDeps): Promise<void>
} }
reasons.push('Pick a reference, browse a subtitle file, or adjust sync options below.'); 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; return;
} }