@@ -470,8 +470,9 @@ def register_native_context_view(directory: str | None, view: dict[str, Any]) ->
470470 _save (root , state )
471471
472472
473- def _capture_required_context (payload : dict [str , Any ], * , directory : Path ,
474- surface : str ) -> tuple [dict [str , Any ], dict [str , Any ]]:
473+ def _capture_required_context (
474+ payload : dict [str , Any ], * , directory : Path , surface : str , archive : dict [str , Any ] | None = None ,
475+ ) -> tuple [dict [str , Any ], dict [str , Any ]]:
475476 """Publish the archive first, then the model view selected by the typed owner.
476477
477478 Only the required view may become a full/delta baseline. Detail pointers
@@ -480,8 +481,9 @@ def _capture_required_context(payload: dict[str, Any], *, directory: Path,
480481 from ..quota .turn_envelope import plan_progressive_context
481482 from ..effect_runtime import EffectRuntimeRemoteError , EffectRuntimeStartupError
482483
483- archive = capture_context_projection (payload , directory = directory ,
484- read_guidance = CONTEXT_CAPTURE_PAGE_GUIDANCE )
484+ if archive is None :
485+ archive = capture_context_projection (payload , directory = directory ,
486+ read_guidance = CONTEXT_CAPTURE_PAGE_GUIDANCE )
485487 try :
486488 plan = plan_progressive_context (payload , surface = surface )
487489 except (EffectRuntimeRemoteError , EffectRuntimeStartupError ):
@@ -569,19 +571,29 @@ def project_codex_context(payload: dict[str, Any], *, directory: str | None, cap
569571 if continuity_generation is not None :
570572 scope ["continuity_generation" ] = continuity_generation
571573 request = _native_context_request (state , scope )
572- required , recovery = _capture_required_context (payload , directory = capture_directory , surface = surface )
574+ recovery = capture_context_projection (payload , directory = capture_directory ,
575+ read_guidance = CONTEXT_CAPTURE_PAGE_GUIDANCE )
573576 page_bytes , output_bytes = native_context_page_budget ()
574- recovery ["command" ] = context_capture_read_command (Path (recovery ["path" ]), recovery ["sha256" ],
575- page_bytes = page_bytes , max_page_output_bytes = output_bytes )
576- state ["recovery" ] = {** {key : recovery [key ] for key in ("path" , "sha256" , "command" )},
577- "goal_id" : goal_id , "agent_id" : agent_id ,
578- "total_bytes" : Path (recovery ["path" ]).stat ().st_size }
579- # Block reuse before optional wire IO, not after a first page
580- # or EOF. A codec/read interruption keeps this incomplete.
581- state ["pending" ] = {"path" : recovery ["path" ], "sha256" : recovery ["sha256" ],
582- "total_bytes" : state ["recovery" ]["total_bytes" ], "ranges" : {}}
583- _save (root , state )
577+
578+ def publish_recovery (reference : dict [str , Any ]) -> None :
579+ reference ["command" ] = context_capture_read_command (Path (reference ["path" ]), reference ["sha256" ],
580+ page_bytes = page_bytes , max_page_output_bytes = output_bytes )
581+ state ["recovery" ] = {** {key : reference [key ] for key in ("path" , "sha256" , "command" )},
582+ "goal_id" : goal_id , "agent_id" : agent_id ,
583+ "total_bytes" : Path (reference ["path" ]).stat ().st_size }
584+ state ["pending" ] = {"path" : reference ["path" ], "sha256" : reference ["sha256" ],
585+ "total_bytes" : state ["recovery" ]["total_bytes" ], "ranges" : {}}
586+ _save (root , state )
587+
588+ # Publish the current complete source before optional planning
589+ # or encoding can be interrupted. Never recover its predecessor.
590+ publish_recovery (recovery )
584591 recovery_committed = True
592+ required , planned_recovery = _capture_required_context (payload , directory = capture_directory ,
593+ surface = surface , archive = recovery )
594+ if planned_recovery is not recovery :
595+ recovery = planned_recovery
596+ publish_recovery (recovery )
585597
586598 result = project_captured_context (required , directory = capture_directory ,
587599 request = request , captured_recovery = recovery ,
0 commit comments