Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
// Encrypted filesystem snapshot: quiesce writers before create/commit; restore runs before config/store load.
import { createCipheriv, createDecipheriv, createHash, randomBytes, randomUUID, scrypt } from "node:crypto";
import {
chmodSync, closeSync, constants, cpSync, createReadStream, createWriteStream, existsSync,
fstatSync, fsyncSync, lstatSync, mkdirSync, openSync, readFileSync, readdirSync,
readSync, realpathSync, renameSync, rmSync, writeFileSync, writeSync,
} from "node:fs";
import { join, parse, posix, resolve, win32 } from "node:path";
import { homedir } from "node:os";
import { DatabaseSync } from "node:sqlite";
import { pipeline } from "node:stream/promises";
import { Worker } from "node:worker_threads";
import { writeFileAtomic } from "./atomic.ts";
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 { browserProfileFolder, bytecodeCache, carriedTwice, dataEngineSpillPath, dataRelativePath, dataRelativePaths, examplePaths, ephemeralWorkspaceTokenPath, excludedWorkspaceAuthPath, installedDependencyFolder, managedTaskSkillLink, pathUnder, portableWorkspaceConfig, redownloadedOrgLibraryPath, restoredWorkspaceConfig, topLevelLinksOut, type FollowedTopLevelLink } 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";
import type { WorkspaceBackupClientState, WorkspaceBackupPrivateMetadata, WorkspaceBackupSummary, WorkspaceLeftBehind } from "../shared/workspace-backup.ts";
export type { WorkspaceBackupSummary, WorkspaceBackupPrivateMetadata } from "../shared/workspace-backup.ts";
export const MAX_WORKSPACE_BACKUP_BYTES = 10 * 1024 ** 3;
export const MAX_WORKSPACE_BACKUP_FILES = 100_000;
export const MAX_WORKSPACE_BACKUP_UPLOAD_BYTES = MAX_WORKSPACE_BACKUP_BYTES + 256 * 1024 ** 2;
const MAX_METADATA_BYTES = 16 * 1024 ** 2;
const MAGIC = Buffer.from("OMB-WORKSPACE-1\n");
const HEADER_BYTES = MAGIC.length + 16 + 12;
const TAG_BYTES = 16;
const ID = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
const WAITS_FILE = "waits.json";
const EXCLUDED = new Set([
".backups", "tools", "browser", "agent-browser-profiles", "cache", ".cache", "tmp", ".tmp", "dist-native", "tunnel-runtime", "evidence",
".openmausbot-server-child", "environment-id", "sessions.json", "tunnel-account.json",
"team-computers.json",
// Slack messages a Cloud took from the Admin (team-slack.ts): this machine's own, of no use anywhere else.
"team-slack-messages.json",
"openmausbot-server.lease", "box-create-requests.lock", "messages.db-wal", "messages.db-shm",
"runtime.db-wal", "runtime.db-shm",
"decider-log",
"cloud-owner.json",
"sync",
// "Also use my Claude Code skills": a link to the person's own ~/.claude/skills, made again on each turn.
"claude-user-skills",
// The bot box's live sockets and seccomp filter (bot-box/box.ts), made again each time the server starts.
"box",
// The processes this machine's servers started (engine-records.ts), stopped by its next start.
"engine-runs",
// Deleted and idle conversations' dependency trees on their way out, what the orphan sweep has seen on this disk and
// what the idle reclaim shed (host/conversation-folder-cleanup.ts, host/orphan-sweep.ts, host/idle-reclaim.ts).
".removing", "orphan-sweep.json", "idle-reclaim.json",
// A Team Cloud's engine homes (engine-homes/layout.ts): CLI login homes, each audience's own transcripts and the held
// folder, which no admin reads.
"engine-homes",
WAITS_FILE,
// The data folder's format marker (data-format.ts) describes the folder it is in. Carried into a server older than the
// one that wrote it, it would stop that server on every start; the destination keeps its own.
"data-format.json",
// The doctor's copies of what it repaired, and the version it last ran for (doctor/backup.ts, doctor/start.ts): this
// folder's own history, and copies of files a backup already holds.
"doctor-backups", "doctor-state.json",
// A Cloud server's database copies for its own backup of the volume (cloud-backup-stage.ts), removed when it is packed.
"backup-stage",
]);
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, Python virtual environments), Python bytecode caches, browser profile folders, symbolic links inside conversation work folders, and links in bot folders to nothing or to outside this backup; 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.",
];
const RESTORE_WARNING = "Restoring pauses routines, webhooks and calendar calls, ends every wait, and does not resume pending delegations, queued chat messages or queued runs. Definitions and historical receipts are retained; review before enabling them again.";
type Entry = { path: string; type: "file" | "directory"; size: number; mode: number; sha256?: string };
interface Manifest extends WorkspaceBackupPrivateMetadata {
sourceDataDir: string;
entries: Entry[];
}
interface RestoreJournal {
id: string;
phase: "applying" | "committed" | "rolled-back";
existing: string[];
incoming: string[];
}
export interface CreateWorkspaceBackupOptions {
password: string;
clientState?: WorkspaceBackupClientState;
appVersion?: string;
}
type CreatedWorkspaceBackup = { id: string; path: string; summary: WorkspaceBackupSummary };
export interface WorkspaceRestoreResult {
restored: boolean;
rolledBack?: boolean;
id?: string;
safetyCopyPath?: string;
summary?: WorkspaceBackupSummary;
leftBehind?: WorkspaceLeftBehind;
clientState?: WorkspaceBackupClientState;
}
/** `pathsRewritten`: the source data folder was rewritten in text too, by the restore or by repairMovedWorkspacePaths. */
export type LastWorkspaceRestore = WorkspaceRestoreResult & { restored: true; id: string; pathsRewritten?: true };
function excluded(name: string): boolean {
return EXCLUDED.has(name) || excludedWorkspaceAuthPath(name) || name.startsWith("openmausbot-server.lease.") || name.startsWith("box-create-requests.lock.") || /^perm-[A-Za-z0-9_-]+\.sock$/.test(name);
}
function forbiddenArchivePath(path: string): boolean {
const folded = path.toLowerCase();
return excluded(folded.split("/")[0]) || excludedWorkspaceAuthPath(folded) ||
(path !== folded && ["config.json", "webhooks.json"].includes(folded));
}
function preserved(name: string): boolean {
// An existing WAL/SHM belongs to the old database, and existing waits to the old chats: neither may accompany restored ones.
const folded = name.toLowerCase();
return excluded(folded) && !LIVE_DATABASE_SIDECARS.has(folded) && folded !== WAITS_FILE;
}
function keptInPlace(name: string): boolean {
return preserved(name) || name.toLowerCase() === "sessions.json.open";
}
function folder(path: string): void {
mkdirSync(path, { recursive: true, mode: 0o700 });
const stat = lstatSync(path);
if (!stat.isDirectory() || stat.isSymbolicLink()) throw new Error("The backup directory must be a real directory, not a symbolic link.");
chmodSync(path, 0o700);
}
function backupRoot(dataDir: string): string {
const root = resolve(dataDir);
if (root === parse(root).root) throw new Error("A filesystem root cannot be a workspace backup target.");
const stat = lstatSync(root);
if (!stat.isDirectory() || stat.isSymbolicLink()) throw new Error("The workspace data directory must not be a symbolic link.");
const path = join(root, ".backups");
folder(path);
return path;
}
function jobPath(dataDir: string, id: string): string {
if (!ID.test(id)) throw new Error("Invalid workspace backup identifier.");
const path = join(backupRoot(dataDir), id);
if (entryExists(path)) {
const stat = lstatSync(path);
if (!stat.isDirectory() || stat.isSymbolicLink()) throw new Error("Unsafe workspace backup job directory.");
}
return path;
}
function newJob(dataDir: string, id: string = randomUUID()): { id: string; directory: string } {
const directory = jobPath(dataDir, id);
mkdirSync(directory, { mode: 0o700 });
return { id, directory };
}
function record(value: unknown): value is Record<string, unknown> {
return value !== null && typeof value === "object" && !Array.isArray(value);
}
function entryExists(path: string): boolean {
try { lstatSync(path); return true; }
catch (error) { if ((error as NodeJS.ErrnoException).code === "ENOENT") return false; throw error; }
}
function privateJson(path: string): unknown {
const fd = openSync(path, constants.O_RDONLY | constants.O_NOFOLLOW);
try {
const stat = fstatSync(fd);
if (!stat.isFile() || stat.size > MAX_METADATA_BYTES) throw new Error("Workspace backup metadata is too large or is not a regular file.");
return JSON.parse(readFileSync(fd, "utf8"));
} finally { closeSync(fd); }
}
function writeJson(path: string, value: unknown): void {
const text = JSON.stringify(value);
if (Buffer.byteLength(text) > MAX_METADATA_BYTES) throw new Error("Workspace backup metadata exceeds the size limit.");
writeFileAtomic(path, text, { mode: 0o600 });
}
function privateMetadata(manifest: Manifest): WorkspaceBackupPrivateMetadata {
return { summary: manifest.summary, clientState: manifest.clientState };
}
const HOME_FIELDS = new Set(["HOME", "USERPROFILE", "XDG_CONFIG_HOME", "XDG_DATA_HOME", "LOCALAPPDATA"]);
function assertLocalAuthOutsideSnapshot(dataDir: string, followed: readonly FollowedTopLevelLink[] = []): void {
const configPath = join(dataDir, "config.json");
const config = existsSync(configPath) ? privateJson(configPath) : {};
const instances = record(config) && record(config.instances) ? config.instances : {};
const fields = ["HOME", "USERPROFILE", "CLAUDE_CONFIG_DIR", "CODEX_HOME", "GROK_HOME", "KIMI_CODE_HOME", "HERMES_HOME", "FACTORY_HOME_OVERRIDE", "XDG_CONFIG_HOME", "XDG_DATA_HOME", "LOCALAPPDATA"];
const check = (value: unknown, field: string, home: string) => {
if (typeof value !== "string" || !value.trim()) return;
const text = value.trim();
const expanded = text.startsWith("~/") || text.startsWith("~\\") ? join(home, text.slice(2)) : text;
const resolved = resolve(expanded);
const exists = entryExists(resolved);
// A followed link into a provider's own login folder carries its sign-in; one into a home folder need not.
const carried = (found: string, path = found.replaceAll("\\", "/")) => !path || !(excluded(path.split("/")[0]) || excludedWorkspaceAuthPath(path));
if (dataRelativePaths(exists ? realpathSync(dataDir) : resolve(dataDir), followed, exists ? realpathSync(resolved) : resolved, !HOME_FIELDS.has(field)).some((found) => carried(found))) throw new Error(`Provider authentication storage (${field}) is inside backed-up workspace files. Move that login storage into the excluded providers folder or outside this workspace before backing up or restoring.`);
};
const environments = [process.env, ...Object.values(instances).filter(record).map((instance) => ({ ...process.env, ...(record(instance.environment) ? instance.environment : {}) }))];
for (const environment of environments) {
const home = String(environment.HOME || environment.USERPROFILE || homedir());
for (const field of fields) check(environment[field], field, home);
}
for (const instance of Object.values(instances)) {
if (!record(instance) || instance.driver !== "claudeAgent" || !record(instance.config)) continue;
const environment = { ...process.env, ...(record(instance.environment) ? instance.environment : {}) };
check(instance.config.configDir, "config.configDir", String(environment.HOME || environment.USERPROFILE || homedir()));
}
}
function passwordKey(password: string, salt: Buffer): Promise<Buffer> {
if (typeof password !== "string" || password.length < 12 || Buffer.byteLength(password) > 1024) {
throw new Error("Use a backup password of at least 12 characters (at most 1024 bytes).");
}
return new Promise((resolveKey, reject) => {
scrypt(password, salt, 32, { N: 131_072, r: 8, p: 1, maxmem: 256 * 1024 ** 2 }, (error, key) => {
if (error) reject(error); else resolveKey(key);
});
});
}
// No link following, and a synchronous copy so this server cannot interleave a mutation.
function copyRegular(source: string, destination: string): { size: number; mode: number; sha256: string } {
const input = openSync(source, constants.O_RDONLY | constants.O_NOFOLLOW);
let output: number | undefined;
try {
const before = fstatSync(input);
if (!before.isFile() || before.size > MAX_WORKSPACE_BACKUP_BYTES) throw new Error("A workspace file is not regular or exceeds the 10 GB backup limit.");
const mode = before.mode & 0o100 ? 0o700 : 0o600;
output = openSync(destination, "wx", mode);
const chunk = Buffer.allocUnsafe(1024 * 1024);
const hash = createHash("sha256");
let size = 0;
for (;;) {
const count = readSync(input, chunk, 0, chunk.length, null);
if (!count) break;
size += count;
if (size > before.size) throw new Error("A workspace file changed during backup. Stop its writer and retry.");
hash.update(chunk.subarray(0, count));
let offset = 0;
while (offset < count) offset += writeSync(output, chunk, offset, count - offset);
}
const after = fstatSync(input);
const current = lstatSync(source);
if (size !== before.size || after.mtimeMs !== before.mtimeMs || after.ctimeMs !== before.ctimeMs ||
current.ino !== before.ino || current.dev !== before.dev || current.isSymbolicLink()) {
throw new Error("A workspace file changed during backup. Stop its writer and retry.");
}
fsyncSync(output);
return { size, mode, sha256: hash.digest("hex") };
} finally {
closeSync(input);
if (output !== undefined) closeSync(output);
}
}
function hashFile(path: string): string {
const fd = openSync(path, constants.O_RDONLY | constants.O_NOFOLLOW);
try {
if (!fstatSync(fd).isFile()) throw new Error("The workspace archive contains an unsafe file.");
const hash = createHash("sha256");
const chunk = Buffer.allocUnsafe(1024 * 1024);
for (;;) {
const count = readSync(fd, chunk, 0, chunk.length, null);
if (!count) return hash.digest("hex");
hash.update(chunk.subarray(0, count));
}
} finally { closeSync(fd); }
}
function countJsonArray(path: string): number {
if (!existsSync(path)) return 0;
const value = privateJson(path);
if (!Array.isArray(value)) throw new Error("A workspace roster is not a valid JSON array.");
const ids = new Set<string>();
for (const item of value) {
if (!record(item) || typeof item.id !== "string" || !/^[A-Za-z0-9_-]{1,128}$/.test(item.id) || ids.has(item.id)) throw new Error("A workspace roster contains an invalid or duplicate identifier.");
ids.add(item.id);
if (item.threadId !== undefined && (typeof item.threadId !== "string" || !/^[A-Za-z0-9_-]{1,128}$/.test(item.threadId))) throw new Error("A workspace roster contains an invalid thread identifier.");
if (item.tasks !== undefined && (!Array.isArray(item.tasks) || !item.tasks.every((task) => record(task) && typeof task.threadId === "string" && /^[A-Za-z0-9_-]{1,128}$/.test(task.threadId)))) throw new Error("A workspace roster contains invalid task records.");
}
return value.length;
}
function databaseCounts(path: string): { threads: number; messages: number } {
if (!existsSync(path)) return { threads: 0, messages: 0 };
const db = new DatabaseSync(path, { readOnly: true });
try {
const check = db.prepare("PRAGMA quick_check").all();
if (check.length !== 1 || Object.values(check[0])[0] !== "ok") throw new Error("The message database failed its integrity check.");
const tables = db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name IN ('messages', 'thread_state')").all();
if (tables.length !== 2) throw new Error("The backup does not contain a supported message database.");
const counts = db.prepare("SELECT COUNT(*) AS messages, COUNT(DISTINCT thread_id) AS threads FROM messages").get()!;
return { threads: Number(counts.threads), messages: Number(counts.messages) };
} finally { db.close(); }
}
/** The existing maintenance gate owns exclusivity until this worker has exited.
* Keep synchronous file validation/fsync intact, but off the request thread. */
export async function createWorkspaceBackup(dataDir: string, options: CreateWorkspaceBackupOptions): Promise<CreatedWorkspaceBackup> {
flushLaterSaves();
const source = new URL("./workspace-backup.worker.ts", import.meta.url);
// Assign ownership before the worker can create staging, even if it crashes before reporting back.
const jobId = randomUUID();
let worker: Worker | undefined;
let completed = false;
try {
worker = new Worker(existsSync(source) ? source : new URL("./workspace-backup.worker.js", import.meta.url), {
workerData: { dataDir, options, jobId }, execArgv: [],
});
const result = await new Promise<CreatedWorkspaceBackup>((resolveBackup, reject) => {
const failed = () => reject(new Error("The backup worker stopped before completing. Try again."));
worker!.once("message", (reply: { result?: CreatedWorkspaceBackup; error?: string } & ReturnType<typeof userErrorFields>) => {
if (reply.result) resolveBackup(reply.result);
else if (reply.error) reject(Object.assign(new Error(reply.error), userErrorFields(reply)));
else failed();
});
worker!.once("error", failed);
worker!.once("exit", failed);
});
completed = true;
return result;
} finally {
await worker?.terminate();
if (!completed && entryExists(join(dataDir, ".backups", jobId))) removeWorkspaceBackupJob(dataDir, jobId);
}
}
export async function createWorkspaceBackupSnapshot(dataDir: string, options: CreateWorkspaceBackupOptions, jobId?: string): Promise<CreatedWorkspaceBackup> {
if (Object.hasOwn(options, "credentials")) throw new Error("Workspace backups do not transfer credentials.");
const linksOut = topLevelLinksOut(realpathSync(dataDir)), walked = linksOut.filter((link) => !excluded(link.name) && !LEGACY_SIDECARS.has(link.name));
assertLocalAuthOutsideSnapshot(dataDir, linksOut);
const salt = randomBytes(16);
const key = await passwordKey(options.password, salt);
const job = newJob(dataDir, jobId);
const snapshot = join(job.directory, "snapshot");
folder(join(snapshot, "data"));
const entries: Entry[] = [];
const foldedPaths = new Map<string, string>();
const warnings = ["Stop external editors and managed desktops before exporting; files written outside this server cannot be frozen by the backup gate.", "Conversation text and user files are not redacted and may contain secrets you pasted. Keep the encrypted backup private.", RESTORE_WARNING];
let bytes = 0;
let dataBytes = 0;
let skippedLinks = 0;
let skippedDependencies = 0;
let skippedWorkLinks = 0;
let copiedLinks = 0;
const leftLinks: string[] = [], leftProfiles: string[] = [], moved: FollowedTopLevelLink[] = [];
try {
const root = realpathSync(dataDir);
await snapshotDatabases(root, snapshot);
const teamPrivate = teamPrivateThreads({ starters: join(root, "thread-starters.json"), groups: join(root, "groups.json"), bots: join(root, "bots.json") });
const privacy = teamPrivate && { ...teamPrivate, attachments: teamPrivateAttachments(root, join(snapshot, SNAPSHOT_DATABASES["messages.db"]!), teamPrivate) };
if (privacy) {
stripTeamPrivate({ messages: join(snapshot, SNAPSHOT_DATABASES["messages.db"]!), runtime: join(snapshot, SNAPSHOT_DATABASES["runtime.db"]!) }, privacy);
warnings.push(TEAM_PRIVATE_NOTE);
}
const walk = (directory: string, prefix = "", chain: string[] = [root]) => {
for (const name of readdirSync(directory).sort()) {
if (!prefix && (excluded(name) || LEGACY_SIDECARS.has(name))) continue;
const path = prefix ? `${prefix}/${name}` : name;
if (excludedWorkspaceAuthPath(path) || ephemeralWorkspaceTokenPath(path) || redownloadedOrgLibraryPath(path) || dataEngineSpillPath(path)) continue;
if (privacy && omittedAsTeamPrivate(path, privacy)) continue;
// Reject noncanonical spellings: a case-sensitive host must not export auth paths active on Windows/Mac.
if (forbiddenArchivePath(path)) throw new Error("A workspace filename conflicts with a protected authentication or runtime path.");
if (!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 (installedDependencyFolder(source, name, stat)) {
skippedDependencies++;
continue;
}
if (bytecodeCache(name, stat)) continue;
if (prefix && browserProfileFolder(source, stat)) { leftProfiles.push(path); 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/")) {
skippedWorkLinks++;
continue;
}
if (stat.isSymbolicLink() && path.startsWith("task-workspaces/")) {
if (managedTaskSkillLink(source, path, [root, resolve(dataDir)])) skippedLinks++; else skippedWorkLinks++;
continue;
}
if (stat.isSymbolicLink()) {
let target: string;
// In a bot's folder, a link to nothing (a browser's SingletonLock) or to outside the data folder is left out and
// named in the backup's note; anywhere else it still stops the backup.
const leave = path.startsWith("workspaces/");
try { target = realpathSync(source); } catch { if (leave) { leftLinks.push(path); continue; } throw new Error(`Cannot back up dangling symbolic link: ${path}`); }
const rel = dataRelativePath(root, prefix ? walked : [], target);
const followed = rel === undefined && !prefix;
if (!followed) {
if (rel === undefined) { if (leave) { leftLinks.push(path); continue; } throw new Error(`Cannot back up symbolic link outside the workspace: ${path}`); }
const portableRel = rel.replaceAll("\\", "/");
// Only the app's disposable native-discovery links may be omitted; any other link is user data.
if (/^workspaces\/[^/]+\/\.(?:claude|agents|grok)\/skills\/[^/]+$/.test(path) &&
portableRel.startsWith(`workspaces/${path.split("/")[1]}/skills/`)) {
skippedLinks++;
continue;
}
const workspace = /^workspaces\/[^/]+\//.exec(path)?.[0];
if (!workspace || !portableRel.startsWith(workspace)) {
throw new Error(`Cannot back up user-created symbolic link; replace it with regular files first: ${path}`);
}
}
if (chain.some((folder) => pathUnder(target, folder) !== undefined)) {
throw userErrorThrown("serverError.backup.recursiveLink", { path });
}
if (followed && carriedTwice(moved, target)) throw new Error(`Cannot back up user-created symbolic link; replace it with regular files first: ${path}`);
source = target;
stat = lstatSync(source);
if (followed) moved.push({ name, target }); else copiedLinks++;
}
if (entries.length >= MAX_WORKSPACE_BACKUP_FILES) throw new Error("The workspace exceeds the 100,000-entry backup limit.");
const caseTwin = foldedPaths.get(path.toLowerCase());
if (caseTwin) throw new Error(`Cannot back up names that differ only by letter case, which collide on Windows and macOS; rename one first: ${caseTwin} and ${path}`);
foldedPaths.set(path.toLowerCase(), path);
const destination = join(snapshot, "data", path);
if (stat.isDirectory()) {
folder(destination);
entries.push({ path, type: "directory", size: 0, mode: 0o700 });
walk(source, path, [...chain, source]);
} else if (stat.isFile()) {
// Said before a copy that could take minutes: Data tables are the files a workspace grows past the limit with.
if (DATA_TABLES.test(path) && bytes + stat.size > MAX_WORKSPACE_BACKUP_BYTES) throw tooLargeError(bytes + stat.size, dataBytes + stat.size);
let copied: { size: number; mode: number; sha256: string };
if (path === "config.json" || path === "webhooks.json" || (privacy && (path === "bots.json" || path === "routines.json"))) {
const value = privateJson(source);
if (path === "config.json") writeJson(destination, portableWorkspaceConfig(value));
else if (path === "bots.json") writeJson(destination, withoutTeamPrivate(value, privacy!));
else if (path === "routines.json") writeJson(destination, routinesWithoutTeamPrivate(value, privateJson(join(root, "bots.json")), privacy!));
else {
if (!record(value)) throw new Error("Invalid webhook definitions in workspace backup.");
if (Array.isArray(value.webhooks)) for (const webhook of value.webhooks) if (record(webhook)) { delete webhook.secretHash; delete webhook.sealedSecret; }
if (Array.isArray(value.deleted)) for (const entry of value.deleted) if (record(entry) && record(entry.webhook)) delete entry.webhook.sealedSecret;
writeJson(destination, value);
}
copied = { size: lstatSync(destination).size, mode: 0o600, sha256: hashFile(destination) };
} else copied = copyRegular(SNAPSHOT_DATABASES[path] ? join(snapshot, SNAPSHOT_DATABASES[path]) : source, destination);
bytes += copied.size;
if (DATA_TABLES.test(path)) dataBytes += copied.size;
if (bytes > MAX_WORKSPACE_BACKUP_BYTES) throw tooLargeError(bytes, dataBytes);
entries.push({ path, type: "file", ...copied });
} else throw new Error(`Cannot back up special or live runtime file: ${path}`);
}
};
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 or Python virtual environments) were omitted; reinstall them in the project after restoring.`);
if (moved.length) warnings.push(`${moved.length} top-level item(s) of the data folder link to another place and were saved as copies of what they point to; restoring puts them back as regular folders and files: ${examplePaths(moved.map((link) => link.name))}.`);
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 (leftProfiles.length) warnings.push(`${leftProfiles.length} browser profile folder(s) in bot folders were left out with their sign-ins, cookies and caches; sign in again after restoring: ${examplePaths(leftProfiles)}.`);
if (leftLinks.length) warnings.push(`${leftLinks.length} symbolic link(s) in bot folders were left out because they point to nothing or outside this backup: ${examplePaths(leftLinks)}.`);
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 = {
format: "openmaus.workspace-backup", version: 1, id: job.id, createdAt: new Date().toISOString(),
appVersion: options.appVersion ?? "unknown", files: entries.filter((entry) => entry.type === "file").length,
directories: entries.filter((entry) => entry.type === "directory").length, bytes,
bots: countJsonArray(join(snapshot, "data", "bots.json")), groups: countJsonArray(join(snapshot, "data", "groups.json")),
...databaseCounts(join(snapshot, "data", "messages.db")),
exclusions: EXCLUSION_NOTES, warnings,
};
const manifest: Manifest = {
summary, sourceDataDir: resolve(dataDir), clientState: options.clientState ?? {}, entries,
};
writeJson(join(snapshot, "manifest.json"), manifest);
validateManifest(privateJson(join(snapshot, "manifest.json")));
const path = join(job.directory, "workspace.ombbackup");
const iv = randomBytes(12);
const header = Buffer.concat([MAGIC, salt, iv]);
const cipher = createCipheriv("aes-256-gcm", key, iv);
cipher.setAAD(header);
writeFileSync(path, header, { mode: 0o600, flag: "wx" });
const tar = await import("tar");
await pipeline(tar.c({ cwd: snapshot, portable: true, strict: true }, ["manifest.json", "data"]), cipher, createWriteStream(path, { flags: "a", mode: 0o600 }));
const fd = openSync(path, "a");
try { writeSync(fd, cipher.getAuthTag()); fsyncSync(fd); } finally { closeSync(fd); }
rmSync(snapshot, { recursive: true, force: true });
return { id: job.id, path, summary };
} catch (error) {
rmSync(job.directory, { recursive: true, force: true });
throw error;
} finally { key.fill(0); }
}
function validateManifest(value: unknown): Manifest {
if (!record(value) || !record(value.summary) || value.summary.format !== "openmaus.workspace-backup" || value.summary.version !== 1 ||
typeof value.summary.id !== "string" || !ID.test(value.summary.id) || typeof value.summary.createdAt !== "string" ||
!Number.isFinite(Date.parse(value.summary.createdAt)) || typeof value.summary.appVersion !== "string" ||
typeof value.sourceDataDir !== "string" || !(posix.isAbsolute(value.sourceDataDir) || win32.isAbsolute(value.sourceDataDir)) ||
value.sourceDataDir.length > 4096 || value.sourceDataDir.includes("\0") ||
value.sourceDataDir === posix.parse(value.sourceDataDir).root || value.sourceDataDir === win32.parse(value.sourceDataDir).root ||
value.sourceDataDir.replaceAll("\\", "/").split("/").some((part) => part === "." || part === "..") ||
Object.keys(value).some((key) => !["summary", "sourceDataDir", "clientState", "entries"].includes(key)) ||
Object.keys(value.summary).some((key) => !["format", "version", "id", "createdAt", "appVersion", "files", "directories", "bytes", "bots", "groups", "threads", "messages", "exclusions", "warnings"].includes(key)) || !record(value.clientState) ||
!Object.values(value.clientState).every((item) => typeof item === "string") || !Array.isArray(value.entries) ||
value.entries.length > MAX_WORKSPACE_BACKUP_FILES) throw new Error("Invalid or unsupported workspace backup metadata.");
if (Object.keys(value.clientState).some((key) => !WORKSPACE_BACKUP_CLIENT_KEYS.includes(key as typeof WORKSPACE_BACKUP_CLIENT_KEYS[number])) ||
Buffer.byteLength(JSON.stringify(value.clientState)) > 2 * 1024 ** 2) throw new Error("Invalid or oversized workspace client preferences.");
for (const key of ["files", "directories", "bytes", "bots", "groups", "threads", "messages"] as const) {
if (!Number.isSafeInteger(value.summary[key]) || (value.summary[key] as number) < 0) throw new Error("Invalid workspace backup summary.");
}
for (const key of ["exclusions", "warnings"] as const) {
if (!Array.isArray(value.summary[key]) || !value.summary[key].every((item) => typeof item === "string")) throw new Error("Invalid workspace backup summary.");
}
const names = new Map<string, string>();
let bytes = 0;
let files = 0;
for (const raw of value.entries) {
if (!record(raw) || typeof raw.path !== "string" || !validRelative(raw.path) || forbiddenArchivePath(raw.path) ||
Object.keys(raw).some((key) => !["path", "type", "size", "mode", "sha256"].includes(key)) ||
!["file", "directory"].includes(String(raw.type)) || !Number.isSafeInteger(raw.size) || (raw.size as number) < 0 ||
![0o600, 0o700].includes(raw.mode as number) || names.has(raw.path.toLowerCase())) throw new Error("Unsafe or duplicate workspace backup entry.");
names.set(raw.path.toLowerCase(), String(raw.type));
if (raw.type === "file") {
if (typeof raw.sha256 !== "string" || !/^[0-9a-f]{64}$/.test(raw.sha256)) throw new Error("A backup file has no valid checksum.");
bytes += raw.size as number;
files++;
} else if (raw.size !== 0) throw new Error("A backup directory has an invalid size.");
}
if (bytes > MAX_WORKSPACE_BACKUP_BYTES || bytes !== value.summary.bytes || files !== value.summary.files ||
names.size - files !== value.summary.directories) {
throw new Error("Workspace backup contents do not match their summary or exceed the limit.");
}
for (const path of names.keys()) {
const parts = path.split("/");
for (let i = 1; i < parts.length; i++) if (names.get(parts.slice(0, i).join("/")) !== "directory") throw new Error("A backup entry has an unsafe parent.");
}
return value as unknown as Manifest;
}
async function decryptArchive(inputPath: string, plaintext: string, password: string): Promise<void> {
const fd = openSync(inputPath, constants.O_RDONLY | constants.O_NOFOLLOW);
let key: Buffer | undefined;
try {
const stat = fstatSync(fd);
if (!stat.isFile() || stat.size < HEADER_BYTES + TAG_BYTES || stat.size > MAX_WORKSPACE_BACKUP_UPLOAD_BYTES) throw new Error("Invalid or oversized workspace backup file.");
const header = Buffer.alloc(HEADER_BYTES);
const tag = Buffer.alloc(TAG_BYTES);
if (readSync(fd, header, 0, header.length, 0) !== header.length || !header.subarray(0, MAGIC.length).equals(MAGIC) ||
readSync(fd, tag, 0, tag.length, stat.size - TAG_BYTES) !== tag.length) throw new Error("This is not a supported encrypted workspace backup.");
key = await passwordKey(password, header.subarray(MAGIC.length, MAGIC.length + 16));
const decipher = createDecipheriv("aes-256-gcm", key, header.subarray(MAGIC.length + 16));
decipher.setAAD(header);
decipher.setAuthTag(tag);
try {
await pipeline(createReadStream(inputPath, { fd, autoClose: false, start: HEADER_BYTES, end: stat.size - TAG_BYTES - 1 }), decipher,
createWriteStream(plaintext, { flags: "wx", mode: 0o600 }));
} catch {
throw new Error("The backup password is incorrect, or the backup file is damaged. Nothing was restored.");
}
} finally { key?.fill(0); closeSync(fd); }
}
async function inspectTar(path: string): Promise<Map<string, { type: string; size: number }>> {
const fd = openSync(path, "r");
const signature = Buffer.alloc(4);
try { readSync(fd, signature, 0, 4, 0); } finally { closeSync(fd); }
// Uncompressed tar only: reject gzip/zstd before tar can inflate an authenticated decompression bomb.
if ((signature[0] === 0x1f && signature[1] === 0x8b) || signature.equals(Buffer.from([0x28, 0xb5, 0x2f, 0xfd]))) {
throw new Error("Compressed payloads are not supported in this workspace backup version.");
}
const entries = new Map<string, { type: string; size: number }>();
const names = new Set<string>();
let bytes = 0;
let problem = "";
const tar = await import("tar");
await tar.t({ file: path, strict: true, brotli: false, zstd: false, onReadEntry(entry) {
const name = entry.type === "Directory" ? entry.path.replace(/\/$/, "") : entry.path;
const expectedRoot = name === "manifest.json" || name === "data" || name.startsWith("data/");
// ReadEntry normalizes backslashes on Windows; the raw header is checked too.
if (entry.header.path?.includes("\\") || !validRelative(name) || !expectedRoot || !["File", "Directory"].includes(entry.type) ||
names.has(name.toLowerCase()) || entries.size >= MAX_WORKSPACE_BACKUP_FILES + 2 ||
!Number.isSafeInteger(entry.size) || entry.size < 0 || (entry.type === "Directory" && entry.size !== 0)) {
problem ||= "The archive contains unsafe, duplicate or unsupported entries.";
return;
}
if (name === "manifest.json" && (entry.type !== "File" || entry.size > MAX_METADATA_BYTES)) problem ||= "Invalid backup metadata file.";
bytes += entry.size;
if (bytes > MAX_WORKSPACE_BACKUP_BYTES + MAX_METADATA_BYTES) problem ||= "The archive exceeds the extracted size limit.";
names.add(name.toLowerCase());
entries.set(name, { type: entry.type, size: entry.size });
} });
if (problem) throw new Error(problem);
if (entries.get("data")?.type !== "Directory" || entries.get("manifest.json")?.type !== "File") throw new Error("The archive has no workspace data or manifest.");
for (const name of entries.keys()) {
const parts = name.split("/");
for (let i = 1; i < parts.length; i++) if (entries.get(parts.slice(0, i).join("/"))?.type !== "Directory") throw new Error("An archive entry has an unsafe parent.");
}
return entries;
}
function validateStaged(directory: string, expected?: Map<string, { type: string; size: number }>): Manifest {
const manifest = validateManifest(privateJson(join(directory, "manifest.json")));
if (expected && expected.size !== manifest.entries.length + 2) throw new Error("Archive entries do not match the backup manifest.");
const declared = new Set(manifest.entries.map((entry) => entry.path));
const inspect = (root: string, prefix = "") => {
for (const name of readdirSync(root)) {
const path = prefix ? `${prefix}/${name}` : name;
if (!declared.has(path)) throw new Error("Staged workspace contains files absent from its manifest.");
const stat = lstatSync(join(root, name));
if (stat.isSymbolicLink()) throw new Error("Staged workspace contains a symbolic link.");
if (stat.isDirectory()) inspect(join(root, name), path);
}
};
inspect(join(directory, "data"));
for (const entry of manifest.entries) {
const path = join(directory, "data", entry.path);
const stat = lstatSync(path);
if (stat.isSymbolicLink() || (entry.type === "file" ? !stat.isFile() || stat.size !== entry.size : !stat.isDirectory())) {
throw new Error("Staged workspace files do not match the backup manifest.");
}
if (expected) {
const archive = expected.get(`data/${entry.path}`);
if (!archive || archive.type !== (entry.type === "file" ? "File" : "Directory") || archive.size !== entry.size) throw new Error("Archive contents do not match the backup manifest.");
}
if (entry.type === "file" && hashFile(path) !== entry.sha256) throw new Error("A workspace backup file failed its checksum.");
chmodSync(path, entry.mode);
}
const data = join(directory, "data");
if (countJsonArray(join(data, "bots.json")) !== manifest.summary.bots || countJsonArray(join(data, "groups.json")) !== manifest.summary.groups) throw new Error("The backup roster does not match its summary.");
if (existsSync(join(data, "config.json"))) {
const config = privateJson(join(data, "config.json"));
if (JSON.stringify(config) !== JSON.stringify(portableWorkspaceConfig(config))) throw new Error("Workspace backup contains non-portable connection settings.");
}
if (existsSync(join(data, "webhooks.json"))) {
const value = privateJson(join(data, "webhooks.json"));
const carries = (webhook: unknown, fields: string[]) => record(webhook) && fields.some((field) => Object.hasOwn(webhook, field));
if (!record(value) || Array.isArray(value.webhooks) && value.webhooks.some((webhook) => carries(webhook, ["secretHash", "sealedSecret"]))
|| Array.isArray(value.deleted) && value.deleted.some((entry) => record(entry) && carries(entry.webhook, ["sealedSecret"]))) throw new Error("Workspace backup contains webhook credentials.");
}
const counts = databaseCounts(join(data, "messages.db"));
if (counts.messages !== manifest.summary.messages || counts.threads !== manifest.summary.threads) throw new Error("The message database does not match the backup summary.");
return manifest;
}
export async function stageWorkspaceBackup(dataDir: string, archivePath: string, options: { password: string; currentAppVersion?: string }) {
const job = newJob(dataDir);
const plaintext = join(job.directory, "archive.tar");
try {
// Nothing is parsed until GCM final() authenticates the whole file.
await decryptArchive(archivePath, plaintext, options.password);
const expected = await inspectTar(plaintext);
const staged = join(job.directory, "staged");
folder(staged);
const tar = await import("tar");
await tar.x({ file: plaintext, cwd: staged, strict: true, preservePaths: false, umask: 0o077, noChmod: true, brotli: false, zstd: false });
const manifest = validateStaged(staged, expected);
const versions = [manifest.summary.appVersion, options.currentAppVersion ?? ""].map((version) => /^(\d+)\.(\d+)\.(\d+)(?:[-+].*)?$/.exec(version)?.slice(1).map(Number));
if (versions[0] && versions[1]) {
for (let i = 0; i < 3; i++) {
if (versions[0][i] > versions[1][i]) throw new Error("This backup was made by a newer OpenMausBot version. Update the app before restoring it.");
if (versions[0][i] < versions[1][i]) break;
}
}
rmSync(plaintext);
writeJson(join(job.directory, "ready.json"), { id: job.id, summary: manifest.summary });
return { id: job.id, summary: manifest.summary };
} catch (error) {
rmSync(job.directory, { recursive: true, force: true });
throw error;
}
}
export function readStagedWorkspaceBackup(dataDir: string, id: string): WorkspaceBackupPrivateMetadata & { id: string } {
const directory = jobPath(dataDir, id);
const ready = privateJson(join(directory, "ready.json"));
if (!record(ready) || ready.id !== id) throw new Error("This workspace backup is not ready to restore.");
const manifest = validateManifest(privateJson(join(directory, "staged", "manifest.json")));
return { id, ...privateMetadata(manifest) };
}
/** A staged backup's own data folder, and the folder it was made from. */
export function stagedWorkspaceData(dataDir: string, id: string): { data: string; sourceDataDir: string } {
readStagedWorkspaceBackup(dataDir, id);
const staged = join(jobPath(dataDir, id), "staged");
return { data: join(staged, "data"), sourceDataDir: validateManifest(privateJson(join(staged, "manifest.json"))).sourceDataDir };
}
export function commitPendingWorkspaceRestore(dataDir: string, id: string) {
const root = backupRoot(dataDir);
if (existsSync(join(root, "pending-restore.json"))) throw new Error("A workspace restore is already waiting for restart.");
if (existsSync(join(root, `safety-${id}`))) throw new Error("This restore was already attempted. Preview the backup again to create a fresh restore attempt.");
const metadata = readStagedWorkspaceBackup(dataDir, id);
const manifest = validateStaged(join(jobPath(dataDir, id), "staged"));
// Adapt before persisting a restart request: a broken legacy file must not trap later boots.
prepareRestore(dataDir, id, manifest);
writeJson(join(root, "pending-restore.json"), { id });
return { id, restartRequired: true as const, summary: metadata.summary };
}
export function readPendingWorkspaceRestoreMetadata(dataDir: string): (WorkspaceBackupPrivateMetadata & { id: string }) | null {
const root = backupRoot(dataDir);
const path = join(root, "pending-restore.json");
if (!existsSync(path)) return null;
const pending = privateJson(path);
if (!record(pending) || typeof pending.id !== "string") throw new Error("Invalid pending workspace restore.");
return readStagedWorkspaceBackup(dataDir, pending.id);
}
function readJournal(path: string): RestoreJournal {
const raw = privateJson(path);
if (!record(raw) || typeof raw.id !== "string" || !ID.test(raw.id) || !["applying", "committed", "rolled-back"].includes(String(raw.phase))) throw new Error("Invalid restore recovery journal. Original data was preserved; recovery needs attention.");
for (const key of ["existing", "incoming"] as const) {
if (!Array.isArray(raw[key]) || !raw[key].every((name) => typeof name === "string" && validRelative(name) && !name.includes("/") && !preserved(name)) || new Set(raw[key]).size !== raw[key].length) throw new Error("Invalid restore recovery paths.");
}
return raw as unknown as RestoreJournal;
}
function rollback(dataDir: string, journal: RestoreJournal): void {
const stage = join(jobPath(dataDir, journal.id), "apply", "data");
const saved = join(backupRoot(dataDir), `safety-${journal.id}`, "data");
// Existence on both sides resolves a crash between rename and journal I/O; new data is moved back, never deleted.
for (const name of journal.incoming) {
const live = join(dataDir, name);
const staged = join(stage, name);
const originalMoved = !journal.existing.includes(name) || entryExists(join(saved, name));
if (originalMoved && !entryExists(staged) && entryExists(live)) renameSync(live, staged);
}
for (const name of journal.existing) {
const original = join(saved, name);
if (!entryExists(original)) continue;
const live = join(dataDir, name);
if (entryExists(live)) throw new Error("Restore rollback found conflicting files. Both copies are preserved for recovery.");
renameSync(original, live);
}
const root = backupRoot(dataDir);
const previousReceipt = join(root, `safety-${journal.id}`, "previous-restore.json");
const last = join(root, "last-restore.json");
if (existsSync(previousReceipt)) writeJson(last, privateJson(previousReceipt));
else if (existsSync(last)) {
const value = privateJson(last);
if (record(value) && value.id === journal.id) rmSync(last);
}
}
function prepareRestore(dataDir: string, id: string, manifest: Manifest): string {
assertLocalAuthOutsideSnapshot(dataDir);
const job = jobPath(dataDir, id);
const prepared = join(job, "apply", "data");
if (existsSync(join(job, "apply"))) rmSync(join(job, "apply"), { recursive: true, force: true });
folder(prepared);
const installed = manifest.entries.filter((entry) => !ephemeralWorkspaceTokenPath(entry.path));
for (const entry of installed.filter((entry) => entry.type === "directory")) folder(join(prepared, entry.path));
for (const entry of installed) {
const destination = join(prepared, entry.path);
if (entry.type === "file") copyRegular(join(job, "staged", "data", entry.path), destination);
}
foldLegacySidecars(prepared);
const changeJson = (name: string, change: (value: unknown) => void) => {
const path = join(prepared, name);
if (!existsSync(path)) return;
const value = privateJson(path);
change(value);
writeJson(path, value);
};
const paths = restorePaths(manifest.sourceDataDir, resolve(dataDir));
changeJson("bots.json", (value) => {
if (!Array.isArray(value)) throw new Error("Invalid bot definitions in workspace backup.");
for (const bot of value) if (record(bot)) {
paths.definitions(bot, String(bot.id));
if (typeof bot.soul === "string") bot.soul = paths.text(bot.soul);
delete bot.voice;
}
});
changeJson("groups.json", (value) => {
paths.definitions(value);
if (Array.isArray(value)) for (const group of value) if (record(group) && typeof group.bulletin === "string") group.bulletin = paths.text(group.bulletin);
});
for (const name of ["config.json", "routines.json", "calendar-calls.json"]) changeJson(name, (value) => paths.definitions(value));
for (const entry of installed) {
if (entry.type !== "file" || entry.size > MAX_METADATA_BYTES || !INSTRUCTION_FILE.test(entry.path)) continue;
const before = readFileSync(join(prepared, entry.path), "utf8");
const after = paths.text(before);
if (after !== before) writeFileAtomic(join(prepared, entry.path), after, { mode: entry.mode });
}
// Never install source connection settings.
const oldConfig = join(dataDir, "config.json");
const newConfig = join(prepared, "config.json");
if (existsSync(oldConfig) || existsSync(newConfig)) {
writeJson(newConfig, restoredWorkspaceConfig(existsSync(newConfig) ? privateJson(newConfig) : {}, existsSync(oldConfig) ? privateJson(oldConfig) : {}));
}
const browserHomes = ["vm-home"];
const vmHomes = join(dataDir, "vm-homes");
if (entryExists(vmHomes)) {
if (!lstatSync(vmHomes).isDirectory()) throw new Error("Stop managed desktops and repair the VM home directory before restoring.");
for (const name of readdirSync(vmHomes)) if (validRelative(name)) browserHomes.push(`vm-homes/${name}`);
}
for (const home of browserHomes) {
const homePath = join(dataDir, home);
if (!entryExists(homePath)) continue;
if (!lstatSync(homePath).isDirectory()) throw new Error("Stop managed desktops and repair the VM home directory before restoring.");
const source = join(dataDir, home, ".browser-profiles");
if (!entryExists(source)) continue;
folder(join(prepared, home));
cpSync(source, join(prepared, home, ".browser-profiles"), { recursive: true, dereference: false, verbatimSymlinks: true, errorOnExist: true, force: false });
}
changeJson("routines.json", (value) => {
if (!record(value)) throw new Error("Invalid routine definitions in workspace backup.");
if (Array.isArray(value.routines)) for (const routine of value.routines) if (record(routine)) {
routine.enabled = false;
// A restore is a change to the routine: nothing may read its old stamp as the moment something else switched it off.
routine.updatedAt = Math.max(Date.now(), (typeof routine.updatedAt === "number" ? routine.updatedAt : 0) + 1);
if (typeof routine.prompt === "string") routine.prompt = paths.text(routine.prompt);
}
if (Array.isArray(value.runs)) for (const run of value.runs) {
if (record(run) && ["queued", "running", "waiting"].includes(String(run.status))) {
run.status = "failed";
run.error = "This queued or active run was not resumed after workspace restore.";
run.finishedAt = Date.now();
if (run.target === "room-goal") run.goalStatus = "failed";
}
}
});
changeJson("webhooks.json", (value) => {
if (!record(value)) throw new Error("Invalid webhook definitions in workspace backup.");
const destination = existsSync(join(dataDir, "webhooks.json")) ? privateJson(join(dataDir, "webhooks.json")) : {};
const oldHooks = record(destination) && Array.isArray(destination.webhooks) ? destination.webhooks : [];
if (Array.isArray(value.webhooks)) for (const webhook of value.webhooks) if (record(webhook)) {
webhook.enabled = false;
webhook.verificationPending = false;
const previous = oldHooks.find((old) => record(old) && old.id === webhook.id && old.endpointId === webhook.endpointId);
// A migrated hook needs a secret rotation: an unknown hash cannot authenticate a caller.
const kept = record(previous) && typeof previous.secretHash === "string" && /^[a-f0-9]{64}$/.test(previous.secretHash);
webhook.secretHash = kept ? previous.secretHash : randomBytes(32).toString("hex");
if (kept && typeof previous.sealedSecret === "string" && previous.sealedSecret) webhook.sealedSecret = previous.sealedSecret;
}
});
changeJson("calendar-calls.json", (value) => {
if (!record(value)) throw new Error("Invalid calendar definitions in workspace backup.");
if (Array.isArray(value.calls)) for (const call of value.calls) if (record(call)) call.nextRunAt = null;
});
changeJson("app-triggers.json", (value) => {
if (!record(value)) throw new Error("Invalid app automations in workspace backup.");
if (Array.isArray(value.triggers)) for (const trigger of value.triggers) if (record(trigger)) trigger.enabled = false;
});
if (existsSync(join(prepared, "delegations.json"))) writeJson(join(prepared, "delegations.json"), {});
if (existsSync(join(prepared, "browser-cleanups.json"))) writeJson(join(prepared, "browser-cleanups.json"), []);
for (const entry of manifest.entries) {
if (/^messages-[^/]+\.json$/.test(entry.path)) changeJson(entry.path, (value) => {
if (Array.isArray(value)) for (const message of value) paths.message(message);
else if (record(value) && Array.isArray(value.messages)) for (const message of value.messages) paths.message(message);
});
}
const dbPath = join(prepared, "messages.db");
if (existsSync(dbPath)) {
const db = new DatabaseSync(dbPath);
try {
db.exec("BEGIN");
if (manifest.sourceDataDir !== resolve(dataDir)) {
const searchText = (db.prepare("PRAGMA table_info(messages)").all() as Array<{ name: string }>).some((column) => column.name === "search_text");
const update = db.prepare(`UPDATE messages SET json = ?, text = ?${searchText ? ", search_text = ?" : ""} WHERE thread_id = ? AND id = ?`);
for (const row of db.prepare("SELECT thread_id, id, json FROM messages").iterate()) {
const message: unknown = JSON.parse(String(row.json));
paths.message(message);
const json = JSON.stringify(message);
const text = record(message) && typeof message.text === "string" ? message.text : null;
if (json !== row.json) update.run(json, text, ...(searchText ? [cjkSearchText(text)] : []), row.thread_id, row.id);
}
}
// Import is not a restart: the source may still be running, so never execute a copy of its pending work.
if (db.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'chat_followups'").get()) {
const pause = db.prepare("UPDATE chat_followups SET status = 'interrupted', payload = ? WHERE id = ?");
for (const row of db.prepare("SELECT id, payload FROM chat_followups WHERE status IN ('pending', 'dispatching', 'interrupted')").iterate()) {
const payload: unknown = JSON.parse(String(row.payload));
paths.message(payload);
if (record(payload)) delete payload.prompt;
pause.run(JSON.stringify(payload), row.id);
}
}
db.exec("COMMIT");
} finally { db.close(); }
}
const runtimePath = join(prepared, "runtime.db");
if (existsSync(runtimePath)) {
const db = new DatabaseSync(runtimePath);
try {
db.exec("BEGIN");
const now = Date.now();
const update = db.prepare("UPDATE assignments SET status = 'failed', finished_at = ?, json = ? WHERE id = ?");
const event = db.prepare("INSERT INTO assignment_events (assignment_id, at, kind, detail) VALUES (?, ?, 'settled', ?)");
for (const row of db.prepare("SELECT id, json FROM assignments WHERE status NOT IN ('completed', 'failed', 'cancelled')").iterate()) {
const assignment: unknown = JSON.parse(String(row.json));
if (!record(assignment)) continue;
assignment.status = "failed";
assignment.outcome = "interrupted";
assignment.outcomeDetail = "Not resumed after workspace restore.";
assignment.result = "Not resumed after workspace restore.";
assignment.finishedAt = now;
assignment.lastActivityAt = now;
delete assignment.restart;
delete assignment.question;
delete assignment.answer;
update.run(now, JSON.stringify(assignment), row.id);
event.run(row.id, now, "failed: interrupted — Not resumed after workspace restore.");
}
db.exec("COMMIT");
db.exec("PRAGMA journal_mode = DELETE");
} finally { db.close(); }
}
const leftBehind = paths.leftBehind();
if (leftBehind) writeJson(join(job, "left-behind.json"), leftBehind);
else rmSync(join(job, "left-behind.json"), { force: true });
return prepared;
}
export function readLastWorkspaceRestore(dataDir: string): LastWorkspaceRestore | null {
const root = backupRoot(dataDir);
const path = join(root, "last-restore.json");
if (!existsSync(path)) return null;
const raw = privateJson(path);
if (!record(raw) || raw.restored !== true || typeof raw.id !== "string" || !ID.test(raw.id)) throw new Error("Invalid workspace restore receipt.");
return raw as unknown as LastWorkspaceRestore;
}
function finishRestore(dataDir: string, id: string, consumePending = true): LastWorkspaceRestore {
const root = backupRoot(dataDir);
const { summary, clientState } = readStagedWorkspaceBackup(dataDir, id);
const draftAttachments = clientState["omb-draft-attachments"];
if (draftAttachments) {
try {
const value: unknown = JSON.parse(draftAttachments);
const manifest = validateManifest(privateJson(join(jobPath(dataDir, id), "staged", "manifest.json")));
restorePaths(manifest.sourceDataDir, resolve(dataDir)).fields(value);
clientState["omb-draft-attachments"] = JSON.stringify(value);
} catch {}
}
const leftBehind = join(jobPath(dataDir, id), "left-behind.json");
const result: LastWorkspaceRestore = { restored: true, id, safetyCopyPath: join(root, `safety-${id}`), summary, clientState, pathsRewritten: true,
...(existsSync(leftBehind) ? { leftBehind: privateJson(leftBehind) as WorkspaceLeftBehind } : {}) };
writeJson(join(root, "last-restore.json"), result);
if (consumePending) rmSync(join(root, "pending-restore.json"), { force: true });
return result;
}
/** Call after the data-directory lease, before anything reads app state; the old tree stays under .backups/safety-ID. */
export function applyPendingWorkspaceRestore(dataDir: string): WorkspaceRestoreResult {
const root = backupRoot(dataDir);
const journalFile = join(root, "restore-journal.json");
const pendingFile = join(root, "pending-restore.json");
if (existsSync(journalFile)) {
const previous = readJournal(journalFile);
if (previous.phase === "applying") {
rollback(dataDir, previous);
writeJson(journalFile, { ...previous, phase: "rolled-back" });
rmSync(pendingFile, { force: true });
return { restored: false, rolledBack: true, id: previous.id, safetyCopyPath: join(root, `safety-${previous.id}`) };
}
if (previous.phase === "committed" && existsSync(pendingFile)) {
const pending = privateJson(pendingFile);
if (record(pending) && pending.id === previous.id) {
return finishRestore(dataDir, previous.id);
}
}
}
const pending = readPendingWorkspaceRestoreMetadata(dataDir);
if (!pending) return { restored: false };
const safetyCopyPath = join(root, `safety-${pending.id}`);
if (entryExists(safetyCopyPath)) {
const saved = join(safetyCopyPath, "data");
// Moves start only after the journal is durable; an empty safety tree is an interrupted preparation.
if (!lstatSync(safetyCopyPath).isDirectory() || (entryExists(saved) &&
(!lstatSync(saved).isDirectory() || readdirSync(saved).length !== 0))) {
throw new Error("A safety copy already exists for this restore. Original files have not been changed.");
}
rmSync(pendingFile, { force: true });
return { restored: false, rolledBack: true, id: pending.id, safetyCopyPath };
}
const staged = join(jobPath(dataDir, pending.id), "staged");
const manifest = validateStaged(staged);
const prepared = prepareRestore(dataDir, pending.id, manifest);
folder(join(safetyCopyPath, "data"));
const journal: RestoreJournal = {
id: pending.id, phase: "applying",
existing: readdirSync(dataDir).filter((name) => !keptInPlace(name)).sort(),
incoming: readdirSync(prepared).sort(),
};
// Names are checked before any move is recorded; a restored root never traverses an archive-controlled directory.
for (const name of [...journal.existing, ...journal.incoming]) {
if (!validRelative(name) || name.includes("/") || preserved(name)) throw new Error("Unsafe workspace restore target.");
}
writeJson(join(safetyCopyPath, "manifest.json"), { id: pending.id, createdAt: new Date().toISOString(), originalEntries: journal.existing });
if (existsSync(join(root, "last-restore.json"))) writeJson(join(safetyCopyPath, "previous-restore.json"), privateJson(join(root, "last-restore.json")));
writeJson(journalFile, journal);
try {
for (const name of journal.existing) renameSync(join(dataDir, name), join(safetyCopyPath, "data", name));
for (const name of journal.incoming) if (entryExists(join(prepared, name))) renameSync(join(prepared, name), join(dataDir, name));
finishRestore(dataDir, pending.id, false);
writeJson(journalFile, { ...journal, phase: "committed" });
} catch (error) {
rollback(dataDir, journal);
writeJson(journalFile, { ...journal, phase: "rolled-back" });
rmSync(pendingFile, { force: true });
throw new Error("The workspace restore failed and the previous workspace was recovered.", { cause: error });
}
return finishRestore(dataDir, pending.id);
}
export function removeWorkspaceBackupJob(dataDir: string, id: string): void {
const path = jobPath(dataDir, id);
const root = backupRoot(dataDir);
for (const name of ["pending-restore.json", "restore-journal.json", "last-restore.json"]) {
if (!existsSync(join(root, name))) continue;
const value = privateJson(join(root, name));
if (record(value) && value.id === id) throw new Error("This backup is still needed for restore or recovery and cannot be removed.");
}
if (existsSync(join(root, `safety-${id}`))) throw new Error("This backup has a recovery copy and cannot be removed.");
if (!entryExists(path)) return;
if (!lstatSync(path).isDirectory() || lstatSync(path).isSymbolicLink()) throw new Error("Unsafe workspace backup cleanup target.");
rmSync(path, { recursive: true, force: true });
}
export function omittedFromWorkspaceBackup(path: string): boolean {
return excluded(path.split("/")[0]) || excludedWorkspaceAuthPath(path) || ephemeralWorkspaceTokenPath(path) || redownloadedOrgLibraryPath(path) || dataEngineSpillPath(path);
}