From 42427d506997405d311fa90986e088e68acef28a Mon Sep 17 00:00:00 2001 From: autodev Date: Fri, 9 Oct 2026 04:40:04 +0800 Subject: [PATCH 1/2] fix(plugin): derive known config keys from TypeBox schema (#2474) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `pruneUnknown` used DEFAULT_CONFIG as the "known keys" oracle, so every schema-declared `Type.Optional(...)` field without a default value was flagged on every boot: config.warning message="unknown config key 'llm.reasoning' (…)" config.warning message="unknown config key 'skillEvolver.reasoning' (…)" config.warning message="unknown config key 'l3Llm.reasoning' (…)" `reasoning` is declared in LlmSchema / SkillEvolverSchema and the value was applied end-to-end — only the warning was wrong. #2248 patched the same class of defect for `llm.maxTokens` / `llm.headers` by seeding defaults, but every future optional field would reintroduce the bug. Walk the TypeBox schema instead: use the `properties` map of `type: "object"` nodes to decide what is known, and treat `patternProperties` (TypeBox `Type.Record`) as a free-form map so `llm.headers` / `logging.channels` continue to accept arbitrary child keys. `FREE_FORM_CONFIG_PATHS` stays as an explicit allowlist for backward compatibility. The `unknown config key …` warning text, the pass-through semantics, and the downstream pipeline (deepMerge/Value.Default/Value.Errors) are all preserved. Tests: full plugin unit suite 1599 pass + 1 skipped (0 fail), the new `schema-driven-unknown-keys.test.ts` adds 9 cases covering reasoning (no warning), typos (still warn), free-form Record maps, and nested unknowns inside a known Optional block. Integration suite 6/6 green. `tsc -p tsconfig.json --noEmit` clean. Fixes #2474 --- apps/memos-local-plugin/core/config/index.ts | 80 +++++++++--- .../config/schema-driven-unknown-keys.test.ts | 121 ++++++++++++++++++ 2 files changed, 186 insertions(+), 15 deletions(-) create mode 100644 apps/memos-local-plugin/tests/unit/config/schema-driven-unknown-keys.test.ts diff --git a/apps/memos-local-plugin/core/config/index.ts b/apps/memos-local-plugin/core/config/index.ts index ae3991017..c8ff52182 100644 --- a/apps/memos-local-plugin/core/config/index.ts +++ b/apps/memos-local-plugin/core/config/index.ts @@ -87,7 +87,7 @@ export async function loadConfig(home: ResolvedHome, agent?: string): Promise>(a: T, b: Record { if (!isPlainObject(raw)) return {}; + const known = schemaObjectProperties(schemaNode); + const freeFormHere = schemaAcceptsAnyKey(schemaNode); const out: Record = {}; for (const [k, v] of Object.entries(raw)) { const path = prefix ? `${prefix}.${k}` : k; - if (isPlainObject(defaults) && !(k in (defaults as Record))) { + // Free-form maps keep every user-defined child key verbatim: TypeBox + // `Type.Record(...)` surfaces as `patternProperties` (detected via + // `freeFormHere`), and the explicit allowlist handles any legacy + // sections we want to treat the same way. + if (freeFormHere || FREE_FORM_CONFIG_PATHS.includes(path)) { + out[k] = v; + continue; + } + if (known && !(k in known)) { warnings?.push(`unknown config key '${path}' (kept as-is for forward compatibility)`); out[k] = v; continue; } - if (isPlainObject(v) && isPlainObject((defaults as Record)[k])) { - if (FREE_FORM_CONFIG_PATHS.includes(path)) { - // Explicitly declared free-form maps keep user-defined child keys. - // Other empty default objects remain structured config sections and - // continue to report unknown nested keys. - out[k] = v; - continue; - } - out[k] = pruneUnknown(v, (defaults as Record)[k], path, warnings); + const childSchema = known ? known[k] : undefined; + if (isPlainObject(v) && isObjectSchema(childSchema)) { + out[k] = pruneUnknown(v, childSchema, path, warnings); } else { out[k] = v; } @@ -343,6 +365,34 @@ function pruneUnknown( return out; } +/** TypeBox `Type.Object(...)` → the `properties` map; null for anything else. */ +function schemaObjectProperties(node: unknown): Record | null { + if (!isPlainObject(node)) return null; + if ((node as { type?: unknown }).type !== "object") return null; + const props = (node as { properties?: unknown }).properties; + return isPlainObject(props) ? (props as Record) : null; +} + +/** + * TypeBox `Type.Record(...)` surfaces as a `type: "object"` schema with + * `patternProperties`. We also treat `additionalProperties: true` the + * same way — nothing in the current schema uses it, but it keeps us + * aligned with JSON Schema semantics for future flexibility. + */ +function schemaAcceptsAnyKey(node: unknown): boolean { + if (!isPlainObject(node)) return false; + if ((node as { type?: unknown }).type !== "object") return false; + if ("patternProperties" in node) return true; + const additional = (node as { additionalProperties?: unknown }).additionalProperties; + return additional === true; +} + +/** Any `type: "object"` schema node (used to decide whether to recurse). */ +function isObjectSchema(node: unknown): boolean { + if (!isPlainObject(node)) return false; + return (node as { type?: unknown }).type === "object"; +} + function isPlainObject(v: unknown): v is Record { return typeof v === "object" && v !== null && !Array.isArray(v); } diff --git a/apps/memos-local-plugin/tests/unit/config/schema-driven-unknown-keys.test.ts b/apps/memos-local-plugin/tests/unit/config/schema-driven-unknown-keys.test.ts new file mode 100644 index 000000000..562051220 --- /dev/null +++ b/apps/memos-local-plugin/tests/unit/config/schema-driven-unknown-keys.test.ts @@ -0,0 +1,121 @@ +/** + * Regression tests for issue #2474. + * + * The `pruneUnknown` validator used to key its "known fields" set on + * `DEFAULT_CONFIG`, so any TypeBox `Type.Optional(...)` field without a + * default (e.g. `llm.reasoning` → `ReasoningSchema`) was reported as an + * unknown key on every boot: + * + * config.warning message="unknown config key 'llm.reasoning' (kept as-is …)" + * config.warning message="unknown config key 'skillEvolver.reasoning' (…)" + * config.warning message="unknown config key 'l3Llm.reasoning' (…)" + * + * Fix: derive the known-key set from the TypeBox schema (`ConfigSchema`), + * not from the defaults tree. This covers every current and future + * optional field in one place. + * + * Precedent: #2248 added `llm.maxTokens` / `llm.headers` to `DEFAULT_CONFIG` + * to cure the same class of warning. The schema-driven rewrite subsumes + * that approach. + */ + +import { describe, expect, it } from "vitest"; + +import { resolveConfig } from "../../../core/config/index.js"; + +const UNKNOWN_WARN_RE = /unknown config key/; + +describe("resolveConfig schema-driven unknown-key detection (issue #2474)", () => { + it("does not warn for llm.reasoning (an optional field declared in the schema)", () => { + const warnings: string[] = []; + const cfg = resolveConfig({ llm: { reasoning: { enabled: false } } }, warnings); + expect(warnings.filter((w) => UNKNOWN_WARN_RE.test(w))).toEqual([]); + expect(cfg.llm.reasoning).toEqual({ enabled: false }); + }); + + it("does not warn for skillEvolver.reasoning", () => { + const warnings: string[] = []; + const cfg = resolveConfig( + { skillEvolver: { reasoning: { effort: "medium" } } }, + warnings, + ); + expect(warnings.filter((w) => UNKNOWN_WARN_RE.test(w))).toEqual([]); + expect(cfg.skillEvolver.reasoning).toEqual({ effort: "medium" }); + }); + + it("does not warn for l3Llm.reasoning", () => { + const warnings: string[] = []; + const cfg = resolveConfig( + { l3Llm: { reasoning: { maxTokens: 2048 } } }, + warnings, + ); + expect(warnings.filter((w) => UNKNOWN_WARN_RE.test(w))).toEqual([]); + expect(cfg.l3Llm.reasoning).toEqual({ maxTokens: 2048 }); + }); + + it("still warns on real typos inside a known schema branch", () => { + const warnings: string[] = []; + resolveConfig({ llm: { reasonning: {} } }, warnings); + expect(warnings).toContain( + "unknown config key 'llm.reasonning' (kept as-is for forward compatibility)", + ); + }); + + it("still warns on top-level unknown branches", () => { + const warnings: string[] = []; + resolveConfig({ entityExtractor: { reasoning: { enabled: true } } }, warnings); + expect( + warnings.some((w) => + /unknown config key 'entityExtractor'/.test(w), + ), + ).toBe(true); + }); + + it("keeps nested free-form map child keys without warning (llm.headers via schema Record)", () => { + const warnings: string[] = []; + const cfg = resolveConfig( + { llm: { headers: { "X-A": "1", "X-B": "2" } } }, + warnings, + ); + expect(warnings.filter((w) => UNKNOWN_WARN_RE.test(w))).toEqual([]); + expect(cfg.llm.headers).toEqual({ "X-A": "1", "X-B": "2" }); + }); + + it("keeps logging.channels user-defined child keys without warning (Record)", () => { + const warnings: string[] = []; + const cfg = resolveConfig( + { logging: { channels: { "core.l2.cross-task": "debug" } } }, + warnings, + ); + expect(warnings.filter((w) => UNKNOWN_WARN_RE.test(w))).toEqual([]); + expect(cfg.logging.channels).toEqual({ "core.l2.cross-task": "debug" }); + }); + + it("resolved config retains the reasoning block exactly as specified", () => { + const cfg = resolveConfig({ + llm: { + reasoning: { enabled: true, effort: "high", maxTokens: 4096 }, + }, + skillEvolver: { + reasoning: { enabled: false }, + }, + l3Llm: { + reasoning: { effort: "low" }, + }, + }); + expect(cfg.llm.reasoning).toEqual({ enabled: true, effort: "high", maxTokens: 4096 }); + expect(cfg.skillEvolver.reasoning).toEqual({ enabled: false }); + expect(cfg.l3Llm.reasoning).toEqual({ effort: "low" }); + }); + + it("still warns on unknown nested keys inside the reasoning block itself", () => { + const warnings: string[] = []; + resolveConfig( + { llm: { reasoning: { unknownKnob: 1 } } }, + warnings, + ); + expect(warnings).toContain( + "unknown config key 'llm.reasoning.unknownKnob' (kept as-is for forward compatibility)", + ); + }); +}); From a94eb697c7a00ca8cbb307be2de153d1440183a7 Mon Sep 17 00:00:00 2001 From: autodev Date: Fri, 9 Oct 2026 04:52:05 +0800 Subject: [PATCH 2/2] fix(plugin): make pruneUnknown fallback explicit + reuse TypeBox KindGuard MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Addresses the two OCR findings on #2475: 1. `pruneUnknown` used to silently fall through when `known` was null — e.g. if a future schema refactor introduced a `Type.Union`, `Type.Intersect`, or `Type.Any` at a recursed-into node, every child key would be passed through verbatim with no warning. The guard `if (known && !(k in known))` short-circuited on falsy `known` and nothing emitted a diagnostic. Now the `known === null && !freeFormHere` case takes an early return with an explicit warning so the bypass is visible rather than silent. `Type.Record` nodes still skip the check via `freeFormHere`, so the current schema's behaviour is unchanged. 2. The three helpers `schemaObjectProperties`, `schemaAcceptsAnyKey`, `isObjectSchema` all repeated the same `(node as { type?: unknown }).type === "object"` inline cast. Replaced with TypeBox's own `KindGuard.IsObject` / `KindGuard.IsRecord` so the introspection stays aligned with TypeBox's internal representation. Tests: tests/unit/config/ (109 tests) still pass; `npm run lint` is clean. --- apps/memos-local-plugin/core/config/index.ts | 65 +++++++++++++++----- 1 file changed, 50 insertions(+), 15 deletions(-) diff --git a/apps/memos-local-plugin/core/config/index.ts b/apps/memos-local-plugin/core/config/index.ts index c8ff52182..76c129d1b 100644 --- a/apps/memos-local-plugin/core/config/index.ts +++ b/apps/memos-local-plugin/core/config/index.ts @@ -11,7 +11,7 @@ import { promises as fs } from "node:fs"; -import { Type } from "@sinclair/typebox"; +import { KindGuard, Type } from "@sinclair/typebox"; import { Value, type ValueError } from "@sinclair/typebox/value"; import { MemosError } from "../../agent-contract/errors.js"; @@ -340,6 +340,26 @@ function pruneUnknown( const known = schemaObjectProperties(schemaNode); const freeFormHere = schemaAcceptsAnyKey(schemaNode); const out: Record = {}; + // Fallback: the schema node at this level is neither a plain `Type.Object` + // (so `known` is null) nor a free-form map (so `freeFormHere` is false). + // We cannot validate child keys here. Pass them through verbatim and emit + // a single diagnostic so the bypass is visible — otherwise a future schema + // refactor that puts a `Type.Union`, `Type.Intersect`, or `Type.Any` at a + // node `pruneUnknown` recurses into would silently disable validation for + // the whole subtree. TypeBox's `Value.Errors` still catches structural + // problems at the end of `resolveConfig`; this warning just makes the + // bypass easy to discover during debugging. The current schema is entirely + // composed of `Type.Object` and `Type.Record` so this branch is dormant + // today, but it stops being a latent trap tomorrow. + if (known === null && !freeFormHere) { + warnings?.push( + `config: cannot validate child keys of '${prefix || ""}' ` + + `(schema node is neither Type.Object nor a free-form map) — ` + + `passing all keys through unchanged`, + ); + for (const [k, v] of Object.entries(raw)) out[k] = v; + return out; + } for (const [k, v] of Object.entries(raw)) { const path = prefix ? `${prefix}.${k}` : k; // Free-form maps keep every user-defined child key verbatim: TypeBox @@ -350,12 +370,16 @@ function pruneUnknown( out[k] = v; continue; } - if (known && !(k in known)) { + // `known` is guaranteed non-null here: the only way `freeFormHere` is + // false while `known` is null was handled by the early-return above. + // TypeScript can't follow that cross-loop narrowing, so pin it locally. + const knownKeys = known as Record; + if (!(k in knownKeys)) { warnings?.push(`unknown config key '${path}' (kept as-is for forward compatibility)`); out[k] = v; continue; } - const childSchema = known ? known[k] : undefined; + const childSchema = knownKeys[k]; if (isPlainObject(v) && isObjectSchema(childSchema)) { out[k] = pruneUnknown(v, childSchema, path, warnings); } else { @@ -365,32 +389,43 @@ function pruneUnknown( return out; } -/** TypeBox `Type.Object(...)` → the `properties` map; null for anything else. */ +/** + * TypeBox `Type.Object(...)` → the `properties` map; null for anything else + * (`Type.Record`, `Type.Union`, scalar schemas, non-schema values, …). We go + * through `KindGuard.IsObject` rather than hand-rolling a `type === "object"` + * check so the introspection stays aligned with TypeBox's own notion of what + * is a `TObject` — if TypeBox ever changes its internal representation we + * only have to update the import, not three helpers. + */ function schemaObjectProperties(node: unknown): Record | null { - if (!isPlainObject(node)) return null; - if ((node as { type?: unknown }).type !== "object") return null; + if (!KindGuard.IsObject(node)) return null; const props = (node as { properties?: unknown }).properties; return isPlainObject(props) ? (props as Record) : null; } /** - * TypeBox `Type.Record(...)` surfaces as a `type: "object"` schema with - * `patternProperties`. We also treat `additionalProperties: true` the - * same way — nothing in the current schema uses it, but it keeps us - * aligned with JSON Schema semantics for future flexibility. + * TypeBox `Type.Record(...)` surfaces as a schema with `patternProperties`; + * `KindGuard.IsRecord` detects it directly. We also treat + * `additionalProperties: true` the same way — nothing in the current schema + * uses it, but it keeps us aligned with JSON Schema semantics for future + * flexibility. */ function schemaAcceptsAnyKey(node: unknown): boolean { + if (KindGuard.IsRecord(node)) return true; if (!isPlainObject(node)) return false; - if ((node as { type?: unknown }).type !== "object") return false; - if ("patternProperties" in node) return true; const additional = (node as { additionalProperties?: unknown }).additionalProperties; return additional === true; } -/** Any `type: "object"` schema node (used to decide whether to recurse). */ +/** + * A schema node that `pruneUnknown` should recurse into — either a + * `Type.Object` (child validation drives the known-key check) or a + * `Type.Record` (free-form map handled by `schemaAcceptsAnyKey`). Other + * schema shapes (unions, intersections, scalars) are left to TypeBox's + * `Value.Errors` to validate. + */ function isObjectSchema(node: unknown): boolean { - if (!isPlainObject(node)) return false; - return (node as { type?: unknown }).type === "object"; + return KindGuard.IsObject(node) || KindGuard.IsRecord(node); } function isPlainObject(v: unknown): v is Record {