@adeu/core 1.21.0 → 1.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1150 -89
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +156 -12
- package/dist/index.d.ts +156 -12
- package/dist/index.js +1142 -88
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/diff.ts +500 -1
- package/src/docx/bridge.ts +13 -0
- package/src/domain.ts +68 -15
- package/src/engine.ts +391 -28
- package/src/index.ts +6 -5
- package/src/ingest.ts +77 -5
- package/src/mapper.ts +101 -12
- package/src/markup.test.ts +2 -1
- package/src/markup.ts +214 -20
- package/src/repro_qa_report_2026_07_18.test.ts +1244 -0
- package/src/repro_qa_report_v5.test.ts +399 -0
- package/src/sanitize/core.ts +3 -0
- package/src/sanitize/transforms.ts +66 -2
- package/src/utils/docx.ts +257 -16
- package/src/utils/safe-regex.ts +90 -0
package/src/markup.ts
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import { trim_common_context } from "./diff.js";
|
|
2
2
|
import { ModifyText } from "./models.js";
|
|
3
|
+
import { RegexTimeoutError, userFindAllMatches } from "./utils/safe-regex.js";
|
|
4
|
+
|
|
5
|
+
/** One entry per input edit in apply_edits_to_markdown's edit_reports. */
|
|
6
|
+
export interface MarkupEditReport {
|
|
7
|
+
index: number;
|
|
8
|
+
status: "applied" | "failed";
|
|
9
|
+
error: string | null;
|
|
10
|
+
occurrences: number;
|
|
11
|
+
}
|
|
3
12
|
export const AMBIGUITY_EXAMPLES_CAP = 5;
|
|
4
13
|
export const AMBIGUITY_CONTEXT_CHARS = 50;
|
|
5
14
|
function _should_strip_markers(text: string, marker: string): boolean {
|
|
@@ -51,6 +60,45 @@ export function _replace_smart_quotes(text: string): string {
|
|
|
51
60
|
.replace(/’/g, "'");
|
|
52
61
|
}
|
|
53
62
|
|
|
63
|
+
/**
|
|
64
|
+
* Strips markdown formatting markers and builds a position map.
|
|
65
|
+
* Returns [stripped_text, position_map] where position_map[i] = original
|
|
66
|
+
* index. Mirrors Python markup._strip_markdown_for_matching.
|
|
67
|
+
*/
|
|
68
|
+
function _strip_markdown_for_matching(text: string): [string, number[]] {
|
|
69
|
+
const result: string[] = [];
|
|
70
|
+
const position_map: number[] = [];
|
|
71
|
+
let i = 0;
|
|
72
|
+
|
|
73
|
+
while (i < text.length) {
|
|
74
|
+
// Skip ** or __
|
|
75
|
+
const pair = text.substring(i, i + 2);
|
|
76
|
+
if (i < text.length - 1 && (pair === "**" || pair === "__")) {
|
|
77
|
+
i += 2;
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
// Skip single * or _ that look like markdown (at word boundaries)
|
|
81
|
+
if (text[i] === "*" || text[i] === "_") {
|
|
82
|
+
const prev_char = i > 0 ? text[i - 1] : " ";
|
|
83
|
+
const next_char = i < text.length - 1 ? text[i + 1] : " ";
|
|
84
|
+
// If at boundary (space or start/end), likely markdown
|
|
85
|
+
if (
|
|
86
|
+
[" ", "\n", "\t"].includes(prev_char) ||
|
|
87
|
+
[" ", "\n", "\t"].includes(next_char)
|
|
88
|
+
) {
|
|
89
|
+
i += 1;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
position_map.push(i);
|
|
95
|
+
result.push(text[i]);
|
|
96
|
+
i += 1;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return [result.join(""), position_map];
|
|
100
|
+
}
|
|
101
|
+
|
|
54
102
|
function _find_safe_boundaries(
|
|
55
103
|
text: string,
|
|
56
104
|
start: number,
|
|
@@ -198,35 +246,107 @@ export function _find_match_in_text(
|
|
|
198
246
|
text: string,
|
|
199
247
|
target: string,
|
|
200
248
|
): [number, number] {
|
|
201
|
-
|
|
249
|
+
const matches = _find_all_matches_in_text(text, target);
|
|
250
|
+
if (matches.length > 0) return matches[0];
|
|
251
|
+
return [-1, -1];
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Every non-overlapping match of `target` in `text` as [start, end] pairs,
|
|
256
|
+
* using the SAME strategy ladder as the apply engine's
|
|
257
|
+
* DocumentMapper.find_all_match_indices: regex (when requested) or
|
|
258
|
+
* exact → smart-quote-normalized → fuzzy. Markup previews must resolve
|
|
259
|
+
* matching identically to apply, or the preview lies (QA 2026-07-18 M1).
|
|
260
|
+
*/
|
|
261
|
+
export function _find_all_matches_in_text(
|
|
262
|
+
text: string,
|
|
263
|
+
target: string,
|
|
264
|
+
is_regex = false,
|
|
265
|
+
): [number, number][] {
|
|
266
|
+
if (!target) return [];
|
|
267
|
+
|
|
268
|
+
if (is_regex) {
|
|
269
|
+
// Same semantics as the mapper: budgeted user regex; an invalid pattern
|
|
270
|
+
// simply produces no matches (surfaced as "not found").
|
|
271
|
+
// RegexTimeoutError propagates for a clean per-edit error.
|
|
272
|
+
try {
|
|
273
|
+
return userFindAllMatches(target, text).map((m) => [m.start, m.end]);
|
|
274
|
+
} catch (e) {
|
|
275
|
+
if (e instanceof RegexTimeoutError) throw e;
|
|
276
|
+
return [];
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// Literal indexOf scans (no RegExp over an arbitrarily long escaped
|
|
281
|
+
// target, mirroring the mapper's exact tiers).
|
|
282
|
+
const findAllLiteral = (
|
|
283
|
+
haystack: string,
|
|
284
|
+
needle: string,
|
|
285
|
+
): [number, number][] => {
|
|
286
|
+
const out: [number, number][] = [];
|
|
287
|
+
let from = 0;
|
|
288
|
+
while (true) {
|
|
289
|
+
const idx = haystack.indexOf(needle, from);
|
|
290
|
+
if (idx === -1) break;
|
|
291
|
+
out.push([idx, idx + needle.length]);
|
|
292
|
+
from = idx + needle.length;
|
|
293
|
+
}
|
|
294
|
+
return out;
|
|
295
|
+
};
|
|
202
296
|
|
|
203
|
-
|
|
204
|
-
|
|
297
|
+
// 1. Exact matches
|
|
298
|
+
let spans = findAllLiteral(text, target);
|
|
299
|
+
if (spans.length > 0) {
|
|
300
|
+
return spans.map(([s, e]) => _find_safe_boundaries(text, s, e));
|
|
301
|
+
}
|
|
205
302
|
|
|
303
|
+
// 2. Smart quote normalization
|
|
206
304
|
const norm_text = _replace_smart_quotes(text);
|
|
207
305
|
const norm_target = _replace_smart_quotes(target);
|
|
208
|
-
|
|
209
|
-
if (
|
|
210
|
-
return _find_safe_boundaries(text,
|
|
306
|
+
spans = findAllLiteral(norm_text, norm_target);
|
|
307
|
+
if (spans.length > 0) {
|
|
308
|
+
return spans.map(([s, e]) => _find_safe_boundaries(text, s, e));
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// 3. Markdown-stripped match, mirroring the mapper's strip-markdown and
|
|
312
|
+
// plain-projection rungs: a plain target must find text whose projection
|
|
313
|
+
// carries **bold**/_italic_ markers (even mid-word), and a marked target
|
|
314
|
+
// must find plain text.
|
|
315
|
+
const [stripped_text, pos_map] = _strip_markdown_for_matching(norm_text);
|
|
316
|
+
const [stripped_target] = _strip_markdown_for_matching(norm_target);
|
|
317
|
+
if (
|
|
318
|
+
stripped_target &&
|
|
319
|
+
(stripped_text !== norm_text || stripped_target !== norm_target)
|
|
320
|
+
) {
|
|
321
|
+
const results: [number, number][] = [];
|
|
322
|
+
for (const [p_start, p_end] of findAllLiteral(stripped_text, stripped_target)) {
|
|
323
|
+
const raw_start = pos_map[p_start];
|
|
324
|
+
const raw_end = pos_map[p_end - 1] + 1;
|
|
325
|
+
results.push(_find_safe_boundaries(text, raw_start, raw_end));
|
|
326
|
+
}
|
|
327
|
+
if (results.length > 0) return results;
|
|
328
|
+
}
|
|
211
329
|
|
|
330
|
+
// 4. Fuzzy regex matches (handles markdown noise, list markers, etc.).
|
|
331
|
+
// The [*_]* character classes in _make_fuzzy_regex prevent catastrophic
|
|
332
|
+
// backtracking.
|
|
212
333
|
try {
|
|
213
|
-
const pattern = new RegExp(_make_fuzzy_regex(target));
|
|
214
|
-
const
|
|
215
|
-
|
|
216
|
-
const raw_start = match.index;
|
|
217
|
-
const raw_end = match.index + match[0].length;
|
|
334
|
+
const pattern = new RegExp(_make_fuzzy_regex(target), "g");
|
|
335
|
+
const results: [number, number][] = [];
|
|
336
|
+
for (const match of text.matchAll(pattern)) {
|
|
218
337
|
const [refined_start, refined_end] = _refine_match_boundaries(
|
|
219
338
|
text,
|
|
220
|
-
|
|
221
|
-
|
|
339
|
+
match.index!,
|
|
340
|
+
match.index! + match[0].length,
|
|
222
341
|
);
|
|
223
|
-
|
|
342
|
+
results.push(_find_safe_boundaries(text, refined_start, refined_end));
|
|
224
343
|
}
|
|
344
|
+
if (results.length > 0) return results;
|
|
225
345
|
} catch (e) {
|
|
226
346
|
// Ignore regex compilation errors from edge cases
|
|
227
347
|
}
|
|
228
348
|
|
|
229
|
-
return [
|
|
349
|
+
return [];
|
|
230
350
|
}
|
|
231
351
|
|
|
232
352
|
export function _build_critic_markup(
|
|
@@ -283,31 +403,93 @@ export function _build_critic_markup(
|
|
|
283
403
|
return parts.join("");
|
|
284
404
|
}
|
|
285
405
|
|
|
406
|
+
/**
|
|
407
|
+
* Applies edits to Markdown text and returns CriticMarkup-annotated output.
|
|
408
|
+
*
|
|
409
|
+
* Edit resolution follows the SAME semantics as `apply` (QA 2026-07-18 M1):
|
|
410
|
+
* - `regex: true` targets match as regular expressions
|
|
411
|
+
* - `match_mode` is honored: "strict" (default) refuses ambiguous targets,
|
|
412
|
+
* "first" marks the first occurrence, "all" marks every occurrence
|
|
413
|
+
* - a missing target is a per-edit failure, never a silent skip
|
|
414
|
+
*
|
|
415
|
+
* When `edit_reports` is provided, one entry per input edit is appended:
|
|
416
|
+
* { index: 0-based input position, status: "applied"|"failed",
|
|
417
|
+
* error: string|null, occurrences: number }.
|
|
418
|
+
*/
|
|
286
419
|
export function apply_edits_to_markdown(
|
|
287
420
|
markdown_text: string,
|
|
288
421
|
edits: ModifyText[],
|
|
289
422
|
include_index = false,
|
|
290
423
|
highlight_only = false,
|
|
424
|
+
edit_reports?: MarkupEditReport[],
|
|
291
425
|
): string {
|
|
292
426
|
if (!edits || edits.length === 0) return markdown_text;
|
|
293
427
|
|
|
428
|
+
const _report = (
|
|
429
|
+
idx: number,
|
|
430
|
+
status: "applied" | "failed",
|
|
431
|
+
error: string | null = null,
|
|
432
|
+
occurrences = 0,
|
|
433
|
+
) => {
|
|
434
|
+
if (edit_reports) edit_reports.push({ index: idx, status, error, occurrences });
|
|
435
|
+
};
|
|
436
|
+
|
|
437
|
+
// Step 1: Find match positions for each edit
|
|
294
438
|
const matched_edits: [number, number, string, ModifyText, number][] = [];
|
|
295
439
|
|
|
296
440
|
for (let idx = 0; idx < edits.length; idx++) {
|
|
297
441
|
const edit = edits[idx];
|
|
298
442
|
const target = edit.target_text || "";
|
|
443
|
+
const match_mode = (edit as any).match_mode || "strict";
|
|
444
|
+
const is_regex = Boolean((edit as any).regex);
|
|
299
445
|
|
|
300
446
|
if (!target) {
|
|
447
|
+
_report(
|
|
448
|
+
idx,
|
|
449
|
+
"failed",
|
|
450
|
+
`- Edit ${idx + 1} Failed: target_text is empty. Pure insertions are expressed as a ` +
|
|
451
|
+
"replacement: put the text immediately around the insertion point in target_text and " +
|
|
452
|
+
"repeat it (plus the new text) in new_text.",
|
|
453
|
+
);
|
|
454
|
+
continue;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
let spans: [number, number][];
|
|
458
|
+
try {
|
|
459
|
+
spans = _find_all_matches_in_text(markdown_text, target, is_regex);
|
|
460
|
+
} catch (e) {
|
|
461
|
+
if (!(e instanceof RegexTimeoutError)) throw e;
|
|
462
|
+
_report(idx, "failed", `- Edit ${idx + 1} Failed: ${e.message}`);
|
|
463
|
+
continue;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
if (spans.length === 0) {
|
|
467
|
+
_report(
|
|
468
|
+
idx,
|
|
469
|
+
"failed",
|
|
470
|
+
`- Edit ${idx + 1} Failed: Target text not found in document:\n "${target.substring(0, 80)}"`,
|
|
471
|
+
);
|
|
301
472
|
continue;
|
|
302
473
|
}
|
|
303
474
|
|
|
304
|
-
|
|
305
|
-
|
|
475
|
+
if (spans.length > 1 && match_mode === "strict") {
|
|
476
|
+
_report(
|
|
477
|
+
idx,
|
|
478
|
+
"failed",
|
|
479
|
+
format_ambiguity_error(idx + 1, target, markdown_text, spans),
|
|
480
|
+
);
|
|
481
|
+
continue;
|
|
482
|
+
}
|
|
306
483
|
|
|
307
|
-
const
|
|
308
|
-
|
|
484
|
+
const selected =
|
|
485
|
+
match_mode === "strict" || match_mode === "first" ? spans.slice(0, 1) : spans;
|
|
486
|
+
for (const [start, end] of selected) {
|
|
487
|
+
matched_edits.push([start, end, markdown_text.substring(start, end), edit, idx]);
|
|
488
|
+
}
|
|
489
|
+
_report(idx, "applied", null, selected.length);
|
|
309
490
|
}
|
|
310
491
|
|
|
492
|
+
// Step 2: Check for overlapping edits
|
|
311
493
|
const matched_edits_filtered: [number, number, string, ModifyText, number][] =
|
|
312
494
|
[];
|
|
313
495
|
const occupied_ranges: [number, number][] = [];
|
|
@@ -319,6 +501,16 @@ export function apply_edits_to_markdown(
|
|
|
319
501
|
for (const [occ_start, occ_end] of occupied_ranges) {
|
|
320
502
|
if (start < occ_end && end > occ_start) {
|
|
321
503
|
overlaps = true;
|
|
504
|
+
if (edit_reports) {
|
|
505
|
+
const msg = `- Edit ${orig_idx + 1} Failed: overlaps with a previously matched edit.`;
|
|
506
|
+
for (const r of edit_reports) {
|
|
507
|
+
if (r.index === orig_idx) {
|
|
508
|
+
r.status = "failed";
|
|
509
|
+
r.error = msg;
|
|
510
|
+
r.occurrences = 0;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
}
|
|
322
514
|
break;
|
|
323
515
|
}
|
|
324
516
|
}
|
|
@@ -360,7 +552,9 @@ export function apply_edits_to_markdown(
|
|
|
360
552
|
isolated_target,
|
|
361
553
|
isolated_new,
|
|
362
554
|
edit.comment,
|
|
363
|
-
|
|
555
|
+
// 1-based, matching apply's "Edit N" reports and batch validation
|
|
556
|
+
// errors (QA 2026-07-17 F10; mirrors Python).
|
|
557
|
+
orig_idx + 1,
|
|
364
558
|
include_index,
|
|
365
559
|
highlight_only,
|
|
366
560
|
);
|