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: guard object-shape assumptions broken by safeJSONParse now parsing scalars (#6181)

Adam Eivy913c1d2e6877
diff --git a/server/lib/hostShutdown.js b/server/lib/hostShutdown.js
index 11a8c626dc..193db5086b 100644
--- a/server/lib/hostShutdown.js
+++ b/server/lib/hostShutdown.js
@@ -122,13 +122,13 @@ export async function writeHostShutdownMarker({ agentIds = [], signal = null } =
* `agentIds` is ALWAYS an array of non-empty strings — that's the one field
* with a production consumer, and boot recovery runs before everything else, so
* a truncated/garbled marker has to degrade to "no agents were interrupted"
- * rather than throw. `allowArray: false` does the rest of the shape check:
- * `readJSONFile` already rejects an unparseable file AND any root that isn't a
- * `{…}` object, so a falsy result is the only failure left to test for.
+ * rather than throw. `allowArray: false` only rejects an unparseable file or a
+ * root array; a bare JSON scalar (a truncated/garbled marker) still parses, so
+ * this also checks `typeof raw === 'object'` before treating it as a marker.
*/
export async function readHostShutdownMarker() {
const raw = await readJSONFile(hostShutdownMarkerPath(), null, { logError: false, allowArray: false });
- if (!raw) return null;
+ if (!raw || typeof raw !== 'object') return null;
return { ...raw, agentIds: Array.isArray(raw.agentIds) ? raw.agentIds.filter(isNonEmptyString) : [] };
}
diff --git a/server/scripts/migrateWritersRoomToDB.js b/server/scripts/migrateWritersRoomToDB.js
index 9f2068b290..e43763c647 100644
--- a/server/scripts/migrateWritersRoomToDB.js
+++ b/server/scripts/migrateWritersRoomToDB.js
@@ -189,7 +189,10 @@ export async function migrateWritersRoomToDB() {
const content = await readFile(manifestPath, 'utf-8').catch(() => null);
if (content === null) continue;
const manifest = safeJSONParse(content, null, { allowArray: false, logError: true, context: manifestPath });
- if (!manifest) continue; // corrupted manifest — skip, leave on disk untouched
+ // `allowArray: false` only rejects a root array — a bare JSON scalar
+ // still parses, so also guard for a genuine object before treating the
+ // manifest as valid.
+ if (!manifest || typeof manifest !== 'object') continue; // corrupted manifest — skip, leave on disk untouched
if (await importWork(manifest)) workCount += 1;
importedManifestPaths.push(manifestPath);
}
diff --git a/server/services/digital-twin-analysis.js b/server/services/digital-twin-analysis.js
index 05b6d0c3a3..71b3509e5a 100644
--- a/server/services/digital-twin-analysis.js
+++ b/server/services/digital-twin-analysis.js
@@ -411,7 +411,10 @@ export function parseTraitsResponse(response) {
}
const parsed = safeJSONParse(jsonStr, null, { allowArray: false });
- if (!parsed) {
+ // A fenced ```json block can wrap a bare scalar (e.g. the model responds
+ // with just `42`) — `allowArray: false` only rejects a root array, so guard
+ // for a genuine object before treating it as a traits response.
+ if (!parsed || typeof parsed !== 'object') {
return { error: 'Failed to parse traits response - invalid JSON', rawResponse: response };
}
diff --git a/server/services/meatspacePostMorse.js b/server/services/meatspacePostMorse.js
index 828e2c0de2..967f0d1df1 100644
--- a/server/services/meatspacePostMorse.js
+++ b/server/services/meatspacePostMorse.js
@@ -52,11 +52,15 @@ function clampLevel(level) {
}
async function loadMorseProgress() {
- const data = await readJSONFile(
+ const raw = await readJSONFile(
MORSE_FILE,
{ kochLevel: null, settings: null, rounds: [] },
{ allowArray: false },
);
+ // `allowArray: false` only rejects a root array — a bare JSON scalar
+ // (corrupted file) still parses, and the field assignments below would
+ // throw on one, so fall back to a fresh object rather than that scalar.
+ const data = raw && typeof raw === 'object' ? raw : { kochLevel: null, settings: null, rounds: [] };
if (!Array.isArray(data.rounds)) data.rounds = [];
// Preserve the "never set" sentinel: anything that isn't a real number stays
// null so adopt-once keeps working across older/partial files.
diff --git a/server/services/pipeline/editorialAnalysis.js b/server/services/pipeline/editorialAnalysis.js
index 6457ad04ae..363ba03a27 100644
--- a/server/services/pipeline/editorialAnalysis.js
+++ b/server/services/pipeline/editorialAnalysis.js
@@ -151,7 +151,11 @@ function sanitizeAnalysis(parsed) {
async function loadSnapshot(issueId) {
const content = await tryReadFile(snapshotPath(issueId));
if (content === null) return null;
- return safeJSONParse(content, null, { allowArray: false, logError: true, context: snapshotPath(issueId) });
+ const parsed = safeJSONParse(content, null, { allowArray: false, logError: true, context: snapshotPath(issueId) });
+ // `allowArray: false` only rejects a root array — a bare JSON scalar
+ // (corrupted snapshot) still parses, and callers spread this into a
+ // response object, so guard for a genuine object here.
+ return parsed && typeof parsed === 'object' ? parsed : null;
}
async function saveSnapshot(snapshot) {
diff --git a/server/services/pipeline/foundationJudge.js b/server/services/pipeline/foundationJudge.js
index 1b10f439f4..12517924d3 100644
--- a/server/services/pipeline/foundationJudge.js
+++ b/server/services/pipeline/foundationJudge.js
@@ -323,7 +323,11 @@ export function residualFindings(dimensions) {
async function loadSnapshot(seriesId) {
const content = await tryReadFile(snapshotPath(seriesId));
if (content === null) return null;
- return safeJSONParse(content, null, { allowArray: false, logError: true, context: snapshotPath(seriesId) });
+ const parsed = safeJSONParse(content, null, { allowArray: false, logError: true, context: snapshotPath(seriesId) });
+ // `allowArray: false` only rejects a root array — a bare JSON scalar
+ // (corrupted snapshot) still parses, and callers spread this into a
+ // response object, so guard for a genuine object here.
+ return parsed && typeof parsed === 'object' ? parsed : null;
}
async function saveSnapshot(snapshot) {
diff --git a/server/services/pipeline/perspectiveRewrite.js b/server/services/pipeline/perspectiveRewrite.js
index 682502ba1a..0feb39657c 100644
--- a/server/services/pipeline/perspectiveRewrite.js
+++ b/server/services/pipeline/perspectiveRewrite.js
@@ -171,7 +171,11 @@ export function sanitizeAnalysis(parsed) {
async function loadDoc(issueId) {
const content = await tryReadFile(docPath(issueId));
if (content === null) return null;
- return safeJSONParse(content, null, { allowArray: false, logError: true, context: docPath(issueId) });
+ const parsed = safeJSONParse(content, null, { allowArray: false, logError: true, context: docPath(issueId) });
+ // `allowArray: false` only rejects a root array — a bare JSON scalar
+ // (corrupted doc) still parses, and callers spread this into a response
+ // object, so guard for a genuine object here.
+ return parsed && typeof parsed === 'object' ? parsed : null;
}
async function saveDoc(doc) {
diff --git a/server/services/pipeline/pipelineJudge.js b/server/services/pipeline/pipelineJudge.js
index 9458972d48..a915bc50a4 100644
--- a/server/services/pipeline/pipelineJudge.js
+++ b/server/services/pipeline/pipelineJudge.js
@@ -175,7 +175,11 @@ export function sanitizeJudge(parsed) {
async function loadSnapshot(issueId) {
const content = await tryReadFile(snapshotPath(issueId));
if (content === null) return null;
- return safeJSONParse(content, null, { allowArray: false, logError: true, context: snapshotPath(issueId) });
+ const parsed = safeJSONParse(content, null, { allowArray: false, logError: true, context: snapshotPath(issueId) });
+ // `allowArray: false` only rejects a root array — a bare JSON scalar
+ // (corrupted snapshot) still parses, and callers spread this into a
+ // response object, so guard for a genuine object here.
+ return parsed && typeof parsed === 'object' ? parsed : null;
}
async function saveSnapshot(snapshot) {
diff --git a/server/services/pipeline/readerPanel.js b/server/services/pipeline/readerPanel.js
index b1ba6ec2d4..d5c69c575e 100644
--- a/server/services/pipeline/readerPanel.js
+++ b/server/services/pipeline/readerPanel.js
@@ -48,7 +48,11 @@ const snapshotPath = (seriesId) => join(panelDir(), `${seriesId}.json`);
async function loadSnapshot(seriesId) {
const content = await tryReadFile(snapshotPath(seriesId));
if (content === null) return null;
- return safeJSONParse(content, null, { allowArray: false, logError: true, context: snapshotPath(seriesId) });
+ const parsed = safeJSONParse(content, null, { allowArray: false, logError: true, context: snapshotPath(seriesId) });
+ // `allowArray: false` only rejects a root array — a bare JSON scalar
+ // (corrupted snapshot) still parses, and callers spread this into a
+ // response object, so guard for a genuine object here.
+ return parsed && typeof parsed === 'object' ? parsed : null;
}
async function saveSnapshot(snapshot) {
diff --git a/server/services/pipeline/seriesReview.js b/server/services/pipeline/seriesReview.js
index ac3ea9c11e..3292a242de 100644
--- a/server/services/pipeline/seriesReview.js
+++ b/server/services/pipeline/seriesReview.js
@@ -572,9 +572,13 @@ async function clearSnapshot(seriesId) {
export async function getSeriesReview(seriesId) {
assertValidSeriesId(seriesId);
const content = await tryReadFile(snapshotPath(seriesId));
- const verdict = content === null
+ const parsed = content === null
? null
: safeJSONParse(content, null, { allowArray: false, logError: true, context: snapshotPath(seriesId) });
+ // `allowArray: false` only rejects a root array — a bare JSON scalar
+ // (corrupt snapshot) still parses, so guard for a genuine object before
+ // treating it as a verdict.
+ const verdict = parsed && typeof parsed === 'object' ? parsed : null;
const [fix, review, currentHash] = await Promise.all([
getFixAvailability(),
// `null` = the findings store couldn't be read (distinct from a store with no
diff --git a/server/services/writersRoom/evaluator.js b/server/services/writersRoom/evaluator.js
index c7b38d7fd8..db35e9d15b 100644
--- a/server/services/writersRoom/evaluator.js
+++ b/server/services/writersRoom/evaluator.js
@@ -145,7 +145,11 @@ async function loadAnalysis(workId, id) {
throw err;
});
if (content === null) return null;
- return safeJSONParse(content, null, { allowArray: false, logError: true, context: analysisPath(workId, id) });
+ const parsed = safeJSONParse(content, null, { allowArray: false, logError: true, context: analysisPath(workId, id) });
+ // `allowArray: false` only rejects a root array — a bare JSON scalar
+ // (corrupted analysis) still parses, and callers spread this into a
+ // response object, so guard for a genuine object here.
+ return parsed && typeof parsed === 'object' ? parsed : null;
}
async function saveAnalysis(workId, snapshot) {
@@ -247,7 +251,9 @@ async function migrateLegacyAnalyses(workId) {
const content = await tryReadFile(path);
if (content === null) return null;
const parsed = safeJSONParse(content, null, { allowArray: false, logError: true, context: path });
- return parsed ? { id, snapshot: parsed } : null;
+ // `allowArray: false` only rejects a root array — a bare JSON scalar
+ // (corrupted legacy file) still parses, so also guard the shape here.
+ return parsed && typeof parsed === 'object' ? { id, snapshot: parsed } : null;
}))).filter(Boolean);
const latestPerKind = new Map();
diff --git a/server/services/writersRoom/polish.js b/server/services/writersRoom/polish.js
index 5feea0111a..15b2f472bf 100644
--- a/server/services/writersRoom/polish.js
+++ b/server/services/writersRoom/polish.js
@@ -213,7 +213,11 @@ async function loadSnapshot(workId, id) {
throw err;
});
if (content === null) return null;
- return safeJSONParse(content, null, { allowArray: false, logError: true, context: snapshotPath(workId, id) });
+ const parsed = safeJSONParse(content, null, { allowArray: false, logError: true, context: snapshotPath(workId, id) });
+ // `allowArray: false` only rejects a root array — a bare JSON scalar
+ // (corrupted snapshot) still parses, and callers use this as a manifest,
+ // so guard for a genuine object here.
+ return parsed && typeof parsed === 'object' ? parsed : null;
}
export async function getSnapshot(workId, id) {
diff --git a/server/services/writersRoom/store.js b/server/services/writersRoom/store.js
index b1f0f6b69d..aa9de755f8 100644
--- a/server/services/writersRoom/store.js
+++ b/server/services/writersRoom/store.js
@@ -78,7 +78,9 @@ function makeFileBackend() {
// pre-#1017 file behavior) so listWorks can drop the work without masking
// the issue and direct callers get an actionable error, not a raw SyntaxError.
const parsed = safeJSONParse(content, null, { allowArray: false, logError: true, context: path });
- if (parsed === null) {
+ // `allowArray: false` only rejects a root array — a bare JSON scalar
+ // (corrupted manifest) still parses, so also check the shape here.
+ if (parsed === null || typeof parsed !== 'object') {
console.warn(`⚠️ wr: corrupted manifest at ${path} (work ${workId})`);
throw new ServerError(`Corrupted writers-room manifest for ${workId}`, {
status: 500, code: 'CORRUPTED_MANIFEST', context: { workId },