Skip to content

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

fix(container): keep browser locks across driver restarts

Sebastian Ba8ac33f3609e
diff --git a/docs/verification/local-vm-resume.md b/docs/verification/local-vm-resume.md
index 957759b1a4..0a3e921209 100644
--- a/docs/verification/local-vm-resume.md
+++ b/docs/verification/local-vm-resume.md
@@ -3,10 +3,16 @@
Run the focused lifecycle checks in disposable homes:
```sh
-pnpm exec vitest run server/container-computer.test.ts server/local-vm-stop-reason.test.ts server/computer-idle.test.ts server/group-local-vm.e2e.test.ts server/routes/desktop-viewer.test.ts src/components/LocalComputerSection.test.ts src/components/ComputerPanel.test.ts src/components/ComputerPanel.i18n.test.ts
+pnpm exec vitest run server/container-computer.test.ts server/local-vm-browser-profiles.test.ts server/local-vm-stop-reason.test.ts server/computer-idle.test.ts server/group-local-vm.e2e.test.ts server/routes/desktop-viewer.test.ts src/components/LocalComputerSection.test.ts src/components/ComputerPanel.test.ts src/components/ComputerPanel.i18n.test.ts
node --experimental-strip-types scripts/verify-local-vm-resume.ts
```
+The image preparation fixture runs the generated shell script in a temporary
+home and workspace. It supplies synthetic Linux boot and PID 1 start records.
+Browser locks are cleared once per container start. A Cua driver restart keeps
+live Chromium and Firefox locks. A changed PID 1 start time or boot ID clears
+old locks again, including when the container reuses its home directory.
+
The browser recipe uses `launchVerificationServer`, the fake engine and a
private browser profile. It mounts the real ComputerPanel with synthetic
desktop transport; it never contacts the host Docker daemon. Set
diff --git a/server/container-computer.test.ts b/server/container-computer.test.ts
index 2f8245f9e1..d4c1a7bdb0 100644
--- a/server/container-computer.test.ts
+++ b/server/container-computer.test.ts
@@ -847,7 +847,7 @@ describe("Cua integration", () => {
expect(dockerfile).toContain("6a73f9541c2de74158c0e7cf6b0a58ef774f5a780bf191f2d7ec9cc53efe2bf2");
expect(dockerfile).toContain("/usr/local/share/licenses/noto-cjk/OFL.txt");
expect(dockerfile).toContain("fc-cache -f");
- expect(IMAGE_LAYER_VERSION).toBe("6");
+ expect(IMAGE_LAYER_VERSION).toBe("7");
});
it("rejects a zero-byte OpenSSL base image before the wheel download needs curl", () => {
diff --git a/server/container-computer.ts b/server/container-computer.ts
index f269f8ec83..b2b5190ae2 100644
--- a/server/container-computer.ts
+++ b/server/container-computer.ts
@@ -8,6 +8,7 @@ import { join, resolve } from "node:path";
import { promisify } from "node:util";
import { augmentedPath, resolveCliSpawn } from "./env-path.ts";
+import { workspacePreparationDockerfile } from "./container-workspace.ts";
import { DATA_DIR } from "./config.ts";
import { PIDS_LIMIT, raisePidsLimit } from "./container-limits.ts";
import { SPAWNED_PROXIES } from "./proxy-paths.ts";
@@ -29,7 +30,7 @@ export const BASE_IMAGE = `${BASE_IMAGE_REPOSITORY}@${BASE_IMAGE_DIGEST}`;
// localhost/ is explicit: Podman prepends it to unqualified build tags and may then resolve the name on Docker Hub.
// Image and container labels, not this mutable tag, decide compatibility.
export const IMAGE_REPOSITORY = "localhost/openmausbot/cua-local-vm";
-export const IMAGE_LAYER_VERSION = "6";
+export const IMAGE_LAYER_VERSION = "7";
export const IMAGE_LAYER_LABEL = "com.openmausbot.image-layer";
export const IMAGE = `${IMAGE_REPOSITORY}:driver-${CUA_DRIVER_VERSION}-v${IMAGE_LAYER_VERSION}`;
export const CONTAINER = "openmausbot-computer";
@@ -150,32 +151,7 @@ RUN set -eux; \\
curl -fsSL 'https://raw.githubusercontent.com/notofonts/noto-cjk/165c01b46ea533872e002e0785ff17e44f6d97d8/LICENSE' -o /usr/local/share/licenses/noto-cjk/OFL.txt; \\
echo '6a73f9541c2de74158c0e7cf6b0a58ef774f5a780bf191f2d7ec9cc53efe2bf2 /usr/local/share/licenses/noto-cjk/OFL.txt' | sha256sum -c -; \\
fc-cache -f
-RUN printf '%s\\n' \\
- '#!/bin/sh' \\
- 'set -eu' \\
- 'workspace=${VM_WORKSPACE_GUEST}' \\
- 'profiles="$workspace/.browser-profiles"' \\
- 'mkdir -p "$profiles/google-chrome" "$profiles/chromium" "$profiles/mozilla" "$HOME/.config"' \\
- 'if ! chmod 0700 "$workspace" "$profiles" "$profiles/google-chrome" "$profiles/chromium" "$profiles/mozilla" 2>/dev/null; then' \\
- ' for directory in "$workspace" "$profiles" "$profiles/google-chrome" "$profiles/chromium" "$profiles/mozilla"; do' \\
- ' test -r "$directory" && test -w "$directory" && test -x "$directory"' \\
- ' done' \\
- 'fi' \\
- 'migrate_profile() {' \\
- ' source="$1"' \\
- ' target="$profiles/$2"' \\
- ' if [ -d "$source" ] && [ ! -L "$source" ] && [ -z "$(find "$target" -mindepth 1 -print -quit)" ]; then' \\
- ' cp -a "$source"/. "$target"/' \\
- ' fi' \\
- ' rm -rf "$source"' \\
- ' ln -s "$target" "$source"' \\
- '}' \\
- 'migrate_profile "$HOME/.config/google-chrome" google-chrome' \\
- 'migrate_profile "$HOME/.config/chromium" chromium' \\
- 'migrate_profile "$HOME/.mozilla" mozilla' \\
- 'find "$profiles" \\( -name SingletonLock -o -name SingletonSocket -o -name SingletonCookie -o -name .parentlock -o -name lock -type l \\) -delete' \\
- > /usr/local/bin/prepare-openmausbot-workspace.sh \\
- && chmod 0755 /usr/local/bin/prepare-openmausbot-workspace.sh
+${workspacePreparationDockerfile(VM_WORKSPACE_GUEST)}
# A bot's own desktop: a 200 ms (not 800) watch for windows an action opens still sees Firefox popups; no cursor glide; plain logs.
RUN printf '%s\\n' \\
'#!/bin/sh' \\
diff --git a/server/container-workspace.ts b/server/container-workspace.ts
new file mode 100644
index 0000000000..d31999b7c3
--- /dev/null
+++ b/server/container-workspace.ts
@@ -0,0 +1,33 @@
+export function workspacePreparationDockerfile(workspace: string): string {
+ return `RUN printf '%s\\n' \\
+ '#!/bin/sh' \\
+ 'set -eu' \\
+ 'workspace=${workspace}' \\
+ 'profiles="$workspace/.browser-profiles"' \\
+ 'mkdir -p "$profiles/google-chrome" "$profiles/chromium" "$profiles/mozilla" "$HOME/.config" "$HOME/.cache"' \\
+ 'if ! chmod 0700 "$workspace" "$profiles" "$profiles/google-chrome" "$profiles/chromium" "$profiles/mozilla" 2>/dev/null; then' \\
+ ' for directory in "$workspace" "$profiles" "$profiles/google-chrome" "$profiles/chromium" "$profiles/mozilla"; do' \\
+ ' test -r "$directory" && test -w "$directory" && test -x "$directory"' \\
+ ' done' \\
+ 'fi' \\
+ 'migrate_profile() {' \\
+ ' source="$1"' \\
+ ' target="$profiles/$2"' \\
+ ' if [ -d "$source" ] && [ ! -L "$source" ] && [ -z "$(find "$target" -mindepth 1 -print -quit)" ]; then' \\
+ ' cp -a "$source"/. "$target"/' \\
+ ' fi' \\
+ ' rm -rf "$source"' \\
+ ' ln -s "$target" "$source"' \\
+ '}' \\
+ 'migrate_profile "$HOME/.config/google-chrome" google-chrome' \\
+ 'migrate_profile "$HOME/.config/chromium" chromium' \\
+ 'migrate_profile "$HOME/.mozilla" mozilla' \\
+ 'container_start="$(cat /proc/sys/kernel/random/boot_id):$(sed "s/.*) //" /proc/1/stat | cut -d " " -f20)"' \\
+ 'prepared="$HOME/.cache/openmausbot-browser-locks"' \\
+ 'if [ "$(cat "$prepared" 2>/dev/null || true)" != "$container_start" ]; then' \\
+ ' find "$profiles" \\( -name SingletonLock -o -name SingletonSocket -o -name SingletonCookie -o -name .parentlock -o -name lock -type l \\) -delete' \\
+ ' printf "%s\\n" "$container_start" > "$prepared"' \\
+ 'fi' \\
+ > /usr/local/bin/prepare-openmausbot-workspace.sh \\
+ && chmod 0755 /usr/local/bin/prepare-openmausbot-workspace.sh`;
+}
diff --git a/server/local-vm-browser-profiles.test.ts b/server/local-vm-browser-profiles.test.ts
index 11ad851e8f..a7a5049fed 100644
--- a/server/local-vm-browser-profiles.test.ts
+++ b/server/local-vm-browser-profiles.test.ts
@@ -2,7 +2,7 @@
// against a fake home, workspace and container runtime, so "sign-ins survive replacing the desktop" is checked, not assumed.
import { execFileSync } from "node:child_process";
import { chmodSync, existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, readlinkSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
-import { tmpdir } from "node:os";
+import { hostname, tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
@@ -26,7 +26,14 @@ function prepareScript(dir: string, workspace: string): string {
const start = dockerfile.lastIndexOf("RUN printf", end);
expect(start).toBeGreaterThan(-1);
const script = join(dir, "prepare.sh");
- const printf = dockerfile.slice(start + "RUN ".length, end).replaceAll(VM_WORKSPACE_GUEST, workspace);
+ const bootId = join(dir, "boot-id");
+ const initStat = join(dir, "init-stat");
+ if (!existsSync(bootId)) {
+ writeFileSync(bootId, "synthetic-boot");
+ writeFileSync(initStat, ["1 (container init) S", ...Array(18).fill(0), 123, 0].join(" "));
+ }
+ const printf = dockerfile.slice(start + "RUN ".length, end).replaceAll(VM_WORKSPACE_GUEST, workspace)
+ .replaceAll("/proc/sys/kernel/random/boot_id", bootId).replaceAll("/proc/1/stat", initStat);
execFileSync("/bin/sh", ["-c", `${printf} > '${script}'`]);
return script;
}
@@ -74,6 +81,36 @@ describe.skipIf(process.platform === "win32")("the image's start-up script keeps
runPrepare(prepareScript(dir, workspace), home);
expect(readFileSync(join(profile, "cookies.sqlite"), "utf8")).toBe("signed in");
});
+
+ it.each(["PID 1 start time", "boot ID"])("keeps live browser locks across a Cua driver restart until the %s changes", (identity) => {
+ const dir = scratch();
+ const home = join(dir, "home");
+ const workspace = join(dir, "workspace");
+ const script = prepareScript(dir, workspace);
+ runPrepare(script, home);
+ const profiles = join(workspace, ".browser-profiles");
+ const firefox = join(profiles, "mozilla", "firefox", "live.default-esr");
+ mkdirSync(firefox, { recursive: true });
+ const locks = [
+ [join(profiles, "chromium", "SingletonLock"), `${hostname()}-${process.pid}`],
+ [join(profiles, "chromium", "SingletonSocket"), "/tmp/synthetic-chromium-socket"],
+ [join(profiles, "chromium", "SingletonCookie"), "synthetic-cookie"],
+ [join(firefox, "lock"), `127.0.0.1:+${process.pid}`],
+ ];
+ for (const [lock, target] of locks) symlinkSync(target, lock);
+ writeFileSync(join(firefox, ".parentlock"), "");
+
+ runPrepare(script, home);
+
+ for (const [lock, target] of locks) expect(readlinkSync(lock)).toBe(target);
+ expect(existsSync(join(firefox, ".parentlock"))).toBe(true);
+
+ if (identity === "boot ID") writeFileSync(join(dir, "boot-id"), "next-synthetic-boot");
+ else writeFileSync(join(dir, "init-stat"), ["1 (container init) S", ...Array(18).fill(0), 456, 0].join(" "));
+ runPrepare(script, home);
+ for (const [lock] of locks) expect(() => lstatSync(lock)).toThrow();
+ expect(existsSync(join(firefox, ".parentlock"))).toBe(false);
+ });
});
/** A container runtime whose `cp` copies out of a fixture "container" tree, as docker cp does for a stopped container. */