Coverage for packages/workshop/src/livery/workshop/_scope.py: 100%

Measured by coverage.py 7.16.2 on CPython 3.14 while the suites of tests, packages/workshop/tests/test_workshop_without_docs.py, packages/workshop/tests/test_workshop_vocabulary.py, packages/workshop/tests/test_workshop_sync_install.py, packages/workshop/tests/test_workshop_sync.py, packages/workshop/tests/test_workshop_shells.py, packages/workshop/tests/test_workshop_runs.py, packages/workshop/tests/test_workshop_quality.py, packages/workshop/tests/test_workshop_proved_shadow.py, packages/workshop/tests/test_workshop_proved_parts.py, packages/workshop/tests/test_workshop_proved_decide.py, packages/workshop/tests/test_workshop_proved.py, packages/workshop/tests/test_workshop_prose.py, packages/workshop/tests/test_workshop_proof_pins.py, packages/workshop/tests/test_workshop_points.py, packages/workshop/tests/test_workshop_new_project.py, packages/workshop/tests/test_workshop_metrics.py, packages/workshop/tests/test_workshop_layering.py, packages/workshop/tests/test_workshop_extension_mount.py, packages/workshop/tests/test_workshop_e2e_rows.py, packages/workshop/tests/test_workshop_e2e.py, packages/workshop/tests/test_workshop_credential_traces.py, packages/workshop/tests/test_workshop_affected_legs.py, packages/extensions/python/tests/test_extensions_python.py, packages/extensions/pytest/tests/test_pytest.py, packages/extensions/nanobind/tests/test_extensions_nanobind.py, packages/extensions/docs/tests/test_task_links.py, packages/extensions/cpp/tests/test_extensions_cpp.py, packages/extensions/coverage/tests/test_coverage_store.py, packages/extensions/coverage/tests/test_coverage_proved_parts.py, packages/extensions/coverage/tests/test_coverage_accept.py, packages/extensions/coverage/tests/test_coverage.py, packages/extensions/coverage/tests/test_affected_legs.py, packages/extensions/conan/tests/test_extensions_conan.py ran.

1"""Local check-leg scope marker and proved-branch lookup for coverage union.
2
3Each check leg leaves its scope in ``fm-gate.json`` beside its trace
4(``full``, ``affected``, ``nothing``, or ``verified``) so the leg's
5entries after the gate, and its timing row, know which suites ran.
6`verified_branch` reads the pull request branch whose proved rows cover
7the checked-out tree on ``main``, so the coverage extension can carry
8over that branch's parts before Phase B6 moves coverage beside the
9proved row itself.
10"""
11
12from __future__ import annotations
13
14import json
15from dataclasses import dataclass
16from pathlib import Path
17
18#: The marker a check leg leaves beside its trace, naming the scope
19#: its gate ran, for the leg's metrics row and coverage top-up.
20MARKER = "fm-gate.json"
21
22#: The scope names a leg records: the whole gate; the gate narrowed to
23#: the affected packages; nothing, when no test suite ran; verified,
24#: when every unit was proved green already.
25FULL = "full"
26AFFECTED = "affected"
27NOTHING = "nothing"
28VERIFIED = "verified"
29
30#: The scopes a leg can leave; anything else means the leg said nothing.
31KNOWN = (FULL, AFFECTED, NOTHING, VERIFIED)
32
33
34def write_marker(
35 root: Path,
36 scope: str,
37 packages: tuple[str, ...] = (),
38 *,
39 leg: str = "",
40 base_tree: str = "",
41 branch: str = "",
42) -> None:
43 """Leave the leg's scope, suites, label, and proving branch beside its trace."""
44 payload: dict[str, object] = {
45 "scope": scope,
46 "packages": list(packages),
47 "leg": leg,
48 "base_tree": base_tree,
49 }
50 if branch:
51 payload["branch"] = branch
52 (root / MARKER).write_text(
53 json.dumps(payload, sort_keys=True),
54 encoding="utf-8",
55 )
56
57
58@dataclass(frozen=True)
59class LegScope:
60 """What the gate ran on one check leg, as the marker the leg left says.
61
62 Attributes:
63 scope: What the gate ran: ``full``, ``affected``, ``nothing``,
64 ``verified``, or ``unknown`` when no marker exists or it
65 does not parse.
66 packages: The paths of the suites a narrowed gate ran; empty
67 for every other scope.
68 leg: The label the leg's rows are keyed by
69 (``check-ubuntu-latest-3.14``); empty when absent.
70 base_tree: The tree a narrowed gate measured from; empty when
71 absent.
72 branch: The pull request branch whose proved rows satisfied a
73 ``main`` push leg, when known; empty otherwise.
74 """
75
76 scope: str
77 packages: tuple[str, ...] = ()
78 leg: str = ""
79 base_tree: str = ""
80 branch: str = ""
81
82
83def leg_scope(root: Path) -> LegScope:
84 """Read what the gate ran on this leg from the marker it left at *root*."""
85 unknown = LegScope("unknown")
86 path = root / MARKER
87 if not path.is_file():
88 return unknown
89 try:
90 loaded = json.loads(path.read_text("utf-8"))
91 except (OSError, ValueError):
92 return unknown
93 if not isinstance(loaded, dict):
94 return unknown
95 return LegScope(
96 scope=str(loaded.get("scope", "unknown")),
97 packages=tuple(str(p) for p in loaded.get("packages", []) or []),
98 leg=str(loaded.get("leg", "") or ""),
99 base_tree=str(loaded.get("base_tree", "") or ""),
100 branch=str(loaded.get("branch", "") or ""),
101 )
102
103
104@dataclass(frozen=True)
105class VerifiedBranch:
106 """The branch whose proved rows cover a tree, or why the record could not say.
107
108 Attributes:
109 branch: The pull request branch whose run proved the tree;
110 empty when no branch proved it or the record could not be read.
111 reason: Why the record decided nothing; empty when the record was read.
112 """
113
114 branch: str
115 reason: str = ""
116
117
118def verified_branch(root: Path) -> VerifiedBranch:
119 """Read which pull request branch's proved rows cover the checkout at *root*."""
120 from livery.workshop._proved import proving_branch
121
122 marker = leg_scope(root)
123 if marker.branch:
124 return VerifiedBranch(marker.branch)
125 branch, why = proving_branch(root)
126 if why:
127 return VerifiedBranch("", why)
128 return VerifiedBranch(branch)