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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: keep array-defaulted readJSONFile callers degrading to a list, not an object root (#2726)
[._.]/ Adam Eivy409c62e94415
diff --git a/PLAN.md b/PLAN.mdindex 447d556218..bfb47cb6f8 100644--- a/PLAN.md+++ b/PLAN.md@@ -195,6 +195,7 @@ by `/do:replan --issues`:### Strict-read follow-ups (#2726)- [ ] **De-dup the three identical `daily-log.json` readers.** `server/services/meatspaceAlcohol.js` (`loadDailyLog`, ~line 142) and `server/services/meatspaceNicotine.js` (`loadDailyLog`, ~line 101) are now character-for-character identical apart from the error label — same `readDailyLogIfEnabled()` MortalLoom probe, same `{ entries: [], lastEntryDate: null }` default, same shape validation — and `server/services/meatspaceHealth.js#getBodyHistory` reads *the same file* (`join(PATHS.meatspace, 'daily-log.json')`) with a third, slightly different inline read. All three now carry a parallel `{ strict }` branch, so the duplication tripled in this PR rather than being introduced by it. Fix shape: extract one `loadMeatspaceDailyLog({ strict })` (a new `server/services/meatspaceDailyLog.js`, or an export from one of the three) and have all three delegate. Deferred from #2726 because each of the three modules' test suites mocks `readJSONFile` at its own module boundary, so the extraction needs those suites re-pointed at the shared module in the same change — a mechanical but wide edit that would have buried the actual strict-read fix under test churn.+- [ ] **MortalLoom store reads conflate "disabled" with "unreadable" — tracked as #2742.** `readEnabledStore()` (`server/services/mortalLoomStore.js`) returns `null` for both, so the five health loaders + `identity/store.js#loadJSON` fall through to the local file on an unreadable ML store — and on an iOS-logging install the local file is a genuine ENOENT, so #2726's strict opt-in is a no-op and Vitalist still reports a fake level 0. Deferred from #2726: it needs an `{ enabled, ok, store }` rework of every `ml*` helper, and one semantic question answered first (is "store readable but key absent" a legitimate empty or a fallthrough?). Full plan in #2742.- [ ] **Audit the remaining `readJSONFile` callers for the same absent-vs-unreadable conflation.** #2726 explicitly scoped itself to the Character sheet's file-backed signals (`meatspacePost`, `meatspacePostTraining`, `identity/goals`, `meatspaceLoggingStats` + its five domain loaders) and left the rest of the ~100 `readJSONFile` call sites alone. `readJSONFileStrict` / `readJSONFile(…, { strict: true })` now exist in `server/lib/fileUtils.js` for any of them. The ones worth checking first are those whose result is COUNTED or shown as a stat (rather than used as a config fallback, where swallowing is correct) — grep for `readJSONFile` results feeding a `.length`, a `reduce`, or a dashboard/widget number. Also note `tryReadFile` has the same conflation (bare `.catch(() => null)`) and no strict counterpart.### CoS stat-card follow-upsdiff --git a/server/lib/fileUtils.js b/server/lib/fileUtils.jsindex ec5b0891aa..a5b11a62dd 100644--- a/server/lib/fileUtils.js+++ b/server/lib/fileUtils.js@@ -483,6 +483,20 @@ export async function readJSONFileStrict(filePath, defaultValue = null, { allowAconst parsed = safeJSONParse(text, PARSE_FAILED, { allowArray, logError, context: filePath });if (parsed === PARSE_FAILED) return { ok: false, value: defaultValue };++ // An array `defaultValue` is the caller declaring "I expect a list" — every such+ // caller in-tree goes straight to `.filter`/`.find`/`.findIndex` on the result. A+ // parsed object root therefore isn't a usable read: swallowing callers must still+ // get their list back (a hand-edited or legacy-shaped file used to degrade to `[]`+ // here, via the manufactured-'[]' path above, and would now TypeError instead), and+ // a strict caller must refuse to count a shape it cannot count.+ if (allowArray && Array.isArray(defaultValue) && !Array.isArray(parsed)) {+ if (logError) {+ console.warn(`Expected a JSON array in ${filePath}, got ${parsed === null ? 'null' : typeof parsed}`);+ }+ return { ok: false, value: defaultValue };+ }+return { ok: true, value: parsed };}diff --git a/server/lib/fileUtils.test.js b/server/lib/fileUtils.test.jsindex edb24804b7..8dce9022c6 100644--- a/server/lib/fileUtils.test.js+++ b/server/lib/fileUtils.test.js@@ -310,6 +310,17 @@ describe('fileUtils', () => {expect(result).toEqual({});});+ it('returns the array default when an array-defaulted file holds an object root', async () => {+ // Regression guard: every array-defaulted caller in-tree goes straight to+ // .filter/.find on the result (services/review.js#getItems,+ // services/videoGen/local.js, routes/videoGen.js), so handing back a parsed+ // object root would TypeError a request that used to degrade to an empty list.+ const filePath = join(testDir, 'object-root.json');+ await writeFile(filePath, '{"a":1}');++ expect(await readJSONFile(filePath, [])).toEqual([]);+ });+it('returns the caller’s array default for garbage, not a manufactured []', async () => {// The noisy-output extraction used to manufacture a literal '[]' from text// holding no array, so a non-empty array default was silently replaced by an@@ -430,6 +441,15 @@ describe('fileUtils', () => {expect(await readJSONFileStrict(filePath, [])).toEqual({ ok: true, value: [{ id: 1 }] });});+ it('reports an object root as NOT ok when the caller declared an array default', async () => {+ // The array default is a declared shape expectation — a strict caller counting+ // a list must refuse an object it cannot count, rather than trusting it.+ const filePath = join(testDir, 'object-root.json');+ await writeFile(filePath, '{"sessions":[1,2]}');++ expect(await readJSONFileStrict(filePath, [])).toEqual({ ok: false, value: [] });+ });+it('does NOT manufacture a trustworthy empty from noise holding no array', async () => {// extractJSONArray returns a literal '[]' when it finds nothing, which parses// cleanly — an array-defaulted strict read would otherwise report corrupt bytes