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(writers-room): post-merge Copilot round 9 fixes (path leak + whitespace + version-switch consistency)

Adam Eivyba8f7884c9ed
diff --git a/client/src/components/writers-room/WorkEditor.jsx b/client/src/components/writers-room/WorkEditor.jsx
index 17a57dfc0c..6472c1ad0f 100644
--- a/client/src/components/writers-room/WorkEditor.jsx
+++ b/client/src/components/writers-room/WorkEditor.jsx
@@ -143,8 +143,9 @@ export default function WorkEditor({ work, onChange }) {
return null;
});
if (!updated || !mountedRef.current) return;
- // Reload via parent so body comes from the server's active version
- onChange?.(updated, { reload: true });
+ // PATCH /works/:id/versions/:draftId now returns manifest + activeDraftBody
+ // in one round-trip, so no separate reload is needed.
+ onChange?.(updated);
};
const activeDraft = useMemo(
diff --git a/client/src/pages/WritersRoom.jsx b/client/src/pages/WritersRoom.jsx
index 55ed828100..8b376344d4 100644
--- a/client/src/pages/WritersRoom.jsx
+++ b/client/src/pages/WritersRoom.jsx
@@ -4,7 +4,6 @@ import { NotebookPen, Timer } from 'lucide-react';
import LibraryPane from '../components/writers-room/LibraryPane';
import WorkEditor from '../components/writers-room/WorkEditor';
import ExercisePanel from '../components/writers-room/ExercisePanel';
-import toast from '../components/ui/Toast';
import {
listWritersRoomFolders,
listWritersRoomWorks,
@@ -60,39 +59,31 @@ export default function WritersRoom() {
navigate(`/writers-room/works/${id}`);
};
- const handleWorkChange = async (updated, opts = {}) => {
- let next = updated;
- if (opts.reload) {
- const fresh = await getWritersRoomWork(updated.id).catch(() => null);
- if (!mountedRef.current) return;
- if (!fresh) {
- // Reload failed — `updated` came from setWritersRoomActiveDraft and
- // lacks activeDraftBody, so setActiveWork(updated) would blank the
- // editor. Skip the swap; the previous activeWork (and its body) stay
- // visible and the user can retry the version-switch click.
- toast.error('Could not load that draft version');
- return;
- }
- next = fresh;
- }
- setActiveWork(next);
+ const handleWorkChange = (updated) => {
+ // Caller (WorkEditor) hands us the freshest manifest + activeDraftBody —
+ // the server-side endpoints (PUT draft, POST snapshot, PATCH version,
+ // PATCH work) all return the full shape now, so no separate reload is
+ // required and there's no inconsistency window between server pointer
+ // and client view.
+ if (!mountedRef.current) return;
+ setActiveWork(updated);
// Splice the updated row into the library list (title / status / word
// count) without refetching N manifests on every save.
setWorks((prev) => {
- const activeDraft = (next.drafts || []).find((d) => d.id === next.activeDraftVersionId);
+ const activeDraft = (updated.drafts || []).find((d) => d.id === updated.activeDraftVersionId);
const summary = {
- id: next.id,
- folderId: next.folderId,
- title: next.title,
- kind: next.kind,
- status: next.status,
- activeDraftVersionId: next.activeDraftVersionId,
+ id: updated.id,
+ folderId: updated.folderId,
+ title: updated.title,
+ kind: updated.kind,
+ status: updated.status,
+ activeDraftVersionId: updated.activeDraftVersionId,
wordCount: activeDraft?.wordCount ?? 0,
- draftCount: (next.drafts || []).length,
- createdAt: next.createdAt,
- updatedAt: next.updatedAt,
+ draftCount: (updated.drafts || []).length,
+ createdAt: updated.createdAt,
+ updatedAt: updated.updatedAt,
};
- const idx = prev.findIndex((w) => w.id === next.id);
+ const idx = prev.findIndex((w) => w.id === updated.id);
const merged = idx < 0 ? summary : { ...prev[idx], ...summary };
const others = idx < 0 ? prev : [...prev.slice(0, idx), ...prev.slice(idx + 1)];
// Library is sorted by updatedAt desc — re-sort after the merge so a
diff --git a/server/routes/writersRoom.js b/server/routes/writersRoom.js
index 76c6100410..999183a7bf 100644
--- a/server/routes/writersRoom.js
+++ b/server/routes/writersRoom.js
@@ -78,9 +78,13 @@ router.post('/works/:id/versions', asyncHandler(async (req, res) => {
}));
router.patch('/works/:id/versions/:draftId', asyncHandler(async (req, res) => {
- // Only one operation supported here today: set the active draft pointer.
- // Routes stays a PATCH so future label/contentHash updates can land cleanly.
- res.json(await setActiveDraft(req.params.id, req.params.draftId));
+ // Set the active draft pointer AND return the new active body in the same
+ // response. This collapses what used to be a PATCH-then-GET round-trip on
+ // the client and eliminates the inconsistency window where the server's
+ // active pointer had advanced but the client still showed the old body.
+ await setActiveDraft(req.params.id, req.params.draftId);
+ const { manifest, body } = await getWorkWithBody(req.params.id);
+ res.json({ ...manifest, activeDraftBody: body });
}));
router.get('/works/:id/versions/:draftId', asyncHandler(async (req, res) => {
diff --git a/server/services/writersRoom/local.js b/server/services/writersRoom/local.js
index 88cf442459..2aae1b1bbd 100644
--- a/server/services/writersRoom/local.js
+++ b/server/services/writersRoom/local.js
@@ -85,7 +85,10 @@ function segId(seq) {
}
export function buildSegmentIndex(text) {
- if (!text) return [];
+ // Whitespace-only counts as empty too — otherwise ' ' would emit a
+ // phantom "(untitled)" segment in the outline panel of a brand-new draft
+ // where the user has only hit space/enter a couple times.
+ if (!text || !String(text).trim()) return [];
const headingRe = /^(#{1,3})\s+(.+)$/gm;
const matches = [];
let m;
@@ -181,13 +184,14 @@ async function loadManifest(workId) {
// SyntaxError bubbling out of JSON.parse.
const parsed = safeJSONParse(content, null, { allowArray: false, logError: true, context: path });
if (parsed === null) {
- // Don't leak the on-disk path to the client; keep it in context for the
- // server logs (errorHandler emits context to logs but only message + code
- // to the response body).
+ // Log the absolute path server-side for debugging, but keep it OUT of
+ // the ServerError — errorHandler ships both message AND context to the
+ // client response body, so anything in context leaks to the UI.
+ 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, path },
+ context: { workId },
});
}
return parsed;