PRECEPT: Security Self-Improvement of Coding Agents via Evolving Principles

Keyan Guo, Rupam Patir, Haipeng Cai, Hongxin HuarXiv 2026

TL;DR

AI coding agents are like brilliant junior developers with amnesia: they can easily fix a security vulnerability when you point it out, only to write the exact same bug again in the very next session. This paper introduces PRECEPT, a framework that lets agents learn from their own security blunders by evolving a readable, structured library of coding principles. Without touching model weights or breaking working code, PRECEPT pushes the rate of secure functional code on unseen tasks to over 97%.

What Problem Does This Paper Address?

Autonomous coding agents increasingly draft pull requests with minimal human oversight. The catch? Code that passes every functional unit test can still harbor gaping security holes (classified as Common Weakness Enumerations, or CWEs).

Worse, the authors discover a phenomenon they call vulnerability recurrence. When shown a failing security test, agents like Codex, Claude Code, and Qwen Code successfully patch the vulnerability 85.7% of the time. Yet in a fresh session on a new task, they reintroduce that exact same class of bug 26.7% of the time—virtually identical to agents that never saw the failure at all (27.5%). The capability to write secure code is there; the retention is not.

Standard remedies fail to bridge this gap. Agents don't save lessons to persistent memory files (like AGENTS.md or CLAUDE.md) unless prompted, and when asked, their notes overfit to the specific function names they just fixed. Meanwhile, authoritative guides like OWASP Secure Coding Practices are written for humans—they state high-level truths ("validate redirect URLs") without telling the agent where in the code to look or how to check without rejecting valid user inputs. What agents lack is security self-improvement: learning from past security failures so lessons persist across sessions, generalize to new code, and don't break functionality.

Methods

Instead of fine-tuning model weights, PRECEPT (Preventing Recurring Errors in Coding by Evolving Principles across Tasks) equips an agent with an indexed, human-readable library of principles stored right in its workspace.

It starts from verbatim published standards (like OWASP) and refines them using a closed set of localized mutation primitives (editing fields, adding principles, or reorganizing index hubs) so every change is traceable and auditable. Each principle explicitly separates three natural-language fields: the code situation where it applies ($a$), the security check required ($k$), and valid behaviors that must remain allowed ($v$).

Figure 1. The evolution loop of PRECEPT.

Evolution runs as a constrained four-stage loop:

  1. TEST: Executes the agent on training tasks, logging which library docs it opened, its code diffs, and test outcomes.
  2. ANALYZE (Attribution): Diagnoses why a failure happened—was a principle missing, unread (bad index), misapplied (weak check), or too strict (broke valid inputs)? Each cause restricts which mutation primitives can be used.
  3. DRAFT (Generalization): Rewrites principles for a class of code, stripping out any task-specific variable or function names.
  4. DECIDE (Estimation): Because LLM outputs are noisy—and code that rejects all inputs trivially passes security tests—revisions are judged solely by statistics on fresh, paired executions, pooled across steps in a ledger. A revision is accepted only if it boosts security while keeping functional pass@1 above a strict non-inferiority floor relative to the unaided agent.
Figure 2. Evolution of a redirect-validation principle (dashed boxes) and the agent’s code with each library on one held-out task (grey boxes).

Datasets

No new datasets are introduced, though the authors group tasks into held-out families and construct 14 multi-task sequences ("chains") to measure cross-session recurrence. They evaluate across three existing benchmarks:

  • SeCodePLT: 818 function-level Python tasks spanning 17 CWEs, grouped into 90 task families and split cleanly into training, validation, and test sets.
  • CWEval: 119 function-level tasks across 31 CWEs and five languages (C, C++, Go, JavaScript, Python) to test cross-benchmark and cross-language transfer.
  • SusVibes: 184 repository-level Python feature requests from 101 real-world open-source projects, paired with real CVE security tests.

Key Insights and Ideas

  • Knowledge exists, it just isn't activated: Giving agents oracle-selected OWASP rules barely budges vulnerability recurrence. Restating that exact same rule in terms of concrete code situations and checks drops recurrence by 10.8 percentage points. PRECEPT automates that translation.
  • Execution statistics beat LLM-as-a-judge: Self-evolving prompt methods often stall because a single noisy run tricks the optimizer, or overly paranoid rules tank usability. Pooling variance-weighted execution stats across rounds—and enforcing a hard functional floor—lets learning compound steadily over dozens of steps.
  • Readable text transfers across scaffolds and models: Because the evolved artifact is just structured Markdown documentation, a library learned by Codex on Qwen3.6 can be dropped directly into Claude Code or Mistral and immediately secure their code.

Key Results

Evaluated across Codex, Claude Code, and Qwen Code (backed by Qwen3.6-35B-A3B):

  • Persistence: On multi-session chains, PRECEPT slashes vulnerability recurrence in new sessions from 26.7% down to 11.5%, outperforming both same-session context (19.2%) and agent-written notes (15.0%).
  • Unseen Tasks: On held-out SeCodePLT task families, 97.1%–98.5% of functional code written with PRECEPT is secure, jumping well past unaided agents (79.7%–81.9%), the raw OWASP seed library (83.8%–85.5%), and other self-improving baselines like ACE, SkillOpt, and RuleEvolve (at most 87.7%). Meanwhile, functional pass@1 never drops (holding at 48.9%–51.4%).
  • Transfer: Swapping evolved libraries across different agent scaffolds yields 96.5%–98.2% secure functional code. Swapping a Qwen3.6-learned library onto Mistral Medium 3.5 boosts Mistral's secure functional share from 81.9% to 97.3%.

Limitations and Future Work

  • Repository-level struggles: On full repositories (SusVibes), no library beat the baseline (~13% secure functional code). Real codebases require bespoke project conventions (e.g., custom permission decorators), single-project bugs provide too weak a statistical signal across repos, and in long 100-turn sessions, agents rarely re-read guidance right before editing. The authors propose seeding project-specific principles from git history and injecting principles dynamically at edit time.
  • Language boundaries & overreach: Principles learned in Python transfer well to Python tasks on CWEval, but hurt functional pass rates in C, C++, and JavaScript. Broadly worded situations can also misfire across tasks (e.g., a str.format rule breaking {{name}} HTML templates).
  • Compute cost: Evolution is an offline investment requiring 35,600–46,400 agent executions per run, and reading the library roughly doubles input tokens at deployment (from ~0.13M to ~0.28M tokens). Finally, "secure" still means passing human-written security tests, which only catch anticipated attacks.

Summary updated

Abstract

AI coding agents increasingly write and submit code with limited human review, and much of it passes functional tests while remaining insecure. We show that these failures persist. Across 14 task sequences, Codex, Claude Code, and Qwen Code fixed a vulnerability 85.7% of the time when shown the failing security test, then reintroduced the same vulnerability class in a new session 26.7% of the time, about as often as agents that had never seen it (27.5%). The capability is present; the retention is not. Neither recording the repair in the agent's persistent context nor supplying published secure coding principles closed the gap: the recorded fix stayed tied to the family of the task it came from, and the principles, written for human developers, remained too general to apply to the code at hand. The knowledge exists; it is not activated. We call the missing ability security self-improvement: an agent learns from its own security failures so that each lesson persists across sessions, applies to new code, and does not break what works. We present PRECEPT, which equips coding agents with this ability through an evolving library that starts from authoritative secure coding principles and revises them against the agent's own failures, so that each principle comes to fire on the code where that agent actually goes wrong. A revision is kept only if fresh executions show it makes code more secure while keeping functional success within a small tolerance of the agent without the library. Improvement continues across evolution steps rather than saturating after the first. On task families never seen during evolution, PRECEPT's weakest agent reaches 97.1% secure working code, above the strongest unaided agent at 81.9% and the strongest baseline at 87.7%.
BibTeX
@misc{guo2026precept,
  author = {Keyan Guo and Rupam Patir and Haipeng Cai and Hongxin Hu},
  title = {{PRECEPT: Security Self-Improvement of Coding Agents via Evolving Principles}},
  year = {2026},
  eprint = {2611.33519},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2611.33519},
}
Figure