Breaks the two import cycles that forced Protocol stand-ins in the F821 sweep, so the two sites now name the real types. gateway/platforms/event.py (new leaf): MessageType, ProcessingOutcome, MessageEvent moved out of base.py verbatim. Their only dependency is gateway.session.SessionSource; base.py imported helpers.py at module level, so helpers could not name MessageEvent. Now TextBatchAggregator is typed by the real MessageEvent. 249 importers repointed (`from gateway.platforms.base import` -> `.event`, preserving each import's layout); gateway.platforms.__init__ re-exports from .event. The three revert-scheduled PLUGIN-COMPAT pointers that named these symbols (gateway.slash_commands → MessageType, dingtalk → MessageType, photon → ProcessingOutcome) and their COMPAT_MANIFEST rows now target gateway.platforms.event. Docs updated: ADDING_A_PLATFORM.md, adding-platform-adapters.md (en + zh-Hans). tools/mcp_tool_sampling.py: ElicitationHandler no longer holds a back-reference to its MCPServerTask (mcp_tool imports sampling, so the task type cannot be named there). It only ever read owner._pending_call_context, so it takes `call_context: Callable[[], Context | None]` and MCPServerTask passes `lambda: self._pending_call_context`. The consent call is one `functools.partial`, run directly or inside the captured Context. ty on the 11 touched production files vs origin/main: 0 new diagnostics, 14 resolved. (The one `source: SessionSource = None` diagnostic moves with the class; typing it Optional exposes ~60 unguarded call sites — separate follow-up.) Tests: tests/gateway + tests/plugins + tests/tools + touched files, 18,235 passed; the 31 failures reproduce identically on origin/main (macOS /private/tmp, systemd socket, long-path fixtures, live-service tests).
104 lines
3.8 KiB
Python
104 lines
3.8 KiB
Python
"""Tests for the document context note prepended to user turns with attachments.
|
|
|
|
A user who attaches a PDF / DOCX in chat used to see the agent treat it as
|
|
"unreadable" because the context note told the model to "Ask the user what
|
|
they'd like you to do with it" — steering it away from extracting the text it
|
|
is perfectly capable of reading. These tests pin the contract:
|
|
|
|
- text documents: note confirms the (adapter-)inlined content + records path.
|
|
- binary documents (PDF/DOCX/…): note tells the agent to extract the text
|
|
itself and never tells it to punt back to the user.
|
|
"""
|
|
|
|
import importlib
|
|
|
|
import pytest
|
|
|
|
from gateway.config import GatewayConfig, Platform, PlatformConfig
|
|
from gateway.platforms.event import MessageEvent, MessageType
|
|
from gateway.run import GatewayRunner
|
|
from gateway.session import SessionSource
|
|
|
|
gateway_run = importlib.import_module("gateway.run")
|
|
_build_document_context_note = gateway_run._build_document_context_note
|
|
|
|
|
|
class TestTextDocumentNote:
|
|
@pytest.mark.parametrize("mtype", ["text/plain", "text/markdown", "text/csv"])
|
|
def test_text_note_mentions_included_content_and_path(self, mtype):
|
|
note = _build_document_context_note("notes.txt", "/cache/doc_notes.txt", mtype)
|
|
assert "text document" in note
|
|
assert "notes.txt" in note
|
|
assert "/cache/doc_notes.txt" in note
|
|
assert "included below" in note
|
|
|
|
def test_non_inlined_text_note_tells_agent_to_read_cached_path(self):
|
|
note = _build_document_context_note(
|
|
"notes.txt",
|
|
"/cache/doc_notes.txt",
|
|
"text/plain",
|
|
content_inlined=False,
|
|
)
|
|
assert "included below" not in note
|
|
assert "/cache/doc_notes.txt" in note
|
|
assert "read" in note.lower()
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_event_contract_marks_non_inlined_text_and_preserves_path(self):
|
|
runner = object.__new__(GatewayRunner)
|
|
runner.config = GatewayConfig(
|
|
platforms={Platform.TELEGRAM: PlatformConfig(enabled=True, token="fake")}
|
|
)
|
|
runner.adapters = {}
|
|
runner._pending_native_image_paths_by_session = {}
|
|
runner._session_model_overrides = {}
|
|
runner._session_reasoning_overrides = {}
|
|
source = SessionSource(
|
|
platform=Platform.TELEGRAM,
|
|
chat_id="text-document",
|
|
chat_type="dm",
|
|
user_id="42",
|
|
user_name="Tester",
|
|
)
|
|
event = MessageEvent(
|
|
text="summarize this",
|
|
message_type=MessageType.DOCUMENT,
|
|
source=source,
|
|
media_urls=["/cache/notes.txt"],
|
|
media_types=["text/plain"],
|
|
media_text_inlined=[False],
|
|
)
|
|
|
|
prepared = await runner._prepare_inbound_message_text(
|
|
event=event,
|
|
source=source,
|
|
history=[],
|
|
)
|
|
|
|
assert prepared is not None
|
|
assert "/cache/notes.txt" in prepared
|
|
assert "included below" not in prepared
|
|
assert "read the cached file" in prepared.lower()
|
|
|
|
|
|
class TestBinaryDocumentNote:
|
|
@pytest.mark.parametrize(
|
|
"mtype",
|
|
[
|
|
"application/pdf",
|
|
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
"application/octet-stream",
|
|
],
|
|
)
|
|
def test_binary_note_guides_extraction(self, mtype):
|
|
note = _build_document_context_note("contract.pdf", "/cache/doc_contract.pdf", mtype)
|
|
# Records the path so the agent can open it.
|
|
assert "/cache/doc_contract.pdf" in note
|
|
# Tells the agent to read it by extracting the text...
|
|
assert "extract" in note.lower()
|
|
# ...and does NOT steer it into punting back to the user (the bug).
|
|
assert "ask the user" not in note.lower()
|
|
assert "paste" in note.lower()
|
|
|