@@ -10,6 +10,7 @@ import (
1010var emptyJSONFencePattern = regexp .MustCompile ("(?is)```json\\ s*```" )
1111var leakedToolCallArrayPattern = regexp .MustCompile (`(?is)\[\{\s*"function"\s*:\s*\{[\s\S]*?\}\s*,\s*"id"\s*:\s*"call[^"]*"\s*,\s*"type"\s*:\s*"function"\s*}\]` )
1212var leakedToolResultBlobPattern = regexp .MustCompile (`(?is)<\s*\|\s*tool\s*\|\s*>\s*\{[\s\S]*?"tool_call_id"\s*:\s*"call[^"]*"\s*}` )
13+ var leakedToolResultSectionPattern = regexp .MustCompile (`(?is)<[\|\x{ff5c}]\s*tool\s*[\|\x{ff5c}]>[\s\S]*?<[\|\x{ff5c}]\s*end[_▁]of[_▁]tool[_▁]?results\s*[\|\x{ff5c}]>` )
1314
1415var leakedThinkTagPattern = regexp .MustCompile (`(?is)</?\s*think\s*>` )
1516
@@ -29,7 +30,8 @@ var leakedThoughtMarkerPattern = regexp.MustCompile(`(?i)<[\|\x{ff5c}]\s*(?:begi
2930// halfwidth or legacy U+FF5C fullwidth delimiters:
3031// - ASCII underscore: <|end_of_sentence|>, <|end_of_toolresults|>, <|end_of_instructions|>
3132// - U+2581 variant: <|end▁of▁sentence|>, <|end▁of▁toolresults|>, <|end▁of▁instructions|>
32- var leakedMetaMarkerPattern = regexp .MustCompile (`(?i)<[\|\x{ff5c}]\s*(?:assistant|tool|end[_▁]of[_▁]sentence|end[_▁]of[_▁]thinking|end[_▁]of[_▁]thought|end[_▁]of[_▁]toolresults|end[_▁]of[_▁]instructions)\s*[\|\x{ff5c}]>` )
33+ // - compound assistant markers: <|Assistant_END_OF_TOOL_CALLS|>
34+ var leakedMetaMarkerPattern = regexp .MustCompile (`(?i)<[\|\x{ff5c}]\s*(?:assistant(?:[_▁]end[_▁]of[_▁]tool[_▁]?calls)?|tool|end[_▁]of[_▁]sentence|end[_▁]of[_▁]thinking|end[_▁]of[_▁]thought|end[_▁]of[_▁]tool[_▁]?results|end[_▁]of[_▁]tool[_▁]?calls|end[_▁]of[_▁]instructions)\s*[\|\x{ff5c}]>` )
3335
3436// leakedAgentXMLBlockPatterns catch agent-style XML blocks that leak through
3537// when the sieve fails to capture them. These are applied only to complete
@@ -52,6 +54,7 @@ func sanitizeLeakedOutput(text string) string {
5254 }
5355 out := emptyJSONFencePattern .ReplaceAllString (text , "" )
5456 out = leakedToolCallArrayPattern .ReplaceAllString (out , "" )
57+ out = leakedToolResultSectionPattern .ReplaceAllString (out , "" )
5558 out = leakedToolResultBlobPattern .ReplaceAllString (out , "" )
5659 out = stripDanglingThinkSuffix (out )
5760 out = leakedThinkTagPattern .ReplaceAllString (out , "" )
0 commit comments