Skip to content

Commit 0d32f85

Browse files
fix(config): model policy refs padded around the provider/model separator fail config validation (#114116)
* fix(config): accept model policy refs padded around the provider/model separator Model policy ref validation split refs on / and rejected any component containing whitespace, so " openai / gpt-5.5 " failed config validation even though parseProviderModelRef in @openclaw/model-catalog-core trims the provider and the model remainder and the runtime resolves the ref fine. Allow-list validation is newer than that resolution path, so configs that loaded before started failing at load. Validate the values parseModelCatalogRef returns — the resolved provider and the whole model remainder — keeping empty-path-segment rejection, so padding around the separator is accepted while whitespace inside the resolved model id stays rejected. Wildcard prefixes trim per segment, since their canonical key is matched on segment boundaries; a padded nested prefix now keeps its namespace instead of failing validation. * refactor(config): simplify model policy normalization * fix(config): normalize padded policy wildcards --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 00ccfce commit 0d32f85

3 files changed

Lines changed: 91 additions & 7 deletions

File tree

‎src/agents/model-visibility-policy.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,27 @@ describe("explicit model visibility policy", () => {
213213
]);
214214
});
215215

216+
it("keeps nested prefix wildcards scoped when segments carry boundary whitespace", () => {
217+
const policy = createPolicy({
218+
agents: {
219+
defaults: {
220+
modelPolicy: { allow: [" clawrouter / anthropic / * ", " openai / gpt-5.5 "] },
221+
},
222+
},
223+
});
224+
225+
// The padded nested wildcard must keep its namespace rather than widening to
226+
// every clawrouter model.
227+
expect(policy.allowsKey("clawrouter/anthropic/claude-haiku-4-5")).toBe(true);
228+
expect(policy.allowsKey("clawrouter/google/gemini-3.5-flash")).toBe(false);
229+
expect(policy.allowsKey("openai/gpt-5.5")).toBe(true);
230+
expect(policy.allowsKey("openai/gpt-5.6-sol")).toBe(false);
231+
expect(policy.allowedCatalog.map((entry) => `${entry.provider}/${entry.id}`)).toEqual([
232+
"clawrouter/anthropic/claude-haiku-4-5",
233+
"openai/gpt-5.5",
234+
]);
235+
});
236+
216237
it("keeps top-level provider wildcard behavior for nested model ids", () => {
217238
const policy = createPolicy({
218239
agents: {

‎src/config/config.model-ref-validation.test.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,64 @@ describe("config model reference validation", () => {
174174
expect(res.config.models?.providers?.myproxy?.models?.[0]?.id).toBe("vendor/modern-model");
175175
}
176176
});
177+
it.each([
178+
[
179+
"default policy",
180+
{
181+
agents: {
182+
defaults: {
183+
modelPolicy: {
184+
allow: [
185+
" openai / gpt-5.5 ",
186+
"clawrouter/ anthropic/claude-haiku-4-5",
187+
" openai / * ",
188+
" clawrouter / anthropic / * ",
189+
],
190+
},
191+
},
192+
},
193+
},
194+
],
195+
[
196+
"per-agent policy",
197+
{
198+
agents: {
199+
list: [
200+
{
201+
id: "worker",
202+
modelPolicy: {
203+
allow: [" openai / gpt-5.5 ", " openai / * ", " openai / ns / * "],
204+
},
205+
},
206+
],
207+
},
208+
},
209+
],
210+
])("accepts separator padding in the %s", (_label, config) => {
211+
const res = validateConfigObjectWithPlugins(config, { pluginValidation: "skip" });
212+
213+
expect(res.ok).toBe(true);
214+
});
215+
216+
it.each(["clawrouter/anthropic /claude-haiku-4-5", "openai/gpt 5.5", "openai//gpt-5.5"])(
217+
"still rejects malformed model policy ref %j",
218+
(ref) => {
219+
const res = validateConfigObjectWithPlugins(
220+
{
221+
agents: {
222+
defaults: {
223+
modelPolicy: { allow: [ref] },
224+
},
225+
},
226+
},
227+
{ pluginValidation: "skip" },
228+
);
229+
230+
expect(res.ok).toBe(false);
231+
if (!res.ok) {
232+
expect(res.issues[0]?.path).toBe("agents.defaults.modelPolicy.allow.0");
233+
expect(res.issues[0]?.message).toContain("invalid model policy ref");
234+
}
235+
},
236+
);
177237
});

‎src/config/model-policy-ref.ts‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@ type ModelPolicyWildcardRef = {
3939
/** Parse and canonicalize a segment-boundary model-policy prefix wildcard. */
4040
export function parseModelPolicyWildcardRef(raw: string): ModelPolicyWildcardRef | null {
4141
const trimmed = raw.trim();
42-
if (!trimmed.endsWith("/*")) {
43-
return null;
44-
}
45-
const segments = trimmed.split("/");
42+
// Wildcard keys match on segment boundaries, so normalize boundary padding
43+
// before building the canonical key used by policy matching.
44+
const segments = trimmed.split("/").map((segment) => segment.trim());
4645
if (
4746
segments.at(-1) !== "*" ||
4847
!hasValidSegments(segments.slice(0, -1), {
@@ -63,9 +62,13 @@ export function parseModelPolicyWildcardRef(raw: string): ModelPolicyWildcardRef
6362

6463
/** True for a syntactically valid exact provider/model policy reference. */
6564
export function isValidExactModelPolicyRef(raw: string): boolean {
66-
const trimmed = raw.trim();
67-
const parsed = parseModelCatalogRef(trimmed);
68-
return Boolean(parsed && hasValidSegments(trimmed.split("/"), { min: 2 }));
65+
const parsed = parseModelCatalogRef(raw);
66+
return Boolean(
67+
parsed &&
68+
hasValidSegments([parsed.provider, ...parsed.modelId.split("/")], {
69+
min: 2,
70+
}),
71+
);
6972
}
7073

7174
/** True for a supported bare selector whose target is resolved from config. */

0 commit comments

Comments
 (0)