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]): missing SDT class counts as chance; generateNBack guarantees both classes (codex round 4)

Adam Eivy249c29f59d8d
diff --git a/client/src/components/meatspace/post/PostCognitiveDrillRunner.jsx b/client/src/components/meatspace/post/PostCognitiveDrillRunner.jsx
index b5a0d49cbf..aeda361490 100644
--- a/client/src/components/meatspace/post/PostCognitiveDrillRunner.jsx
+++ b/client/src/components/meatspace/post/PostCognitiveDrillRunner.jsx
@@ -79,7 +79,9 @@ export function localNBackScore(drill, questions) {
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);
+ // A missing signal class counts as chance (0.5), mirroring the server — a
+ // never-press run through an all-non-target sequence must not show 100.
+ const accuracy = hitRate == null && crRate == null ? null : ((hitRate ?? 0.5) + (crRate ?? 0.5)) / 2;
return accuracy == null ? 0 : Math.min(100, Math.max(0, Math.round(accuracy * 100)));
}
diff --git a/server/services/meatspacePostCognitive.js b/server/services/meatspacePostCognitive.js
index 313eb438ac..1ad0e5d9fb 100644
--- a/server/services/meatspacePostCognitive.js
+++ b/server/services/meatspacePostCognitive.js
@@ -189,6 +189,34 @@ export function generateNBack(config = {}) {
targets.push(false);
}
}
+
+ // Guarantee BOTH signal classes among the decision positions (issue #2094):
+ // the SDT scorer needs at least one target and one non-target, or a missing
+ // class degrades to chance and caps the achievable score at 75. The random
+ // 30% roll can produce an all-non-target (or, rarely, all-target) run, so
+ // mutate one decision position until both classes exist. Targets are always
+ // recomputed from the sequence after a mutation — changing sequence[i] also
+ // flips whether i+n is a target, so the mirror can never be hand-patched.
+ const recomputeTargets = () => {
+ for (let i = 0; i < length; i++) {
+ targets[i] = i >= n && sequence[i] === sequence[i - n];
+ }
+ };
+ let classGuard = 0;
+ while (classGuard++ < 20) {
+ const decisions = targets.slice(n);
+ const hasTarget = decisions.some(Boolean);
+ const hasNonTarget = decisions.some(t => !t);
+ if (hasTarget && hasNonTarget) break;
+ const i = n + Math.floor(Math.random() * (length - n));
+ if (!hasTarget) {
+ sequence[i] = sequence[i - n];
+ } else {
+ sequence[i] = pick(NBACK_LETTERS.filter(l => l !== sequence[i - n]));
+ }
+ recomputeTargets();
+ }
+
return {
type: 'n-back',
config: { n, length, stimulusMs: clampInt(config.stimulusMs, 1000, 5000, 2500) },
@@ -395,13 +423,15 @@ function scoreNBack(drillData, questions) {
const nonTargets = correctRejections + falseAlarms;
const hitRate = targets ? hits / targets : null;
const correctRejectionRate = nonTargets ? correctRejections / nonTargets : null;
- // Balanced accuracy over whichever signal classes are present. A run with only
- // targets (or only non-targets) falls back to the one rate it can measure.
- let accuracy;
- if (hitRate != null && correctRejectionRate != null) accuracy = (hitRate + correctRejectionRate) / 2;
- else if (hitRate != null) accuracy = hitRate;
- else if (correctRejectionRate != null) accuracy = correctRejectionRate;
- else accuracy = null;
+ // Balanced accuracy. A missing signal class (a run with only targets or only
+ // non-targets — possible in legacy/short stored sequences even though the
+ // generator now guarantees both) counts as CHANCE (0.5), not as a free pass:
+ // otherwise never pressing through an all-non-target run would bank a perfect
+ // correct-rejection rate and score 100 — the exact do-nothing exploit this
+ // scorer exists to close. Single-class ceilings are therefore 75, floors 25.
+ const accuracy = hitRate == null && correctRejectionRate == null
+ ? null
+ : ((hitRate ?? 0.5) + (correctRejectionRate ?? 0.5)) / 2;
const presses = recomputed.filter(q => q.answered === 'match' && q.responseMs > 0);
const avgResponseMs = presses.length
diff --git a/server/services/meatspacePostCognitive.test.js b/server/services/meatspacePostCognitive.test.js
index a278aae52f..6ebfba9b13 100644
--- a/server/services/meatspacePostCognitive.test.js
+++ b/server/services/meatspacePostCognitive.test.js
@@ -156,21 +156,60 @@ describe('cognitive drill generators', () => {
describe('cognitive drill scorers (recompute the answer key, never trust client)', () => {
it('n-back scores a perfect run at 100 via signal-detection (all targets hit, no false alarms)', () => {
- // Deterministic sequence: A B A B A → with n=2, positions 2,3,4 are all targets.
- const drillData = { type: 'n-back', config: { n: 2, stimulusMs: 2000 }, sequence: ['A', 'B', 'A', 'B', 'A'] };
+ // Deterministic mixed sequence: A B A C A → with n=2, positions 2 and 4 are
+ // targets, 3 is a non-target — both signal classes present.
+ const drillData = { type: 'n-back', config: { n: 2, stimulusMs: 2000 }, sequence: ['A', 'B', 'A', 'C', 'A'] };
const questions = [
{ index: 2, answered: 'match', responseMs: 400 },
- { index: 3, answered: 'match', responseMs: 400 },
+ { index: 3, answered: null, responseMs: 0 },
{ index: 4, answered: 'match', responseMs: 400 },
];
const { score, accuracy, hits, misses, falseAlarms, correctRejections, questions: scored } =
scoreCognitiveDrill('n-back', drillData, questions);
expect(scored.every(q => q.correct)).toBe(true);
- // Balanced accuracy — all 3 targets hit (hitRate 1), no non-targets to falsely
- // alarm on — is 1.0 → 100. Speed no longer folds into the n-back score.
+ // Balanced accuracy — both targets hit (hitRate 1) and the non-target
+ // correctly rejected (CR rate 1) — is 1.0 → 100. Speed no longer folds in.
expect(score).toBe(100);
expect(accuracy).toBe(1);
- expect({ hits, misses, falseAlarms, correctRejections }).toEqual({ hits: 3, misses: 0, falseAlarms: 0, correctRejections: 0 });
+ expect({ hits, misses, falseAlarms, correctRejections }).toEqual({ hits: 2, misses: 0, falseAlarms: 0, correctRejections: 1 });
+ });
+
+ it('n-back: a single-class run caps at 75 — a missing SDT class counts as chance, not a free pass', () => {
+ // Legacy/stored all-non-target sequence (A B C D E, n=2 → no position matches
+ // 2 back): never pressing is a perfect correct-rejection rate, but with no
+ // targets ever presented the missing hit rate counts as 0.5 → (1+0.5)/2 = 75.
+ const drillData = { type: 'n-back', config: { n: 2 }, sequence: ['A', 'B', 'C', 'D', 'E'] };
+ const { score, accuracy } = scoreCognitiveDrill('n-back', drillData, [
+ { index: 2, answered: null, responseMs: 0 },
+ { index: 3, answered: null, responseMs: 0 },
+ { index: 4, answered: null, responseMs: 0 },
+ ]);
+ expect(accuracy).toBe(0.75);
+ expect(score).toBe(75);
+ // All-target sequence, all hits: same cap from the other side.
+ const allTargets = { type: 'n-back', config: { n: 2 }, sequence: ['A', 'B', 'A', 'B', 'A'] };
+ const perfectPresses = scoreCognitiveDrill('n-back', allTargets, [
+ { index: 2, answered: 'match', responseMs: 300 },
+ { index: 3, answered: 'match', responseMs: 300 },
+ { index: 4, answered: 'match', responseMs: 300 },
+ ]);
+ expect(perfectPresses.score).toBe(75);
+ });
+
+ it('generateNBack always yields BOTH signal classes among decision positions', () => {
+ // The SDT scorer needs at least one target and one non-target or the score
+ // caps at 75 — the generator guarantees both even at the shortest lengths.
+ for (let run = 0; run < 50; run++) {
+ const drill = generateCognitiveDrill('n-back', { n: 2, length: 7 });
+ const decisions = drill.targets.slice(drill.config.n);
+ expect(decisions.some(Boolean)).toBe(true);
+ expect(decisions.some(t => !t)).toBe(true);
+ // The targets mirror stays derived from the sequence (never hand-patched).
+ drill.targets.forEach((t, i) => {
+ const expected = i >= drill.config.n && drill.sequence[i] === drill.sequence[i - drill.config.n];
+ expect(t).toBe(expected);
+ });
+ }
});
it('n-back marks a false-positive press wrong even if client claims correct', () => {