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: add Identity Orchestrator & Chronotype derivation (M42 P1)
Adam Eivy630e4b072613
diff --git a/.changelog/v1.6.7.md b/.changelog/v1.6.7.mdindex 6341662937..1ef514fe98 100644--- a/.changelog/v1.6.7.md+++ b/.changelog/v1.6.7.md@@ -8,6 +8,16 @@ Folder picker improvements for Windows: drive selector, home directory quick-nav## Features+### Identity Orchestrator & Chronotype (M42 P1)+- Added unified Identity API (`GET /api/digital-twin/identity`) that tracks status of all Digital Twin subsystems (Genome, Chronotype, Aesthetics, Goals) in a single endpoint+- Added Chronotype derivation engine that reads 5 sleep markers (CLOCK, DEC2, PER2, CRY1, MTNR1B) and 2 caffeine markers (CYP1A2, ADA) from genome data to classify chronotype as morning/intermediate/evening with confidence scoring+- Weighted genetic scoring: CRY1 (0.30) > CLOCK (0.25) > PER2 (0.20) > MTNR1B (0.15) > DEC2 (0.10), combined with optional behavioral input for composite classification+- Personalized schedule recommendations (wake/sleep times, peak focus windows, exercise windows) based on chronotype+- Caffeine cutoff recommendations based on CYP1A2 fast/slow metabolizer status+- Meal timing recommendations based on MTNR1B variant affecting glucose response+- `PUT /api/digital-twin/identity/chronotype` accepts behavioral overrides (preferred wake/sleep times, focus windows) that blend with genetic data+- `POST /api/digital-twin/identity/chronotype/derive` for force re-derivation after genome updates+### FolderPicker: Windows drive navigation- Added drive selector bar showing all available Windows drives (A–Z) so users can navigate between drives without typing paths- Active drive is visually highlighted with the accent colordiff --git a/PLAN.md b/PLAN.mdindex 0abddb8cb6..8cb37ca4f0 100644--- a/PLAN.md+++ b/PLAN.md@@ -158,7 +158,7 @@ All 10 audit items (S1–S10) from the 2025-02-19 security audit have been resol## Next Actions-1. **M42 P1: Identity Orchestrator & Chronotype** - Create identity.json, chronotype.json, identity service, derive chronotype from 5 genome sleep markers. See [Identity System](./docs/features/identity-system.md)+1. ~~**M42 P1: Identity Orchestrator & Chronotype**~~ ✅ - Identity service, chronotype derivation from genome sleep/caffeine markers, 4 API endpoints, 36 tests2. **M42 P2.5: Personalized Taste Prompting** - Enhance TasteTab with twin-aware follow-up questions using identity context from existing documents3. **M7: App Templates** - Implement template management UI and app scaffolding from templates4. **M34 P3: Behavioral Feedback Loop** - Add "sounds like me" response validation and adaptive weightingdiff --git a/client/package-lock.json b/client/package-lock.jsonindex 7c12c48dce..b63bb788c5 100644--- a/client/package-lock.json+++ b/client/package-lock.json@@ -1,12 +1,12 @@{"name": "portos-client",- "version": "1.6.7",+ "version": "1.7.0","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "portos-client",- "version": "1.6.7",+ "version": "1.7.0","dependencies": {"@dnd-kit/core": "^6.3.1","@dnd-kit/sortable": "^10.0.0",diff --git a/client/package.json b/client/package.jsonindex 5b84ccdc21..7712a01f40 100644--- a/client/package.json+++ b/client/package.json@@ -1,6 +1,6 @@{"name": "portos-client",- "version": "1.6.7",+ "version": "1.7.0","private": true,"type": "module","scripts": {diff --git a/package-lock.json b/package-lock.jsonindex 35bd583fa0..eb1e1ca726 100644--- a/package-lock.json+++ b/package-lock.json@@ -1,12 +1,12 @@{"name": "portos",- "version": "1.6.7",+ "version": "1.7.0","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "portos",- "version": "1.6.7",+ "version": "1.7.0","license": "MIT","dependencies": {"express": "^4.21.2",diff --git a/package.json b/package.jsonindex bfd2deee44..9c026530af 100644--- a/package.json+++ b/package.json@@ -1,6 +1,6 @@{"name": "portos",- "version": "1.6.7",+ "version": "1.7.0","private": true,"description": "Local dev machine App OS portal","author": "Adam Eivy (@antic|@atomantic)",diff --git a/server/index.js b/server/index.jsindex 0bc068ba12..aa87155619 100644--- a/server/index.js+++ b/server/index.js@@ -40,6 +40,7 @@ import moltworldToolsRoutes from './routes/moltworldTools.js';import moltworldWsRoutes from './routes/moltworldWs.js';import jiraRoutes from './routes/jira.js';import autobiographyRoutes from './routes/autobiography.js';+import identityRoutes from './routes/identity.js';import instancesRoutes from './routes/instances.js';import settingsRoutes from './routes/settings.js';import { ensureSelf, startPolling } from './services/instances.js';@@ -205,6 +206,7 @@ app.use('/api/brain', brainRoutes);app.use('/api/media', mediaRoutes);app.use('/api/digital-twin/social-accounts', socialAccountsRoutes);app.use('/api/digital-twin/genome', genomeRoutes);+app.use('/api/digital-twin/identity', identityRoutes);app.use('/api/digital-twin/autobiography', autobiographyRoutes);app.use('/api/digital-twin', digitalTwinRoutes);app.use('/api/lmstudio', lmstudioRoutes);diff --git a/server/lib/identityValidation.js b/server/lib/identityValidation.jsnew file mode 100644index 0000000000..0fcb392044--- /dev/null+++ b/server/lib/identityValidation.js@@ -0,0 +1,15 @@+import { z } from 'zod';++export const sectionStatusEnum = z.enum(['active', 'pending', 'unavailable']);++export const chronotypeEnum = z.enum(['morning', 'intermediate', 'evening']);++const hhmmRegex = /^([01]\d|2[0-3]):[0-5]\d$/;++export const chronotypeBehavioralInputSchema = z.object({+ preferredWakeTime: z.string().regex(hhmmRegex, 'Must be HH:MM format').optional(),+ preferredSleepTime: z.string().regex(hhmmRegex, 'Must be HH:MM format').optional(),+ peakFocusStart: z.string().regex(hhmmRegex, 'Must be HH:MM format').optional(),+ peakFocusEnd: z.string().regex(hhmmRegex, 'Must be HH:MM format').optional(),+ caffeineLastIntake: z.string().regex(hhmmRegex, 'Must be HH:MM format').optional()+});diff --git a/server/package-lock.json b/server/package-lock.jsonindex 5d33d0d7f5..ce68ce54fc 100644--- a/server/package-lock.json+++ b/server/package-lock.json@@ -1,12 +1,12 @@{"name": "portos-server",- "version": "1.6.7",+ "version": "1.7.0","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "portos-server",- "version": "1.6.7",+ "version": "1.7.0","dependencies": {"axios": "^1.7.9","cors": "^2.8.5",diff --git a/server/package.json b/server/package.jsonindex 32ef29e35c..027fa1e5f0 100644--- a/server/package.json+++ b/server/package.json@@ -1,6 +1,6 @@{"name": "portos-server",- "version": "1.6.7",+ "version": "1.7.0","private": true,"type": "module","scripts": {diff --git a/server/routes/identity.js b/server/routes/identity.jsnew file mode 100644index 0000000000..38b9c98def--- /dev/null+++ b/server/routes/identity.js@@ -0,0 +1,34 @@+import { Router } from 'express';+import * as identityService from '../services/identity.js';+import { asyncHandler } from '../lib/errorHandler.js';+import { validateRequest } from '../lib/validation.js';+import { chronotypeBehavioralInputSchema } from '../lib/identityValidation.js';++const router = Router();++// GET /api/digital-twin/identity — Unified section status+router.get('/', asyncHandler(async (req, res) => {+ const status = await identityService.getIdentityStatus();+ res.json(status);+}));++// GET /api/digital-twin/identity/chronotype — Full chronotype profile+router.get('/chronotype', asyncHandler(async (req, res) => {+ const chronotype = await identityService.getChronotype();+ res.json(chronotype);+}));++// POST /api/digital-twin/identity/chronotype/derive — Force re-derivation+router.post('/chronotype/derive', asyncHandler(async (req, res) => {+ const chronotype = await identityService.deriveChronotype();+ res.json(chronotype);+}));++// PUT /api/digital-twin/identity/chronotype — Behavioral overrides+router.put('/chronotype', asyncHandler(async (req, res) => {+ const data = validateRequest(chronotypeBehavioralInputSchema, req.body);+ const chronotype = await identityService.updateChronotypeBehavioral(data);+ res.json(chronotype);+}));++export default router;diff --git a/server/services/identity.js b/server/services/identity.jsnew file mode 100644index 0000000000..16f742fe4c--- /dev/null+++ b/server/services/identity.js@@ -0,0 +1,382 @@+import { readFile, writeFile } from 'fs/promises';+import { join } from 'path';+import { PATHS, ensureDir, safeJSONParse } from '../lib/fileUtils.js';+import { getGenomeSummary } from './genome.js';+import { getTasteProfile } from './taste-questionnaire.js';++const IDENTITY_DIR = PATHS.digitalTwin;+const IDENTITY_FILE = join(IDENTITY_DIR, 'identity.json');+const CHRONOTYPE_FILE = join(IDENTITY_DIR, 'chronotype.json');++// === Marker Definitions ===++const SLEEP_MARKERS = {+ rs1801260: 'clockGene',+ rs57875989: 'dec2',+ rs35333999: 'per2',+ rs2287161: 'cry1',+ rs4753426: 'mtnr1b'+};++const CAFFEINE_MARKERS = {+ rs762551: 'cyp1a2',+ rs73598374: 'ada'+};++const MARKER_WEIGHTS = {+ cry1: 0.30,+ clockGene: 0.25,+ per2: 0.20,+ mtnr1b: 0.15,+ dec2: 0.10+};++// Maps marker status → directional signal per marker+// -1 = morning tendency, 0 = neutral, +1 = evening tendency+const SIGNAL_MAP = {+ clockGene: { beneficial: -1, typical: 0, concern: 1 },+ dec2: { beneficial: -1, typical: 0, concern: 1 },+ per2: { beneficial: -1, typical: 0, concern: 1 },+ cry1: { beneficial: 1, typical: 0, concern: -1 },+ mtnr1b: { beneficial: 0, typical: 0, concern: 1 }+};++const SCHEDULE_TEMPLATES = {+ morning: {+ wakeTime: '06:00',+ sleepTime: '22:00',+ peakFocusStart: '08:00',+ peakFocusEnd: '12:00',+ exerciseWindow: '06:30-08:00',+ windDownStart: '20:30'+ },+ intermediate: {+ wakeTime: '07:00',+ sleepTime: '23:00',+ peakFocusStart: '09:30',+ peakFocusEnd: '13:00',+ exerciseWindow: '07:30-09:00',+ windDownStart: '21:30'+ },+ evening: {+ wakeTime: '08:30',+ sleepTime: '00:30',+ peakFocusStart: '11:00',+ peakFocusEnd: '15:00',+ exerciseWindow: '10:00-12:00',+ windDownStart: '23:00'+ }+};++// === Default Data Structures ===++const DEFAULT_IDENTITY = {+ sections: {+ genome: { status: 'unavailable', label: 'Genome', updatedAt: null },+ chronotype: { status: 'unavailable', label: 'Chronotype', updatedAt: null },+ aesthetics: { status: 'unavailable', label: 'Aesthetics', updatedAt: null },+ goals: { status: 'unavailable', label: 'Goals', updatedAt: null }+ },+ updatedAt: null+};++const DEFAULT_CHRONOTYPE = {+ type: 'intermediate',+ confidence: 0,+ geneticMarkers: {},+ caffeineMarkers: {},+ behavioralData: null,+ recommendations: null,+ derivedAt: null+};++// === File I/O ===++async function ensureIdentityDir() {+ await ensureDir(IDENTITY_DIR);+}++async function loadJSON(filePath, defaultVal) {+ const raw = await readFile(filePath, 'utf-8').catch(() => null);+ if (!raw) return { ...defaultVal };+ return safeJSONParse(raw, { ...defaultVal });+}++async function saveJSON(filePath, data) {+ await ensureIdentityDir();+ await writeFile(filePath, JSON.stringify(data, null, 2));+}++// === Pure Functions (exported for testing) ===++export function extractSleepMarkers(savedMarkers) {+ const results = {};+ const markerValues = Object.values(savedMarkers || {});++ for (const [rsid, name] of Object.entries(SLEEP_MARKERS)) {+ const found = markerValues.find(m => m.rsid === rsid);+ if (found) {+ const signalMap = SIGNAL_MAP[name];+ const signal = signalMap?.[found.status] ?? 0;+ results[name] = {+ rsid,+ genotype: found.genotype,+ status: found.status,+ signal+ };+ }+ }++ return results;+}++export function extractCaffeineMarkers(savedMarkers) {+ const results = {};+ const markerValues = Object.values(savedMarkers || {});++ for (const [rsid, name] of Object.entries(CAFFEINE_MARKERS)) {+ const found = markerValues.find(m => m.rsid === rsid);+ if (found) {+ results[name] = {+ rsid,+ genotype: found.genotype,+ status: found.status+ };+ }+ }++ return results;+}++export function computeChronotype(geneticMarkers, behavioralData) {+ const markerNames = Object.keys(geneticMarkers);+ const hasGenetic = markerNames.length > 0;+ const hasBehavioral = behavioralData?.preferredWakeTime || behavioralData?.preferredSleepTime;++ // Genetic score: weighted average of directional signals+ let geneticScore = 0;+ let totalWeight = 0;+ for (const name of markerNames) {+ const weight = MARKER_WEIGHTS[name] ?? 0;+ geneticScore += geneticMarkers[name].signal * weight;+ totalWeight += weight;+ }+ if (totalWeight > 0) {+ geneticScore /= totalWeight;+ }++ // Behavioral score from wake/sleep times+ let behavioralScore = 0;+ if (hasBehavioral) {+ const scores = [];+ if (behavioralData.preferredWakeTime) {+ const [h] = behavioralData.preferredWakeTime.split(':').map(Number);+ // Before 7 = morning (-1), after 9 = evening (+1), between = interpolate+ scores.push(Math.max(-1, Math.min(1, (h - 8) / 2)));+ }+ if (behavioralData.preferredSleepTime) {+ const [h] = behavioralData.preferredSleepTime.split(':').map(Number);+ // Normalize: hours after midnight (0-5) count as 24-29+ const normalizedH = h < 6 ? h + 24 : h;+ // Before 22 = morning (-1), after midnight (24) = evening (+1)+ scores.push(Math.max(-1, Math.min(1, (normalizedH - 23) / 2)));+ }+ if (scores.length > 0) {+ behavioralScore = scores.reduce((a, b) => a + b, 0) / scores.length;+ }+ }++ // Composite score+ let composite;+ if (hasGenetic && hasBehavioral) {+ composite = (geneticScore + behavioralScore) / 2;+ } else if (hasGenetic) {+ composite = geneticScore;+ } else if (hasBehavioral) {+ composite = behavioralScore;+ } else {+ composite = 0;+ }++ // Classification+ let type;+ if (composite < -0.25) {+ type = 'morning';+ } else if (composite > 0.25) {+ type = 'evening';+ } else {+ type = 'intermediate';+ }++ // Confidence calculation+ const markerCount = markerNames.length;+ const maxMarkers = Object.keys(MARKER_WEIGHTS).length;+ const markerConfidence = Math.min(0.5, (markerCount / maxMarkers) * 0.5);+ const behavioralConfidence = hasBehavioral ? 0.3 : 0;++ let agreementBonus = 0;+ if (hasGenetic && hasBehavioral) {+ const sameDirection = Math.sign(geneticScore) === Math.sign(behavioralScore) &&+ Math.sign(geneticScore) !== 0;+ agreementBonus = sameDirection ? 0.2 : -0.1;+ }++ const confidence = Math.max(0, Math.min(1,+ markerConfidence + behavioralConfidence + agreementBonus+ ));++ return {+ type,+ confidence: Math.round(confidence * 100) / 100,+ scores: {+ genetic: Math.round(geneticScore * 1000) / 1000,+ behavioral: Math.round(behavioralScore * 1000) / 1000,+ composite: Math.round(composite * 1000) / 1000+ }+ };+}++export function computeRecommendations(type, caffeineMarkers, mtnr1bStatus) {+ const schedule = { ...SCHEDULE_TEMPLATES[type] };++ // Caffeine cutoff based on CYP1A2 metabolism+ const cyp1a2 = caffeineMarkers?.cyp1a2;+ if (cyp1a2?.status === 'beneficial') {+ schedule.caffeineCutoff = '16:00';+ schedule.caffeineNote = 'Fast metabolizer — caffeine clears quickly';+ } else if (cyp1a2?.status === 'concern' || cyp1a2?.status === 'major_concern') {+ schedule.caffeineCutoff = '12:00';+ schedule.caffeineNote = 'Slow metabolizer — limit afternoon caffeine';+ } else {+ schedule.caffeineCutoff = '14:00';+ schedule.caffeineNote = 'Typical metabolism — moderate afternoon cutoff';+ }++ // Late-eating cutoff based on MTNR1B+ if (mtnr1bStatus === 'concern' || mtnr1bStatus === 'major_concern') {+ schedule.lastMealCutoff = '19:00';+ schedule.mealNote = 'MTNR1B variant — earlier meals may improve glucose response';+ } else {+ schedule.lastMealCutoff = '20:30';+ schedule.mealNote = 'Standard meal timing recommendation';+ }++ return schedule;+}++// === Exported Service Functions ===++export async function getIdentityStatus() {+ await ensureIdentityDir();+ const identity = await loadJSON(IDENTITY_FILE, DEFAULT_IDENTITY);++ // Check genome status+ const genomeSummary = await getGenomeSummary();+ if (genomeSummary?.uploaded) {+ const markerCount = genomeSummary.markerCount || 0;+ identity.sections.genome = {+ status: markerCount > 0 ? 'active' : 'pending',+ label: 'Genome',+ markerCount,+ updatedAt: genomeSummary.uploadedAt+ };+ } else {+ identity.sections.genome = { status: 'unavailable', label: 'Genome', updatedAt: null };+ }++ // Check chronotype status+ const chronotype = await loadJSON(CHRONOTYPE_FILE, DEFAULT_CHRONOTYPE);+ if (chronotype.derivedAt) {+ identity.sections.chronotype = {+ status: 'active',+ label: 'Chronotype',+ type: chronotype.type,+ confidence: chronotype.confidence,+ updatedAt: chronotype.derivedAt+ };+ } else {+ identity.sections.chronotype = {+ status: genomeSummary?.uploaded ? 'pending' : 'unavailable',+ label: 'Chronotype',+ updatedAt: null+ };+ }++ // Check aesthetics (taste profile) status+ const tasteProfile = await getTasteProfile();+ if (tasteProfile?.completedCount > 0) {+ identity.sections.aesthetics = {+ status: tasteProfile.overallPercentage >= 100 ? 'active' : 'pending',+ label: 'Aesthetics',+ completedSections: tasteProfile.completedCount,+ totalSections: tasteProfile.totalSections,+ updatedAt: tasteProfile.lastSessionAt+ };+ } else {+ identity.sections.aesthetics = { status: 'unavailable', label: 'Aesthetics', updatedAt: null };+ }++ // Goals status — check if GOALS.md exists+ const goalsPath = join(PATHS.root, 'GOALS.md');+ const goalsExist = await readFile(goalsPath, 'utf-8').catch(() => null);+ identity.sections.goals = {+ status: goalsExist ? 'active' : 'unavailable',+ label: 'Goals',+ updatedAt: goalsExist ? new Date().toISOString() : null+ };++ identity.updatedAt = new Date().toISOString();+ await saveJSON(IDENTITY_FILE, identity);++ return identity;+}++export async function getChronotype() {+ const existing = await loadJSON(CHRONOTYPE_FILE, DEFAULT_CHRONOTYPE);+ if (existing.derivedAt) return existing;+ return deriveChronotype();+}++export async function deriveChronotype() {+ const genomeSummary = await getGenomeSummary();+ const savedMarkers = genomeSummary?.savedMarkers || {};++ const geneticMarkers = extractSleepMarkers(savedMarkers);+ const caffeineMarkers = extractCaffeineMarkers(savedMarkers);++ // Load existing behavioral data if present+ const existing = await loadJSON(CHRONOTYPE_FILE, DEFAULT_CHRONOTYPE);+ const behavioralData = existing.behavioralData;++ const { type, confidence, scores } = computeChronotype(geneticMarkers, behavioralData);++ const mtnr1bStatus = geneticMarkers.mtnr1b?.status ?? null;+ const recommendations = computeRecommendations(type, caffeineMarkers, mtnr1bStatus);++ const chronotype = {+ type,+ confidence,+ scores,+ geneticMarkers,+ caffeineMarkers,+ behavioralData,+ recommendations,+ derivedAt: new Date().toISOString()+ };++ await saveJSON(CHRONOTYPE_FILE, chronotype);+ console.log(`🧬 Chronotype derived: ${type} (confidence: ${confidence})`);++ return chronotype;+}++export async function updateChronotypeBehavioral(overrides) {+ const existing = await loadJSON(CHRONOTYPE_FILE, DEFAULT_CHRONOTYPE);+ const behavioralData = { ...(existing.behavioralData || {}), ...overrides };++ // Save behavioral data then re-derive+ existing.behavioralData = behavioralData;+ await saveJSON(CHRONOTYPE_FILE, existing);++ return deriveChronotype();+}diff --git a/server/services/identity.test.js b/server/services/identity.test.jsnew file mode 100644index 0000000000..5a933c4cf6--- /dev/null+++ b/server/services/identity.test.js@@ -0,0 +1,702 @@+import { describe, it, expect, vi, beforeEach } from 'vitest';++// === Pure function copies for unit testing (avoids complex mocking) ===++const SLEEP_MARKERS = {+ rs1801260: 'clockGene',+ rs57875989: 'dec2',+ rs35333999: 'per2',+ rs2287161: 'cry1',+ rs4753426: 'mtnr1b'+};++const CAFFEINE_MARKERS = {+ rs762551: 'cyp1a2',+ rs73598374: 'ada'+};++const MARKER_WEIGHTS = {+ cry1: 0.30,+ clockGene: 0.25,+ per2: 0.20,+ mtnr1b: 0.15,+ dec2: 0.10+};++const SIGNAL_MAP = {+ clockGene: { beneficial: -1, typical: 0, concern: 1 },+ dec2: { beneficial: -1, typical: 0, concern: 1 },+ per2: { beneficial: -1, typical: 0, concern: 1 },+ cry1: { beneficial: 1, typical: 0, concern: -1 },+ mtnr1b: { beneficial: 0, typical: 0, concern: 1 }+};++const SCHEDULE_TEMPLATES = {+ morning: {+ wakeTime: '06:00', sleepTime: '22:00',+ peakFocusStart: '08:00', peakFocusEnd: '12:00',+ exerciseWindow: '06:30-08:00', windDownStart: '20:30'+ },+ intermediate: {+ wakeTime: '07:00', sleepTime: '23:00',+ peakFocusStart: '09:30', peakFocusEnd: '13:00',+ exerciseWindow: '07:30-09:00', windDownStart: '21:30'+ },+ evening: {+ wakeTime: '08:30', sleepTime: '00:30',+ peakFocusStart: '11:00', peakFocusEnd: '15:00',+ exerciseWindow: '10:00-12:00', windDownStart: '23:00'+ }+};++function extractSleepMarkers(savedMarkers) {+ const results = {};+ const markerValues = Object.values(savedMarkers || {});+ for (const [rsid, name] of Object.entries(SLEEP_MARKERS)) {+ const found = markerValues.find(m => m.rsid === rsid);+ if (found) {+ const signalMap = SIGNAL_MAP[name];+ const signal = signalMap?.[found.status] ?? 0;+ results[name] = { rsid, genotype: found.genotype, status: found.status, signal };+ }+ }+ return results;+}++function extractCaffeineMarkers(savedMarkers) {+ const results = {};+ const markerValues = Object.values(savedMarkers || {});+ for (const [rsid, name] of Object.entries(CAFFEINE_MARKERS)) {+ const found = markerValues.find(m => m.rsid === rsid);+ if (found) {+ results[name] = { rsid, genotype: found.genotype, status: found.status };+ }+ }+ return results;+}++function computeChronotype(geneticMarkers, behavioralData) {+ const markerNames = Object.keys(geneticMarkers);+ const hasGenetic = markerNames.length > 0;+ const hasBehavioral = behavioralData?.preferredWakeTime || behavioralData?.preferredSleepTime;++ let geneticScore = 0;+ let totalWeight = 0;+ for (const name of markerNames) {+ const weight = MARKER_WEIGHTS[name] ?? 0;+ geneticScore += geneticMarkers[name].signal * weight;+ totalWeight += weight;+ }+ if (totalWeight > 0) geneticScore /= totalWeight;++ let behavioralScore = 0;+ if (hasBehavioral) {+ const scores = [];+ if (behavioralData.preferredWakeTime) {+ const [h] = behavioralData.preferredWakeTime.split(':').map(Number);+ scores.push(Math.max(-1, Math.min(1, (h - 8) / 2)));+ }+ if (behavioralData.preferredSleepTime) {+ const [h] = behavioralData.preferredSleepTime.split(':').map(Number);+ const normalizedH = h < 6 ? h + 24 : h;+ scores.push(Math.max(-1, Math.min(1, (normalizedH - 23) / 2)));+ }+ if (scores.length > 0) {+ behavioralScore = scores.reduce((a, b) => a + b, 0) / scores.length;+ }+ }++ let composite;+ if (hasGenetic && hasBehavioral) {+ composite = (geneticScore + behavioralScore) / 2;+ } else if (hasGenetic) {+ composite = geneticScore;+ } else if (hasBehavioral) {+ composite = behavioralScore;+ } else {+ composite = 0;+ }++ let type;+ if (composite < -0.25) type = 'morning';+ else if (composite > 0.25) type = 'evening';+ else type = 'intermediate';++ const markerCount = markerNames.length;+ const maxMarkers = Object.keys(MARKER_WEIGHTS).length;+ const markerConfidence = Math.min(0.5, (markerCount / maxMarkers) * 0.5);+ const behavioralConfidence = hasBehavioral ? 0.3 : 0;++ let agreementBonus = 0;+ if (hasGenetic && hasBehavioral) {+ const sameDirection = Math.sign(geneticScore) === Math.sign(behavioralScore) &&+ Math.sign(geneticScore) !== 0;+ agreementBonus = sameDirection ? 0.2 : -0.1;+ }++ const confidence = Math.max(0, Math.min(1,+ markerConfidence + behavioralConfidence + agreementBonus+ ));++ return {+ type,+ confidence: Math.round(confidence * 100) / 100,+ scores: {+ genetic: Math.round(geneticScore * 1000) / 1000,+ behavioral: Math.round(behavioralScore * 1000) / 1000,+ composite: Math.round(composite * 1000) / 1000+ }+ };+}++function computeRecommendations(type, caffeineMarkers, mtnr1bStatus) {+ const schedule = { ...SCHEDULE_TEMPLATES[type] };++ const cyp1a2 = caffeineMarkers?.cyp1a2;+ if (cyp1a2?.status === 'beneficial') {+ schedule.caffeineCutoff = '16:00';+ schedule.caffeineNote = 'Fast metabolizer — caffeine clears quickly';+ } else if (cyp1a2?.status === 'concern' || cyp1a2?.status === 'major_concern') {+ schedule.caffeineCutoff = '12:00';+ schedule.caffeineNote = 'Slow metabolizer — limit afternoon caffeine';+ } else {+ schedule.caffeineCutoff = '14:00';+ schedule.caffeineNote = 'Typical metabolism — moderate afternoon cutoff';+ }++ if (mtnr1bStatus === 'concern' || mtnr1bStatus === 'major_concern') {+ schedule.lastMealCutoff = '19:00';+ schedule.mealNote = 'MTNR1B variant — earlier meals may improve glucose response';+ } else {+ schedule.lastMealCutoff = '20:30';+ schedule.mealNote = 'Standard meal timing recommendation';+ }++ return schedule;+}++// === Helper: build savedMarkers map from rsid/status pairs ===++function buildSavedMarkers(entries) {+ const markers = {};+ for (const [rsid, status, genotype] of entries) {+ markers[`uuid-${rsid}`] = { rsid, status, genotype: genotype || 'AG', category: 'sleep', gene: 'TEST' };+ }+ return markers;+}++// ============================================================+// Tests+// ============================================================++describe('extractSleepMarkers', () => {+ it('should extract all 5 sleep markers when present', () => {+ const saved = buildSavedMarkers([+ ['rs1801260', 'beneficial', 'AA'],+ ['rs57875989', 'typical', 'GG'],+ ['rs35333999', 'concern', 'AG'],+ ['rs2287161', 'beneficial', 'TT'],+ ['rs4753426', 'concern', 'CC']+ ]);++ const result = extractSleepMarkers(saved);++ expect(Object.keys(result)).toHaveLength(5);+ expect(result.clockGene.rsid).toBe('rs1801260');+ expect(result.clockGene.signal).toBe(-1); // beneficial → morning+ expect(result.cry1.signal).toBe(1); // beneficial → evening (CRY1 is inverted)+ expect(result.mtnr1b.signal).toBe(1); // concern → evening+ });++ it('should return partial results when only some markers exist', () => {+ const saved = buildSavedMarkers([+ ['rs1801260', 'typical', 'AG'],+ ['rs2287161', 'concern', 'CT']+ ]);++ const result = extractSleepMarkers(saved);++ expect(Object.keys(result)).toHaveLength(2);+ expect(result.clockGene).toBeDefined();+ expect(result.cry1).toBeDefined();+ expect(result.per2).toBeUndefined();+ });++ it('should return empty object for empty savedMarkers', () => {+ expect(extractSleepMarkers({})).toEqual({});+ expect(extractSleepMarkers(null)).toEqual({});+ expect(extractSleepMarkers(undefined)).toEqual({});+ });++ it('should ignore non-sleep markers', () => {+ const saved = buildSavedMarkers([+ ['rs762551', 'beneficial', 'AA'], // caffeine marker+ ['rs9999999', 'typical', 'GG'] // unknown+ ]);++ const result = extractSleepMarkers(saved);+ expect(Object.keys(result)).toHaveLength(0);+ });+});++describe('extractCaffeineMarkers', () => {+ it('should extract CYP1A2 and ADA markers', () => {+ const saved = buildSavedMarkers([+ ['rs762551', 'beneficial', 'AA'],+ ['rs73598374', 'typical', 'GG']+ ]);++ const result = extractCaffeineMarkers(saved);++ expect(Object.keys(result)).toHaveLength(2);+ expect(result.cyp1a2.rsid).toBe('rs762551');+ expect(result.ada.rsid).toBe('rs73598374');+ });++ it('should return empty for no caffeine markers', () => {+ const saved = buildSavedMarkers([+ ['rs1801260', 'beneficial', 'AA'] // sleep marker+ ]);++ expect(extractCaffeineMarkers(saved)).toEqual({});+ });+});++describe('computeChronotype', () => {+ describe('genetic-only derivation', () => {+ it('should classify as morning when all markers show morning tendency', () => {+ // beneficial sleep markers → morning signals, except CRY1 (inverted)+ const markers = {+ clockGene: { signal: -1 }, // beneficial = morning+ dec2: { signal: -1 },+ per2: { signal: -1 },+ cry1: { signal: -1 }, // concern = morning+ mtnr1b: { signal: 0 } // beneficial = neutral+ };++ const result = computeChronotype(markers, null);++ expect(result.type).toBe('morning');+ expect(result.scores.genetic).toBeLessThan(-0.25);+ expect(result.confidence).toBeGreaterThan(0);+ });++ it('should classify as evening when markers show evening tendency', () => {+ const markers = {+ clockGene: { signal: 1 },+ dec2: { signal: 1 },+ per2: { signal: 1 },+ cry1: { signal: 1 },+ mtnr1b: { signal: 1 }+ };++ const result = computeChronotype(markers, null);++ expect(result.type).toBe('evening');+ expect(result.scores.genetic).toBeGreaterThan(0.25);+ });++ it('should classify as intermediate for mixed signals', () => {+ const markers = {+ clockGene: { signal: -1 },+ cry1: { signal: 1 },+ per2: { signal: 0 }+ };++ const result = computeChronotype(markers, null);++ expect(result.type).toBe('intermediate');+ });++ it('should return intermediate with 0 confidence for no data', () => {+ const result = computeChronotype({}, null);++ expect(result.type).toBe('intermediate');+ expect(result.confidence).toBe(0);+ expect(result.scores.composite).toBe(0);+ });+ });++ describe('behavioral-only derivation', () => {+ it('should classify early riser as morning', () => {+ const result = computeChronotype({}, {+ preferredWakeTime: '05:30',+ preferredSleepTime: '21:00'+ });++ expect(result.type).toBe('morning');+ expect(result.scores.behavioral).toBeLessThan(0);+ });++ it('should classify late sleeper as evening', () => {+ const result = computeChronotype({}, {+ preferredWakeTime: '10:00',+ preferredSleepTime: '02:00'+ });++ expect(result.type).toBe('evening');+ expect(result.scores.behavioral).toBeGreaterThan(0);+ });+ });++ describe('combined genetic + behavioral', () => {+ it('should boost confidence when genetic and behavioral agree', () => {+ const morningMarkers = {+ clockGene: { signal: -1 },+ per2: { signal: -1 }+ };++ const agreeing = computeChronotype(morningMarkers, {+ preferredWakeTime: '05:30',+ preferredSleepTime: '21:30'+ });++ const geneticOnly = computeChronotype(morningMarkers, null);++ expect(agreeing.confidence).toBeGreaterThan(geneticOnly.confidence);+ });++ it('should penalize confidence when genetic and behavioral disagree', () => {+ const morningMarkers = {+ clockGene: { signal: -1 },+ per2: { signal: -1 }+ };++ const disagreeing = computeChronotype(morningMarkers, {+ preferredWakeTime: '11:00',+ preferredSleepTime: '03:00'+ });++ // Confidence should be lower due to disagreement penalty+ expect(disagreeing.confidence).toBeLessThan(0.5);+ });++ it('should average genetic and behavioral scores', () => {+ const markers = {+ clockGene: { signal: -1 },+ per2: { signal: -1 }+ };++ const result = computeChronotype(markers, {+ preferredWakeTime: '10:00', // evening behavioral+ preferredSleepTime: '01:00'+ });++ // Genetic is morning, behavioral is evening — should moderate+ expect(Math.abs(result.scores.composite)).toBeLessThan(+ Math.abs(result.scores.genetic)+ );+ });+ });++ describe('confidence calculation', () => {+ it('should give max marker confidence (0.5) with all 5 markers', () => {+ const allMarkers = {+ clockGene: { signal: 0 },+ dec2: { signal: 0 },+ per2: { signal: 0 },+ cry1: { signal: 0 },+ mtnr1b: { signal: 0 }+ };++ const result = computeChronotype(allMarkers, null);+ expect(result.confidence).toBe(0.5);+ });++ it('should give proportional marker confidence with partial markers', () => {+ const twoMarkers = {+ clockGene: { signal: 0 },+ per2: { signal: 0 }+ };++ const result = computeChronotype(twoMarkers, null);+ expect(result.confidence).toBe(0.2); // 2/5 * 0.5+ });+ });+});++describe('computeRecommendations', () => {+ describe('schedule templates', () => {+ it('should return morning schedule for morning type', () => {+ const result = computeRecommendations('morning', {}, null);+ expect(result.wakeTime).toBe('06:00');+ expect(result.sleepTime).toBe('22:00');+ expect(result.peakFocusStart).toBe('08:00');+ });++ it('should return intermediate schedule for intermediate type', () => {+ const result = computeRecommendations('intermediate', {}, null);+ expect(result.wakeTime).toBe('07:00');+ expect(result.peakFocusStart).toBe('09:30');+ });++ it('should return evening schedule for evening type', () => {+ const result = computeRecommendations('evening', {}, null);+ expect(result.wakeTime).toBe('08:30');+ expect(result.sleepTime).toBe('00:30');+ expect(result.peakFocusStart).toBe('11:00');+ });+ });++ describe('caffeine cutoff', () => {+ it('should set late cutoff for fast metabolizer (beneficial CYP1A2)', () => {+ const result = computeRecommendations('intermediate', {+ cyp1a2: { status: 'beneficial' }+ }, null);++ expect(result.caffeineCutoff).toBe('16:00');+ expect(result.caffeineNote).toContain('Fast metabolizer');+ });++ it('should set early cutoff for slow metabolizer (concern CYP1A2)', () => {+ const result = computeRecommendations('intermediate', {+ cyp1a2: { status: 'concern' }+ }, null);++ expect(result.caffeineCutoff).toBe('12:00');+ expect(result.caffeineNote).toContain('Slow metabolizer');+ });++ it('should set moderate cutoff for typical CYP1A2', () => {+ const result = computeRecommendations('intermediate', {+ cyp1a2: { status: 'typical' }+ }, null);++ expect(result.caffeineCutoff).toBe('14:00');+ });++ it('should handle major_concern same as concern', () => {+ const result = computeRecommendations('intermediate', {+ cyp1a2: { status: 'major_concern' }+ }, null);++ expect(result.caffeineCutoff).toBe('12:00');+ });+ });++ describe('MTNR1B meal timing', () => {+ it('should recommend earlier meals for MTNR1B concern', () => {+ const result = computeRecommendations('intermediate', {}, 'concern');++ expect(result.lastMealCutoff).toBe('19:00');+ expect(result.mealNote).toContain('MTNR1B');+ });++ it('should recommend standard meals for MTNR1B beneficial', () => {+ const result = computeRecommendations('intermediate', {}, 'beneficial');++ expect(result.lastMealCutoff).toBe('20:30');+ expect(result.mealNote).toContain('Standard');+ });++ it('should recommend standard meals for null MTNR1B', () => {+ const result = computeRecommendations('morning', {}, null);++ expect(result.lastMealCutoff).toBe('20:30');+ });+ });+});++// === Integration tests (mock fs + genome service) ===++describe('Integration: deriveChronotype', () => {+ let deriveChronotype, getChronotype, updateChronotypeBehavioral, getIdentityStatus;++ beforeEach(async () => {+ vi.resetModules();++ // Mock fs/promises+ vi.doMock('fs/promises', () => {+ const store = {};+ return {+ readFile: vi.fn(async (path) => {+ if (store[path]) return store[path];+ throw new Error('ENOENT');+ }),+ writeFile: vi.fn(async (path, data) => {+ store[path] = data;+ }),+ mkdir: vi.fn(async () => {})+ };+ });++ // Mock genome service+ vi.doMock('./genome.js', () => ({+ getGenomeSummary: vi.fn(async () => ({+ uploaded: true,+ markerCount: 7,+ uploadedAt: '2025-01-01T00:00:00.000Z',+ savedMarkers: buildSavedMarkers([+ ['rs1801260', 'beneficial', 'TT'], // CLOCK — morning+ ['rs57875989', 'typical', 'GG'], // DEC2 — neutral+ ['rs35333999', 'beneficial', 'CC'], // PER2 — morning+ ['rs2287161', 'concern', 'AG'], // CRY1 — morning (inverted)+ ['rs4753426', 'concern', 'CG'], // MTNR1B — evening+ ['rs762551', 'concern', 'AC'], // CYP1A2 — slow+ ['rs73598374', 'typical', 'GG'] // ADA — typical+ ])+ }))+ }));++ // Mock taste-questionnaire service+ vi.doMock('./taste-questionnaire.js', () => ({+ getTasteProfile: vi.fn(async () => ({+ completedCount: 2,+ totalSections: 5,+ overallPercentage: 40,+ lastSessionAt: '2025-01-15T00:00:00.000Z'+ }))+ }));++ const mod = await import('./identity.js');+ deriveChronotype = mod.deriveChronotype;+ getChronotype = mod.getChronotype;+ updateChronotypeBehavioral = mod.updateChronotypeBehavioral;+ getIdentityStatus = mod.getIdentityStatus;+ });++ it('should derive chronotype from genome markers', async () => {+ const result = await deriveChronotype();++ expect(result.type).toBeDefined();+ expect(['morning', 'intermediate', 'evening']).toContain(result.type);+ expect(result.confidence).toBeGreaterThan(0);+ expect(result.derivedAt).toBeDefined();+ expect(result.recommendations).toBeDefined();+ expect(result.geneticMarkers.clockGene).toBeDefined();+ expect(result.caffeineMarkers.cyp1a2).toBeDefined();+ });++ it('should return slow-metabolizer caffeine cutoff for concern CYP1A2', async () => {+ const result = await deriveChronotype();++ expect(result.recommendations.caffeineCutoff).toBe('12:00');+ });++ it('should recommend early meals for MTNR1B concern', async () => {+ const result = await deriveChronotype();++ expect(result.recommendations.lastMealCutoff).toBe('19:00');+ });++ it('should re-derive with behavioral overrides', async () => {+ const initial = await deriveChronotype();+ expect(initial.behavioralData).toBeNull();++ const updated = await updateChronotypeBehavioral({+ preferredWakeTime: '05:00',+ preferredSleepTime: '21:00'+ });++ expect(updated.behavioralData.preferredWakeTime).toBe('05:00');+ expect(updated.behavioralData.preferredSleepTime).toBe('21:00');+ expect(updated.confidence).toBeGreaterThan(initial.confidence);+ });++ it('should return cached chronotype on second getChronotype call', async () => {+ const first = await getChronotype();+ const second = await getChronotype();++ expect(first.derivedAt).toBe(second.derivedAt);+ });+});++describe('Integration: getIdentityStatus', () => {+ let getIdentityStatus;++ beforeEach(async () => {+ vi.resetModules();++ vi.doMock('fs/promises', () => {+ const store = {};+ return {+ readFile: vi.fn(async (path) => {+ if (store[path]) return store[path];+ throw new Error('ENOENT');+ }),+ writeFile: vi.fn(async (path, data) => {+ store[path] = data;+ }),+ mkdir: vi.fn(async () => {})+ };+ });++ vi.doMock('./genome.js', () => ({+ getGenomeSummary: vi.fn(async () => ({+ uploaded: true,+ markerCount: 3,+ uploadedAt: '2025-01-01T00:00:00.000Z',+ savedMarkers: {}+ }))+ }));++ vi.doMock('./taste-questionnaire.js', () => ({+ getTasteProfile: vi.fn(async () => ({+ completedCount: 0,+ totalSections: 5,+ overallPercentage: 0,+ lastSessionAt: null+ }))+ }));++ const mod = await import('./identity.js');+ getIdentityStatus = mod.getIdentityStatus;+ });++ it('should return all four sections', async () => {+ const result = await getIdentityStatus();++ expect(result.sections.genome).toBeDefined();+ expect(result.sections.chronotype).toBeDefined();+ expect(result.sections.aesthetics).toBeDefined();+ expect(result.sections.goals).toBeDefined();+ });++ it('should show genome as active when markers exist', async () => {+ vi.resetModules();++ vi.doMock('fs/promises', () => {+ const store = {};+ return {+ readFile: vi.fn(async (path) => {+ if (store[path]) return store[path];+ throw new Error('ENOENT');+ }),+ writeFile: vi.fn(async (path, data) => { store[path] = data; }),+ mkdir: vi.fn(async () => {})+ };+ });++ vi.doMock('./genome.js', () => ({+ getGenomeSummary: vi.fn(async () => ({+ uploaded: true,+ markerCount: 10,+ uploadedAt: '2025-01-01T00:00:00.000Z',+ savedMarkers: {}+ }))+ }));++ vi.doMock('./taste-questionnaire.js', () => ({+ getTasteProfile: vi.fn(async () => ({+ completedCount: 0, totalSections: 5, overallPercentage: 0, lastSessionAt: null+ }))+ }));++ const mod = await import('./identity.js');+ const result = await mod.getIdentityStatus();++ expect(result.sections.genome.status).toBe('active');+ });++ it('should show aesthetics as unavailable when no taste data', async () => {+ const result = await getIdentityStatus();++ expect(result.sections.aesthetics.status).toBe('unavailable');+ });++ it('should show chronotype as pending when genome uploaded but not derived', async () => {+ const result = await getIdentityStatus();++ expect(result.sections.chronotype.status).toBe('pending');+ });+});