Skip to content

Commit 25a1037

Browse files
committed
fix(gateway): reject model selections without an activatable harness
1 parent 3a9d6cc commit 25a1037

4 files changed

Lines changed: 140 additions & 30 deletions

File tree

‎docs/concepts/models.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ allowlist replaces the shared default, including `[]` to allow any model.
186186
Policy permission does not supply provider credentials or guarantee that the
187187
selected model is available to its runtime.
188188

189+
Before saving a model selection, these Gateway methods check that any required
190+
embedded harness has an installed, activatable plugin. A missing or disabled
191+
plugin rejects the change and preserves the previous session selection and
192+
configured default. Install and enable the named harness plugin, restart the
193+
Gateway, then select the model again. This check does not start the runtime or
194+
verify provider credentials.
195+
189196
Choose the model when you create a session whenever possible. The Control UI's
190197
**New Chat** composer includes the model picker for this reason: a fresh session
191198
gives the selected model a clean conversation boundary.

‎src/agents/harness/runtime-plugin-load-plan.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ export function resolveSelectedAgentHarnessRuntime(
296296
}
297297

298298
// Returns whether a selection needs a plugin-owned harness in its prepared generation.
299-
function requiresAgentHarnessPluginSelection(
299+
export function requiresAgentHarnessPluginSelection(
300300
selection: AgentHarnessPluginSelection,
301301
config?: OpenClawConfig,
302302
): boolean {

‎src/gateway/server.sessions.agent-model-catalog.test.ts‎

Lines changed: 85 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
import fs from "node:fs/promises";
2+
import path from "node:path";
13
import { afterEach, describe, expect, test, vi } from "vitest";
24
import type { SessionEntry } from "../config/sessions.js";
35
import { loadSessionEntry } from "../config/sessions/session-accessor.js";
6+
import {
7+
createColdPluginFixture,
8+
isColdPluginRuntimeLoaded,
9+
} from "../plugins/test-helpers/cold-plugin-fixtures.js";
410
import { closeOpenClawStateDatabaseForTest } from "../state/openclaw-state-db.js";
511
import type { PrepareGatewaySessionLifecycle } from "./session-lifecycle-preparation.js";
612
import { writeSessionStore } from "./test-helpers.js";
@@ -37,16 +43,21 @@ type ModelSelectionCase = {
3743
agentAllow?: string[];
3844
globalAlias?: string;
3945
agentAlias?: string;
46+
agentRuntime?: string;
47+
harness?: "enabled" | "disabled" | "denied";
48+
codexDenied?: boolean;
4049
model: string;
4150
expectedModel: string;
4251
denied?: boolean;
52+
error?: string;
4353
};
4454

4555
function configureAgentModels(
4656
scenario: Pick<
4757
ModelSelectionCase,
48-
"globalAllow" | "agentAllow" | "globalAlias" | "agentAlias"
58+
"globalAllow" | "agentAllow" | "globalAlias" | "agentAlias" | "agentRuntime"
4959
> & { subagentModel?: string },
60+
runtimeModel = workRef,
5061
) {
5162
testState.agentConfig = {
5263
model: { primary: "synthetic/base" },
@@ -65,13 +76,50 @@ function configureAgentModels(
6576
id: "work",
6677
subagents: scenario.subagentModel ? { model: scenario.subagentModel } : undefined,
6778
...(scenario.agentAllow ? { modelPolicy: { allow: scenario.agentAllow } } : {}),
68-
models: scenario.agentAlias ? { [workRef]: { alias: scenario.agentAlias } } : {},
79+
models: {
80+
[workRef]: scenario.agentAlias ? { alias: scenario.agentAlias } : {},
81+
...(scenario.agentRuntime
82+
? { [runtimeModel]: { agentRuntime: { id: scenario.agentRuntime } } }
83+
: {}),
84+
},
6985
},
7086
],
7187
};
7288
}
7389

7490
const cases: ModelSelectionCase[] = [
91+
{
92+
label: "rejects configured Codex selection when its harness is denied",
93+
globalAllow: [],
94+
agentRuntime: "codex",
95+
codexDenied: true,
96+
model: "openai/gpt-5.4",
97+
expectedModel: "openai/gpt-5.4",
98+
denied: true,
99+
error:
100+
'Model openai/gpt-5.4 requires agent harness "codex", but no enabled plugin provides it. Install and enable its plugin, restart the Gateway, then select the model again.',
101+
},
102+
{
103+
label: "preserves the session when the selected model requires an unavailable harness",
104+
globalAllow: [],
105+
agentRuntime: "missing-harness",
106+
model: workRef,
107+
expectedModel: workRef,
108+
denied: true,
109+
error:
110+
'Model work-provider/work-only requires agent harness "missing-harness", but no enabled plugin provides it. Install and enable its plugin, restart the Gateway, then select the model again.',
111+
},
112+
...(["enabled", "disabled", "denied"] as const).map((harness) => ({
113+
label: `checks an installed ${harness} harness without loading its runtime`,
114+
globalAllow: [],
115+
agentRuntime: "fixture-harness",
116+
harness,
117+
model: workRef,
118+
expectedModel: workRef,
119+
denied: harness !== "enabled",
120+
error:
121+
'Model work-provider/work-only requires agent harness "fixture-harness", but no enabled plugin provides it. Install and enable its plugin, restart the Gateway, then select the model again.',
122+
})),
75123
{
76124
label: "loads the explicit agent model catalog",
77125
explicitAgent: true,
@@ -158,8 +206,30 @@ const cases: ModelSelectionCase[] = [
158206

159207
describe.each(["sessions.create", "sessions.patch"] as const)("%s", (method) => {
160208
test.each(cases)("$label", async (scenario) => {
161-
const { workStorePath } = await createSelectedGlobalSessionStore();
162-
configureAgentModels(scenario);
209+
const { dir, workStorePath } = await createSelectedGlobalSessionStore();
210+
configureAgentModels(scenario, scenario.expectedModel);
211+
let fixture: ReturnType<typeof createColdPluginFixture> | undefined;
212+
if (scenario.harness) {
213+
const rootDir = await fs.mkdtemp(path.join(dir, "harness-"));
214+
fixture = createColdPluginFixture({
215+
rootDir,
216+
pluginId: "fixture-harness",
217+
manifest: { activation: { onAgentHarnesses: ["fixture-harness"] } },
218+
});
219+
const { writeConfigFile } = await getGatewayConfigModule();
220+
await writeConfigFile({
221+
plugins: {
222+
load: { paths: [rootDir] },
223+
...(scenario.harness === "denied"
224+
? {}
225+
: { entries: { "fixture-harness": { enabled: scenario.harness !== "disabled" } } }),
226+
...(scenario.harness === "denied" ? { deny: ["fixture-harness"] } : {}),
227+
},
228+
});
229+
} else if (scenario.codexDenied) {
230+
const { writeConfigFile } = await getGatewayConfigModule();
231+
await writeConfigFile({ plugins: { deny: ["codex"] } });
232+
}
163233
const key = "agent:work:dashboard:catalog-owner";
164234
const access = { agentId: "work", sessionKey: key, storePath: workStorePath };
165235
if (method === "sessions.patch") {
@@ -177,6 +247,8 @@ describe.each(["sessions.create", "sessions.patch"] as const)("%s", (method) =>
177247
});
178248
}
179249
const before = loadSessionEntry(access);
250+
const { readConfigFileSnapshot } = await getGatewayConfigModule();
251+
const beforeConfig = await readConfigFileSnapshot();
180252
const loadGatewayModelCatalog = createAgentModelCatalogLoader();
181253
const result = await directSessionReq<{ entry?: SessionEntry }>(
182254
method,
@@ -186,17 +258,24 @@ describe.each(["sessions.create", "sessions.patch"] as const)("%s", (method) =>
186258
model: scenario.model,
187259
label: "Updated label",
188260
},
189-
{ context: { loadGatewayModelCatalog } },
261+
{
262+
context: { loadGatewayModelCatalog },
263+
...(scenario.error ? { client: { connect: { scopes: ["operator.admin"] } } as never } : {}),
264+
},
190265
);
191266

192267
expect(loadGatewayModelCatalog).toHaveBeenCalledWith({ agentId: "work" });
268+
if (fixture) {
269+
expect(isColdPluginRuntimeLoaded(fixture)).toBe(false);
270+
}
193271
if (scenario.denied) {
194272
expect.soft(result.ok).toBe(false);
195273
expect.soft(result.error).toMatchObject({
196274
code: "INVALID_REQUEST",
197-
message: `model not allowed: ${scenario.expectedModel}`,
275+
message: scenario.error ?? `model not allowed: ${scenario.expectedModel}`,
198276
});
199277
expect(loadSessionEntry(access)).toEqual(before);
278+
expect((await readConfigFileSnapshot()).config).toEqual(beforeConfig.config);
200279
return;
201280
}
202281
expect(result.ok, result.error?.message).toBe(true);

‎src/gateway/sessions-patch.ts‎

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ import {
88
type SessionsPatchParams,
99
} from "../../packages/gateway-protocol/src/index.js";
1010
import { readAcpSessionMetaForEntry } from "../acp/runtime/session-meta.js";
11-
import { resolveAgentDir, resolveDefaultAgentId } from "../agents/agent-scope.js";
11+
import {
12+
resolveAgentDir,
13+
resolveAgentWorkspaceDir,
14+
resolveDefaultAgentId,
15+
} from "../agents/agent-scope.js";
16+
import {
17+
requiresAgentHarnessPluginSelection,
18+
resolveAgentHarnessOwnerPluginIds,
19+
} from "../agents/harness/runtime-plugin-load-plan.js";
1220
import type { ModelCatalogEntry } from "../agents/model-catalog.js";
1321
import { splitTrailingAuthProfile } from "../agents/model-ref-profile.js";
1422
import {
@@ -468,22 +476,11 @@ export async function projectSessionsPatchEntry(params: {
468476
: undefined;
469477
delete next.modelFallback;
470478
const raw = patch.model;
479+
let selection:
480+
| { provider: string; model: string; profile?: string; isDefault: boolean }
481+
| undefined;
471482
if (raw === null) {
472-
applyModelOverrideWithAuthProfileCompatibility({
473-
cfg,
474-
agentDir: resolveAgentDir(cfg, sessionAgentId),
475-
entry: next,
476-
currentProvider: next.providerOverride ?? next.modelProvider ?? resolvedDefault.provider,
477-
selection: {
478-
provider: resolvedDefault.provider,
479-
model: resolvedDefault.model,
480-
isDefault: true,
481-
},
482-
...(params.providerAuthMetadataSnapshot
483-
? { metadataSnapshot: params.providerAuthMetadataSnapshot }
484-
: {}),
485-
});
486-
delete next.liveModelSwitchPending;
483+
selection = { ...resolvedDefault, isDefault: true };
487484
} else if (raw !== undefined) {
488485
const trimmed = normalizeOptionalString(raw) ?? "";
489486
if (!trimmed) {
@@ -511,22 +508,49 @@ export async function projectSessionsPatchEntry(params: {
511508
if (!resolved.ok) {
512509
return invalid(resolved.error);
513510
}
511+
selection = resolved;
512+
}
513+
if (selection) {
514+
// Catalog membership does not guarantee an activatable harness. Reject before
515+
// committing the session so sticky defaults cannot retain an unusable selection.
516+
const harnessSelection = {
517+
provider: selection.provider,
518+
modelId: selection.model,
519+
runtime: resolveThinkingRuntime(selection.provider, selection.model, next),
520+
agentId: sessionAgentId,
521+
};
522+
if (
523+
!readAcpSessionMetaForEntry({
524+
sessionKey: storeKey,
525+
agentId: sessionAgentId,
526+
entry: next,
527+
}) &&
528+
requiresAgentHarnessPluginSelection(harnessSelection, cfg) &&
529+
resolveAgentHarnessOwnerPluginIds({
530+
...harnessSelection,
531+
config: cfg,
532+
workspaceDir: resolveAgentWorkspaceDir(cfg, sessionAgentId),
533+
}).length === 0
534+
) {
535+
return invalid(
536+
`Model ${selection.provider}/${selection.model} requires agent harness "${harnessSelection.runtime}", but no enabled plugin provides it. Install and enable its plugin, restart the Gateway, then select the model again.`,
537+
);
538+
}
514539
applyModelOverrideWithAuthProfileCompatibility({
515540
cfg,
516541
agentDir: resolveAgentDir(cfg, sessionAgentId),
517542
entry: next,
518543
currentProvider: next.providerOverride ?? next.modelProvider ?? resolvedDefault.provider,
519-
selection: {
520-
provider: resolved.provider,
521-
model: resolved.model,
522-
isDefault: resolved.isDefault,
523-
},
524-
profileOverride: resolved.profile,
544+
selection,
545+
profileOverride: selection.profile,
525546
...(params.providerAuthMetadataSnapshot
526547
? { metadataSnapshot: params.providerAuthMetadataSnapshot }
527548
: {}),
528-
markLiveSwitchPending: true,
549+
markLiveSwitchPending: raw !== null,
529550
});
551+
if (raw === null) {
552+
delete next.liveModelSwitchPending;
553+
}
530554
}
531555
if (agentModelFallback) {
532556
next.modelFallback = agentModelFallback;

0 commit comments

Comments
 (0)