Skip to content

Commit ea7f084

Browse files
refactor: read the side documents' analyses through analysis_reader.read
analysis_reader now classifies a document's format once, where it is loaded, and hands back an Analysis instead of answering per accessor (is_v1, method_spans, component_members, method_edges are gone). _side reads each analysis.json with read(), so an unsupported or newer format fails there with the reader's message, and side_document works on the Analysis: its methods, members and edges, and its document for the component tree and relations. The format-equivalence test is named by role (current vs legacy); the fixture keeps its v1 file name. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 96ff064 commit ea7f084

2 files changed

Lines changed: 19 additions & 15 deletions

File tree

‎scripts/remote_features.py‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
service reads from that commit's analysis.json and call graph (static_analysis.pkl), each fact once:
1616
the methods and graph nodes as one table per file, the calls, the component tree, and the few
1717
analysis edges that are no call. Analyses are read through analysis_reader, which takes either of
18-
Core's analysis.json formats, legacy or "v1".
18+
Core's analysis.json formats, current or legacy.
1919
- metadata.json: the review artifact's metadata, with the merge base as the base.
2020
- pr.json: the pull request's title, body, head commit and commit subjects.
2121
- pr-files.json: the files changed between the two analysed commits, as GitHub's compare API lists them.
@@ -78,7 +78,7 @@
7878
from urllib.parse import quote, urlparse
7979
from urllib.request import Request, urlopen
8080

81-
from analysis_reader import Method, component_members, method_edges, method_spans
81+
from analysis_reader import Analysis, Method, read
8282

8383
# The service's limits on a request, which move with it. A Function URL takes 6 MB, measured after
8484
# base64, and a compressed body goes through base64 on the way in: 4.5 MB of it arrives, and 5 MB is
@@ -298,7 +298,7 @@ def member(self, key: str) -> int | str:
298298
return key if row is None else row.number
299299

300300

301-
def side_document(analysis: dict, graph: dict) -> dict:
301+
def side_document(analysis: Analysis, graph: dict) -> dict:
302302
"""One commit's analysis and call graph as the service reads them, each fact once. The service's
303303
bundle.expand_side turns it back into both, in every field a feature reads.
304304
@@ -313,7 +313,7 @@ def side_document(analysis: dict, graph: dict) -> dict:
313313
(a key where the analysis lacks the method), or null for a parent holding exactly its children's.
314314
- extra_edges: the analysis's edges that are no call. The rest repeat the calls: on crate one of
315315
149,316 is not a call."""
316-
table = _Table(method_spans(analysis))
316+
table = _Table(analysis.methods)
317317
spans = []
318318
for name, node in (graph.get("nodes") or {}).items():
319319
row = table.add_node(name, node)
@@ -322,14 +322,14 @@ def side_document(analysis: dict, graph: dict) -> dict:
322322
files = table.number_rows()
323323
calls, unplaced = _calls(graph.get("edges") or [], table)
324324
called = {(caller, callee) for caller, callees in calls for callee, *_ in callees}
325-
edges = {(table.member(source), table.member(target)) for source, target in method_edges(analysis)}
325+
edges = {(table.member(source), table.member(target)) for source, target in analysis.edges}
326326
return {
327327
"version": 1,
328328
"files": files,
329329
"unplaced": unplaced,
330330
"graph_spans": [[row.number, start, end] for row, start, end in spans],
331331
"components": _components(analysis, table),
332-
"relations": _relations(analysis.get("components_relations")),
332+
"relations": _relations(analysis.document.get("components_relations")),
333333
"calls": calls,
334334
"extra_edges": sorted(edges - called, key=repr),
335335
}
@@ -350,15 +350,15 @@ def _calls(edges: list[dict], table: _Table) -> tuple[list, list[str]]:
350350
return [[caller, by_caller[caller]] for caller in sorted(by_caller)], unplaced
351351

352352

353-
def _components(analysis: dict, table: _Table) -> list:
353+
def _components(analysis: Analysis, table: _Table) -> list:
354354
"""The component tree in pre-order, each `[id, name, description, parent, members, relations]`.
355355
A parent's members are null when they are exactly its children's, which the service rebuilds."""
356-
flat = _flatten(analysis.get("components"))
356+
flat = _flatten(analysis.document.get("components"))
357357
children: list[list[int]] = [[] for _ in flat]
358358
for position, (_, parent) in enumerate(flat):
359359
if parent >= 0:
360360
children[parent].append(position)
361-
by_file = component_members(analysis)
361+
by_file = analysis.members
362362
members = [
363363
[table.member(f"{path}|{name}") for path, names in by_file[component["component_id"]].items() for name in names]
364364
for component, _ in flat
@@ -437,7 +437,7 @@ def collect(env: dict) -> dict:
437437

438438
def _side(analysis_path: Path) -> str:
439439
"""The side document of the commit whose analysis this is; its call graph lies beside it."""
440-
analysis = json.loads(analysis_path.read_text())
440+
analysis = read(json.loads(analysis_path.read_text()))
441441
graph = call_graph(analysis_path.parent / "static_analysis.pkl")
442442
return json.dumps(side_document(analysis, graph), separators=(",", ":"))
443443

‎tests/test_remote_features.py‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
from unittest import mock
1515

1616
sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "scripts"))
17+
from analysis_reader import read # noqa: E402
18+
1719
_SCRIPT = Path(__file__).resolve().parent.parent / "scripts" / "remote_features.py"
1820
_SPEC = importlib.util.spec_from_file_location("remote_features", _SCRIPT)
1921
remote_features = importlib.util.module_from_spec(_SPEC)
@@ -267,7 +269,7 @@ class SideDocumentTests(unittest.TestCase):
267269
}
268270

269271
def test_each_fact_once_and_only_what_the_service_reads(self):
270-
doc = json.loads(json.dumps(remote_features.side_document(self.ANALYSIS, self.GRAPH)))
272+
doc = json.loads(json.dumps(remote_features.side_document(read(self.ANALYSIS), self.GRAPH)))
271273
self.assertEqual(doc, self.DOCUMENT)
272274

273275
def test_a_parent_is_left_out_only_when_its_childrens_members_rebuild_it(self):
@@ -287,19 +289,21 @@ def component(cid, names, children=()):
287289
component("P", "xy", [component("C1", "x"), component("C2", "y")]),
288290
],
289291
}
290-
members = [c[4] for c in remote_features.side_document(analysis, {})["components"]]
292+
members = [c[4] for c in remote_features.side_document(read(analysis), {})["components"]]
291293
self.assertEqual(members, [[0, 1], [0, 1, 2], [0], [1], None, [0], [1]])
292294

293-
def test_a_v1_analysis_gives_the_same_document_as_its_legacy_one(self):
295+
def test_a_current_analysis_gives_the_same_document_as_its_legacy_one(self):
294296
# One analysis written by Core's two writers: legacy at main 53f0bb8e, "v1" at 3adb6969
295297
# (feat/analysis-json-lean-index), both after that branch's align_relation_edges.
296298
fixtures = Path(__file__).parent / "fixtures" / "side_document"
297299
graph = {
298300
"nodes": {**self.GRAPH["nodes"], "pkg.c.load": {"file": "pkg/c.py", "start": 1, "end": 3, "cb": False}},
299301
"edges": [*self.GRAPH["edges"], {"s": "pkg.c.load", "d": "pkg.b.save", "lines": [2]}],
300302
}
301-
legacy, v1 = (json.loads((fixtures / f"{name}.analysis.json").read_text()) for name in ("legacy", "v1"))
302-
doc = remote_features.side_document(v1, graph)
303+
legacy, current = (
304+
read(json.loads((fixtures / f"{name}.analysis.json").read_text())) for name in ("legacy", "v1")
305+
)
306+
doc = remote_features.side_document(current, graph)
303307
self.assertEqual(doc, remote_features.side_document(legacy, graph))
304308
# Of the edges, the calls are left to the graph: load -> save is one.
305309
self.assertEqual(doc["extra_edges"], [(0, 4), (1, 2), (3, 4)])

0 commit comments

Comments
 (0)