mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 18:00:54 +00:00
perf(gateway): trim startup watcher imports
This commit is contained in:
33
src/gateway/config-diff.ts
Normal file
33
src/gateway/config-diff.ts
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import { isDeepStrictEqual } from "node:util";
|
||||||
|
import { isPlainObject } from "../utils.js";
|
||||||
|
|
||||||
|
export function diffConfigPaths(prev: unknown, next: unknown, prefix = ""): string[] {
|
||||||
|
if (prev === next) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
if (isPlainObject(prev) && isPlainObject(next)) {
|
||||||
|
const keys = new Set([...Object.keys(prev), ...Object.keys(next)]);
|
||||||
|
const paths: string[] = [];
|
||||||
|
for (const key of keys) {
|
||||||
|
const prevValue = prev[key];
|
||||||
|
const nextValue = next[key];
|
||||||
|
if (prevValue === undefined && nextValue === undefined) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const childPrefix = prefix ? `${prefix}.${key}` : key;
|
||||||
|
const childPaths = diffConfigPaths(prevValue, nextValue, childPrefix);
|
||||||
|
if (childPaths.length > 0) {
|
||||||
|
paths.push(...childPaths);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return paths;
|
||||||
|
}
|
||||||
|
if (Array.isArray(prev) && Array.isArray(next)) {
|
||||||
|
// Arrays can contain object entries (for example memory.qmd.paths/scope.rules);
|
||||||
|
// compare structurally so identical values are not reported as changed.
|
||||||
|
if (isDeepStrictEqual(prev, next)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return [prefix || "<root>"];
|
||||||
|
}
|
||||||
26
src/gateway/config-reload-settings.ts
Normal file
26
src/gateway/config-reload-settings.ts
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import type { GatewayReloadMode } from "../config/types.gateway.js";
|
||||||
|
import type { OpenClawConfig } from "../config/types.openclaw.js";
|
||||||
|
|
||||||
|
export type GatewayReloadSettings = {
|
||||||
|
mode: GatewayReloadMode;
|
||||||
|
debounceMs: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
const DEFAULT_RELOAD_SETTINGS: GatewayReloadSettings = {
|
||||||
|
mode: "hybrid",
|
||||||
|
debounceMs: 300,
|
||||||
|
};
|
||||||
|
|
||||||
|
export function resolveGatewayReloadSettings(cfg: OpenClawConfig): GatewayReloadSettings {
|
||||||
|
const rawMode = cfg.gateway?.reload?.mode;
|
||||||
|
const mode =
|
||||||
|
rawMode === "off" || rawMode === "restart" || rawMode === "hot" || rawMode === "hybrid"
|
||||||
|
? rawMode
|
||||||
|
: DEFAULT_RELOAD_SETTINGS.mode;
|
||||||
|
const debounceRaw = cfg.gateway?.reload?.debounceMs;
|
||||||
|
const debounceMs =
|
||||||
|
typeof debounceRaw === "number" && Number.isFinite(debounceRaw)
|
||||||
|
? Math.max(0, Math.floor(debounceRaw))
|
||||||
|
: DEFAULT_RELOAD_SETTINGS.debounceMs;
|
||||||
|
return { mode, debounceMs };
|
||||||
|
}
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
import { isDeepStrictEqual } from "node:util";
|
|
||||||
import chokidar from "chokidar";
|
import chokidar from "chokidar";
|
||||||
import { bumpSkillsSnapshotVersion } from "../agents/skills/refresh-state.js";
|
import { bumpSkillsSnapshotVersion } from "../agents/skills/refresh-state.js";
|
||||||
import type { ConfigWriteNotification } from "../config/io.js";
|
import type { ConfigWriteNotification } from "../config/io.js";
|
||||||
@@ -9,7 +8,6 @@ import {
|
|||||||
shouldAttemptLastKnownGoodRecovery,
|
shouldAttemptLastKnownGoodRecovery,
|
||||||
} from "../config/recovery-policy.js";
|
} from "../config/recovery-policy.js";
|
||||||
import { resolveConfigWriteFollowUp } from "../config/runtime-snapshot.js";
|
import { resolveConfigWriteFollowUp } from "../config/runtime-snapshot.js";
|
||||||
import type { GatewayReloadMode } from "../config/types.gateway.js";
|
|
||||||
import type { ConfigFileSnapshot, OpenClawConfig } from "../config/types.openclaw.js";
|
import type { ConfigFileSnapshot, OpenClawConfig } from "../config/types.openclaw.js";
|
||||||
import type { PluginInstallRecord } from "../config/types.plugins.js";
|
import type { PluginInstallRecord } from "../config/types.plugins.js";
|
||||||
import { validateConfigObjectWithPlugins } from "../config/validation.js";
|
import { validateConfigObjectWithPlugins } from "../config/validation.js";
|
||||||
@@ -17,30 +15,23 @@ import {
|
|||||||
loadInstalledPluginIndexInstallRecords,
|
loadInstalledPluginIndexInstallRecords,
|
||||||
loadInstalledPluginIndexInstallRecordsSync,
|
loadInstalledPluginIndexInstallRecordsSync,
|
||||||
} from "../plugins/installed-plugin-index-records.js";
|
} from "../plugins/installed-plugin-index-records.js";
|
||||||
import { isPlainObject } from "../utils.js";
|
import { diffConfigPaths } from "./config-diff.js";
|
||||||
import {
|
import {
|
||||||
buildGatewayReloadPlan,
|
buildGatewayReloadPlan,
|
||||||
listPluginInstallTimestampMetadataPaths,
|
listPluginInstallTimestampMetadataPaths,
|
||||||
listPluginInstallWholeRecordPaths,
|
listPluginInstallWholeRecordPaths,
|
||||||
type GatewayReloadPlan,
|
type GatewayReloadPlan,
|
||||||
} from "./config-reload-plan.js";
|
} from "./config-reload-plan.js";
|
||||||
|
import { resolveGatewayReloadSettings } from "./config-reload-settings.js";
|
||||||
|
|
||||||
export {
|
export {
|
||||||
buildGatewayReloadPlan,
|
buildGatewayReloadPlan,
|
||||||
|
diffConfigPaths,
|
||||||
listPluginInstallTimestampMetadataPaths,
|
listPluginInstallTimestampMetadataPaths,
|
||||||
listPluginInstallWholeRecordPaths,
|
listPluginInstallWholeRecordPaths,
|
||||||
|
resolveGatewayReloadSettings,
|
||||||
};
|
};
|
||||||
export type { ChannelKind, GatewayReloadPlan } from "./config-reload-plan.js";
|
export type { ChannelKind, GatewayReloadPlan } from "./config-reload-plan.js";
|
||||||
|
|
||||||
type GatewayReloadSettings = {
|
|
||||||
mode: GatewayReloadMode;
|
|
||||||
debounceMs: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
const DEFAULT_RELOAD_SETTINGS: GatewayReloadSettings = {
|
|
||||||
mode: "hybrid",
|
|
||||||
debounceMs: 300,
|
|
||||||
};
|
|
||||||
const MISSING_CONFIG_RETRY_DELAY_MS = 150;
|
const MISSING_CONFIG_RETRY_DELAY_MS = 150;
|
||||||
const MISSING_CONFIG_MAX_RETRIES = 2;
|
const MISSING_CONFIG_MAX_RETRIES = 2;
|
||||||
|
|
||||||
@@ -115,51 +106,6 @@ function resolvePluginLocalInvalidReloadSnapshot(params: {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function diffConfigPaths(prev: unknown, next: unknown, prefix = ""): string[] {
|
|
||||||
if (prev === next) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
if (isPlainObject(prev) && isPlainObject(next)) {
|
|
||||||
const keys = new Set([...Object.keys(prev), ...Object.keys(next)]);
|
|
||||||
const paths: string[] = [];
|
|
||||||
for (const key of keys) {
|
|
||||||
const prevValue = prev[key];
|
|
||||||
const nextValue = next[key];
|
|
||||||
if (prevValue === undefined && nextValue === undefined) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
const childPrefix = prefix ? `${prefix}.${key}` : key;
|
|
||||||
const childPaths = diffConfigPaths(prevValue, nextValue, childPrefix);
|
|
||||||
if (childPaths.length > 0) {
|
|
||||||
paths.push(...childPaths);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return paths;
|
|
||||||
}
|
|
||||||
if (Array.isArray(prev) && Array.isArray(next)) {
|
|
||||||
// Arrays can contain object entries (for example memory.qmd.paths/scope.rules);
|
|
||||||
// compare structurally so identical values are not reported as changed.
|
|
||||||
if (isDeepStrictEqual(prev, next)) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return [prefix || "<root>"];
|
|
||||||
}
|
|
||||||
|
|
||||||
export function resolveGatewayReloadSettings(cfg: OpenClawConfig): GatewayReloadSettings {
|
|
||||||
const rawMode = cfg.gateway?.reload?.mode;
|
|
||||||
const mode =
|
|
||||||
rawMode === "off" || rawMode === "restart" || rawMode === "hot" || rawMode === "hybrid"
|
|
||||||
? rawMode
|
|
||||||
: DEFAULT_RELOAD_SETTINGS.mode;
|
|
||||||
const debounceRaw = cfg.gateway?.reload?.debounceMs;
|
|
||||||
const debounceMs =
|
|
||||||
typeof debounceRaw === "number" && Number.isFinite(debounceRaw)
|
|
||||||
? Math.max(0, Math.floor(debounceRaw))
|
|
||||||
: DEFAULT_RELOAD_SETTINGS.debounceMs;
|
|
||||||
return { mode, debounceMs };
|
|
||||||
}
|
|
||||||
|
|
||||||
type GatewayConfigReloader = {
|
type GatewayConfigReloader = {
|
||||||
stop: () => Promise<void>;
|
stop: () => Promise<void>;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -10,12 +10,12 @@ import {
|
|||||||
activateSecretsRuntimeSnapshot,
|
activateSecretsRuntimeSnapshot,
|
||||||
getActiveSecretsRuntimeSnapshot,
|
getActiveSecretsRuntimeSnapshot,
|
||||||
} from "../secrets/runtime.js";
|
} from "../secrets/runtime.js";
|
||||||
|
import { diffConfigPaths } from "./config-diff.js";
|
||||||
import {
|
import {
|
||||||
buildGatewayReloadPlan,
|
buildGatewayReloadPlan,
|
||||||
diffConfigPaths,
|
|
||||||
type ChannelKind,
|
type ChannelKind,
|
||||||
type GatewayReloadPlan,
|
type GatewayReloadPlan,
|
||||||
} from "./config-reload.js";
|
} from "./config-reload-plan.js";
|
||||||
import { createExecApprovalIosPushDelivery } from "./exec-approval-ios-push.js";
|
import { createExecApprovalIosPushDelivery } from "./exec-approval-ios-push.js";
|
||||||
import { ExecApprovalManager } from "./exec-approval-manager.js";
|
import { ExecApprovalManager } from "./exec-approval-manager.js";
|
||||||
import { createExecApprovalHandlers } from "./server-methods/exec-approval.js";
|
import { createExecApprovalHandlers } from "./server-methods/exec-approval.js";
|
||||||
|
|||||||
@@ -16,6 +16,16 @@ describe("gateway startup import boundaries", () => {
|
|||||||
expect(serverImpl).not.toContain('from "./server-cron.js"');
|
expect(serverImpl).not.toContain('from "./server-cron.js"');
|
||||||
expect(serverImpl).toContain('from "./server-cron-lazy.js"');
|
expect(serverImpl).toContain('from "./server-cron-lazy.js"');
|
||||||
expect(serverImpl).not.toContain('from "./server-methods.js"');
|
expect(serverImpl).not.toContain('from "./server-methods.js"');
|
||||||
|
expect(serverImpl).not.toContain('from "./config-reload.js"');
|
||||||
|
expect(readSource("src/gateway/server-shared-auth-generation.ts")).not.toContain(
|
||||||
|
'from "./config-reload.js"',
|
||||||
|
);
|
||||||
|
expect(readSource("src/gateway/server-aux-handlers.ts")).not.toContain(
|
||||||
|
'from "./config-reload.js"',
|
||||||
|
);
|
||||||
|
expect(readSource("src/gateway/server-runtime-state.ts")).not.toContain(
|
||||||
|
'createCanvasHostHandler } from "../canvas-host/server.js"',
|
||||||
|
);
|
||||||
expect(serverImpl).not.toContain('from "../plugins/hook-runner-global.js"');
|
expect(serverImpl).not.toContain('from "../plugins/hook-runner-global.js"');
|
||||||
expect(validation).not.toContain("legacy-secretref-env-marker");
|
expect(validation).not.toContain("legacy-secretref-env-marker");
|
||||||
expect(validation).not.toContain("commands/doctor");
|
expect(validation).not.toContain("commands/doctor");
|
||||||
|
|||||||
@@ -14,7 +14,8 @@ import {
|
|||||||
import { scheduleGatewaySigusr1Restart } from "../../infra/restart.js";
|
import { scheduleGatewaySigusr1Restart } from "../../infra/restart.js";
|
||||||
import { getActiveSecretsRuntimeSnapshot } from "../../secrets/runtime.js";
|
import { getActiveSecretsRuntimeSnapshot } from "../../secrets/runtime.js";
|
||||||
import { resolveEffectiveSharedGatewayAuth } from "../auth.js";
|
import { resolveEffectiveSharedGatewayAuth } from "../auth.js";
|
||||||
import { buildGatewayReloadPlan, resolveGatewayReloadSettings } from "../config-reload.js";
|
import { buildGatewayReloadPlan } from "../config-reload-plan.js";
|
||||||
|
import { resolveGatewayReloadSettings } from "../config-reload-settings.js";
|
||||||
import { formatControlPlaneActor, type ControlPlaneActor } from "../control-plane-audit.js";
|
import { formatControlPlaneActor, type ControlPlaneActor } from "../control-plane-audit.js";
|
||||||
import { parseRestartRequestParams } from "./restart-request.js";
|
import { parseRestartRequestParams } from "./restart-request.js";
|
||||||
import type { GatewayRequestContext } from "./types.js";
|
import type { GatewayRequestContext } from "./types.js";
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ import {
|
|||||||
prepareSecretsRuntimeSnapshot,
|
prepareSecretsRuntimeSnapshot,
|
||||||
type PreparedSecretsRuntimeSnapshot,
|
type PreparedSecretsRuntimeSnapshot,
|
||||||
} from "../../secrets/runtime.js";
|
} from "../../secrets/runtime.js";
|
||||||
import { diffConfigPaths } from "../config-reload.js";
|
import { diffConfigPaths } from "../config-diff.js";
|
||||||
import {
|
import {
|
||||||
formatControlPlaneActor,
|
formatControlPlaneActor,
|
||||||
resolveControlPlaneActor,
|
resolveControlPlaneActor,
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import type { IncomingMessage, Server as HttpServer, ServerResponse } from "node:http";
|
import type { IncomingMessage, Server as HttpServer, ServerResponse } from "node:http";
|
||||||
import { WebSocketServer } from "ws";
|
import { WebSocketServer } from "ws";
|
||||||
import { CANVAS_HOST_PATH } from "../canvas-host/a2ui.js";
|
import { CANVAS_HOST_PATH } from "../canvas-host/a2ui.js";
|
||||||
import { type CanvasHostHandler, createCanvasHostHandler } from "../canvas-host/server.js";
|
import type { CanvasHostHandler } from "../canvas-host/server.js";
|
||||||
import type { CliDeps } from "../cli/deps.types.js";
|
import type { CliDeps } from "../cli/deps.types.js";
|
||||||
import type { createSubsystemLogger } from "../logging/subsystem.js";
|
import type { createSubsystemLogger } from "../logging/subsystem.js";
|
||||||
import type { PluginRegistry } from "../plugins/registry.js";
|
import type { PluginRegistry } from "../plugins/registry.js";
|
||||||
@@ -120,6 +120,7 @@ export async function createGatewayRuntimeState(params: {
|
|||||||
let canvasHost: CanvasHostHandler | null = null;
|
let canvasHost: CanvasHostHandler | null = null;
|
||||||
if (params.canvasHostEnabled) {
|
if (params.canvasHostEnabled) {
|
||||||
try {
|
try {
|
||||||
|
const { createCanvasHostHandler } = await import("../canvas-host/server.js");
|
||||||
const handler = await createCanvasHostHandler({
|
const handler = await createCanvasHostHandler({
|
||||||
runtime: params.canvasRuntime,
|
runtime: params.canvasRuntime,
|
||||||
rootDir: params.cfg.canvasHost?.root,
|
rootDir: params.cfg.canvasHost?.root,
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import type { OpenClawConfig } from "../config/types.openclaw.js";
|
import type { OpenClawConfig } from "../config/types.openclaw.js";
|
||||||
import { resolveGatewayReloadSettings } from "./config-reload.js";
|
import { resolveGatewayReloadSettings } from "./config-reload-settings.js";
|
||||||
|
|
||||||
export type SharedGatewayAuthClient = {
|
export type SharedGatewayAuthClient = {
|
||||||
usesSharedGatewayAuth?: boolean;
|
usesSharedGatewayAuth?: boolean;
|
||||||
|
|||||||
Reference in New Issue
Block a user