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$).
Evolution runs as a constrained four-stage loop:
- TEST: Executes the agent on training tasks, logging which library docs it opened, its code diffs, and test outcomes.
- 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.
- DRAFT (Generalization): Rewrites principles for a class of code, stripping out any task-specific variable or function names.
- 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@1above a strict non-inferiority floor relative to the unaided agent.
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@1never 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.formatrule 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