Robot comment classifier
Here’s a comment I read in some code I was working on.
Tagger walks the entire collection to locate flagged clusters. Skip it<br>altogether when the cached, dirty-tracked flag count says there are none (the<br>usual case): nothing needs marking, so the walk is pure waste. When the count is<br>stale (e.g. right after an edit) or non-zero, we fall through to the real<br>lookup, so stale marks never show. Same eventually-consistent signal the “Flags”<br>badge relies on.
One thing in this comment caught my mind: the small parenthetical that says “the<br>usual case”. If that were true, it would be important information! However, this<br>comment is generated by a robot, which has no sense of what counts as the<br>usual case in this domain. But by generating that comment, the robot tricks<br>every future reader (both human and robot) into believing in a property of the<br>system that does not exist.
Since I knew, in this case, that the comment was generated by a robot and that<br>the property it contained was hallucinated, I could fix the comment. But that’s<br>not always so easy. Here’s another comment.
AcmeRate’s live currency conversion (in our per-line totals) are incompatible<br>with connection pooling: on a reused connection the rate lookup runs against the<br>previous session’s locale and returns figures in the wrong currency. So fall<br>back to a fresh connection when the order has a foreign-currency line (and<br>therefore conversion), and keep pooling otherwise so large domestic orders stay<br>fast. sessionInit runs before the request config is assembled, so localeState is<br>already populated when this is read.
This comment implies that it is important that we “keep pooling” so that “large<br>domestic orders stay fast”. If a human wrote this comment, I would assume they<br>had thought carefully about it, and determined that the optimisation must be<br>kept in. But if it’s written by a robot, it sounds more like it defensibly kept<br>something in the code that it has no business deciding about, because it doesn’t<br>know what it’s doing.11 The optimisation was useless. Only a little domain<br>experience is needed to know that nearly every order contains a foreign<br>currency, and large domestic orders are fast even without pooling.
The robot comment classifier
To aid my intuition, I wrote a classifier for source code comments. When fed the<br>opening example of this article, it correctly predicts the comment was generated<br>by a robot with almost certainty. The second example is also predicted to be<br>robot-generated with very high confidence.
I wish at this point I could say “and it achieves 98 % accuracy!” like some of<br>the llm detection papers out there do, but no. Some other spot checked<br>examples include:
Provenance<br>Classification<br>Confidence
Robot<br>Misclassified<br>53 %
Robot<br>Correct<br>67 %
Robot<br>Correct<br>78 %
Robot<br>Correct<br>79 %
Robot<br>Correct<br>92 %
Robot<br>Correct<br>93 %
Robot<br>Correct<br>99 %
Human<br>Correct<br>55 %
Human<br>Misclassified<br>71 %
Human<br>Correct<br>74 %
Human<br>Correct<br>85 %
Human<br>Correct<br>90 %
Human<br>Correct<br>96 %
Human<br>Correct<br>97 %
Mixed<br>Robot<br>54 %
Mixed<br>Robot<br>74 %
Mixed<br>Human<br>76 %
Mixed<br>Human<br>86 %
As you can see, misclassifications happen often enough to show up in this short<br>table, but it is encouraging that the misclassifications generally happen at lower<br>confidence levels.22 The mixed-provenance comments were originally generated<br>by a robot, but a human – that would be me – found the comments so terribly<br>written they had to be rewritten. The rewritten comments retained some of the<br>robot-generated structure, but they also have elements of human style. It makes<br>sense that these comments don’t get super high confidence levels either.
I don’t think the accuracy can go beyond the roughly 80 % this model gets, for<br>two reasons.
Code comments are often short, around 20 words or less. There’s only so much<br>signal that can be extracted from that little data.
The training data is inaccuractely labeled. See the methodology notes further<br>down for more on that.
This model is good enough to be an additional piece of evidence when I need it,<br>but I wouldn’t trust it alone.
How do detect differences in language
The most interesting bit is probably not the classifier itself, but what it<br>looks at to distinguish robot-generated comments from human ones. It’s worth<br>knowing that while this is trained on a wide range of human authors, I suspect<br>the vast majority of robot-generated comments in the training data come from<br>Anthropic models, so the features we see below don’t really distinguish<br>robots-in-general from humans as much as they distinguish Anthropic llm<br>models from humans. This is an important point we’ll get back to later.
I’m not versed in computational linguistics, so for this project I did some<br>cursory reading and picked up three basic ways to decompose texts to try to<br>extract style...