From 33d9c0e17eaf06bde98a3a918d895d5d2c829e23 Mon Sep 17 00:00:00 2001 From: clover caruso Date: Sat, 10 Oct 2026 19:07:38 -0700 Subject: [PATCH] fix(web): retain failed folder writes before source deletion Reject durability receipts when a mounted-folder write fails and retry the original ranges before following writes. Bound each receipt to the writes present at submission so later edits cannot starve it. Coalesce best-effort folder scans and propagate removal errors except already missing entries. Old glue fails both destination-error and removal-error regressions (2 failed, exit 1). New glue passes all 8 Node tests, including prefix receipt timing and retry without duplicate successful writes. Fixes #111 Assisted-by: gpt-6.1-sol --- crates/snowbound/web/glue.js | 59 +++++++++++++----- tools/web/test_folder_durability.mjs | 90 ++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 14 deletions(-) create mode 100644 tools/web/test_folder_durability.mjs diff --git a/crates/snowbound/web/glue.js b/crates/snowbound/web/glue.js index 420770abc801f655a5cfa1c99a59345bdabb1364..145dc52eed3fb9749451e9a545291983b3042479 100644 --- a/crates/snowbound/web/glue.js +++ b/crates/snowbound/web/glue.js @@ -186,9 +186,30 @@ export function storeFiles(changes) { // `notebook::fs` under FOLDERS, their handles kept in IndexedDB to reach them again. const FOLDERS = "/Folders"; const folders = new Map(); -let writing = Promise.resolve(); -const serially = (work) => - (writing = writing.then(work).catch((error) => console.error("Writing to the folder", error))); +const pendingWrites = []; +let writing; +function writeFolders(through = pendingWrites.at(-1)) { + if (!through) return writing ?? Promise.resolve(); + if (writing) return writing.then(() => { + if (pendingWrites.includes(through)) return writeFolders(through); + }); + return writing = Promise.resolve().then(async () => { + try { + while (pendingWrites.length) { + const work = pendingWrites[0]; + await work(); + pendingWrites.shift(); + if (work === through) return; + } + } finally { + writing = undefined; + } + }); +} +const serially = (work) => { + pendingWrites.push(work); + writeFolders().catch((error) => console.error("Writing to the folder", error)); +}; const folderOf = (path) => [...folders.keys()].find((root) => path === root || path.startsWith(`${root}/`)); const stamp = (file) => `${file.size}:${file.lastModified}`; @@ -252,21 +273,27 @@ export async function loadFolders() { } } // Other apps' writes reach the page once they land, as a watch would report them. - setInterval(() => serially(look), 3000); + setInterval(() => { + if (!pendingWrites.includes(look)) serially(look); + }, 3000); return out; } /** Hands the page what changed in each folder since it was last read. */ async function look() { - for (const [root, folder] of folders) { - const seen = new Map(); - for (const [path, file] of await walk(folder.handle, root)) { - seen.set(path, file ? stamp(file) : "folder"); - if (folder.known.get(path) !== seen.get(path)) - wasm.refreshed(path, file ? new Uint8Array(await file.arrayBuffer()) : null, file?.lastModified ?? 0); + try { + for (const [root, folder] of folders) { + const seen = new Map(); + for (const [path, file] of await walk(folder.handle, root)) { + seen.set(path, file ? stamp(file) : "folder"); + if (folder.known.get(path) !== seen.get(path)) + wasm.refreshed(path, file ? new Uint8Array(await file.arrayBuffer()) : null, file?.lastModified ?? 0); + } + for (const path of folder.known.keys()) if (!seen.has(path)) wasm.refreshed(path, undefined, 0); + folder.known = seen; } - for (const path of folder.known.keys()) if (!seen.has(path)) wasm.refreshed(path, undefined, 0); - folder.known = seen; + } catch (error) { + console.error("Reading the folder", error); } } @@ -282,7 +309,11 @@ async function writeFolder([path, length, ranges, committed]) { if (folders.has(path)) return; const [dir, name, folder] = await locate(path); if (length === undefined) { - await dir.removeEntry(name, { recursive: true }).catch(() => {}); + try { + await dir.removeEntry(name, { recursive: true }); + } catch (error) { + if (error.name !== "NotFoundError") throw error; + } for (const known of [...folder.known.keys()]) if (known === path || known.startsWith(`${path}/`)) folder.known.delete(known); return; @@ -354,7 +385,7 @@ function settled() { settling.push({ resolve, reject }); storage.postMessage({ kind: "settle" }); }); - return Promise.all([kept, writing]); + return Promise.all([kept, writeFolders()]); } // A deployed build's module and JavaScript lie in b/HASH/, which names the build. diff --git a/tools/web/test_folder_durability.mjs b/tools/web/test_folder_durability.mjs new file mode 100644 index 0000000000000000000000000000000000000000..03abaa1961b3135b6bcc4bdfb4b00fd8455d66de --- /dev/null +++ b/tools/web/test_folder_durability.mjs @@ -0,0 +1,90 @@ +import assert from 'node:assert/strict'; +import { readFile } from 'node:fs/promises'; +import test from 'node:test'; +import vm from 'node:vm'; + +async function browser(folder) { + const source = await readFile(new URL('../../crates/snowbound/web/glue.js', import.meta.url), 'utf8'); + const context = vm.createContext({ folder, queueMicrotask, console: { error() {} } }); + vm.runInContext(source.replace(/\bexport /g, '').replaceAll('import.meta.url', JSON.stringify(import.meta.url)), context); + vm.runInContext(` + storage = { postMessage() { queueMicrotask(() => settling.shift().resolve()); } }; + wasm = { refreshed() {} }; + folders.set('/Folders/Notebook', { handle: folder, known: new Map() }); + `, context); + return context; +} + +test('a failed destination write refuses source deletion and retries before later writes', async () => { + const attempts = []; + const written = []; + let failing = true; + const context = await browser({ + async getFileHandle(name) { + return { + async getFile() { return { size: 0, lastModified: 0 }; }, + async createWritable() { + attempts.push(name); + if (failing) throw Error('Destination unavailable'); + return { + async write({ data }) { written.push([name, [...data]]); }, + async truncate() {}, + async close() {}, + }; + }, + }; + }, + }); + vm.runInContext(` + storeFiles([ + ['/Folders/Notebook/destination.one', 1, [[0, new Uint8Array([1])]]], + ['/Folders/Notebook/following.one', 1, [[0, new Uint8Array([2])]]], + ]); + `, context); + let sourceDeleted = false; + const move = vm.runInContext('settled()', context).then(() => { sourceDeleted = true; }); + await assert.rejects(move, /Destination unavailable/); + assert.equal(sourceDeleted, false); + assert.deepEqual(attempts, ['destination.one']); + assert.deepEqual(written, []); + failing = false; + await vm.runInContext('settled()', context).then(() => { sourceDeleted = true; }); + assert.equal(sourceDeleted, true); + assert.deepEqual(attempts, ['destination.one', 'destination.one', 'following.one']); + assert.deepEqual(written, [['destination.one', [1]], ['following.one', [2]]]); + vm.runInContext("storeFiles([['/Folders/Notebook/later.one', 1, [[0, new Uint8Array([3])]]]])", context); + await vm.runInContext('settled()', context); + assert.deepEqual(written, [['destination.one', [1]], ['following.one', [2]], ['later.one', [3]]]); +}); + +test('folder deletion retains permission failures and accepts an already missing entry', async () => { + const attempts = []; + let failing = true; + const context = await browser({ + async removeEntry(name) { + attempts.push(name); + const error = Error(failing ? 'Permission refused' : 'Already removed'); + error.name = failing ? 'NotAllowedError' : 'NotFoundError'; + throw error; + }, + }); + vm.runInContext("storeFiles([['/Folders/Notebook/deleted.one']])", context); + await assert.rejects(vm.runInContext('settled()', context), /Permission refused/); + failing = false; + await vm.runInContext('settled()', context); + assert.deepEqual(attempts, ['deleted.one', 'deleted.one']); +}); + + +test('a destination receipt completes without waiting for later folder writes', async () => { + let release; + const first = new Promise((resolve) => { release = resolve; }); + const context = await browser({}); + context.first = first; + vm.runInContext('serially(() => first)', context); + const receipt = vm.runInContext('settled()', context); + vm.runInContext("serially(() => Promise.reject(Error('Later failure')))", context); + release(); + await receipt; + await assert.rejects(vm.runInContext('settled()', context), /Later failure/); +}); -- 2.54.0