From 209dbf38d8c02b64caddb048e5c10f749db240e7 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Tue, 24 Nov 2026 06:11:05 +0100 Subject: [PATCH] refactor: move report inputs out of the resolver The refs a report anchors, the repositories it reads and the revisions it compares move to profiling.reportinputs. The resolver keeps the per-profile _Resolver and build_report. --- scripts/profiling/reportinputs.py | 302 ++++++++++++++++++++++++++++++ scripts/profiling/resolver.py | 298 ++--------------------------- tests/sources.py | 4 +- tests/test_launcher_evidence.py | 2 +- tests/test_profile_sync.py | 3 +- 5 files changed, 318 insertions(+), 291 deletions(-) create mode 100644 scripts/profiling/reportinputs.py diff --git a/scripts/profiling/reportinputs.py b/scripts/profiling/reportinputs.py new file mode 100644 index 000000000..5dc9b6967 --- /dev/null +++ b/scripts/profiling/reportinputs.py @@ -0,0 +1,302 @@ +"""What a profile report starts from: the refs it anchors, the repositories it reads, the revisions it compares.""" + +from __future__ import annotations + +import posixpath +from collections.abc import Callable, Iterable +from dataclasses import replace + +from core import forgehttp, upstream, upstreamtree +from model.profilerepos import declared_repositories +from model.sourceref import RefPart, ref_role, resolve_project_word, split_source_ref +from profiling.citations import ( + Citation, + anchor_tokens, + collect_citations, + entry_hashes, + is_external_citation, + strip_repo_word, +) +from profiling.reports import ProfileReport, SkipKind +from profiling.repos import ( + RepoView, + build_revision, + detect_pinned_tag, + moved_repositories, + reads_own_revision, + resolve_through_gitlinks, + select_repo, + select_views, + shipped_build_holders, + tag_pinned_views, +) + + +def _resolve_project_words( + refs: list, tree_order: Callable[[], Iterable[list[str] | None]], + link_order: Callable[[], Iterable[Iterable[str]]] = lambda: (), +) -> list: + """Point `project file` parts at the declared tree that vendors them. + + tree_order() yields the trees to search, pin before HEAD, and + link_order() the submodule paths of the declared trees at their pins. + Each is only called for a part that needs it, since reading a tree costs + a request. Parts no tree carries stay external. + """ + resolved: dict[str, str | None] = {} + out = [] + for display, ref, tokens, hashes, parts, citation in refs: + rewritten = [] + for part in parts: + if is_external_citation(part.path): + if part.path not in resolved: + resolved[part.path] = resolve_project_word( + part.path, tree_order() + ) or resolve_through_gitlinks(part.path, link_order()) + if resolved[part.path]: + part = replace(part, path=resolved[part.path]) + rewritten.append(part) + out.append((display, ref, tokens, hashes, rewritten, citation)) + return out + + +def _path_roles(refs: list) -> dict[str, set[str]]: + """The roles each cited path is written under across the profile.""" + roles: dict[str, set[str]] = {} + for *_, parts, _citation in refs: + for part in parts: + if part.role: + roles.setdefault(part.path, set()).add(part.role) + return roles + + +def _check_build_refs( + profile: dict, + deferred: list[tuple[Citation, dict]], + views: list[RepoView], + fetch: Callable[[upstream.Repo, str, str], list[str] | None], +) -> list[dict]: + """Each deferred config_reads ref read at the build of its platforms. + + The file must exist at that revision and every cited line lie inside + it. The entry declares no value to anchor on, so this is the check a + ref past the end of its file fails (BUILD_GONE), not a content match. + """ + results: list[dict] = [] + for citation, read in deferred: + for platform in read.get("platforms") or []: + # A closed build is named by its binary: an address ref is + # skipped below, a source path cannot exist at a sha256. + revision = (str(read.get("revision") or read.get("binary_sha256") or "") + or build_revision(profile, str(platform))) + row = {"platform": str(platform), "revision": revision or "", + "ref": citation.ref, "field": citation.field} + if revision is None: + results.append({**row, "status": "BUILD_UNREAD", + "detail": "no pin_build revision for this platform"}) + continue + problems: list[str] = [] + for part in split_source_ref(citation.ref): + role, path = ref_role(part.path) + if " " in path: + # A project word in front (`ROCKNIX patches/x.patch:4`): + # a file of the platform's tree, read with the record. + continue + lines = next( + (found for view in views + if (not role or view.field == role) + and (found := fetch(view.repo, revision, path)) is not None), + None, + ) + if lines is None: + problems.append(f"{part.path} absent at {revision[:12]}") + elif (part.end or part.start or 0) > len(lines): + problems.append(f"{part.raw} past the end ({len(lines)} lines)") + results.append({ + **row, + "status": "BUILD_GONE" if problems else "BUILD_OK", + "detail": "; ".join(problems), + }) + return results + + +def _gather_refs( + profile: dict, text: str | None +) -> tuple[list, list[tuple[Citation, dict]]]: + """The citations to anchor, and the refs read at a shipped build instead. + + One citation surface for the whole document. A prose citation carries + no declared value, so it anchors on content alone: the nudge and + relocation heuristics stay off rather than guess from a profile-wide + token pool. + """ + known = frozenset( + repo.name.lower() + for _, _, url in declared_repositories(profile) + if (repo := upstream.parse_repo(url)) is not None + ) + modes = {mode for _, mode, _ in declared_repositories(profile) if mode} + refs = [] + at_build = shipped_build_holders(profile) + deferred: list[tuple[Citation, dict]] = [] + for citation in collect_citations(profile, text): + if citation.kind == "ref" and id(citation.holder) in at_build: + # Written for the build a platform ships: read at pin_build. + deferred.append((citation, at_build[id(citation.holder)])) + continue + if citation.kind == "ref": + entry = citation.entry or {} + display = str(entry.get("name") or "") or citation.field + if citation.label: + display += f" [{citation.label}]" + # The repository a part says its lines are in (`upstream + # src/a.c:5`) stays on that part: the same path can be cited + # under both roles in one profile. + # An entry of one build mode reads that build's repository when + # the profile keys its source by mode. + mode = str(citation.label or entry.get("mode") or "") + mode = mode if mode in modes else "" + parts = [ + RefPart( + strip_repo_word(p.path, known), p.start, p.end, p.raw, + ref_role(p.path)[0] or mode, + ) + for p in split_source_ref(citation.ref) + ] + refs.append(( + display, citation.ref, anchor_tokens(entry), + entry_hashes(entry), parts, citation, + )) + else: + refs.append(( + citation.field, citation.ref, [], [], + list(citation.parts), citation, + )) + return refs, deferred + + +def _cited_dirs(refs: list) -> set[str]: + """Directories the profile already cites, deepest first, to settle a bare + filename that several trees of one repository carry.""" + cited_dirs: set[str] = set() + for _, _, _, _, parts, _ in refs: + for part in parts: + directory = posixpath.dirname(part.path) + while directory: + cited_dirs.add(directory) + directory = posixpath.dirname(directory) + return cited_dirs + + +def _unanswered_skip(profile: dict) -> str: + """Why a profile no declared repository answers for is skipped.""" + # A declared pin whose repository or branch does not answer is not a + # profile without a pin: pyxel's kitao/pyxel answers 404, and the + # message sent the reader looking for a missing source_commit. + return ( + "no declared repository answers (moved, removed, or a branch the " + "forge does not know)" + if profile.get("source_commit") or profile.get("upstream_commit") + else "no source_commit and no resolvable profiled_date " + "on any declared repository" + ) + + +def _readable_views( + profile: dict, report: ProfileReport, cache_dir: str, offline: bool +) -> list[RepoView] | None: + """The repositories to read, None once the report says why it is skipped.""" + report.unread = [ + url + for _, _, url in declared_repositories(profile) + if upstream.parse_repo(url) is None + ] + if select_repo(profile) is None: + declared = str(profile.get("source") or profile.get("upstream") or "") + report.skip( + SkipKind.UNSUPPORTED_HOST, f"unsupported host: {declared or 'none declared'}" + ) + return None + + stated_gone = str(profile.get("upstream_gone") or "").strip() + + try: + views = select_views(profile, cache_dir, offline) + except forgehttp.RateLimitError: + raise + except forgehttp.UpstreamError: + # citron's dead domain now resolves to an unrelated host that fails + # TLS: for a forge the profile declares gone, any refusal agrees. + if stated_gone: + # The profile says the forge is gone and the forge agrees. That is + # a recorded fact, not an open problem: the refs describe the last + # revision anyone can reach, and nothing further is possible. + report.skip(SkipKind.GONE_DECLARED, f"upstream gone (declared): {stated_gone}") + return None + raise + if stated_gone and any(view.field != "source_mirror" for view in views): + # Declared dead, yet it answered. A profile that keeps saying so stops + # being read, so the contradiction is the finding. + report.skip( + SkipKind.GONE_CONTRADICTED, + f"upstream_gone is declared but the forge answers: {stated_gone}", + ) + return None + if not views and stated_gone: + report.skip(SkipKind.GONE_DECLARED, f"upstream gone (declared): {stated_gone}") + return None + if not views: + report.skip(SkipKind.UNANSWERED, _unanswered_skip(profile)) + return None + return views + + +def _record_revisions( + profile: dict, report: ProfileReport, views: list[RepoView], + cache_dir: str, offline: bool, +) -> dict[tuple[str, str], str]: + """Put the revisions under comparison on the report, and return the views + read at their release tag.""" + primary = views[0] + report.repo, report.host = primary.repo.slug, primary.repo.host + report.repos = [v.repo.slug for v in views] + report.pin, report.pin_origin = primary.pin, primary.origin + report.head = primary.head + for view in views[1:]: + if view.field == "upstream": + report.upstream_pin = view.pin + report.upstream_head = view.head + report.upstream_origin = view.origin + report.moved_repos = moved_repositories(views, cache_dir, offline) + # A profile can hold its pin on purpose, documenting a build that is that + # revision. Comparing it to HEAD says nothing and recaling would repoint + # its refs at code the build never had, so it is judged like a frozen tag: + # against itself. + frozen = str(profile.get("pin_frozen") or "").strip() + report.pin_frozen = bool(frozen) + report.pinned_tag = frozen or detect_pinned_tag( + profile, views, cache_dir, offline + ) + # A repository pinned on its release tag is read at that tag, whichever + # role it plays: comparing mame0174 refs with mamedev HEAD says nothing. + tagged = tag_pinned_views(profile, views, cache_dir, offline) + report.upstream_tag = next( + ( + tagged[(view.repo.host, view.repo.slug)] + for view in views + if view.field == "upstream" and (view.repo.host, view.repo.slug) in tagged + ), + None, + ) + + if not report.pinned_tag: + # A tag or a held pin is off the tracked branch by design. + report.orphaned = [ + f"{view.repo.slug}: pin {view.pin[:8]} is no ancestor of {view.head[:8]}" + for view in views + if not reads_own_revision(view, tagged) + and upstreamtree.compare( + view.repo, view.pin, view.head, cache_dir, offline + ).diverged + ] + return tagged diff --git a/scripts/profiling/resolver.py b/scripts/profiling/resolver.py index e3c3de598..3e20759dc 100644 --- a/scripts/profiling/resolver.py +++ b/scripts/profiling/resolver.py @@ -7,14 +7,14 @@ import itertools import posixpath import re import sys -from collections.abc import Callable, Iterable, Iterator, Sequence +from collections.abc import Iterable, Iterator, Sequence from dataclasses import dataclass, field, replace from core import upstream from core import forgehttp from core import upstreamtree from core.upstreamtree import CompareResult -from model.sourceref import RefPart, ref_role, resolve_project_word, split_source_ref +from model.sourceref import RefPart from profiling.anchoring import ( BLANK_LOOKAHEAD, PartResult, @@ -31,301 +31,25 @@ from profiling.anchoring import ( ) from profiling.attribution import answers, field_role, owner_order from profiling.citations import ( - Citation, - anchor_tokens, cites_another_version, - collect_citations, - entry_hashes, - is_external_citation, narrow_by_cited, - strip_repo_word, symlink_target, ) from profiling.reports import CitedSource, EntryReport, ProfileReport, SkipKind -from model.profilerepos import declared_repositories from profiling.repos import ( PIN, RepoView, reads_own_revision, - build_revision, - detect_pinned_tag, - moved_repositories, - resolve_through_gitlinks, - select_repo, - select_views, - shipped_build_holders, - tag_pinned_views, ) - - -def _resolve_project_words( - refs: list, tree_order: Callable[[], Iterable[list[str] | None]], - link_order: Callable[[], Iterable[Iterable[str]]] = lambda: (), -) -> list: - """Point `project file` parts at the declared tree that vendors them. - - tree_order() yields the trees to search, pin before HEAD, and - link_order() the submodule paths of the declared trees at their pins. - Each is only called for a part that needs it, since reading a tree costs - a request. Parts no tree carries stay external. - """ - resolved: dict[str, str | None] = {} - out = [] - for display, ref, tokens, hashes, parts, citation in refs: - rewritten = [] - for part in parts: - if is_external_citation(part.path): - if part.path not in resolved: - resolved[part.path] = resolve_project_word( - part.path, tree_order() - ) or resolve_through_gitlinks(part.path, link_order()) - if resolved[part.path]: - part = replace(part, path=resolved[part.path]) - rewritten.append(part) - out.append((display, ref, tokens, hashes, rewritten, citation)) - return out - - -def _path_roles(refs: list) -> dict[str, set[str]]: - """The roles each cited path is written under across the profile.""" - roles: dict[str, set[str]] = {} - for *_, parts, _citation in refs: - for part in parts: - if part.role: - roles.setdefault(part.path, set()).add(part.role) - return roles - - -def _check_build_refs( - profile: dict, - deferred: list[tuple[Citation, dict]], - views: list[RepoView], - fetch: Callable[[upstream.Repo, str, str], list[str] | None], -) -> list[dict]: - """Each deferred config_reads ref read at the build of its platforms. - - The file must exist at that revision and every cited line lie inside - it. The entry declares no value to anchor on, so this is the check a - ref past the end of its file fails (BUILD_GONE), not a content match. - """ - results: list[dict] = [] - for citation, read in deferred: - for platform in read.get("platforms") or []: - # A closed build is named by its binary: an address ref is - # skipped below, a source path cannot exist at a sha256. - revision = (str(read.get("revision") or read.get("binary_sha256") or "") - or build_revision(profile, str(platform))) - row = {"platform": str(platform), "revision": revision or "", - "ref": citation.ref, "field": citation.field} - if revision is None: - results.append({**row, "status": "BUILD_UNREAD", - "detail": "no pin_build revision for this platform"}) - continue - problems: list[str] = [] - for part in split_source_ref(citation.ref): - role, path = ref_role(part.path) - if " " in path: - # A project word in front (`ROCKNIX patches/x.patch:4`): - # a file of the platform's tree, read with the record. - continue - lines = next( - (found for view in views - if (not role or view.field == role) - and (found := fetch(view.repo, revision, path)) is not None), - None, - ) - if lines is None: - problems.append(f"{part.path} absent at {revision[:12]}") - elif (part.end or part.start or 0) > len(lines): - problems.append(f"{part.raw} past the end ({len(lines)} lines)") - results.append({ - **row, - "status": "BUILD_GONE" if problems else "BUILD_OK", - "detail": "; ".join(problems), - }) - return results - - -def _gather_refs( - profile: dict, text: str | None -) -> tuple[list, list[tuple[Citation, dict]]]: - """The citations to anchor, and the refs read at a shipped build instead. - - One citation surface for the whole document. A prose citation carries - no declared value, so it anchors on content alone: the nudge and - relocation heuristics stay off rather than guess from a profile-wide - token pool. - """ - known = frozenset( - repo.name.lower() - for _, _, url in declared_repositories(profile) - if (repo := upstream.parse_repo(url)) is not None - ) - modes = {mode for _, mode, _ in declared_repositories(profile) if mode} - refs = [] - at_build = shipped_build_holders(profile) - deferred: list[tuple[Citation, dict]] = [] - for citation in collect_citations(profile, text): - if citation.kind == "ref" and id(citation.holder) in at_build: - # Written for the build a platform ships: read at pin_build. - deferred.append((citation, at_build[id(citation.holder)])) - continue - if citation.kind == "ref": - entry = citation.entry or {} - display = str(entry.get("name") or "") or citation.field - if citation.label: - display += f" [{citation.label}]" - # The repository a part says its lines are in (`upstream - # src/a.c:5`) stays on that part: the same path can be cited - # under both roles in one profile. - # An entry of one build mode reads that build's repository when - # the profile keys its source by mode. - mode = str(citation.label or entry.get("mode") or "") - mode = mode if mode in modes else "" - parts = [ - RefPart( - strip_repo_word(p.path, known), p.start, p.end, p.raw, - ref_role(p.path)[0] or mode, - ) - for p in split_source_ref(citation.ref) - ] - refs.append(( - display, citation.ref, anchor_tokens(entry), - entry_hashes(entry), parts, citation, - )) - else: - refs.append(( - citation.field, citation.ref, [], [], - list(citation.parts), citation, - )) - return refs, deferred - - -def _cited_dirs(refs: list) -> set[str]: - """Directories the profile already cites, deepest first, to settle a bare - filename that several trees of one repository carry.""" - cited_dirs: set[str] = set() - for _, _, _, _, parts, _ in refs: - for part in parts: - directory = posixpath.dirname(part.path) - while directory: - cited_dirs.add(directory) - directory = posixpath.dirname(directory) - return cited_dirs - - -def _unanswered_skip(profile: dict) -> str: - """Why a profile no declared repository answers for is skipped.""" - # A declared pin whose repository or branch does not answer is not a - # profile without a pin: pyxel's kitao/pyxel answers 404, and the - # message sent the reader looking for a missing source_commit. - return ( - "no declared repository answers (moved, removed, or a branch the " - "forge does not know)" - if profile.get("source_commit") or profile.get("upstream_commit") - else "no source_commit and no resolvable profiled_date " - "on any declared repository" - ) - - -def _readable_views( - profile: dict, report: ProfileReport, cache_dir: str, offline: bool -) -> list[RepoView] | None: - """The repositories to read, None once the report says why it is skipped.""" - report.unread = [ - url - for _, _, url in declared_repositories(profile) - if upstream.parse_repo(url) is None - ] - if select_repo(profile) is None: - declared = str(profile.get("source") or profile.get("upstream") or "") - report.skip( - SkipKind.UNSUPPORTED_HOST, f"unsupported host: {declared or 'none declared'}" - ) - return None - - stated_gone = str(profile.get("upstream_gone") or "").strip() - - try: - views = select_views(profile, cache_dir, offline) - except forgehttp.RateLimitError: - raise - except forgehttp.UpstreamError: - # citron's dead domain now resolves to an unrelated host that fails - # TLS: for a forge the profile declares gone, any refusal agrees. - if stated_gone: - # The profile says the forge is gone and the forge agrees. That is - # a recorded fact, not an open problem: the refs describe the last - # revision anyone can reach, and nothing further is possible. - report.skip(SkipKind.GONE_DECLARED, f"upstream gone (declared): {stated_gone}") - return None - raise - if stated_gone and any(view.field != "source_mirror" for view in views): - # Declared dead, yet it answered. A profile that keeps saying so stops - # being read, so the contradiction is the finding. - report.skip( - SkipKind.GONE_CONTRADICTED, - f"upstream_gone is declared but the forge answers: {stated_gone}", - ) - return None - if not views and stated_gone: - report.skip(SkipKind.GONE_DECLARED, f"upstream gone (declared): {stated_gone}") - return None - if not views: - report.skip(SkipKind.UNANSWERED, _unanswered_skip(profile)) - return None - return views - - -def _record_revisions( - profile: dict, report: ProfileReport, views: list[RepoView], - cache_dir: str, offline: bool, -) -> dict[tuple[str, str], str]: - """Put the revisions under comparison on the report, and return the views - read at their release tag.""" - primary = views[0] - report.repo, report.host = primary.repo.slug, primary.repo.host - report.repos = [v.repo.slug for v in views] - report.pin, report.pin_origin = primary.pin, primary.origin - report.head = primary.head - for view in views[1:]: - if view.field == "upstream": - report.upstream_pin = view.pin - report.upstream_head = view.head - report.upstream_origin = view.origin - report.moved_repos = moved_repositories(views, cache_dir, offline) - # A profile can hold its pin on purpose, documenting a build that is that - # revision. Comparing it to HEAD says nothing and recaling would repoint - # its refs at code the build never had, so it is judged like a frozen tag: - # against itself. - frozen = str(profile.get("pin_frozen") or "").strip() - report.pin_frozen = bool(frozen) - report.pinned_tag = frozen or detect_pinned_tag( - profile, views, cache_dir, offline - ) - # A repository pinned on its release tag is read at that tag, whichever - # role it plays: comparing mame0174 refs with mamedev HEAD says nothing. - tagged = tag_pinned_views(profile, views, cache_dir, offline) - report.upstream_tag = next( - ( - tagged[(view.repo.host, view.repo.slug)] - for view in views - if view.field == "upstream" and (view.repo.host, view.repo.slug) in tagged - ), - None, - ) - - if not report.pinned_tag: - # A tag or a held pin is off the tracked branch by design. - report.orphaned = [ - f"{view.repo.slug}: pin {view.pin[:8]} is no ancestor of {view.head[:8]}" - for view in views - if not reads_own_revision(view, tagged) - and upstreamtree.compare( - view.repo, view.pin, view.head, cache_dir, offline - ).diverged - ] - return tagged +from profiling.reportinputs import ( + _check_build_refs, + _cited_dirs, + _gather_refs, + _path_roles, + _readable_views, + _record_revisions, + _resolve_project_words, +) def _line_score(lines: list[str], start: int | None, tokens: tuple) -> int: diff --git a/tests/sources.py b/tests/sources.py index a48bd652e..5000a7ba7 100644 --- a/tests/sources.py +++ b/tests/sources.py @@ -179,8 +179,8 @@ COMMANDS = { ), "profile_sync.py": ( "profiling/citations.py", "profiling/anchoring.py", "profiling/repos.py", - "profiling/resolver.py", "profiling/reports.py", "profiling/versions.py", - "profiling/versionbasis.py", "profiling/detection.py", "profiling/triage.py", + "profiling/resolver.py", "profiling/reportinputs.py", "profiling/reports.py", + "profiling/versions.py", "profiling/versionbasis.py", "profiling/detection.py", "profiling/triage.py", "profiling/yamledit.py", "profiling/proseedit.py", "profiling/rebase.py", "profiling/bump.py", "profiling/realign.py", "profiling/run.py", "profiling/worktree.py", "profiling/quota.py", "profiling/sourcepaths.py", "profiling/realignpass.py", diff --git a/tests/test_launcher_evidence.py b/tests/test_launcher_evidence.py index 2387a0382..15e31d514 100644 --- a/tests/test_launcher_evidence.py +++ b/tests/test_launcher_evidence.py @@ -27,7 +27,7 @@ from resolve.launcherconfig import ( ) from resolve.launchers import handed_directories from resolve.launcherplaces import launcher_places -from profiling.resolver import _check_build_refs +from profiling.reportinputs import _check_build_refs from profiling.repos import shipped_build_holders from scraper.retrobat_launchers import _choices_apart from scraper.shell_launchers import LauncherPatternMissing, Script, config_placement diff --git a/tests/test_profile_sync.py b/tests/test_profile_sync.py index f4cf9c923..6747e3c6b 100644 --- a/tests/test_profile_sync.py +++ b/tests/test_profile_sync.py @@ -28,6 +28,7 @@ from profiling import rebase from profiling import reports as profiling_reports from profiling import repos as profiling_repos from profiling import resolver +from profiling import reportinputs from profiling import run as profiling_run from profiling import sourcepaths as profiling_sourcepaths from profiling import worktree as profiling_worktree @@ -6197,7 +6198,7 @@ class TestAnUnansweringRepository(unittest.TestCase): def _report(self, profile): from unittest import mock - with mock.patch.object(resolver, "select_views", return_value=[]): + with mock.patch.object(reportinputs, "select_views", return_value=[]): return resolver.build_report("p", profile, ".cache", True) def test_a_declared_pin_names_the_repository(self):