@@ -108,6 +108,7 @@ interface ReadbackRequest {
108108 infer_turn_instance_id : boolean ;
109109 allow_unbound_binding : boolean ;
110110 resolve_original_binding : boolean ;
111+ pending_closeout : boolean ;
111112 borrow_source_admission : boolean ;
112113 include_record_bodies : boolean ;
113114 tolerate_malformed_history : boolean ;
@@ -236,6 +237,13 @@ function decodeRequest(
236237 if ( request . resolve_original_binding === true && request . infer_turn_instance_id ) {
237238 throw new EffectRuntimeRequestError ( "original binding requires an explicit Turn identity" ) ;
238239 }
240+ if ( request . pending_closeout !== undefined && typeof request . pending_closeout !== "boolean" ) {
241+ throw new EffectRuntimeRequestError ( "pending_closeout must be a boolean" ) ;
242+ }
243+ if ( request . pending_closeout === true && ( request . infer_turn_instance_id || request . allow_unbound_binding
244+ || request . refresh_retry != null || request . heartbeat_reentry_guard != null || request . spend_precheck === true ) ) {
245+ throw new EffectRuntimeRequestError ( "pending closeout is a read-only receipt recovery, not inference or settlement admission" ) ;
246+ }
239247 if (
240248 request . borrow_source_admission !== undefined
241249 && typeof request . borrow_source_admission !== "boolean"
@@ -290,6 +298,7 @@ function decodeRequest(
290298 infer_turn_instance_id : request . infer_turn_instance_id ,
291299 allow_unbound_binding : request . allow_unbound_binding ,
292300 resolve_original_binding : request . resolve_original_binding === true ,
301+ pending_closeout : request . pending_closeout === true ,
293302 borrow_source_admission : request . borrow_source_admission === true ,
294303 include_record_bodies : request . include_record_bodies !== false ,
295304 tolerate_malformed_history : request . tolerate_malformed_history === true ,
@@ -1119,7 +1128,8 @@ function owedPriorTurnFromSnapshot(
11191128 request : ReadbackRequest ,
11201129 snapshot : QuotaSettlementReadbackSnapshot ,
11211130 agentId : string ,
1122- turnInstanceId : string ,
1131+ turnInstanceId : string | null ,
1132+ options ?: { includeAdmittedReplans : boolean } ,
11231133) : OwedPriorTurn | null {
11241134 const receipts = heartbeatReceiptsFromEvents ( snapshot . events , request . goal_id , agentId )
11251135 . filter ( ( event ) => quotaOwnerOwnsProjection ( request . owner , event . goal_ref ) ) ;
@@ -1128,6 +1138,7 @@ function owedPriorTurnFromSnapshot(
11281138 request . goal_id ,
11291139 agentId ,
11301140 turnInstanceId ,
1141+ options ,
11311142 ) ;
11321143 for ( const selected of candidates ) {
11331144 // A committed monitor poll is its own exact no-spend closeout. This is the
@@ -1151,6 +1162,7 @@ function owedPriorTurnFromSnapshot(
11511162 infer_turn_instance_id : false ,
11521163 allow_unbound_binding : false ,
11531164 resolve_original_binding : false ,
1165+ pending_closeout : false ,
11541166 refresh_retry : null ,
11551167 heartbeat_reentry_guard : null ,
11561168 } ,
@@ -1165,6 +1177,48 @@ function owedPriorTurnFromSnapshot(
11651177 return null ;
11661178}
11671179
1180+ /** Resolve pending recovery without another provider read or a latest-Run heuristic. */
1181+ function readPendingCloseoutFromRequest (
1182+ request : ReadbackRequest ,
1183+ snapshot : QuotaSettlementReadbackSnapshot ,
1184+ ) : JsonObject {
1185+ const explicitAgentId = normalizeAgentId ( request . agent_id ) ;
1186+ if ( explicitAgentId === null ) {
1187+ throw new EffectRuntimeRequestError ( "pending closeout requires an explicit Agent" ) ;
1188+ }
1189+ const notFound = { schema_version : QUOTA_SETTLEMENT_READBACK_RESULT_SCHEMA , found : false } ;
1190+ const turn = request . turn_instance_id ?? owedPriorTurnFromSnapshot (
1191+ request , snapshot , explicitAgentId , null , { includeAdmittedReplans : true } ,
1192+ ) ?. prior_turn_instance_id ;
1193+ if ( ! turn ) return notFound ;
1194+ // Selection, original binding validation and receipt progress share this
1195+ // immutable snapshot and admitted owner. Maintenance Run classifications
1196+ // cannot conceal the guard's debt or select another Agent's work.
1197+ const result = readQuotaSettlementFromRequest ( { ...request , turn_instance_id : turn ,
1198+ pending_closeout : false , resolve_original_binding : true } , snapshot , false ) ;
1199+ const identity = jsonObject ( jsonObject ( jsonObject ( result . identity ) ?. result ) ?. value ) ;
1200+ const admission = jsonObject ( result . native_child_admission ) ;
1201+ const originalReceipt = jsonObject ( result . heartbeat_receipt ) ;
1202+ const workAdmitted = originalReceipt !== null &&
1203+ heartbeatReceiptFactFromEvent ( originalReceipt ) . settlement_effect_id !== null &&
1204+ ( admission ?. report_permission === "new_operation" ||
1205+ admission ?. report_permission === "existing_operation_only" ) &&
1206+ ( ! identity ?. replan_obligation_id ||
1207+ jsonObject ( result . semantic_replan_guard ) ?. selected_obligation_id === identity . replan_obligation_id ) ;
1208+ const partialSettlement = [ result . writeback , result . spend ] . some ( step =>
1209+ jsonObject ( jsonObject ( step ) ?. payload ) ?. ok === true ) ;
1210+ // Binding a candidate is not work admission. Reuse the receipt owner's
1211+ // qualified work facts, while retaining verified partial commits even if
1212+ // the original guard did not admit new work. Identity failures stay visible.
1213+ if ( identity !== null && ! workAdmitted && ! partialSettlement ) return notFound ;
1214+ if ( jsonObject ( jsonObject ( result . settlement ) ?. payload ) ?. ok === true ||
1215+ ( identity ?. todo_id && committedMonitorPollFromSnapshot ( snapshot , {
1216+ goal_id : request . goal_id , agent_id : explicitAgentId , turn_instance_id : turn ,
1217+ todo_id : String ( identity . todo_id ) ,
1218+ } , request . owner ) !== null ) ) return notFound ;
1219+ return result ;
1220+ }
1221+
11681222function readQuotaSettlementFromRequest (
11691223 request : ReadbackRequest ,
11701224 snapshot : QuotaSettlementReadbackSnapshot ,
@@ -1180,6 +1234,7 @@ function readQuotaSettlementFromRequest(
11801234 ) ;
11811235 }
11821236 const explicitAgentId = normalizeAgentId ( request . agent_id ) ;
1237+ if ( request . pending_closeout ) return readPendingCloseoutFromRequest ( request , snapshot ) ;
11831238 // Only latest-Turn inference needs the full run history. Explicit readback
11841239 // must use the existing Turn indexes before applying the Goal owner fence;
11851240 // otherwise a batch over N Turns repeatedly scans all N receipts.
0 commit comments