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

refactor: hoist POST memory-type and n-back scoring constants to server/lib leaves (#6818)

[._.]/ Adam Eivy4bc864ee6c53
diff --git a/client/src/components/meatspace/post/constants.js b/client/src/components/meatspace/post/constants.js
index 5b9dae5fd8..3f08249972 100644
--- a/client/src/components/meatspace/post/constants.js
+++ b/client/src/components/meatspace/post/constants.js
@@ -1,19 +1,22 @@
export const LLM_DRILL_TYPES = ['word-association', 'story-recall', 'verbal-fluency', 'wit-comeback', 'pun-wordplay', 'compound-chain', 'bridge-word', 'double-meaning', 'idiom-twist', 'what-if', 'alternative-uses', 'story-prompt', 'invention-pitch', 'reframe'];
-// Mirrors the server's POST_SUPPORTED_MEMORY_TYPES (server/lib/postValidation.js)
-// — all three memory drill types are now fully scored in a POST session (issue
+// All three memory drill types are now fully scored in a POST session (issue
// #2099/#2116): usePostSession.finishDrill uses this to tag the result's
// module as `memory` (not `mental-math`) and preserve memoryItemId so the
-// server's schedule/mastery advancement fires.
-export const MEMORY_DRILL_TYPES = ['memory-fill-blank', 'memory-sequence', 'memory-element-flash'];
-// Deterministic cognitive drills (no LLM). Mirror the server's
-// COGNITIVE_DRILL_TYPES in server/lib/postDrillTypes.js (re-exported by
-// server/services/meatspacePostCognitive.js, which owns the generators).
-export const COGNITIVE_DRILL_TYPES = ['n-back', 'digit-span', 'stroop', 'schulte-table', 'mental-rotation', 'reaction-time', 'task-switching', 'go-no-go', 'flanker'];
+// server's schedule/mastery advancement fires. Re-exported from the server's
+// POST_SUPPORTED_MEMORY_TYPES (server/lib/postDrillTypes.js) under its
+// established client name.
+export { POST_SUPPORTED_MEMORY_TYPES as MEMORY_DRILL_TYPES } from '../../../../../server/lib/postDrillTypes.js';
+// Deterministic cognitive drills (no LLM), backed by generators in
+// server/services/meatspacePostCognitive.js.
+export { COGNITIVE_DRILL_TYPES } from '../../../../../server/lib/postDrillTypes.js';
-// Cognitive drills that have a progressive difficulty ladder. Mirror of the
-// COGNITIVE_LADDERS keys in server/lib/postProgression.js — reaction-time is the
-// one cognitive drill with no ladder, so its stored config always runs as-is.
-export const COGNITIVE_LADDER_TYPES = ['n-back', 'digit-span', 'schulte-table', 'mental-rotation', 'stroop', 'task-switching', 'go-no-go', 'flanker'];
+// Cognitive drills that have a progressive difficulty ladder — reaction-time is
+// the one cognitive drill with no ladder, so its stored config always runs
+// as-is. Re-exported from the COGNITIVE_LADDERS keys in
+// server/lib/postProgression.js. (Imported, not re-exported directly, because
+// cognitiveRungPending below reads it too.)
+import { COGNITIVE_LADDER_TYPES } from '../../../../../server/lib/postProgression.js';
+export { COGNITIVE_LADDER_TYPES };
/**
* Whether this drill's effective config is still UNKNOWN: a laddered drill left
@@ -441,24 +444,7 @@ export const getDifficultyColor = (difficulty) => {
return 'bg-port-success/20 text-port-success';
};
-// Balanced (signal-detection) accuracy for n-back questions, derived from only
-// `answered` + `correct` — the fields BOTH legacy stored sessions and pre-save
-// client results carry. `correct` has always been computed as
-// "(pressed ? match : no-match) === expected", so `isTarget = pressed === correct`
-// is an identity across old and new scorers; legacy raw `correct` flags must
-// NOT be averaged directly (a never-press run would still read ~70%). A missing
-// signal class counts as chance (0.5). Mirrors `nBackBalancedAccuracy` in
-// server/services/meatspacePost.js — keep the two in sync (issue #2094).
-export function nBackBalancedAccuracy(questions) {
- let hits = 0, misses = 0, falseAlarms = 0, correctRejections = 0;
- for (const q of Array.isArray(questions) ? questions : []) {
- const pressed = q?.answered === 'match';
- const isTarget = pressed === !!q?.correct;
- if (isTarget) { if (pressed) hits += 1; else misses += 1; }
- else if (pressed) falseAlarms += 1;
- else correctRejections += 1;
- }
- const hitRate = hits + misses ? hits / (hits + misses) : null;
- const crRate = correctRejections + falseAlarms ? correctRejections / (correctRejections + falseAlarms) : null;
- return hitRate == null && crRate == null ? null : ((hitRate ?? 0.5) + (crRate ?? 0.5)) / 2;
-}
+// Balanced (signal-detection) accuracy for n-back questions (issue #2094).
+// Re-exported from the leaf both server/services/meatspacePost.js and this
+// file used to carry separate copies of.
+export { nBackBalancedAccuracy } from '../../../../../server/lib/postScoring.js';
diff --git a/server/lib/postDrillTypes.js b/server/lib/postDrillTypes.js
index 7241c4eb51..8f2af86df4 100644
--- a/server/lib/postDrillTypes.js
+++ b/server/lib/postDrillTypes.js
@@ -32,3 +32,11 @@ export const COGNITIVE_DRILL_TYPES = Object.freeze([
'go-no-go',
'flanker',
]);
+
+// Memory drills supported by the POST runner (client-side scoring with string
+// comparison) — trusted for score + schedule/mastery advancement on session
+// submit (issue #2099). Currently identical to MEMORY_DRILL_TYPES; kept as a
+// separate list (rather than aliasing MEMORY_DRILL_TYPES directly) so a FUTURE
+// memory drill type can ship generation-only, ahead of its scoring support,
+// without silently trusting a client-supplied score for it.
+export const POST_SUPPORTED_MEMORY_TYPES = ['memory-fill-blank', 'memory-sequence', 'memory-element-flash'];
diff --git a/server/lib/postScoring.js b/server/lib/postScoring.js
new file mode 100644
index 0000000000..a4d067f0d4
--- /dev/null
+++ b/server/lib/postScoring.js
@@ -0,0 +1,25 @@
+/**
+ * Scoring and accuracy helpers for post-training drill types shared between
+ * server (`services/meatspacePost.js`) and client (drill UI, via
+ * `components/meatspace/post/constants.js`).
+ */
+
+// Balanced (signal-detection) accuracy for n-back questions, derived from only
+// `answered` + `correct` — fields both legacy stored sessions and pre-save
+// client results carry. Works because `correct` was always computed as
+// "(pressed ? match : no-match) === expected", so `isTarget = pressed === correct`
+// is an identity across old and new scorers. A missing signal class counts as
+// chance (0.5), matching scoreNBack.
+export function nBackBalancedAccuracy(questions) {
+ let hits = 0, misses = 0, falseAlarms = 0, correctRejections = 0;
+ for (const q of Array.isArray(questions) ? questions : []) {
+ const pressed = q?.answered === 'match';
+ const isTarget = pressed === !!q?.correct;
+ if (isTarget) { if (pressed) hits += 1; else misses += 1; }
+ else if (pressed) falseAlarms += 1;
+ else correctRejections += 1;
+ }
+ const hitRate = hits + misses ? hits / (hits + misses) : null;
+ const crRate = correctRejections + falseAlarms ? correctRejections / (correctRejections + falseAlarms) : null;
+ return hitRate == null && crRate == null ? null : ((hitRate ?? 0.5) + (crRate ?? 0.5)) / 2;
+}
diff --git a/server/lib/postValidation.js b/server/lib/postValidation.js
index eb1d40a07d..94d125cc1b 100644
--- a/server/lib/postValidation.js
+++ b/server/lib/postValidation.js
@@ -1,5 +1,5 @@
import { z } from 'zod';
-import { CACHEABLE_TYPES, COGNITIVE_DRILL_TYPES } from './postDrillTypes.js';
+import { CACHEABLE_TYPES, COGNITIVE_DRILL_TYPES, POST_SUPPORTED_MEMORY_TYPES } from './postDrillTypes.js';
import { TOPIC_IDS } from './postTopics.js';
import { HHMM_STRICT_RE } from './timezone.js';
import { POST_LLM_MAX_SEMANTIC_CANDIDATES, postLlmEvaluationSchema } from './postLlmContracts.js';
@@ -99,13 +99,8 @@ const llmResponseSchema = z.object({
const MATH_DRILL_TYPES = ['doubling-chain', 'serial-subtraction', 'multiplication', 'powers', 'estimation', 'applied-numeracy'];
const LLM_DRILL_TYPES = ['word-association', 'story-recall', 'verbal-fluency', 'wit-comeback', 'pun-wordplay', 'compound-chain', 'bridge-word', 'double-meaning', 'idiom-twist', 'what-if', 'alternative-uses', 'story-prompt', 'invention-pitch', 'reframe'];
const MEMORY_DRILL_TYPES = ['memory-fill-blank', 'memory-sequence', 'memory-element-flash'];
-// Memory drills supported by the POST runner (client-side scoring with string
-// comparison) — trusted for score + schedule/mastery advancement on session
-// submit (issue #2099). Currently identical to MEMORY_DRILL_TYPES; kept as a
-// separate list (rather than aliasing MEMORY_DRILL_TYPES directly) so a FUTURE
-// memory drill type can ship generation-only, ahead of its scoring support,
-// without silently trusting a client-supplied score for it.
-const POST_SUPPORTED_MEMORY_TYPES = ['memory-fill-blank', 'memory-sequence', 'memory-element-flash'];
+// POST_SUPPORTED_MEMORY_TYPES lives in ./postDrillTypes.js (a zod-free leaf,
+// re-exported by the client's meatspace/post/constants.js) — imported above.
// Canonical set of coarse "module" tags a scored POST task/session can carry
// (mental-math / llm-drills / cognitive drills / memory drills). Shared by the
// session-submit schema (below) and sessionModules config so a typo'd module
diff --git a/server/services/meatspacePost.js b/server/services/meatspacePost.js
index a32c608e40..dbc8df90f6 100644
--- a/server/services/meatspacePost.js
+++ b/server/services/meatspacePost.js
@@ -11,6 +11,7 @@ import { EventEmitter } from 'events';
import { atomicWrite, PATHS, ensureDir, readJSONFile } from '../lib/fileUtils.js';
import { deepMerge } from '../lib/objects.js';
import { LLM_DRILL_TYPES, MEMORY_DRILL_TYPES, POST_SUPPORTED_MEMORY_TYPES } from '../lib/postValidation.js';
+import { nBackBalancedAccuracy } from '../lib/postScoring.js';
import { normalizeHistoricalPostLlmEvaluation, normalizePostLlmEvaluation } from '../lib/postLlmContracts.js';
import { resolveTopicForDrillType, isTopicEnabled, isMemoryItemEnabled } from '../lib/postTopics.js';
import {
@@ -866,28 +867,10 @@ export async function getPostReviewReps(now = new Date(), limit = 2) {
* `null` — never NaN. Used by stats aggregation and the adaptive signal so old
* and new session shapes both read cleanly.
*/
-/**
- * Balanced (signal-detection) accuracy for n-back questions, derived from only
- * `answered` + `correct` — fields both legacy stored sessions and pre-save
- * client results carry. Works because `correct` was always computed as
- * "(pressed ? match : no-match) === expected", so `isTarget = pressed === correct`
- * is an identity across old and new scorers. A missing signal class counts as
- * chance (0.5), matching scoreNBack. Exported for the client-fallback mirror
- * tests; the client copy lives in components/meatspace/post/constants.js.
- */
-export function nBackBalancedAccuracy(questions) {
- let hits = 0, misses = 0, falseAlarms = 0, correctRejections = 0;
- for (const q of Array.isArray(questions) ? questions : []) {
- const pressed = q?.answered === 'match';
- const isTarget = pressed === !!q?.correct;
- if (isTarget) { if (pressed) hits += 1; else misses += 1; }
- else if (pressed) falseAlarms += 1;
- else correctRejections += 1;
- }
- const hitRate = hits + misses ? hits / (hits + misses) : null;
- const crRate = correctRejections + falseAlarms ? correctRejections / (correctRejections + falseAlarms) : null;
- return hitRate == null && crRate == null ? null : ((hitRate ?? 0.5) + (crRate ?? 0.5)) / 2;
-}
+// nBackBalancedAccuracy now lives in ../lib/postScoring.js (imported above) —
+// re-exported here for existing importers of this module, and the client copy
+// in components/meatspace/post/constants.js re-exports the same leaf.
+export { nBackBalancedAccuracy };
export function deriveTaskAccuracy(task) {
if (typeof task?.accuracy === 'number' && !Number.isNaN(task.accuracy)) return task.accuracy;