Skip to content

Commit 907625c

Browse files
feat(anthropic): gate live model discovery on contract coverage
Live catalog discovery cloned a template for any newly discovered Claude id, so a future model generation would be selectable while request shaping treated it as pre-4.6 and 400d. Add an opt-in acceptUnknownModel gate to the shared live catalog seam and have the Anthropic plugin accept a discovered model only when Anthropic's advertised capabilities agree with the contracts we would apply. Manifest-published ids bypass the gate and keep their metadata. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent e279076 commit 907625c

8 files changed

Lines changed: 248 additions & 3 deletions

‎docs/docs_map.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7868,6 +7868,7 @@ Do not edit it by hand; run `pnpm docs:map:gen`.
78687868
- H2: Usage and cost tracking
78697869
- H2: Getting started
78707870
- H2: Claude sessions across computers
7871+
- H2: Live model discovery
78717872
- H2: Thinking defaults (Claude Opus 5, Sonnet 5, Mythos 5, Fable 5, 4.8, and 4.6)
78727873
- H2: Safety refusal fallback (Claude Opus 5 and Fable 5)
78737874
- H3: Why this exists

‎docs/providers/anthropic.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,20 @@ remain read-only even on a continuation-enabled node.
281281
See [Nodes: Claude sessions and transcripts](/nodes#claude-sessions-and-transcripts)
282282
for the node command and security boundary.
283283

284+
## Live model discovery
285+
286+
With an Anthropic API key configured, OpenClaw refreshes the Claude catalog from
287+
Anthropic's models endpoint, so newly published snapshots of supported model
288+
families appear without an OpenClaw release. Models the shipped catalog already
289+
describes always keep their published metadata and pricing.
290+
291+
A newly discovered model is only offered when Anthropic's advertised
292+
capabilities match the request shaping OpenClaw would apply to it. A brand-new
293+
model generation therefore stays hidden until OpenClaw adds support for it,
294+
rather than appearing in the picker and failing every request. Discovery is
295+
advisory: without an API key, or if the endpoint is unreachable, the shipped
296+
catalog is used unchanged.
297+
284298
## Thinking defaults (Claude Opus 5, Sonnet 5, Mythos 5, Fable 5, 4.8, and 4.6)
285299

286300
Bare family aliases are rolling: `opus` tracks the current supported Claude
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Anthropic tests cover the live-discovery contract gate.
2+
import { describe, expect, it } from "vitest";
3+
import { acceptsAnthropicLiveModelContract } from "./live-model-contract-gate.js";
4+
5+
/** Capability tree shaped like Anthropic's `/v1/models` response. */
6+
function capabilities(params: {
7+
adaptive: boolean;
8+
xhigh: boolean;
9+
max: boolean;
10+
}): Record<string, unknown> {
11+
return {
12+
capabilities: {
13+
image_input: { supported: true },
14+
thinking: {
15+
supported: true,
16+
types: {
17+
enabled: { supported: !params.adaptive },
18+
adaptive: { supported: params.adaptive },
19+
},
20+
},
21+
effort: {
22+
low: { supported: params.max },
23+
high: { supported: params.max },
24+
xhigh: { supported: params.xhigh },
25+
max: { supported: params.max },
26+
},
27+
},
28+
};
29+
}
30+
31+
const MODERN = { adaptive: true, xhigh: true, max: true };
32+
const LEGACY = { adaptive: false, xhigh: false, max: false };
33+
34+
describe("acceptsAnthropicLiveModelContract", () => {
35+
it("accepts current models whose advertised capabilities match our contracts", () => {
36+
// Adaptive + full effort range: what the contracts already shape correctly.
37+
for (const id of ["claude-opus-5", "claude-sonnet-5", "claude-fable-5", "claude-opus-4-8"]) {
38+
expect(acceptsAnthropicLiveModelContract({ id, record: capabilities(MODERN) }), id).toBe(
39+
true,
40+
);
41+
}
42+
});
43+
44+
it("accepts legacy models that correctly advertise no adaptive thinking", () => {
45+
// The gate must not confuse "old model we shape with budget_tokens" for
46+
// "model we do not understand" — both miss the modern predicates.
47+
for (const id of ["claude-haiku-4-5", "claude-sonnet-4-5", "claude-3-opus-20240229"]) {
48+
expect(acceptsAnthropicLiveModelContract({ id, record: capabilities(LEGACY) }), id).toBe(
49+
true,
50+
);
51+
}
52+
});
53+
54+
it("rejects an unreleased model generation our contracts do not recognize", () => {
55+
// The regression this gate exists to prevent: a future Claude family that
56+
// needs adaptive shaping but matches no version predicate, so it would be
57+
// sent manual budget_tokens plus sampling params and 400 on every request.
58+
for (const id of ["claude-opus-6", "claude-sonnet-6", "claude-zephyr-1"]) {
59+
expect(acceptsAnthropicLiveModelContract({ id, record: capabilities(MODERN) }), id).toBe(
60+
false,
61+
);
62+
}
63+
});
64+
65+
it("rejects a known model whose advertised capabilities drift from our contracts", () => {
66+
// If Anthropic changes a shipped model's capabilities, our shaping is stale
67+
// for it; hide it rather than keep sending the old request shape.
68+
expect(
69+
acceptsAnthropicLiveModelContract({
70+
id: "claude-opus-5",
71+
record: capabilities({ adaptive: false, xhigh: true, max: true }),
72+
}),
73+
).toBe(false);
74+
expect(
75+
acceptsAnthropicLiveModelContract({
76+
id: "claude-haiku-4-5",
77+
record: capabilities(MODERN),
78+
}),
79+
).toBe(false);
80+
});
81+
82+
it("fails closed when the capability tree is missing or unreadable", () => {
83+
for (const record of [
84+
{},
85+
{ capabilities: null },
86+
{ capabilities: "yes" },
87+
{ capabilities: {} },
88+
{ capabilities: { thinking: { types: { adaptive: {} } } } },
89+
] as Record<string, unknown>[]) {
90+
expect(acceptsAnthropicLiveModelContract({ id: "claude-opus-5", record })).toBe(false);
91+
}
92+
});
93+
});
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Contract gate for live-discovered Anthropic models the manifest does not
3+
* publish. Claude request shaping is selected by model-version predicates, so a
4+
* model those predicates do not recognize is shaped like a pre-4.6 model:
5+
* manual `budget_tokens` thinking plus caller sampling parameters. Current
6+
* Claude models reject both, so surfacing an unrecognized model would offer a
7+
* selectable entry whose every request 400s.
8+
*
9+
* Rather than hand-maintaining a family allowlist that rots on each launch,
10+
* accept a discovered model only when Anthropic's advertised capabilities agree
11+
* with what our contracts would apply. Disagreement means our shaping is stale
12+
* for that model, so it stays hidden until the contracts are updated.
13+
*/
14+
import {
15+
supportsClaudeAdaptiveThinking,
16+
supportsClaudeNativeMaxEffort,
17+
supportsClaudeNativeXhighEffort,
18+
} from "openclaw/plugin-sdk/provider-model-shared";
19+
import { isRecord } from "openclaw/plugin-sdk/string-coerce-runtime";
20+
21+
function readCapabilityFlag(root: unknown, path: readonly string[]): boolean | undefined {
22+
let current: unknown = root;
23+
for (const key of path) {
24+
if (!isRecord(current)) {
25+
return undefined;
26+
}
27+
current = current[key];
28+
}
29+
return typeof current === "boolean" ? current : undefined;
30+
}
31+
32+
/**
33+
* Anthropic publishes a capability tree on `/v1/models`. Each entry pairs a
34+
* capability we can read from that tree with the contract predicate that must
35+
* agree, so a mismatch on any axis rejects the model.
36+
*/
37+
const CLAUDE_CONTRACT_CAPABILITY_CHECKS = [
38+
{
39+
path: ["thinking", "types", "adaptive", "supported"],
40+
matches: supportsClaudeAdaptiveThinking,
41+
},
42+
{ path: ["effort", "xhigh", "supported"], matches: supportsClaudeNativeXhighEffort },
43+
{ path: ["effort", "max", "supported"], matches: supportsClaudeNativeMaxEffort },
44+
] as const;
45+
46+
/**
47+
* Return whether a live-discovered Claude model can be shaped by the current
48+
* contracts. Fails closed: a model without a readable capability tree is
49+
* rejected, because we cannot prove our shaping matches it.
50+
*/
51+
export function acceptsAnthropicLiveModelContract(params: {
52+
id: string;
53+
record: Record<string, unknown>;
54+
}): boolean {
55+
const capabilities = params.record.capabilities;
56+
if (!isRecord(capabilities)) {
57+
return false;
58+
}
59+
const ref = { id: params.id };
60+
return CLAUDE_CONTRACT_CAPABILITY_CHECKS.every((check) => {
61+
const advertised = readCapabilityFlag(capabilities, check.path);
62+
return advertised !== undefined && advertised === check.matches(ref);
63+
});
64+
}

‎extensions/anthropic/register.runtime.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import {
5858
applyAnthropicConfigDefaults,
5959
normalizeAnthropicProviderConfigForProvider,
6060
} from "./config-defaults.js";
61+
import { acceptsAnthropicLiveModelContract } from "./live-model-contract-gate.js";
6162
import { anthropicMediaUnderstandingProvider } from "./media-understanding-provider.js";
6263
import manifest from "./openclaw.plugin.json" with { type: "json" };
6364
import { resolveClaudeCliSyntheticAuth } from "./provider-discovery.js";
@@ -950,6 +951,7 @@ export function buildAnthropicProvider(): ProviderPlugin {
950951
...(key ? { "x-api-key": key } : {}),
951952
};
952953
},
954+
acceptUnknownModel: acceptsAnthropicLiveModelContract,
953955
},
954956
}),
955957
},
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Plugin SDK tests cover the live-discovery unknown-model gate.
2+
import { describe, expect, it } from "vitest";
3+
import { buildOpenAICompatibleLiveModels } from "./provider-catalog-live-normalize.internal.js";
4+
import type { ModelProviderConfig } from "./provider-model-shared.js";
5+
6+
const fallback = {
7+
baseUrl: "https://api.example.com",
8+
api: "anthropic-messages",
9+
models: [
10+
{
11+
id: "known-model",
12+
name: "Known",
13+
reasoning: true,
14+
input: ["text"],
15+
cost: { input: 5, output: 25, cacheRead: 0.5, cacheWrite: 6.25 },
16+
contextWindow: 1_000_000,
17+
maxTokens: 128_000,
18+
},
19+
],
20+
} as unknown as ModelProviderConfig;
21+
22+
const rows = [
23+
{ id: "known-model", object: "model" },
24+
{ id: "brand-new-model", object: "model" },
25+
];
26+
27+
describe("live discovery unknown-model gate", () => {
28+
it("publishes discovered models unchanged when no gate is supplied", () => {
29+
const ids = buildOpenAICompatibleLiveModels(rows, fallback).map((m) => m.id);
30+
expect(ids).toContain("known-model");
31+
expect(ids).toContain("brand-new-model");
32+
});
33+
34+
it("drops only the ids the manifest does not publish when the gate rejects", () => {
35+
const seen: string[] = [];
36+
const models = buildOpenAICompatibleLiveModels(rows, fallback, ({ id }) => {
37+
seen.push(id);
38+
return false;
39+
});
40+
// The manifest-published row bypasses the gate entirely and survives.
41+
expect(models.map((m) => m.id)).toEqual(["known-model"]);
42+
expect(seen).toEqual(["brand-new-model"]);
43+
});
44+
45+
it("keeps the manifest entry verbatim for published ids", () => {
46+
const [model] = buildOpenAICompatibleLiveModels(rows, fallback, () => false);
47+
expect(model).toEqual(fallback.models[0]);
48+
});
49+
50+
it("admits unknown ids the gate accepts", () => {
51+
const ids = buildOpenAICompatibleLiveModels(rows, fallback, () => true).map((m) => m.id);
52+
expect(ids).toEqual(["brand-new-model", "known-model"]);
53+
});
54+
});

‎src/plugin-sdk/provider-catalog-live-normalize.internal.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ function inferLiveModelReasoning(modelId: string): boolean {
172172
function buildOpenAICompatibleLiveModel(
173173
row: unknown,
174174
fallback: ModelProviderConfig,
175+
acceptUnknownModel?: (params: { id: string; record: Record<string, unknown> }) => boolean,
175176
): ModelDefinitionConfig | undefined {
176177
const record = readLiveModelCatalogRecord(row);
177178
const id = readLiveModelString(record, ["id", "model", "model_name", "modelName"]);
@@ -202,6 +203,13 @@ function buildOpenAICompatibleLiveModel(
202203
if (exact) {
203204
return exact;
204205
}
206+
// Manifest-published ids returned above are known-good. Everything past this
207+
// point is a model the manifest has never described, so an opted-in provider
208+
// gate decides whether its request shaping is understood well enough to
209+
// surface it at all.
210+
if (acceptUnknownModel && !acceptUnknownModel({ id, record })) {
211+
return undefined;
212+
}
205213
const template = findLiveModelTemplate(id, fallback.models);
206214
const inputModalities = readLiveModelStringArray(
207215
[record, architecture, capabilities, modelInfo],
@@ -276,9 +284,10 @@ function buildOpenAICompatibleLiveModel(
276284
export function buildOpenAICompatibleLiveModels(
277285
rows: readonly unknown[],
278286
fallback: ModelProviderConfig,
287+
acceptUnknownModel?: (params: { id: string; record: Record<string, unknown> }) => boolean,
279288
): ModelDefinitionConfig[] {
280289
const models = rows
281-
.map((row) => buildOpenAICompatibleLiveModel(row, fallback))
290+
.map((row) => buildOpenAICompatibleLiveModel(row, fallback, acceptUnknownModel))
282291
.filter((model): model is ModelDefinitionConfig => Boolean(model));
283292
return [...new Map(models.map((model) => [model.id, model])).values()].toSorted((a, b) =>
284293
a.id.localeCompare(b.id),

‎src/plugin-sdk/provider-catalog-live-runtime.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ export type OpenAICompatibleModelDiscoveryOptions = {
9393
readRows?: FetchLiveProviderModelRowsParams["readRows"];
9494
/** Provider-specific authorization headers for non-Bearer model-list APIs. */
9595
buildRequestHeaders?: FetchLiveProviderModelRowsParams["buildRequestHeaders"];
96+
/**
97+
* Gate for discovered ids the manifest does not already publish. Providers
98+
* whose request shaping is model-version specific use this to drop models
99+
* they cannot yet shape, so discovery never surfaces a selectable model that
100+
* would build an invalid request. Manifest-published ids bypass it.
101+
*/
102+
acceptUnknownModel?: (params: { id: string; record: Record<string, unknown> }) => boolean;
96103
};
97104

98105
export type BuildOpenAICompatibleProviderCatalogParams = {
@@ -491,6 +498,7 @@ export async function buildOpenAICompatibleLiveModelProviderConfig(params: {
491498
...params.providerConfig,
492499
...(params.apiKey ? { apiKey: params.apiKey } : {}),
493500
};
501+
const acceptUnknownModel = params.modelDiscovery?.acceptUnknownModel;
494502
const endpoint = params.modelDiscovery?.endpointUrl
495503
? resolveFixedLiveModelDiscoveryEndpoint(fallback.baseUrl, params.modelDiscovery.endpointUrl)
496504
: resolveLiveModelDiscoveryEndpoint(
@@ -513,9 +521,9 @@ export async function buildOpenAICompatibleLiveModelProviderConfig(params: {
513521
readRows: params.modelDiscovery?.readRows,
514522
buildRequestHeaders: params.modelDiscovery?.buildRequestHeaders,
515523
shouldCacheRows: (modelRows) =>
516-
buildOpenAICompatibleLiveModels(modelRows, fallback).length > 0,
524+
buildOpenAICompatibleLiveModels(modelRows, fallback, acceptUnknownModel).length > 0,
517525
});
518-
const models = buildOpenAICompatibleLiveModels(rows, fallback);
526+
const models = buildOpenAICompatibleLiveModels(rows, fallback, acceptUnknownModel);
519527
if (models.length > 0) {
520528
return { ...fallback, models };
521529
}

0 commit comments

Comments
 (0)