From 2f8db04f6f24df9d52984029208210d20cfc8a58 Mon Sep 17 00:00:00 2001 From: yansfil Date: Mon, 5 Oct 2026 22:26:13 +0900 Subject: [PATCH] Read a saved document as not yet while a Windows save has its name empty --- docs/ARCHITECTURE.md | 2 +- hide-host/src/save.rs | 7 +++++-- web/e2e/s3.spec.ts | 44 +++++++++++++++++++++---------------------- web/e2e/s4.spec.ts | 3 ++- web/e2e/s7.spec.ts | 10 +++++----- web/e2e/wait.ts | 16 ++++++++++++++++ 6 files changed, 51 insertions(+), 31 deletions(-) diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 041e6a6ff8..1dfe6189a5 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -496,7 +496,7 @@ It takes a path where a caller holds a name and `Handle` (`AsFd` on Unix, `AsHan `cap-std` opens its folders with `O_PATH` on Linux and the kernel answers `EBADF` to `flock` and `fsync` on such a descriptor, which refused every save on Linux once (#285), so a folder is always locked and flushed through a fresh readable descriptor of the same folder; `hide-host/tests/save.rs` and the contract test that locks through an `O_PATH` handle are the proof. Windows refuses a byte-range lock on a folder (`ERROR_ACCESS_DENIED`) and a lock file inside a checkout would put a file in someone's work, so a folder is locked through one of 65536 empty files in `%LOCALAPPDATA%\hide\locks`, picked by the folder's identity: two folders that pick the same file only wait for each other, the count cannot grow, and the folder is trusted only when the account owns it and nobody else can change it. That lock is one account's, so two accounts working in one folder do not exclude each other on Windows as they do under `flock`. -`atomic` replaces a file whole or not at all (`write_file`, `replace_file`), exchanges two names (`exchange`: `RENAME_SWAP` on macOS, `RENAME_EXCHANGE` on Linux; on Windows a `ReplaceFileW` that parks the old file under a `.hide-swap-` name and a rename, so the replaced name never shows a partial file but the other name is empty for a moment, which `EXCHANGE_IS_ATOMIC` says) and renames without replacing (`rename_no_replace`: `RENAME_EXCL`, `RENAME_NOREPLACE`, `MoveFileExW`). +`atomic` replaces a file whole or not at all (`write_file`, `replace_file`), exchanges two names (`exchange`: `RENAME_SWAP` on macOS, `RENAME_EXCHANGE` on Linux; on Windows a `ReplaceFileW` that parks the old file under a `.hide-swap-` name and a rename, so the replaced name never shows a partial file, but it names nothing for the moment `ReplaceFileW` moves the old file aside and the other name is empty until the rename, which `EXCHANGE_IS_ATOMIC` says) and renames without replacing (`rename_no_replace`: `RENAME_EXCL`, `RENAME_NOREPLACE`, `MoveFileExW`). The legacy writers provide whole-file visibility with best-effort parent sync; Windows's legacy `sync_dir` is a no-op and does not acknowledge persistence. The opt-in `write_file_durable` shares the bounded temporary pipeline and checks file sync plus rename and fallible parent open/sync on Unix, or same-directory `MoveFileExW(REPLACE_EXISTING | WRITE_THROUGH)` on Windows, where it supports only private access and refuses other modes before mutation to preserve their existing ACL contract. Its errors distinguish `BeforeReplace`, `ReplacementUncertain` and `ReplacedNotDurable`; the last retains the installed `FileId`, all retain the original I/O cause, and failed owned-temp cleanup reports its own path and cause separately. diff --git a/hide-host/src/save.rs b/hide-host/src/save.rs index 6b4296d044..0194c057f1 100644 --- a/hide-host/src/save.rs +++ b/hide-host/src/save.rs @@ -12,8 +12,11 @@ //! 3. the temporary file and the original are exchanged by the platform layer //! (`hide_platform::fs::atomic::exchange`: one atomic call on macOS and //! Linux, a replace that keeps the old file under another name on -//! Windows), so the path names either the old file or the new one and -//! never a truncated one; +//! Windows), so the path never names a truncated file. On macOS and Linux +//! it names the old file or the new one at every moment; on Windows +//! `ReplaceFileW` moves the old file aside before the new one takes its +//! name, so for that moment the path names nothing, and a reader there is +//! told the file does not exist; //! 4. the file that was displaced is hashed again. If it is not the revision //! the caller read, someone changed it between step 1 and step 3: the two //! are exchanged back, their version stays at the path, and the save is a diff --git a/web/e2e/s3.spec.ts b/web/e2e/s3.spec.ts index 39d8e6e64e..8acf50387a 100644 --- a/web/e2e/s3.spec.ts +++ b/web/e2e/s3.spec.ts @@ -14,7 +14,7 @@ import { startHerdr, type HerdrFixture } from "./herdr-fixture"; import { startHided, type Daemon } from "./hided-fixture"; import { countSent, enterWorkspace, screenshot, showExplorer } from "./wire"; import { chord } from "./chords"; -import { quietFor } from "./wait"; +import { onDisk, quietFor } from "./wait"; const SOURCE = "export const answer = 41;\n"; @@ -277,7 +277,7 @@ test("editing a document marks it dirty, saves it, and a disk change asks how to // with the tab clean again (D-10). await expect.poll(() => sent.get("file_save"), { timeout: 5_000 }).toBeGreaterThanOrEqual(1); expect(lastSent.get("file_save")).toMatchObject({ path: file, contents_utf8: "export const answer = 42;\n" }); - await expect.poll(() => fs.readFileSync(file, "utf8")).toBe("export const answer = 42;\n"); + await expect.poll(() => onDisk(file)).toBe("export const answer = 42;\n"); await expect(page.locator('[data-editor-dirty="true"]')).toHaveCount(0); await screenshot(page, "s3-editor-saved"); @@ -296,7 +296,7 @@ test("editing a document marks it dirty, saves it, and a disk change asks how to await page.keyboard.type("export const answer = 43;\n"); await page.keyboard.press(chord("save_file")); await expect(page.locator("[data-editor-conflict]")).toBeVisible(); - await expect.poll(() => fs.readFileSync(file, "utf8")).toBe("export const answer = 0;\n"); + await expect.poll(() => onDisk(file)).toBe("export const answer = 0;\n"); await screenshot(page, "s3-editor-conflict"); // Reload takes the disk contents into the editor. @@ -319,7 +319,7 @@ test("a refused save keeps the tab dirty and takes the saving mark off", async ( await page.keyboard.press("ControlOrMeta+KeyA"); await page.keyboard.type("export const answer = 42;\n"); await expect.poll(() => sent.get("file_save"), { timeout: 5_000 }).toBeGreaterThanOrEqual(1); - await expect.poll(() => fs.readFileSync(file, "utf8")).toBe("export const answer = 42;\n"); + await expect.poll(() => onDisk(file)).toBe("export const answer = 42;\n"); // A file the process can no longer write: the next autosave is refused, so // the tab stops saying it is saving and stays dirty, with the detail in @@ -333,7 +333,7 @@ test("a refused save keeps the tab dirty and takes the saving mark off", async ( const tab = page.locator('[data-tab-kind="file"]'); await expect(tab).toHaveAttribute("data-saving", "false", { timeout: 10_000 }); await expect(page.locator('[data-editor-dirty="true"]')).toBeVisible(); - await expect.poll(() => fs.readFileSync(file, "utf8")).toBe("export const answer = 42;\n"); + await expect.poll(() => onDisk(file)).toBe("export const answer = 42;\n"); } finally { fs.chmodSync(file, 0o644); close(fixture); @@ -380,7 +380,7 @@ test("a draft that cannot be stored stays editable and holds other documents rea await page.locator('[data-tab-kind="file"][data-tab-only="true"]').click(); await expect(page.locator('[data-editor-draft-hold="unstored"]')).toBeVisible(); await page.keyboard.press(chord("save_file")); - await expect.poll(() => fs.readFileSync(file, "utf8"), { timeout: 10_000 }).toBe("export const answer = 44;\n"); + await expect.poll(() => onDisk(file), { timeout: 10_000 }).toBe("export const answer = 44;\n"); await expect(page.locator("[data-editor-draft-hold]")).toHaveCount(0); await page.locator('[data-tab-kind="file"]', { hasText: "README.md" }).click(); await expect(page.locator("[data-editor-draft-hold]")).toHaveCount(0); @@ -436,7 +436,7 @@ test("leaving a dirty tab saves it and never writes it into the next file", asyn // Leave within the idle window: the draft still reaches its own file, and // the tab that takes the screen never receives it (D-10, D-14). await page.locator(`[data-explorer-row="${repo}/README.md"]`).click(); - await expect.poll(() => fs.readFileSync(file, "utf8"), { timeout: 10_000 }).toBe("export const answer = 99;\n"); + await expect.poll(() => onDisk(file), { timeout: 10_000 }).toBe("export const answer = 99;\n"); expect(fs.readFileSync(readme, "utf8")).toBe(readmeBefore); await expect(page.locator('[data-editor-body] .cm-content')).not.toContainText("answer = 99"); } finally { @@ -452,7 +452,7 @@ test("an undone edit is what the next save writes", async ({ page }) => { await content.click(); await page.keyboard.press("ControlOrMeta+End"); await page.keyboard.type("X"); - await expect.poll(() => fs.readFileSync(file, "utf8"), { timeout: 10_000 }).toBe(`${SOURCE}X`); + await expect.poll(() => onDisk(file), { timeout: 10_000 }).toBe(`${SOURCE}X`); // Undo that keystroke: the core hears it, so the save that follows writes // the text the editor shows rather than the text that was undone (B4). @@ -461,7 +461,7 @@ test("an undone edit is what the next save writes", async ({ page }) => { const savesBefore = sent.get("file_save") ?? 0; await page.keyboard.press(chord("save_file")); await expect.poll(() => sent.get("file_save")).toBeGreaterThan(savesBefore); - await expect.poll(() => fs.readFileSync(file, "utf8")).toBe(SOURCE); + await expect.poll(() => onDisk(file)).toBe(SOURCE); await expect(page.locator('[data-editor-dirty="true"]')).toHaveCount(0); } finally { close(fixture); @@ -505,7 +505,7 @@ test("closing a dirty tab saves the draft instead of dropping it", async ({ page .poll(() => (lastSent.get("view_layout.close")?.pending_save as { contents_utf8?: string } | null)?.contents_utf8) .toBe(`${SOURCE}X`); await expect(page.locator('[data-tab-kind="file"]')).toHaveCount(0); - await expect.poll(() => fs.readFileSync(file, "utf8"), { timeout: 10_000 }).toBe(`${SOURCE}X`); + await expect.poll(() => onDisk(file), { timeout: 10_000 }).toBe(`${SOURCE}X`); } finally { close(fixture); } @@ -523,7 +523,7 @@ test("a closed tab's draft is not written back when the file is reopened", async await tab.hover(); await tab.locator('button[aria-label^="Close view"]').click(); await expect(page.locator('[data-tab-kind="file"]')).toHaveCount(0); - await expect.poll(() => fs.readFileSync(file, "utf8"), { timeout: 10_000 }).toBe("export const answer = 5;\n"); + await expect.poll(() => onDisk(file), { timeout: 10_000 }).toBe("export const answer = 5;\n"); // An agent rewrites the file, the operator reopens it, and saves with // nothing typed: the closed tab's draft must not come back with it (B5). @@ -533,7 +533,7 @@ test("a closed tab's draft is not written back when the file is reopened", async const savesBefore = sent.get("file_save") ?? 0; await page.keyboard.press(chord("save_file")); await expect.poll(() => sent.get("file_save")).toBeGreaterThan(savesBefore); - await expect.poll(() => fs.readFileSync(file, "utf8")).toBe("export const answer = 0;\n"); + await expect.poll(() => onDisk(file)).toBe("export const answer = 0;\n"); } finally { close(fixture); } @@ -567,7 +567,7 @@ test("a conflicted background tab is not closed away with its draft", async ({ p await conflicted.locator('button[aria-label^="Close view"]').click(); await expect.poll(() => sent.get("view_layout.close")).toBe(1); await expect(page.locator(`[data-tab-kind="file"]`, { hasText: "main.ts" })).toHaveCount(1); - await expect.poll(() => fs.readFileSync(file, "utf8")).toBe("export const answer = 0;\n"); + await expect.poll(() => onDisk(file)).toBe("export const answer = 0;\n"); // The refused close kept the tab, the draft and the choice to resolve it. await page.locator(`[data-tab-kind="file"]`, { hasText: "main.ts" }).click(); @@ -1054,7 +1054,7 @@ test("an unsaved edit survives a socket drop and reconnect", async ({ page }) => await page.context().setOffline(false); await expect(page.locator("[data-connection]")).toHaveCount(0, { timeout: 20_000 }); await expect(content).toContainText("edited across a reconnect"); - await expect.poll(() => fs.readFileSync(path.join(repo, "notes.md"), "utf8"), { timeout: 20_000 }).toContain("edited across a reconnect"); + await expect.poll(() => onDisk(path.join(repo, "notes.md")), { timeout: 20_000 }).toContain("edited across a reconnect"); await expect(page.locator('[data-editor-dirty="true"]')).toHaveCount(0); await screenshot(page, "s3-buffer-reconnect"); } finally { @@ -1090,7 +1090,7 @@ test("an existing v1 recovery draft survives the IndexedDB upgrade", async ({ pa await page.goto(app); await enterWorkspace(page, "repo"); await expect(page.locator('[data-editor-codemirror] .cm-content')).toContainText("answer = 99"); - await expect.poll(() => fs.readFileSync(file, "utf8"), { timeout: 20_000 }).toBe("export const answer = 99;\n"); + await expect.poll(() => onDisk(file), { timeout: 20_000 }).toBe("export const answer = 99;\n"); } finally { close(fixture); } @@ -1123,7 +1123,7 @@ test("a root-keyed v2 recovery draft survives the IndexedDB upgrade", async ({ p await page.goto(app); await enterWorkspace(page, "repo"); await expect(page.locator('[data-editor-codemirror] .cm-content')).toContainText("answer = 98"); - await expect.poll(() => fs.readFileSync(file, "utf8"), { timeout: 20_000 }).toBe("export const answer = 98;\n"); + await expect.poll(() => onDisk(file), { timeout: 20_000 }).toBe("export const answer = 98;\n"); } finally { close(fixture); } @@ -1159,7 +1159,7 @@ test("of two legacy drafts for one file the newer is restored, and the older is await page.goto(app); await enterWorkspace(page, "repo"); await expect(page.locator('[data-editor-codemirror] .cm-content')).toContainText("answer = 96"); - await expect.poll(() => fs.readFileSync(file, "utf8"), { timeout: 20_000 }).toBe("export const answer = 96;\n"); + await expect.poll(() => onDisk(file), { timeout: 20_000 }).toBe("export const answer = 96;\n"); const kept = await page.evaluate(() => new Promise((resolve, reject) => { const opened = indexedDB.open("hide-shell"); opened.onerror = () => reject(opened.error); @@ -1210,7 +1210,7 @@ test("one draft id in both legacy stores migrates as its newer row (S5.5 B12)", await page.goto(app); await enterWorkspace(page, "repo"); await expect(page.locator('[data-editor-codemirror] .cm-content')).toContainText("answer = 96"); - await expect.poll(() => fs.readFileSync(file, "utf8"), { timeout: 20_000 }).toBe("export const answer = 96;\n"); + await expect.poll(() => onDisk(file), { timeout: 20_000 }).toBe("export const answer = 96;\n"); } finally { close(fixture); } @@ -1226,7 +1226,7 @@ test("an old draft is restored, never discarded for its age (S5.5 B12)", async ( }); await page.reload(); await expect(page.locator('[data-editor-codemirror] .cm-content')).toContainText("answer = 97"); - await expect.poll(() => fs.readFileSync(file, "utf8"), { timeout: 20_000 }).toBe("export const answer = 97;\n"); + await expect.poll(() => onDisk(file), { timeout: 20_000 }).toBe("export const answer = 97;\n"); } finally { close(fixture); } @@ -1278,7 +1278,7 @@ test("a draft whose tab a daemon restart lost is kept for recovery and opens whe await page.locator(`[data-draft-open="${id}"]`).click(); await expect(content).toContainText("answer = 77", { timeout: 20_000 }); await expect(line).toHaveCount(0); - await expect.poll(() => fs.readFileSync(file, "utf8"), { timeout: 20_000 }).toBe("export const answer = 77;\n"); + await expect.poll(() => onDisk(file), { timeout: 20_000 }).toBe("export const answer = 77;\n"); await expect(page.locator('[data-editor-dirty="true"]')).toHaveCount(0, { timeout: 10_000 }); await expect.poll(async () => (await storedDrafts(page)).some((row) => row.path === file), { timeout: 10_000 }).toBe(false); } finally { @@ -1393,7 +1393,7 @@ test("an interrupted draft store upgrade loses nothing and completes on the next await page.goto(app); await enterWorkspace(page, "repo"); await expect(page.locator('[data-editor-codemirror] .cm-content')).toContainText("answer = 96"); - await expect.poll(() => fs.readFileSync(file, "utf8"), { timeout: 20_000 }).toBe("export const answer = 96;\n"); + await expect.poll(() => onDisk(file), { timeout: 20_000 }).toBe("export const answer = 96;\n"); } finally { close(fixture); } @@ -1443,7 +1443,7 @@ test("a save refused at the daemon's boundary keeps the draft unsaved, across a refusing = false; await content.click(); await page.keyboard.press(chord("save_file")); - await expect.poll(() => fs.readFileSync(file, "utf8"), { timeout: 20_000 }).toBe("export const answer = 55;\n"); + await expect.poll(() => onDisk(file), { timeout: 20_000 }).toBe("export const answer = 55;\n"); await expect(page.locator('[data-editor-dirty="true"]')).toHaveCount(0); } finally { close(fixture); diff --git a/web/e2e/s4.spec.ts b/web/e2e/s4.spec.ts index a4f6888eaa..30a9850a6c 100644 --- a/web/e2e/s4.spec.ts +++ b/web/e2e/s4.spec.ts @@ -11,6 +11,7 @@ import { startHided } from "./hided-fixture"; import { countSent, registerFolder, screenshot, showExplorer, showTool } from "./wire"; import { chord } from "./chords"; import { toPage } from "../../desktop/src/main/wirePath"; +import { onDisk } from "./wait"; test.describe.configure({ timeout: 90_000 }); @@ -80,7 +81,7 @@ test("History opens a scoped patch, then updates after editing the original file await content.click(); await page.keyboard.press("ControlOrMeta+KeyA"); await page.keyboard.type("first\nthird\n"); - await expect.poll(() => fs.readFileSync(file, "utf8")).toBe("first\nthird\n"); + await expect.poll(() => onDisk(file)).toBe("first\nthird\n"); await showTool(page, "changes"); await row.click(); await expect(page.locator('[data-patch-view] .cm-content')).toContainText("+third"); diff --git a/web/e2e/s7.spec.ts b/web/e2e/s7.spec.ts index 40305f0460..6d15b4f602 100644 --- a/web/e2e/s7.spec.ts +++ b/web/e2e/s7.spec.ts @@ -19,7 +19,7 @@ import { startHerdr, type HerdrFixture } from "./herdr-fixture"; import { startHided, type Daemon } from "./hided-fixture"; import { bindChordlessCommand, countSent, enterWorkspace, screenshot, showExplorer, showTool } from "./wire"; import { chord } from "./chords"; -import { quietFor } from "./wait"; +import { onDisk, quietFor } from "./wait"; /** `--size-rail`: the always-shown device rail takes this much of a window that measured its areas without one. */ const RAIL = 48; @@ -260,7 +260,7 @@ test("a click previews in the last used area, a pin keeps a view, and a shown fi await row("f.txt").click(); await expect.poll(() => shape(page)).toBe("@(b.txt d.txt e.txt >f.txt*) | (c.txt >a.txt*)"); expect(stack.sent.get("file_open")).toBe(opensBefore + 1); - await expect.poll(() => fs.readFileSync(path.join(stack.root, "e.txt"), "utf8")).toBe("e line edited\n"); + await expect.poll(() => onDisk(path.join(stack.root, "e.txt"))).toBe("e line edited\n"); // Opening a file another area shows focuses that view: no tab is added (B3). await row("c.txt").click(); @@ -350,7 +350,7 @@ test("Open to the side shows one document twice: edits and Korean input reach bo ]); await expect(right).toContainText("line 001-B 한글안녕"); await expect(left).toContainText("line 001-B 한글안녕"); - await expect.poll(() => fs.readFileSync(shared, "utf8").split("\n")[0], { timeout: 10_000 }).toBe("line 001-B 한글안녕"); + await expect.poll(() => onDisk(shared)?.split("\n")[0], { timeout: 10_000 }).toBe("line 001-B 한글안녕"); await screenshot(page, "s7-beside-korean"); // Each view keeps its own scroll and cursor: the left one scrolls to the @@ -368,7 +368,7 @@ test("Open to the side shows one document twice: edits and Korean input reach bo await expect(left).toContainText("line 300-A"); expect(await scrollers[1]!.evaluate((node) => node.scrollTop)).toBe(0); expect(await scrollers[0]!.evaluate((node) => node.scrollTop)).toBeGreaterThan(1000); - await expect.poll(() => fs.readFileSync(shared, "utf8"), { timeout: 10_000 }).toBe( + await expect.poll(() => onDisk(shared), { timeout: 10_000 }).toBe( LONG.replace("line 001\n", "line 001-B 한글안녕-C\n").replace("line 300\n", "line 300-A\n"), ); @@ -412,7 +412,7 @@ test("Open to the side shows one document twice: edits and Korean input reach bo await tab(page, "shared.txt").getByRole("button", { name: /Close view/ }).click(); // The last view gone, the View areas leave and the panel keeps the Explorer. await expect(page.locator("[data-view-area]")).toHaveCount(0, { timeout: 10_000 }); - await expect.poll(() => fs.readFileSync(shared, "utf8").split("\n")[0]).toBe("line 001-B 한글안녕-C-D"); + await expect.poll(() => onDisk(shared)?.split("\n")[0]).toBe("line 001-B 한글안녕-C-D"); // Unsaved text this page never loaded (a background view after a reload) // is closed only by the operator's explicit Don't save, which sends diff --git a/web/e2e/wait.ts b/web/e2e/wait.ts index 6d4144beb4..54c3907f22 100644 --- a/web/e2e/wait.ts +++ b/web/e2e/wait.ts @@ -1,4 +1,5 @@ import type { Page } from "@playwright/test"; +import fs from "node:fs"; /** * Wait for the colour transitions and other finite animations the page has @@ -61,3 +62,18 @@ export async function measureFor(page: Page, milliseconds: number, why: string): void why; await sleep(page, milliseconds); } + +/** + * What a document the product saves holds on disk, or null while its name is + * absent. A Windows save replaces the file with `ReplaceFileW`, which leaves + * the name empty for a moment (`hide-host/src/save.rs`), so a poll reads that + * as not yet; any other read error still fails the poll. + */ +export function onDisk(file: string): string | null { + try { + return fs.readFileSync(file, "utf8"); + } catch (error) { + if ((error as NodeJS.ErrnoException).code === "ENOENT") return null; + throw error; + } +}