fix: pass oversized goose prompts via instructions file
Linux execve caps a single argv string at MAX_ARG_STRLEN (128 KiB), so review prompts larger than that failed to spawn goose with E2BIG before any model call was made. Prompts above a 96 KiB safety margin are now written to a temporary file and handed to goose through --instructions (-i) on both the direct runner path and the Docker agent path (mounted read-only at /prompt/goose-prompt.md). Small prompts keep using --text.
This commit is contained in:
1 parent
a6e3fd2fc5
commit
d933e99ad9
2 files changed
+113
-7
No files matched your search
+71
-6
@@ -13,6 +13,48 @@ const GOOSE_DOCKER_IMAGE = "node:24-bookworm";
|
||||
const GOOSE_DOCKER_VERSION = "v1.46.0";
|
||||
const GOOSE_CONTAINER_PATH = "/usr/local/bin/goose";
|
||||
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 = [
|
||||
"set -eu",
|
||||
"cleanup_workspace() { chmod -R a+rwX /workspace 2>/dev/null || true; }",
|
||||
@@ -73,8 +115,7 @@ export async function runGoosePrompt(
|
||||
"openai",
|
||||
"--model",
|
||||
config.gooseModel,
|
||||
"--text",
|
||||
prompt
|
||||
...(await resolveGooseInputArgs(prompt, tempDir))
|
||||
];
|
||||
|
||||
logger.info(
|
||||
@@ -119,12 +160,21 @@ export async function runGooseAgent(
|
||||
apiToken: proxy.token,
|
||||
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({
|
||||
containerName,
|
||||
realWorkingDirectory,
|
||||
containerEnv,
|
||||
hostGooseBinary,
|
||||
prompt
|
||||
prompt,
|
||||
hostInstructionsFile
|
||||
});
|
||||
|
||||
try {
|
||||
@@ -144,7 +194,13 @@ export async function runGooseAgent(
|
||||
try {
|
||||
await restoreAgentWorkspacePermissions(realWorkingDirectory);
|
||||
} finally {
|
||||
await proxy.close();
|
||||
try {
|
||||
await proxy.close();
|
||||
} finally {
|
||||
if (promptTempDir) {
|
||||
await fs.rm(promptTempDir, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -219,8 +275,12 @@ export function buildGooseAgentDockerArgs(params: {
|
||||
realWorkingDirectory: string;
|
||||
containerEnv: Record<string, string>;
|
||||
prompt: string;
|
||||
hostInstructionsFile?: string;
|
||||
hostGooseBinary?: string;
|
||||
}): string[] {
|
||||
const inputArgs = params.hostInstructionsFile
|
||||
? ["--instructions", GOOSE_CONTAINER_PROMPT_PATH]
|
||||
: ["--text", params.prompt];
|
||||
return [
|
||||
"run",
|
||||
"--name",
|
||||
@@ -250,6 +310,12 @@ export function buildGooseAgentDockerArgs(params: {
|
||||
: []),
|
||||
"--workdir",
|
||||
"/workspace",
|
||||
...(params.hostInstructionsFile
|
||||
? [
|
||||
"--mount",
|
||||
`type=bind,source=${params.hostInstructionsFile},target=${GOOSE_CONTAINER_PROMPT_PATH},readonly`
|
||||
]
|
||||
: []),
|
||||
...Object.keys(params.containerEnv).flatMap((key) => ["--env", key]),
|
||||
GOOSE_DOCKER_IMAGE,
|
||||
"sh",
|
||||
@@ -268,8 +334,7 @@ export function buildGooseAgentDockerArgs(params: {
|
||||
config.gooseModel,
|
||||
"--with-builtin",
|
||||
"developer",
|
||||
"--text",
|
||||
params.prompt
|
||||
...inputArgs
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user