@@ -31,6 +31,32 @@ const QA_EMPTY_RESPONSE_RETRY_INSTRUCTION =
3131 "The previous attempt did not produce a user-visible answer. Continue from the current state and produce the visible answer now. Do not restart from scratch." ;
3232const QA_SETTLED_TOOL_TERMINAL_CONTINUATION_INSTRUCTION =
3333 "The previous assistant turn completed its tool calls but did not produce a user-visible answer. Continue from the current transcript and produce the final user-visible answer now. Do not repeat completed tool calls or restart from scratch." ;
34+ const QA_COMPACTION_RETRY_CODE_MODE_WRITE_RESULT = {
35+ status : "completed" ,
36+ value : {
37+ changed : true ,
38+ created : true ,
39+ diff : "+1 Replay safety: unsafe after write." ,
40+ patch : [
41+ "--- compaction-retry-summary.txt" ,
42+ "+++ compaction-retry-summary.txt" ,
43+ "@@ -0,0 +1,1 @@" ,
44+ "+Replay safety: unsafe after write." ,
45+ "" ,
46+ ] . join ( "\n" ) ,
47+ firstChangedLine : 1 ,
48+ } ,
49+ output : [ ] ,
50+ replaySafe : false ,
51+ telemetry : {
52+ catalogSize : 32 ,
53+ sources : { openclaw : 32 , mcp : 0 , client : 0 } ,
54+ counterScope : "qaFixtureScope01" ,
55+ searchCount : 0 ,
56+ describeCount : 0 ,
57+ callCount : 1 ,
58+ } ,
59+ } as const ;
3460
3561afterEach ( async ( ) => {
3662 while ( cleanups . length > 0 ) {
@@ -2267,6 +2293,146 @@ describe("qa mock openai server", () => {
22672293 expect ( outputText ( await finalReply . json ( ) ) ) . toBe ( "Protocol note: replay unsafe after write." ) ;
22682294 } ) ;
22692295
2296+ it ( "finishes Anthropic Code Mode compaction retry after the structured write result" , async ( ) => {
2297+ const server = await startMockServer ( ) ;
2298+ const callId = "toolu_compaction_retry_write" ;
2299+ const response = await postJson ( server , "/v1/messages" , {
2300+ model : "claude-opus-4-8" ,
2301+ max_tokens : 256 ,
2302+ messages : [
2303+ makeAnthropicUserText (
2304+ "Compaction retry mutating tool check: read COMPACTION_RETRY_CONTEXT.md, then create compaction-retry-summary.txt and keep replay safety explicit." ,
2305+ ) ,
2306+ {
2307+ role : "assistant" ,
2308+ content : [ { type : "tool_use" , id : callId , name : "exec" , input : { } } ] ,
2309+ } ,
2310+ makeAnthropicToolResult ( callId , JSON . stringify ( QA_COMPACTION_RETRY_CODE_MODE_WRITE_RESULT ) ) ,
2311+ ] ,
2312+ } ) ;
2313+
2314+ expect ( response . status ) . toBe ( 200 ) ;
2315+ const body = requireRecord ( await response . json ( ) , "Anthropic compaction retry response" ) ;
2316+ expect ( body . stop_reason ) . toBe ( "end_turn" ) ;
2317+ expect ( requireArray ( body . content , "Anthropic response content" ) ) . toContainEqual ( {
2318+ type : "text" ,
2319+ text : "Protocol note: replay unsafe after write." ,
2320+ } ) ;
2321+ } ) ;
2322+
2323+ it ( "accepts semantic Code Mode write evidence across diff formatting changes" , async ( ) => {
2324+ const server = await startMockServer ( ) ;
2325+ const callId = "toolu_compaction_retry_write_semantic" ;
2326+ const response = await postJson ( server , "/v1/messages" , {
2327+ model : "claude-opus-4-8" ,
2328+ max_tokens : 256 ,
2329+ messages : [
2330+ makeAnthropicUserText (
2331+ "Compaction retry mutating tool check: read COMPACTION_RETRY_CONTEXT.md, then create compaction-retry-summary.txt and keep replay safety explicit." ,
2332+ ) ,
2333+ {
2334+ role : "assistant" ,
2335+ content : [ { type : "tool_use" , id : callId , name : "exec" , input : { } } ] ,
2336+ } ,
2337+ makeAnthropicToolResult (
2338+ callId ,
2339+ JSON . stringify ( {
2340+ ...QA_COMPACTION_RETRY_CODE_MODE_WRITE_RESULT ,
2341+ value : {
2342+ ...QA_COMPACTION_RETRY_CODE_MODE_WRITE_RESULT . value ,
2343+ diff : "display formatting may change independently" ,
2344+ patch : [
2345+ "--- compaction-retry-summary.txt" ,
2346+ "+++ compaction-retry-summary.txt" ,
2347+ "@@ -0,0 +1 @@" ,
2348+ "+Replay safety: unsafe after write." ,
2349+ ] . join ( "\r\n" ) ,
2350+ } ,
2351+ } ) ,
2352+ ) ,
2353+ ] ,
2354+ } ) ;
2355+
2356+ expect ( response . status ) . toBe ( 200 ) ;
2357+ const body = requireRecord ( await response . json ( ) , "Anthropic compaction retry response" ) ;
2358+ expect ( requireArray ( body . content , "Anthropic response content" ) ) . toContainEqual ( {
2359+ type : "text" ,
2360+ text : "Protocol note: replay unsafe after write." ,
2361+ } ) ;
2362+ } ) ;
2363+
2364+ it . each ( [
2365+ {
2366+ label : "failed status" ,
2367+ result : { ...QA_COMPACTION_RETRY_CODE_MODE_WRITE_RESULT , status : "failed" } ,
2368+ } ,
2369+ {
2370+ label : "replay-safe result" ,
2371+ result : { ...QA_COMPACTION_RETRY_CODE_MODE_WRITE_RESULT , replaySafe : true } ,
2372+ } ,
2373+ {
2374+ label : "unchanged result" ,
2375+ result : {
2376+ ...QA_COMPACTION_RETRY_CODE_MODE_WRITE_RESULT ,
2377+ value : { changed : false } ,
2378+ } ,
2379+ } ,
2380+ {
2381+ label : "wrong target patch" ,
2382+ result : {
2383+ ...QA_COMPACTION_RETRY_CODE_MODE_WRITE_RESULT ,
2384+ value : {
2385+ ...QA_COMPACTION_RETRY_CODE_MODE_WRITE_RESULT . value ,
2386+ patch :
2387+ "--- other.txt\n+++ other.txt\n@@ -0,0 +1,1 @@\n+Replay safety: unsafe after write.\n" ,
2388+ } ,
2389+ } ,
2390+ } ,
2391+ {
2392+ label : "content added under another target" ,
2393+ result : {
2394+ ...QA_COMPACTION_RETRY_CODE_MODE_WRITE_RESULT ,
2395+ value : {
2396+ ...QA_COMPACTION_RETRY_CODE_MODE_WRITE_RESULT . value ,
2397+ patch : [
2398+ "--- compaction-retry-summary.txt" ,
2399+ "+++ compaction-retry-summary.txt" ,
2400+ "@@ -0,0 +1 @@" ,
2401+ "+Unrelated content." ,
2402+ "--- other.txt" ,
2403+ "+++ other.txt" ,
2404+ "@@ -0,0 +1 @@" ,
2405+ "+Replay safety: unsafe after write." ,
2406+ ] . join ( "\n" ) ,
2407+ } ,
2408+ } ,
2409+ } ,
2410+ ] ) ( "rejects Anthropic Code Mode compaction retry result with $label" , async ( { result } ) => {
2411+ const server = await startMockServer ( ) ;
2412+ const callId = "toolu_compaction_retry_invalid" ;
2413+ const response = await postJson ( server , "/v1/messages" , {
2414+ model : "claude-opus-4-8" ,
2415+ max_tokens : 256 ,
2416+ messages : [
2417+ makeAnthropicUserText (
2418+ "Compaction retry mutating tool check: read COMPACTION_RETRY_CONTEXT.md, then create compaction-retry-summary.txt and keep replay safety explicit." ,
2419+ ) ,
2420+ {
2421+ role : "assistant" ,
2422+ content : [ { type : "tool_use" , id : callId , name : "exec" , input : { } } ] ,
2423+ } ,
2424+ makeAnthropicToolResult ( callId , JSON . stringify ( result ) ) ,
2425+ ] ,
2426+ } ) ;
2427+
2428+ expect ( response . status ) . toBe ( 200 ) ;
2429+ const body = requireRecord ( await response . json ( ) , "Anthropic compaction retry response" ) ;
2430+ expect ( requireArray ( body . content , "Anthropic response content" ) ) . not . toContainEqual ( {
2431+ type : "text" ,
2432+ text : "Protocol note: replay unsafe after write." ,
2433+ } ) ;
2434+ } ) ;
2435+
22702436 it ( "does not finish a compacted retry when failure text quotes the write success marker" , async ( ) => {
22712437 const server = await startMockServer ( ) ;
22722438
0 commit comments