From 3647944ad7d293cb2d2991000448dedf6e98c387 Mon Sep 17 00:00:00 2001 From: lezi-fun <177434121+lezi-fun@users.noreply.github.com> Date: Fri, 14 Aug 2026 10:07:48 +0800 Subject: [PATCH] fix: reuse Goose binary in agent containers --- .github/workflows/review-reusable.yml | 1 + src/ai/gooseCli.ts | 145 ++++++++++++++++++-------- test/goose-cli.test.ts | 33 +++++- test/workflow-security.test.ts | 1 + 4 files changed, 137 insertions(+), 43 deletions(-) diff --git a/.github/workflows/review-reusable.yml b/.github/workflows/review-reusable.yml index ff60431..932c0ae 100644 --- a/.github/workflows/review-reusable.yml +++ b/.github/workflows/review-reusable.yml @@ -272,6 +272,7 @@ jobs: GHBOT_REPOSITORY_ID: ${{ inputs.repository_id }} GHBOT_PULL_NUMBER: ${{ inputs.pull_number }} GHBOT_PULL_HEAD_REPOSITORY: ${{ inputs.pull_head_repository }} + GHBOT_GOOSE_BINARY: ${{ runner.temp }}/goose/bin/goose GHBOT_COMMENT_ID: ${{ inputs.comment_id }} GHBOT_COMMENTER_LOGIN: ${{ inputs.commenter_login }} GHBOT_COMMENT_BODY: ${{ inputs.comment_body }} diff --git a/src/ai/gooseCli.ts b/src/ai/gooseCli.ts index b4ba760..c57d56c 100644 --- a/src/ai/gooseCli.ts +++ b/src/ai/gooseCli.ts @@ -1,4 +1,5 @@ import fs from "node:fs/promises"; +import { constants as fsConstants } from "node:fs"; import { spawn } from "node:child_process"; import { randomBytes } from "node:crypto"; import path from "node:path"; @@ -10,6 +11,19 @@ import { startOneRunApiProxy } from "./apiProxy.js"; const GOOSE_RUN_TIMEOUT_MS = 10 * 60 * 1000; const GOOSE_DOCKER_IMAGE = "node:24-bookworm"; const GOOSE_DOCKER_VERSION = "v1.46.0"; +const GOOSE_CONTAINER_PATH = "/usr/local/bin/goose"; +const GOOSE_CONTAINER_BOOTSTRAP = [ + "set -eu", + "if ! command -v goose >/dev/null 2>&1; then", + " curl -fsSL https://github.com/aaif-goose/goose/releases/download/stable/download_cli.sh -o /tmp/download_cli.sh", + ` if ! GOOSE_VERSION="${GOOSE_DOCKER_VERSION}" GOOSE_BIN_DIR=/tmp/goose-bin CONFIGURE=false bash /tmp/download_cli.sh >/tmp/goose-install.log 2>&1; then`, + " cat /tmp/goose-install.log >&2", + " exit 1", + " fi", + ' export PATH="/tmp/goose-bin:$PATH"', + "fi", + 'exec goose "$@"' +].join("\n"); export async function runGoosePrompt( prompt: string, @@ -73,6 +87,7 @@ export async function runGooseAgent( config.gooseApiKey ); const containerName = `ghbot-agent-${randomBytes(12).toString("hex")}`; + const hostGooseBinary = await resolveHostGooseBinary(); const containerEnv = { HOME: "/tmp/goose-home", XDG_CONFIG_HOME: "/tmp/goose-home/config", @@ -94,50 +109,13 @@ export async function runGooseAgent( ? { GOOSE_THINKING_EFFORT: config.gooseThinkingEffort } : {}) }; - const args = [ - "run", - "--name", + const args = buildGooseAgentDockerArgs({ containerName, - "--init", - "--user", - "root", - "--cpus", - "2", - "--memory", - "4g", - "--pids-limit", - "512", - "--security-opt", - "no-new-privileges", - "--cap-drop", - "ALL", - "--add-host", - "host.docker.internal:host-gateway", - "--mount", - `type=bind,source=${realWorkingDirectory},target=/workspace`, - "--workdir", - "/workspace", - ...Object.keys(containerEnv).flatMap((key) => ["--env", key]), - GOOSE_DOCKER_IMAGE, - "sh", - "-lc", - `set -eu; curl -fsSL https://github.com/aaif-goose/goose/releases/download/stable/download_cli.sh -o /tmp/download_cli.sh; GOOSE_VERSION="${GOOSE_DOCKER_VERSION}" GOOSE_BIN_DIR=/tmp/goose-bin CONFIGURE=false bash /tmp/download_cli.sh >/tmp/goose-install.log; export PATH="/tmp/goose-bin:$PATH"; exec goose "$@"`, - "ghbot", - "run", - "--no-session", - "--no-profile", - "--quiet", - "--output-format", - "json", - "--provider", - "openai", - "--model", - config.gooseModel, - "--with-builtin", - "developer", - "--text", + realWorkingDirectory, + containerEnv, + hostGooseBinary, prompt - ]; + }); try { const stdout = await runProcess( @@ -157,6 +135,89 @@ export async function runGooseAgent( } } +export function buildGooseAgentDockerArgs(params: { + containerName: string; + realWorkingDirectory: string; + containerEnv: Record; + prompt: string; + hostGooseBinary?: string; +}): string[] { + return [ + "run", + "--name", + params.containerName, + "--init", + "--user", + "root", + "--cpus", + "2", + "--memory", + "4g", + "--pids-limit", + "512", + "--security-opt", + "no-new-privileges", + "--cap-drop", + "ALL", + "--add-host", + "host.docker.internal:host-gateway", + "--mount", + `type=bind,source=${params.realWorkingDirectory},target=/workspace`, + ...(params.hostGooseBinary + ? [ + "--mount", + `type=bind,source=${params.hostGooseBinary},target=${GOOSE_CONTAINER_PATH},readonly` + ] + : []), + "--workdir", + "/workspace", + ...Object.keys(params.containerEnv).flatMap((key) => ["--env", key]), + GOOSE_DOCKER_IMAGE, + "sh", + "-lc", + GOOSE_CONTAINER_BOOTSTRAP, + "ghbot", + "run", + "--no-session", + "--no-profile", + "--quiet", + "--output-format", + "json", + "--provider", + "openai", + "--model", + config.gooseModel, + "--with-builtin", + "developer", + "--text", + params.prompt + ]; +} + +async function resolveHostGooseBinary(): Promise { + const configuredPath = process.env.GHBOT_GOOSE_BINARY?.trim(); + if (!configuredPath || process.platform !== "linux") { + return undefined; + } + + try { + const resolvedPath = await fs.realpath(configuredPath); + const stat = await fs.stat(resolvedPath); + if (!stat.isFile() || resolvedPath.includes(",")) { + throw new Error("configured Goose binary is not a mountable file path"); + } + await fs.access(resolvedPath, fsConstants.X_OK); + logger.info({ hostGooseBinary: resolvedPath }, "Using the workflow Goose binary inside the agent container."); + return resolvedPath; + } catch (error) { + logger.warn( + { error, configuredPath }, + "Configured workflow Goose binary is unavailable; falling back to an in-container install." + ); + return undefined; + } +} + function buildGooseEnv(tempDir: string): Record { return { HOME: path.join(tempDir, "home"), diff --git a/test/goose-cli.test.ts b/test/goose-cli.test.ts index b2916dc..5ee4eef 100644 --- a/test/goose-cli.test.ts +++ b/test/goose-cli.test.ts @@ -1,6 +1,37 @@ import assert from "node:assert/strict"; import test from "node:test"; -import { extractGooseFinalText } from "../src/ai/gooseCli.js"; +import { + buildGooseAgentDockerArgs, + extractGooseFinalText +} from "../src/ai/gooseCli.js"; + +test("goose agent mounts the workflow binary read-only and keeps a visible install fallback", () => { + const args = buildGooseAgentDockerArgs({ + containerName: "ghbot-agent-test", + realWorkingDirectory: "/tmp/worktree", + containerEnv: { OPENAI_API_KEY: "one-run-token" }, + hostGooseBinary: "/tmp/goose/bin/goose", + prompt: "introduce this pull request" + }); + + assert.ok(args.includes("type=bind,source=/tmp/goose/bin/goose,target=/usr/local/bin/goose,readonly")); + const bootstrap = args[args.indexOf("-lc") + 1]; + assert.match(bootstrap!, /command -v goose/); + assert.match(bootstrap!, /cat \/tmp\/goose-install\.log >&2/); + assert.equal(args.at(-1), "introduce this pull request"); +}); + +test("goose agent can fall back to installing inside the container", () => { + const args = buildGooseAgentDockerArgs({ + containerName: "ghbot-agent-test", + realWorkingDirectory: "/tmp/worktree", + containerEnv: {}, + prompt: "inspect this pull request" + }); + + assert.equal(args.some((arg) => arg.includes("target=/usr/local/bin/goose")), false); + assert.match(args[args.indexOf("-lc") + 1]!, /GOOSE_VERSION="v1\.46\.0"/); +}); test("goose output extracts the latest assistant text", () => { const output = JSON.stringify({ diff --git a/test/workflow-security.test.ts b/test/workflow-security.test.ts index 10e00de..625bd81 100644 --- a/test/workflow-security.test.ts +++ b/test/workflow-security.test.ts @@ -7,5 +7,6 @@ test("pull_request_target checks out a PR worktree only for same-repository head const caller = await fs.readFile(".github/workflows/review.yml", "utf8"); assert.match(workflow, /pull_head_repository == format\('\{0\}\/\{1\}'/); assert.doesNotMatch(workflow, /allow-unsafe-pr-checkout:\s*true/); + assert.match(workflow, /GHBOT_GOOSE_BINARY:\s*\$\{\{ runner\.temp \}\}\/goose\/bin\/goose/); assert.match(caller, /pull_head_repository:\s*\$\{\{ github\.event\.pull_request\.head\.repo\.full_name \|\| '' \}\}/); });