| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974 |
- import json
- import asyncio
- import logging
- import subprocess
- from collections.abc import AsyncIterator
- from pathlib import Path
- import httpx
- import pytest
- from fastapi.testclient import TestClient
- from pydantic import ValidationError
- from agent_lab.application.contracts import AgentParams, DebugRunRequest, EventAgentParams
- from agent_lab.application.queues import RuntimeQueues
- from agent_lab.application.runtime import DebugRuntime
- from agent_lab.domain.events import ToolCallEvent
- from agent_lab.domain.messages import ChatMessage, StreamItem, TokenUsage
- from agent_lab.infrastructure.chat_client import OpenAICompatibleChatClient
- from agent_lab.infrastructure.sqlite_store import SQLiteSessionStore
- from agent_lab.presentation.web import create_app
- from agent_lab.settings import Settings
- def _css_rule(css: str, selector: str) -> str:
- start = css.index(f"{selector} {{")
- end = css.index("}", start)
- return css[start:end]
- def _run_node_json(source: str) -> object:
- completed = subprocess.run(
- ["node", "--input-type=module", "-e", source],
- capture_output=True,
- text=True,
- check=False,
- )
- assert completed.returncode == 0, completed.stderr
- return json.loads(completed.stdout)
- def _run_load_session_replay_scenario(scenario: str) -> object:
- js = Path("src/agent_lab/presentation/static/app.js").read_text()
- load_replay = js[
- js.index("async function loadSessionReplay(") :
- js.index("function setSessionStatus(")
- ]
- harness = """
- let currentSessionId = null;
- let sessionReplayLoadToken = 0;
- const sessionTitle = { value: "" };
- const sessionList = { value: "" };
- const mutations = {
- workspace: [],
- transcript: [],
- audit: [],
- usage: [],
- status: [],
- };
- const pending = new Map();
- function fetchJson(url) {
- let resolve;
- let reject;
- const promise = new Promise((resolvePromise, rejectPromise) => {
- resolve = resolvePromise;
- reject = rejectPromise;
- });
- pending.set(url, { resolve, reject });
- return promise;
- }
- function resolveReplay(sessionId) {
- pending.get(`/api/sessions/${sessionId}`).resolve({
- title: `${sessionId} title`,
- config: { marker: sessionId },
- });
- pending.get(`/api/sessions/${sessionId}/messages`).resolve([
- { content: sessionId },
- ]);
- pending.get(`/api/sessions/${sessionId}/audit`).resolve([
- { event: sessionId },
- ]);
- pending.get(`/api/sessions/${sessionId}/usage`).resolve({
- session: { marker: sessionId },
- });
- }
- function restoreWorkspaceSnapshot(config) {
- mutations.workspace.push(config.marker);
- }
- function renderReplayMessages(messages) {
- mutations.transcript.push(messages[0].content);
- }
- function renderAuditReplay(audit) {
- mutations.audit.push(audit[0].event);
- }
- function renderSessionUsage(usage) {
- mutations.usage.push(usage.session.marker);
- }
- function setSessionStatus(status) {
- mutations.status.push(status);
- }
- function isSocketOpen() {
- return false;
- }
- """
- return _run_node_json("\n".join([harness, load_replay, scenario]))
- class FakeRuntime:
- def __init__(self) -> None:
- self.requests: list[DebugRunRequest] = []
- async def run(self, request: DebugRunRequest) -> AsyncIterator[dict]:
- self.requests.append(request)
- yield {"type": "session_started"}
- yield {"type": "message_delta", "content": "hello"}
- yield {"type": "done"}
- class QueueAwareRuntime:
- def __init__(self) -> None:
- self.requests: list[DebugRunRequest] = []
- self.queues: RuntimeQueues | None = None
- self.task: asyncio.Task | None = None
- def start(self, request: DebugRunRequest) -> RuntimeQueues:
- self.requests.append(request)
- self.queues = RuntimeQueues()
- self.task = asyncio.create_task(self._run())
- return self.queues
- async def _run(self) -> None:
- assert self.queues is not None
- await self.queues.output.put({"type": "session_started"})
- message = await self.queues.input.get()
- await self.queues.output.put(
- {"type": "message_delta", "content": message.content}
- )
- await self.queues.output.put({"type": "done"})
- async def aclose(self) -> None:
- if self.task is not None and not self.task.done():
- self.task.cancel()
- await asyncio.gather(self.task, return_exceptions=True)
- class PersistentSessionRuntime:
- def __init__(self) -> None:
- self.requests: list[DebugRunRequest] = []
- self.queues: RuntimeQueues | None = None
- self.task: asyncio.Task | None = None
- def start_session(self, request: DebugRunRequest) -> RuntimeQueues:
- self.requests.append(request)
- self.queues = RuntimeQueues()
- self.task = asyncio.create_task(self._run(request))
- return self.queues
- async def _run(self, request: DebugRunRequest) -> None:
- assert self.queues is not None
- await self.queues.output.put({"type": "session_started"})
- await self._emit_turn(1, request.user_message)
- turn_index = 1
- while True:
- message = await self.queues.input.get()
- turn_index += 1
- await self._emit_turn(turn_index, message.content)
- async def _emit_turn(self, turn_index: int, content: str) -> None:
- assert self.queues is not None
- await self.queues.output.put({"type": "turn_started", "turn_index": turn_index})
- await self.queues.output.put({"type": "message_delta", "content": content})
- await self.queues.output.put(
- {"type": "turn_completed", "turn_index": turn_index}
- )
- async def aclose(self) -> None:
- if self.task is not None and not self.task.done():
- self.task.cancel()
- await asyncio.gather(self.task, return_exceptions=True)
- def _request_payload() -> dict:
- return {
- "user_message": "debug this",
- "system_prompts": ["You are a debugger."],
- "pre_messages": [{"role": "user", "content": "previous turn"}],
- "chat_agent": {
- "model": "fake-model",
- "temperature": 0.1,
- "max_tokens": 200,
- },
- "event_agent": {
- "enabled_tools": ["handoff_note"],
- "max_event_loops": 2,
- },
- }
- def _event_tool_call_from_tools(
- tools: list[dict],
- messages: list[ChatMessage],
- ) -> StreamItem:
- tool_name = tools[0]["function"]["name"]
- content = ""
- for role in ("assistant", "user"):
- content = next(
- (
- message.content
- for message in reversed(messages)
- if message.role == role and message.content.strip()
- ),
- "",
- )
- if content:
- break
- arguments = {
- "message": content,
- "query": content,
- "title": content,
- }
- return StreamItem.provider_tool_call(
- ToolCallEvent(
- id="event_agent_call_1",
- name=tool_name,
- arguments=arguments,
- raw_arguments=json.dumps(arguments),
- )
- )
- def test_agent_params_defaults_include_extra_body_and_event_agent_defaults_to_one_round():
- chat_params = AgentParams()
- event_params = EventAgentParams()
- assert chat_params.extra_body == {
- "thinking": {"type": "disabled"},
- "enable_search": False,
- "search_options": {"forced_search": False},
- }
- assert event_params.extra_body == chat_params.extra_body
- assert event_params.max_event_loops == 1
- assert event_params.max_parallel_events == 4
- assert event_params.batch_timeout_seconds == 15.0
- assert event_params.system_prompt == ""
- @pytest.mark.parametrize("value", [0, 17])
- def test_event_agent_params_rejects_out_of_range_parallelism(value: int):
- with pytest.raises(ValidationError, match="max_parallel_events"):
- EventAgentParams(max_parallel_events=value)
- @pytest.mark.parametrize("value", [0, -1, 121])
- def test_event_agent_params_rejects_invalid_batch_timeout(value: float):
- with pytest.raises(ValidationError, match="batch_timeout_seconds"):
- EventAgentParams(batch_timeout_seconds=value)
- def test_debug_run_request_defaults_to_dual_agent_tool_invocation_mode():
- request = DebugRunRequest.model_validate(_request_payload())
- assert request.tool_invocation_mode == "dual_agent"
- @pytest.mark.parametrize("mode", ["dual_agent", "chat_agent_tools"])
- def test_debug_run_request_accepts_supported_tool_invocation_modes(mode: str):
- payload = _request_payload()
- payload["tool_invocation_mode"] = mode
- request = DebugRunRequest.model_validate(payload)
- assert request.tool_invocation_mode == mode
- def test_debug_run_request_rejects_unknown_tool_invocation_mode():
- payload = _request_payload()
- payload["tool_invocation_mode"] = "unknown"
- with pytest.raises(ValidationError, match="tool_invocation_mode"):
- DebugRunRequest.model_validate(payload)
- def test_health_returns_ok():
- app = create_app(runtime_factory=FakeRuntime)
- client = TestClient(app)
- response = client.get("/health")
- assert response.status_code == 200
- assert response.json() == {"status": "ok"}
- def test_api_tools_returns_default_tool_catalog():
- app = create_app(runtime_factory=FakeRuntime)
- client = TestClient(app)
- response = client.get("/api/tools")
- assert response.status_code == 200
- tools = response.json()
- assert [tool["name"] for tool in tools] == [
- "handoff_note",
- "mock_search",
- "mock_ticket",
- "session.terminate",
- "device.volume.adjust",
- "calendar.schedule.create",
- "knowledge.web.search",
- ]
- assert tools[0]["parameters"]["required"] == ["message"]
- def test_session_api_creates_lists_and_returns_empty_replay_data(tmp_path):
- settings = Settings(database_path=str(tmp_path / "agent_lab.sqlite3"))
- app = create_app(settings=settings, runtime_factory=FakeRuntime)
- client = TestClient(app)
- created = client.post(
- "/api/sessions",
- json={
- "title": "Debug session",
- "config": {"chat_agent": {"model": "chat-model"}},
- },
- )
- assert created.status_code == 200
- session_id = created.json()["id"]
- assert created.json()["title"] == "Debug session"
- sessions = client.get("/api/sessions")
- assert sessions.status_code == 200
- assert sessions.json()[0]["id"] == session_id
- assert sessions.json()[0]["turn_count"] == 0
- detail = client.get(f"/api/sessions/{session_id}")
- assert detail.status_code == 200
- assert detail.json()["config"]["chat_agent"]["model"] == "chat-model"
- assert client.get(f"/api/sessions/{session_id}/messages").json() == []
- assert client.get(f"/api/sessions/{session_id}/audit").json() == []
- usage = client.get(f"/api/sessions/{session_id}/usage").json()
- assert usage == {
- "calls": [],
- "turns": [],
- "session": {
- "prompt_tokens": 0,
- "completion_tokens": 0,
- "total_tokens": 0,
- "cached_tokens": 0,
- "elapsed_ms": 0,
- "call_count": 0,
- "fallback_count": 0,
- "tool_count": 0,
- "turn_wall_time_ms": None,
- },
- }
- def test_session_api_returns_persisted_replay_data(tmp_path):
- database_path = tmp_path / "agent_lab.sqlite3"
- settings = Settings(database_path=str(database_path))
- app = create_app(settings=settings, runtime_factory=FakeRuntime)
- client = TestClient(app)
- session_id = client.post("/api/sessions", json={"title": "Replay"}).json()["id"]
- store = SQLiteSessionStore(database_path)
- store.start_turn(session_id, turn_index=1, user_message="debug this")
- store.append_message(
- session_id,
- turn_index=1,
- message=ChatMessage(role="user", content="debug this"),
- )
- store.append_message(
- session_id,
- turn_index=1,
- message=ChatMessage(
- role="assistant",
- content="answer",
- tool_calls=[
- ToolCallEvent(
- id="call-1",
- name="mock_search",
- arguments={"query": "docs"},
- raw_arguments='{"query":"docs"}',
- )
- ],
- ),
- )
- store.append_message(
- session_id,
- turn_index=1,
- message=ChatMessage(
- role="tool",
- content='{"results":[]}',
- name="mock_search",
- tool_call_id="call-1",
- ),
- )
- store.append_audit(
- session_id,
- event="chat_agent_request",
- details={"model": "chat-model"},
- turn_index=1,
- round_index=1,
- )
- store.append_usage(
- session_id,
- turn_index=1,
- round_index=1,
- usage=TokenUsage(prompt_tokens=1, completion_tokens=2, total_tokens=3),
- ttft_ms=10,
- elapsed_ms=20,
- )
- messages = client.get(f"/api/sessions/{session_id}/messages").json()
- assert [message["content"] for message in messages] == [
- "debug this",
- "answer",
- '{"results":[]}',
- ]
- assert messages[0]["tool_calls"] == []
- assert messages[1]["tool_calls"] == [
- {
- "id": "call-1",
- "name": "mock_search",
- "arguments": {"query": "docs"},
- "raw_arguments": '{"query":"docs"}',
- }
- ]
- assert messages[2]["tool_call_id"] == "call-1"
- assert messages[2]["tool_calls"] == []
- assert client.get(f"/api/sessions/{session_id}/audit").json()[0]["details"] == {
- "model": "chat-model"
- }
- usage = client.get(f"/api/sessions/{session_id}/usage").json()
- assert usage["calls"][0]["total_tokens"] == 3
- assert usage["turns"][0]["total_tokens"] == 3
- assert usage["session"]["total_tokens"] == 3
- def test_default_websocket_runtime_factory_accepts_session_store(tmp_path):
- settings = Settings(database_path=str(tmp_path / "agent_lab.sqlite3"))
- app = create_app(settings=settings)
- client = TestClient(app)
- with client.websocket_connect("/ws/debug") as websocket:
- websocket.send_json({"chat_agent": {"model": "fake-model"}})
- message = websocket.receive_json()
- assert message["type"] == "error"
- assert "user_message" in message["message"]
- def test_websocket_debug_streams_runtime_messages():
- runtime = FakeRuntime()
- app = create_app(runtime_factory=lambda: runtime)
- client = TestClient(app)
- with client.websocket_connect("/ws/debug") as websocket:
- websocket.send_json(_request_payload())
- assert websocket.receive_json() == {"type": "session_started"}
- assert websocket.receive_json() == {
- "type": "message_delta",
- "content": "hello",
- }
- assert websocket.receive_json() == {"type": "done"}
- assert runtime.requests[0].user_message == "debug this"
- assert runtime.requests[0].pre_messages[0].content == "previous turn"
- def test_websocket_debug_logs_session_lifecycle(caplog):
- runtime = FakeRuntime()
- app = create_app(runtime_factory=lambda: runtime)
- client = TestClient(app)
- with caplog.at_level(logging.INFO, logger="agent_lab.presentation.web"):
- with client.websocket_connect("/ws/debug") as websocket:
- websocket.send_json(_request_payload())
- assert websocket.receive_json() == {"type": "session_started"}
- assert websocket.receive_json() == {
- "type": "message_delta",
- "content": "hello",
- }
- assert websocket.receive_json() == {"type": "done"}
- assert "websocket session accepted" in caplog.text
- assert "websocket request accepted" in caplog.text
- assert "websocket session closed" in caplog.text
- def test_websocket_debug_enqueues_user_messages_during_running_session():
- runtime = QueueAwareRuntime()
- app = create_app(runtime_factory=lambda: runtime)
- client = TestClient(app)
- with client.websocket_connect("/ws/debug") as websocket:
- websocket.send_json(_request_payload())
- assert websocket.receive_json() == {"type": "session_started"}
- websocket.send_json({"type": "user_message", "content": "follow-up"})
- assert websocket.receive_json() == {
- "type": "message_delta",
- "content": "follow-up",
- }
- assert websocket.receive_json() == {"type": "done"}
- assert runtime.requests[0].user_message == "debug this"
- def test_websocket_debug_keeps_session_open_for_multiple_turns():
- runtime = PersistentSessionRuntime()
- app = create_app(runtime_factory=lambda: runtime)
- client = TestClient(app)
- with client.websocket_connect("/ws/debug") as websocket:
- websocket.send_json(_request_payload())
- assert websocket.receive_json() == {"type": "session_started"}
- assert websocket.receive_json() == {"type": "turn_started", "turn_index": 1}
- assert websocket.receive_json() == {
- "type": "message_delta",
- "content": "debug this",
- }
- assert websocket.receive_json() == {"type": "turn_completed", "turn_index": 1}
- websocket.send_json({"type": "user_message", "content": "follow-up"})
- assert websocket.receive_json() == {"type": "turn_started", "turn_index": 2}
- assert websocket.receive_json() == {
- "type": "message_delta",
- "content": "follow-up",
- }
- assert websocket.receive_json() == {"type": "turn_completed", "turn_index": 2}
- websocket.close()
- assert runtime.requests[0].user_message == "debug this"
- def test_websocket_debug_sends_error_for_invalid_request():
- app = create_app(runtime_factory=FakeRuntime)
- client = TestClient(app)
- with client.websocket_connect("/ws/debug") as websocket:
- websocket.send_json({"chat_agent": {"model": "fake-model"}})
- message = websocket.receive_json()
- assert message["type"] == "error"
- assert "user_message" in message["message"]
- def test_debug_run_request_rejects_invalid_pre_message_role():
- payload = _request_payload()
- payload["pre_messages"] = [{"role": "developer", "content": "invalid"}]
- with pytest.raises(ValidationError) as exc_info:
- DebugRunRequest.model_validate(payload)
- assert "role" in str(exc_info.value)
- def test_debug_run_request_rejects_tool_pre_messages():
- payload = _request_payload()
- payload["pre_messages"] = [
- {
- "role": "tool",
- "content": "orphan result",
- "tool_call_id": "call_1",
- }
- ]
- with pytest.raises(ValidationError, match="pre_messages cannot include tool messages"):
- DebugRunRequest.model_validate(payload)
- def test_debug_run_request_rejects_assistant_tool_calls_in_pre_messages():
- payload = _request_payload()
- payload["pre_messages"] = [
- {
- "role": "assistant",
- "content": "I will inspect this.",
- "tool_calls": [
- {
- "id": "call_1",
- "name": "mock_search",
- "arguments": {"query": "latency"},
- "raw_arguments": '{"query":"latency"}',
- }
- ],
- }
- ]
- with pytest.raises(
- ValidationError,
- match="pre_messages cannot include assistant tool calls",
- ):
- DebugRunRequest.model_validate(payload)
- def test_chat_message_allows_internal_tool_replies():
- message = ChatMessage(
- role="tool",
- content='{"message":"handled"}',
- name="handoff_note",
- tool_call_id="call_1",
- )
- assert message.role == "tool"
- assert message.tool_call_id == "call_1"
- def test_chat_message_preserves_assistant_tool_calls():
- message = ChatMessage(
- role="assistant",
- content="I will inspect both sources.",
- tool_calls=[
- ToolCallEvent(
- id="call_1",
- name="mock_search",
- arguments={"query": "latency docs"},
- raw_arguments='{"query":"latency docs"}',
- ),
- ToolCallEvent(
- id="call_2",
- name="handoff_note",
- arguments={"message": "inspect provider behavior"},
- raw_arguments='{"message":"inspect provider behavior"}',
- ),
- ],
- )
- assert message.model_dump()["tool_calls"] == [
- {
- "id": "call_1",
- "name": "mock_search",
- "arguments": {"query": "latency docs"},
- "raw_arguments": '{"query":"latency docs"}',
- },
- {
- "id": "call_2",
- "name": "handoff_note",
- "arguments": {"message": "inspect provider behavior"},
- "raw_arguments": '{"message":"inspect provider behavior"}',
- },
- ]
- def test_chat_message_rejects_tool_calls_on_non_assistant_role():
- with pytest.raises(ValidationError, match="tool_calls require assistant role"):
- ChatMessage(
- role="user",
- content="invalid",
- tool_calls=[
- ToolCallEvent(
- id="call_1",
- name="mock_search",
- arguments={},
- raw_arguments="{}",
- )
- ],
- )
- def test_chat_message_rejects_tool_role_without_tool_call_id():
- with pytest.raises(ValidationError, match="tool messages require tool_call_id"):
- ChatMessage(role="tool", content="orphan result")
- @pytest.mark.parametrize("role", ["system", "user", "assistant"])
- def test_chat_message_rejects_tool_call_id_on_non_tool_roles(role: str):
- with pytest.raises(ValidationError, match="tool_call_id requires tool role"):
- ChatMessage(role=role, content="invalid", tool_call_id="call_1")
- def test_chat_message_rejects_duplicate_assistant_tool_call_ids():
- with pytest.raises(ValidationError, match="duplicate assistant tool-call ID: call_1"):
- ChatMessage(
- role="assistant",
- content="checking twice",
- tool_calls=[
- ToolCallEvent(
- id="call_1",
- name="mock_search",
- arguments={"query": "first"},
- raw_arguments='{"query":"first"}',
- ),
- ToolCallEvent(
- id="call_1",
- name="handoff_note",
- arguments={"message": "second"},
- raw_arguments='{"message":"second"}',
- ),
- ],
- )
- class HistoryCapturingChatClient:
- def __init__(self) -> None:
- self.calls = 0
- self.second_call_messages: list[ChatMessage] = []
- async def stream_chat(
- self,
- messages: list[ChatMessage],
- tools: list[dict],
- params: AgentParams,
- tool_choice: dict | None = None,
- ) -> AsyncIterator[StreamItem]:
- if tools:
- yield _event_tool_call_from_tools(tools, messages)
- return
- self.calls += 1
- if self.calls == 1:
- yield StreamItem.message_delta("Need event help.")
- yield StreamItem.text_event(
- ToolCallEvent(
- id="call_1",
- name="handoff_note",
- arguments={},
- raw_arguments="{}",
- )
- )
- return
- self.second_call_messages = list(messages)
- yield StreamItem.message_delta("Final answer.")
- @pytest.mark.asyncio
- async def test_runtime_appends_event_summary_without_tool_reply_history():
- request = DebugRunRequest(
- user_message="debug this",
- system_prompts=["You are a debugger."],
- pre_messages=[],
- chat_agent=AgentParams(model="fake-model", temperature=0.1, max_tokens=200),
- event_agent=EventAgentParams(enabled_tools=["handoff_note"], max_event_loops=2),
- )
- client = HistoryCapturingChatClient()
- runtime = DebugRuntime(client)
- outputs = [message async for message in runtime.run(request)]
- assert client.calls == 2
- assert [message.role for message in client.second_call_messages] == [
- "system",
- "system",
- "user",
- "assistant",
- "user",
- ]
- assert "Available events:" in client.second_call_messages[1].content
- assert client.second_call_messages[3].content == "Need event help."
- assert not any(message.role == "tool" for message in client.second_call_messages)
- assert client.second_call_messages[4].content.startswith("EventAgent results:\n")
- assert client.second_call_messages[4].name == "event_agent"
- assert outputs[-1] == {"type": "done"}
- @pytest.mark.asyncio
- async def test_openai_chat_client_streams_sse_chunks_through_parser():
- requests: list[httpx.Request] = []
- def handler(request: httpx.Request) -> httpx.Response:
- requests.append(request)
- payload = json.loads(request.content)
- assert payload["stream"] is True
- assert payload["model"] == "model-x"
- assert payload["messages"] == [{"role": "user", "content": "hi"}]
- assert payload["tools"][0]["function"]["name"] == "handoff_note"
- assert "tool_choice" not in payload
- assert payload["temperature"] == 0.3
- assert payload["max_tokens"] == 50
- assert request.headers["authorization"] == "Bearer test-key"
- return httpx.Response(
- 200,
- content=(
- b'data: {"choices":[{"delta":{"content":"hi"},"finish_reason":null}]}\n\n'
- b"data: [DONE]\n\n"
- ),
- )
- async with httpx.AsyncClient(
- transport=httpx.MockTransport(handler),
- base_url="https://llm.test/v1",
- ) as http_client:
- client = OpenAICompatibleChatClient(
- api_key="test-key",
- base_url="https://llm.test/v1",
- default_model="default-model",
- request_timeout_seconds=5,
- http_client=http_client,
- )
- items = [
- item
- async for item in client.stream_chat(
- messages=[ChatMessage(role="user", content="hi")],
- tools=[
- {
- "type": "function",
- "function": {"name": "handoff_note", "parameters": {}},
- }
- ],
- params=AgentParams(model="model-x", temperature=0.3, max_tokens=50),
- )
- ]
- assert requests[0].url.path == "/v1/chat/completions"
- assert [item.content for item in items if item.kind == "message_delta"] == ["hi"]
- assert [item.raw_chunk for item in items if item.kind == "raw_chunk"] == [
- {"choices": [{"delta": {"content": "hi"}, "finish_reason": None}]}
- ]
- @pytest.mark.asyncio
- async def test_openai_chat_client_serializes_explicit_tool_choice():
- captured_payloads: list[dict] = []
- forced_choice = {
- "type": "function",
- "function": {"name": "handoff_note"},
- }
- def handler(request: httpx.Request) -> httpx.Response:
- captured_payloads.append(json.loads(request.content))
- return httpx.Response(200, content=b"data: [DONE]\n\n")
- async with httpx.AsyncClient(
- transport=httpx.MockTransport(handler),
- base_url="https://llm.test/v1",
- ) as http_client:
- client = OpenAICompatibleChatClient(
- api_key="",
- base_url="https://llm.test/v1",
- default_model="provider-default",
- request_timeout_seconds=5,
- http_client=http_client,
- )
- [
- item
- async for item in client.stream_chat(
- messages=[ChatMessage(role="user", content="prepare handoff")],
- tools=[
- {
- "type": "function",
- "function": {"name": "handoff_note", "parameters": {}},
- }
- ],
- params=AgentParams(model="provider-default"),
- tool_choice=forced_choice,
- )
- ]
- assert captured_payloads[0]["tool_choice"] == forced_choice
- @pytest.mark.asyncio
- async def test_openai_chat_client_flushes_one_provider_tool_call_at_done():
- def handler(request: httpx.Request) -> httpx.Response:
- return httpx.Response(
- 200,
- content=(
- b'data: {"choices":[{"delta":{"tool_calls":[{"index":0,'
- b'"id":"call_1","function":{"name":"mock_search",'
- b'"arguments":"{\\"query\\":\\""}}]},"finish_reason":null}]}\n\n'
- b'data: {"choices":[{"delta":{"tool_calls":[{"index":0,'
- b'"function":{"arguments":"latency docs\\"}"}}]},'
- b'"finish_reason":null}]}\n\n'
- b"data: [DONE]\n\n"
- ),
- )
- async with httpx.AsyncClient(
- transport=httpx.MockTransport(handler),
- base_url="https://llm.test/v1",
- ) as http_client:
- client = OpenAICompatibleChatClient(
- api_key="",
- base_url="https://llm.test/v1",
- default_model="provider-default",
- request_timeout_seconds=5,
- http_client=http_client,
- )
- items = [
- item
- async for item in client.stream_chat(
- messages=[ChatMessage(role="user", content="find docs")],
- tools=[
- {
- "type": "function",
- "function": {"name": "mock_search", "parameters": {}},
- }
- ],
- params=AgentParams(model="provider-default"),
- )
- ]
- provider_calls = [
- item.event for item in items if item.kind == "provider_tool_call"
- ]
- assert provider_calls == [
- ToolCallEvent(
- id="call_1",
- name="mock_search",
- arguments={"query": "latency docs"},
- raw_arguments='{"query":"latency docs"}',
- )
- ]
- @pytest.mark.asyncio
- async def test_openai_chat_client_uses_default_model_when_request_model_is_blank():
- captured_payloads: list[dict] = []
- def handler(request: httpx.Request) -> httpx.Response:
- captured_payloads.append(json.loads(request.content))
- return httpx.Response(
- 200,
- content=b'data: {"choices":[{"delta":{"content":"hi"},"finish_reason":null}]}\n\n',
- )
- async with httpx.AsyncClient(
- transport=httpx.MockTransport(handler),
- base_url="https://llm.test/v1",
- ) as http_client:
- client = OpenAICompatibleChatClient(
- api_key="",
- base_url="https://llm.test/v1",
- default_model="provider-default",
- request_timeout_seconds=5,
- http_client=http_client,
- )
- [
- item
- async for item in client.stream_chat(
- messages=[ChatMessage(role="user", content="hi")],
- tools=[],
- params=AgentParams(model=" ", temperature=0.3, max_tokens=50),
- )
- ]
- assert captured_payloads[0]["model"] == "provider-default"
- @pytest.mark.asyncio
- async def test_openai_chat_client_serializes_provider_tool_transcript():
- captured_payloads: list[dict] = []
- def handler(request: httpx.Request) -> httpx.Response:
- captured_payloads.append(json.loads(request.content))
- return httpx.Response(
- 200,
- content=b'data: {"choices":[{"delta":{"content":"done"},"finish_reason":null}]}\n\n',
- )
- async with httpx.AsyncClient(
- transport=httpx.MockTransport(handler),
- base_url="https://llm.test/v1",
- ) as http_client:
- client = OpenAICompatibleChatClient(
- api_key="",
- base_url="https://llm.test/v1",
- default_model="provider-default",
- request_timeout_seconds=5,
- http_client=http_client,
- )
- [
- item
- async for item in client.stream_chat(
- messages=[
- ChatMessage(role="user", content="inspect both"),
- ChatMessage(
- role="assistant",
- content="I will inspect both sources.",
- tool_calls=[
- ToolCallEvent(
- id="call_1",
- name="mock_search",
- arguments={"query": "latency docs"},
- raw_arguments='{"query":"latency docs"}',
- ),
- ToolCallEvent(
- id="call_2",
- name="handoff_note",
- arguments={"message": "inspect provider behavior"},
- raw_arguments='{"message":"inspect provider behavior"}',
- ),
- ],
- ),
- ChatMessage(
- role="tool",
- content='{"results":[]}',
- name="mock_search",
- tool_call_id="call_1",
- ),
- ChatMessage(
- role="tool",
- content='{"message":"handled"}',
- name="handoff_note",
- tool_call_id="call_2",
- ),
- ChatMessage(role="user", content="continue"),
- ],
- tools=[],
- params=AgentParams(
- model="provider-default",
- temperature=0.3,
- max_tokens=50,
- ),
- )
- ]
- assert captured_payloads[0]["messages"] == [
- {"role": "user", "content": "inspect both"},
- {
- "role": "assistant",
- "content": "I will inspect both sources.",
- "tool_calls": [
- {
- "id": "call_1",
- "type": "function",
- "function": {
- "name": "mock_search",
- "arguments": '{"query":"latency docs"}',
- },
- },
- {
- "id": "call_2",
- "type": "function",
- "function": {
- "name": "handoff_note",
- "arguments": '{"message":"inspect provider behavior"}',
- },
- },
- ],
- },
- {
- "role": "tool",
- "content": '{"results":[]}',
- "tool_call_id": "call_1",
- },
- {
- "role": "tool",
- "content": '{"message":"handled"}',
- "tool_call_id": "call_2",
- },
- {"role": "user", "content": "continue"},
- ]
- async def _assert_tool_transcript_rejected(
- messages: list[ChatMessage],
- error_match: str,
- ) -> None:
- network_called = False
- def handler(request: httpx.Request) -> httpx.Response:
- nonlocal network_called
- network_called = True
- return httpx.Response(200)
- async with httpx.AsyncClient(
- transport=httpx.MockTransport(handler),
- base_url="https://llm.test/v1",
- ) as http_client:
- client = OpenAICompatibleChatClient(
- api_key="",
- base_url="https://llm.test/v1",
- default_model="provider-default",
- request_timeout_seconds=5,
- http_client=http_client,
- )
- with pytest.raises(ValueError, match=error_match):
- [
- item
- async for item in client.stream_chat(
- messages=messages,
- tools=[],
- params=AgentParams(model="provider-default"),
- )
- ]
- assert network_called is False
- @pytest.mark.asyncio
- async def test_openai_chat_client_rejects_orphan_tool_reply_before_network():
- await _assert_tool_transcript_rejected(
- [
- ChatMessage(
- role="tool",
- content="orphan",
- tool_call_id="call_1",
- )
- ],
- "orphan tool reply: call_1",
- )
- @pytest.mark.asyncio
- async def test_openai_chat_client_rejects_mismatched_tool_call_id_before_network():
- await _assert_tool_transcript_rejected(
- [
- ChatMessage(
- role="assistant",
- content="",
- tool_calls=[
- ToolCallEvent(
- id="call_1",
- name="mock_search",
- arguments={},
- raw_arguments="{}",
- )
- ],
- ),
- ChatMessage(role="tool", content="wrong", tool_call_id="call_2"),
- ],
- "mismatched tool_call_id: call_2",
- )
- @pytest.mark.asyncio
- async def test_openai_chat_client_rejects_duplicate_tool_reply_before_network():
- await _assert_tool_transcript_rejected(
- [
- ChatMessage(
- role="assistant",
- content="",
- tool_calls=[
- ToolCallEvent(
- id="call_1",
- name="mock_search",
- arguments={},
- raw_arguments="{}",
- )
- ],
- ),
- ChatMessage(role="tool", content="first", tool_call_id="call_1"),
- ChatMessage(role="tool", content="duplicate", tool_call_id="call_1"),
- ],
- "duplicate tool reply: call_1",
- )
- @pytest.mark.asyncio
- async def test_openai_chat_client_rejects_non_tool_message_with_unresolved_calls():
- await _assert_tool_transcript_rejected(
- [
- ChatMessage(
- role="assistant",
- content="checking",
- tool_calls=[
- ToolCallEvent(
- id="call_1",
- name="mock_search",
- arguments={},
- raw_arguments="{}",
- ),
- ToolCallEvent(
- id="call_2",
- name="handoff_note",
- arguments={},
- raw_arguments="{}",
- ),
- ],
- ),
- ChatMessage(role="tool", content="done", tool_call_id="call_1"),
- ChatMessage(role="user", content="continue too early"),
- ],
- "unresolved tool calls before user message: call_2",
- )
- @pytest.mark.asyncio
- async def test_openai_chat_client_rejects_unfinished_tool_calls_at_end():
- await _assert_tool_transcript_rejected(
- [
- ChatMessage(
- role="assistant",
- content="checking",
- tool_calls=[
- ToolCallEvent(
- id="call_1",
- name="mock_search",
- arguments={},
- raw_arguments="{}",
- )
- ],
- )
- ],
- "unresolved tool calls at end: call_1",
- )
- @pytest.mark.asyncio
- async def test_openai_chat_client_rejects_reused_tool_call_id_across_transcript():
- await _assert_tool_transcript_rejected(
- [
- ChatMessage(
- role="assistant",
- content="first round",
- tool_calls=[
- ToolCallEvent(
- id="call_1",
- name="mock_search",
- arguments={},
- raw_arguments="{}",
- )
- ],
- ),
- ChatMessage(role="tool", content="done", tool_call_id="call_1"),
- ChatMessage(
- role="assistant",
- content="second round",
- tool_calls=[
- ToolCallEvent(
- id="call_1",
- name="handoff_note",
- arguments={},
- raw_arguments="{}",
- )
- ],
- ),
- ],
- "duplicate assistant tool-call ID across transcript: call_1",
- )
- async def _assert_tool_choice_rejected(
- tools: list[dict],
- tool_choice: dict,
- error_match: str,
- ) -> None:
- network_called = False
- def handler(request: httpx.Request) -> httpx.Response:
- nonlocal network_called
- network_called = True
- return httpx.Response(200)
- async with httpx.AsyncClient(
- transport=httpx.MockTransport(handler),
- base_url="https://llm.test/v1",
- ) as http_client:
- client = OpenAICompatibleChatClient(
- api_key="",
- base_url="https://llm.test/v1",
- default_model="provider-default",
- request_timeout_seconds=5,
- http_client=http_client,
- )
- with pytest.raises(ValueError, match=error_match):
- [
- item
- async for item in client.stream_chat(
- messages=[ChatMessage(role="user", content="use a tool")],
- tools=tools,
- params=AgentParams(model="provider-default"),
- tool_choice=tool_choice,
- )
- ]
- assert network_called is False
- @pytest.mark.asyncio
- async def test_openai_chat_client_rejects_forced_choice_without_tools():
- await _assert_tool_choice_rejected(
- tools=[],
- tool_choice={
- "type": "function",
- "function": {"name": "handoff_note"},
- },
- error_match="forced tool choice requires tools",
- )
- @pytest.mark.asyncio
- async def test_openai_chat_client_rejects_forced_choice_for_unknown_tool():
- await _assert_tool_choice_rejected(
- tools=[
- {
- "type": "function",
- "function": {"name": "mock_search", "parameters": {}},
- }
- ],
- tool_choice={
- "type": "function",
- "function": {"name": "handoff_note"},
- },
- error_match="forced tool choice references unknown tool: handoff_note",
- )
- def test_static_pre_message_role_selector_does_not_offer_tool():
- html = Path("src/agent_lab/presentation/static/index.html").read_text()
- assert '<option value="tool">tool</option>' not in html
- def test_static_tools_ui_is_dynamic_and_not_hardcoded_to_handoff_note_checkbox():
- html = Path("src/agent_lab/presentation/static/index.html").read_text()
- js = Path("src/agent_lab/presentation/static/app.js").read_text()
- assert 'id="tool-handoff-note"' not in html
- assert "#tool-handoff-note" not in js
- assert 'id="tool-list"' in html
- assert '"/api/tools"' in js
- assert "fetch" in js
- def test_static_workspace_snapshot_controls_are_available_outside_agent_config():
- html = Path("src/agent_lab/presentation/static/index.html").read_text()
- chat_dialog_start = html.index('<dialog id="chat-config-dialog"')
- event_dialog_start = html.index('<dialog id="event-config-dialog"')
- chat_dialog_html = html[chat_dialog_start:event_dialog_start]
- sidebar_html = html[:chat_dialog_start]
- assert 'id="open-prompt-config"' not in html
- assert '<dialog id="prompt-dialog"' not in html
- assert 'id="workspace-snapshot-name"' in sidebar_html
- assert 'id="saved-workspace-snapshots"' in sidebar_html
- assert 'id="save-workspace-snapshot"' in sidebar_html
- assert 'id="load-workspace-snapshot"' in sidebar_html
- assert 'id="delete-workspace-snapshot"' in sidebar_html
- assert 'id="workspace-snapshot-name"' not in chat_dialog_html
- assert 'id="saved-workspace-snapshots"' not in chat_dialog_html
- assert 'id="save-workspace-snapshot"' not in chat_dialog_html
- assert 'id="system-prompts"' in chat_dialog_html
- assert 'id="pre-messages"' in chat_dialog_html
- def test_static_session_ui_controls_and_replay_panels_are_available():
- html = Path("src/agent_lab/presentation/static/index.html").read_text()
- chat_dialog_start = html.index('<dialog id="chat-config-dialog"')
- event_dialog_start = html.index('<dialog id="event-config-dialog"')
- chat_dialog_html = html[chat_dialog_start:event_dialog_start]
- event_dialog_html = html[event_dialog_start:]
- sidebar_html = html[:chat_dialog_start]
- for control_id in [
- "session-title",
- "session-list",
- "new-session",
- "refresh-sessions",
- "load-session",
- "session-status",
- ]:
- assert f'id="{control_id}"' in sidebar_html
- assert f'id="{control_id}"' not in chat_dialog_html
- assert f'id="{control_id}"' not in event_dialog_html
- for replay_id in [
- "audit-replay",
- "audit-count",
- "session-usage-summary",
- "session-usage-turns",
- "session-usage-calls",
- ]:
- assert f'id="{replay_id}"' in html
- def test_static_chat_agent_prompt_is_a_single_sortable_list_with_event_instructions():
- html = Path("src/agent_lab/presentation/static/index.html").read_text()
- js = Path("src/agent_lab/presentation/static/app.js").read_text()
- css = Path("src/agent_lab/presentation/static/styles.css").read_text()
- assert 'id="chat-system-prompt"' not in html
- assert 'id="chat-event-prompt-preview"' not in html
- assert 'class="prompt-list stack"' in html
- assert 'class="prompt-item"' in html
- assert 'data-prompt-role="system"' in html
- assert "prompt-type" in html
- assert "function buildAvailableEventsPrompt(" in js
- assert "Available events:" in js
- assert "function updateEventPromptItem(" in js
- assert "function createSystemPrompt(" in js
- assert "function movePromptItem(" in js
- assert 'draggable = true' in js
- assert "dataset.promptRole" in js
- assert "prompt-title" in js
- assert "delete-system-prompt" in js
- assert 'prompt_kind: "event_agent_system"' in js
- assert "system_prompts: collectSystemPrompts()" in js
- assert 'system_prompt: ""' in js
- assert ".prompt-item" in css
- assert ".prompt-item.dragging" in css
- assert ".prompt-meta" in css
- assert ".prompt-type" in css
- assert ".prompt-actions button" in css
- assert "#open-prompt-config" not in js
- assert "promptDialog" not in js
- def test_static_app_script_is_versioned_and_click_binding_is_guarded():
- html = Path("src/agent_lab/presentation/static/index.html").read_text()
- js = Path("src/agent_lab/presentation/static/app.js").read_text()
- assert '<script src="/static/app.js?v=' in html
- assert "function bindClick(" in js
- assert 'bindClick("#open-chat-config-panel", openChatConfig)' in js
- assert 'bindClick("#open-event-config-panel", openEventConfig)' in js
- def test_static_agent_config_controls_are_aligned_for_chat_and_event_agents():
- html = Path("src/agent_lab/presentation/static/index.html").read_text()
- js = Path("src/agent_lab/presentation/static/app.js").read_text()
- assert '<dialog id="chat-config-dialog"' in html
- assert '<dialog id="event-config-dialog"' in html
- assert html.count('id="open-chat-config-panel"') == 1
- assert html.count('id="open-event-config-panel"') == 1
- assert 'id="open-chat-config"' not in html
- assert 'id="open-event-config"' not in html
- assert '#open-chat-config"' not in js
- assert '#open-event-config"' not in js
- assert '#open-chat-config-panel"' in js
- assert '#open-event-config-panel"' in js
- assert 'id="close-chat-config"' in html
- assert 'id="close-event-config"' in html
- assert "chatConfigDialog.showModal()" in js
- assert "eventConfigDialog.showModal()" in js
- assert "chatConfigDialog.close()" in js
- assert "eventConfigDialog.close()" in js
- for control_id in [
- "model",
- "temperature",
- "max-tokens",
- "chat-thinking-disabled",
- "chat-enable-search",
- "chat-forced-search",
- "event-model",
- "event-temperature",
- "event-max-tokens",
- "event-system-prompt",
- "event-thinking-disabled",
- "event-enable-search",
- "event-forced-search",
- ]:
- assert f'id="{control_id}"' in html
- assert 'extra_body: buildExtraBody("chat")' in js
- assert 'extra_body: buildExtraBody("event")' in js
- assert 'system_prompt: ""' in js
- assert 'system_prompt: document.querySelector("#event-system-prompt")' in js
- def test_static_tool_mode_and_batch_controls_explain_active_configuration():
- html = Path("src/agent_lab/presentation/static/index.html").read_text()
- js = Path("src/agent_lab/presentation/static/app.js").read_text()
- assert 'id="tool-invocation-mode"' in html
- assert '<option value="dual_agent" selected>' in html
- assert '<option value="chat_agent_tools">' in html
- assert 'id="tool-mode-help"' in html
- assert 'id="max-parallel-events"' in html
- assert 'value="4"' in html
- assert 'id="batch-timeout-seconds"' in html
- assert 'value="15"' in html
- assert "missing-argument fallback" in js
- assert "EventAgent model, temperature, max tokens, system prompt, and extra body are inactive" in js
- assert "enabled tools, loop budget, batch concurrency, and timeout remain active" in js
- assert "function updateToolModeHelp()" in js
- def test_static_tool_mode_config_roundtrips_request_snapshot_and_session_restore():
- js = Path("src/agent_lab/presentation/static/app.js").read_text()
- build_snapshot = js[
- js.index("function buildWorkspaceSnapshot()") :
- js.index("function restoreWorkspaceSnapshot(")
- ]
- restore_snapshot = js[
- js.index("function restoreWorkspaceSnapshot(") :
- js.index("function buildExtraBody(")
- ]
- build_request = js[
- js.index("function buildRequest(") :
- js.index("function selectedTools(")
- ]
- load_replay = js[
- js.index("async function loadSessionReplay(") :
- js.index("function setSessionStatus(")
- ]
- create_session = js[
- js.index("async function createSession()") :
- js.index("async function loadSelectedSession()")
- ]
- for source in [build_snapshot, build_request]:
- assert 'tool_invocation_mode: document.querySelector("#tool-invocation-mode").value' in source
- assert 'max_parallel_events: Number(document.querySelector("#max-parallel-events").value)' in source
- assert 'batch_timeout_seconds: Number(document.querySelector("#batch-timeout-seconds").value)' in source
- assert 'snapshot.tool_invocation_mode || "dual_agent"' in restore_snapshot
- assert 'eventAgent.max_parallel_events ?? 4' in restore_snapshot
- assert 'eventAgent.batch_timeout_seconds ?? 15' in restore_snapshot
- assert "updateToolModeHelp();" in restore_snapshot
- assert '`/api/sessions/${sessionId}`' in load_replay
- assert "const [session, messages, audit, usage] = await Promise.all([" in load_replay
- assert "restoreWorkspaceSnapshot(session.config || {});" in load_replay
- assert "config: buildWorkspaceSnapshot()" in create_session
- def test_static_tools_ui_shows_tool_metadata_and_selection_actions():
- html = Path("src/agent_lab/presentation/static/index.html").read_text()
- js = Path("src/agent_lab/presentation/static/app.js").read_text()
- assert 'id="tool-count"' in html
- assert 'id="select-all-tools"' in html
- assert 'id="clear-tools"' in html
- assert "tool-card" in js
- assert "tool-description" in js
- assert "tool-required" in js
- assert "function updateToolCount()" in js
- def test_static_app_handles_audit_messages():
- js = Path("src/agent_lab/presentation/static/app.js").read_text()
- css = Path("src/agent_lab/presentation/static/styles.css").read_text()
- assert 'message.type === "audit"' in js
- assert "appendAuditEntry(message)" in js
- assert 'appendLog("audit"' not in js
- assert "function renderAuditDetail(" in js
- assert "chat_agent_request" in js
- assert "event_agent_response" in js
- assert "chat_message_stream_started" in js
- assert "chat_message_stream_finished" in js
- assert ".audit-event" in css
- assert ".audit-detail" in css
- def test_static_audit_replay_uses_event_stream_and_detail_panel():
- html = Path("src/agent_lab/presentation/static/index.html").read_text()
- js = Path("src/agent_lab/presentation/static/app.js").read_text()
- css = Path("src/agent_lab/presentation/static/styles.css").read_text()
- assert '<link rel="stylesheet" href="/static/styles.css?v=' in html
- assert "tool-mode-session-restore" in html
- assert 'id="audit-dialog"' in html
- assert 'id="open-audit-dialog"' in html
- assert 'id="close-audit-dialog"' in html
- assert 'id="audit-summary"' in html
- assert 'id="audit-replay" class="audit-stream"' in html
- assert 'id="audit-detail"' in html
- assert "auditDialog.showModal()" in js
- assert "auditDialog.close()" in js
- assert "auditEntryKey(entry)" in js
- assert "selectAuditEntry(key)" in js
- assert 'meta.className = "audit-detail-meta"' in js
- assert 'appendMetaField(meta, "Since User Message"' in js
- assert "auditRelativeTime(entry)" in js
- assert 'const list = document.createElement("ul")' in js
- assert "appendKeyValueSection(auditDetail, \"Model Parameters\"" in js
- assert "appendRepliesSection(auditDetail, details.replies)" in js
- assert "appendRawChunksSection(auditDetail, \"Raw LLM Chunks\"" in js
- assert "appendRawModelChunksSection(auditDetail, details.raw_model_chunks)" in js
- assert "Enabled This Round" in js
- assert "Configured Events" in js
- assert "details.configured_events" in js
- assert "enabled this round" in js
- assert ".audit-modal" in css
- assert ".audit-modal-body" in css
- assert ".audit-modal-body section + section" in css
- assert ".audit-detail-title" in css
- assert ".detail-tags code" in css
- assert ".audit-detail-meta span" not in css
- assert ".detail-tags span" not in css
- assert "display: block" in _css_rule(css, ".audit-detail")
- assert "display: grid" not in _css_rule(css, ".audit-detail")
- assert "display: block" in _css_rule(css, ".audit-detail-header")
- assert "display: grid" not in _css_rule(css, ".audit-detail-header")
- assert "display: inline-flex" in _css_rule(css, ".audit-detail-meta div")
- assert ".audit-marker::before" in css
- assert ".audit-event.is-selected" in css
- def test_static_layout_keeps_controls_and_composer_fixed_with_scrollable_panes():
- css = Path("src/agent_lab/presentation/static/styles.css").read_text()
- assert "height: calc(100vh - 56px)" in css
- assert ".layout" in css and "overflow: hidden" in css
- assert ".controls" in css and "overflow: auto" in css
- assert ".workspace {\n display: grid;\n grid-template-rows: minmax(0, 1fr) auto auto;" in css
- assert ".workspace-body" in css and "overflow: hidden" in css
- assert ".messages" in css and "overflow: auto" in css
- assert ".audit-stream" in css and "overflow: auto" in css
- def test_static_app_shows_wait_state_and_immediate_user_echo():
- html = Path("src/agent_lab/presentation/static/index.html").read_text()
- js = Path("src/agent_lab/presentation/static/app.js").read_text()
- css = Path("src/agent_lab/presentation/static/styles.css").read_text()
- assert 'id="run-button"' in html
- assert 'const runButton = document.querySelector("#run-button")' in js
- assert 'appendLog("user", submittedMessage)' in js
- assert 'statusEl.textContent = "Waiting for first token"' in js
- assert "function startElapsedTimer()" in js
- assert "function stopElapsedTimer()" in js
- assert "function setRunState(" in js
- assert "if (firstTokenAt) {\n hasBackendRoundStats = true;" in js
- assert "hasBackendRoundStats && firstTokenAt" in js
- assert ".user" in css
- def test_static_app_reuses_websocket_for_session_turns():
- js = Path("src/agent_lab/presentation/static/app.js").read_text()
- assert "function isSocketOpen()" in js
- assert "socket.send(JSON.stringify({ type: \"user_message\"" in js
- assert "session_id: currentSessionId || undefined" in js
- assert "message.session_id" in js
- assert 'message.type === "turn_completed"' in js
- assert 'message.type === "done"' in js
- def test_static_app_loads_session_replay_and_usage():
- js = Path("src/agent_lab/presentation/static/app.js").read_text()
- create_session = js[
- js.index("async function createSession()") :
- js.index("async function loadSelectedSession()")
- ]
- load_session = js[
- js.index("async function loadSelectedSession()") :
- js.index("async function loadSessionReplay(")
- ]
- assert "function loadSessions(" in js
- assert "function createSession(" in js
- assert "function setSessionStatus(" in js
- assert '"Refreshing sessions"' in js
- assert '"Creating session"' in js
- assert '"Session created"' in js
- assert 'sessionStatus.textContent = message' in js
- assert "function loadSessionReplay(" in js
- assert 'fetchJson("/api/sessions")' in js
- assert 'method: "POST"' in js
- assert '`/api/sessions/${sessionId}/messages`' in js
- assert '`/api/sessions/${sessionId}/audit`' in js
- assert '`/api/sessions/${sessionId}/usage`' in js
- assert "function renderAuditReplay(" in js
- assert "function renderSessionUsage(" in js
- assert "session.turn_wall_time_ms ?? session.elapsed_ms" in js
- assert "turn.turn_wall_time_ms ?? turn.elapsed_ms" in js
- assert "call.mode" in js
- assert "call.agent" in js
- assert "call.call_kind" in js
- assert "call.used_fallback" in js
- assert "call.tool_latency_ms" in js
- assert "sessionUsageSummary" in js
- assert "Finish current turn before switching sessions" in js
- assert "await loadSessionReplay(session.id, { preserveStatus: true });" in create_session
- assert "await loadSessionReplay(sessionId);" in load_session
- def test_static_session_replay_ignores_superseded_success():
- js = Path("src/agent_lab/presentation/static/app.js").read_text()
- result = _run_load_session_replay_scenario(
- """
- currentSessionId = "old";
- const oldLoad = loadSessionReplay("old");
- currentSessionId = "new";
- const newLoad = loadSessionReplay("new");
- resolveReplay("new");
- await newLoad;
- resolveReplay("old");
- await oldLoad;
- console.log(JSON.stringify({
- ...mutations,
- title: sessionTitle.value,
- selected: sessionList.value,
- }));
- """
- )
- assert result == {
- "workspace": ["new"],
- "transcript": ["new"],
- "audit": ["new"],
- "usage": ["new"],
- "status": ["Session loaded"],
- "title": "new title",
- "selected": "new",
- }
- assert "let sessionReplayLoadToken = 0;" in js
- def test_static_session_replay_ignores_superseded_failure_status():
- result = _run_load_session_replay_scenario(
- """
- currentSessionId = "old";
- const oldLoad = loadSessionReplay("old");
- currentSessionId = "new";
- const newLoad = loadSessionReplay("new");
- resolveReplay("new");
- await newLoad;
- pending.get("/api/sessions/old").reject(new Error("stale failure"));
- await oldLoad;
- console.log(JSON.stringify(mutations.status));
- """
- )
- assert result == ["Session loaded"]
- def test_static_session_replay_rejects_stale_invocation_before_token_advance():
- result = _run_load_session_replay_scenario(
- """
- currentSessionId = "new";
- const newLoad = loadSessionReplay("new");
- const tokenAfterNew = sessionReplayLoadToken;
- const oldLoad = loadSessionReplay("old");
- const oldResult = await Promise.race([
- oldLoad.then(() => "returned"),
- new Promise((resolve) => setTimeout(() => resolve("pending"), 0)),
- ]);
- const tokenAfterOld = sessionReplayLoadToken;
- const oldFetchStarted = pending.has("/api/sessions/old");
- resolveReplay("new");
- await newLoad;
- console.log(JSON.stringify({
- oldResult,
- tokenAfterNew,
- tokenAfterOld,
- oldFetchStarted,
- mutations,
- title: sessionTitle.value,
- selected: sessionList.value,
- }));
- """
- )
- assert result == {
- "oldResult": "returned",
- "tokenAfterNew": 1,
- "tokenAfterOld": 1,
- "oldFetchStarted": False,
- "mutations": {
- "workspace": ["new"],
- "transcript": ["new"],
- "audit": ["new"],
- "usage": ["new"],
- "status": ["Session loaded"],
- },
- "title": "new title",
- "selected": "new",
- }
- def test_static_replay_hides_whitespace_assistant_tool_protocol_records():
- js = Path("src/agent_lab/presentation/static/app.js").read_text()
- render_replay = js[
- js.index("function renderReplayMessages(") :
- js.index("function appendAuditEntry(")
- ]
- result = _run_node_json(
- "\n".join(
- [
- """
- let activeAssistant = null;
- const messagesEl = {
- children: [],
- scrollHeight: 0,
- scrollTop: 0,
- set textContent(value) {
- if (value === "") {
- this.children = [];
- }
- },
- append(item) {
- this.children.push(item);
- },
- };
- const document = {
- createElement() {
- return { className: "", textContent: "" };
- },
- };
- function appendLog(kind, content) {
- messagesEl.append({ className: `message ${kind}`, textContent: content });
- }
- """,
- render_replay,
- """
- renderReplayMessages([
- { role: "assistant", content: " \\n\\t", tool_calls: [{ id: "call-1" }] },
- { role: "assistant", content: "visible", tool_calls: [] },
- ]);
- console.log(JSON.stringify(messagesEl.children.map((item) => item.textContent)));
- """,
- ]
- )
- )
- assert result == ["visible"]
- def test_static_session_switch_guard_uses_active_turn_not_open_socket():
- js = Path("src/agent_lab/presentation/static/app.js").read_text()
- create_session = js[js.index("async function createSession()") : js.index("async function loadSelectedSession()")]
- load_session = js[js.index("async function loadSelectedSession()") : js.index("async function loadSessionReplay(")]
- assert "function hasActiveTurn()" in js
- assert "turnActive = isRunning" in js
- assert "if (hasActiveTurn())" in create_session
- assert "if (hasActiveTurn())" in load_session
- assert "if (isSocketOpen())" not in create_session
- assert "if (isSocketOpen())" not in load_session
- assert "closeSocket();" in create_session
- assert "closeSocket();" in load_session
- assert "const activeSocket = socket" in js
- assert "if (socket !== activeSocket)" in js
- def test_static_workspace_snapshot_uses_stable_storage_hooks():
- js = Path("src/agent_lab/presentation/static/app.js").read_text()
- assert "agent-lab.workspace-snapshots.v1" in js
- assert "agent-lab.prompt-sets.v1" in js
- assert "function saveWorkspaceSnapshot()" in js
- assert "function loadWorkspaceSnapshot()" in js
- assert "function deleteWorkspaceSnapshot()" in js
- assert "function buildWorkspaceSnapshot()" in js
- assert "prompt_items: buildPromptItems()" in js
- assert "enabled_tools: selectedTools()" in js
- assert "extra_body: buildExtraBody(\"chat\")" in js
- assert "extra_body: buildExtraBody(\"event\")" in js
- def test_static_app_handles_backend_round_stats_messages():
- js = Path("src/agent_lab/presentation/static/app.js").read_text()
- assert 'message.type === "round_stats"' in js
- assert "function updateRoundStats(" in js
- assert "#stat-ttft" in js
- assert "#stat-elapsed" in js
- @pytest.mark.asyncio
- async def test_openai_chat_client_omits_stream_options_when_usage_disabled():
- captured_payloads: list[dict] = []
- def handler(request: httpx.Request) -> httpx.Response:
- captured_payloads.append(json.loads(request.content))
- return httpx.Response(
- 200,
- content=b'data: {"choices":[{"delta":{"content":"hi"},"finish_reason":null}]}\n\n',
- )
- async with httpx.AsyncClient(
- transport=httpx.MockTransport(handler),
- base_url="https://llm.test/v1",
- ) as http_client:
- client = OpenAICompatibleChatClient(
- api_key="",
- base_url="https://llm.test/v1",
- default_model="provider-default",
- request_timeout_seconds=5,
- include_usage=False,
- http_client=http_client,
- )
- [
- item
- async for item in client.stream_chat(
- messages=[ChatMessage(role="user", content="hi")],
- tools=[],
- params=AgentParams(model="provider-default", temperature=0.3, max_tokens=50),
- )
- ]
- assert "stream_options" not in captured_payloads[0]
- @pytest.mark.asyncio
- async def test_openai_chat_client_merges_agent_extra_body_into_payload():
- captured_payloads: list[dict] = []
- def handler(request: httpx.Request) -> httpx.Response:
- captured_payloads.append(json.loads(request.content))
- return httpx.Response(
- 200,
- content=b'data: {"choices":[{"delta":{"content":"hi"},"finish_reason":null}]}\n\n',
- )
- async with httpx.AsyncClient(
- transport=httpx.MockTransport(handler),
- base_url="https://llm.test/v1",
- ) as http_client:
- client = OpenAICompatibleChatClient(
- api_key="",
- base_url="https://llm.test/v1",
- default_model="provider-default",
- request_timeout_seconds=5,
- http_client=http_client,
- )
- [
- item
- async for item in client.stream_chat(
- messages=[ChatMessage(role="user", content="hi")],
- tools=[],
- params=AgentParams(
- model="provider-default",
- temperature=0.3,
- max_tokens=50,
- extra_body={
- "thinking": {"type": "disabled"},
- "enable_search": False,
- "search_options": {"forced_search": False},
- },
- ),
- )
- ]
- assert captured_payloads[0]["thinking"] == {"type": "disabled"}
- assert captured_payloads[0]["enable_search"] is False
- assert captured_payloads[0]["search_options"] == {"forced_search": False}
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("reserved_key", "override_value"),
- [
- ("model", "overridden-model"),
- ("messages", []),
- ("tools", []),
- (
- "tool_choice",
- {"type": "function", "function": {"name": "other_tool"}},
- ),
- ("stream", False),
- ("stream_options", {"include_usage": False}),
- ("temperature", 1.0),
- ("max_tokens", 1),
- ],
- )
- async def test_openai_chat_client_rejects_reserved_extra_body_fields_before_network(
- reserved_key: str,
- override_value: object,
- ):
- network_called = False
- def handler(request: httpx.Request) -> httpx.Response:
- nonlocal network_called
- network_called = True
- return httpx.Response(200)
- async with httpx.AsyncClient(
- transport=httpx.MockTransport(handler),
- base_url="https://llm.test/v1",
- ) as http_client:
- client = OpenAICompatibleChatClient(
- api_key="",
- base_url="https://llm.test/v1",
- default_model="provider-default",
- request_timeout_seconds=5,
- http_client=http_client,
- )
- with pytest.raises(
- ValueError,
- match=f"extra_body contains reserved field: {reserved_key}",
- ):
- [
- item
- async for item in client.stream_chat(
- messages=[ChatMessage(role="user", content="hi")],
- tools=[],
- params=AgentParams(extra_body={reserved_key: override_value}),
- )
- ]
- assert network_called is False
|