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 (claude): fix DDL upgrade-path gap + caption prefix boundary

Adam Eivyeb2766b8da37
diff --git a/server/lib/db.catalogDdlParity.test.js b/server/lib/db.catalogDdlParity.test.js
index 14589342c8..f709035deb 100644
--- a/server/lib/db.catalogDdlParity.test.js
+++ b/server/lib/db.catalogDdlParity.test.js
@@ -309,6 +309,34 @@ describe('catalog DDL parity (init-db.sql ↔ db.js ensureSchema)', () => {
.toEqual([...new Set(extractColumnNames(jsBody))].sort());
});
+ // Broad drift guard for the whole upgrade-vs-fresh-install gap (beyond the
+ // catalog tables above). init-db.sql runs ONLY on fresh `db.sh setup-native`
+ // provisioning; existing installs + federated peers get new tables solely
+ // through db.js `ensureSchema()`'s catalogDDL array. A table added to one
+ // source but not the other is invisible until a real upgrade fails in the
+ // wild (e.g. lora_training_runs shipped in init-db.sql but was missing from
+ // ensureSchema — every existing install would 500 on the first training run).
+ it('every table in init-db.sql is also created in db.js ensureSchema', () => {
+ const tableNames = (source) => {
+ const out = new Set();
+ const re = /CREATE TABLE IF NOT EXISTS\s+([a-z0-9_]+)/gi;
+ let m;
+ while ((m = re.exec(source)) !== null) out.add(m[1].toLowerCase());
+ return out;
+ };
+ // The memory subsystem provisions its own tables via a separate path
+ // (server/scripts/migrate*Memories*.js), so they live in init-db.sql but
+ // intentionally NOT in ensureSchema — the documented exception in this
+ // file's header. Everything else must be in both.
+ const PROVISIONED_ELSEWHERE = new Set(['memories', 'memory_links']);
+ const sqlTables = tableNames(INIT_SQL);
+ const jsTables = tableNames(DB_JS);
+ expect(sqlTables.size, 'init-db.sql declares no tables — parser broke').toBeGreaterThan(8);
+ const missing = [...sqlTables]
+ .filter((t) => !jsTables.has(t) && !PROVISIONED_ELSEWHERE.has(t));
+ expect(missing, `tables in init-db.sql but missing from db.js ensureSchema (existing installs won't get them): ${missing.join(', ')}`).toEqual([]);
+ });
+
it('search_tsv payload field set matches', () => {
// Both files re-declare the GENERATED ALWAYS expression character-for-
// character today. If one side adds a payload key (e.g. voiceNotes) and
diff --git a/server/lib/db.js b/server/lib/db.js
index 994576e032..db6e84642e 100644
--- a/server/lib/db.js
+++ b/server/lib/db.js
@@ -837,6 +837,21 @@ async function ensureSchemaImpl() {
finished_at TIMESTAMPTZ
)`,
`CREATE INDEX IF NOT EXISTS idx_wr_exercises_work ON writers_room_exercises (work_id, started_at DESC)`,
+
+ // LoRA training runs (character LoRA training). MUST live here, not only
+ // in init-db.sql — init-db.sql runs only on fresh `db.sh setup-native`
+ // provisioning, so existing installs + federated peers get new tables
+ // exclusively through this boot-time upgrade path. Mirrors init-db.sql.
+ `CREATE TABLE IF NOT EXISTS lora_training_runs (
+ id TEXT PRIMARY KEY,
+ status TEXT NOT NULL,
+ character_id TEXT,
+ data JSONB NOT NULL DEFAULT '{}'::jsonb,
+ created_at TIMESTAMPTZ DEFAULT NOW(),
+ updated_at TIMESTAMPTZ DEFAULT NOW()
+ )`,
+ `CREATE INDEX IF NOT EXISTS idx_lora_training_runs_status ON lora_training_runs (status)`,
+ `CREATE INDEX IF NOT EXISTS idx_lora_training_runs_character ON lora_training_runs (character_id)`,
];
for (const sql of catalogDDL) {
await pool.query(sql);
diff --git a/server/lib/loraDataset.js b/server/lib/loraDataset.js
index 67cc2e4fc7..84e62af63d 100644
--- a/server/lib/loraDataset.js
+++ b/server/lib/loraDataset.js
@@ -152,7 +152,11 @@ export function prefixCaption(triggerWord, text, { previousTriggerWord = null }
let body = trim(text);
const stripPrefix = (value, token) => {
if (!token) return value;
- return value.replace(new RegExp(`^${escapeRe(token)}\\s*,?\\s*`, 'i'), '');
+ // The `(?=[\s,]|$)` boundary is load-bearing: trigger words are short
+ // [a-z0-9_] tokens that often prefix a real word (e.g. `her` in `heroic`),
+ // and the trailing comma is optional — without the boundary, stripping
+ // `her` from `heroic stance` would amputate it to `oic stance`.
+ return value.replace(new RegExp(`^${escapeRe(token)}(?=[\\s,]|$)\\s*,?\\s*`, 'i'), '');
};
body = stripPrefix(body, word);
const prev = trim(previousTriggerWord);
diff --git a/server/lib/loraDataset.test.js b/server/lib/loraDataset.test.js
index 9b7d19e47a..bd4bfe23c0 100644
--- a/server/lib/loraDataset.test.js
+++ b/server/lib/loraDataset.test.js
@@ -102,6 +102,16 @@ describe('prefixCaption', () => {
expect(out).toBe('kessa_v2, a woman with copper hair');
});
+ it('does not amputate a body word that the trigger is a substring-prefix of', () => {
+ // `her` prefixes `heroic`; without a token boundary the strip would
+ // mangle this to `her, oic stance`.
+ expect(prefixCaption('her', 'heroic stance')).toBe('her, heroic stance');
+ expect(prefixCaption('her', 'her, heroic stance')).toBe('her, heroic stance');
+ // Re-prefix path: renaming away from a substring-prefix trigger keeps the body.
+ expect(prefixCaption('hero_v2', 'heroic stance', { previousTriggerWord: 'her' }))
+ .toBe('hero_v2, heroic stance');
+ });
+
it('returns just the trigger word for empty text', () => {
expect(prefixCaption('kessa', '')).toBe('kessa');
expect(prefixCaption('kessa', ' ')).toBe('kessa');