Skip to content

Commit a52e7f2

Browse files
committed
fix: tighten adjacent function response stripping
1 parent 7ca11f5 commit a52e7f2

3 files changed

Lines changed: 18 additions & 24 deletions

File tree

src/agents/pi-embedded-helpers.sanitizeuserfacingtext.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,13 @@ describe("sanitizeUserFacingText", () => {
346346
expect(sanitizeUserFacingText(input)).toBe("After");
347347
});
348348

349+
it("strips adjacent function response payloads that match explanation wording", () => {
350+
const input =
351+
'<function_calls><invoke name="exec">internal</invoke></function_calls><function_response> response wrapper secret</function_response>\nAfter';
352+
353+
expect(sanitizeUserFacingText(input)).toBe("After");
354+
});
355+
349356
it("strips dangling same-line function response payloads with leading spaces", () => {
350357
const input =
351358
'Checking. <function_calls><invoke name="exec">internal</invoke></function_calls><function_response> raw output';

src/shared/text/assistant-visible-text.test.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -685,22 +685,25 @@ describe("stripToolCallXmlTags", () => {
685685
expect(stripToolCallXmlTags(input, { stripFunctionCallsXmlPayloads: true })).toBe("");
686686
});
687687

688-
it("preserves literal function_response prose after a stripped tool-call block", () => {
688+
it("strips function_response-looking prose adjacent to a stripped tool-call block", () => {
689689
const input =
690690
'<tool_call>{"name":"exec"}</tool_call>\n\n<function_response> is the response wrapper.';
691691

692-
expect(stripToolCallXmlTags(input, { stripFunctionCallsXmlPayloads: true })).toBe(
693-
"\n\n<function_response> is the response wrapper.",
694-
);
692+
expect(stripToolCallXmlTags(input, { stripFunctionCallsXmlPayloads: true })).toBe("\n\n");
695693
});
696694

697-
it("preserves literal closed function_response prose after a stripped tool-call block", () => {
695+
it("strips closed function_response-looking prose adjacent to a stripped tool-call block", () => {
698696
const input =
699697
'<tool_call>{"name":"exec"}</tool_call>\n<function_response> is the response wrapper; close it with </function_response>.';
700698

701-
expect(stripToolCallXmlTags(input, { stripFunctionCallsXmlPayloads: true })).toBe(
702-
"\n<function_response> is the response wrapper; close it with </function_response>.",
703-
);
699+
expect(stripToolCallXmlTags(input, { stripFunctionCallsXmlPayloads: true })).toBe("\n.");
700+
});
701+
702+
it("strips adjacent function_response payloads that match explanation wording", () => {
703+
const input =
704+
'<function_calls><invoke name="exec">internal</invoke></function_calls><function_response> response wrapper secret</function_response>\nAfter';
705+
706+
expect(stripToolCallXmlTags(input, { stripFunctionCallsXmlPayloads: true })).toBe("\nAfter");
704707
});
705708

706709
it("strips compact function_response wrappers while preserving same-line prose tails", () => {

src/shared/text/assistant-visible-text.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -204,21 +204,6 @@ function hasSameLineContentAfterOpeningTag(text: string, tag: ParsedToolCallTag)
204204
return after < text.length && text[after] !== "\n" && text[after] !== "\r";
205205
}
206206

207-
function isLikelyFunctionResponseTagProse(
208-
text: string,
209-
tag: ParsedToolCallTag,
210-
closeStart: number,
211-
): boolean {
212-
if (text[tag.end] !== " " && text[tag.end] !== "\t") {
213-
return false;
214-
}
215-
const body = text.slice(tag.end, closeStart === -1 ? text.length : closeStart);
216-
if (body.includes("\n") || body.includes("\r")) {
217-
return false;
218-
}
219-
return /\b(?:response wrapper|wrapper syntax|xml tag|tag syntax)\b/i.test(body);
220-
}
221-
222207
function isVisibleLineStart(text: string): boolean {
223208
let idx = text.length - 1;
224209
while (idx >= 0 && (text[idx] === " " || text[idx] === "\t")) {
@@ -388,7 +373,6 @@ export function stripToolCallXmlTags(
388373
: -1;
389374
const shouldStripAdjacentResult =
390375
isAdjacentToStrippedToolCallBlock(text, idx, lastStrippedToolCallBlockEnd) &&
391-
!isLikelyFunctionResponseTagProse(text, tag, functionResponseCloseStart) &&
392376
(isOpeningTagFollowedByLineBreak(text, tag) ||
393377
functionResponseCloseStart !== -1 ||
394378
hasSameLineContentAfterOpeningTag(text, tag));

0 commit comments

Comments
 (0)