1"""``fm speed.judge`` and ``fm speed.accept``: the test speed marks' verbs.
3``speed.judge`` runs in the gate job after the timing rows are
4collected: it judges every check leg's summed test time per package
5against the marks ([livery.workshop._speed][]), records the first marks
6and the ratchets, warns on a run over the mark, and is red on the
7second run over in a row on a reference leg. ``speed.accept`` is the
8person's raise of a mark, with the reason on the record. Both verbs
9do nothing until the contract declares ``[ci] speed-marks = true``,
10and ``speed.judge`` runs on a CI leg alone.
11"""
13from __future__ import annotations
15from pathlib import Path
16from typing import Annotated
18import livery.footman as footman
19from livery.footman import doc, fail, group, requires
20from livery.workshop import _speed
21from livery.workshop._ci_context import RunContext, run_context, state_store
22from livery.workshop._extensions import workspace_root
23from livery.workshop._packages import discover_packages
25speed = group("speed", help="The test suites' speed marks")
28def _root() -> Path:
29 root = workspace_root()
30 if root is None:30 ↛ 31line 30 didn't jump to line 31 because the condition on line 30 was never true
31 fail("no workspace: no workshop.toml above the working directory")
32 return root
35def judge_flow(root: Path, run: RunContext) -> list[str]:
36 """Judge the run's legs, record what moves, and return the red lines.
38 Prints every verdict and every write. Fails open: a store that
39 cannot be read prints its reason and reddens nothing.
40 """
41 from livery.git import remote_snapshot
43 with remote_snapshot(state_store(root), fetch=("metrics", "speed/marks")):
44 verdicts, why = _speed.judge_run(root, run)
46 print(line)
47 for verdict in verdicts:
48 for line in _speed.render(verdict):
49 print(line)
50 for line in _speed.apply(root, verdicts, by=f"run {run.run_id}"):
51 print(line)
52 return [
53 f"{v.sample.package} on {v.sample.leg}: {v.sample.seconds:.1f}s over the"
54 f" limit {v.limit:.1f}s for the second run in a row"
55 for v in verdicts
56 if v.red and v.limit is not None
57 ]
60@speed.task(name="judge", hidden=True)
61@requires(
62 lambda: run_context() is not None,
63 reason="a CI run: the speed marks are judged on the CI legs",
64)
65def speed_judge() -> None:
66 """Judge the run's test times against the speed marks; red on the second run over.
68 Runs in the gate job after the timing rows are collected. Off
69 until the contract declares ``[ci] speed-marks = true``: it says
70 so, and drops any marks left from before the switch, so a later
71 opt-in starts from the timings alone. A CI verb: outside CI it is
72 listed as unavailable with the reason and refused, since a
73 machine's clock is not a leg the marks are taken on.
74 """
75 # The CI question first: outside a run the verb refuses with that
76 # reason wherever it stands, a workspace or not.
77 run = run_context()
78 if run is None:
79 fail("not a CI run: the speed marks are judged on the CI legs")
80 root = _root()
81 if not _speed.enabled(root):
82 print(
83 f" speed marks are off: declare [ci] {_speed.ENABLED_KEY} = true"
84 " in workshop.toml to judge test time"
85 )
86 for line in _speed.drop_marks(root):
87 print(line)
88 return
89 red = judge_flow(root, run)
91 fail(
92 "test speed over the mark for two runs in a row:\n "
93 + "\n ".join(red)
94 + "\n make the suite faster, or accept the cost with"
95 f" `{footman.prog()} speed.accept <package> <seconds> --reason=<why>`"
96 )
99@speed.task(name="accept")
100def speed_accept(
101 package: Annotated[str, doc("the package path, as packages/forge")],
102 seconds: Annotated[float, doc("the new mark: the suite's summed test time")],
103 reason: Annotated[
104 str, doc("why the suite may take longer; goes on the record")
105 ] = "",
106 leg: Annotated[str, doc("the check leg; the reference leg when empty")] = "",
107) -> None:
108 """Raise a package's speed mark deliberately, with the reason on the record.
110 The mark comes down on its own when the runs beat it; raising it
111 is a person's act, so this writes a dated row naming who and why,
112 and the next gated run judges from it. Refuses while the marks
113 are off, without a reason, for an unknown package, at or below
114 the current mark, or when the marks cannot be read.
115 """
116 from livery.workshop._points import check_legs
117 from livery.workshop._quality import _git_identity
119 root = _root()
120 if not _speed.enabled(root):120 ↛ 125line 120 didn't jump to line 125 because the condition on line 120 was always true
121 fail(
122 f"speed marks are off: declare [ci] {_speed.ENABLED_KEY} = true"
123 " in workshop.toml before accepting a mark"
124 )
125 packages = tuple(item.path for item in discover_packages(root))
126 legs = check_legs(root)
127 chosen = leg or next((item for item in legs if _speed.is_reference(item)), "")
128 if not chosen:
129 chosen = legs[0] if legs else ""
130 if not chosen:
131 fail("no check leg: the contract names no runner to judge on")
132 for line in _speed.accept(
133 root,
134 package=package,
135 leg=chosen,
136 seconds=seconds,
137 reason=reason,
138 by=_git_identity(root),
139 packages=packages,
140 ):
141 print(line)