1- """Settlement remains executable and signed through the opt-in compact view ."""
1+ """Verify historical settlement signatures and the current C1 CLI plan ."""
22from copy import deepcopy
3+ import shlex
34
45import pytest
56
1112from tests .test_turn_envelope import _full_decision
1213from tests .control_plane .test_quota_settlement_cli import (
1314 AGENT_ID , GOAL_ID , TODO_ID , TURN_ID ,
14- _configure_read_only_todo , _run_cli , _run_generated_cli ,
15+ _classification_count , _configure_read_only_todo , _run_cli , _run_generated_cli ,
1516 _spend_run_count , _write_fixture , _initialize_fixture_authority ,
1617)
1718
@@ -91,31 +92,37 @@ def test_absent_plan_is_not_invented_for_historical_or_unbound_packets():
9192 ("--runtime-profile" , "generic_cli" ),
9293 ("--runtime-profile" , "codex_app_ssh_goal" ),
9394])
94- def test_generated_json_commands_settle_original_turn_once (tmp_path , profile_args ):
95+ def test_generated_c1_commands_settle_original_turn_once (tmp_path , profile_args ):
9596 project , runtime , registry = _write_fixture (tmp_path / "quoted inputs" )
9697 _configure_read_only_todo (project )
9798 _initialize_fixture_authority (project , runtime )
98- args = ("quota" , "should-run" , * profile_args , "--goal-id" , GOAL_ID ,
99- "--agent-id" , AGENT_ID , "--todo-id" , TODO_ID ,
100- "--turn-instance-id" , TURN_ID , )
101- rc , full = _run_cli (registry , runtime , * args )
102- assert rc == 0 , full
103- rc , envelope = _run_cli (registry , runtime , * args , "--turn-envelope" )
104- assert rc == 0 , envelope
105- assert envelope ["schema_version" ] == "loopx_turn_envelope_v0"
106- plan = envelope ["writeback" ]["settlement_plan" ]
107- assert plan == full ["interaction_contract" ]["cli_channel" ]["settlement_plan" ]
108- assert plan ["identity" ] == full ["heartbeat_receipt" ]["settlement_identity" ]
99+ binding = (
100+ "--goal-id" , GOAL_ID , "--agent-id" , AGENT_ID ,
101+ "--todo-id" , TODO_ID , "--turn-instance-id" , TURN_ID ,
102+ )
103+ args = ("quota" , "should-run" , * profile_args , * binding )
104+ rc , default = _run_cli (registry , runtime , * args )
105+ assert rc == 0 and default ["schema_version" ] == "loopx_turn_envelope_v1" , default
106+ rc , alias = _run_cli (registry , runtime , * args , "--turn-envelope" )
107+ assert rc == 0 and alias ["schema_version" ] == "loopx_turn_envelope_v1" , alias
108+ plan = alias ["interaction_contract" ]["cli_channel" ]["settlement_plan" ]
109+ assert plan == default ["interaction_contract" ]["cli_channel" ]["settlement_plan" ]
110+ identity = default ["heartbeat_receipt" ]["settlement_identity" ]
111+ assert plan ["identity" ] == alias ["heartbeat_receipt" ]["settlement_identity" ] == identity
112+ assert plan ["schema_version" ] == "quota_settlement_plan_v2"
109113 assert [step ["kind" ] for step in plan ["ordered_steps" ]] == [
110114 "validation" , "durable_writeback" , "quota_spend" , "terminal_closeout" ,
111115 ]
112116 assert plan ["ordered_steps" ][- 1 ]["conditional" ] is True
113117 assert plan ["host_handoff" ]["inside_agent_settlement" ] is False
114- spend_command = plan ["ordered_steps" ][2 ]["command_template" ]
115- rc , premature = _run_generated_cli (spend_command , registry_path = registry )
118+ spend = plan ["ordered_steps" ][2 ]
119+ assert spend ["owner" ] == "runtime" and "command_template" not in spend
120+ rc , premature = _run_cli (registry , runtime , "quota" , "spend-slot" , * binding ,
121+ "--slots" , "1" , "--source" , "heartbeat" , "--execute" )
116122 assert rc != 0 , premature
117123 assert _spend_run_count (runtime ) == 0
118124 writeback = plan ["ordered_steps" ][1 ]["command_template" ]
125+ assert {"--settle-turn" , "--settlement-receipt" } <= set (shlex .split (writeback ))
119126 # A validated exclusion advances research without claiming the objective is met.
120127 # The same negative result is not a blocker merely because a candidate failed.
121128 guidance = plan ["ordered_steps" ][1 ]["precondition" ]
@@ -140,18 +147,16 @@ def test_generated_json_commands_settle_original_turn_once(tmp_path, profile_arg
140147 assert _spend_run_count (runtime ) == 0
141148 writeback = writeback .replace ("<validated_progress>" , "validated_exclusion" ).replace (
142149 "<scale>" , "single_surface" ).replace ("<outcome>" , "outcome_progress" )
143- writeback += " --progress-result-class advanced --progress-evidence-id exclusion-proof"
144- rc , refreshed = _run_generated_cli (
145- writeback + " --delivery-boundary in_flight_continuation --no-global-sync --suppress-external-sinks" ,
146- registry_path = registry ,
147- )
148- assert rc == 0 , refreshed
149- # Recovery instructions must also execute verbatim, with no format repair.
150- owed_command = refreshed ["settlement_owed" ]["command" ]
151- for replay , command in ((False , owed_command ), (True , spend_command )):
152- rc , spent = _run_generated_cli (command , registry_path = registry )
153- assert rc == 0 , spent
154- assert spent ["settlement_result" ]["ok" ] is True
155- if replay :
156- assert spent ["idempotent_replay" ] is True
157- assert _spend_run_count (runtime ) == 1
150+ writeback += (" --progress-result-class advanced --progress-evidence-id exclusion-proof"
151+ " --delivery-boundary in_flight_continuation --no-global-sync --suppress-external-sinks" )
152+ for replay in (False , True ):
153+ rc , settled = _run_generated_cli (writeback , registry_path = registry )
154+ assert rc == 0 and settled ["settlement_progress" ]["state" ] == "settled" , settled
155+ assert settled ["settlement_identity" ] == identity
156+ receipts = settled ["settlement_result" ]["receipts" ]
157+ assert [item ["step_kind" ] for item in receipts ] == ["validation" , "durable_writeback" , "quota_spend" ]
158+ assert all (item ["status" ] == "committed" and item ["effect_id" ] == identity ["effect_id" ]
159+ for item in receipts )
160+ assert settled .get ("idempotent_replay" , False ) is replay
161+ assert _classification_count (runtime , "validated_exclusion" ) == 1
162+ assert _spend_run_count (runtime ) == 1
0 commit comments