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

fix: stop peer-push bundles overwriting another record's catalog ingredients and existing tracks (#12466)

[._.]/ Adam Eivy138faf66cc25
diff --git a/server/services/catalogDB.facets.db.test.js b/server/services/catalogDB.facets.db.test.js
index 60f9c5f35e..ab83789887 100644
--- a/server/services/catalogDB.facets.db.test.js
+++ b/server/services/catalogDB.facets.db.test.js
@@ -245,4 +245,31 @@ describe.skipIf(!runDb)('catalogDB.findIngredientsByName', () => {
expect(found.map((r) => r.id)).not.toContain(otherType.id);
expect(await catalogDB.findIngredientsByName('character', [' '])).toEqual([]);
});
+
+ it('reports which ingredients exist locally and which already carry a live or tombstoned ref to a record (#12466)', async () => {
+ const make = async (name) => {
+ const row = await catalogDB.createIngredient({ type: 'character', name: `${name} ${nonce}` });
+ createdIngredientIds.add(row.id);
+ return row;
+ };
+ const live = await make('Ownership live');
+ const tombstoned = await make('Ownership tombstoned');
+ const foreign = await make('Ownership foreign');
+ const absentId = `absent-${nonce}`;
+ const insertRef = (ingredientId, refId, deleted = false) => query(
+ `INSERT INTO catalog_ingredient_refs (ingredient_id, ref_kind, ref_id, role, deleted, deleted_at)
+ VALUES ($1, 'universe', $2, 'cast', $3, CASE WHEN $3 THEN NOW() END)`,
+ [ingredientId, refId, deleted],
+ );
+ await insertRef(live.id, UNI_ID);
+ await insertRef(tombstoned.id, UNI_ID, true);
+ await insertRef(foreign.id, DEAD_UNI_ID);
+
+ const { existing, linked } = await catalogDB.getLocalIngredientRefOwnership(
+ [live.id, tombstoned.id, foreign.id, absentId], 'universe', UNI_ID,
+ );
+ expect([...existing].sort()).toEqual([live.id, tombstoned.id, foreign.id].sort());
+ expect([...linked].sort()).toEqual([live.id, tombstoned.id].sort());
+ expect((await catalogDB.getLocalIngredientRefOwnership([], 'universe', UNI_ID)).existing.size).toBe(0);
+ });
});
diff --git a/server/services/catalogDB/facets.js b/server/services/catalogDB/facets.js
index 075c4236b4..220e27b341 100644
--- a/server/services/catalogDB/facets.js
+++ b/server/services/catalogDB/facets.js
@@ -148,6 +148,29 @@ export async function getCatalogBundleForRef(refKind, refId) {
return { ingredients, refs };
}
+/**
+ * Which of `ingredientIds` already have a local row (live or tombstoned), and
+ * which of those already carry a local ref (live or tombstoned) to
+ * `refKind`/`refId`. A peer-push receiver uses it to refuse a bundled
+ * ingredient that belongs to another record (#12466).
+ */
+export async function getLocalIngredientRefOwnership(ingredientIds, refKind, refId) {
+ const ids = [...new Set(ingredientIds)];
+ if (ids.length === 0) return { existing: new Set(), linked: new Set() };
+ const [ingResult, refResult] = await Promise.all([
+ query(`SELECT id FROM catalog_ingredients WHERE id = ANY($1)`, [ids]),
+ query(
+ `SELECT DISTINCT ingredient_id FROM catalog_ingredient_refs
+ WHERE ingredient_id = ANY($1) AND ref_kind = $2 AND ref_id = $3`,
+ [ids, refKind, refId],
+ ),
+ ]);
+ return {
+ existing: new Set(ingResult.rows.map((row) => row.id)),
+ linked: new Set(refResult.rows.map((row) => row.ingredient_id)),
+ };
+}
+
export async function getCatalogStats() {
const [byTypeResult, scrapResult, withEmb] = await Promise.all([
query(`SELECT type, COUNT(*) AS count FROM catalog_ingredients WHERE deleted = false AND id = catalog_canonical_ingredient_id(id) GROUP BY type`),
diff --git a/server/services/sharing/peerSync.test.js b/server/services/sharing/peerSync.test.js
index ec1a2ac1a1..7d8c7973d5 100644
--- a/server/services/sharing/peerSync.test.js
+++ b/server/services/sharing/peerSync.test.js
@@ -226,6 +226,7 @@ vi.mock('../memoryBackend.js', async () => ({
}));
vi.mock('../catalogDB.js', async () => ({
getCatalogBundleForRef: vi.fn(),
+ getLocalIngredientRefOwnership: vi.fn(),
}));
vi.mock('../catalogSync.js', async () => ({
applyRemoteChanges: vi.fn(),
@@ -316,7 +317,7 @@ import { peerFetch } from '../../lib/peerHttpClient.js';
import { RESPONSE_TOO_LARGE } from '../../lib/httpClient.js';
import { reconcileMediaAssets } from '../mediaAssetIndex/index.js';
import { getBackendName } from '../memoryBackend.js';
-import { getCatalogBundleForRef } from '../catalogDB.js';
+import { getCatalogBundleForRef, getLocalIngredientRefOwnership } from '../catalogDB.js';
import { applyRemoteChanges as applyCatalogRemoteChanges } from '../catalogSync.js';
import { listCursors, __drainForTests as __drainCursors } from './peerTombstoneCursors.js';
import { contentHashForRecord, __resetBaseHashCacheForTests } from '../../lib/conflictJournal.js';
@@ -441,6 +442,7 @@ beforeEach(async () => {
// Catalog bundle defaults: non-postgres backend (no bundle), empty DB read,
// no-op apply. The catalog-bundle suite overrides these per-test.
vi.mocked(getBackendName).mockReset().mockReturnValue('file');
+ vi.mocked(getLocalIngredientRefOwnership).mockReset().mockResolvedValue({ existing: new Set(), linked: new Set() });
vi.mocked(getCatalogBundleForRef).mockReset().mockResolvedValue({ ingredients: [], refs: [] });
vi.mocked(applyCatalogRemoteChanges).mockReset().mockResolvedValue({ errors: [] });
@@ -3611,23 +3613,57 @@ describe('peerSync', () => {
expect(applyCatalogRemoteChanges).not.toHaveBeenCalled();
});
- it('does not let a music-video push overwrite a local track the local project does not link', async () => {
+ describe('bundled catalog ingredients owned by another record (#12466)', () => {
+ const ing = (id) => ({ id, type: 'character', name: id, updatedAt: '2026-01-02T00:00:00Z' });
+ const ref = (ingredientId) => ({ ingredientId, refKind: 'universe', refId: 'u1', role: 'canon-character', createdAt: '2026-01-02T00:00:00Z' });
+ const pushBundle = (catalogBundle) => applyIncomingPush({
+ kind: 'universe', record: { id: 'u1', ...live }, assetManifest: [], catalogBundle,
+ sourceInstanceId: 'peer-a', portosMeta: { schemaVersions: { ...PORTOS_SCHEMA_VERSIONS } },
+ });
+ const ownership = (existing, linked = []) => ({ existing: new Set(existing), linked: new Set(linked) });
+
+ beforeEach(() => {
+ vi.mocked(getBackendName).mockReturnValue('postgres');
+ });
+
+ it('drops an existing ingredient another record owns, with its forged ref, and merges the rest', async () => {
+ vi.mocked(getLocalIngredientRefOwnership).mockResolvedValue(ownership(['cat-victim', 'cat-mine'], ['cat-mine']));
+ await pushBundle({
+ ingredients: [ing('cat-victim'), ing('cat-mine'), ing('cat-new')],
+ refs: [ref('cat-victim'), ref('cat-mine'), ref('cat-new')],
+ });
+ expect(getLocalIngredientRefOwnership).toHaveBeenCalledWith(['cat-victim', 'cat-mine', 'cat-new'], 'universe', 'u1');
+ const arg = vi.mocked(applyCatalogRemoteChanges).mock.calls[0][0];
+ expect(arg.ingredients).toEqual([ing('cat-mine'), ing('cat-new')]);
+ expect(arg.refs).toEqual([ref('cat-mine'), ref('cat-new')]);
+ });
+
+ it('applies nothing when the only ingredient belongs to another record, even with no bundled row', async () => {
+ vi.mocked(getLocalIngredientRefOwnership).mockResolvedValue(ownership(['cat-victim']));
+ await pushBundle({ ingredients: [], refs: [ref('cat-victim')] });
+ expect(applyCatalogRemoteChanges).not.toHaveBeenCalled();
+ });
+
+ it('fails closed when the local ownership lookup throws', async () => {
+ vi.mocked(getLocalIngredientRefOwnership).mockRejectedValue(new Error('db unavailable'));
+ await pushBundle({ ingredients: [ing('cat-new')], refs: [ref('cat-new')] });
+ expect(applyCatalogRemoteChanges).not.toHaveBeenCalled();
+ });
+ });
+
+ it('never lets a music-video push overwrite an existing local track, linked by the project or not (#12466)', async () => {
const linkedTrack = { id: 'track-victim', title: 'Hijacked', ...live };
const push = (record) => applyIncomingPush({
kind: 'musicVideoProject', record: { ...record, ...live }, linkedTrack, assetManifest: [], sourceInstanceId: 'peer-a',
});
vi.mocked(getTrack).mockResolvedValue({ id: 'track-victim', title: 'Mine' });
- // No local copy of the project, or one linking a different track: refused.
+ // First push points the project at the victim track; the second finds
+ // that link already stored locally — neither may overwrite the track.
+ await push({ id: 'mv-new', trackId: 'track-victim' });
+ vi.mocked(getMusicVideoProject).mockResolvedValue({ id: 'mv-new', trackId: 'track-victim' });
await push({ id: 'mv-new', trackId: 'track-victim' });
- vi.mocked(getMusicVideoProject).mockResolvedValue({ id: 'mv-old', trackId: 'track-mine' });
- await push({ id: 'mv-old', trackId: 'track-victim' });
expect(mergeTracksFromSync).not.toHaveBeenCalled();
-
- // The local copy already links this track: the update merges.
- vi.mocked(getMusicVideoProject).mockResolvedValue({ id: 'mv-linked', trackId: 'track-victim' });
- await push({ id: 'mv-linked', trackId: 'track-victim' });
- expect(mergeTracksFromSync).toHaveBeenCalledWith([linkedTrack], SOURCE);
});
it('merges a linkedTrack that does not exist locally yet', async () => {
diff --git a/server/services/sharing/peerSyncReceive.js b/server/services/sharing/peerSyncReceive.js
index 38b9923e58..8be2540f23 100644
--- a/server/services/sharing/peerSyncReceive.js
+++ b/server/services/sharing/peerSyncReceive.js
@@ -50,6 +50,11 @@ import {
import { isStr, isNonBlankStr } from '../../lib/textUtils.js';
+async function isPostgresBackend() {
+ const { getBackendName } = await import('../memoryBackend.js');
+ return getBackendName() === 'postgres';
+}
+
/**
* Apply a received catalog bundle on the receiver. Reuses
* `catalogSync.applyRemoteChanges` so the bundle goes through the exact same
@@ -60,8 +65,7 @@ import { isStr, isNonBlankStr } from '../../lib/textUtils.js';
* the backend here so a stray call on a non-Postgres install is a clean no-op.
*/
async function applyCatalogBundle(catalogBundle, portosMeta) {
- const { getBackendName } = await import('../memoryBackend.js');
- if (getBackendName() !== 'postgres') return;
+ if (!(await isPostgresBackend())) return;
const { applyRemoteChanges } = await import('../catalogSync.js');
await applyRemoteChanges({
ingredients: Array.isArray(catalogBundle.ingredients) ? catalogBundle.ingredients : [],
@@ -111,37 +115,57 @@ async function bindLinkedCollection({ kind, record, linkedCollection, sourceInst
return linkedCollection;
}
-function bindCatalogBundle({ kind, record, catalogBundle, sourceInstanceId }) {
+async function bindCatalogBundle({ kind, record, catalogBundle, sourceInstanceId }) {
if (!isPlainObject(catalogBundle)) return null;
const rawRefs = Array.isArray(catalogBundle.refs) ? catalogBundle.refs : [];
const rawIngredients = Array.isArray(catalogBundle.ingredients) ? catalogBundle.ingredients : [];
- const refs = rawRefs.filter((ref) => isPlainObject(ref) && ref.refKind === kind && ref.refId === record.id);
- const named = new Set(refs.map((ref) => ref.ingredientId));
- const ingredients = rawIngredients.filter((ing) => isPlainObject(ing) && named.has(ing.id));
+ let refs = rawRefs.filter((ref) => isPlainObject(ref) && ref.refKind === kind && ref.refId === record.id);
+ let ingredients = rawIngredients.filter((ing) => isPlainObject(ing) && refs.some((ref) => ref.ingredientId === ing.id));
const dropped = (rawRefs.length - refs.length) + (rawIngredients.length - ingredients.length);
if (dropped > 0) {
console.warn(`⚠️ peerSync: dropped ${dropped} catalog bundle rows from ${sourceInstanceId} not bound to ${kind} ${record.id}`);
}
+ // A ref naming the pushed record proves nothing about an ingredient another
+ // record already owns: the sender writes both. Only an ingredient with no
+ // local row, or one this record already refs locally, may merge (#12466).
+ // The ref is dropped with it, or the forged link would authorize the
+ // overwrite on the next push. Postgres-only, like the apply itself.
+ const named = [...new Set(refs.map((ref) => ref.ingredientId))];
+ if (named.length > 0 && (await isPostgresBackend())) {
+ let ownership;
+ try {
+ const { getLocalIngredientRefOwnership } = await import('../catalogDB.js');
+ ownership = await getLocalIngredientRefOwnership(named, kind, record.id);
+ } catch (err) {
+ console.warn(`⚠️ peerSync: skipped catalog bundle from ${sourceInstanceId} — local ingredient lookup failed: ${err.message}`);
+ return null;
+ }
+ const foreign = new Set(named.filter((id) => ownership.existing.has(id) && !ownership.linked.has(id)));
+ if (foreign.size > 0) {
+ console.warn(`⚠️ peerSync: skipped ${foreign.size} catalog ingredients from ${sourceInstanceId} owned by another record`);
+ refs = refs.filter((ref) => !foreign.has(ref.ingredientId));
+ ingredients = ingredients.filter((ing) => !foreign.has(ing.id));
+ }
+ }
return refs.length === 0 && ingredients.length === 0 ? null : { ingredients, refs };
}
-// A project may bundle only the track it links, and may not replace a local
-// track the local copy of this project does not already link.
+// A project bundles only the track it links, and only when no local track has
+// that id. Track records carry no sender provenance, so a track that already
+// exists locally is never overwritten here — not even one the local project
+// links, since a prior push can set that link (#12466). Later edits arrive
+// through the Tracks category.
async function bindLinkedTrack({ record, linkedTrack, sourceInstanceId, onLookupError }) {
if (!isPlainObject(linkedTrack) || linkedTrack.id !== record.trackId) return null;
let localTrack;
- let localProject;
try {
- [localTrack, localProject] = await Promise.all([
- loadOrNotFound(() => RECORD_KINDS.track.load(linkedTrack.id)),
- loadOrNotFound(() => RECORD_KINDS.musicVideoProject.load(record.id)),
- ]);
+ localTrack = await loadOrNotFound(() => RECORD_KINDS.track.load(linkedTrack.id));
} catch (err) {
onLookupError(err);
return null;
}
- if (localTrack && localProject?.trackId !== linkedTrack.id) {
- console.warn(`⚠️ peerSync: skipped linkedTrack from ${sourceInstanceId} — local track is not linked by local project ${record.id}`);
+ if (localTrack) {
+ console.warn(`⚠️ peerSync: skipped linkedTrack from ${sourceInstanceId} — a local track with that id already exists`);
return null;
}
return linkedTrack;
@@ -335,7 +359,7 @@ export async function applyIncomingPush(payload, authorization) {
? await bindLinkedCollection({ kind, record, linkedCollection: pushedCollection, sourceInstanceId })
: null;
const catalogBundle = bundlesMergeable
- ? bindCatalogBundle({ kind, record, catalogBundle: pushedCatalog, sourceInstanceId })
+ ? await bindCatalogBundle({ kind, record, catalogBundle: pushedCatalog, sourceInstanceId })
: null;
// Only a music-video push has a linked track to bundle.
const linkedTrack = bundlesMergeable && kind === 'musicVideoProject'