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
fix: address claude review — night-owl chronotype + preserve AI interpretation across sync (#2156)
Adam Eivy5da4d969e3ad
diff --git a/server/services/digital-twin-sync.js b/server/services/digital-twin-sync.jsindex ed227dd084..27ad6e1c23 100644--- a/server/services/digital-twin-sync.js+++ b/server/services/digital-twin-sync.js@@ -142,6 +142,29 @@ export function unionByKey(localArr, remoteArr, keyField, timestampField = null)return { merged: Array.from(map.values()), changed };}+/**+ * Merge observed taste evidence (#2156). The rollup BODY is LWW on `derivedAt`+ * (newest observation wins wholesale), but the AI `interpretation` block is the+ * one piece of EXPLICIT user-triggered content on the record — so it's+ * preserved separately: whichever side carries the newest interpretation (by+ * its own `generatedAt`) is kept regardless of which body won. Without this, an+ * unattended LLM-free daily recompute on another peer (newer `derivedAt`, no+ * interpretation) would silently drop the user's interpretation cross-machine.+ */+export function mergeTasteObserved(local, remote) {+ const { merged, changed } = mergeObjectLWW(local, remote, 'derivedAt');+ const li = isPlainObject(local) && isPlainObject(local.interpretation) ? local.interpretation : null;+ const ri = isPlainObject(remote) && isPlainObject(remote.interpretation) ? remote.interpretation : null;+ const newest = (ri?.generatedAt || '') > (li?.generatedAt || '') ? ri : li;+ if (!newest) return { merged, changed };+ const surviving = isPlainObject(merged) ? merged.interpretation : null;+ if (surviving === newest) return { merged, changed };+ // Only overlay when the newest interpretation is strictly newer than whatever+ // survived on the winning body (or the body has none).+ if ((surviving?.generatedAt || '') >= newest.generatedAt) return { merged, changed };+ return { merged: { ...merged, interpretation: newest }, changed: true };+}+const TASTE_STATUS_RANK = { pending: 0, in_progress: 1, completed: 2 };function pickStatus(a, b) {return (TASTE_STATUS_RANK[b] ?? -1) > (TASTE_STATUS_RANK[a] ?? -1) ? b : a;@@ -509,8 +532,9 @@ export async function applyDigitalTwinRemote(remoteData) {count += await applyMerge(LONGEVITY_FILE, remoteData.longevity, (l, r) => mergeDeepUnion(l, r, 'derivedAt'));count += await applyMerge(FEEDBACK_FILE, remoteData.feedback, (l, r) => mergeObjectLWW(l, r, 'updatedAt'));count += await applyTaste(remoteData.taste);- // Observed evidence (Phase 7): regenerated derived records — newest wins.- count += await applyMerge(TASTE_OBSERVED_FILE, remoteData.tasteObserved, (l, r) => mergeObjectLWW(l, r, 'derivedAt'));+ // Observed evidence (Phase 7): regenerated derived records — body is LWW on+ // derivedAt; taste also preserves the newest user-triggered AI interpretation.+ count += await applyMerge(TASTE_OBSERVED_FILE, remoteData.tasteObserved, mergeTasteObserved);count += await applyMerge(CHRONOTYPE_OBSERVED_FILE, remoteData.chronotypeObserved, (l, r) => mergeObjectLWW(l, r, 'derivedAt'));// Meta BEFORE documents: applyMeta()'s loadMeta() rebuilds meta from a disk// .md scan when no meta.json exists, creating DEFAULT document entries. If thediff --git a/server/services/digital-twin-sync.test.js b/server/services/digital-twin-sync.test.jsindex 8c8f3bf9c8..50bf76b508 100644--- a/server/services/digital-twin-sync.test.js+++ b/server/services/digital-twin-sync.test.js@@ -8,6 +8,7 @@ import {mergeConfidence,mergeSocialAccounts,mergeAutobiographyStories,+ mergeTasteObserved,safeMdName,} from './digital-twin-sync.js';@@ -39,6 +40,42 @@ describe('mergeObjectLWW', () => {});});+describe('mergeTasteObserved (#2156 — preserve user AI interpretation across LWW)', () => {+ const body = (derivedAt, extra = {}) => ({ source: 'observed', derivedAt, ...extra });+ const interp = (generatedAt, text = 'x') => ({ text, generatedAt });++ it('LWW on derivedAt for the rollup body', () => {+ const local = body('2026-07-01T00:00:00Z', { total: 1 });+ const remote = body('2026-07-05T00:00:00Z', { total: 9 });+ expect(mergeTasteObserved(local, remote).merged.total).toBe(9);+ });++ it('keeps the local interpretation when a newer remote recompute carries none', () => {+ // The exact drop bug: peer recompute wins the body but must NOT lose the+ // user's interpretation.+ const local = body('2026-07-01T00:00:00Z', { interpretation: interp('2026-07-02T00:00:00Z', 'mine') });+ const remote = body('2026-07-05T00:00:00Z'); // newer body, no interpretation+ const { merged, changed } = mergeTasteObserved(local, remote);+ expect(merged.derivedAt).toBe('2026-07-05T00:00:00Z'); // remote body won+ expect(merged.interpretation.text).toBe('mine'); // interpretation survived+ expect(changed).toBe(true);+ });++ it('takes the newer interpretation regardless of which body won', () => {+ const local = body('2026-07-05T00:00:00Z', { interpretation: interp('2026-07-01T00:00:00Z', 'old') });+ const remote = body('2026-07-01T00:00:00Z', { interpretation: interp('2026-07-06T00:00:00Z', 'new') });+ const merged = mergeTasteObserved(local, remote).merged;+ expect(merged.derivedAt).toBe('2026-07-05T00:00:00Z'); // local body won (newer)+ expect(merged.interpretation.text).toBe('new'); // remote interpretation is newer+ });++ it('is a no-op when neither side has an interpretation and the body is unchanged', () => {+ const local = body('2026-07-05T00:00:00Z');+ const remote = body('2026-07-01T00:00:00Z');+ expect(mergeTasteObserved(local, remote).changed).toBe(false);+ });+});+describe('mergeDeepUnion', () => {it('unions nested marker objects, local wins per-key', () => {const local = { markers: { a: 1 }, derivedAt: '2026-01-01' };diff --git a/server/services/twinEnrichment.js b/server/services/twinEnrichment.jsindex b78cfcba64..c666c6e02e 100644--- a/server/services/twinEnrichment.js+++ b/server/services/twinEnrichment.js@@ -210,11 +210,15 @@ export function classifyObservedChronotype(histogram) {let meanAngle = Math.atan2(sumY, sumX);if (meanAngle < 0) meanAngle += 2 * Math.PI;const centerHour = Math.round((meanAngle / (2 * Math.PI)) * 24) % 24;- // Center-of-mass hour → type. Daytime activity clusters, so a center before- // ~11:00 skews morning, after ~15:00 skews evening, between is intermediate.+ // Center-of-mass hour → type. The circular mean wraps a late-night cluster to+ // a LOW hour (a night owl active 22:00–02:00 centers near 0), so the+ // small-hours band (<6) must classify as evening, NOT morning — otherwise the+ // wrap that the circular mean exists to handle is undone here. Bands: evening+ // = center ≥15:00 or in the small hours (<6:00); morning = 6:00–10:59;+ // intermediate = 11:00–14:59.let type;- if (centerHour < 11) type = 'morning';- else if (centerHour >= 15) type = 'evening';+ if (centerHour >= 15 || centerHour < 6) type = 'evening';+ else if (centerHour < 11) type = 'morning';else type = 'intermediate';return { type, centerHour, sampleSize: total };}diff --git a/server/services/twinEnrichment.test.js b/server/services/twinEnrichment.test.jsindex b85e196505..dc6a88a1c3 100644--- a/server/services/twinEnrichment.test.js+++ b/server/services/twinEnrichment.test.js@@ -171,14 +171,20 @@ describe('classifyObservedChronotype', () => {const hist = chronotypeHistogram(events, 'UTC');expect(classifyObservedChronotype(hist).type).toBe('evening');});- it('uses a circular mean so a midnight-wrapped night-owl is not averaged to noon', () => {- // Active 22:00–02:00 — a linear mean would land near noon (wrong); circular- // mean keeps the center in the late/early band, never 'morning' midday.+ it('uses a circular mean so a midnight-wrapped night-owl classifies as evening, not morning', () => {+ // Active 22:00–02:00 — a linear mean would land near noon (wrong); the+ // circular mean wraps the center near midnight, and the small-hours band+ // must then read as evening (a night owl), never 'morning'.const events = [22, 23, 0, 1, 2, 23].map((h) => at(h));const hist = chronotypeHistogram(events, 'UTC');const { type, centerHour } = classifyObservedChronotype(hist);expect(centerHour).not.toBeGreaterThan(3); // wrapped near midnight, not ~12- expect(type).not.toBe('intermediate');+ expect(type).toBe('evening');+ });+ it('classifies a small-hours center (night owl) as evening', () => {+ const events = [23, 0, 0, 1, 1, 2].map((h) => at(h));+ const { type } = classifyObservedChronotype(chronotypeHistogram(events, 'UTC'));+ expect(type).toBe('evening');});});