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(catalog): soft-delete catalog_ingredient_refs so unlinks propagate to peers

[._.]/ Adam Eivy79697de76ac4
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index ec1edf9c0f..ab4a274b41 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -20,6 +20,7 @@
## Fixed
+- **[catalog-ref-deletion-tombstones] Catalog ingredient unlinks now propagate to federation peers.** Unlinking an ingredient from a universe / series / issue used to hard-DELETE the `catalog_ingredient_refs` row with no `sync_sequence` bump and no tombstone — peers that already pulled the link kept it forever, leaving the "Appears in" panel stale across the federation. The ref table now soft-deletes (new `deleted` / `deleted_at` columns + an UPDATE trigger that bumps `sync_sequence` on delete/revive), `linkIngredientToRef` revives a tombstoned row on re-link, and `upsertRefFromPeer` mirrors remote deletes via `ON CONFLICT DO UPDATE`. `PORTOS_SCHEMA_VERSIONS.catalog` bumps from 1 to 2 so older peers can't push a v1-shaped payload that would silently miss tombstones.
- **[civitai-hidream-qwen-baseModels] Civitai LoRA suggestions now include HiDream and Qwen.** Both runner families existed in `RUNNER_FAMILIES` but were missing from the Civitai `baseModels` search map and the LoRA Manager UI — so every page load logged `Unknown runner family for Civitai search: hidream` / `qwen`, neither family got a "Top for …" section, and any HiDream/Qwen LoRA installed via URL ended up with `runnerFamily=null` (broken compat filter). The two families now map to Civitai's `HiDream` / `Qwen` baseModel strings and render alongside Flux 1, Flux 2, Z-Image, and ERNIE in the suggestions panel.
- **Creative Director long sessions stay fast.** Per-scene render orchestration no longer slows down as a project accumulates more scenes — previously a project could degrade from ~3.5 minutes per scene to 10–30 minutes after a long run, because every render reread and rewrote the full per-project run history as that history grew unbounded. The Runs tab now shows up to 200 of the most recent run entries per project; older completed/failed runs are dropped automatically while every in-flight task is always preserved.
- **[triage-gemini-out-of-scope-findings] Editor and Claude Code can now open managed apps on Windows.** "Open in Editor" and "Open in Claude" buttons now resolve `.cmd`/`.bat` shims (e.g. `code.cmd`, `claude.cmd`) that Node otherwise refuses to spawn — previously these actions silently failed on Windows installs.
diff --git a/server/lib/db.js b/server/lib/db.js
index e28c284430..8250b0608d 100644
--- a/server/lib/db.js
+++ b/server/lib/db.js
@@ -222,9 +222,17 @@ export async function ensureSchema() {
ref_id TEXT NOT NULL,
role VARCHAR(64) NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW(),
+ deleted BOOLEAN DEFAULT FALSE,
+ deleted_at TIMESTAMPTZ,
sync_sequence BIGSERIAL,
PRIMARY KEY (ingredient_id, ref_kind, ref_id, role)
)`,
+ // Idempotent upgrade path for existing installs predating the soft-delete
+ // columns. Without these, an old install boots the new code and silently
+ // hard-DELETEs on unlink (no tombstone, no sync_sequence bump) — peers
+ // never learn the ref was removed.
+ `ALTER TABLE catalog_ingredient_refs ADD COLUMN IF NOT EXISTS deleted BOOLEAN DEFAULT FALSE`,
+ `ALTER TABLE catalog_ingredient_refs ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ`,
`CREATE INDEX IF NOT EXISTS idx_catalog_ing_refs_target ON catalog_ingredient_refs (ref_kind, ref_id)`,
`CREATE INDEX IF NOT EXISTS idx_catalog_ing_refs_sync_seq ON catalog_ingredient_refs (sync_sequence)`,
@@ -303,6 +311,27 @@ export async function ensureSchema() {
BEFORE UPDATE ON catalog_ingredient_sources
FOR EACH ROW
EXECUTE FUNCTION update_catalog_source_sync_seq()`,
+
+ // Ref-link UPDATE bumps sync_sequence so a soft-delete or revival of a
+ // ref row ships as a normal sync event. Without this, the soft-delete
+ // path would update `deleted`/`deleted_at` but leave sync_sequence at
+ // the original INSERT value — peers past that cursor would never see
+ // the tombstone and their "Appears in" panels would stay stale.
+ `CREATE OR REPLACE FUNCTION update_catalog_ref_sync_seq()
+ RETURNS TRIGGER AS $$
+ BEGIN
+ IF NEW.deleted IS DISTINCT FROM OLD.deleted
+ OR NEW.deleted_at IS DISTINCT FROM OLD.deleted_at THEN
+ NEW.sync_sequence := nextval(pg_get_serial_sequence('catalog_ingredient_refs', 'sync_sequence'));
+ END IF;
+ RETURN NEW;
+ END;
+ $$ LANGUAGE plpgsql`,
+ `DROP TRIGGER IF EXISTS trg_catalog_ref_sync_seq ON catalog_ingredient_refs`,
+ `CREATE TRIGGER trg_catalog_ref_sync_seq
+ BEFORE UPDATE ON catalog_ingredient_refs
+ FOR EACH ROW
+ EXECUTE FUNCTION update_catalog_ref_sync_seq()`,
];
for (const sql of catalogDDL) {
await pool.query(sql);
diff --git a/server/lib/schemaVersions.js b/server/lib/schemaVersions.js
index 4804dc06cb..40c7056f57 100644
--- a/server/lib/schemaVersions.js
+++ b/server/lib/schemaVersions.js
@@ -47,21 +47,27 @@ export const PORTOS_SCHEMA_VERSIONS = Object.freeze({
// pauses with old peers; issues/universes keep flowing.
pipelineSeries: 2,
mediaCollections: 1,
+ // v1 = creative ingredients catalog (Postgres tables: catalog_scraps,
+ // catalog_ingredients, catalog_ingredient_sources, catalog_ingredient_refs).
// v2 = `catalog_ingredients.search_tsv` expanded to also index the
// character canon fields (physicalDescription, personality) and the
// type-specific role/motivations/significance fields, so bible-promoted
// characters become searchable on their main narrative text. The schema
// is a DROP+re-ADD of the STORED generated column (Postgres can't ALTER
- // its expression); applied in lockstep by `ensureSchema` in
- // server/lib/db.js. Per-category gate so a new peer can sync its catalog
- // independently of whether other categories are version-locked. An older
- // v1 peer pushing to a v2 receiver is sender-behind on `catalog` (not
- // ahead), so the receiver still accepts and re-derives `search_tsv`
- // locally via the STORED expression. A v2 peer pushing to a v1 receiver
- // is sender-ahead and gets 412 — the older code can't index the new
- // payload fields. `cat-ingredient` and `cat-scrap` record kinds map back
+ // its expression); applied in lockstep by `ensureSchema`.
+ // v3 = `catalog_ingredient_refs` gained `deleted`/`deleted_at` soft-delete
+ // tombstones + an UPDATE trigger that bumps sync_sequence on delete/revive.
+ // Older peers (≤v2) hard-DELETE on unlink and never tombstone, so the
+ // version gate prevents a v3 receiver from accepting an older sender's
+ // payload that would silently miss tombstones.
+ //
+ // Per-category gate so a new peer can sync its catalog independently of
+ // whether other categories are version-locked. Older peers are
+ // sender-behind on `catalog` (not ahead), so the receiver still accepts
+ // their pushes; newer peers pushing to older receivers are sender-ahead
+ // and get 412. `cat-ingredient` and `cat-scrap` record kinds map back
// here via RECORD_KIND_SCHEMA_CATEGORIES.
- catalog: 2,
+ catalog: 3,
// NOTE: `videoHistory` is intentionally NOT listed here. The version gate
// rejects the ENTIRE snapshot/push payload on ANY ahead-mismatch (the
// comparator walks the union of keys), so declaring a brand-new key would
diff --git a/server/scripts/init-db.sql b/server/scripts/init-db.sql
index 6db256bf56..08185d7e4b 100644
--- a/server/scripts/init-db.sql
+++ b/server/scripts/init-db.sql
@@ -243,6 +243,8 @@ CREATE TABLE IF NOT EXISTS catalog_ingredient_refs (
ref_id TEXT NOT NULL,
role VARCHAR(64) NOT NULL, -- 'canon-character'|'canon-place'|'canon-object'|'cast'|'mentioned'
created_at TIMESTAMPTZ DEFAULT NOW(),
+ deleted BOOLEAN DEFAULT FALSE, -- soft-delete tombstone so unlinks propagate to peers
+ deleted_at TIMESTAMPTZ,
sync_sequence BIGSERIAL,
PRIMARY KEY (ingredient_id, ref_kind, ref_id, role)
);
@@ -339,3 +341,25 @@ CREATE TRIGGER trg_catalog_source_sync_seq
BEFORE UPDATE ON catalog_ingredient_sources
FOR EACH ROW
EXECUTE FUNCTION update_catalog_source_sync_seq();
+
+-- Ref-link UPDATE bumps sync_sequence on soft-delete or revival so peers
+-- receive the tombstone as a normal sync event. Without this, the soft-delete
+-- path would update `deleted`/`deleted_at` but leave sync_sequence at the
+-- original INSERT value — peers past that cursor would never see the change
+-- and their "Appears in" panels would stay stale forever.
+CREATE OR REPLACE FUNCTION update_catalog_ref_sync_seq()
+RETURNS TRIGGER AS $$
+BEGIN
+ IF NEW.deleted IS DISTINCT FROM OLD.deleted
+ OR NEW.deleted_at IS DISTINCT FROM OLD.deleted_at THEN
+ NEW.sync_sequence := nextval(pg_get_serial_sequence('catalog_ingredient_refs', 'sync_sequence'));
+ END IF;
+ RETURN NEW;
+END;
+$$ LANGUAGE plpgsql;
+
+DROP TRIGGER IF EXISTS trg_catalog_ref_sync_seq ON catalog_ingredient_refs;
+CREATE TRIGGER trg_catalog_ref_sync_seq
+ BEFORE UPDATE ON catalog_ingredient_refs
+ FOR EACH ROW
+ EXECUTE FUNCTION update_catalog_ref_sync_seq();
diff --git a/server/services/catalogDB.js b/server/services/catalogDB.js
index d1f5060fab..ecf4d89df2 100644
--- a/server/services/catalogDB.js
+++ b/server/services/catalogDB.js
@@ -79,6 +79,8 @@ function rowToRef(row) {
refId: row.ref_id,
role: row.role,
createdAt: row.created_at.toISOString(),
+ deleted: !!row.deleted,
+ deletedAt: row.deleted_at?.toISOString() ?? null,
syncSequence: String(row.sync_sequence),
};
}
@@ -429,25 +431,39 @@ export async function listSourcesForScrap(scrapId) {
}
export async function linkIngredientToRef(ingredientId, refKind, refId, role) {
+ // ON CONFLICT DO UPDATE revives a soft-deleted row instead of leaving it
+ // tombstoned. The trigger only bumps sync_sequence when `deleted` or
+ // `deleted_at` actually change, so a link-on-active-row stays a no-op for
+ // peers (no spurious sync event).
await query(
`INSERT INTO catalog_ingredient_refs (ingredient_id, ref_kind, ref_id, role)
VALUES ($1, $2, $3, $4)
- ON CONFLICT (ingredient_id, ref_kind, ref_id, role) DO NOTHING`,
+ ON CONFLICT (ingredient_id, ref_kind, ref_id, role) DO UPDATE
+ SET deleted = false, deleted_at = NULL`,
[ingredientId, refKind, refId, role],
);
}
export async function unlinkIngredientFromRef(ingredientId, refKind, refId, role) {
+ // Soft-delete via UPDATE so the row stays around as a tombstone and the
+ // trg_catalog_ref_sync_seq trigger bumps sync_sequence — peers pick the
+ // unlink up on their next pull. The `AND deleted = false` filter keeps
+ // re-unlinks from re-bumping sync_sequence unnecessarily.
await query(
- `DELETE FROM catalog_ingredient_refs
- WHERE ingredient_id = $1 AND ref_kind = $2 AND ref_id = $3 AND role = $4`,
+ `UPDATE catalog_ingredient_refs
+ SET deleted = true, deleted_at = NOW()
+ WHERE ingredient_id = $1 AND ref_kind = $2 AND ref_id = $3 AND role = $4
+ AND deleted = false`,
[ingredientId, refKind, refId, role],
);
}
export async function listRefsForIngredient(ingredientId) {
+ // Filter `deleted = false` so the "Appears in" panel doesn't surface
+ // tombstoned unlinks. Tombstones are read-only state for sync purposes;
+ // user-facing list paths only show live links.
const result = await query(
- `SELECT * FROM catalog_ingredient_refs WHERE ingredient_id = $1`,
+ `SELECT * FROM catalog_ingredient_refs WHERE ingredient_id = $1 AND deleted = false`,
[ingredientId],
);
return result.rows.map(rowToRef);
@@ -458,7 +474,8 @@ export async function listIngredientsForRef(refKind, refId) {
`SELECT i.*, r.role, r.created_at AS ref_created_at
FROM catalog_ingredients i
JOIN catalog_ingredient_refs r ON r.ingredient_id = i.id
- WHERE r.ref_kind = $1 AND r.ref_id = $2 AND i.deleted = false`,
+ WHERE r.ref_kind = $1 AND r.ref_id = $2
+ AND r.deleted = false AND i.deleted = false`,
[refKind, refId],
);
return result.rows.map((row) => ({ ingredient: rowToIngredient(row), role: row.role }));
@@ -599,11 +616,28 @@ export async function upsertSourceFromPeer(src) {
}
export async function upsertRefFromPeer(ref) {
+ // ON CONFLICT DO UPDATE so a peer's soft-delete (or revival) of a ref row
+ // is mirrored locally. Refs don't carry an `updated_at` column — they're
+ // tuple-unique — so a strict LWW window doesn't apply; the receiver simply
+ // adopts the peer's `deleted` / `deleted_at` state. The trigger only bumps
+ // sync_sequence when those columns change, so a no-op replay (peer already
+ // matches local) stays silent on the next outbound pull.
await query(
- `INSERT INTO catalog_ingredient_refs (ingredient_id, ref_kind, ref_id, role, created_at)
- VALUES ($1, $2, $3, $4, $5)
- ON CONFLICT (ingredient_id, ref_kind, ref_id, role) DO NOTHING`,
- [ref.ingredientId, ref.refKind, ref.refId, ref.role, ref.createdAt],
+ `INSERT INTO catalog_ingredient_refs
+ (ingredient_id, ref_kind, ref_id, role, created_at, deleted, deleted_at)
+ VALUES ($1, $2, $3, $4, $5, $6, $7)
+ ON CONFLICT (ingredient_id, ref_kind, ref_id, role) DO UPDATE
+ SET deleted = EXCLUDED.deleted,
+ deleted_at = EXCLUDED.deleted_at`,
+ [
+ ref.ingredientId,
+ ref.refKind,
+ ref.refId,
+ ref.role,
+ ref.createdAt,
+ !!ref.deleted,
+ ref.deletedAt || null,
+ ],
);
}