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): stop migration 214 deleting user-added score parts

[._.]/ Adam Eivy93e3e7f39972
diff --git a/scripts/migrations/214-fix-hey-ho-fourth-phrase.js b/scripts/migrations/214-fix-hey-ho-fourth-phrase.js
index 8e2787a928..196fc8cbe6 100644
--- a/scripts/migrations/214-fix-hey-ho-fourth-phrase.js
+++ b/scripts/migrations/214-fix-hey-ho-fourth-phrase.js
@@ -50,6 +50,13 @@ export const NEW_HEY_HO_SCORE = NEW_SEED.score;
export const NEW_HEY_HO_SCORE_PARTS = NEW_SEED.scoreParts;
export const NEW_HEY_HO_NOTATION = NEW_SEED.notation;
export const NEW_HEY_HO_LYRICS = NEW_SEED.sections.find((s) => s.id === SECTION_ID).lyrics;
+// The two voice-guidance strings the four-phrase melody invalidates: voice-3 said
+// "the three phrases stack", and voice-4 described itself as a unison double of
+// the lead (which is what an entry at bar 7 of a SIX-bar round is). With a real
+// fourth phrase they are both wrong, so they migrate like `notation` does.
+const newLayerNotes = (id) => NEW_SEED.layers.find((l) => l.id === id).notes;
+export const NEW_HEY_HO_VOICE_3_NOTES = newLayerNotes('voice-3');
+export const NEW_HEY_HO_VOICE_4_NOTES = newLayerNotes('voice-4');
// The OLD shipped melody — frozen exactly as SEED_ROUNDS shipped it AFTER #2105
// but BEFORE #3238 (D-centered and correct, but only three phrases / 6 bars).
@@ -78,6 +85,14 @@ export const OLD_HEY_HO_LYRICS =
export const OLD_HEY_HO_NOTATION =
'A round in up to six voices (Ravenscroft\'s Pammelia, 1609). New voices enter one two-bar phrase behind the last. Centered on D with no key signature — all naturals (D Dorian / natural minor), no B. Melody after the Wikibooks Songbook and Kodály teaching transcriptions, transposed to a D tonal center.';
+// The OLD shipped voice guidance — frozen exactly as it read before #3238. Each
+// gates its own layer's correction, so a user who rewrote one note keeps it while
+// the other still gets fixed.
+export const OLD_HEY_HO_VOICE_3_NOTES =
+ 'Enters at "Still I will be merry" (phrase 3); the three phrases stack into the full minor chord.';
+export const OLD_HEY_HO_VOICE_4_NOTES =
+ 'Optional fourth entry: comes in as Voice 1 loops back to the top, doubling the lead in unison or an octave up.';
+
const fileExists = (path) => stat(path).then(() => true, (err) => {
if (err.code === 'ENOENT') return false;
throw err;
@@ -110,13 +125,24 @@ export default {
let fixedScore = false;
let fixedLyrics = false;
let fixedNotation = false;
+ let fixedLayers = 0;
- // Replace score + scoreParts only when the stored score is still the exact
- // 6-bar shipped string. Deep-clone the scoreParts so the persisted record
+ // Replace score + the SHIPPED canon voices only when the stored score is still
+ // the exact 6-bar shipped string. Deep-clone the parts so the persisted record
// can't share array/object identity with the in-memory seed.
+ //
+ // Parts the user added themselves are preserved. `scoreParts` is independently
+ // editable (SongScoreParts' "Add part" and the AI-derive path both append a
+ // part with a generated id), so a stock melody can still carry a hand-written
+ // bass line. Rewriting the whole array — as a naive replace would — would
+ // delete that work with no backup, which is exactly the clobbering this
+ // migration's per-field gating exists to prevent.
if (round.score === OLD_HEY_HO_SCORE_6BAR) {
round.score = NEW_HEY_HO_SCORE;
- round.scoreParts = NEW_HEY_HO_SCORE_PARTS.map((p) => ({ ...p }));
+ const shippedIds = new Set(NEW_HEY_HO_SCORE_PARTS.map((p) => p.id));
+ const userParts = (Array.isArray(round.scoreParts) ? round.scoreParts : [])
+ .filter((p) => p && !shippedIds.has(p.id));
+ round.scoreParts = [...NEW_HEY_HO_SCORE_PARTS.map((p) => ({ ...p })), ...userParts];
fixedScore = true;
}
@@ -136,14 +162,32 @@ export default {
fixedNotation = true;
}
- if (!fixedScore && !fixedLyrics && !fixedNotation) {
+ // Correct the stale voice guidance the same way — per layer, on its own old
+ // string. Without this an upgraded install would carry a real fourth canon
+ // voice while its Voice 4 panel still told the singer to double the lead in
+ // unison, and Voice 3 still claimed the round had three phrases.
+ const staleLayerNotes = [
+ ['voice-3', OLD_HEY_HO_VOICE_3_NOTES, NEW_HEY_HO_VOICE_3_NOTES],
+ ['voice-4', OLD_HEY_HO_VOICE_4_NOTES, NEW_HEY_HO_VOICE_4_NOTES],
+ ];
+ for (const [layerId, oldNotes, nextNotes] of staleLayerNotes) {
+ const layer = Array.isArray(round.layers)
+ ? round.layers.find((l) => l && l.id === layerId)
+ : null;
+ if (layer && layer.notes === oldNotes) {
+ layer.notes = nextNotes;
+ fixedLayers += 1;
+ }
+ }
+
+ if (!fixedScore && !fixedLyrics && !fixedNotation && !fixedLayers) {
console.log('📦 migration 214: Hey Ho already has its fourth phrase or is customized; leaving it untouched.');
return { updated: 0, reason: 'already-applied' };
}
round.updatedAt = new Date().toISOString();
await writeFile(path, JSON.stringify(doc, null, 2) + '\n');
- console.log(`📦 migration 214: extended Hey Ho Nobody Home (score: ${fixedScore}, lyrics: ${fixedLyrics}, notation: ${fixedNotation}).`);
- return { updated: 1, fixedScore, fixedLyrics, fixedNotation };
+ console.log(`📦 migration 214: extended Hey Ho Nobody Home (score: ${fixedScore}, lyrics: ${fixedLyrics}, notation: ${fixedNotation}, layers: ${fixedLayers}).`);
+ return { updated: 1, fixedScore, fixedLyrics, fixedNotation, fixedLayers };
},
};
diff --git a/scripts/migrations/214-fix-hey-ho-fourth-phrase.test.js b/scripts/migrations/214-fix-hey-ho-fourth-phrase.test.js
index f7e5f1c4cc..8b582a283a 100644
--- a/scripts/migrations/214-fix-hey-ho-fourth-phrase.test.js
+++ b/scripts/migrations/214-fix-hey-ho-fourth-phrase.test.js
@@ -7,10 +7,14 @@ import migration, {
OLD_HEY_HO_SCORE_6BAR,
OLD_HEY_HO_LYRICS,
OLD_HEY_HO_NOTATION,
+ OLD_HEY_HO_VOICE_3_NOTES,
+ OLD_HEY_HO_VOICE_4_NOTES,
NEW_HEY_HO_SCORE,
NEW_HEY_HO_SCORE_PARTS,
NEW_HEY_HO_LYRICS,
NEW_HEY_HO_NOTATION,
+ NEW_HEY_HO_VOICE_3_NOTES,
+ NEW_HEY_HO_VOICE_4_NOTES,
} from './214-fix-hey-ho-fourth-phrase.js';
import { OLD_HEY_HO_SCORE as PRE_2105_SCORE } from './158-fix-hey-ho-melody.js';
import { SEED_ROUNDS } from '../../server/services/rounds.js';
@@ -21,6 +25,7 @@ const writeJson = (path, value) => writeFileSync(path, JSON.stringify(value, nul
const readJson = (path) => JSON.parse(readFileSync(path, 'utf-8'));
const findRound = (path, id) => readJson(path).rounds.find((r) => r.id === id);
const findLyrics = (path) => findRound(path, ROUND_ID).sections.find((s) => s.id === SECTION_ID).lyrics;
+const findLayer = (path, id) => findRound(path, ROUND_ID).layers.find((l) => l.id === id);
// A stored record in the exact pre-#3238 shipped shape.
const oldRecord = () => ({
@@ -29,6 +34,12 @@ const oldRecord = () => ({
scoreParts: [{ id: 'part-heyho-v2' }, { id: 'part-heyho-v3' }],
notation: OLD_HEY_HO_NOTATION,
sections: [{ id: SECTION_ID, label: 'Round', lyrics: OLD_HEY_HO_LYRICS }],
+ layers: [
+ { id: 'voice-1', notes: 'Starts the round and sings it straight through. Everyone learns this line first.' },
+ { id: 'voice-2', notes: 'Enters as Voice 1 reaches "Meat nor drink…" (phrase 2) — one full phrase behind the lead.' },
+ { id: 'voice-3', notes: OLD_HEY_HO_VOICE_3_NOTES },
+ { id: 'voice-4', notes: OLD_HEY_HO_VOICE_4_NOTES },
+ ],
});
describe('migration 214 — Hey Ho Nobody Home fourth phrase + lyric', () => {
@@ -58,6 +69,14 @@ describe('migration 214 — Hey Ho Nobody Home fourth phrase + lyric', () => {
// migration would then write `undefined` over a user's lyrics.
expect(typeof NEW_HEY_HO_LYRICS).toBe('string');
expect(NEW_HEY_HO_LYRICS).toContain('Hey, hey, ho.');
+ // Same vacuous-pass hazard for the layer notes, which are looked up by id.
+ expect(NEW_HEY_HO_VOICE_3_NOTES).toBe(seed.layers.find((l) => l.id === 'voice-3').notes);
+ expect(NEW_HEY_HO_VOICE_4_NOTES).toBe(seed.layers.find((l) => l.id === 'voice-4').notes);
+ expect(NEW_HEY_HO_VOICE_3_NOTES).not.toMatch(/three phrases/i);
+ expect(NEW_HEY_HO_VOICE_4_NOTES).not.toMatch(/unison/i);
+ // And the frozen old strings must be the pre-fix text, not the new text.
+ expect(OLD_HEY_HO_VOICE_3_NOTES).toMatch(/three phrases/i);
+ expect(OLD_HEY_HO_VOICE_4_NOTES).toMatch(/unison/i);
});
// Drift guard (b): the frozen OLD constants are the post-158 / pre-3238 shipped
@@ -111,14 +130,72 @@ describe('migration 214 — Hey Ho Nobody Home fourth phrase + lyric', () => {
expect(round.updatedAt).toBeTruthy();
});
- it('persists the fourth canon voice without sharing identity with the in-memory seed', async () => {
+ it('persists the fourth canon voice', async () => {
writeJson(roundsPath, { rounds: [oldRecord()] });
await migration.up({ rootDir });
const round = findRound(roundsPath, ROUND_ID);
expect(round.scoreParts).toHaveLength(3);
expect(round.scoreParts.map((p) => p.id)).toEqual(['part-heyho-v2', 'part-heyho-v3', 'part-heyho-v4']);
- // Deep-cloned on write, so mutating the persisted copy can't reach the seed.
- expect(round.scoreParts[2]).not.toBe(NEW_HEY_HO_SCORE_PARTS[2]);
+ });
+
+ // The write deep-clones the shipped parts. Re-reading the file can't prove that
+ // (JSON.parse always yields fresh objects, so an identity assertion there passes
+ // whether or not the clone exists) — what it protects is the in-memory seed,
+ // which a later migration in the same process would otherwise share and mutate.
+ it('does not mutate the in-memory seed while writing the record', async () => {
+ const before = JSON.parse(JSON.stringify(NEW_HEY_HO_SCORE_PARTS));
+ writeJson(roundsPath, { rounds: [oldRecord()] });
+ await migration.up({ rootDir });
+ expect(NEW_HEY_HO_SCORE_PARTS).toEqual(before);
+ });
+
+ it('keeps a user-added score part when it extends the shipped canon voices', async () => {
+ // scoreParts is independently editable (Add part / AI-derive), so a stock
+ // melody can carry a hand-written part. Replacing the whole array would
+ // silently delete it.
+ const mine = { id: 'part-mine-a1b2', label: 'My bass line', role: '', score: 'clef: bass\n\n| D3w |' };
+ const record = oldRecord();
+ record.scoreParts = [{ id: 'part-heyho-v2' }, { id: 'part-heyho-v3' }, mine];
+ writeJson(roundsPath, { rounds: [record] });
+ await migration.up({ rootDir });
+
+ const parts = findRound(roundsPath, ROUND_ID).scoreParts;
+ expect(parts.map((p) => p.id)).toEqual(['part-heyho-v2', 'part-heyho-v3', 'part-heyho-v4', 'part-mine-a1b2']);
+ expect(parts.at(-1)).toEqual(mine);
+ });
+
+ it('corrects the stale voice guidance the fourth phrase invalidates', async () => {
+ writeJson(roundsPath, { rounds: [oldRecord()] });
+ const result = await migration.up({ rootDir });
+ expect(result).toMatchObject({ updated: 1, fixedLayers: 2 });
+ // Voice 4 is a real staggered entry now, not a unison double of the lead.
+ expect(findLayer(roundsPath, 'voice-4').notes).toBe(NEW_HEY_HO_VOICE_4_NOTES);
+ expect(findLayer(roundsPath, 'voice-4').notes).not.toMatch(/unison/i);
+ // Voice 3 no longer claims the round is three phrases long.
+ expect(findLayer(roundsPath, 'voice-3').notes).toBe(NEW_HEY_HO_VOICE_3_NOTES);
+ expect(findLayer(roundsPath, 'voice-3').notes).not.toMatch(/three phrases/i);
+ // An untouched layer stays untouched.
+ expect(findLayer(roundsPath, 'voice-1').notes).toBe(oldRecord().layers[0].notes);
+ });
+
+ it('never clobbers voice guidance the user rewrote, and fixes the other layer', async () => {
+ const myNotes = 'I come in on the last line, an octave down.';
+ const record = oldRecord();
+ record.layers = record.layers.map((l) => (l.id === 'voice-4' ? { ...l, notes: myNotes } : l));
+ writeJson(roundsPath, { rounds: [record] });
+ const result = await migration.up({ rootDir });
+ expect(result).toMatchObject({ fixedLayers: 1 });
+ expect(findLayer(roundsPath, 'voice-4').notes).toBe(myNotes);
+ expect(findLayer(roundsPath, 'voice-3').notes).toBe(NEW_HEY_HO_VOICE_3_NOTES);
+ });
+
+ it('tolerates a record with no layers array', async () => {
+ const record = oldRecord();
+ delete record.layers;
+ writeJson(roundsPath, { rounds: [record] });
+ const result = await migration.up({ rootDir });
+ expect(result).toMatchObject({ updated: 1, fixedScore: true, fixedLayers: 0 });
+ expect(findRound(roundsPath, ROUND_ID).score).toBe(NEW_HEY_HO_SCORE);
});
it('is idempotent across re-runs', async () => {
diff --git a/server/services/rounds.js b/server/services/rounds.js
index 9f918a5735..1d5d0d8339 100644
--- a/server/services/rounds.js
+++ b/server/services/rounds.js
@@ -773,7 +773,7 @@ export const SEED_ROUNDS = [
layers: [
{ id: 'voice-1', label: 'Voice 1 (lead)', part: 'Any', notes: 'Starts the round and sings it straight through. Everyone learns this line first.' },
{ id: 'voice-2', label: 'Voice 2', part: 'Any', notes: 'Enters as Voice 1 reaches "Meat nor drink…" (phrase 2) — one full phrase behind the lead.' },
- { id: 'voice-3', label: 'Voice 3', part: 'Any', notes: 'Enters at "Still I will be merry" (phrase 3); the three phrases stack into the full minor chord.' },
+ { id: 'voice-3', label: 'Voice 3', part: 'Any', notes: 'Enters at "Still I will be merry" (phrase 3); by here the stacked phrases fill out the minor chord.' },
{ id: 'voice-4', label: 'Voice 4', part: 'Any', notes: 'Enters at the closing "Hey, hey, ho" (phrase 4, bar 7) — the last of the four staggered entries, filling the round out.' },
],
references: [],