/* insights.js — where a profile's last second opinion lives. * * One small file per profile, beside the health file it was judged from and written the same way * (write-temp-then-rename, mode 0600). It holds the answers, when they were made and which model * made them — never the health data itself, which stays in vitals-.json. * * Separate from server.js because two other readers want it: the Coach, which carries the judgment * into its prompt when it is recent enough to still be true, and the routes. Nothing here interprets * the document; the shape belongs to jev.js and the meaning to the screen. */ import fs from 'node:fs'; import path from 'node:path'; const DATA = process.env.DATA_DIR || '/data'; export const insightFile = uid => path.join(DATA, 'insight-' + String(uid).replace(/[^a-zA-Z0-9_-]/g, '') + '.json'); export function readInsight(uid) { try { return JSON.parse(fs.readFileSync(insightFile(uid), 'utf8')); } catch { return null; } } export function writeInsight(uid, doc) { const file = insightFile(uid); const tmp = file + '.tmp'; fs.writeFileSync(tmp, JSON.stringify(doc), { mode: 0o600 }); fs.renameSync(tmp, file); } export function clearInsight(uid) { try { fs.unlinkSync(insightFile(uid)); } catch { /* nothing stored */ } }