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 (codex): don't persist in-session temp row ids

Adam Eivy2913ab90cad4
diff --git a/client/src/pages/SongEditor.jsx b/client/src/pages/SongEditor.jsx
index 5c44773f2b..30a2b4cc29 100644
--- a/client/src/pages/SongEditor.jsx
+++ b/client/src/pages/SongEditor.jsx
@@ -24,12 +24,19 @@ import { useAsyncAction } from '../hooks/useAsyncAction';
import { getSong, updateSong } from '../services/api';
import { RHYTHM_SHAPES, VOICE_LAYERS } from '../lib/songCraft';
-// Stable client-side id for a freshly-added section/layer before the server
-// assigns one. Counter-based (not Math.random, which is unavailable in some
-// harnesses and unnecessary here) — uniqueness only needs to hold within the
-// editing session for React keys; the server re-ids on save.
+// In-session-only id for a freshly-added section/layer, used purely as a React
+// key until the row is saved. Counter-based (not Math.random, which is
+// unavailable in some harnesses and unnecessary here) — uniqueness only needs
+// to hold within the editing session. These TEMP ids are stripped on save (see
+// stripTempId) so the server assigns a stable `sec-<uuid>`/`layer-<uuid>`; if
+// they were persisted, a reload (localSeq → 0) could re-mint `sec-new-0` and
+// collide with a saved row, breaking per-id update/remove.
+const TEMP_ID_RE = /-new-\d+$/;
let localSeq = 0;
const localId = (prefix) => `${prefix}-new-${localSeq++}`;
+// Blank a temp id before save so the server re-ids it; keep stable ids
+// (preset ids like `lead`, server-assigned uuids) so dedup + matching survive.
+const stripTempId = (row) => (TEMP_ID_RE.test(row.id) ? { ...row, id: '' } : row);
// Mirror the server tempo band (services/songs.js TEMPO_MIN/MAX). We clamp on
// BLUR, not on every keystroke — clamping each keystroke would turn typing
@@ -76,7 +83,10 @@ export default function SongEditor() {
title: song.title, artist: song.artist, key: song.key,
tempo: song.tempo ?? null, rhythmShapeId: song.rhythmShapeId,
notation: song.notation, notes: song.notes, learned: song.learned,
- sections: song.sections, layers: song.layers,
+ // Strip in-session temp ids so the server assigns stable uuids — keeps
+ // them from being persisted and later colliding after a reload.
+ sections: (song.sections || []).map(stripTempId),
+ layers: (song.layers || []).map(stripTempId),
};
const data = await updateSong(id, patch, { silent: true });
if (data?.song) setSong(data.song);
@@ -100,8 +110,12 @@ export default function SongEditor() {
// --- Layer helpers ------------------------------------------------------
const addLayer = (preset) => setSong((prev) => ({
...prev,
+ // Presets carry the STABLE bare preset id (`lead`) — the picker already
+ // prevents adding the same preset twice, so it's unique among layers, it
+ // survives save (not a temp id), and it keeps remainingPresets dedup
+ // working. Blank layers use a temp id stripped on save.
layers: [...(prev.layers || []), preset
- ? { id: localId(preset.id), label: preset.label, part: preset.voices, notes: preset.advice }
+ ? { id: preset.id, label: preset.label, part: preset.voices, notes: preset.advice }
: { id: localId('layer'), label: 'Layer', part: '', notes: '' }],
}));
const updateLayer = (lid, key, value) => setSong((prev) => ({
@@ -113,11 +127,11 @@ export default function SongEditor() {
}));
// Layer presets the user hasn't added yet, in foundation-first order. Match
- // on the preset id (seed layers carry the bare id like `lead`; ladder-added
- // layers carry `${id}-new-N`), so renaming a layer's label doesn't make its
- // preset reappear and two presets sharing a label can't collide.
+ // on the preset id: preset layers (seed or ladder-added) carry the bare
+ // preset id like `lead`, so renaming a layer's label can't make its preset
+ // reappear and two presets sharing a label can't collide.
const remainingPresets = useMemo(() => {
- const have = new Set((song?.layers || []).map((l) => l.id.replace(/-new-\d+$/, '')));
+ const have = new Set((song?.layers || []).map((l) => l.id));
return VOICE_LAYERS.filter((p) => !have.has(p.id));
}, [song?.layers]);
diff --git a/server/services/songs.test.js b/server/services/songs.test.js
index 15214b92df..a0540c9ca2 100644
--- a/server/services/songs.test.js
+++ b/server/services/songs.test.js
@@ -92,6 +92,22 @@ describe('songs service', () => {
expect(song.layers[0].label).toBe('Layer'); // defaulted when blank but kept (has part/notes)
});
+ it('assigns a unique id to sections/layers submitted with a blank id', async () => {
+ // The editor strips in-session temp ids before save (sends id: '') so the
+ // server mints stable, non-colliding ids — guards against the reload
+ // duplicate-key bug where persisted temp ids could be re-minted.
+ const song = await svc.createSong({
+ title: 'Blank ids',
+ sections: [{ id: '', label: 'A', lyrics: '1' }, { id: '', label: 'B', lyrics: '2' }],
+ layers: [{ id: '', label: 'L1', part: 'x' }, { id: '', label: 'L2', part: 'y' }],
+ });
+ const secIds = song.sections.map((s) => s.id);
+ const layerIds = song.layers.map((l) => l.id);
+ expect(new Set(secIds).size).toBe(2); // unique
+ expect(new Set(layerIds).size).toBe(2);
+ for (const id of [...secIds, ...layerIds]) expect(id).toBeTruthy();
+ });
+
it('merges patches field-by-field — absent key preserves, present key applies', async () => {
const song = await svc.createSong({ title: 'Original', artist: 'Someone', key: 'C' });
const patched = await svc.updateSong(song.id, { title: 'Renamed', key: '' });