Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Adolf is a fork/vendored clone of github.com/openclaw/openclaw (v2026.6.11), free to diverge. Tree copied sans upstream .git; upstream remote added for future syncs. Node pinned to 24 (.nvmrc); engines already require >=22.19. Preserves docs/ARCHITECTURE.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LeqyaxJF2nbRXJtae2kNB2
292 lines
9.4 KiB
TypeScript
292 lines
9.4 KiB
TypeScript
// Mcp Code Mode Gateway E2E script supports OpenClaw repository automation.
|
|
import fs from "node:fs/promises";
|
|
import net from "node:net";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import process from "node:process";
|
|
import { setTimeout as setNodeTimeout, clearTimeout as clearNodeTimeout } from "node:timers";
|
|
import { pathToFileURL } from "node:url";
|
|
import { startQaMockOpenAiServer } from "../extensions/qa-lab/src/providers/mock-openai/server.js";
|
|
import { stageQaMockAuthProfiles } from "../extensions/qa-lab/src/providers/shared/mock-auth.js";
|
|
import { buildQaGatewayConfig } from "../extensions/qa-lab/src/qa-gateway-config.js";
|
|
import { resetConfigRuntimeState } from "../src/config/config.js";
|
|
import { startGatewayServer } from "../src/gateway/server.js";
|
|
import { deleteTestEnvValue, setTestEnvValue } from "../src/test-utils/env.js";
|
|
import { writeProbeMcpServer } from "./e2e/lib/mcp-code-mode-probe-server.ts";
|
|
import {
|
|
type McpCodeModeMentions,
|
|
validateMcpCodeModeResult,
|
|
} from "./e2e/lib/mcp-code-mode-validation.ts";
|
|
import { countSessionLogMentions } from "./e2e/lib/session-log-mentions.ts";
|
|
import { readBoundedResponseText } from "./lib/bounded-response.ts";
|
|
|
|
async function freePort(): Promise<number> {
|
|
return await new Promise((resolve, reject) => {
|
|
const server = net.createServer();
|
|
server.once("error", reject);
|
|
server.listen(0, "127.0.0.1", () => {
|
|
const address = server.address();
|
|
const port = typeof address === "object" && address ? address.port : 0;
|
|
server.close((error) => (error ? reject(error) : resolve(port)));
|
|
});
|
|
});
|
|
}
|
|
|
|
async function fetchJson(url: string, init: RequestInit = {}): Promise<unknown> {
|
|
const timeoutMs = 180_000;
|
|
const controller = new AbortController();
|
|
let timeout: ReturnType<typeof setNodeTimeout> | undefined;
|
|
const timeoutPromise = new Promise<never>((_, reject) => {
|
|
timeout = setNodeTimeout(() => {
|
|
const error = Object.assign(new Error(`HTTP request to ${url} timed out`), {
|
|
code: "ETIMEDOUT",
|
|
});
|
|
controller.abort(error);
|
|
reject(error);
|
|
}, timeoutMs);
|
|
});
|
|
try {
|
|
const response = await Promise.race([
|
|
fetch(url, { ...init, signal: controller.signal }),
|
|
timeoutPromise,
|
|
]);
|
|
const text = await readBoundedResponseText(response, url, 1024 * 1024, {
|
|
createTooLargeError(message) {
|
|
return Object.assign(new Error(message), { code: "ETOOBIG" });
|
|
},
|
|
formatTooLargeMessage(targetUrl, byteLimit) {
|
|
return `HTTP response from ${targetUrl} exceeded ${byteLimit} bytes`;
|
|
},
|
|
timeoutPromise,
|
|
signal: controller.signal,
|
|
});
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP ${response.status} from ${url}: ${text}`);
|
|
}
|
|
return text ? JSON.parse(text) : {};
|
|
} finally {
|
|
if (timeout) {
|
|
clearNodeTimeout(timeout);
|
|
}
|
|
}
|
|
}
|
|
|
|
async function readSessionLogMentions(stateDir: string): Promise<Record<string, number>> {
|
|
const sessionsDir = path.join(stateDir, "agents", "qa", "sessions");
|
|
return await countSessionLogMentions({
|
|
sessionsDir,
|
|
needles: {
|
|
apiCall: "MCP.$api",
|
|
apiFileList: "API.list",
|
|
apiFileRead: "API.read",
|
|
mcpNamespace: "MCP.fixture",
|
|
mcpTool: "fixture__lookup_note",
|
|
toolSearchPollution: 'tools.search("lookup note"',
|
|
},
|
|
});
|
|
}
|
|
|
|
function restoreEnvValue(key: string, value: string | undefined): void {
|
|
if (value === undefined) {
|
|
deleteTestEnvValue(key);
|
|
} else {
|
|
setTestEnvValue(key, value);
|
|
}
|
|
}
|
|
|
|
async function writeConfig(params: {
|
|
configPath: string;
|
|
stateDir: string;
|
|
workspaceDir: string;
|
|
gatewayPort: number;
|
|
providerBaseUrl: string;
|
|
serverPath: string;
|
|
}) {
|
|
let cfg = buildQaGatewayConfig({
|
|
bind: "loopback",
|
|
gatewayPort: params.gatewayPort,
|
|
gatewayToken: "mcp-code-mode-e2e",
|
|
providerBaseUrl: `${params.providerBaseUrl}/v1`,
|
|
workspaceDir: params.workspaceDir,
|
|
controlUiEnabled: false,
|
|
providerMode: "mock-openai",
|
|
});
|
|
cfg = {
|
|
...cfg,
|
|
plugins: {
|
|
...cfg.plugins,
|
|
slots: {
|
|
...cfg.plugins?.slots,
|
|
memory: "none",
|
|
},
|
|
},
|
|
agents: {
|
|
...cfg.agents,
|
|
defaults: {
|
|
...cfg.agents?.defaults,
|
|
memorySearch: {
|
|
...cfg.agents?.defaults?.memorySearch,
|
|
enabled: false,
|
|
sync: {
|
|
...cfg.agents?.defaults?.memorySearch?.sync,
|
|
onSearch: false,
|
|
onSessionStart: false,
|
|
watch: false,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
tools: {
|
|
...cfg.tools,
|
|
alsoAllow: [...new Set([...(cfg.tools?.alsoAllow ?? []), "bundle-mcp"])],
|
|
codeMode: {
|
|
enabled: true,
|
|
timeoutMs: 20_000,
|
|
maxPendingToolCalls: 16,
|
|
},
|
|
},
|
|
mcp: {
|
|
servers: {
|
|
fixture: {
|
|
command: "node",
|
|
args: [params.serverPath],
|
|
cwd: path.dirname(params.serverPath),
|
|
connectionTimeoutMs: 30_000,
|
|
},
|
|
},
|
|
},
|
|
gateway: {
|
|
...cfg.gateway,
|
|
http: {
|
|
endpoints: {
|
|
responses: {
|
|
enabled: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
};
|
|
cfg = await stageQaMockAuthProfiles({
|
|
cfg,
|
|
stateDir: params.stateDir,
|
|
agentIds: ["qa"],
|
|
providers: ["mock-openai", "openai", "anthropic"],
|
|
});
|
|
await fs.mkdir(path.dirname(params.configPath), { recursive: true });
|
|
await fs.writeFile(params.configPath, `${JSON.stringify(cfg, null, 2)}\n`, "utf8");
|
|
}
|
|
|
|
export async function main() {
|
|
const rootDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-mcp-code-mode-"));
|
|
const keep = process.env.OPENCLAW_MCP_CODE_MODE_GATEWAY_E2E_KEEP === "1";
|
|
const previousEnv = {
|
|
configPath: process.env.OPENCLAW_CONFIG_PATH,
|
|
stateDir: process.env.OPENCLAW_STATE_DIR,
|
|
testFast: process.env.OPENCLAW_TEST_FAST,
|
|
};
|
|
let provider: Awaited<ReturnType<typeof startQaMockOpenAiServer>> | undefined;
|
|
let server: Awaited<ReturnType<typeof startGatewayServer>> | undefined;
|
|
try {
|
|
provider = await startQaMockOpenAiServer();
|
|
const stateDir = path.join(rootDir, "state");
|
|
const workspaceDir = path.join(rootDir, "workspace");
|
|
const serverPath = path.join(rootDir, "mcp", "fixture-server.mjs");
|
|
const configPath = path.join(stateDir, "openclaw.json");
|
|
const gatewayPort = await freePort();
|
|
await fs.mkdir(workspaceDir, { recursive: true });
|
|
await writeProbeMcpServer(serverPath);
|
|
await writeConfig({
|
|
configPath,
|
|
stateDir,
|
|
workspaceDir,
|
|
gatewayPort,
|
|
providerBaseUrl: provider.baseUrl,
|
|
serverPath,
|
|
});
|
|
|
|
setTestEnvValue("OPENCLAW_STATE_DIR", stateDir);
|
|
setTestEnvValue("OPENCLAW_CONFIG_PATH", configPath);
|
|
setTestEnvValue("OPENCLAW_TEST_FAST", "1");
|
|
resetConfigRuntimeState();
|
|
|
|
server = await startGatewayServer(gatewayPort, {
|
|
host: "127.0.0.1",
|
|
auth: { mode: "none" },
|
|
controlUiEnabled: false,
|
|
openResponsesEnabled: true,
|
|
});
|
|
|
|
const beforeRequests = (await fetchJson(`${provider.baseUrl}/debug/requests`)) as unknown[];
|
|
const response = await fetchJson(`http://127.0.0.1:${gatewayPort}/v1/responses`, {
|
|
method: "POST",
|
|
headers: {
|
|
"content-type": "application/json",
|
|
"x-openclaw-scopes": "operator.write",
|
|
"x-openclaw-agent": "qa",
|
|
},
|
|
body: JSON.stringify({
|
|
model: "openclaw/qa",
|
|
input: [
|
|
{
|
|
type: "message",
|
|
role: "user",
|
|
content: [
|
|
{
|
|
type: "input_text",
|
|
text: "mcp code mode api file qa check: inspect the MCP TypeScript declaration files through API.read, call the fixture lookup_note tool for alpha, and return the note text plus what was unclear.",
|
|
},
|
|
],
|
|
},
|
|
],
|
|
max_output_tokens: 256,
|
|
stream: false,
|
|
}),
|
|
});
|
|
const requests = (await fetchJson(`${provider.baseUrl}/debug/requests`)) as Array<{
|
|
raw?: string;
|
|
body?: { tools?: unknown[] };
|
|
plannedToolName?: string;
|
|
}>;
|
|
const laneRequests = requests.slice(beforeRequests.length);
|
|
const firstRequest = laneRequests[0] ?? {};
|
|
const mentions = await readSessionLogMentions(stateDir);
|
|
const plannedTools = laneRequests
|
|
.map((request) => request.plannedToolName)
|
|
.filter((name): name is string => typeof name === "string");
|
|
const finalText = validateMcpCodeModeResult(response, mentions as McpCodeModeMentions, {
|
|
plannedTools,
|
|
requireExec: true,
|
|
});
|
|
|
|
const summary = {
|
|
ok: true,
|
|
rootDir,
|
|
stateDir,
|
|
gatewayUrl: `http://127.0.0.1:${gatewayPort}`,
|
|
finalText,
|
|
providerRequestCount: laneRequests.length,
|
|
providerDeclaredToolCount: Array.isArray(firstRequest.body?.tools)
|
|
? firstRequest.body.tools.length
|
|
: 0,
|
|
providerRawBytes: typeof firstRequest.raw === "string" ? firstRequest.raw.length : 0,
|
|
plannedTools,
|
|
sessionLogMentions: mentions,
|
|
};
|
|
process.stdout.write(`${JSON.stringify(summary, null, 2)}\n`);
|
|
} finally {
|
|
await server?.close({ reason: "mcp code-mode gateway e2e complete" });
|
|
await provider?.stop();
|
|
resetConfigRuntimeState();
|
|
restoreEnvValue("OPENCLAW_STATE_DIR", previousEnv.stateDir);
|
|
restoreEnvValue("OPENCLAW_CONFIG_PATH", previousEnv.configPath);
|
|
restoreEnvValue("OPENCLAW_TEST_FAST", previousEnv.testFast);
|
|
if (!keep) {
|
|
await fs.rm(rootDir, { recursive: true, force: true });
|
|
}
|
|
}
|
|
}
|
|
|
|
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
await main();
|
|
}
|