
Bridging the Gap: From AI Slop to Mathematical Governance
A mathematical framework for detecting AI-generated code slop using AST distributions, Jensen-Shannon divergence, and geometric governance gates.
Β

The Vacuum Nobody Talks About

The AI industry has a measurement problem.
Models are evaluated with extreme mathematical rigor. Loss functions, perplexity scores, BLEU, ROUGE, BERTScore. Researchers publish benchmark comparisons down to four decimal places. The science of evaluating AI outputs has never been more sophisticated.
Then the model ships a code change into a production repository.
And the measurement framework collapses.
The SWE-bench leaderboard, the primary benchmark for AI coding agents, asks one question per task: did the tests pass?
No structural analysis.
No dependency audit.
No measure of whether the agent introduced a 400-line monolithic function that will require three senior engineers and two weeks to untangle.
Pass or fail. Green or red.
This is not a minor gap.
It is a categorical one.
The tools used to evaluate AI reasoning are built on distributional analysis, tensor calculus, and information theory. The tools used to evaluate AI-generated code are built on regex and boolean test results. Between those two worlds, existing tooling was built for human errors, not AI structural patterns.
The gap is obvious. The harder question is why it has remained open for so long.
This is no longer theoretical.
The cost is already being externalized into review time, maintainer fatigue, and structural debt.
Why Mathematical Models Do Not Exist for Code

The absence is not accidental.
Traditional static analysis tools were built to catch human mistakes.
Null pointer dereferences.
Missing return statements.
Variables declared but never used.
These are discrete, local errors.
A finite set of pattern-matching rules handles them well.
The tools are fast, predictable, and accurate within their domain.
AI produces a different failure mode.
AI writes code that compiles cleanly, passes tests, satisfies linters, and is structurally wrong in ways none of those tools were designed to detect.
This is the same silent degradation pattern described in AI Agents Are Poisoning Your Codebase From the Inside: the surface signals stay green while the architecture underneath continues to decay.
The problem is not a missing rule. It is a missing abstraction layer.
To detect structural bloat, hallucinated dependencies, and architectural redundancy, you need to represent the code as a mathematical object first. That requires a different kind of analysis entirely.
Static analysis vendors have no incentive to build it. Their customers measure quality by test pass rates. As long as the tests pass, the tooling is considered sufficient. The demand signal for structural mathematical analysis of AI-generated code does not exist in most procurement conversations.
There is also a harder problem.
The math is genuinely difficult to apply at scale.
Probability distributions over AST node types, divergence metrics across file graphs, weighted geometric aggregation with empirically calibrated parameters. These are not concepts that fit naturally into a CI/CD pipeline configuration file. The gap between what is mathematically necessary and what is operationally deployable has kept this space empty.
For many teams, that gap is inconvenient. In some domains, it becomes unacceptable.
Where Mathematical Governance Becomes Non-Negotiable

For most software teams, AI slop is a technical debt problem.
Code accumulates.
Quality degrades.
Engineers spend increasing time refactoring rather than building.
This is a real cost, but it is a recoverable one.
For certain fields, the stakes are different.
In medical software, an AI-generated hallucinated dependency can pull in a library with known CVEs. In a financial trading system, a God function with cyclomatic complexity above 15 is not just bad engineering. It is an unreviewed execution path in a system that moves capital. In regulatory environments, inflated jargon ratios are not a style problem. They are evidence that the system cannot explain what it is doing.
The common thread is accountability.
These domains require not just that the code works, but that someone can demonstrate, mathematically, that the code was evaluated against a defined standard. A human reviewer signing off is not sufficient.
Human review is inconsistent, exhaustible, and subject to sycophancy. A reviewer who spent eight hours in meetings before a code review is not evaluating the same way as one who is fresh.
Mathematics does not get tired.
Once accountability becomes non-negotiable, review alone stops being a sufficient answer.
From Harness to Governance: Why This Is the Next Layer

The AI governance conversation has been accelerating since late 2025.
What matters here is not any single vendor incident. It is the recurring shape of the failure.
Agent systems rarely fail because a model suddenly becomes unintelligent.
They fail because the control layer around the model degrades silently: a rule stops applying, an exception path grows unnoticed, or a safeguard exists on paper but not in the execution path that actually matters.
That is the structural problem.
A system can contain rules and still drift into unsafe behavior if the layer enforcing those rules is embedded inside the same runtime, state, and incentives as the system it is supposed to govern. When the governed system and the governing mechanism share the same blind spots, silent failure stops being an exception and becomes a property of the architecture.
The same logic applies to code.
An AI agent that writes code cannot be its own reviewer. An LLM used as a judge of LLM-generated code inherits the same approval bias: it tends to validate outputs that are fluent, familiar, or locally coherent, even when the deeper structure is degrading. This is not primarily a prompt engineering problem. It is an independence problem.
Real governance therefore requires a measurement layer that is external to the system being measured.
For agent behavior, that means Policy-as-Code outside the runtime, tamper-evident audit trails, and fail-closed defaults. For code quality, the parallel requirement is a mathematical measurement layer that does not rely on LLM judgment, does not collapse to test pass rates, and cannot be diluted by surrounding one catastrophic file with dozens of clean wrappers.
This is where code governance and agent governance converge.
Drift is the accumulation of small accepted changes that eventually alter the safety profile of the whole system. In a harness, drift means an approved action becomes dangerous because the surrounding context changed. In a codebase, drift means tests continue to pass while structural bloat, hallucinated dependencies, and architectural redundancy quietly compound.
Binary evaluation will not surface either form of drift.
Independent measurement will.
Independent measurement is the requirement.
The remaining question is how to build it.
The rest of this article describes how to build that measurement layer at the code level using Information Theory.
The Mathematical Governance Framework
1. Abstracting Code: The Distributional Code Fingerprint (DCF)
To measure code mathematically, you must first convert it into a mathematical object. We parse the Python code into an Abstract Syntax Tree (AST) and transform it into a genuine probability distribution over node types. We call this the Distributional Code Fingerprint (DCF).
For any given file, the probability of encountering an AST node of type (e.g., FunctionDef, Assign, Call) is: πP(N_i | file): probability of observing AST node type N_i in a file, used to represent code as a distribution.
Because this is a true probability distribution , we can apply Information Theory to it.
Yes. If you run the detector's AST parser against its own manual test suite (clean.py vs generated_slop.py), the distinction is stark.
Human-written business logic maintains a dynamic, purposeful distribution (e.g., Expr nodes at 7.6%, Name lookups at 20.0%). The AI-generated slop completely collapses into repetitive, padded structural patterns, causing hollow Expr nodes to spike to 18.6% while functional Name lookups plummet to 5.9%.
While this is a controlled N=2 example, it clearly demonstrates how the DCF captures the structural fingerprint of AI padding.
But a fingerprint alone is not yet a governance signal. To get there, we need measurable inputs.
2. The Core Inputs: Density, Dependencies, Inflation, and Patterns

Before we aggregate scores, we calculate four primary dimensions of structural integrity from the AST:
1. Logic Density Ratio (LDR): The ratio of actual executable AST logic nodes to total lines. It mathematically penalizes empty boilerplate and over-commenting.
2. Deep Dependency Check (DDC): The intersection ratio of imported modules versus modules actually utilized in the AST. It catches the classic AI hallucination of importing
json or logging "just in case."3. Inflation-to-Code Ratio (ICR): ICR measures semantic bloat. It calculates the density of unverified domain jargon (e.g., "scalable") relative to logic lines, but amplifies this penalty using the file's mean McCabe cyclomatic complexity: π modifier: complexity amplifier that increases the inflation penalty as mean McCabe complexity rises.
Any complexity above the absolute minimum immediately acts as an inflation multiplier. An AI-generated monolithic function cannot hide behind its own algorithmic weight.
4. Explicit Pattern Penalties: Hard structural violations (like deeply nested loops or functions exceeding 50 lines) are assigned strict, linear penalty scores (pattern_penalty) and categorized by severity (e.g., Critical, High).
These inputs describe a file in isolation. The next problem is architectural repetition across files.
3. Detecting AI Clones: Jensen-Shannon Divergence (JSD)

When an AI agent is lost, it hallucinates architectural redundancy. It generates structurally identical boilerplate across multiple files, just changing variable names. A standard regex linter cannot catch this.
To find these clones, we measure the distance between the DCFs of two files using Jensen-Shannon Divergence (JSD). Using as our distance metric, proven to satisfy the triangle inequality and form a true metric space by Endres & Schindele (2003), the detector constructs a complete graph of the repository.
To measure project-wide coherence, we compute Prim's Minimum Spanning Tree (MST) on this graph. The MST finds the backbone of structural similarity. The longest edge in this MST represents the worst-case structural divergence in the repository:
The longest edge is used rather than the average because it represents the single most structurally divergent file pair. If even that worst-case edge is short , it is a strong structural indicator that the entire project is a single, cloned architectural cluster.
π§ Note: To prevent false positives from intentionally identical files like MVC templates or test fixtures, the detector enforces strict "Rule-0 Exclusions" on test directories and structural boilerplate before computing coherence.
Detecting similarity is useful. Governance begins only when the score can no longer be ignored.
Development optimizes for "does it run."
Science optimizes for "what is its structure."
This framework forces those two worlds to meet.
4. Measurement Becomes Governance: The Geometric Quality Gate (GQG)

Metrics are useless if they can be gamed. If we use a simple arithmetic average, an AI can write one catastrophic 2,000-line monolithic file and hide it by generating 50 perfectly clean wrapper files.
The arithmetic average would still look great.
This is where the Dev vs. Science gap is bridged.
We integrate scientific measurement into a strict CI/CD governance pipeline.
The engine aggregates the core inputs into a Geometric Quality Gate (GQG). It combines the continuous metrics (LDR, ICR, DDC) with a fourth dimension, Purity , directly inside the geometric mean: π Omega: weighted geometric aggregate that acts as the core AND-gate of the quality gate.
Because the GQG uses a weighted geometric mean, it acts as an unforgiving AND-gate. The Purity dimension has a fixed, non-calibratable architectural weight .
Because , this dimension drags the entire score down proportionally in log space whenever critical patterns exist, causing an exponential collapse of the final score.
To guarantee governance, the engine calculates the definitive deficit_score (where is a critical failure):
The two mechanisms operate on different dimensions.
The Purity term inside the GQG degrades the structural quality score continuously.
The pattern_penalty addresses discrete rule violations that warrant a hard override regardless of the overall structural score.
This is an intentional two-layer fail-closed design, not double counting. The
min(..., 100.0) cap ensures the metric remains a bounded percentage, representing 100 as the absolute theoretical worst. The AI cannot "average out" its mistakes.A hard gate solves one problem. It immediately raises another.
5. Defeating Arbitrary Heuristics: Empirical Self-Calibration

Critics point out a valid flaw. The math is objective, but the weights in the GQG formula are chosen by a human. Isn't that just another biased Dev heuristic?
To address this, the detector includes an adaptive Self-Calibration Engine. It parses a project's actual Git history to observe empirical developer behavior, tracking the mathematical delta:
β«οΈTrue Positive: The tool flagged a file, and a subsequent human commit caused the structural deficit score to drop by more than 10 points. This 10-point drop is an operationally defined threshold to filter out minor typo fixes, ensuring the engine only learns from substantial structural refactoring.
β«οΈFalse Positive: The tool flagged a file, but the developer ignored it (the file hash remained unchanged) across 10 or more consecutive commits.
The engine runs a continuous grid search over the weight simplex to find weights ($w_i$) that best predict that specific team's refactoring behavior. The goal is not universal bias elimination. It is team-specific weight optimization: the engine learns what structural problems a given team consistently prioritizes, and calibrates accordingly.
During a live calibration run on the ai-slop-detector's own Git history (180 unique files, 62 improvement events), the engine discovered that the default weight for DDC (Dependency Hallucination) was too low. The optimal weight shifted from the baseline The Git history proved that our developers were consistently prioritizing the removal of dead AI imports over raw logic density.
This weight is specific to our team's habits.
That is exactly the point. A weight profile calibrated to a financial trading team will look different from one calibrated to a medical device team.
The engine produces a GQG profile that reflects the specific domain and architectural priorities of each team, without arbitrary human guessing.
At that point, the framework is no longer just mathematically defined. It is operationally anchored.
Conclusion: The Record Either Exists or It Does Not

The record either exists or it does not.
If you cannot produce the record, you do not have governance.
You have belief.
Mathematics cannot be prompt-injected.
While an LLM judge can be flattered, confused, or overridden by a system prompt, Jensen-Shannon Divergence does not care if the AI left a polite comment. A Geometric Quality Gate cannot be talked out of throwing a HALT in your CI pipeline.
The harness was the first layer. It controlled what the model could see, what tools it could use, and what actions it could take. The missing layer is not a better harness. It is independent measurement that the harness cannot override.
For code, that measurement starts with translating ASTs into probability distributions. It continues through information-theoretic divergence analysis. It enforces itself through a geometric quality gate that no arithmetic averaging can defeat. It calibrates itself through empirical Git history rather than human guesswork.
This is what mathematical governance looks like at the code layer. Not a policy document. Not an LLM-as-a-judge wrapper. A measurement system that produces a record, at machine speed, that cannot be argued away after the fact.
The demand for that record is already arriving.
This argument does not stand alone inside the broader Flamehaven writing arc.
- When the Michelin Recipe Fails in Your Kitchen argued that the gap between "published" and "usable" is often the main event, not a rounding error.
- The AI Flight Crash made the same point from the deployment side: the gap between "demo works" and "system ships" is where most projects die.
This article narrows that broader thesis down to one specific layer of the problem: code quality drift that remains invisible under binary evaluation.
From teams who deployed AI coding agents and are now untangling the structural debt. From organizations in regulated domains who cannot accept "the tests passed" as a governance answer. From anyone who has ever asked an AI reviewer to evaluate AI-generated code and received a confident approval for code that was architecturally empty.
Build the measurement layer before the drift compounds.
If you want to run this mathematical gauntlet on your own repos, check out the open-source detector here: [GitHub: AI-Slop-Detector]
Related Arguments in Prior Essays
- AI Agents Are Poisoning Your Codebase From the Inside β how passing tests and green CI can still conceal architectural degradation.
- When the Michelin Recipe Fails in Your Kitchen β why the gap between published results and usable systems is often the real story.
- The AI Flight Crash β why most impressive demos die in the distance between runnable and shippable.
- The Repo Is Right There. Why Are You Checking Their CV? β why production trust should start with the artifact, not the credential.
Share
Related Reading
Scientific & BioAI Infrastructure
Bio-AI Repository Audit 2026: A Technical Report on 10 Open-Source Systems
Scientific & BioAI Infrastructure
I Audited 10 Open-Source Bio-AI Repos. Most Could Produce Outputs. Few Could Establish Trust.
Scientific & BioAI Infrastructure