Skip to content

Commit bb43f3b

Browse files
fix(clients): keep typed context XML in answer history
Only an envelope that directly follows its own provider marker, with a matching ref id, is folded into the marker's label. Context XML a user typed into an answer is shown as written, as on main. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 3798ffb commit bb43f3b

2 files changed

Lines changed: 23 additions & 12 deletions

File tree

‎packages/client-runtime/src/work-log/userInput.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,9 @@ describe("question answer display", () => {
3131
expect(history).toBe("See my note: [Review comment: notes.md L2]");
3232
expect(preview).toContain("See my note:");
3333
expect(preview).not.toContain("<t3_context");
34+
35+
const typedXml =
36+
'Use this XML literally: <t3_context version="1"><context kind="review-comment" id="example">keep me</context></t3_context>';
37+
expect(getQuestionAnswerText(typedXml)).toBe(typedXml);
3438
});
3539
});

‎packages/shared/src/composerContextReferences.ts‎

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -427,24 +427,31 @@ export function projectComposerContextForProvider(input: {
427427
return `${body}\n\n<${CONTEXT_ENVELOPE_TAG} version="1">\n${entries.join("\n")}\n</${CONTEXT_ENVELOPE_TAG}>`;
428428
}
429429

430-
const ENVELOPE_OPEN = `<${CONTEXT_ENVELOPE_TAG} version="1">`;
431430
const ENVELOPE_CLOSE = `</${CONTEXT_ENVELOPE_TAG}>`;
432-
const PROVIDER_MARKER = /\[([A-Z][^[\]:;\n]*): ([^[\];\n]*); ref=[^[\]\s]*\]/g;
431+
const PROVIDER_MARKER = /\[([A-Z][^[\]:;\n]*): ([^[\];\n]*); ref=([^[\]\s]+)\]/g;
432+
const MARKER_ENVELOPE_PREFIX = `\n\n<${CONTEXT_ENVELOPE_TAG} version="1">\n<${CONTEXT_ENTRY_TAG} kind="`;
433433

434-
/** Readable form of `projectComposerContextForProvider` output: markers keep their label, envelopes go. */
434+
/**
435+
* Readable form of a single-reference `projectComposerContextForProvider` output, as question
436+
* answers store it. Only an envelope right after its own marker is removed; typed XML stays.
437+
*/
435438
export function composerContextProviderTextForDisplay(text: string): string {
436439
let result = "";
437440
let cursor = 0;
438-
for (;;) {
439-
const start = text.indexOf(ENVELOPE_OPEN, cursor);
440-
if (start === -1) break;
441-
const end = text.indexOf(ENVELOPE_CLOSE, start);
442-
if (end === -1) break;
443-
result += text.slice(cursor, start).trimEnd();
444-
cursor = end + ENVELOPE_CLOSE.length;
441+
for (const match of text.matchAll(PROVIDER_MARKER)) {
442+
if (match.index < cursor) continue;
443+
const markerEnd = match.index + match[0].length;
444+
if (!text.startsWith(MARKER_ENVELOPE_PREFIX, markerEnd)) continue;
445+
const kindEnd = text.indexOf('"', markerEnd + MARKER_ENVELOPE_PREFIX.length);
446+
if (kindEnd === -1 || !text.startsWith(` id="${escapeAttribute(match[3]!)}"`, kindEnd + 1)) {
447+
continue;
448+
}
449+
const close = text.indexOf(ENVELOPE_CLOSE, kindEnd);
450+
if (close === -1) continue;
451+
result += `${text.slice(cursor, match.index)}[${match[1]}: ${match[2]}]`;
452+
cursor = close + ENVELOPE_CLOSE.length;
445453
}
446-
if (cursor === 0) return text;
447-
return (result + text.slice(cursor)).replace(PROVIDER_MARKER, "[$1: $2]").trim();
454+
return result + text.slice(cursor);
448455
}
449456

450457
// An agent reads threads on its own machine and on those it links to, even when a draft

0 commit comments

Comments
 (0)