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(
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,
+42 -18
View File
@@ -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<void>
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<void>
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<void>
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<void>
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<void>
}
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;
}