Skip to content

Commit e1b1d4b

Browse files
fix(config): stop plugin schemas rejecting the channel key core writes (#117992)
* fix(config): stop plugin schemas rejecting the channel key core writes A published channel plugin declares a closed config schema listing only the keys it knew about when that version shipped. Core's doctor migration writes heartbeatVisibility into channels.<id> and its accounts for every channel, so a plugin predating that key turns config core itself produced into a gateway that refuses to start with 78/CONFIG — and doctor --fix reports it without repairing it, leaving no in-product recovery. This took the live gateway down after the last deploy: @openclaw/signal 2026.7.2-beta.3 ships a 44-property closed schema with no heartbeatVisibility, while core reads all three precedence layers (src/infra/heartbeat-visibility.ts) and documents them (docs/gateway/heartbeat.md). Channel schemas built from core's shape may omit common fields on purpose — buildCommonChannelAccountShape takes an `omit` list — so widening every common key would silently accept config that can never take effect. The fix stays on the keys core writes: core wrote it, so core accepts it. Everything else is untouched, including unknown-key rejection, which is covered by a test and verified end to end. Applied once when the channel schema map is built, so the AJV cache key still maps to exactly one schema per channel, and unchanged schemas are returned by identity. Verified against the real failure: with the live plugin set loaded, the config that crashed the gateway now validates at both channel and per-account level, while an unknown key on the same channel is still rejected. * fix(config): normalize external channel schemas at metadata owner * fix(config): normalize core-owned channel visibility schemas * style(config): format channel metadata regression coverage * refactor(config): simplify channel schema normalization coverage --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 65a0db9 commit e1b1d4b

3 files changed

Lines changed: 470 additions & 1 deletion

File tree

‎src/config/channel-config-metadata.ts‎

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
* Converts plugin manifest metadata into deterministic config UI metadata for docs, validation, and runtime schema.
33
* When multiple plugin origins expose the same id/channel, the closest origin owns the surfaced schema.
44
*/
5+
import { isRecord } from "@openclaw/normalization-core/record-coerce";
56
import type { PluginManifestRegistry } from "../plugins/manifest-registry.js";
67
import type { PluginOrigin } from "../plugins/plugin-origin.types.js";
78
import type { ChannelUiMetadata, PluginUiMetadata } from "./schema.js";
9+
import { ChannelHeartbeatVisibilitySchema } from "./zod-schema.channels.js";
810

911
type ChannelSchemaMetadataWithOwnership = ChannelUiMetadata & {
1012
schemaPluginId?: string;
@@ -34,6 +36,92 @@ const PLUGIN_ORIGIN_RANK: Readonly<Record<PluginOrigin, number>> = {
3436
bundled: 3,
3537
};
3638

39+
const CHANNEL_HEARTBEAT_VISIBILITY_JSON_SCHEMA =
40+
ChannelHeartbeatVisibilitySchema.unwrap().toJSONSchema({ target: "draft-07" });
41+
42+
function normalizeCoreOwnedChannelSchema(schema: Record<string, unknown>): Record<string, unknown> {
43+
const normalized = structuredClone(schema);
44+
let changed = false;
45+
const normalizeNode = (
46+
node: Record<string, unknown>,
47+
accountMap = false,
48+
rootScope = true,
49+
): void => {
50+
let withinRootScope = rootScope && (node === normalized || typeof node.$id !== "string");
51+
if (typeof node.$ref === "string") {
52+
const match = withinRootScope
53+
? /^#\/(\$defs|definitions)\/([A-Za-z0-9_.-]+)$/.exec(node.$ref)
54+
: null;
55+
const definitions = match?.[1] ? normalized[match[1]] : undefined;
56+
const target = isRecord(definitions) && match?.[2] ? definitions[match[2]] : undefined;
57+
if (
58+
!isRecord(target) ||
59+
Object.keys(node).some(
60+
(key) => !["$ref", "$defs", "definitions", "$id", "$schema"].includes(key),
61+
) ||
62+
["$id", "$anchor", "$dynamicAnchor", "$recursiveAnchor", "$schema", "$ref"].some((key) =>
63+
Object.hasOwn(target, key),
64+
)
65+
) {
66+
return;
67+
}
68+
// Inline only this owner; changing shared definitions would affect unrelated consumers.
69+
const owner = { ...node };
70+
Object.assign(node, structuredClone(target), owner);
71+
delete node.$ref;
72+
changed = true;
73+
withinRootScope = node === normalized;
74+
}
75+
76+
for (const key of ["allOf", "anyOf", "oneOf"] as const) {
77+
const variants = node[key];
78+
for (const variant of Array.isArray(variants) ? variants : []) {
79+
if (isRecord(variant)) {
80+
normalizeNode(variant, accountMap, withinRootScope);
81+
}
82+
}
83+
}
84+
85+
if (accountMap) {
86+
if (node.additionalProperties === true) {
87+
node.additionalProperties = {};
88+
changed = true;
89+
}
90+
const entries = [
91+
node.additionalProperties,
92+
...Object.values(isRecord(node.patternProperties) ? node.patternProperties : {}),
93+
];
94+
for (const entry of entries) {
95+
if (isRecord(entry)) {
96+
normalizeNode(entry, false, withinRootScope);
97+
}
98+
}
99+
return;
100+
}
101+
102+
const properties = isRecord(node.properties) ? node.properties : {};
103+
if (
104+
JSON.stringify(properties.heartbeatVisibility) !==
105+
JSON.stringify(CHANNEL_HEARTBEAT_VISIBILITY_JSON_SCHEMA)
106+
) {
107+
node.properties = {
108+
...properties,
109+
heartbeatVisibility: CHANNEL_HEARTBEAT_VISIBILITY_JSON_SCHEMA,
110+
};
111+
changed = true;
112+
}
113+
114+
// Account maps are containers; only each account entry owns heartbeat visibility.
115+
const accounts = properties.accounts;
116+
if (isRecord(accounts)) {
117+
normalizeNode(accounts, true, withinRootScope);
118+
}
119+
};
120+
121+
normalizeNode(normalized);
122+
return changed ? normalized : schema;
123+
}
124+
37125
/** Collects plugin config UI metadata with deterministic origin precedence and output ordering. */
38126
export function collectPluginSchemaMetadata(registry: PluginManifestRegistry): PluginUiMetadata[] {
39127
const deduped = new Map<
@@ -109,7 +197,11 @@ export function collectChannelSchemaMetadataWithOwnership(
109197
id: channelId,
110198
label: channelConfig.label ?? rootLabel ?? current?.label,
111199
description: channelConfig.description ?? rootDescription ?? current?.description,
112-
configSchema: channelConfig.schema,
200+
// Installed plugin schemas can lag core; bundled schemas share its release and identity.
201+
configSchema:
202+
record.origin === "bundled" || channelConfig.schema === undefined
203+
? channelConfig.schema
204+
: normalizeCoreOwnedChannelSchema(channelConfig.schema),
113205
configUiHints: channelConfig.uiHints as ChannelUiMetadata["configUiHints"],
114206
schemaPluginId: channelConfig.schema === undefined ? undefined : record.id,
115207
schemaPluginOrigin: channelConfig.schema === undefined ? undefined : record.origin,

‎src/config/runtime-schema.test.ts‎

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,128 @@ describe("loadGatewayRuntimeConfigSchema", () => {
303303
expect(channelProps).toHaveProperty("matrix");
304304
});
305305

306+
it("projects strict heartbeat visibility for external channels and their accounts", () => {
307+
mockLoadPluginManifestRegistry.mockReturnValue({
308+
diagnostics: [],
309+
plugins: [
310+
{
311+
id: "external-chat",
312+
origin: "workspace",
313+
channels: ["external-chat"],
314+
channelConfigs: {
315+
"external-chat": {
316+
schema: {
317+
type: "object",
318+
properties: {
319+
endpoint: { type: "string" },
320+
accounts: {
321+
type: "object",
322+
additionalProperties: {
323+
type: "object",
324+
properties: { endpoint: { type: "string" } },
325+
additionalProperties: false,
326+
},
327+
},
328+
},
329+
additionalProperties: false,
330+
},
331+
},
332+
},
333+
},
334+
],
335+
});
336+
337+
const result = loadGatewayRuntimeConfigSchema();
338+
const schema = result.schema as { properties?: Record<string, unknown> };
339+
const channels = schema.properties?.channels as { properties?: Record<string, unknown> };
340+
const heartbeatVisibility = {
341+
type: "object",
342+
properties: {
343+
showOk: { type: "boolean" },
344+
showAlerts: { type: "boolean" },
345+
useIndicator: { type: "boolean" },
346+
},
347+
additionalProperties: false,
348+
};
349+
350+
expect(channels.properties?.["external-chat"]).toMatchObject({
351+
additionalProperties: false,
352+
properties: {
353+
heartbeatVisibility,
354+
accounts: {
355+
additionalProperties: {
356+
additionalProperties: false,
357+
properties: { heartbeatVisibility },
358+
},
359+
},
360+
},
361+
});
362+
});
363+
364+
it("projects canonical heartbeats into composed schemas and referenced open accounts", () => {
365+
mockLoadPluginManifestRegistry.mockReturnValue({
366+
diagnostics: [],
367+
plugins: [
368+
{
369+
id: "external-chat",
370+
origin: "workspace",
371+
channels: ["external-chat"],
372+
channelConfigs: {
373+
"external-chat": {
374+
schema: {
375+
$defs: { Account: {} },
376+
anyOf: [
377+
{ type: "object", additionalProperties: true },
378+
{
379+
type: "object",
380+
properties: {
381+
accounts: {
382+
type: "object",
383+
additionalProperties: { $ref: "#/$defs/Account" },
384+
},
385+
},
386+
additionalProperties: false,
387+
},
388+
],
389+
},
390+
},
391+
},
392+
},
393+
],
394+
});
395+
396+
const result = loadGatewayRuntimeConfigSchema();
397+
const schema = result.schema as { properties?: Record<string, unknown> };
398+
const channels = schema.properties?.channels as { properties?: Record<string, unknown> };
399+
const heartbeatVisibility = {
400+
type: "object",
401+
additionalProperties: false,
402+
properties: {
403+
showOk: { type: "boolean" },
404+
showAlerts: { type: "boolean" },
405+
useIndicator: { type: "boolean" },
406+
},
407+
};
408+
409+
const projected = channels.properties?.["external-chat"] as Record<string, unknown>;
410+
expect(projected).toMatchObject({
411+
properties: { heartbeatVisibility },
412+
anyOf: [
413+
{ additionalProperties: true, properties: { heartbeatVisibility } },
414+
{
415+
additionalProperties: false,
416+
properties: {
417+
heartbeatVisibility,
418+
accounts: {
419+
additionalProperties: { properties: { heartbeatVisibility } },
420+
},
421+
},
422+
},
423+
],
424+
});
425+
expect(projected.$defs).toEqual({ Account: {} });
426+
});
427+
306428
it("reuses the current gateway plugin metadata snapshot for config schema requests", () => {
307429
mockGetCurrentPluginMetadataSnapshot.mockReturnValueOnce({
308430
manifestRegistry: {

0 commit comments

Comments
 (0)