plotroom 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. plotroom/__init__.py +0 -0
  2. plotroom/__main__.py +8 -0
  3. plotroom/_secret_file.py +8 -0
  4. plotroom/adapters/__init__.py +0 -0
  5. plotroom/adapters/claude_desktop.py +219 -0
  6. plotroom/adapters/embedding_http.py +175 -0
  7. plotroom/adapters/github/__init__.py +337 -0
  8. plotroom/adapters/github/app_credential.py +209 -0
  9. plotroom/adapters/github/ci.py +738 -0
  10. plotroom/adapters/github/commit_statuses.py +63 -0
  11. plotroom/adapters/github/deployments.py +213 -0
  12. plotroom/adapters/github/details.py +867 -0
  13. plotroom/adapters/github/issue_details.py +368 -0
  14. plotroom/adapters/github/junit.py +210 -0
  15. plotroom/adapters/github/junit_xml.py +162 -0
  16. plotroom/adapters/github/kodus_review.py +347 -0
  17. plotroom/adapters/github/pr_details.py +192 -0
  18. plotroom/adapters/github/pull_comparison.py +108 -0
  19. plotroom/adapters/github/read.py +530 -0
  20. plotroom/adapters/github/ticket_tree.py +336 -0
  21. plotroom/adapters/mcp/__init__.py +5 -0
  22. plotroom/adapters/mcp/proxy.py +722 -0
  23. plotroom/adapters/mcp/server.py +931 -0
  24. plotroom/adapters/openrouter.py +151 -0
  25. plotroom/adapters/store_sqlite/__init__.py +6 -0
  26. plotroom/adapters/store_sqlite/actions_sql.py +148 -0
  27. plotroom/adapters/store_sqlite/asks_sql.py +153 -0
  28. plotroom/adapters/store_sqlite/backfill_sql.py +138 -0
  29. plotroom/adapters/store_sqlite/budgets_sql.py +102 -0
  30. plotroom/adapters/store_sqlite/checklists_sql.py +77 -0
  31. plotroom/adapters/store_sqlite/claims_sql.py +54 -0
  32. plotroom/adapters/store_sqlite/embedding_sql.py +275 -0
  33. plotroom/adapters/store_sqlite/github_sql.py +331 -0
  34. plotroom/adapters/store_sqlite/goals_sql.py +57 -0
  35. plotroom/adapters/store_sqlite/human_queue_sql.py +60 -0
  36. plotroom/adapters/store_sqlite/items_sql.py +129 -0
  37. plotroom/adapters/store_sqlite/lanes_sql.py +85 -0
  38. plotroom/adapters/store_sqlite/ledger_sql.py +506 -0
  39. plotroom/adapters/store_sqlite/migrate.py +194 -0
  40. plotroom/adapters/store_sqlite/migrations/env.py +36 -0
  41. plotroom/adapters/store_sqlite/migrations/script.py.mako +19 -0
  42. plotroom/adapters/store_sqlite/migrations/versions/0001_project_meta.py +23 -0
  43. plotroom/adapters/store_sqlite/migrations/versions/0002_event_log.py +40 -0
  44. plotroom/adapters/store_sqlite/migrations/versions/0003_next_items.py +49 -0
  45. plotroom/adapters/store_sqlite/migrations/versions/0004_actions.py +28 -0
  46. plotroom/adapters/store_sqlite/migrations/versions/0005_lanes.py +36 -0
  47. plotroom/adapters/store_sqlite/migrations/versions/0006_work_items.py +22 -0
  48. plotroom/adapters/store_sqlite/migrations/versions/0007_item_changes.py +33 -0
  49. plotroom/adapters/store_sqlite/migrations/versions/0008_claude_turn_aliases.py +30 -0
  50. plotroom/adapters/store_sqlite/migrations/versions/0009_github_projection.py +38 -0
  51. plotroom/adapters/store_sqlite/migrations/versions/0010_github_pull_freshness.py +37 -0
  52. plotroom/adapters/store_sqlite/migrations/versions/0011_github_resources.py +27 -0
  53. plotroom/adapters/store_sqlite/migrations/versions/0012_local_triggers_outbox.py +80 -0
  54. plotroom/adapters/store_sqlite/migrations/versions/0013_lane_claims.py +22 -0
  55. plotroom/adapters/store_sqlite/migrations/versions/0015_recurring_triggers.py +45 -0
  56. plotroom/adapters/store_sqlite/migrations/versions/0016_merge_controller.py +28 -0
  57. plotroom/adapters/store_sqlite/migrations/versions/0017_lane_checklists.py +36 -0
  58. plotroom/adapters/store_sqlite/migrations/versions/0018_guardrails.py +33 -0
  59. plotroom/adapters/store_sqlite/migrations/versions/0019_clerk_import_metadata.py +16 -0
  60. plotroom/adapters/store_sqlite/migrations/versions/0020_search.py +30 -0
  61. plotroom/adapters/store_sqlite/migrations/versions/0021_human_queue.py +46 -0
  62. plotroom/adapters/store_sqlite/migrations/versions/0022_search_records.py +77 -0
  63. plotroom/adapters/store_sqlite/migrations/versions/0023_operator_goals.py +25 -0
  64. plotroom/adapters/store_sqlite/migrations/versions/0024_embedding_rejections.py +36 -0
  65. plotroom/adapters/store_sqlite/outbox_sql.py +78 -0
  66. plotroom/adapters/store_sqlite/routes_sql.py +26 -0
  67. plotroom/adapters/store_sqlite/search_sql.py +116 -0
  68. plotroom/adapters/store_sqlite/store.py +623 -0
  69. plotroom/adapters/store_sqlite/triggers_sql.py +111 -0
  70. plotroom/adapters/store_sqlite/work_items_sql.py +36 -0
  71. plotroom/adapters/switchboard.py +152 -0
  72. plotroom/adapters/usage_fixture.py +11 -0
  73. plotroom/adapters/work_items_bmad.py +256 -0
  74. plotroom/adapters/work_items_github_issues.py +323 -0
  75. plotroom/app/__init__.py +0 -0
  76. plotroom/app/bridge.py +162 -0
  77. plotroom/app/clerk_import.py +349 -0
  78. plotroom/app/cli.py +290 -0
  79. plotroom/app/clock.py +12 -0
  80. plotroom/app/config.py +984 -0
  81. plotroom/app/daemon_start.py +453 -0
  82. plotroom/app/lock.py +77 -0
  83. plotroom/app/logging.py +93 -0
  84. plotroom/app/portfile.py +218 -0
  85. plotroom/app/runtime.py +518 -0
  86. plotroom/app/serve.py +671 -0
  87. plotroom/app/session_intake.py +51 -0
  88. plotroom/app/sqlite_floor.py +35 -0
  89. plotroom/app/tokens.py +265 -0
  90. plotroom/core/__init__.py +0 -0
  91. plotroom/core/actions.py +67 -0
  92. plotroom/core/ask_checks.py +124 -0
  93. plotroom/core/asks.py +123 -0
  94. plotroom/core/branch_updates.py +207 -0
  95. plotroom/core/bridge.py +112 -0
  96. plotroom/core/briefs.py +83 -0
  97. plotroom/core/budgets.py +182 -0
  98. plotroom/core/catalog.py +464 -0
  99. plotroom/core/checklists.py +151 -0
  100. plotroom/core/claims.py +296 -0
  101. plotroom/core/clerk_import.py +379 -0
  102. plotroom/core/commands.py +85 -0
  103. plotroom/core/deploy_window.py +21 -0
  104. plotroom/core/envelope.py +20 -0
  105. plotroom/core/events.py +227 -0
  106. plotroom/core/github.py +306 -0
  107. plotroom/core/github_projection.py +310 -0
  108. plotroom/core/goal_language.py +374 -0
  109. plotroom/core/goal_projection.py +146 -0
  110. plotroom/core/human_queue.py +119 -0
  111. plotroom/core/intake.py +106 -0
  112. plotroom/core/kodus_review.py +53 -0
  113. plotroom/core/lanes.py +281 -0
  114. plotroom/core/ledger.py +808 -0
  115. plotroom/core/merge_controller.py +344 -0
  116. plotroom/core/merge_flakes.py +422 -0
  117. plotroom/core/merge_gate.py +724 -0
  118. plotroom/core/merge_lane.py +70 -0
  119. plotroom/core/merge_overlap.py +76 -0
  120. plotroom/core/next_items.py +60 -0
  121. plotroom/core/operator_goals.py +103 -0
  122. plotroom/core/outbox.py +70 -0
  123. plotroom/core/pull_overlaps.py +152 -0
  124. plotroom/core/reports.py +62 -0
  125. plotroom/core/roles.py +100 -0
  126. plotroom/core/routing.py +55 -0
  127. plotroom/core/run_reports.py +53 -0
  128. plotroom/core/search.py +266 -0
  129. plotroom/core/search_sources.py +94 -0
  130. plotroom/core/snapshot.py +204 -0
  131. plotroom/core/trigger_actions.py +89 -0
  132. plotroom/core/triggers.py +550 -0
  133. plotroom/core/unit_of_work.py +541 -0
  134. plotroom/core/watchers.py +35 -0
  135. plotroom/core/work_items.py +23 -0
  136. plotroom/core/writer.py +164 -0
  137. plotroom/ports/__init__.py +6 -0
  138. plotroom/ports/clock.py +12 -0
  139. plotroom/ports/github_write.py +72 -0
  140. plotroom/ports/search.py +197 -0
  141. plotroom/ports/session_reader.py +23 -0
  142. plotroom/ports/store.py +323 -0
  143. plotroom/ports/usage_meter.py +35 -0
  144. plotroom/ports/work_items.py +22 -0
  145. plotroom/py.typed +0 -0
  146. plotroom-0.1.0.dist-info/METADATA +230 -0
  147. plotroom-0.1.0.dist-info/RECORD +150 -0
  148. plotroom-0.1.0.dist-info/WHEEL +4 -0
  149. plotroom-0.1.0.dist-info/entry_points.txt +2 -0
  150. plotroom-0.1.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,422 @@
1
+ """Derive durable flake candidates from complete, read-only GitHub evidence."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import hashlib
6
+ import json
7
+ from collections import Counter
8
+ from collections.abc import Iterable
9
+ from dataclasses import dataclass
10
+ from typing import TYPE_CHECKING, Any, Protocol
11
+
12
+ from plotroom.core.events import Event, FieldSpec, NewEvent
13
+ from plotroom.core.github_projection import GitHubRecord, GitHubResource
14
+
15
+ if TYPE_CHECKING:
16
+ from plotroom.core.github import PullRequest
17
+ from plotroom.core.unit_of_work import UnitOfWork
18
+
19
+
20
+ FLAKE_CANDIDATE = "flake_candidate"
21
+ FLAKE_EVENT = "core.flake_candidate"
22
+ MAIN_FLAKE_EVENT = "core.main_check_flake"
23
+ FLAKE_COMPARISONS = "flake_comparisons"
24
+ SOURCE = "core"
25
+ FAILURES = {"failure", "timed_out"}
26
+ MAX_MAIN_RUNS = 12
27
+
28
+ FAILURE_FIELDS = (
29
+ FieldSpec("repository", "str"),
30
+ FieldSpec("head_sha", "str"),
31
+ FieldSpec("check_id", "int"),
32
+ )
33
+ FLAKE_FIELDS = (
34
+ *FAILURE_FIELDS,
35
+ FieldSpec("check_name", "str"),
36
+ FieldSpec("run_url", "str", required=False),
37
+ FieldSpec("pattern", "str"),
38
+ FieldSpec("number", "int", required=False),
39
+ FieldSpec("test_id", "str", required=False),
40
+ FieldSpec("path", "str", required=False),
41
+ FieldSpec("success_head_sha", "str", required=False),
42
+ FieldSpec("success_check_id", "int", required=False),
43
+ )
44
+
45
+
46
+ class EventLookup(Protocol):
47
+ async def find_event(self, source: str, kind: str, upstream_id: str) -> Event | None: ...
48
+
49
+
50
+ def is_failure(record: GitHubRecord) -> bool:
51
+ return record.fields["status"] == "completed" and record.fields.get("conclusion") in FAILURES
52
+
53
+
54
+ def _is_success(record: GitHubRecord) -> bool:
55
+ return record.fields["status"] == "completed" and record.fields.get("conclusion") == "success"
56
+
57
+
58
+ def _failure_key(repository: str, sha: str, check_id: int) -> str:
59
+ return f"{repository}:{sha}:{check_id}"
60
+
61
+
62
+ async def main_check_is_flake(
63
+ view: EventLookup, repository: str, head_sha: str, check_id: int
64
+ ) -> bool:
65
+ return (
66
+ await view.find_event(
67
+ SOURCE, MAIN_FLAKE_EVENT, _failure_key(repository, head_sha, check_id)
68
+ )
69
+ is not None
70
+ )
71
+
72
+
73
+ def prior_check_failures(checks: GitHubResource) -> tuple[GitHubRecord, ...]:
74
+ return tuple(
75
+ GitHubRecord(key, "check_run", row["fields"], details=row["details"])
76
+ for key, row in checks.attributes.get("prior_failures", {}).items()
77
+ )
78
+
79
+
80
+ def _same_check(failure: GitHubRecord, success: GitHubRecord) -> bool:
81
+ app = failure.details.get("app_id")
82
+ suite = failure.details.get("suite_id")
83
+ same_head = failure.fields["head_sha"] == success.fields["head_sha"]
84
+ prior: dict[str, int] = success.details.get("junit_prior_identity", {}) if same_head else {}
85
+ return failure.fields["name"] == success.fields["name"] and (
86
+ failure.key == success.key
87
+ or (
88
+ app is not None
89
+ and app == success.details.get("app_id", prior.get("app_id"))
90
+ and (
91
+ not same_head
92
+ or (
93
+ suite is not None
94
+ and suite == success.details.get("suite_id", prior.get("suite_id"))
95
+ )
96
+ )
97
+ )
98
+ )
99
+
100
+
101
+ def _later_check(failure: GitHubRecord, success: GitHubRecord) -> bool:
102
+ earlier = failure.details.get("completed_at")
103
+ later = success.details.get("completed_at")
104
+ if earlier is not None and later is not None:
105
+ return later > earlier or (later == earlier and success.fields["id"] > failure.fields["id"])
106
+ # An identical check ID in the durable prior-failure history also proves an observed
107
+ # failure-to-success transition. Distinct runs need completion times to prove order.
108
+ return success.key == failure.key
109
+
110
+
111
+ def same_head_recovery(failure: GitHubRecord, success: GitHubRecord) -> bool:
112
+ return (
113
+ failure.fields["head_sha"] == success.fields["head_sha"]
114
+ and _same_check(failure, success)
115
+ and _later_check(failure, success)
116
+ )
117
+
118
+
119
+ def _junit_unresolved(check: GitHubRecord) -> bool:
120
+ return check.details.get("junit_state") in (
121
+ "deferred",
122
+ "retry_deferred",
123
+ "unavailable",
124
+ )
125
+
126
+
127
+ def _junit_recovery_evidence(check: GitHubRecord) -> bool:
128
+ return check.details.get("junit_evidence") is True or _junit_unresolved(check)
129
+
130
+
131
+ def _same_head_requires_test_pass(failure: GitHubRecord, successes: Iterable[GitHubRecord]) -> bool:
132
+ return _junit_recovery_evidence(failure) or any(
133
+ _junit_recovery_evidence(success) and same_head_recovery(failure, success)
134
+ for success in successes
135
+ )
136
+
137
+
138
+ def has_test_identity(path: str, test_id: str) -> bool:
139
+ return bool(path and test_id) and all(part not in {"", ".", ".."} for part in path.split("/"))
140
+
141
+
142
+ @dataclass(frozen=True)
143
+ class Candidate:
144
+ failure: GitHubRecord
145
+ pattern: str
146
+ evidence: dict[str, str] | None
147
+ success: GitHubRecord | None = None
148
+ number: int | None = None
149
+
150
+
151
+ def _candidates(
152
+ failure: GitHubRecord,
153
+ success: GitHubRecord | None,
154
+ *,
155
+ number: int | None = None,
156
+ changed_paths: set[str] | None = None,
157
+ require_test_pass: bool = False,
158
+ ) -> tuple[tuple[Candidate, ...], bool]:
159
+ annotations = failure.details.get("annotations")
160
+ if annotations is None:
161
+ return (), False
162
+ same_head = success is not None and failure.fields["head_sha"] == success.fields["head_sha"]
163
+ if same_head:
164
+ pattern = "same_head_pass"
165
+ elif changed_paths is not None:
166
+ pattern = "pr_unchanged_test" if number is not None else "main_unchanged_test_pass"
167
+ else:
168
+ return (), False
169
+ if not annotations:
170
+ if (
171
+ success is None
172
+ or not same_head
173
+ or _junit_unresolved(success)
174
+ or not failure.details.get("run_url")
175
+ ):
176
+ return (), False
177
+ return (Candidate(failure, pattern, None, success, number),), True
178
+ passed_tests = (
179
+ {(test["path"], test["test_id"]) for test in success.details.get("passed_tests", [])}
180
+ if success is not None
181
+ and (
182
+ not same_head
183
+ or require_test_pass
184
+ or _junit_recovery_evidence(failure)
185
+ or _junit_recovery_evidence(success)
186
+ )
187
+ else None
188
+ )
189
+ result: list[Candidate] = []
190
+ for annotation in annotations:
191
+ path = annotation["path"]
192
+ if not has_test_identity(path, annotation["test_id"]):
193
+ continue
194
+ if not same_head and (changed_paths is None or path in changed_paths):
195
+ continue
196
+ if passed_tests is not None and (path, annotation["test_id"]) not in passed_tests:
197
+ continue
198
+ result.append(Candidate(failure, pattern, annotation, success, number))
199
+ return tuple(result), len(result) == len(annotations)
200
+
201
+
202
+ async def _record_candidates(
203
+ uow: UnitOfWork, repository: str, candidates: tuple[Candidate, ...]
204
+ ) -> None:
205
+ for candidate in candidates:
206
+ failure = candidate.failure
207
+ head, check_id = failure.fields["head_sha"], failure.fields["id"]
208
+ fields: dict[str, Any] = {
209
+ "repository": repository,
210
+ "head_sha": head,
211
+ "check_id": check_id,
212
+ "check_name": failure.fields["name"],
213
+ "pattern": candidate.pattern,
214
+ }
215
+ if url := failure.details.get("run_url"):
216
+ fields["run_url"] = url
217
+ identity = _failure_key(repository, head, check_id)
218
+ excerpt = ""
219
+ if candidate.evidence is not None:
220
+ # A check-level candidate already represents this failure, including after ack.
221
+ if await uow.find_event(SOURCE, FLAKE_EVENT, identity) is not None:
222
+ continue
223
+ path, test_id = candidate.evidence["path"], candidate.evidence["test_id"]
224
+ test_key = hashlib.sha256(json.dumps([path, test_id]).encode()).hexdigest()
225
+ identity = f"{identity}:{test_key}"
226
+ fields.update(path=path, test_id=test_id)
227
+ excerpt = candidate.evidence["excerpt"]
228
+ if candidate.number is not None:
229
+ fields["number"] = candidate.number
230
+ if candidate.success is not None:
231
+ fields.update(
232
+ success_head_sha=candidate.success.fields["head_sha"],
233
+ success_check_id=candidate.success.fields["id"],
234
+ )
235
+ recorded = await uow.append(NewEvent(SOURCE, FLAKE_EVENT, identity, fields, excerpt))
236
+ if recorded.created:
237
+ await uow.raise_item(FLAKE_CANDIDATE, identity, "clerk", {**fields, "excerpt": excerpt})
238
+
239
+
240
+ async def record_same_head(uow: UnitOfWork, repository: str, checks: GitHubResource) -> None:
241
+ if not checks.complete:
242
+ return
243
+ successes = [check for check in checks.records if _is_success(check)]
244
+ for failure in prior_check_failures(checks):
245
+ require_test_pass = _same_head_requires_test_pass(failure, successes)
246
+ for success in successes:
247
+ if not same_head_recovery(failure, success):
248
+ continue
249
+ candidates, fully_proven = _candidates(
250
+ failure, success, require_test_pass=require_test_pass
251
+ )
252
+ await _record_candidates(uow, repository, candidates)
253
+ if fully_proven:
254
+ break
255
+
256
+
257
+ async def record_pull_flakes(
258
+ uow: UnitOfWork,
259
+ repository: str,
260
+ pull: PullRequest,
261
+ checks: GitHubResource,
262
+ files: GitHubResource,
263
+ ) -> None:
264
+ if not checks.complete or not files.complete:
265
+ return
266
+ current = await uow.pull_request(repository, pull.number)
267
+ if (
268
+ current is None
269
+ or not current.complete
270
+ or current.state != "open"
271
+ or (current.head_sha, current.version, current.updated_at)
272
+ != (pull.head_sha, pull.version, pull.updated_at)
273
+ or (current.head_sha, current.version, current.updated_at)
274
+ != (
275
+ files.attributes.get("head_sha"),
276
+ files.attributes.get("pull_version"),
277
+ files.attributes.get("pull_updated_at"),
278
+ )
279
+ or not files.attributes.get("base_ref")
280
+ or not files.attributes.get("base_sha")
281
+ ):
282
+ return
283
+ paths: set[str] = set()
284
+ for record in files.records:
285
+ row = record.fields
286
+ if row["status"] == "renamed" and not row.get("previous_path"):
287
+ return
288
+ paths.update(row[key] for key in ("path", "previous_path") if key in row)
289
+ for failure in checks.records:
290
+ if is_failure(failure) and failure.fields["head_sha"] == pull.head_sha:
291
+ candidates, _ = _candidates(failure, None, number=pull.number, changed_paths=paths)
292
+ await _record_candidates(uow, repository, candidates)
293
+
294
+
295
+ def main_runs(
296
+ main: GitHubResource | None, *, limit: int = MAX_MAIN_RUNS
297
+ ) -> tuple[GitHubRecord, ...]:
298
+ if main is None or not main.complete:
299
+ return ()
300
+ runs = tuple(
301
+ run
302
+ for run in main.records
303
+ if run.details.get("main_branch_run") is True
304
+ and "workflow_id" in run.details
305
+ and run.fields["status"] == "completed"
306
+ and any("check_run_id" in job for job in run.fields["jobs"])
307
+ )[:limit]
308
+ workflows_with_failed_jobs = {
309
+ run.details["workflow_id"] for run in runs if _unique_job_links(run, FAILURES)
310
+ }
311
+ return tuple(run for run in runs if run.details["workflow_id"] in workflows_with_failed_jobs)
312
+
313
+
314
+ def _unique_job_links(run: GitHubRecord, conclusions: set[str]) -> dict[str, int]:
315
+ jobs: list[dict[str, Any]] = run.fields["jobs"]
316
+ counts = Counter(job["name"] for job in jobs)
317
+ return {
318
+ job["name"]: job["check_run_id"]
319
+ for job in jobs
320
+ if counts[job["name"]] == 1
321
+ and "check_run_id" in job
322
+ and job["status"] == "completed"
323
+ and job.get("conclusion") in conclusions
324
+ }
325
+
326
+
327
+ def main_recovery_check_ids(runs: tuple[GitHubRecord, ...]) -> dict[str, set[int]]:
328
+ """Find successful main jobs with an earlier failed job in the same workflow."""
329
+ earlier_failures: dict[int, set[str]] = {}
330
+ recoveries: dict[str, set[int]] = {}
331
+ for run in reversed(runs):
332
+ names = earlier_failures.setdefault(run.details["workflow_id"], set())
333
+ matching = {
334
+ identifier
335
+ for name, identifier in _unique_job_links(run, {"success"}).items()
336
+ if name in names
337
+ }
338
+ if matching:
339
+ recoveries.setdefault(run.fields["head_sha"], set()).update(matching)
340
+ names.update(_unique_job_links(run, FAILURES))
341
+ return recoveries
342
+
343
+
344
+ @dataclass(frozen=True)
345
+ class MainPair:
346
+ failure: GitHubRecord
347
+ success: GitHubRecord
348
+ require_test_pass: bool = False
349
+
350
+ @property
351
+ def base(self) -> str:
352
+ return self.failure.fields["head_sha"]
353
+
354
+ @property
355
+ def head(self) -> str:
356
+ return self.success.fields["head_sha"]
357
+
358
+
359
+ def main_pairs(
360
+ runs: tuple[GitHubRecord, ...], checks: dict[str, GitHubResource]
361
+ ) -> tuple[MainPair, ...]:
362
+ pairs: list[MainPair] = []
363
+ for index, run in enumerate(runs):
364
+ observed = checks.get(run.fields["head_sha"])
365
+ if observed is None or not observed.complete:
366
+ continue
367
+ failures = {row.fields["id"]: row for row in prior_check_failures(observed)}
368
+ failed_jobs = _unique_job_links(run, FAILURES)
369
+ for newer in runs[:index]:
370
+ if newer.details["workflow_id"] != run.details["workflow_id"]:
371
+ continue
372
+ passed = checks.get(newer.fields["head_sha"])
373
+ if passed is None or not passed.complete:
374
+ continue
375
+ successes = {
376
+ check.fields["id"]: check for check in passed.records if _is_success(check)
377
+ }
378
+ passed_jobs = _unique_job_links(newer, {"success"})
379
+ for name, identifier in failed_jobs.items():
380
+ failure = failures.get(identifier)
381
+ success = successes.get(passed_jobs.get(name))
382
+ if failure is None or success is None:
383
+ continue
384
+ if run.fields["head_sha"] != newer.fields["head_sha"] and not failure.details.get(
385
+ "annotations"
386
+ ):
387
+ continue
388
+ if _same_check(failure, success) and _later_check(failure, success):
389
+ require_test_pass = failure.fields["head_sha"] == success.fields[
390
+ "head_sha"
391
+ ] and _same_head_requires_test_pass(failure, successes.values())
392
+ pairs.append(MainPair(failure, success, require_test_pass))
393
+ return tuple(pairs)
394
+
395
+
396
+ async def record_main_flakes(
397
+ uow: UnitOfWork, repository: str, pair: MainPair, comparison: GitHubResource | None
398
+ ) -> None:
399
+ paths: set[str] | None = None
400
+ if pair.base != pair.head:
401
+ if comparison is None or not comparison.complete:
402
+ return
403
+ paths = set(comparison.attributes["paths"])
404
+ candidates, fully_proven = _candidates(
405
+ pair.failure,
406
+ pair.success,
407
+ changed_paths=paths,
408
+ require_test_pass=pair.require_test_pass,
409
+ )
410
+ await _record_candidates(uow, repository, candidates)
411
+ if fully_proven:
412
+ check_id = pair.failure.fields["id"]
413
+ proof = await uow.append(
414
+ NewEvent(
415
+ SOURCE,
416
+ MAIN_FLAKE_EVENT,
417
+ _failure_key(repository, pair.base, check_id),
418
+ {"repository": repository, "head_sha": pair.base, "check_id": check_id},
419
+ )
420
+ )
421
+ if proof.created:
422
+ uow.all_merge_pulls = True