Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions src/lib/onboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ const pRetry = require("p-retry");
const runner: typeof import("./runner") = require("./runner");
const { ROOT, SCRIPTS, redact, run, runCapture, runCaptureEx, runFile, validateName } = runner;
const braveProviderProfile: typeof import("./onboard/brave-provider-profile") = require("./onboard/brave-provider-profile");
const { runSandboxProviderPreDeleteCleanup } =
const { reconcileRegisteredExtraProviders, runSandboxProviderPreDeleteCleanup } =
require("./onboard/sandbox-provider-cleanup") as typeof import("./onboard/sandbox-provider-cleanup");
const nameValidation: typeof import("./name-validation") = require("./name-validation");
const { getNameValidationGuidance } = nameValidation;
Expand Down Expand Up @@ -2749,7 +2749,7 @@ async function createSandboxWithBaseImageResolution(
messagingTokenDefs,
reusableMessagingChannels,
reusableMessagingProviders,
extraProviders: registry.listExtraProviders(),
extraProviders: reconcileRegisteredExtraProviders({ runOpenshell, gatewayName: GATEWAY_NAME }),
hermesToolGateways,
sandboxGpuConfig: effectiveSandboxGpuConfig,
dockerDriverGateway,
Expand Down
123 changes: 123 additions & 0 deletions src/lib/onboard/extra-provider-reconciliation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

import { assertNoOpenShellGatewayEndpointOverride } from "../openshell-gateway-endpoint-guard";
import type { SandboxProviderRunOpenshell } from "./sandbox-provider-cleanup";

export type ReconcileExtraProvidersDeps = {
runOpenshell?: SandboxProviderRunOpenshell;
/**
* Scope existence probes to this gateway (`provider get -g <name>`),
* mirroring the gateway-scoped runner the other onboarding provider
* probes use. When set, the same endpoint-override guard applies.
*/
gatewayName?: string;
listExtraProviders?: () => string[];
forgetExtraProvider?: (name: string) => boolean;
warn?: (message: string) => void;
};

/**
* Diagnostic shapes for "the probed provider does not exist": both the CLI's
* `provider 'X' not found` and the gRPC-style `NotFound: provider "X"`
* orderings. Anchored to the word "provider" on the same line so missing-
* sandbox or missing-gateway errors never count as a provider-not-found.
*/
const PROVIDER_NOT_FOUND_RE =
/provider[^\n]{0,200}?(?:\bNotFound\b|\bnot\s+found\b)|(?:\bNotFound\b|\bnot\s+found\b)(?::|\s)[^\n]{0,200}?\bprovider\b/i;
Comment on lines +20 to +27

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Map the relevant file and nearby tests/usages first.
git ls-files 'src/lib/onboard/extra-provider-reconciliation.ts' \
  'src/lib/onboard/**' \
  'src/**/__tests__/**' \
  'src/**/test/**' \
  'test/**' | sed -n '1,200p'

echo
echo "== outline =="
ast-grep outline src/lib/onboard/extra-provider-reconciliation.ts --view expanded || true

echo
echo "== search for regex/tests/usages =="
rg -n "PROVIDER_NOT_FOUND_RE|provider not found|NotFound: provider|not found.*provider|gateway|sandbox" src/lib/onboard src test -S || true

echo
echo "== file excerpt =="
wc -l src/lib/onboard/extra-provider-reconciliation.ts
sed -n '1,220p' src/lib/onboard/extra-provider-reconciliation.ts

Repository: NVIDIA/NemoClaw

Length of output: 50372


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== extra-provider-reconciliation.ts =="
sed -n '1,220p' src/lib/onboard/extra-provider-reconciliation.ts

echo
echo "== nearby tests/usages =="
rg -n "PROVIDER_NOT_FOUND_RE|reconcileRegisteredExtraProviders|forget\\(|provider-not-found|provider not found|NotFound: provider|not found.*provider" \
  src/lib/onboard src/lib -g '!**/dist/**' -g '!**/build/**' -S

echo
echo "== regex behavior probe =="
python3 - <<'PY'
import re

pattern = re.compile(r'provider[^\n]{0,200}?(?:\bNotFound\b|\bnot\s+found\b)|(?:\bNotFound\b|\bnot\s+found\b)(?::|\s)[^\n]{0,200}?\bprovider\b', re.I)

samples = [
    'provider "alpha" not found',
    'NotFound: provider "alpha"',
    'gateway "nemoclaw" not found while listing provider records',
    'sandbox "foo" not found; provider "alpha" is cached',
    'missing-gateway: provider "alpha" not found',
    'NotFound: gateway "nemoclaw" not found while listing provider records',
    'provider "alpha" NotFound',
]

for s in samples:
    print(f"{pattern.search(s) is not None!s:5}  {s}")
PY

Repository: NVIDIA/NemoClaw

Length of output: 9941


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== extra-provider reconciliation tests =="
fd -a 'extra-provider-reconciliation.test.ts|onboard-extra-provider-prune.test.ts|rebuild-provider-preflight.test.ts' test src || true

echo
echo "== extra-provider-reconciliation.test.ts =="
sed -n '1,240p' test/extra-provider-reconciliation.test.ts

echo
echo "== onboard-extra-provider-prune.test.ts =="
sed -n '1,260p' test/onboard-extra-provider-prune.test.ts

echo
echo "== gateway / outage related samples in tests =="
rg -n "gateway.*not found|not found.*gateway|provider not found|provider-not-found|fail-open|keep.*provider|skip.*provider|reconcileRegisteredExtraProviders" \
  test src/lib -S

Repository: NVIDIA/NemoClaw

Length of output: 27725


Tighten the provider-not-found match The second branch still matches diagnostics like NotFound: gateway ... not found while listing provider records, so a gateway outage can prune a healthy provider record. Narrow it to the provider subject itself or add a gateway-down regression case.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/lib/onboard/extra-provider-reconciliation.ts` around lines 20 - 27, The
provider-not-found detection in PROVIDER_NOT_FOUND_RE is still too broad because
the second branch can match gateway-related diagnostics; tighten the regex in
extra-provider-reconciliation so it only matches when the “provider” subject is
the thing reported as not found, or add an explicit guard to exclude
gateway/sandbox diagnostics. Update the matching logic around
PROVIDER_NOT_FOUND_RE and verify the reconciliation flow that uses it only
prunes providers for true provider-not-found errors.


function toText(value: string | Buffer | null | undefined): string {
if (typeof value === "string") return value;
if (value && typeof (value as Buffer).toString === "function") {
return (value as Buffer).toString();
}
return "";
}

function defaultRunOpenshell(
args: string[],
opts?: Record<string, unknown>,
): ReturnType<SandboxProviderRunOpenshell> {
const runtime = require("../adapters/openshell/runtime") as {
runOpenshell: SandboxProviderRunOpenshell;
};
return runtime.runOpenshell(args, opts);
}

function defaultListExtraProviders(): string[] {
const { listExtraProviders } = require("../state/registry") as {
listExtraProviders: () => string[];
};
return listExtraProviders();
}

function defaultForgetExtraProvider(name: string): boolean {
const { removeExtraProvider } = require("../state/registry") as {
removeExtraProvider: (name: string) => boolean;
};
return removeExtraProvider(name);
}

// SOURCE_OF_TRUTH_REVIEW (extra-provider registry vs gateway drift, #6501):
// invalid state = the host registry records an extra provider (written by
// `credentials add` → `addExtraProvider`) that the gateway no longer knows,
// created by gateway-side `provider delete` or pointing the CLI at a rebuilt
// gateway — neither path can update the host record because OpenShell emits
// no provider-deletion signal the CLI could observe. Passing the dangling
// name to `sandbox create --provider` then fails every subsequent onboard
// with "provider not found", even when the user declined the feature that
// once created it. Reconciling at consumption time recovers regardless of
// how the desync happened. Regression proof lives in
// test/extra-provider-reconciliation.test.ts and the spawn-level onboard
// test in test/onboard-extra-provider-prune.test.ts. Remove this helper when
// OpenShell exposes a structured provider-deletion event (or the registry
// stops mirroring gateway provider state).
/**
* Resolve the registry-recorded extra providers that sandbox creation may
* attach, dropping records the gateway no longer knows about (#6501).
*
* Each recorded name is probed with `provider get` (the same existence
* check `upsertProvider` uses); a record is pruned only when the gateway
* explicitly answers "provider … not found". Any other failure — gateway
* down, timeout, unexpected diagnostic — keeps the record (fail-open, with
* a debug note for diagnosability) so a real outage still surfaces through
* the sandbox-create diagnostics instead of silently dropping a healthy
* provider.
*/
export function reconcileRegisteredExtraProviders(
deps: ReconcileExtraProvidersDeps = {},
): string[] {
const recorded = (deps.listExtraProviders ?? defaultListExtraProviders)();
if (recorded.length === 0) return recorded;
if (deps.gatewayName) assertNoOpenShellGatewayEndpointOverride();
const gatewayArgs = deps.gatewayName ? ["-g", deps.gatewayName] : [];
const runOpenshell = deps.runOpenshell ?? defaultRunOpenshell;
const warn = deps.warn ?? ((message: string) => console.warn(message));
const forget = deps.forgetExtraProvider ?? defaultForgetExtraProvider;
return recorded.filter((name) => {
const result = runOpenshell(["provider", "get", ...gatewayArgs, name], {
ignoreError: true,
stdio: ["ignore", "pipe", "pipe"],
suppressOutput: true,
});
if (result.status === 0) return true;
const output = `${toText(result.stdout)}${toText(result.stderr)}`;
if (!PROVIDER_NOT_FOUND_RE.test(output)) {
console.debug(
`reconcileRegisteredExtraProviders: keeping '${name}' — existence probe failed without a provider-not-found diagnostic (fail-open).`,
);
return true;
}
warn(
` Skipping recorded provider '${name}': not registered with the OpenShell gateway. ` +
`Removing the stale local record; recreate it with 'nemoclaw credentials add' if needed.`,
);
try {
forget(name);
} catch {
// A registry write failure must not abort onboarding — the dangling
// record is still skipped for this run and re-pruned on the next one.
}
return false;
});
}
5 changes: 5 additions & 0 deletions src/lib/onboard/sandbox-provider-cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import { listMessagingProviderSuffixes } from "../messaging/channels";
import { NAME_MAX_LENGTH, NAME_VALID_PATTERN } from "../name-validation";

export {
type ReconcileExtraProvidersDeps,
reconcileRegisteredExtraProviders,
} from "./extra-provider-reconciliation";

export type SandboxProviderRunOpenshell = (
args: string[],
opts?: Record<string, unknown>,
Expand Down
132 changes: 132 additions & 0 deletions test/extra-provider-reconciliation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

import { describe, expect, it, vi } from "vitest";

import { reconcileRegisteredExtraProviders } from "../src/lib/onboard/extra-provider-reconciliation.js";

type Argv = string[];
type RunResult = { status: number | null; stderr?: string; stdout?: string | Buffer };

function buildRunOpenshell(
responses: Map<string, RunResult>,
defaultResponse: RunResult = { status: 0 },
) {
const calls: Argv[] = [];
const fn = vi.fn((args: Argv, _opts?: Record<string, unknown>) => {
calls.push(args);
const key = args.join(" ");
return responses.get(key) ?? defaultResponse;
});
return { runOpenshell: fn, calls };
}

describe("reconcileRegisteredExtraProviders", () => {
it("returns the empty set without querying the gateway when nothing is recorded", () => {
const { runOpenshell, calls } = buildRunOpenshell(new Map());
const forget = vi.fn();

const result = reconcileRegisteredExtraProviders({
runOpenshell,
listExtraProviders: () => [],
forgetExtraProvider: forget,
});

expect(result).toEqual([]);
expect(calls).toEqual([]);
expect(forget).not.toHaveBeenCalled();
});

it("keeps recorded providers that the gateway confirms via a scoped 'provider get'", () => {
const responses = new Map([
["provider get -g nemoclaw tavily-search", { status: 0, stdout: "name: tavily-search\n" }],
]);
const { runOpenshell, calls } = buildRunOpenshell(responses, { status: 1 });
const forget = vi.fn();
const warn = vi.fn();

const result = reconcileRegisteredExtraProviders({
runOpenshell,
gatewayName: "nemoclaw",
listExtraProviders: () => ["tavily-search"],
forgetExtraProvider: forget,
warn,
});

expect(result).toEqual(["tavily-search"]);
expect(calls).toEqual([["provider", "get", "-g", "nemoclaw", "tavily-search"]]);
expect(forget).not.toHaveBeenCalled();
expect(warn).not.toHaveBeenCalled();
});

it("skips, warns about, and forgets a recorded provider the gateway reports not found (#6501)", () => {
const responses = new Map<string, RunResult>([
[
"provider get brave-search",
{ status: 1, stderr: "Error: provider 'brave-search' not found\n" },
],
[
"provider get tavily-search",
{ status: 1, stderr: 'rpc error: NotFound: provider "tavily-search"\n' },
],
]);
const { runOpenshell } = buildRunOpenshell(responses);
const forget = vi.fn();
const warn = vi.fn();

const result = reconcileRegisteredExtraProviders({
runOpenshell,
listExtraProviders: () => ["brave-search", "tavily-search"],
forgetExtraProvider: forget,
warn,
});

expect(result).toEqual([]);
expect(forget.mock.calls.map((c) => c[0])).toEqual(["brave-search", "tavily-search"]);
const messages = warn.mock.calls.map((c) => c[0] as string);
expect(messages[0]).toContain("'brave-search'");
expect(messages[1]).toContain("'tavily-search'");
expect(messages[1]).toContain("nemoclaw credentials add");
});

it("keeps the recorded set unchanged when the probe fails without a not-found diagnostic", () => {
const responses = new Map<string, RunResult>([
["provider get tavily-search", { status: 1, stderr: "gateway not running" }],
]);
const { runOpenshell } = buildRunOpenshell(responses);
const forget = vi.fn();
const warn = vi.fn();

const result = reconcileRegisteredExtraProviders({
runOpenshell,
listExtraProviders: () => ["tavily-search"],
forgetExtraProvider: forget,
warn,
});

expect(result).toEqual(["tavily-search"]);
expect(forget).not.toHaveBeenCalled();
expect(warn).not.toHaveBeenCalled();
});

it("prunes on a Buffer not-found diagnostic while keeping confirmed bridge providers", () => {
const responses = new Map<string, RunResult>([
["provider get my-slack-bridge", { status: 0 }],
[
"provider get tavily-search",
{ status: 1, stdout: Buffer.from("provider 'tavily-search' not found\n") },
],
]);
const { runOpenshell } = buildRunOpenshell(responses);
const forget = vi.fn();

const result = reconcileRegisteredExtraProviders({
runOpenshell,
listExtraProviders: () => ["my-slack-bridge", "tavily-search"],
forgetExtraProvider: forget,
});

expect(result).toEqual(["my-slack-bridge"]);
expect(forget.mock.calls.map((c) => c[0])).toEqual(["tavily-search"]);
});
});
Loading
Loading