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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
feat([issue-2912]): make conflict-journal content hash schema-version-aware
[._.]/ Adam Eivy0e83158a1018
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.mdindex 6c7ba0f428..d54d77cd7e 100644--- a/.changelog/NEXT.md+++ b/.changelog/NEXT.md@@ -2,6 +2,7 @@## Music studio+- **[issue-2912] Chiptune score edits on two synced machines now show up as a real conflict** — if you compose a chiptune score for the same track on two federated installs before they sync with each other, that divergence now surfaces in Sharing → Conflicts (so you can review and choose which version to keep) instead of one edit silently overwriting the other.- **[issue-2911] Compose looping 8-bit game music from a prompt** — the Track editor gains a "Chiptune score" generation mode alongside the on-device audio models: describe the music, pick any configured AI provider/model, and it composes a structured NES-style score (two pulse channels, triangle bass, noise drums) that plays as a seamless loop right in the browser. Revise it conversationally ("more drums", "make it spookier") or start fresh, render takes into the track's regular render history as audio loops, and publish straight into a managed game app's repo (default `game/assets/music/`) as a game-ready OGG plus its editable score source — so a Godot game can drop the loop in as background music. The provider pin and publish target are remembered between sessions.## Sprite Managerdiff --git a/server/lib/conflictJournal.js b/server/lib/conflictJournal.jsindex df273d818a..a8f9219c74 100644--- a/server/lib/conflictJournal.js+++ b/server/lib/conflictJournal.js@@ -89,12 +89,62 @@ const MEDIA_COLLECTION_SCALAR_FIELDS = Object.freeze(['name', 'description', 'co// (local and remote both differ from a base computed without renders). Excluding// it from the hash keeps base-hash compatibility; render-only divergences are// LWW-ordered by `updatedAt` regardless, and renders stays restorable (below).-// track: renders (server-managed take history) and the additive chiptune-// fields (#2911) stay out of the content hash — an older peer's copy simply-// lacks the keys, and including them would leave mixed-version peers with a-// permanent base-hash mismatch (the churn the wire comments warn about).-// Score changes still propagate via the whole-record LWW merge (updatedAt).-const HASH_EXCLUDED_FIELDS = Object.freeze({ issue: ['number'], track: ['renders', 'chiptuneScore', 'chiptunePrompt'] });+// This is for fields that are GENUINELY server-managed (mutate without a user+// edit) and must NEVER participate in conflict detection — contrast with+// HASH_FIELDS below, for user-authored additive fields that SHOULD eventually+// participate once it's safe to.+const HASH_EXCLUDED_FIELDS = Object.freeze({ issue: ['number'], track: ['renders'] });++// User-authored fields that were added to a kind's wire shape at a later+// point, keyed by a LOCAL version number (this mechanism's own — deliberately+// independent of PORTOS_SCHEMA_VERSIONS in schemaVersions.js, which gates+// cross-peer WIRE compatibility, a different concern from "which fields does+// the local base-hash store already know about"). Unlike HASH_EXCLUDED_FIELDS+// these DO belong in conflict detection — the problem #2912 solves isn't+// "never hash this," it's "don't let adding it retroactively invalidate every+// base hash stamped before it existed." `contentHashForRecord`'s `maxVersion`+// option drops any field introduced after that version; `setSyncBaseHash`+// always stamps at the CURRENT (highest defined) version for the kind, and+// `detectConflict` recomputes local/remote at the STORED base's version so+// the comparison is apples-to-apples. A base hash predating the field will+// therefore MISS a divergence limited to that field until the next successful+// sync re-stamps it at the current version — the same one-time-miss tradeoff+// this file already accepts for `baseHash == null` (see the module doc+// comment above).+const HASH_FIELDS = Object.freeze({+ // v3 (#2911): chiptuneScore/chiptunePrompt. Previously permanently excluded+ // via HASH_EXCLUDED_FIELDS; migrated here by #2912 so two peers that both+ // understand the field (base stamped at v3+) get real conflict detection on+ // it instead of silent LWW. The version number "3" is chosen to match+ // PORTOS_SCHEMA_VERSIONS.tracks for a human reading both files side by side+ // — nothing enforces the two staying in lockstep; it's fine for them to+ // diverge (a wire-compat bump doesn't always change what the local hash+ // cares about, and vice versa).+ track: Object.freeze({ 3: Object.freeze(['chiptuneScore', 'chiptunePrompt']) }),+});++// Precomputed field→version lookup per kind, inverted from HASH_FIELDS once+// at module load (HASH_FIELDS is static, so rebuilding this per hash call —+// on every record write in the system — would be pure waste).+const HASH_FIELD_VERSION_BY_KIND = Object.freeze(Object.fromEntries(+ Object.entries(HASH_FIELDS).map(([kind, versioned]) => {+ const byField = new Map();+ for (const [v, fields] of Object.entries(versioned)) {+ for (const f of fields) byField.set(f, Number(v));+ }+ return [kind, byField];+ }),+));++// The highest HASH_FIELDS version defined for `kind` — the version+// `setSyncBaseHash` stamps new base hashes at. Kinds with no HASH_FIELDS+// entry have nothing to gate; the fallback of 1 is inert for them (no field+// is ever excluded by a maxVersion check that never finds a match).+function currentHashVersionForKind(kind) {+ const versioned = HASH_FIELDS[kind];+ if (!versioned) return 1;+ return Math.max(1, ...Object.keys(versioned).map(Number));+}/*** sha256 of the canonical content projection. Reuses sanitizeRecordForWire +@@ -106,16 +156,28 @@ const HASH_EXCLUDED_FIELDS = Object.freeze({ issue: ['number'], track: ['renders* the base hash stays consistent across peers. Returns null when the record has* no wire form (ephemeral non-tombstone, or invalid) — callers treat a null* hash as "cannot compare".+ *+ * `maxVersion` (see HASH_FIELDS above) restricts the hash to fields introduced+ * at or below that version — pass the STORED base hash's version when+ * recomputing for comparison so an old base isn't compared against a+ * differently-shaped hash. Omitted (the default, used when stamping a NEW base+ * hash) means "include every field at the current version."*/-export function contentHashForRecord(kind, record) {+export function contentHashForRecord(kind, record, { maxVersion } = {}) {const wire = sanitizeRecordForWire(kind, record);if (!wire) return null;let hashInput = wire;if (kind === 'mediaCollection') {hashInput = projectCollectionScalars(wire);- } else if (HASH_EXCLUDED_FIELDS[kind]) {- const excluded = HASH_EXCLUDED_FIELDS[kind];- hashInput = Object.fromEntries(Object.entries(wire).filter(([k]) => !excluded.includes(k)));+ } else if (HASH_EXCLUDED_FIELDS[kind] || HASH_FIELD_VERSION_BY_KIND[kind]) {+ const excluded = HASH_EXCLUDED_FIELDS[kind] || [];+ const introducedAt = HASH_FIELD_VERSION_BY_KIND[kind];+ hashInput = Object.fromEntries(Object.entries(wire).filter(([k]) => {+ if (excluded.includes(k)) return false;+ const fieldVersion = introducedAt?.get(k);+ if (fieldVersion == null) return true; // not a versioned field — always included+ return maxVersion == null || fieldVersion <= maxVersion;+ }));}return createHash('sha256').update(canonicalStringify(hashInput)).digest('hex');}@@ -134,7 +196,12 @@ function projectCollectionScalars(wire) {// ---- base-hash side store (in-memory cache, batched write-through) -----let _baseHashes = null; // Map<`${kind}:${id}`, sha256>+// Map<`${kind}:${id}`, sha256 | { h: sha256, v: number }>. A bare string is a+// legacy entry written before #2912 (or a v1 entry — the two are equivalent,+// see readBaseEntry's "absent = v1" fallback); new entries are always written+// as `{ h, v }` so the hash-fields version travels with the hash it was+// computed under.+let _baseHashes = null;let _loadPromise = null;let _baseDirty = false;let _flushTail = Promise.resolve();@@ -157,17 +224,45 @@ async function ensureBaseLoaded() {return _loadPromise;}-export async function getSyncBaseHash(kind, id) {+// Normalize a raw map value (legacy bare string, or `{ h, v }`) to+// `{ hash, version }`. Absent version defaults to 1 — the pre-#2912 hash+// never included any HASH_FIELDS-versioned field, so it's the correct floor.+function readBaseEntry(raw) {+ if (raw == null) return null;+ if (typeof raw === 'string') return { hash: raw, version: 1 };+ const version = Number.isInteger(raw.v) ? raw.v : 1;+ return { hash: raw.h ?? null, version };+}++/** Shared lookup behind getSyncBaseHash and detectConflict — one place that+ * loads the map and normalizes the stored entry. */+async function readStoredBaseEntry(kind, id) {const map = await ensureBaseLoaded();- return map.get(baseKey(kind, id)) ?? null;+ return readBaseEntry(map.get(baseKey(kind, id)));+}++/** The stored base hash for (kind, id), or null. String-only for backward+ * compatibility with every existing caller/test — use detectConflict (which+ * reads the paired version internally) when the version matters. */+export async function getSyncBaseHash(kind, id) {+ return (await readStoredBaseEntry(kind, id))?.hash ?? null;}-/** Set the base hash in memory (write-through deferred to flushBaseHashes). */-export async function setSyncBaseHash(kind, id, hash) {+/**+ * Set the base hash in memory (write-through deferred to flushBaseHashes).+ * Stamps the CURRENT hash-fields version for `kind` (see+ * currentHashVersionForKind) unless `version` is explicitly given — tests use+ * the override to plant a hash computed at an older version, simulating a+ * base stamped before an additive HASH_FIELDS entry existed.+ */+export async function setSyncBaseHash(kind, id, hash, { version } = {}) {const map = await ensureBaseLoaded();if (!hash) return;- if (map.get(baseKey(kind, id)) === hash) return;- map.set(baseKey(kind, id), hash);+ const v = Number.isInteger(version) ? version : currentHashVersionForKind(kind);+ const key = baseKey(kind, id);+ const existing = readBaseEntry(map.get(key));+ if (existing?.hash === hash && existing?.version === v) return;+ map.set(key, { h: hash, v });_baseDirty = true;}@@ -288,11 +383,19 @@ export async function withBaseHashFlushBatch(fn) {/*** Detect a true 3-way divergence for a record about to be overwritten.* Returns `{ isConflict, baseHash, localHash, remoteHash }`.+ *+ * local/remote are recomputed at the STORED base's hash-fields version (see+ * HASH_FIELDS), not the current one — so a base stamped before an additive+ * field existed is compared apples-to-apples against the same field set it+ * was originally built from, instead of unconditionally mismatching the+ * moment the field starts appearing in the wire shape.*/export async function detectConflict({ kind, id, local, remote }) {- const baseHash = await getSyncBaseHash(kind, id);- const localHash = contentHashForRecord(kind, local);- const remoteHash = contentHashForRecord(kind, remote);+ const base = await readStoredBaseEntry(kind, id);+ const baseHash = base?.hash ?? null;+ const maxVersion = base ? base.version : undefined;+ const localHash = contentHashForRecord(kind, local, { maxVersion });+ const remoteHash = contentHashForRecord(kind, remote, { maxVersion });const isConflict = baseHash != null && localHash != null && remoteHash != null&& localHash !== baseHash && remoteHash !== baseHash && localHash !== remoteHash;return { isConflict, baseHash, localHash, remoteHash };@@ -322,9 +425,10 @@ export const RESTORABLE_FIELDS = Object.freeze({// `renders` IS restorable (a conflict restore must bring back the local render// history) but is EXCLUDED FROM THE CONTENT HASH — see HASH_EXCLUDED_FIELDS for// why (base-hash compatibility for this additive backfilled field).- // `chiptuneScore`/`chiptunePrompt` (#2911) follow the `renders` pattern:- // restorable (a conflict restore must bring back the local composition) but- // excluded from the content hash for cross-version base-hash stability.+ // `chiptuneScore`/`chiptunePrompt` (#2911) are also restorable and, as of+ // #2912, DO participate in the content hash via the version-gated HASH_FIELDS+ // mechanism (see above) — a base hash predating the field just compares at+ // its own stored version until the next sync re-stamps it.track: ['title', 'albumId', 'artistId', 'artist', 'lyrics', 'prompt', 'engine', 'modelId', 'durationSec', 'audioFilename', 'renders', 'chiptuneScore', 'chiptunePrompt'],// Issue: the user-authored content the merge can restore. `stages` carries// the bulk of the work (prose, comic pages, render metadata). Server-owned /diff --git a/server/lib/conflictJournal.test.js b/server/lib/conflictJournal.test.jsindex 5b279333d0..cf53c259ce 100644--- a/server/lib/conflictJournal.test.js+++ b/server/lib/conflictJournal.test.js@@ -40,6 +40,12 @@ const uni = (over = {}) => ({ id: 'u-1', name: 'X', starterPrompt: 'base', updat// the issue conflict hash (renumber-managed; see HASH_EXCLUDED_FIELDS).const iss = (over = {}) => ({ id: 'iss-1', seriesId: 'ser-1', title: 'T', number: 1, status: 'draft', stages: {}, updatedAt: '2026-05-01T00:00:00Z', ...over });+// Minimal track-shaped record — sanitizeRecordForWire('track', …) passes the+// content through verbatim (whole-record LWW). `chiptuneScore`/`chiptunePrompt`+// default to their "untouched" shape (null/'') so a fixture that omits them+// still round-trips exactly like a pre-#2911 track.+const trk = (over = {}) => ({ id: 't-1', title: 'Song', albumId: 'al-1', artistId: 'ar-1', renders: [], chiptuneScore: null, chiptunePrompt: '', updatedAt: '2026-05-01T00:00:00Z', ...over });+const pendingEntries = async () => cj.conflictJournalStore().loadAll();describe('conflictJournal', () => {@@ -259,6 +265,51 @@ describe('conflictJournal', () => {expect(await cj.getSyncBaseHash('issue', 'iss-1')).toBe(cj.contentHashForRecord('issue', remote));});+ describe('track content hash — version-gated additive fields (#2912)', () => {+ it('still ignores renders (server-managed take history, permanently excluded)', () => {+ const base = cj.contentHashForRecord('track', trk());+ const withRenders = cj.contentHashForRecord('track', trk({ renders: [{ id: 'r-1', url: 'a.wav' }] }));+ expect(withRenders).toBe(base);+ });++ it('at the current version, chiptuneScore/chiptunePrompt DO participate in the hash (no longer excluded)', () => {+ const base = cj.contentHashForRecord('track', trk());+ const withScore = cj.contentHashForRecord('track', trk({ chiptuneScore: { bpm: 120 }, chiptunePrompt: 'farm loop' }));+ expect(withScore).not.toBe(base);+ });++ it('a base hash stamped BEFORE the field existed (version 2) does not churn when only chiptuneScore differs', async () => {+ // Simulate the pre-#2912 base: hashed with maxVersion 2, so chiptuneScore/+ // chiptunePrompt (introduced at tracks v3) were never part of it — the+ // same shape `HASH_EXCLUDED_FIELDS` used to produce permanently.+ const legacyBase = trk({ chiptuneScore: null, chiptunePrompt: '' });+ const legacyHash = cj.contentHashForRecord('track', legacyBase, { maxVersion: 2 });+ await cj.setSyncBaseHash('track', 't-1', legacyHash, { version: 2 });++ // Nobody has touched chiptuneScore since; only unrelated fields exist+ // identically on both sides.+ const local = trk();+ const remote = trk();+ const { isConflict, baseHash, localHash } = await cj.detectConflict({ kind: 'track', id: 't-1', local, remote });+ expect(baseHash).toBe(legacyHash);+ expect(localHash).toBe(legacyHash); // compared at the STORED (v2) version — no false divergence+ expect(isConflict).toBe(false);+ });++ it('two peers independently composing a chiptune score off a CURRENT-version base trips a real conflict', async () => {+ const base = trk({ chiptuneScore: null, chiptunePrompt: '' });+ // setSyncBaseHash with no explicit version stamps the CURRENT (v3) version.+ await cj.setSyncBaseHash('track', 't-1', cj.contentHashForRecord('track', base));++ const local = trk({ chiptuneScore: { bpm: 120 }, chiptunePrompt: 'mine', updatedAt: '2026-05-02T00:00:00Z' });+ const remote = trk({ chiptuneScore: { bpm: 90 }, chiptunePrompt: 'theirs', updatedAt: '2026-05-03T00:00:00Z' });+ await cj.maybeJournalBeforeOverwrite({ kind: 'track', id: 't-1', local, remote, source: { via: 'sync' } });+ const entries = await pendingEntries();+ expect(entries).toHaveLength(1);+ expect(entries[0]).toMatchObject({ recordKind: 'track', recordId: 't-1', status: 'pending' });+ });+ });+it('does NOT journal when the local side is a tombstone (no content to lose)', async () => {const base = uni({ starterPrompt: 'base' });await cj.setSyncBaseHash('universe', 'u-1', cj.contentHashForRecord('universe', base));diff --git a/server/lib/schemaVersions.js b/server/lib/schemaVersions.jsindex 862e88b104..2276373583 100644--- a/server/lib/schemaVersions.js+++ b/server/lib/schemaVersions.js@@ -228,8 +228,13 @@ export const PORTOS_SCHEMA_VERSIONS = Object.freeze({// looping 8-bit score). Version-gated for the identical strip-and-push-back// reason as v2: a ≤v2 peer re-sanitizing through its chiptune-unaware// `sanitizeTrack` would silently drop the score and LWW the loss back onto- // the composing peer. (The fields are also excluded from the conflict-journal- // content hash — see lib/conflictJournal.js HASH_EXCLUDED_FIELDS.)+ // the composing peer. (As of #2912 the fields DO participate in the+ // conflict-journal content hash, version-gated so it doesn't retroactively+ // invalidate a base hash stamped before they existed — see+ // lib/conflictJournal.js HASH_FIELDS, whose own version number for this+ // field was chosen to match this one for a human reading both files; the+ // two mechanisms are otherwise independent — HASH_FIELDS gates a purely+ // local hash-store concern, not cross-peer wire compatibility.)tracks: 3,// v1 = creative ingredients catalog (Postgres tables: catalog_scraps,// catalog_ingredients, catalog_ingredient_sources, catalog_ingredient_refs).diff --git a/server/services/sharing/peerSync.test.js b/server/services/sharing/peerSync.test.jsindex 7c4ae82352..c776a35492 100644--- a/server/services/sharing/peerSync.test.js+++ b/server/services/sharing/peerSync.test.js@@ -880,8 +880,10 @@ describe('peerSync', () => {// in-memory map and would pass even if the terminal flush never wrote. A// present-on-disk entry proves the batch's coalesced write actually ran// inside the awaited fan-out (no drain).+ // On-disk entries are `{ h, v }` (#2912 — the hash-fields version travels+ // with the hash it was computed under); `.h` is the plain hash string.const onDisk = JSON.parse(await readFile(join(tmp, 'sharing', 'sync_base_hashes.json'), 'utf8'));- expect(onDisk['universe:u1']).toBe(contentHashForRecord('universe', { id: 'u1', name: 'U1' }));+ expect(onDisk['universe:u1'].h).toBe(contentHashForRecord('universe', { id: 'u1', name: 'U1' }));});});@@ -1033,9 +1035,10 @@ describe('peerSync', () => {// Both records' stamps are in the single on-disk file — read it directly// (not getSyncBaseHash's in-memory map) so the assertion proves the// coalesced terminal flush wrote all N stamps before the helper returned.+ // On-disk entries are `{ h, v }` (#2912) — `.h` is the plain hash string.const onDisk = JSON.parse(await readFile(join(tmp, 'sharing', 'sync_base_hashes.json'), 'utf8'));- expect(onDisk['universe:u1']).toBe(contentHashForRecord('universe', { id: 'u1', name: 'U1' }));- expect(onDisk['universe:u2']).toBe(contentHashForRecord('universe', { id: 'u2', name: 'U2' }));+ expect(onDisk['universe:u1'].h).toBe(contentHashForRecord('universe', { id: 'u1', name: 'U1' }));+ expect(onDisk['universe:u2'].h).toBe(contentHashForRecord('universe', { id: 'u2', name: 'U2' }));});it('drops ephemeral records before computing the set-diff', async () => {diff --git a/server/services/tracks/file.test.js b/server/services/tracks/file.test.jsindex c587a1a891..c3b2040fd1 100644--- a/server/services/tracks/file.test.js+++ b/server/services/tracks/file.test.js@@ -30,9 +30,14 @@ function reset() {const journalEntries = () => cj.conflictJournalStore().loadAll();+// chiptuneScore/chiptunePrompt default to sanitizeTrack's own untouched shape+// (null/'') so a fixture that omits them hashes identically to what+// mergeTracksFromSync actually persists (the fields participate in the+// content hash as of #2912 — see conflictJournal.js HASH_FIELDS).const track = (id, extra = {}) => ({id, title: id, albumId: '', artistId: '', artist: '', lyrics: '', prompt: '',engine: '', modelId: '', durationSec: null, audioFilename: '',+ chiptuneScore: null, chiptunePrompt: '',createdAt: '2026-01-01T00:00:00.000Z', updatedAt: '2026-01-01T00:00:00.000Z',deleted: false, deletedAt: null, ...extra,});