Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Backups leave out Python virtual environments and bytecode caches
milind-sonicbb2b7d58581
diff --git a/server/workspace-backup-policy.ts b/server/workspace-backup-policy.tsindex ab769682d1..cd9074067e 100644--- a/server/workspace-backup-policy.ts+++ b/server/workspace-backup-policy.ts@@ -1,4 +1,4 @@-import { readlinkSync } from "node:fs";+import { lstatSync, readlinkSync, type Stats } from "node:fs";import { dirname, isAbsolute, join, relative, resolve } from "node:path";import { parseStoredConfig } from "./config.ts";import { inSecretFolder } from "./secret-folder.ts";@@ -84,3 +84,15 @@ export function managedTaskSkillLink(source: string, path: string, roots: stringreturn (rel !== "" && !rel.startsWith("..") && !isAbsolute(rel)) || relative(join(root, "imported-skills", match[2]), target) === "";});}++/** A folder a project installs and can install again, never carried: node_modules, and a Python virtual environment (any+ * folder holding pyvenv.cfg, as `python -m venv`, virtualenv, uv and Poetry make them), whose bin/python links to the+ * Python it was made with, outside the workspace. Either may itself be a link. */+export function installedDependencyFolder(source: string, name: string, stat: Stats): boolean {+ if (!stat.isDirectory() && !stat.isSymbolicLink()) return false;+ if (name === "node_modules") return true;+ try { return lstatSync(join(source, "pyvenv.cfg")).isFile(); } catch { return false; }+}++/** Python's bytecode cache, written again the next time the code runs. */+export const bytecodeCache = (name: string, stat: Stats): boolean => name === "__pycache__" && (stat.isDirectory() || stat.isSymbolicLink());diff --git a/server/workspace-backup.test.ts b/server/workspace-backup.test.tsindex 93fb1da4b1..4b0290f9da 100644--- a/server/workspace-backup.test.ts+++ b/server/workspace-backup.test.ts@@ -713,7 +713,7 @@ describe("encrypted full workspace backups", () => {mkdirSync(installed, { recursive: true });writeFileSync(join(installed, "index.js"), "module.exports = 1");const exported = await createWorkspaceBackup(root, { password: PASSWORD });- expect(exported.summary.warnings.some((warning) => warning.startsWith("2 installed dependency folder(s) (node_modules)"))).toBe(true);+ expect(exported.summary.warnings.some((warning) => warning.startsWith("2 installed dependency folder(s) (node_modules or Python virtual environments)"))).toBe(true);expect(exported.summary.warnings.some((warning) => warning.startsWith("1 symbolic link(s) in conversation work folders"))).toBe(true);const target = directory();const staged = await stageWorkspaceBackup(target, exported.path, { password: PASSWORD });@@ -726,6 +726,47 @@ describe("encrypted full workspace backups", () => {await expect(createWorkspaceBackup(root, { password: PASSWORD })).rejects.toThrow(/outside the workspace/);});+ it("leaves out Python virtual environments, whose python links outside, and bytecode caches; other outside links still stop it", async () => {+ const root = directory();+ const outside = directory();+ writeFileSync(join(outside, "python3"), "#!/bin/sh");+ const bot = join(root, "workspaces", "bot");+ const venv = join(bot, ".venv");+ mkdirSync(join(venv, "bin"), { recursive: true });+ mkdirSync(join(venv, "lib", "site-packages", "requests"), { recursive: true });+ writeFileSync(join(venv, "pyvenv.cfg"), `home = ${outside}\n`);+ writeFileSync(join(venv, "lib", "site-packages", "requests", "__init__.py"), "");+ symlinkSync(join(outside, "python3"), join(venv, "bin", "python"));+ // Any name: a venv is known by its pyvenv.cfg, here one a conversation made, reached through a link.+ const made = join(outside, "made-env");+ mkdirSync(join(made, "bin"), { recursive: true });+ writeFileSync(join(made, "pyvenv.cfg"), "home = /usr/bin\n");+ symlinkSync(join(outside, "python3"), join(made, "bin", "python"));+ const work = join(root, "task-workspaces", "bot", "thread");+ mkdirSync(work, { recursive: true });+ symlinkSync(made, join(work, "env"), process.platform === "win32" ? "junction" : "dir");+ mkdirSync(join(bot, "app", "__pycache__"), { recursive: true });+ writeFileSync(join(bot, "app", "main.py"), "print(1)");+ writeFileSync(join(bot, "app", "__pycache__", "main.cpython-312.pyc"), "bytes");+ // A folder named like a venv but with no pyvenv.cfg is the person's own: it goes.+ mkdirSync(join(bot, "venv"), { recursive: true });+ writeFileSync(join(bot, "venv", "notes.txt"), "mine");++ const exported = await createWorkspaceBackup(root, { password: PASSWORD });+ expect(exported.summary.warnings.some((warning) => warning.startsWith("2 installed dependency folder(s) (node_modules or Python virtual environments)"))).toBe(true);+ const target = directory();+ const staged = await stageWorkspaceBackup(target, exported.path, { password: PASSWORD });+ const data = join(target, ".backups", staged.id, "staged", "data");+ expect(readFileSync(join(data, "workspaces", "bot", "app", "main.py"), "utf8")).toBe("print(1)");+ expect(readFileSync(join(data, "workspaces", "bot", "venv", "notes.txt"), "utf8")).toBe("mine");+ expect(existsSync(join(data, "workspaces", "bot", ".venv"))).toBe(false);+ expect(existsSync(join(data, "task-workspaces", "bot", "thread", "env"))).toBe(false);+ expect(existsSync(join(data, "workspaces", "bot", "app", "__pycache__"))).toBe(false);++ symlinkSync(join(outside, "python3"), join(bot, "venv", "python"));+ await expect(createWorkspaceBackup(root, { password: PASSWORD })).rejects.toThrow("Cannot back up symbolic link outside the workspace: workspaces/bot/venv/python");+ });+it("counts the app's skill links in work folders as managed, not as outside data", async () => {const root = directory();const outside = directory();diff --git a/server/workspace-backup.ts b/server/workspace-backup.tsindex 8be163a6d8..574f1c0474 100644--- a/server/workspace-backup.ts+++ b/server/workspace-backup.ts@@ -15,7 +15,7 @@ import { userErrorFields, userErrorThrown } from "./user-errors.ts";import { WORKSPACE_BACKUP_CLIENT_KEYS } from "../shared/workspace-backup-client.ts";import { INSTRUCTION_FILE, restorePaths, validRelative } from "./workspace-backup-paths.ts";import { cjkSearchText } from "./cjk-search.ts";-import { dataEngineSpillPath, ephemeralWorkspaceTokenPath, excludedWorkspaceAuthPath, managedTaskSkillLink, portableWorkspaceConfig, redownloadedOrgLibraryPath, restoredWorkspaceConfig } from "./workspace-backup-policy.ts";+import { bytecodeCache, dataEngineSpillPath, ephemeralWorkspaceTokenPath, excludedWorkspaceAuthPath, installedDependencyFolder, managedTaskSkillLink, portableWorkspaceConfig, redownloadedOrgLibraryPath, restoredWorkspaceConfig } from "./workspace-backup-policy.ts";import { omittedAsTeamPrivate, routinesWithoutTeamPrivate, stripTeamPrivate, TEAM_PRIVATE_NOTE, teamPrivateAttachments, teamPrivateThreads, withoutTeamPrivate } from "./workspace-backup-team-private.ts";import { flushLaterSaves } from "./save-batch.ts";import { DATA_TABLES, foldLegacySidecars, LEGACY_SIDECARS, LIVE_DATABASE_SIDECARS, SNAPSHOT_DATABASES, snapshotDatabases, tooLargeError } from "./workspace-backup-databases.ts";@@ -68,7 +68,7 @@ const EXCLUSION_NOTES = ["Device pairing, server identity, live leases and runtime files (existing destination identities are preserved).","Saved credentials, provider and MCP connections, managed provider login homes and browser login profiles are not transferred. Destination connections are preserved; reconnect on a new device.","Downloaded tools and caches (they can be installed again), and recorded browser step screenshots (export them from a conversation to keep them).",- "Installed dependency folders (node_modules) and symbolic links inside conversation work folders; reinstall or recreate them in the project.",+ "Installed dependency folders (node_modules, Python virtual environments), Python bytecode caches and symbolic links inside conversation work folders; reinstall or recreate them in the project.","External project folders, CLI login homes, browser session homes, OS keychains, companion devices and other servers.","VM/container disk layers and remote cloud data; durable files inside this workspace are included.","Waits bots set up: they end within a week, so ask the bot again after restoring.",@@ -351,10 +351,11 @@ export async function createWorkspaceBackupSnapshot(dataDir: string, options: Crif (!validRelative(path)) throw new Error(`Cannot back up a name that cannot be restored on every supported platform, such as one with ":" or "\\", a trailing dot or space, or a Windows device name like CON or AUX; rename it first: ${path}`);let source = join(directory, name);let stat = lstatSync(source);- if (name === "node_modules" && (stat.isDirectory() || stat.isSymbolicLink())) {+ if (installedDependencyFolder(source, name, stat)) {skippedDependencies++;continue;}+ if (bytecodeCache(name, stat)) continue;// A link a bot made in a conversation's folder, or in Team files (which a bot may be let to write), is never// followed or carried, and never stops the backup.if (stat.isSymbolicLink() && path.startsWith("team-files/")) {@@ -425,7 +426,7 @@ export async function createWorkspaceBackupSnapshot(dataDir: string, options: Cr};walk(root);if (skippedLinks) warnings.push(`${skippedLinks} managed skill discovery link(s) were omitted and are recreated by the app.`);- if (skippedDependencies) warnings.push(`${skippedDependencies} installed dependency folder(s) (node_modules) were omitted; reinstall them in the project after restoring.`);+ if (skippedDependencies) warnings.push(`${skippedDependencies} installed dependency folder(s) (node_modules or Python virtual environments) were omitted; reinstall them in the project after restoring.`);if (copiedLinks) warnings.push(`${copiedLinks} symbolic link(s) inside a workspace were saved as copies of what they point to; restoring creates regular files and folders there.`);if (skippedWorkLinks) warnings.push(`${skippedWorkLinks} symbolic link(s) in conversation work folders were omitted; what they point to is outside this backup.`);const summary: WorkspaceBackupSummary = {