Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

Wave B 1b-i: pre-quote fence beyond the head also refuses when it would drop a CR

garrytan3d7b9677e821
diff --git a/src/core/page-body/body-scan.ts b/src/core/page-body/body-scan.ts
index fbd308dbf9..0eeaafc33f 100644
--- a/src/core/page-body/body-scan.ts
+++ b/src/core/page-body/body-scan.ts
@@ -13,7 +13,7 @@
* is `frontmatter_head_unbounded` and the caller holds the file as
* `invalid_frontmatter`. The readers that must close inside `P`: the data
* fence (`scanDataFrontmatter`), the pre-quote pass's fence (unless no later
- * line closes it, or closing it would rewrite nothing), and with `validate`
+ * line closes it, or closing it would change no line), and with `validate`
* the validator's `---` line. A NUL beyond `P` is added to the findings as
* parseMarkdown's whole-file NULL_BYTES check would report it.
*
@@ -157,7 +157,7 @@ interface BareSection extends BareCandidate {
timelineEnd: Pos;
}
-interface QuoteWatch { anyBefore: boolean; prev: boolean; prevStripped: boolean }
+interface QuoteWatch { anyBefore: boolean; prev: boolean; prevChanged: boolean }
const utf8 = (text: string) => Buffer.byteLength(text, 'utf8');
@@ -275,7 +275,7 @@ export class BodyScanner {
if (fence.opens && fence.closeLine === -1) return this.unbounded('validator_fence');
}
if (frontmatterQuoteFence(head.lifted) === 'open') {
- const watch = { anyBefore: false, prev: false, prevStripped: false };
+ const watch = { anyBefore: false, prev: false, prevChanged: false };
const lines = head.lifted.split('\n');
for (let k = 1; k < lines.length - 1; k++) watchQuoteLine(watch, lines[k]!);
this.quoteWatch = watch;
@@ -349,7 +349,7 @@ export class BodyScanner {
}
if (this.quoteWatch && inFile && this.fileOffset(start) >= this.headEnd) {
if (text === '---' || (text === '---\r' && hasNewline)) {
- if (this.quoteWatch.anyBefore || this.quoteWatch.prevStripped) return this.unbounded('quote_fence');
+ if (this.quoteWatch.anyBefore || this.quoteWatch.prevChanged) return this.unbounded('quote_fence');
this.quoteWatch = null;
} else {
watchQuoteLine(this.quoteWatch, text);
@@ -535,13 +535,14 @@ export class BodyScanner {
/**
* The pre-quote pass's view of fence lines when its fence did not close in the
- * head: whether any line so far would be rewritten. Every line keeps its CR
- * except the one just before the closing `---`, whose CR the regex absorbs.
+ * head: whether closing it on the next line would change any line so far.
+ * Every line keeps its CR except the one just before the closing `---`, which
+ * the pass rebuilds without its CR (and then may rewrite).
*/
function watchQuoteLine(watch: QuoteWatch, text: string): void {
watch.anyBefore ||= watch.prev;
watch.prev = quoteAmbiguousFrontmatterLine(text) !== null;
- watch.prevStripped = quoteAmbiguousFrontmatterLine(text.endsWith('\r') ? text.slice(0, -1) : text) !== null;
+ watch.prevChanged = text.endsWith('\r') || quoteAmbiguousFrontmatterLine(text) !== null;
}
/** Pass 1 over a streamed file. */
diff --git a/test/large-pages/body-scan.test.ts b/test/large-pages/body-scan.test.ts
index 7f5447c8ac..301aa7867d 100644
--- a/test/large-pages/body-scan.test.ts
+++ b/test/large-pages/body-scan.test.ts
@@ -108,7 +108,7 @@ describe('body scan vs parseMarkdown (whole file within the head bound)', () =>
fc.assert(fc.property(docArb, fc.constantFrom(...PATHS), fc.boolean(), (doc, path, validate) => {
expectAgrees(render(doc), path, { validate });
}), { numRuns: 3000 });
- });
+ }, 60_000);
test('named shapes', () => {
const shapes = [
@@ -155,7 +155,8 @@ describe('body scan on files larger than the head bound', () => {
const got = capture(() => scanMarkdownString(content, 'notes/x.md', { validate }));
if (!('thrown' in got) && got.kind === 'frontmatter_head_unbounded') {
refused++;
- expect(content.trimStart().startsWith('-') || content.startsWith('\uFEFF-')).toBe(true);
+ const head = content.slice(0, FRONTMATTER_HEAD_MAX_BYTES);
+ expect(!/\S/.test(head) || /^\s*-/.test(head)).toBe(true);
return;
}
expectAgrees(content, 'notes/x.md', { validate });
@@ -180,6 +181,9 @@ describe('body scan on files larger than the head bound', () => {
expect(scanMarkdownString(blockRewrite).kind).toBe('frontmatter_head_unbounded');
expectAgrees(`---\ntitle: x\n--- \n${pad('prose')}---\nend\n`, 'notes/x.md', { validate: true });
expectAgrees(`---\ntitle: x\n--- \n${pad('Note: one: two')}end\n`, 'notes/x.md', { validate: true });
+ // The pass rebuilds the line before its closing `---` without the CR, so a CRLF body line changes too.
+ const crlf = `---\r\n---\r\n${pad('## notes\r')}---`;
+ expect(scanMarkdownString(crlf).kind).toBe('frontmatter_head_unbounded');
});
test('the validator fence must close in the head only under validate', () => {