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

address review (codex): coerce non-array drafts to [] in synced works too (#1565)

[._.]/ Adam Eivybdb8f854f351
diff --git a/server/services/writersRoom/syncLogic.js b/server/services/writersRoom/syncLogic.js
index 3f87ade96d..f2cc4ef38f 100644
--- a/server/services/writersRoom/syncLogic.js
+++ b/server/services/writersRoom/syncLogic.js
@@ -46,17 +46,20 @@ export function sanitizeWorkForSync(raw) {
const updatedAt = isStr(raw.updatedAt) ? raw.updatedAt : createdAt;
const { deleted, deletedAt } = sanitizeSoftDeleteFields(raw);
// Draft-version ids are ALSO filesystem path segments (drafts/<draftId>.md), so
- // drop any draft whose id isn't DRAFT_ID_RE-valid — otherwise a malformed peer
- // payload persists a work whose body read throws in wrDraftPath() and can't be
- // opened locally. Clamp the active pointer to a surviving draft (or null).
+ // ALWAYS normalize to a filtered array — drop any draft whose id isn't
+ // DRAFT_ID_RE-valid (and coerce a missing/non-array `drafts` to []). Otherwise
+ // a malformed peer payload persists a work whose body read throws in
+ // wrDraftPath(), or whose `manifest.drafts` isn't the array local readers
+ // assume. Clamp the active pointer to a surviving draft (or null). Legitimate
+ // works (live or tombstone) always carry a drafts[] array, so this is a no-op
+ // for them — only malformed input is reshaped.
const out = { ...raw, createdAt, updatedAt, deleted, deletedAt };
- if (Array.isArray(raw.drafts)) {
- out.drafts = raw.drafts.filter((d) => d && typeof d === 'object' && isStr(d.id) && DRAFT_ID_RE.test(d.id));
- const ids = new Set(out.drafts.map((d) => d.id));
- out.activeDraftVersionId = isStr(raw.activeDraftVersionId) && ids.has(raw.activeDraftVersionId)
- ? raw.activeDraftVersionId
- : (out.drafts[0]?.id ?? null);
- }
+ const draftsArray = Array.isArray(raw.drafts) ? raw.drafts : [];
+ out.drafts = draftsArray.filter((d) => d && typeof d === 'object' && isStr(d.id) && DRAFT_ID_RE.test(d.id));
+ const ids = new Set(out.drafts.map((d) => d.id));
+ out.activeDraftVersionId = isStr(raw.activeDraftVersionId) && ids.has(raw.activeDraftVersionId)
+ ? raw.activeDraftVersionId
+ : (out.drafts[0]?.id ?? null);
return out;
}
diff --git a/server/services/writersRoom/syncLogic.test.js b/server/services/writersRoom/syncLogic.test.js
index 361ad6b0fa..cc055714bb 100644
--- a/server/services/writersRoom/syncLogic.test.js
+++ b/server/services/writersRoom/syncLogic.test.js
@@ -83,6 +83,13 @@ describe('sanitizeWorkForSync', () => {
expect(empty.drafts).toEqual([]);
expect(empty.activeDraftVersionId).toBeNull();
});
+
+ it('coerces a missing/non-array drafts field to [] (malformed payload)', () => {
+ const noDrafts = sanitizeWorkForSync({ id: WORK, createdAt: '2026-03-01T00:00:00.000Z', activeDraftVersionId: '../x' });
+ expect(noDrafts.drafts).toEqual([]);
+ expect(noDrafts.activeDraftVersionId).toBeNull();
+ expect(sanitizeWorkForSync(work({ drafts: 'not-an-array' })).drafts).toEqual([]);
+ });
});
describe('mergeWorkRecord', () => {