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

refactor: share one manuscript find locator between preview and accept (#13163)

Adam Eivy4a509b8a23db
diff --git a/client/src/lib/manuscriptAnchors.js b/client/src/lib/manuscriptAnchors.js
index 6a45755a91..b3cd5675d1 100644
--- a/client/src/lib/manuscriptAnchors.js
+++ b/client/src/lib/manuscriptAnchors.js
@@ -4,9 +4,9 @@
*
* Editorial comments anchor to manuscript text by VERBATIM substring
* (`anchorQuote`), not character offsets — mirroring the server. `locateFind`
- * here mirrors `locateFind` in `server/services/pipeline/manuscriptFix.js`
- * (nearest-occurrence disambiguation) so a highlight lands where an accepted fix
- * would actually splice. It is also reused by `applyManuscriptEdits.js` for the
+ * is re-exported from `server/lib/manuscriptLocate.js` — the same implementation
+ * the server splices accepted fixes with — so a highlight lands where an accepted
+ * fix would actually splice. It is also reused by `applyManuscriptEdits.js` for the
* client-side impact preview.
*
* locateAnchors(content, comments) → [{ commentId, severity, start, end }]
@@ -18,51 +18,11 @@
* carrying every comment covering it, toned by the highest severity present.
*/
-import { escapeRegExp } from './textUtils.js';
+import { locateFind, locateFindSpan } from '../../../server/lib/manuscriptLocate.js';
const SEVERITY_RANK = { high: 3, medium: 2, low: 1 };
-// Locate the `find` span to highlight/replace. `indexOf` alone targets the FIRST
-// match, which is the wrong spot when `find` recurs. When ambiguous, pick the
-// occurrence nearest the `anchorQuote`. Returns the start index, or -1 if absent.
-export function locateFind(text, find, anchorQuote) {
- if (!find) return -1;
- const first = text.indexOf(find);
- if (first === -1) return -1;
- if (text.indexOf(find, first + 1) === -1) return first; // unique
- 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;
-}
-
-// Locate `find` tolerating whitespace-only differences (LLMs reformat spacing
-// when quoting). Tries exact first, then a regex where each run of whitespace
-// matches any run of whitespace. Returns the matched { start, end } in the
-// original text (the span length may differ from find.length) or null. Mirrors
-// `locateFindSpan` in `server/services/pipeline/manuscriptFix.js`.
-export function locateFindSpan(text, find, anchorQuote) {
- if (!find) return null;
- const exact = locateFind(text, find, anchorQuote);
- if (exact !== -1) return { start: exact, end: exact + find.length };
-
- const escaped = escapeRegExp(find);
- const re = new RegExp(escaped.replace(/\s+/g, '\\s+'), 'g');
- const anchorIdx = anchorQuote ? text.indexOf(anchorQuote) : -1;
- let best = null;
- for (let m = re.exec(text); m; m = re.exec(text)) {
- const cand = { start: m.index, end: m.index + m[0].length };
- if (!best) best = cand;
- else if (anchorIdx !== -1 && Math.abs(cand.start - anchorIdx) < Math.abs(best.start - anchorIdx)) best = cand;
- if (m.index === re.lastIndex) re.lastIndex += 1;
- }
- return best;
-}
+export { locateFind, locateFindSpan };
// Resolve each comment's anchorQuote to a [start, end) span in `content`.
// Comments whose anchor isn't present (edited since review, or fuzzy) are
diff --git a/client/src/lib/manuscriptAnchors.test.js b/client/src/lib/manuscriptAnchors.test.js
index 30e8f97d46..6c6654eaf7 100644
--- a/client/src/lib/manuscriptAnchors.test.js
+++ b/client/src/lib/manuscriptAnchors.test.js
@@ -97,3 +97,12 @@ describe('buildHighlightSegments', () => {
});
});
// @vitest-environment node
+
+import { locateFind as sharedLocateFind, locateFindSpan as sharedLocateFindSpan } from '../../../server/lib/manuscriptLocate.js';
+
+describe('locator parity with the server accept path', () => {
+ it('client preview re-exports the shared server locator', () => {
+ expect(locateFind).toBe(sharedLocateFind);
+ expect(locateFindSpan).toBe(sharedLocateFindSpan);
+ });
+});
diff --git a/server/lib/README.md b/server/lib/README.md
index cacd21a4fb..bb561502ab 100644
--- a/server/lib/README.md
+++ b/server/lib/README.md
@@ -968,3 +968,4 @@ The barrel `server/lib/index.js` is a machine-checkable enumeration of every pub
- `eventLoopLagProbe.js`: `createEventLoopLagProbe({ clock, intervalMs, toleranceMs, resources, attributeAboveMs })` opens phase-tracking spans that charge overdue time of one shared, span-scoped tick to the current phase, so a slow-operation diagnostic splits each phase into event-loop/host scheduling lag and asynchronous wait (#12329), and a span charged more than `attributeAboveMs` of lag carries `load` (process `cpuMs`, `gcMs`, `invCtx`, host `load1`/`cpus`) to tell a busy process from a starved one (#12852); `beginLoadSample(resources)`, `formatLoadAttribution(load)` and `systemResources` (the same counters for a caller without a span); `systemClock` (globals resolved at call time), `__createManualClock()` (deterministic `advance` = on-time timers vs `stall` = blocked loop, for tests), `roundMs`, and `phaseGroup(summary, name)` (a phase plus its `name.` sub-phases).
- `pgJson.js`: text repair for values bound to PostgreSQL, which refuses a lone UTF-16 surrogate in `jsonb` and NUL in `jsonb`/`text` where every file store accepted both. `normalizePgString(s)` → `toWellFormed()` with NUL → U+FFFD (the same lossy repair a UTF-8 file write applied). `toPgJson(value)` → `JSON.stringify` that repairs string values and object keys; **use it, never bare `JSON.stringify`, for a parameter bound to a `json`/`jsonb` column or `::jsonb` cast** (`pgJsonConventions.test.js` enforces this). `normalizePgParams(params)` is applied by `db.js` `query()` and the `withTransaction` client: repairs string and array-element params (never unescapes `\^@`) and serializes plain-object params via `toPgJson`.
- `errorLogFingerprint.js`: stderr-log triage primitives for `error-log-triage` (#12851). `groupStderrEntries(text)` strips the PM2 date prefix and folds stack/continuation lines under their head line; `fingerprintEntry(entry, { extraTerms })` redacts the entry (home paths, hosts, IPs, UUIDs, credentials, quoted record names, plus install literals such as the hostname and OS user) and reduces the head to a template (timestamps, ids, SHAs, filesystem paths and numbers become placeholders; API routes stay distinct) with a stable 16-hex fingerprint; `templateSearchExcerpt(template)` picks a tracker search phrase, or null when too generic. Pure; the collector and tracker dedupe live in `services/errorLogTriage.js`.
+| `manuscriptLocate.js` | `locateFind` / `locateFindSpan` — the manuscript `find` locator (nearest-to-`anchorQuote` occurrence, then whitespace-tolerant regex fallback). The one implementation behind both the server accept path (`services/pipeline/manuscriptFix.js`) and the client highlight/impact preview (`client/src/lib/manuscriptAnchors.js` re-exports it), so the preview selects the span an accept replaces. Pure / browser-safe. |
diff --git a/server/lib/index.js b/server/lib/index.js
index 97ea05eace..cc4c82574a 100644
--- a/server/lib/index.js
+++ b/server/lib/index.js
@@ -954,3 +954,4 @@ export * from './checkoutRetirementAdmission.js';
export * from './moltworldExplore.js';
export * from './pgJson.js';
export * from './errorLogFingerprint.js';
+export * from './manuscriptLocate.js';
diff --git a/server/lib/manuscriptLocate.js b/server/lib/manuscriptLocate.js
new file mode 100644
index 0000000000..3b17316262
--- /dev/null
+++ b/server/lib/manuscriptLocate.js
@@ -0,0 +1,49 @@
+/**
+ * Manuscript `find` locator shared by the server (accepted editorial edits,
+ * `services/pipeline/manuscriptFix.js`) and the client (highlighting and the
+ * impact preview, via `client/src/lib/manuscriptAnchors.js`). One implementation
+ * so the preview always selects the span the persisted accept will replace.
+ * Pure / browser-safe.
+ */
+
+import { escapeRegExp } from './textUtils.js';
+
+// `indexOf` alone targets the FIRST match, which is the wrong spot when `find`
+// recurs. When ambiguous, pick the occurrence nearest the `anchorQuote`.
+// Returns the start index, or -1 if absent.
+export function locateFind(text, find, anchorQuote) {
+ if (!find) return -1;
+ const first = text.indexOf(find);
+ if (first === -1) return -1;
+ if (text.indexOf(find, first + 1) === -1) return first; // unique
+ 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;
+}
+
+// LLMs reformat whitespace when quoting a passage, so exact `find` often isn't
+// verbatim. Try exact first, then a regex where each whitespace run matches any
+// whitespace run. Returns { start, end } in the ORIGINAL text (span length may
+// differ from find.length) or null.
+export function locateFindSpan(text, find, anchorQuote) {
+ if (!find) return null;
+ const exact = locateFind(text, find, anchorQuote);
+ if (exact !== -1) return { start: exact, end: exact + find.length };
+
+ const re = new RegExp(escapeRegExp(find).replace(/\s+/g, '\\s+'), 'g');
+ const anchorIdx = anchorQuote ? text.indexOf(anchorQuote) : -1;
+ let best = null;
+ for (let m = re.exec(text); m; m = re.exec(text)) {
+ const cand = { start: m.index, end: m.index + m[0].length };
+ if (!best) best = cand;
+ else if (anchorIdx !== -1 && Math.abs(cand.start - anchorIdx) < Math.abs(best.start - anchorIdx)) best = cand;
+ if (m.index === re.lastIndex) re.lastIndex += 1; // guard against zero-width matches
+ }
+ return best;
+}
diff --git a/server/services/pipeline/manuscriptFix.js b/server/services/pipeline/manuscriptFix.js
index 8755a56b13..8e9334f076 100644
--- a/server/services/pipeline/manuscriptFix.js
+++ b/server/services/pipeline/manuscriptFix.js
@@ -19,7 +19,7 @@ import { getSeries, MANUSCRIPT_TYPES } from './series.js';
import { getIssue, updateStageWithLatest, updateStagesWithLatest } from './issues.js';
import { collectManuscriptSections, stageVersionsOf, sectionsCorpus, manuscriptSectionHeader } from './arcPlanner.js';
import { getComment, updateComment } from './manuscriptComments.js';
-import { escapeRegExp } from '../../lib/textUtils.js';
+import { locateFindSpan } from '../../lib/manuscriptLocate.js';
import { codedError } from '../../lib/codedError.js';
export const ERR_VALIDATION = 'PIPELINE_MANUSCRIPT_FIX_VALIDATION';
@@ -203,55 +203,8 @@ function editsFromAcceptRequest({ comment, find, replace, edits }) {
return [];
}
-// 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;
-}
-
-// LLMs routinely reformat whitespace when "quoting" a passage (an extra blank
-// line between a page header and its first panel, collapsed indentation, etc.),
-// so an exact `find` often isn't a verbatim substring even when it clearly
-// targets a real span. Locate the span tolerating whitespace-only differences:
-// try exact first (with nearest-anchor disambiguation), then a regex where every
-// run of whitespace in `find` matches any run of whitespace in the text. Returns
-// the matched { start, end } in the ORIGINAL text (so the splice covers the real
-// span, whose length may differ from `find.length`), or null if not found.
-function buildWhitespaceTolerantRegex(find) {
- const escaped = escapeRegExp(find);
- return new RegExp(escaped.replace(/\s+/g, '\\s+'));
-}
-
-export function locateFindSpan(text, find, anchorQuote) {
- if (!find) return null;
- const exact = locateFind(text, find, anchorQuote);
- if (exact !== -1) return { start: exact, end: exact + find.length };
-
- const re = new RegExp(buildWhitespaceTolerantRegex(find).source, 'g');
- const anchorIdx = anchorQuote ? text.indexOf(anchorQuote) : -1;
- let best = null;
- for (let m = re.exec(text); m; m = re.exec(text)) {
- const cand = { start: m.index, end: m.index + m[0].length };
- if (!best) best = cand;
- else if (anchorIdx !== -1 && Math.abs(cand.start - anchorIdx) < Math.abs(best.start - anchorIdx)) best = cand;
- if (m.index === re.lastIndex) re.lastIndex += 1; // guard against zero-width matches
- }
- return best;
-}
+// Re-exported for compatibility; the implementation is shared with the client preview.
+export { locateFindSpan };
// Shape one manuscript section response from a freshly-written stage.
const sectionFrom = (issue, stageId, stage) => ({
diff --git a/server/services/pipeline/manuscriptFix.test.js b/server/services/pipeline/manuscriptFix.test.js
index 5a7f4b50cf..acef8a306b 100644
--- a/server/services/pipeline/manuscriptFix.test.js
+++ b/server/services/pipeline/manuscriptFix.test.js
@@ -3,6 +3,7 @@ import { readFileSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import { locateFindSpan, normalizeFix, PROMPT_EXAMPLE_PLACEHOLDERS } from './manuscriptFix.js';
+import { locateFindSpan as sharedLocateFindSpan } from '../../lib/manuscriptLocate.js';
describe('locateFindSpan', () => {
it('locates an exact substring', () => {
@@ -119,3 +120,9 @@ describe('normalizeFix — echoed-placeholder guard', () => {
}
});
});
+
+describe('locateFindSpan parity with the client preview', () => {
+ it('is the shared locator the client impact preview resolves spans with', () => {
+ expect(locateFindSpan).toBe(sharedLocateFindSpan);
+ });
+});