Skip to content

Commit 9910e95

Browse files
authored
Revert "fix(proxy): slim oversized response.create history (#560)" (#569)
This reverts merge commit e42af5e. The slimming approach rewrites conversation history on the codex-lb side (a stateless forwarding proxy) and conflicts with several stateful surfaces that have to stay consistent with the upstream view: - Prompt-cache invalidation: every slimmed turn changes the input prefix and forces a cache miss against the upstream prompt cache, silently re-billing input tokens. This intersects with #515's pin / affinity work and is invisible to the user. - Assistant-role omission notice ('[codex-lb omitted N items]'): the model treats this as its own prior speech and starts apologising for missing context, hallucinating that earlier history is known, or contradicting the trimmed turns. - WebSocket turn_state mismatch: the Codex WS path is stateful through previous_response_id. After slimming, codex-lb's request view diverges from the upstream session view; replays of the slimmed payload can reference items the upstream copy expected but we already trimmed. These are not knobs we can tune: history compaction belongs in the Codex client (or upstream Responses API), not in a forwarding proxy. The 413 -> slim transition is strictly better for the immediate symptom, but it shifts the failure mode from a clear hard fail into silent degradation of cache cost, model trust, and WS continuity. Reverting before this lands in a release. Tracking the broader follow-up in #568. The original 413 reporter (#556) reopens for now while we route this through Codex CLI / file-upload paths instead.
1 parent e42af5e commit 9910e95

6 files changed

Lines changed: 13 additions & 482 deletions

File tree

‎app/core/clients/proxy.py‎

Lines changed: 5 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,6 @@
9595
"[codex-lb omitted historical tool output ({bytes} bytes) to fit upstream websocket budget]"
9696
)
9797
_RESPONSE_CREATE_IMAGE_OMISSION_NOTICE = "[codex-lb omitted historical inline image to fit upstream websocket budget]"
98-
_RESPONSE_CREATE_HISTORY_OMISSION_NOTICE = (
99-
"[codex-lb omitted {count} historical input items to fit upstream websocket budget]"
100-
)
10198
_UPSTREAM_TRACE_HEADER_ALLOWLIST = frozenset(
10299
{
103100
"accept",
@@ -1331,14 +1328,13 @@ def _prepare_websocket_response_create_payload(payload_dict: JsonObject) -> Json
13311328
(
13321329
"Slimmed response.create before upstream websocket connect request_id=%s "
13331330
"original_bytes=%s slimmed_bytes=%s historical_tool_outputs_slimmed=%s "
1334-
"historical_images_slimmed=%s historical_items_omitted=%s"
1331+
"historical_images_slimmed=%s"
13351332
),
13361333
get_request_id(),
13371334
payload_size,
13381335
len(slimmed_text.encode("utf-8")),
13391336
slim_summary["historical_tool_outputs_slimmed"],
13401337
slim_summary["historical_images_slimmed"],
1341-
slim_summary["historical_items_omitted"],
13421338
)
13431339
payload_text = slimmed_text
13441340
payload_size = len(payload_text.encode("utf-8"))
@@ -1379,6 +1375,7 @@ def _slim_response_create_payload_for_upstream(
13791375
*,
13801376
max_bytes: int,
13811377
) -> tuple[JsonObject, dict[str, int] | None]:
1378+
del max_bytes
13821379
input_value = payload.get("input")
13831380
if not isinstance(input_value, list) or not input_value:
13841381
return payload, None
@@ -1398,113 +1395,17 @@ def _slim_response_create_payload_for_upstream(
13981395
images_slimmed += item_images_slimmed
13991396
slimmed_historical.append(slimmed_item)
14001397

1401-
candidate_payload = dict(payload)
1402-
candidate_payload["input"] = slimmed_historical + recent
1403-
1404-
items_omitted = 0
1405-
if _serialized_json_size(candidate_payload) > max_bytes:
1406-
candidate_payload, items_omitted = _omit_historical_response_input_items_to_fit(
1407-
candidate_payload,
1408-
slimmed_historical=slimmed_historical,
1409-
recent=recent,
1410-
max_bytes=max_bytes,
1411-
)
1412-
1413-
if tool_outputs_slimmed == 0 and images_slimmed == 0 and items_omitted == 0:
1398+
if tool_outputs_slimmed == 0 and images_slimmed == 0:
14141399
return payload, None
14151400

1401+
candidate_payload = dict(payload)
1402+
candidate_payload["input"] = slimmed_historical + recent
14161403
return candidate_payload, {
14171404
"historical_tool_outputs_slimmed": tool_outputs_slimmed,
14181405
"historical_images_slimmed": images_slimmed,
1419-
"historical_items_omitted": items_omitted,
14201406
}
14211407

14221408

1423-
def _omit_historical_response_input_items_to_fit(
1424-
payload: JsonObject,
1425-
*,
1426-
slimmed_historical: list[JsonValue],
1427-
recent: list[JsonValue],
1428-
max_bytes: int,
1429-
) -> tuple[JsonObject, int]:
1430-
if not slimmed_historical:
1431-
return payload, 0
1432-
1433-
def candidate_with_omitted_count(prefix_omitted_count: int) -> tuple[JsonObject, int]:
1434-
remaining_historical, orphaned_outputs = _drop_orphaned_function_call_outputs(
1435-
slimmed_historical[prefix_omitted_count:],
1436-
recent,
1437-
)
1438-
omitted_count = prefix_omitted_count + orphaned_outputs
1439-
candidate_payload = dict(payload)
1440-
candidate_payload["input"] = [
1441-
_response_create_history_omission_notice_item(omitted_count),
1442-
*remaining_historical,
1443-
*recent,
1444-
]
1445-
return candidate_payload, omitted_count
1446-
1447-
low = 1
1448-
high = len(slimmed_historical)
1449-
best: tuple[JsonObject, int] | None = None
1450-
while low <= high:
1451-
prefix_omitted_count = (low + high) // 2
1452-
candidate_payload, omitted_count = candidate_with_omitted_count(prefix_omitted_count)
1453-
if _serialized_json_size(candidate_payload) <= max_bytes:
1454-
best = (candidate_payload, omitted_count)
1455-
high = prefix_omitted_count - 1
1456-
else:
1457-
low = prefix_omitted_count + 1
1458-
1459-
if best is not None:
1460-
return best
1461-
1462-
recent_only_payload = dict(payload)
1463-
recent_only_payload["input"] = list(recent)
1464-
if _serialized_json_size(recent_only_payload) <= max_bytes:
1465-
return recent_only_payload, len(slimmed_historical)
1466-
1467-
return candidate_with_omitted_count(len(slimmed_historical))
1468-
1469-
1470-
def _drop_orphaned_function_call_outputs(
1471-
historical: list[JsonValue],
1472-
recent: list[JsonValue],
1473-
) -> tuple[list[JsonValue], int]:
1474-
kept_call_ids = {
1475-
item.get("call_id")
1476-
for item in [*historical, *recent]
1477-
if is_json_mapping(item) and item.get("type") == "function_call" and isinstance(item.get("call_id"), str)
1478-
}
1479-
if not kept_call_ids:
1480-
filtered = [
1481-
item
1482-
for item in historical
1483-
if not (
1484-
is_json_mapping(item)
1485-
and item.get("type") == "function_call_output"
1486-
and isinstance(item.get("call_id"), str)
1487-
)
1488-
]
1489-
return filtered, len(historical) - len(filtered)
1490-
1491-
filtered = [
1492-
item
1493-
for item in historical
1494-
if not (
1495-
is_json_mapping(item)
1496-
and item.get("type") == "function_call_output"
1497-
and isinstance(item.get("call_id"), str)
1498-
and item.get("call_id") not in kept_call_ids
1499-
)
1500-
]
1501-
return filtered, len(historical) - len(filtered)
1502-
1503-
1504-
def _serialized_json_size(payload: JsonObject) -> int:
1505-
return len(json.dumps(payload, ensure_ascii=True, separators=(",", ":")).encode("utf-8"))
1506-
1507-
15081409
def _response_create_recent_suffix_start(input_items: list[JsonValue]) -> int:
15091410
last_user_index: int | None = None
15101411
for index, item in enumerate(input_items):
@@ -1590,18 +1491,6 @@ def _response_create_inline_image_notice_item() -> JsonObject:
15901491
return {"role": "user", "content": [_response_create_inline_image_notice_part()]}
15911492

15921493

1593-
def _response_create_history_omission_notice_item(count: int) -> JsonObject:
1594-
return {
1595-
"role": "assistant",
1596-
"content": [
1597-
{
1598-
"type": "output_text",
1599-
"text": _RESPONSE_CREATE_HISTORY_OMISSION_NOTICE.format(count=count),
1600-
}
1601-
],
1602-
}
1603-
1604-
16051494
def _is_inline_image_reference(value: JsonValue) -> bool:
16061495
return isinstance(value, str) and value.startswith("data:image/")
16071496

‎app/modules/proxy/service.py‎

Lines changed: 4 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from datetime import datetime, timezone
1515
from hashlib import sha256
1616
from ipaddress import ip_address
17+
from pathlib import Path
1718
from typing import Any, AsyncIterator, Literal, Mapping, NoReturn, TypeVar, cast, overload
1819
from urllib.parse import urlparse
1920
from uuid import uuid4
@@ -59,7 +60,7 @@
5960
connect_responses_websocket,
6061
filter_inbound_websocket_headers,
6162
)
62-
from app.core.config.settings import DEFAULT_HOME_DIR, Settings, get_settings
63+
from app.core.config.settings import Settings, get_settings
6364
from app.core.config.settings_cache import get_settings_cache
6465
from app.core.crypto import TokenEncryptor
6566
from app.core.errors import (
@@ -170,7 +171,7 @@
170171

171172
_UPSTREAM_RESPONSE_CREATE_MAX_BYTES = get_settings().upstream_response_create_max_bytes
172173
_UPSTREAM_RESPONSE_CREATE_WARN_BYTES = int(_UPSTREAM_RESPONSE_CREATE_MAX_BYTES * 0.8)
173-
_OVERSIZED_RESPONSE_CREATE_DUMP_DIR = DEFAULT_HOME_DIR / "debug" / "response-create-dumps"
174+
_OVERSIZED_RESPONSE_CREATE_DUMP_DIR = Path("/var/lib/codex-lb/debug/response-create-dumps")
174175
_OVERSIZED_RESPONSE_CREATE_LARGEST_ITEMS = 10
175176
_RESPONSE_CREATE_HISTORY_OMISSION_NOTICE = (
176177
"[codex-lb omitted {count} historical input items to fit upstream websocket budget]"
@@ -9114,108 +9115,13 @@ def _slim_response_create_payload_for_upstream(
91149115
candidate_payload = dict(payload)
91159116
candidate_payload["input"] = slimmed_historical + recent
91169117

9117-
items_omitted = 0
9118-
if _serialized_json_size(candidate_payload) > max_bytes:
9119-
candidate_payload, items_omitted = _omit_historical_response_input_items_to_fit(
9120-
candidate_payload,
9121-
slimmed_historical=slimmed_historical,
9122-
recent=recent,
9123-
max_bytes=max_bytes,
9124-
)
9125-
9126-
if tool_outputs_slimmed == 0 and images_slimmed == 0 and items_omitted == 0:
9118+
if tool_outputs_slimmed == 0 and images_slimmed == 0:
91279119
return payload, None
91289120

91299121
return candidate_payload, {
91309122
"historical_tool_outputs_slimmed": tool_outputs_slimmed,
91319123
"historical_images_slimmed": images_slimmed,
9132-
"historical_items_omitted": items_omitted,
9133-
}
9134-
9135-
9136-
def _omit_historical_response_input_items_to_fit(
9137-
payload: dict[str, JsonValue],
9138-
*,
9139-
slimmed_historical: list[JsonValue],
9140-
recent: list[JsonValue],
9141-
max_bytes: int,
9142-
) -> tuple[dict[str, JsonValue], int]:
9143-
if not slimmed_historical:
9144-
return payload, 0
9145-
9146-
def candidate_with_omitted_count(prefix_omitted_count: int) -> tuple[dict[str, JsonValue], int]:
9147-
remaining_historical, orphaned_outputs = _drop_orphaned_function_call_outputs(
9148-
slimmed_historical[prefix_omitted_count:],
9149-
recent,
9150-
)
9151-
omitted_count = prefix_omitted_count + orphaned_outputs
9152-
candidate_payload = dict(payload)
9153-
candidate_payload["input"] = [
9154-
_response_create_history_omission_notice_item(omitted_count),
9155-
*remaining_historical,
9156-
*recent,
9157-
]
9158-
return candidate_payload, omitted_count
9159-
9160-
low = 1
9161-
high = len(slimmed_historical)
9162-
best: tuple[dict[str, JsonValue], int] | None = None
9163-
while low <= high:
9164-
prefix_omitted_count = (low + high) // 2
9165-
candidate_payload, omitted_count = candidate_with_omitted_count(prefix_omitted_count)
9166-
if _serialized_json_size(candidate_payload) <= max_bytes:
9167-
best = (candidate_payload, omitted_count)
9168-
high = prefix_omitted_count - 1
9169-
else:
9170-
low = prefix_omitted_count + 1
9171-
9172-
if best is not None:
9173-
return best
9174-
9175-
recent_only_payload = dict(payload)
9176-
recent_only_payload["input"] = list(recent)
9177-
if _serialized_json_size(recent_only_payload) <= max_bytes:
9178-
return recent_only_payload, len(slimmed_historical)
9179-
9180-
return candidate_with_omitted_count(len(slimmed_historical))
9181-
9182-
9183-
def _drop_orphaned_function_call_outputs(
9184-
historical: list[JsonValue],
9185-
recent: list[JsonValue],
9186-
) -> tuple[list[JsonValue], int]:
9187-
kept_call_ids = {
9188-
item.get("call_id")
9189-
for item in [*historical, *recent]
9190-
if is_json_mapping(item) and item.get("type") == "function_call" and isinstance(item.get("call_id"), str)
91919124
}
9192-
if not kept_call_ids:
9193-
filtered = [
9194-
item
9195-
for item in historical
9196-
if not (
9197-
is_json_mapping(item)
9198-
and item.get("type") == "function_call_output"
9199-
and isinstance(item.get("call_id"), str)
9200-
)
9201-
]
9202-
return filtered, len(historical) - len(filtered)
9203-
9204-
filtered = [
9205-
item
9206-
for item in historical
9207-
if not (
9208-
is_json_mapping(item)
9209-
and item.get("type") == "function_call_output"
9210-
and isinstance(item.get("call_id"), str)
9211-
and item.get("call_id") not in kept_call_ids
9212-
)
9213-
]
9214-
return filtered, len(historical) - len(filtered)
9215-
9216-
9217-
def _serialized_json_size(payload: dict[str, JsonValue]) -> int:
9218-
return len(json.dumps(payload, ensure_ascii=True, separators=(",", ":")).encode("utf-8"))
92199125

92209126

92219127
def _response_create_recent_suffix_start(input_items: list[JsonValue]) -> int:

‎openspec/changes/slim-oversized-response-create-history/proposal.md‎

Lines changed: 0 additions & 29 deletions
This file was deleted.

‎openspec/changes/slim-oversized-response-create-history/specs/responses-api-compat/spec.md‎

Lines changed: 0 additions & 34 deletions
This file was deleted.

‎openspec/changes/slim-oversized-response-create-history/tasks.md‎

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)