-
Notifications
You must be signed in to change notification settings - Fork 3.1k
fix(sandbox): target the owning gateway for status and exec queries #7113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
723b3ec
fix(sandbox): target the owning gateway for status and exec queries
laitingsheng a0091be
fix(sandbox): fail closed when owning-gateway selection fails
laitingsheng d108d4f
test(sandbox): assert observable exit codes in gateway-target exec tests
laitingsheng 1ace377
fix(sandbox): pin sandbox operations to gateway
apurvvkumaria a8ab4b2
fix(sandbox): pin exec policy probes to gateway
apurvvkumaria 87162bd
fix(sandbox): keep gateway ownership snapshot consistent
apurvvkumaria 3968c28
test(sandbox): match rebuild preflight to gateway-pinned RPC semantics
laitingsheng e69aad6
Merge remote-tracking branch 'origin/main' into fix/sandbox-gateway-t…
laitingsheng fd600b2
test: match E2E fixtures to gateway-pinned sandbox RPC argv
laitingsheng 3f1e648
Merge branch 'main' into fix/sandbox-gateway-target-resolution
cv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,255 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| import { execSandbox, type SandboxExecCleanupDeps } from "./exec"; | ||
|
|
||
| const cleanupSkipped: SandboxExecCleanupDeps = { | ||
| getSandbox: () => null, | ||
| inspectMutableConfigPerms: (() => { | ||
| throw new Error("cleanup should be skipped"); | ||
| }) as unknown as SandboxExecCleanupDeps["inspectMutableConfigPerms"], | ||
| repairMutableConfigPerms: (() => { | ||
| throw new Error("cleanup should be skipped"); | ||
| }) as unknown as SandboxExecCleanupDeps["repairMutableConfigPerms"], | ||
| }; | ||
|
|
||
| describe("execSandbox gateway targeting", () => { | ||
| afterEach(() => { | ||
| vi.unstubAllEnvs(); | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it("selects the sandbox's owning gateway before dispatching the exec", async () => { | ||
| const order: string[] = []; | ||
| const selectGateway = vi.fn((name: string) => { | ||
| order.push(`select:${name}`); | ||
| return { outcome: "selected" as const, gatewayName: name }; | ||
| }); | ||
| const run = vi.fn(async (_binary: string, _args: readonly string[]) => { | ||
| order.push("run"); | ||
| return { status: 0 }; | ||
| }); | ||
| vi.spyOn(process, "exit").mockImplementation(((code?: number) => { | ||
| throw new Error(`__exit_${code ?? 0}__`); | ||
| }) as never); | ||
| vi.spyOn(console, "error").mockImplementation(() => undefined); | ||
|
|
||
| await expect( | ||
| execSandbox( | ||
| "beta", | ||
| ["hostname"], | ||
| {}, | ||
| { | ||
| resolveBinary: () => "openshell", | ||
| selectGateway, | ||
| run, | ||
| cleanupDeps: cleanupSkipped, | ||
| policyHint: { | ||
| now: () => 0, | ||
| env: {}, | ||
| probeLogs: () => "", | ||
| enableAudit: () => {}, | ||
| sleep: async () => {}, | ||
| attempts: 1, | ||
| writeStderr: () => {}, | ||
| }, | ||
| }, | ||
| ), | ||
| ).rejects.toThrow("__exit_0__"); | ||
|
|
||
| expect(selectGateway).toHaveBeenCalledWith("beta"); | ||
| expect(run).toHaveBeenCalled(); | ||
| const execArgs = run.mock.calls[0]?.[1] ?? []; | ||
| expect(execArgs.slice(0, 7)).toEqual(["sandbox", "exec", "--name", "beta", "-g", "beta", "--"]); | ||
| expect(execArgs.at(-2)).toBe("nemoclaw-runtime-env"); | ||
| expect(execArgs.at(-1)).toBe("hostname"); | ||
| expect(order.indexOf("select:beta")).toBeGreaterThanOrEqual(0); | ||
| expect(order.indexOf("select:beta")).toBeLessThan(order.indexOf("run")); | ||
| }); | ||
|
|
||
| it("selects the owning gateway before the workdir probe when a workdir is set", async () => { | ||
| const order: string[] = []; | ||
| vi.stubEnv("OPENSHELL_GATEWAY", "ambient-sibling"); | ||
| const selectGateway = vi.fn((name: string) => { | ||
| order.push(`select:${name}`); | ||
| process.env.OPENSHELL_GATEWAY = "drifted-sibling"; | ||
| return { outcome: "selected" as const, gatewayName: "nemoclaw-8091" }; | ||
| }); | ||
| const probeWorkdir = vi.fn((_binary: string, _args: readonly string[]) => { | ||
| order.push("probe"); | ||
| return { status: 0, error: undefined }; | ||
| }); | ||
| const run = vi.fn(async (_binary: string, _args: readonly string[]) => { | ||
| order.push("run"); | ||
| return { status: 0 }; | ||
| }); | ||
| vi.spyOn(process, "exit").mockImplementation(((code?: number) => { | ||
| throw new Error(`__exit_${code ?? 0}__`); | ||
| }) as never); | ||
| vi.spyOn(console, "error").mockImplementation(() => undefined); | ||
|
|
||
| await expect( | ||
| execSandbox( | ||
| "beta", | ||
| ["hostname"], | ||
| { workdir: "/work" }, | ||
| { | ||
| resolveBinary: () => "openshell", | ||
| selectGateway, | ||
| probeWorkdir, | ||
| run, | ||
| cleanupDeps: cleanupSkipped, | ||
| policyHint: { | ||
| now: () => 0, | ||
| env: {}, | ||
| probeLogs: () => "", | ||
| enableAudit: () => {}, | ||
| sleep: async () => {}, | ||
| attempts: 1, | ||
| writeStderr: () => {}, | ||
| }, | ||
| }, | ||
| ), | ||
| ).rejects.toThrow("__exit_0__"); | ||
|
|
||
| expect(order).toEqual(["select:beta", "probe", "run"]); | ||
| expect(process.env.OPENSHELL_GATEWAY).toBe("drifted-sibling"); | ||
| expect(probeWorkdir.mock.calls[0]?.[1]).toEqual([ | ||
| "sandbox", | ||
| "exec", | ||
| "--name", | ||
| "beta", | ||
| "-g", | ||
| "nemoclaw-8091", | ||
| "--", | ||
| "test", | ||
| "-d", | ||
| "/work", | ||
| ]); | ||
| const execArgs = run.mock.calls[0]?.[1] ?? []; | ||
| expect(execArgs.slice(0, 9)).toEqual([ | ||
| "sandbox", | ||
| "exec", | ||
| "--name", | ||
| "beta", | ||
| "-g", | ||
| "nemoclaw-8091", | ||
| "--workdir", | ||
| "/work", | ||
| "--", | ||
| ]); | ||
| expect(execArgs.at(-2)).toBe("nemoclaw-runtime-env"); | ||
| expect(execArgs.at(-1)).toBe("hostname"); | ||
| }); | ||
|
|
||
| it("rejects a direct endpoint override before selecting, probing, or dispatching", async () => { | ||
| vi.stubEnv("OPENSHELL_GATEWAY_ENDPOINT", "https://sibling.invalid"); | ||
| const resolveBinary = vi.fn(() => "openshell"); | ||
| const selectGateway = vi.fn(); | ||
| const probeWorkdir = vi.fn(); | ||
| const run = vi.fn(); | ||
| vi.spyOn(process, "exit").mockImplementation(((code?: number) => { | ||
| throw new Error(`__exit_${code ?? 0}__`); | ||
| }) as never); | ||
| vi.spyOn(console, "error").mockImplementation(() => undefined); | ||
|
|
||
| await expect( | ||
| execSandbox( | ||
| "beta", | ||
| ["hostname"], | ||
| { workdir: "/work" }, | ||
| { resolveBinary, selectGateway, probeWorkdir, run }, | ||
| ), | ||
| ).rejects.toThrow("__exit_1__"); | ||
|
|
||
| expect(console.error).toHaveBeenCalledWith( | ||
| expect.stringContaining("OPENSHELL_GATEWAY_ENDPOINT is set"), | ||
| ); | ||
| expect(resolveBinary).not.toHaveBeenCalled(); | ||
| expect(selectGateway).not.toHaveBeenCalled(); | ||
| expect(probeWorkdir).not.toHaveBeenCalled(); | ||
| expect(run).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("keeps post-exec policy probes pinned after ambient gateway selection drifts", async () => { | ||
| vi.stubEnv("OPENSHELL_GATEWAY", "ambient-sibling"); | ||
| const enableAudit = vi.fn(); | ||
| const probeLogs = vi.fn(() => ""); | ||
| const selectGateway = vi.fn(() => { | ||
| process.env.OPENSHELL_GATEWAY = "drifted-sibling"; | ||
| return { outcome: "selected" as const, gatewayName: "nemoclaw-8091" }; | ||
| }); | ||
| vi.spyOn(process, "exit").mockImplementation(((code?: number) => { | ||
| throw new Error(`__exit_${code ?? 0}__`); | ||
| }) as never); | ||
| vi.spyOn(console, "error").mockImplementation(() => undefined); | ||
|
|
||
| await expect( | ||
| execSandbox( | ||
| "beta", | ||
| ["curl", "https://example.invalid"], | ||
| {}, | ||
| { | ||
| resolveBinary: () => "openshell", | ||
| selectGateway, | ||
| run: async () => ({ status: 56 }), | ||
| cleanupDeps: cleanupSkipped, | ||
| policyHint: { | ||
| now: () => 0, | ||
| env: {}, | ||
| enableAudit, | ||
| probeLogs, | ||
| attempts: 1, | ||
| sleep: async () => {}, | ||
| writeStderr: () => {}, | ||
| }, | ||
| }, | ||
| ), | ||
| ).rejects.toThrow("__exit_56__"); | ||
|
|
||
| expect(process.env.OPENSHELL_GATEWAY).toBe("drifted-sibling"); | ||
| expect(enableAudit).toHaveBeenCalledWith("beta", "nemoclaw-8091"); | ||
| expect(probeLogs).toHaveBeenCalledWith("beta", "nemoclaw-8091"); | ||
| }); | ||
|
|
||
| it("aborts before the workdir probe and exec when gateway selection fails", async () => { | ||
| const order: string[] = []; | ||
| const selectGateway = vi.fn(() => { | ||
| order.push("select"); | ||
| return { outcome: "failed" as const, gatewayName: "nemoclaw-8091" }; | ||
| }); | ||
| const probeWorkdir = vi.fn(() => { | ||
| order.push("probe"); | ||
| return { status: 0, error: undefined }; | ||
| }); | ||
| const run = vi.fn(async () => { | ||
| order.push("run"); | ||
| return { status: 0 }; | ||
| }); | ||
| vi.spyOn(process, "exit").mockImplementation(((code?: number) => { | ||
| throw new Error(`__exit_${code ?? 0}__`); | ||
| }) as never); | ||
| vi.spyOn(console, "error").mockImplementation(() => undefined); | ||
|
|
||
| await expect( | ||
| execSandbox( | ||
| "beta", | ||
| ["hostname"], | ||
| { workdir: "/work" }, | ||
| { | ||
| resolveBinary: () => "openshell", | ||
| selectGateway, | ||
| probeWorkdir, | ||
| run, | ||
| cleanupDeps: cleanupSkipped, | ||
| }, | ||
| ), | ||
| ).rejects.toThrow("__exit_1__"); | ||
|
|
||
| expect(order).toEqual(["select"]); | ||
| expect(probeWorkdir).not.toHaveBeenCalled(); | ||
| expect(run).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.