Skip to content
37 changes: 26 additions & 11 deletions nemoclaw-blueprint/scripts/nemotron-inference-fix.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,9 @@
// field (the endpoint returns `reasoning_content` either way), so the field
// carries no behavior to preserve.
//
// Scope: the exact `nvidia/nemotron-3-ultra-550b-a55b` model ID reproduced
// in #6913. Prefix collisions and other Nemotron-3 IDs preserve their
// top-level `thinking` field unless their own accepted scope establishes the
// same endpoint contract.
// Scope: the `nvidia/nemotron-3-` model family. Ultra, Super, and Nano all
// reject the top-level field on NVIDIA Build, while unrelated models such as
// `openai/gpt-oss-120b` accept it and must remain untouched.
//
// Source boundary: NemoClaw owns the sandbox preload that wraps outgoing
// chat-completions traffic. The `thinking` field originates in OpenClaw's
Expand Down Expand Up @@ -145,9 +144,9 @@
// set `chat_template_kwargs.thinking` (a chat-template arg the endpoint
// accepts); this strips the *top-level* `thinking` request field entirely.
//
// Scope is the exact Ultra model ID accepted by #6913. Do not infer support
// for suffix variants or other Nemotron-3 models from this workaround.
var STRIP_TOP_LEVEL_THINKING_RE = /^nvidia\/nemotron-3-ultra-550b-a55b$/i;
// Scope is the Nemotron-3 family verified by #6913. Do not widen this to all
// NVIDIA endpoint models: some models accept and use top-level `thinking`.
var STRIP_TOP_LEVEL_THINKING_RE = /^nvidia\/nemotron-3-/i;

// #4851: Ultra 550B silently drops intermediate steps from `content` when
// asked to perform multi-step tasks without execution-capable tools —
Expand Down Expand Up @@ -310,12 +309,12 @@
return true;
}

function patchJsonBody(raw) {
function patchJsonBody(raw, stripTopLevelThinking) {
try {
var body = JSON.parse(raw.toString('utf-8'));
var changed = false;
if (applyChatTemplateKwargs(body)) changed = true;
if (applyStripTopLevelThinking(body)) changed = true;
if (stripTopLevelThinking && applyStripTopLevelThinking(body)) changed = true;
if (applyToolLessSystemPrompt(body)) changed = true;
if (!changed) return null;
return Buffer.from(JSON.stringify(body), 'utf-8');
Expand Down Expand Up @@ -346,6 +345,22 @@
return isChatCompletionsPost(fetchMethod(input, init), fetchUrl(input));
}

function isManagedBuildHost(host) {
Comment thread
prekshivyas marked this conversation as resolved.
Resolved
return /^inference\.local(?::\d+)?$/i.test(String(host || ''));
}

function isManagedBuildFetch(input) {
try {
return new URL(fetchUrl(input)).hostname.toLowerCase() === 'inference.local';
} catch (_e) {
return false;
}
}

function isManagedBuildRequest(options) {
return isManagedBuildHost(options.hostname || options.host);
}

function headersWithoutContentLength(headers) {
if (typeof Headers !== 'undefined' && headers instanceof Headers) {
var copy = new Headers(headers);
Expand Down Expand Up @@ -431,7 +446,7 @@
return origFetch.apply(this, arguments);
}

var modified = patchJsonBody(await rawPromise);
var modified = patchJsonBody(await rawPromise, isManagedBuildFetch(input));
if (!modified) {
return origFetch.apply(this, arguments);
}
Expand Down Expand Up @@ -479,7 +494,7 @@
}

var raw = Buffer.concat(chunks);
var modified = patchJsonBody(raw);
var modified = patchJsonBody(raw, isManagedBuildRequest(options));
var bodyToSend = modified || raw;
if (modified && req.getHeader && req.setHeader) {
req.removeHeader('content-length');
Expand Down
71 changes: 47 additions & 24 deletions test/nemotron-inference-fix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ console.log(JSON.stringify(records));
expect(JSON.parse(records[6].writes[0]).chat_template_kwargs).toBeUndefined();
});

it("preload strips top-level `thinking` only for the exact Ultra model ID (#6913)", () => {
it("preload strips top-level `thinking` only for the Nemotron-3 family (#6913)", () => {
const preload = extractStartScriptHeredoc(src, "NEMOTRON_FIX_EOF");
const harness = `
const http = require('http');
Expand Down Expand Up @@ -203,14 +203,16 @@ function send(mod, options, body) {
}
// A system message is present so the #4851 tool-less nudge does not fire and
// the assertions stay focused on the top-level thinking strip.
send(https, { method: 'POST', path: '/v1/chat/completions' }, JSON.stringify({ model: 'nvidia/nemotron-3-ultra-550b-a55b', messages: [{ role: 'system', content: 'x' }], thinking: { type: 'enabled' } }));
send(https, { method: 'POST', path: '/v1/chat/completions' }, JSON.stringify({ model: 'nvidia/nemotron-3-ultra-550b-a55b', messages: [{ role: 'system', content: 'x' }], thinking: true }));
send(https, { method: 'POST', path: '/v1/chat/completions' }, JSON.stringify({ model: 'nvidia/nemotron-3-ultra-550b-a55b', messages: [{ role: 'system', content: 'x' }] }));
send(https, { method: 'POST', path: '/v1/chat/completions' }, JSON.stringify({ model: 'nvidia/nemotron-3-ultra-550b-a55b-other', messages: [{ role: 'system', content: 'x' }], thinking: { type: 'enabled' } }));
send(https, { method: 'POST', path: '/v1/chat/completions' }, JSON.stringify({ model: 'nvidia/nemotron-3-super-120b-a12b', messages: [{ role: 'system', content: 'x' }], thinking: { type: 'enabled' } }));
send(https, { method: 'POST', path: '/v1/chat/completions' }, JSON.stringify({ model: 'nvidia/nemotron-3-nano-30b-a3b', messages: [{ role: 'system', content: 'x' }], thinking: true }));
send(https, { method: 'POST', path: '/v1/chat/completions' }, JSON.stringify({ model: 'deepseek-ai/deepseek-v4-pro', messages: [], thinking: { type: 'enabled' } }));
send(https, { method: 'POST', path: '/v1/chat/completions' }, JSON.stringify({ model: 'openai/gpt-oss-120b', messages: [], thinking: { type: 'enabled' } }));
send(http, { method: 'POST', host: 'inference.local', path: '/v1/chat/completions' }, JSON.stringify({ model: 'nvidia/nemotron-3-ultra-550b-a55b', messages: [{ role: 'system', content: 'x' }], thinking: { type: 'enabled' } }));
send(https, { method: 'POST', hostname: 'inference.local', path: '/v1/chat/completions' }, JSON.stringify({ model: 'nvidia/nemotron-3-ultra-550b-a55b', messages: [{ role: 'system', content: 'x' }], thinking: true }));
send(https, { method: 'POST', host: 'inference.local', path: '/v1/chat/completions' }, JSON.stringify({ model: 'nvidia/nemotron-3-ultra-550b-a55b', messages: [{ role: 'system', content: 'x' }] }));
send(https, { method: 'POST', host: 'inference.local', path: '/v1/chat/completions' }, JSON.stringify({ model: 'nvidia/nemotron-4-ultra-550b-a55b', messages: [{ role: 'system', content: 'x' }], thinking: { type: 'enabled' } }));
send(https, { method: 'POST', host: 'inference.local', path: '/v1/chat/completions' }, JSON.stringify({ model: 'nvidia/nemotron-3-super-120b-a12b', messages: [{ role: 'system', content: 'x' }], thinking: { type: 'enabled' } }));
send(https, { method: 'POST', host: 'inference.local', path: '/v1/chat/completions' }, JSON.stringify({ model: 'nvidia/nemotron-3-nano-30b-a3b', messages: [{ role: 'system', content: 'x' }], thinking: true }));
send(https, { method: 'POST', host: 'inference.local', path: '/v1/chat/completions' }, JSON.stringify({ model: 'deepseek-ai/deepseek-v4-pro', messages: [], thinking: { type: 'enabled' } }));
send(https, { method: 'POST', host: 'inference.local', path: '/v1/chat/completions' }, JSON.stringify({ model: 'openai/gpt-oss-120b', messages: [], thinking: { type: 'enabled' } }));
send(http, { method: 'POST', host: 'local-nim.example.com', path: '/v1/chat/completions' }, JSON.stringify({ model: 'nvidia/nemotron-3-super-120b-a12b', messages: [{ role: 'system', content: 'x' }], thinking: { type: 'enabled' } }));
send(https, { method: 'POST', hostname: 'custom-openai.example.com', path: '/v1/chat/completions' }, JSON.stringify({ model: 'nvidia/nemotron-3-nano-30b-a3b', messages: [{ role: 'system', content: 'x' }], thinking: true }));
console.log(JSON.stringify(records));
`;

Expand Down Expand Up @@ -240,11 +242,11 @@ console.log(JSON.stringify(records));
const ultraNone = JSON.parse(records[2].writes[0]);
expect(ultraNone).toEqual(ultraObj);

// Prefix collisions and other Nemotron-3 IDs remain outside the accepted
// strip scope. Their pre-existing force_nonempty_content rewrite remains.
const prefixCollision = JSON.parse(records[3].writes[0]);
expect(prefixCollision).toEqual({
model: "nvidia/nemotron-3-ultra-550b-a55b-other",
// Adjacent Nemotron families remain outside the accepted strip scope. Their
// pre-existing force_nonempty_content rewrite remains.
const otherFamily = JSON.parse(records[3].writes[0]);
expect(otherFamily).toEqual({
model: "nvidia/nemotron-4-ultra-550b-a55b",
messages: [{ role: "system", content: "x" }],
thinking: { type: "enabled" },
chat_template_kwargs: { force_nonempty_content: true },
Expand All @@ -254,15 +256,13 @@ console.log(JSON.stringify(records));
expect(superObj).toEqual({
model: "nvidia/nemotron-3-super-120b-a12b",
messages: [{ role: "system", content: "x" }],
thinking: { type: "enabled" },
chat_template_kwargs: { force_nonempty_content: true },
});

const nanoBool = JSON.parse(records[5].writes[0]);
expect(nanoBool).toEqual({
model: "nvidia/nemotron-3-nano-30b-a3b",
messages: [{ role: "system", content: "x" }],
thinking: true,
chat_template_kwargs: { force_nonempty_content: true },
});

Expand All @@ -285,6 +285,12 @@ console.log(JSON.stringify(records));
messages: [],
thinking: { type: "enabled" },
});

const localNim = JSON.parse(records[8].writes[0]);
expect(localNim.thinking).toEqual({ type: "enabled" });

const customOpenAi = JSON.parse(records[9].writes[0]);
expect(customOpenAi.thinking).toBe(true);
});

it("preload also injects model-specific kwargs for stubbed fetch requests", () => {
Expand All @@ -310,6 +316,22 @@ async function main() {
messages: [{ role: 'user', content: 'hello' }],
}),
});
await fetch('https://inference.local/v1/chat/completions', {
method: 'POST',
body: JSON.stringify({
model: 'nvidia/nemotron-3-super-120b-a12b',
messages: [{ role: 'system', content: 'x' }],
thinking: true,
}),
});
await fetch('https://custom-openai.example.com/v1/chat/completions', {
method: 'POST',
body: JSON.stringify({
model: 'nvidia/nemotron-3-super-120b-a12b',
messages: [{ role: 'system', content: 'x' }],
thinking: true,
}),
});
await fetch('https://inference.local/v1/chat/completions', {
method: 'POST',
headers: new Headers({ 'content-type': 'application/json', 'content-length': '999' }),
Expand Down Expand Up @@ -349,10 +371,12 @@ main().catch((err) => {
const records = JSON.parse(result.stdout.trim());
expect(JSON.parse(records[0].body).chat_template_kwargs).toEqual({ thinking: false });
expect(records[0].contentLength).toBeNull();
expect(JSON.parse(records[1].body).chat_template_kwargs).toEqual({ thinking: false });
expect(records[1].contentLength).toBeNull();
expect(JSON.parse(records[2].body).chat_template_kwargs).toBeUndefined();
expect(records[2].contentLength).toBe("999");
expect(JSON.parse(records[1].body).thinking).toBeUndefined();
expect(JSON.parse(records[2].body).thinking).toBe(true);
expect(JSON.parse(records[3].body).chat_template_kwargs).toEqual({ thinking: false });
expect(records[3].contentLength).toBeNull();
expect(JSON.parse(records[4].body).chat_template_kwargs).toBeUndefined();
expect(records[4].contentLength).toBe("999");
});

it("preload mutates real Node fetch/undici requests and refreshes Content-Length", () => {
Expand Down Expand Up @@ -444,11 +468,10 @@ main().catch((err) => {
const otherBody = JSON.parse(records[2].body);
expect(otherBody.chat_template_kwargs).toBeUndefined();

// #4851/#6913: Ultra rewrites take the fetch/undici path too: top-level
// thinking is stripped, the system message is prepended, and Content-Length
// is refreshed for the changed body.
// #4851: The tool-less system prompt still takes the fetch/undici path, but
// #6913's Build-specific thinking strip leaves this local endpoint intact.
const ultraBody = JSON.parse(records[3].body);
expect("thinking" in ultraBody).toBe(false);
expect(ultraBody.thinking).toEqual({ type: "enabled" });
expect(ultraBody.messages[0].role).toBe("system");
expect(ultraBody.messages[0].content).toMatch(/do not have tools/i);
expect(ultraBody.messages[1]).toEqual({
Expand Down
Loading