Skip to content

Commit ebf121a

Browse files
authored
perf(gateway): mark recovery before model preparation (#117544)
* perf(gateway): keep recovery runtime off startup path * perf(gateway): mark recovery before model preparation * test(gateway): lock recovery marking failure order
1 parent 71b35c3 commit ebf121a

2 files changed

Lines changed: 75 additions & 14 deletions

File tree

‎src/gateway/server-startup-post-attach.test.ts‎

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,11 @@ vi.mock("../agents/subagent-registry.js", () => ({
123123
scheduleSubagentOrphanRecovery: hoisted.scheduleSubagentOrphanRecovery,
124124
}));
125125

126-
vi.mock("../agents/main-session-restart-recovery.js", () => ({
126+
vi.mock("../agents/main-session-restart-recovery-marking.js", () => ({
127127
markStartupOrphanedMainSessionsForRecovery: hoisted.markStartupOrphanedMainSessionsForRecovery,
128+
}));
129+
130+
vi.mock("../agents/main-session-restart-recovery.js", () => ({
128131
scheduleRestartAbortedMainSessionRecovery: hoisted.scheduleRestartAbortedMainSessionRecovery,
129132
}));
130133

@@ -1812,9 +1815,12 @@ describe("startGatewayPostAttachRuntime", () => {
18121815
});
18131816
});
18141817

1815-
it("marks startup main-session orphans before channel startup", async () => {
1818+
it("marks startup main-session orphans before model runtime and channel startup", async () => {
18161819
const events: string[] = [];
18171820
let releaseMarking: (() => void) | undefined;
1821+
const prewarmPrimaryModel = vi.fn(async () => {
1822+
events.push("model-runtime");
1823+
});
18181824
const startChannels = vi.fn(async () => {
18191825
events.push("channels");
18201826
});
@@ -1835,6 +1841,7 @@ describe("startGatewayPostAttachRuntime", () => {
18351841
defaultWorkspaceDir: "/tmp/openclaw-workspace",
18361842
deps: {} as never,
18371843
startChannels,
1844+
prewarmPrimaryModel,
18381845
log: { warn: vi.fn() },
18391846
logHooks: {
18401847
info: vi.fn(),
@@ -1858,11 +1865,54 @@ describe("startGatewayPostAttachRuntime", () => {
18581865
releaseMarking();
18591866
await sidecars;
18601867

1861-
expect(events).toEqual(["main-session-mark:start", "main-session-mark:done", "channels"]);
1868+
expect(events).toEqual([
1869+
"main-session-mark:start",
1870+
"main-session-mark:done",
1871+
"model-runtime",
1872+
"channels",
1873+
]);
1874+
expect(prewarmPrimaryModel).toHaveBeenCalledTimes(1);
18621875
expect(startChannels).toHaveBeenCalledTimes(1);
18631876
expect(hoisted.scheduleRestartAbortedMainSessionRecovery).not.toHaveBeenCalled();
18641877
});
18651878

1879+
it("marks startup main-session orphans before propagating model runtime failure", async () => {
1880+
const modelRuntimeError = new Error("model runtime unavailable");
1881+
const startChannels = vi.fn(async () => {});
1882+
const prewarmPrimaryModel = vi.fn(async () => {
1883+
throw modelRuntimeError;
1884+
});
1885+
hoisted.markStartupOrphanedMainSessionsForRecovery.mockResolvedValueOnce({
1886+
marked: 1,
1887+
skipped: 0,
1888+
});
1889+
1890+
await expect(
1891+
startGatewaySidecars({
1892+
cfg: { hooks: { internal: { enabled: false } } } as never,
1893+
pluginRegistry: createPostAttachParams().pluginRegistry,
1894+
defaultWorkspaceDir: "/tmp/openclaw-workspace",
1895+
deps: {} as never,
1896+
startChannels,
1897+
prewarmPrimaryModel,
1898+
log: { warn: vi.fn() },
1899+
logHooks: {
1900+
info: vi.fn(),
1901+
warn: vi.fn(),
1902+
error: vi.fn(),
1903+
},
1904+
logChannels: {
1905+
info: vi.fn(),
1906+
error: vi.fn(),
1907+
},
1908+
}),
1909+
).rejects.toBe(modelRuntimeError);
1910+
1911+
expect(hoisted.markStartupOrphanedMainSessionsForRecovery).toHaveBeenCalledTimes(1);
1912+
expect(prewarmPrimaryModel).toHaveBeenCalledTimes(1);
1913+
expect(startChannels).not.toHaveBeenCalled();
1914+
});
1915+
18661916
it("logs startup main-session marker failures and still starts channels", async () => {
18671917
const log = { warn: vi.fn() };
18681918
const startChannels = vi.fn(async () => {});

‎src/gateway/server-startup-post-attach.ts‎

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ type GatewayMemoryStartupPolicy =
5555
const loadMainSessionRestartRecoveryModule = createLazyRuntimeModule(
5656
() => import("../agents/main-session-restart-recovery.js"),
5757
);
58+
// Startup only needs orphan marking; keep resume and delivery runtime out of the pre-channel path.
59+
const loadMainSessionRestartRecoveryMarkingModule = createLazyRuntimeModule(
60+
() => import("../agents/main-session-restart-recovery-marking.js"),
61+
);
5862

5963
const loadAgentDefaultsModule = createLazyRuntimeModule(() => import("../agents/defaults.js"));
6064

@@ -658,6 +662,24 @@ export async function startGatewaySidecars(params: {
658662
const skipChannels =
659663
isTruthyEnvValue(process.env.OPENCLAW_SKIP_CHANNELS) ||
660664
isTruthyEnvValue(process.env.OPENCLAW_SKIP_PROVIDERS);
665+
// These runs were orphaned by the previous Gateway lifecycle. Record that fact
666+
// even if this process later fails model preparation and never starts channels.
667+
await measureStartup(params.startupTrace, "sidecars.main-session-recovery", async () => {
668+
try {
669+
const { markStartupOrphanedMainSessionsForRecovery } = await measureStartup(
670+
params.startupTrace,
671+
"sidecars.main-session-recovery-load",
672+
loadMainSessionRestartRecoveryMarkingModule,
673+
);
674+
await measureStartup(params.startupTrace, "sidecars.main-session-recovery-scan", () =>
675+
markStartupOrphanedMainSessionsForRecovery({ cfg: params.cfg }),
676+
);
677+
} catch (err) {
678+
params.log.warn(
679+
`main-session startup orphan marking failed before channel startup: ${String(err)}`,
680+
);
681+
}
682+
});
661683
// Agent RPC remains available when transports are disabled. Publish configured/static facts before
662684
// accepting work; live provider catalogs stay advisory and never enter the Gateway lifecycle.
663685
await measureStartup(params.startupTrace, "sidecars.model-runtime", () =>
@@ -671,17 +693,6 @@ export async function startGatewaySidecars(params: {
671693
params.prewarmPrimaryModel,
672694
),
673695
);
674-
await measureStartup(params.startupTrace, "sidecars.main-session-recovery", async () => {
675-
try {
676-
const { markStartupOrphanedMainSessionsForRecovery } =
677-
await loadMainSessionRestartRecoveryModule();
678-
await markStartupOrphanedMainSessionsForRecovery({ cfg: params.cfg });
679-
} catch (err) {
680-
params.log.warn(
681-
`main-session startup orphan marking failed before channel startup: ${String(err)}`,
682-
);
683-
}
684-
});
685696
await measureStartup(params.startupTrace, "sidecars.channels", async () => {
686697
if (!skipChannels) {
687698
try {

0 commit comments

Comments
 (0)