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: preserve partial POST practice configuration saves

Adam Eivy8e35c037a8d9
diff --git a/server/lib/postValidation.js b/server/lib/postValidation.js
index 9ff1911a2e..3d1f46480e 100644
--- a/server/lib/postValidation.js
+++ b/server/lib/postValidation.js
@@ -360,13 +360,13 @@ export const postGoalsSchema = z.object({
export const postConfigUpdateSchema = z.object({
mentalMath: z.object({
enabled: z.boolean().optional(),
- drillTypes: z.record(z.enum(MATH_DRILL_TYPES), drillTypeConfigSchema).optional()
+ drillTypes: z.partialRecord(z.enum(MATH_DRILL_TYPES), drillTypeConfigSchema).optional()
}).optional(),
llmDrills: z.object({
enabled: z.boolean().optional(),
providerId: z.string().nullable().optional(),
model: z.string().nullable().optional(),
- drillTypes: z.record(z.enum(LLM_DRILL_TYPES), llmDrillTypeConfigSchema).optional()
+ drillTypes: z.partialRecord(z.enum(LLM_DRILL_TYPES), llmDrillTypeConfigSchema).optional()
}).optional(),
rhetoricEvaluator: postRhetoricEvaluatorConfigSchema.optional(),
// Deterministic cognitive drills — no provider, so no provider/model fields.
@@ -390,11 +390,8 @@ export const postConfigUpdateSchema = z.object({
// — can be dropped from the daily rotation without deleting it (which would
// throw away its mastery/schedule history). Keys are memory item ids, so this
// is an open `z.string()` record rather than an enum.
- // `partialRecord`, not `record`: an enum-keyed `z.record` is EXHAUSTIVE in
- // zod 4 (every enum member required), which is why the sibling module blocks
- // above force their UI to write a complete drillTypes map. These blocks are
- // new, so they take the more forgiving contract — a patch may carry just the
- // one flag it is changing.
+ // Sparse maps preserve older-client saves when the registry gains a drill,
+ // matching the other module blocks' incremental-update contract.
memory: z.object({
enabled: z.boolean().optional(),
drillTypes: z.partialRecord(z.enum(MEMORY_DRILL_TYPES), drillTypeConfigSchema).optional(),
diff --git a/server/lib/postValidation.test.js b/server/lib/postValidation.test.js
index 4ed3aebf35..e8fbe1d867 100644
--- a/server/lib/postValidation.test.js
+++ b/server/lib/postValidation.test.js
@@ -1029,18 +1029,20 @@ describe('Practice Plan save body ↔ postConfigUpdateSchema (issue #3252)', ()
});
it('every enum-keyed drill-type record the body sends is complete', () => {
- // The sibling module blocks use an EXHAUSTIVE z.record — a missing key is a
- // 400, not a silent strip — so assert the registry supplies every enum member.
+ // The current UI must offer every drill even though older clients and
+ // incremental saves may submit only a subset of the server's registry.
const body = practicePlanSaveBody();
expect(Object.keys(body.mentalMath.drillTypes).sort()).toEqual([...MATH_DRILL_TYPE_IDS].sort());
expect(Object.keys(body.llmDrills.drillTypes).sort()).toEqual([...LLM_DRILL_TYPE_IDS].sort());
expect(Object.keys(body.cognitive.drillTypes).sort()).toEqual([...COGNITIVE_DRILL_TYPE_IDS].sort());
});
- it('rejects the same body with one drill type dropped, proving the guard bites', () => {
+ it('accepts a legacy plan body that predates a drill without inventing its settings', () => {
const body = practicePlanSaveBody();
delete body.llmDrills.drillTypes['bridge-word'];
- expect(() => postConfigUpdateSchema.parse(body)).toThrow();
+ const parsed = postConfigUpdateSchema.parse(body);
+ expect(parsed).toEqual(body);
+ expect(parsed.llmDrills.drillTypes).not.toHaveProperty('bridge-word');
});
});
diff --git a/server/routes/meatspacePostRoutes.reminder.test.js b/server/routes/meatspacePostRoutes.reminder.test.js
index c8eadb6388..a6d05205a2 100644
--- a/server/routes/meatspacePostRoutes.reminder.test.js
+++ b/server/routes/meatspacePostRoutes.reminder.test.js
@@ -64,4 +64,33 @@ describe('PUT /api/meatspace/post/config', () => {
expect(r.status).toBe(200);
expect(postService.updatePostConfig).toHaveBeenCalledWith({ adaptive: { enabled: true } });
});
+
+ it('accepts sparse math and LLM drill maps without requiring unrelated drills', async () => {
+ const patch = {
+ mentalMath: { drillTypes: { multiplication: { enabled: false } } },
+ llmDrills: { drillTypes: { 'word-association': {
+ count: 3,
+ providerId: 'example-provider',
+ model: 'example-model',
+ } } },
+ };
+ postService.updatePostConfig.mockImplementation(async updates => updates);
+
+ const r = await request(app).put('/api/meatspace/post/config').send(patch);
+
+ expect(r.status).toBe(200);
+ expect(r.body).toEqual(patch);
+ expect(postService.updatePostConfig).toHaveBeenCalledWith(patch);
+ });
+
+ it.each([
+ ['an unknown drill', { mentalMath: { drillTypes: { 'not-a-drill': { enabled: true } } } }],
+ ['an excessive LLM drill count', { llmDrills: { drillTypes: { 'word-association': { count: 21 } } } }],
+ ])('rejects %s before attempting a configuration write', async (_label, patch) => {
+ const r = await request(app).put('/api/meatspace/post/config').send(patch);
+
+ expect(r.status).toBe(400);
+ expect(r.body).toMatchObject({ error: 'Validation failed', code: 'VALIDATION_ERROR' });
+ expect(postService.updatePostConfig).not.toHaveBeenCalled();
+ });
});
diff --git a/server/services/meatspacePost.js b/server/services/meatspacePost.js
index 67a22855fc..2267e22942 100644
--- a/server/services/meatspacePost.js
+++ b/server/services/meatspacePost.js
@@ -10,6 +10,7 @@ import { randomUUID } from 'crypto';
import { EventEmitter } from 'events';
import { atomicWrite, PATHS, ensureDir, readJSONFile } from '../lib/fileUtils.js';
import { deepMerge } from '../lib/objects.js';
+import { createFileWriteQueue } from '../lib/fileWriteQueue.js';
import { LLM_DRILL_TYPES, MEMORY_DRILL_TYPES, POST_SUPPORTED_MEMORY_TYPES } from '../lib/postValidation.js';
import { nBackBalancedAccuracy, resolveEstimationTolerancePct } from '../lib/postScoring.js';
import { normalizeHistoricalPostLlmEvaluation, normalizePostLlmEvaluation } from '../lib/postLlmContracts.js';
@@ -74,6 +75,7 @@ export { computePostStreaks };
const MEATSPACE_DIR = PATHS.meatspace;
const CONFIG_FILE = join(MEATSPACE_DIR, 'post-config.json');
+const queueConfigWrite = createFileWriteQueue();
// Tiny pub/sub (mirrors settingsEvents in server/services/settings.js) so
// features that react to a specific config slice — e.g. meatspacePostReminder.js
@@ -333,7 +335,11 @@ export async function getPostConfig() {
return deepMerge(baseDefaults, config);
}
-export async function updatePostConfig(updates) {
+export function updatePostConfig(updates) {
+ return queueConfigWrite(() => applyPostConfigUpdate(updates));
+}
+
+async function applyPostConfigUpdate(updates) {
const config = await getPostConfig();
const merged = deepMerge(config, updates);
// A partial patch (one field of a pair) is judged against the merged record
diff --git a/server/services/meatspacePost.test.js b/server/services/meatspacePost.test.js
index fd37ad4c59..6a0665661b 100644
--- a/server/services/meatspacePost.test.js
+++ b/server/services/meatspacePost.test.js
@@ -1250,6 +1250,51 @@ describe('updatePostConfig — postConfigEvents', () => {
expect(handler).toHaveBeenCalledTimes(1);
});
+
+ it('retains independent patches submitted together and returns the committed configuration', async () => {
+ let persisted = { quickDurationMin: 5, llmDrills: { providerId: null, model: null } };
+ readJSONFile.mockImplementation(async () => structuredClone(persisted));
+ atomicWrite.mockImplementation(async (_path, config) => { persisted = structuredClone(config); });
+
+ const [, saved] = await Promise.all([
+ updatePostConfig({ llmDrills: { providerId: 'example-provider', model: 'example-model' } }),
+ updatePostConfig({ quickDurationMin: 10 }),
+ ]);
+
+ const expected = {
+ quickDurationMin: 10,
+ llmDrills: { providerId: 'example-provider', model: 'example-model' },
+ };
+ expect(persisted).toMatchObject(expected);
+ expect(saved).toMatchObject(expected);
+ expect(await getPostConfig()).toMatchObject(expected);
+ });
+
+ it('does not publish a failed save and lets the next queued patch use the last committed configuration', async () => {
+ let persisted = { quickDurationMin: 5, adaptive: { enabled: false } };
+ readJSONFile.mockImplementation(async () => structuredClone(persisted));
+ atomicWrite.mockRejectedValueOnce(new Error('Example write failure'))
+ .mockImplementation(async (_path, config) => { persisted = structuredClone(config); });
+ const handler = vi.fn();
+ postConfigEvents.on('post-config:updated', handler);
+
+ try {
+ const results = await Promise.allSettled([
+ updatePostConfig({ quickDurationMin: 10 }),
+ updatePostConfig({ adaptive: { enabled: true } }),
+ ]);
+
+ expect(results[0]).toMatchObject({ status: 'rejected', reason: { message: 'Example write failure' } });
+ expect(results[1]).toMatchObject({ status: 'fulfilled' });
+ expect(persisted).toMatchObject({ quickDurationMin: 5, adaptive: { enabled: true } });
+ expect(handler).toHaveBeenCalledExactlyOnceWith({
+ config: persisted,
+ updates: { adaptive: { enabled: true } },
+ });
+ } finally {
+ postConfigEvents.off('post-config:updated', handler);
+ }
+ });
});
// =============================================================================