基于本地模型的代码审查结构与语义组件
2 分•作者: rs545837•4 个月前
我一直对改进代码审查很感兴趣,因为它们目前的效果仍然不尽如人意。因此,我开始研究一个可以附加到本地 LLM/API 调用的分诊层,以实现更好的代码审查。
大多数审查工具会将 PR 差异转储到模型中,并希望它能找到错误。模型会看到新增/删除的行、代码块头、上下文行。但它并不知道它正在查看的函数被其他 x 个文件中的 y 个函数调用,或者这里的一个类型更改会破坏三个目录之外的接口。
这个分诊层使用 tree-sitter 将源代码解析成 AST,提取语义上有意义的实体(函数、类、方法、结构体),并构建一个跨文件的依赖关系图。它根据传递性影响范围对每个更改的实体进行排名。它将审查范围缩减了 80-90%,并显著提高了对错误的关注度。现在我确信它可能会出现几次超出分布的情况,但为了快速的代码审查,这种权衡是值得的。
一旦你将问题缩小到“这是这个 PR 中风险最高的 n 个实体”,你就不再需要一个前沿模型。你需要一个只了解你代码的模型。一个在你的代码库上微调的 70 亿参数模型了解你的模式、约定和常见错误。结构化分诊处理全局推理,这使得你的模型能够很好地处理判断。
命令:
- inspect diff - 实体级别的差异,带有风险评分和影响范围
- inspect predict - 显示哪些未更改的实体有损坏风险
- inspect review - 结构化分诊 + LLM 审查
- inspect pr - 审查一个 GitHub PR
21 种语言解析器。使用 Rust 编写。开源。
Github: https://github.com/Ataraxy-Labs/inspect
查看原文
I was curious in improving code reviews because they still suck, so researching on a triage layer that you can attach to your local LLMs/api calls for better code reviews.<p>Most review tools dump a PR diff into a model and hope it finds bugs. The model sees added/removed lines, hunk headers, context lines. It has no idea that the function it's looking at is called by x other functions across y files, or that a type change here breaks an interface three directories away.<p>The triage layer parses source code into ASTs using tree-sitters, extracts semantically meaningful entities (functions, classes, methods, structs), and builds a cross-file dependency graph. It ranks every changed entity by transitive blast radius. Cuts the review surface by 80-90%, and increases the attention score on the bug significantly. Now I am sure it can be out of distribution few times but for fast code reviews this tradeoff is worth making.<p>Once you've narrowed the problem to "here are the n riskiest entities in this PR," you don't need a frontier model. You need a model that just knows your code. A 7B fine-tuned on your codebase knows your patterns, your conventions, your common bugs. Structural triage handles the global reasoning that results in your model handling the judgment call really well.<p>Commands:<p>- inspect diff - entity-level diff with risk scoring and blast radius<p>- inspect predict - show which unchanged entities are at risk of breaking<p>- inspect review - structural triage + LLM review<p>- inspect pr - review a GitHub PR<p>21 language parsers. Written in Rust. Open source.<p>Github: https://github.com/Ataraxy-Labs/inspect