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-907]): address codex+claude review — preserve .strict() and strip nested partial defaults

Adam Eivy4ece823b54fd
diff --git a/server/lib/meatspaceValidation.js b/server/lib/meatspaceValidation.js
index bf93e8769f..db6871492a 100644
--- a/server/lib/meatspaceValidation.js
+++ b/server/lib/meatspaceValidation.js
@@ -56,7 +56,13 @@ export const configSchema = z.object({
lifestyle: lifestyleSchema.optional()
});
-export const configUpdateSchema = configSchema.partial();
+// configSchema has no top-level defaults, but its nested `lifestyle` object is
+// field-merged by updateConfig() — so a config PATCH carrying `lifestyle` must
+// strip that object's inner defaults too, or the untouched lifestyle fields get
+// reset to their defaults on every save (top-level .partial() doesn't recurse).
+export const configUpdateSchema = configSchema.partial().extend({
+ lifestyle: partialWithoutDefaults(lifestyleSchema).optional(),
+});
export const lifestyleUpdateSchema = partialWithoutDefaults(lifestyleSchema);
// =============================================================================
diff --git a/server/lib/validation.js b/server/lib/validation.js
index 04b6eb7782..2c830b947c 100644
--- a/server/lib/validation.js
+++ b/server/lib/validation.js
@@ -97,7 +97,13 @@ export const agentSchema = z.object({
aiConfig: agentAiConfigSchema
});
-export const agentUpdateSchema = partialWithoutDefaults(agentSchema);
+// partialWithoutDefaults handles the top-level fields; the nested `personality`
+// object is also field-merged by updateAgent(), so it needs its own default-free
+// partial — otherwise a PATCH of one personality key (e.g. just `style`) injects
+// the other keys' defaults and clobbers the stored tone/topics/quirks/promptPrefix.
+export const agentUpdateSchema = partialWithoutDefaults(agentSchema).extend({
+ personality: partialWithoutDefaults(agentPersonalitySchema).optional(),
+});
// =============================================================================
// PLATFORM ACCOUNT SCHEMAS
diff --git a/server/lib/zodCompat.js b/server/lib/zodCompat.js
index 216f6c4e9d..b55e874adf 100644
--- a/server/lib/zodCompat.js
+++ b/server/lib/zodCompat.js
@@ -42,6 +42,17 @@ function stripDefault(schema) {
* first — so the parsed result contains only the keys the caller actually sent.
* Use for any PATCH/update schema derived from a base that carries field defaults.
*
+ * The base's strict-mode is preserved: a `.strict()` source produces a strict
+ * partial (unknown keys still rejected), matching what `objectSchema.partial()`
+ * did. Only the field-level defaults are unwound — field bounds/refinements,
+ * optional/nullable wrappers, and the object's unknown-key policy survive.
+ *
+ * Note this only strips *top-level* field defaults. A field that is itself a
+ * defaulted nested object still inflates its own inner defaults when present —
+ * if a PATCH route field-merges such a nested object onto stored state, apply
+ * `partialWithoutDefaults` to that nested field too (don't rely on the
+ * top-level partial to recurse).
+ *
* @param {import('zod').ZodObject} objectSchema
* @returns {import('zod').ZodObject} partial schema with defaults stripped
*/
@@ -50,5 +61,9 @@ export function partialWithoutDefaults(objectSchema) {
const stripped = Object.fromEntries(
Object.entries(shape).map(([key, field]) => [key, stripDefault(field)]),
);
- return z.object(stripped).partial();
+ const rebuilt = z.object(stripped).partial();
+ // z.object() rebuild defaults to stripping unknown keys; re-apply .strict()
+ // when the source schema rejected them, so the rebuild doesn't loosen the
+ // unknown-key contract.
+ return objectSchema.def?.catchall?.def?.type === 'never' ? rebuilt.strict() : rebuilt;
}
diff --git a/server/lib/zodCompat.test.js b/server/lib/zodCompat.test.js
index c67954bf9d..0a343e9224 100644
--- a/server/lib/zodCompat.test.js
+++ b/server/lib/zodCompat.test.js
@@ -46,4 +46,21 @@ describe('partialWithoutDefaults', () => {
// The helper does not:
expect(partialWithoutDefaults(base).parse({})).toEqual({});
});
+
+ it('preserves .strict() from the source schema (rejects unknown keys)', () => {
+ const strictBase = z.object({
+ enabled: z.boolean().default(false),
+ debounceMs: z.number().int().default(2500),
+ }).strict();
+ const schema = partialWithoutDefaults(strictBase);
+ expect(schema.safeParse({ enabled: true }).success).toBe(true);
+ expect(schema.safeParse({}).success).toBe(true);
+ // Unknown key still rejected, matching the strict source's contract.
+ expect(schema.safeParse({ enabled: true, bogus: 1 }).success).toBe(false);
+ });
+
+ it('stays loose when the source schema is loose (strips unknown keys)', () => {
+ const schema = partialWithoutDefaults(base); // base is not .strict()
+ expect(schema.parse({ debounceMs: 3000, bogus: 1 })).toEqual({ debounceMs: 3000 });
+ });
});