-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgit_bayesect.py
More file actions
1191 lines (956 loc) · 39.7 KB
/
git_bayesect.py
File metadata and controls
1191 lines (956 loc) · 39.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "numpy",
# "scipy",
# ]
# ///
from __future__ import annotations
import argparse
import enum
import json
import subprocess
import sys
from pathlib import Path
from typing import Any
import numpy as np
ndarray = np.ndarray[Any, Any]
# ==============================
# Core pure logic
# ==============================
class Bisector:
"""
There is some index B such that for all index:
P(obs_yes | index <= B) = p_obs_new
P(obs_yes | index > B) = p_obs_old
We'd like to find B (and we don't know p_obs_new and p_obs_old).
"""
def __init__(
self,
prior_weights: list[float] | list[int] | ndarray,
# WARNING: Moving the Beta priors away from the Jeffreys prior can cause overconfidence issues
alpha_new: float = 0.5,
beta_new: float = 0.5,
alpha_old: float = 0.5,
beta_old: float = 0.5,
) -> None:
if isinstance(prior_weights, list):
prior_weights = np.array(prior_weights, dtype=np.float64)
assert isinstance(prior_weights, np.ndarray)
if np.any(prior_weights < 0):
raise ValueError("prior_weights must be non-negative")
self.prior_weights = prior_weights
self.obs_yes = np.zeros_like(prior_weights, dtype=np.int64)
self.obs_total = np.zeros_like(prior_weights, dtype=np.int64)
# E.g. p_obs_new ~ Beta(0.9, 0.1), so E[p_obs_new] = 0.9
self.alpha_new = alpha_new
self.beta_new = beta_new
# E.g. p_obs_old ~ Beta(0.05, 0.95), so E[p_obs_old] = 0.05
self.alpha_old = alpha_old
self.beta_old = beta_old
self._post_weights: ndarray | None = None
def _maybe_update_posteriors(self) -> None:
if self._post_weights is None:
self._update_posteriors()
def _update_posteriors(self) -> None:
from scipy.special import loggamma, logsumexp
# fmt: off
# left: yes and no counts on or before index
# right: yes and no counts after index
total_left = self.obs_total
total_right = self.obs_total[-1] - total_left
yes_left = self.obs_yes
yes_right = yes_left[-1] - yes_left
no_left = total_left - yes_left
no_right = total_right - yes_right
# At this point, if we knew p_obs_new and p_obs_old, we could just apply Bayes' theorem
# and things would be straightforward. But we don't, so we have to integrate over our
# priors of what p_obs_new and p_obs_old might be.
# P(data) = ∫ P(data | p) P(p) dp for left and right observations
# Thanks to Beta distribution magic, we can compute this analytically
log_beta = lambda a, b: loggamma(a) + loggamma(b) - loggamma(a + b)
log_likelihood_left = (
log_beta(self.alpha_new + yes_left, self.beta_new + no_left)
- log_beta(self.alpha_new, self.beta_new)
)
log_likelihood_right = (
log_beta(self.alpha_old + yes_right, self.beta_old + no_right)
- log_beta(self.alpha_old, self.beta_old)
)
# This gives us:
# log P(data | index=b) = log_likelihood_left[b] + log_likelihood_right[b]
log_prior = np.where(self.prior_weights > 0, np.log(self.prior_weights), -np.inf)
# log_post[b] is now numerator of Bayes' theorem, so just normalise by sum(exp(log_post))
log_post = log_prior + log_likelihood_left + log_likelihood_right
self._post_weights = np.exp(log_post - logsumexp(log_post))
# fmt: on
def record(self, index: int, observation: bool | None) -> None:
"""Record an observation at index."""
assert 0 <= index < len(self.prior_weights)
self._post_weights = None
if observation is None:
# Similar to git bisect skip, let's just zero out the prior
# Note we might want to lower the prior instead
self.prior_weights[index] = 0
return
self.obs_total[index:] += 1
if observation:
self.obs_yes[index:] += 1
def select(self) -> int:
"""Return the index which will most reduce entropy."""
self._maybe_update_posteriors()
assert self._post_weights is not None
# fmt: off
total_left = self.obs_total
total_right = self.obs_total[-1] - total_left
yes_left = self.obs_yes
yes_right = yes_left[-1] - yes_left
# posterior means of the two Bernoulli parameters at each b
p_obs_new = (self.alpha_new + yes_left) / (self.alpha_new + self.beta_new + total_left)
p_obs_old = (self.alpha_old + yes_right) / (self.alpha_old + self.beta_old + total_right)
# p_obs_new = yes_left / np.maximum(1e-10, total_left)
# p_obs_old = yes_right / np.maximum(1e-10, total_right)
# p_obs_yes[b]
# = P(obs_yes | select=b)
# = \sum_{i=0}^{b-1} p_obs_old[i] * post[i] + \sum_{i=b}^{n-1} p_obs_new[i] * post[i]
w_new_yes = self._post_weights * p_obs_new
w_old_yes = self._post_weights * p_obs_old
p_obs_yes = (np.cumsum(w_old_yes) - w_old_yes) + np.cumsum(w_new_yes[::-1])[::-1]
w_new_no = self._post_weights * (1.0 - p_obs_new)
w_old_no = self._post_weights * (1.0 - p_obs_old)
p_obs_no = (np.cumsum(w_old_no) - w_old_no) + np.cumsum(w_new_no[::-1])[::-1]
assert np.allclose(p_obs_yes + p_obs_no, 1)
wlog = lambda w: np.where(w > 0.0, w * np.log2(w), 0.0)
# To get entropy from unnormalised w_i, calculate S = \sum w_i
# Then log S - (\sum w_i log w_i) / S
w_new_yes_log = wlog(w_new_yes)
w_old_yes_log = wlog(w_old_yes)
p_obs_yes_log = (np.cumsum(w_old_yes_log) - w_old_yes_log) + np.cumsum(w_new_yes_log[::-1])[::-1]
H_yes = np.where(p_obs_yes > 0, np.log2(p_obs_yes) - p_obs_yes_log / p_obs_yes, 0.0)
w_new_no_log = wlog(w_new_no)
w_old_no_log = wlog(w_old_no)
p_obs_no_log = (np.cumsum(w_old_no_log) - w_old_no_log) + np.cumsum(w_new_no_log[::-1])[::-1]
H_no = np.where(p_obs_no > 0, np.log2(p_obs_no) - p_obs_no_log / p_obs_no, 0.0)
# fmt: on
expected_H = H_yes * p_obs_yes + H_no * p_obs_no
# tie break the argmin by choosing the middle
candidate_indices = np.flatnonzero(
np.isclose(expected_H, np.min(expected_H), rtol=1e-12, atol=1e-15)
)
return int(candidate_indices[np.argmin(np.abs(candidate_indices - len(expected_H) // 2))])
@property
def distribution(self) -> ndarray:
"""Current posterior P(index=B | data)"""
self._maybe_update_posteriors()
assert self._post_weights is not None
return self._post_weights
@property
def entropy(self) -> float:
"""Posterior entropy in bits"""
self._maybe_update_posteriors()
assert self._post_weights is not None
probs = self._post_weights[self._post_weights > 0]
return -float(np.dot(probs, np.log2(probs)))
@property
def empirical_p_obs(self) -> tuple[ndarray, ndarray]:
"""Return what we've observed for p_obs_new and p_obs_old are if each commit is B."""
# fmt: off
total_left = self.obs_total
total_right = self.obs_total[-1] - total_left
yes_left = self.obs_yes
yes_right = yes_left[-1] - yes_left
# Use the following if you want to take the prior into account:
# p_obs_new = (self.alpha_new + yes_left) / (self.alpha_new + self.beta_new + total_left)
# p_obs_old = (self.alpha_old + yes_right) / (self.alpha_old + self.beta_old + total_right)
p_obs_new = yes_left / np.maximum(1e-10, total_left)
p_obs_old = yes_right / np.maximum(1e-10, total_right)
return p_obs_new, p_obs_old
# fmt: on
@property
def empirical_counts(self) -> tuple[tuple[ndarray, ndarray], tuple[ndarray, ndarray]]:
total_left = self.obs_total
total_right = self.obs_total[-1] - total_left
yes_left = self.obs_yes
yes_right = yes_left[-1] - yes_left
return (yes_left, total_left), (yes_right, total_right)
@property
def num_total_observations(self) -> int:
return int(self.obs_total[-1])
@property
def num_yes_observations(self) -> int:
return int(self.obs_yes[-1])
def central_range(self, mass: float) -> tuple[int, int]:
"""Return the range of indices that contain the central mass of the posterior, inclusive."""
self._maybe_update_posteriors()
assert self._post_weights is not None
assert 0 <= mass <= 1
cumsum = np.cumsum(self._post_weights)
tail = (1 - mass) / 2
left = np.searchsorted(cumsum, tail, side="left")
right = np.searchsorted(cumsum, 1 - tail, side="right")
right = min(right, len(cumsum) - 1) # type: ignore[arg-type]
return int(left), int(right)
# ==============================
# State logic
# ==============================
class BayesectError(Exception):
pass
class Result(enum.Enum):
FAIL = "fail"
PASS = "pass"
SKIP = "skip"
class BetaPriors:
def __init__(
self, alpha_new: float, beta_new: float, alpha_old: float, beta_old: float
) -> None:
self.alpha_new = alpha_new
self.beta_new = beta_new
self.alpha_old = alpha_old
self.beta_old = beta_old
def as_dict(self) -> dict[str, float]:
return {
"alpha_new": self.alpha_new,
"beta_new": self.beta_new,
"alpha_old": self.alpha_old,
"beta_old": self.beta_old,
}
STATE_FILENAME = "BAYESECT_STATE"
STATE_VERSION = 2
class State:
def __init__(
self,
old_sha: bytes,
new_sha: bytes,
beta_priors: BetaPriors,
priors: dict[bytes, float],
results: list[tuple[bytes, Result]],
commit_indices: dict[bytes, int],
) -> None:
self.old_sha = old_sha
self.new_sha = new_sha
self.beta_priors = beta_priors
self.priors = priors
self.results = results
self.commit_indices = commit_indices
def dump(self, repo_path: Path) -> None:
state_dict = {
"version": STATE_VERSION,
"old_sha": self.old_sha.decode(),
"new_sha": self.new_sha.decode(),
"beta_priors": self.beta_priors.as_dict(),
"priors": {k.decode(): v for k, v in self.priors.items()},
"results": [(k.decode(), v.value) for k, v in self.results],
}
with open(git_dir(repo_path) / STATE_FILENAME, "w") as f:
json.dump(state_dict, f)
@classmethod
def from_git_state(cls, repo_path: Path) -> State:
try:
with open(git_dir(repo_path) / STATE_FILENAME) as f:
data = f.read()
except FileNotFoundError:
raise BayesectError("No state file found, run `git bayesect start` first") from None
try:
state_dict = json.loads(data)
except json.JSONDecodeError:
raise BayesectError(
"Invalid state file, run `git bayesect reset` to start afresh"
) from None
if not isinstance(state_dict, dict):
raise BayesectError("Invalid state file, run `git bayesect reset` to start afresh")
if state_dict.get("version") != STATE_VERSION:
raise BayesectError(
f"State file version {state_dict.get('version')} does not match, "
"run `git bayesect reset` to start afresh"
)
assert set(state_dict) == {
"version",
"old_sha",
"new_sha",
"beta_priors",
"priors",
"results",
}
old_sha: bytes = state_dict["old_sha"].encode()
new_sha: bytes = state_dict["new_sha"].encode()
beta_priors: BetaPriors = BetaPriors(**state_dict["beta_priors"])
priors: dict[bytes, float] = {k.encode(): float(v) for k, v in state_dict["priors"].items()}
results: list[tuple[bytes, Result]] = [
(k.encode(), Result(v)) for k, v in state_dict["results"]
]
commit_indices = get_commit_indices(repo_path, new_sha.decode())
return cls(
old_sha=old_sha,
new_sha=new_sha,
beta_priors=beta_priors,
priors=priors,
results=results,
commit_indices=commit_indices,
)
# ==============================
# Git logic
# ==============================
def smolsha(commit: bytes) -> str:
return commit.decode()[:10]
def git_dir(path: Path) -> Path:
path_str = subprocess.check_output(["git", "rev-parse", "--git-dir"], cwd=path)
return Path(path_str.strip().decode()).absolute()
def parse_commit(repo_path: Path, commit: str | bytes | None) -> bytes:
if isinstance(commit, bytes):
assert len(commit) == 40
return commit
if commit is None:
commit = "HEAD"
commit = subprocess.check_output(["git", "rev-parse", commit], cwd=repo_path).strip()
assert len(commit) == 40
return commit
def get_commit_indices(repo_path: Path, head: str | bytes) -> dict[bytes, int]:
if isinstance(head, bytes):
head = head.decode()
# Oldest commit has index 0
# TODO: think about non-linear history
# --first-parent: When finding commits to include, follow only the first parent commit
# upon seeing a merge commit.
output = subprocess.check_output(
["git", "rev-list", "--reverse", "--first-parent", head], cwd=repo_path
)
return {line.strip(): i for i, line in enumerate(output.splitlines())}
def get_current_commit(repo_path: Path) -> bytes:
return subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=repo_path).strip()
def get_merge_base(repo_path: Path, c1: bytes, c2: bytes) -> bytes:
return subprocess.check_output(
["git", "merge-base", c1.decode(), c2.decode()], cwd=repo_path
).strip()
def get_commit_files_mapping(repo_path: Path, commits: list[bytes]) -> dict[bytes, list[str]]:
output = subprocess.check_output(
[
"git",
"diff-tree",
"--stdin",
"-r",
"--root",
"--name-only",
"--no-renames",
"-z",
"--pretty=format:%H%x00",
],
cwd=repo_path,
input=b"\n".join(commits),
)
sections = output.split(b"\x00\x00")
ret = {}
for s in sections:
commit, section = s.split(b"\n")
commit = commit.rstrip(b"\x00")
files = section.rstrip(b"\x00").split(b"\x00")
ret[commit] = [p.decode() for p in files]
return ret
def get_commit_text_mapping(repo_path: Path, commits: list[bytes]) -> dict[bytes, str]:
ret = {}
for commit in commits:
output = subprocess.check_output(
[
"git",
"show",
"--no-color",
"--format=%B",
"--patch",
"--no-ext-diff",
"--no-renames",
commit.decode(),
],
cwd=repo_path,
)
ret[commit] = output.decode(errors="replace")
return ret
# ==============================
# CLI logic
# ==============================
def get_bisector(state: State) -> Bisector:
old_index = state.commit_indices[state.old_sha]
new_index = state.commit_indices[state.new_sha]
assert new_index >= old_index
prior = np.ones(new_index - old_index + 1)
for commit_sha, weight in state.priors.items():
commit_index = state.commit_indices.get(commit_sha, -1)
if commit_index < old_index:
continue
relative_index = new_index - commit_index
assert 0 <= relative_index <= new_index - old_index
prior[relative_index] = weight
bisector = Bisector(
prior,
alpha_new=state.beta_priors.alpha_new,
beta_new=state.beta_priors.beta_new,
alpha_old=state.beta_priors.alpha_old,
beta_old=state.beta_priors.beta_old,
)
for commit_sha, result in state.results:
if result not in {Result.FAIL, Result.PASS}:
# TODO: handle SKIP maybe by adjusting the prior
continue
commit_index = state.commit_indices.get(commit_sha, -1)
if commit_index < old_index:
continue
# Our bisector is set up so that:
# - index 0 is newest commit
# - we're recording failures
relative_index = new_index - commit_index
assert 0 <= relative_index <= new_index - old_index
bisector.record(relative_index, result == Result.FAIL)
return bisector
def print_beta_priors(state: State) -> None:
print(
f"Prior of failure at new commit is "
f"{state.beta_priors.alpha_new / (state.beta_priors.alpha_new + state.beta_priors.beta_new):.0%} "
f"(α_new = {state.beta_priors.alpha_new}, β_new = {state.beta_priors.beta_new})"
)
print(
f"Prior of failure at old commit is "
f"{state.beta_priors.alpha_old / (state.beta_priors.alpha_old + state.beta_priors.beta_old):.0%} "
f"(α_old = {state.beta_priors.alpha_old}, β_old = {state.beta_priors.beta_old})"
)
print("=" * 80)
def print_status(
repo_path: Path, state: State, bisector: Bisector, confidence: float = 0.95
) -> None:
new_index = state.commit_indices[state.new_sha]
old_index = state.commit_indices[state.old_sha]
dist = bisector.distribution
dist_p_obs_new, dist_p_obs_old = bisector.empirical_p_obs
p_obs_new = np.dot(dist_p_obs_new, dist)
p_obs_old = np.dot(dist_p_obs_old, dist)
# tie break towards the middle
max_indices = np.flatnonzero(np.isclose(dist, np.max(dist), rtol=1e-12, atol=1e-15))
most_likely_index = int(max_indices[np.argmin(np.abs(max_indices - len(dist) // 2))])
most_likely_prob = dist[most_likely_index]
most_likely_p_obs_new = dist_p_obs_new[most_likely_index]
most_likely_p_obs_old = dist_p_obs_old[most_likely_index]
p90_left, p90_right = bisector.central_range(0.9)
p90_range = p90_right - p90_left + 1
indices_commits = {i: c for c, i in state.commit_indices.items()}
most_likely_commit = smolsha(indices_commits[new_index - most_likely_index])
p90_left_commit = smolsha(indices_commits[new_index - p90_left])
p90_right_commit = smolsha(indices_commits[new_index - p90_right])
if most_likely_prob >= confidence:
most_likely_commit = smolsha(indices_commits[new_index - most_likely_index])
msg = (
f"Bisection converged to {most_likely_commit} ({most_likely_prob:.1%}) "
f"after {bisector.num_total_observations} observations\n"
f"Observed subsequent failure rate is {most_likely_p_obs_new:.1%}, "
f"prior failure rate is {most_likely_p_obs_old:.1%}"
)
msg = msg.rstrip()
print("=" * 80)
print(msg)
print("=" * 80)
print(
subprocess.check_output(
["git", "show", "--color", "--no-patch", "--stat", most_likely_commit],
cwd=repo_path,
).decode()
)
print("=" * 80)
else:
msg = (
f"Bisection narrowed to `{p90_right_commit}^...{p90_left_commit}` "
f"({p90_range} commits) with 90% confidence "
f"after {bisector.num_total_observations} observations\n"
)
msg += f"New failure rate estimate: {p_obs_new:.1%}, old failure rate estimate: {p_obs_old:.1%}\n\n"
if most_likely_prob >= max(0.1, 2 / (new_index - old_index + 1)):
msg += f"Most likely commit: {most_likely_commit} ({most_likely_prob:.1%})\n"
msg += f"Observed subsequent failure rate is {most_likely_p_obs_new:.1%}, "
msg += f"prior failure rate is {most_likely_p_obs_old:.1%}\n"
msg = msg.rstrip()
print("=" * 80)
print(msg)
print("=" * 80)
def select_and_checkout(repo_path: Path, state: State, bisector: Bisector) -> bytes:
new_index = state.commit_indices[state.new_sha]
relative_index = bisector.select()
commit_index = new_index - relative_index
commit_sha = {c: i for i, c in state.commit_indices.items()}[commit_index]
print(f"Checking out next commit to test: {smolsha(commit_sha)}")
subprocess.run(
["git", "checkout", commit_sha.decode()], cwd=repo_path, check=True, capture_output=True
)
return commit_sha
def cli_start(old: str, new: str | None) -> None:
repo_path = Path.cwd()
new_sha = parse_commit(repo_path, new)
old_sha = parse_commit(repo_path, old)
commit_indices = get_commit_indices(repo_path, new_sha)
if old_sha not in commit_indices:
print(f"Old commit {smolsha(old_sha)} is not an ancestor of new commit {smolsha(new_sha)}")
merge_sha = get_merge_base(repo_path, old_sha, new_sha)
print(f"Using merge base {smolsha(merge_sha)} as old commit instead...")
old_sha = merge_sha
assert old_sha in commit_indices
b = Bisector([])
default_beta_priors = BetaPriors(
alpha_new=b.alpha_new,
beta_new=b.beta_new,
alpha_old=b.alpha_old,
beta_old=b.beta_old,
)
state = State(
old_sha=old_sha,
new_sha=new_sha,
beta_priors=default_beta_priors,
priors={},
results=[],
commit_indices=commit_indices,
)
state.dump(repo_path)
bisector = get_bisector(state)
print_status(repo_path, state, bisector)
print_beta_priors(state)
select_and_checkout(repo_path, state, bisector)
def cli_reset() -> None:
repo_path = Path.cwd()
(git_dir(repo_path) / STATE_FILENAME).unlink(missing_ok=True)
def cli_fail(commit: str | bytes | None) -> None:
repo_path = Path.cwd()
commit = parse_commit(repo_path, commit)
state = State.from_git_state(repo_path)
state.results.append((commit, Result.FAIL))
state.dump(repo_path)
bisector = get_bisector(state)
print_status(repo_path, state, bisector)
select_and_checkout(repo_path, state, bisector)
def cli_pass(commit: str | bytes | None) -> None:
repo_path = Path.cwd()
commit = parse_commit(repo_path, commit)
state = State.from_git_state(repo_path)
state.results.append((commit, Result.PASS))
state.dump(repo_path)
bisector = get_bisector(state)
print_status(repo_path, state, bisector)
select_and_checkout(repo_path, state, bisector)
def cli_undo() -> None:
repo_path = Path.cwd()
state = State.from_git_state(repo_path)
if state.results:
commit, result = state.results.pop()
match result:
case Result.FAIL:
print(f"Undid last observation: git bayesect fail {smolsha(commit)}")
case Result.PASS:
print(f"Undid last observation: git bayesect pass {smolsha(commit)}")
case Result.SKIP:
print(f"Undid last observation: git bayesect skip {smolsha(commit)}")
else:
raise BayesectError("No observation to undo")
state.dump(repo_path)
bisector = get_bisector(state)
print_status(repo_path, state, bisector)
select_and_checkout(repo_path, state, bisector)
def cli_run(cmd: list[str], confidence: float = 0.95) -> None:
repo_path = Path.cwd()
if not cmd:
raise BayesectError("No command to run")
if not 0 < confidence < 1:
raise BayesectError("Confidence threshold must be between 0 and 1") from None
state = State.from_git_state(repo_path)
bisector = get_bisector(state)
old_index = state.commit_indices[state.old_sha]
new_index = state.commit_indices[state.new_sha]
assert new_index >= old_index
try:
while True:
commit = select_and_checkout(repo_path, state, bisector)
proc = subprocess.run(cmd, cwd=repo_path, check=False)
result = Result.PASS if proc.returncode == 0 else Result.FAIL
state.results.append((commit, result))
relative_index = new_index - state.commit_indices[commit]
assert 0 <= relative_index <= new_index - old_index
bisector.record(relative_index, result == Result.FAIL)
print_status(repo_path, state, bisector, confidence=confidence)
if bisector.distribution.max() >= confidence:
break
finally:
state.dump(repo_path)
def cli_prior(commit: str | bytes, weight: float) -> None:
if weight < 0:
raise BayesectError("Weight for prior must be non-negative") from None
repo_path = Path.cwd()
commit = parse_commit(repo_path, commit)
state = State.from_git_state(repo_path)
state.priors[commit] = weight
state.dump(repo_path)
print(f"Updated prior for {smolsha(commit)} to {weight}")
def cli_priors_from_filenames(filenames_callback: str) -> None:
if "return" not in filenames_callback:
raise BayesectError("filenames callback must end with a return statement")
repo_path = Path.cwd()
state = State.from_git_state(repo_path)
files_mapping = get_commit_files_mapping(repo_path, commits=list(state.commit_indices.keys()))
cb_globals: dict[str, Any] = {}
cb_locals: dict[str, Any] = {}
import textwrap
filenames_callback = textwrap.indent(filenames_callback, " ")
filenames_callback = f"def _callback(filenames: list[str]) -> float:\n{filenames_callback}"
exec(filenames_callback, cb_globals, cb_locals)
filenames_fn = cb_locals["_callback"]
for commit, files in files_mapping.items():
prior = filenames_fn(files)
if prior is not None:
if not isinstance(prior, (int, float)) or prior < 0:
raise BayesectError("Weight for prior must be non-negative number")
state.priors[commit] = prior
state.dump(repo_path)
print(f"Updated priors for {len(state.priors)} commits")
bisector = get_bisector(state)
print_status(repo_path, state, bisector)
select_and_checkout(repo_path, state, bisector)
def cli_priors_from_text(text_callback: str) -> None:
if "return" not in text_callback:
raise BayesectError("text callback must end with a return statement")
repo_path = Path.cwd()
state = State.from_git_state(repo_path)
text_mapping = get_commit_text_mapping(repo_path, commits=list(state.commit_indices.keys()))
cb_globals: dict[str, Any] = {}
cb_locals: dict[str, Any] = {}
import textwrap
text_callback = textwrap.indent(text_callback, " ")
text_callback = f"def _callback(text: str) -> float:\n{text_callback}"
exec(text_callback, cb_globals, cb_locals)
text_fn = cb_locals["_callback"]
for commit, text in text_mapping.items():
prior = text_fn(text)
if prior is not None:
if not isinstance(prior, (int, float)) or prior < 0:
raise BayesectError("Weight for prior must be non-negative number")
state.priors[commit] = prior
state.dump(repo_path)
print(f"Updated priors for {len(state.priors)} commits")
bisector = get_bisector(state)
print_status(repo_path, state, bisector)
select_and_checkout(repo_path, state, bisector)
def cli_beta_priors(
alpha_new: float | None, beta_new: float | None, alpha_old: float | None, beta_old: float | None
) -> None:
repo_path = Path.cwd()
state = State.from_git_state(repo_path)
if alpha_new is not None:
state.beta_priors.alpha_new = alpha_new
if beta_new is not None:
state.beta_priors.beta_new = beta_new
if alpha_old is not None:
state.beta_priors.alpha_old = alpha_old
if beta_old is not None:
state.beta_priors.beta_old = beta_old
state.dump(repo_path)
bisector = get_bisector(state)
print_status(repo_path, state, bisector)
print_beta_priors(state)
select_and_checkout(repo_path, state, bisector)
def cli_checkout() -> None:
repo_path = Path.cwd()
state = State.from_git_state(repo_path)
bisector = get_bisector(state)
print_status(repo_path, state, bisector)
select_and_checkout(repo_path, state, bisector)
def cli_status() -> None:
repo_path = Path.cwd()
state = State.from_git_state(repo_path)
bisector = get_bisector(state)
new_index = state.commit_indices[state.new_sha]
old_index = state.commit_indices[state.old_sha]
dist = bisector.distribution
dist_p_obs_new, dist_p_obs_old = bisector.empirical_p_obs
(yes_new, total_new), (yes_old, total_old) = bisector.empirical_counts
rows = []
for commit, i in sorted(state.commit_indices.items(), key=lambda c: c[1], reverse=True):
relative_index = new_index - i
if relative_index == 0:
observations = f"{yes_new[relative_index]}/{total_new[relative_index]}"
else:
observations = (
f"{yes_new[relative_index] - yes_new[relative_index - 1]}/"
f"{total_new[relative_index] - total_new[relative_index - 1]}"
)
rows.append(
(
smolsha(commit),
f"{dist[relative_index]:.1%}",
observations,
f"{dist_p_obs_new[relative_index]:.1%}",
f"({yes_new[relative_index]}/{total_new[relative_index]})",
f"{dist_p_obs_old[relative_index]:.1%}",
f"({yes_old[relative_index]}/{total_old[relative_index]})",
"yes" if dist[relative_index] > max(0.1, 2 / (new_index - old_index + 1)) else "",
)
)
if commit == state.old_sha:
break
widths = [max(len(row[i]) for row in rows) for i in range(len(rows[0]))]
for (
commit_str,
likelihood,
observations,
p_obs_new,
c_obs_new,
p_obs_old,
c_obs_old,
should_highlight,
) in rows:
if should_highlight:
print("\033[103m", end="")
print(
f"{commit_str:<{widths[0]}} "
f"likelihood {likelihood:<{widths[1]}}, "
f"observed {observations:<{widths[2]}} failures, "
f"subsequent failure rate {p_obs_new:<{widths[3]}} "
f"{c_obs_new:<{widths[4]}}, "
f"prior failure rate {p_obs_old:<{widths[5]}} "
f"{c_obs_old:<{widths[6]}}",
end="",
)
if should_highlight:
print("\033[0m")
else:
print()
print_status(repo_path, state, bisector)
def cli_log() -> None:
repo_path = Path.cwd()
state = State.from_git_state(repo_path)
print(f"git bayesect start --old {smolsha(state.old_sha)} --new {smolsha(state.new_sha)}")
print(
f"git bayesect beta_priors "
f"--alpha-new {state.beta_priors.alpha_new} "
f"--beta-new {state.beta_priors.beta_new} "
f"--alpha-old {state.beta_priors.alpha_old} "
f"--beta-old {state.beta_priors.beta_old}"
)
for commit, weight in state.priors.items():
print(f"git bayesect prior --commit {smolsha(commit)} --weight {weight}")
print()
for commit, result in state.results:
match result:
case Result.PASS:
print(f"git bayesect pass --commit {smolsha(commit)}")
case Result.FAIL:
print(f"git bayesect fail --commit {smolsha(commit)}")
case Result.SKIP:
print(f"git bayesect skip --commit {smolsha(commit)}")
START_DESC = """\
Start a git bayesection.
Example:
$ git bayesect start --old "HEAD~100"
"""
FAIL_DESC = """\
Record one failure observation.
Example:
$ git bayesect fail
Recording a failure observation at a specific commit:
$ git bayesect fail --commit $SHA
"""
PASS_DESC = """\
Record one success observation.
Example:
$ git bayesect pass
Recording a success observation at a specific commit:
$ git bayesect pass --commit $SHA
"""
UNDO_DESC = """\
Remove the last observation.
Example:
$ git bayesect fail # oops, meant to run git bayesect pass
$ git bayesect undo
"""
RESET_DESC = """Delete all git bayesection state."""
PRIOR_DESC = """\
Set the prior for a specific commit.
The prior represents how likely it is a given commit introduced a change (prior to making any observations).
The prior is an unnormalised weight that we default to 1.
Marking $SHA as 10x more likely to have introduced a change (relative to other commits):
$ git bayesect prior --commit $SHA --weight 10
"""
PRIOR_FROM_FILENAMES_DESC = """\
Set priors for several commits based on filenames touched by the commit.
See `git bayesect prior --help` for more details on prior.
The `--filenames-callback` is Python code that is executed for each commit given the filenames the commit touches.
If it returns a number, that weight is used as the prior.
If it returns None, any previous prior is preserved.
Mark all files touching a given folder as 10x more likely to have introduced a change:
$ git bayesect prior_from_filenames --filenames-callback 'return 10 if any("suspicious" in f for f in filenames)'
"""
PRIOR_FROM_TEXT_DESC = """\
Set priors for several commits based on commit text (message + patch diff).
See `git bayesect prior --help` for more details on prior.
The `--text-callback` is Python code that is executed for each commit given the commit text.
The text is the commit message followed by the patch produced by `git show`.
If it returns a number, that weight is used as the prior.
If it returns None, any previous prior is preserved.
Mark commits mentioning timeout-related changes as 10x more likely:
$ git bayesect priors_from_text --text-callback 'return 10 if "timeout" in text.lower() else 1'
"""
BETA_PRIORS_DESC = """\
Set the beta priors for the likelihood of failure before and after the change.
Default Beta prior is α_new = 0.5, β_new = 0.5
Default Beta prior is α_old = 0.5, β_old = 0.5
WARNING: Moving the Beta priors away from the Jeffreys prior can cause overconfidence issues.
Any unspecified value is left unchanged.