@@ -91,6 +91,8 @@ const DEFAULT_QUALITY_GUARD_MAX_RETRIES = 1;
9191const MAX_RECENT_TURNS_PRESERVE = 12 ;
9292const MAX_QUALITY_GUARD_MAX_RETRIES = 3 ;
9393const MAX_RECENT_TURN_TEXT_CHARS = 600 ;
94+ const MAX_REQUIRED_ASK_CONTEXT_CHARS = 2_000 ;
95+ const REQUIRED_ASK_CONTEXT_TRUNCATED_MARKER = "\n[... split-turn ask context truncated ...]\n" ;
9496const TOOL_CALL_BLOCK_TYPES = new Set ( [ "toolCall" , "toolUse" , "functionCall" ] ) ;
9597const PREVIOUS_SUMMARY_REDISTILL_PREFIX =
9698 "Previous compaction summary to re-distill with the current conversation. " +
@@ -339,6 +341,7 @@ type SummaryQualityRetention = {
339341 auditSummary : string ;
340342 identifiers : string [ ] ;
341343 latestAsk : string | null ;
344+ requiredAskContext : string ;
342345 identifierPolicy : "strict" | "off" | "custom" ;
343346} ;
344347
@@ -625,19 +628,21 @@ function budgetCompactionSummary(
625628 bodyBudget : maxChars ,
626629 bodyTrimmed : false ,
627630 suffixTrimmed : false ,
631+ qualityRetentionInfeasible : false ,
628632 } ;
629633 }
630634
631635 const retentionPlan = qualityRetention
632636 ? createSummaryQualityRetentionPlan ( summaryBody , SUMMARY_TRUNCATED_MARKER , qualityRetention )
633637 : null ;
638+ const bodyCapacity = retentionPlan ? maxChars : summaryBody . length ;
634639 const bodyFloor = Math . min (
635- summaryBody . length ,
640+ bodyCapacity ,
636641 maxChars ,
637642 Math . max ( 1 , Math . ceil ( maxChars / 2 ) , retentionPlan ?. minimumChars ?? 0 ) ,
638643 ) ;
639644 const suffixReservation = Math . min ( suffix . text . length , maxChars ) ;
640- const bodySlot = Math . min ( summaryBody . length , Math . max ( bodyFloor , maxChars - suffixReservation ) ) ;
645+ const bodySlot = Math . min ( bodyCapacity , Math . max ( bodyFloor , maxChars - suffixReservation ) ) ;
641646 const cappedBody = retentionPlan ?. render ( bodySlot ) ?? capCompactionSummary ( summaryBody , bodySlot ) ;
642647 const suffixBudget = Math . max ( 0 , maxChars - cappedBody . length ) ;
643648 const cappedSuffix = capCompactionSuffix ( suffix , suffixBudget ) ;
@@ -647,6 +652,7 @@ function budgetCompactionSummary(
647652 bodyBudget : bodySlot ,
648653 bodyTrimmed : cappedBody . length < summaryBody . length ,
649654 suffixTrimmed : cappedSuffix . length < suffix . text . length ,
655+ qualityRetentionInfeasible : retentionPlan !== null && retentionPlan . minimumChars > maxChars ,
650656 } ;
651657}
652658
@@ -921,6 +927,33 @@ function formatGeneratedSplitTurnSection(summary: string, onTruncated?: () => vo
921927 return `${ heading } ${ cappedSummary } ` ;
922928}
923929
930+ function formatRequiredAskContext ( summary : string ) : string {
931+ const originalRequestHeading = "## Original Request" ;
932+ const earlyProgressHeading = "## Early Progress" ;
933+ const originalRequestStart = summary . indexOf ( originalRequestHeading ) ;
934+ const originalRequestEnd =
935+ originalRequestStart >= 0
936+ ? summary . indexOf ( earlyProgressHeading , originalRequestStart + originalRequestHeading . length )
937+ : - 1 ;
938+ const source =
939+ originalRequestStart >= 0
940+ ? summary
941+ . slice (
942+ originalRequestStart + originalRequestHeading . length ,
943+ originalRequestEnd >= 0 ? originalRequestEnd : undefined ,
944+ )
945+ . trim ( )
946+ : summary . trim ( ) ;
947+ if ( source . length <= MAX_REQUIRED_ASK_CONTEXT_CHARS ) {
948+ return source ;
949+ }
950+ const contentBudget =
951+ MAX_REQUIRED_ASK_CONTEXT_CHARS - REQUIRED_ASK_CONTEXT_TRUNCATED_MARKER . length ;
952+ const headBudget = Math . floor ( contentBudget / 2 ) ;
953+ const tailBudget = contentBudget - headBudget ;
954+ return `${ truncateUtf16Safe ( source , headBudget ) } ${ REQUIRED_ASK_CONTEXT_TRUNCATED_MARKER } ${ sliceUtf16Safe ( source , - tailBudget ) } ` ;
955+ }
956+
924957function extractLatestUserAsk ( messages : AgentMessage [ ] ) : string | null {
925958 for ( const message of messages . toReversed ( ) ) {
926959 if ( message . role !== "user" ) {
@@ -1338,6 +1371,7 @@ export default function compactionSafeguardExtension(api: ExtensionAPI): void {
13381371
13391372 for ( let attempt = 0 ; attempt < totalAttempts ; attempt += 1 ) {
13401373 let splitTurnSectionLocal = "" ;
1374+ let splitTurnAskContextLocal = "" ;
13411375 let historySummary = "" ;
13421376 const producerLosses = new Set < CompactionLoss > ( ) ;
13431377 try {
@@ -1363,6 +1397,7 @@ export default function compactionSafeguardExtension(api: ExtensionAPI): void {
13631397 splitTurnSectionLocal = formatGeneratedSplitTurnSection ( prefixSummary , ( ) => {
13641398 producerLosses . add ( "split-turn-tail" ) ;
13651399 } ) ;
1400+ splitTurnAskContextLocal = formatRequiredAskContext ( prefixSummary ) ;
13661401 }
13671402 } catch ( attemptError ) {
13681403 if ( signal ?. aborted ) {
@@ -1401,6 +1436,8 @@ export default function compactionSafeguardExtension(api: ExtensionAPI): void {
14011436 auditSummary : unbudgetedSummary ,
14021437 identifiers,
14031438 latestAsk : latestUserAsk ,
1439+ requiredAskContext :
1440+ splitTurnAskContextLocal || formatRequiredAskContext ( latestUserAsk ?? "" ) ,
14041441 identifierPolicy,
14051442 }
14061443 : undefined ,
@@ -1412,6 +1449,17 @@ export default function compactionSafeguardExtension(api: ExtensionAPI): void {
14121449 if ( ! qualityGuardEnabled ) {
14131450 return compactionResult ( finalized . summary ) ;
14141451 }
1452+ if ( finalized . qualityRetentionInfeasible ) {
1453+ log . warn (
1454+ "Compaction safeguard: required quality facts exceed finalized artifact budget; " +
1455+ `requiredChars>${ MAX_COMPACTION_SUMMARY_CHARS } identifierCount=${ identifiers . length } ` ,
1456+ ) ;
1457+ setCompactionSafeguardCancelReason (
1458+ ctx . sessionManager ,
1459+ "Compaction safeguard required facts exceed the finalized summary budget." ,
1460+ ) ;
1461+ return { cancel : true } ;
1462+ }
14151463 const quality = auditSummaryQuality ( {
14161464 summary : finalized . summary ,
14171465 structuralSummary : finalized . structuralSummary ,
0 commit comments