Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix([issue-3713]): measure unlink against a dataless iCloud file and pin deleteNote's no-guard decision

[._.]/ Adam Eivy97ac0809f3b9
diff --git a/server/lib/icloudFile.js b/server/lib/icloudFile.js
index d916efa786..4f3fd1abe3 100644
--- a/server/lib/icloudFile.js
+++ b/server/lib/icloudFile.js
@@ -31,13 +31,14 @@
* | `stat` | no | instant |
* | `open` — `'r'`, `'a'`, `'r+'` | no | 0 ms |
* | `rename` | no | 0–1 ms |
+ * | `unlink` | no | 0 ms |
* | `read` | **yes** | ~900 ms |
* | `write` after `O_TRUNC` (`'w'`) | **yes** | ~820 ms |
* | `write` in append mode (`'a'`) | **yes** | ~690 ms |
* | `link` (hard link) | **yes** | — |
* | `clonefile` (`cp -c`) | **yes** | — |
*
- * Two consequences that are easy to get wrong:
+ * Three consequences that are easy to get wrong:
*
* 1. **`O_TRUNC` does NOT short-circuit materialization.** Truncating to zero
* discards every byte the download would fetch, so "there is nothing to
@@ -49,10 +50,27 @@
* 2. **Screening is free.** `stat` and `open` never materialize, so a guard
* costs nothing on the healthy path — including `existsSync` precondition
* checks, which can stay exactly where they are.
+ * 3. **`unlink` is free — but that is a measurement, not an inference** (#3713).
+ * "Unlink only drops a directory entry, so there is nothing to download" is
+ * the same shape of argument as the `O_TRUNC` one above, and `link` is pure
+ * metadata too yet it *does* materialize. Measured instead: `unlinkSync` on a
+ * freshly-evicted file returned in **0.1 ms** at both 512 KB and 5 MB — three
+ * runs of three — against **884 ms** for a `read` of the same 5 MB dataless
+ * fixture, and no download was issued. So delete paths need no screen.
*
- * `unlink` is deliberately absent above: it is untested. Do not assume it is
- * free by analogy with `rename` — `link` is also pure metadata and it *does*
- * materialize.
+ * ## Reproducing these measurements
+ *
+ * Eviction is the hard part, and it is NOT unavailable to a CLI: `brctl evict
+ * <path>` works and is the mechanism used for all of the above. Like `brctl
+ * download` (which the heal path below uses) it is absent from `brctl --help`
+ * but fully functional; it refuses only when run as root. Everything else that
+ * looks like it should work does not — `fileproviderctl` has no `evict`,
+ * `chflags dataless` silently no-ops, and `FileManager.evictUbiquitousItem`
+ * fails `NSCocoaErrorDomain 512` from an unentitled process. Verify the state
+ * with `stat -f "blocks=%b flags=%Sf size=%z"`: evicted reads `blocks=0` and
+ * `compressed,dataless`. Always time the syscall in a CHILD process with a hard
+ * kill — a materialization that stalls cannot be cancelled — and use a fresh
+ * subject per case, since the first materializing call heals the file.
*
* ## The screen
*
diff --git a/server/services/obsidian.js b/server/services/obsidian.js
index 653a925d42..de9ae9cef3 100644
--- a/server/services/obsidian.js
+++ b/server/services/obsidian.js
@@ -392,6 +392,15 @@ export async function deleteNote(vaultId, notePath) {
if (!fullPath) return { error: 'INVALID_PATH' };
if (!existsSync(fullPath)) return { error: 'NOTE_NOT_FOUND' };
+ // No dataless screen here, deliberately (#3713): `unlink` does NOT materialize
+ // an evicted vnode. That is measured, not inferred from POSIX — `link` is pure
+ // metadata too and it *does* materialize, so the analogy with `rename` was not
+ // safe to lean on. On a freshly-evicted iCloud file `unlinkSync` returned in
+ // 0.1 ms at both 512 KB and 5 MB, versus 884 ms to `read` the same 5 MB
+ // dataless fixture. Deleting an offloaded note therefore cannot wedge the libuv
+ // threadpool the way `updateNote`'s overwrite could, and guarding it would be
+ // strictly worse than useless: the guard's own remedy is `materializeAndWait`,
+ // i.e. downloading every byte of a file purely to throw it away.
await unlink(fullPath);
console.log(`📓 Deleted note: ${notePath} from vault ${vault.name}`);
return true;
diff --git a/server/services/obsidianEvicted.test.js b/server/services/obsidianEvicted.test.js
index aa8268bdf9..3c98b04873 100644
--- a/server/services/obsidianEvicted.test.js
+++ b/server/services/obsidianEvicted.test.js
@@ -14,7 +14,7 @@
*/
import { describe, it, expect, beforeEach, afterAll, vi } from 'vitest';
-import { mkdirSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'fs';
+import { existsSync, mkdirSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'fs';
import { readFile } from 'fs/promises';
import { join } from 'path';
import { createTempDataRoot, makePathsProxy } from '../lib/mockPathsDataRoot.js';
@@ -42,8 +42,10 @@ vi.mock('../lib/icloudFile.js', () => ({
materializeAndWait: vi.fn(async () => true),
}));
-const { addVault, scanVault, searchNotes, getVaultTags, getVaultGraph, getNote, updateNote, createNote, upsertNote } =
- await import('./obsidian.js');
+const {
+ addVault, scanVault, searchNotes, getVaultTags, getVaultGraph,
+ getNote, updateNote, createNote, upsertNote, deleteNote
+} = await import('./obsidian.js');
const VAULT_DIR = join(tempRoot, 'vault');
let vaultId;
@@ -224,3 +226,41 @@ describe('obsidian updateNote against an evicted note', () => {
expect(readFileSync(join(VAULT_DIR, NOTE), 'utf-8')).toBe(ORIGINAL);
});
});
+
+/**
+ * The DELETE side — #3713.
+ *
+ * `deleteNote` was the third candidate for the #3706 treatment, on the theory
+ * that `unlink` might materialize the way `link` does. Measured: it does not
+ * (0.1 ms dataless at 512 KB and 5 MB, vs 884 ms to read the same 5 MB dataless
+ * fixture), so the correct outcome is *no guard*. These pin that absence — a
+ * "for symmetry with updateNote" change has to argue with a test, because the
+ * guard it would add is actively harmful: `materializeAndWait` would download
+ * the whole file just to unlink it.
+ */
+describe('obsidian deleteNote against an evicted note', () => {
+ beforeEach(async () => {
+ const icloud = await import('../lib/icloudFile.js');
+ icloud.isSuspectedDataless.mockReset().mockResolvedValue(true);
+ icloud.materializeAndWait.mockReset().mockResolvedValue(true);
+ });
+
+ it('deletes an evicted note outright — no screen, no download', async () => {
+ const icloud = await import('../lib/icloudFile.js');
+
+ // The screen is mocked to report EVERY path dataless; the delete must still
+ // go through, because the unlink it issues cannot block.
+ await expect(deleteNote(vaultId, 'evicted.md')).resolves.toBe(true);
+
+ expect(existsSync(join(VAULT_DIR, 'evicted.md'))).toBe(false);
+ expect(icloud.isSuspectedDataless).not.toHaveBeenCalled();
+ expect(icloud.materializeAndWait).not.toHaveBeenCalled();
+ });
+
+ it('still reports NOTE_NOT_FOUND for a path with nothing at it', async () => {
+ // The existsSync precondition is a stat, which never materializes, so it
+ // keeps working unchanged on an evicted vault.
+ const result = await deleteNote(vaultId, 'no-such-note.md');
+ expect(result.error).toBe('NOTE_NOT_FOUND');
+ });
+});