Merge branch 'main' into feat/sync-nodeloc-v1.2.0
Combines the oversized-prompt instructions-file fix from main with the fork's cleanup and redaction hardening: agent container, workspace permissions, proxy, and prompt temp directory all clean up without aborting each other.
This commit is contained in:
commit
a5108b2c36
2 files changed
+111
-7
No files matched your search
+69
-6
@@ -16,6 +16,45 @@ const GOOSE_DOCKER_IMAGE = "node:24-bookworm";
|
|||||||
const GOOSE_DOCKER_VERSION = "v1.46.0";
|
const GOOSE_DOCKER_VERSION = "v1.46.0";
|
||||||
const GOOSE_CONTAINER_PATH = "/usr/local/bin/goose";
|
const GOOSE_CONTAINER_PATH = "/usr/local/bin/goose";
|
||||||
const ISOLATED_VALIDATION_WORKSPACE = "/tmp/ghbot-validation-workspace";
|
const ISOLATED_VALIDATION_WORKSPACE = "/tmp/ghbot-validation-workspace";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Linux execve caps a single argv string at MAX_ARG_STRLEN (128 KiB). A review
|
||||||
|
* prompt larger than that makes spawn fail with E2BIG before goose even runs,
|
||||||
|
* so oversized prompts are handed over through an instructions file instead.
|
||||||
|
*/
|
||||||
|
export const MAX_PROMPT_ARG_BYTES = 96 * 1024;
|
||||||
|
const GOOSE_PROMPT_FILE_NAME = "goose-prompt.md";
|
||||||
|
const GOOSE_CONTAINER_PROMPT_PATH = `/prompt/${GOOSE_PROMPT_FILE_NAME}`;
|
||||||
|
|
||||||
|
function promptExceedsArgvLimit(prompt: string): boolean {
|
||||||
|
return Buffer.byteLength(prompt, "utf8") > MAX_PROMPT_ARG_BYTES;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function writePromptInstructionsFile(prompt: string, tempDirectory: string): Promise<string> {
|
||||||
|
const instructionsFile = path.join(tempDirectory, GOOSE_PROMPT_FILE_NAME);
|
||||||
|
await fs.writeFile(instructionsFile, prompt, "utf8");
|
||||||
|
logger.info(
|
||||||
|
{ bytes: Buffer.byteLength(prompt, "utf8"), instructionsFile },
|
||||||
|
"Prompt exceeds the argv size limit; passing it via an instructions file."
|
||||||
|
);
|
||||||
|
return instructionsFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Chooses how goose receives its task: inline via --text while it safely fits
|
||||||
|
* into execve argv, otherwise through an instructions file (-i) written under
|
||||||
|
* the provided temporary directory.
|
||||||
|
*/
|
||||||
|
export async function resolveGooseInputArgs(
|
||||||
|
prompt: string,
|
||||||
|
tempDirectory: string
|
||||||
|
): Promise<string[]> {
|
||||||
|
if (!promptExceedsArgvLimit(prompt)) {
|
||||||
|
return ["--text", prompt];
|
||||||
|
}
|
||||||
|
return ["--instructions", await writePromptInstructionsFile(prompt, tempDirectory)];
|
||||||
|
}
|
||||||
|
|
||||||
const GOOSE_CONTAINER_BOOTSTRAP = [
|
const GOOSE_CONTAINER_BOOTSTRAP = [
|
||||||
"set -eu",
|
"set -eu",
|
||||||
"cleanup_workspace() { chmod -R a+rwX /workspace 2>/dev/null || true; }",
|
"cleanup_workspace() { chmod -R a+rwX /workspace 2>/dev/null || true; }",
|
||||||
@@ -76,8 +115,7 @@ export async function runGoosePrompt(
|
|||||||
"openai",
|
"openai",
|
||||||
"--model",
|
"--model",
|
||||||
config.gooseModel,
|
config.gooseModel,
|
||||||
"--text",
|
...(await resolveGooseInputArgs(prompt, tempDir))
|
||||||
prompt
|
|
||||||
];
|
];
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
@@ -126,12 +164,21 @@ export async function runGooseAgent(
|
|||||||
apiToken: proxy.token,
|
apiToken: proxy.token,
|
||||||
proxyPort: proxy.port
|
proxyPort: proxy.port
|
||||||
});
|
});
|
||||||
|
let promptTempDir: string | undefined;
|
||||||
|
let hostInstructionsFile: string | undefined;
|
||||||
|
if (promptExceedsArgvLimit(prompt)) {
|
||||||
|
const tempRoot = path.join(process.cwd(), ".ghbot-tmp");
|
||||||
|
await fs.mkdir(tempRoot, { recursive: true });
|
||||||
|
promptTempDir = await fs.mkdtemp(path.join(tempRoot, "goose-agent-"));
|
||||||
|
hostInstructionsFile = await writePromptInstructionsFile(prompt, promptTempDir);
|
||||||
|
}
|
||||||
const args = buildGooseAgentDockerArgs({
|
const args = buildGooseAgentDockerArgs({
|
||||||
containerName,
|
containerName,
|
||||||
realWorkingDirectory,
|
realWorkingDirectory,
|
||||||
containerEnv,
|
containerEnv,
|
||||||
hostGooseBinary,
|
hostGooseBinary,
|
||||||
prompt
|
prompt,
|
||||||
|
hostInstructionsFile
|
||||||
});
|
});
|
||||||
|
|
||||||
let primaryError: unknown;
|
let primaryError: unknown;
|
||||||
@@ -149,7 +196,9 @@ export async function runGooseAgent(
|
|||||||
primaryError = error;
|
primaryError = error;
|
||||||
throw error;
|
throw error;
|
||||||
} finally {
|
} finally {
|
||||||
await removeDockerContainer(containerName, realWorkingDirectory);
|
await removeDockerContainer(containerName, realWorkingDirectory).catch((error: unknown) => {
|
||||||
|
logger.warn({ error, containerName }, "Agent container cleanup failed.");
|
||||||
|
});
|
||||||
await restoreAgentWorkspacePermissions(realWorkingDirectory).catch((error: unknown) => {
|
await restoreAgentWorkspacePermissions(realWorkingDirectory).catch((error: unknown) => {
|
||||||
logger.warn(
|
logger.warn(
|
||||||
{ error, workingDirectory: realWorkingDirectory },
|
{ error, workingDirectory: realWorkingDirectory },
|
||||||
@@ -159,6 +208,11 @@ export async function runGooseAgent(
|
|||||||
await proxy.close().catch((error: unknown) => {
|
await proxy.close().catch((error: unknown) => {
|
||||||
logger.warn({ error }, "One-run Goose API proxy cleanup failed.");
|
logger.warn({ error }, "One-run Goose API proxy cleanup failed.");
|
||||||
});
|
});
|
||||||
|
if (promptTempDir) {
|
||||||
|
await fs.rm(promptTempDir, { recursive: true, force: true }).catch((error: unknown) => {
|
||||||
|
logger.warn({ error, promptTempDir }, "Agent prompt temp directory cleanup failed.");
|
||||||
|
});
|
||||||
|
}
|
||||||
void primaryError;
|
void primaryError;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -230,8 +284,12 @@ export function buildGooseAgentDockerArgs(params: {
|
|||||||
realWorkingDirectory: string;
|
realWorkingDirectory: string;
|
||||||
containerEnv: Record<string, string>;
|
containerEnv: Record<string, string>;
|
||||||
prompt: string;
|
prompt: string;
|
||||||
|
hostInstructionsFile?: string;
|
||||||
hostGooseBinary?: string;
|
hostGooseBinary?: string;
|
||||||
}): string[] {
|
}): string[] {
|
||||||
|
const inputArgs = params.hostInstructionsFile
|
||||||
|
? ["--instructions", GOOSE_CONTAINER_PROMPT_PATH]
|
||||||
|
: ["--text", params.prompt];
|
||||||
return [
|
return [
|
||||||
"run",
|
"run",
|
||||||
"--name",
|
"--name",
|
||||||
@@ -261,6 +319,12 @@ export function buildGooseAgentDockerArgs(params: {
|
|||||||
: []),
|
: []),
|
||||||
"--workdir",
|
"--workdir",
|
||||||
"/workspace",
|
"/workspace",
|
||||||
|
...(params.hostInstructionsFile
|
||||||
|
? [
|
||||||
|
"--mount",
|
||||||
|
`type=bind,source=${params.hostInstructionsFile},target=${GOOSE_CONTAINER_PROMPT_PATH},readonly`
|
||||||
|
]
|
||||||
|
: []),
|
||||||
...Object.keys(params.containerEnv).flatMap((key) => ["--env", key]),
|
...Object.keys(params.containerEnv).flatMap((key) => ["--env", key]),
|
||||||
GOOSE_DOCKER_IMAGE,
|
GOOSE_DOCKER_IMAGE,
|
||||||
"sh",
|
"sh",
|
||||||
@@ -279,8 +343,7 @@ export function buildGooseAgentDockerArgs(params: {
|
|||||||
config.gooseModel,
|
config.gooseModel,
|
||||||
"--with-builtin",
|
"--with-builtin",
|
||||||
"developer",
|
"developer",
|
||||||
"--text",
|
...inputArgs
|
||||||
params.prompt
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+42
-1
@@ -1,4 +1,7 @@
|
|||||||
import assert from "node:assert/strict";
|
import assert from "node:assert/strict";
|
||||||
|
import fs from "node:fs/promises";
|
||||||
|
import os from "node:os";
|
||||||
|
import path from "node:path";
|
||||||
import test from "node:test";
|
import test from "node:test";
|
||||||
import {
|
import {
|
||||||
buildGooseAgentDockerArgs,
|
buildGooseAgentDockerArgs,
|
||||||
@@ -6,7 +9,9 @@ import {
|
|||||||
buildIsolatedWorkspaceCommandDockerArgs,
|
buildIsolatedWorkspaceCommandDockerArgs,
|
||||||
buildWorkspacePermissionDockerArgs,
|
buildWorkspacePermissionDockerArgs,
|
||||||
extractGooseFinalText,
|
extractGooseFinalText,
|
||||||
redactProcessArgs
|
MAX_PROMPT_ARG_BYTES,
|
||||||
|
redactProcessArgs,
|
||||||
|
resolveGooseInputArgs
|
||||||
} from "../src/ai/gooseCli.js";
|
} from "../src/ai/gooseCli.js";
|
||||||
|
|
||||||
test("goose agent mounts the workflow binary read-only and keeps a visible install fallback", () => {
|
test("goose agent mounts the workflow binary read-only and keeps a visible install fallback", () => {
|
||||||
@@ -31,6 +36,42 @@ test("goose agent mounts the workflow binary read-only and keeps a visible insta
|
|||||||
assert.equal(args.at(-1), "introduce this pull request");
|
assert.equal(args.at(-1), "introduce this pull request");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("oversized prompts switch from --text to an instructions file", async () => {
|
||||||
|
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "goose-input-"));
|
||||||
|
try {
|
||||||
|
const small = await resolveGooseInputArgs("tiny prompt", tempDir);
|
||||||
|
assert.deepEqual(small, ["--text", "tiny prompt"]);
|
||||||
|
|
||||||
|
const oversized = "x".repeat(MAX_PROMPT_ARG_BYTES + 1);
|
||||||
|
const args = await resolveGooseInputArgs(oversized, tempDir);
|
||||||
|
assert.equal(args[0], "--instructions");
|
||||||
|
assert.match(args[1]!, /goose-prompt\.md$/);
|
||||||
|
assert.equal(await fs.readFile(args[1]!, "utf8"), oversized);
|
||||||
|
} finally {
|
||||||
|
await fs.rm(tempDir, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test("docker agent mounts the instructions file instead of passing prompt text", () => {
|
||||||
|
const args = buildGooseAgentDockerArgs({
|
||||||
|
containerName: "ghbot-agent-test",
|
||||||
|
realWorkingDirectory: "/tmp/worktree",
|
||||||
|
containerEnv: {},
|
||||||
|
prompt: "oversized prompt body",
|
||||||
|
hostInstructionsFile: "/tmp/goose-agent-x/goose-prompt.md"
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.ok(
|
||||||
|
args.includes(
|
||||||
|
"type=bind,source=/tmp/goose-agent-x/goose-prompt.md,target=/prompt/goose-prompt.md,readonly"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
const instructionsIndex = args.indexOf("--instructions");
|
||||||
|
assert.ok(instructionsIndex > 0);
|
||||||
|
assert.equal(args[instructionsIndex + 1], "/prompt/goose-prompt.md");
|
||||||
|
assert.equal(args.includes("--text"), false);
|
||||||
|
});
|
||||||
|
|
||||||
test("isolated validation receives the repository but no model or GitHub credentials", () => {
|
test("isolated validation receives the repository but no model or GitHub credentials", () => {
|
||||||
const args = buildIsolatedWorkspaceCommandDockerArgs({
|
const args = buildIsolatedWorkspaceCommandDockerArgs({
|
||||||
containerName: "ghbot-validation-test",
|
containerName: "ghbot-validation-test",
|
||||||
|
|||||||
Reference in new issue
Block a user