Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: drop tilde-block tracking from the closing-fence search (concatenated cut windows double-charged the scan budget)

garrytane3ac3855e7b6
diff --git a/src/core/search/transcript-quote.ts b/src/core/search/transcript-quote.ts
index 567e1930a9..cd1d3b13e2 100644
--- a/src/core/search/transcript-quote.ts
+++ b/src/core/search/transcript-quote.ts
@@ -556,7 +556,7 @@ function unquoteMarked(text: string, find: (from: number) => { i: number; length
const { i, length } = found;
const open = /^(`{3,})text\n/.exec(text.slice(i + length));
const start = i + length + (open?.[0].length ?? 0);
- const close = open ? closingLine(text, start, open[1], work, true) ?? closingLine(text, start, open[1], work, false) : null;
+ const close = open ? closingLine(text, start, open[1], work) : null;
if (close === 'spent') break;
if (!close && unclosed && open && at === 0 && !text.slice(0, i).trim()) return text.slice(start);
if (!close) { out += text.slice(at, i + length); at = i + length; continue; }
@@ -570,13 +570,12 @@ function unquoteMarked(text: string, find: (from: number) => { i: number; length
* #8384: where the quote fence opened at `start` closes: the first line that is exactly `fence` (trailing blanks allowed,
* or the next quote's marker right after it, as concatenated windows join) outside a code block the text opens itself,
* so an agent's added block (```js … ```) is kept whole. A host's quoted text never holds a backtick run as long as its
- * fence, so a line of exactly `fence` inside a shorter backtick block still closes it; a tilde block holds it only while
- * `tilde` is set, and the caller searches again without it when the text leaves one unclosed (a host's cut window).
+ * fence, so a line of exactly `fence` inside a shorter or tilde block (an unclosed one in a cut window too) still closes it.
* `end` is the newline before that line and `after` the end of the fence and its blanks. Every search of one text shares
* `work` (twice the text's length): closes found are disjoint, so only fences that never close spend it twice, and past
* it ('spent') the rest of the text is kept as written, so hostile content of many unclosed markers stays linear.
*/
-function closingLine(text: string, start: number, fence: string, work: { left: number }, tilde: boolean): { end: number; after: number } | null | 'spent' {
+function closingLine(text: string, start: number, fence: string, work: { left: number }): { end: number; after: number } | null | 'spent' {
let inner: { char: string; length: number } | null = null;
for (let pos = start; pos <= text.length;) {
const nl = text.indexOf('\n', pos);
@@ -584,7 +583,7 @@ function closingLine(text: string, start: number, fence: string, work: { left: n
if ((work.left -= line.length + 1) < 0) return 'spent';
const bare = /^(`{3,}|~{3,})[ \t]*\r?$/.exec(line);
const closes = bare?.[1] === fence || line.startsWith(`${fence}${MARKER_START}`);
- if (pos > start && closes && (!inner || (inner.char === '`' ? inner.length < fence.length : !tilde))) return { end: pos - 1, after: pos + (bare ? line.replace(/\r$/, '').length : fence.length) };
+ if (pos > start && closes && (!inner || inner.char !== '`' || inner.length < fence.length)) return { end: pos - 1, after: pos + (bare ? line.replace(/\r$/, '').length : fence.length) };
const run = /^(`{3,}|~{3,})/.exec(line)?.[1];
if (inner) { if (bare && bare[1][0] === inner.char && bare[1].length >= inner.length) inner = null; }
else if (run && !(run[0] === '`' && line.slice(run.length).includes('`'))) inner = { char: run[0], length: run.length };
diff --git a/test/search-transcript-quote-8384.test.ts b/test/search-transcript-quote-8384.test.ts
index 1cb7de158d..8297aeda7e 100644
--- a/test/search-transcript-quote-8384.test.ts
+++ b/test/search-transcript-quote-8384.test.ts
@@ -79,9 +79,7 @@ for (const backend of testBackends()) {
test('a code block an agent adds inside, in place of the closing fence, or after the read is stored intact', async () => {
const block = '```js\nconst x = 1;\n```';
- const tilde = '~~~md\n```\n~~~';
const cases: Array<[string, (c: string) => string, string]> = [
- ['notes/tilde-inside', c => c.replace('Second line.', `Second line.\n\n${tilde}`), `Second line.\n\n${tilde}`],
['notes/block-inside', c => c.replace('Second line.', `Second line.\n\n${block}`), `Second line.\n\n${block}`],
['notes/block-replaces-closer', c => `${dropCloser(c)}${block}\n`, `Second line.\n\n${block}`],
['notes/block-after', c => `${c}\n\n${block}\n`, `Second line.\n\n${block}`],
@@ -163,6 +161,12 @@ describe('#8384: a host\'s own quoted text unquotes exactly, whatever code fence
}
});
+ test('concatenated cut windows that leave a tilde block open all unquote', () => {
+ const pad = 'filler line\n'.repeat(50);
+ const parts = [1, 2, 3, 4, 5].map(k => fenceTranscriptText(`${pad}window ${k}\n~~~\ncut code ${k}`, 'slack/a'));
+ expect(unquoteTranscriptText(parts.join('\n'), 'slack/a', true)).toBe([1, 2, 3, 4, 5].map(k => `${pad}window ${k}\n~~~\ncut code ${k}`).join('\n'));
+ });
+
test('a megabyte of markers that never close is kept as written in linear time', () => {
const fence = fenceTranscriptText('x', 'slack/a').split('\n').slice(0, 2).join('\n');
const hostile = `intro\n${`${fence}\n`.repeat(Math.ceil(1_048_576 / (fence.length + 1)))}`;