"""Assistant response phases across tool use, provider replay, and history.""" import json from harness import Reply, text from integration_support import IntegrationScenario class AssistantPhaseTests(IntegrationScenario): def test_chat_response_text_is_commentary_before_tools_and_final_after(self): def script(request): if any(item.get("role") == "tool" for item in request["messages"]): return text("the answer") return Reply( "raw", events=[ { "id": "fixture", "choices": [ { "index": 0, "delta": { "content": "checking now", "tool_calls": [ { "index": 0, "id": "phase-call", "type": "function", "function": { "name": "python", "arguments": json.dumps( {"code": "1 + 1"} ), }, } ], }, "finish_reason": None, } ], }, { "id": "fixture", "choices": [ {"index": 0, "delta": {}, "finish_reason": "tool_calls"} ], }, ], ) self.provider.script = script app = self.app_for("chat_completions") session = app.session() self.send(app, session, "check and answer") entries = self.history(app, session) self.assertEqual( [ (self.entry_text(row), row["phase"]) for row in entries if row["kind"] == "assistant" ], [("checking now", "commentary"), ("the answer", "final")], ) self.assertTrue( all(row["phase"] is None for row in entries if row["kind"] != "assistant") ) def test_responses_provider_phase_wins_and_replays_unchanged(self): def script(request): done = any( item.get("type") == "function_call_output" for item in request["input"] ) message = { "type": "message", "id": "answer" if done else "progress", "role": "assistant", "phase": "commentary" if done else "final_answer", "content": [ { "type": "output_text", "text": "last note" if done else "answer before tool", } ], } output = [message] if not done: output.append( { "type": "function_call", "id": "call", "call_id": "phase-call", "name": "python", "arguments": json.dumps({"code": "1 + 1"}), "status": "completed", } ) return Reply( "raw", events=[ { "type": "response.completed", "response": { "id": "fixture", "status": "completed", "output": output, "usage": {"input_tokens": 10, "output_tokens": 20}, }, } ], ) self.provider.script = script app = self.app_for("responses") session = app.session() self.send(app, session, "check provider labels") entries = self.history(app, session) self.assertEqual( [ (self.entry_text(row), row["phase"]) for row in entries if row["kind"] == "assistant" ], [("answer before tool", "final"), ("last note", "commentary")], ) replay = self.provider.requests[-1]["request"]["input"] self.assertEqual( next(item for item in replay if item.get("id") == "progress")["phase"], "final_answer", ) self.assertTrue( all(row["phase"] is None for row in entries if row["kind"] != "assistant") )