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: treat offloaded placeholder and wrong-typed collections as unreadable under strict (#2742)

[._.]/ Adam Eivye72553982d92
diff --git a/server/services/mortalLoomStore.js b/server/services/mortalLoomStore.js
index ed5a4517b3..e82febc0c4 100644
--- a/server/services/mortalLoomStore.js
+++ b/server/services/mortalLoomStore.js
@@ -339,7 +339,18 @@ async function readEnabledStore() {
* `ok: false` into an explicit `unavailable`.
*/
async function readStoreAtPathResult(path) {
- if (!existsSync(path)) return { present: false, ok: true, store: null };
+ 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 };
+ }
let unreadable = false;
const raw = await withTransientRetry(() => readFile(path, 'utf-8')).catch((err) => {
// existsSync→readFile race: file disappeared between the two calls.
@@ -554,8 +565,18 @@ export async function mlArrayIfEnabled(key, { strict = false } = {}) {
if (strict && enabled && !ok) {
throw new Error(`MortalLoom store unreadable for key: ${key}`);
}
- if (!store || !Array.isArray(store[key])) return null;
- return store[key];
+ if (!store) return null;
+ const value = store[key];
+ if (Array.isArray(value)) return value;
+ // A key that is PRESENT but not an array (`goals: {}`, `bodyEntries: "bad"`) is
+ // corruption, not the legitimate "no such records" of an omitted key — under
+ // strict it must surface as a failure rather than read as an omitted-key
+ // fall-through and score a fake 0 (codex #2742 review). Non-strict keeps falling
+ // through to the local mirror for both, as before.
+ if (strict && enabled && value !== undefined) {
+ throw new Error(`MortalLoom key ${key} is present but not an array`);
+ }
+ return null;
}
// === Daily-log composition (alcohol + nicotine records → day-keyed log) ===
@@ -602,7 +623,19 @@ export async function readDailyLogIfEnabled({ strict = false } = {}) {
if (strict && enabled && !ok) {
throw new Error('MortalLoom store unreadable for daily log');
}
- return store ? buildDailyLogFromMortalLoom(store) : null;
+ if (!store) return null;
+ // The daily log is composed from two arrays; buildDailyLogFromMortalLoom
+ // silently degrades a wrong-typed field to empty (a truthy string iterates as
+ // chars, etc.). Under strict a PRESENT-but-non-array source is corruption, not
+ // an empty day — throw rather than compose a fake-empty log (codex #2742 review).
+ if (strict && enabled) {
+ for (const key of ['alcoholDrinks', 'nicotineEntries']) {
+ if (store[key] !== undefined && !Array.isArray(store[key])) {
+ throw new Error(`MortalLoom ${key} is present but not an array`);
+ }
+ }
+ }
+ return buildDailyLogFromMortalLoom(store);
}
// === Status / import (used by Settings UI) ===
diff --git a/server/services/mortalLoomStore.test.js b/server/services/mortalLoomStore.test.js
index 89ae229056..db52658910 100644
--- a/server/services/mortalLoomStore.test.js
+++ b/server/services/mortalLoomStore.test.js
@@ -206,6 +206,31 @@ describe('mlArrayIfEnabled', () => {
await expect(store.mlArrayIfEnabled('goals', { strict: true })).resolves.toBeNull();
});
+ it('throws under strict when the key is PRESENT but not an array (corruption, not omitted)', async () => {
+ readFileMock.mockResolvedValue(JSON.stringify({ goals: { not: 'an array' } }));
+ await expect(store.mlArrayIfEnabled('goals', { strict: true }))
+ .rejects.toThrow(/not an array/i);
+ });
+
+ it('non-strict treats a present-but-non-array key as null (fall through, unchanged)', async () => {
+ readFileMock.mockResolvedValue(JSON.stringify({ goals: { not: 'an array' } }));
+ await expect(store.mlArrayIfEnabled('goals')).resolves.toBeNull();
+ });
+
+ 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);
+ 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);
+ await expect(store.mlArrayIfEnabled('goals')).resolves.toBeNull();
+ });
+
it('non-strict swallows an unreadable store (unchanged): returns null', async () => {
readFileMock.mockRejectedValue(Object.assign(new Error('denied'), { code: 'EACCES' }));
await expect(store.mlArrayIfEnabled('goals')).resolves.toBeNull();
@@ -229,6 +254,18 @@ describe('readDailyLogIfEnabled strict (#2742)', () => {
const result = await store.readDailyLogIfEnabled({ strict: true });
expect(result).toEqual({ entries: [], lastEntryDate: null });
});
+
+ it('throws under strict when a daily-log source is present but not an array', async () => {
+ // A truthy non-array (e.g. a string) would otherwise degrade to an empty log.
+ readFileMock.mockResolvedValue(JSON.stringify({ alcoholDrinks: 'nope' }));
+ await expect(store.readDailyLogIfEnabled({ strict: true })).rejects.toThrow(/not an array/i);
+ });
+
+ it('non-strict composes an empty log from a wrong-typed source (unchanged)', async () => {
+ readFileMock.mockResolvedValue(JSON.stringify({ alcoholDrinks: 'nope' }));
+ const result = await store.readDailyLogIfEnabled();
+ expect(result).toEqual({ entries: [], lastEntryDate: null });
+ });
});
describe('updateStore', () => {