跳到正文
DEV Community· Revin·· 1 小時前AI 評分60

Revin 實測 36 個合併 PR:高分支密度組每行變更的審查輸入 token 中位數高 29%

I measured token cost per changed line on 36 merged PRs: the knotted diffs cost 29% more to review

AI 導讀

Revin 分析一個 Node 服務 11 週內合併的 36 個 PR,發現條件分支密度最高組每行變更的審查輸入 token 中位數比最低組高 29%。兩組中位數分別為 714 和 553;複雜組的審查輸入 token 有 61% 用於讀取已修改檔案,整潔組為 38%。作者提醒,結果僅來自一個服務、單一模型及 36 個 PR,不能據此確認因果關係。

正文

My agent review bill for one Node service climbed about 18% last month and the pull request count barely moved. Twenty-two merged in October, twenty-three in November, same five step pipeline, same model, same prompt. The number that actually moved was input tokens per review, and I could not have told you which pull requests were doing it.

I spent two weeks assuming something had changed on the provider side. Then a fragment published on martinfowler.com on November 20 pointed at a different suspect. Marcus Borg put 465 pull requests from 246 open source repositories through a review and fix pipeline with five agents, across three LLMs, and compared cost against the code health score from CodeScene. Between a typical unhealthy pull request at health 2.8 and a typical healthy one at 9.7, the difference added up to 22 to 35%. The two smells that drove it were Complex Method and Complex Conditional.

That is the first number I have seen from outside my own logs that ties code health to inference cost instead of to how developers feel about the codebase. I do not run CodeScene here, and 36 pull requests is not 465. I still wanted to know whether the effect is visible on a single service with a crude proxy, so I spent a Saturday counting.

What I counted

One service, TypeScript on Node, 36 merged pull requests over 11 weeks. Two numbers per pull request taken from the diff alone, plus one number from my own usage logs.

Pulling the diffs is the easy part:

mkdir -p diffs
gh pr list --state merged --limit 40 --json number \
  | jq -r '.[].number' \
  | while read -r n; do gh pr diff "$n" > "diffs/$n.diff"; done

Then a proxy for Complex Conditional. I count branching tokens in the added lines, drop test files so that a big spec does not inflate the score, and normalize per 100 changed lines so that a large diff does not win just by being large. Diffs under 30 code lines are out, because one renamed constant produced garbage ratios.

import re, pathlib

COND = re.compile(r"\b(if|switch|case|while|for)\b|&&|\|\||\?\?|\?[^.]")

rows = []
for f in sorted(pathlib.Path("diffs").glob("*.diff")):
    added = [l[1:] for l in f.read_text().splitlines()
             if l.startswith("+") and not l.startswith("+++")]
    code = [l for l in added if ".test." not in l and ".spec." not in l]
    if len(code) < 30:
        continue
    conds = sum(len(COND.findall(l)) for l in code)
    depth = max((len(l) - len(l.lstrip())) // 2 for l in code)
    rows.append((f.stem, len(code), round(conds * 100 / len(code), 1), depth))

for r in sorted(rows, key=lambda r: -r[2]):
    print("\t".join(str(x) for x in r))

The third number comes from the pipeline itself. My review runs in five steps: summarize the diff, read the touched files, check whether the tests cover the change, check migrations and config, write the review. Each step logs its own usage, and that split turned out to be the whole story.

Joining the diff numbers with the usage log gives this:

$ python3 rank.py | join-usage | head -7
pr   lines  cond/100  depth   in_tok  tok/line
377     96      10.9      3   71_040      740
402    331       9.8      5  238_988      722
418    214      11.2      4  152_804      714
...
366     71       2.1      2   38_340      540
391    176       2.8      2   94_336      536

The result

Split the 36 into thirds by conditional density. The top third sits at a median of 10.4 branching tokens per 100 changed lines and nesting depth 4. The bottom third sits at 2.7 and depth 2.

Input tokens per changed line: median 714 in the top third, 553 in the bottom third. A shade over 29% more. That lands inside the 22 to 35% band from the Borg experiment, and I would not read it as confirmation of anything, only as a result that fails to contradict it at a much smaller scale.

The per step log is the part I did not expect. On the knotted third, step two, reading the touched files, ate 61% of input tokens. On the clean third it was 38%. The agent was not writing longer reviews because the code was worse. It was pulling more files in to work out what the control flow did, and every extra file gets paid for again on every retry inside that step.

Review quality moved in the same direction. Output on the knotted third came back about 40% longer, and the share of comments that turned into a code change before merge fell from 31% to 19%. More text, less of it actionable, at a higher price per line.

Three things I tried that went nowhere

Total tokens per pull request tracked diff size and nothing else. Obvious in hindsight: big diff, big bill. Normalizing per changed line is what made any signal appear at all.

File count as a predictor was flat. A six file diff of flat mappers costs less to review than a one file diff with a four deep conditional in the middle of it.

Max nesting depth on its own was noisy. Indentation lies after a formatter pass and after any long argument list, so depth only earns its place as a tiebreaker next to the density number.

I also asked the model why it had read one file three times in the same run. It produced a confident, well written explanation that I had no way to verify, so I threw that away and went back to the usage log, which does not have opinions.

What I do now, before blaming the model

Rank the diff before the review runs. Anything over 8 branching tokens per 100 changed lines goes back to the author with the hunk named, not into the pipeline. Cap what step two is allowed to load: the function and its direct callers, not the whole file. And keep tokens per step per pull request, because a monthly total tells you the bill went up and nothing about where it went up.

The honest limits of this: one service, one model, 36 pull requests, and a regex that is not a code health score. Causation can run both ways here, since the knotted diffs are also the hard tickets, and hard tickets would cost more to review even in tidy code. What I do trust is narrow, and it is the step split. The money showed up in reading, not in writing.

If you run agent reviews at any volume: are you logging cost per changed line, or just watching the monthly invoice? And has anyone wired a health score into the gate before the agent runs, so the expensive diffs never reach the pipeline in the first place?

I keep the longer version of the three measurements I use on technical debt on the Revin blog: https://revin.com.br/en/blog/technical-debt-cost-three-measurements

來源:DEV Community · dev.to