3333
3434Size limits
3535-----------
36- - MAX_BUNDLE_BYTES (4.3 MB compressed): the most a request may carry. Past it the request is skipped,
37- after first dropping source.json if architecture carried it.
36+ - The service sets two limits on every request, and _encode checks both before anything is sent: the
37+ JSON, measured first because that is cheap, against MAX_FEATURE_JSON_BYTES (MAX_BRIEFING_JSON_BYTES
38+ for the problem history), then the xz once against MAX_COMPRESSED_BYTES. Past either a feature request
39+ is skipped, after first dropping source.json if architecture carried it.
3840- MAX_FILES: how many changed files have their contents sent; past it a feature reads diffs only.
3941- MAX_FILE_BYTES: a larger file is sent neither in files.json nor in source.json.
4042- MAX_SOURCE_BYTES: the most source.json holds; past it source is not sent.
7375
7476from analysis_reader import Method , component_members , method_edges , method_spans
7577
76- # A Function URL takes 6 MB, measured after base64, and a compressed body goes through base64 on the
77- # way in: 4.5 MB of bundle arrives, and 5 MB is refused with a 413.
78- MAX_BUNDLE_BYTES = 4_300_000
78+ # The service's limits on a request, which move with it. A Function URL takes 6 MB, measured after
79+ # base64, and a compressed body goes through base64 on the way in: 4.5 MB of it arrives, and 5 MB is
80+ # refused with a 413. Decompressed, the service refuses a feature request over 256 MiB and a problem
81+ # history request over 32 MiB: MAX_BUNDLE_BYTES and MAX_BRIEFING_BYTES in licensing-aws
82+ # closed_functions/handler.py.
83+ MAX_COMPRESSED_BYTES = 4_300_000
84+ MAX_FEATURE_JSON_BYTES = 256 * 1024 * 1024
85+ MAX_BRIEFING_JSON_BYTES = 32 * 1024 * 1024
7986# Changed files whose contents are sent at both commits; past this a feature reads diffs only.
8087MAX_FILES = 300
8188# A file larger than this is sent neither as a changed file's contents nor as source.
@@ -106,6 +113,15 @@ class Skip(Exception):
106113 """A step that did not happen, for a reason worth one line in the log."""
107114
108115
116+ class TooLarge (Skip ):
117+ """A request over one of the service's limits. Over the compressed one, `fits` is how much JSON the
118+ compression ratio just measured would bring under it."""
119+
120+ def __init__ (self , message : str , fits : int | None = None ):
121+ super ().__init__ (message )
122+ self .fits = fits
123+
124+
109125# --- GitHub ---------------------------------------------------------------------------------------
110126
111127
@@ -578,21 +594,25 @@ def _post(url: str, body: bytes, headers: dict[str, str], timeout: int = 600) ->
578594 raise RuntimeError (reason ) from error
579595
580596
581- def _compress (body : dict ) -> bytes :
582- return lzma .compress (json .dumps (body ).encode (), preset = 9 | lzma .PRESET_EXTREME )
597+ def _encode (body : dict , max_json_bytes : int , what : str = "bundle" ) -> bytes :
598+ """The request as sent, xz-compressed JSON, or TooLarge when it is over either of the service's
599+ limits. The JSON is measured before compressing it, which is the slow part, and it is compressed once."""
600+ text = json .dumps (body ).encode ()
601+ if len (text ) > max_json_bytes :
602+ raise TooLarge (f"its { what } is { len (text ) / 1e6 :.0f} MB of JSON, over the service's limit" )
603+ data = lzma .compress (text , preset = 9 | lzma .PRESET_EXTREME )
604+ if len (data ) > MAX_COMPRESSED_BYTES :
605+ message = f"its { what } is { len (data ) / 1e6 :.1f} MB compressed, over the service's limit"
606+ raise TooLarge (message , fits = len (text ) * MAX_COMPRESSED_BYTES // len (data ))
607+ return data
583608
584609
585610def _pack (ctx : dict , files : dict [str , str ], omitted : dict [str , str ]) -> bytes :
586- """A request body : the named files, and why any optional one is missing."""
611+ """A feature request : the named files, and why any optional one is missing."""
587612 body = {"version" : 1 , "repo" : ctx ["repo" ], "pr" : ctx ["pr" ], "files" : files }
588613 if omitted :
589614 body ["omitted" ] = omitted
590- return _compress (body )
591-
592-
593- def _check_size (data : bytes , what : str = "bundle" ) -> None :
594- if len (data ) > MAX_BUNDLE_BYTES :
595- raise Skip (f"its { what } is { len (data ) / 1e6 :.1f} MB, over the service's limit" )
615+ return _encode (body , MAX_FEATURE_JSON_BYTES )
596616
597617
598618def _no_architecture (ctx : dict , status : str , reason : str ) -> dict :
@@ -616,7 +636,6 @@ def run_review_guide(service_url: str, ctx: dict, inputs: dict, headers: dict, a
616636 if "x-llm-provider" in headers and "x-llm-api-key" not in headers :
617637 raise Skip ("the service cannot call this run's provider" )
618638 data = _pack (ctx , inputs , {})
619- _check_size (data )
620639 answer = _post (f"{ service_url .rstrip ('/' )} /v1/review-guide" , data , headers )
621640 guide = answer .get ("guide" )
622641 if not isinstance (guide , dict ):
@@ -637,12 +656,14 @@ def run_architecture(service_url: str, ctx: dict, inputs: dict, headers: dict, e
637656 omitted ["source.json" ] = why or "the source was not collected"
638657 else :
639658 files ["source.json" ] = source
640- data = _pack (ctx , files , omitted )
641- if "source.json" in files and len (data ) > MAX_BUNDLE_BYTES :
659+ try :
660+ data = _pack (ctx , files , omitted )
661+ except TooLarge :
662+ if "source.json" not in files :
663+ raise
642664 del files ["source.json" ]
643665 omitted ["source.json" ] = "the repository is too large to send"
644666 data = _pack (ctx , files , omitted )
645- _check_size (data )
646667 answer = _post (f"{ service_url .rstrip ('/' )} /v1/architecture" , data , headers )
647668 envelope = answer .get ("envelope" )
648669 if not isinstance (envelope , dict ) or envelope .get ("status" ) not in ("ok" , "partial" , "skipped" , "failed" ):
0 commit comments