@@ -17,7 +17,7 @@ import {quotaSummaryReferences, contextSourceIdentity, contextProjectionChanges,
1717import { encodeContextDelivery , fileNavigationPaths , supportingIndexSource ,
1818 SUPPORTING_INDEX_HEADER_FIELDS } from "./context_delivery.ts" ;
1919import { capturedContextSelection } from "./context_selection.ts" ;
20- import { COMPLETE_TURN_DECISION_VERSION } from "./turn_envelope_codec.ts" ;
20+ import { COMPLETE_TURN_DECISION_VERSION , contextWithCliVisionAudit } from "./turn_envelope_codec.ts" ;
2121import { SCOPED_OUTCOME_PROJECTION_MODE , SCOPED_OUTCOME_THRESHOLD } from "./turn_envelope_scoping.ts" ;
2222import { createHash } from "node:crypto" ;
2323
@@ -183,9 +183,9 @@ function hasBoundDatedTodoReentry(context: JsonObject, interaction: JsonObject,
183183 && isDeepStrictEqual ( identity , receipt . settlement_identity ) ;
184184}
185185
186- function reusableWritebackPrecondition ( context : JsonObject ) : ReusableReadField | undefined {
186+ function reusableSettlementReadFields ( context : JsonObject ) : ReusableReadField [ ] {
187187 const interaction = jsonObject ( context . interaction_contract ) ;
188- if ( interaction ?. schema_version !== "loopx_interaction_contract_v0" ) return undefined ;
188+ if ( interaction ?. schema_version !== "loopx_interaction_contract_v0" ) return [ ] ;
189189 const plan = jsonObject ( at ( context , SETTLEMENT_PLAN_PATH ) ) , steps = orderedSettlementSteps ( plan ) ;
190190 const identity = readySettlementIdentity ( context , interaction , plan ) ;
191191 const identityAgent = jsonObject ( context . agent_identity ) ?. agent_id ;
@@ -194,25 +194,35 @@ function reusableWritebackPrecondition(context: JsonObject): ReusableReadField |
194194 || ( identityAgent !== undefined && identityAgent !== agent )
195195 || typeof context . goal_id !== "string" || context . goal_id !== identity . goal_id
196196 || typeof context . registry !== "string" || ! context . registry
197- || typeof context . runtime_root !== "string" || ! context . runtime_root ) return undefined ;
197+ || typeof context . runtime_root !== "string" || ! context . runtime_root ) return [ ] ;
198198 const writebacks = steps . map ( ( value , index ) => ( { step : jsonObject ( value ) , index} ) )
199199 . filter ( ( { step} ) => step ?. kind === "durable_writeback" ) ;
200- if ( writebacks . length !== 1 ) return undefined ;
200+ if ( writebacks . length !== 1 ) return [ ] ;
201201 const { step, index} = writebacks [ 0 ] ! ;
202- if ( step ?. owner !== "agent" || typeof step . precondition !== "string" ) return undefined ;
203- return { path : [ ...SETTLEMENT_PLAN_PATH , "ordered_steps" , index , "precondition" ] ,
204- freshKeys : [ ] , minimumBytes : MIN_REUSABLE_BODY_BYTES ,
205- sourceIdentity : JSON . stringify ( [ context . registry , context . runtime_root , context . goal_ref ?? null ,
206- plan . schema_version , identity . goal_id , agent , identity . todo_id , step . kind , step . owner ] ) } ;
202+ if ( step ?. owner !== "agent" || typeof step . precondition !== "string" ) return [ ] ;
203+ const sourceIdentity = JSON . stringify ( [ context . registry , context . runtime_root , context . goal_ref ?? null ,
204+ plan . schema_version , identity . goal_id , agent , identity . todo_id , step . kind , step . owner ] ) ;
205+ const fields : ReusableReadField [ ] = [ {
206+ path : [ ...SETTLEMENT_PLAN_PATH , "ordered_steps" , index , "precondition" ] ,
207+ freshKeys : [ ] , minimumBytes : MIN_REUSABLE_BODY_BYTES , sourceIdentity,
208+ } ] ;
209+ const path = [ "interaction_contract" , "cli_channel" , "vision_continuation_audit" ] ;
210+ const audit = jsonObject ( at ( context , path ) ) ;
211+ const judge = jsonObject ( audit ?. vision_gap_judge ) ;
212+ if ( audit ?. required === true && judge ?. decision === "continue" && judge . done === false ) {
213+ // Keep the current audit requirement visible. Reuse the guidance only
214+ // when the entire view still matches the observed, scoped source.
215+ fields . push ( { path, freshKeys : [ "required" ] , minimumBytes : MIN_REUSABLE_BODY_BYTES , sourceIdentity} ) ;
216+ }
217+ return fields ;
207218}
208219
209220/** One registry for bodies eligible for observed reuse, regardless of whether
210221 * the reader delivered the body itself or packaged it in a complete parent. */
211222function reusableFirstReadFields ( context : JsonObject , exactBody ?: ReusableExactBody ) : ReusableReadField [ ] {
212223 const exactFields = exactBody && at ( context , exactBody . path ) !== undefined
213224 ? [ { ...exactBody , freshKeys : [ ] , minimumBytes : MIN_REUSABLE_BODY_BYTES } ] : [ ] ;
214- const writeback = reusableWritebackPrecondition ( context ) ;
215- const fields = [ ...exactFields , ...( writeback ? [ writeback ] : [ ] ) ] ;
225+ const fields = [ ...exactFields , ...reusableSettlementReadFields ( context ) ] ;
216226 const workPath : Path = [ "interaction_contract" , "agent_channel" , "work_context" ] ;
217227 const work = jsonObject ( at ( context , workPath ) ) ;
218228 const base = [ ...workPath , "sources" ] ;
@@ -499,7 +509,7 @@ export function contextReadSectionDigests(request: JsonObject, paths: Path[], ex
499509/** Only registered, previously delivered bodies may use a reference. Current
500510 * decisions, stops, selection, settlement and unknown source shapes stay inline. */
501511function observedFirstRead ( request : JsonObject , pointers : string [ ] , exactBody ?: ReusableExactBody ) : JsonObject | undefined {
502- const context = requireJsonObject ( request . context , "current first read" ) ;
512+ const context = contextWithCliVisionAudit ( requireJsonObject ( request . context , "current first read" ) ) ;
503513 const seen = requireJsonObject ( request . consumed_pointers , "observed sections" ) ;
504514 if ( typeof jsonObject ( context . selected_todo ) ?. todo_id !== "string" ) return undefined ;
505515 const fields = new Map ( reusableFirstReadFields ( context , exactBody ) . map ( field => [ pointer ( field . path ) , field ] ) ) ;
@@ -1605,8 +1615,9 @@ function initialContextReadGroups(context: JsonObject, decisionFields: readonly
16051615 sourceRows . sort ( ( left , right ) => Number ( hasText ( right . source ) ) - Number ( hasText ( left . source ) ) ) ;
16061616 sourceRows . forEach ( ( { source : _source , index, agenda} ) => {
16071617 const source = [ ...work , "sources" , index ] ;
1618+ // A verified pending Inbox agenda uses the exact route in required_reads.
16081619 for ( const field of [ "source" , "kind" , "reason" , "ordering" , "command" ] )
1609- include ( ...source , field ) ;
1620+ if ( field !== "command" || ! agenda ) include ( ...source , field ) ;
16101621 // Current Goal/Todo text and source obligations stay whole. Expansive
16111622 // evidence pages, example recipes and historical bodies remain on demand.
16121623 include ( ...source , "content" , "text" ) ;
0 commit comments