Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/lib/inference/local-adapter-lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
isLocalAdapterProcess,
killLocalAdapterPid,
loadLocalAdapterPid,
LOCAL_ADAPTER_HEALTH_MAX_RESPONSE_BYTES,
localAdapterTokenHash,
persistLocalAdapterPid,
probeLocalAdapterHealth,
Expand Down Expand Up @@ -155,6 +156,23 @@ describe("local adapter lifecycle", () => {
}),
).resolves.toBe(false);
});

it("fails closed when a chunked health response exceeds the memory budget", async () => {
const server = http.createServer((_req, res) => {
res.writeHead(200, { "Content-Type": "application/json" });
res.write(Buffer.alloc(LOCAL_ADAPTER_HEALTH_MAX_RESPONSE_BYTES, 0x20));
res.end(Buffer.from([0x20]));
});
const port = await listen(server);

await expect(
probeLocalAdapterHealth({
host: "127.0.0.1",
port,
expectedTokenHash: localAdapterTokenHash("secret-token"),
}),
).resolves.toBe(false);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
});
});

describe("ensureLocalAdapterStateDir", () => {
Expand Down
47 changes: 40 additions & 7 deletions src/lib/inference/local-adapter-lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type RunFn = (
export type LocalAdapterProcessMatcher = string | RegExp | ((commandLine: string) => boolean);

export const DEFAULT_LOCAL_ADAPTER_STATE_DIR = nemoclawStateRoot(os.homedir(), GATEWAY_PORT);
export const LOCAL_ADAPTER_HEALTH_MAX_RESPONSE_BYTES = 64 * 1024;

export function ensureLocalAdapterStateDir(stateDir = DEFAULT_LOCAL_ADAPTER_STATE_DIR): void {
rejectSymlinksOnPath(stateDir);
Expand Down Expand Up @@ -191,6 +192,12 @@ export function probeLocalAdapterHealth(options: {
tokenHashField?: string;
}): Promise<boolean> {
return new Promise((resolve) => {
let settled = false;
const settle = (healthy: boolean) => {
if (settled) return;
settled = true;
resolve(healthy);
};
const req = http.request(
{
hostname: options.host,
Expand All @@ -200,31 +207,57 @@ export function probeLocalAdapterHealth(options: {
timeout: options.timeoutMs || 1000,
},
(res) => {
const declaredBytes = Number(res.headers["content-length"]);
if (
Number.isFinite(declaredBytes) &&
declaredBytes > LOCAL_ADAPTER_HEALTH_MAX_RESPONSE_BYTES
) {
res.destroy();
settle(false);
return;
}

const chunks: Buffer[] = [];
res.on("data", (chunk) => chunks.push(Buffer.from(chunk)));
let receivedBytes = 0;
res.on("data", (chunk) => {
if (settled) return;
const chunkBytes = Buffer.isBuffer(chunk) ? chunk.length : Buffer.byteLength(chunk);
if (receivedBytes + chunkBytes > LOCAL_ADAPTER_HEALTH_MAX_RESPONSE_BYTES) {
chunks.length = 0;
res.destroy();
settle(false);
return;
}
receivedBytes += chunkBytes;
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
chunks.push(buffer);
});
res.on("aborted", () => settle(false));
res.on("error", () => settle(false));
res.on("end", () => {
if (settled) return;
if (res.statusCode !== 200) {
resolve(false);
settle(false);
return;
}
if (!options.expectedTokenHash) {
resolve(true);
settle(true);
return;
}
try {
const body = JSON.parse(Buffer.concat(chunks).toString("utf8")) as JsonObject;
resolve(body[options.tokenHashField || "tokenHash"] === options.expectedTokenHash);
settle(body[options.tokenHashField || "tokenHash"] === options.expectedTokenHash);
} catch {
resolve(false);
settle(false);
}
});
},
);
req.on("timeout", () => {
req.destroy();
resolve(false);
settle(false);
});
req.on("error", () => resolve(false));
req.on("error", () => settle(false));
req.end();
});
}
Expand Down
Loading