Skip to content

Commit 7569ddc

Browse files
committed
fix(doctor): report runtime tool schema validation errors
# Conflicts: # src/flows/doctor-core-checks.runtime.ts
1 parent 2dae897 commit 7569ddc

2 files changed

Lines changed: 257 additions & 39 deletions

File tree

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import { beforeEach, describe, expect, it, vi } from "vitest";
2+
import type { AnyAgentTool } from "../agents/tools/common.js";
3+
import { setPluginToolMeta } from "../plugins/tools.js";
4+
5+
const mocks = vi.hoisted(() => ({
6+
createBundleMcpToolRuntime: vi.fn(),
7+
createOpenClawCodingTools: vi.fn(),
8+
disposeBundleRuntime: vi.fn(),
9+
loadModelCatalog: vi.fn(async (): Promise<Array<Record<string, unknown>>> => []),
10+
normalizeProviderToolSchemasWithPlugin: vi.fn(),
11+
resolveDefaultModelForAgent: vi.fn(() => ({ provider: "openai", model: "gpt-5.5" })),
12+
}));
13+
14+
vi.mock("../agents/model-catalog.js", () => ({
15+
findModelInCatalog: (
16+
catalog: Array<{ provider?: string; id?: string }>,
17+
provider: string,
18+
modelId: string,
19+
) => catalog.find((entry) => entry.provider === provider && entry.id === modelId),
20+
loadModelCatalog: mocks.loadModelCatalog,
21+
}));
22+
23+
vi.mock("../agents/model-selection.js", async (importOriginal) => ({
24+
...(await importOriginal<typeof import("../agents/model-selection.js")>()),
25+
resolveDefaultModelForAgent: mocks.resolveDefaultModelForAgent,
26+
}));
27+
28+
vi.mock("../agents/agent-bundle-mcp-tools.js", () => ({
29+
createBundleMcpToolRuntime: mocks.createBundleMcpToolRuntime,
30+
}));
31+
32+
vi.mock("../agents/agent-tools.js", () => ({
33+
createOpenClawCodingTools: mocks.createOpenClawCodingTools,
34+
}));
35+
36+
vi.mock("../plugins/provider-runtime.js", () => ({
37+
inspectProviderToolSchemasWithPlugin: () => [],
38+
normalizeProviderToolSchemasWithPlugin: mocks.normalizeProviderToolSchemasWithPlugin,
39+
}));
40+
41+
const { collectRuntimeToolSchemaFindings } = await import("./doctor-core-checks.runtime.js");
42+
43+
function tool(name: string, parameters: unknown): AnyAgentTool {
44+
return {
45+
name,
46+
label: name,
47+
description: name,
48+
parameters,
49+
execute: async () => ({ text: "ok" }),
50+
} as unknown as AnyAgentTool;
51+
}
52+
53+
function bundleMcpTool(name: string, parameters: unknown): AnyAgentTool {
54+
const entry = tool(name, parameters);
55+
setPluginToolMeta(entry, { pluginId: "bundle-mcp", optional: false });
56+
return entry;
57+
}
58+
59+
describe("doctor runtime tool schema error handling", () => {
60+
beforeEach(() => {
61+
mocks.createOpenClawCodingTools.mockReset().mockReturnValue([]);
62+
mocks.createBundleMcpToolRuntime.mockReset().mockResolvedValue({
63+
tools: [],
64+
dispose: mocks.disposeBundleRuntime,
65+
});
66+
mocks.disposeBundleRuntime.mockReset().mockResolvedValue(undefined);
67+
mocks.loadModelCatalog.mockClear();
68+
mocks.normalizeProviderToolSchemasWithPlugin
69+
.mockReset()
70+
.mockImplementation(({ context }) => context.tools);
71+
mocks.resolveDefaultModelForAgent.mockClear();
72+
});
73+
74+
it("reports agent runtime tool construction failures without aborting schema checks", async () => {
75+
mocks.createOpenClawCodingTools.mockImplementationOnce(() => {
76+
throw new Error("fuzzplugin startup failed");
77+
});
78+
79+
await expect(collectRuntimeToolSchemaFindings({})).resolves.toContainEqual({
80+
checkId: "core/doctor/runtime-tool-schemas",
81+
severity: "error",
82+
message: "Agent main runtime tool schema validation could not load the runtime tool set.",
83+
path: "agents.main.tools",
84+
requirement: "fuzzplugin startup failed",
85+
fixHint:
86+
"Fix provider/plugin tool loading errors, then rerun doctor before relying on assistant tool startup.",
87+
});
88+
expect(mocks.createBundleMcpToolRuntime).toHaveBeenCalledTimes(1);
89+
expect(mocks.disposeBundleRuntime).toHaveBeenCalledTimes(1);
90+
});
91+
92+
it("reports agent runtime tool normalization failures without aborting doctor", async () => {
93+
mocks.createOpenClawCodingTools.mockReturnValueOnce([
94+
tool("fuzzplugin_move_angles", { type: "object", properties: {} }),
95+
]);
96+
mocks.normalizeProviderToolSchemasWithPlugin.mockImplementation(({ context }) => {
97+
const tools = context.tools as AnyAgentTool[];
98+
if (tools.some((entry) => entry.name === "fuzzplugin_move_angles")) {
99+
throw new Error("fuzzplugin schema normalization failed");
100+
}
101+
return tools;
102+
});
103+
104+
await expect(collectRuntimeToolSchemaFindings({})).resolves.toContainEqual({
105+
checkId: "core/doctor/runtime-tool-schemas",
106+
severity: "error",
107+
message:
108+
"Agent main runtime tool schema validation could not normalize the runtime tool set.",
109+
path: "agents.main.tools",
110+
requirement: "fuzzplugin schema normalization failed",
111+
fixHint:
112+
"Fix provider/plugin schema normalization errors, then rerun doctor before relying on assistant tool startup.",
113+
});
114+
expect(mocks.disposeBundleRuntime).toHaveBeenCalledTimes(1);
115+
});
116+
117+
it("reports bundle MCP runtime tool normalization failures without aborting doctor", async () => {
118+
mocks.createBundleMcpToolRuntime.mockResolvedValueOnce({
119+
tools: [bundleMcpTool("fuzzplugin__move_angles", { type: "object", properties: {} })],
120+
dispose: mocks.disposeBundleRuntime,
121+
});
122+
mocks.normalizeProviderToolSchemasWithPlugin.mockImplementation(({ context }) => {
123+
const tools = context.tools as AnyAgentTool[];
124+
if (tools.some((entry) => entry.name === "fuzzplugin__move_angles")) {
125+
throw new Error("fuzzplugin MCP schema normalization failed");
126+
}
127+
return tools;
128+
});
129+
130+
await expect(collectRuntimeToolSchemaFindings({})).resolves.toContainEqual({
131+
checkId: "core/doctor/runtime-tool-schemas",
132+
severity: "error",
133+
message: "Configured MCP tool schema validation could not normalize the runtime tool set.",
134+
path: "mcp.servers",
135+
requirement: "fuzzplugin MCP schema normalization failed",
136+
fixHint:
137+
"Fix provider/plugin schema normalization errors, then rerun doctor before relying on assistant tool startup.",
138+
});
139+
expect(mocks.disposeBundleRuntime).toHaveBeenCalledTimes(1);
140+
});
141+
});

‎src/flows/doctor-core-checks.runtime.ts‎

Lines changed: 116 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,36 @@ function collectBundleMcpRuntimeToolSchemaFindings(params: {
647647
];
648648
}
649649

650+
function agentRuntimeToolLoadFailureFinding(params: {
651+
agentId: string;
652+
error: unknown;
653+
}): HealthFinding {
654+
return {
655+
checkId: "core/doctor/runtime-tool-schemas",
656+
severity: "error",
657+
message: `Agent ${params.agentId} runtime tool schema validation could not load the runtime tool set.`,
658+
path: `agents.${params.agentId}.tools`,
659+
requirement: formatErrorMessage(params.error),
660+
fixHint:
661+
"Fix provider/plugin tool loading errors, then rerun doctor before relying on assistant tool startup.",
662+
};
663+
}
664+
665+
function agentRuntimeToolNormalizationFailureFinding(params: {
666+
agentId: string;
667+
error: unknown;
668+
}): HealthFinding {
669+
return {
670+
checkId: "core/doctor/runtime-tool-schemas",
671+
severity: "error",
672+
message: `Agent ${params.agentId} runtime tool schema validation could not normalize the runtime tool set.`,
673+
path: `agents.${params.agentId}.tools`,
674+
requirement: formatErrorMessage(params.error),
675+
fixHint:
676+
"Fix provider/plugin schema normalization errors, then rerun doctor before relying on assistant tool startup.",
677+
};
678+
}
679+
650680
function bundleMcpRuntimeLoadFailureFinding(error: unknown): HealthFinding {
651681
return {
652682
checkId: "core/doctor/runtime-tool-schemas",
@@ -659,6 +689,73 @@ function bundleMcpRuntimeLoadFailureFinding(error: unknown): HealthFinding {
659689
};
660690
}
661691

692+
function bundleMcpRuntimeNormalizationFailureFinding(error: unknown): HealthFinding {
693+
return {
694+
checkId: "core/doctor/runtime-tool-schemas",
695+
severity: "error",
696+
message: "Configured MCP tool schema validation could not normalize the runtime tool set.",
697+
path: "mcp.servers",
698+
requirement: formatErrorMessage(error),
699+
fixHint:
700+
"Fix provider/plugin schema normalization errors, then rerun doctor before relying on assistant tool startup.",
701+
};
702+
}
703+
704+
function collectAgentRuntimeToolSchemaFindings(params: {
705+
cfg: OpenClawConfig;
706+
agentId: string;
707+
workspaceDir: string;
708+
modelRef: { provider: string; model: string };
709+
model: ProviderRuntimeModel;
710+
}): readonly HealthFinding[] {
711+
let tools: AnyAgentTool[];
712+
try {
713+
tools = createOpenClawCodingTools({
714+
agentId: params.agentId,
715+
workspaceDir: params.workspaceDir,
716+
config: params.cfg,
717+
modelProvider: params.modelRef.provider,
718+
modelId: params.modelRef.model,
719+
modelApi: params.model.api,
720+
modelCompat: params.model.compat,
721+
modelContextWindowTokens: params.model.contextWindow,
722+
allowGatewaySubagentBinding: true,
723+
emitBeforeToolCallDiagnostics: false,
724+
});
725+
} catch (error) {
726+
return [agentRuntimeToolLoadFailureFinding({ agentId: params.agentId, error })];
727+
}
728+
729+
const activeToolProjection = inspectProviderNormalizableTools({
730+
agentId: params.agentId,
731+
tools,
732+
});
733+
734+
let normalizedTools: AnyAgentTool[];
735+
try {
736+
normalizedTools = normalizeAgentRuntimeTools({
737+
tools: [...activeToolProjection.tools],
738+
provider: params.modelRef.provider,
739+
config: params.cfg,
740+
workspaceDir: params.workspaceDir,
741+
env: process.env,
742+
modelId: params.modelRef.model,
743+
modelApi: params.model.api,
744+
model: params.model,
745+
});
746+
} catch (error) {
747+
return [agentRuntimeToolNormalizationFailureFinding({ agentId: params.agentId, error })];
748+
}
749+
750+
return [
751+
...activeToolProjection.findings,
752+
...collectToolSchemaFindings({
753+
agentId: params.agentId,
754+
tools: normalizedTools,
755+
}),
756+
];
757+
}
758+
662759
function bundleMcpRuntimeDiagnosticFinding(diagnostic: McpToolCatalogDiagnostic): HealthFinding {
663760
return {
664761
checkId: "core/doctor/runtime-tool-schemas",
@@ -815,37 +912,13 @@ export async function collectRuntimeToolSchemaFindings(
815912
if (!supportsModelTools(model)) {
816913
continue;
817914
}
818-
const tools = createOpenClawCodingTools({
819-
agentId,
820-
workspaceDir,
821-
config: cfg,
822-
modelProvider: modelRef.provider,
823-
modelId: modelRef.model,
824-
modelApi: model.api,
825-
modelCompat: model.compat,
826-
modelContextWindowTokens: model.contextWindow,
827-
allowGatewaySubagentBinding: true,
828-
emitBeforeToolCallDiagnostics: false,
829-
});
830-
const activeToolProjection = inspectProviderNormalizableTools({
831-
agentId,
832-
tools,
833-
});
834-
const normalizedTools = normalizeAgentRuntimeTools({
835-
tools: [...activeToolProjection.tools],
836-
provider: modelRef.provider,
837-
config: cfg,
838-
workspaceDir,
839-
env: process.env,
840-
modelId: modelRef.model,
841-
modelApi: model.api,
842-
model,
843-
});
844915
findings.push(
845-
...activeToolProjection.findings,
846-
...collectToolSchemaFindings({
916+
...collectAgentRuntimeToolSchemaFindings({
917+
cfg,
847918
agentId,
848-
tools: normalizedTools,
919+
workspaceDir,
920+
modelRef,
921+
model,
849922
}),
850923
);
851924
if (!shouldCreateBundleMcpRuntimeForAttempt({ toolsEnabled: true })) {
@@ -889,16 +962,20 @@ export async function collectRuntimeToolSchemaFindings(
889962
});
890963
findings.push(...policyActiveDiagnostics.map(bundleMcpRuntimeDiagnosticFinding));
891964
}
892-
findings.push(
893-
...collectBundleMcpRuntimeToolSchemaFindings({
894-
bundleRuntime,
895-
cfg,
896-
agentId,
897-
workspaceDir,
898-
modelRef,
899-
model,
900-
}),
901-
);
965+
try {
966+
findings.push(
967+
...collectBundleMcpRuntimeToolSchemaFindings({
968+
bundleRuntime,
969+
cfg,
970+
agentId,
971+
workspaceDir,
972+
modelRef,
973+
model,
974+
}),
975+
);
976+
} catch (error) {
977+
findings.push(bundleMcpRuntimeNormalizationFailureFinding(error));
978+
}
902979
}
903980
}
904981
} finally {

0 commit comments

Comments
 (0)