fix: clean agent-owned snapshot files

This commit is contained in:
lezi-fun committed 2026-08-14 11:24:32 +08:00
1 parent fae82f9151
commit 8f9101e5cd
2 files changed
+68 -2

No files matched your search

+52 -2
View File
@@ -14,6 +14,8 @@ const GOOSE_DOCKER_VERSION = "v1.46.0";
const GOOSE_CONTAINER_PATH = "/usr/local/bin/goose"; const GOOSE_CONTAINER_PATH = "/usr/local/bin/goose";
const GOOSE_CONTAINER_BOOTSTRAP = [ const GOOSE_CONTAINER_BOOTSTRAP = [
"set -eu", "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", "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", " 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`, ` 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", " fi",
' export PATH="/tmp/goose-bin:$PATH"', ' export PATH="/tmp/goose-bin:$PATH"',
"fi", "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"); ].join("\n");
export async function runGoosePrompt( export async function runGoosePrompt(
@@ -130,7 +136,11 @@ export async function runGooseAgent(
try { try {
await removeDockerContainer(containerName, realWorkingDirectory); await removeDockerContainer(containerName, realWorkingDirectory);
} finally { } 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<void> {
await runProcess(
"docker",
buildWorkspacePermissionDockerArgs(realWorkingDirectory),
{},
realWorkingDirectory,
"goose agent workspace permission cleanup"
);
}
async function resolveHostGooseBinary(): Promise<string | undefined> { async function resolveHostGooseBinary(): Promise<string | undefined> {
const configuredPath = process.env.GHBOT_GOOSE_BINARY?.trim(); const configuredPath = process.env.GHBOT_GOOSE_BINARY?.trim();
if (!configuredPath || process.platform !== "linux") { if (!configuredPath || process.platform !== "linux") {
+16
View File
@@ -2,6 +2,7 @@ import assert from "node:assert/strict";
import test from "node:test"; import test from "node:test";
import { import {
buildGooseAgentDockerArgs, buildGooseAgentDockerArgs,
buildWorkspacePermissionDockerArgs,
extractGooseFinalText extractGooseFinalText
} from "../src/ai/gooseCli.js"; } 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]; const bootstrap = args[args.indexOf("-lc") + 1];
assert.match(bootstrap!, /command -v goose/); assert.match(bootstrap!, /command -v goose/);
assert.match(bootstrap!, /cat \/tmp\/goose-install\.log >&2/); 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"); 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"/); 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", () => { test("goose output extracts the latest assistant text", () => {
const output = JSON.stringify({ const output = JSON.stringify({
messages: [ messages: [