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): preserve apostrophes in the reformat integrity skeleton

[._.]/ Adam Eivy03d24b4465d8
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index 175b1efa8d..f5a1c69151 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -6,7 +6,7 @@
- **"Format" button on each manuscript section cleans up pasted-PDF artifacts.** Text pasted out of a PDF export arrives hard-wrapped at the page margin (every visual line a real `\n`), with stylized drop-caps split onto their own line ("T\nhe dawn …"), words hyphenated across the wrap ("approxi-\nmating"), and closing quotes orphaned from the text they terminate (alone on a line, wrapped to the next line, or with a stray space before them) — so a paragraph reads as a column of ragged lines. A new **Format** button in each section's header (both Live and Review modes) normalizes the section: it rejoins drop-caps, repairs hyphen splits, strips trailing whitespace, and collapses runaway blank lines for every stage, and — for **prose** only — re-attaches orphaned closing quotes, drops a stray opening-quote fragment the export duplicated onto its own line (`"I\n"I need …` → `"I need …`, guarded so genuine back-to-back dialogue is untouched), and reflows the soft-wrapped lines back into paragraphs (comic/teleplay keep their structural line breaks). The reflow joins a line onto the previous one only when it begins with a lowercase letter — a real sentence/paragraph/heading never starts lowercase, so headings and attributions keep their own line and two paragraphs are never fused (a global line-width threshold is unsound on documents that mix already-joined and still-wrapped paragraphs). The prior text is snapshotted server-side, so History ▸ Revert undoes a format in one click. (`client/src/lib/manuscriptFormat.js`, `client/src/components/pipeline/manuscript/ManuscriptSectionFrame.jsx`, `client/src/pages/PipelineManuscriptEditor.jsx`)
-- **"Reformat (AI)" button on each manuscript section — fixes paste artifacts the deterministic Format can't.** PDF-pasted prose is sometimes genuinely scrambled (duplicated/misplaced quotes, ambiguous wraps before a capitalized word) in ways regex heuristics can't resolve. The new **Reformat (AI)** button sends the section to the AI provider selected in the sidebar with a strict "fix only whitespace, line breaks, drop-caps, and quotation-mark placement — do not change any words" prompt (stage `manuscript-reformat`), then persists the result (snapshotted, so History ▸ Revert undoes it). It is trustworthy by construction: a server-side **integrity guard** requires the letter/digit "skeleton" of the text to be **identical** before and after, rejecting (400, nothing saved) any result that changed, added, or dropped even a single letter or word — the model may only move whitespace and re-attach quotation-mark/punctuation glyphs. The endpoint is compute-only (returns the cleaned text without persisting); the client sends its live content and owns the save, so unsaved edits aren't reformatted-away and a mid-call edit discards the stale result instead of clobbering it. The core `reformatManuscriptText()` is shared so the importer can reuse it. (`data.reference/prompts/stages/manuscript-reformat.md`, `server/services/pipeline/manuscriptFix.js`, `server/routes/pipeline/manuscript.js`, `client/src/services/apiPipeline.js`, `client/src/components/pipeline/manuscript/ManuscriptSectionFrame.jsx`, `client/src/pages/PipelineManuscriptEditor.jsx`)
+- **"Reformat (AI)" button on each manuscript section — fixes paste artifacts the deterministic Format can't.** PDF-pasted prose is sometimes genuinely scrambled (duplicated/misplaced quotes, ambiguous wraps before a capitalized word) in ways regex heuristics can't resolve. The new **Reformat (AI)** button sends the section to the AI provider selected in the sidebar with a strict "fix only whitespace, line breaks, drop-caps, and quotation-mark placement — do not change any words" prompt (stage `manuscript-reformat`), then persists the result (snapshotted, so History ▸ Revert undoes it). It is trustworthy by construction: a server-side **integrity guard** requires the case-sensitive sequence of letters, digits, and apostrophes to be **identical** before and after, rejecting (400, nothing saved) any result that substituted, dropped, inserted, or recased a word — or mangled a contraction (`don't` → `dont`). Only whitespace, wrap-hyphens, and quotation marks may move (that's what reformatting *is*); the prompt forbids other punctuation edits. The endpoint is compute-only (returns the cleaned text without persisting); the client sends its live content and owns the save, so unsaved edits aren't reformatted-away and a mid-call edit discards the stale result instead of clobbering it. The core `reformatManuscriptText()` is shared so the importer can reuse it. (`data.reference/prompts/stages/manuscript-reformat.md`, `server/services/pipeline/manuscriptFix.js`, `server/routes/pipeline/manuscript.js`, `client/src/services/apiPipeline.js`, `client/src/components/pipeline/manuscript/ManuscriptSectionFrame.jsx`, `client/src/pages/PipelineManuscriptEditor.jsx`)
## Changed
diff --git a/server/services/pipeline/manuscriptFix.js b/server/services/pipeline/manuscriptFix.js
index 8470e1b42d..2b49558467 100644
--- a/server/services/pipeline/manuscriptFix.js
+++ b/server/services/pipeline/manuscriptFix.js
@@ -348,12 +348,23 @@ export async function saveManuscriptSection(seriesId, { issueId, stageId, output
const MANUSCRIPT_FORMAT_LABEL = { prose: 'Prose', comicScript: 'Comic script', teleplay: 'Teleplay' };
-// Letters + digits only, CASE-PRESERVING — the "skeleton" a pure reformat
-// leaves identical. No reformat operation (reflow, de-hyphenation, drop-cap
-// rejoin, quote re-attachment) changes letter case, so we compare
-// case-sensitively: a case-only rewrite ("US" → "us", a de-capitalized name) is
-// a wording change and must be rejected too.
-const wordSkeleton = (s) => (typeof s === 'string' ? s.replace(/[^\p{L}\p{N}]+/gu, '') : '');
+// The "skeleton" a pure reformat leaves identical: letters, digits, and
+// apostrophes (curly normalized to straight so a benign smart-quote pass isn't
+// flagged), CASE-PRESERVING. No reformat op (reflow, de-hyphenation, drop-cap
+// rejoin, quote re-attachment) changes case or touches an apostrophe, so
+// comparing this catches a substituted/dropped/inserted word, a case-only
+// rewrite ("US" → "us"), AND a mangled contraction ("don't" → "dont").
+//
+// Deliberately NOT guarded (these would break legitimate reformatting and are
+// instead forbidden by the prompt + caught by eye / undone via Revert): a
+// hyphen removed mid-word ("X-ray" → "Xray") is indistinguishable from the
+// de-hyphenation of a wrap-split word, and a removed inter-word boundary
+// ("now here" → "nowhere") is indistinguishable from a drop-cap rejoin
+// ("T\nhe" → "The") — both are whitespace/hyphen edits the skeleton ignores by
+// design so real reflow passes.
+const wordSkeleton = (s) => (typeof s === 'string'
+ ? s.replace(/[’ʼ]/g, '\'').replace(/[^\p{L}\p{N}']+/gu, '')
+ : '');
// Reject any reformat that altered the actual wording. A pure reformat only
// moves whitespace and quotation-mark/punctuation glyphs (all non-alphanumeric)
diff --git a/server/services/pipeline/manuscriptReformat.test.js b/server/services/pipeline/manuscriptReformat.test.js
index 988b35f8bb..b0ea411124 100644
--- a/server/services/pipeline/manuscriptReformat.test.js
+++ b/server/services/pipeline/manuscriptReformat.test.js
@@ -66,6 +66,19 @@ describe('reformatManuscriptText — integrity guard', () => {
.rejects.toThrow(/changed the wording/i);
});
+ it('rejects a mangled contraction (apostrophe dropped: don\'t → dont)', async () => {
+ runStagedLLM.mockResolvedValue({ content: 'I dont know.', runId: 'r1' });
+ await expect(reformatManuscriptText("I don't\nknow.", { stageId: 'prose' }))
+ .rejects.toThrow(/changed the wording/i);
+ });
+
+ it('accepts a benign smart-quote apostrophe normalization', async () => {
+ // curly ’ ↔ straight ' is normalized in the skeleton, so it is not flagged.
+ runStagedLLM.mockResolvedValue({ content: 'I don’t know.', runId: 'r1' });
+ const r = await reformatManuscriptText("I don't\nknow.", { stageId: 'prose' });
+ expect(r.text).toBe('I don’t know.');
+ });
+
it('rejects a dropped clause', async () => {
const input = 'The pool hums softly. The nebula churns in slow motion outside the wide viewport.';
runStagedLLM.mockResolvedValue({ content: 'The pool hums softly.', runId: 'r1' });