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): forward-compat song enums on the write path, kit: cannot hide a played row, loop cannot wedge on a rest-only bar

[._.]/ Adam Eivyfaa32fe5d98f
diff --git a/client/src/components/songbook/constants.js b/client/src/components/songbook/constants.js
index c27b125270..d4005eb354 100644
--- a/client/src/components/songbook/constants.js
+++ b/client/src/components/songbook/constants.js
@@ -44,6 +44,12 @@ export const DRUM_INSTRUMENT = 'drums';
// the record on the next save. So append the stored value as its own option and
// let the save round-trip it unchanged.
//
+// The server half of this contract is in `server/lib/brainValidation.js`: the
+// write schemas accept the enum OR any short slug, precisely so the value this
+// preserves can actually be saved rather than 400ing on a field the user never
+// touched. Keep the two ends together — a client that preserves an unknown value
+// against a server that rejects it just makes the song uneditable.
+//
// `options` may be `[{ id, label }]` (INSTRUMENTS) or `['tab', …]` (SONG_FORMATS);
// both normalize to `{ id, label }`.
export const withStoredOption = (options, stored) => {
diff --git a/client/src/hooks/useDrumPlayer.js b/client/src/hooks/useDrumPlayer.js
index b7e5127008..0767d9622e 100644
--- a/client/src/hooks/useDrumPlayer.js
+++ b/client/src/hooks/useDrumPlayer.js
@@ -117,6 +117,9 @@ export default function useDrumPlayer(text, { songId } = {}) {
const player = playerRef.current;
if (!player) return;
if (playingRef.current) { stop(); return; }
+ // Same gate the Play button carries — an all-rest chart has nothing to sound,
+ // and the space-key binding must not route around a disabled button.
+ if (!hasMusic) return;
setPlayingBoth(true);
// play() resolves once playback has STARTED; an autoplay-policy failure lands
// here — reset the button rather than lying "playing".
@@ -125,7 +128,7 @@ export default function useDrumPlayer(text, { songId } = {}) {
setPlayingBoth(false);
setActiveStep(null);
});
- }, [setPlayingBoth, stop]);
+ }, [setPlayingBoth, stop, hasMusic]);
// Timing-critical edits stop playback first (see the header note); the sync
// effect above then hands the new value to the now-idle player.
diff --git a/client/src/lib/drumNotation.js b/client/src/lib/drumNotation.js
index 3738484a1d..e74878866f 100644
--- a/client/src/lib/drumNotation.js
+++ b/client/src/lib/drumNotation.js
@@ -273,11 +273,16 @@ export const parseDrumChart = (text) => {
}
}
- // Rows the chart actually uses, in kit order (or the header's `kit:` order,
- // filtered to what's present — an override must not invent empty rows).
+ // Rows the chart actually uses. `kit:` reorders them; it must NOT hide any —
+ // a piece the header omits is still played back, so dropping it from `pieces`
+ // would sound a snare the sheet never draws. So: the listed pieces first (in
+ // the author's order, filtered to what's present, since an override must not
+ // invent empty rows), then everything else in default kit order.
const present = new Set(bars.flatMap((bar) => bar.rows.map((r) => r.piece)));
- const order = header.kit || KIT_PIECES.map((p) => p.id);
- const pieces = order.filter((id) => present.has(id));
+ const preferred = (header.kit || []).filter((id) => present.has(id));
+ const rest = KIT_PIECES.map((p) => p.id)
+ .filter((id) => present.has(id) && !preferred.includes(id));
+ const pieces = [...preferred, ...rest];
return {
time: { beats: header.beats, beatValue: header.beatValue },
diff --git a/client/src/lib/drumNotation.test.js b/client/src/lib/drumNotation.test.js
index 6ebef02e08..f36ca4d229 100644
--- a/client/src/lib/drumNotation.test.js
+++ b/client/src/lib/drumNotation.test.js
@@ -65,6 +65,21 @@ describe('parseDrumChart — headers', () => {
expect(chart.pieces).toEqual(['K', 'S', 'HH']);
});
+ it('kit: REORDERS rows, never hides one the chart plays', () => {
+ // A piece the header omits is still scheduled, so dropping it from `pieces`
+ // would sound a snare the sheet never draws. Listed pieces come first, the
+ // rest follow in default kit order.
+ const chart = parseDrumChart('kit: K, HH\n\nHH: xxxx\nS: --o-\nK: o---');
+ expect(chart.pieces).toEqual(['K', 'HH', 'S']);
+ const played = new Set(chart.bars.flatMap((b) => b.rows.map((r) => r.piece)));
+ for (const id of played) expect(chart.pieces).toContain(id);
+ });
+
+ it('never invents an empty row for a kit: piece the chart does not use', () => {
+ const chart = parseDrumChart('kit: CR, RD, K\n\nK: o---');
+ expect(chart.pieces).toEqual(['K']);
+ });
+
it('falls back to kit order when kit: names nothing known', () => {
const chart = parseDrumChart('kit: cowbell, vuvuzela\n\nHH: xxxx\nK: o---');
expect(chart.pieces).toEqual(['HH', 'K']);
diff --git a/client/src/lib/drumPlayback.js b/client/src/lib/drumPlayback.js
index e0cf378aa7..63d7f44be4 100644
--- a/client/src/lib/drumPlayback.js
+++ b/client/src/lib/drumPlayback.js
@@ -283,9 +283,14 @@ export const createDrumPlayer = (chart, options = {}) => {
};
// Length of one LOOP pass: the music only, since the count-in belongs to pass 0.
const musicSec = () => schedule.totalSec - schedule.countInSec;
- // A pass with no music has nothing to advance past — a looping player must not
- // spin its rebase branch forever on one.
- const canLoop = () => !!schedule.loop && musicSec() > 0;
+ // A loop pass must have BOTH real duration AND at least one music event to
+ // advance past. A rest-only selected bar has duration but no events, so
+ // `firstMusicIdx()` would return the exhausted end index and the rebase branch
+ // would spin forever (a wedged tab, not just silence) — that's the case this
+ // event check exists for, distinct from the duration check.
+ const canLoop = () => !!schedule.loop
+ && musicSec() > 0
+ && schedule.events.some((ev) => !ev.countIn);
// A cursor walks one list (events or clicks) forward through loop passes. Each
// walker keeps its OWN `pass` because they advance at different rates: the
diff --git a/client/src/lib/drumPlayer.test.js b/client/src/lib/drumPlayer.test.js
index ce753544ab..692b5c41bc 100644
--- a/client/src/lib/drumPlayer.test.js
+++ b/client/src/lib/drumPlayer.test.js
@@ -182,9 +182,7 @@ describe('createDrumPlayer', () => {
expect(player.isPlaying()).toBe(false);
});
- it('a loop range over an all-rest chart does not spin forever', async () => {
- // canLoop guards the rebase: a pass with no music would otherwise loop the
- // scheduler's rebase branch endlessly.
+ it('loops a single-bar range without spinning', async () => {
const player = createDrumPlayer(parseDrumChart('subdivision: 1\n\nHH: x---'), {
loopBars: { from: 1, to: 1 }, countInBars: 0,
});
@@ -193,4 +191,18 @@ describe('createDrumPlayer', () => {
expect(player.isPlaying()).toBe(true);
player.stop();
});
+
+ it('does NOT wedge when the looped bar is all rests but the chart has hits', async () => {
+ // canLoop must check for a music EVENT, not just pass duration: a rest-only
+ // selected bar has real duration but no events, so the pass-rebase branch
+ // would reset to the exhausted index and spin forever (a frozen tab, not
+ // just silence). The chart's OTHER bar has hits, so Play is enabled.
+ const chart = parseDrumChart('tempo: 240\nsubdivision: 1\n\n# A\nK: o---\n\n# B\nK: ----');
+ const player = createDrumPlayer(chart, { loopBars: { from: 2, to: 2 }, countInBars: 1 });
+ await player.play();
+ // Would never return if the rebase spun; the count-in still sounds.
+ drive(3);
+ expect(audio.oscillators.length).toBeGreaterThan(0);
+ player.stop();
+ });
});
diff --git a/client/src/pages/SongBookViewer.test.jsx b/client/src/pages/SongBookViewer.test.jsx
index 2bc233d95c..4df9c4027b 100644
--- a/client/src/pages/SongBookViewer.test.jsx
+++ b/client/src/pages/SongBookViewer.test.jsx
@@ -269,11 +269,15 @@ K: o - - - - - o -`;
await waitFor(() => expect(screen.getByLabelText('BPM').value).toBe('20'));
});
- it('disables Play for an all-rest chart (bars parse, nothing sounds)', async () => {
+ it('disables Play for an all-rest chart, and space cannot route around it', async () => {
api.getSong.mockResolvedValue(drumSong({ content: { format: 'drum', text: 'HH: ----\nK: ----' } }));
renderPage();
const play = await screen.findByLabelText('Play along');
expect(play.disabled).toBe(true);
+ // The keyboard binding shares the button's gate — no AudioContext is
+ // touched (jsdom has none, so a start attempt would throw).
+ fireEvent.keyDown(window, { key: ' ' });
+ expect(screen.getByLabelText('Play along').disabled).toBe(true);
});
it('reveals the loop bar range only when looping is on', async () => {
diff --git a/server/lib/brainValidation.js b/server/lib/brainValidation.js
index 9ac01f4cff..f07b7d6899 100644
--- a/server/lib/brainValidation.js
+++ b/server/lib/brainValidation.js
@@ -577,10 +577,29 @@ export const songInstrumentEnum = z.enum(['guitar', 'piano', 'ukulele', 'bass',
// <DrumSheetView> — everything else goes through tabNotation/<TabSheetView>.
export const songContentFormatEnum = z.enum(['chordpro', 'tab', 'plain', 'drum']);
+// The SHAPES the write endpoints accept for those two fields — the enum above,
+// OR any other short slug. Deliberately wider than the enum, because brain songs
+// sync raw between installs on independent upgrade schedules (LWW, no Zod on
+// receive): a peer running a newer version can hand this install an instrument or
+// format it has never heard of, and that value is already sitting in the record.
+// If a write then rejected it, the user could not edit that song AT ALL — every
+// save would 400 on a field they never touched — and the client's own
+// "keep an unknown stored value in the select" behavior (see
+// `withStoredOption` in client/src/components/songbook/constants.js) would be
+// unable to round-trip what it preserved.
+//
+// The enum stays the source of truth for what the UI OFFERS and what the client
+// mirrors; this is only the acceptance boundary. The slug pattern keeps it from
+// becoming a free-text field: lowercase alphanumerics plus `-`/`_`, ≤32 chars,
+// which is what every value either side has ever used.
+const songForwardCompatSlug = z.string().trim().regex(/^[a-z0-9][a-z0-9_-]{0,31}$/);
+const songInstrumentValue = z.union([songInstrumentEnum, songForwardCompatSlug]);
+const songContentFormatValue = z.union([songContentFormatEnum, songForwardCompatSlug]);
+
// Nested content object — named so the update schema below can rebuild it
// defaults-free (partialWithoutDefaults only strips TOP-LEVEL field defaults).
const songContentSchema = z.object({
- format: songContentFormatEnum.optional().default('tab'),
+ format: songContentFormatValue.optional().default('tab'),
text: z.string().max(200000).optional().default('')
});
@@ -591,7 +610,7 @@ const songContentSchema = z.object({
export const songInputSchema = z.object({
title: z.string().trim().min(1).max(300),
artist: z.string().trim().max(300).optional().default(''),
- instrument: songInstrumentEnum.optional().default('guitar'),
+ instrument: songInstrumentValue.optional().default('guitar'),
stage: songStageEnum.optional().default('new'),
tags: z.array(z.string().trim().min(1).max(50)).max(50).optional().default([]),
key: z.string().trim().max(20).optional().default(''),
diff --git a/server/lib/brainValidation.test.js b/server/lib/brainValidation.test.js
index dd9f972169..07e8438d25 100644
--- a/server/lib/brainValidation.test.js
+++ b/server/lib/brainValidation.test.js
@@ -813,5 +813,28 @@ describe('brainValidation.js', () => {
expect(result.data.instrument).toBe('guitar');
expect(result.data.content).toEqual({ format: 'tab', text: '' });
});
+
+ // Brain songs sync raw between installs on independent upgrade schedules
+ // (LWW, no Zod on receive), so a newer peer's instrument/format can already be
+ // sitting in a local record. A write that rejected it would make that song
+ // permanently uneditable — every save 400ing on a field the user never
+ // touched — so the WRITE boundary is deliberately wider than the enum.
+ it('accepts (round-trips) an unknown instrument/format from a newer peer', () => {
+ const result = songInputSchema.safeParse({
+ title: 'Synced Song',
+ instrument: 'hurdy-gurdy',
+ content: { format: 'futureformat', text: 'x' },
+ });
+ expect(result.success).toBe(true);
+ expect(result.data.instrument).toBe('hurdy-gurdy');
+ expect(result.data.content.format).toBe('futureformat');
+ });
+
+ it('keeps the forward-compat slot a SLUG, not free text', () => {
+ for (const bad of ['Has Spaces', 'UPPER', 'x'.repeat(33), '', '-leading', 'sym!bol']) {
+ const result = songInputSchema.safeParse({ title: 'T', instrument: bad });
+ expect(result.success, `instrument "${bad}"`).toBe(false);
+ }
+ });
});
});