Anti-AIClone

Log date · Sat

Can clone detectors find rewritten code?

Possibly. Test rewritten code and hard negatives: text similarity can miss equivalent code and flag changed behavior.

Can a detector find rewritten code?

Possibly, but you need to test the detector against real rewrites. In our text comparison, a variable-renamed version matched the original on all 201 inputs but scored only 0.829; a changed discount matched on just 100 inputs yet scored 0.988. A 0.90 threshold misses the first and accepts the second.

On 2026-08-15 at 21:01 UTC, arXiv received v1 of “Invariant Pretraining for Robust Code Representations”, classified primarily as cs.LG, with a comment that it would appear at LMPL 2026. The paper reports that invariant pretraining improved clone detection robustness by up to 11 percentage points on its transformed tests. [1] Our Python text comparison illustrates the evaluation problem; it is not a test or replication of InvPT.

Questions and findings
QuestionFinding
Can renaming cause a miss?It does at 0.90 in our example.
Does a high score prove equal behavior?No; the changed discount scores 0.988.
What did the paper measure?Four encoder baselines, two tasks and four datasets.

The sample: rename versus changed rule

We wrote a Python fee function and two variants. One consistently changes the parameter from amount to value; the other keeps the names but changes the discount from 10 to 20. We ran all three on every integer from 0 through 200, then compared source characters with Python 3.9.1 difflib.SequenceMatcher and autojunk disabled. The utility compares sequences; it does not understand program meaning. [3]

Original sample; the variants rename the parameter and change the discount, respectively.
def fee(amount):
    if amount >= 100:
        return amount - 10
    return amount
Measured output, copied verbatim.
renamed: matching outputs=201/201; text similarity=0.829; >=0.90=False
changed: matching outputs=100/201; text similarity=0.988; >=0.90=True

The 0.90 threshold is chosen for illustration, not as a general recommendation. Our inputs cover both sides of the boundary and expose this change, but passing finite tests does not prove equivalence for arbitrary programs, inputs or environments. A single similarity score can also send textually close but behaviorally different code to the wrong conclusion.

What invariant pretraining changes

The paper studies source-code encoders. InvPT applies semantics-preserving transformations and combines masked language modeling with multi-positive contrastive learning. Variants of one function are positives; the training mixes different masks of the same code with transformed code and does not require paired natural-language descriptions. [2] The idea is to make representations more stable for those specified changes, rather than demand identical strings.

The authors evaluate clone retrieval and code classification on POJ104 and three CodeNet subsets. Retrieval uses MAP@R; classification uses accuracy. The abstract reports gains of up to 11 percentage points for clone detection and 19 for classification on transformed tests, while matching or improving standard accuracy. [1][2] Those are the authors’ results, not gains measured by us.

The test composes transformations from the same operator family used during pretraining, so the result does not establish robustness to arbitrary rewrites. Each configuration was run once without repeated random seeds; evaluate your own languages, code and rewrite types before selecting a production tool. [2] Similarity alone does not establish copying: independent implementations, shared dependencies and licences need human review.

Recommendations for an evaluation

These are evaluation recommendations, not detector performance measured by our toy experiment. Fix the model version, candidate pool and threshold, and retain scores for each variant so you can identify the source of a gap.

Recommendations by case
CaseRecommendation
Originals and rewritesGroup by source function; report recall and score changes separately.
Similar but different behaviorInclude hard negatives; count false positives and review them.
Training and evaluationSplit by source to keep variants of a function out of both sets.
Production candidateRecord candidate pool, threshold and version; retest at realistic scale.

Report hits and denominators for each rewrite type. For example, 90/100 originals and 70/100 renamed examples would show a 20 percentage-point drop. For retrieval, fix the number of candidates returned and record the review cost of misses and false positives. Do not show only the best threshold, and do not treat these illustrative numbers as our measurements.

Repeat the check on your own code

Prepare reviewed positive and hard-negative pairs from functions you are authorized to use. Give each original function a stable identifier and group its renamed, branch-rewritten and unchanged variants under it. Check expected behavior before sending them to a detector; pay particular attention to side effects, exceptions, overflow and external state.

Reproduce this Python comparison.
python3 --version
python3 - <<'PY'
from difflib import SequenceMatcher

original = '''def fee(amount):
    if amount >= 100:
        return amount - 10
    return amount
'''
renamed = '''def fee(value):
    if value >= 100:
        return value - 10
    return value
'''
changed = '''def fee(amount):
    if amount >= 100:
        return amount - 20
    return amount
'''
namespace = []
for source in (original, renamed, changed):
    scope = {}
    exec(source, scope)
    namespace.append(scope['fee'])
inputs = range(0, 201)
for name, source, func in zip(('renamed', 'changed'), (renamed, changed), namespace[1:]):
    same = sum(namespace[0](x) == func(x) for x in inputs)
    score = SequenceMatcher(None, original, source, autojunk=False).ratio()
    print(f'{name}: matching outputs={same}/201; text similarity={score:.3f}; >=0.90={score >= 0.90}')
PY

Record why each candidate is labeled positive or negative. Mark variants with uncertain equivalence for review instead of forcing them into the recall denominator. Retain failures and expected outputs so the team can compare the same cases after a model update and see whether false positives cluster around common templates or utility functions.

Save raw detector scores, threshold, tool version, dataset version and results by category. After changing a model or candidate pool, rerun the same held-out set; if you also tune the threshold, report results at the fixed threshold too. Have someone familiar with the code and its licensing review candidates rather than treating a score as a plagiarism verdict.

Your checklist

  • Create and review equivalent rewrites of your own functions.
  • Include hard negatives with similar text but different behavior.
  • Split training and held-out data by source function.
  • Fix the candidate pool, threshold and version; report recall and false positives by category.
  • Keep commit history and review detection results manually.

Primary sources

All sources and reproduction status ↗