论文

从 token 到文件:检测并定位代码中的 AI 参与痕迹

From Tokens to Files: Multi-Granularity Detection and Localization of AI-Involved Code

精选理由

现在代码审查越来越需要区分人和 AI 写的代码,这篇论文做到了行级定位,比以往只给一个文件总分的方法细多了,做代码安全或审计的可以看看。

这篇论文提出在 token 级别建模 AI 参与,可同时预测行、块、文件三个层级的代码来源。方法上使用一个冻结的代码语言模型,配上分别在人写和机器写文件上训练的两个低秩适配器(LoRA),用两者的对数似然比作为局部证据。在包含 11,773 个 AI 参与文件的内部测试集上,1% 误报率下达到 72.4 macro F1。在外部基准 HybridCodeAuthorship 的行级检测上拿到 62.5 F1,超过该基准论文报告的最好成绩 56。在 19,445 个 2022 年前的真实人写仓库文件上,误报率保持在 0.55% 到 0.81% 之间。

原文 · arXiv cs.LG

From Tokens to Files: Multi-Granularity Detection and Localization of AI-Involved Code

The increasing use of large language models for code generation calls for methods that distinguish purely human-written code from AI-involved code: files in which any part, however small, was generated or substantially modified by a model. Beyond flagging such files, we aim to localize the lines or blocks attributable to AI. Existing approaches compress a file into a single score, from surface stylistic features or a global code representation, which averages away the evidence when only a few blocks are AI-involved. We propose a framework that models AI authorship at the token level and predicts at the line, block, and file levels. A frozen code language model is equipped with two low-rank adapters, trained separately on machine-written and human-written files; their per-token log-likelihood ratio provides local evidence of AI authorship. Lightweight heads aggregate this evidence within lines and blocks, using the surrounding code context, and their local predictions are aggregated into a file-level decision. We train on mixed-authorship files built from human originals and their AI-generated or AI-modified counterparts, including continuation and fill-in-the-middle scenarios. On an internal test set of 11,773 AI-involved files across six generation regimes against 2,541 human files, our method reaches 72.4 macro F1 at a 1% FPR operating point. With no adaptation, it reaches 62.5 line-level F1 on HybridCodeAuthorship, an external human-AI co-writing benchmark with no training split, above the 56 its own paper reports as the best line-level result. On 19,445 human-written files from repositories created before 2022, the false-positive rate stays between 0.55% and 0.81% under the transported 1% threshold, so the operating point holds on real human code.