Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(move): rehash files after timestamp-preserving edits
Afloate9d276571524
diff --git a/server/cloud-send-files.test.ts b/server/cloud-send-files.test.tsindex f9b51f2e1b..dd35a28957 100644--- a/server/cloud-send-files.test.ts+++ b/server/cloud-send-files.test.ts@@ -1,5 +1,5 @@import { createHash } from "node:crypto";-import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, symlinkSync, writeFileSync } from "node:fs";+import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, renameSync, symlinkSync, utimesSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";import { Readable } from "node:stream";@@ -32,6 +32,30 @@ describe("which bot files a move carries", () => {});});+describe("bot-file digests after a timestamp-preserving edit", () => {+ it.each(["overwrite", "replace"])("rechecks a same-size file after %s with its old mtime", async (operation) => {+ const data = mkdtempSync(join(tmpdir(), "omb-files-digest-"));+ const path = "workspaces/b1/notes.md";+ const full = join(data, path);+ const old = new Date("2020-01-01T00:00:00Z");+ mkdirSync(join(data, "workspaces", "b1"), { recursive: true });+ writeFileSync(full, "first");+ utimesSync(full, old, old);+ expect(await listBotFiles(data, ["b1"])).toEqual([{ path, size: 5, sha256: sha("first") }]);+ if (operation === "replace") {+ const replacement = join(data, "replacement.md");+ writeFileSync(replacement, "later");+ utimesSync(replacement, old, old);+ renameSync(replacement, full);+ } else {+ writeFileSync(full, "later");+ utimesSync(full, old, old);+ }+ expect(await listBotFiles(data, ["b1"])).toEqual([{ path, size: 5, sha256: sha("later") }]);+ expect(await listBotFiles(data, ["b1"])).toEqual([{ path, size: 5, sha256: sha("later") }]);+ });+});+describe("My Cloud takes a file", () => {const data = mkdtempSync(join(tmpdir(), "omb-files-"));const owner = { person: "p_owner", name: "Amit" };diff --git a/server/cloud-send-files.ts b/server/cloud-send-files.tsindex 0a55ef69fa..e9ad8919d9 100644--- a/server/cloud-send-files.ts+++ b/server/cloud-send-files.ts@@ -4,7 +4,7 @@// My Cloud takes a file in parts (POST, owner's session only), resumable from the bytes it holds, and never writes over// a file it has: a file there that is not the one the move put there is a conflict, and the sender keeps both.import { createHash, randomUUID } from "node:crypto";-import { createReadStream } from "node:fs";+import { createReadStream, type Stats } from "node:fs";import { link, lstat, mkdir, open, readdir, rename, stat, unlink } from "node:fs/promises";import { statfsSync } from "node:fs";import type { IncomingMessage } from "node:http";@@ -41,16 +41,20 @@ export function movedFilePath(path: string): boolean {}const hashes = new Map<string, string>();-async function fileSha256(full: string, size: number, mtimeMs: number): Promise<string> {- const key = `${full}\0${size}\0${mtimeMs}`;- const known = hashes.get(key);+async function fileSha256(full: string, info: Stats): Promise<string> {+ const key = `${full}\0${info.dev}\0${info.ino}\0${info.size}\0${info.mtimeMs}\0${info.ctimeMs}`;+ // A restored mtime does not restore ctime; recent writes can still share a coarse filesystem timestamp.+ const settled = Date.now() - Math.max(info.mtimeMs, info.ctimeMs) >= 2_000;+ const known = settled ? hashes.get(key) : undefined;if (known) return known;const hash = createHash("sha256");for await (const chunk of createReadStream(full)) hash.update(chunk as Buffer);const digest = hash.digest("hex");- // As many as one listing holds, oldest out first: a listing never pushes out what it hashed itself.- if (hashes.size >= MAX_FILES) hashes.delete(hashes.keys().next().value!);- hashes.set(key, digest);+ if (settled) {+ // As many as one listing holds, oldest out first: a listing never pushes out what it hashed itself.+ if (hashes.size >= MAX_FILES) hashes.delete(hashes.keys().next().value!);+ hashes.set(key, digest);+ }return digest;}@@ -66,7 +70,7 @@ export async function listBotFiles(dataDir: string, botIds: readonly string[]):const info = await lstat(join(dataDir, path)).catch(() => null);if (!info || name === "node_modules") continue;if (info.isDirectory()) { if (!CARRIED_OTHERWISE.test(`${path}/`) && !omittedFromWorkspaceBackup(path)) await walk(path); continue; }- if (info.isFile() && movedFilePath(path)) out.push({ path, size: info.size, sha256: await fileSha256(join(dataDir, path), info.size, info.mtimeMs) });+ if (info.isFile() && movedFilePath(path)) out.push({ path, size: info.size, sha256: await fileSha256(join(dataDir, path), info) });}};for (const id of botIds) {