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 on manuscript fix accept

[._.]/ Adam Eivyd2ff16f91038
diff --git a/server/services/pipeline/manuscriptEditor.test.js b/server/services/pipeline/manuscriptEditor.test.js
index 188c3dced3..2132c8b729 100644
--- a/server/services/pipeline/manuscriptEditor.test.js
+++ b/server/services/pipeline/manuscriptEditor.test.js
@@ -215,6 +215,51 @@ describe('manuscriptFix', () => {
expect(after2.stages.prose.output).toContain('looks afraid');
});
+ it('rejects explicit edit targets outside the comment manuscript scope', async () => {
+ const { s } = await setupSeriesWithDraft();
+ const other = await setupSeriesWithDraft('Other series text.');
+ const seeded = await review.seedReviewFromFindings(s.id, [finding()]);
+
+ await expect(
+ fixer.acceptManuscriptFix(s.id, {
+ commentId: seeded.comments[0].id,
+ edits: [{
+ issueId: other.issue.id,
+ stageId: 'prose',
+ find: 'Other series text.',
+ replace: 'Tampered text.',
+ }],
+ }),
+ ).rejects.toMatchObject({ code: 'PIPELINE_MANUSCRIPT_FIX_VALIDATION' });
+
+ const afterOther = await issuesSvc.getIssue(other.issue.id);
+ expect(afterOther.stages.prose.output).toBe('Other series text.');
+ });
+
+ it('validates every multi-section anchor before writing any section', async () => {
+ const { s, issue } = await setupSeriesWithDraft('First anchor.');
+ const issue2 = await issuesSvc.createIssue({
+ seriesId: s.id, number: 2, title: 'Two', arcPosition: 2,
+ stages: { prose: { output: 'Second anchor.', status: 'ready' } },
+ });
+ const seeded = await review.seedReviewFromFindings(s.id, [finding({ issueNumber: null, anchorQuote: '' })]);
+
+ await expect(
+ fixer.acceptManuscriptFix(s.id, {
+ commentId: seeded.comments[0].id,
+ edits: [
+ { issueId: issue.id, stageId: 'prose', find: 'First anchor.', replace: 'First changed.' },
+ { issueId: issue2.id, stageId: 'prose', find: 'Missing second anchor.', replace: 'Second changed.' },
+ ],
+ }),
+ ).rejects.toMatchObject({ code: 'PIPELINE_MANUSCRIPT_FIX_VALIDATION' });
+
+ const after1 = await issuesSvc.getIssue(issue.id);
+ const after2 = await issuesSvc.getIssue(issue2.id);
+ expect(after1.stages.prose.output).toBe('First anchor.');
+ expect(after2.stages.prose.output).toBe('Second anchor.');
+ });
+
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 22e6e2e5be..b23620d6d8 100644
--- a/server/services/pipeline/manuscriptFix.js
+++ b/server/services/pipeline/manuscriptFix.js
@@ -190,16 +190,18 @@ const sectionFrom = (issue, stageId, stage) => ({
async function resolveMissingEditTargets(seriesId, comment, edits) {
const targets = await resolveTargets(seriesId, comment);
+ const targetByKey = new Map(targets.map((s) => [`${s.issueId}:${s.stageId}`, s]));
return edits.map((edit) => {
- if (edit.issueId && edit.stageId) return edit;
- const section = resolveEditSection(edit, targets);
+ const hasExplicitTarget = edit.issueId && edit.stageId;
+ const section = hasExplicitTarget ? targetByKey.get(`${edit.issueId}:${edit.stageId}`) : resolveEditSection(edit, targets);
+ if (hasExplicitTarget && !section) return { ...edit, issueId: null, stageId: null };
return section
? { ...edit, issueId: section.issueId, stageId: section.stageId, issueNumber: section.number }
: edit;
});
}
-async function applyEditsToSections(edits, comment) {
+async function planEditsBySection(edits, comment) {
const groups = new Map();
for (const edit of edits) {
const key = `${edit.issueId}:${edit.stageId}`;
@@ -207,18 +209,31 @@ async function applyEditsToSections(edits, comment) {
groups.get(key).edits.push(edit);
}
- const sections = [];
+ const planned = [];
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]);
+ for (const edit of group.edits) {
+ const idx = locateFind(text, 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);
+ }
+ planned.push({ ...group, originalText: stageTextOf(issue.stages?.[group.stageId]), output: text });
+ }
+ return planned;
+}
+
+async function applyPlannedEdits(planned) {
+ const sections = [];
+ for (const group of planned) {
const { issue, stage } = await updateStageWithLatest(group.issueId, group.stageId, (cur) => {
- let text = stageTextOf(cur);
- for (const edit of group.edits) {
- const idx = locateFind(text, 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);
+ if (stageTextOf(cur) !== group.originalText) {
+ throw makeErr('Manuscript changed while applying the fix — regenerate the fix', ERR_VALIDATION);
}
- return { output: text, status: 'edited', lastRunId: `fix-${randomUUID()}` };
+ return { output: group.output, status: 'edited', lastRunId: `fix-${randomUUID()}` };
}, { snapshotPrior: true });
sections.push(sectionFrom(issue, group.stageId, stage));
}
@@ -320,7 +335,8 @@ export async function acceptManuscriptFix(seriesId, { commentId, find, replace,
throw makeErr('One selected edit is not anchored to a manuscript section — regenerate the fix', ERR_VALIDATION);
}
- const sections = await applyEditsToSections(acceptedEdits, comment);
+ const planned = await planEditsBySection(acceptedEdits, comment);
+ const sections = await applyPlannedEdits(planned);
const updated = await updateComment(seriesId, commentId, { status: 'accepted' });
return { comment: updated, section: sections[0] || null, sections };
}