@@ -219,6 +219,51 @@ def test_contract4_schema2_final_correction_does_not_confirm(self):
219219 self .assertEqual (receipt ["action" ], "confirmed" )
220220 self .assertEqual (receipt ["stage" ], "final" )
221221
222+ def test_pi_chat_prompt_records_the_final_contract_receipt (self ):
223+ run_rel = ".kimiflow/schema2-pi-chat"
224+ run_dir = os .path .join (self .root , run_rel )
225+ os .makedirs (run_dir )
226+ scope = self .schema2_scope ()
227+ final = self .schema2_final ()
228+ with open (os .path .join (run_dir , "INTAKE.md" ), "w" , encoding = "utf-8" ) as handle :
229+ handle .write (scope )
230+ scope_intake = active_run .parse_intake_document (scope , 4 , 1 , "de" )
231+ with open (os .path .join (run_dir , "INTAKE-RECEIPT-1.json" ), "w" , encoding = "utf-8" ) as handle :
232+ json .dump ({
233+ "schema_version" : 2 ,
234+ "contract" : 4 ,
235+ "round" : 1 ,
236+ "stage" : "scope" ,
237+ "action" : "scope_ready" ,
238+ "request" : "INTAKE.md" ,
239+ "request_digest" : "sha256:" + hashlib .sha256 (scope .encode ("utf-8" )).hexdigest (),
240+ "contract_digest" : active_run .structured_intake_digest (scope_intake ),
241+ "user_language" : "de" ,
242+ "channel" : "chat" ,
243+ "responded_at" : "2026-07-31T12:00:00Z" ,
244+ }, handle )
245+ with open (os .path .join (run_dir , "INTAKE-2.md" ), "w" , encoding = "utf-8" ) as handle :
246+ handle .write (final )
247+ active = self .schema2_active (run_rel , 2 , final )
248+ active ["host" ] = "pi"
249+ active ["owner" ] = {"host" : "pi" , "session_id" : "pi-worker-00000001" }
250+ active_run .write_active (self .root , active )
251+
252+ payload = json .dumps ({
253+ "cwd" : self .root ,
254+ "session_id" : "pi-worker-00000001" ,
255+ "prompt" : "Vertrag bestätigen" ,
256+ })
257+ with mock .patch .dict (os .environ , {"KIMIFLOW_HOST" : "pi" }):
258+ rc , _ = run_main (["prompt-context" ], stdin_text = payload )
259+
260+ self .assertEqual (rc , 0 )
261+ self .assertFalse (active_run .load_active (self .root ).get ("awaiting_user" , False ))
262+ with open (os .path .join (run_dir , "INTAKE-RECEIPT-2.json" ), encoding = "utf-8" ) as handle :
263+ receipt = json .load (handle )
264+ self .assertEqual (receipt ["action" ], "confirmed" )
265+ self .assertEqual (receipt ["channel" ], "chat" )
266+
222267 def test_contract4_schema2_rejects_language_drift (self ):
223268 run_rel = ".kimiflow/schema2-language"
224269 run_dir = os .path .join (self .root , run_rel )
0 commit comments