commit 5126ff0d26e5919e4f2e86396c5c26f6ca322a20
parent d6f3de11fa8c5addfa5046f1762524301aacb59d
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date: Fri, 13 Nov 2026 21:17:48 -0500
compact-saved-videos: the clock map keeps its id when it is retargeted
A recording clock re-measured against the re-encode and found within its
tolerance is the same map: it is written with the old id and the new target
(and `retargeted`: when, from which file, the measurement that proved it), so
every window cut through it stays on the current clock instead of being
retired. A pointer write that fails after the rename puts the re-encode back
under its scratch name.
Co-Authored-By: Claude <noreply@anthropic.com>
Diffstat:
2 files changed, 27 insertions(+), 10 deletions(-)
diff --git a/common/controller/compactSavedVideos.test.ts b/common/controller/compactSavedVideos.test.ts
@@ -615,6 +615,10 @@ test("the recording clock: re-measured against the re-encode and kept, or a move
assert.equal(after.compacted?.checks.clock, "kept");
assert.equal(written.target.file, after.file);
assert.equal(written.target.bytes, after.bytes);
+ // The same map (its id kept: windows cut through it stay current), retargeted.
+ assert.equal(written.id, "c1");
+ assert.equal(written.retargeted.from.file, ORIGINAL);
+ assert.equal(written.retargeted.measured, "c2");
} else {
assert.equal(r.status, "failed");
assert.match((r as { reason: string }).reason, /the recording clock moved/);
diff --git a/common/controller/compactSavedVideos.ts b/common/controller/compactSavedVideos.ts
@@ -612,7 +612,7 @@ export async function compactOne(
await deps.mux({ list: listFile, src, videoStart: probe.videoStart, out: muxed, signal });
// ---- 4. verify ----
- const { checks, fresh: freshClock } = await verify({ ...opts, deps, tag, state, src, muxed, probe, fps, videoDir, pointer, scratch, log });
+ const { checks, clock: keptClock } = await verify({ ...opts, deps, tag, state, src, muxed, probe, fps, videoDir, pointer, scratch, log });
// ---- 5. the swap ----
const at = deps.now();
@@ -704,18 +704,27 @@ export async function compactOne(
};
}
raw.alternates = [...(Array.isArray(raw.alternates) ? raw.alternates : []), original];
- await writeJsonAtomic(pointerFile, raw);
+ try {
+ await writeJsonAtomic(pointerFile, raw);
+ } catch (err) {
+ // The pointer still names the original: the re-encode goes back to
+ // the scratch name, never left as a file no pointer names.
+ await rename(final, muxed).catch(() => {});
+ throw err;
+ }
return true;
});
if (!swapped) {
await rm(muxed, { force: true });
return { status: "failed", reason: "the saved copy changed while it was being compacted (the pointer names other bytes now): nothing swapped" };
}
- // The clock map, measured against the re-encode during the checks, is now
- // the one about the active copy.
- if (checks.clock === "kept" && freshClock) {
+ // THE SAME MAP, NOW ABOUT THE ACTIVE COPY: re-measured against the
+ // re-encode during the checks and found within its tolerance, so it is the
+ // old map (its id kept — every window cut through it stays on this clock)
+ // with the new target, and a note of the measurement that proved it.
+ if (checks.clock === "kept" && keptClock) {
await recordingClockSidecar.write(videoDir, {
- ...freshClock,
+ ...keptClock,
target: { file: name, bytes: resultBytes },
});
}
@@ -774,13 +783,13 @@ async function verify(o: {
scratch: string;
log: (s: string) => void;
signal?: AbortSignal;
-}): Promise<{ checks: SavedVideoCompactedChecks; fresh: RecordingClock | null }> {
+}): Promise<{ checks: SavedVideoCompactedChecks; clock: RecordingClock | null }> {
const { deps, tag, state, settings: s, log, signal } = o;
const checks: Partial<SavedVideoCompactedChecks> = { passed: false };
const fail = (why: string): never => {
throw new VerifyFailure(why, checks);
};
- let fresh: RecordingClock | null = null;
+ let keep: RecordingClock | null = null;
log(`${tag}: verifying — a full decode first…`);
const dec = await deps.fullDecode(o.muxed, signal);
checks.decoded = dec.ok;
@@ -854,7 +863,7 @@ async function verify(o: {
const old = plan.clock;
const targetBytes = (await stat(o.muxed)).size;
const okAnchors = (old.anchors ?? []).filter((x: { ok?: boolean }) => x.ok).map((x: { at: number }) => x.at);
- fresh = await deps.measureClock({
+ const fresh = await deps.measureClock({
videoDir: o.videoDir,
source: plan.source,
container: o.muxed,
@@ -875,12 +884,16 @@ async function verify(o: {
);
}
checks.clock = "kept";
+ keep = {
+ ...old,
+ retargeted: { at: deps.now().toISOString(), from: old.target, measured: fresh.id, by: COMPACTION_TOOL },
+ } as RecordingClock;
log(`${tag}: the recording clock re-measured against the re-encode: within ${tol} s of the old map at ${okAnchors.length} anchor(s).`);
} else if (plan?.kind === "measure") {
checks.clock = "stale";
}
checks.passed = true;
- return { checks: checks as SavedVideoCompactedChecks, fresh };
+ return { checks: checks as SavedVideoCompactedChecks, clock: keep };
}
// --- after the swap ------------------------------------------------------------