@@ -854,6 +854,87 @@ def test_refused_selection_handoff_is_typed_and_keeps_the_committed_capture(tmp_
854854 assert _heartbeat_receipt_count (runtime , TURN_ID ) >= 1
855855
856856
857+ @pytest .mark .parametrize ('provider' , ['file' , 'sqlite' ])
858+ @pytest .mark .parametrize ('delivery' , ['off' , 'on' ])
859+ def test_failed_complete_composition_captures_only_recovery_evidence (
860+ tmp_path , monkeypatch , capsys , provider , delivery ,
861+ ):
862+ from loopx .cli import main
863+ from loopx .control_plane .effect_runtime import EffectRuntimeStartupError
864+ from loopx .control_plane .quota import turn_envelope
865+ from loopx .control_plane .turn_driver import codex_context
866+ from tests .control_plane .canonical_authority_fixture import isolate_sqlite_runtime
867+ from tests .control_plane .test_quota_settlement_cli import _spend_run_count
868+
869+ isolate_sqlite_runtime (tmp_path , monkeypatch )
870+ project , runtime , registry = _write_fixture (tmp_path )
871+ project = project .resolve ()
872+ monkeypatch .chdir (project )
873+ state = _configure_read_only_todo (project )
874+ mode = 'hard_lease' if delivery == 'on' else 'soft_claim'
875+ initialize_canonical_source_fixture (runtime , GOAL_ID , state_path = state , provider = provider , handoff_mode = mode )
876+ original_state = state .read_bytes ()
877+ capture , native = tmp_path / 'failure' , tmp_path / 'native'
878+ delivery_args = ['--codex-app' , '--context-delivery' , delivery ]
879+ if delivery == 'on' :
880+ native .mkdir (mode = 0o700 )
881+ session = 'composition-failure-session'
882+ monkeypatch .setenv ('CODEX_THREAD_ID' , session )
883+ assert codex_context .codex_context_hook (str (native ), {
884+ 'session_id' : session , 'cwd' : str (project ), 'hook_event_name' : 'SessionStart' , 'source' : 'startup' ,
885+ }) == {}
886+ delivery_args += ['--codex-context-dir' , str (native )]
887+ native_before = {path .name : path .read_bytes () for path in native .glob ('*' ) if path .is_file ()}
888+ binding = ['--goal-id' , GOAL_ID , '--agent-id' , AGENT_ID , '--turn-instance-id' , TURN_ID ]
889+ captured = []
890+ run_typed = turn_envelope ._turn_envelope_result
891+
892+ def interrupt_compose (action , ** kwargs ):
893+ if action == 'compose_complete' :
894+ captured .append (kwargs ['payload' ])
895+ raise EffectRuntimeStartupError ('fixture composition unavailable' , diagnostic_code = 'fixture_unavailable' )
896+ return run_typed (action , ** kwargs )
897+
898+ with monkeypatch .context () as fault :
899+ fault .setattr (turn_envelope , '_turn_envelope_result' , interrupt_compose )
900+ code = main (['--registry' , str (registry ), '--runtime-root' , str (runtime ), '--format' , 'json' ,
901+ 'quota' , 'should-run' , * binding , '--todo-id' , TODO_ID , * delivery_args , '--decision-output-dir' , str (capture )])
902+ failure = json .loads (capsys .readouterr ().out )
903+ assert code == 1 and failure ['ok' ] is False
904+ assert failure ['error_code' ] == 'quota_decision_transport_failed'
905+ assert len (captured ) == 1 and captured [0 ]['ok' ] is True
906+ original_receipt = captured [0 ]['heartbeat_receipt' ]
907+ assert failure ['heartbeat_receipt' ] == original_receipt
908+ assert original_receipt ['settlement_identity' ]['todo_id' ] == TODO_ID
909+ if delivery == 'on' :
910+ assert captured [0 ]['execution_preparation' ]['state' ] == 'claim_required'
911+ before_readback = _heartbeat_receipt_count (runtime , TURN_ID )
912+ assert before_readback == 1
913+
914+ saved = capture / 'decision.json'
915+ reference = failure ['captured_decision' ]
916+ assert saved .read_bytes () and reference ['sha256' ] == sha256 (saved .read_bytes ()).hexdigest ()
917+ code , projection = _run_cli (registry , runtime , 'quota' , 'context-project' , * binding , * delivery_args ,
918+ '--capture-file' , reference ['path' ], '--capture-sha256' , reference ['sha256' ], cwd = project )
919+ assert code == 1 and projection ['ok' ] is False
920+ assert 'diagnostic' in projection ['reason' ]
921+ diagnostic = json .loads (saved .read_bytes ())
922+ assert diagnostic ['ok' ] is False and diagnostic ['should_run' ] is False
923+ assert 'interaction_contract' not in diagnostic and 'execution_preparation' not in diagnostic
924+ assert diagnostic ['heartbeat_receipt' ] == original_receipt
925+ reader = shlex .split (reference ['command' ])
926+ code , page = _run_cli (registry , runtime , * reader [reader .index ('quota' ):],
927+ '--select' , '/heartbeat_receipt' , cwd = project )
928+ assert code == 0 and page ['ok' ] and page ['eof' ]
929+ assert json .loads (page ['content' ])['selections' ] == [
930+ {'pointer' : '/heartbeat_receipt' , 'value' : original_receipt },
931+ ]
932+ assert {path .name : path .read_bytes () for path in native .glob ('*' ) if path .is_file ()} == native_before
933+ assert _heartbeat_receipt_count (runtime , TURN_ID ) == before_readback
934+ assert _spend_run_count (runtime ) == 0
935+ assert state .read_bytes () == original_state
936+
937+
857938@pytest .mark .parametrize ('provider' , ['file' , 'sqlite' ])
858939@pytest .mark .parametrize ('turn_envelope' , [False , True ])
859940def test_native_recovery_failure_keeps_original_receipt_and_capture (
@@ -877,10 +958,7 @@ def test_native_recovery_failure_keeps_original_receipt_and_capture(
877958
878959 def refuse_recovery (payload , ** kwargs ):
879960 saved = kwargs ['capture_directory' ] / 'decision.json'
880- attempts .append (saved .read_bytes ())
881- if turn_envelope :
882- assert payload ['schema_version' ] == 'loopx_turn_envelope_v0'
883- assert 'heartbeat_receipt' not in payload
961+ attempts .append ((payload , saved .read_bytes ()))
884962 raise QuotaDecisionTransportError (
885963 'Current context recovery could not be published or safely invalidated. '
886964 'Read the saved original decision/receipt; do not rerun admission.'
@@ -900,15 +978,19 @@ def refuse_recovery(payload, **kwargs):
900978 assert code == 1 and failure ['ok' ] is False
901979 assert failure ['error_code' ] == 'quota_decision_transport_failed'
902980 assert failure ['status' ] == 'decision_transport_failed'
981+ assert failure ['reason' ].startswith ('Current context recovery could not be published' )
903982 saved = list (captures .glob ('*/decision.json' ))
904983 assert len (saved ) == len (attempts ) == 1
905- assert saved [0 ].read_bytes () == attempts [0 ]
906- original = json .loads (attempts [0 ])
984+ projected , raw = attempts [0 ]
985+ assert saved [0 ].read_bytes () == raw
986+ original = json .loads (raw )
907987 assert original ['ok' ] is True
988+ assert projected ['schema_version' ] == original ['schema_version' ] == 'loopx_turn_envelope_v1'
989+ assert projected ['heartbeat_receipt' ] == original ['heartbeat_receipt' ]
908990 assert failure ['heartbeat_receipt' ] == original ['heartbeat_receipt' ]
909991 assert failure ['rollout_event' ] == original ['rollout_event' ]
910992 assert failure ['captured_decision' ]['path' ] == str (saved [0 ])
911- assert failure ['captured_decision' ]['sha256' ] == sha256 (attempts [ 0 ] ).hexdigest ()
993+ assert failure ['captured_decision' ]['sha256' ] == sha256 (raw ).hexdigest ()
912994 assert _heartbeat_receipt_count (runtime , TURN_ID ) == 1
913995 assert _spend_run_count (runtime ) == 0
914996 assert state .read_bytes () == original_state
0 commit comments