fix: merge login shell PATH for gateway exec
This commit is contained in:
100
src/agents/bash-tools.exec.path.test.ts
Normal file
100
src/agents/bash-tools.exec.path.test.ts
Normal file
@@ -0,0 +1,100 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import type { ExecApprovalsResolved } from "../infra/exec-approvals.js";
|
||||
import { sanitizeBinaryOutput } from "./shell-utils.js";
|
||||
|
||||
const isWin = process.platform === "win32";
|
||||
|
||||
vi.mock("../infra/shell-env.js", async (importOriginal) => {
|
||||
const mod = await importOriginal<typeof import("../infra/shell-env.js")>();
|
||||
return {
|
||||
...mod,
|
||||
getShellPathFromLoginShell: vi.fn(() => "/custom/bin:/opt/bin"),
|
||||
resolveShellEnvFallbackTimeoutMs: vi.fn(() => 1234),
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("../infra/exec-approvals.js", async (importOriginal) => {
|
||||
const mod = await importOriginal<typeof import("../infra/exec-approvals.js")>();
|
||||
const approvals: ExecApprovalsResolved = {
|
||||
path: "/tmp/exec-approvals.json",
|
||||
socketPath: "/tmp/exec-approvals.sock",
|
||||
token: "token",
|
||||
defaults: {
|
||||
security: "full",
|
||||
ask: "off",
|
||||
askFallback: "full",
|
||||
autoAllowSkills: false,
|
||||
},
|
||||
agent: {
|
||||
security: "full",
|
||||
ask: "off",
|
||||
askFallback: "full",
|
||||
autoAllowSkills: false,
|
||||
},
|
||||
allowlist: [],
|
||||
file: {
|
||||
version: 1,
|
||||
socket: { path: "/tmp/exec-approvals.sock", token: "token" },
|
||||
defaults: {
|
||||
security: "full",
|
||||
ask: "off",
|
||||
askFallback: "full",
|
||||
autoAllowSkills: false,
|
||||
},
|
||||
agents: {},
|
||||
},
|
||||
};
|
||||
return { ...mod, resolveExecApprovals: () => approvals };
|
||||
});
|
||||
|
||||
const normalizeText = (value?: string) =>
|
||||
sanitizeBinaryOutput(value ?? "")
|
||||
.replace(/\r\n/g, "\n")
|
||||
.replace(/\r/g, "\n")
|
||||
.trim();
|
||||
|
||||
describe("exec PATH login shell merge", () => {
|
||||
const originalPath = process.env.PATH;
|
||||
|
||||
afterEach(() => {
|
||||
process.env.PATH = originalPath;
|
||||
});
|
||||
|
||||
it("merges login-shell PATH for host=gateway", async () => {
|
||||
if (isWin) return;
|
||||
process.env.PATH = "/usr/bin";
|
||||
|
||||
const { createExecTool } = await import("./bash-tools.exec.js");
|
||||
const { getShellPathFromLoginShell } = await import("../infra/shell-env.js");
|
||||
const shellPathMock = vi.mocked(getShellPathFromLoginShell);
|
||||
shellPathMock.mockClear();
|
||||
shellPathMock.mockReturnValue("/custom/bin:/opt/bin");
|
||||
|
||||
const tool = createExecTool({ host: "gateway", security: "full", ask: "off" });
|
||||
const result = await tool.execute("call1", { command: "echo $PATH" });
|
||||
const text = normalizeText(result.content.find((c) => c.type === "text")?.text);
|
||||
|
||||
expect(text).toBe("/custom/bin:/opt/bin:/usr/bin");
|
||||
expect(shellPathMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("skips login-shell PATH when env.PATH is provided", async () => {
|
||||
if (isWin) return;
|
||||
process.env.PATH = "/usr/bin";
|
||||
|
||||
const { createExecTool } = await import("./bash-tools.exec.js");
|
||||
const { getShellPathFromLoginShell } = await import("../infra/shell-env.js");
|
||||
const shellPathMock = vi.mocked(getShellPathFromLoginShell);
|
||||
shellPathMock.mockClear();
|
||||
|
||||
const tool = createExecTool({ host: "gateway", security: "full", ask: "off" });
|
||||
const result = await tool.execute("call1", {
|
||||
command: "echo $PATH",
|
||||
env: { PATH: "/explicit/bin" },
|
||||
});
|
||||
const text = normalizeText(result.content.find((c) => c.type === "text")?.text);
|
||||
|
||||
expect(text).toBe("/explicit/bin");
|
||||
expect(shellPathMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -18,6 +18,10 @@ import {
|
||||
} from "../infra/exec-approvals.js";
|
||||
import { requestHeartbeatNow } from "../infra/heartbeat-wake.js";
|
||||
import { buildNodeShellCommand } from "../infra/node-shell.js";
|
||||
import {
|
||||
getShellPathFromLoginShell,
|
||||
resolveShellEnvFallbackTimeoutMs,
|
||||
} from "../infra/shell-env.js";
|
||||
import { enqueueSystemEvent } from "../infra/system-events.js";
|
||||
import { logInfo } from "../logger.js";
|
||||
import {
|
||||
@@ -249,6 +253,17 @@ function applyPathPrepend(
|
||||
if (merged) env.PATH = merged;
|
||||
}
|
||||
|
||||
function applyShellPath(env: Record<string, string>, shellPath?: string | null) {
|
||||
if (!shellPath) return;
|
||||
const entries = shellPath
|
||||
.split(path.delimiter)
|
||||
.map((part) => part.trim())
|
||||
.filter(Boolean);
|
||||
if (entries.length === 0) return;
|
||||
const merged = mergePathPrepend(env.PATH, entries);
|
||||
if (merged) env.PATH = merged;
|
||||
}
|
||||
|
||||
function maybeNotifyOnExit(session: ProcessSession, status: "completed" | "failed") {
|
||||
if (!session.backgrounded || !session.notifyOnExit || session.exitNotified) return;
|
||||
const sessionKey = session.sessionKey?.trim();
|
||||
@@ -422,6 +437,13 @@ export function createExecTool(
|
||||
containerWorkdir: containerWorkdir ?? sandbox.containerWorkdir,
|
||||
})
|
||||
: mergedEnv;
|
||||
if (!sandbox && host === "gateway" && !params.env?.PATH) {
|
||||
const shellPath = getShellPathFromLoginShell({
|
||||
env: process.env,
|
||||
timeoutMs: resolveShellEnvFallbackTimeoutMs(process.env),
|
||||
});
|
||||
applyShellPath(env, shellPath);
|
||||
}
|
||||
applyPathPrepend(env, defaultPathPrepend);
|
||||
|
||||
if (host === "node") {
|
||||
|
||||
Reference in New Issue
Block a user