mirror of
https://github.com/farcasclaudiu/openclaw.git
synced 2026-06-29 07:01:40 +03:00
perf(test): minimize gateway startup in vitest
This commit is contained in:
+67
-11
@@ -1,5 +1,6 @@
|
|||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import type { CanvasHostServer } from "../canvas-host/server.js";
|
import type { CanvasHostServer } from "../canvas-host/server.js";
|
||||||
|
import type { PluginRegistry } from "../plugins/registry.js";
|
||||||
import type { PluginServicesHandle } from "../plugins/services.js";
|
import type { PluginServicesHandle } from "../plugins/services.js";
|
||||||
import type { RuntimeEnv } from "../runtime.js";
|
import type { RuntimeEnv } from "../runtime.js";
|
||||||
import type { ControlUiRootState } from "./control-ui.js";
|
import type { ControlUiRootState } from "./control-ui.js";
|
||||||
@@ -31,7 +32,7 @@ import { isDiagnosticsEnabled } from "../infra/diagnostic-events.js";
|
|||||||
import { logAcceptedEnvOption } from "../infra/env.js";
|
import { logAcceptedEnvOption } from "../infra/env.js";
|
||||||
import { createExecApprovalForwarder } from "../infra/exec-approval-forwarder.js";
|
import { createExecApprovalForwarder } from "../infra/exec-approval-forwarder.js";
|
||||||
import { onHeartbeatEvent } from "../infra/heartbeat-events.js";
|
import { onHeartbeatEvent } from "../infra/heartbeat-events.js";
|
||||||
import { startHeartbeatRunner } from "../infra/heartbeat-runner.js";
|
import { startHeartbeatRunner, type HeartbeatRunner } from "../infra/heartbeat-runner.js";
|
||||||
import { getMachineDisplayName } from "../infra/machine-name.js";
|
import { getMachineDisplayName } from "../infra/machine-name.js";
|
||||||
import { ensureOpenClawCliOnPath } from "../infra/path-env.js";
|
import { ensureOpenClawCliOnPath } from "../infra/path-env.js";
|
||||||
import { setGatewaySigusr1RestartPolicy, setPreRestartDeferralCheck } from "../infra/restart.js";
|
import { setGatewaySigusr1RestartPolicy, setPreRestartDeferralCheck } from "../infra/restart.js";
|
||||||
@@ -161,6 +162,9 @@ export async function startGatewayServer(
|
|||||||
port = 18789,
|
port = 18789,
|
||||||
opts: GatewayServerOptions = {},
|
opts: GatewayServerOptions = {},
|
||||||
): Promise<GatewayServer> {
|
): Promise<GatewayServer> {
|
||||||
|
const minimalTestGateway =
|
||||||
|
process.env.VITEST === "1" && process.env.OPENCLAW_TEST_MINIMAL_GATEWAY === "1";
|
||||||
|
|
||||||
// Ensure all default port derivations (browser/canvas) see the actual runtime port.
|
// Ensure all default port derivations (browser/canvas) see the actual runtime port.
|
||||||
process.env.OPENCLAW_GATEWAY_PORT = String(port);
|
process.env.OPENCLAW_GATEWAY_PORT = String(port);
|
||||||
logAcceptedEnvOption({
|
logAcceptedEnvOption({
|
||||||
@@ -235,7 +239,24 @@ export async function startGatewayServer(
|
|||||||
const defaultAgentId = resolveDefaultAgentId(cfgAtStart);
|
const defaultAgentId = resolveDefaultAgentId(cfgAtStart);
|
||||||
const defaultWorkspaceDir = resolveAgentWorkspaceDir(cfgAtStart, defaultAgentId);
|
const defaultWorkspaceDir = resolveAgentWorkspaceDir(cfgAtStart, defaultAgentId);
|
||||||
const baseMethods = listGatewayMethods();
|
const baseMethods = listGatewayMethods();
|
||||||
const { pluginRegistry, gatewayMethods: baseGatewayMethods } = loadGatewayPlugins({
|
const emptyPluginRegistry: PluginRegistry = {
|
||||||
|
plugins: [],
|
||||||
|
tools: [],
|
||||||
|
hooks: [],
|
||||||
|
typedHooks: [],
|
||||||
|
channels: [],
|
||||||
|
providers: [],
|
||||||
|
gatewayHandlers: {},
|
||||||
|
httpHandlers: [],
|
||||||
|
httpRoutes: [],
|
||||||
|
cliRegistrars: [],
|
||||||
|
services: [],
|
||||||
|
commands: [],
|
||||||
|
diagnostics: [],
|
||||||
|
};
|
||||||
|
const { pluginRegistry, gatewayMethods: baseGatewayMethods } = minimalTestGateway
|
||||||
|
? { pluginRegistry: emptyPluginRegistry, gatewayMethods: baseMethods }
|
||||||
|
: loadGatewayPlugins({
|
||||||
cfg: cfgAtStart,
|
cfg: cfgAtStart,
|
||||||
workspaceDir: defaultWorkspaceDir,
|
workspaceDir: defaultWorkspaceDir,
|
||||||
log,
|
log,
|
||||||
@@ -402,6 +423,7 @@ export async function startGatewayServer(
|
|||||||
const { getRuntimeSnapshot, startChannels, startChannel, stopChannel, markChannelLoggedOut } =
|
const { getRuntimeSnapshot, startChannels, startChannel, stopChannel, markChannelLoggedOut } =
|
||||||
channelManager;
|
channelManager;
|
||||||
|
|
||||||
|
if (!minimalTestGateway) {
|
||||||
const machineDisplayName = await getMachineDisplayName();
|
const machineDisplayName = await getMachineDisplayName();
|
||||||
const discovery = await startGatewayDiscovery({
|
const discovery = await startGatewayDiscovery({
|
||||||
machineDisplayName,
|
machineDisplayName,
|
||||||
@@ -416,15 +438,20 @@ export async function startGatewayServer(
|
|||||||
logDiscovery,
|
logDiscovery,
|
||||||
});
|
});
|
||||||
bonjourStop = discovery.bonjourStop;
|
bonjourStop = discovery.bonjourStop;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!minimalTestGateway) {
|
||||||
setSkillsRemoteRegistry(nodeRegistry);
|
setSkillsRemoteRegistry(nodeRegistry);
|
||||||
void primeRemoteSkillsCache();
|
void primeRemoteSkillsCache();
|
||||||
|
}
|
||||||
// Debounce skills-triggered node probes to avoid feedback loops and rapid-fire invokes.
|
// Debounce skills-triggered node probes to avoid feedback loops and rapid-fire invokes.
|
||||||
// Skills changes can happen in bursts (e.g., file watcher events), and each probe
|
// Skills changes can happen in bursts (e.g., file watcher events), and each probe
|
||||||
// takes time to complete. A 30-second delay ensures we batch changes together.
|
// takes time to complete. A 30-second delay ensures we batch changes together.
|
||||||
let skillsRefreshTimer: ReturnType<typeof setTimeout> | null = null;
|
let skillsRefreshTimer: ReturnType<typeof setTimeout> | null = null;
|
||||||
const skillsRefreshDelayMs = 30_000;
|
const skillsRefreshDelayMs = 30_000;
|
||||||
const skillsChangeUnsub = registerSkillsChangeListener((event) => {
|
const skillsChangeUnsub = minimalTestGateway
|
||||||
|
? () => {}
|
||||||
|
: registerSkillsChangeListener((event) => {
|
||||||
if (event.reason === "remote-node") {
|
if (event.reason === "remote-node") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -438,7 +465,12 @@ export async function startGatewayServer(
|
|||||||
}, skillsRefreshDelayMs);
|
}, skillsRefreshDelayMs);
|
||||||
});
|
});
|
||||||
|
|
||||||
const { tickInterval, healthInterval, dedupeCleanup } = startGatewayMaintenanceTimers({
|
const noopInterval = () => setInterval(() => {}, 1 << 30);
|
||||||
|
let tickInterval = noopInterval();
|
||||||
|
let healthInterval = noopInterval();
|
||||||
|
let dedupeCleanup = noopInterval();
|
||||||
|
if (!minimalTestGateway) {
|
||||||
|
({ tickInterval, healthInterval, dedupeCleanup } = startGatewayMaintenanceTimers({
|
||||||
broadcast,
|
broadcast,
|
||||||
nodeSendToAllSubscribed,
|
nodeSendToAllSubscribed,
|
||||||
getPresenceVersion,
|
getPresenceVersion,
|
||||||
@@ -453,9 +485,12 @@ export async function startGatewayServer(
|
|||||||
removeChatRun,
|
removeChatRun,
|
||||||
agentRunSeq,
|
agentRunSeq,
|
||||||
nodeSendToSession,
|
nodeSendToSession,
|
||||||
});
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
const agentUnsub = onAgentEvent(
|
const agentUnsub = minimalTestGateway
|
||||||
|
? null
|
||||||
|
: onAgentEvent(
|
||||||
createAgentEventHandler({
|
createAgentEventHandler({
|
||||||
broadcast,
|
broadcast,
|
||||||
broadcastToConnIds,
|
broadcastToConnIds,
|
||||||
@@ -468,15 +503,25 @@ export async function startGatewayServer(
|
|||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
const heartbeatUnsub = onHeartbeatEvent((evt) => {
|
const heartbeatUnsub = minimalTestGateway
|
||||||
|
? null
|
||||||
|
: onHeartbeatEvent((evt) => {
|
||||||
broadcast("heartbeat", evt, { dropIfSlow: true });
|
broadcast("heartbeat", evt, { dropIfSlow: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
let heartbeatRunner = startHeartbeatRunner({ cfg: cfgAtStart });
|
let heartbeatRunner: HeartbeatRunner = minimalTestGateway
|
||||||
|
? {
|
||||||
|
stop: () => {},
|
||||||
|
updateConfig: () => {},
|
||||||
|
}
|
||||||
|
: startHeartbeatRunner({ cfg: cfgAtStart });
|
||||||
|
|
||||||
|
if (!minimalTestGateway) {
|
||||||
void cron.start().catch((err) => logCron.error(`failed to start: ${String(err)}`));
|
void cron.start().catch((err) => logCron.error(`failed to start: ${String(err)}`));
|
||||||
|
}
|
||||||
|
|
||||||
// Recover pending outbound deliveries from previous crash/restart.
|
// Recover pending outbound deliveries from previous crash/restart.
|
||||||
|
if (!minimalTestGateway) {
|
||||||
void (async () => {
|
void (async () => {
|
||||||
const { recoverPendingDeliveries } = await import("../infra/outbound/delivery-queue.js");
|
const { recoverPendingDeliveries } = await import("../infra/outbound/delivery-queue.js");
|
||||||
const { deliverOutboundPayloads } = await import("../infra/outbound/deliver.js");
|
const { deliverOutboundPayloads } = await import("../infra/outbound/deliver.js");
|
||||||
@@ -487,6 +532,7 @@ export async function startGatewayServer(
|
|||||||
cfg: cfgAtStart,
|
cfg: cfgAtStart,
|
||||||
});
|
});
|
||||||
})().catch((err) => log.error(`Delivery recovery failed: ${String(err)}`));
|
})().catch((err) => log.error(`Delivery recovery failed: ${String(err)}`));
|
||||||
|
}
|
||||||
|
|
||||||
const execApprovalManager = new ExecApprovalManager();
|
const execApprovalManager = new ExecApprovalManager();
|
||||||
const execApprovalForwarder = createExecApprovalForwarder();
|
const execApprovalForwarder = createExecApprovalForwarder();
|
||||||
@@ -564,8 +610,12 @@ export async function startGatewayServer(
|
|||||||
log,
|
log,
|
||||||
isNixMode,
|
isNixMode,
|
||||||
});
|
});
|
||||||
|
if (!minimalTestGateway) {
|
||||||
scheduleGatewayUpdateCheck({ cfg: cfgAtStart, log, isNixMode });
|
scheduleGatewayUpdateCheck({ cfg: cfgAtStart, log, isNixMode });
|
||||||
const tailscaleCleanup = await startGatewayTailscaleExposure({
|
}
|
||||||
|
const tailscaleCleanup = minimalTestGateway
|
||||||
|
? null
|
||||||
|
: await startGatewayTailscaleExposure({
|
||||||
tailscaleMode,
|
tailscaleMode,
|
||||||
resetOnExit: tailscaleConfig.resetOnExit,
|
resetOnExit: tailscaleConfig.resetOnExit,
|
||||||
port,
|
port,
|
||||||
@@ -574,6 +624,7 @@ export async function startGatewayServer(
|
|||||||
});
|
});
|
||||||
|
|
||||||
let browserControl: Awaited<ReturnType<typeof startBrowserControlServerIfEnabled>> = null;
|
let browserControl: Awaited<ReturnType<typeof startBrowserControlServerIfEnabled>> = null;
|
||||||
|
if (!minimalTestGateway) {
|
||||||
({ browserControl, pluginServices } = await startGatewaySidecars({
|
({ browserControl, pluginServices } = await startGatewaySidecars({
|
||||||
cfg: cfgAtStart,
|
cfg: cfgAtStart,
|
||||||
pluginRegistry,
|
pluginRegistry,
|
||||||
@@ -585,9 +636,10 @@ export async function startGatewayServer(
|
|||||||
logChannels,
|
logChannels,
|
||||||
logBrowser,
|
logBrowser,
|
||||||
}));
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
// Run gateway_start plugin hook (fire-and-forget)
|
// Run gateway_start plugin hook (fire-and-forget)
|
||||||
{
|
if (!minimalTestGateway) {
|
||||||
const hookRunner = getGlobalHookRunner();
|
const hookRunner = getGlobalHookRunner();
|
||||||
if (hookRunner?.hasHooks("gateway_start")) {
|
if (hookRunner?.hasHooks("gateway_start")) {
|
||||||
void hookRunner.runGatewayStart({ port }, { port }).catch((err) => {
|
void hookRunner.runGatewayStart({ port }, { port }).catch((err) => {
|
||||||
@@ -596,6 +648,9 @@ export async function startGatewayServer(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const configReloader = minimalTestGateway
|
||||||
|
? { stop: async () => {} }
|
||||||
|
: (() => {
|
||||||
const { applyHotReload, requestGatewayRestart } = createGatewayReloadHandlers({
|
const { applyHotReload, requestGatewayRestart } = createGatewayReloadHandlers({
|
||||||
deps,
|
deps,
|
||||||
broadcast,
|
broadcast,
|
||||||
@@ -622,7 +677,7 @@ export async function startGatewayServer(
|
|||||||
logReload,
|
logReload,
|
||||||
});
|
});
|
||||||
|
|
||||||
const configReloader = startGatewayConfigReloader({
|
return startGatewayConfigReloader({
|
||||||
initialConfig: cfgAtStart,
|
initialConfig: cfgAtStart,
|
||||||
readSnapshot: readConfigFileSnapshot,
|
readSnapshot: readConfigFileSnapshot,
|
||||||
onHotReload: applyHotReload,
|
onHotReload: applyHotReload,
|
||||||
@@ -634,6 +689,7 @@ export async function startGatewayServer(
|
|||||||
},
|
},
|
||||||
watchPath: CONFIG_PATH,
|
watchPath: CONFIG_PATH,
|
||||||
});
|
});
|
||||||
|
})();
|
||||||
|
|
||||||
const close = createGatewayCloseHandler({
|
const close = createGatewayCloseHandler({
|
||||||
bonjourStop,
|
bonjourStop,
|
||||||
|
|||||||
@@ -50,6 +50,10 @@ let previousSkipBrowserControl: string | undefined;
|
|||||||
let previousSkipGmailWatcher: string | undefined;
|
let previousSkipGmailWatcher: string | undefined;
|
||||||
let previousSkipCanvasHost: string | undefined;
|
let previousSkipCanvasHost: string | undefined;
|
||||||
let previousBundledPluginsDir: string | undefined;
|
let previousBundledPluginsDir: string | undefined;
|
||||||
|
let previousSkipChannels: string | undefined;
|
||||||
|
let previousSkipProviders: string | undefined;
|
||||||
|
let previousSkipCron: string | undefined;
|
||||||
|
let previousMinimalGateway: string | undefined;
|
||||||
let tempHome: string | undefined;
|
let tempHome: string | undefined;
|
||||||
let tempConfigRoot: string | undefined;
|
let tempConfigRoot: string | undefined;
|
||||||
|
|
||||||
@@ -90,6 +94,10 @@ async function setupGatewayTestHome() {
|
|||||||
previousSkipGmailWatcher = process.env.OPENCLAW_SKIP_GMAIL_WATCHER;
|
previousSkipGmailWatcher = process.env.OPENCLAW_SKIP_GMAIL_WATCHER;
|
||||||
previousSkipCanvasHost = process.env.OPENCLAW_SKIP_CANVAS_HOST;
|
previousSkipCanvasHost = process.env.OPENCLAW_SKIP_CANVAS_HOST;
|
||||||
previousBundledPluginsDir = process.env.OPENCLAW_BUNDLED_PLUGINS_DIR;
|
previousBundledPluginsDir = process.env.OPENCLAW_BUNDLED_PLUGINS_DIR;
|
||||||
|
previousSkipChannels = process.env.OPENCLAW_SKIP_CHANNELS;
|
||||||
|
previousSkipProviders = process.env.OPENCLAW_SKIP_PROVIDERS;
|
||||||
|
previousSkipCron = process.env.OPENCLAW_SKIP_CRON;
|
||||||
|
previousMinimalGateway = process.env.OPENCLAW_TEST_MINIMAL_GATEWAY;
|
||||||
tempHome = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-gateway-home-"));
|
tempHome = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-gateway-home-"));
|
||||||
process.env.HOME = tempHome;
|
process.env.HOME = tempHome;
|
||||||
process.env.USERPROFILE = tempHome;
|
process.env.USERPROFILE = tempHome;
|
||||||
@@ -101,6 +109,10 @@ function applyGatewaySkipEnv() {
|
|||||||
process.env.OPENCLAW_SKIP_BROWSER_CONTROL_SERVER = "1";
|
process.env.OPENCLAW_SKIP_BROWSER_CONTROL_SERVER = "1";
|
||||||
process.env.OPENCLAW_SKIP_GMAIL_WATCHER = "1";
|
process.env.OPENCLAW_SKIP_GMAIL_WATCHER = "1";
|
||||||
process.env.OPENCLAW_SKIP_CANVAS_HOST = "1";
|
process.env.OPENCLAW_SKIP_CANVAS_HOST = "1";
|
||||||
|
process.env.OPENCLAW_SKIP_CHANNELS = "1";
|
||||||
|
process.env.OPENCLAW_SKIP_PROVIDERS = "1";
|
||||||
|
process.env.OPENCLAW_SKIP_CRON = "1";
|
||||||
|
process.env.OPENCLAW_TEST_MINIMAL_GATEWAY = "1";
|
||||||
process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = tempHome
|
process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = tempHome
|
||||||
? path.join(tempHome, "openclaw-test-no-bundled-extensions")
|
? path.join(tempHome, "openclaw-test-no-bundled-extensions")
|
||||||
: "openclaw-test-no-bundled-extensions";
|
: "openclaw-test-no-bundled-extensions";
|
||||||
@@ -203,6 +215,26 @@ async function cleanupGatewayTestHome(options: { restoreEnv: boolean }) {
|
|||||||
} else {
|
} else {
|
||||||
process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = previousBundledPluginsDir;
|
process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = previousBundledPluginsDir;
|
||||||
}
|
}
|
||||||
|
if (previousSkipChannels === undefined) {
|
||||||
|
delete process.env.OPENCLAW_SKIP_CHANNELS;
|
||||||
|
} else {
|
||||||
|
process.env.OPENCLAW_SKIP_CHANNELS = previousSkipChannels;
|
||||||
|
}
|
||||||
|
if (previousSkipProviders === undefined) {
|
||||||
|
delete process.env.OPENCLAW_SKIP_PROVIDERS;
|
||||||
|
} else {
|
||||||
|
process.env.OPENCLAW_SKIP_PROVIDERS = previousSkipProviders;
|
||||||
|
}
|
||||||
|
if (previousSkipCron === undefined) {
|
||||||
|
delete process.env.OPENCLAW_SKIP_CRON;
|
||||||
|
} else {
|
||||||
|
process.env.OPENCLAW_SKIP_CRON = previousSkipCron;
|
||||||
|
}
|
||||||
|
if (previousMinimalGateway === undefined) {
|
||||||
|
delete process.env.OPENCLAW_TEST_MINIMAL_GATEWAY;
|
||||||
|
} else {
|
||||||
|
process.env.OPENCLAW_TEST_MINIMAL_GATEWAY = previousMinimalGateway;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (options.restoreEnv && tempHome) {
|
if (options.restoreEnv && tempHome) {
|
||||||
await fs.rm(tempHome, {
|
await fs.rm(tempHome, {
|
||||||
|
|||||||
Reference in New Issue
Block a user