% SemEval/ACL-style (system paper)
\documentclass[11pt]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{amsmath, amssymb}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{microtype}
\usepackage[nohyperref]{acl}
\usepackage{times}
\usepackage{latexsym}
\usepackage{enumitem}
\usepackage{hyperref}
\hypersetup{hidelinks}
\usepackage{url}
\setlength{\textfloatsep}{10pt plus 2pt minus 2pt}
\setlength{\floatsep}{8pt plus 2pt minus 2pt}
\setlength{\intextsep}{8pt plus 2pt minus 2pt}

\title{LAFED at SemEval-2026 Task 13: Language-Agnostic Feature Engineering for Cross-Lingual AI-Generated Code Detection}

\author{Juan David Villate \\
  Independent Researcher \\
  \texttt{jdvillate@hotmail.com}}

\begin{document}
\maketitle

\begin{abstract}
Robust detection of AI-generated source code across programming languages remains challenging due to language-specific cues and train--test distribution shifts.
We present \textbf{LAFED} (\emph{Language-Agnostic Feature Engineering Detector}), a feature-engineering approach trained on \{Python, Java, C++\} and evaluated on a multilingual test set that includes unseen languages \{C, C\#, Go, JavaScript, PHP\}.
LAFED combines (i) structural \emph{skeletal} features (indentation, control-flow density, and approximations of McCabe/Halstead complexity), (ii) character and whitespace statistics inspired by stylometry, and (iii) micro-style patterns (operator spacing, blank lines, indentation consistency).
Using XGBoost~\cite{xgboost2016} with Optuna hyperparameter search~\cite{optuna2019}, our best model achieves \textbf{macro-F1=0.7570} on the organizer-provided 1,000-sample test set for reproducible experiments, while the official hidden evaluation yields \textbf{macro-F1=0.75209} (\textbf{5th} place in Subtask~A).
Per-language analysis shows strong transfer to C\# (0.7753) and JavaScript (0.7683), but weaker performance on Go (0.6400) and PHP (0.5238).
\end{abstract}

\section{Introduction}
SemEval-2026 Task 13 focuses on detecting AI-generated source code across multiple programming languages and code generators; we refer to the task paper for dataset details and the official evaluation protocol~\cite{orel-etal-2026-semeval-2026}.
Cross-language detection is particularly challenging because many lexical cues identify the programming language rather than the origin (human vs.~AI), yielding brittle detectors under language shift.

\paragraph{Core hypothesis.}
We hypothesize that cross-language generalization is driven primarily by \emph{language-invariant} signals (structure, character-level patterns, and micro-style), rather than by post-hoc domain adaptation over language-specific representations.

\paragraph{Contributions.}
\begin{itemize}[leftmargin=*]
  \item We design language-agnostic features spanning skeletal structure, character/whitespace stylometry, and micro-style patterns.
  \item We build an end-to-end pipeline (extraction, interactions, normalization, leakage-aware selection, Optuna optimization) and report cross-language results with per-language analysis and ablations.
\end{itemize}

The full code, including the feature-extraction pipeline, is available at: \href{https://github.com/DavidVilem/LAFED-SemEval2026}{github.com/DavidVilem/LAFED-SemEval2026}.

\section{Related work}
Our approach connects (i) classic software metrics such as cyclomatic complexity~\cite{mccabe1976complexity} and Halstead-style measures~\cite{halstead1977elements}, (ii) character-level stylometry for authorship attribution~\cite{stamatatos2013robustness, sari2017continuous}, and (iii) probability calibration and label-shift correction for distribution changes~\cite{zadrozny2002transforming, niculescu2005predicting, saerens2002adjusting}. Prior work on AI-generated code detection spans stylometry-based signals, perplexity/perturbation-style detectors~\cite{mitchell2023detectgpt, xu2024detecting}, and learned token/AST representations in benchmark settings for machine-generated code detection~\cite{orel2026aicdbenchchallengingbenchmark}. Our contribution is complementary: we explicitly reduce language leakage during feature selection, which is especially relevant under cross-language OOD evaluation such as SemEval-2026 Task 13.

\section{Task and metric}
Given a code snippet $x$ in language $\ell$, we predict $y\in\{0,1\}$, where $y=1$ denotes AI-generated code. The key challenge is generalization to unseen languages.

We use \textbf{macro-F1} as the primary metric, defined as the average of class-wise F1:
\begin{equation}
\mathrm{F1}_{\mathrm{macro}} = \frac{\mathrm{F1}_{y=0} + \mathrm{F1}_{y=1}}{2}.
\end{equation}

We also report per-language precision and recall.

\section{System overview}
Figure~\ref{fig:overview} summarizes the pipeline. The system is a deterministic feature extractor followed by a discriminative classifier.

\begin{figure*}[t]
  \centering
  \includegraphics[width=\textwidth,trim=0 18 0 10,clip]{system_architecture.png}
  \caption{Overview of the proposed pipeline (feature extraction/engineering, selection, and training).}
  \label{fig:overview}
\end{figure*}


\subsection{Pipeline}
Our training script implements a deterministic pipeline: data loading, feature extraction, feature interactions, global normalization, leakage-aware feature selection, Optuna optimization, and final training with lightweight post-processing.

\section{Language-agnostic features}
We extract 286 candidate features and select 214 in the best configuration. Below we summarize the families and motivation.

\subsection{Skeletal features (structure and complexity)}
Skeletal features approximate program structure without relying on language-specific tokens. They include indentation statistics, approximate control-flow counts, and lightweight complexity proxies.

\paragraph{Indentation nesting and instability.}
We estimate per-line indentation (tabs mapped to 4 spaces) and compute summary statistics over indentation levels $\{d_i\}$, including the mean, maximum, standard deviation, and the average change magnitude $|d_i-d_{i-1}|$.

\paragraph{Control flow.}
We approximate counts of control-flow constructs (e.g., \texttt{if/for/while/try/switch}) via regexes and normalize by non-empty lines:
\begin{equation}
\mathrm{control\_density} = \frac{N_{\mathrm{control}}}{N_{\mathrm{lines}}}.
\end{equation}

\paragraph{Approximate cyclomatic complexity.}
We approximate McCabe complexity~\cite{mccabe1976complexity} as:
\begin{equation}
\mathrm{CC}_{\mathrm{approx}} = 1 + N_{\mathrm{decision}},
\end{equation}
where decision points come from the control-flow counts.

\paragraph{Halstead-style metrics.}
Following Halstead~\cite{halstead1977elements}, we count operators and operands to build vocabulary $n=n_1+n_2$, length $N=N_1+N_2$, and volume:
\begin{equation}
V = N\log_2(n).
\end{equation}
These proxies capture coarse notions of code ``effort''/``difficulty''.

\paragraph{Approximate cognitive complexity.}
Inspired by cognitive complexity guidelines~\cite{campbell2017cognitive}, we penalize control-flow nesting and logical connectors (e.g., \texttt{and/or}, \texttt{\&\&/||}).

\subsection{Character and whitespace features}
Character-level features capture spacing/indentation regularities that are often more uniform in model-generated code~\cite{stamatatos2013robustness, sari2017continuous}. We use informative character n-grams, statistics of space runs (mean/std/max), an indentation-multiples proxy (divisible by 2/4), and character entropy.

\subsection{Micro-style patterns}
Micro-style features capture fine-grained habits (operator spacing, blank-line patterns, indentation consistency). In our experiments, blank-line features (\texttt{micro\_blank\_*}) are among the most transferable signals.
\footnote{All experiments use a fixed random seed (42) and 5-fold stratified CV.}

\section{Feature engineering and selection}
\subsection{Interactions}
We add a small set of interaction features (products and stabilized ratios), which rank among the most important signals.

\subsection{Global normalization (domain-invariant)}
We apply global z-normalization to a subset of universal features (Eq.~5) to reduce sensitivity to scale changes across domains:
\begin{equation}
\tilde f = \frac{f - \mu_f}{\sigma_f + \epsilon}.
\end{equation}

\subsection{Leakage-aware selection}
We follow supervised feature selection principles~\cite{guyon2003introduction} while explicitly penalizing language predictivity. We compute:
\begin{itemize}[leftmargin=*]
  \item \textbf{AI vs.~human discriminability}: mean/min per-language AUC (using $\max(\mathrm{AUC},1-\mathrm{AUC})$).
  \item \textbf{Language predictivity}: mean one-vs-rest AUC for predicting the programming language.
\end{itemize}

We select a (non-risky) feature if it satisfies:
\begin{equation}
\begin{aligned}
\mathrm{AUC}_{\mathrm{mean}} &\ge \tau_{\mathrm{auc}}, \\
\mathrm{AUC}_{\mathrm{min}}  &\ge \tau_{\mathrm{auc}} - 0.05, \\
\mathrm{LangPred}            &\le \tau_{\mathrm{lang}}.
\end{aligned}
\end{equation}

We also always include a small forced set (universal + skeletal) and exclude explicitly risky syntax-dependent features (e.g., counting \texttt{main} or semicolons per line).

\section{Model, optimization, and post-processing}
\subsection{Classifier}
We train an XGBoost classifier~\cite{xgboost2016}, using inverse-frequency language weights with clipping for stability.

\subsection{Optuna optimization}
We jointly optimize XGBoost hyperparameters and selection thresholds $(\tau_{\mathrm{auc}},\tau_{\mathrm{lang}})$ with Optuna~\cite{optuna2019} (700 trials). Each trial trains a 5-fold stratified CV ensemble; optimization is performed using OOF-CV. The test score is reported for completeness and was not used for selecting hyperparameters. We also track the generalization gap:
\begin{equation}
\mathrm{gap} = \mathrm{F1}_{\mathrm{OOF}} - \mathrm{F1}_{\mathrm{test}}.
\end{equation}

\subsection{Post-processing: calibration and label shift}
Given a prior shift (train 52.30\% AI vs.~test 22.30\% AI), we evaluate:
\begin{itemize}[leftmargin=*]
  \item \textbf{Calibration} via isotonic regression~\cite{zadrozny2002transforming, niculescu2005predicting} fitted on OOF predictions.
  \item \textbf{Label-shift correction} following Saerens et al.~\cite{saerens2002adjusting}.
\end{itemize}
In our experiments, these post-processing steps did not improve macro-F1, so we report the uncalibrated model.

\section{Experimental setup}
Training: 500,000 samples in \{C++, Java, Python\}. Test: 1,000 samples in 8 languages \{C, C\#, C++, Go, Java, JavaScript, PHP, Python\}. Unseen languages: \{C, C\#, Go, JavaScript, PHP\}.

\begin{table}[!t]
\centering
\begin{tabular}{lcc}
\toprule
Split & \#Samples & AI ratio \\
\midrule
Train & 500\,000 & 52.3\% \\
Test & 1\,000 & 22.3\% \\
\bottomrule
\end{tabular}
\caption{Dataset summary.}
\label{tab:data}
\end{table}

\subsection{Official 100K validation set and model selection}
We primarily relied on 5-fold OOF-CV for model selection to reduce sensitivity to a single split and to estimate stability under re-sampling~\cite{stone1974cross, bousquet2002stability}. We did not use the official 100K validation set for hyperparameter tuning; instead, all selection decisions were made from OOF-CV only. This avoids tuning on a single external split and prevents test-set optimization.

\section{Results}
\subsection{Overall results and generalization gap}
\noindent We report macro-F1=0.7570 on the organizer-provided 1,000-sample test set for reproducible experiments, while the official hidden evaluation yields macro-F1=0.75209 (5th place in Subtask~A). Our best trial (243) reaches OOF macro-F1=0.9740, implying a gap of 0.2170 relative to the 1,000-sample test set and suggesting additional shift beyond language.

Post-processing note. Isotonic calibration reduced macro-F1 to 0.7468, while label-shift correction matched the uncalibrated score (0.7570); therefore we report the uncalibrated model.

\subsection{Domain shift as a contributor to the OOF--test gap (discussion)}
Beyond language transfer, domain differences (algorithmic vs.~research/production) can alter formatting regularities and structural/style cues (including whitespace consistency), affecting feature-engineered detectors even when the language is unchanged. Thus, the OOF--test gap (0.2170) likely reflects combined language, domain, and generator/prompt/style shifts. Future work should better disentangle these factors and explore format-robust signals (e.g., AST-based representations).

\subsection{Multiple runs and variants}
Beyond the official submission, we tested internal baselines and robustness variants (all negative); Table~\ref{tab:multirun} summarizes representative runs.

\begin{table}[!t]
\centering
\resizebox{\columnwidth}{!}{%
\begin{tabular}{lcc}
\toprule
Setting & Test macro-F1 & Notes \\
\midrule
Baseline (V2, 17 feats) & 0.7180 & Whitespace + structure \\
Intermediate (V5) & 0.7623 & Skeletal features \\
Final system (V6, 214 feats) & 0.7570 & Joint selection/HP \\
\midrule
V6 + Augmentation & 0.7500 & $\Delta$F1=-0.007 \\
V6 + Projections & 0.7410 & $\Delta$F1=-0.016 \\
V6 + DRO-style & 0.7310 & $\Delta$F1=-0.026 \\
\bottomrule
\end{tabular}%
}
\caption{Representative runs/variants (Augmentation=Mixup-style feature mixing~\cite{zhang2018mixup}; Projections=linear language-signal suppression~\cite{elazar2018adversarial}; DRO-style=group-robust reweighting~\cite{sagawa2020distributionally}).}
\label{tab:multirun}
\end{table}


\subsection{Baselines}
Simpler whitespace/structure subsets are competitive but consistently below the final model, suggesting that micro-style signals and interactions add robustness under cross-language shift. Although V5 peaks slightly higher on aggregate macro-F1 internally, V6 was selected as the official run due to leakage-aware selection and reduced reliance on language-predictive cues (Eq.~6).

\subsection{Negative results: why DRO did not help}
Group-DRO over seen-language groups did not improve OOD performance. Reweighting increases variance and worst-case optimization over seen languages does not necessarily transfer to unseen languages or domains, which likely dominate our test-time shifts.

\begin{table}[!t]
\centering
\resizebox{\columnwidth}{!}{%
\begin{tabular}{lcccc}
\toprule
Setting & OOF macro-F1 & Test macro-F1 & Gap & \#Features \\
\midrule
Proposed model (best, Trial 243) & 0.9740 & 0.7570 & 0.2170 & 214 \\
\bottomrule
\end{tabular}
}
\caption{Overall result of the best model.}
\label{tab:overall}
\end{table}


\subsection{Per-language transfer}
Table~\ref{tab:perlang} reports test performance by language: transfer is strong to C\# and JavaScript, while Go shows high precision but low recall, and PHP is close to chance. Averaging over languages, seen languages reach macro-F1=0.7458 while unseen languages reach macro-F1=0.6797, highlighting the expected OOD drop under cross-language transfer.

\subsection{Error analysis}
Manual inspection suggests frequent Go false negatives under enforced formatting (e.g., \texttt{gofmt}) and mixed PHP errors due to hybrid syntax and embedded blocks (e.g., HTML) that distort indentation-based cues.

\begin{table}[!t]
\centering
\resizebox{\columnwidth}{!}{%
\begin{tabular}{lccccc}
\toprule
Language & Seen? & F1 & Prec. & Rec. & N \\
\midrule
C & Unseen & 0.6912 & 0.6781 & 0.7146 & 51 \\
C\# & Unseen & 0.7753 & 0.7598 & 0.8005 & 122 \\
C++ & Seen & 0.7208 & 0.7208 & 0.7208 & 75 \\
Go & Unseen & 0.6400 & 0.8929 & 0.6250 & 60 \\
Java & Seen & 0.7161 & 0.7071 & 0.7270 & 256 \\
JavaScript & Unseen & 0.7683 & 0.7649 & 0.7774 & 85 \\
PHP & Unseen & 0.5238 & 0.5299 & 0.5778 & 48 \\
Python & Seen & 0.8004 & 0.7904 & 0.8140 & 303 \\
\bottomrule
\end{tabular}%
}
\caption{Per-language results on test.}
\label{tab:perlang}
\end{table}


\subsection{Feature analysis}
Feature importances (XGBoost gain) are dominated by whitespace cues and their interactions; Table~\ref{tab:top10features} shows the top-10 features (8/10 whitespace-related).

\begin{table}[!t]
\centering
\resizebox{\columnwidth}{!}{%
\begin{tabular}{lc}
\toprule
Feature & Gain \\
\midrule
tab\_space\_mix\_ratio\_div\_style\_spacing\_consistency & 0.1301 \\
tab\_space\_mix\_ratio\_div\_avg\_line\_length & 0.1061 \\
skel\_control\_density & 0.0536 \\
tab\_space\_mix\_ratio & 0.0532 \\
tab\_space\_mix\_ratio\_gnorm & 0.0498 \\
tab\_char\_ratio & 0.0428 \\
tab\_char\_ratio\_gnorm & 0.0402 \\
bracket\_balance & 0.0266 \\
tab\_space\_mix\_ratio\_x\_style\_spacing\_consistency & 0.0226 \\
tab\_space\_mix\_ratio\_x\_avg\_line\_length & 0.0220 \\
\bottomrule
\end{tabular}%
}
\caption{Top-10 features by XGBoost gain (Trial 243).}
\label{tab:top10features}
\end{table}

\subsection{Ablation}
Component-level ablations indicate that (i) a whitespace/structure baseline is already strong, (ii) skeletal features provide additional gains, (iii) micro-style features (notably blank-line patterns) transfer well, and (iv) the tested adaptation variants did not improve performance (details in the repository).

\section{Discussion}
We evaluate four hypotheses based on our experiments.

\paragraph{H1: Whitespace provides a universal discriminative signal.}
\textbf{Supported.}
Top-ranked features are dominated by whitespace cues, especially \texttt{tab\_space\_mix\_ratio} and its interactions.

\paragraph{H2: Structural metrics (McCabe/Halstead) help cross-language detection.}
\textbf{Partially supported.}
Skeletal features carry signal but are also more predictive of language, requiring leakage-aware selection.

\paragraph{H3: Character n-grams are fully language-agnostic in code.}
\textbf{Not supported.}
Several n-grams have high AUC but also predict the programming language, explaining why only a small subset is selected.

\paragraph{H4: Blank-line micro-patterns generalize best.}
\textbf{Supported.}
\texttt{micro\_blank\_*} features show lower language predictivity and rank among the most useful.

\section{Limitations and future work}
\begin{itemize}[leftmargin=*]
  \item \textbf{Multiple simultaneous shifts}: the gap likely reflects combined language, domain, and generator/prompt/style shifts. Disentangling these effects and designing signals robust to domain conventions remains important future work~\cite{bousquet2002stability, orel2026aicdbenchchallengingbenchmark}.
  \item \textbf{Outlier languages}: PHP and Go likely require additional semantic signals (e.g., parsing/AST).
  \item \textbf{Reducing the gap}: training on more languages and using stricter validation (LOLO/LOOCV) may improve robustness~\cite{stone1974cross}.
\end{itemize}

\section{Conclusion}
LAFED reaches macro-F1=0.7570 on the organizer-provided 1,000-sample test set and macro-F1=0.75209 (5th place) on the official hidden evaluation. Ablations show consistent gains from the full feature set over simpler baselines, while robustness/adaptation variants did not improve macro-F1. Feature importance is dominated by whitespace interactions (Table~\ref{tab:top10features}); skeletal signals are complementary but require leakage-aware selection (Eq.~6). Remaining failures on Go/PHP and the OOF--test gap (0.2170) motivate more format-robust semantic signals (e.g., AST) in future work.

\input{references.tex}

\end{document}