Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(timeline): linear HTML-comment handling in the inline-citation parser (#6184)

garrytanbb480fdb9c30
diff --git a/src/core/timeline-citations.ts b/src/core/timeline-citations.ts
index 0b9cb37978..23c7f21643 100644
--- a/src/core/timeline-citations.ts
+++ b/src/core/timeline-citations.ts
@@ -13,10 +13,32 @@ interface CitationParagraph {
}
/** #6184: HTML comments (section markers, materialized-row markers) are markup, never summary text. */
-const HTML_COMMENT_RE = /<!--[\s\S]*?-->/g;
-const COMMENT_ONLY_LINE_RE = /^\s*(?:<!--[\s\S]*?-->\s*)+$/;
+/**
+ * A line made only of comments: once trimmed it opens with `<!--` and closes
+ * with a later `-->`. String checks, not a nested lazy regex, so a run of
+ * empty comments ending in text cannot backtrack catastrophically.
+ */
+function isCommentOnlyLine(line: string): boolean {
+ const trimmed = line.trim();
+ return trimmed.length >= 7 && trimmed.startsWith('<!--') && trimmed.endsWith('-->');
+}
+
+/**
+ * Each `<!-- … -->` becomes a space (the first `-->` after the opener closes
+ * it), then any stray `<!--` / `-->` does. One forward scan: an opener with no
+ * later `-->` ends the scan, so unclosed openers cost linear time.
+ */
export function stripHtmlComments(text: string): string {
- return text.replace(HTML_COMMENT_RE, ' ').replace(/<!--|-->/g, ' ');
+ let out = '';
+ let at = 0;
+ for (;;) {
+ const open = text.indexOf('<!--', at);
+ const close = open < 0 ? -1 : text.indexOf('-->', open + 4);
+ if (close < 0) break;
+ out += `${text.slice(at, open)} `;
+ at = close + 3;
+ }
+ return (out + text.slice(at)).replace(/<!--|-->/g, ' ');
}
function startsMarkdownBlock(line: string): boolean {
@@ -38,7 +60,7 @@ function citationParagraphs(
};
for (const line of stripCodeBlocks(content).split(/\r?\n/)) {
- if (line.trim().length === 0 || COMMENT_ONLY_LINE_RE.test(line)) {
+ if (line.trim().length === 0 || isCommentOnlyLine(line)) {
flush();
continue;
}
diff --git a/test/timeline-citations.test.ts b/test/timeline-citations.test.ts
index 8bc84a4019..2102d510cd 100644
--- a/test/timeline-citations.test.ts
+++ b/test/timeline-citations.test.ts
@@ -1,6 +1,6 @@
/** #6184: an HTML comment next to an inline citation is markup, never a timeline summary. */
import { describe, expect, test } from 'bun:test';
-import { parseInlineCitationTimelineEntries } from '../src/core/timeline-citations.ts';
+import { parseInlineCitationTimelineEntries, stripHtmlComments } from '../src/core/timeline-citations.ts';
import { renderMaterializedBullet } from '../src/core/persistence/canonical-projections.ts';
describe('#6184 inline-citation timeline parsing', () => {
@@ -34,3 +34,30 @@ describe('#6184 write-back guard', () => {
expect(renderMaterializedBullet({ date: '2026-10-04', source: 'Slack import', summary: 'Talked with alice-example.' }, 'notes/x')).not.toBeNull();
});
});
+
+describe('#6184 comment handling stays linear (security review: a remote put_page body must not stall the parser)', () => {
+ test('lines of empty comment runs that end in text parse in milliseconds', () => {
+ const body = `Met alice-example. [Source: Slack import, 2026-10-04]\n${Array.from({ length: 20 }, () => `${'<!---->'.repeat(40)}x`).join('\n')}`;
+ const started = performance.now();
+ const entries = parseInlineCitationTimelineEntries(body);
+ expect(performance.now() - started).toBeLessThan(1500);
+ expect(entries.map(e => e.date)).toEqual(['2026-10-04']);
+ }, 60_000);
+
+ test('many unclosed comment openers strip in linear time with the same result as before', () => {
+ const opened = `${'<!--'.repeat(60_000)} tail`;
+ const started = performance.now();
+ const stripped = stripHtmlComments(opened);
+ expect(performance.now() - started).toBeLessThan(1500);
+ expect(stripped.trim()).toBe('tail');
+ expect(stripHtmlComments('a <!-- x --> b <!-- y')).toBe('a b y');
+ expect(stripHtmlComments('<!---->a<!-->b-->c')).toBe(' a c');
+ expect(stripHtmlComments('a --> b <!-- c --> d')).toBe('a b d');
+ }, 60_000);
+
+ test('a line that opens and closes with comment markup is still a boundary, as before', () => {
+ expect(parseInlineCitationTimelineEntries('[Source: Slack import, 2026-10-04]\n <!-- a --> <!-- b --> ')).toEqual([]);
+ expect(parseInlineCitationTimelineEntries('Met alice-example. [Source: Slack import, 2026-10-04]\n<!-- a --> more -->'))
+ .toEqual([{ date: '2026-10-04', source: 'Slack import', summary: 'Met alice-example.' }]);
+ });
+});