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: read the MortalLoom store first and classify from errno, not a swallowed existsSync (#2742)

[._.]/ Adam Eivy068c5c65fde3
diff --git a/server/services/mortalLoomStore.js b/server/services/mortalLoomStore.js
index e82febc0c4..48af21349b 100644
--- a/server/services/mortalLoomStore.js
+++ b/server/services/mortalLoomStore.js
@@ -339,30 +339,34 @@ async function readEnabledStore() {
* `ok: false` into an explicit `unavailable`.
*/
async function readStoreAtPathResult(path) {
- if (!existsSync(path)) {
- // A legacy `.MortalLoom.json.icloud` placeholder shadowing the real path
- // means the store EXISTS but is offloaded under macOS's older rename-based
- // eviction (the same case updateStore's overwrite guard checks). Classifying
- // that as "absent" would let a strict read fall through to an empty local
- // mirror and score a fake 0 (codex #2742 review). Report it present-but-
- // unreadable instead. The read side never blocks on `brctl` to materialize it
- // (that stays updateStore's job) — it just refuses to report a trustworthy
- // empty for a store it has not actually read.
- if (existsSync(icloudPlaceholderPath(path))) return { present: true, ok: false, store: null };
- return { present: false, ok: true, store: null };
- }
+ // Attempt the read directly and classify from the error code rather than gating
+ // on `existsSync` first (codex #2742 review): `existsSync` swallows its errno —
+ // an un-traversable parent dir (EACCES) or an EIO returns `false`, which the old
+ // guard could not tell from a genuine ENOENT and reported as trustworthy-absent,
+ // letting a strict read fall through and score a fake 0. Reading first also
+ // closes the existsSync→readFile TOCTOU where iCloud offloads the file between
+ // the check and the read. `readFile`'s ENOENT is the ONLY genuine "absent".
let unreadable = false;
const raw = await withTransientRetry(() => readFile(path, 'utf-8')).catch((err) => {
- // existsSync→readFile race: file disappeared between the two calls.
- // Treat as "absent" silently — no warning noise, no failure.
- if (err.code === 'ENOENT') return null;
+ if (err.code === 'ENOENT') {
+ // Genuinely absent — UNLESS a legacy `.MortalLoom.json.icloud` placeholder
+ // shadows the real path, which means the store EXISTS but is offloaded under
+ // macOS's older rename-based eviction (the case updateStore's overwrite
+ // guard also checks). Treat that as present-but-unreadable so a strict read
+ // refuses to report a trustworthy empty for a store it has not actually
+ // read. This also covers an offload that races in after the read began.
+ // Silent either way — a missing store is normal, not warn-worthy. The read
+ // side never blocks on `brctl` to materialize (that stays updateStore's job).
+ if (existsSync(icloudPlaceholderPath(path))) unreadable = true;
+ return null;
+ }
+ // Any other error (EACCES/EIO/EAGAIN/EDEADLK/unknown errno) is a store we could
+ // not read — never a trustworthy empty.
console.warn(`⚠️ MortalLoom store unavailable (${err.code || err.errno || 'unknown'}): ${path}`);
unreadable = true;
return null;
});
if (raw === null || raw === undefined) {
- // null + !unreadable is the ENOENT race → absent; null + unreadable is a
- // real read failure that already warned above.
return unreadable
? { present: true, ok: false, store: null }
: { present: false, ok: true, store: null };
diff --git a/server/services/mortalLoomStore.test.js b/server/services/mortalLoomStore.test.js
index db52658910..6aa6d63226 100644
--- a/server/services/mortalLoomStore.test.js
+++ b/server/services/mortalLoomStore.test.js
@@ -84,11 +84,12 @@ afterEach(() => {
});
describe('readStore', () => {
- it('returns null when file does not exist', async () => {
- existsResult = false;
+ it('returns null when the store is absent (readFile ENOENT, no placeholder)', async () => {
+ readFileMock.mockRejectedValue(Object.assign(new Error('ENOENT'), { code: 'ENOENT' }));
+ existsResult = false; // no `.icloud` placeholder shadowing the path
const result = await store.readStore();
expect(result).toBeNull();
- expect(readFileMock).not.toHaveBeenCalled();
+ expect(console.warn).not.toHaveBeenCalled(); // a missing store is normal, not warn-worthy
});
it('returns parsed data on success', async () => {
@@ -182,9 +183,10 @@ describe('mlArrayIfEnabled', () => {
});
it('returns null (no throw) when the store is genuinely absent, even under strict', async () => {
- existsResult = false; // ENOENT: never written by either device → trustworthy empty
+ // ENOENT + no placeholder shadow: never written by either device → trustworthy empty.
+ readFileMock.mockRejectedValue(Object.assign(new Error('ENOENT'), { code: 'ENOENT' }));
+ existsResult = false;
await expect(store.mlArrayIfEnabled('goals', { strict: true })).resolves.toBeNull();
- expect(readFileMock).not.toHaveBeenCalled();
});
it('throws when enabled but the store read fails (EACCES) under strict', async () => {
@@ -218,16 +220,19 @@ describe('mlArrayIfEnabled', () => {
});
it('throws under strict when a legacy .icloud placeholder shadows an offloaded store', async () => {
- // Real path absent but the `.MortalLoom.json.icloud` placeholder present: the
- // store exists, just offloaded. Must not read as trustworthy-absent.
- existsQueue.push(false, true);
+ // readFile reports ENOENT (real path offloaded) but the `.MortalLoom.json.icloud`
+ // placeholder is present: the store exists, just evicted. Must not read as
+ // trustworthy-absent — the read is attempted first, then the placeholder probe
+ // reclassifies it as unreadable.
+ readFileMock.mockRejectedValue(Object.assign(new Error('ENOENT'), { code: 'ENOENT' }));
+ existsResult = true; // the placeholder existsSync probe returns true
await expect(store.mlArrayIfEnabled('goals', { strict: true }))
.rejects.toThrow(/unreadable/i);
- expect(readFileMock).not.toHaveBeenCalled();
});
it('non-strict treats a placeholder-shadowed store as null (fall through)', async () => {
- existsQueue.push(false, true);
+ readFileMock.mockRejectedValue(Object.assign(new Error('ENOENT'), { code: 'ENOENT' }));
+ existsResult = true;
await expect(store.mlArrayIfEnabled('goals')).resolves.toBeNull();
});