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 review — server-side score recomputation, tolerancePct preservation, startRange support
Adam Eivy463e73a5ca2a
diff --git a/server/lib/postValidation.js b/server/lib/postValidation.jsindex ff08ecf591..927c5b3d33 100644--- a/server/lib/postValidation.js+++ b/server/lib/postValidation.js@@ -8,21 +8,23 @@ import { z } from 'zod';export const postTagsSchema = z.record(z.string().max(200));// Individual question result+// correct is optional — the server recomputes it via scoreDrillconst questionResultSchema = z.object({prompt: z.string(),expected: z.number(),answered: z.number().nullable(),- correct: z.boolean(),+ correct: z.boolean().optional(),responseMs: z.number().min(0)});// Task result within a session+// score is optional — the server recomputes it via scoreDrillconst taskResultSchema = z.object({module: z.string(),type: z.string(),config: z.record(z.unknown()).optional().default({}),questions: z.array(questionResultSchema),- score: z.number().min(0).max(100),+ score: z.number().min(0).max(100).optional(),totalMs: z.number().min(0)});diff --git a/server/services/meatspacePost.js b/server/services/meatspacePost.jsindex 6cb9a97d03..34e3987536 100644--- a/server/services/meatspacePost.js+++ b/server/services/meatspacePost.js@@ -69,18 +69,35 @@ export async function getPostSession(id) {}export async function submitPostSession(sessionData) {+ const config = await getPostConfig();const data = await readJSONFile(SESSIONS_FILE, { sessions: [] });const now = new Date().toISOString();++ // Strip client-provided score/correct and recompute server-side+ const rawTasks = Array.isArray(sessionData.tasks) ? sessionData.tasks : [];+ const rescoredTasks = rawTasks.map(t => {+ const { score: _score, correct: _correct, ...rest } = t || {};+ // Strip correct from individual questions too+ const sanitizedQuestions = (rest.questions || []).map(q => {+ const { correct: _qCorrect, ...qRest } = q;+ return qRest;+ });+ const drillConfig = config.mentalMath?.drillTypes?.[rest.type] || {};+ const timeLimitMs = (drillConfig.timeLimitSec || 120) * 1000;+ const { score, questions } = scoreDrill(rest.type, sanitizedQuestions, timeLimitMs, rest.config || drillConfig);+ return { ...rest, questions, score };+ });+const session = {id: randomUUID(),date: now.split('T')[0],startedAt: now,completedAt: now,- durationMs: sessionData.tasks.reduce((sum, t) => sum + t.totalMs, 0),+ durationMs: rescoredTasks.reduce((sum, t) => sum + (t.totalMs || 0), 0),cadence: sessionData.cadence || 'daily',modules: sessionData.modules,- tasks: sessionData.tasks,- score: computeSessionScore(sessionData.tasks, sessionData.modules),+ tasks: rescoredTasks,+ score: computeSessionScore(rescoredTasks, sessionData.modules),tags: sessionData.tags || {}};@@ -94,10 +111,13 @@ export async function submitPostSession(sessionData) {export async function getPostStats(days = 30) {const sessions = await getPostSessions();- const cutoff = new Date();- cutoff.setDate(cutoff.getDate() - days);- const cutoffStr = cutoff.toISOString().split('T')[0];- const recent = sessions.filter(s => s.date >= cutoffStr);+ let recent = sessions;+ if (days > 0) {+ const cutoff = new Date();+ cutoff.setDate(cutoff.getDate() - days);+ const cutoffStr = cutoff.toISOString().split('T')[0];+ recent = sessions.filter(s => s.date >= cutoffStr);+ }if (recent.length === 0) {return { days, sessionCount: 0, overall: null, byModule: {}, byDrill: {} };@@ -142,8 +162,13 @@ export function generateDoublingChain(startValue, steps = 8) {return { type: 'doubling-chain', config: { startValue: start, steps }, questions };}-export function generateSerialSubtraction(start, subtrahend = 7, steps = 10) {- const startVal = start ?? (Math.floor(Math.random() * 101) + 100); // 100-200+export function generateSerialSubtraction(start, subtrahend = 7, steps = 10, startRange) {+ let startVal = start;+ if (startVal == null && Array.isArray(startRange) && startRange.length === 2) {+ const [lo, hi] = startRange;+ startVal = Math.floor(Math.random() * (hi - lo + 1)) + lo;+ }+ startVal = startVal ?? (Math.floor(Math.random() * 101) + 100); // 100-200const questions = [];let current = startVal;for (let i = 0; i < steps; i++) {@@ -176,7 +201,7 @@ export function generatePowers(bases = [2, 3, 5], maxExponent = 10, count = 8) {return { type: 'powers', config: { bases, maxExponent, count }, questions };}-export function generateEstimation(count = 5) {+export function generateEstimation(count = 5, tolerancePct) {const ops = ['+', '-', 'x'];const questions = [];for (let i = 0; i < count; i++) {@@ -197,7 +222,9 @@ export function generateEstimation(count = 5) {}questions.push({ prompt, expected });}- return { type: 'estimation', config: { count }, questions };+ const config = { count };+ if (tolerancePct != null) config.tolerancePct = tolerancePct;+ return { type: 'estimation', config, questions };}export function generateDrill(type, config = {}) {@@ -205,13 +232,13 @@ export function generateDrill(type, config = {}) {case 'doubling-chain':return generateDoublingChain(config.startValue, config.steps);case 'serial-subtraction':- return generateSerialSubtraction(config.startValue, config.subtrahend, config.steps);+ return generateSerialSubtraction(config.startValue, config.subtrahend, config.steps, config.startRange);case 'multiplication':return generateMultiplication(config.count, config.maxDigits);case 'powers':return generatePowers(config.bases, config.maxExponent, config.count);case 'estimation':- return generateEstimation(config.count);+ return generateEstimation(config.count, config.tolerancePct);default:return null;}@@ -221,21 +248,33 @@ export function generateDrill(type, config = {}) {// SCORING (pure functions)// =============================================================================-export function scoreDrill(type, questions, timeLimitMs) {- if (!questions?.length) return 0;+export function scoreDrill(type, questions, timeLimitMs, config = {}) {+ if (!questions?.length) return { score: 0, questions };++ // Recompute correctness server-side rather than trusting client flags+ const recomputed = questions.map(q => {+ let correct;+ if (q.answered == null) {+ correct = false;+ } else if (type === 'estimation') {+ const tolerance = ((config.tolerancePct ?? 10) / 100);+ correct = Math.abs(q.answered - q.expected) <= Math.abs(q.expected * tolerance);+ } else {+ correct = q.answered === q.expected;+ }+ return { ...q, correct };+ });- const answered = questions.filter(q => q.answered !== null && q.answered !== undefined);- const correct = questions.filter(q => q.correct);- const correctRatio = correct.length / questions.length;+ const answered = recomputed.filter(q => q.answered != null);+ const correctCount = recomputed.filter(q => q.correct).length;+ const correctRatio = correctCount / recomputed.length;const totalResponseMs = answered.reduce((sum, q) => sum + (q.responseMs || 0), 0);const avgResponseMs = answered.length > 0 ? totalResponseMs / answered.length : timeLimitMs;- // For estimation drills, accuracy is based on tolerance rather than exact match- // but we use the pre-computed correct flag from the clientconst speedBonus = Math.max(0, 1 - avgResponseMs / timeLimitMs);const score = Math.round((correctRatio * 0.8 + speedBonus * 0.2) * 100);- return Math.min(100, Math.max(0, score));+ return { score: Math.min(100, Math.max(0, score)), questions: recomputed };}function computeSessionScore(tasks, modules) {diff --git a/server/services/meatspacePost.test.js b/server/services/meatspacePost.test.jsindex 5ccf3181ab..b0db40959b 100644--- a/server/services/meatspacePost.test.js+++ b/server/services/meatspacePost.test.js@@ -1,80 +1,12 @@import { describe, it, expect } from 'vitest';--// Inline pure functions to avoid mocking file I/O--function generateDoublingChain(startValue, steps = 8) {- const start = startValue ?? (Math.floor(Math.random() * 7) + 3);- const questions = [];- let current = start;- for (let i = 0; i < steps; i++) {- const next = current * 2;- questions.push({ prompt: `${current} x 2`, expected: next });- current = next;- }- return { type: 'doubling-chain', config: { startValue: start, steps }, questions };-}--function generateSerialSubtraction(start, subtrahend = 7, steps = 10) {- const startVal = start ?? (Math.floor(Math.random() * 101) + 100);- const questions = [];- let current = startVal;- for (let i = 0; i < steps; i++) {- const next = current - subtrahend;- questions.push({ prompt: `${current} - ${subtrahend}`, expected: next });- current = next;- }- return { type: 'serial-subtraction', config: { startValue: startVal, subtrahend, steps }, questions };-}--function generateMultiplication(count = 10, maxDigits = 2) {- const maxVal = Math.pow(10, maxDigits) - 1;- const minVal = maxDigits > 1 ? Math.pow(10, maxDigits - 1) : 1;- const questions = [];- for (let i = 0; i < count; i++) {- const a = Math.floor(Math.random() * (maxVal - minVal + 1)) + minVal;- const b = Math.floor(Math.random() * (maxVal - minVal + 1)) + minVal;- questions.push({ prompt: `${a} x ${b}`, expected: a * b });- }- return { type: 'multiplication', config: { count, maxDigits }, questions };-}--function generatePowers(bases = [2, 3, 5], maxExponent = 10, count = 8) {- const questions = [];- for (let i = 0; i < count; i++) {- const base = bases[Math.floor(Math.random() * bases.length)];- const exp = Math.floor(Math.random() * (maxExponent - 1)) + 2;- questions.push({ prompt: `${base}^${exp}`, expected: Math.pow(base, exp) });- }- return { type: 'powers', config: { bases, maxExponent, count }, questions };-}--function generateEstimation(count = 5) {- const ops = ['+', '-', 'x'];- const questions = [];- for (let i = 0; i < count; i++) {- const a = Math.floor(Math.random() * 900) + 100;- const b = Math.floor(Math.random() * 900) + 100;- const op = ops[Math.floor(Math.random() * ops.length)];- let expected, prompt;- if (op === '+') { expected = a + b; prompt = `${a} + ${b}`; }- else if (op === '-') { expected = a - b; prompt = `${a} - ${b}`; }- else { expected = a * b; prompt = `${a} x ${b}`; }- questions.push({ prompt, expected });- }- return { type: 'estimation', config: { count }, questions };-}--function scoreDrill(type, questions, timeLimitMs) {- if (!questions?.length) return 0;- const answered = questions.filter(q => q.answered !== null && q.answered !== undefined);- const correct = questions.filter(q => q.correct);- const correctRatio = correct.length / questions.length;- const totalResponseMs = answered.reduce((sum, q) => sum + (q.responseMs || 0), 0);- const avgResponseMs = answered.length > 0 ? totalResponseMs / answered.length : timeLimitMs;- const speedBonus = Math.max(0, 1 - avgResponseMs / timeLimitMs);- const score = Math.round((correctRatio * 0.8 + speedBonus * 0.2) * 100);- return Math.min(100, Math.max(0, score));-}+import {+ generateDoublingChain,+ generateSerialSubtraction,+ generateMultiplication,+ generatePowers,+ generateEstimation,+ scoreDrill,+} from './meatspacePost.js';// =============================================================================// DOUBLING CHAIN TESTS@@ -133,6 +65,18 @@ describe('generateSerialSubtraction', () => {expect(start).toBeGreaterThanOrEqual(100);expect(start).toBeLessThanOrEqual(200);});++ it('samples start value from startRange when startValue is not provided', () => {+ const result = generateSerialSubtraction(undefined, 7, 3, [50, 60]);+ const start = result.config.startValue;+ expect(start).toBeGreaterThanOrEqual(50);+ expect(start).toBeLessThanOrEqual(60);+ });++ it('prefers explicit startValue over startRange', () => {+ const result = generateSerialSubtraction(150, 7, 3, [50, 60]);+ expect(result.config.startValue).toBe(150);+ });});// =============================================================================@@ -244,6 +188,16 @@ describe('generateEstimation', () => {}}});++ it('preserves tolerancePct in config when provided', () => {+ const result = generateEstimation(3, 25);+ expect(result.config.tolerancePct).toBe(25);+ });++ it('omits tolerancePct from config when not provided', () => {+ const result = generateEstimation(3);+ expect(result.config).not.toHaveProperty('tolerancePct');+ });});// =============================================================================@@ -252,37 +206,37 @@ describe('generateEstimation', () => {describe('scoreDrill', () => {it('returns 0 for empty questions', () => {- expect(scoreDrill('multiplication', [], 60000)).toBe(0);- expect(scoreDrill('multiplication', null, 60000)).toBe(0);+ expect(scoreDrill('multiplication', [], 60000).score).toBe(0);+ expect(scoreDrill('multiplication', null, 60000).score).toBe(0);});it('100% accuracy with fast responses gives high score', () => {const questions = [- { prompt: '5 x 3', expected: 15, answered: 15, correct: true, responseMs: 1000 },- { prompt: '7 x 4', expected: 28, answered: 28, correct: true, responseMs: 1500 },- { prompt: '6 x 8', expected: 48, answered: 48, correct: true, responseMs: 2000 }+ { prompt: '5 x 3', expected: 15, answered: 15, responseMs: 1000 },+ { prompt: '7 x 4', expected: 28, answered: 28, responseMs: 1500 },+ { prompt: '6 x 8', expected: 48, answered: 48, responseMs: 2000 }];- const score = scoreDrill('multiplication', questions, 120000);+ const { score } = scoreDrill('multiplication', questions, 120000);expect(score).toBeGreaterThanOrEqual(90);expect(score).toBeLessThanOrEqual(100);});- it('0% accuracy gives 0', () => {+ it('0% accuracy gives low score', () => {const questions = [- { prompt: '5 x 3', expected: 15, answered: 10, correct: false, responseMs: 1000 },- { prompt: '7 x 4', expected: 28, answered: 30, correct: false, responseMs: 1500 }+ { prompt: '5 x 3', expected: 15, answered: 10, responseMs: 1000 },+ { prompt: '7 x 4', expected: 28, answered: 30, responseMs: 1500 }];- const score = scoreDrill('multiplication', questions, 60000);+ const { score } = scoreDrill('multiplication', questions, 60000);// 0 accuracy * 0.8 = 0, plus small speed bonusexpect(score).toBeLessThanOrEqual(20);});it('unanswered questions count against accuracy', () => {const questions = [- { prompt: '5 x 3', expected: 15, answered: 15, correct: true, responseMs: 1000 },- { prompt: '7 x 4', expected: 28, answered: null, correct: false, responseMs: 0 }+ { prompt: '5 x 3', expected: 15, answered: 15, responseMs: 1000 },+ { prompt: '7 x 4', expected: 28, answered: null, responseMs: 0 }];- const score = scoreDrill('multiplication', questions, 60000);+ const { score } = scoreDrill('multiplication', questions, 60000);// 50% accuracy = 40 base, plus speed bonusexpect(score).toBeGreaterThanOrEqual(40);expect(score).toBeLessThanOrEqual(60);@@ -290,22 +244,44 @@ describe('scoreDrill', () => {it('slow responses reduce speed bonus', () => {const fast = [- { prompt: '5 x 3', expected: 15, answered: 15, correct: true, responseMs: 1000 }+ { prompt: '5 x 3', expected: 15, answered: 15, responseMs: 1000 }];const slow = [- { prompt: '5 x 3', expected: 15, answered: 15, correct: true, responseMs: 55000 }+ { prompt: '5 x 3', expected: 15, answered: 15, responseMs: 55000 }];- const fastScore = scoreDrill('multiplication', fast, 60000);- const slowScore = scoreDrill('multiplication', slow, 60000);+ const { score: fastScore } = scoreDrill('multiplication', fast, 60000);+ const { score: slowScore } = scoreDrill('multiplication', slow, 60000);expect(fastScore).toBeGreaterThan(slowScore);});it('score is clamped between 0 and 100', () => {const questions = [- { prompt: '1 x 1', expected: 1, answered: 1, correct: true, responseMs: 100 }+ { prompt: '1 x 1', expected: 1, answered: 1, responseMs: 100 }];- const score = scoreDrill('multiplication', questions, 120000);+ const { score } = scoreDrill('multiplication', questions, 120000);expect(score).toBeLessThanOrEqual(100);expect(score).toBeGreaterThanOrEqual(0);});++ it('recomputes correct flags server-side, ignoring client values', () => {+ const questions = [+ { prompt: '5 x 3', expected: 15, answered: 15, correct: false, responseMs: 1000 },+ { prompt: '7 x 4', expected: 28, answered: 99, correct: true, responseMs: 1000 }+ ];+ const { questions: recomputed } = scoreDrill('multiplication', questions, 60000);+ expect(recomputed[0].correct).toBe(true); // client said false, server recomputes true+ expect(recomputed[1].correct).toBe(false); // client said true, server recomputes false+ });++ it('estimation drill uses tolerancePct from config', () => {+ const questions = [+ { prompt: '500 + 300', expected: 800, answered: 850, responseMs: 1000 }+ ];+ // 850 is within 10% of 800 (80 tolerance), so correct+ const { questions: q10 } = scoreDrill('estimation', questions, 60000, { tolerancePct: 10 });+ expect(q10[0].correct).toBe(true);+ // 850 is NOT within 5% of 800 (40 tolerance), so incorrect+ const { questions: q5 } = scoreDrill('estimation', questions, 60000, { tolerancePct: 5 });+ expect(q5[0].correct).toBe(false);+ });});