Skip to content

Commit 7394262

Browse files
authored
feat: per-model code-mode capability flags with an "auto" master-switch tier (#114695)
* feat(tools): per-model code-mode capability flags and auto master-switch tier * feat(anthropic): flag claude-sonnet-4-6 as code-mode preferred * revert(anthropic): drop claude-sonnet-4-6 from the code-mode preferred set * test(anthropic): type the manifest compat field in the catalog contract test * fix(anthropic): carry catalog compat onto hand-built forward-compat model rows
1 parent 5210e1b commit 7394262

36 files changed

Lines changed: 413 additions & 41 deletions

‎docs/docs_map.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9907,6 +9907,10 @@ Do not edit it by hand; run `pnpm docs:map:gen`.
99079907
- H2: Scope
99089908
- H2: Terms
99099909
- H2: Configuration
9910+
- H2: Automatic per-model activation
9911+
- H3: The compat.codeMode catalog flag
9912+
- H3: Shipped preferred models
9913+
- H3: Choosing when to enable
99109914
- H2: Activation
99119915
- H2: Model-visible tools
99129916
- H2: exec

‎docs/gateway/config-tools.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ The shorthand is also accepted:
108108
}
109109
```
110110

111+
`enabled` also accepts `"auto"`, which engages code mode only for models whose
112+
catalog entry flags `compat.codeMode: "preferred"`. See
113+
[Code Mode - automatic per-model activation](/tools/code-mode#automatic-per-model-activation).
114+
111115
MCP declarations are exposed through the read-only virtual API file surface in
112116
code mode. Guest code can call `API.list("mcp")` and
113117
`API.read("mcp/<server>.d.ts")` to inspect TypeScript-style signatures before

‎docs/tools/code-mode.md‎

Lines changed: 88 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ separate implementations:
2424
prefixed by a `// @exec: {...}` pragma line for execution options), executed
2525
in Codex's in-process V8 Code Mode runtime.
2626
- OpenClaw code mode runs in the generic OpenClaw agent runtime and is
27-
disabled unless `tools.codeMode.enabled: true` is configured. Its `exec`
27+
disabled unless `tools.codeMode.enabled` is `true` or `"auto"`. Its `exec`
2828
tool takes a JSON `{ code, language }` payload, executed in a QuickJS-WASI
2929
worker.
3030

@@ -112,7 +112,21 @@ Shorthand:
112112
```
113113

114114
Code mode stays off when `tools.codeMode` is omitted, `false`, or an object
115-
without `enabled: true`.
115+
without `enabled: true` or `enabled: "auto"`.
116+
117+
To engage code mode only for models whose catalog marks them as strong code-mode
118+
performers, use the `"auto"` tier instead of `true`:
119+
120+
```json5
121+
{
122+
tools: {
123+
codeMode: "auto",
124+
},
125+
}
126+
```
127+
128+
See [Automatic per-model activation](#automatic-per-model-activation) for the
129+
exact semantics and the shipped model list.
116130

117131
If you use sandboxed agents with configured MCP servers, also allow the
118132
bundled MCP plugin in the sandbox tool policy, for example
@@ -238,7 +252,7 @@ enable the feature on its own.
238252

239253
| Field | Default | Clamp |
240254
| --------------------- | ------------------------------ | ----------------------------------------------- |
241-
| `enabled` | `false` | boolean; only `true` enables code mode |
255+
| `enabled` | `false` | `false`, `true`, or `"auto"` (per-model) |
242256
| `runtime` | `"quickjs-wasi"` | only supported value |
243257
| `mode` | `"only"` | exposes control/direct tools, catalogs the rest |
244258
| `languages` | `["javascript", "typescript"]` | any subset of the two |
@@ -252,7 +266,75 @@ enable the feature on its own.
252266
| `maxSearchLimit` | `50` | `1`-`50` |
253267

254268
If code mode is enabled but QuickJS-WASI cannot load, OpenClaw fails closed
255-
for that run; it does not silently expose normal tools as a fallback.
269+
for that run; it does not silently expose normal tools as a fallback. This
270+
holds for `true` and for `"auto"` runs where the model resolves as preferred:
271+
an engaged run never silently falls back to broad direct tool exposure.
272+
273+
## Automatic per-model activation
274+
275+
`tools.codeMode.enabled` accepts three values:
276+
277+
- `false` (default): code mode is off for every run.
278+
- `true`: code mode engages for every tool-capable run, regardless of model.
279+
- `"auto"`: code mode engages only when the run's model is flagged as a
280+
preferred code-mode performer in its provider catalog.
281+
282+
`false` and `true` behave exactly as before the `"auto"` tier existed; `"auto"`
283+
is purely additive.
284+
285+
### The `compat.codeMode` catalog flag
286+
287+
Provider catalogs can tier a model with `compat.codeMode` on its model entry,
288+
next to flags like `compat.supportsTools`:
289+
290+
- `"preferred"`: the model reliably writes short orchestration programs and
291+
benefits from the compact code-mode surface; `"auto"` engages code mode.
292+
- `"capable"` (or absent): the model can run code mode when forced with
293+
`enabled: true`, but `"auto"` keeps normal tool exposure.
294+
295+
Models without tool support cannot use code mode at all; there is no separate
296+
"unsupported" tier. The flag is capability metadata owned by the provider
297+
plugin's catalog; core only reads the generic compat field.
298+
299+
### Shipped preferred models
300+
301+
Bundled provider catalogs currently flag these models as `"preferred"`:
302+
303+
| Provider | Models |
304+
| --------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
305+
| anthropic | `claude-fable-5`, `claude-opus-5`, `claude-sonnet-5`, `claude-mythos-5`, `claude-opus-4-8`, `claude-haiku-4-5` |
306+
| deepseek | `deepseek-v4-pro`, `deepseek-v4-flash` |
307+
| google | `gemini-3-flash-preview`, `gemini-3.1-pro-preview`, `gemini-3.1-flash-lite`, `gemini-3.5-flash`, `gemini-3.5-flash-lite`, `gemini-3.6-flash` |
308+
| minimax | `MiniMax-M3` |
309+
| moonshot | `kimi-k3` |
310+
| openai | `gpt-5.6`, `gpt-5.6-sol`, `gpt-5.6-terra`, `gpt-5.6-luna`, `gpt-5.5`, `gpt-5.5-pro` |
311+
| xiaomi | `mimo-v2.5` |
312+
| zai | `glm-5.2`, `glm-5.1` |
313+
314+
Everything else, including all Ollama-served local models, stays unflagged and
315+
keeps normal tool exposure under `"auto"`.
316+
317+
For OpenAI models, the flag matters only when the run resolves to the OpenClaw
318+
embedded agent runtime. Default OpenAI routing uses the Codex-style harness
319+
surface, where OpenClaw code mode does not apply; the catalog flag never
320+
changes that routing decision.
321+
322+
### Choosing when to enable
323+
324+
In A/B evaluations on the preferred models above, code mode reduced total
325+
token usage by roughly 30-50% at equal-or-better task pass rates, mostly by
326+
replacing many full tool schemas and per-tool round trips with one compact
327+
program surface. Models below the preferred tier showed no consistent win and
328+
sometimes regressed, which is why `"auto"` leaves them on direct tools.
329+
330+
Use `"auto"` when agents switch between models: strong models get the compact
331+
surface, weaker or local ones keep the exposure they handle best. Use `true`
332+
when you have verified a specific unflagged model performs well with code
333+
mode; global force-on is most predictable for single-model deployments. For
334+
open-weight or uncached serving where every prompt token is billed or
335+
recomputed, prefer enabling per model (via `"auto"` or a per-agent override)
336+
rather than globally, since the token savings depend on the model actually
337+
using the program surface well.
256338

257339
## Activation
258340

@@ -264,7 +346,8 @@ final model request is assembled:
264346
2. Build the effective OpenClaw tool list, adding eligible plugin, MCP, and
265347
client tools.
266348
3. Apply allow/deny policy.
267-
4. If `tools.codeMode.enabled` is false, continue with normal tool exposure.
349+
4. If `tools.codeMode.enabled` is `false`, or is `"auto"` and the run's model
350+
is not catalog-preferred, continue with normal tool exposure.
268351
5. If enabled and tools are active for the run, retain required direct-only
269352
tools and register every catalog-eligible effective tool in the code-mode
270353
catalog.

‎extensions/anthropic/forward-compat-generation.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,32 @@ describe("unreleased Claude generations", () => {
4747
expect(supportsClaudeAdaptiveThinking({ id: "claude-haiku-4-5-20251001" })).toBe(false);
4848
});
4949

50+
it("carries manifest catalog compat onto hand-built modern rows", () => {
51+
// The hand-built forward-compat row replaces the catalog row when the
52+
// runtime prefers plugin-resolved modern models. Dropping compat here
53+
// silently disables catalog-driven behavior such as codeMode "auto",
54+
// including on env-key-only runs whose model registry is empty.
55+
expect(resolveModel("claude-opus-5")?.compat).toEqual({ codeMode: "preferred" });
56+
expect(resolveModel("claude-sonnet-5")?.compat).toEqual({ codeMode: "preferred" });
57+
// The Claude CLI provider rows are intentionally unflagged: those runs use
58+
// the CLI harness where OpenClaw code mode does not apply.
59+
expect(resolveModel("claude-opus-5", "claude-cli")?.compat).toBeUndefined();
60+
});
61+
62+
it("prefers registry compat over the manifest index", () => {
63+
const model = buildAnthropicProvider().resolveDynamicModel?.({
64+
provider: "anthropic",
65+
modelId: "claude-opus-5",
66+
config: {},
67+
modelRegistry: {
68+
find: () => ({ compat: { codeMode: "capable" } }),
69+
},
70+
} as unknown as Parameters<
71+
NonNullable<ReturnType<typeof buildAnthropicProvider>["resolveDynamicModel"]>
72+
>[0]);
73+
expect(model?.compat).toEqual({ codeMode: "capable" });
74+
});
75+
5076
it("does not claim non-Claude ids", () => {
5177
// Third-party models reach the Anthropic-compatible transport too.
5278
expect(resolveModel("mimo-v2-flash")).toBeUndefined();

‎extensions/anthropic/openclaw.plugin.json‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@
8686
"minimal": "low",
8787
"xhigh": "xhigh",
8888
"max": "max"
89-
}
89+
},
90+
"compat": { "codeMode": "preferred" }
9091
},
9192
{
9293
"id": "claude-opus-5",
@@ -102,7 +103,8 @@
102103
"thinkingLevelMap": {
103104
"xhigh": "xhigh",
104105
"max": "max"
105-
}
106+
},
107+
"compat": { "codeMode": "preferred" }
106108
},
107109
{
108110
"id": "claude-sonnet-5",
@@ -118,7 +120,8 @@
118120
"thinkingLevelMap": {
119121
"xhigh": "xhigh",
120122
"max": "max"
121-
}
123+
},
124+
"compat": { "codeMode": "preferred" }
122125
},
123126
{
124127
"id": "claude-mythos-5",
@@ -136,7 +139,8 @@
136139
"minimal": "low",
137140
"xhigh": "xhigh",
138141
"max": "max"
139-
}
142+
},
143+
"compat": { "codeMode": "preferred" }
140144
},
141145
{
142146
"id": "claude-opus-4-8",
@@ -149,7 +153,8 @@
149153
"image": { "maxSidePx": 2576, "preferredSidePx": 2576, "tokenMode": "provider" }
150154
},
151155
"contextWindow": 1000000,
152-
"maxTokens": 128000
156+
"maxTokens": 128000,
157+
"compat": { "codeMode": "preferred" }
153158
},
154159
{
155160
"id": "claude-haiku-4-5",
@@ -160,7 +165,8 @@
160165
"image": { "maxSidePx": 1568, "preferredSidePx": 1568, "tokenMode": "provider" }
161166
},
162167
"contextWindow": 200000,
163-
"maxTokens": 64000
168+
"maxTokens": 64000,
169+
"compat": { "codeMode": "preferred" }
164170
}
165171
]
166172
}

‎extensions/anthropic/openclaw.plugin.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type AnthropicCatalogModel = {
2525
thinkingLevelMap?: Record<string, string | null>;
2626
status?: string;
2727
replacedBy?: string;
28+
compat?: { codeMode?: string };
2829
};
2930

3031
type AnthropicManifest = {
@@ -42,6 +43,14 @@ const manifest = JSON.parse(
4243
) as AnthropicManifest;
4344

4445
describe("Anthropic plugin manifest", () => {
46+
it("flags every static Anthropic API model as code-mode preferred", () => {
47+
const models = manifest.modelCatalog?.providers?.anthropic?.models ?? [];
48+
expect(models.length).toBeGreaterThan(0);
49+
for (const model of models) {
50+
expect(model.compat?.codeMode, model.id).toBe("preferred");
51+
}
52+
});
53+
4554
it("publishes the exact Claude Opus 5 API contract", () => {
4655
const models = manifest.modelCatalog?.providers?.anthropic?.models ?? [];
4756
expect(models.find((model) => model.id === "claude-opus-5")).toEqual({
@@ -56,6 +65,7 @@ describe("Anthropic plugin manifest", () => {
5665
contextWindow: 1_000_000,
5766
maxTokens: 128_000,
5867
thinkingLevelMap: { xhigh: "xhigh", max: "max" },
68+
compat: { codeMode: "preferred" },
5969
});
6070
});
6171

@@ -73,6 +83,7 @@ describe("Anthropic plugin manifest", () => {
7383
contextWindow: 1_000_000,
7484
maxTokens: 128_000,
7585
thinkingLevelMap: { xhigh: "xhigh", max: "max" },
86+
compat: { codeMode: "preferred" },
7687
});
7788
});
7889

@@ -90,6 +101,7 @@ describe("Anthropic plugin manifest", () => {
90101
contextWindow: 1_000_000,
91102
maxTokens: 128_000,
92103
thinkingLevelMap: { xhigh: "xhigh", max: "max" },
104+
compat: { codeMode: "preferred" },
93105
});
94106
// Opus 5's 1M window is the model default, so the CLI row is not clamped to 200k.
95107
const cliModels = manifest.modelCatalog?.providers?.["claude-cli"]?.models ?? [];
@@ -134,6 +146,7 @@ describe("Anthropic plugin manifest", () => {
134146
},
135147
contextWindow: 200000,
136148
maxTokens: 64000,
149+
compat: { codeMode: "preferred" },
137150
});
138151
expect(models.find((model) => model.id === "claude-haiku-4-5-20251001")).toBeUndefined();
139152
});

‎extensions/anthropic/register.runtime.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
import {
3434
buildProviderReplayFamilyHooks,
3535
cloneFirstTemplateModel,
36+
type ModelCompatConfig,
3637
modelCostsEqual,
3738
type ProviderPlugin,
3839
resolveClaudeFable5ModelIdentity,
@@ -442,6 +443,30 @@ function resolveAnthropicUnreleasedCanonicalModelId(modelId: string): string {
442443
return /(?:^|-)claude-sonnet-/.test(modelId) ? "claude-sonnet-5" : "claude-opus-5";
443444
}
444445

446+
// Lazily indexed manifest compat per provider so hand-built dynamic rows keep
447+
// catalog capability metadata even when the run's model registry is empty
448+
// (for example env-key-only runs without a generated models.json).
449+
let anthropicManifestCompatIndex: Map<string, ModelCompatConfig> | undefined;
450+
451+
function resolveAnthropicManifestCompat(
452+
provider: string,
453+
modelId: string,
454+
): ModelCompatConfig | undefined {
455+
if (!anthropicManifestCompatIndex) {
456+
anthropicManifestCompatIndex = new Map();
457+
const providers = manifest.modelCatalog?.providers ?? {};
458+
for (const [providerId, catalog] of Object.entries(providers)) {
459+
for (const model of catalog.models ?? []) {
460+
const compat = (model as { compat?: ModelCompatConfig }).compat;
461+
if (compat) {
462+
anthropicManifestCompatIndex.set(`${providerId}/${model.id}`, compat);
463+
}
464+
}
465+
}
466+
}
467+
return anthropicManifestCompatIndex.get(`${provider}/${modelId}`);
468+
}
469+
445470
function buildAnthropicForwardCompatModel(
446471
ctx: ProviderResolveDynamicModelContext,
447472
): ProviderRuntimeModel | undefined {
@@ -457,10 +482,21 @@ function buildAnthropicForwardCompatModel(
457482
}
458483
const provider =
459484
normalizedProvider === CLAUDE_CLI_BACKEND_ID ? CLAUDE_CLI_BACKEND_ID : PROVIDER_ID;
485+
// This hand-built row replaces the catalog row when the runtime prefers
486+
// plugin-resolved modern models, so it must carry the catalog's compat
487+
// capability metadata (for example compat.codeMode) instead of dropping it.
488+
// Registry compat wins when present (it may carry config overrides); the
489+
// manifest index covers empty-registry runs such as env-key-only sessions.
490+
const catalogModel = ctx.modelRegistry.find(provider, trimmedModelId) as
491+
| Pick<ProviderRuntimeModel, "compat">
492+
| null
493+
| undefined;
494+
const compat = catalogModel?.compat ?? resolveAnthropicManifestCompat(provider, trimmedModelId);
460495
return {
461496
id: trimmedModelId,
462497
name: trimmedModelId,
463498
provider,
499+
...(compat ? { compat } : {}),
464500
api: "anthropic-messages",
465501
baseUrl: "https://api.anthropic.com",
466502
reasoning: true,

‎extensions/copilot/src/tool-bridge.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ export async function createCopilotToolBridge(
201201
executeTool: (toolParams) => executeCatalogTool(input, toolParams),
202202
forceMessageTool: shouldForceCopilotMessageTool(attemptParams),
203203
isRawModelRun: isCopilotRawModelRun(attemptParams),
204+
// Carries catalog compat so `tools.codeMode.enabled: "auto"` can resolve per model.
205+
model: attemptParams.model,
204206
modelId: input.modelId,
205207
modelProvider: input.modelProvider,
206208
modelToolsEnabled: true,

‎extensions/deepseek/openclaw.plugin.json‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
"compat": {
4747
"supportsUsageInStreaming": true,
4848
"supportsReasoningEffort": true,
49-
"maxTokensField": "max_tokens"
49+
"maxTokensField": "max_tokens",
50+
"codeMode": "preferred"
5051
}
5152
},
5253
{
@@ -65,7 +66,8 @@
6566
"compat": {
6667
"supportsUsageInStreaming": true,
6768
"supportsReasoningEffort": true,
68-
"maxTokensField": "max_tokens"
69+
"maxTokensField": "max_tokens",
70+
"codeMode": "preferred"
6971
}
7072
}
7173
]

0 commit comments

Comments
 (0)