Skip to content

Commit e44ee56

Browse files
committed
perf(gateway): separate startup maintenance timing
1 parent 71b35c3 commit e44ee56

3 files changed

Lines changed: 130 additions & 17 deletions

File tree

‎src/gateway/server-startup-bootstrap.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,12 +475,20 @@ export async function prepareGatewayServerBootstrap(input: {
475475
const workerModule = await loadWorkerEnvironmentStartupModule();
476476
return await workerModule.loadGatewayWorkerEnvironmentStartupState();
477477
});
478-
const { prepareGatewayPluginBootstrap } = await loadStartupPluginsModule();
478+
const { prepareGatewayPluginBootstrap, runGatewayStartupMaintenance } =
479+
await loadStartupPluginsModule();
480+
await startupTrace.measure("startup.maintenance", () =>
481+
runGatewayStartupMaintenance({
482+
cfgAtStart,
483+
startupRuntimeConfig,
484+
minimalTestGateway,
485+
log,
486+
}),
487+
);
479488
const pluginBootstrap = await startupTrace.measure("plugins.bootstrap", () =>
480489
prepareGatewayPluginBootstrap({
481490
cfgAtStart,
482491
activationSourceConfig: startupActivationSourceConfig,
483-
startupRuntimeConfig,
484492
pluginMetadataSnapshot: startupConfigLoad.pluginMetadataSnapshot,
485493
workerProviderIds: workerEnvironmentStartup?.durableProviderIds ?? [],
486494
minimalTestGateway,

‎src/gateway/server-startup-plugins.test.ts‎

Lines changed: 102 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ const listAmbientOnlyConfiguredChannelIds = vi.hoisted(() =>
108108
vi.fn((_params: unknown) => [] as string[]),
109109
);
110110
const runStartupSessionMigration = vi.hoisted(() => vi.fn(async (_params: unknown) => undefined));
111+
const migrateLegacyDevicePairingStore = vi.hoisted(() =>
112+
vi.fn(async (_params: unknown) => undefined),
113+
);
114+
const migrateLegacyNodePairingStore = vi.hoisted(() =>
115+
vi.fn(async (_params: unknown) => undefined),
116+
);
111117
vi.mock("../agents/agent-scope.js", () => ({
112118
resolveAgentWorkspaceDir: () => "/workspace",
113119
resolveDefaultAgentId: () => "default",
@@ -130,6 +136,14 @@ vi.mock("../infra/openclaw-root.js", () => ({
130136
resolveOpenClawPackageRootSync: (params: unknown) => resolveOpenClawPackageRootSync(params),
131137
}));
132138

139+
vi.mock("../infra/device-pairing-migration.js", () => ({
140+
migrateLegacyDevicePairingStore: (params: unknown) => migrateLegacyDevicePairingStore(params),
141+
}));
142+
143+
vi.mock("../infra/node-pairing-migration.js", () => ({
144+
migrateLegacyNodePairingStore: (params: unknown) => migrateLegacyNodePairingStore(params),
145+
}));
146+
133147
vi.mock("../plugins/channel-presence-policy.js", () => ({
134148
listAmbientOnlyConfiguredChannelIds: (params: unknown) =>
135149
listAmbientOnlyConfiguredChannelIds(params),
@@ -222,7 +236,6 @@ async function prepareBootstrapWithRuntimeConfig(
222236

223237
return await prepareGatewayPluginBootstrap({
224238
cfgAtStart: cfg,
225-
startupRuntimeConfig: cfg,
226239
minimalTestGateway: false,
227240
log,
228241
...options,
@@ -246,6 +259,83 @@ function expectStartupPluginLoad(params: {
246259
expect(startupInput.suppressPluginInfoLogs).toBe(params.suppressPluginInfoLogs);
247260
}
248261

262+
describe("runGatewayStartupMaintenance", () => {
263+
beforeEach(() => {
264+
runChannelPluginStartupMaintenance.mockClear();
265+
runStartupSessionMigration.mockClear();
266+
migrateLegacyDevicePairingStore.mockClear();
267+
migrateLegacyNodePairingStore.mockClear();
268+
});
269+
270+
it("runs channel, session, and ordered pairing maintenance for a normal gateway", async () => {
271+
const log = createLog();
272+
const { runGatewayStartupMaintenance } = await import("./server-startup-plugins.js");
273+
274+
await runGatewayStartupMaintenance({
275+
cfgAtStart: {},
276+
startupRuntimeConfig: {},
277+
minimalTestGateway: false,
278+
log,
279+
});
280+
281+
expect(runChannelPluginStartupMaintenance).toHaveBeenCalledWith({
282+
cfg: {},
283+
env: process.env,
284+
log,
285+
});
286+
expect(runStartupSessionMigration).toHaveBeenCalledWith({
287+
cfg: {},
288+
env: process.env,
289+
log,
290+
});
291+
expect(migrateLegacyDevicePairingStore).toHaveBeenCalledWith({ log });
292+
expect(migrateLegacyNodePairingStore).toHaveBeenCalledWith({ log });
293+
const deviceMigrationOrder = migrateLegacyDevicePairingStore.mock.invocationCallOrder[0];
294+
const nodeMigrationOrder = migrateLegacyNodePairingStore.mock.invocationCallOrder[0];
295+
expect(deviceMigrationOrder).toBeDefined();
296+
expect(nodeMigrationOrder).toBeDefined();
297+
expect(deviceMigrationOrder!).toBeLessThan(nodeMigrationOrder!);
298+
});
299+
300+
it("skips maintenance for a minimal gateway without channel config", async () => {
301+
const { runGatewayStartupMaintenance } = await import("./server-startup-plugins.js");
302+
303+
await runGatewayStartupMaintenance({
304+
cfgAtStart: {},
305+
startupRuntimeConfig: {},
306+
minimalTestGateway: true,
307+
log: createLog(),
308+
});
309+
310+
expect(runChannelPluginStartupMaintenance).not.toHaveBeenCalled();
311+
expect(runStartupSessionMigration).not.toHaveBeenCalled();
312+
expect(migrateLegacyDevicePairingStore).not.toHaveBeenCalled();
313+
expect(migrateLegacyNodePairingStore).not.toHaveBeenCalled();
314+
});
315+
316+
it("runs only channel maintenance for a minimal gateway with recovered channel config", async () => {
317+
const log = createLog();
318+
const recoveredConfig = slackConfig();
319+
const { runGatewayStartupMaintenance } = await import("./server-startup-plugins.js");
320+
321+
await runGatewayStartupMaintenance({
322+
cfgAtStart: {},
323+
startupRuntimeConfig: recoveredConfig,
324+
minimalTestGateway: true,
325+
log,
326+
});
327+
328+
expect(runChannelPluginStartupMaintenance).toHaveBeenCalledWith({
329+
cfg: recoveredConfig,
330+
env: process.env,
331+
log,
332+
});
333+
expect(runStartupSessionMigration).not.toHaveBeenCalled();
334+
expect(migrateLegacyDevicePairingStore).not.toHaveBeenCalled();
335+
expect(migrateLegacyNodePairingStore).not.toHaveBeenCalled();
336+
});
337+
});
338+
249339
describe("prepareGatewayPluginBootstrap startup plugins", () => {
250340
beforeEach(() => {
251341
applyPluginAutoEnable.mockClear();
@@ -263,7 +353,18 @@ describe("prepareGatewayPluginBootstrap startup plugins", () => {
263353
resolveOpenClawPackageRootSync.mockClear().mockReturnValue("/package");
264354
runChannelPluginStartupMaintenance.mockClear();
265355
runStartupSessionMigration.mockClear();
356+
migrateLegacyDevicePairingStore.mockClear();
357+
migrateLegacyNodePairingStore.mockClear();
266358
});
359+
it("does not run startup maintenance", async () => {
360+
await prepareBootstrapWithRuntimeConfig({});
361+
362+
expect(runChannelPluginStartupMaintenance).not.toHaveBeenCalled();
363+
expect(runStartupSessionMigration).not.toHaveBeenCalled();
364+
expect(migrateLegacyDevicePairingStore).not.toHaveBeenCalled();
365+
expect(migrateLegacyNodePairingStore).not.toHaveBeenCalled();
366+
});
367+
267368
it("derives startup activation from source config instead of runtime plugin defaults", async () => {
268369
const sourceConfig = {
269370
channels: {
@@ -328,7 +429,6 @@ describe("prepareGatewayPluginBootstrap startup plugins", () => {
328429
await prepareGatewayPluginBootstrap({
329430
cfgAtStart: runtimeConfig,
330431
activationSourceConfig: sourceConfig,
331-
startupRuntimeConfig: runtimeConfig,
332432
pluginMetadataSnapshot,
333433
minimalTestGateway: false,
334434
log,
@@ -435,7 +535,6 @@ describe("prepareGatewayPluginBootstrap startup plugins", () => {
435535
const { prepareGatewayPluginBootstrap } = await import("./server-startup-plugins.js");
436536
const result = await prepareGatewayPluginBootstrap({
437537
cfgAtStart: { channels: {} },
438-
startupRuntimeConfig: { channels: {} },
439538
minimalTestGateway: false,
440539
ambientEnvTriggers: "suppress",
441540
log,

‎src/gateway/server-startup-plugins.ts‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// Gateway plugin startup bootstrap.
2-
// Runs startup maintenance, loads plugin runtime, and prepares advertised methods.
1+
// Gateway plugin startup bootstrap and adjacent startup maintenance.
32
import { resolveAgentWorkspaceDir, resolveDefaultAgentId } from "../agents/agent-scope.js";
43
import { initSubagentRegistry } from "../agents/subagent-registry.js";
54
import type { AmbientEnvTriggerPolicy } from "../channels/config-presence.js";
@@ -45,20 +44,13 @@ export function resolveGatewayStartupMaintenanceConfig(params: {
4544
: params.cfgAtStart;
4645
}
4746

48-
/** Builds plugin startup state and gateway method lists before the server binds. */
49-
export async function prepareGatewayPluginBootstrap(params: {
47+
/** Runs channel, session, and pairing maintenance before plugin bootstrap. */
48+
export async function runGatewayStartupMaintenance(params: {
5049
cfgAtStart: OpenClawConfig;
51-
activationSourceConfig?: OpenClawConfig;
5250
startupRuntimeConfig: OpenClawConfig;
53-
pluginMetadataSnapshot?: PluginMetadataSnapshot;
54-
workerProviderIds?: readonly string[];
5551
minimalTestGateway: boolean;
5652
log: GatewayPluginBootstrapLog;
57-
loadRuntimePlugins?: boolean;
58-
loadSetupRuntimePlugins?: boolean;
59-
ambientEnvTriggers?: AmbientEnvTriggerPolicy;
60-
}) {
61-
const activationSourceConfig = params.activationSourceConfig ?? params.cfgAtStart;
53+
}): Promise<void> {
6254
const startupMaintenanceConfig = resolveGatewayStartupMaintenanceConfig({
6355
cfgAtStart: params.cfgAtStart,
6456
startupRuntimeConfig: params.startupRuntimeConfig,
@@ -110,7 +102,21 @@ export async function prepareGatewayPluginBootstrap(params: {
110102
}
111103
await Promise.all(startupTasks);
112104
}
105+
}
113106

107+
/** Builds plugin startup state and gateway method lists before the server binds. */
108+
export async function prepareGatewayPluginBootstrap(params: {
109+
cfgAtStart: OpenClawConfig;
110+
activationSourceConfig?: OpenClawConfig;
111+
pluginMetadataSnapshot?: PluginMetadataSnapshot;
112+
workerProviderIds?: readonly string[];
113+
minimalTestGateway: boolean;
114+
log: GatewayPluginBootstrapLog;
115+
loadRuntimePlugins?: boolean;
116+
loadSetupRuntimePlugins?: boolean;
117+
ambientEnvTriggers?: AmbientEnvTriggerPolicy;
118+
}) {
119+
const activationSourceConfig = params.activationSourceConfig ?? params.cfgAtStart;
114120
initSubagentRegistry();
115121

116122
// Activation uses the pre-runtime source so auto-enable policy cannot be skewed by

0 commit comments

Comments
 (0)