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
refactor: keep cityChronotype's helpers internal so the utils barrel only carries computeChronotypeEnergy (#3898)
[._.]/ Adam Eivye77c28c60890
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.mdindex ca90d2a3c9..4fbb74b0dd 100644--- a/.changelog/NEXT.md+++ b/.changelog/NEXT.md@@ -61,3 +61,4 @@- Uploading several SongBook attachments at once no longer stops at the first failure. The upload loop awaited each file without catching, so one rejected read or a single server error broke out of the loop: the remaining files in the selection were never attempted, and the one generic "Upload failed" toast gave no clue which file failed or that anything had been skipped. Each file's read-and-upload now catches on its own, toasting a message that names the file, and the batch continues so the successful files still land.- Keyboard-shortcut hints across the client now render the right modifier for the machine they're on. `client/src/utils/platform.js` exports a `modKey` (`⌘` / `Ctrl`) label, but only the command palette and the keyboard-help sheet read it — five other hints hardcoded a glyph instead: the 404 page and the Ask composer advertised `⌘K` / `⌘/Ctrl + Enter` to Windows and Linux users, the OpenClaw composer printed both forms side by side, the Writers Room save button's tooltip said `Ctrl/Cmd+S`, and the digital-twin enrichment prompt said `Ctrl+Enter` on a Mac. All five now read `modKey`, which prefers `navigator.userAgentData.platform` over the deprecated `navigator.platform` and degrades to non-Mac when neither exists — behaviour the module now has tests for.- Leaving the SongBook editor with unsaved edits now asks first. The editor holds every change — sheet text, practice notes, metadata — in an in-memory draft until an explicit Save, so clicking View, following the "All songs" link, or closing the tab dropped the work silently. The page now compares the draft against the saved song (numerically for capo, since the number input hands back a string and retyping the stored value would otherwise read as an edit) and defers those exits behind an inline "Discard your unsaved changes?" band under the header, with Discard resetting the draft and Keep editing leaving it intact; a tab close or reload arms the browser's own prompt. Saving clears an armed confirm, since the draft is no longer dirty.+- `client/src/utils/cityChronotype.js` now exports only `computeChronotypeEnergy`. Its six internal pieces — the two range constants, the neutral-modifier sentinel, and the `parseHour` / `computeEnergy` / `energyModifiers` steps — were exported purely so the unit tests could reach them, but the utils barrel re-exports every module flat, so generic names like `parseHour` and `computeEnergy` sat in the shared namespace waiting to collide with a future module. The tests now drive every one of those behaviors through the public entry point (an unparseable or out-of-range peak window yields the neutral no-op; the `:30` half-hour still lands the peak center at 11.25), plus one assertion that the module's export list stays a single name.diff --git a/client/src/utils/cityChronotype.js b/client/src/utils/cityChronotype.jsindex cd5c24a1b5..534abe4329 100644--- a/client/src/utils/cityChronotype.js+++ b/client/src/utils/cityChronotype.js@@ -7,16 +7,21 @@// The hour-of-day is ALWAYS injected as a parameter — no `new Date()` here — so the// energy curve is deterministic in tests. The component computes the live hour and// passes it in.+//+// `computeChronotypeEnergy` is the module's ONLY export: everything below it is an+// internal step of that one calculation, and keeping it unexported keeps the flat+// `client/src/utils/index.js` barrel free of generic names like `parseHour` that+// would collide with a future module.// Sentinel energy for "no usable profile" — distinct from any real curve value so a// missing profile is recognizable. It maps to NEUTRAL_MODIFIERS (no visible change),// NOT to peak brightness, so an unconfigured city looks untouched rather than washed out.-export const NEUTRAL_ENERGY = 1.0;+const NEUTRAL_ENERGY = 1.0;// Tasteful clamp ranges so the overlay stays atmospheric, never washing out or// blacking out the existing scene. Brightness rides slightly above/below 1; tempo// (animation-speed multiplier) is gentler still.-export const ENERGY_RANGE = {+const ENERGY_RANGE = {brightnessMin: 0.7,brightnessMax: 1.15,tempoMin: 0.8,@@ -26,14 +31,14 @@ export const ENERGY_RANGE = {// What the overlay applies when there's no usable chronotype profile: a true no-op —// brightness and tempo at 1.0 so the scene renders exactly as it would without the// overlay. Keeps "unconfigured / failed to fetch" visually distinct from "low energy."-export const NEUTRAL_MODIFIERS = { energy: NEUTRAL_ENERGY, brightness: 1.0, tempo: 1.0 };+const NEUTRAL_MODIFIERS = { energy: NEUTRAL_ENERGY, brightness: 1.0, tempo: 1.0 };const clamp = (v, lo, hi) => Math.max(lo, Math.min(hi, v));// Parse "HH:MM" → fractional hours in [0,24). Returns NaN for unparseable input.// Hours past midnight that belong to "today's" late night (e.g. a 00:30 sleep time)// are returned as-is (0.5); callers that need a continuous timeline normalize.-export function parseHour(str) {+function parseHour(str) {if (typeof str !== 'string') return NaN;const [h, m] = str.split(':').map(Number);if (!Number.isFinite(h) || !Number.isFinite(m)) return NaN;@@ -90,7 +95,7 @@ function buildAnchors(profile) {// Returns `null` (sentinel) when there's no usable profile or the hour is non-finite,// so callers can distinguish "no profile" from a real curve value that happens to be// 1.0 (peak focus center). Callers map null → neutral (no visible change).-export function computeEnergy(profile, hour) {+function computeEnergy(profile, hour) {const anchors = buildAnchors(profile);if (!anchors || !Number.isFinite(hour)) return null;@@ -111,7 +116,7 @@ export function computeEnergy(profile, hour) {// applies: a brightness multiplier and a tempo (animation-speed) multiplier. Energy// 1 → top of each range, energy 0 → bottom; linear in between. A null/non-finite// energy (no usable profile) returns NEUTRAL_MODIFIERS — a true no-op.-export function energyModifiers(energy) {+function energyModifiers(energy) {if (!Number.isFinite(energy)) return { ...NEUTRAL_MODIFIERS };const e = clamp(energy, 0, 1);const { brightnessMin, brightnessMax, tempoMin, tempoMax } = ENERGY_RANGE;diff --git a/client/src/utils/cityChronotype.test.js b/client/src/utils/cityChronotype.test.jsindex f365370b89..1beed9ba31 100644--- a/client/src/utils/cityChronotype.test.js+++ b/client/src/utils/cityChronotype.test.js@@ -1,13 +1,18 @@import { describe, it, expect } from 'vitest';-import {- NEUTRAL_ENERGY,- NEUTRAL_MODIFIERS,- ENERGY_RANGE,- parseHour,- computeEnergy,- energyModifiers,- computeChronotypeEnergy,-} from './cityChronotype';+import { computeChronotypeEnergy } from './cityChronotype';++// The module's internals (parseHour / computeEnergy / energyModifiers and the range+// constants) are deliberately NOT exported — computeChronotypeEnergy is the sole+// public entry point, so every behavior below is exercised through it. The expected+// bounds are written as literals here rather than imported, so a change to the+// tasteful ranges has to be re-affirmed in the test instead of silently agreeing+// with itself.+const BRIGHTNESS_MIN = 0.7;+const BRIGHTNESS_MAX = 1.15;+const TEMPO_MIN = 0.8;+const TEMPO_MAX = 1.15;+// A missing/partial profile is a true no-op: brightness and tempo untouched.+const NEUTRAL = { energy: 1.0, brightness: 1.0, tempo: 1.0 };// A representative "intermediate" chronotype profile (mirrors the shape returned by// GET /api/digital-twin/identity/chronotype — only the recommendations the overlay@@ -25,57 +30,28 @@ const PROFILE = {// Peak focus center is (9.5 + 13) / 2 = 11.25.const PEAK_HOUR = 11.25;-describe('parseHour', () => {- it('parses HH:MM to fractional hours', () => {- expect(parseHour('09:30')).toBeCloseTo(9.5);- expect(parseHour('00:00')).toBe(0);- expect(parseHour('23:45')).toBeCloseTo(23.75);- });-- it('returns NaN for non-strings or out-of-range values', () => {- expect(parseHour(null)).toBeNaN();- expect(parseHour(undefined)).toBeNaN();- expect(parseHour('not-a-time')).toBeNaN();- expect(parseHour('24:00')).toBeNaN();- });-});--describe('computeEnergy', () => {+describe('computeChronotypeEnergy — energy curve', () => {it('is highest at the peak focus center', () => {- const atPeak = computeEnergy(PROFILE, PEAK_HOUR);- const atWake = computeEnergy(PROFILE, 7);- const atSleep = computeEnergy(PROFILE, 23);- expect(atPeak).toBeGreaterThan(atWake);- expect(atPeak).toBeGreaterThan(atSleep);- expect(atPeak).toBeCloseTo(1.0, 5);+ const atPeak = computeChronotypeEnergy(PROFILE, PEAK_HOUR);+ const atWake = computeChronotypeEnergy(PROFILE, 7);+ const atSleep = computeChronotypeEnergy(PROFILE, 23);+ expect(atPeak.energy).toBeGreaterThan(atWake.energy);+ expect(atPeak.energy).toBeGreaterThan(atSleep.energy);+ expect(atPeak.energy).toBeCloseTo(1.0, 5);});it('is lowest during recovery (sleep) hours', () => {- const atSleep = computeEnergy(PROFILE, 23);- const atPeak = computeEnergy(PROFILE, PEAK_HOUR);- expect(atSleep).toBeLessThan(atPeak);- expect(atSleep).toBeLessThan(0.5);+ const atSleep = computeChronotypeEnergy(PROFILE, 23);+ expect(atSleep.energy).toBeLessThan(computeChronotypeEnergy(PROFILE, PEAK_HOUR).energy);+ expect(atSleep.energy).toBeLessThan(0.5);});- it('returns null (sentinel) for a missing or partial profile (no crash)', () => {- expect(computeEnergy(null, 12)).toBeNull();- expect(computeEnergy({}, 12)).toBeNull();- expect(computeEnergy({ recommendations: {} }, 12)).toBeNull();- // peak window missing → can't anchor → null- expect(computeEnergy({ recommendations: { wakeTime: '07:00' } }, 12)).toBeNull();- });-- it('returns null when the hour is not finite', () => {- expect(computeEnergy(PROFILE, NaN)).toBeNull();- expect(computeEnergy(PROFILE, undefined)).toBeNull();- });-- it('stays within [0,1] across the whole day', () => {- for (let h = 0; h < 24; h += 0.5) {- const e = computeEnergy(PROFILE, h);- expect(e).toBeGreaterThanOrEqual(0);- expect(e).toBeLessThanOrEqual(1);- }+ it('parses HH:MM recommendations into fractional hours (09:30 peak start ⇒ 11.25 center)', () => {+ // The peak center is the maximum of the curve, so an exact hit at 11.25 proves+ // the ":30" half-hour was parsed as 9.5 rather than truncated to 9 or 10.+ expect(computeChronotypeEnergy(PROFILE, 11.25).energy).toBeCloseTo(1.0, 5);+ expect(computeChronotypeEnergy(PROFILE, 11).energy).toBeLessThan(1.0);+ expect(computeChronotypeEnergy(PROFILE, 11.5).energy).toBeLessThan(1.0);});it('handles wrap-around midnight for an evening chronotype with after-midnight sleep', () => {@@ -89,64 +65,77 @@ describe('computeEnergy', () => {},};// The post-midnight small hours should read as low energy (close to the 00:30 sleep anchor).- const at1am = computeEnergy(evening, 1);- const atPeak = computeEnergy(evening, 13); // peak center- expect(at1am).toBeLessThan(atPeak);- expect(at1am).toBeLessThan(0.5);+ const at1am = computeChronotypeEnergy(evening, 1);+ const atPeak = computeChronotypeEnergy(evening, 13); // peak center+ expect(at1am.energy).toBeLessThan(atPeak.energy);+ expect(at1am.energy).toBeLessThan(0.5);});});-describe('energyModifiers', () => {- it('maps energy 1 to the top of each clamped range', () => {- const m = energyModifiers(1);- expect(m.brightness).toBeCloseTo(ENERGY_RANGE.brightnessMax);- expect(m.tempo).toBeCloseTo(ENERGY_RANGE.tempoMax);- expect(m.energy).toBe(1);+describe('computeChronotypeEnergy — neutral no-op fallbacks', () => {+ it('returns the neutral no-op for a missing or partial profile (no crash)', () => {+ expect(computeChronotypeEnergy(null, 12)).toEqual(NEUTRAL);+ expect(computeChronotypeEnergy({}, 12)).toEqual(NEUTRAL);+ expect(computeChronotypeEnergy({ recommendations: {} }, 12)).toEqual(NEUTRAL);+ // peak window missing → can't anchor → neutral+ expect(computeChronotypeEnergy({ recommendations: { wakeTime: '07:00' } }, 12)).toEqual(NEUTRAL);});- it('maps energy 0 to the bottom of each clamped range', () => {- const m = energyModifiers(0);- expect(m.brightness).toBeCloseTo(ENERGY_RANGE.brightnessMin);- expect(m.tempo).toBeCloseTo(ENERGY_RANGE.tempoMin);+ it('returns the neutral no-op when the peak window is unparseable', () => {+ const bad = (peakFocusStart, peakFocusEnd) => ({+ recommendations: { ...PROFILE.recommendations, peakFocusStart, peakFocusEnd },+ });+ expect(computeChronotypeEnergy(bad('not-a-time', '13:00'), 12)).toEqual(NEUTRAL);+ expect(computeChronotypeEnergy(bad('09:30', '24:00'), 12)).toEqual(NEUTRAL);+ expect(computeChronotypeEnergy(bad(null, '13:00'), 12)).toEqual(NEUTRAL);+ expect(computeChronotypeEnergy(bad(undefined, undefined), 12)).toEqual(NEUTRAL);});- it('clamps out-of-range energy and treats null/non-finite as a neutral no-op', () => {- expect(energyModifiers(5).brightness).toBeCloseTo(ENERGY_RANGE.brightnessMax);- expect(energyModifiers(-3).brightness).toBeCloseTo(ENERGY_RANGE.brightnessMin);- // null/NaN energy (no usable profile) → neutral no-op, brightness 1.0 (untouched scene).- expect(energyModifiers(null)).toEqual(NEUTRAL_MODIFIERS);- expect(energyModifiers(NaN)).toEqual(NEUTRAL_MODIFIERS);+ it('returns the neutral no-op when the hour is not finite', () => {+ expect(computeChronotypeEnergy(PROFILE, NaN)).toEqual(NEUTRAL);+ expect(computeChronotypeEnergy(PROFILE, undefined)).toEqual(NEUTRAL);});- it('keeps brightness and tempo strictly inside the tasteful bounds', () => {- for (let e = 0; e <= 1; e += 0.1) {- const m = energyModifiers(e);- expect(m.brightness).toBeGreaterThanOrEqual(ENERGY_RANGE.brightnessMin);- expect(m.brightness).toBeLessThanOrEqual(ENERGY_RANGE.brightnessMax);- expect(m.tempo).toBeGreaterThanOrEqual(ENERGY_RANGE.tempoMin);- expect(m.tempo).toBeLessThanOrEqual(ENERGY_RANGE.tempoMax);- }+ it('ignores unparseable secondary anchors rather than falling back to neutral', () => {+ const partial = {+ recommendations: { peakFocusStart: '09:30', peakFocusEnd: '13:00', sleepTime: 'nope' },+ };+ const m = computeChronotypeEnergy(partial, PEAK_HOUR);+ expect(m).not.toEqual(NEUTRAL);+ expect(m.brightness).toBeCloseTo(BRIGHTNESS_MAX);});});-describe('computeChronotypeEnergy', () => {- it('peak hour → high brightness, recovery hour → low brightness', () => {+describe('computeChronotypeEnergy — display modifiers', () => {+ it('maps the peak focus center to the top of each clamped range', () => {+ const m = computeChronotypeEnergy(PROFILE, PEAK_HOUR);+ expect(m.brightness).toBeCloseTo(BRIGHTNESS_MAX);+ expect(m.tempo).toBeCloseTo(TEMPO_MAX);+ // A real curve value of exactly 1.0 must map to peak brightness, NOT the neutral no-op.+ expect(m.brightness).not.toBe(1.0);+ });++ it('peak hour → higher brightness and tempo than a recovery hour', () => {const peak = computeChronotypeEnergy(PROFILE, PEAK_HOUR);const recovery = computeChronotypeEnergy(PROFILE, 23);expect(peak.brightness).toBeGreaterThan(recovery.brightness);expect(peak.tempo).toBeGreaterThan(recovery.tempo);});- it('missing profile → neutral no-op modifiers (brightness 1.0), no crash', () => {- const m = computeChronotypeEnergy(null, 12);- expect(m).toEqual(NEUTRAL_MODIFIERS);- expect(m.energy).toBe(NEUTRAL_ENERGY);- expect(m.brightness).toBe(1.0);+ it('keeps energy, brightness and tempo inside the tasteful bounds across the whole day', () => {+ for (let h = 0; h < 24; h += 0.5) {+ const m = computeChronotypeEnergy(PROFILE, h);+ expect(m.energy).toBeGreaterThanOrEqual(0);+ expect(m.energy).toBeLessThanOrEqual(1);+ expect(m.brightness).toBeGreaterThanOrEqual(BRIGHTNESS_MIN);+ expect(m.brightness).toBeLessThanOrEqual(BRIGHTNESS_MAX);+ expect(m.tempo).toBeGreaterThanOrEqual(TEMPO_MIN);+ expect(m.tempo).toBeLessThanOrEqual(TEMPO_MAX);+ }});- it('peak focus center (real energy 1.0) maps to peak brightness, not the neutral no-op', () => {- const m = computeChronotypeEnergy(PROFILE, PEAK_HOUR);- expect(m.brightness).toBeCloseTo(ENERGY_RANGE.brightnessMax);- expect(m.brightness).not.toBe(1.0);+ it('exposes only computeChronotypeEnergy as the module surface', async () => {+ const mod = await import('./cityChronotype');+ expect(Object.keys(mod)).toEqual(['computeChronotypeEnergy']);});});