展示 HN:Git bayesect – 贝叶斯 Git 二分查找,用于非确定性错误
Show HN: Git bayesect – Bayesian Git bisection for non-deterministic bugs

原始链接: https://github.com/hauntsaninja/git_bayesect

## git-bayesect: 贝叶斯Git二分查找 `git-bayesect` 是一个用于识别引入事件发生概率变化的提交的工具(例如,不稳定的测试)。与传统的 `git bisect` 不同,它使用贝叶斯推理,只需要一个变化*已经*发生,而不需要精确的失败率。 它通过迭代地缩小可能的提交范围,基于最小化预期熵来选择下一个要测试的提交。该工具巧妙地使用 Beta-Bernoulli 共轭性来处理未知的失败概率。 **关键命令:** * `git bayesect start --old `:开始二分查找。 * `git bayesect pass --commit `:记录一次成功的测试。 * `git bayesect prior --commit --weight `:设置关于某个提交的先验信念。 * `git bayesect run `:使用给定的命令自动化测试。 你还可以基于文件名或提交消息/diff内容设置先验概率,以获得更准确的结果。提供了一个演示仓库和脚本来帮助你入门。

## Git Bayesect:贝叶斯二分查找用于间歇性错误 - 摘要 Git Bayesect 是一种新工具,旨在高效地识别引入非确定性(间歇性)错误的提交,这对于传统的 `git bisect` 来说是一个挑战。与需要一致复现的 `git bisect` 不同,Bayesect 使用贝叶斯推理来处理可变错误的发生情况。 基准测试表明,在存在间歇性的情况下,Bayesect 显著优于 `git bisect`——在 90/10 的间歇性下,Bayesect 实现了约 96% 的准确率,而 `git bisect` 降至约 44%。通过基于代码结构对先验进行加权,优先考虑影响高度连接函数的提交,还可以获得进一步的准确率提升(10-15%)。 该工具通过策略性地选择下一个要测试的提交,最大限度地提高信息增益,从而最大限度地减少测试运行次数。虽然并非完全最优,但它为调试间歇性问题提供了实质性的改进,尤其是在测试缓慢或涉及 LLM 交互等外部因素时,这一点非常有价值。该项目在 GitHub 上可用 ([https://github.com/hauntsaninja/git_bayesect](https://github.com/hauntsaninja/git_bayesect)) 并且正在集成到 sem 中 ([https://github.com/ataraxy-labs/sem](https://github.com/ataraxy-labs/sem))。
相关文章

原文

Bayesian git bisection!

Use this to detect changes in likelihoods of events, for instance, to isolate a commit where a slightly flaky test became very flaky.

You don't need to know the likelihoods (although you can provide priors), just that something has changed at some point in some direction

Or:

uv tool install git_bayesect

git_bayesect uses Bayesian inference to identify the commit introducing a change, with commit selection performed via greedy minimisation of expected entropy, and using a Beta-Bernoulli conjugacy trick while calculating posterior probabilities to make handling unknown failure rates tractable.

See https://hauntsaninja.github.io/git_bayesect.html for a write up.

Start a Bayesian bisection:

git bayesect start --old $COMMIT

Record an observation on the current commit:

Or on a specific commit:

git bayesect pass --commit $COMMIT

Check the overall status of the bisection:

Reset:

Set the prior for a given commit:

git bayesect prior --commit $COMMIT --weight 10

Set prior for all commits based on filenames:

git bayesect priors_from_filenames --filenames-callback "return 10 if any('suspicious' in f for f in filenames) else 1"

Set prior for all commits based on the text in the commit message + diff:

git bayesect priors_from_text --text-callback "return 10 if 'timeout' in text.lower() else 1"

Get a log of commands to let you reconstruct the state:

Undo the last observation:

Run the bisection automatically using a command to make observations:

Checkout the best commmit to test:

This repository contains a little demo, in case you'd like to play around:

# Create a fake repository with a history to bayesect over
python scripts/generate_fake_repo.py
cd fake_repo

# The fake repo contains a script called flaky.py
# This is a simple script that fails some fraction of the time
# At some point in the history of the repo, that fraction was changed
python flaky.py
git log --oneline

# Start the bayesection
OLD_COMMIT=$(git rev-list HEAD --reverse | head -n 2 | tail -n 1)
git bayesect start --new main --old $OLD_COMMIT

# Run a bayesection to find the commit that introduced the change
git bayesect run python flaky.py
联系我们 contact @ memedata.com