Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

const {
startBedrockRuntimeAdapterFromEnv,
} = require("../dist/lib/inference/bedrock-runtime-adapter");
import { startBedrockRuntimeAdapterFromEnv } from "../dist/lib/inference/bedrock-runtime-adapter.js";

try {
startBedrockRuntimeAdapterFromEnv();
Expand Down
37 changes: 37 additions & 0 deletions src/lib/inference/bedrock-runtime-adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
createOpenAiChatCompletion,
streamOpenAiChatCompletion,
} from "./bedrock-runtime-adapter";
import { isLocalAdapterProcess } from "./local-adapter-lifecycle";

const servers: http.Server[] = [];

Expand Down Expand Up @@ -382,6 +383,42 @@ describe("Bedrock Runtime OpenAI adapter", () => {
expect(body.error.message).toContain("Could not load credentials");
});

it("spawns the typed .mts launcher entrypoint", () => {
expect(__test.getAdapterScriptPath().endsWith("bedrock-runtime-adapter.mts")).toBe(true);
});

it("recognizes adapter processes launched from the old and new launcher filenames", () => {
const needle = __test.adapterProcessNeedle;
expect(
isLocalAdapterProcess(
4321,
needle,
() => "node /opt/nemoclaw/scripts/bedrock-runtime-adapter.mts",
),
).toBe(true);
expect(
isLocalAdapterProcess(
4321,
needle,
() => "node /opt/nemoclaw/scripts/bedrock-runtime-adapter.js",
),
).toBe(true);
expect(
isLocalAdapterProcess(
4321,
needle,
() => "node /opt/nemoclaw/scripts/openrouter-runtime-adapter-entry.js",
),
).toBe(false);
expect(
isLocalAdapterProcess(
4321,
needle,
() => "node /opt/nemoclaw/scripts/my-bedrock-runtime-adapter.mts",
),
).toBe(false);
});

it("includes forwarded AWS environment in the adapter reuse hash", () => {
const savedContainerCredentials = process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI;
const savedSharedCredentials = process.env.AWS_SHARED_CREDENTIALS_FILE;
Expand Down
15 changes: 12 additions & 3 deletions src/lib/inference/bedrock-runtime-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,22 +316,29 @@ function loadPersistedPid(): number | null {
return loadLocalAdapterPid(PID_PATH);
}

const ADAPTER_LAUNCHER_BASENAMES = ["bedrock-runtime-adapter.mts", "bedrock-runtime-adapter.js"];
const ADAPTER_PROCESS_NEEDLE = new RegExp(
`(?:^|[^A-Za-z0-9_.-])(?:${ADAPTER_LAUNCHER_BASENAMES.map((name) =>
name.replaceAll(".", "\\."),
).join("|")})(?:$|[^A-Za-z0-9_.-])`,
);

function isAdapterProcess(pid: number | null | undefined): boolean {
return isLocalAdapterProcess(pid, "bedrock-runtime-adapter.js", runCapture);
return isLocalAdapterProcess(pid, ADAPTER_PROCESS_NEEDLE, runCapture);
}

function killStaleAdapter(): void {
killLocalAdapterPid({
pidPath: PID_PATH,
processNeedle: "bedrock-runtime-adapter.js",
processNeedle: ADAPTER_PROCESS_NEEDLE,
run,
runCapture,
});
}

function getAdapterScriptPath(): string {
const scriptsDir = typeof SCRIPTS === "string" ? SCRIPTS : path.join(process.cwd(), "scripts");
return path.join(scriptsDir, "bedrock-runtime-adapter.js");
return path.join(scriptsDir, "bedrock-runtime-adapter.mts");
}

function copyAwsEnv(extra: Record<string, string>): void {
Expand Down Expand Up @@ -478,4 +485,6 @@ export function getCompatibleAnthropicCredentialForBedrock(): string | null {

export const __test = {
adapterCredentialHash,
adapterProcessNeedle: ADAPTER_PROCESS_NEEDLE,
getAdapterScriptPath,
};
12 changes: 8 additions & 4 deletions src/lib/inference/local-adapter-lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,21 @@ export function loadLocalAdapterPid(filePath: string): number | null {

export function isLocalAdapterProcess(
pid: number | null | undefined,
processNeedle: string,
processNeedle: string | RegExp,
runCapture: RunCaptureFn,
): boolean {
if (!Number.isInteger(pid) || !pid || pid <= 0) return false;
const cmdline = runCapture(["ps", "-p", String(pid), "-o", "args="], { ignoreError: true });
return Boolean(String(cmdline || "").includes(processNeedle));
const cmdline = String(
runCapture(["ps", "-p", String(pid), "-o", "args="], { ignoreError: true }) || "",
);
return typeof processNeedle === "string"
? cmdline.includes(processNeedle)
: processNeedle.test(cmdline);
}

export function killLocalAdapterPid(options: {
pidPath: string;
processNeedle: string;
processNeedle: string | RegExp;
run: RunFn;
runCapture: RunCaptureFn;
}): void {
Expand Down
8 changes: 5 additions & 3 deletions test/e2e/live/bedrock-runtime-compatible-anthropic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,11 +581,13 @@ function stopBedrockAdapter(home: string): void {
}
}

const BEDROCK_ADAPTER_LAUNCHER_PATTERN =
/(?:^|[^A-Za-z0-9_.-])bedrock-runtime-adapter\.(?:mts|js)(?:$|[^A-Za-z0-9_.-])/;

function isBedrockAdapterProcess(pid: number): boolean {
const expectedScript = "bedrock-runtime-adapter.js";
try {
const cmdline = fs.readFileSync(`/proc/${pid}/cmdline`, "utf8").replaceAll("\0", " ");
if (cmdline.includes(expectedScript)) return true;
if (BEDROCK_ADAPTER_LAUNCHER_PATTERN.test(cmdline)) return true;
} catch {
// Fall back to ps on platforms without procfs.
}
Expand All @@ -594,7 +596,7 @@ function isBedrockAdapterProcess(pid: number): boolean {
encoding: "utf8",
stdio: ["ignore", "pipe", "ignore"],
});
return ps.status === 0 && ps.stdout.includes(expectedScript);
return ps.status === 0 && BEDROCK_ADAPTER_LAUNCHER_PATTERN.test(ps.stdout);
}

async function restoreHostsFile(
Expand Down
Loading