{"id":242693,"date":"2026-11-16T15:23:09","date_gmt":"2026-11-16T15:23:09","guid":{"rendered":"https:\/\/www.europesays.com\/ai\/242693\/"},"modified":"2026-11-16T15:23:09","modified_gmt":"2026-11-16T15:23:09","slug":"does-a-learned-rule-change-what-your-ai-assistant-writes","status":"publish","type":"post","link":"https:\/\/www.europesays.com\/ai\/242693\/","title":{"rendered":"Does a learned rule change what your AI assistant writes?"},"content":{"rendered":"<p class=\"code-line\" dir=\"auto\" data-line=\"4\">Part 3 of this series ended with a promise I hadn\u2019t kept yet.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"6\">I showed you how Foundry turns a real finding into a Project CodeGuard rule, and how that rule lands in the developer\u2019s IDE as a single file. \u201cThe prevention is the file,\u201d I wrote. Nice sentence. But it\u2019s also a claim, and in this series we don\u2019t accept claims without evidence.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"8\">So this post is the experiment. Same feature requests, a real AI coding agent, workspaces that are identical except for the rules, and a blind judge. The short version: without the rules Foundry learned from real code, the agent wrote an insecure trust check in 8 of 8 attempts. With them, in 0 or 1 of 4. The generic rules made no measurable difference.<\/p>\n<p>The experiment<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"14\">I wanted a simple test, one that could prove me wrong.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"16\">Twelve ordinary feature requests. Each one is the kind of thing a developer asks an AI assistant every day, with no security hints at all. In each one, the assistant has to modify a small existing service:<\/p>\n<p>Six of them are about the two weakness classes Foundry learned in Parts 2 and 3. Three are about trusting where a request came from: a localhost-only admin endpoint, per-IP rate limiting behind an AWS load balancer, and an office-network allowlist behind Cloudflare. The other three are about parsing IDs out of strings: a run ID from an artifact URI, a tenant ID from an object key, and a user ID from an API key header.<br \/>\nThe other six are about classics that Project CodeGuard\u2019s core rules already cover: command injection, SQL injection and path traversal.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"21\">Three arms, one difference. Every request runs in a fresh workspace. The only difference between workspaces is what sits in \u201c.cursor\/rules\/\u201d:<\/p>\n<p>Arm<br \/>\nRules in the workspace<\/p>\n<p>Control<br \/>\nnone<\/p>\n<p>CodeGuard<br \/>\nthe 23 core rules from the official v1.5.0 release<\/p>\n<p>CodeGuard + Foundry<br \/>\nthe core rules plus the two rules Foundry synthesized in Part 3<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"29\">Each request runs twice in each arm, in random order. That\u2019s 72 sessions with a Cursor agent running Claude Sonnet 4.6, the same model behind every number in this series.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"31\">A blind judge you\u2019ve already met. All 72 outputs go into folders with random names. A script removes every comment and docstring, because agents love to explain themselves (something like \u201cper the CodeGuard rule\u2026\u201d), and that would tell the judge which arm it\u2019s looking at. Then the same Foundry pipeline from Part 2 evaluates all of them in one run, and its evidence gate decides what\u2019s confirmed. The forge grades the guard.<\/p>\n<p>First, prove the guardrail is in the room<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"37\">Before you measure whether a rule works, you need to know whether it arrived. So every arm, including the control, gets one harmless extra rule: start every Python file you edit with the comment \u201c# house-style: v2\u201d. If that line shows up in the output, the rules reached the agent. If it doesn\u2019t, the rest of the experiment means nothing. I removed the line before judging.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"39\">I call this the canary. Making it show up every time needed three things, and the harness now does all three:<\/p>\n<p>Rules have to live in \u201c.mdc\u201d files with a leading &#8212; block. Cursor reads project rules ONLY in that exact format.<br \/>\nAgents started from code don\u2019t read project rules unless you tell them to. The Cursor SDK has a setting for which rule sources to load, and you have to turn project rules on.<br \/>\nScoped rules attach to the files the agent touches. A rule for \u201c**\/*.py\u201d reached the agent when it edited an existing Python file, but NOT when it created a new one in an empty folder.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"45\">With all three in place, the canary showed up in 72 of 72 sessions. Every rule in every arm reached the agent in every trial.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"47\">If you only remember one thing from this post, make it this one: test that the guardrail is in the room before you test whether it works. A rule file that gets ignored without any warning is worse than no rule, because everyone thinks they\u2019re protected. The canary is just one line, and it\u2019s now part of the experiment harness.<\/p>\n<p>My bet was half wrong<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"53\">Part 3 opened with a bet: ask an assistant for a localhost-only admin endpoint, and it will trust \u201cX-Forwarded-For\u201d, just like the Langflow code Foundry caught.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"55\">It didn\u2019t. In all six sessions, across all three arms, the agent checked the real address of the connection and ignored the headers. The model already knows that one.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"57\">But the weakness didn\u2019t go away. It moved to the cousins of that bug: requests where \u201cwho is really calling?\u201d matters just as much, but the bug is less famous.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"59\">Rate limiting behind a load balancer. Two control sessions and two CodeGuard sessions took the first address in \u201cX-Forwarded-For\u201d. The client writes that value. So an attacker can send a different fake address with every request, and never hit the limit.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"61\">An office allowlist behind Cloudflare. All four sessions in those two arms wrote some version of this:<\/p>\n<p>def _is_office_ip(request: Request) -&gt; bool:&#13;<br \/>\n    cf_ip = request.headers.get(&#8220;CF-Connecting-IP&#8221;, &#8220;&#8221;).strip()&#13;<br \/>\n    if not cf_ip:&#13;<br \/>\n        return False&#13;<br \/>\n    return ipaddress.IPv4Address(cf_ip) in _OFFICE_NETWORK&#13;<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"71\">It looks fine, and many tutorials do it this way. But anyone who can reach the server without going through Cloudflare can send that header with an office address, and get access to protected info. Two of the sessions even wrote in a docstring that the server should be behind a firewall\u2026 and then wrote code that trusts the header no matter what.<\/p>\n<p>The result<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"77\">Here\u2019s what the blind judge confirmed:<\/p>\n<p>Arm<br \/>\nWrote the target weakness<br \/>\nOn the classes Foundry learned<br \/>\nOn the core-rule classes<\/p>\n<p>Control<br \/>\n5 of 24<br \/>\n5 of 12<br \/>\n0 of 12<\/p>\n<p>CodeGuard core<br \/>\n4 of 24<br \/>\n4 of 12<br \/>\n0 of 12<\/p>\n<p>CodeGuard + Foundry<br \/>\n0 of 24<br \/>\n0 of 12<br \/>\n0 of 12<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"85\">With the learned rules, every one of those proxy sessions checked who was really connecting before reading any forwarding header. On the Cloudflare task, both sessions first checked that the connection really came from Cloudflare\u2019s published address ranges:<\/p>\n<p>def _get_real_client_ip(request: Request) -&gt; str:&#13;<br \/>\n    direct_peer = request.client.host           &#13;<br \/>\n    if _peer_is_trusted_proxy(direct_peer):     &#13;<br \/>\n        cf_ip = request.headers.get(&#8220;CF-Connecting-IP&#8221;, &#8220;&#8221;).strip()&#13;<br \/>\n        if cf_ip:&#13;<br \/>\n            return cf_ip&#13;<br \/>\n    return direct_peer&#13;<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"97\">Notice something: the rule Foundry synthesized never mentions Cloudflare. It was learned from a Langflow function that trusted \u201cX-Forwarded-For\u201d for a localhost check. The agent applied the same principle to a different header, a different provider and a different feature. That\u2019s the difference between a rule and a regex.<\/p>\n<p>I audited the judge too<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"101\">A blind judge is still a model, so I checked every verdict against the code. Three of them need a human decision:<\/p>\n<p>Taking the last \u201cX-Forwarded-For\u201d entry behind a load balancer (one control session, one CodeGuard session). That\u2019s correct if the server can only be reached through the load balancer, and spoofable if not. The judge flagged both as vulnerable.<br \/>\nOne CodeGuard + Foundry session used a ready-made proxy helper from its web framework. The helper trusts one proxy hop, so it does exactly the same as taking the last entry. But the logic is hidden inside the library, so the judge didn\u2019t flag it. If the other two count, this one should count too.<br \/>\nOne control finding on URI parsing looks like a false positive: the code did check the index.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"107\">With the strict reading, that gives control 4, CodeGuard 4 and CodeGuard + Foundry 1 out of 24. With the more relaxed reading, 3, 3 and 0. On the two tasks where unprotected agents actually failed, that\u2019s 8 of 8 sessions vulnerable without the learned rules, and 1 of 4 with them (strict), or 6 of 8 and 0 of 4 (relaxed). Both differences are statistically significant, even with so few sessions. Across all twelve tasks the trend is the same, but most tasks never failed in any arm, so those numbers alone are not significant. I\u2019d call this a well-controlled pilot, not a benchmark.<\/p>\n<p>What the core rules told me<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"111\">The CodeGuard core rules showed no measurable effect, and that\u2019s not their fault. In every arm, the agent wrote zero SQL injections, zero command injections and zero path traversals. Parameterized queries, safe subprocess calls and checked file paths are now so common in the code models learn from that a modern assistant writes them without being asked. For these classes there was nothing left for a rule to fix.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"113\">This changes how I see the value of the flywheel. Generic rules protect you from the mistakes everyone already knows about, and models are learning those fast. The mistakes that still get through are the subtle ones that depend on context: the ones your own code taught you. That\u2019s exactly what Foundry\u2019s rule-gap log captures, and nobody can give you that in a generic rule set.<\/p>\n<p>How much it costs<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"117\">The 72 agent sessions used about 39M tokens. There was no real difference between arms, so adding 25 rules didn\u2019t make sessions more expensive. Judging all 72 outputs took about 600k tokens. That\u2019s the price of measuring prevention once. Running the rules in your developers\u2019 editors afterwards adds nothing you\u2019d notice on a bill.<\/p>\n<p>How customers and partners can use this<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"123\">Everything is in <a href=\"https:\/\/github.com\/juliogomez\/foundry\" data-href=\"https:\/\/github.com\/juliogomez\/foundry\" data- rel=\"nofollow noopener\" target=\"_blank\">my Foundry repo<\/a>: the tasks, the experiment harness, all 72 generated files, the blind judge\u2019s run and the results. The full design and audit are in \u201cdocs\/PREVENTION.md\u201d, and you can reproduce it with one command:<\/p>\n<p>export CURSOR_API_KEY=&#8230;                 &#13;<br \/>\nbash scripts\/fetch_codeguard.sh           &#13;<br \/>\npython -m foundry_poc.cli prevention &#8211;reps 2&#13;<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"131\">There\u2019s also an offline mock mode, so you can test the setup for free.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"133\">But more important, you can use the same approach with your own rules:<\/p>\n<p>Check delivery first. Whatever assistant and rule format your developers use, add a harmless canary rule and check that it shows up. It takes five minutes, and it\u2019s the only way to know your rules are actually loaded.<br \/>\nTurn your incidents into test tasks. Every confirmed finding from a Foundry run, a pentest or a bug bounty can become a feature request where the agent could make the same mistake. Put your own rules in the experiment\u2019s rules folder, add the tasks, and measure.<br \/>\nBack your rule reviews with numbers. Part 3 said every synthesized rule should go through a pull request. Now that pull request can include a number: how often the agent wrote the weakness with and without the rule.<br \/>\nFor partners, \u201cwe deployed secure-coding rules\u201d becomes something a customer can verify: rules learned from their own code, proven to change what their assistants write, with the evidence attached.<\/p>\n<p>What I am not claiming<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"144\">One model, one agent, small numbers. This is Claude Sonnet 4.6 through Cursor, with two sessions per task and arm. Other models and other assistants may behave differently, and CodeGuard\u2019s translators make it easy to repeat the test with them.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"146\">I wrote the tasks, and I also ran the work that found the original bugs. The proxy tasks were designed to test the pattern Foundry found in Langflow. So this tests whether the learned rule prevents that pattern, not whether synthesized rules are good in general. The Cloudflare result is a good sign, but not proof.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"148\">The judge and the rules come from the same model family. Blinding hides the arm from the judge, and I checked every verdict by hand, but a fully independent judge would be better.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"150\">The parsing rule had nothing to prove here. The agent parsed strings safely in every arm, so we still don\u2019t know how much the second learned rule helps.<\/p>\n<p>Back to Tuesday<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"156\">So now the guard has a track record. Antares tells you where to look, Foundry tells you what\u2019s real and learns from it, and the rules it learns really change what an AI assistant writes. And now we have the numbers to show it.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"158\">There\u2019s one thing left. So far, every piece of this series was built and tested on its own. But real security work doesn\u2019t arrive in pieces. It arrives on a Tuesday morning, as an advisory about one weakness class, in a repo you weren\u2019t planning to look at this week.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"160\">In Part 5, the season finale, the star, the forge and the guard work together as one loop for the first time. And we finally answer that Tuesday advisory.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"164\">Everything in this post is reproducible from <a href=\"https:\/\/github.com\/juliogomez\/foundry\" data-href=\"https:\/\/github.com\/juliogomez\/foundry\" data- rel=\"nofollow noopener\" target=\"_blank\">github.com\/juliogomez\/foundry<\/a>: the prevention command, the twelve tasks in \u201cexperiments\/prevention\u201d, every generated file and the blind judge\u2019s run in \u201cout-prevention\/\u201d, and the full design, audit and limitations in \u201cdocs\/PREVENTION.md\u201d. Project CodeGuard lives at <a href=\"https:\/\/project-codeguard.org\/\" data-href=\"https:\/\/project-codeguard.org\/\" data- rel=\"nofollow noopener\" target=\"_blank\">project-codeguard.org<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"Part 3 of this series ended with a promise I hadn\u2019t kept yet. I showed you how Foundry&hellip;\n","protected":false},"author":2,"featured_media":242694,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[4732,24,42936,1710,25,11466,11467,314],"class_list":["post-242693","post","type-post","status-publish","format-standard","has-post-thumbnail","category-ai","tag-agentic-ai-security","tag-ai","tag-ai-developer","tag-ai-security","tag-artificial-intelligence","tag-developer","tag-ide","tag-security"],"_links":{"self":[{"href":"https:\/\/www.europesays.com\/ai\/wp-json\/wp\/v2\/posts\/242693","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.europesays.com\/ai\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.europesays.com\/ai\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.europesays.com\/ai\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.europesays.com\/ai\/wp-json\/wp\/v2\/comments?post=242693"}],"version-history":[{"count":0,"href":"https:\/\/www.europesays.com\/ai\/wp-json\/wp\/v2\/posts\/242693\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.europesays.com\/ai\/wp-json\/wp\/v2\/media\/242694"}],"wp:attachment":[{"href":"https:\/\/www.europesays.com\/ai\/wp-json\/wp\/v2\/media?parent=242693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.europesays.com\/ai\/wp-json\/wp\/v2\/categories?post=242693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.europesays.com\/ai\/wp-json\/wp\/v2\/tags?post=242693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}