Coverage for packages/workshop/src/livery/workshop/_fold.py: 94%

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 gate job's fold: a platform's shards become one check leg on the run's refs.
2
3A platform's check leg may run as several shards, each on a runner of
4its own, each leaving a per-run ref of its own ([livery.workshop.RUNS][])
5with its timing row and the files its entries left. Every reader after
6the gate job's first entries, the coverage union, the verified stamp
7and the speed marks, reads one leg per platform, keyed by the platform
8leg ([livery.workshop.platform_leg][]). `fold_run` is that fold, run
9once in the gate job before any of them: it lists the run's refs,
10groups them by platform, proves that the shards of a platform are one
11leg's work, puts one folded ref under the platform leg's key, and drops
12the shards' refs. A platform whose one ref is one shard of one is left
13as it is.
14
15The proof is the plan's first ground-truth contract: nothing is assumed
16from a shard that did not write. A platform's shards fold only when
17every shard the matrix produces left a ref with a row and the record of
18what it partitioned, every row names one scope, every record names one
19item set and one list of durations rows read under one bound, no item
20ran in two shards, and the items the shards ran are together the set
21they partitioned. Anything else is red by name: the platform,
22the shards and what differs. A shard whose record says it ran as one
23shard of one fell open on the record and ran the whole set; when every
24shard of the platform did, the first stands for the platform. A
25platform none of whose shards left a record partitioned nothing: the
26gate skipped on every shard, the tree already proved or nothing
27affected, or no step that reads the shard ran. Its shards fold on their
28scopes and files alone, with no partition to prove, and the line says
29so; one shard with a record beside one without is red by name. Shards
30whose partitions differ and whose records name two attempts of the run
31are a rerun of some jobs: the refusal names the attempts and says to
32rerun the whole run.
33
34The folded row is the first shard's timing row (its tasks, steps and
35waits) with every shard's package sums added and its seconds per item
36merged, keyed by the platform leg and named by the platform's own
37display name ([livery.workshop._speed.platform_display][]), with the
38shards listed under ``shard``. Every other file of the shards' refs
39folds through the ``ci.fold`` query ([livery.workshop._queries.FOLDERS][]):
40the extension whose leg entry leaves the file answers with the folder;
41a file nobody answers for is red by name.
42
43A fold that dies midway is re-run as its own recovery: the shards' refs
44stay until the folded ref is put, a put replaces the folded ref in
45place, and a shard's ref a failed drop left behind is known by the
46stamp the folded row recorded for it and dropped on the next run.
47"""
48
49from __future__ import annotations(empty)
50
51from collections import defaultdict(empty)
52from collections.abc import Mapping, Sequence(empty)
53from dataclasses import dataclass(empty)
54from pathlib import Path(empty)
55from typing import Any(empty)
56
57import livery.footman as footman(empty)
58from livery import git(empty)
59from livery.footman import fail(empty)
60from livery.workshop._ci_context import RunContext, state_store(empty)
61from livery.workshop._metrics import ROW_FILE, RUNS, SLOWEST(empty)
62from livery.workshop._queries import FOLDERS, Folder, QueryError, answer(empty)
63from livery.workshop._shard import ((empty)
64 ONE,
65 SHARD_FILE,
66 Shard,
67 ShardMarker,
68 files_digest,
69 parse_marker,
70 platform_leg,
71 shard_of,
72)
73from livery.workshop._speed import leg_label, platform_display(empty)
74
75#: How many items a refusal names before it counts the rest.
76NAMED = 5(empty)
77
78
79@dataclass(frozen=True)(empty)
80class _Ref:(empty)
81 """One per-run ref of the run, as the fold reads it.
82
83 Attributes:
84 key: The ref's key under the family, as the ref spells it.
85 ref: The full ref.
86 label: The leg's label: the row's own, else the one its
87 display name spells, else the key.
88 row: The leg's timing row, ``None`` when the ref carries none.
89 files: The ref's other rows, by file name.
90 marker: The record the shard left of what it partitioned, when
91 the row carries one.
92 why: Why the row's record is not one, when it is not.
93 skipped: The ref's files that are not rows, named.
94 """
95
96 key: tuple[str, ...](empty)
97 ref: str(empty)
98 label: str(empty)
99 row: dict[str, Any] | None(empty)
100 files: dict[str, dict[str, Any]](empty)
101 marker: ShardMarker | None(empty)
102 why: str(empty)
103 skipped: tuple[str, ...](empty)
104
105 @property(empty)
106 def shard(self) -> Shard:(empty)
107 """The shard the label spells; `ONE` for a label outside the form."""
108 return _label_shard(self.label)
109
110 @property(empty)
111 def folded(self) -> bool:(empty)
112 """Whether the row is a fold's own: its ``shard`` lists the shards."""
113 shard = self.row.get("shard") if self.row is not None else None
114 return isinstance(shard, dict) and "shards" in shard
115
116 @property(empty)
117 def sharded(self) -> bool:(empty)
118 """Whether the ref is one shard's: its label or its record says so."""
119 return self.shard != ONE or self.marker is not None or bool(self.why)
120
121 @property(empty)
122 def stamp(self) -> str:(empty)
123 """When the store stamped the row; empty without a row."""
124 return str(self.row.get("when", "") or "") if self.row is not None else ""
125
126
127def _label_shard(label: str) -> Shard:(empty)
128 try:
129 return shard_of(label)
130 except ValueError:
131 return ONE
132
133
134def _index(ref: _Ref) -> tuple[int, str]:(empty)
135 return ref.shard.index, ref.label
136
137
138def _seconds(item: Mapping[str, Any]) -> float:(empty)
139 held = item.get("s", 0.0)
140 return -float(held) if isinstance(held, int | float) else 0.0
141
142
143def _read_ref(store: git.StateStore, key: tuple[str, ...]) -> _Ref:(empty)
144 """The ref of *key*, its row, its other files and the shard record the row carries.
145
146 Raises:
147 Failed: When the ref cannot be read: a fold proves nothing from
148 a ref it cannot read.
149 """
150 series = RUNS.at(*key)
151 found = series.rows(store)
152 if found.failed:152 ↛ 153line 152 didn't jump to line 153 because the condition on line 152 was never true
153 fail(
154 f"{series.ref}: {found.reason}; the fold proves nothing from a ref it"
155 " cannot read"
156 )
157 rows = {row.name: row.data for row in found.rows}
158 row = rows.pop(ROW_FILE, None)
159 label = key[-1]
160 marker: ShardMarker | None = None
161 why = ""
162 if row is not None:
163 scope = row.get("scope")
164 own = str(scope.get("leg") or "") if isinstance(scope, Mapping) else ""
165 label = own or leg_label(str(row.get("job", ""))) or key[-1]
166 shard = row.get("shard")
167 if shard is not None and not (isinstance(shard, dict) and "shards" in shard):
168 marker, why = parse_marker(shard, source=f"{series.ref}'s shard record")
169 return _Ref(
170 key,
171 series.ref,
172 label,
173 row,
174 rows,
175 marker,
176 why,
177 tuple(skipped.name for skipped in found.skipped),
178 )
179
180
181def expected_shards(root: Path, run: RunContext) -> dict[str, tuple[str, ...]]:(empty)
182 """The shard labels the matrix produces, by platform leg, where it splits one.
183
184 From ``[ci] pull-request-shards`` on a pull request and ``[ci]
185 shards`` otherwise ([livery.workshop.pull_request_shards][],
186 [livery.workshop.check_shards][]). A platform of one shard is left
187 out. Empty without a root contract, as a test workspace may be.
188 """
189 from livery.workshop._points import check_shards, pull_request_shards
190
191 if not (root / "workshop.toml").is_file():
192 return {}
193 legs = (
194 pull_request_shards(root) if run.event == "pull_request" else check_shards(root)
195 )
196 by_platform: dict[str, list[str]] = defaultdict(list)
197 for leg in legs:
198 by_platform[platform_leg(leg)].append(leg)
199 return {
200 platform: tuple(labels)
201 for platform, labels in by_platform.items()
202 if len(labels) > 1
203 }
204
205
206def fold_run((empty)
207 root: Path, run: RunContext, *, folders: Mapping[str, Folder] | None = None
208) -> None:
209 """Fold each platform's shards on *run*'s refs into one leg, or refuse by name.
210
211 Lists the run's per-run refs and groups them by platform leg. A
212 platform with one ref that is one shard of one is left as it is.
213 A platform the matrix splits, or whose refs are shards', is proved
214 and folded (`_fold_platform`), its line printed; with no platform
215 sharded, one line says so. *folders* fold the files beside the
216 row, by file name; the listed extensions' answers to the
217 ``ci.fold`` query when absent.
218
219 Raises:
220 Failed: When the refs cannot be listed or read, when a
221 platform's shards are not one leg's work, naming the
222 platform, the shards and what differs, when a file nobody
223 folds is on a shard's ref, and when the folded ref cannot
224 be put; the shards' refs then stay for the next run.
225 """
226 store = state_store(root)
227 keys = RUNS.listed(store, run.run_id)
228 if keys is None:
229 fail(
230 f"{RUNS.prefix}{run.run_id}/*: the remote could not be listed; the fold"
231 " proves nothing from refs it cannot read"
232 )
233 refs = [_read_ref(store, key) for key in keys]
234 expected = expected_shards(root, run)
235 # Grouped by the platform's slug, since a ref without a row has no
236 # label but its key, which is one; named by the matrix's spelling
237 # or the first row's.
238 groups: dict[str, list[_Ref]] = defaultdict(list)
239 names: dict[str, str] = {git.slug(platform): platform for platform in expected}
240 for ref in refs:
241 platform = platform_leg(ref.label)
242 groups[git.slug(platform)].append(ref)
243 if ref.row is not None:
244 names.setdefault(git.slug(platform), platform)
245 if folders is None:245 ↛ 246line 245 didn't jump to line 246 because the condition on line 245 was never true
246 try:
247 folders = answer(root, FOLDERS)
248 except QueryError as error:
249 fail(str(error))
250 folded = 0
251 for key in sorted(set(groups) | set(names)):
252 platform = names.get(key, key)
253 held = groups.get(key, [])
254 shards = [ref for ref in held if ref.sharded]
255 wanted = expected.get(platform, ())
256 if not shards and not wanted and not any(ref.folded for ref in held):
257 continue
258 folded += 1
259 _fold_platform(store, run, platform, shards, held, wanted, folders)
260 if not folded:
261 print(" fold: no check leg ran as shards; nothing to fold")
262
263
264def _fold_platform((empty)
265 store: git.StateStore,
266 run: RunContext,
267 platform: str,
268 shards: list[_Ref],
269 held: list[_Ref],
270 wanted: tuple[str, ...],
271 folders: Mapping[str, Folder],
272) -> None:
273 """Prove *platform*'s *shards* are one leg's work, put the folded ref, drop theirs.
274
275 *held* is every ref of the platform, *wanted* the shard labels the
276 matrix produces for it. A folded ref already there with no shard
277 ref beside it is the fold's earlier work, said so and left; one
278 with shard refs beside it whose rows the fold stamped is a drop
279 that failed, and the leftovers go.
280 """
281 whole = [ref for ref in held if not ref.sharded]
282 already = [ref for ref in whole if ref.folded]
283 stray = [ref for ref in whole if not ref.folded]
284 if stray and (shards or wanted):284 ↛ 285line 284 didn't jump to line 285 because the condition on line 284 was never true
285 fail(
286 f"{platform}: {', '.join(ref.ref for ref in stray)} carries no shard"
287 " record while the platform runs as shards; a leg that did not say"
288 " what it partitioned proves nothing"
289 )
290 if already and not shards:
291 count = _recorded_count(already[0])
292 print(f" fold: {platform}: already folded, {count} shard(s)")
293 return
294 if not shards:
295 fail(
296 f"{platform}: the matrix splits it into {len(wanted)} shards"
297 f" ({', '.join(wanted)}) and none left a ref; a shard that did not"
298 " write proves nothing"
299 )
300 if already and all(_leftover(already[0], ref) for ref in shards):
301 for ref in shards:
302 _drop(store, ref.ref)
303 print(
304 f" fold: {platform}: already folded, {_recorded_count(already[0])}"
305 f" shard(s); {len(shards)} ref(s) a failed drop left behind dropped"
306 )
307 return
308 chosen, items, whole_set, unpartitioned = _prove(platform, shards, wanted)
309 row = _folded_row(platform, chosen, shards)
310 folded_files = _fold_files(platform, chosen, folders)
311 why = RUNS.series(run.run_id, platform).put(
312 store,
313 {ROW_FILE: row, **folded_files},
314 message=f"fold: {platform} of run {run.run_id}, {len(shards)} shard(s)",
315 )
316 if why:316 ↛ 317line 316 didn't jump to line 317 because the condition on line 316 was never true
317 fail(
318 f"{platform}: the folded ref could not be put ({why}); the shards' refs"
319 " stay for the next run of the fold"
320 )
321 for ref in shards:
322 _drop(store, ref.ref)
323 print(
324 f" fold: {platform}: {len(shards)} shard(s) folded, {len(items)} item(s) ran"
325 )
326 if whole_set:
327 print(
328 f" fold: {platform}: each shard ran the whole set as one shard of one;"
329 f" {chosen[0].label} stands for the platform"
330 )
331 if unpartitioned:
332 print(
333 f" fold: {platform}: no shard partitioned items ({unpartitioned});"
334 " folded without a partition"
335 )
336
337
338def _recorded_count(folded: _Ref) -> int:(empty)
339 shard: Any = (folded.row or {}).get("shard")
340 listed: Any = shard.get("shards") if isinstance(shard, dict) else None
341 return len(listed) if isinstance(listed, list) else 0
342
343
344def _leftover(folded: _Ref, ref: _Ref) -> bool:(empty)
345 """Whether *ref* is a shard's ref the fold that put *folded* already read.
346
347 The folded row records the stamp of each shard's row it folded; a
348 shard ref whose row carries that stamp is the same write, left
349 behind by a drop that failed, and a later write is a new one.
350 """
351 shard: Any = (folded.row or {}).get("shard")
352 listed: Any = shard.get("shards") if isinstance(shard, dict) else None
353 if not isinstance(listed, list):353 ↛ 354line 353 didn't jump to line 354 because the condition on line 353 was never true
354 return False
355 stamps = {
356 str(item.get("leg", "")): str(item.get("when", ""))
357 for item in listed
358 if isinstance(item, dict)
359 }
360 return bool(ref.stamp) and stamps.get(ref.label) == ref.stamp
361
362
363def _drop(store: git.StateStore, ref: str) -> None:(empty)
364 gone = git.drop(store, ref)
365 if gone:
366 print(f" fold: {ref}: not dropped ({gone}); the next fold drops it")
367
368
369def _scope_of(row: Mapping[str, Any]) -> tuple[str, tuple[str, ...], str]:(empty)
370 scope: Any = row.get("scope")
371 if not isinstance(scope, Mapping):371 ↛ 372line 371 didn't jump to line 372 because the condition on line 371 was never true
372 return "unknown", (), ""
373 packages: Any = scope.get("packages") or []
374 named = (
375 tuple(sorted(str(item) for item in packages))
376 if isinstance(packages, list)
377 else ()
378 )
379 return (
380 str(scope.get("scope", "unknown")),
381 named,
382 str(scope.get("base_tree", "") or ""),
383 )
384
385
386def _shown_scope(scope: tuple[str, tuple[str, ...], str]) -> str:(empty)
387 name, packages, base = scope
388 shown = name + (f" ({', '.join(packages)})" if packages else "")
389 return shown + (f" from {base[:12]}" if base else "")
390
391
392def _prove((empty)
393 platform: str, shards: list[_Ref], wanted: tuple[str, ...]
394) -> tuple[list[_Ref], set[str], bool, str]:
395 """Prove *shards* are one leg's work; what to fold, what ran, how partitioned.
396
397 Each check fails by name, in this order: a shard without a row, a
398 ref carrying a file that is not a row, a record that is not one, a
399 shard without a record beside one with, a record whose shard is not
400 the label's, a shard the matrix produces that left no ref (or,
401 without a matrix, an index of the records' count that did, or of
402 the labels' count when no shard left a record), the shards' scopes,
403 their item sets, their suites, their durations rows, their bounds,
404 and the items: an item in two shards, or a union whose digest is
405 not the set's. Where the partitions disagree and the shards'
406 records name two attempts of the run, the refusal names the
407 attempts instead (`_refuse_attempts`). A record of one shard of one
408 says the shard fell open on the record and ran the whole set; every
409 shard of the platform must have, and each one's items must be the
410 set, and then the first is folded alone.
411
412 Returns:
413 The shards to fold, the items they ran, whether each ran the
414 whole set, and the shards' one scope when no shard left a
415 record, so the shards partitioned nothing and the proof ends
416 at their scopes; empty when they partitioned.
417 """
418 shards = sorted(shards, key=_index)
419 names = ", ".join(ref.label for ref in shards)
420 without_row = [ref.ref for ref in shards if ref.row is None]
421 if without_row:
422 fail(
423 f"{platform}: {', '.join(without_row)} left no row; a shard that did"
424 " not write proves nothing"
425 )
426 for ref in shards:
427 if ref.skipped:427 ↛ 428line 427 didn't jump to line 428 because the condition on line 427 was never true
428 fail(
429 f"{platform}: {ref.ref} carries {', '.join(ref.skipped)}, which is"
430 " not a row; the fold cannot carry it"
431 )
432 for ref in shards:
433 if ref.why:
434 fail(f"{platform}: {ref.label}: {ref.why}")
435 without_record = [ref.label for ref in shards if ref.marker is None]
436 with_record = [ref.label for ref in shards if ref.marker is not None]
437 if without_record and with_record:
438 fail(
439 f"{platform}: {', '.join(without_record)} left no shard record"
440 f" ({SHARD_FILE} rides the leg's row) and {', '.join(with_record)} did;"
441 " a shard that did not say what it partitioned proves nothing"
442 )
443 markers = {ref.label: ref.marker for ref in shards if ref.marker is not None}
444 disagree = [
445 f"{label} ran as {marker.shard}"
446 for label, marker in markers.items()
447 if marker.shard not in (ONE, _label_shard(label))
448 ]
449 if disagree:
450 fail(
451 f"{platform}: {', '.join(disagree)}; a shard's label and its record"
452 " name one shard"
453 )
454 present = {git.slug(ref.label) for ref in shards}
455 if wanted:
456 missing = [label for label in wanted if git.slug(label) not in present]
457 if missing:
458 fail(
459 f"{platform}: {', '.join(missing)} left no ref ({len(present)} of"
460 f" {len(wanted)} shards did: {names}); a shard that did not write"
461 " proves nothing"
462 )
463 else:
464 # The records' counts, or the labels' when no shard left a
465 # record: either way every index of the count must be present.
466 counts = sorted(
467 {marker.shard.count for marker in markers.values() if marker.shard != ONE}
468 or {ref.shard.count for ref in shards if ref.shard != ONE}
469 )
470 if len(counts) > 1:470 ↛ 471line 470 didn't jump to line 471 because the condition on line 470 was never true
471 fail(
472 f"{platform}: the shards disagree on their count"
473 f" ({', '.join(str(count) for count in counts)}): {names}"
474 )
475 if counts:475 ↛ 487line 475 didn't jump to line 487 because the condition on line 475 was always true
476 spelled = [
477 f"{platform}-{Shard(index, counts[0])}"
478 for index in range(1, counts[0] + 1)
479 ]
480 missing = [label for label in spelled if git.slug(label) not in present]
481 if missing:
482 fail(
483 f"{platform}: {', '.join(missing)} left no ref ({len(present)}"
484 f" of {counts[0]} shards did: {names}); a shard that did not"
485 " write proves nothing"
486 )
487 scopes = {ref.label: _scope_of(ref.row or {}) for ref in shards}
488 if len(set(scopes.values())) > 1:
489 shown = ", ".join(
490 f"{label} {_shown_scope(scope)}" for label, scope in scopes.items()
491 )
492 fail(
493 f"{platform}: the shards name different scopes ({shown}); a platform's"
494 " shards run one gate"
495 )
496 if not markers:
497 # No shard partitioned: the gate skipped on every shard, or no
498 # step that reads the shard ran. One scope is the whole proof.
499 return shards, set(), False, _shown_scope(next(iter(scopes.values())))
500 digests = {label: marker.digest for label, marker in markers.items()}
501 if len(set(digests.values())) > 1:
502 _refuse_attempts(platform, markers)
503 shown = ", ".join(f"{label} {digest[:12]}" for label, digest in digests.items())
504 fail(
505 f"{platform}: the shards partitioned different item sets ({shown});"
506 " a platform's shards partition one set"
507 )
508 suites = {label: marker.suites for label, marker in markers.items()}
509 if len(set(suites.values())) > 1:
510 _refuse_attempts(platform, markers)
511 shown = ", ".join(
512 f"{label} {', '.join(named) if named else 'none'}"
513 for label, named in suites.items()
514 )
515 fail(
516 f"{platform}: the shards partitioned different suites ({shown}); a"
517 " platform's shards partition one set"
518 )
519 rows = {label: marker.rows for label, marker in markers.items()}
520 if len(set(rows.values())) > 1:
521 _refuse_attempts(platform, markers)
522 shown = ", ".join(
523 f"{label} {', '.join(read) if read else 'none'}"
524 for label, read in rows.items()
525 )
526 fail(
527 f"{platform}: the shards read different durations rows ({shown}); a"
528 " platform's shards partition by one row"
529 )
530 bounds = {label: marker.before for label, marker in markers.items()}
531 if len(set(bounds.values())) > 1:
532 _refuse_attempts(platform, markers)
533 shown = ", ".join(
534 f"{label} {before.isoformat()}" for label, before in bounds.items()
535 )
536 fail(
537 f"{platform}: the shards read the durations under different bounds"
538 f" ({shown}); every shard of a run reads before its checkout's time"
539 )
540 recorded = next(iter(digests.values()))
541 whole_set = [ref for ref in shards if markers[ref.label].shard == ONE]
542 if whole_set and len(whole_set) != len(shards):
543 sliced = [ref.label for ref in shards if ref not in whole_set]
544 fail(
545 f"{platform}: {', '.join(ref.label for ref in whole_set)} ran the whole"
546 f" set as one shard of one while {', '.join(sliced)} ran a slice; a"
547 " platform's shards ran one partition or none"
548 )
549 if whole_set:
550 for ref in whole_set:
551 own = files_digest(markers[ref.label].items)
552 if own != recorded:
553 fail(
554 f"{platform}: {ref.label} says it ran the whole set, and its"
555 f" items' digest {own[:12]} is not the set's {recorded[:12]}"
556 )
557 return [whole_set[0]], set(markers[whole_set[0].label].items), True, ""
558 seen: dict[str, str] = {}
559 twice: list[str] = []
560 for ref in shards:
561 for name in markers[ref.label].items:
562 if name in seen:
563 twice.append(f"{name} ({seen[name]} and {ref.label})")
564 else:
565 seen[name] = ref.label
566 if twice:
567 _refuse_attempts(platform, markers)
568 rest = f" and {len(twice) - NAMED} more" if len(twice) > NAMED else ""
569 fail(
570 f"{platform}: {len(twice)} item(s) ran in two shards:"
571 f" {', '.join(twice[:NAMED])}{rest}; every item lands in exactly one"
572 " shard"
573 )
574 together = files_digest(seen)
575 if together != recorded:
576 _refuse_attempts(platform, markers)
577 fail(
578 f"{platform}: the items the shards ran together have digest"
579 f" {together[:12]}, and the set they partitioned has {recorded[:12]}:"
580 " an item of the set ran in no shard"
581 )
582 return shards, set(seen), False, ""
583
584
585def _refuse_attempts(platform: str, markers: Mapping[str, ShardMarker]) -> None:(empty)
586 """Refuse naming the shards' attempts when they differ; return when they agree.
587
588 Called where the shards' partitions disagree. A rerun of some jobs
589 of a run keeps the per-run refs the first attempt's passing shards
590 left, and the shard that reruns partitions afresh from the record
591 as it then is, so the shards come from two attempts with two
592 partitions; the fold cannot reconcile them, and the recovery is a
593 rerun of the whole run (``fm ci.rerun``, which does that for a
594 sharded contract). Shards of one attempt, or of records that name
595 none, return to the refusal that names what differs. The line is
596 printed and is the refusal's reason.
597 """
598 attempts = {label: marker.attempt for label, marker in markers.items()}
599 if len(set(attempts.values())) < 2:
600 return
601 shown = ", ".join(
602 f"{label} attempt {attempt or 'unknown'}" for label, attempt in attempts.items()
603 )
604 reason = (
605 f"{platform}: its shards come from different attempts of the run ({shown});"
606 " a rerun of some jobs partitions afresh, so rerun the whole run"
607 f" ({footman.prog()} ci.rerun)"
608 )
609 print(f" fold: {reason}")
610 fail(reason)
611
612
613def _folded_row((empty)
614 platform: str, chosen: Sequence[_Ref], shards: Sequence[_Ref]
615) -> dict[str, Any]:
616 """The platform's one timing row from *chosen*'s rows, listing *shards*.
617
618 The first chosen shard's row stands, its tasks, steps, waits and
619 wall as they are; the package sums and the seconds per item are
620 every chosen shard's added, the slowest tests every chosen
621 shard's, slowest first; the row is named by the platform's display
622 name and keyed by the platform leg under ``scope.leg``; ``shard``
623 records the set's digest, its suites, the rows and the bound the
624 shards read, and each shard's label, job, row stamp, item count,
625 wall and package sums. Shards that left no record partitioned
626 nothing: the digest, the suites, the rows and the bound are empty.
627 """
628 first = chosen[0].row or {}
629 marker = chosen[0].marker
630 packages: dict[str, dict[str, float | int]] = defaultdict(
631 lambda: {"tests_ms": 0.0, "tests": 0}
632 )
633 per_item: dict[str, float] = defaultdict(float)
634 slowest: list[dict[str, Any]] = []
635 for ref in chosen:
636 row = ref.row or {}
637 held: Any = row.get("packages")
638 for name, data in held.items() if isinstance(held, dict) else ():
639 if not isinstance(data, dict):639 ↛ 640line 639 didn't jump to line 640 because the condition on line 639 was never true
640 continue
641 ms = data.get("tests_ms", 0.0)
642 tests = data.get("tests", 0)
643 packages[str(name)]["tests_ms"] = round(
644 float(packages[str(name)]["tests_ms"])
645 + (float(ms) if isinstance(ms, int | float) else 0.0),
646 1,
647 )
648 packages[str(name)]["tests"] = int(packages[str(name)]["tests"]) + (
649 tests if isinstance(tests, int) else 0
650 )
651 timed: Any = row.get("items")
652 for name, seconds in timed.items() if isinstance(timed, dict) else ():
653 if isinstance(seconds, int | float):653 ↛ 652line 653 didn't jump to line 652 because the condition on line 653 was always true
654 per_item[str(name)] = round(per_item[str(name)] + float(seconds), 1)
655 slow: Any = row.get("slowest")
656 if isinstance(slow, list):656 ↛ 635line 656 didn't jump to line 635 because the condition on line 656 was always true
657 slowest.extend(item for item in slow if isinstance(item, dict))
658 slowest.sort(key=_seconds)
659 scope: Any = first.get("scope")
660 folded = {
661 key: value for key, value in first.items() if key not in ("schema", "when")
662 }
663 folded.update(
664 {
665 "job": platform_display(str(first.get("job", ""))),
666 "packages": dict(sorted(packages.items())),
667 "items": dict(sorted(per_item.items())),
668 "slowest": slowest[:SLOWEST],
669 "scope": {**(scope if isinstance(scope, dict) else {}), "leg": platform},
670 "shard": {
671 "count": len(shards),
672 "digest": marker.digest if marker is not None else "",
673 "suites": list(marker.suites) if marker is not None else [],
674 "rows": list(marker.rows) if marker is not None else [],
675 "before": marker.before.isoformat() if marker is not None else "",
676 "shards": [
677 {
678 "shard": str(ref.shard),
679 "leg": ref.label,
680 "job": str((ref.row or {}).get("job", "")),
681 "when": ref.stamp,
682 "items": len(ref.marker.items) if ref.marker else 0,
683 "total_ms": (ref.row or {}).get("total_ms"),
684 "packages": (ref.row or {}).get("packages", {}),
685 }
686 for ref in sorted(shards, key=_index)
687 ],
688 },
689 }
690 )
691 return folded
692
693
694def _fold_files((empty)
695 platform: str, chosen: Sequence[_Ref], folders: Mapping[str, Folder]
696) -> dict[str, Mapping[str, Any]]:
697 """Fold every file beside the row through its folder; red by name when one cannot.
698
699 Every chosen shard left the same file names, since a leg's entries
700 run alike on every shard; a name some left and some did not fails
701 naming both sides. A name nobody folds, and a folder that refuses,
702 fail naming the platform and the file.
703 """
704 names = sorted({name for ref in chosen for name in ref.files})
705 folded: dict[str, Mapping[str, Any]] = {}
706 for name in names:
707 lacking = [ref.label for ref in chosen if name not in ref.files]
708 if lacking:
709 having = [ref.label for ref in chosen if name in ref.files]
710 fail(
711 f"{platform}: {name} is on {', '.join(having)} and not on"
712 f" {', '.join(lacking)}; a leg's entries leave the same files on"
713 " every shard"
714 )
715 folder = folders.get(name)
716 if folder is None:
717 fail(
718 f"{platform}: nobody folds {name}: an extension answers the ci.fold"
719 " query with a folder for each file its leg entry leaves"
720 )
721 shards_files = [
722 {
723 key: value
724 for key, value in ref.files[name].items()
725 if key not in ("schema", "when")
726 }
727 for ref in chosen
728 ]
729 try:
730 folded[name] = folder(shards_files)
731 except ValueError as error:
732 fail(f"{platform}: {name} does not fold ({error})")
733 return folded