Skip to content

Commit e800ecf

Browse files
committed
refactor(gateway): one owner for the submitted-config validation pipeline
config.set and config.patch each hand-rolled the same normalize -> raw-validate -> plugin-validate -> respond-invalid pipeline (including the duplicated errorShape/details blocks the round-5 error-text fix touched in both places). validateSubmittedConfigOrRespond now owns it; both handlers consume { validationCandidate, config }. Behavior-neutral: same validators in the same order, same error shapes (pinned by the existing 26 config tests including the round-5 regressions). Net -11 production LOC and one place to evolve validation error content.
1 parent f870d93 commit e800ecf

1 file changed

Lines changed: 56 additions & 67 deletions

File tree

‎src/gateway/server-methods/config.ts‎

Lines changed: 56 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -516,42 +516,18 @@ function parseValidateConfigFromRawOrRespond(
516516
createMergePatch(snapshot.config, restored.result),
517517
)
518518
: restored.result;
519-
const validationCandidate = normalizeSubmittedConfigModelRefs(
520-
stripBundledProviderRuntimeDefaults({
521-
candidate: projectedValidationCandidate,
522-
sourceConfig: snapshot.sourceConfig,
523-
}) as OpenClawConfig,
519+
const validatedSubmission = validateSubmittedConfigOrRespond({
520+
candidate: projectedValidationCandidate,
521+
sourceConfig: snapshot.sourceConfig,
524522
modelIdNormalizationPolicies,
525-
);
526-
const sourceValidated = validateConfigObjectRawWithPlugins(validationCandidate);
527-
if (!sourceValidated.ok) {
528-
respond(
529-
false,
530-
undefined,
531-
errorShape(
532-
ErrorCodes.INVALID_REQUEST,
533-
summarizeConfigValidationIssues(sourceValidated.issues),
534-
{
535-
details: { issues: sourceValidated.issues },
536-
},
537-
),
538-
);
539-
return null;
540-
}
541-
const validated = validateConfigObjectWithPlugins(validationCandidate);
542-
if (!validated.ok) {
543-
respond(
544-
false,
545-
undefined,
546-
errorShape(ErrorCodes.INVALID_REQUEST, summarizeConfigValidationIssues(validated.issues), {
547-
details: { issues: validated.issues },
548-
}),
549-
);
523+
respond,
524+
});
525+
if (!validatedSubmission) {
550526
return null;
551527
}
552528
return {
553-
config: validated.config,
554-
writeConfig: validationCandidate as OpenClawConfig,
529+
config: validatedSubmission.config,
530+
writeConfig: validatedSubmission.validationCandidate,
555531
schema,
556532
};
557533
}
@@ -595,6 +571,42 @@ function rejectDroppedAgentRosterEntries(params: {
595571
return true;
596572
}
597573

574+
/** Shared normalize -> raw-validate -> plugin-validate pipeline for submitted configs; responds on failure. */
575+
function validateSubmittedConfigOrRespond(params: {
576+
candidate: unknown;
577+
sourceConfig: OpenClawConfig | undefined;
578+
modelIdNormalizationPolicies: Parameters<typeof normalizeSubmittedConfigModelRefs>[1];
579+
respond: RespondFn;
580+
}): { validationCandidate: OpenClawConfig; config: OpenClawConfig } | null {
581+
const validationCandidate = normalizeSubmittedConfigModelRefs(
582+
stripBundledProviderRuntimeDefaults({
583+
candidate: params.candidate,
584+
sourceConfig: params.sourceConfig,
585+
}) as OpenClawConfig,
586+
params.modelIdNormalizationPolicies,
587+
);
588+
const respondInvalid = (issues: ReadonlyArray<ConfigValidationIssue>) => {
589+
params.respond(
590+
false,
591+
undefined,
592+
errorShape(ErrorCodes.INVALID_REQUEST, summarizeConfigValidationIssues(issues), {
593+
details: { issues },
594+
}),
595+
);
596+
};
597+
const sourceValidated = validateConfigObjectRawWithPlugins(validationCandidate);
598+
if (!sourceValidated.ok) {
599+
respondInvalid(sourceValidated.issues);
600+
return null;
601+
}
602+
const validated = validateConfigObjectWithPlugins(validationCandidate);
603+
if (!validated.ok) {
604+
respondInvalid(validated.issues);
605+
return null;
606+
}
607+
return { validationCandidate: validationCandidate as OpenClawConfig, config: validated.config };
608+
}
609+
598610
function summarizeConfigValidationIssues(issues: ReadonlyArray<ConfigValidationIssue>): string {
599611
const trimmed = issues.slice(0, MAX_CONFIG_ISSUES_IN_ERROR_MESSAGE);
600612
const lines = normalizeStringEntries(
@@ -1080,48 +1092,25 @@ export const configHandlers: GatewayRequestHandlers = {
10801092
});
10811093
return;
10821094
}
1083-
const validationCandidate = normalizeSubmittedConfigModelRefs(
1084-
stripBundledProviderRuntimeDefaults({
1085-
candidate: restoredMerge.result,
1086-
sourceConfig: snapshot.sourceConfig,
1087-
}) as OpenClawConfig,
1095+
const validatedSubmission = validateSubmittedConfigOrRespond({
1096+
candidate: restoredMerge.result,
1097+
sourceConfig: snapshot.sourceConfig,
10881098
modelIdNormalizationPolicies,
1089-
);
1090-
const sourceValidated = validateConfigObjectRawWithPlugins(validationCandidate);
1091-
if (!sourceValidated.ok) {
1092-
respond(
1093-
false,
1094-
undefined,
1095-
errorShape(
1096-
ErrorCodes.INVALID_REQUEST,
1097-
summarizeConfigValidationIssues(sourceValidated.issues),
1098-
{
1099-
details: { issues: sourceValidated.issues },
1100-
},
1101-
),
1102-
);
1103-
return;
1104-
}
1105-
const writeConfig = validationCandidate as OpenClawConfig;
1106-
const validated = validateConfigObjectWithPlugins(validationCandidate);
1107-
if (!validated.ok) {
1108-
respond(
1109-
false,
1110-
undefined,
1111-
errorShape(ErrorCodes.INVALID_REQUEST, summarizeConfigValidationIssues(validated.issues), {
1112-
details: { issues: validated.issues },
1113-
}),
1114-
);
1099+
respond,
1100+
});
1101+
if (!validatedSubmission) {
11151102
return;
11161103
}
1104+
const writeConfig = validatedSubmission.validationCandidate;
1105+
const validatedConfig = validatedSubmission.config;
11171106
const preparedSecretsSnapshot = await ensureResolvableSecretRefsOrRespond({
1118-
config: validated.config,
1107+
config: validatedConfig,
11191108
respond,
11201109
});
11211110
if (!preparedSecretsSnapshot) {
11221111
return;
11231112
}
1124-
const changedPaths = diffConfigPaths(snapshot.config, validated.config);
1113+
const changedPaths = diffConfigPaths(snapshot.config, validatedConfig);
11251114

11261115
// No-op: if the validated config is identical to the current config,
11271116
// skip the file write and SIGUSR1 restart entirely. This avoids a full
@@ -1130,7 +1119,7 @@ export const configHandlers: GatewayRequestHandlers = {
11301119
if (changedPaths.length === 0) {
11311120
respondConfigPatchNoop({
11321121
snapshot,
1133-
config: validated.config,
1122+
config: validatedConfig,
11341123
uiHints: schemaPatch.uiHints,
11351124
actor,
11361125
context,
@@ -1147,7 +1136,7 @@ export const configHandlers: GatewayRequestHandlers = {
11471136
const disconnectSharedAuthClients = shouldDisconnectSharedAuthClientsForConfigWrite({
11481137
prevConfig: snapshot.config,
11491138
prevSourceConfig: snapshot.sourceConfig,
1150-
nextConfig: validated.config,
1139+
nextConfig: validatedConfig,
11511140
preparedSecretsSnapshot,
11521141
});
11531142
const writeResult = await commitGatewayConfigWriteOrRespond({

0 commit comments

Comments
 (0)