Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (copilot): exclude id-less videoHistory rows from wire snapshot/checksum so peers converge

Adam Eivy4993742fbfe8
diff --git a/server/services/dataSync.js b/server/services/dataSync.js
index 317d6c2a1a..aa487b30d1 100644
--- a/server/services/dataSync.js
+++ b/server/services/dataSync.js
@@ -595,6 +595,11 @@ async function applyMediaCollectionsRemote(remoteData) {
// categories' `readJSONFile`+`atomicWrite` pattern) rather than importing
// videoGen/local.js — that module drags in ffmpeg/spawn machinery we don't
// need on the sync read path, and the on-disk shape is a plain array.
+// A video-history row is syncable only if it carries a non-empty string `id`.
+// Shared by the snapshot (wire) side and the apply (merge) side so the two can
+// never disagree on which rows are keyable — see the convergence note below.
+const hasVideoRowId = (r) => typeof r?.id === 'string' && r.id;
+
async function getVideoHistorySnapshot() {
const raw = await readJSONFile(VIDEO_HISTORY_FILE, []);
// Exclude rows the user hid from their own gallery (`hidden: true`) — hiding
@@ -602,14 +607,18 @@ async function getVideoHistorySnapshot() {
// or a clip the user tucked away) and must NOT propagate to peers. The whole
// point of this category is to render a SHARED collection's video items, and
// a hidden row is by definition not part of that shared surface.
- const rows = (Array.isArray(raw) ? raw : []).filter((r) => r && !r.hidden);
+ //
+ // Also exclude rows without a string `id`: applyVideoHistoryRemote can only
+ // merge id-keyed rows, so an id-less row in the wire snapshot/checksum would
+ // be un-appliable on the receiver — its recomputed checksum would never match
+ // the sender's and the two peers would re-download this category forever.
+ // Treat id-less rows as strictly local-only on BOTH sides.
+ const rows = (Array.isArray(raw) ? raw : []).filter((r) => r && !r.hidden && hasVideoRowId(r));
// Canonicalize ordering for the wire so two peers holding identical sets
// produce identical checksums regardless of insertion (newest-first) order.
- // Mirrors getMediaCollectionsSnapshot's sort-by-id rationale. Rows without a
- // string id sort last (and won't merge — see applyVideoHistoryRemote's guard).
+ // Mirrors getMediaCollectionsSnapshot's sort-by-id rationale.
const data = {
- videos: [...rows].sort((a, b) =>
- String(a?.id ?? '').localeCompare(String(b?.id ?? ''))),
+ videos: [...rows].sort((a, b) => a.id.localeCompare(b.id)),
};
return { data, checksum: computeChecksum(data) };
}
@@ -627,8 +636,8 @@ async function applyVideoHistoryRemote(remoteData) {
// `createdAt` is a sufficient (and the only) freshness signal — there's no
// `updatedAt`. A row with no string id can't be keyed and is skipped (a
// hand-edited or corrupt entry shouldn't clobber a real row at key
- // `undefined`).
- const hasId = (r) => typeof r?.id === 'string' && r.id;
+ // `undefined`); the snapshot side excludes the same rows so checksums agree.
+ const hasId = hasVideoRowId;
const keyed = local.filter(hasId);
const before = new Map(keyed.map((r) => [r.id, r]));
const { merged, changed } = mergeArraysByKey(
diff --git a/server/services/dataSync.pipelineUniverse.test.js b/server/services/dataSync.pipelineUniverse.test.js
index f014266cc8..fd1ecbe7de 100644
--- a/server/services/dataSync.pipelineUniverse.test.js
+++ b/server/services/dataSync.pipelineUniverse.test.js
@@ -705,6 +705,24 @@ describe('dataSync — videoHistory category', () => {
expect(snap.data.videos.map((v) => v.id).sort()).toEqual(['v1', 'v2']);
});
+ it('snapshot excludes id-less rows so checksums converge (apply side drops them too)', async () => {
+ // An id-less local row can't be merged by applyVideoHistoryRemote, so it
+ // MUST be kept off the wire snapshot/checksum — otherwise a receiver that
+ // drops it recomputes a different checksum and the peers re-download forever.
+ writeJSON(VIDEO_HISTORY_PATH, [row('v1'), { prompt: 'legacy no-id', filename: 'legacy.mp4' }, row('v2')]);
+ const snap = await dataSync.getSnapshot('videoHistory');
+ expect(snap.data.videos.map((v) => v.id)).toEqual(['v1', 'v2']);
+ // Convergence: a fresh receiver applies the wire snapshot onto an empty
+ // store, then recomputes its OWN snapshot checksum — it must equal the
+ // sender's. If an id-less row had leaked onto the wire, the receiver (which
+ // drops it on apply) would compute a different checksum and never converge.
+ writeJSON(VIDEO_HISTORY_PATH, []);
+ const applied = await dataSync.applyRemote('videoHistory', snap.data);
+ expect(applied.applied).toBe(true);
+ const receiverSnap = await dataSync.getSnapshot('videoHistory');
+ expect(receiverSnap.checksum).toBe(snap.checksum);
+ });
+
it('snapshot checksum is order-insensitive (rows sorted by id on the wire)', async () => {
writeJSON(VIDEO_HISTORY_PATH, [row('v-b'), row('v-a')]);
const a = await dataSync.getSnapshot('videoHistory');