@@ -173,6 +173,35 @@ def inject_external_before_second_precondition(file_descriptor: int):
173173 assert paths ["user" ].read_bytes () == external_content
174174
175175
176+ def test_temporary_verifies_complete_candidate_bundle_before_entering_body (tmp_path : Path , monkeypatch ):
177+ paths , baseline = _prompt_files (tmp_path )
178+ snapshot = snapshot_prompt_files (paths )
179+ external_content = b"external after second candidate replace"
180+ original_replace = os .replace
181+ replace_calls = 0
182+ body_entered = False
183+
184+ def update_first_file_after_second_replace (source : str | bytes | Path , destination : str | bytes | Path ):
185+ nonlocal replace_calls
186+ original_replace (source , destination )
187+ replace_calls += 1
188+ if replace_calls == 2 :
189+ paths ["system" ].write_bytes (external_content )
190+
191+ monkeypatch .setattr (writeback .os , "replace" , update_first_file_after_second_replace )
192+
193+ with pytest .raises ((ConcurrentPromptUpdateError , RuntimeError )):
194+ with temporary_prompt_bundle (
195+ snapshot ,
196+ {"system" : "candidate system" , "user" : "candidate user" },
197+ ):
198+ body_entered = True
199+
200+ assert not body_entered
201+ assert paths ["system" ].read_bytes () == external_content
202+ assert paths ["user" ].read_bytes () == baseline ["user" ]
203+
204+
176205def test_commit_rejects_concurrent_update_before_any_write (tmp_path : Path , monkeypatch ):
177206 paths , baseline = _prompt_files (tmp_path )
178207 snapshot = snapshot_prompt_files (paths )
0 commit comments