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): drop malformed draft ids from synced works (#1565)

[._.]/ Adam Eivy9bfd5dd7a4a2
diff --git a/server/services/writersRoom/syncLogic.js b/server/services/writersRoom/syncLogic.js
index 5fc1126742..3f87ade96d 100644
--- a/server/services/writersRoom/syncLogic.js
+++ b/server/services/writersRoom/syncLogic.js
@@ -45,7 +45,19 @@ export function sanitizeWorkForSync(raw) {
const createdAt = isStr(raw.createdAt) ? raw.createdAt : new Date().toISOString();
const updatedAt = isStr(raw.updatedAt) ? raw.updatedAt : createdAt;
const { deleted, deletedAt } = sanitizeSoftDeleteFields(raw);
- return { ...raw, createdAt, updatedAt, deleted, deletedAt };
+ // 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).
+ 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);
+ }
+ return out;
}
/**
diff --git a/server/services/writersRoom/syncLogic.test.js b/server/services/writersRoom/syncLogic.test.js
index 5a574b5318..361ad6b0fa 100644
--- a/server/services/writersRoom/syncLogic.test.js
+++ b/server/services/writersRoom/syncLogic.test.js
@@ -62,6 +62,27 @@ describe('sanitizeWorkForSync', () => {
const s = sanitizeWorkForSync({ id: WORK, createdAt: '2026-03-01T00:00:00.000Z' });
expect(s.updatedAt).toBe('2026-03-01T00:00:00.000Z');
});
+
+ it('drops drafts with a malformed id and clamps the active pointer', () => {
+ const DRAFT2 = 'wr-draft-33333333-3333-3333-3333-333333333333';
+ const s = sanitizeWorkForSync(work({
+ activeDraftVersionId: '../etc/passwd',
+ drafts: [
+ { id: '../evil', label: 'bad' },
+ { id: DRAFT, label: 'Draft 1', contentFile: `drafts/${DRAFT}.md` },
+ { id: DRAFT2, label: 'Draft 2', contentFile: `drafts/${DRAFT2}.md` },
+ ],
+ }));
+ expect(s.drafts.map((d) => d.id)).toEqual([DRAFT, DRAFT2]); // malformed dropped
+ expect(s.activeDraftVersionId).toBe(DRAFT); // bad pointer clamped to first survivor
+ });
+
+ it('preserves a valid active pointer and nulls it when no drafts survive', () => {
+ expect(sanitizeWorkForSync(work()).activeDraftVersionId).toBe(DRAFT);
+ const empty = sanitizeWorkForSync(work({ drafts: [{ id: 'bad' }], activeDraftVersionId: 'bad' }));
+ expect(empty.drafts).toEqual([]);
+ expect(empty.activeDraftVersionId).toBeNull();
+ });
});
describe('mergeWorkRecord', () => {