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

Anchor manuscript fix edits to original text

[._.]/ Adam Eivyc7ce4a737b46
diff --git a/server/services/pipeline/manuscriptEditor.test.js b/server/services/pipeline/manuscriptEditor.test.js
index 4349f9bb46..6c0f4e1837 100644
--- a/server/services/pipeline/manuscriptEditor.test.js
+++ b/server/services/pipeline/manuscriptEditor.test.js
@@ -281,6 +281,24 @@ describe('manuscriptFix', () => {
expect(after2.stages.prose.output).toBe('Second anchor.');
});
+ it('anchors multiple same-section edits against the original text', async () => {
+ const { s, issue } = await setupSeriesWithDraft('One. Two. Three.');
+ const seeded = await review.seedReviewFromFindings(s.id, [finding()]);
+
+ const result = await fixer.acceptManuscriptFix(s.id, {
+ commentId: seeded.comments[0].id,
+ edits: [
+ { issueId: issue.id, stageId: 'prose', find: 'One.', replace: 'One. Two.' },
+ { issueId: issue.id, stageId: 'prose', find: 'Two.', replace: 'Two changed.' },
+ ],
+ });
+
+ expect(result.comment.status).toBe('accepted');
+ expect(result.section.content).toBe('One. Two. Two changed. Three.');
+ const after = await issuesSvc.getIssue(issue.id);
+ expect(after.stages.prose.output).toBe('One. Two. Two changed. Three.');
+ });
+
it('acceptManuscriptFix throws when the anchor text is gone', async () => {
const { s } = await setupSeriesWithDraft();
const seeded = await review.seedReviewFromFindings(s.id, [finding()]);
diff --git a/server/services/pipeline/manuscriptFix.js b/server/services/pipeline/manuscriptFix.js
index 0e994a73fb..016274d8d0 100644
--- a/server/services/pipeline/manuscriptFix.js
+++ b/server/services/pipeline/manuscriptFix.js
@@ -217,15 +217,24 @@ async function planEditsBySection(edits, comment) {
for (const group of groups.values()) {
const issue = await getIssue(group.issueId).catch(() => null);
if (!issue) throw makeErr(`Issue not found: ${group.issueId}`, ERR_NOT_FOUND);
- let text = stageTextOf(issue.stages?.[group.stageId]);
+ const originalText = stageTextOf(issue.stages?.[group.stageId]);
+ const spans = [];
for (const edit of group.edits) {
- const idx = locateFind(text, edit.find, comment.anchorQuote);
+ const idx = locateFind(originalText, edit.find, comment.anchorQuote);
if (idx === -1) {
throw makeErr('Anchor text is no longer present in the manuscript — regenerate the fix', ERR_VALIDATION);
}
- text = text.slice(0, idx) + edit.replace + text.slice(idx + edit.find.length);
+ const span = { start: idx, end: idx + edit.find.length, replace: edit.replace };
+ if (spans.some((other) => span.start < other.end && other.start < span.end)) {
+ throw makeErr('Selected edits overlap in the manuscript — regenerate the fix', ERR_VALIDATION);
+ }
+ spans.push(span);
+ }
+ let output = originalText;
+ for (const span of spans.sort((a, b) => b.start - a.start)) {
+ output = output.slice(0, span.start) + span.replace + output.slice(span.end);
}
- planned.push({ ...group, originalText: stageTextOf(issue.stages?.[group.stageId]), output: text });
+ planned.push({ ...group, originalText, output });
}
return planned;
}