feat: conformance test runner + protocol fixes
- conformance.py: declarative scenario runner (subset matcher, $kind/$gt/ $enum/$required assertions, call_id/session_id echo checks), reference bridge, CLI entry (python -m aicc.conformance) - bridge.py: protocol version check before parsing (protocol_mismatch), session expiry handling (session_expired), malformed tool_call -> invalid_input, emit_event() for async events - client.py: Self return type - in_process.py: drain bridge event queue after response (event delivery), Self return type - websocket.py: Self return type, best-effort shutdown - tests: 14 passing (incl. conformance scenarios, 9/9 core scenarios) - ruff: all checks pass
This commit is contained in:
@@ -6,12 +6,11 @@ Useful for tests, embedded use, and single-process agents that drive the bridge
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import uuid
|
||||
from typing import Any
|
||||
from typing import Any, Self
|
||||
|
||||
from aicc.bridge import Bridge
|
||||
from aicc.errors import ConnectionError_
|
||||
from aicc.protocol import PROTOCOL_VERSION, SessionInit
|
||||
from aicc.protocol import SessionInit
|
||||
|
||||
|
||||
class InProcessTransport:
|
||||
@@ -35,7 +34,7 @@ class InProcessTransport:
|
||||
self._connected = False
|
||||
|
||||
@classmethod
|
||||
def start(cls, bridge: Bridge) -> "InProcessTransport":
|
||||
def start(cls, bridge: Bridge) -> InProcessTransport:
|
||||
t = cls()
|
||||
t._bridge = bridge
|
||||
return t
|
||||
@@ -63,12 +62,20 @@ class InProcessTransport:
|
||||
sid = message.get("session_id") or self._session_id
|
||||
msg_type = message.get("type")
|
||||
# session_resume carries the session_id; bind it
|
||||
if msg_type == "session_resume" and not self._bridge._sessions.get(sid): # noqa: SLF001
|
||||
if msg_type == "session_resume" and not self._bridge._sessions.get(sid):
|
||||
# Auto-open if bridge forgot
|
||||
pass
|
||||
response = await self._bridge.handle_message(sid, message)
|
||||
if response is not None:
|
||||
await self._recv_q.put(response)
|
||||
# Drain any events the bridge queued during handling (e.g. from
|
||||
# tool handlers calling bridge.emit_event). Deliver them after the
|
||||
# response so the client sees a coherent request -> response order.
|
||||
sess = self._bridge._sessions.get(sid)
|
||||
if sess is not None:
|
||||
while not sess.send_queue.empty():
|
||||
event = sess.send_queue.get_nowait()
|
||||
await self._recv_q.put(event)
|
||||
|
||||
async def receive(self) -> dict[str, Any]:
|
||||
if not self._connected:
|
||||
@@ -79,7 +86,7 @@ class InProcessTransport:
|
||||
"""Bridge-side helper to push an event to the connected client."""
|
||||
await self._recv_q.put(event)
|
||||
|
||||
async def __aenter__(self) -> "InProcessTransport":
|
||||
async def __aenter__(self) -> Self:
|
||||
await self.connect()
|
||||
return self
|
||||
|
||||
|
||||
Reference in New Issue
Block a user