Manuscript Offprint

arXiv:2610.27863

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

Yue Xu, Xiyang Wang, Hui Liu, Bolei He

ABSTRACT

The increasing use of large language models for code generation has created an urgent need for reliable methods to distinguish purely human-written code from AI-involved code — files in which any part was generated or substantially modified by a model, however small that part is. Our goal is not only to determine whether a program is purely human-written or has been generated or modified by AI, but also to localize the specific lines or blocks attributable to AI. Existing approaches typically compress a file into a single score, either from surface-level stylistic features or from a global code representation, which averages away the evidence when only a few blocks of a file are AI-involved. We propose a detection framework that models AI authorship at the token level and supports predictions 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. The per-token log-likelihood ratio between the two adapters provides local evidence of AI authorship. Lightweight prediction heads then aggregate this evidence within lines and blocks, using the surrounding code context to estimate the probability that each line or block was generated or substantially modified by AI. The resulting local predictions are further aggregated to produce a file-level decision. We train these predictions using mixed-authorship files constructed 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 a shared pool of 2,541 human files, our method reaches 72.4 macro F1 at a 1% FPR operating point. With no adaptation at all, the method reaches 62.5 line-level F1 on HybridCodeAuthorship (Patterson et al., 2026), an external human–AI co-writing benchmark that ships no training split, above the 56 that the benchmark’s own paper reports as its best line-level result. We further run an extra transfer evaluation on a human-written-only test set: across 19,445 files from repositories created before 2022, our false-positive rate stays between 0.55 and 0.81% when the false-positive rate threshold is transported at 1%. So the operating point remains valid after transferring from paired synthetic negatives to real human code.

1 INTRODUCTION

Assistants that write code inside an existing file have moved the unit of authorship below the file. A commit may contain a completed function body, a refilled interior span, or a locally refactored block, with the rest of the file untouched. The question a provenance tool has to answer is therefore not only was this file written by a model but is any part of it, and, if a reviewer is to act on the answer, which part.

∗^{*}Corresponding author.

Two families of detectors dominate the current literature, and both answer only the first question. Zero-shot likelihood statistics score a file against a reference language model and threshold a scalar summary: log-rank (Gehrmann et al., 2019), LRR (Su et al., 2023), perturbation curvature (Mitchell et al., 2023; Bao et al., 2024), or cross-perplexity between two models (Hans et al., 2024). Supervised detectors fine-tune a code encoder (Feng et al., 2020; Guo et al., 2022; Wang et al., 2023a) or align a proxy model to machine style (Chen et al., 2025) and read a single decision out of a global representation. In both cases the file is compressed to one number before any decision is made, which is adequate when the whole file is machine written and lossy when only a few blocks are.

Mixed authorship breaks these detectors in two distinct ways. A global score averages a short AI span together with the human majority of the file, so the evidence is diluted in proportion to the AI fraction. Independently, the span itself is weaker evidence than a whole machine-written file, because the model that produced it was conditioned on the surrounding human code and inherited its naming and layout. Our measurements show both effects: on the two interior-segment test cells, the strongest supervised file-level baseline drops to 0.647 and 0.565 AUROC, while the same detector reaches 0.945–0.963 on the from-scratch cells.

We therefore keep the evidence local for as long as possible. A single frozen code language model is equipped with two LoRA adapters (Hu et al., 2022), trained with a plain causal language modeling objective on machine-written and on human-written files respectively; neither has a classification head. Their per-token log-likelihood ratio is a local score whose human-side term cancels the confound of how predictable a token is in the first place.

Evaluating this design requires separating what a detector cannot represent from what its training distribution never contained. The transfer–adaptation gap on external benchmarks is the sharpest finding in the paper, and we read it as a statement about training corpora rather than detector capacity.

The main contributions of this paper are:

• A detection framework that couples a token-level likelihood ratio with line- and block-level supervised heads over a single frozen code language model, so that local generation evidence and its surrounding context are modeled jointly rather than as alternatives.

• A mixed-authorship data construction protocol that synthesizes continuation and fill-in-the-middle edits with character-level ground truth and a controlled AI ratio ρ\rho, which supplies within-file supervision for the line and block heads and test cells covering both tail and interior segment geometries. Appendix A.5 ablates the training-side composition and finds corpus volume, rather than the mixture of forms, to be the load-bearing factor.

• An evaluation protocol that separates capacity from distribution on external benchmarks under two protocols (zero-adaptation transfer and per-benchmark adaptation), reporting results at the file, block and line granularities, together with a carrier-artifact control and a false-positive measurement on real repository code.

We defer a full discussion of related work to Appendix A.1.

References (34)

  • Fast-DetectGPT: Efficient zero-shot detection of machine-generated text via conditional probability curvature
  • CodeBERT: A pre-trained model for programming and natural languages
  • GLTR: Statistical detection and visualization of generated text
  • UniXcoder: Unified cross-modal pre-training for code representation
  • Spotting LLMs with Binoculars: Zero-shot detection of machine-generated text
  • LoRA: Low-rank adaptation of large language models
  • DetectGPT: Zero-shot machine-generated text detection using probability curvature
  • Whitespaces don’t lie: Feature-driven and embedding-based approaches for detecting machine-generated code (opens arXiv in a new tab)
  • Between lines of code: Unraveling the distinct patterns of machine and human programmers
  • Imitate before detect: Aligning machine stylistic preference for machine-revised text detection
  • DetectLLM: Leveraging log rank information for zero-shot detection of machine-generated text
  • CodeT5+: Open code large language models for code understanding and generation
  • Detecting AI-generated code assignments using perplexity of large language models
  • Droid: A resource suite for AI-generated code detection
  • AICD Bench: A challenging benchmark for AI-generated code detection
  • HybridCodeAuthorship: A benchmark dataset for line-level code authorship detection
  • GPTSniffer: A CodeBERT-based classifier to detect source code written by ChatGPT
  • Distinguishing LLM-generated from human-written code by contrastive learning (opens arXiv in a new tab)
  • Whodunit: Classifying code as human authored or GPT-4 generated — a case study on CodeChef problems
  • Distinguishing AI- and human-generated code: A case study
  • SeqXGPT: Sentence-level AI-generated text detection
  • SemEval-2024 Task 8: Multigenerator, multidomain, and multilingual black-box machine-generated text detection
  • M4GT-Bench: Evaluation benchmark for black-box machine-generated text detection
  • RoFT: A tool for evaluating human detection of machine-generated text
  • Contrastive decoding: Open-ended text generation as optimization
  • DExperts: Decoding-time controlled text generation with experts and anti-experts
  • Tuning language models by proxy
  • Efficient training of language models to fill in the middle (opens arXiv in a new tab)
  • Learning phrase representations using RNN encoder–decoder for statistical machine translation
  • DeepSeek-Coder: When the large language model meets programming — the rise of code intelligence (opens arXiv in a new tab)
  • Qwen2.5-Coder Technical Report
  • Optimal detection of changepoints with a linear computational cost
  • Adam: A method for stochastic optimization
  • Selective review of offline change point detection methods