Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
35 changes: 29 additions & 6 deletions src/prompts/xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (`<concept>x</color>`).
const STRAY_TRAILING_CLOSE = /<\/[a-zA-Z_][a-zA-Z0-9_-]*\s*>\s*(?:<!--(?:(?!-->)[\s\S])*-->\s*)*$/;

export function getXmlChildren(
xml: string,
parentTag: string,
Expand All @@ -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]*?-->|<!\[CDATA\[[\s\S]*?\]\]>|<\?[\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;
}
Expand Down
32 changes: 32 additions & 0 deletions test/xml.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,38 @@ describe('getXmlChildren', () => {
it('returns empty for invalid parent tag name', () => {
expect(getXmlChildren('<facts><fact>A</fact></facts>', '.*', '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 = '<concepts><concept>VAAPI renderD128</color>\n <concept>Jellyfin acceleration</concept></concepts>'
expect(getXmlChildren(xml, 'concepts', 'concept')).toEqual(['VAAPI renderD128', 'Jellyfin acceleration'])
})

it('keeps a last item closed with the wrong tag', () => {
const xml = '<concepts><concept>CPAL</concept><concept>alsa.headroom</cpu>\n</concepts>'
expect(getXmlChildren(xml, 'concepts', 'concept')).toEqual(['CPAL', 'alsa.headroom'])
})

it('preserves nested markup with the same child tag', () => {
const xml = '<facts><fact>Use <fact>inner</fact> here</fact><fact>Next</fact></facts>'
expect(getXmlChildren(xml, 'facts', 'fact')).toEqual(['Use <fact>inner</fact> here', 'Next'])
})

it('ignores child delimiters inside CDATA, comments and processing instructions', () => {
const item = '<![CDATA[<fact>]]><!-- </fact><fact> --><?example <fact>?>Value'
expect(getXmlChildren(`<facts><fact>${item}</fact></facts>`, 'facts', 'fact')).toEqual([item])
})

it('drops inter-item comments after a mismatched close', () => {
const xml = '<facts><fact>First</typo><!-- note --><fact>Next</fact></facts>'
expect(getXmlChildren(xml, 'facts', 'fact')).toEqual(['First', 'Next'])
})

it('keeps inline markup inside a properly closed item', () => {
const xml = '<decisions><decision>Wrap it in <code>x</code></decision></decisions>'
expect(getXmlChildren(xml, 'decisions', 'decision')).toEqual(['Wrap it in <code>x</code>'])
})
})

describe('getXmlPayload', () => {
Expand Down