Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(computer): a failed or cut-off image build no longer leaves stopped containers behind

Klaus Singerf89622527417
diff --git a/server/container-computer.test.ts b/server/container-computer.test.ts
index af106085a4..2fe7a0fc90 100644
--- a/server/container-computer.test.ts
+++ b/server/container-computer.test.ts
@@ -201,9 +201,9 @@ describe("managed image preparation", () => {
if (args.join(" ") === (runtime === "container" ? `image pull ${BASE_IMAGE}` : `pull ${BASE_IMAGE}`)) {
return { stdout: "pulled" };
}
- if (args.slice(0, 3).join(" ") === `build -t ${IMAGE}`) {
- context = args[3];
- expect(readFileSync(join(context, "Dockerfile"), "utf8")).toBe(managedImageDockerfile());
+ if (args[0] === "build") {
+ context = args.at(-1);
+ expect(readFileSync(join(context!, "Dockerfile"), "utf8")).toBe(managedImageDockerfile());
prepared = true;
return { stdout: "built" };
}
@@ -217,6 +217,31 @@ describe("managed image preparation", () => {
expect(pullIndex).toBeGreaterThanOrEqual(0);
expect(buildIndex).toBeGreaterThan(pullIndex);
expect(context).toBeDefined();
+ const buildFlags = runtime === "container" ? [] : ["--force-rm"];
+ expect(calls[buildIndex]).toEqual([runtime, "build", ...buildFlags, "-t", IMAGE, context]);
+ expect(existsSync(context!)).toBe(false);
+ });
+
+ it.each(["docker", "podman"] as const)("has %s remove a failed build's step containers", async runtime => {
+ let build: string[] | undefined;
+ const run: CommandRunner = async (command, args) => {
+ if (command === "/usr/bin/which") {
+ if (args[0] === runtime) return { stdout: runtime };
+ throw new Error("missing executable");
+ }
+ if (args[0] === "info") return { stdout: "running" };
+ if (args.slice(0, 2).join(" ") === "image inspect") throw new Error("missing image");
+ if (args[0] === "inspect") throw new Error("missing container");
+ if (args[0] === "pull") return { stdout: "pulled" };
+ if (args[0] === "build") {
+ build = [command, ...args];
+ throw new Error("The command '/bin/sh -c pip install' returned a non-zero code: 1");
+ }
+ throw new Error(`unsupported command: ${command} ${args.join(" ")}`);
+ };
+ await expect(containerComputerAction("pull", run, "darwin")).rejects.toThrow("non-zero code");
+ const context = build?.at(-1);
+ expect(build).toEqual([runtime, "build", "--force-rm", "-t", IMAGE, context]);
expect(existsSync(context!)).toBe(false);
});
});
diff --git a/server/container-computer.ts b/server/container-computer.ts
index 6869c73332..6c06ddb626 100644
--- a/server/container-computer.ts
+++ b/server/container-computer.ts
@@ -918,12 +918,22 @@ function baseImagePullArgs(runtime: Runtime): string[] {
return runtime === "container" ? ["image", "pull", BASE_IMAGE] : ["pull", BASE_IMAGE];
}
+/**
+ * Docker 29's legacy builder, which it still uses when it can't find the buildx plugin or `DOCKER_BUILDKIT=0` is set,
+ * keeps a step's stopped container when the build fails or the client goes away during the commit. Only `--force-rm`
+ * removes it. BuildKit accepts the flag and leaves no containers. Apple's `container build` has no such flag and
+ * builds with BuildKit.
+ */
+export function imageBuildArgs(runtime: Runtime, tag: string, context: string): string[] {
+ return runtime === "container" ? ["build", "-t", tag, context] : ["build", "--force-rm", "-t", tag, context];
+}
+
async function prepareManagedImage(runtime: Runtime, runner: CommandRunner): Promise<void> {
await runner(runtime, baseImagePullArgs(runtime), 10 * 60_000);
const context = await mkdtemp(join(tmpdir(), "openmausbot-cua-image-"));
try {
await writeFile(join(context, "Dockerfile"), managedImageDockerfile(), { mode: 0o600 });
- await runner(runtime, ["build", "-t", IMAGE, context], 10 * 60_000);
+ await runner(runtime, imageBuildArgs(runtime, IMAGE, context), 10 * 60_000);
} finally {
await rm(context, { recursive: true, force: true });
}
diff --git a/server/vps-computer.test.ts b/server/vps-computer.test.ts
index 4c821a85f4..daaeeec0e9 100644
--- a/server/vps-computer.test.ts
+++ b/server/vps-computer.test.ts
@@ -471,7 +471,8 @@ describe("VPS computer", () => {
expect(run.some((arg) => /^VNC_PW=[A-Za-z0-9_-]{8,}$/.test(arg))).toBe(true);
expect(run).not.toContain("--mount");
expect(run).not.toContain("-p");
- expect(provision.calls.some(({ args }) => args[2] === "build")).toBe(true);
+ const build = provision.calls.find(({ args }) => args[2] === "build");
+ expect(build?.args.slice(2)).toEqual(["build", "--force-rm", "-t", VPS_IMAGE, "-"]);
});
it("uses the image id produced by a rebuild", async () => {
diff --git a/server/vps-computer.ts b/server/vps-computer.ts
index e141fd08dd..5fc61d7fbc 100644
--- a/server/vps-computer.ts
+++ b/server/vps-computer.ts
@@ -10,6 +10,7 @@ import {
IMAGE as CUA_IMAGE,
cuaExecArgs,
dockerSecurityIsHardened,
+ imageBuildArgs,
imageLabelsMatch,
managedImageDockerfile,
wholeScreenshot,
@@ -896,7 +897,7 @@ function assertUsableContainer(status: VpsComputerStatus) {
async function prepareVpsImage(alias: string, runner: VpsCommandRunner) {
await runner(vpsDockerArgs(alias, ["pull", BASE_IMAGE]), { timeoutMs: 10 * 60_000 });
- await runner(vpsDockerArgs(alias, ["build", "-t", VPS_IMAGE, "-"]), {
+ await runner(vpsDockerArgs(alias, imageBuildArgs("docker", VPS_IMAGE, "-")), {
input: managedImageDockerfile(),
timeoutMs: 10 * 60_000,
});