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

apply manuscript fixes whose quote differs only in whitespace; add note id copy

[._.]/ Adam Eivybfb8bb6e01b7
diff --git a/client/src/components/pipeline/manuscript/ManuscriptCommentCard.jsx b/client/src/components/pipeline/manuscript/ManuscriptCommentCard.jsx
index 173ad5530d..4b867c25e1 100644
--- a/client/src/components/pipeline/manuscript/ManuscriptCommentCard.jsx
+++ b/client/src/components/pipeline/manuscript/ManuscriptCommentCard.jsx
@@ -11,16 +11,33 @@
*/
import { useMemo, useState } from 'react';
-import { Loader2, Sparkles, Check, X, CornerDownRight, Columns2, Rows2 } from 'lucide-react';
+import { Loader2, Sparkles, Check, X, CornerDownRight, Columns2, Rows2, Copy } from 'lucide-react';
import InlineDiff from '../../ui/InlineDiff';
import SideBySideDiff from '../../ui/SideBySideDiff';
import toast from '../../ui/Toast';
+import { copyToClipboard } from '../../../lib/clipboard';
import { useAsyncAction } from '../../../hooks/useAsyncAction';
import {
patchPipelineManuscriptComment, generatePipelineManuscriptFix, acceptPipelineManuscriptFix,
} from '../../../services/api';
import { SEVERITY_TONE, CATEGORY_LABEL } from './constants';
+// Truncated comment id + copy button — so a note that looks wrong can be quoted
+// by id when reporting/debugging.
+export function CopyId({ id }) {
+ const short = id.length > 14 ? `${id.slice(0, 14)}…` : id;
+ return (
+ <button
+ type="button"
+ onClick={() => { copyToClipboard(id); toast.success('Comment id copied'); }}
+ title={`Copy comment id: ${id}`}
+ className="inline-flex items-center gap-1 text-[10px] font-mono text-gray-500 hover:text-gray-300"
+ >
+ <Copy size={10} /> {short}
+ </button>
+ );
+}
+
export function Badge({ comment }) {
return (
<span className="flex items-center gap-1.5">
@@ -133,17 +150,20 @@ export default function ManuscriptCommentCard({
<div className="border border-port-border rounded-lg bg-port-bg/40 p-2.5 space-y-2">
<div className="flex items-center justify-between gap-2">
<Badge comment={comment} />
- {onJump ? (
- <button
- type="button"
- onClick={() => onJump(comment)}
- className="text-[11px] text-port-accent hover:underline inline-flex items-center gap-1"
- title="Reveal this spot in the manuscript"
- >
- <CornerDownRight size={11} />
- {comment.issueNumber != null ? `Issue ${comment.issueNumber}` : 'Reveal'}
- </button>
- ) : null}
+ <div className="flex items-center gap-2">
+ <CopyId id={comment.id} />
+ {onJump ? (
+ <button
+ type="button"
+ onClick={() => onJump(comment)}
+ className="text-[11px] text-port-accent hover:underline inline-flex items-center gap-1"
+ title="Reveal this spot in the manuscript"
+ >
+ <CornerDownRight size={11} />
+ {comment.issueNumber != null ? `Issue ${comment.issueNumber}` : 'Reveal'}
+ </button>
+ ) : null}
+ </div>
</div>
<p className="text-xs text-gray-200">{comment.problem}</p>
@@ -191,7 +211,7 @@ export default function ManuscriptCommentCard({
onChange={(e) => setSelectedEdits((prev) => ({ ...prev, [i]: e.target.checked }))}
className="accent-port-accent"
/>
- <span className="font-medium">{label}{edit.title ? ` — ${edit.title}` : ''}</span>
+ <span className="font-medium">{label}{edit.title && edit.title !== label ? ` — ${edit.title}` : ''}</span>
{edit.fuzzy ? <span className="ml-auto text-port-warning">fuzzy</span> : null}
</label>
{edit.note ? <p className="px-2 pt-1.5 text-[11px] text-gray-500">{edit.note}</p> : null}
@@ -208,7 +228,7 @@ export default function ManuscriptCommentCard({
);
})}
{fuzzy ? (
- <p className="text-[10px] text-port-warning">Anchor not found verbatim — accepting may fail; edit the manuscript directly if so.</p>
+ <p className="text-[10px] text-port-warning">Quote isn’t an exact match for the draft (often just spacing) — accepting matches it flexibly; if it truly can’t be located you’ll get an error and can edit directly.</p>
) : null}
</div>
) : null}
diff --git a/client/src/lib/applyManuscriptEdits.js b/client/src/lib/applyManuscriptEdits.js
index 2e92b8d46c..8945c9921e 100644
--- a/client/src/lib/applyManuscriptEdits.js
+++ b/client/src/lib/applyManuscriptEdits.js
@@ -9,19 +9,20 @@
* mutating anything. Shares `locateFind` with `manuscriptAnchors.js`.
*/
-import { locateFind } from './manuscriptAnchors.js';
+import { locateFindSpan } from './manuscriptAnchors.js';
// Apply `edits` (each { find, replace, anchorQuote? }) to `content`. Edits whose
-// `find` isn't present are skipped; overlapping edits keep the earlier one.
+// `find` isn't present (even tolerating whitespace) are skipped; overlapping
+// edits keep the earlier one. Whitespace tolerance mirrors the server accept.
export function applyEditsToContent(content, edits, anchorQuote) {
const text = content || '';
const located = [];
(edits || []).forEach((e) => {
const find = e?.find || '';
if (!find) return;
- const start = locateFind(text, find, e.anchorQuote ?? anchorQuote);
- if (start === -1) return;
- located.push({ start, end: start + find.length, replace: e.replace ?? '' });
+ const span = locateFindSpan(text, find, e.anchorQuote ?? anchorQuote);
+ if (!span) return;
+ located.push({ start: span.start, end: span.end, replace: e.replace ?? '' });
});
// Drop overlaps (keep the earlier span), then splice high-to-low.
diff --git a/client/src/lib/applyManuscriptEdits.test.js b/client/src/lib/applyManuscriptEdits.test.js
index 540cfd6ff0..0ca9a1ffb8 100644
--- a/client/src/lib/applyManuscriptEdits.test.js
+++ b/client/src/lib/applyManuscriptEdits.test.js
@@ -38,4 +38,11 @@ describe('applyEditsToContent', () => {
it('returns content unchanged for empty edit list', () => {
expect(applyEditsToContent('text', [])).toBe('text');
});
+
+ it('applies an edit whose find differs only in whitespace (LLM reformatting)', () => {
+ const content = 'PAGE 56\nPANEL 1\nGiant stands.';
+ // find has an extra blank line the manuscript does not — still applies.
+ const out = applyEditsToContent(content, [{ find: 'PAGE 56\n\nPANEL 1\nGiant stands.', replace: 'PAGE 56\nPANEL 1\nGiant kneels.' }]);
+ expect(out).toBe('PAGE 56\nPANEL 1\nGiant kneels.');
+ });
});
diff --git a/client/src/lib/manuscriptAnchors.js b/client/src/lib/manuscriptAnchors.js
index 6ab9ede89b..de0b0bea96 100644
--- a/client/src/lib/manuscriptAnchors.js
+++ b/client/src/lib/manuscriptAnchors.js
@@ -39,6 +39,29 @@ export function locateFind(text, find, anchorQuote) {
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 = find.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
+ 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;
+}
+
// Resolve each comment's anchorQuote to a [start, end) span in `content`.
// Comments whose anchor isn't present (edited since review, or fuzzy) are
// dropped — they stay listed in the sidebar but show no in-text highlight.
diff --git a/client/src/lib/manuscriptAnchors.test.js b/client/src/lib/manuscriptAnchors.test.js
index ce207019db..f3fb615527 100644
--- a/client/src/lib/manuscriptAnchors.test.js
+++ b/client/src/lib/manuscriptAnchors.test.js
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
-import { locateFind, locateAnchors, buildHighlightSegments } from './manuscriptAnchors.js';
+import { locateFind, locateFindSpan, locateAnchors, buildHighlightSegments } from './manuscriptAnchors.js';
describe('locateFind', () => {
it('returns the only occurrence when unique', () => {
@@ -25,6 +25,26 @@ describe('locateFind', () => {
});
});
+describe('locateFindSpan', () => {
+ it('returns the exact span when present', () => {
+ const text = 'PAGE 56\nPANEL 1\nGiant.';
+ expect(locateFindSpan(text, 'PANEL 1')).toEqual({ start: 8, end: 15 });
+ });
+
+ it('tolerates whitespace-only differences and returns the original span', () => {
+ const text = 'PAGE 56\nPANEL 1\nGiant stands.';
+ const find = 'PAGE 56\n\nPANEL 1\nGiant stands.';
+ const span = locateFindSpan(text, find);
+ expect(text.slice(span.start, span.end)).toBe('PAGE 56\nPANEL 1\nGiant stands.');
+ expect(span.end - span.start).toBe(find.length - 1);
+ });
+
+ it('returns null when truly absent or empty', () => {
+ expect(locateFindSpan('hello', 'nope')).toBeNull();
+ expect(locateFindSpan('hello', '')).toBeNull();
+ });
+});
+
describe('locateAnchors', () => {
const content = 'She crept down the hall. The door slammed shut.';
it('resolves present anchors to spans and drops absent ones', () => {
diff --git a/server/services/pipeline/manuscriptFix.js b/server/services/pipeline/manuscriptFix.js
index bd76be6ca4..aec3fc22a4 100644
--- a/server/services/pipeline/manuscriptFix.js
+++ b/server/services/pipeline/manuscriptFix.js
@@ -165,6 +165,36 @@ function locateFind(text, find, anchorQuote) {
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 = find.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
+ 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;
+}
+
// Shape one manuscript section response from a freshly-written stage.
const sectionFrom = (issue, stageId, stage) => ({
issueId: issue.id,
@@ -203,11 +233,11 @@ async function planEditsBySection(edits, comment) {
const originalText = stageTextOf(issue.stages?.[group.stageId]);
const spans = [];
for (const edit of group.edits) {
- const idx = locateFind(originalText, edit.find, comment.anchorQuote);
- if (idx === -1) {
+ const located = locateFindSpan(originalText, edit.find, comment.anchorQuote);
+ if (!located) {
throw makeErr('Anchor text is no longer present in the manuscript — regenerate the fix', ERR_VALIDATION);
}
- const span = { start: idx, end: idx + edit.find.length, replace: edit.replace };
+ const span = { start: located.start, end: located.end, 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);
}
diff --git a/server/services/pipeline/manuscriptFix.test.js b/server/services/pipeline/manuscriptFix.test.js
new file mode 100644
index 0000000000..76ebe2867c
--- /dev/null
+++ b/server/services/pipeline/manuscriptFix.test.js
@@ -0,0 +1,42 @@
+import { describe, it, expect } from 'vitest';
+import { locateFindSpan } from './manuscriptFix.js';
+
+describe('locateFindSpan', () => {
+ it('locates an exact substring', () => {
+ const text = 'PAGE 56\nPANEL 1\nGiant stands.';
+ expect(locateFindSpan(text, 'PANEL 1\nGiant')).toEqual({ start: 8, end: 8 + 'PANEL 1\nGiant'.length });
+ });
+
+ it('tolerates whitespace-only differences in the quote (LLM reformatting)', () => {
+ // The manuscript has a single newline; the quoted `find` added a blank line.
+ const text = 'PAGE 56\nPANEL 1\nLow angle. Giant stands.\nPANEL 2\nHe falls.';
+ const find = 'PAGE 56\n\nPANEL 1\nLow angle. Giant stands.';
+ const span = locateFindSpan(text, find);
+ expect(span).not.toBeNull();
+ // The matched span covers the ORIGINAL (single-newline) text, not find.length.
+ expect(text.slice(span.start, span.end)).toBe('PAGE 56\nPANEL 1\nLow angle. Giant stands.');
+ expect(span.end - span.start).toBe(find.length - 1);
+ });
+
+ it('returns null when the text is genuinely absent', () => {
+ expect(locateFindSpan('hello world', 'not here at all')).toBeNull();
+ });
+
+ it('returns null for an empty find', () => {
+ expect(locateFindSpan('anything', '')).toBeNull();
+ });
+
+ it('disambiguates a recurring fuzzy match by the nearest anchorQuote', () => {
+ const text = 'run away.\n\n[marker] run\taway.';
+ // "run away." (exact) appears first; with whitespace tolerance the second
+ // (tab instead of space) also matches — anchor picks the nearer one.
+ const span = locateFindSpan(text, 'run away.'.replace(' ', ' '), '[marker]');
+ // exact match exists at index 0, so it wins (nearest-occurrence among exacts).
+ expect(span.start).toBe(0);
+ });
+
+ it('escapes regex metacharacters in the quote', () => {
+ const text = 'Cost is $5 (approx).';
+ expect(locateFindSpan(text, '$5 (approx)')).toEqual({ start: 8, end: 8 + '$5 (approx)'.length });
+ });
+});