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(creative-director): backfill a project id from the PK column when the JSONB blob lost it

[._.]/ Adam Eivy3e44df300aba
diff --git a/server/services/creativeDirector/projectsDB.js b/server/services/creativeDirector/projectsDB.js
index 4ed92d337a..918de4d4e7 100644
--- a/server/services/creativeDirector/projectsDB.js
+++ b/server/services/creativeDirector/projectsDB.js
@@ -42,17 +42,31 @@ import {
// The `data` JSONB is the whole record. status/created_at/updated_at are
// mirrored into columns (kept in lockstep with the JSONB on every write) for
-// future queries, but reads always return `data` verbatim so callers see the
-// exact record shape the file backend gave — the columns are never read back.
+// future queries and are never read back; reads return `data` verbatim so
+// callers see the exact record shape the file backend gave. The lone exception
+// is the `id` primary key — see rowToProject.
function rowToProject(row) {
if (!row) return null;
- // `data` already holds id/status/createdAt/updatedAt; the columns are a
- // queryable mirror. Return the JSONB verbatim so consumers (and the route
- // slim projection) see an identical shape to the file backend.
+ // Verbatim, EXCEPT `id`: a legacy row can carry a `data` blob with no id
+ // while the PK column — what every lookup here already matches on — has one.
+ // An id-less record is unaddressable downstream (the UI keys its grid on it,
+ // and sanitizeProjectForSync drops it from peer pushes), so backfill from the
+ // authoritative column. Repairing on read rather than by migration is
+ // deliberate: no current write path can produce the shape (persist() now
+ // asserts the invariant), so this only heals blobs left by older code, and it
+ // does so on every install without one having to run a backfill. The stored
+ // blob catches up on the record's next write. Healthy rows short-circuit on
+ // the first check and are still returned verbatim, without copying.
+ if (row.data && !row.data.id) return { ...row.data, id: row.id };
return row.data;
}
async function persist(exec, project) {
+ // The id lives in BOTH the PK column and the `data` blob, and rowToProject
+ // has to repair blobs that lost it. Assert the invariant at the one write
+ // chokepoint so a future refactor that drops `id` from the record fails
+ // loudly here instead of being silently healed on every subsequent read.
+ if (!project?.id) throw new ServerError('Cannot persist a Creative Director project without an id', { status: 500, code: 'PROJECT_ID_MISSING' });
// Cap runs[] at the single write chokepoint (mirrors the file backend's
// saveAll) so legacy over-cap rows shrink on first write.
if (Array.isArray(project.runs)) project.runs = trimRuns(project.runs);
@@ -87,13 +101,13 @@ export async function listProjects({ includeDeleted = false } = {}) {
// created_at ASC preserves the file backend's append order (the UI sorts
// client-side; recovery + tests don't depend on order, but stable beats random).
const result = includeDeleted
- ? await query(`SELECT data FROM creative_director_projects ORDER BY created_at ASC`)
- : await query(`SELECT data FROM creative_director_projects WHERE deleted = FALSE ORDER BY created_at ASC`);
+ ? await query(`SELECT id, data FROM creative_director_projects ORDER BY created_at ASC`)
+ : await query(`SELECT id, data FROM creative_director_projects WHERE deleted = FALSE ORDER BY created_at ASC`);
return result.rows.map(rowToProject);
}
export async function getProject(id, { includeDeleted = false } = {}) {
- const result = await query(`SELECT data FROM creative_director_projects WHERE id = $1`, [id]);
+ const result = await query(`SELECT id, data FROM creative_director_projects WHERE id = $1`, [id]);
const project = rowToProject(result.rows[0]);
if (!project) return null;
return includeDeleted || !project.deleted ? project : null;
@@ -125,7 +139,7 @@ export async function createProject(input) {
async function withLockedProject(id, mutate, { allowMissing = false } = {}) {
return withTransaction(async (client) => {
const sel = await client.query(
- `SELECT data FROM creative_director_projects WHERE id = $1 FOR UPDATE`,
+ `SELECT id, data FROM creative_director_projects WHERE id = $1 FOR UPDATE`,
[id],
);
const project = rowToProject(sel.rows[0]);
@@ -156,7 +170,7 @@ export async function deleteProject(id) {
// flips and `updatedAt`/`deletedAt` stamp now so the tombstone wins on merge.
return withTransaction(async (client) => {
const sel = await client.query(
- `SELECT data FROM creative_director_projects WHERE id = $1 FOR UPDATE`,
+ `SELECT id, data FROM creative_director_projects WHERE id = $1 FOR UPDATE`,
[id],
);
const current = rowToProject(sel.rows[0]);
@@ -183,7 +197,7 @@ export async function mergeProjectsFromSync(remoteProjects, { source = { via: 's
let changed = 0;
for (const remote of remoteProjects) {
const applied = await withTransaction(async (client) => {
- const sel = await client.query(`SELECT data FROM creative_director_projects WHERE id = $1 FOR UPDATE`, [remote?.id]);
+ const sel = await client.query(`SELECT id, data FROM creative_director_projects WHERE id = $1 FOR UPDATE`, [remote?.id]);
const local = rowToProject(sel.rows[0]);
const { next, inserted, remoteWins, changed: didChange } = mergeProjectRecord(local, remote);
if (!next) return false; // malformed remote → dropped
diff --git a/server/services/creativeDirector/projectsDB.test.js b/server/services/creativeDirector/projectsDB.test.js
index 8c089e056f..19432469df 100644
--- a/server/services/creativeDirector/projectsDB.test.js
+++ b/server/services/creativeDirector/projectsDB.test.js
@@ -75,6 +75,29 @@ describe.skipIf(!dbReady)('projectsDB round-trip', () => {
expect(list.some((x) => x.id === p.id)).toBe(true);
});
+ // A legacy/partial write can leave the JSONB without an `id` while the PK
+ // column still has one. Reads used to hand that record out id-less, and the
+ // Creative Director grid then rendered a card keyed on `undefined` that
+ // linked to /creative-director/undefined/overview (start/pause/delete all
+ // acting on `undefined`). Reads must backfill from the authoritative column.
+ it('backfills a missing JSONB id from the primary-key column', async () => {
+ const id = 'cd-legacy-no-json-id';
+ created.push(id);
+ // Deliberately write `data` WITHOUT an id to reproduce the bad row.
+ await query(
+ `INSERT INTO creative_director_projects (id, status, data, created_at, updated_at, deleted)
+ VALUES ($1, 'draft', $2::jsonb, now(), now(), FALSE)`,
+ [id, JSON.stringify({ name: 'Legacy row', status: 'draft' })],
+ );
+
+ const fetched = await db.getProject(id);
+ expect(fetched.id).toBe(id);
+ expect(fetched.name).toBe('Legacy row'); // the spread must not drop fields
+
+ const all = await db.listProjects();
+ expect(all.find((p) => p.name === 'Legacy row')?.id).toBe(id);
+ });
+
it('applies a treatment and patches a scene', async () => {
const p = await db.createProject(CREATE_INPUT);
created.push(p.id);