From 90ac5ee6cf2ea82c883877133fb087429ba809d2 Mon Sep 17 00:00:00 2001 From: Kit Langton Date: Thu, 13 Aug 2026 12:41:59 -0400 Subject: [PATCH] fix(core): harden webfetch markdown boundaries --- packages/core/perf/html-markdown.md | 26 +- packages/core/src/tool/html-markdown.ts | 475 +++++++++++++++++------ packages/core/src/tool/webfetch.ts | 4 +- packages/core/test/tool-webfetch.test.ts | 178 ++++++++- 4 files changed, 544 insertions(+), 139 deletions(-) diff --git a/packages/core/perf/html-markdown.md b/packages/core/perf/html-markdown.md index 82caec41c0d..5a8c56381ba 100644 --- a/packages/core/perf/html-markdown.md +++ b/packages/core/perf/html-markdown.md @@ -17,16 +17,28 @@ Replace Turndown and Domino in V2 Core only when an htmlparser2 event renderer p ## Experiment Log -| Experiment | Hypothesis | Before | After | Decision | -| --- | --- | --- | --- | --- | -| Event renderer | Avoiding Domino's DOM lowers conversion cost while retaining semantics. | Turndown 4.23 MiB/s median (72.55 ms, 66.93-109.75) | Candidate 10.12 MiB/s median (30.32 ms, 22.29-83.55) | Keep: 2.39x throughput | -| Safe fences | Fence length derived from code content prevents embedded backticks from closing blocks. | Turndown emitted triple fences around embedded triples | Candidate expands to four backticks | Keep | -| Tables | Row/cell events retain tabular relationships better than flattened cell blocks. | Turndown flattened cells | Candidate emits GFM-readable tables | Keep | -| Malformed inline blocks | Delimiters spanning implied block closes produce malformed Markdown. | Candidate left open emphasis | Candidate drops the delimiter and preserves visible text | Keep | +| Experiment | Hypothesis | Before | After | Decision | +| ----------------------- | ----------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------- | +| Event renderer | Avoiding Domino's DOM lowers conversion cost while retaining semantics. | Turndown 4.75 MiB/s median (64.60 ms, 62.51-76.02) | Candidate 19.28 MiB/s median (15.92 ms, 14.51-20.58) | Keep: 4.06x throughput | +| Safe fences | Choosing the shorter bounded backtick or tilde fence prevents embedded markers from closing blocks without amplifying output. | Turndown emitted triple fences around embedded triples | Candidate chooses the shorter marker and accounts for the full quoted fence within the content budget | Keep | +| Tables | Row/cell events retain tabular relationships better than flattened cell blocks. | Turndown flattened cells | Candidate emits GFM-readable tables | Keep | +| Malformed inline blocks | Delimiters spanning implied block closes produce malformed Markdown. | Candidate left open emphasis | Candidate drops the delimiter and preserves visible text | Keep | +| Regex depth prepass | A whole-input tag regex can backtrack quadratically on repeated malformed prefixes. | 2 MiB `