Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(attachments): serialize original retention within its storage budget
Afloat5e4bd5f2f7bd
diff --git a/server/attachment-originals.test.ts b/server/attachment-originals.test.tsindex 414a5be716..0a0019e5da 100644--- a/server/attachment-originals.test.ts+++ b/server/attachment-originals.test.ts@@ -71,6 +71,29 @@ describe("kept originals", () => {expect(keptOriginalsBytes(kept)).toBe(0);});+ it("keeps concurrent uploads within the shared folder's total limit", async () => {+ const uploads = Array.from({ length: 20 }, (_, index) => join(store, name(index + 20)));+ for (const [index, upload] of uploads.entries()) writeFileSync(upload, Buffer.alloc(32, index));+ await Promise.all(uploads.map((upload, index) => keepUploadedOriginal(upload, index % 2 ? `${kept}/.` : kept, 128)));+ expect(keptOriginalsBytes(kept)).toBe(128);+ expect(readdirSync(kept)).toHaveLength(4);+ expect(keptOriginal(uploads.at(-1)!, store, kept)).toBe(join(kept, name(39)));+ for (const copy of readdirSync(kept)) expect(readFileSync(join(kept, copy))).toEqual(readFileSync(join(store, copy)));+ });++ it("lets queued uploads keep their originals after a copy fails", async () => {+ mkdirSync(kept, { recursive: true });+ const first = join(store, name(40)), second = join(store, name(41));+ writeFileSync(first, "first upload");+ writeFileSync(second, "second upload");+ const partial = `${join(kept, name(40))}.${process.pid}.partial`;+ writeFileSync(partial, "unfinished copy");+ await Promise.all([keepUploadedOriginal(first, kept), keepUploadedOriginal(second, kept)]);+ expect(existsSync(partial)).toBe(false);+ expect(keptOriginal(first, store, kept)).toBeNull();+ expect(readFileSync(keptOriginal(second, store, kept)!, "utf8")).toBe("second upload");+ });+it("never fails the upload when the copy cannot be made", async () => {writeFileSync(join(root, "not-a-folder"), "");await expect(keepUploadedOriginal(join(store, name(9)), join(root, "not-a-folder", "kept"))).resolves.toBeUndefined();diff --git a/server/attachment-originals.ts b/server/attachment-originals.tsindex 415562dac5..5df696f7d8 100644--- a/server/attachment-originals.ts+++ b/server/attachment-originals.ts@@ -18,6 +18,7 @@ export const KEPT_TOTAL_MAX_BYTES = 256 * 1024 * 1024;const STORED_NAME = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\.([a-z0-9]+)$/i;const DOCUMENT = new Set(["md", "txt", "csv", "tsv", "json", "rtf", "doc", "docx", "odt", "xls", "xlsx", "ods", "ppt", "pptx", "odp"]);+const pendingCopies = new Map<string, Promise<void>>();export function isKeptDocumentName(name: string): boolean {const match = STORED_NAME.exec(name);@@ -53,6 +54,12 @@ async function makeRoom(keptDir: string, size: number, total: number): Promise<vexport async function keepUploadedOriginal(path: string, keptDir = KEPT_ORIGINALS_DIR, total = KEPT_TOTAL_MAX_BYTES): Promise<void> {const name = basename(path);if (!isKeptDocumentName(name)) return;+ const directory = resolve(keptDir);+ const previous = pendingCopies.get(directory) ?? Promise.resolve();+ let release!: () => void;+ const current = new Promise<void>((resolve) => { release = resolve; });+ pendingCopies.set(directory, current);+ await previous;const target = join(keptDir, name);const partial = `${target}.${process.pid}.partial`;try {@@ -68,6 +75,8 @@ export async function keepUploadedOriginal(path: string, keptDir = KEPT_ORIGINAL// The upload stands; it simply has no kept copy, and a comparison says so.} finally {await rm(partial, { force: true }).catch(() => undefined);+ release();+ if (pendingCopies.get(directory) === current) pendingCopies.delete(directory);}}