Skip to content

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

fix([issue-2094]): mirror SDT n-back and latency reaction-time scoring in the pre-save client results (codex round 3)

Adam Eivy8056fd062602
diff --git a/client/src/components/meatspace/post/PostCognitiveDrillRunner.jsx b/client/src/components/meatspace/post/PostCognitiveDrillRunner.jsx
index 38d9c8a9f3..b5a0d49cbf 100644
--- a/client/src/components/meatspace/post/PostCognitiveDrillRunner.jsx
+++ b/client/src/components/meatspace/post/PostCognitiveDrillRunner.jsx
@@ -60,6 +60,45 @@ export function localAccuracyScore(questions) {
return Math.round((correct / questions.length) * 100);
}
+// Pre-save mirror of the server's signal-detection n-back score (issue #2094):
+// balanced accuracy over targets (hit rate) and non-targets (correct-rejection
+// rate), so the results screen matches what the save persists instead of raw
+// position accuracy (which pays ~70% for never pressing).
+export function localNBackScore(drill, questions) {
+ const seq = drill?.sequence || [];
+ const n = drill?.config?.n ?? 2;
+ let hits = 0, misses = 0, falseAlarms = 0, correctRejections = 0;
+ for (const q of questions) {
+ const isTarget = q.index >= n && seq[q.index] != null && seq[q.index] === seq[q.index - n];
+ const pressed = q.answered === 'match';
+ if (isTarget) { if (pressed) hits += 1; else misses += 1; }
+ else if (pressed) falseAlarms += 1;
+ else correctRejections += 1;
+ }
+ const targets = hits + misses;
+ const nonTargets = correctRejections + falseAlarms;
+ const hitRate = targets ? hits / targets : null;
+ const crRate = nonTargets ? correctRejections / nonTargets : null;
+ const accuracy = hitRate != null && crRate != null ? (hitRate + crRate) / 2 : (hitRate ?? crRate);
+ return accuracy == null ? 0 : Math.min(100, Math.max(0, Math.round(accuracy * 100)));
+}
+
+// Pre-save mirror of the server's latency-based reaction-time score (issue
+// #2094): median RT of VALID trials against the mode's reference curve, scaled
+// by the valid-trial rate so false starts pull the headline number down.
+export function localReactionTimeScore(drill, questions) {
+ const mode = drill?.config?.mode === 'choice' ? 'choice' : 'simple';
+ const refMs = mode === 'choice' ? 1200 : 600;
+ const fastMs = mode === 'choice' ? 400 : 200;
+ const valid = questions.filter(q => q.correct && !q.falseStart && (q.responseMs || 0) > 0);
+ if (!questions.length || !valid.length) return 0;
+ const sorted = valid.map(q => Math.min(q.responseMs, refMs * 3)).sort((a, b) => a - b);
+ const mid = Math.floor(sorted.length / 2);
+ const medianMs = sorted.length % 2 ? sorted[mid] : Math.round((sorted[mid - 1] + sorted[mid]) / 2);
+ const latencyScore = Math.min(100, Math.max(0, (100 * (refMs - medianMs)) / (refMs - fastMs)));
+ return Math.round(latencyScore * (valid.length / questions.length));
+}
+
// Renders a `[x, y]` cell list (mental-rotation shapes) as a small filled-cell
// grid within a `size x size` box — no images/canvas, just CSS grid squares.
function ShapeGrid({ cells, size = 4, cellPx = 14 }) {
@@ -86,13 +125,22 @@ function ShapeGrid({ cells, size = 4, cellPx = 14 }) {
// mirror, and its `index`/`answered`/`responseMs` fields are what that
// rescoring depends on.
export function buildCognitiveResult({ type, drill, questions, totalMs }) {
+ // n-back and reaction-time mirror the server's rescoring semantics (SDT
+ // balanced accuracy / valid-median latency, issue #2094) so the pre-save
+ // results screen doesn't show a number that jumps on save. Other types keep
+ // the raw-accuracy approximation (server adds only a small speed bonus).
+ const score = type === 'n-back'
+ ? localNBackScore(drill, questions)
+ : type === 'reaction-time'
+ ? localReactionTimeScore(drill, questions)
+ : localAccuracyScore(questions);
return {
module: 'cognitive',
type,
config: drill.config,
drillData: drill,
questions,
- score: localAccuracyScore(questions),
+ score,
totalMs,
};
}
diff --git a/client/src/components/meatspace/post/PostCognitiveDrillRunner.test.jsx b/client/src/components/meatspace/post/PostCognitiveDrillRunner.test.jsx
index c2a62f2005..b133a29bd1 100644
--- a/client/src/components/meatspace/post/PostCognitiveDrillRunner.test.jsx
+++ b/client/src/components/meatspace/post/PostCognitiveDrillRunner.test.jsx
@@ -31,20 +31,61 @@ describe('localAccuracyScore', () => {
});
describe('buildCognitiveResult', () => {
- it('assembles the full onComplete payload shape', () => {
- const drill = { type: 'n-back', config: { n: 2 }, sequence: ['A', 'B'] };
+ it('assembles the full onComplete payload shape (stroop keeps raw accuracy)', () => {
+ const drill = { type: 'stroop', config: { count: 2 }, trials: [] };
const questions = [{ correct: true }, { correct: false }];
- const result = buildCognitiveResult({ type: 'n-back', drill, questions, totalMs: 4200 });
+ const result = buildCognitiveResult({ type: 'stroop', drill, questions, totalMs: 4200 });
expect(result).toEqual({
module: 'cognitive',
- type: 'n-back',
- config: { n: 2 },
+ type: 'stroop',
+ config: { count: 2 },
drillData: drill,
questions,
score: 50,
totalMs: 4200,
});
});
+
+ it('n-back pre-save score mirrors the server SDT balanced accuracy (issue #2094)', () => {
+ // A B A C A with n=2 → indices 2,4 are targets, 3 is a non-target.
+ const drill = { type: 'n-back', config: { n: 2 }, sequence: ['A', 'B', 'A', 'C', 'A'] };
+ // Never pressing: hitRate 0, correct-rejection rate 1 → balanced 50, not ~67.
+ const silent = [
+ { index: 2, answered: null, correct: false, responseMs: 0 },
+ { index: 3, answered: null, correct: true, responseMs: 0 },
+ { index: 4, answered: null, correct: false, responseMs: 0 },
+ ];
+ expect(buildCognitiveResult({ type: 'n-back', drill, questions: silent, totalMs: 1 }).score).toBe(50);
+ // Perfect run → 100.
+ const perfect = [
+ { index: 2, answered: 'match', correct: true, responseMs: 300 },
+ { index: 3, answered: null, correct: true, responseMs: 0 },
+ { index: 4, answered: 'match', correct: true, responseMs: 300 },
+ ];
+ expect(buildCognitiveResult({ type: 'n-back', drill, questions: perfect, totalMs: 1 }).score).toBe(100);
+ });
+
+ it('reaction-time pre-save score mirrors the server latency scoring (issue #2094)', () => {
+ const drill = { type: 'reaction-time', config: { mode: 'simple' }, trials: [] };
+ // All valid at 240ms median → round(100*(600-240)/400) = 90 (matches server).
+ const clean = [
+ { correct: true, falseStart: false, responseMs: 200 },
+ { correct: true, falseStart: false, responseMs: 240 },
+ { correct: true, falseStart: false, responseMs: 260 },
+ ];
+ expect(buildCognitiveResult({ type: 'reaction-time', drill, questions: clean, totalMs: 1 }).score).toBe(90);
+ // One perfect press among 3 false starts → 100 × 1/4 = 25 (valid-rate scaling).
+ const sloppy = [
+ { correct: true, falseStart: false, responseMs: 200 },
+ { correct: false, falseStart: true, responseMs: 0 },
+ { correct: false, falseStart: true, responseMs: 0 },
+ { correct: false, falseStart: true, responseMs: 0 },
+ ];
+ expect(buildCognitiveResult({ type: 'reaction-time', drill, questions: sloppy, totalMs: 1 }).score).toBe(25);
+ // A clean-but-very-slow run no longer shows a pre-save 100.
+ const slow = [{ correct: true, falseStart: false, responseMs: 580 }];
+ expect(buildCognitiveResult({ type: 'reaction-time', drill, questions: slow, totalMs: 1 }).score).toBe(5);
+ });
});
describe('buildNBackQuestions', () => {