diff --git a/src/ai/gooseCli.ts b/src/ai/gooseCli.ts index c57d56c..4a3593a 100644 --- a/src/ai/gooseCli.ts +++ b/src/ai/gooseCli.ts @@ -14,6 +14,8 @@ const GOOSE_DOCKER_VERSION = "v1.46.0"; const GOOSE_CONTAINER_PATH = "/usr/local/bin/goose"; const GOOSE_CONTAINER_BOOTSTRAP = [ "set -eu", + "cleanup_workspace() { chmod -R a+rwX /workspace 2>/dev/null || true; }", + "trap cleanup_workspace EXIT", "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`, @@ -22,7 +24,11 @@ const GOOSE_CONTAINER_BOOTSTRAP = [ " fi", ' export PATH="/tmp/goose-bin:$PATH"', "fi", - 'exec goose "$@"' + 'goose "$@"' +].join("\n"); +const GOOSE_WORKSPACE_PERMISSION_SCRIPT = [ + "set -eu", + "find /workspace \\( -type d -o -type f \\) -uid 0 -exec chmod a+rwX {} +" ].join("\n"); export async function runGoosePrompt( @@ -130,7 +136,11 @@ export async function runGooseAgent( try { await removeDockerContainer(containerName, realWorkingDirectory); } finally { - await proxy.close(); + try { + await restoreAgentWorkspacePermissions(realWorkingDirectory); + } finally { + await proxy.close(); + } } } } @@ -194,6 +204,46 @@ export function buildGooseAgentDockerArgs(params: { ]; } +export function buildWorkspacePermissionDockerArgs(realWorkingDirectory: string): string[] { + return [ + "run", + "--rm", + "--init", + "--user", + "root", + "--cpus", + "1", + "--memory", + "256m", + "--pids-limit", + "64", + "--network", + "none", + "--security-opt", + "no-new-privileges", + "--cap-drop", + "ALL", + "--mount", + `type=bind,source=${realWorkingDirectory},target=/workspace`, + "--workdir", + "/workspace", + GOOSE_DOCKER_IMAGE, + "sh", + "-lc", + GOOSE_WORKSPACE_PERMISSION_SCRIPT + ]; +} + +async function restoreAgentWorkspacePermissions(realWorkingDirectory: string): Promise { + await runProcess( + "docker", + buildWorkspacePermissionDockerArgs(realWorkingDirectory), + {}, + realWorkingDirectory, + "goose agent workspace permission cleanup" + ); +} + async function resolveHostGooseBinary(): Promise { const configuredPath = process.env.GHBOT_GOOSE_BINARY?.trim(); if (!configuredPath || process.platform !== "linux") { diff --git a/test/goose-cli.test.ts b/test/goose-cli.test.ts index 5ee4eef..92eda5f 100644 --- a/test/goose-cli.test.ts +++ b/test/goose-cli.test.ts @@ -2,6 +2,7 @@ import assert from "node:assert/strict"; import test from "node:test"; import { buildGooseAgentDockerArgs, + buildWorkspacePermissionDockerArgs, extractGooseFinalText } from "../src/ai/gooseCli.js"; @@ -18,6 +19,9 @@ test("goose agent mounts the workflow binary read-only and keeps a visible insta const bootstrap = args[args.indexOf("-lc") + 1]; assert.match(bootstrap!, /command -v goose/); assert.match(bootstrap!, /cat \/tmp\/goose-install\.log >&2/); + assert.match(bootstrap!, /trap cleanup_workspace EXIT/); + assert.match(bootstrap!, /chmod -R a\+rwX \/workspace/); + assert.doesNotMatch(bootstrap!, /exec goose "\$@"/); assert.equal(args.at(-1), "introduce this pull request"); }); @@ -33,6 +37,18 @@ test("goose agent can fall back to installing inside the container", () => { assert.match(args[args.indexOf("-lc") + 1]!, /GOOSE_VERSION="v1\.46\.0"/); }); +test("goose agent restores host-cleanable permissions for root-owned workspace entries", () => { + const args = buildWorkspacePermissionDockerArgs("/tmp/worktree"); + + assert.ok(args.includes("--rm")); + assert.ok(args.includes("none")); + assert.ok(args.includes("type=bind,source=/tmp/worktree,target=/workspace")); + const script = args[args.indexOf("-lc") + 1]; + assert.match(script!, /find \/workspace/); + assert.match(script!, /-uid 0/); + assert.match(script!, /chmod a\+rwX/); +}); + test("goose output extracts the latest assistant text", () => { const output = JSON.stringify({ messages: [