Skip to content

Commit 175d7cd

Browse files
perf: compress each feature request once, one at a time
The guide and architecture each compressed their own body in parallel, nearly the same bytes twice, and architecture compressed again after dropping source.json. At preset 9 | EXTREME each compressor takes about 700 MB. Both features can send the body without source, so it is compressed once and carries why source is missing, which only architecture reads. Architecture's body with source is compressed only when there is source; when it does not fit, the shared bytes stand in. A run compresses once without source and at most twice with it, and never two at a time. The JSON check needs no ratio guess: the 256 MiB limit is 62 times the 4.3 MB one, so it already turns away whatever could only fit at better than 62:1, and no tighter cut-off holds for every request. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1d392af commit 175d7cd

2 files changed

Lines changed: 61 additions & 52 deletions

File tree

‎scripts/remote_features.py‎

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
99
What a request sends
1010
--------------------
11-
One JSON object of named files, compressed with xz. Both requests send the same files:
11+
One JSON object of named files, compressed with xz. Both requests send the same files, and the same
12+
bytes unless architecture's carries source:
1213
1314
- head.json and base.json: one side document per commit (see side_document). It holds what the
1415
service reads from that commit's analysis.json and call graph (static_analysis.pkl), each fact once:
@@ -35,8 +36,12 @@
3536
-----------
3637
- The service sets two limits on every request, and _encode checks both before anything is sent: the
3738
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.
39+
for the problem history), then the xz once against MAX_COMPRESSED_BYTES. The JSON limit is 62 times
40+
the compressed one, so the cheap check already turns away whatever could only fit at better than 62:1.
41+
These bundles compress about 10:1, but no tighter cut-off holds for every request.
42+
- xz at this preset takes about 700 MB and seconds per 10 MB, so a run compresses one body at a time and
43+
each at most once: the body without source, which both features send, and architecture's with source
44+
when there is source. When that one does not fit, architecture sends the shared one.
4045
- MAX_FILES: how many changed files have their contents sent; past it a feature reads diffs only.
4146
- MAX_FILE_BYTES: a larger file is sent neither in files.json nor in source.json.
4247
- MAX_SOURCE_BYTES: the most source.json holds; past it source is not sent.
@@ -634,12 +639,24 @@ def _encode(body: dict, max_json_bytes: int, what: str = "bundle") -> bytes:
634639
return data
635640

636641

637-
def _pack(ctx: dict, files: dict[str, str], omitted: dict[str, str]) -> bytes:
638-
"""A feature request: the named files, and why any optional one is missing."""
639-
body = {"version": 1, "repo": ctx["repo"], "pr": ctx["pr"], "files": files}
640-
if omitted:
641-
body["omitted"] = omitted
642-
return _encode(body, MAX_FEATURE_JSON_BYTES)
642+
def _requests(ctx: dict, inputs: dict[str, str], env: dict) -> tuple[bytes, bytes]:
643+
"""The review guide's request and architecture's, compressed one after the other. Both can send the
644+
body without source, so it is compressed once; it says why source is missing, which only architecture
645+
reads. Architecture's body with source is compressed only when there is source, and the shared one
646+
stands in when it does not fit. TooLarge when not even the shared one fits."""
647+
try:
648+
source, why = collect_source(env)
649+
except Exception as error:
650+
source, why = None, f"reading it failed: {type(error).__name__}"
651+
body = {"version": 1, "repo": ctx["repo"], "pr": ctx["pr"], "files": inputs}
652+
missing = (why or "the source was not collected") if source is None else "the repository is too large to send"
653+
shared = _encode({**body, "omitted": {"source.json": missing}}, MAX_FEATURE_JSON_BYTES)
654+
if source is None:
655+
return shared, shared
656+
try:
657+
return shared, _encode({**body, "files": {**inputs, "source.json": source}}, MAX_FEATURE_JSON_BYTES)
658+
except TooLarge:
659+
return shared, shared
643660

644661

645662
def _no_architecture(ctx: dict, status: str, reason: str) -> dict:
@@ -658,11 +675,10 @@ def _no_architecture(ctx: dict, status: str, reason: str) -> dict:
658675
}
659676

660677

661-
def run_review_guide(service_url: str, ctx: dict, inputs: dict, headers: dict, artifact: Path) -> None:
678+
def run_review_guide(service_url: str, data: bytes, headers: dict, artifact: Path) -> None:
662679
try:
663680
if "x-llm-provider" in headers and "x-llm-api-key" not in headers:
664681
raise Skip("the service cannot call this run's provider")
665-
data = _pack(ctx, inputs, {})
666682
answer = _post(f"{service_url.rstrip('/')}/v1/review-guide", data, headers)
667683
guide = answer.get("guide")
668684
if not isinstance(guide, dict):
@@ -672,25 +688,8 @@ def run_review_guide(service_url: str, ctx: dict, inputs: dict, headers: dict, a
672688
print(f"::notice::review-guide {'skipped' if isinstance(error, Skip) else 'failed'}: {error}")
673689

674690

675-
def run_architecture(service_url: str, ctx: dict, inputs: dict, headers: dict, env: dict) -> dict:
691+
def run_architecture(service_url: str, ctx: dict, data: bytes, headers: dict) -> dict:
676692
try:
677-
try:
678-
source, why = collect_source(env)
679-
except Exception as error:
680-
source, why = None, f"reading it failed: {type(error).__name__}"
681-
files, omitted = dict(inputs), {}
682-
if source is None:
683-
omitted["source.json"] = why or "the source was not collected"
684-
else:
685-
files["source.json"] = source
686-
try:
687-
data = _pack(ctx, files, omitted)
688-
except TooLarge:
689-
if "source.json" not in files:
690-
raise
691-
del files["source.json"]
692-
omitted["source.json"] = "the repository is too large to send"
693-
data = _pack(ctx, files, omitted)
694693
answer = _post(f"{service_url.rstrip('/')}/v1/architecture", data, headers)
695694
envelope = answer.get("envelope")
696695
if not isinstance(envelope, dict) or envelope.get("status") not in ("ok", "partial", "skipped", "failed"):
@@ -711,15 +710,18 @@ def main(env: dict | None = None) -> int:
711710
return 0
712711
try:
713712
headers = {**_bearer(env), **model_headers(env)}
714-
inputs = collect(env)
713+
guide_request, architecture_request = _requests(ctx, collect(env), env)
714+
except TooLarge as error:
715+
print(f"::notice::review-guide skipped: {error}")
716+
architecture = _no_architecture(ctx, "skipped", str(error))
715717
except Exception as error:
716718
reason = f"collecting inputs or credentials failed: {type(error).__name__}: {error}"
717719
print(f"::notice::review-guide failed: {reason}")
718720
architecture = _no_architecture(ctx, "failed", reason)
719721
else:
720722
with ThreadPoolExecutor(max_workers=2) as pool:
721-
guide = pool.submit(run_review_guide, service_url, ctx, inputs, headers, artifact)
722-
result = pool.submit(run_architecture, service_url, ctx, inputs, headers, env)
723+
guide = pool.submit(run_review_guide, service_url, guide_request, headers, artifact)
724+
result = pool.submit(run_architecture, service_url, ctx, architecture_request, headers)
723725
architecture = result.result()
724726
guide.result()
725727
path = artifact / "closed" / "architecture.json"

‎tests/test_remote_features.py‎

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
import tempfile
1010
import threading
1111
import unittest
12-
from importlib import metadata
13-
from contextlib import nullcontext
1412
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
13+
from importlib import metadata
1514
from pathlib import Path
1615
from types import SimpleNamespace
1716
from unittest import mock
@@ -529,19 +528,27 @@ def test_empty_url_disables_both(self):
529528
post.assert_not_called()
530529
self.assertFalse((Path(self.env["ARTIFACT_DIR"]) / "closed").exists())
531530

532-
def test_source_toggle_and_read_failure_only_omit_architecture_source(self):
531+
def run_counting(self, **patches):
532+
"""run_service with `patches` on remote_features, and how often it compressed."""
533+
compress = mock.Mock(wraps=lzma.compress)
534+
with mock.patch.object(lzma, "compress", compress), mock.patch.dict(remote_features.__dict__, patches):
535+
service = self.run_service()
536+
return {r["path"]: r["body"] for r in service.requests}, compress.call_count
537+
538+
def test_without_source_both_send_one_body_compressed_once(self):
533539
for failure in (False, True):
534540
self.env["CLOSED_FEATURES_SOURCE"] = "false"
535-
with (
536-
mock.patch.object(remote_features, "collect_source", side_effect=OSError())
537-
if failure
538-
else nullcontext()
539-
):
540-
service = self.run_service()
541-
for request in service.requests:
542-
body = json.loads(lzma.decompress(request["body"]))
543-
self.assertNotIn("source.json", body["files"])
544-
self.assertEqual("omitted" in body, request["path"] == "/v1/architecture")
541+
patches = {"collect_source": mock.Mock(side_effect=OSError())} if failure else {}
542+
bodies, compressions = self.run_counting(**patches)
543+
self.assertEqual((compressions, len(set(bodies.values()))), (1, 1))
544+
body = json.loads(lzma.decompress(bodies["/v1/architecture"]))
545+
self.assertNotIn("source.json", body["files"])
546+
self.assertIn("reading it failed" if failure else "turned off", body["omitted"]["source.json"])
547+
548+
def test_source_is_compressed_once_more_and_only_for_architecture(self):
549+
bodies, compressions = self.run_counting(collect_source=mock.Mock(return_value=("{}", None)))
550+
self.assertEqual(compressions, 2)
551+
self.assertNotEqual(bodies["/v1/architecture"], bodies["/v1/review-guide"])
545552

546553
def test_either_limit_omits_source_then_skips_if_required_inputs_too_large(self):
547554
with mock.patch.object(remote_features, "collect_source", return_value=(None, "off")):
@@ -552,13 +559,13 @@ def test_either_limit_omits_source_then_skips_if_required_inputs_too_large(self)
552559
("MAX_COMPRESSED_BYTES", 1000, os.urandom(2000).hex()),
553560
("MAX_FEATURE_JSON_BYTES", without_source + 1000, "x" * 100_000),
554561
):
555-
with (
556-
mock.patch.object(remote_features, limit, room),
557-
mock.patch.object(remote_features, "collect_source", return_value=(source, None)),
558-
):
559-
service = self.run_service()
560-
arch = next(r for r in service.requests if r["path"] == "/v1/architecture")
561-
body = json.loads(lzma.decompress(arch["body"]))
562+
bodies, compressions = self.run_counting(
563+
**{limit: room, "collect_source": mock.Mock(return_value=(source, None))}
564+
)
565+
# Architecture sends the guide's body, which already says why source is missing.
566+
self.assertEqual(bodies["/v1/architecture"], bodies["/v1/review-guide"])
567+
self.assertEqual(compressions, 2 if limit == "MAX_COMPRESSED_BYTES" else 1)
568+
body = json.loads(lzma.decompress(bodies["/v1/architecture"]))
562569
self.assertNotIn("source.json", body["files"])
563570
self.assertIn("too large", body["omitted"]["source.json"])
564571
with mock.patch.object(remote_features, limit, 1):

0 commit comments

Comments
 (0)