@@ -28,6 +28,7 @@ export type ReusableExactBody = {path: Path; sourceIdentity: string};
2828const MIN_REUSABLE_BODY_BYTES = 1024 ;
2929const SETTLEMENT_PLAN_PATH = [ "interaction_contract" , "cli_channel" , "settlement_plan" ] ;
3030const REPLAN_CONTEXT_PATH = [ "autonomous_replan_obligation" , "replan_context" ] ;
31+ const COMPLETE_REQUIREMENTS_CLAUSE = "This view retains complete requirements and current review/check states. " ;
3132const pointer = ( path : Path ) : string => path . length ? "/" + path . map ( key =>
3233 String ( key ) . replaceAll ( "~" , "~0" ) . replaceAll ( "/" , "~1" ) ) . join ( "/" ) : "" ;
3334const at = ( root : unknown , path : Path ) : unknown => path . reduce < unknown > ( ( row , key ) =>
@@ -647,7 +648,7 @@ function requiredNativeContext(request: JsonObject, sourceValues: ProjectionSour
647648 } ) : [ ] ,
648649 } ,
649650 instruction : deferred . map ( edit => typeof edit . read_instruction === "string" ? edit . read_instruction : "" ) . join ( "" )
650- + "This view retains complete requirements and current review/check states. "
651+ + COMPLETE_REQUIREMENTS_CLAUSE
651652 + "Source history, review reasoning and evidence references are supporting material. "
652653 + "Read them from this same immutable capture when a decision, verification or review depends on them. "
653654 + "Use the outcome's supporting_material_pointer when shown, or remove /id from its selected JSON pointer. "
@@ -742,8 +743,11 @@ export function prepareProgressiveContextDelivery(request: JsonObject, legacyFie
742743 // Full native delivery can use that copy; delta still carries its own edits.
743744 if ( request . source_edits === true && delivery . mode === "full" ) delete delivery . context ;
744745 const observe = request . observe_first_read === true && ! ! jsonObject ( required . context . selected_todo ) ?. todo_id ;
746+ const firstReadBytes = jsonObject ( request . native_projection ) ?. initial_read_output_bytes ;
747+ const openingBudget = typeof firstReadBytes === "number" && Number . isSafeInteger ( firstReadBytes ) && firstReadBytes > 0
748+ ? firstReadBytes : undefined ;
745749 const groups = observe || request . include_initial_read_plan === true
746- ? initialContextReadGroups ( required . context , decisionFields ) : undefined ;
750+ ? initialContextReadGroups ( required . context , decisionFields , openingBudget ) : undefined ;
747751 const openingSplit = request . include_initial_read_plan === true && groups
748752 ? prepareInitialOpening ( required . context , groups , exactBody ?. path ) : undefined ;
749753 const pointers = groups ? [ ...groups . opening , ...groups . remaining ] : [ ] ;
@@ -830,12 +834,27 @@ function prepareInitialOpening(context: JsonObject, groups: ReturnType<typeof in
830834 return { controls, details, linkedDetail} ;
831835}
832836
837+ function deferredOutcomesInstruction ( context : JsonObject , deferredOutcomes : readonly string [ ] ) : string | undefined {
838+ if ( ! deferredOutcomes . length ) return undefined ;
839+ const details = jsonObject ( context . context_details ) ;
840+ const instruction = typeof details ?. instruction === "string" ? details . instruction : undefined ;
841+ if ( ! instruction ) return undefined ;
842+ const notice =
843+ "This opening omits full requirement bodies for large suggested outcomes, delivering only their id, objective and revision. "
844+ + "Before dependent work, read each applicable complete outcome from recovery.command "
845+ + `(--select <pointer>): ${ deferredOutcomes . join ( ", " ) } . `
846+ + "Supporting material below still comes from the original capture in context_details. " ;
847+ return instruction . includes ( COMPLETE_REQUIREMENTS_CLAUSE )
848+ ? instruction . replace ( COMPLETE_REQUIREMENTS_CLAUSE , notice )
849+ : notice + instruction ;
850+ }
851+
833852/** Invocation-local navigation, not captured input or a read/retention receipt.
834853 * Decision vocabulary comes from the same owner as standalone initial reads. */
835854function nativeContextReadPlan ( context : JsonObject , pointers : string [ ] ,
836855 groups : ReturnType < typeof initialContextReadGroups > , openingSplit ?: ReturnType < typeof prepareInitialOpening > ) : {
837856 initial : boolean ; plan : JsonObject ; opening ?: JsonObject ; remaining ?: JsonObject ; linked_opening_detail ?: string ;
838- ordinary_opening_rows ?: number ;
857+ ordinary_opening_rows ?: number ; deferred_suggested_outcomes ?: string [ ] ; opening_context_instruction ?: string ;
839858 } {
840859 if ( ! pointers . length ) return { initial : false ,
841860 plan : capturedContextSelection ( { context, select : [ "" ] , list_fields : true } ) } ;
@@ -850,10 +869,15 @@ function nativeContextReadPlan(context: JsonObject, pointers: string[],
850869 const ordinaryRows = openingSplit ? ( plan . selections as ReadSelection [ ] ) . reduce ( ( end , row , index ) =>
851870 groups . opening . some ( field => field === row . pointer || field . startsWith ( row . pointer + "/" ) )
852871 ? index + 1 : end , 0 ) : undefined ;
872+ const deferredOutcomes = groups . deferredSuggestedOutcomes ?? [ ] ;
873+ const openingInstruction = deferredOutcomes . length && pointers . includes ( "/context_details/instruction" )
874+ ? deferredOutcomesInstruction ( context , deferredOutcomes ) : undefined ;
853875 // Keep the ordinary fallback in the same order as the observed selection.
854876 return { initial : true , plan, ...( opening . length && remaining . length
855877 ? { opening : select ( opening ) , remaining : select ( remaining ) } : { } ) ,
856- ...( openingSplit ? { linked_opening_detail : openingSplit . linkedDetail , ordinary_opening_rows : ordinaryRows } : { } ) } ;
878+ ...( openingSplit ? { linked_opening_detail : openingSplit . linkedDetail , ordinary_opening_rows : ordinaryRows } : { } ) ,
879+ ...( deferredOutcomes . length ? { deferred_suggested_outcomes : deferredOutcomes } : { } ) ,
880+ ...( openingInstruction ? { opening_context_instruction : openingInstruction } : { } ) } ;
857881}
858882
859883export function initialContextReadPointers ( context : JsonObject , decisionFields : readonly string [ ] ) : string [ ] {
@@ -951,8 +975,8 @@ function managerReturnAttention(value: unknown, requiredReads: unknown[]): JsonO
951975 return attention ;
952976}
953977
954- function initialContextReadGroups ( context : JsonObject , decisionFields : readonly string [ ] ) : {
955- opening : string [ ] ; remaining : string [ ] ;
978+ function initialContextReadGroups ( context : JsonObject , decisionFields : readonly string [ ] , openingBudget ?: number ) : {
979+ opening : string [ ] ; remaining : string [ ] ; deferredSuggestedOutcomes ?: string [ ] ;
956980} {
957981 const object = ( value : unknown ) : JsonObject | undefined =>
958982 value !== null && typeof value === "object" && ! Array . isArray ( value ) ? value as JsonObject : undefined ;
@@ -1015,6 +1039,7 @@ function initialContextReadGroups(context: JsonObject, decisionFields: readonly
10151039 include ( "interaction_contract" , "agent_channel" , field ) ;
10161040 const cli = object ( interaction . cli_channel ) , planPath = SETTLEMENT_PLAN_PATH ;
10171041 const plan = object ( cli ?. settlement_plan ) ;
1042+ const readyIdentity = readySettlementIdentity ( context , interaction , plan ) ;
10181043 const deferredReentry = hasBoundDatedTodoReentry ( context , interaction , plan ) ;
10191044 const turnBindingRequired = cli ?. turn_binding_required === true ;
10201045 const admissionFields = [ "next_cli_actions" , "spend_allowed_now" , "spend_after_validation" , "spend_policy" ] ;
@@ -1109,6 +1134,7 @@ function initialContextReadGroups(context: JsonObject, decisionFields: readonly
11091134 // before selecting other sources' metadata: complete-parent coalescing keeps
11101135 // the first selected child's position. Preserve stable order within each group.
11111136 const hasText = ( source : unknown ) : boolean => typeof object ( object ( source ) ?. content ) ?. text === "string" ;
1137+ const deferredSuggestedOutcomes : string [ ] = [ ] ;
11121138 sourceRows . sort ( ( left , right ) => Number ( hasText ( right . source ) ) - Number ( hasText ( left . source ) ) ) ;
11131139 sourceRows . forEach ( ( { source : _source , index} ) => {
11141140 const source = [ ...work , "sources" , index ] ;
@@ -1157,11 +1183,31 @@ function initialContextReadGroups(context: JsonObject, decisionFields: readonly
11571183 && suggested . every ( id => typeof id === "string"
11581184 && outcomes . filter ( value => object ( value ) ?. id === id ) . length === 1 ) ;
11591185 const expanded = new Set ( expand ? suggested as string [ ] : [ ] ) ;
1186+ const outcomesPath = [ ...source , "content" , "goal_acceptance_contract" , "outcome_requirements" , "outcomes" ] ;
1187+ const canDeferSuggested = openingBudget !== undefined && readyIdentity !== undefined && expanded . size > 0
1188+ && inlineSupportingDetails && details ?. outcome_support_from_id === true
1189+ && details . path === supportingSource ?. path && details . sha256 === supportingSource ?. sha256
1190+ && typeof details . command_template === "string" && details . command_template . length > 0 ;
1191+ const referenced = canDeferSuggested && Array . isArray ( outcomes ) ? outcomes . filter ( ( value , index ) => {
1192+ const row = object ( value ) ;
1193+ return typeof row ?. id === "string" && expanded . has ( row . id )
1194+ && row . supporting_material_pointer === pointer ( [ ...outcomesPath , index ] ) ;
1195+ } ) : [ ] ;
1196+ // The whole suggested set must have exact supporting addresses before
1197+ // exceeding the native opening window moves its bodies to selected reads.
1198+ const deferSuggested = canDeferSuggested && referenced . length === expanded . size
1199+ && Buffer . byteLength ( JSON . stringify ( referenced ) , "utf8" ) > openingBudget ;
11601200 if ( Array . isArray ( outcomes ) ) outcomes . forEach ( ( _outcome , outcomeIndex ) => {
1161- const outcome = [ ...source , "content" , "goal_acceptance_contract" , "outcome_requirements" , "outcomes" , outcomeIndex ] ;
1201+ const outcome = [ ...outcomesPath , outcomeIndex ] ;
11621202 const row = object ( _outcome ) , outcomeId = row ?. id ;
1163- if ( typeof outcomeId === "string" && expanded . has ( outcomeId ) ) { include ( ...outcome ) ; return ; }
1203+ const suggested = typeof outcomeId === "string" && expanded . has ( outcomeId ) ;
1204+ if ( suggested && ! deferSuggested ) { include ( ...outcome ) ; return ; }
11641205 include ( ...outcome , "id" ) ;
1206+ if ( suggested ) {
1207+ include ( ...outcome , "objective" ) ;
1208+ include ( ...outcome , "revision" ) ;
1209+ deferredSuggestedOutcomes . push ( pointer ( outcome ) ) ;
1210+ }
11651211 // The id selection already carries the same absolute outcome address.
11661212 // Keep aliases whose source, value or read guidance cannot be verified.
11671213 if ( ! ( inlineSupportingDetails && details ?. outcome_support_from_id === true
@@ -1188,7 +1234,7 @@ function initialContextReadGroups(context: JsonObject, decisionFields: readonly
11881234 // A ready Turn already shows its ordered writeback and spend with their
11891235 // conditions. Hide only the exact duplicate fallback in this first read;
11901236 // recovery, legacy readers and the original capture keep both forms.
1191- const repeatsSettlement = readySettlementIdentity ( context , interaction , plan ) !== undefined
1237+ const repeatsSettlement = readyIdentity !== undefined
11921238 && settlementSteps ?. length === 2 && writeback ?. kind === "durable_writeback" && spend ?. kind === "quota_spend"
11931239 && templates ?. every ( command => typeof command === "string" && command . length > 0 )
11941240 && isDeepStrictEqual ( cli ?. next_cli_actions , templates ) ;
@@ -1251,5 +1297,6 @@ function initialContextReadGroups(context: JsonObject, decisionFields: readonly
12511297 } else if ( details && known ( details , [ "command_template" , "path" , "sha256" ] ) ) {
12521298 include ( "context_details" ) ;
12531299 } else include ( "context_details" , "unread_context_index" ) ;
1254- return { opening, remaining : pointers } ;
1300+ return { opening, remaining : pointers ,
1301+ ...( deferredSuggestedOutcomes . length ? { deferredSuggestedOutcomes} : { } ) } ;
12551302}
0 commit comments