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
fix(proxy): strip only one leading space from SSE field values (#4306)
kevin9226eaf3852f
diff --git a/headroom/proxy/sse_byte_buffer_policy.py b/headroom/proxy/sse_byte_buffer_policy.pyindex c96a19df5..42f5d9fcf 100644--- a/headroom/proxy/sse_byte_buffer_policy.py+++ b/headroom/proxy/sse_byte_buffer_policy.py@@ -10,6 +10,12 @@ SSE_EVENT_LINE_PREFIX = "event:"SSE_DATA_LINE_PREFIX = "data:"+def _field_value(line: str, prefix: str) -> str:+ """Value after ``prefix``; per the SSE spec only one leading space is removed."""+ value = line[len(prefix) :]+ return value[1:] if value.startswith(" ") else value++def find_sse_event_terminator(buf: bytearray) -> tuple[int, int] | None:"""Return the earliest complete SSE event terminator in ``buf``."""matches = [@@ -51,9 +57,9 @@ def parse_sse_events_from_byte_buffer(if line.startswith(":"):continueif line.startswith(SSE_EVENT_LINE_PREFIX):- event_name = line[len(SSE_EVENT_LINE_PREFIX) :].lstrip()+ event_name = _field_value(line, SSE_EVENT_LINE_PREFIX)elif line.startswith(SSE_DATA_LINE_PREFIX):- data_lines.append(line[len(SSE_DATA_LINE_PREFIX) :].lstrip())+ data_lines.append(_field_value(line, SSE_DATA_LINE_PREFIX))if data_lines:events.append((event_name, "\n".join(data_lines)))diff --git a/tests/test_sse_byte_buffer_policy.py b/tests/test_sse_byte_buffer_policy.pyindex d12466dfd..839cab622 100644--- a/tests/test_sse_byte_buffer_policy.py+++ b/tests/test_sse_byte_buffer_policy.py@@ -31,3 +31,11 @@ def test_parse_sse_events_preserves_split_utf8_tail() -> None:def test_parse_sse_events_raises_on_complete_invalid_utf8_event() -> None:with pytest.raises(UnicodeDecodeError):parse_sse_events_from_byte_buffer(bytearray(b"data: \xff\n\n"))+++def test_parse_sse_events_strips_only_one_leading_space() -> None:+ # SSE spec: "If value starts with a U+0020 SPACE character, remove it".+ # Further leading whitespace is part of the payload.+ buf = bytearray(b"event: delta\ndata: indented\ndata:\ttab\ndata:none\n\n")++ assert parse_sse_events_from_byte_buffer(buf) == [("delta", " indented\n\ttab\nnone")]