Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix: never delete a home profile copy the workspace does not fully contain
koeria0d10dbd31bf4
diff --git a/server/container-computer.test.ts b/server/container-computer.test.tsindex 20a751fc10..f0c92b98e5 100644--- a/server/container-computer.test.ts+++ b/server/container-computer.test.ts@@ -1030,9 +1030,12 @@ describe("Cua integration", () => {expect(dockerfile).toContain("prepare-openmausbot-workspace.sh");expect(dockerfile).toContain('if ! chmod 0700 "$workspace"');expect(dockerfile).toContain('test -r "$directory" && test -w "$directory" && test -x "$directory"');- expect(dockerfile).toContain('migrate_profile "$HOME/.config/google-chrome" google-chrome');- expect(dockerfile).toContain('migrate_profile "$HOME/.config/chromium" chromium');- expect(dockerfile).toContain('migrate_profile "$HOME/.mozilla" mozilla');+ expect(dockerfile).toContain('migrate_profile "$HOME/.config/google-chrome" "$profiles/google-chrome"');+ expect(dockerfile).toContain('migrate_profile "$HOME/.config/chromium" "$profiles/chromium"');+ expect(dockerfile).toContain('migrate_profile "$HOME/.mozilla" "$profiles/mozilla"');+ expect(dockerfile).toContain('migrate_profile "$HOME/.local" "$apps/local"');+ expect(dockerfile).toContain("cp -an");+ expect(dockerfile).toContain("unmerged-");expect(dockerfile).toContain("SingletonLock");expect(dockerfile).toContain(`${IMAGE_LAYER_LABEL}="${IMAGE_LAYER_VERSION}"`);expect(dockerfile).toContain("did not become ready within 45 seconds");diff --git a/server/container-workspace.ts b/server/container-workspace.tsindex ecada14d18..1d3cfb86f2 100644--- a/server/container-workspace.ts+++ b/server/container-workspace.ts@@ -13,21 +13,41 @@ export function workspacePreparationDockerfile(workspace: string): string {'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"/' \\+ ' target="$2"' \\+ ' if [ -e "$source" ] && [ ! -L "$source" ]; then' \\+ ' if [ -d "$source" ]; then' \\+ ' cp -an "$source/." "$target/"' \\+ ' missing=$(cd "$source" && find . -mindepth 1 -print | while IFS= read -r item; do' \\+ ' if [ -f "$item" ]; then' \\+ ' cmp -s "$item" "$target/$item" || echo "$item"' \\+ ' elif [ ! -e "$target/$item" ]; then' \\+ ' echo "$item"' \\+ ' fi' \\+ ' done)' \\+ ' if [ -z "$missing" ]; then' \\+ ' rm -rf "$source"' \\+ ' else' \\+ ' mv "$source" "$workspace/unmerged-$(basename "$target")-$(date +%s)-$$"' \\+ ' fi' \\+ ' else' \\+ ' name=$(basename "$source")' \\+ ' if [ -f "$target/$name" ] && cmp -s "$source" "$target/$name"; then' \\+ ' rm -f "$source"' \\+ ' elif [ ! -e "$target/$name" ]; then' \\+ ' cp -a "$source" "$target/$name"' \\+ ' rm -f "$source"' \\+ ' else' \\+ ' mv "$source" "$workspace/unmerged-$name-$(date +%s)-$$"' \\+ ' fi' \\+ ' fi' \\' 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' \\- 'if [ -d "$HOME/.local" ] && [ ! -L "$HOME/.local" ] && [ -z "$(find "$apps/local" -mindepth 1 -print -quit)" ]; then' \\- ' cp -a "$HOME/.local"/. "$apps/local"/' \\- 'fi' \\- 'rm -rf "$HOME/.local"' \\- 'ln -s "$apps/local" "$HOME/.local"' \\+ 'migrate_profile "$HOME/.config/google-chrome" "$profiles/google-chrome"' \\+ 'migrate_profile "$HOME/.config/chromium" "$profiles/chromium"' \\+ 'migrate_profile "$HOME/.mozilla" "$profiles/mozilla"' \\+ 'migrate_profile "$HOME/.local" "$apps/local"' \\'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' \\diff --git a/server/local-vm-browser-profiles.test.ts b/server/local-vm-browser-profiles.test.tsindex 113586cf36..f0f5b2f998 100644--- a/server/local-vm-browser-profiles.test.ts+++ b/server/local-vm-browser-profiles.test.ts@@ -69,6 +69,49 @@ describe.skipIf(process.platform === "win32")("the image's start-up script keepsexpect(readFileSync(join(home, ".local", "share", "applications", "example.desktop"), "utf8")).toBe("[Desktop Entry]");});+ it("recovers a home copy left incomplete by an interrupted copy instead of deleting it", () => {+ const dir = scratch();+ const home = join(dir, "home");+ const workspace = join(dir, "workspace");+ // A previous start was killed mid-copy: the workspace copy is non-empty but incomplete.+ mkdirSync(join(home, ".local", "share", "applications"), { recursive: true });+ writeFileSync(join(home, ".local", "share", "applications", "only-in-home.desktop"), "home");+ writeFileSync(join(home, ".local", "share", "bookmarks"), "home bookmarks");+ mkdirSync(join(workspace, ".desktop-apps", "local", "share"), { recursive: true });+ writeFileSync(join(workspace, ".desktop-apps", "local", "share", "partial-from-killed-copy"), "partial");+ runPrepare(prepareScript(dir, workspace), home);+ const durable = join(workspace, ".desktop-apps", "local");+ expect(readFileSync(join(durable, "share", "applications", "only-in-home.desktop"), "utf8")).toBe("home");+ expect(readFileSync(join(durable, "share", "bookmarks"), "utf8")).toBe("home bookmarks");+ expect(readFileSync(join(durable, "share", "partial-from-killed-copy"), "utf8")).toBe("partial");+ expect(lstatSync(join(home, ".local")).isSymbolicLink()).toBe(true);+ });++ it("moves a home copy aside rather than deleting it when the workspace copy differs, and never overwrites a differing workspace file", () => {+ const dir = scratch();+ const home = join(dir, "home");+ const workspace = join(dir, "workspace");+ mkdirSync(join(home, ".local", "share"), { recursive: true });+ writeFileSync(join(home, ".local", "share", "bookmarks"), "newer home version");+ mkdirSync(join(workspace, ".desktop-apps", "local", "share"), { recursive: true });+ writeFileSync(join(workspace, ".desktop-apps", "local", "share", "bookmarks"), "workspace version");+ runPrepare(prepareScript(dir, workspace), home);+ expect(readFileSync(join(workspace, ".desktop-apps", "local", "share", "bookmarks"), "utf8")).toBe("workspace version");+ expect(lstatSync(join(home, ".local")).isSymbolicLink()).toBe(true);+ expect(readdirSync(workspace).some((name) => name.startsWith("unmerged-local-"))).toBe(true);+ });++ it("keeps a home path that is a regular file instead of deleting it silently", () => {+ const dir = scratch();+ const home = join(dir, "home");+ const workspace = join(dir, "workspace");+ mkdirSync(home, { recursive: true });+ writeFileSync(join(home, ".mozilla"), "saved file, not a profile");+ runPrepare(prepareScript(dir, workspace), home);+ expect(readFileSync(join(workspace, ".browser-profiles", "mozilla", ".mozilla"), "utf8")).toBe("saved file, not a profile");+ expect(lstatSync(join(home, ".mozilla")).isSymbolicLink()).toBe(true);+ });+it("keeps a durable profile over the image's empty one and clears the old desktop's Firefox locks", () => {const dir = scratch();const home = join(dir, "home");