Skip to content

Commit f354a17

Browse files
committed
perf(gateway): keep reload and recovery machinery off gateway cold start
The run loop primes cli/gateway-cli/lifecycle.runtime.ts before installing signal handlers, so every symbol that hub re-exports loads before the HTTP listener binds. Two of those re-exports resolved through forwarding modules: gateway/server-reload-handlers.ts also re-exports the hot-reload and managed reloader modules, and agents/main-session-restart-recovery.ts also re-exports its -runtime sibling. ESM evaluates the whole forwarding module, so priming the hub pulled both graphs onto the cold-start path. Re-export each symbol from the module that defines it instead. abortPendingChannelReloads lives in gateway/server-reload-contracts.ts, whose own imports are all type-only, and markRestartAbortedMainSessions lives in agents/main-session-restart-recovery-marking.ts. Both retargets resolve to the same module instances, so shared reload-generation state and behavior are unchanged, and the eager priming that protects signal handlers from dist chunk rotation stays in place. Add a CLI-lane import-boundary test so these re-exports cannot be routed back through a forwarding module, and point the two run-loop test mocks at the defining modules.
1 parent 84cfc43 commit f354a17

3 files changed

Lines changed: 44 additions & 4 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// The run loop primes lifecycle.runtime.ts before the HTTP listener binds, so the
2+
// hub's re-exports decide how much module graph loads during gateway cold start.
3+
import { readFileSync } from "node:fs";
4+
import path from "node:path";
5+
import { describe, expect, it } from "vitest";
6+
7+
const repoRoot = path.resolve(import.meta.dirname, "../../..");
8+
9+
function readSource(relativePath: string): string {
10+
return readFileSync(path.join(repoRoot, relativePath), "utf8");
11+
}
12+
13+
describe("gateway lifecycle hub import boundaries", () => {
14+
it("re-exports primed symbols from their defining modules instead of facades", () => {
15+
const hub = readSource("src/cli/gateway-cli/lifecycle.runtime.ts");
16+
17+
// server-reload-handlers.ts also re-exports server-reload-hot.ts and
18+
// server-reload-managed.ts, so routing through it loads the hot-reload and
19+
// managed-reloader graphs before the gateway can accept a connection.
20+
expect(hub).toContain('from "../../gateway/server-reload-contracts.js"');
21+
expect(hub).not.toContain('from "../../gateway/server-reload-handlers.js"');
22+
23+
// main-session-restart-recovery.ts also re-exports its -runtime sibling,
24+
// which the primed hub never calls.
25+
expect(hub).toContain('from "../../agents/main-session-restart-recovery-marking.js"');
26+
expect(hub).not.toContain('from "../../agents/main-session-restart-recovery.js"');
27+
});
28+
29+
it("still primes the hub eagerly so signal handlers survive dist chunk rotation", () => {
30+
const runLoop = readSource("src/cli/gateway-cli/run-loop.ts");
31+
const eagerPrime = runLoop.indexOf("await loadGatewayLifecycleRuntimeModule()");
32+
const signalInstall = runLoop.indexOf("process.on(");
33+
34+
expect(eagerPrime).toBeGreaterThan(-1);
35+
expect(signalInstall).toBeGreaterThan(eagerPrime);
36+
});
37+
});

‎src/cli/gateway-cli/lifecycle.runtime.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
// Lazy lifecycle runtime export hub used by gateway run-loop restart paths.
2+
// run-loop.ts primes this hub before the HTTP listener binds, so each re-export
3+
// must target the module that defines the symbol rather than a re-export facade;
4+
// a facade also evaluates its siblings and drags their graphs onto cold start.
25
export {
36
abortEmbeddedAgentRun,
47
getActiveEmbeddedRunCount,
58
listActiveEmbeddedRunSessionIds,
69
listActiveEmbeddedRunSessionKeys,
710
waitForActiveEmbeddedRuns,
811
} from "../../agents/embedded-agent-runner/runs.js";
9-
export { markRestartAbortedMainSessions } from "../../agents/main-session-restart-recovery.js";
12+
export { markRestartAbortedMainSessions } from "../../agents/main-session-restart-recovery-marking.js";
1013
export { getRuntimeConfig } from "../../config/config.js";
1114
export {
1215
respawnGatewayProcessForUpdate,
@@ -56,4 +59,4 @@ export {
5659
export { waitForActiveGatewayRootWork } from "../../process/gateway-work-admission.js";
5760
export { getInspectableActiveTaskRestartBlockers } from "../../tasks/task-registry.maintenance.js";
5861
export { reloadTaskRuntimeStateFromStore } from "../../tasks/runtime-internal.js";
59-
export { abortPendingChannelReloads } from "../../gateway/server-reload-handlers.js";
62+
export { abortPendingChannelReloads } from "../../gateway/server-reload-contracts.js";

‎src/cli/gateway-cli/run-loop.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ vi.mock("../../agents/embedded-agent-runner/runs.js", () => ({
244244
waitForActiveEmbeddedRuns: (timeoutMs?: number) => waitForActiveEmbeddedRuns(timeoutMs),
245245
}));
246246

247-
vi.mock("../../agents/main-session-restart-recovery.js", () => ({
247+
vi.mock("../../agents/main-session-restart-recovery-marking.js", () => ({
248248
markRestartAbortedMainSessions: (params: unknown) => markRestartAbortedMainSessions(params),
249249
}));
250250

@@ -261,7 +261,7 @@ vi.mock("../../logging/logger.js", () => ({
261261
flushLogger: () => flushLogger(),
262262
}));
263263

264-
vi.mock("../../gateway/server-reload-handlers.js", () => ({
264+
vi.mock("../../gateway/server-reload-contracts.js", () => ({
265265
abortPendingChannelReloads: () => abortPendingChannelReloads(),
266266
}));
267267

0 commit comments

Comments
 (0)