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:
+83
-6
@@ -3,14 +3,16 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import inspect
|
||||
import uuid
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any, Callable
|
||||
from typing import Any
|
||||
|
||||
from aicc.protocol import (
|
||||
PROTOCOL_VERSION,
|
||||
Capabilities,
|
||||
ErrorCode,
|
||||
ErrorMessage,
|
||||
ErrorPayload,
|
||||
Heartbeat,
|
||||
MessageType,
|
||||
@@ -24,7 +26,6 @@ from aicc.protocol import (
|
||||
ToolSpec,
|
||||
WorldKind,
|
||||
WorldSpec,
|
||||
PROTOCOL_VERSION,
|
||||
)
|
||||
from aicc.tool import ToolDefinition, call_tool_impl
|
||||
|
||||
@@ -144,6 +145,32 @@ class Bridge:
|
||||
if sess is not None:
|
||||
sess.closed = True
|
||||
|
||||
def emit_event(
|
||||
self,
|
||||
session_id: str,
|
||||
topic: str,
|
||||
payload: dict[str, Any] | None = None,
|
||||
*,
|
||||
tick: int | None = None,
|
||||
) -> None:
|
||||
"""Queue an async event for delivery to a connected agent.
|
||||
|
||||
The transport is responsible for draining `session.send_queue` and
|
||||
forwarding events to the client. Safe to call from tool handlers.
|
||||
"""
|
||||
sess = self._sessions.get(session_id)
|
||||
if sess is None or sess.closed:
|
||||
return
|
||||
from aicc.protocol import EventMessage
|
||||
|
||||
event = EventMessage(
|
||||
session_id=session_id,
|
||||
topic=topic,
|
||||
payload=payload or {},
|
||||
meta={"tick": tick} if tick is not None else None,
|
||||
)
|
||||
sess.send_queue.put_nowait(event.model_dump(by_alias=True))
|
||||
|
||||
def session(self, session_id: str) -> Session:
|
||||
sess = self._sessions.get(session_id)
|
||||
if sess is None or sess.closed:
|
||||
@@ -196,11 +223,48 @@ class Bridge:
|
||||
May also push async events to the session queue.
|
||||
"""
|
||||
msg_type = raw.get("type")
|
||||
|
||||
# Protocol version check BEFORE any model parsing: a wrong version
|
||||
# must be answered with protocol_mismatch, not a parse error.
|
||||
proto = raw.get("protocol")
|
||||
if proto is not None and proto != PROTOCOL_VERSION:
|
||||
return ErrorMessage(
|
||||
session_id=session_id,
|
||||
error=ErrorPayload(
|
||||
code=ErrorCode.PROTOCOL_MISMATCH,
|
||||
message=(
|
||||
f"Protocol mismatch: bridge='{PROTOCOL_VERSION}', "
|
||||
f"message='{proto}'"
|
||||
),
|
||||
retryable=False,
|
||||
),
|
||||
).model_dump(by_alias=True)
|
||||
|
||||
if msg_type == MessageType.MANIFEST_REQUEST.value:
|
||||
sess = self.session(session_id)
|
||||
try:
|
||||
sess = self.session(session_id)
|
||||
except KeyError:
|
||||
return self._session_expired(session_id)
|
||||
return sess.manifest.model_dump(by_alias=True) if sess.manifest else None
|
||||
if msg_type == MessageType.TOOL_CALL.value:
|
||||
return await self._handle_tool_call(session_id, ToolCall.model_validate(raw))
|
||||
try:
|
||||
call = ToolCall.model_validate(raw)
|
||||
except Exception: # noqa: BLE001 - malformed tool_call
|
||||
return ToolResult(
|
||||
session_id=session_id,
|
||||
call_id=raw.get("call_id") or "",
|
||||
ok=False,
|
||||
error=ErrorPayload(
|
||||
code=ErrorCode.INVALID_INPUT,
|
||||
message="Malformed tool_call message.",
|
||||
retryable=False,
|
||||
),
|
||||
).model_dump(by_alias=True)
|
||||
try:
|
||||
self.session(session_id)
|
||||
except KeyError:
|
||||
return self._session_expired(session_id)
|
||||
return await self._handle_tool_call(session_id, call)
|
||||
if msg_type == MessageType.SESSION_CLOSE.value:
|
||||
self.close_session(session_id)
|
||||
return SessionClose(
|
||||
@@ -210,10 +274,23 @@ class Bridge:
|
||||
if msg_type == MessageType.HEARTBEAT.value:
|
||||
return Heartbeat(session_id=session_id).model_dump(by_alias=True)
|
||||
if msg_type == MessageType.SESSION_RESUME.value:
|
||||
sess = self.session(raw.get("session_id") or session_id)
|
||||
try:
|
||||
sess = self.session(raw.get("session_id") or session_id)
|
||||
except KeyError:
|
||||
return self._session_expired(session_id)
|
||||
return sess.manifest.model_dump(by_alias=True) if sess.manifest else None
|
||||
return None
|
||||
|
||||
def _session_expired(self, session_id: str) -> dict[str, Any]:
|
||||
return ErrorMessage(
|
||||
session_id=session_id,
|
||||
error=ErrorPayload(
|
||||
code=ErrorCode.SESSION_EXPIRED,
|
||||
message=f"Session '{session_id}' is unknown or closed.",
|
||||
retryable=False,
|
||||
),
|
||||
).model_dump(by_alias=True)
|
||||
|
||||
async def _handle_tool_call(self, session_id: str, call: ToolCall) -> dict[str, Any]:
|
||||
defn = self._tools.get(call.tool)
|
||||
if defn is None:
|
||||
|
||||
Reference in New Issue
Block a user