Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

wip(memory): preserve non-destructive backup worker checkpoint

Rahmanaa7c2716b4fc
diff --git a/lib/memory-backup/io.ts b/lib/memory-backup/io.ts
new file mode 100644
index 00000000..ede2bbae
--- /dev/null
+++ b/lib/memory-backup/io.ts
@@ -0,0 +1,48 @@
+import { constants, promises as fs } from "node:fs";
+import path from "node:path";
+import { createHash } from "node:crypto";
+import { pinSecurityStorePath } from "@/lib/security-store-path";
+export const digest = (data: Buffer) => createHash("sha256").update(data).digest("hex");
+export function safeRelative(value: string) {
+ return value.length > 0 && value.length < 2048 && value.split("/").every(p => /^[A-Za-z0-9_.-]{1,180}$/.test(p) && p !== "." && p !== "..");
+}
+// Do not create missing source directories (the shared write pin can create parents).
+export async function existing(file: string) {
+ if (!path.isAbsolute(file) || file.split(path.sep).includes("..")) throw new Error("unsafe path");
+ let current = "/";
+ for (const part of file.split("/").filter(Boolean)) {
+ current = path.join(current, part);
+ if ((await fs.lstat(current)).isSymbolicLink()) throw new Error("unsafe path");
+ }
+}
+export async function readBytes(file: string, max: number) {
+ await existing(file);
+ const pin = await pinSecurityStorePath(file);
+ let handle;
+ try {
+ handle = await fs.open(pin.file, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
+ const before = await handle.stat();
+ if (!before.isFile() || before.uid !== process.getuid?.() || (before.mode & 0o022) || before.size > max) throw new Error("unsafe or oversized input");
+ const bytes = Buffer.alloc(before.size + 1);
+ let used = 0;
+ while (used < bytes.length) {
+ const part = await handle.read(bytes, used, bytes.length - used, used);
+ if (!part.bytesRead) break;
+ used += part.bytesRead;
+ }
+ const after = await handle.stat();
+ const replaced = after.nlink === 0 && after.ino === before.ino && after.dev === before.dev;
+ if (used !== before.size || after.size !== before.size || after.mtimeMs !== before.mtimeMs ||
+ (after.ctimeMs !== before.ctimeMs && !replaced) || after.mode !== before.mode || after.uid !== before.uid) throw new Error("changed input");
+ return bytes.subarray(0, used);
+ } finally { await handle?.close(); await pin.directory.close(); }
+}
+export async function writeExclusive(file: string, bytes: Buffer) {
+ const pin = await pinSecurityStorePath(file);
+ let handle;
+ try {
+ if ((await pin.directory.stat()).mode & 0o077) throw new Error("backup directory is not private");
+ handle = await fs.open(pin.file, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600);
+ await handle.writeFile(bytes); await handle.sync();
+ } finally { await handle?.close(); await pin.directory.close(); }
+}
diff --git a/lib/memory-backup/scan.ts b/lib/memory-backup/scan.ts
new file mode 100644
index 00000000..432d39c0
--- /dev/null
+++ b/lib/memory-backup/scan.ts
@@ -0,0 +1,55 @@
+import { promises as fs } from "node:fs";
+import path from "node:path";
+import { gunzipSync } from "node:zlib";
+import { pinSecurityStorePath } from "@/lib/security-store-path";
+import { redactText } from "@/lib/security/redact-text";
+import { existing, safeRelative, readBytes } from "./io";
+import type { Source } from "./sources";
+export const LIMITS = { files: 5000, entries: 15000, bytes: 128 * 1024 * 1024, fileBytes: 8 * 1024 * 1024, ms: 20000 };
+export type Limits = typeof LIMITS;
+export type Scan = { files: number; bytes: number; missing: number; rejected: number; truncated: boolean };
+const excluded = /(?:^\.env|credential|secret|password|token|config|browser|profile|^\.ssh$|^\.git$|^node_modules$|^locks?$|\.lock(?:\.|$)|\.tmp$)/i;
+export async function scanSources(sources: Source[], limits: Limits, consume?: (name: string, data: Buffer) => Promise<void>): Promise<Scan> {
+ const report: Scan = { files: 0, bytes: 0, missing: 0, rejected: 0, truncated: false };
+ const deadline = Date.now() + limits.ms;
+ let entries = 0;
+ const stopped = () => {
+ if (Date.now() >= deadline || entries >= limits.entries || report.files >= limits.files) report.truncated = true;
+ return report.truncated;
+ };
+ async function visit(file: string, name: string, depth: number) {
+ if (stopped()) return;
+ entries++;
+ if (depth > 32 || !safeRelative(name)) { report.rejected++; return; }
+ try {
+ await existing(file);
+ const stat = await fs.lstat(file);
+ if (stat.isDirectory()) {
+ const pin = await pinSecurityStorePath(path.join(file, ".backup-index"));
+ try {
+ const dir = await fs.opendir(path.dirname(pin.file));
+ for await (const entry of dir) {
+ if (stopped()) break;
+ if (excluded.test(entry.name)) { entries++; continue; }
+ await visit(path.join(file, entry.name), `${name}/${entry.name}`, depth + 1);
+ }
+ } finally { await pin.directory.close(); }
+ } else {
+ if (!stat.isFile() || excluded.test(path.basename(file))) { report.rejected++; return; }
+ if (stat.size > limits.fileBytes || stat.size + report.bytes > limits.bytes) { report.truncated = true; return; }
+ const bytes = await readBytes(file, limits.fileBytes);
+ if (bytes.length + report.bytes > limits.bytes) { report.truncated = true; return; }
+ // Never transform a snapshot silently: suspected embedded secrets omit the file.
+ const text = (file.endsWith(".gz") ? gunzipSync(bytes, { maxOutputLength: limits.fileBytes }) : bytes).toString("utf8");
+ if (/-----BEGIN [A-Z ]*PRIVATE KEY-----/.test(text) || redactText(text) !== text) { report.rejected++; return; }
+ await consume?.(name, bytes);
+ report.files++; report.bytes += bytes.length;
+ }
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") report.missing++;
+ else report.rejected++;
+ }
+ }
+ for (const source of sources) await visit(source.path, source.key, 0);
+ return report;
+}
diff --git a/lib/memory-backup/sources.ts b/lib/memory-backup/sources.ts
new file mode 100644
index 00000000..76081bb0
--- /dev/null
+++ b/lib/memory-backup/sources.ts
@@ -0,0 +1,25 @@
+import path from "node:path";
+import os from "node:os";
+import { agentSessionsDir } from "@/lib/agent/session-paths";
+import { agentSessionArchiveRoot } from "@/lib/agent/session-archive";
+import { workflowStorePath } from "@/lib/workflow/storage";
+import { listProjectDirs } from "@/lib/host/project-roots";
+export type Source = { key: string; path: string };
+export async function memorySources(): Promise<{ sources: Source[]; incomplete: boolean }> {
+ const home = path.join(os.homedir(), ".mso");
+ const recipes = workflowStorePath();
+ const sources: Source[] = [
+ { key: "sessions", path: agentSessionsDir() },
+ { key: "memory", path: path.resolve(process.env.OS_AGENT_MEMORY_DIR || path.join(home, "agent-memory")) },
+ { key: "session-archive", path: agentSessionArchiveRoot() },
+ { key: "recipes", path: recipes },
+ { key: "recipe-archive", path: recipes + ".archive-v1" },
+ { key: "organization", path: (process.env.OS_ORGANIZATION_STORE || path.join(home, "private", "organization.json")).replace(/^~(?=$|\/)/, os.homedir()) },
+ ];
+ const projects = await listProjectDirs();
+ projects.dirs.forEach(({ dir }, index) => {
+ sources.push({ key: `project-${index}-agent`, path: path.join(dir, ".agent") });
+ sources.push({ key: `project-${index}-knowledge`, path: path.join(dir, ".mso", "KNOWLEDGE.md") });
+ });
+ return { sources, incomplete: projects.scan.truncated || projects.scan.skippedRoots.length > 0 || projects.scan.skippedProjects > 0 };
+}