Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
[codex] fix(proxy): parse CRLF SSE event terminators (#649)
Yui(ゆい)4b1c449c73af
diff --git a/headroom/proxy/helpers.py b/headroom/proxy/helpers.pyindex 1930ec188..baa0e868e 100644--- a/headroom/proxy/helpers.py+++ b/headroom/proxy/helpers.py@@ -669,10 +669,24 @@ def get_body_too_large_status() -> int:return value-# Sentinel used by the SSE byte-buffer helper to mark events that have no-# `event:` line. Per the SSE spec the default event name is "message"; we-# return ``None`` so callers can decide whether to apply that default.-_SSE_EVENT_TERMINATOR = b"\n\n"+# SSE byte-buffer helper supports LF and CRLF event separators. Per the SSE+# spec the default event name is "message"; we return ``None`` so callers can+# decide whether to apply that default.+_SSE_EVENT_TERMINATORS = (b"\n\n", b"\r\n\r\n")+++def _find_sse_event_terminator(buf: bytearray) -> tuple[int, int] | None:+ """Return the earliest complete SSE event terminator in ``buf``."""+ matches = [+ (idx, len(terminator))+ for terminator in _SSE_EVENT_TERMINATORS+ if (idx := buf.find(terminator)) != -1+ ]+ if not matches:+ return None+ return min(matches, key=lambda match: match[0])++_SSE_EVENT_LINE_PREFIX = b"event:"_SSE_DATA_LINE_PREFIX = b"data:"@@ -718,20 +732,20 @@ def parse_sse_events_from_byte_buffer(multi-byte characters split across TCP reads will corrupt content."""events: list[tuple[str | None, str]] = []- terminator = _SSE_EVENT_TERMINATORwhile True:- idx = buf.find(terminator)- if idx == -1:+ terminator_match = _find_sse_event_terminator(buf)+ if terminator_match is None:break+ idx, terminator_len = terminator_matchevent_bytes = bytes(buf[:idx])# Drain the event + the trailing terminator from the buffer.- del buf[: idx + len(terminator)]+ del buf[: idx + terminator_len]# Decoding the COMPLETE event must succeed. If it doesn't, the# upstream emitted invalid UTF-8 mid-stream — surface loudly.event_text = event_bytes.decode("utf-8")event_name: str | None = Nonedata_lines: list[str] = []- for line in event_text.split("\n"):+ for line in event_text.splitlines():if not line:continue# SSE comment line — ignored per spec.diff --git a/tests/test_sse_utf8_split.py b/tests/test_sse_utf8_split.pyindex 989506738..256a44f6e 100644--- a/tests/test_sse_utf8_split.py+++ b/tests/test_sse_utf8_split.py@@ -85,6 +85,16 @@ def test_cjk_split_across_chunks_preserved() -> None:assert parsed["text"] == "日本語テスト"+def test_crlf_terminated_event_is_parsed() -> None:+ """SSE permits CRLF event terminators as well as LF terminators."""+ buf = bytearray(b'event: message\r\ndata: {"ok": true}\r\n\r\n')++ events = parse_sse_events_from_byte_buffer(buf)++ assert events == [("message", '{"ok": true}')]+ assert buf == bytearray()++def test_complete_event_with_invalid_utf8_raises_loud() -> None:"""Invalid UTF-8 in a *complete* event surfaces loudly (not silent corruption)."""# Build a complete event whose data field has invalid UTF-8 bytes