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): queue first-read seed write; clamp tempo on blur not keystroke

Adam Eivy39acf2778ced
diff --git a/client/src/pages/SongEditor.jsx b/client/src/pages/SongEditor.jsx
index 608b623284..5c44773f2b 100644
--- a/client/src/pages/SongEditor.jsx
+++ b/client/src/pages/SongEditor.jsx
@@ -31,15 +31,23 @@ import { RHYTHM_SHAPES, VOICE_LAYERS } from '../lib/songCraft';
let localSeq = 0;
const localId = (prefix) => `${prefix}-new-${localSeq++}`;
-// Mirror the server tempo band (services/songs.js TEMPO_MIN/MAX) so the input
-// clamps locally — otherwise an out-of-band value only surfaces as an opaque
-// "Failed to save" toast after the server 400s. Empty input clears (null).
+// 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
+// "68" into "208" (the lone "6" clamps up to 20 first). While editing we keep
+// the raw parsed number; clampTempo runs on blur so the saved value lands in
+// band without an opaque server 400. Empty input clears (null).
const TEMPO_MIN = 20;
const TEMPO_MAX = 320;
-const clampTempo = (raw) => {
+// Parse a number input's value to a number or null, without clamping (used
+// on change so intermediate digits aren't mangled).
+const parseTempo = (raw) => {
if (raw === '' || raw == null) return null;
const n = Number(raw);
- if (!Number.isFinite(n)) return null;
+ return Number.isFinite(n) ? n : null;
+};
+// Clamp a tempo into the supported band (used on blur).
+const clampTempo = (n) => {
+ if (n == null) return null;
return Math.max(TEMPO_MIN, Math.min(TEMPO_MAX, Math.round(n)));
};
@@ -187,7 +195,8 @@ export default function SongEditor() {
min={TEMPO_MIN}
max={TEMPO_MAX}
value={song.tempo ?? ''}
- onChange={(e) => setField('tempo', clampTempo(e.target.value))}
+ onChange={(e) => setField('tempo', parseTempo(e.target.value))}
+ onBlur={() => setField('tempo', clampTempo(song.tempo))}
placeholder="e.g. 68"
className={inputCls}
/>
diff --git a/server/services/songs.js b/server/services/songs.js
index e1ff3108a9..66e2cdc33b 100644
--- a/server/services/songs.js
+++ b/server/services/songs.js
@@ -147,16 +147,38 @@ export const SEED_SONGS = [
// is re-entrancy hygiene, not a multi-actor lock (see CLAUDE.md Security Model).
const enqueue = createFileWriteQueue();
-// Read + sanitize every song. Seeds on first read (file absent) and persists
-// the seed so subsequent reads are stable and the user can edit the example.
-export async function listSongs() {
+// Pure read + sanitize — NO write side effect. When the file is absent or
+// malformed, returns the seed in-memory without persisting it. Mutations call
+// this inside their enqueue() so the read-modify-write cycle never re-enters
+// the queue (which would deadlock).
+async function readSongs() {
const state = await readJSONFile(STATE_PATH, null, { allowArray: false });
if (!state || !Array.isArray(state.songs)) {
+ return SEED_SONGS.map(sanitizeSong).filter(Boolean);
+ }
+ return state.songs.map(sanitizeSong).filter(Boolean);
+}
+
+// Public read. On first read (file absent) it persists the seed so the example
+// is stable and editable — but the seed write is routed through the SAME queue
+// as mutations and re-checks inside the queue, so a create that landed first
+// can't be clobbered by a late seed write (read-path lazy-init race).
+export async function listSongs() {
+ const state = await readJSONFile(STATE_PATH, null, { allowArray: false });
+ if (state && Array.isArray(state.songs)) {
+ return state.songs.map(sanitizeSong).filter(Boolean);
+ }
+ return enqueue(async () => {
+ // Re-check inside the queue: a queued create may have already written the
+ // file (with seed + new song). If so, don't overwrite it with bare seed.
+ const fresh = await readJSONFile(STATE_PATH, null, { allowArray: false });
+ if (fresh && Array.isArray(fresh.songs)) {
+ return fresh.songs.map(sanitizeSong).filter(Boolean);
+ }
const seeded = SEED_SONGS.map(sanitizeSong).filter(Boolean);
await atomicWrite(STATE_PATH, { songs: seeded });
return seeded;
- }
- return state.songs.map(sanitizeSong).filter(Boolean);
+ });
}
export async function getSong(id) {
@@ -166,7 +188,7 @@ export async function getSong(id) {
export async function createSong(input) {
return enqueue(async () => {
- const songs = await listSongs();
+ const songs = await readSongs();
const now = new Date().toISOString();
const song = sanitizeSong({ ...input, id: `song-${randomUUID()}`, createdAt: now, updatedAt: now });
songs.unshift(song);
@@ -178,7 +200,7 @@ export async function createSong(input) {
export async function updateSong(id, patch) {
return enqueue(async () => {
- const songs = await listSongs();
+ const songs = await readSongs();
const idx = songs.findIndex((s) => s.id === id);
if (idx === -1) throw makeErr(`Song ${id} not found`, ERR_NOT_FOUND);
// Merge field-by-field so an absent key preserves the stored value while a
@@ -200,7 +222,7 @@ export async function updateSong(id, patch) {
export async function deleteSong(id) {
return enqueue(async () => {
- const songs = await listSongs();
+ const songs = await readSongs();
const idx = songs.findIndex((s) => s.id === id);
if (idx === -1) throw makeErr(`Song ${id} not found`, ERR_NOT_FOUND);
const [removed] = songs.splice(idx, 1);
diff --git a/server/services/songs.test.js b/server/services/songs.test.js
index bc8024cc1b..15214b92df 100644
--- a/server/services/songs.test.js
+++ b/server/services/songs.test.js
@@ -57,6 +57,21 @@ describe('songs service', () => {
expect(noTempo.tempo).toBeNull();
});
+ it('does not lose a song when a create races the first-read seed write', async () => {
+ // Regression guard: on a fresh (unseeded) store, listSongs() previously
+ // wrote the seed from the read path OUTSIDE the write queue, so a late
+ // seed write could clobber a just-created song. Fire both without awaiting
+ // the list first; the created song must survive.
+ expect(existsSync(STATE_FILE)).toBe(false);
+ const [, created] = await Promise.all([
+ svc.listSongs(),
+ svc.createSong({ title: 'Race winner' }),
+ ]);
+ const songs = await svc.listSongs();
+ expect(songs.find((s) => s.id === created.id)).toBeTruthy();
+ expect(songs.find((s) => s.title === '500 Miles')).toBeTruthy(); // seed also present
+ });
+
it('clamps an out-of-band tempo on the sanitize/read path', () => {
// The route 400s an out-of-band tempo before createSong runs, so this
// clamp only ever fires when reading hand-edited JSON — exercise it there.