@@ -64,12 +64,25 @@ const QA_COMPACTION_RETRY_CODE_MODE_WRITE_RESULT = {
6464const QA_COMPACTION_RETRY_PROMPT =
6565 "Compaction retry mutating tool check. Current durable context marker: QA-COMPACTION-DURABLE-MARKER. Create compaction-retry-summary.txt." ;
6666const QA_COMPACTION_RETRY_OVERFLOW_PADDING = "x" . repeat ( 300_000 ) ;
67+ const QA_COMPACTION_RETRY_HISTORICAL_PHRASE = "post-marker historical user block" ;
6768const QA_COMPACTION_EMPTY_RECOVERY_SUMMARY_MARKER = "QA-COMPACTION-EMPTY-RECOVERED-SUMMARY" ;
6869const QA_COMPACTION_REASONING_RECOVERY_SUMMARY_MARKER = "QA-COMPACTION-REASONING-RECOVERED-SUMMARY" ;
70+ const QA_COMPACTION_SUMMARY_HEADINGS = [
71+ "## Decisions" ,
72+ "## Open TODOs" ,
73+ "## Constraints/Rules" ,
74+ "## Pending user asks" ,
75+ "## Exact identifiers" ,
76+ ] as const ;
6977const 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.
7078
7179Do NOT continue the conversation. Do NOT respond to any questions in the conversation. ONLY output the structured summary.` ;
7280
81+ function expectCurrentCompactionSummaryHeadings ( summary : string ) {
82+ expect ( summary . match ( / ^ # # .+ $ / gmu) ) . toEqual ( QA_COMPACTION_SUMMARY_HEADINGS ) ;
83+ expect ( summary ) . not . toContain ( "## Goal" ) ;
84+ }
85+
7386afterEach ( async ( ) => {
7487 while ( cleanups . length > 0 ) {
7588 await cleanups . pop ( ) ?.( ) ;
@@ -2395,7 +2408,7 @@ describe("qa mock openai server", () => {
23952408
23962409 expect ( response . status ) . toBe ( 200 ) ;
23972410 const summary = outputText ( await response . json ( ) ) ;
2398- expect ( summary ) . toContain ( "## Goal" ) ;
2411+ expectCurrentCompactionSummaryHeadings ( summary ) ;
23992412 expect ( summary ) . not . toContain ( "QA-COMPACTION-DURABLE-MARKER" ) ;
24002413 expect ( summary ) . not . toContain ( "QA-COMPACTION-BULKY-HISTORICAL-MARKER" ) ;
24012414 const requests = requireArray (
@@ -2570,9 +2583,16 @@ describe("qa mock openai server", () => {
25702583
25712584 expect ( response . status ) . toBe ( 200 ) ;
25722585 const body = requireRecord ( await response . json ( ) , "Anthropic summary response" ) ;
2573- expect ( requireArray ( body . content , "content" ) ) . toContainEqual (
2574- expect . objectContaining ( { type : "text" , text : expect . stringContaining ( "## Goal" ) } ) ,
2586+ const content = requireArray ( body . content , "content" ) ;
2587+ expect ( content ) . toContainEqual (
2588+ expect . objectContaining ( { type : "text" , text : expect . stringContaining ( "## Decisions" ) } ) ,
25752589 ) ;
2590+ const summaryText = requireRecord ( content [ 0 ] , "summary content" ) . text ;
2591+ expect ( typeof summaryText ) . toBe ( "string" ) ;
2592+ if ( typeof summaryText !== "string" ) {
2593+ throw new TypeError ( "Anthropic summary content text must be a string" ) ;
2594+ }
2595+ expectCurrentCompactionSummaryHeadings ( summaryText ) ;
25762596 expect ( await getJson ( server , "/debug/last-request" ) ) . toMatchObject ( {
25772597 requestKind : "compaction-summary" ,
25782598 outcome : "success" ,
@@ -2588,20 +2608,43 @@ describe("qa mock openai server", () => {
25882608 "<conversation>\n[Chunk 1 - oldest messages]\nunrelated historical context\n</conversation>\n\nAdditional focus: preserve exact identifiers." ,
25892609 } ) ;
25902610 const genericChunkSummary = outputText ( genericChunkPayload ) ;
2591- expect ( genericChunkSummary ) . toContain ( "## Goal" ) ;
2611+ expectCurrentCompactionSummaryHeadings ( genericChunkSummary ) ;
25922612 expect ( genericChunkSummary ) . not . toContain ( "QA-COMPACTION-DURABLE-MARKER" ) ;
25932613
25942614 const scenarioChunkPayload = await expectOpenAiNonStreamingResponsesJson ( server , {
25952615 model : "gpt-5.6-luna" ,
25962616 instructions : QA_COMPACTION_SUMMARY_INSTRUCTIONS ,
2597- input :
2598- "<conversation>\n[Chunk 2 - most recent messages]\nQA-COMPACTION-BULKY-HISTORICAL-MARKER\n</conversation>\n\nAdditional focus: preserve exact identifiers." ,
2617+ input : `<conversation>
2618+ [Chunk 2 - most recent messages]
2619+ QA-COMPACTION-BULKY-HISTORICAL-MARKER ${ QA_COMPACTION_RETRY_HISTORICAL_PHRASE } 10
2620+ </conversation>
2621+
2622+ Additional focus: preserve exact identifiers.` ,
25992623 } ) ;
26002624 const scenarioChunkSummary = outputText ( scenarioChunkPayload ) ;
2601- expect ( scenarioChunkSummary ) . toContain ( "## Goal" ) ;
2625+ expectCurrentCompactionSummaryHeadings ( scenarioChunkSummary ) ;
2626+ expect ( scenarioChunkSummary ) . toContain ( QA_COMPACTION_RETRY_HISTORICAL_PHRASE ) ;
26022627 expect ( scenarioChunkSummary ) . not . toContain ( "QA-COMPACTION-DURABLE-MARKER" ) ;
26032628 expect ( scenarioChunkSummary ) . not . toContain ( "QA-COMPACTION-BULKY-HISTORICAL-MARKER" ) ;
26042629
2630+ const historyMergePayload = await expectOpenAiNonStreamingResponsesJson ( server , {
2631+ model : "gpt-5.6-luna" ,
2632+ instructions : QA_COMPACTION_SUMMARY_INSTRUCTIONS ,
2633+ input : `<conversation>
2634+ [Chunk 1 - oldest messages]
2635+ ${ genericChunkSummary }
2636+ [Chunk 2 - most recent messages]
2637+ ${ scenarioChunkSummary }
2638+ </conversation>
2639+
2640+ Update and merge these partial structured summaries.` ,
2641+ } ) ;
2642+ const historyMergedSummary = outputText ( historyMergePayload ) ;
2643+ expectCurrentCompactionSummaryHeadings ( historyMergedSummary ) ;
2644+ expect ( historyMergedSummary ) . toContain ( QA_COMPACTION_RETRY_HISTORICAL_PHRASE ) ;
2645+ expect ( historyMergedSummary ) . not . toContain ( "QA-COMPACTION-DURABLE-MARKER" ) ;
2646+ expect ( historyMergedSummary ) . not . toContain ( "QA-COMPACTION-BULKY-HISTORICAL-MARKER" ) ;
2647+
26052648 const durableChunkPayload = await expectOpenAiNonStreamingResponsesJson ( server , {
26062649 model : "gpt-5.6-luna" ,
26072650 instructions : QA_COMPACTION_SUMMARY_INSTRUCTIONS ,
@@ -2612,6 +2655,7 @@ Retain QA-COMPACTION-DURABLE-MARKER for the active task.
26122655Additional focus: preserve QA-COMPACTION-DURABLE-MARKER.` ,
26132656 } ) ;
26142657 const durableChunkSummary = outputText ( durableChunkPayload ) ;
2658+ expectCurrentCompactionSummaryHeadings ( durableChunkSummary ) ;
26152659 expect ( durableChunkSummary ) . toContain ( "QA-COMPACTION-DURABLE-MARKER" ) ;
26162660
26172661 const promptOnlyChunkPayload = await expectOpenAiNonStreamingResponsesJson ( server , {
@@ -2624,6 +2668,7 @@ Compaction retry mutating tool check. Create compaction-retry-summary.txt.
26242668Additional focus: preserve current work.` ,
26252669 } ) ;
26262670 const promptOnlyChunkSummary = outputText ( promptOnlyChunkPayload ) ;
2671+ expectCurrentCompactionSummaryHeadings ( promptOnlyChunkSummary ) ;
26272672 expect ( promptOnlyChunkSummary ) . not . toContain ( "QA-COMPACTION-DURABLE-MARKER" ) ;
26282673
26292674 const currentChunkPayload = await expectOpenAiNonStreamingResponsesJson ( server , {
@@ -2637,6 +2682,7 @@ Create compaction-retry-summary.txt.
26372682Additional focus: preserve current work.` ,
26382683 } ) ;
26392684 const currentChunkSummary = outputText ( currentChunkPayload ) ;
2685+ expectCurrentCompactionSummaryHeadings ( currentChunkSummary ) ;
26402686 expect ( currentChunkSummary ) . toContain ( "QA-COMPACTION-DURABLE-MARKER" ) ;
26412687
26422688 const mergePayload = await expectOpenAiNonStreamingResponsesJson ( server , {
@@ -2657,22 +2703,25 @@ ${durableChunkSummary}
26572703
26582704Update and merge these partial structured summaries.` ,
26592705 } ) ;
2660- expect ( outputText ( mergePayload ) ) . toContain ( "QA-COMPACTION-DURABLE-MARKER" ) ;
2706+ const mergedSummary = outputText ( mergePayload ) ;
2707+ expectCurrentCompactionSummaryHeadings ( mergedSummary ) ;
2708+ expect ( mergedSummary ) . toContain ( "QA-COMPACTION-DURABLE-MARKER" ) ;
26612709
26622710 const unrelatedPayload = await expectOpenAiNonStreamingResponsesJson ( server , {
26632711 model : "gpt-5.6-luna" ,
26642712 instructions : QA_COMPACTION_SUMMARY_INSTRUCTIONS ,
26652713 input :
26662714 "<conversation>\na later unrelated scenario\n</conversation>\n\nCreate a structured summary." ,
26672715 } ) ;
2668- expect ( outputText ( unrelatedPayload ) ) . toContain ( "## Goal" ) ;
2669- expect ( outputText ( unrelatedPayload ) ) . not . toContain ( "QA-COMPACTION-DURABLE-MARKER" ) ;
2716+ const unrelatedSummary = outputText ( unrelatedPayload ) ;
2717+ expectCurrentCompactionSummaryHeadings ( unrelatedSummary ) ;
2718+ expect ( unrelatedSummary ) . not . toContain ( "QA-COMPACTION-DURABLE-MARKER" ) ;
26702719
26712720 const requests = requireArray (
26722721 await getJson ( server , "/debug/requests" ) ,
26732722 "compaction requests" ,
26742723 ) . map ( ( request ) => requireRecord ( request , "compaction request" ) ) ;
2675- expect ( requests ) . toHaveLength ( 7 ) ;
2724+ expect ( requests ) . toHaveLength ( 8 ) ;
26762725 expect (
26772726 requests . every (
26782727 ( request ) =>
@@ -2705,6 +2754,7 @@ Update and merge these partial structured summaries.`,
27052754 ] ,
27062755 } ) ;
27072756 const compactedSummary = outputText ( summaryPayload ) ;
2757+ expectCurrentCompactionSummaryHeadings ( compactedSummary ) ;
27082758 expect ( compactedSummary ) . toContain ( "QA-COMPACTION-DURABLE-MARKER" ) ;
27092759
27102760 const writePlan = await expectOpenAiStreamingResponsesText ( server , {
0 commit comments