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+ )
98101_UPSTREAM_TRACE_HEADER_ALLOWLIST = frozenset (
99102 {
100103 "accept" ,
@@ -1328,13 +1331,14 @@ def _prepare_websocket_response_create_payload(payload_dict: JsonObject) -> Json
13281331 (
13291332 "Slimmed response.create before upstream websocket connect request_id=%s "
13301333 "original_bytes=%s slimmed_bytes=%s historical_tool_outputs_slimmed=%s "
1331- "historical_images_slimmed=%s"
1334+ "historical_images_slimmed=%s historical_items_omitted=%s "
13321335 ),
13331336 get_request_id (),
13341337 payload_size ,
13351338 len (slimmed_text .encode ("utf-8" )),
13361339 slim_summary ["historical_tool_outputs_slimmed" ],
13371340 slim_summary ["historical_images_slimmed" ],
1341+ slim_summary ["historical_items_omitted" ],
13381342 )
13391343 payload_text = slimmed_text
13401344 payload_size = len (payload_text .encode ("utf-8" ))
@@ -1375,7 +1379,6 @@ def _slim_response_create_payload_for_upstream(
13751379 * ,
13761380 max_bytes : int ,
13771381) -> tuple [JsonObject , dict [str , int ] | None ]:
1378- del max_bytes
13791382 input_value = payload .get ("input" )
13801383 if not isinstance (input_value , list ) or not input_value :
13811384 return payload , None
@@ -1395,17 +1398,113 @@ def _slim_response_create_payload_for_upstream(
13951398 images_slimmed += item_images_slimmed
13961399 slimmed_historical .append (slimmed_item )
13971400
1398- if tool_outputs_slimmed == 0 and images_slimmed == 0 :
1399- return payload , None
1400-
14011401 candidate_payload = dict (payload )
14021402 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 :
1414+ return payload , None
1415+
14031416 return candidate_payload , {
14041417 "historical_tool_outputs_slimmed" : tool_outputs_slimmed ,
14051418 "historical_images_slimmed" : images_slimmed ,
1419+ "historical_items_omitted" : items_omitted ,
14061420 }
14071421
14081422
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+
14091508def _response_create_recent_suffix_start (input_items : list [JsonValue ]) -> int :
14101509 last_user_index : int | None = None
14111510 for index , item in enumerate (input_items ):
@@ -1491,6 +1590,18 @@ def _response_create_inline_image_notice_item() -> JsonObject:
14911590 return {"role" : "user" , "content" : [_response_create_inline_image_notice_part ()]}
14921591
14931592
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+
14941605def _is_inline_image_reference (value : JsonValue ) -> bool :
14951606 return isinstance (value , str ) and value .startswith ("data:image/" )
14961607
0 commit comments