diff --git a/crates/tare-py/python/tare/integrations.py b/crates/tare-py/python/tare/integrations.py index 2062d04..c0ad1d3 100644 --- a/crates/tare-py/python/tare/integrations.py +++ b/crates/tare-py/python/tare/integrations.py @@ -251,6 +251,16 @@ async def __call__(self, scope: dict, receive: Any, send: Any) -> None: await self._send_payload_too_large(send) return + # A transformed body can have a different length. Downstream consumers + # must read the ASGI body stream instead of trusting the original length. + scope = { + **scope, + "headers": [ + (name, value) + for name, value in scope.get("headers", []) + if name.lower() != b"content-length" + ], + } body: bytes | None = None response_started = False @@ -262,6 +272,8 @@ async def patched_receive() -> dict: body_buffer = bytearray() event = await receive() while True: + if event.get("type") != "http.request": + return event chunk = event.get("body", b"") if len(body_buffer) + len(chunk) > self.max_body_bytes: raise _RequestBodyTooLarge @@ -310,7 +322,12 @@ def _maybe_compress_body(self, body: bytes) -> bytes: return body if not isinstance(payload, dict) or "messages" not in payload: return body - payload["messages"] = compress_messages(payload["messages"], self.task) + messages = payload["messages"] + if not isinstance(messages, list) or not all( + isinstance(message, dict) for message in messages + ): + return body + payload["messages"] = compress_messages(messages, self.task) return json.dumps(payload).encode() @@ -490,7 +507,11 @@ def __call__(self, messages: list, **kwargs: Any) -> Any: if isinstance(m, dict): content_parts = m.get("content", []) text = ( - content_parts[0].get("text", "") + "\n".join( + part.get("text", "") + for part in content_parts + if isinstance(part, dict) + ) if content_parts and isinstance(content_parts[0], dict) else str(content_parts) ) diff --git a/crates/tare-py/tests/test_nov3_regressions.py b/crates/tare-py/tests/test_nov3_regressions.py new file mode 100644 index 0000000..256a1c0 --- /dev/null +++ b/crates/tare-py/tests/test_nov3_regressions.py @@ -0,0 +1,93 @@ +import asyncio +import importlib +import json +import sys +import types + +import pytest + + +@pytest.fixture +def integrations(): + return importlib.import_module("tare.integrations") + + +@pytest.mark.parametrize( + "prefix", + [[], [{"type": "http.request", "body": b"partial", "more_body": True}]], +) +def test_disconnect_is_not_rewritten_as_a_request(prefix, integrations): + disconnect = {"type": "http.disconnect"} + incoming = iter([*prefix, disconnect]) + observed = [] + + async def receive(): + return next(incoming) + + async def send(event): + pass + + async def app(scope, app_receive, app_send): + observed.append(await app_receive()) + + asyncio.run(integrations.CompressionMiddleware(app)({"type": "http"}, receive, send)) + assert observed == [disconnect] + + +def test_compressed_body_has_no_stale_content_length(monkeypatch, integrations): + monkeypatch.setattr( + integrations, "compress_messages", + lambda messages, task: [{"role": "user", "content": "x"}], + ) + body = json.dumps({"messages": [{"role": "user", "content": "long input" * 50}]}).encode() + scope = { + "type": "http", + "headers": [ + (b"content-type", b"application/json"), + (b"content-length", str(len(body)).encode()), + ], + } + original_headers = list(scope["headers"]) + observed = [] + + async def receive(): + return {"type": "http.request", "body": body, "more_body": False} + + async def send(event): + pass + + async def app(app_scope, app_receive, app_send): + headers = dict(app_scope["headers"]) + event = await app_receive() + observed.append((headers.get(b"content-length"), len(event["body"]))) + + asyncio.run(integrations.CompressionMiddleware(app)(scope, receive, send)) + declared, actual = observed[0] + assert declared is None or int(declared) == actual + assert scope["headers"] == original_headers + + +@pytest.mark.parametrize("messages", ["invalid", {"role": "user"}, [42]]) +def test_unsupported_message_shapes_pass_through_to_application(messages, integrations): + body = json.dumps({"messages": messages}).encode() + middleware = integrations.CompressionMiddleware(None) + assert middleware._maybe_compress_body(body) == body + + +def test_strands_preserves_all_text_parts_before_compression(monkeypatch, integrations): + monkeypatch.setitem(sys.modules, "strands", types.ModuleType("strands")) + received = [] + + def compress(messages, task): + received.extend(messages) + return messages + + monkeypatch.setattr(integrations, "compress_messages", compress) + + class Model: + def __call__(self, messages, **kwargs): + return messages + + wrapper = integrations.strands_model(Model)() + wrapper([{"role": "user", "content": [{"text": "first"}, {"text": "second"}]}]) + assert received == [{"role": "user", "content": "first\nsecond"}]