diff --git a/CLAUDE.md b/CLAUDE.md index e0198f1b5f..e8de79249d 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -317,6 +317,11 @@ prompt's query is unchanged. The Injection Gate scores only the first `RERANK_QU of that combined query, so for a terse prompt it sees the prompt and the start of the reply tail. Measured in `docs/benchmarks/2026-11-08-terse-prompt-query.md`. +### Model reply lists +`getXmlChildren()` (`src/prompts/xml.ts`) keeps list items separate when a model closes one +with the wrong tag. It drops the trailing mismatched close from that item and preserves inline +markup in correctly closed items. This applies to new replies; it does not rewrite stored records. + ### Session Summaries in Injection After the gate, `renderWithSessionDecisions` (`src/functions/prompt-context.ts`) gives an Observation hit that its Session Summary covers (`summaryCovers()`, the same `coveredThrough` diff --git a/src/prompts/xml.ts b/src/prompts/xml.ts index bb0eec36ce..a4af56ea64 100644 --- a/src/prompts/xml.ts +++ b/src/prompts/xml.ts @@ -45,6 +45,9 @@ export function getXmlTag(xml: string, tag: string): string { return firstElementContent(xml, tag)?.trim() ?? ""; } +// Models sometimes close an item with the wrong tag (`x`). +const STRAY_TRAILING_CLOSE = /<\/[a-zA-Z_][a-zA-Z0-9_-]*\s*>\s*(?:)[\s\S])*-->\s*)*$/; + export function getXmlChildren( xml: string, parentTag: string, @@ -53,14 +56,34 @@ export function getXmlChildren( if (!VALID_TAG.test(parentTag) || !VALID_TAG.test(childTag)) return []; const parent = firstElementContent(xml, parentTag); if (parent === null) return []; - const items: string[] = []; - const re = new RegExp( - `${openTag(childTag)}([\\s\\S]*?)${closeTag(childTag)}`, + const tokens = new RegExp( + `${String.raw`||<\?[\s\S]*?\?>`}|(${openTag(childTag)})|(${closeTag(childTag)})`, "g", ); - let m; - while ((m = re.exec(parent)) !== null) { - items.push(m[1].trim()); + const children: { start: number; contentStart: number; close?: number }[] = []; + const open: (typeof children)[number][] = []; + for (const match of parent.matchAll(tokens)) { + if (match[1] !== undefined) { + const child = { start: match.index, contentStart: match.index + match[0].length }; + children.push(child); + open.push(child); + } else if (match[2] !== undefined) { + const child = open.pop(); + if (child) child.close = match.index; + } + } + + const items: string[] = []; + let skipBefore = 0; + for (const [i, child] of children.entries()) { + if (child.start < skipBefore) continue; + if (child.close !== undefined) { + items.push(parent.slice(child.contentStart, child.close).trim()); + skipBefore = child.close; + } else { + const end = children[i + 1]?.start ?? parent.length; + items.push(parent.slice(child.contentStart, end).replace(STRAY_TRAILING_CLOSE, "").trim()); + } } return items; } diff --git a/test/xml.test.ts b/test/xml.test.ts index 4f023ab52a..3f36b0524b 100644 --- a/test/xml.test.ts +++ b/test/xml.test.ts @@ -79,6 +79,38 @@ describe('getXmlChildren', () => { it('returns empty for invalid parent tag name', () => { expect(getXmlChildren('A', '.*', 'fact')).toEqual([]) }) + + // #472: models sometimes close an item with the wrong tag; the lazy match + // then fused it with the next item and stored the stray tag in the field. + it('splits an item closed with the wrong tag from the next one', () => { + const xml = 'VAAPI renderD128\n Jellyfin acceleration' + expect(getXmlChildren(xml, 'concepts', 'concept')).toEqual(['VAAPI renderD128', 'Jellyfin acceleration']) + }) + + it('keeps a last item closed with the wrong tag', () => { + const xml = 'CPALalsa.headroom\n' + expect(getXmlChildren(xml, 'concepts', 'concept')).toEqual(['CPAL', 'alsa.headroom']) + }) + + it('preserves nested markup with the same child tag', () => { + const xml = 'Use inner hereNext' + expect(getXmlChildren(xml, 'facts', 'fact')).toEqual(['Use inner here', 'Next']) + }) + + it('ignores child delimiters inside CDATA, comments and processing instructions', () => { + const item = ']]>?>Value' + expect(getXmlChildren(`${item}`, 'facts', 'fact')).toEqual([item]) + }) + + it('drops inter-item comments after a mismatched close', () => { + const xml = 'FirstNext' + expect(getXmlChildren(xml, 'facts', 'fact')).toEqual(['First', 'Next']) + }) + + it('keeps inline markup inside a properly closed item', () => { + const xml = 'Wrap it in x' + expect(getXmlChildren(xml, 'decisions', 'decision')).toEqual(['Wrap it in x']) + }) }) describe('getXmlPayload', () => {