Coverage for packages/workshop/src/livery/workshop/_speed.py: 97%

Measured by coverage.py 7.16.2 on CPython 3.14 while the suites of tests, packages/workshop, packages/extensions/ty, packages/extensions/ruff, packages/extensions/python, packages/extensions/pytest, packages/extensions/pyrefly, packages/extensions/nanobind, packages/extensions/mypy, packages/extensions/mkdocstrings, packages/extensions/lfs, packages/extensions/docs, packages/extensions/cpp, packages/extensions/coverage, packages/extensions/conan, packages/extensions/cmake, packages/extensions/claude, packages/extensions/clang-tidy, packages/extensions/clang-format, packages/extensions/changelog, packages/extensions/basedpyright ran.

1"""The test speed marks: a package's suite time per check leg, and how it moves.
2
3The gate job judges each check leg's summed test time per package, the
4``packages`` field of the timing rows ([livery.workshop._metrics][]),
5against a mark on the ``speed/marks`` series. The mark is the median of
6the leg's last five green runs for the package, recorded once five are
7seen; it ratchets down when that median beats it by more than five per
8cent, so a suite cannot regress slowly, and a person raises it with
9``fm speed.accept``, since a new heavy test is a cost someone chose.
10
11A run over the mark by more than fifteen per cent and twenty seconds
12warns, naming the leg's slowest tests and the ones that grew most
13against their own medians; the second such run in a row on a reference
14leg (ubuntu) is red, and the other legs warn only until their variance
15is measured. A store that cannot be read falls open with its reason and
16writes nothing. The margins are constants here, chosen before the rows
17could set them; a reading of a month of rows may move them.
18
19Reach for `judge_run` to judge the run the gate job is in, `accept` for
20the person's raise, `render_marks` for what ``fm ci.timings`` adds, and
21[livery.workshop.speed_marks][] for the current marks. A test's
22package, which the speed plugin sums by and the timing rows key by, is
23what [livery.workshop.package_of][] names.
24"""
25
26from __future__ import annotations(empty)
27
28import re(empty)
29import statistics(empty)
30from collections import defaultdict(empty)
31from collections.abc import Mapping(empty)
32from dataclasses import dataclass(empty)
33from datetime import UTC, datetime(empty)
34from pathlib import Path(empty)
35from typing import Any(empty)
36
37from livery import git(empty)
38from livery.footman import fail(empty)
39from livery.workshop._ci_context import RunContext, state_store(empty)
40from livery.workshop._shard import ONE, Shard(empty)
41
42#: The environment variable a test runner sets to the file the speed
43#: plugin writes each package's summed test time into, as JSON; with it
44#: unset the plugin sums nothing and writes nothing. It lives here, in
45#: a module that imports no pytest, so a runner reads it without pytest.
46SPEED_FILE_VARIABLE = "WORKSHOP_SPEED_FILE"(empty)
47
48#: The directories a package's tests live under: the path before the
49#: first of them names the package, one level deeper in a group
50#: directory (``packages/extensions/widgets/tests/...``).
51PACKAGE_TREES = frozenset({"tests", "src", "docs"})(empty)
52
53
54def package_of(nodeid: str) -> str:(empty)
55 """Name the workspace package a test belongs to, from its pytest node id.
56
57 The package is the path before the first directory a package keeps
58 its tests or sources in, ``tests``, ``src`` or ``docs``:
59 ``packages/forge/tests/test_x.py::test_y`` belongs to
60 ``packages/forge``. A package in a group directory is one level
61 deeper, and its own name may be one of those three: in
62 ``packages/extensions/docs/tests/test_x.py::test_y`` the ``tests``
63 beneath ``docs`` says ``docs`` is the package, so the answer is
64 ``packages/extensions/docs``. Nothing is read from the disk.
65
66 Args:
67 nodeid: A test's node id, relative to the workspace root.
68
69 Returns:
70 The package's path relative to the workspace root, or ``""`` for
71 a node outside ``packages/``, such as a workspace test under
72 ``tests/``.
73 """
74 parts = nodeid.split("::", 1)[0].split("/")
75 if len(parts) < 3 or parts[0] != "packages":
76 return ""
77 for index in range(2, len(parts) - 1):
78 if parts[index] not in PACKAGE_TREES:
79 continue
80 if index == 2 and parts[3] in PACKAGE_TREES and len(parts) > 4:
81 continue
82 return "/".join(parts[:index])
83 return "/".join(parts[:2])
84
85
86#: The marks: one row per write, the newest per package and leg the
87#: mark; anyone may write, since an accept is a person's act, and the
88#: gate job's writer guards its own rule that only a run records or
89#: ratchets.
90SERIES = git.Series("speed/marks", window=400, ci_only=False)(empty)
91
92#: The green runs behind a mark: the median of this many is the mark.
93SAMPLE = 5(empty)
94
95#: Over the mark by more than this fraction ...
96MARGIN = 0.15(empty)
97
98#: ... and by more than this many seconds is over.
99FLOOR_SECONDS = 20.0(empty)
100
101#: The sample's median under the mark by more than this fraction
102#: moves the mark down to it.
103RATCHET = 0.05(empty)
104
105#: The runner whose legs are red on the second run over; every other
106#: leg warns only.
107REFERENCE = "ubuntu"(empty)
108
109#: The contract key that turns the speed marks on.
110ENABLED_KEY = "speed-marks"(empty)
111
112
113def enabled(root: Path) -> bool:(empty)
114 """The contract's ``[ci] speed-marks``; false when undeclared.
115
116 Off by default: a hosted runner's test time varies by tens of
117 seconds between two runs of one tree, and a mark taken from such
118 runs says nothing about the suite. A workspace whose runners keep
119 a steady clock declares the key, which the contract's judge holds
120 to a boolean.
121 """
122 from livery.workshop._contract import load_contract
123
124 ci = load_contract(root / "workshop.toml").get("ci") or {}
125 return bool(ci.get(ENABLED_KEY, False))
126
127
128#: The slowest tests a leg's row names, and a warning prints.
129SLOWEST = 10(empty)
130
131#: The tests that grew most a warning prints.
132GROWN = 5(empty)
133
134_DISPLAY = re.compile((empty)
135 r"^(?P<job>[^ (]+) \((?P<os>[^,]+), (?P<python>[^,)]+)(?:, (?P<shard>[^)]+))?\)$"
136)
137
138#: GitLab lists a job that runs as ``parallel: matrix`` by its key and
139#: the matrix value: ``check: [2of4]``.
140_GITLAB_DISPLAY = re.compile(r"^(?P<job>[^ :\[]+): \[(?P<shard>[^\]]+)\]$")(empty)
141
142
143def drop_marks(root: Path) -> list[str]:(empty)
144 """Drop the marks series while the marks are off; the lines to print.
145
146 Rows written before the switch went off would be the marks a
147 later opt-in judges from, so the gate job drops the whole series
148 when it finds any. Fails open: a store that cannot be read, or a
149 delete the remote refuses, prints its reason and drops nothing.
150 Nothing to print when the series is already gone.
151 """
152 from livery.git import drop
153
154 found = SERIES.rows(state_store(root))
155 if found.failed:
156 return [f" speed marks: not dropped: {found.reason}"]
157 if not found.rows and not found.skipped:
158 return []
159 why = drop(state_store(root), SERIES.ref)
160 if why:160 ↛ 161line 160 didn't jump to line 161 because the condition on line 160 was never true
161 return [f" speed marks: not dropped: {why}"]
162 count = len(found.rows) + len(found.skipped)
163 return [
164 f" speed marks: dropped {count} stale row(s) from {SERIES.ref};"
165 f" the marks are off ([ci] {ENABLED_KEY})"
166 ]
167
168
169def leg_label(display: str) -> str:(empty)
170 """The leg label of a matrix job's display name; a plain name stands.
171
172 ``check (ubuntu-latest, 3.14)`` is the leg ``check-ubuntu-latest-3.14``
173 and ``check (windows-latest, 3.14, 2of4)`` the shard leg
174 ``check-windows-latest-3.14-2of4``, the spelling the job runner gives
175 every entry it spawns.
176 """
177 match = _DISPLAY.match(display)
178 if match is None:
179 return display
180 parts = (match["job"], match["os"], match["python"], match["shard"])
181 return "-".join(part for part in parts if part)
182
183
184def display_shard(display: str) -> Shard:(empty)
185 """The shard a job's display name spells; `ONE` when it spells none.
186
187 ``check (windows-latest, 3.14, 2of4)`` on GitHub and Gitea,
188 ``check: [2of4]`` on GitLab. A tail that is not spelled
189 ``<i>of<n>`` names no shard, so a matrix with another third value
190 reads as one shard of one.
191 """
192 match = _DISPLAY.match(display) or _GITLAB_DISPLAY.match(display)
193 spelled = match["shard"] if match else None
194 if not spelled:
195 return ONE
196 try:
197 return Shard.parse(spelled)
198 except ValueError:
199 return ONE
200
201
202def platform_display(display: str) -> str:(empty)
203 """The display name of a shard's platform: *display* without its shard.
204
205 ``check (windows-latest, 3.14, 2of4)`` gives ``check (windows-latest,
206 3.14)`` and ``check: [2of4]`` gives ``check``; a name that spells no
207 shard stands. The gate job's fold names a platform's folded row by
208 it, and the collect joins that row with the forge's jobs.
209 """
210 if display_shard(display) == ONE:
211 return display
212 match = _DISPLAY.match(display)
213 if match is not None:
214 # The shard follows a comma and a space; what comes before is
215 # the platform's own name, closed again.
216 return f"{display[: match.start('shard') - 2]})"
217 gitlab = _GITLAB_DISPLAY.match(display)
218 return gitlab["job"] if gitlab is not None else display
219
220
221def forge_shard_row(job: str, row: Mapping[str, Any]) -> bool:(empty)
222 """Whether *job*'s *row* is the forge's own row of a shard: no scope, a shard named.
223
224 The gate job folds a platform's shards into one row under the
225 platform's display name, and the collect keeps the forge's rows of
226 the shards beside it for ``fm ci.timings``; those carry the forge's
227 times and no scope. The stamp and the speed judge skip them, since
228 the folded row carries what they read.
229 """
230 return "scope" not in row and display_shard(job) != ONE
231
232
233def leg_of(job: str, row: Mapping[str, Any]) -> str:(empty)
234 """The leg a run's job row measured: the row's own label, or the name's.
235
236 A check leg writes its label (``check-ubuntu-latest-3.14``) into
237 its row under ``scope.leg``, and that spelling holds on every
238 forge; the job's display name carries the matrix on GitHub and
239 Gitea alone, and on GitLab is the bare ``check``, which would name
240 no runner and never count as the reference.
241 """
242 scope = row.get("scope")
243 label = str(scope.get("leg") or "") if isinstance(scope, Mapping) else ""
244 return label or leg_label(job)
245
246
247def is_reference(leg: str) -> bool:(empty)
248 """Whether *leg* runs on the reference runner, the one whose red counts."""
249 return REFERENCE in leg
250
251
252@dataclass(frozen=True)(empty)
253class SpeedMark:(empty)
254 """A package's mark on one check leg and the row that set it.
255
256 The mark is the suite's summed test time the gate job judges the
257 leg's runs against; [livery.workshop.speed_marks][] reads the
258 current mark of each package on each leg.
259
260 Attributes:
261 package: The package path (``packages/forge``).
262 leg: The check leg (``check-ubuntu-latest-3.14``).
263 seconds: The mark: the suite's summed test time.
264 kind: ``first`` (a run recorded it), ``ratchet`` (a run moved
265 it down), or ``accept`` (a person raised it).
266 by: The run or the person that wrote the row.
267 reason: The reason an accept gave; empty otherwise.
268 when: When the row was written, ISO 8601.
269 """
270
271 package: str(empty)
272 leg: str(empty)
273 seconds: float(empty)
274 kind: str(empty)
275 by: str(empty)
276 reason: str(empty)
277 when: str(empty)
278
279
280@dataclass(frozen=True)(empty)
281class Sample:(empty)
282 """One package's time on one leg in this run, with the leg's history.
283
284 Attributes:
285 package: The package path.
286 leg: The check leg.
287 seconds: This run's summed test time for the package.
288 tests: How many tests ran.
289 previous: The leg's earlier green runs' times for the package,
290 newest first, at most one fewer than `SAMPLE`.
291 slowest: This run's slowest tests, name and seconds, slowest first.
292 grown: The tests that grew most against their median over the
293 earlier runs: name, the median, this run's seconds.
294 """
295
296 package: str(empty)
297 leg: str(empty)
298 seconds: float(empty)
299 tests: int(empty)
300 previous: tuple[float, ...](empty)
301 slowest: tuple[tuple[str, float], ...] = ()(empty)
302 grown: tuple[tuple[str, float, float], ...] = ()(empty)
303
304
305@dataclass(frozen=True)(empty)
306class Verdict:(empty)
307 """One package on one leg, judged against its mark.
308
309 Attributes:
310 sample: The run's time and the leg's history.
311 mark: The current mark, or ``None`` when none is recorded.
312 """
313
314 sample: Sample(empty)
315 mark: SpeedMark | None(empty)
316
317 @property(empty)
318 def limit(self) -> float | None:(empty)
319 """The seconds above which a run is over; ``None`` without a mark."""
320 if self.mark is None:
321 return None
322 return round(
323 max(self.mark.seconds * (1 + MARGIN), self.mark.seconds + FLOOR_SECONDS), 1
324 )
325
326 @property(empty)
327 def over(self) -> bool:(empty)
328 """Whether this run is over the mark by the margin."""
329 return self.limit is not None and self.sample.seconds > self.limit
330
331 @property(empty)
332 def previous_over(self) -> bool:(empty)
333 """Whether the leg's newest earlier green run was over too."""
334 return (
335 self.limit is not None
336 and bool(self.sample.previous)
337 and self.sample.previous[0] > self.limit
338 )
339
340 @property(empty)
341 def red(self) -> bool:(empty)
342 """The second run over in a row, on a reference leg."""
343 return self.over and self.previous_over and is_reference(self.sample.leg)
344
345 @property(empty)
346 def median(self) -> float | None:(empty)
347 """The median of the sample, this run included; ``None`` short of one."""
348 values = (self.sample.seconds, *self.sample.previous)
349 if len(values) < SAMPLE:
350 return None
351 return round(statistics.median(values[:SAMPLE]), 1)
352
353 @property(empty)
354 def records(self) -> bool:(empty)
355 """Whether this run records the first mark: no mark, a full sample."""
356 return self.mark is None and self.median is not None
357
358 @property(empty)
359 def ratchets(self) -> bool:(empty)
360 """Whether the sample's median beats the mark by more than `RATCHET`."""
361 return (
362 self.mark is not None
363 and self.median is not None
364 and self.median < self.mark.seconds * (1 - RATCHET)
365 )
366
367
368def _row_name(when: datetime, package: str, leg: str) -> str:(empty)
369 return f"{when.strftime('%Y%m%dT%H%M%S.%fZ')}--{git.slug(package)}--{git.slug(leg)}"
370
371
372def speed_marks(root: Path) -> tuple[dict[tuple[str, str], SpeedMark] | None, str]:(empty)
373 """Read each package's current test-time mark on each check leg.
374
375 The marks live on the checkout's state store
376 ([livery.workshop.state_store][]). The newest row per package and
377 leg is its mark. A row that does not parse, is of another schema,
378 or lacks a mark's fields is skipped, so one bad row disables
379 nothing. A store that cannot be read falls open: the caller prints
380 the reason and judges nothing.
381
382 Args:
383 root: The checkout whose state store holds the marks.
384
385 Returns:
386 Each mark by its package's path and its leg, with an empty
387 reason; an empty store gives ``({}, "")``. When the store
388 cannot be read, None and the reason.
389 """
390 found = SERIES.rows(state_store(root))
391 if found.failed:
392 return None, found.reason
393 current: dict[tuple[str, str], SpeedMark] = {}
394 for row in found.rows:
395 mark = _mark(row.data)
396 if mark is not None:
397 current.setdefault((mark.package, mark.leg), mark)
398 return current, ""
399
400
401def _mark(data: dict[str, Any]) -> SpeedMark | None:(empty)
402 try:
403 return SpeedMark(
404 package=str(data["package"]),
405 leg=str(data["leg"]),
406 seconds=float(data["seconds"]),
407 kind=str(data.get("kind", "")),
408 by=str(data.get("by", "")),
409 reason=str(data.get("reason", "")),
410 when=str(data.get("when", "")),
411 )
412 except (KeyError, TypeError, ValueError):
413 return None
414
415
416def write_mark((empty)
417 root: Path,
418 *,
419 package: str,
420 leg: str,
421 seconds: float,
422 kind: str,
423 by: str,
424 reason: str = "",
425) -> str:
426 """Append one row setting *package*'s mark on *leg*; ``""`` or why not."""
427 row = {
428 "package": package,
429 "leg": leg,
430 "seconds": round(seconds, 1),
431 "kind": kind,
432 "by": by,
433 "reason": reason,
434 }
435 return SERIES.put(
436 state_store(root),
437 {_row_name(datetime.now(UTC), package, leg): row},
438 message=f"speed mark: {package} on {leg} {kind} {seconds:.1f}s by {by}",
439 )
440
441
442def _package_times(row: dict[str, Any]) -> dict[str, tuple[float, int]]:(empty)
443 """Each package's summed seconds and test count in a leg's row; ran ones only."""
444 out: dict[str, tuple[float, int]] = {}
445 packages = row.get("packages")
446 if not isinstance(packages, dict):446 ↛ 447line 446 didn't jump to line 447 because the condition on line 446 was never true
447 return out
448 for name, data in packages.items():
449 if not isinstance(data, dict):449 ↛ 450line 449 didn't jump to line 450 because the condition on line 449 was never true
450 continue
451 tests = data.get("tests", 0)
452 ms = data.get("tests_ms", 0.0)
453 if isinstance(tests, int) and tests > 0 and isinstance(ms, int | float):
454 out[f"packages/{name}"] = (round(float(ms) / 1000.0, 1), tests)
455 return out
456
457
458def _slowest(row: dict[str, Any]) -> dict[str, float]:(empty)
459 """The slowest tests a leg's row names, seconds by node id."""
460 out: dict[str, float] = {}
461 for item in row.get("slowest", []) or []:
462 if isinstance(item, dict) and isinstance(item.get("s"), int | float):462 ↛ 461line 462 didn't jump to line 461 because the condition on line 462 was always true
463 out[str(item.get("test", ""))] = float(item["s"])
464 return out
465
466
467def _grown((empty)
468 now: dict[str, float], earlier: list[dict[str, float]], package: str
469) -> tuple[tuple[str, float, float], ...]:
470 """The tests of *package* that grew most against their median in *earlier*."""
471 history: dict[str, list[float]] = defaultdict(list)
472 for row in earlier:
473 for test, seconds in row.items():
474 history[test].append(seconds)
475 grown: list[tuple[float, str, float, float]] = []
476 for test, seconds in now.items():
477 if package_of(test) != package or test not in history:
478 continue
479 before = statistics.median(history[test])
480 if seconds > before * (1 + MARGIN) and seconds > before + 1.0:
481 grown.append((seconds - before, test, round(before, 2), seconds))
482 grown.sort(reverse=True)
483 return tuple((test, before, now_s) for _, test, before, now_s in grown[:GROWN])
484
485
486def samples(entries: list[dict[str, Any]], run_id: str) -> list[Sample]:(empty)
487 """This run's package times per check leg, with each leg's green history.
488
489 *entries* are the timing rows newest first. The run's own entry
490 is the one whose ``run`` is *run_id*; a check leg of it yields one
491 sample per package whose tests ran, and the forge's own row of a
492 shard (`forge_shard_row`) yields none. The history is the same leg's
493 earlier entries that concluded green and ran the package's tests,
494 newest first, up to one fewer than `SAMPLE`. Empty when the run
495 has no entry.
496 """
497 current = next((e for e in entries if str(e.get("run", "")) == run_id), None)
498 if current is None:
499 return []
500 earlier = [
501 e for e in entries if e is not current and str(e.get("run", "")) != run_id
502 ]
503 out: list[Sample] = []
504 for job, row in sorted((current.get("jobs") or {}).items()):
505 if not job.startswith("check") or not isinstance(row, dict):
506 continue
507 if forge_shard_row(job, row):
508 continue
509 leg = leg_of(job, row)
510 now_slowest = _slowest(row)
511 for package, (seconds, tests) in sorted(_package_times(row).items()):
512 previous: list[float] = []
513 slow_rows: list[dict[str, float]] = []
514 for entry in earlier:
515 past = (entry.get("jobs") or {}).get(job)
516 if not isinstance(past, dict) or past.get("conclusion") != "success":
517 continue
518 times = _package_times(past)
519 if package not in times:
520 continue
521 previous.append(times[package][0])
522 slow_rows.append(_slowest(past))
523 if len(previous) >= SAMPLE - 1:
524 break
525 slowest = tuple(
526 sorted(
527 (
528 (t, s)
529 for t, s in now_slowest.items()
530 if package_of(t) == package
531 ),
532 key=lambda item: -item[1],
533 )[:SLOWEST]
534 )
535 out.append(
536 Sample(
537 package=package,
538 leg=leg,
539 seconds=seconds,
540 tests=tests,
541 previous=tuple(previous),
542 slowest=slowest,
543 grown=_grown(now_slowest, slow_rows, package),
544 )
545 )
546 return out
547
548
549def judge((empty)
550 found: list[Sample], current: dict[tuple[str, str], SpeedMark]
551) -> list[Verdict]:
552 """Each sample against its mark; one verdict each."""
553 return [
554 Verdict(sample=sample, mark=current.get((sample.package, sample.leg)))
555 for sample in found
556 ]
557
558
559def render(verdict: Verdict) -> list[str]:(empty)
560 """The lines for *verdict*: the time, the mark, the state, the culprits."""
561 sample, mark = verdict.sample, verdict.mark
562 head = f" speed {sample.package} on {sample.leg}: {sample.seconds:.1f}s"
563 if mark is None:
564 seen = len(sample.previous) + 1
565 if verdict.records:
566 return [f"{head}, no mark yet; this run records {verdict.median:.1f}s"]
567 return [f"{head}, no mark yet ({seen}/{SAMPLE} green runs)"]
568 limit = verdict.limit
569 assert limit is not None
570 state = ""
571 if verdict.red:
572 state = " OVER for the second run in a row: red"
573 elif verdict.over and not is_reference(sample.leg):
574 state = " OVER (this leg warns only)"
575 elif verdict.over:
576 state = " OVER (a second run over in a row is red)"
577 lines = [
578 f"{head} (mark {mark.seconds:.1f}s {mark.kind} by {mark.by},"
579 f" limit {limit:.1f}s){state}"
580 ]
581 if mark.reason:581 ↛ 582line 581 didn't jump to line 582 because the condition on line 581 was never true
582 lines.append(f" accepted: {mark.reason}")
583 if verdict.over:
584 if sample.slowest:
585 named = ", ".join(f"{test} {s:.1f}s" for test, s in sample.slowest)
586 lines.append(f" slowest: {named}")
587 if sample.grown:
588 named = ", ".join(
589 f"{test} {before:.1f}s -> {now:.1f}s"
590 for test, before, now in sample.grown
591 )
592 lines.append(f" grew: {named}")
593 if verdict.ratchets:
594 lines.append(
595 f" new mark: {verdict.median:.1f}s (the median of {SAMPLE} beats"
596 f" {mark.seconds:.1f}s by more than {RATCHET:.0%})"
597 )
598 return lines
599
600
601def judge_run(root: Path, run: RunContext) -> tuple[list[Verdict], list[str]]:(empty)
602 """Judge the run's check legs; the verdicts and the lines for what could not be.
603
604 Reads the timing rows and the marks; either unreadable falls open
605 with its reason and no verdict, so a store fault never reddens a
606 run and never writes.
607 """
608 from livery.workshop._metrics import SERIES as METRICS
609
610 rows = METRICS.rows(state_store(root))
611 if rows.failed:
612 return [], [f" speed: not judged ({rows.reason})"]
613 current, why = speed_marks(root)
614 if current is None:614 ↛ 615line 614 didn't jump to line 615 because the condition on line 614 was never true
615 return [], [f" speed: not judged ({why})"]
616 found = samples([row.data for row in rows.rows], run.run_id)
617 if not found:
618 return [], [
619 f" speed: run {run.run_id} left no check leg with tests; nothing to judge"
620 ]
621 return judge(found, current), []
622
623
624def apply(root: Path, verdicts: list[Verdict], *, by: str) -> list[str]:(empty)
625 """Write the first marks and the ratchets among *verdicts*; the lines."""
626 lines: list[str] = []
627 for verdict in verdicts:
628 if not (verdict.records or verdict.ratchets):
629 continue
630 median = verdict.median
631 assert median is not None
632 kind = "first" if verdict.records else "ratchet"
633 why = write_mark(
634 root,
635 package=verdict.sample.package,
636 leg=verdict.sample.leg,
637 seconds=median,
638 kind=kind,
639 by=by,
640 )
641 lines.append(
642 f" {'recorded' if not why else 'not recorded: ' + why}"
643 f" ({verdict.sample.package} on {verdict.sample.leg}, {kind} {median:.1f}s)"
644 )
645 return lines
646
647
648def accept((empty)
649 root: Path,
650 *,
651 package: str,
652 leg: str,
653 seconds: float,
654 reason: str,
655 by: str,
656 packages: tuple[str, ...],
657) -> list[str]:
658 """Raise *package*'s mark on *leg* to *seconds* deliberately; the lines.
659
660 Refuses without a reason, for a path not among *packages*, a time
661 that is not positive, an unreadable store, and a value at or below
662 the current mark: lowering is the ratchet's own move.
663 """
664 if not reason.strip():
665 fail(
666 "a reason is required: --reason=<why the suite may take longer> goes"
667 " on the record beside the new mark"
668 )
669 if package not in packages:
670 fail(f"no package at {package!r}; the packages are {', '.join(packages)}")
671 if seconds <= 0:
672 fail(f"the mark is a time in seconds; {seconds!r} is not")
673 current, why = speed_marks(root)
674 if current is None:
675 fail(f"refusing: {why}; a write from an unread store would erase its rows")
676 mark = current.get((package, leg))
677 if mark is not None and seconds <= mark.seconds:
678 fail(
679 f"{package}'s mark on {leg} is {mark.seconds:.1f}s; {seconds:.1f}s does"
680 " not raise it. Lowering is the ratchet's own move, when the runs beat"
681 " the mark."
682 )
683 written = write_mark(
684 root,
685 package=package,
686 leg=leg,
687 seconds=seconds,
688 kind="accept",
689 by=by,
690 reason=reason.strip(),
691 )
692 if written:692 ↛ 693line 692 didn't jump to line 693 because the condition on line 692 was never true
693 fail(f"the mark was not written: {written}")
694 was = f"{mark.seconds:.1f}s" if mark is not None else "none"
695 return [
696 f" speed {package} on {leg}: mark {was} -> {seconds:.1f}s accepted by {by}",
697 f" reason: {reason.strip()}",
698 ]
699
700
701def render_marks(root: Path) -> list[str]:(empty)
702 """The marks beside the newest run's times, for ``fm ci.timings``."""
703 from livery.workshop._metrics import SERIES as METRICS
704
705 current, why = speed_marks(root)
706 if current is None:
707 return [f" speed marks: not read ({why})"]
708 if not current:
709 return [" speed marks: none yet; the gate job records one per package and leg"]
710 latest: dict[tuple[str, str], float] = {}
711 rows = METRICS.rows(state_store(root))
712 if not rows.failed and rows.rows:712 ↛ 718line 712 didn't jump to line 718 because the condition on line 712 was always true
713 newest = rows.rows[0].data
714 for job, row in (newest.get("jobs") or {}).items():
715 if isinstance(row, dict):715 ↛ 714line 715 didn't jump to line 714 because the condition on line 715 was always true
716 for package, (seconds, _tests) in _package_times(row).items():
717 latest[(package, leg_of(job, row))] = seconds
718 lines = [" speed marks", f" {'package on leg':<52} {'mark':>8} {'latest':>8}"]
719 for (package, leg), mark in sorted(current.items()):
720 now = latest.get((package, leg))
721 shown = f"{now:.1f}s" if now is not None else "-"
722 lines.append(
723 f" {f'{package} on {leg}'[:52]:<52} {mark.seconds:7.1f}s {shown:>8}"
724 f" {mark.kind} by {mark.by}"
725 )
726 return lines