@@ -1331,6 +1331,23 @@ describe("parseCliOutput", () => {
13311331} ) ;
13321332
13331333describe ( "createCliJsonlStreamingParser" , ( ) => {
1334+ function createClaudeTaggedReasoningHarness ( ) {
1335+ const assistant : Array < { text : string ; delta : string } > = [ ] ;
1336+ const thinking : Array < { text : string ; delta : string ; isReasoningSnapshot ?: boolean } > = [ ] ;
1337+ const parser = createCliJsonlStreamingParser ( {
1338+ backend : {
1339+ command : "local-cli" ,
1340+ output : "jsonl" ,
1341+ jsonlDialect : "claude-stream-json" ,
1342+ sessionIdFields : [ "session_id" ] ,
1343+ } ,
1344+ providerId : "local-cli" ,
1345+ onAssistantDelta : ( delta ) => assistant . push ( delta ) ,
1346+ onThinkingDelta : ( delta ) => thinking . push ( delta ) ,
1347+ } ) ;
1348+ return { assistant, parser, thinking } ;
1349+ }
1350+
13341351 it . each ( OPENAI_COMPATIBLE_CLI_USAGE_CASES ) (
13351352 "normalizes $name while incrementally streaming CLI JSONL" ,
13361353 ( { raw, normalized } ) => {
@@ -2011,6 +2028,210 @@ describe("createCliJsonlStreamingParser", () => {
20112028 } ) ;
20122029 } ) ;
20132030
2031+ it ( "promotes complete leading tagged Claude reasoning and keeps only the answer visible" , ( ) => {
2032+ const { assistant, parser, thinking } = createClaudeTaggedReasoningHarness ( ) ;
2033+
2034+ parser . push (
2035+ [
2036+ JSON . stringify ( { type : "stream_event" , event : { type : "message_start" } } ) ,
2037+ JSON . stringify ( {
2038+ type : "stream_event" ,
2039+ event : {
2040+ type : "content_block_delta" ,
2041+ delta : {
2042+ type : "text_delta" ,
2043+ text : "<thinking>Private analysis.</thinking>Visible answer." ,
2044+ } ,
2045+ } ,
2046+ } ) ,
2047+ JSON . stringify ( {
2048+ type : "result" ,
2049+ session_id : "session-tagged" ,
2050+ result : "<thinking>Private analysis.</thinking>Visible answer." ,
2051+ } ) ,
2052+ "" ,
2053+ ] . join ( "\n" ) ,
2054+ ) ;
2055+ parser . finish ( ) ;
2056+
2057+ expect ( thinking ) . toEqual ( [
2058+ {
2059+ text : "Private analysis." ,
2060+ delta : "Private analysis." ,
2061+ isReasoningSnapshot : true ,
2062+ } ,
2063+ ] ) ;
2064+ expect ( assistant ) . toEqual ( [
2065+ {
2066+ text : "Visible answer." ,
2067+ delta : "Visible answer." ,
2068+ sessionId : undefined ,
2069+ usage : undefined ,
2070+ } ,
2071+ ] ) ;
2072+ expect ( parser . getOutput ( ) ) . toEqual ( {
2073+ text : "Visible answer." ,
2074+ sessionId : "session-tagged" ,
2075+ usage : undefined ,
2076+ } ) ;
2077+ } ) ;
2078+
2079+ it ( "holds chunk-split tagged reasoning until its close tag is complete" , ( ) => {
2080+ const { assistant, parser, thinking } = createClaudeTaggedReasoningHarness ( ) ;
2081+ const pushText = ( text : string ) =>
2082+ parser . push (
2083+ `${ JSON . stringify ( {
2084+ type : "stream_event" ,
2085+ event : {
2086+ type : "content_block_delta" ,
2087+ delta : { type : "text_delta" , text } ,
2088+ } ,
2089+ } ) } \n`,
2090+ ) ;
2091+
2092+ pushText ( "<thi" ) ;
2093+ pushText ( "nking>Private " ) ;
2094+ expect ( assistant ) . toEqual ( [ ] ) ;
2095+ expect ( thinking ) . toEqual ( [ ] ) ;
2096+ pushText ( "analysis.</think" ) ;
2097+ expect ( assistant ) . toEqual ( [ ] ) ;
2098+ expect ( thinking ) . toEqual ( [ ] ) ;
2099+ pushText ( "ing>Visible answer." ) ;
2100+ parser . finish ( ) ;
2101+
2102+ expect ( thinking . at ( - 1 ) ?. text ) . toBe ( "Private analysis." ) ;
2103+ expect ( assistant . at ( - 1 ) ?. text ) . toBe ( "Visible answer." ) ;
2104+ expect ( parser . getOutput ( ) ?. text ) . toBe ( "Visible answer." ) ;
2105+ } ) ;
2106+
2107+ it ( "promotes consecutive leading blocks but preserves later literal tags" , ( ) => {
2108+ const { assistant, parser, thinking } = createClaudeTaggedReasoningHarness ( ) ;
2109+
2110+ parser . push (
2111+ `${ JSON . stringify ( {
2112+ type : "stream_event" ,
2113+ event : {
2114+ type : "content_block_delta" ,
2115+ delta : {
2116+ type : "text_delta" ,
2117+ text : [
2118+ "<think>First.</think>" ,
2119+ "<reasoning>Second.</reasoning>" ,
2120+ "Answer with <think>literal</think> markup." ,
2121+ ] . join ( "\n" ) ,
2122+ } ,
2123+ } ,
2124+ } ) } \n`,
2125+ ) ;
2126+ parser . finish ( ) ;
2127+
2128+ expect ( thinking . map ( ( entry ) => entry . text ) ) . toEqual ( [ "First.Second." ] ) ;
2129+ expect ( assistant . at ( - 1 ) ?. text ) . toBe ( "\nAnswer with <think>literal</think> markup." ) ;
2130+ expect ( parser . getOutput ( ) ?. text ) . toBe ( "Answer with <think>literal</think> markup." ) ;
2131+ } ) ;
2132+
2133+ it ( "prefers native Claude thinking over a mirrored leading tagged block" , ( ) => {
2134+ const { assistant, parser, thinking } = createClaudeTaggedReasoningHarness ( ) ;
2135+
2136+ parser . push (
2137+ [
2138+ JSON . stringify ( {
2139+ type : "stream_event" ,
2140+ event : {
2141+ type : "content_block_delta" ,
2142+ index : 0 ,
2143+ delta : { type : "thinking_delta" , thinking : "Native analysis." } ,
2144+ } ,
2145+ } ) ,
2146+ JSON . stringify ( {
2147+ type : "stream_event" ,
2148+ event : {
2149+ type : "content_block_delta" ,
2150+ index : 1 ,
2151+ delta : {
2152+ type : "text_delta" ,
2153+ text : "<thinking>Native analysis.</thinking>Visible answer." ,
2154+ } ,
2155+ } ,
2156+ } ) ,
2157+ JSON . stringify ( {
2158+ type : "result" ,
2159+ result : "<thinking>Native analysis.</thinking>Visible answer." ,
2160+ } ) ,
2161+ "" ,
2162+ ] . join ( "\n" ) ,
2163+ ) ;
2164+ parser . finish ( ) ;
2165+
2166+ expect ( thinking ) . toEqual ( [
2167+ { text : "Native analysis." , delta : "Native analysis." , isReasoningSnapshot : true } ,
2168+ ] ) ;
2169+ expect ( assistant . at ( - 1 ) ?. text ) . toBe ( "Visible answer." ) ;
2170+ expect ( parser . getOutput ( ) ?. text ) . toBe ( "Visible answer." ) ;
2171+ } ) ;
2172+
2173+ it . each ( [
2174+ {
2175+ name : "fenced code example" ,
2176+ text : "```xml\n<thinking>literal example</thinking>\n```" ,
2177+ } ,
2178+ { name : "incomplete leading tag" , text : "<thinking>unfinished visible text" } ,
2179+ { name : "malformed leading tag" , text : "<thinking broken visible text" } ,
2180+ ] ) ( "preserves $name on the visible path" , ( { text } ) => {
2181+ const { assistant, parser, thinking } = createClaudeTaggedReasoningHarness ( ) ;
2182+
2183+ parser . push (
2184+ `${ JSON . stringify ( {
2185+ type : "stream_event" ,
2186+ event : {
2187+ type : "content_block_delta" ,
2188+ delta : { type : "text_delta" , text } ,
2189+ } ,
2190+ } ) } \n`,
2191+ ) ;
2192+ parser . finish ( ) ;
2193+
2194+ expect ( thinking ) . toEqual ( [ ] ) ;
2195+ expect ( assistant . map ( ( entry ) => entry . delta ) . join ( "" ) ) . toBe ( text ) ;
2196+ expect ( parser . getOutput ( ) ?. text ) . toBe ( text ) ;
2197+ } ) ;
2198+
2199+ it ( "resets tagged reasoning across Claude tool-round assistant messages" , ( ) => {
2200+ const { assistant, parser, thinking } = createClaudeTaggedReasoningHarness ( ) ;
2201+ const textEvent = ( text : string ) =>
2202+ JSON . stringify ( {
2203+ type : "stream_event" ,
2204+ event : {
2205+ type : "content_block_delta" ,
2206+ delta : { type : "text_delta" , text } ,
2207+ } ,
2208+ } ) ;
2209+
2210+ parser . push (
2211+ [
2212+ JSON . stringify ( { type : "stream_event" , event : { type : "message_start" } } ) ,
2213+ textEvent ( "<think>First thought.</think>Before tool." ) ,
2214+ JSON . stringify ( {
2215+ type : "stream_event" ,
2216+ event : {
2217+ type : "content_block_start" ,
2218+ content_block : { type : "tool_use" , id : "tool-1" , name : "Read" } ,
2219+ } ,
2220+ } ) ,
2221+ JSON . stringify ( { type : "stream_event" , event : { type : "message_stop" } } ) ,
2222+ JSON . stringify ( { type : "stream_event" , event : { type : "message_start" } } ) ,
2223+ textEvent ( "<reasoning>Second thought.</reasoning>Final answer." ) ,
2224+ JSON . stringify ( { type : "result" , result : "Final answer." } ) ,
2225+ "" ,
2226+ ] . join ( "\n" ) ,
2227+ ) ;
2228+ parser . finish ( ) ;
2229+
2230+ expect ( thinking . map ( ( entry ) => entry . text ) ) . toEqual ( [ "First thought." , "Second thought." ] ) ;
2231+ expect ( assistant . at ( - 1 ) ?. text ) . toBe ( "Before tool.\n\nFinal answer." ) ;
2232+ expect ( parser . getOutput ( ) ?. text ) . toBe ( "Before tool.\n\nFinal answer." ) ;
2233+ } ) ;
2234+
20142235 it ( "streams thinking deltas, skips signature deltas, and dedupes the snapshot" , ( ) => {
20152236 const thinking : Array < { text : string ; delta : string ; isReasoningSnapshot ?: boolean } > = [ ] ;
20162237 const parser = createCliJsonlStreamingParser ( {
0 commit comments