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 (local): disambiguate find/replace by anchorQuote when the span recurs

[._.]/ Adam Eivy24a0aa5adb20
diff --git a/server/services/pipeline/manuscriptEditor.test.js b/server/services/pipeline/manuscriptEditor.test.js
index 779bd430db..c217015e5d 100644
--- a/server/services/pipeline/manuscriptEditor.test.js
+++ b/server/services/pipeline/manuscriptEditor.test.js
@@ -143,6 +143,17 @@ describe('manuscriptFix', () => {
expect(after.stages.prose.output).toBe('The hero walked in. She left, but paused.');
});
+ it('acceptManuscriptFix targets the occurrence nearest the anchorQuote when find is ambiguous', async () => {
+ // "the door" appears twice; the comment anchors the SECOND one.
+ const { s, issue } = await setupSeriesWithDraft('She opened the door. Later, she slammed the door shut.');
+ const seeded = await review.seedReviewFromFindings(s.id, [finding({ anchorQuote: 'slammed the door shut', problem: 'weak verb' })]);
+ const result = await fixer.acceptManuscriptFix(s.id, { commentId: seeded.comments[0].id, find: 'the door', replace: 'the oak door' });
+ // The first "the door" is untouched; the one by the anchor is edited.
+ expect(result.section.content).toBe('She opened the door. Later, she slammed the oak door shut.');
+ const after = await issuesSvc.getIssue(issue.id);
+ expect(after.stages.prose.output).toBe('She opened the door. Later, she slammed the oak door shut.');
+ });
+
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 2c71fbdad5..57a9e70972 100644
--- a/server/services/pipeline/manuscriptFix.js
+++ b/server/services/pipeline/manuscriptFix.js
@@ -54,6 +54,26 @@ async function loadStageText(issueId, stageId) {
return stageTextOf(issue.stages?.[stageId]);
}
+// Locate the `find` span to replace. `indexOf` alone edits the FIRST match,
+// which silently corrupts the wrong spot when `find` recurs in the issue. When
+// it's ambiguous, disambiguate by the finding's `anchorQuote` — pick the
+// occurrence nearest the anchor — so the edit lands where the comment points.
+// Returns the start index, or -1 if `find` isn't present at all.
+function locateFind(text, find, anchorQuote) {
+ const first = text.indexOf(find);
+ if (first === -1) return -1;
+ if (text.indexOf(find, first + 1) === -1) return first; // unique — no ambiguity
+ const anchorIdx = anchorQuote ? text.indexOf(anchorQuote) : -1;
+ if (anchorIdx === -1) return first; // can't disambiguate — first match
+ let best = first;
+ let bestDist = Math.abs(first - anchorIdx);
+ for (let i = text.indexOf(find, first + 1); i !== -1; i = text.indexOf(find, i + 1)) {
+ const dist = Math.abs(i - anchorIdx);
+ if (dist < bestDist) { best = i; bestDist = dist; }
+ }
+ return best;
+}
+
// Shape one manuscript section response from a freshly-written stage.
const sectionFrom = (issue, stageId, stage) => ({
issueId: issue.id,
@@ -150,7 +170,7 @@ export async function acceptManuscriptFix(seriesId, { commentId, find, replace }
const { issue, stage } = await updateStageWithLatest(issueId, stageId, (cur) => {
const text = stageTextOf(cur);
- const idx = text.indexOf(find);
+ const idx = locateFind(text, find, comment.anchorQuote);
if (idx === -1) {
throw makeErr('Anchor text is no longer present in the manuscript — regenerate the fix', ERR_VALIDATION);
}