@@ -62,6 +62,8 @@ const QA_COMPACTION_RETRY_CODE_MODE_WRITE_RESULT = {
6262const QA_COMPACTION_RETRY_PROMPT =
6363 "Compaction retry mutating tool check. Current durable context marker: QA-COMPACTION-DURABLE-MARKER. Create compaction-retry-summary.txt." ;
6464const QA_COMPACTION_RETRY_OVERFLOW_PADDING = "x" . repeat ( 300_000 ) ;
65+ const QA_COMPACTION_EMPTY_RECOVERY_SUMMARY_MARKER = "QA-COMPACTION-EMPTY-RECOVERED-SUMMARY" ;
66+ const QA_COMPACTION_REASONING_RECOVERY_SUMMARY_MARKER = "QA-COMPACTION-REASONING-RECOVERED-SUMMARY" ;
6567const QA_COMPACTION_SUMMARY_INSTRUCTIONS = `You are a context summarization assistant. Your task is to read a conversation between a user and an AI assistant, then produce a structured summary following the exact format specified.
6668
6769Do NOT continue the conversation. Do NOT respond to any questions in the conversation. ONLY output the structured summary.` ;
@@ -2350,6 +2352,150 @@ describe("qa mock openai server", () => {
23502352 expect ( summaryRequest . allInputText ) . toContain ( "[Chunk 1 - oldest messages]" ) ;
23512353 } ) ;
23522354
2355+ it . each ( [
2356+ {
2357+ faultMode : "empty-output-once" ,
2358+ markerPrefix : "QA-COMPACTION-EMPTY-OUTPUT-ONCE" ,
2359+ recoveredMarker : QA_COMPACTION_EMPTY_RECOVERY_SUMMARY_MARKER ,
2360+ reasoningOnly : false ,
2361+ } ,
2362+ {
2363+ faultMode : "reasoning-only-output-once" ,
2364+ markerPrefix : "QA-COMPACTION-REASONING-ONLY-OUTPUT-ONCE" ,
2365+ recoveredMarker : QA_COMPACTION_REASONING_RECOVERY_SUMMARY_MARKER ,
2366+ reasoningOnly : true ,
2367+ } ,
2368+ ] as const ) (
2369+ "injects one coded overflow for $faultMode scenario markers" ,
2370+ async ( { markerPrefix } ) => {
2371+ const server = await startMockServer ( ) ;
2372+ const body = {
2373+ model : "gpt-5.6-luna" ,
2374+ instructions : `Runtime: embedded | sessionId=compaction-output-${ markerPrefix } ` ,
2375+ input : [ makeUserInput ( `${ markerPrefix } -http\n${ "x" . repeat ( 100_000 ) } ` ) ] ,
2376+ } ;
2377+
2378+ const first = await postNonStreamingResponses ( server , body ) ;
2379+ expect ( first . status ) . toBe ( 400 ) ;
2380+ expect ( await first . json ( ) ) . toMatchObject ( {
2381+ error : { code : "context_length_exceeded" } ,
2382+ } ) ;
2383+ expect ( ( await postNonStreamingResponses ( server , body ) ) . status ) . toBe ( 200 ) ;
2384+
2385+ const requests = requireArray (
2386+ await getJson ( server , "/debug/requests" ) ,
2387+ "compaction output overflow requests" ,
2388+ ) . map ( ( request ) => requireRecord ( request , "compaction output overflow request" ) ) ;
2389+ expect ( requests ) . toHaveLength ( 2 ) ;
2390+ expect ( Number ( requests [ 0 ] ?. rawByteLength ) ) . toBeLessThan ( 256 * 1024 ) ;
2391+ expect ( requests [ 0 ] ) . toMatchObject ( {
2392+ requestKind : "agent-initial" ,
2393+ compactionSummaryFaultMode : "none" ,
2394+ outcome : "error" ,
2395+ errorCode : "context_length_exceeded" ,
2396+ } ) ;
2397+ expect ( requests [ 1 ] ) . toMatchObject ( {
2398+ requestKind : "agent-initial" ,
2399+ compactionSummaryFaultMode : "none" ,
2400+ outcome : "success" ,
2401+ } ) ;
2402+ } ,
2403+ ) ;
2404+
2405+ it . each ( [
2406+ {
2407+ faultMode : "empty-output-once" ,
2408+ markerPrefix : "QA-COMPACTION-EMPTY-OUTPUT-ONCE" ,
2409+ recoveredMarker : QA_COMPACTION_EMPTY_RECOVERY_SUMMARY_MARKER ,
2410+ reasoningOnly : false ,
2411+ } ,
2412+ {
2413+ faultMode : "reasoning-only-output-once" ,
2414+ markerPrefix : "QA-COMPACTION-REASONING-ONLY-OUTPUT-ONCE" ,
2415+ recoveredMarker : QA_COMPACTION_REASONING_RECOVERY_SUMMARY_MARKER ,
2416+ reasoningOnly : true ,
2417+ } ,
2418+ ] as const ) (
2419+ "scopes $faultMode compaction faults to one scenario session" ,
2420+ async ( { faultMode, markerPrefix, recoveredMarker, reasoningOnly } ) => {
2421+ const server = await startMockServer ( ) ;
2422+ const requestFor = ( session : string ) => ( {
2423+ model : "gpt-5.6-luna" ,
2424+ instructions : QA_COMPACTION_SUMMARY_INSTRUCTIONS ,
2425+ input : `<conversation>\n${ markerPrefix } -${ session } \nretain current work\n</conversation>\n\nCreate a structured summary.` ,
2426+ } ) ;
2427+
2428+ const first = await expectOpenAiNonStreamingResponsesJson ( server , requestFor ( "session-a" ) ) ;
2429+ if ( reasoningOnly ) {
2430+ expect ( JSON . stringify ( first ) ) . toContain ( "reasoning_compaction_summary_fault" ) ;
2431+ expect ( JSON . stringify ( first ) ) . not . toContain ( recoveredMarker ) ;
2432+ } else {
2433+ expect ( outputText ( first ) ) . toBe ( "" ) ;
2434+ }
2435+ const recovered = await expectOpenAiNonStreamingResponsesJson (
2436+ server ,
2437+ requestFor ( "session-a" ) ,
2438+ ) ;
2439+ const recoveredText = outputText ( recovered ) ;
2440+ expect ( recoveredText ) . toContain ( recoveredMarker ) ;
2441+ expect ( recoveredText ) . toContain ( `${ markerPrefix } -session-a` ) ;
2442+ expect ( recoveredText ) . toContain ( "## Decisions" ) ;
2443+ expect ( recoveredText ) . toContain ( "## Open TODOs" ) ;
2444+ expect ( recoveredText ) . toContain ( "## Constraints/Rules" ) ;
2445+ expect ( recoveredText ) . toContain ( "## Pending user asks" ) ;
2446+ expect ( recoveredText ) . toContain ( "## Exact identifiers" ) ;
2447+ const independent = await expectOpenAiNonStreamingResponsesJson (
2448+ server ,
2449+ requestFor ( "session-b" ) ,
2450+ ) ;
2451+ if ( reasoningOnly ) {
2452+ expect ( JSON . stringify ( independent ) ) . toContain ( "reasoning_compaction_summary_fault" ) ;
2453+ expect ( JSON . stringify ( independent ) ) . not . toContain ( recoveredMarker ) ;
2454+ } else {
2455+ expect ( outputText ( independent ) ) . toBe ( "" ) ;
2456+ }
2457+
2458+ const requests = requireArray (
2459+ await getJson ( server , "/debug/requests" ) ,
2460+ "compaction output fault requests" ,
2461+ ) . map ( ( request ) => requireRecord ( request , "compaction output fault request" ) ) ;
2462+ expect ( requests . map ( ( request ) => request . compactionSummaryFaultMode ) ) . toEqual ( [
2463+ faultMode ,
2464+ "none" ,
2465+ faultMode ,
2466+ ] ) ;
2467+ expect ( requests . every ( ( request ) => request . requestKind === "compaction-summary" ) ) . toBe ( true ) ;
2468+ } ,
2469+ ) ;
2470+
2471+ it ( "classifies developer-role compaction instructions before applying a typed fault" , async ( ) => {
2472+ const server = await startMockServer ( ) ;
2473+ const response = await expectOpenAiNonStreamingResponsesJson ( server , {
2474+ model : "gpt-5.6-luna" ,
2475+ input : [
2476+ {
2477+ role : "developer" ,
2478+ content : [ { type : "input_text" , text : QA_COMPACTION_SUMMARY_INSTRUCTIONS } ] ,
2479+ } ,
2480+ makeUserInput (
2481+ "<conversation>QA-COMPACTION-EMPTY-OUTPUT-ONCE-developer-wire</conversation>" ,
2482+ ) ,
2483+ ] ,
2484+ } ) ;
2485+
2486+ expect ( outputText ( response ) ) . toBe ( "" ) ;
2487+ const requests = requireArray (
2488+ await getJson ( server , "/debug/requests" ) ,
2489+ "developer-role compaction requests" ,
2490+ ) . map ( ( request ) => requireRecord ( request , "developer-role compaction request" ) ) ;
2491+ expect ( requests ) . toEqual ( [
2492+ expect . objectContaining ( {
2493+ requestKind : "compaction-summary" ,
2494+ compactionSummaryFaultMode : "empty-output-once" ,
2495+ } ) ,
2496+ ] ) ;
2497+ } ) ;
2498+
23532499 it ( "handles staged scalar compaction summaries and promotes the durable merge without state leakage" , async ( ) => {
23542500 const server = await startMockServer ( ) ;
23552501 const genericChunkPayload = await expectOpenAiNonStreamingResponsesJson ( server , {
0 commit comments