Skip to content

Commit 76e78cd

Browse files
committed
fix(cron): mirror runner env proxy in model preflight
The cron model preflight used STRICT DNS pinning while the model runner selects the env HTTP proxy for proxy-routed local vhosts (e.g. `inference.local`). That mismatch made the preflight false-negative and skip every run. Apply the same `shouldUseEnvHttpProxyForUrl` selection as the runner via `withTrustedEnvProxyGuardedFetchMode`; the hostname allowlist and allowPrivateNetwork policy still apply. Ref: NVIDIA/NemoClaw#5129
1 parent 6418e19 commit 76e78cd

2 files changed

Lines changed: 93 additions & 6 deletions

File tree

‎src/cron/isolated-agent/model-preflight.runtime.test.ts‎

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
// Runtime model preflight tests cover provider/model checks before cron execution.
22
import { beforeEach, describe, expect, it, vi } from "vitest";
33

4-
const { fetchWithSsrFGuardMock } = vi.hoisted(() => ({
4+
const {
5+
fetchWithSsrFGuardMock,
6+
shouldUseEnvHttpProxyForUrlMock,
7+
withTrustedEnvProxyGuardedFetchModeMock,
8+
} = vi.hoisted(() => ({
59
fetchWithSsrFGuardMock: vi.fn(),
10+
shouldUseEnvHttpProxyForUrlMock: vi.fn(() => false),
11+
withTrustedEnvProxyGuardedFetchModeMock: vi.fn((params: Record<string, unknown>) => ({
12+
...params,
13+
mode: "trusted_env_proxy",
14+
})),
615
}));
716

817
vi.mock("../../infra/net/fetch-guard.js", () => ({
918
fetchWithSsrFGuard: fetchWithSsrFGuardMock,
19+
withTrustedEnvProxyGuardedFetchMode: withTrustedEnvProxyGuardedFetchModeMock,
20+
}));
21+
22+
vi.mock("../../infra/net/proxy-env.js", () => ({
23+
shouldUseEnvHttpProxyForUrl: shouldUseEnvHttpProxyForUrlMock,
1024
}));
1125

1226
import {
@@ -25,9 +39,10 @@ function requireFetchPreflightRequest(): {
2539
url?: string;
2640
timeoutMs?: number;
2741
auditContext?: string;
42+
mode?: string;
2843
} {
2944
const request = fetchWithSsrFGuardMock.mock.calls[0]?.[0] as
30-
| { url?: string; timeoutMs?: number; auditContext?: string }
45+
| { url?: string; timeoutMs?: number; auditContext?: string; mode?: string }
3146
| undefined;
3247
if (!request) {
3348
throw new Error("Expected cron model preflight fetch request");
@@ -38,6 +53,9 @@ function requireFetchPreflightRequest(): {
3853
describe("preflightCronModelProvider", () => {
3954
beforeEach(() => {
4055
fetchWithSsrFGuardMock.mockReset();
56+
shouldUseEnvHttpProxyForUrlMock.mockReset();
57+
shouldUseEnvHttpProxyForUrlMock.mockReturnValue(false);
58+
withTrustedEnvProxyGuardedFetchModeMock.mockClear();
4159
resetCronModelProviderPreflightCacheForTest();
4260
});
4361

@@ -171,4 +189,59 @@ describe("preflightCronModelProvider", () => {
171189
expect(second).toEqual({ status: "available" });
172190
expect(fetchWithSsrFGuardMock).toHaveBeenCalledTimes(2);
173191
});
192+
193+
it("uses trusted-env-proxy mode when the env proxy applies, mirroring the runner", async () => {
194+
// Proxy-routed local vhosts (e.g. `inference.local`) need the env proxy;
195+
// STRICT pinning would EAI_AGAIN. The runner uses env proxy, so the
196+
// preflight must mirror it or it false-negatives and skips every run.
197+
shouldUseEnvHttpProxyForUrlMock.mockReturnValue(true);
198+
mockReachableResponse(200);
199+
200+
const result = await preflightCronModelProvider({
201+
cfg: {
202+
models: {
203+
providers: {
204+
openshell: {
205+
api: "openai-completions",
206+
baseUrl: "http://inference.local/v1",
207+
models: [],
208+
},
209+
},
210+
},
211+
},
212+
provider: "openshell",
213+
model: "qwen3",
214+
});
215+
216+
expect(result).toEqual({ status: "available" });
217+
expect(withTrustedEnvProxyGuardedFetchModeMock).toHaveBeenCalledTimes(1);
218+
const request = requireFetchPreflightRequest();
219+
expect(request.url).toBe("http://inference.local/v1/models");
220+
expect(request.mode).toBe("trusted_env_proxy");
221+
});
222+
223+
it("keeps strict mode when the env proxy does not apply", async () => {
224+
mockReachableResponse(200);
225+
226+
const result = await preflightCronModelProvider({
227+
cfg: {
228+
models: {
229+
providers: {
230+
vllm: {
231+
api: "openai-completions",
232+
baseUrl: "http://127.0.0.1:8000/v1",
233+
models: [],
234+
},
235+
},
236+
},
237+
},
238+
provider: "vllm",
239+
model: "llama",
240+
});
241+
242+
expect(result).toEqual({ status: "available" });
243+
expect(withTrustedEnvProxyGuardedFetchModeMock).not.toHaveBeenCalled();
244+
const request = requireFetchPreflightRequest();
245+
expect(request.mode).toBeUndefined();
246+
});
174247
});

‎src/cron/isolated-agent/model-preflight.runtime.ts‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { normalizeProviderId } from "@openclaw/model-catalog-core/provider-id";
33
import { normalizeLowercaseStringOrEmpty } from "@openclaw/normalization-core/string-coerce";
44
import type { ModelProviderConfig } from "../../config/types.models.js";
55
import type { OpenClawConfig } from "../../config/types.openclaw.js";
6-
import { fetchWithSsrFGuard } from "../../infra/net/fetch-guard.js";
6+
import {
7+
fetchWithSsrFGuard,
8+
withTrustedEnvProxyGuardedFetchMode,
9+
} from "../../infra/net/fetch-guard.js";
10+
import { shouldUseEnvHttpProxyForUrl } from "../../infra/net/proxy-env.js";
711
import type { SsrFPolicy } from "../../infra/net/ssrf.js";
812

913
const PREFLIGHT_CACHE_TTL_MS = 5 * 60_000;
@@ -161,13 +165,23 @@ async function probeLocalProviderEndpoint(params: {
161165
api: PreflightApi;
162166
baseUrl: string;
163167
}): Promise<void> {
164-
const { response, release } = await fetchWithSsrFGuard({
165-
url: buildProbeUrl(params.api, params.baseUrl),
168+
// Mirror the model runner's proxy selection (provider-transport-fetch) so
169+
// the preflight predicts the same reachability. Env proxy resolves
170+
// proxy-routed local vhosts (e.g. `inference.local`) that STRICT DNS pinning
171+
// cannot; without it the preflight false-negatives and skips every run.
172+
// The hostname allowlist + allowPrivateNetwork policy still apply.
173+
const url = buildProbeUrl(params.api, params.baseUrl);
174+
const useEnvProxy = shouldUseEnvHttpProxyForUrl(url);
175+
const baseOptions = {
176+
url,
166177
init: { method: "GET" },
167178
policy: buildLocalProviderSsrFPolicy(params.baseUrl),
168179
timeoutMs: PREFLIGHT_TIMEOUT_MS,
169180
auditContext: "cron-model-provider-preflight",
170-
});
181+
};
182+
const { response, release } = await fetchWithSsrFGuard(
183+
useEnvProxy ? withTrustedEnvProxyGuardedFetchMode(baseOptions) : baseOptions,
184+
);
171185
try {
172186
// Any HTTP response means the local endpoint is alive. Auth/model errors
173187
// still belong to the normal model runner where fallback and diagnostics

0 commit comments

Comments
 (0)