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): index physicalDescription, personality, role, motivations, significance in search_tsv

[._.]/ Adam Eivya1a6b3896afd
diff --git a/server/lib/db.js b/server/lib/db.js
index 62b4023a1e..9f6c91b0e0 100644
--- a/server/lib/db.js
+++ b/server/lib/db.js
@@ -148,15 +148,6 @@ export async function ensureSchema() {
tags TEXT[] DEFAULT '{}',
embedding vector(768),
embedding_model VARCHAR(100),
- search_tsv tsvector GENERATED ALWAYS AS (
- setweight(to_tsvector('english', coalesce(name, '')), 'A') ||
- setweight(to_tsvector('english',
- coalesce(payload->>'description', '') || ' ' ||
- coalesce(payload->>'notes', '') || ' ' ||
- coalesce(payload->>'background', '') || ' ' ||
- coalesce(payload->>'summary', '')
- ), 'B')
- ) STORED,
origin_instance_id VARCHAR(36),
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
@@ -164,6 +155,29 @@ export async function ensureSchema() {
deleted_at TIMESTAMPTZ,
sync_sequence BIGSERIAL
)`,
+ // Postgres can't ALTER the expression of a STORED generated column, so the
+ // FTS column is DROPped and re-ADDed on every boot. The whole catalogDDL
+ // block is idempotent: on a fresh install the DROP is a no-op (column
+ // didn't exist) and ADD creates it; on an existing v1 install the DROP
+ // removes the narrow expression and ADD installs the expanded one that
+ // also indexes physicalDescription / personality / role / motivations /
+ // significance. Bumped PORTOS_SCHEMA_VERSIONS.catalog → 2 in lockstep.
+ `ALTER TABLE catalog_ingredients DROP COLUMN IF EXISTS search_tsv`,
+ `ALTER TABLE catalog_ingredients ADD COLUMN IF NOT EXISTS search_tsv tsvector
+ GENERATED ALWAYS AS (
+ setweight(to_tsvector('english', coalesce(name, '')), 'A') ||
+ setweight(to_tsvector('english',
+ coalesce(payload->>'description', '') || ' ' ||
+ coalesce(payload->>'physicalDescription', '') || ' ' ||
+ coalesce(payload->>'personality', '') || ' ' ||
+ coalesce(payload->>'background', '') || ' ' ||
+ coalesce(payload->>'summary', '') || ' ' ||
+ coalesce(payload->>'notes', '') || ' ' ||
+ coalesce(payload->>'role', '') || ' ' ||
+ coalesce(payload->>'motivations', '') || ' ' ||
+ coalesce(payload->>'significance', '')
+ ), 'B')
+ ) STORED`,
`CREATE INDEX IF NOT EXISTS idx_catalog_ing_embedding
ON catalog_ingredients USING hnsw (embedding vector_cosine_ops)
WITH (m = 16, ef_construction = 64)`,
diff --git a/server/lib/schemaVersions.js b/server/lib/schemaVersions.js
index 6f4c0bd1e4..4804dc06cb 100644
--- a/server/lib/schemaVersions.js
+++ b/server/lib/schemaVersions.js
@@ -47,12 +47,21 @@ 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).
- // Per-category gate so a new peer can sync its catalog independently of
- // whether other categories are version-locked. `cat-ingredient` and
- // `cat-scrap` record kinds map back here via RECORD_KIND_SCHEMA_CATEGORIES.
- catalog: 1,
+ // 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
+ // here via RECORD_KIND_SCHEMA_CATEGORIES.
+ catalog: 2,
// 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 c4dfcb78c3..f3765ca6ac 100644
--- a/server/scripts/init-db.sql
+++ b/server/scripts/init-db.sql
@@ -161,18 +161,6 @@ CREATE TABLE IF NOT EXISTS catalog_ingredients (
tags TEXT[] DEFAULT '{}',
embedding vector(768),
embedding_model VARCHAR(100),
- -- Weighted FTS column. Name carries the most weight (A); description/notes/
- -- background fall under B. Generated/stored so the GIN index stays fresh
- -- without trigger code.
- search_tsv tsvector GENERATED ALWAYS AS (
- setweight(to_tsvector('english', coalesce(name, '')), 'A') ||
- setweight(to_tsvector('english',
- coalesce(payload->>'description', '') || ' ' ||
- coalesce(payload->>'notes', '') || ' ' ||
- coalesce(payload->>'background', '') || ' ' ||
- coalesce(payload->>'summary', '')
- ), 'B')
- ) STORED,
origin_instance_id VARCHAR(36),
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
@@ -180,6 +168,33 @@ CREATE TABLE IF NOT EXISTS catalog_ingredients (
deleted_at TIMESTAMPTZ,
sync_sequence BIGSERIAL
);
+-- Weighted FTS column. Name carries the most weight (A); the character canon
+-- fields (description, physicalDescription, personality, background, summary,
+-- notes) plus the role/motivations/significance type-specific fields fall under
+-- B. Generated/stored so the GIN index stays fresh without trigger code.
+-- Postgres can't ALTER the expression of a STORED generated column, so
+-- ensureSchema (server/lib/db.js) DROPs + re-ADDs the column on every boot —
+-- mirrored here as DROP IF EXISTS + ADD IF NOT EXISTS so a fresh-install run
+-- of this script produces the same end-state as an existing-install boot
+-- through ensureSchema. PORTOS_SCHEMA_VERSIONS.catalog is bumped to 2 in
+-- lockstep so older peers can't push pre-expansion-shape rows that would
+-- mismatch the indexed expression.
+ALTER TABLE catalog_ingredients DROP COLUMN IF EXISTS search_tsv;
+ALTER TABLE catalog_ingredients ADD COLUMN IF NOT EXISTS search_tsv tsvector
+ GENERATED ALWAYS AS (
+ setweight(to_tsvector('english', coalesce(name, '')), 'A') ||
+ setweight(to_tsvector('english',
+ coalesce(payload->>'description', '') || ' ' ||
+ coalesce(payload->>'physicalDescription', '') || ' ' ||
+ coalesce(payload->>'personality', '') || ' ' ||
+ coalesce(payload->>'background', '') || ' ' ||
+ coalesce(payload->>'summary', '') || ' ' ||
+ coalesce(payload->>'notes', '') || ' ' ||
+ coalesce(payload->>'role', '') || ' ' ||
+ coalesce(payload->>'motivations', '') || ' ' ||
+ coalesce(payload->>'significance', '')
+ ), 'B')
+ ) STORED;
CREATE INDEX IF NOT EXISTS idx_catalog_ing_embedding
ON catalog_ingredients USING hnsw (embedding vector_cosine_ops)
WITH (m = 16, ef_construction = 64);