Skip to content
Merged
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 ci/source-architecture-budget.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"src/lib/adapters/openshell/client.ts": 21,
"src/lib/adapters/openshell/command-argv.ts": 23,
"src/lib/adapters/openshell/resolve.ts": 25,
"src/lib/adapters/openshell/runtime.ts": 54,
"src/lib/adapters/openshell/runtime.ts": 53,
"src/lib/adapters/openshell/timeouts.ts": 33,
"src/lib/agent/defs.ts": 32,
"src/lib/cli/branding.ts": 80,
Expand Down Expand Up @@ -62,7 +62,7 @@
"maxRootFiles": {
"src/lib/onboard": 301,
"src/lib/actions": 18,
"src/lib/actions/sandbox": 179,
"src/lib/actions/sandbox": 178,
"src/lib/state": 38,
"src/lib/inference": 63,
"scripts": 42
Expand Down
2 changes: 1 addition & 1 deletion src/lib/actions/sandbox/agent/passthrough-dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
// non-interactive one-shot, yet PR #8191 moved the non-JSON transport
// off `execSandbox` onto a raw `spawnSync` with a hard-coded
// `stdio[0] = "inherit"`, dropping the TTY-aware stdin guard that
// `buildSandboxExecStdio` applies to every other sandbox exec. The JSON
// `buildSandboxCommandStdio` applies to every other sandbox exec. The JSON
// transport has carried the same hard-coded inherit since #5683. The
// result is a live terminal on fd 0 handed to a dispatch whose stdout
// and stderr are pipes and whose argv says `--no-tty`.
Expand Down
185 changes: 153 additions & 32 deletions src/lib/actions/sandbox/exec-gateway-target.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,29 @@

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

import {
createCliOpenShellSandboxCommandExecutor,
type OpenShellCommandChild,
type OpenShellCommandSpawner,
} from "../../adapters/openshell/sandbox-command-cli";
import type { OpenShellSandboxCommandExecutor } from "../../adapters/openshell/sandbox-command";
import { execSandbox, type SandboxExecCleanupDeps } from "./exec";

function commandExecutor(options: {
run?: OpenShellSandboxCommandExecutor["runStreaming"];
probe?: OpenShellSandboxCommandExecutor["probeDirectory"];
}): OpenShellSandboxCommandExecutor {
return {
probeDirectory: options.probe ?? (async () => ({ state: "present" })),
runStreaming:
options.run ??
(async () => ({
outcome: { kind: "completed", exitCode: 0 },
release: () => {},
})),
};
}

const cleanupSkipped: SandboxExecCleanupDeps = {
getSandbox: () => null,
inspectMutableConfigPerms: (() => {
Expand All @@ -27,9 +48,9 @@ describe("execSandbox gateway targeting", () => {
order.push(`select:${name}`);
return { outcome: "selected" as const, gatewayName: name };
});
const run = vi.fn(async (_binary: string, _args: readonly string[]) => {
const run = vi.fn<OpenShellSandboxCommandExecutor["runStreaming"]>(async () => {
order.push("run");
return { status: 0 };
return { outcome: { kind: "completed", exitCode: 0 }, release: () => {} };
});
vi.spyOn(process, "exit").mockImplementation(((code?: number) => {
throw new Error(`__exit_${code ?? 0}__`);
Expand All @@ -42,9 +63,8 @@ describe("execSandbox gateway targeting", () => {
["hostname"],
{},
{
resolveBinary: () => "openshell",
selectGateway,
run,
commandExecutor: commandExecutor({ run }),
cleanupDeps: cleanupSkipped,
policyHint: {
now: () => 0,
Expand All @@ -61,10 +81,12 @@ describe("execSandbox gateway targeting", () => {

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(run.mock.calls[0]?.[0]).toMatchObject({
sandboxName: "beta",
target: { kind: "named", gatewayName: "beta" },
});
expect(run.mock.calls[0]?.[0].command.at(-2)).toBe("nemoclaw-runtime-env");
expect(run.mock.calls[0]?.[0].command.at(-1)).toBe("hostname");
expect(order.indexOf("select:beta")).toBeGreaterThanOrEqual(0);
expect(order.indexOf("select:beta")).toBeLessThan(order.indexOf("run"));
});
Expand All @@ -77,13 +99,13 @@ describe("execSandbox gateway targeting", () => {
process.env.OPENSHELL_GATEWAY = "drifted-sibling";
return { outcome: "selected" as const, gatewayName: "nemoclaw-8091" };
});
const probeWorkdir = vi.fn((_binary: string, _args: readonly string[]) => {
const probeWorkdir = vi.fn<OpenShellSandboxCommandExecutor["probeDirectory"]>(async () => {
order.push("probe");
return { status: 0, error: undefined };
return { state: "present" };
});
const run = vi.fn(async (_binary: string, _args: readonly string[]) => {
const run = vi.fn<OpenShellSandboxCommandExecutor["runStreaming"]>(async () => {
order.push("run");
return { status: 0 };
return { outcome: { kind: "completed", exitCode: 0 }, release: () => {} };
});
vi.spyOn(process, "exit").mockImplementation(((code?: number) => {
throw new Error(`__exit_${code ?? 0}__`);
Expand All @@ -96,10 +118,8 @@ describe("execSandbox gateway targeting", () => {
["hostname"],
{ workdir: "/work" },
{
resolveBinary: () => "openshell",
selectGateway,
probeWorkdir,
run,
commandExecutor: commandExecutor({ probe: probeWorkdir, run }),
cleanupDeps: cleanupSkipped,
policyHint: {
now: () => 0,
Expand All @@ -116,7 +136,72 @@ describe("execSandbox gateway targeting", () => {

expect(order).toEqual(["select:beta", "probe", "run"]);
expect(process.env.OPENSHELL_GATEWAY).toBe("drifted-sibling");
expect(probeWorkdir.mock.calls[0]?.[1]).toEqual([
expect(probeWorkdir).toHaveBeenCalledWith({
sandboxName: "beta",
target: { kind: "named", gatewayName: "nemoclaw-8091" },
path: "/work",
});
expect(run.mock.calls[0]?.[0]).toMatchObject({
sandboxName: "beta",
target: { kind: "named", gatewayName: "nemoclaw-8091" },
workdir: "/work",
});
expect(run.mock.calls[0]?.[0].command.at(-2)).toBe("nemoclaw-runtime-env");
expect(run.mock.calls[0]?.[0].command.at(-1)).toBe("hostname");
});

it("carries the selected gateway through the CLI workdir probe and command dispatch", async () => {
const spawnProbe = vi.fn(() => ({ status: 0 }));
const child = {
exitCode: null,
signalCode: null,
kill: vi.fn(() => true),
once: vi.fn((event: "error" | "close", listener: (...args: unknown[]) => void) => {
const notify = {
error: () => undefined,
close: () => queueMicrotask(() => listener(0, null)),
}[event];
notify();
return child;
}),
} as unknown as OpenShellCommandChild;
const spawnChild = vi.fn<OpenShellCommandSpawner>(() => child);
const executor = createCliOpenShellSandboxCommandExecutor({
resolveBinary: () => "/usr/bin/openshell",
spawnProbe,
spawnChild,
});
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" },
{
selectGateway: () => ({
outcome: "selected",
gatewayName: "nemoclaw-8091",
}),
commandExecutor: executor,
cleanupDeps: cleanupSkipped,
policyHint: {
now: () => 0,
env: {},
probeLogs: () => "",
enableAudit: () => {},
sleep: async () => {},
attempts: 1,
writeStderr: () => {},
},
},
),
).rejects.toThrow("__exit_0__");

expect(spawnProbe).toHaveBeenCalledWith("/usr/bin/openshell", [
"sandbox",
"exec",
"--name",
Expand All @@ -128,8 +213,9 @@ describe("execSandbox gateway targeting", () => {
"-d",
"/work",
]);
const execArgs = run.mock.calls[0]?.[1] ?? [];
expect(execArgs.slice(0, 9)).toEqual([
expect(spawnChild.mock.calls[0]?.[0]).toBe("/usr/bin/openshell");
const execArgs = spawnChild.mock.calls[0]?.[1];
expect(execArgs?.slice(0, 9)).toEqual([
"sandbox",
"exec",
"--name",
Expand All @@ -140,16 +226,50 @@ describe("execSandbox gateway targeting", () => {
"/work",
"--",
]);
expect(execArgs.at(-2)).toBe("nemoclaw-runtime-env");
expect(execArgs.at(-1)).toBe("hostname");
expect(execArgs?.at(-1)).toBe("hostname");
});

it("reports a rejected workdir probe as a normal invocation failure", async () => {
const resolveBinary = vi.fn(() => "/usr/bin/openshell");
const spawnProbe = vi.fn();
const spawnChild = vi.fn();
const executor = createCliOpenShellSandboxCommandExecutor({
resolveBinary,
spawnProbe,
spawnChild,
});
vi.spyOn(process, "exit").mockImplementation(((code?: number) => {
throw new Error(`__exit_${code ?? 0}__`);
}) as never);
vi.spyOn(console, "error").mockImplementation(() => undefined);

await expect(
execSandbox(
"invalid/name",
["hostname"],
{ workdir: "/work" },
{
selectGateway: () => ({ outcome: "unregistered", gatewayName: null }),
commandExecutor: executor,
cleanupDeps: cleanupSkipped,
},
),
).rejects.toThrow("__exit_1__");

expect(console.error).toHaveBeenCalledWith(
" Failed to invoke openshell: Invalid OpenShell sandbox name",
);
expect(resolveBinary).not.toHaveBeenCalled();
expect(spawnProbe).not.toHaveBeenCalled();
expect(spawnChild).not.toHaveBeenCalled();
});

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();
const executor = commandExecutor({ probe: probeWorkdir, run });
vi.spyOn(process, "exit").mockImplementation(((code?: number) => {
throw new Error(`__exit_${code ?? 0}__`);
}) as never);
Expand All @@ -160,14 +280,13 @@ describe("execSandbox gateway targeting", () => {
"beta",
["hostname"],
{ workdir: "/work" },
{ resolveBinary, selectGateway, probeWorkdir, run },
{ commandExecutor: executor, selectGateway },
),
).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();
Expand All @@ -192,9 +311,13 @@ describe("execSandbox gateway targeting", () => {
["curl", "https://example.invalid"],
{},
{
resolveBinary: () => "openshell",
selectGateway,
run: async () => ({ status: 56 }),
commandExecutor: commandExecutor({
run: async () => ({
outcome: { kind: "completed", exitCode: 56 },
release: () => {},
}),
}),
cleanupDeps: cleanupSkipped,
policyHint: {
now: () => 0,
Expand All @@ -220,13 +343,13 @@ describe("execSandbox gateway targeting", () => {
order.push("select");
return { outcome: "failed" as const, gatewayName: "nemoclaw-8091" };
});
const probeWorkdir = vi.fn(() => {
const probeWorkdir = vi.fn<OpenShellSandboxCommandExecutor["probeDirectory"]>(async () => {
order.push("probe");
return { status: 0, error: undefined };
return { state: "present" };
});
const run = vi.fn(async () => {
const run = vi.fn<OpenShellSandboxCommandExecutor["runStreaming"]>(async () => {
order.push("run");
return { status: 0 };
return { outcome: { kind: "completed", exitCode: 0 }, release: () => {} };
});
vi.spyOn(process, "exit").mockImplementation(((code?: number) => {
throw new Error(`__exit_${code ?? 0}__`);
Expand All @@ -239,10 +362,8 @@ describe("execSandbox gateway targeting", () => {
["hostname"],
{ workdir: "/work" },
{
resolveBinary: () => "openshell",
selectGateway,
probeWorkdir,
run,
commandExecutor: commandExecutor({ probe: probeWorkdir, run }),
cleanupDeps: cleanupSkipped,
},
),
Expand Down
23 changes: 17 additions & 6 deletions src/lib/actions/sandbox/exec-googlechat-pairing-restart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";

import { createCliOpenShellSandboxCommandExecutor } from "../../adapters/openshell/sandbox-command-cli";
import {
execSandbox,
isGoogleChatPairingApproval,
Expand All @@ -27,9 +28,14 @@ const CLEANUP_SKIPPED: SandboxExecCleanupDeps = {

function depsFor(status: number, restartGateway = vi.fn(() => ({ ok: true }))): ExecSandboxDeps {
return {
resolveBinary: () => "openshell",
selectGateway: () => ({ outcome: "selected", gatewayName: "nemoclaw-alpha" }),
run: () => ({ status }),
commandExecutor: {
probeDirectory: async () => ({ state: "present" }),
runStreaming: async () => ({
outcome: { kind: "completed", exitCode: status },
release: () => {},
}),
},
cleanupDeps: CLEANUP_SKIPPED,
restartGateway,
resolveSandboxAgent: () => "openclaw",
Expand Down Expand Up @@ -110,9 +116,12 @@ describe("Google Chat pairing approval gateway activation (#8553)", () => {
return { ok: true };
});
const deps = depsFor(0, restartGateway);
deps.run = () => {
order.push("command");
return { status: 0 };
deps.commandExecutor = {
probeDirectory: async () => ({ state: "present" }),
runStreaming: async () => {
order.push("command");
return { outcome: { kind: "completed", exitCode: 0 }, release: () => {} };
},
};
deps.cleanupDeps = {
getSandbox: () => ({ agent: "openclaw" }),
Expand Down Expand Up @@ -181,8 +190,10 @@ describe("Google Chat pairing approval gateway activation (#8553)", () => {
const exitCode = await runAndCaptureExit(
["openclaw", "pairing", "approve", "googlechat", "ABCD1234"],
{
resolveBinary: () => openshellPath,
selectGateway: () => ({ outcome: "selected", gatewayName: "nemoclaw-alpha" }),
commandExecutor: createCliOpenShellSandboxCommandExecutor({
resolveBinary: () => openshellPath,
}),
cleanupDeps: {
getSandbox: () => ({ agent: "openclaw" }),
inspectMutableConfigPerms: () => ({
Expand Down
Loading
Loading