<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kevin Alemán</title>
    <description>The latest articles on DEV Community by Kevin Alemán (@kaleman15).</description>
    <link>https://dev.to/kaleman15</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F255126%2F6bdaeb84-6c1e-4e76-9d45-54c59eaac908.jpeg</url>
      <title>DEV Community: Kevin Alemán</title>
      <link>https://dev.to/kaleman15</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kaleman15"/>
    <language>en</language>
    <item>
      <title>Who gets to own the AI we build on?</title>
      <dc:creator>Kevin Alemán</dc:creator>
      <pubDate>Sat, 05 Dec 2026 16:18:58 +0000</pubDate>
      <link>https://dev.to/kaleman15/who-gets-to-own-the-ai-we-build-on-40l7</link>
      <guid>https://dev.to/kaleman15/who-gets-to-own-the-ai-we-build-on-40l7</guid>
      <description>&lt;p&gt;There's money in building a great AI model. There's likely more money in being the &lt;em&gt;only&lt;/em&gt; company that can build one. What do Anthropic and OpenAI stand to gain by slowing the race?&lt;/p&gt;

&lt;p&gt;OpenAI and Anthropic can both come out ahead under stronger safeguards. If both have to test their next model before shipping it, neither has to worry as much about the other signing up customers by skipping the tests. Meanwhile, their investors can fund the work and their customers can keep paying for the models already on sale. Dario Amodei &lt;a href="https://darioamodei.com/post/we-must-pace-the-frontier" rel="noopener noreferrer"&gt;makes that case himself&lt;/a&gt;: he wants time for safety work “without sacrificing commercial advantage.”&lt;/p&gt;

&lt;p&gt;In 1958, the U.S., Britain and the Soviet Union &lt;a href="https://history.state.gov/historicaldocuments/frus1961-63v07-09mSupp/summaryvii" rel="noopener noreferrer"&gt;suspended nuclear testing&lt;/a&gt;. They spent the next three years negotiating a ban. In September 1961, the Soviets resumed testing. &lt;a href="https://www.presidency.ucsb.edu/documents/the-presidents-news-conference-190" rel="noopener noreferrer"&gt;Kennedy said afterward&lt;/a&gt; that Moscow had prepared the series in secret while the talks were still going on. Now, was it because each other didn't trust themselves enough to actually &lt;em&gt;pause&lt;/em&gt; the development?&lt;/p&gt;

&lt;p&gt;OpenAI ran into that uncertainty in September. &lt;a href="https://openai.com/index/people-must-remain-in-control-of-the-future/" rel="noopener noreferrer"&gt;Sam Altman and Greg Brockman&lt;/a&gt; say they paused training and evaluation of their most capable models for safety work “at a real competitive cost to OpenAI.” Now, let's think for a second: I run a company that wants to make money. I publicly call for pacing the frontier, then I pause training for safety. If my rival keeps training and signs up the customers I was counting on, what are the odds I'm going to lose money being nice a second time?&lt;/p&gt;

&lt;p&gt;Both companies want their model to be the gold standard for AI: the one developers reach for when they start a project and keep paying for once it ships. If I can persuade my rival to slow down while I keep going, I get the first shot at becoming that default. That's where the money is, and there is where the problem starts.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://darioamodei.com/post/we-must-pace-the-frontier" rel="noopener noreferrer"&gt;We Must Pace the Frontier&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://darioamodei.com/post/we-must-pace-the-frontier-part-2" rel="noopener noreferrer"&gt;second essay&lt;/a&gt; puts the decision in government hands: an agency could block training or deployment when a model crosses a capability threshold and its developer hasn't met the safety requirements. Amodei limits the proposal to developers at or near the frontier, but, again, if you ask the referee to help slow down everyone else when they are close to you, why don't just slow down &lt;em&gt;everyone&lt;/em&gt;?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://darioamodei.com/post/we-must-pace-the-frontier-part-2" rel="noopener noreferrer"&gt;We Must Pace the Frontier, Part 2&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://openai.com/index/people-must-remain-in-control-of-the-future/" rel="noopener noreferrer"&gt;Altman and Brockman&lt;/a&gt; argue that competition keeps power from concentrating in one lab. And they are right! However, they also propose limits on how far models can advance beyond human capabilities. That's exactly what we need!, you could think, but, who do you think it's gonna decide when an &lt;em&gt;advanced&lt;/em&gt; model is too advanced? If you're thinking of an independent and incorruptible panel of experts, then you are WRONG. &lt;a href="https://openai.com/index/perplexity-and-intelligence/" rel="noopener noreferrer"&gt;OpenAI would be the one creating the benchmark.&lt;/a&gt; Could that be used to slow down rivals? Of course not...&lt;/p&gt;

&lt;h2&gt;
  
  
  Everyone else that's not Anthropic and OpenAI
&lt;/h2&gt;

&lt;p&gt;Suppose a university team and a commercial lab build models with comparable capabilities. I'd expect both to answer the same hard questions about what their models can do. But there's a difference between requiring evidence and requiring a particular way of producing it. If the only accepted route involves retaining an audit team the university can't afford, the commercial lab gets to ship. The university's cheapest option might be to abandon its own model and buy access to the one that's vetted. The rule can say nothing about who owns the model and still favor someone.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.yanisvaroufakis.eu/2023/09/07/technofeudalism-has-just-arrived/" rel="noopener noreferrer"&gt;Yanis Varoufakis's technofeudalism&lt;/a&gt; illustrates it very well. Paying someone to run GPUs is a perfectly good trade. The problem is what happens when you can't take your work anywhere else. You build the application, earn your customers' trust and take the support calls. Someone else can raise the price of every request or retire the model, and you get to explain the consequences to your customers. Varoufakis calls the income owners can extract from dependence on their platforms &lt;em&gt;cloud rent&lt;/em&gt;. At some point, you will &lt;em&gt;always&lt;/em&gt; have to pay them for something, cause there would be no viable options.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Open Source helps
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2ahjx2xxpsdhdgq1uut1.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2ahjx2xxpsdhdgq1uut1.webp" alt="A technician moves an orange cartridge between two servers with matching sockets." width="800" height="534"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Take a team routing support tickets in Spanish. They've tested a model on the difficult cases: a billing question that sounds like a cancellation, or a regional expression that gets mistaken for abuse. A new model might do better overall and still get those cases wrong. What you win is control. You know what you're using and you can use it as long as you want. You are not paying rent, you own.&lt;/p&gt;

&lt;p&gt;Downloadable weights already give you part of that freedom. Open source goes further because the next person may want to understand or change how the model was built. &lt;a href="https://allenai.org/olmo2" rel="noopener noreferrer"&gt;Ai2's OLMo 2&lt;/a&gt;, for example, includes training code, data, recipes and intermediate checkpoints. A researcher investigating a failure can compare stages of training and share what they find without needing a job at Ai2. That's why the &lt;a href="https://opensource.org/ai/open-source-ai-definition" rel="noopener noreferrer"&gt;Open Source Initiative's definition&lt;/a&gt; asks for the code and information about the training data as well as the parameters. Being able to run the finished product and being able to work on it are different freedoms.&lt;/p&gt;

&lt;p&gt;The hardest argument against releasing model weights is also the strongest argument for keeping them: once I have a copy, the lab can't switch it off. That protects my support team's working software. It also means the lab can't recall a dangerous model. AI companies says that having access to powerful AI is problematic, unsafe, etc. They say that you should not use that, it's bad for your health!&lt;/p&gt;

&lt;p&gt;But, currently, how do we know these companies have no "dangerous" models that can be used against us? :)&lt;/p&gt;

&lt;p&gt;Worst part is that you don't have to read between lines to see what they plan and why OSS models get in between. They say it out loud. Obviously, they want to frame it as security cause AI could end humanity. So they propose expensive processes (that limit everyone but them from competing) to validate the results.&lt;/p&gt;

&lt;p&gt;Now, suppose that one of the rules placed on the regulations is: "every model must have the ability to be recalled if deemed unsafe". An open model would fail before even being released. If that happens, we'd have written a rule about &lt;em&gt;who&lt;/em&gt; can own the software and called it a &lt;em&gt;safety standard&lt;/em&gt;.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>opensource</category>
      <category>programming</category>
    </item>
    <item>
      <title>If software is solved, why do we keep finding bugs?</title>
      <dc:creator>Kevin Alemán</dc:creator>
      <pubDate>Tue, 24 Nov 2026 18:10:23 +0000</pubDate>
      <link>https://dev.to/kaleman15/if-software-is-solved-why-do-we-keep-finding-bugs-amc</link>
      <guid>https://dev.to/kaleman15/if-software-is-solved-why-do-we-keep-finding-bugs-amc</guid>
      <description>&lt;p&gt;Sixteen Claude agents wrote a C compiler that could build Linux on x86. I would've guessed that meant it could compile the little bit of 16-bit code Linux needs to boot, too. &lt;a href="https://www.anthropic.com/research/building-c-compiler" rel="noopener noreferrer"&gt;Nicholas Carlini explains what happened there&lt;/a&gt;: the agents' compiler produced over 60 KB of startup code, but Linux only had room for 32 KB. Fortunately for the new C compiler, the old one was still around. GCC built the startup code small enough to fit and everyone was happy after, except for the agents.&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;div class="c-embed__content"&gt;
        &lt;div class="c-embed__cover"&gt;
          &lt;a href="https://www.anthropic.com/research/building-c-compiler" class="c-link align-middle" rel="noopener noreferrer"&gt;
            &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.claude.com%2F9a08ac31fda9ee626886c344803ffcc350174369.jpg" height="420" class="m-0" width="800"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="c-embed__body"&gt;
        &lt;h2 class="fs-xl lh-tight"&gt;
          &lt;a href="https://www.anthropic.com/research/building-c-compiler" rel="noopener noreferrer" class="c-link"&gt;
            Building a C compiler with parallel Claudes \ Anthropic
          &lt;/a&gt;
        &lt;/h2&gt;
          &lt;p class="truncate-at-3"&gt;
            How a team of 16 parallel Claude agents wrote a Rust-based C compiler from scratch, capable of compiling the Linux kernel, and what it taught us.
          &lt;/p&gt;
        &lt;div class="color-secondary fs-s flex items-center"&gt;
            &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.anthropic.com%2Ffavicon.ico" width="48" height="48"&gt;
          anthropic.com
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;On a separate occasion, &lt;a href="https://materialize.com/blog/finding-bugs-using-llms/" rel="noopener noreferrer"&gt;Materialize&lt;/a&gt; sent agents back through commits that had already landed, allegedly tested. Across those reviews and its other searches, the team found hundreds of bugs its tests had missed. But why?&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;div class="c-embed__content"&gt;
        &lt;div class="c-embed__cover"&gt;
          &lt;a href="https://materialize.com/blog/finding-bugs-using-llms/" class="c-link align-middle" rel="noopener noreferrer"&gt;
            &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.sanity.io%2Fimages%2Frz2gt56r%2Fproduction%2F9d8ad23400d3e21d4e45eb3b381a69bbb38e11ae-987x654.png" height="530" class="m-0" width="800"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="c-embed__body"&gt;
        &lt;h2 class="fs-xl lh-tight"&gt;
          &lt;a href="https://materialize.com/blog/finding-bugs-using-llms/" rel="noopener noreferrer" class="c-link"&gt;
            Finding Bugs using LLMs | Materialize
          &lt;/a&gt;
        &lt;/h2&gt;
          &lt;p class="truncate-at-3"&gt;
            At Materialize we’ve had success in finding bugs in existing code and open pull requests using LLM-based coding agents since February 2026, coinciding with the release of Anthropic’s Opus 4.6 (now mostly running on 4.7). In this post we’ll look into some of the considerations that went into the system we are currently using as well as lessons learned.
          &lt;/p&gt;
        &lt;div class="color-secondary fs-s flex items-center"&gt;
            &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmaterialize.com%2F__static%2Ff26f8b69cfe23e2f729be081ae141dca65ddeb74%2Ffavicon-32x32.png" width="32" height="32"&gt;
          materialize.com
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;Carlini created something new, with bugs. Materialize tried to find bugs on existing code and found some. However, we keep hearing every now and then that "software has been solved" and that we're gonna lose our jobs in a few months, right?&lt;/p&gt;

&lt;h2&gt;
  
  
  Is software &lt;em&gt;really&lt;/em&gt; solved?
&lt;/h2&gt;

&lt;p&gt;Getting Linux to boot showed that the agents' compiler could handle most of the x86 build. It didn't show that the compiler could replace GCC for the startup code, because GCC was still doing that job. The 32 KB limit made the shortcoming easy to measure. Meeting it was another matter. This doesn't mean coding with agents is bad, or that agents that code are bad. LLMs are really powerful and very much capable of doing all of that.&lt;/p&gt;

&lt;p&gt;That also doesn't mean GCC is perfect. The only perfect software is TempleOS.&lt;/p&gt;

&lt;p&gt;But there's an interesting limitation. Think about this: suppose you want to learn how to bake a pizza, for the first time (been there). You get the materials, high-quality ones. And you get someone that helps you do it. Your output will be heavily influenced by the trainer if you have no prior experience. If the trainer uses a certain technique, you're very likely to use the same.&lt;/p&gt;

&lt;p&gt;That tells us something about the &lt;em&gt;current&lt;/em&gt; state of models: they're not trained on &lt;em&gt;perfect&lt;/em&gt; code. We, humans, like to talk about how good the pre-AI software was, but the reality is that it was as buggy as it is right now. We cannot produce perfect code. So, it's expected that current agents training on imperfect code produce imperfect results.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Not every past time was better&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Not every problem is in the training data
&lt;/h2&gt;

&lt;p&gt;There's a 2nd layer of issues, also related to humans, that also affect the output of models: humans don't know what they want. Tell me, when was the last time you built a feature only to realize that a small &lt;em&gt;something&lt;/em&gt; you did was not quite the thing your client expected? It happens. And it will continue happening. We keep adding things. Customers keep asking for changes. &lt;em&gt;Humans are never satisfied&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;So it's expected that an agent cannot produce perfect code that satisfies humans.&lt;/p&gt;

&lt;p&gt;Think of a coffee machine. Mine (a Ninja Luxe, btw) has a bunch of presets for doing coffee. They're all trash (to me), but when I invite people over, they love it... That's not a machine's problem. That's a human's problem: we have different tastes, different preferences, etc. We're different.&lt;/p&gt;

&lt;p&gt;That makes &lt;em&gt;solving&lt;/em&gt; software a difficult problem. It's like saying that because we have a bunch of machines that produce food we have solved it...&lt;/p&gt;

&lt;p&gt;Carlini had GCC compile most of the Linux kernel and used the new compiler for the rest. By changing which files went to each compiler, he tracked down failures involving &lt;em&gt;pairs of files&lt;/em&gt;. The kernel worked when GCC compiled one file and the new compiler compiled the other, either way around. It failed when the new compiler compiled both. So which file was broken? Checking them one at a time, you'd say neither.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fr3erdow76hmp6yl6yw5h.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fr3erdow76hmp6yl6yw5h.webp" alt="Two wooden gears, one mint and one lilac, meet tooth to tooth instead of meshing." width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Ok, so, is software solved?
&lt;/h2&gt;

&lt;p&gt;Maybe not yet. But I don't think It won't be solved in the following years. There's still some "human" factor inside how AIs think that's limiting them. They're capable of great things, like &lt;a href="https://leanprover-community.github.io/blog/posts/riemann-hypothesis/" rel="noopener noreferrer"&gt;helping prove the Riemann hypothesis&lt;/a&gt;, but they can also create some incredible ugly slop. But that depends a lot on the human, the setup, the harness, etc. There's still &lt;em&gt;too many&lt;/em&gt; factors that can affect a model's performance, one of them is the humans using it.&lt;/p&gt;

&lt;p&gt;Some day, we'll be bathing in the sun while our AI does everything for us.&lt;/p&gt;

&lt;p&gt;Just kidding. Some day we'll be forced to buy tokens for survive :ahhhhhh: and the only work available will be to apply WD-40 to the bot's joints.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>programming</category>
      <category>softwareengineering</category>
      <category>testing</category>
    </item>
    <item>
      <title>You don't have to let AI think for you</title>
      <dc:creator>Kevin Alemán</dc:creator>
      <pubDate>Mon, 16 Nov 2026 20:59:48 +0000</pubDate>
      <link>https://dev.to/kaleman15/you-dont-have-to-let-ai-think-for-you-55k</link>
      <guid>https://dev.to/kaleman15/you-dont-have-to-let-ai-think-for-you-55k</guid>
      <description>&lt;p&gt;You can open an LLM, describe a problem, and have a plausible answer before you've finished thinking about it. It can draft an email, explain a concept, suggest a fix for a bug, or give you a plan for a difficult conversation.&lt;/p&gt;

&lt;p&gt;That's useful. It's also a little strange. When an answer is always a prompt away, it's easy to reach for one before you've had a chance to decide what you think.&lt;/p&gt;

&lt;p&gt;And I know, we've heard something like this before. When Stack Overflow was on the rise, programmers got the same advice: "Don't just copy and paste the answer. Understand it." Why was that important then, and why does it sometimes seem forgotten now?&lt;/p&gt;

&lt;p&gt;I've written about &lt;a href="https://kevinaleman.com/the-future-after-ai-2767/" rel="noopener noreferrer"&gt;what happens when developers produce code they don't understand&lt;/a&gt;. But this goes beyond programming. What happens when we ask a model to organize every thought, settle every uncertainty, and fill every quiet moment with an answer?&lt;/p&gt;

&lt;p&gt;I don't think we need to stop using AI. I think we can get better at deciding &lt;strong&gt;which parts of a problem we want to work through ourselves&lt;/strong&gt;, and where a model can help us go further.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finishing the task isn't the same as learning
&lt;/h2&gt;

&lt;p&gt;Imagine you're trying to understand why a database query is slow. You paste it into an LLM, it suggests an index, and the query gets faster. That's a good outcome. If you were trying to fix an urgent issue, you might be done.&lt;/p&gt;

&lt;p&gt;But if you want to understand what happened, you're far from done. There are a bunch of questions you may now have: What can you explain or do the next time the query is different? Why is an index a good solution? What other options are available? Could you use an existing index?&lt;/p&gt;

&lt;p&gt;This distinction shows up in a &lt;a href="https://www.pnas.org/doi/10.1073/pnas.2422633122" rel="noopener noreferrer"&gt;2025 field experiment with high school math students in Turkey&lt;/a&gt;. Students who had access to a GPT-4-based assistant performed better during practice, but the group using a more open-ended version performed worse on a subsequent exam without AI than students who practiced with textbooks and notes. A separate version, designed to give teacher-provided hints and avoid giving away answers, largely mitigated that difference.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.pnas.org/doi/10.1073/pnas.2422633122" rel="noopener noreferrer"&gt;Read the high school mathematics study&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's one school, one subject, and a short-term result from a particular setup. It doesn't prove that using a chatbot makes everyone worse at thinking. Well, I mean, sometimes chatbots can drive you crazy, especially the customer support ones. But the study does suggest that &lt;strong&gt;getting through practice and learning from it are different outcomes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;There's encouraging evidence too. In a &lt;a href="https://doi.org/10.1038/s41598-025-97652-6" rel="noopener noreferrer"&gt;2025 study of a Harvard physics course&lt;/a&gt;, students who used a specially designed AI tutor scored higher on immediate post-lesson tests than students taught the same material in an active-learning classroom. The tutor guided students through problems instead of simply giving them answers. To me, that's an example of what &lt;em&gt;augmenting&lt;/em&gt; human abilities can look like.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://doi.org/10.1038/s41598-025-97652-6" rel="noopener noreferrer"&gt;Read the Harvard physics study&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I like what these results leave open: AI can help us learn. It can help us get better.&lt;/p&gt;

&lt;p&gt;But how?&lt;/p&gt;

&lt;h2&gt;
  
  
  Make a first attempt
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fw7tqumpqparcc48oma9a.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fw7tqumpqparcc48oma9a.webp" alt="A person draws a branching path from a question toward an idea and an experiment, with a laptop nearby." width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Before asking for an answer, try to describe the problem in your own words. You don't need a complete solution. Write down what you know, what you suspect, and what you can't explain yet.&lt;/p&gt;

&lt;p&gt;For the slow query, that might be: “I think the database is scanning too many rows. I don't know whether an index would help, or which columns it should include.” Now you have something to investigate. You can ask the model to challenge that guess, explain the query plan, or point you toward the documentation. Then check its suggestions against the actual plan and measurements. Even if you're wrong (or very wrong), you can still learn something.&lt;/p&gt;

&lt;p&gt;This isn't a rule that says you have to struggle alone for an hour before you're allowed to ask for help. Sometimes you don't have the background to make a useful guess, and asking for an explanation is a perfectly good first step. The point is to notice when you're trying to learn and give yourself a chance to participate in the work.&lt;/p&gt;

&lt;p&gt;You don't have to do this every time, either. If you're in a rush or fixing a simple problem, you can leave the learning for later, or skip it altogether.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ask for help that leaves you something to do
&lt;/h2&gt;

&lt;p&gt;If you're learning a subject, try asking for a hint instead of the full solution. Or explain your reasoning and ask the model where it might break. You can ask it to quiz you, give you a small example, or offer a counterexample to your idea.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I'm learning how database indexes work. Here's my guess about why this query is slow. Ask me one question at a time and give me a hint if I'm stuck. Don't give me the full answer yet.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are also skills that get an LLM to ask you questions. Matt Pocock's &lt;code&gt;/grill-me&lt;/code&gt;, for example, interviews you to help clarify a plan or idea. His &lt;a href="https://github.com/mattpocock/skills/blob/main/skills/productivity/teach/SKILL.md" rel="noopener noreferrer"&gt;&lt;code&gt;/teach&lt;/code&gt; skill&lt;/a&gt; is aimed at learning: it creates lessons and keeps track of your progress.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/mattpocock/skills/blob/main/skills/productivity/grill-me/SKILL.md" rel="noopener noreferrer"&gt;Matt Pocock’s grill-me skill&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then try another problem without looking at the conversation. If you can't explain the idea yet, that's useful information, not a failure. Go back, ask a better question, and try again. A chatbot can still give a bad hint or an incorrect explanation, so check important details against documentation, a working example, or someone who knows the subject. What you learn can help you ask better questions and make better use of the answers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keep your judgment in the loop
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8wug4j08of0oy9g75gs8.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8wug4j08of0oy9g75gs8.webp" alt="A person uses a magnifying glass to compare a shape on a laptop with a physical object." width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thinking isn't only producing an answer from memory. It can also mean asking what evidence would change your mind, comparing options, noticing what's missing, and deciding whether a result fits the situation.&lt;/p&gt;

&lt;p&gt;A model can help with that. Ask it for assumptions, alternatives, or ways an idea could fail. But don't treat a confident explanation as proof. An AI-generated citation needs to lead to a real source that supports the claim. A suggested fix needs a test that could actually fail. A plan for a conversation needs to account for the person you know, not just the description you typed into a prompt.&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://kevinaleman.com/proof-driven-development/" rel="noopener noreferrer"&gt;Proof Driven Development&lt;/a&gt;, I wrote about why a proof is only as useful as the property being checked. The same idea applies here: even when a model helps you evaluate an answer, you still need to ask whether you're checking the right thing, or if the model is actually answering what you need.&lt;/p&gt;

&lt;p&gt;It's easy to treat an LLM's response as authoritative, especially when it sounds confident about a subject we don't know well. But a confident answer can still be wrong. That's true for people, and it's true for LLMs too.&lt;/p&gt;

&lt;p&gt;Sometimes that means getting another person involved. A colleague might know a constraint that isn't in the ticket. A friend might hear something in your story that you left out of the prompt. You can use an LLM to prepare for those conversations, but the conversation itself can give you context no model was given.&lt;/p&gt;

&lt;p&gt;Remember, your brain has a good context window too. Use it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Leave space for your own curiosity
&lt;/h2&gt;

&lt;p&gt;Not every question needs an instant answer. Sometimes it's worth following a thought for a while: read the article instead of only its summary, try the recipe, sketch the idea badly, or take a walk and see what comes to mind.&lt;/p&gt;

&lt;p&gt;Those aren't productivity rules. You don't owe anyone an AI-free hobby. The point is that understanding something, making something, or changing your mind can be enjoyable in its own right. We can use a tool to get past the parts we don't care about and still keep the parts that make an activity meaningful to us.&lt;/p&gt;

&lt;p&gt;And when you are tired, under pressure, or dealing with a task outside your expertise, using more help is fine. Time and energy aren't distributed equally. The goal isn't to prove that you can do everything unaided; it's to make a deliberate choice when you have one.&lt;/p&gt;

&lt;p&gt;An LLM can offer a starting point, explain something in a different way, or help us find a mistake. It can make it easier to explore a question we wouldn't have had the time or confidence to approach alone.&lt;/p&gt;

&lt;p&gt;What we want, as humans, is the future where we get &lt;em&gt;augmented&lt;/em&gt; by AI, not the one where we end up serving an AI overlord cause we don't know how to add or subtract anymore.&lt;/p&gt;

&lt;p&gt;Keep curious.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>learning</category>
      <category>career</category>
    </item>
    <item>
      <title>Cross-posting to DEV with an agent skill</title>
      <dc:creator>Kevin Alemán</dc:creator>
      <pubDate>Fri, 13 Nov 2026 00:10:07 +0000</pubDate>
      <link>https://dev.to/kaleman15/cross-posting-to-dev-with-an-agent-skill-31jp</link>
      <guid>https://dev.to/kaleman15/cross-posting-to-dev-with-an-agent-skill-31jp</guid>
      <description>&lt;p&gt;I publish articles on my blog, and I also want to share them on DEV. That means copying the post, fixing the links that only work on my site, and remembering to point DEV back to the original. None of that is hard. I just don't want to do it by hand every time.&lt;/p&gt;

&lt;p&gt;So I made a &lt;a href="https://github.com/KevLehman/agent-skills/tree/main/publish-devto" rel="noopener noreferrer"&gt;DEV publishing skill&lt;/a&gt;. It's a set of instructions for an agent: give it an article and ask for a DEV draft or a published post. The agent then uses the &lt;a href="https://developers.forem.com/api/v1" rel="noopener noreferrer"&gt;Forem API&lt;/a&gt; to do the work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where's the original?
&lt;/h2&gt;

&lt;p&gt;The original lives on my blog. Before cross-posting it, the skill checks that the original URL is public and sets it as the canonical URL on DEV. That tells search engines which version I prefer them to treat as the original, though they still decide how to index it.&lt;/p&gt;

&lt;p&gt;It also checks my existing DEV posts and drafts. If the article is already there, the agent should update that post instead of making a duplicate. And those relative image paths that work on my blog? They need public URLs on DEV, so the skill handles that too.&lt;/p&gt;

&lt;p&gt;The same goes for relative links. I'd rather catch those before someone clicks a link in the DEV post and ends up somewhere unexpected.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to use it
&lt;/h2&gt;

&lt;p&gt;You'll need a &lt;a href="https://dev.to/settings/extensions"&gt;DEV API key&lt;/a&gt;, available to the agent as &lt;code&gt;DEVTO_API_KEY&lt;/code&gt; in your environment or an ignored &lt;code&gt;.env&lt;/code&gt; file. Keep it private. The skill checks the account before making changes and reports the URL and publication state afterward.&lt;/p&gt;

&lt;p&gt;I can ask for a draft if I want to review it first. The agent shouldn't publish it unless I ask it to. Either way, I'll check the DEV version before sharing it. I still want to know what readers are going to see.&lt;/p&gt;

&lt;p&gt;You can find this and the others I've been working on in &lt;a href="https://kevinaleman.com/skills/" rel="noopener noreferrer"&gt;my skills collection&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>devto</category>
      <category>writing</category>
      <category>automation</category>
    </item>
    <item>
      <title>What does it mean to formally verify software?</title>
      <dc:creator>Kevin Alemán</dc:creator>
      <pubDate>Thu, 12 Nov 2026 18:52:40 +0000</pubDate>
      <link>https://dev.to/kaleman15/what-does-it-mean-to-formally-verify-software-1o3b</link>
      <guid>https://dev.to/kaleman15/what-does-it-mean-to-formally-verify-software-1o3b</guid>
      <description>&lt;p&gt;You ask an agent to implement a feature. It changes the code, adds tests, runs them and gives you a nice summary. Everything is green. Then you open the pull request and realize you still have to answer a question: &lt;strong&gt;how do I know this actually works?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The tests help. Reading the code helps. Asking another agent to review it can help too. But a passing test tells us what happened for the cases we ran, and a confident explanation is still an explanation. Neither automatically tells us what happens in all the cases we didn't think to check.&lt;/p&gt;

&lt;p&gt;This is where formal verification gets interesting. Not because it's new, or because AI invented it, but because we're producing more code with tools that can also help us reason about it. Having a separate way to check some of the claims about that code sounds pretty useful.&lt;/p&gt;

&lt;h2&gt;
  
  
  So, what are we verifying?
&lt;/h2&gt;

&lt;p&gt;Before anything else, what's &lt;em&gt;correct&lt;/em&gt;?. Well, as any experienced software developer would say, &lt;em&gt;it depends&lt;/em&gt;. On what? Well, on you, or your product. &lt;em&gt;Correct&lt;/em&gt; can mean a lot of things to different people, so before verifying something, we have to be sure that the behavior we're verifying is &lt;em&gt;the right one&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;For that, let me introduce you to some terms:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A &lt;strong&gt;specification&lt;/strong&gt; makes the behavior we care about precise enough for a tool to reason about it.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;property&lt;/strong&gt; is a particular claim we want to check against that specification or the implementation.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example: "a user without access to a document must not be allowed to delete it". That contains both: the specification and the property. Now that we have this clear, we can start to verify stuff.&lt;/p&gt;

&lt;p&gt;Formal verification uses mathematical reasoning to check whether the thing we're examining satisfies those properties, within the model and assumptions used for the check. Sometimes that "thing" is a design; sometimes it's an implementation; sometimes is an Jira message you received at 3am... Anyways, &lt;a href="https://shemesh.larc.nasa.gov/fm/fm-what.html" rel="noopener noreferrer"&gt;NASA's introduction to formal methods&lt;/a&gt; gives a useful overview of the family of techniques behind this work.&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;div class="c-embed__content"&gt;
      &lt;div class="c-embed__body flex items-center justify-between"&gt;
        &lt;a href="https://shemesh.larc.nasa.gov/fm/fm-what.html" rel="noopener noreferrer" class="c-link fw-bold flex items-center"&gt;
          &lt;span class="mr-2"&gt;shemesh.larc.nasa.gov&lt;/span&gt;
          

        &lt;/a&gt;
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  How does a verifier check that?
&lt;/h2&gt;

&lt;p&gt;The verifier needs two things: something to check and a precise rule to check it against. How it does that depends on the tool. A model checker can look at the possible states in a model and how the system moves between them. A program verifier can use a theorem prover to reason about the code and its specification.&lt;/p&gt;

&lt;p&gt;For a function, you can describe what must be true before it runs and what it should guarantee afterward. Those are a &lt;strong&gt;precondition&lt;/strong&gt; and a &lt;strong&gt;postcondition&lt;/strong&gt;. You can also describe an &lt;strong&gt;invariant&lt;/strong&gt;: a property that should remain true as the system changes. The &lt;a href="https://dafny.org/v4.8.1/OnlineTutorial/guide" rel="noopener noreferrer"&gt;Dafny tutorial&lt;/a&gt; explains these with small examples.&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;div class="c-embed__content"&gt;
      &lt;div class="c-embed__body"&gt;
        &lt;h2 class="fs-xl lh-tight"&gt;
          &lt;a href="https://dafny.org/v4.8.1/OnlineTutorial/guide" rel="noopener noreferrer" class="c-link"&gt;
            Getting Started with Dafny: A Guide | Dafny Documentation
          &lt;/a&gt;
        &lt;/h2&gt;
        &lt;div class="color-secondary fs-s flex items-center"&gt;
            &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdafny.org%2Fimages%2Fdafny-favicon.svg" width="32" height="32"&gt;
          dafny.org
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;The verifier checks whether the rule holds for the behavior it's examining. Depending on the method, that might mean all the behaviors described by a model, or only a bounded set. If it finds a violation, it may show you a &lt;strong&gt;counterexample&lt;/strong&gt;: an input or sequence of events that breaks the rule. That's useful because you get something specific to investigate.&lt;/p&gt;

&lt;p&gt;But a failed verification doesn't always mean the code is wrong. Sometimes the tool can't establish the claim with the information it has, or can't complete the proof. You might need to give it more detail before it can tell you either way.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8xd7kvra378r26r6hu46.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8xd7kvra378r26r6hu46.webp" alt="A hand-drawn network of branching paths, representing possible behaviors in a model." width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You don't have to verify an entire application to get value from this. A team can focus on a critical decision, algorithm or design where a mistake would be particularly expensive.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wait, don't we already have tests?
&lt;/h2&gt;

&lt;p&gt;We do, and if you have a decent testing coverage, then you should be proud. Take a medal (not really). Now, since we already have, what can we gain from Formal Verification? Formal verification reasons about properties over the behaviors covered by its model or proof. That can let us make a stronger claim about those properties than a handful of passing examples can.&lt;/p&gt;

&lt;p&gt;But the proof has a boundary. If we've verified a function that makes a permission decision, we still need to know whether the rest of the application calls it when it should, and whether it gives the function the right information. A proof of the decision doesn't automatically establish either of those things. (This may sound very familiar to what a unit test is, but trust me, it's different... a bit).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://csrc.nist.gov/Projects/automated-combinatorial-testing-for-software/autonomous-systems-assurance/formal-methods" rel="noopener noreferrer"&gt;NIST makes this point when discussing formal methods and testing&lt;/a&gt;: assumptions used in a proof may not hold when the specification is connected to the implementation and its dependencies. Tests can help us check those connections and exercise the actual application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7djhqg48tl9tgwh232to.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7djhqg48tl9tgwh232to.webp" alt="A checked group of connected nodes inside a rectangle, with other nodes outside it." width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This is also why I'd be careful with “the software is verified.” Verified against &lt;em&gt;what&lt;/em&gt;, and how much of it? Those questions are more useful than the label on its own.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Where does AI fit in?
&lt;/h2&gt;

&lt;p&gt;Writing specifications and proofs takes work. The verifier needs precise statements, and some properties need more guidance before the tool can establish them. That's a different skill from writing the implementation, and it's one reason this isn't something most of us add to every pull request. Just imagine if 5 years ago you had a Jira task for a feature, and then a sub task for unit tests, and then a sub task for a formal verification of your work... Workflows defined by the utterly deranged.&lt;/p&gt;

&lt;p&gt;But now, we have agents! An agent can help with parts of that work: discussing requirements, proposing a specification, writing an implementation and working through feedback from a verifier. The important part is that the verifier checks the formal claim according to its rules. We're not relying only on the agent telling us it got the answer right.&lt;/p&gt;

&lt;p&gt;That's the idea that interested me in &lt;a href="https://kevinaleman.com/proof-driven-development/" rel="noopener noreferrer"&gt;Proof Driven Development&lt;/a&gt;. The agent helps produce the work, and a verification tool checks it against an agreed specification. I also &lt;a href="https://kevinaleman.com/proving-binary-search-with-dafny/" rel="noopener noreferrer"&gt;tried proving binary search with Dafny&lt;/a&gt;, which gave me a better appreciation for the amount of detail involved in checking one damn function.&lt;/p&gt;

&lt;p&gt;But, as everything humans do, Formal Verification would be as good as the people that define what &lt;em&gt;correct&lt;/em&gt; means. An agent can help you understand a codebase, identify possible issues and ask you questions for scenarios you hadn't thought (sometimes even for scenarios that are only possible in 1 out of 200M universes...). If your property definition never mentions a key detail, or how a special behavior should behave, a proof generated by an agent won't add it magically to your work. The code can follow the written rules perfectly, and the agent may have wrote the most beautiful proof your eyes have seen, but if you didn't know all the details...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhf16zcqumhrzn5jc8ar6.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhf16zcqumhrzn5jc8ar6.webp" alt="Checked paths inside a circle, with another branch outside the boundary." width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Is this only for critical systems?
&lt;/h2&gt;

&lt;p&gt;Formal methods are useful in critical systems, but the ideas aren't limited to them. In a &lt;a href="https://www.amazon.science/publications/how-amazon-web-services-uses-formal-methods" rel="noopener noreferrer"&gt;2015 paper on formal methods at AWS&lt;/a&gt;, the authors describe using a model checker on a DynamoDB design. It found a sequence of failures and recovery steps that could lead to data loss, after the team had already done extensive testing. So there is some value!&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;div class="c-embed__content"&gt;
        &lt;div class="c-embed__cover"&gt;
          &lt;a href="https://www.amazon.science/publications/how-amazon-web-services-uses-formal-methods" class="c-link align-middle" rel="noopener noreferrer"&gt;
            &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.amazon.science%2Fdims4%2Fdefault%2F5e7b277%2F2147483647%2Fstrip%2Ftrue%2Fcrop%2F1200x630%2B0%2B0%2Fresize%2F1200x630%21%2Fquality%2F90%2F%3Furl%3Dhttps%253A%252F%252Famzn-science-production-science.s3.us-east-1.amazonaws.com%252Fscience%252F7c%252F4e%252Fd4e963d24f6c966def82a45d2bf1%252Famazon-science-og-image-squid.png" height="420" class="m-0" width="800"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="c-embed__body"&gt;
        &lt;h2 class="fs-xl lh-tight"&gt;
          &lt;a href="https://www.amazon.science/publications/how-amazon-web-services-uses-formal-methods" rel="noopener noreferrer" class="c-link"&gt;
            How Amazon Web Services uses formal methods - Amazon Science
          &lt;/a&gt;
        &lt;/h2&gt;
        &lt;div class="color-secondary fs-s flex items-center"&gt;
            &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.amazon.science%2Ffavicon-32x32.png" width="32" height="32"&gt;
          amazon.science
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;Most of us aren't building DynamoDB. But we do write permission checks, payment flows and other logic where the consequences of a missed case can be significant. Those are places where making a property explicit—and deciding how strongly we need to check it—is worth the discussion.&lt;/p&gt;

&lt;p&gt;I don't think every feature needs a formal proof. But as agents make code easier to produce, we need to start thinking about stronger ways to prove the output. We cannot humanly verify everything the agent does, we weren't able to verify everything when we wrote it either, that's why bugs exists and will continue to exist.&lt;/p&gt;

&lt;p&gt;The point is: can we take this concept to help us write &lt;em&gt;better&lt;/em&gt; software? That's on us.&lt;/p&gt;

</description>
      <category>softwareengineering</category>
      <category>programming</category>
      <category>ai</category>
    </item>
    <item>
      <title>AI found the vulnerability. Are we done yet?</title>
      <dc:creator>Kevin Alemán</dc:creator>
      <pubDate>Thu, 12 Nov 2026 18:51:56 +0000</pubDate>
      <link>https://dev.to/kaleman15/ai-found-the-vulnerability-are-we-done-yet-39ab</link>
      <guid>https://dev.to/kaleman15/ai-found-the-vulnerability-are-we-done-yet-39ab</guid>
      <description>&lt;p&gt;What caught my attention about PHP2Shell was &lt;a href="https://thephp.foundation/blog/2026/10/21/todays-php-security-release-and-how-were-strengthening-php-security/" rel="noopener noreferrer"&gt;the PHP team's explanation of why fixing it took so long&lt;/a&gt;. A developer had noticed signs of the problem back in 2024. They had several possible fixes, but each came with problems: breaking compatibility, creating denial-of-service risks, or requiring changes to third-party extensions.&lt;/p&gt;

&lt;p&gt;Imagine knowing there's a security problem in your code and having to choose which other problem you're willing to create to fix it. Now imagine that code is PHP.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.wiz.io/blog/php2shell-php-rce" rel="noopener noreferrer"&gt;AI helped researchers turn these flaws into working exploits&lt;/a&gt;. That's a big deal. But reading the maintainers' account, I kept coming back to everything they had to work through before they could ship a fix.&lt;/p&gt;

&lt;p&gt;In October, PHP released emergency security updates for two critical vulnerabilities known as &lt;strong&gt;PHP2Shell&lt;/strong&gt;. They affect the PHP engine itself, and under the right conditions can allow an unauthenticated attacker to execute code on a vulnerable server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Finding a flaw, demonstrating an exploit, fixing the underlying problem and safely deploying that fix are different jobs.&lt;/strong&gt; Being good at one doesn't automatically finish the others.&lt;/p&gt;

&lt;p&gt;And before anyone puts off updating PHP because an exploit sounds difficult: please don't. Security researchers have demonstrated working attacks, and &lt;a href="https://blog.nviso.eu/2026/10/23/php2shell-operational-guidance/" rel="noopener noreferrer"&gt;NVISO's October 28 update cited reports of targeted exploitation&lt;/a&gt;. Difficulty is not a security patch.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ok, but the AI found it. Isn't that the hard part?
&lt;/h2&gt;

&lt;p&gt;It's &lt;em&gt;a&lt;/em&gt; hard part. And in this case the models helped with more than just finding the bugs.&lt;/p&gt;

&lt;p&gt;According to Wiz, the flaws involve how PHP handles fatal errors, especially out-of-memory failures. An interrupted request can leave internal state partially initialized or pointing at freed memory. Later processing may reuse that invalid state, leading to memory disclosure or corruption.&lt;/p&gt;

&lt;p&gt;The first signs of the out-of-memory problem appeared while a PHP developer was fixing an issue from a 2024 security audit. A possible exploit was theorized, but no concrete path to remote code execution had been established at the time.&lt;/p&gt;

&lt;p&gt;The Foundation describes the stalled decision on a fix as a structural failure involving both the team's process and limited resources. Reports from Calif, OpenAI and Wiz in 2026 made the impact clearer, and the security team prioritized a fix across supported PHP versions.&lt;/p&gt;

&lt;p&gt;Wiz says frontier models helped it develop and adapt working exploits across multiple applications &lt;strong&gt;in hours&lt;/strong&gt;. Each target still required testing, debugging and adaptation to its memory layout.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fw1otbuc39pde8tg5mb1x.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fw1otbuc39pde8tg5mb1x.webp" alt="A magnifying glass reveals a crack in a complex brass lock mechanism." width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Finding the crack is a start. The rest of the mechanism still matters.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The distinction mattered after disclosure too. In its &lt;a href="https://blog.nviso.eu/2026/10/23/php2shell-operational-guidance/" rel="noopener noreferrer"&gt;October 28 guidance&lt;/a&gt;, NVISO cited a report of targeted compromise and a successful attack against a challenge server, with a public proof of concept. It had not observed successful exploitation in its own telemetry. There was no reliable universal public exploit at that point, and many non-functional tools were being circulated.&lt;/p&gt;

&lt;p&gt;AWS &lt;a href="https://aws.amazon.com/blogs/security/amazon-threat-intelligence-identifies-php2shell-exploitation-prior-to-public-disclosure-in-madpot/" rel="noopener noreferrer"&gt;identified attempted exploits against its honeypot before public disclosure&lt;/a&gt;, but couldn't tell whether they would have succeeded against real systems. When a model generates an exploit, I'd ask the same question: &lt;strong&gt;what shows that it actually works?&lt;/strong&gt; Sending a payload or getting a convincing explanation isn't enough. We need to reproduce the result under realistic conditions.&lt;/p&gt;

&lt;h2&gt;
  
  
  What happened with Firefox?
&lt;/h2&gt;

&lt;p&gt;Mozilla used a security analysis pipeline with Claude Mythos Preview and other models to investigate Firefox. Its engineers reported that Firefox 150 included fixes for &lt;strong&gt;271 vulnerabilities identified with Mythos Preview&lt;/strong&gt;. Their &lt;a href="https://hacks.mozilla.org/2026/05/behind-the-scenes-hardening-firefox/" rel="noopener noreferrer"&gt;technical write-up&lt;/a&gt; describes findings across different browser components, including some involving sandbox escapes and bugs that had been around for years.&lt;/p&gt;

&lt;p&gt;Some of those sandbox escape reports assume an attacker has already compromised a content process. Escaping the sandbox is a serious vulnerability, but a complete browser attack would also need a way to compromise that process first.&lt;/p&gt;

&lt;p&gt;That's a lot of real bugs. Mozilla also says the improvement came from both more capable models and the work it put into the pipeline: steering the models, filtering reports and giving them a way to produce test cases. The pipeline was adapted to Firefox, with feedback from the engineers handling the reports.&lt;/p&gt;

&lt;p&gt;Mozilla says more than 100 people contributed code to the effort, with others involved in triage, testing and releases. In &lt;a href="https://techcrunch.com/2026/05/07/how-anthropics-mythos-has-rewritten-firefoxs-approach-to-cybersecurity/" rel="noopener noreferrer"&gt;an interview with TechCrunch&lt;/a&gt;, Mozilla's Brian Grinstead said that, for the bugs discussed there, an engineer wrote each patch and another reviewed it. The team also asked AI for suggested patches, but those generally weren't ready to deploy directly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F72mbff1dvf7q1a16shzt.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F72mbff1dvf7q1a16shzt.webp" alt="A robotic arm places small mechanical parts on a workbench while an engineer carefully repairs another part." width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;A finding still has to become a fix people can safely use.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What does a working fix actually prove?
&lt;/h2&gt;

&lt;p&gt;With PHP, stopping one proof of concept wasn't enough. The fix had to address memory management and error handling across supported versions.&lt;/p&gt;

&lt;p&gt;If the agent's patch stops the exploit, I still want to know: does it address the underlying failure across the affected paths? Does it introduce a different way to exhaust memory? Does it work on the supported versions without breaking extensions? &lt;strong&gt;Passing the test for one exploit isn't proof that we've fixed the whole problem.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What do the benchmarks tell us?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://arxiv.org/abs/2509.22097v5" rel="noopener noreferrer"&gt;SecureVibeBench&lt;/a&gt; reconstructs 105 C/C++ implementation tasks from changes that introduced real vulnerabilities. The agents implement the original requirements, rather than patch an already identified bug. In the June 2026 version of the paper, the best tested combination, SWE-agent with Claude Sonnet 4.5, met both the functionality and security criteria on &lt;strong&gt;23.8% of those tasks&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;These were specific, difficult tasks using models from 2025, not the newer frontier systems in the PHP and Firefox examples above. The result doesn't tell us how those newer models would perform. What I find useful is the question the benchmark asks: &lt;strong&gt;did the change meet the functional requirement without introducing a security problem?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For PHP, a benchmark score won't tell us whether a proposed patch handles each affected configuration. For Firefox, it won't tell us whether a sandbox escape starts from an already compromised process. Those answers have to come from investigating the actual behavior and checking the evidence.&lt;/p&gt;

&lt;h2&gt;
  
  
  So what do we do with the findings?
&lt;/h2&gt;

&lt;p&gt;I'd use AI to find the bug and help fix it. But if I can't explain why the patch works, I'm not ready to ship it.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>security</category>
      <category>programming</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>What it takes to prove binary search works</title>
      <dc:creator>Kevin Alemán</dc:creator>
      <pubDate>Thu, 12 Nov 2026 18:51:56 +0000</pubDate>
      <link>https://dev.to/kaleman15/what-it-takes-to-prove-binary-search-works-118f</link>
      <guid>https://dev.to/kaleman15/what-it-takes-to-prove-binary-search-works-118f</guid>
      <description>&lt;p&gt;In my &lt;a href="https://kevinaleman.com/proof-driven-development/" rel="noopener noreferrer"&gt;previous article about Proof Driven Development&lt;/a&gt;, I wrote about using formal specifications to check whether code does what we agreed it should do. Microsoft Research's PDD Kit uses Verus for Rust, though, and most of my work is in JavaScript. So I was left with a question: what can I actually try without rewriting my whole application in Rust?&lt;/p&gt;

&lt;p&gt;I ended up trying &lt;a href="https://dafny.org/" rel="noopener noreferrer"&gt;Dafny&lt;/a&gt;. With help from a coding agent, I wrote and verified a small binary search, deliberately broke it, and compiled the working version to JavaScript.&lt;/p&gt;

&lt;p&gt;If you haven't heard of Dafny, it's an open-source programming language and verifier. You write code and describe what must be true before and after it runs. Dafny checks the code against those rules using a solver called Z3, and can compile it to languages including JavaScript.&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://assets.dev.to/assets/github-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/dafny-lang" rel="noopener noreferrer"&gt;
        dafny-lang
      &lt;/a&gt; / &lt;a href="https://github.com/dafny-lang/dafny" rel="noopener noreferrer"&gt;
        dafny
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Dafny is a verification-aware programming language
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;Dafny&lt;/h1&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a href="https://github.com/dafny-lang/dafny/actions?query=workflow%3A%22Build+and+Test%22" rel="noopener noreferrer"&gt;&lt;img src="https://github.com/dafny-lang/dafny/workflows/Build%20and%20Test/badge.svg" alt="Build and Test"&gt;&lt;/a&gt; &lt;a href="https://gitter.im/dafny-lang/community?utm_source=badge&amp;amp;utm_medium=badge&amp;amp;utm_campaign=pr-badge" rel="nofollow noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/b06d762d4c0a648287b28cc5809ac34ab81c440d80335547fb9d674ba07ecc53/68747470733a2f2f6261646765732e6769747465722e696d2f6461666e792d6c616e672f636f6d6d756e6974792e737667" alt="Gitter"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Dafny is a &lt;strong&gt;verification-ready programming language&lt;/strong&gt;. As you type in your program, Dafny's verifier constantly looks over your shoulder, flags any errors, shows you counterexamples, and congratulates you when your code matches your specifications. When you're done, Dafny can &lt;strong&gt;compile your code to C#, Go, Python, Java, or JavaScript&lt;/strong&gt; (more to come!), so it can integrate with your existing workflow.&lt;/p&gt;
&lt;p&gt;&lt;a rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/3601079/140799975-f3ac0925-10d9-4c14-b1a9-cd449854c6ae.gif"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F3601079%2F140799975-f3ac0925-10d9-4c14-b1a9-cd449854c6ae.gif" alt="vs-code-dafny-2 0 0-demo"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Dafny will give you &lt;strong&gt;assurance that your code meets the specifications you write&lt;/strong&gt;, while letting you write both code and specifications in the Dafny programming language itself. Since verification is an integral part of development, it will thus &lt;strong&gt;reduce the risk of costly late-stage bugs&lt;/strong&gt; that are typically missed by testing.&lt;/p&gt;
&lt;p&gt;Dafny has support for common programming concepts such as &lt;a href="https://dafny-lang.github.io/dafny/DafnyRef/DafnyRef#sec-class-types" rel="nofollow noopener noreferrer"&gt;classes and trait inheritance&lt;/a&gt;, &lt;a href="https://dafny-lang.github.io/dafny/DafnyRef/DafnyRef#181-inductive-datatypes" rel="nofollow noopener noreferrer"&gt;inductive datatypes&lt;/a&gt; that can have methods and are suitable for pattern matching, &lt;a href="https://dafny-lang.github.io/dafny/DafnyRef/DafnyRef#182-co-inductive-datatypes" rel="nofollow noopener noreferrer"&gt;lazily unbounded datatypes&lt;/a&gt;, &lt;a href="https://dafny-lang.github.io/dafny/DafnyRef/DafnyRef#sec-subset-types" rel="nofollow noopener noreferrer"&gt;subset types e.g. for bounded integers&lt;/a&gt;…&lt;/p&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/dafny-lang/dafny" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;What is Z3?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/Z3Prover/z3" rel="noopener noreferrer"&gt;Z3&lt;/a&gt; is a solver: you give it logical constraints and it checks whether they can be satisfied. Dafny uses it to help check whether the code could violate the rules we wrote.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Why binary search?
&lt;/h2&gt;

&lt;p&gt;If you don't know binary search, don't worry: the internet definitely doesn't have enough articles about it yet. I even wrote &lt;a href="https://kevinaleman.com/algorithms-in-real-life-binary-search/" rel="noopener noreferrer"&gt;one of them&lt;/a&gt; a few years ago, including the most important requirement:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your list must be sorted.&lt;/li&gt;
&lt;li&gt;Your list must be sorted.&lt;/li&gt;
&lt;li&gt;Your list &lt;em&gt;must&lt;/em&gt; be sorted.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Still true! Binary search looks at the middle of a sorted collection, discards the half that can't contain the value, and repeats. That sounds simple until you have to decide what happens with an empty array, a missing value, duplicates, or a search range with one element left.&lt;/p&gt;

&lt;p&gt;I wanted to see what it would take to check those cases without listing each input in a test. So I wrote down what a correct result should look like and asked Dafny to check the implementation against it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are we actually promising?
&lt;/h2&gt;

&lt;p&gt;Here's the contract I used:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dafny"&gt;&lt;code&gt;&lt;span class="n"&gt;method&lt;/span&gt; &lt;span class="nc"&gt;BinarySearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;requires&lt;/span&gt; &lt;span class="n"&gt;forall&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt;
    &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;==&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

  &lt;span class="n"&gt;ensures&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;==&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;forall&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;==&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;

  &lt;span class="n"&gt;ensures&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;==&amp;gt;&lt;/span&gt;
    &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;code&gt;requires&lt;/code&gt; is the condition the caller must satisfy: the array is sorted in ascending order. Equal values are allowed.&lt;/p&gt;

&lt;p&gt;The first &lt;code&gt;ensures&lt;/code&gt; says the result is &lt;code&gt;-1&lt;/code&gt; &lt;strong&gt;if and only if&lt;/strong&gt; the value is absent. The second says that any other result must be a valid index containing the value. If there are duplicates, returning any matching index is fine; this contract doesn't ask for the first one.&lt;/p&gt;

&lt;p&gt;In other words, there are two cases: if the value is missing, return &lt;code&gt;-1&lt;/code&gt;; if it exists, return an index where it appears. Both clauses work together: the first describes when &lt;code&gt;-1&lt;/code&gt; is allowed, and the second checks that any other result points to the value.&lt;/p&gt;
&lt;h2&gt;
  
  
  The implementation needs an explanation too
&lt;/h2&gt;

&lt;p&gt;Here is the complete method, including the loop invariants:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dafny"&gt;&lt;code&gt;&lt;span class="n"&gt;method&lt;/span&gt; &lt;span class="nc"&gt;BinarySearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;requires&lt;/span&gt; &lt;span class="n"&gt;forall&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt;
    &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;==&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="n"&gt;ensures&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;==&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;forall&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;==&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;
  &lt;span class="n"&gt;ensures&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;==&amp;gt;&lt;/span&gt;
    &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;low&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;high&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;low&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;high&lt;/span&gt;
    &lt;span class="n"&gt;invariant&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;low&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;high&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Length&lt;/span&gt;
    &lt;span class="n"&gt;invariant&lt;/span&gt; &lt;span class="n"&gt;forall&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;low&lt;/span&gt; &lt;span class="o"&gt;==&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;
    &lt;span class="n"&gt;invariant&lt;/span&gt; &lt;span class="n"&gt;forall&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="n"&gt;high&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;==&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;decreases&lt;/span&gt; &lt;span class="n"&gt;high&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;low&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;low&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;high&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;low&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;low&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;high&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The search range is &lt;code&gt;[low, high)&lt;/code&gt;: it includes &lt;code&gt;low&lt;/code&gt;, but not &lt;code&gt;high&lt;/code&gt;. At the beginning, that's the whole array. At the end, when &lt;code&gt;low == high&lt;/code&gt;, there's nothing left to search.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fkevinaleman.com%2Fimages%2Fdafny-search-range.svg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fkevinaleman.com%2Fimages%2Fdafny-search-range.svg" alt="Searching for 5 in 1, 3, 5, 7, 9: low is 2 and high is 3. The remaining range contains 5; values outside it are too small or too large." width="800" height="280"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The invariants describe what must remain true throughout the loop. The first keeps the range inside the array. The other two say that everything before &lt;code&gt;low&lt;/code&gt; is smaller than the key, and everything at or after &lt;code&gt;high&lt;/code&gt; is larger. If we find the key, we return its index. If the range becomes empty, those two facts explain why the key can't be anywhere in the array.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;decreases high - low&lt;/code&gt; is the termination argument. The verifier checks that the remaining range gets smaller on each iteration that continues the loop. Without that check, we could miss a search that gets stuck repeating the same step.&lt;/p&gt;

&lt;p&gt;With Dafny 4.11.0, the verifier reported:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Dafny program verifier finished with 3 verified, 0 errors
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Those three verified items are the verifier's reported count for the sample; they aren't a count of all the individual properties. The method's contract, bounds and termination are checked under its precondition. Dafny's guide goes into more detail on preconditions, postconditions and invariants.&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;div class="c-embed__content"&gt;
      &lt;div class="c-embed__body"&gt;
        &lt;h2 class="fs-xl lh-tight"&gt;
          &lt;a href="https://dafny.org/dafny/OnlineTutorial/guide" rel="noopener noreferrer" class="c-link"&gt;
            Getting Started with Dafny: A Guide | Dafny Documentation
          &lt;/a&gt;
        &lt;/h2&gt;
        &lt;div class="color-secondary fs-s flex items-center"&gt;
            &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdafny.org%2Fimages%2Fdafny-favicon.svg" width="32" height="32"&gt;
          dafny.org
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;



&lt;h2&gt;
  
  
  Let's break it
&lt;/h2&gt;

&lt;p&gt;I changed one line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gd"&gt;- low := mid + 1;&lt;/span&gt;
&lt;span class="gi"&gt;+ low := mid;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The middle value is too small, so moving the lower bound to the middle might seem reasonable. But we already checked that position; we need to move past it.&lt;/p&gt;

&lt;p&gt;Suppose we're looking for &lt;code&gt;6&lt;/code&gt; and only &lt;code&gt;5&lt;/code&gt; remains. &lt;code&gt;low&lt;/code&gt; and &lt;code&gt;mid&lt;/code&gt; are both &lt;code&gt;2&lt;/code&gt;, so setting &lt;code&gt;low&lt;/code&gt; to &lt;code&gt;mid&lt;/code&gt; leaves the range exactly where it was:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fkevinaleman.com%2Fimages%2Fdafny-search-stuck-diagram.svg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fkevinaleman.com%2Fimages%2Fdafny-search-stuck-diagram.svg" alt="Searching for 6 with low and mid at 2 and high at 3 leaves only 5. Setting low to mid leaves the same range, so the next iteration checks 5 again." width="800" height="280"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The next iteration checks &lt;code&gt;5&lt;/code&gt; again, and the one after that does the same. With &lt;code&gt;low := mid + 1&lt;/code&gt;, &lt;code&gt;low&lt;/code&gt; becomes &lt;code&gt;3&lt;/code&gt;, the range is empty, and the search returns &lt;code&gt;-1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Dafny rejected the changed version:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Error: decreases expression might not decrease

Dafny program verifier finished with 2 verified, 1 error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The error doesn't spell out the one-element case, but it points us to the right place: the search range isn't guaranteed to shrink.&lt;/p&gt;
&lt;h2&gt;
  
  
  But I write JavaScript
&lt;/h2&gt;

&lt;p&gt;Dafny can compile to JavaScript. I compiled this sample with &lt;code&gt;dafny build --target js&lt;/code&gt; and ran the generated program with Node. For &lt;code&gt;[1, 3, 5, 7, 9]&lt;/code&gt;, it returned &lt;code&gt;3&lt;/code&gt; for &lt;code&gt;7&lt;/code&gt; and &lt;code&gt;-1&lt;/code&gt; for &lt;code&gt;4&lt;/code&gt;.&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;div class="c-embed__content"&gt;
      &lt;div class="c-embed__body"&gt;
        &lt;h2 class="fs-xl lh-tight"&gt;
          &lt;a href="https://dafny.org/dafny/Installation" rel="noopener noreferrer" class="c-link"&gt;
            Installation Instructions | Dafny Documentation
          &lt;/a&gt;
        &lt;/h2&gt;
        &lt;div class="color-secondary fs-s flex items-center"&gt;
            &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdafny.org%2Fimages%2Fdafny-favicon.svg" width="32" height="32"&gt;
          dafny.org
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;



&lt;p&gt;The generated JavaScript uses &lt;code&gt;bignumber.js&lt;/code&gt; for Dafny integers. It also includes generated runtime code, so this isn't the same experience as adding a type annotation to an existing JavaScript function. To call the generated method from an application, we need an integration layer that supplies values in the expected representation. I tried that separately with a small session-policy function; the generated sample needed an export added before a Node wrapper could call it.&lt;/p&gt;

&lt;p&gt;And remember the first rule? &lt;strong&gt;The array must be sorted.&lt;/strong&gt; Dafny checks that calls from verified Dafny code satisfy the &lt;code&gt;requires&lt;/code&gt; clause; our sample's &lt;code&gt;Main&lt;/code&gt; does. But the search method itself assumes its input is sorted, and the generated JavaScript doesn't automatically check an array supplied by a JavaScript caller. We need to establish that guarantee at the boundary or elsewhere in the application. Checking every element on every call takes linear time, which can outweigh the benefit of binary search.&lt;/p&gt;

&lt;p&gt;We also need to check how the JavaScript adapter converts values. Dafny integers are mathematical integers; JavaScript &lt;code&gt;Number&lt;/code&gt; values have their own limits.&lt;/p&gt;

&lt;p&gt;The proof also doesn't establish that we picked the right behavior. If the product needs the &lt;em&gt;first&lt;/em&gt; occurrence of a duplicate, our current contract allows a different matching index. Dafny can verify exactly what we asked for and still leave the product team asking why the result isn't the one they expected.&lt;/p&gt;

&lt;h2&gt;
  
  
  How is this different from TDD?
&lt;/h2&gt;

&lt;p&gt;With TDD, I could start with a failing test for searching &lt;code&gt;[5]&lt;/code&gt; for &lt;code&gt;6&lt;/code&gt;, then write the search until that test passes. That case would expose the broken &lt;code&gt;low := mid&lt;/code&gt; update, although the test runner would need a timeout because the function gets stuck. I'd also add tests for an empty array, a value at either end, a missing value and duplicates.&lt;/p&gt;

&lt;p&gt;Those tests check the inputs I chose. Dafny checks the method against the contract for &lt;strong&gt;all sorted integer arrays&lt;/strong&gt; in its model, including the cases I didn't think to write down. It also checks that the loop makes progress, so I don't have to wait for a particular test to hang before finding that problem.&lt;/p&gt;

&lt;p&gt;There's a tradeoff: I had to write the contract and loop invariants. If I forget to specify that the result should be the &lt;em&gt;first&lt;/em&gt; matching index, Dafny won't invent that requirement for me. A test can miss the same requirement, but it's often easier to write and read a concrete example of what a user expects.&lt;/p&gt;

&lt;p&gt;I'd still use tests around the generated JavaScript: they can check the adapter and real inputs from the application, which sit outside this proof. TDD would help me develop those behaviors one example at a time; Dafny gives me a way to check a general rule about the search itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  What changes with AI?
&lt;/h2&gt;

&lt;p&gt;Formal verification isn't new. What's changing for me is how practical it feels to try it. Learning a verification language, writing the specification and working out the loop invariants all take time. On top of building the feature, that can make verification a difficult sell.&lt;/p&gt;

&lt;p&gt;Here, a coding agent helped write the Dafny example and work through the verification. That gave me something concrete to read and experiment with instead of starting from a blank file. I could change &lt;code&gt;mid + 1&lt;/code&gt; to &lt;code&gt;mid&lt;/code&gt;, run the verifier again and see what happened. Getting to that point took less effort than learning the whole tool before trying a small example.&lt;/p&gt;

&lt;p&gt;That doesn't mean I can accept whatever the agent writes. I still need to understand the contract well enough to notice that it allows any matching index, and check what happens when the code is called from JavaScript. But if AI can help with the mechanics and make these experiments cheaper, formal verification becomes something more of us can try on code where it would previously have been hard to justify the time.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>algorithms</category>
      <category>formalverification</category>
    </item>
    <item>
      <title>Proof Driven Development: can the agent prove it works?</title>
      <dc:creator>Kevin Alemán</dc:creator>
      <pubDate>Thu, 12 Nov 2026 18:51:11 +0000</pubDate>
      <link>https://dev.to/kaleman15/proof-driven-development-can-the-agent-prove-it-works-1oea</link>
      <guid>https://dev.to/kaleman15/proof-driven-development-can-the-agent-prove-it-works-1oea</guid>
      <description>&lt;p&gt;One of the problems with using agents is that you can ask them to implement something, add tests, run everything and explain why it works, and still end up with a bug. You can ask another agent to review the result, which helps, but then you have to check whether the review makes sense too. We've made producing code incredibly easy, and now we're trying to figure out how to keep up with checking all of it. Asking the same LLM “are you sure?” a few more times doesn't give us much of a guarantee.&lt;/p&gt;

&lt;p&gt;So I found &lt;a href="https://x.com/GitHubNext/status/2112936525457740139" rel="noopener noreferrer"&gt;this announcement from GitHub Next&lt;/a&gt; pretty interesting. Microsoft Research is working on &lt;strong&gt;Proof Driven Development Kit&lt;/strong&gt;, or PDD Kit, which brings formal verification into the agent's development workflow. The idea is that we can ask for a mathematical proof that the implementation satisfies a specification, and have a separate verifier check that claim. What makes this interesting to me is that the correctness argument has to survive something more demanding than the agent's own explanation of why its code is correct.&lt;/p&gt;

&lt;p&gt;If formal verification is new to you, I wrote an &lt;a href="https://kevinaleman.com/what-does-it-mean-to-formally-verify-software/" rel="noopener noreferrer"&gt;introduction to how it works and why it matters for AI-generated code&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-2112936525457740139-333" src="https://platform.twitter.com/embed/Tweet.html?id=2112936525457740139"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-2112936525457740139-333');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=2112936525457740139&amp;amp;theme=dark"
  }



&lt;/p&gt;

&lt;h2&gt;
  
  
  Ok, what are they doing?
&lt;/h2&gt;

&lt;p&gt;According to the announcement, PDD Kit discusses the code with you, figures out what you're trying to achieve and proposes a formal specification. You review that specification, approve it, and the implementation gets checked against it using &lt;strong&gt;Verus&lt;/strong&gt;, a verification tool for Rust. The demo follows a login flow from the initial intent to a verified pull request. It also integrates with &lt;a href="https://githubnext.com/projects/chopin/" rel="noopener noreferrer"&gt;Chopin&lt;/a&gt;, GitHub Next's collaborative planning environment, so the team can work through the plan together. Where formal proof isn't possible, the workflow uses tests and computer-use agents to check the behavior. The announced language support is Rust, and Verus adds syntax for specifications and proofs around the Rust code.&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;div class="c-embed__content"&gt;
        &lt;div class="c-embed__cover"&gt;
          &lt;a href="https://githubnext.com/projects/chopin/" class="c-link align-middle" rel="noopener noreferrer"&gt;
            &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithubnext.com%2Fog%2Fprojects%2Fchopin.png" height="420" class="m-0" width="800"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="c-embed__body"&gt;
        &lt;h2 class="fs-xl lh-tight"&gt;
          &lt;a href="https://githubnext.com/projects/chopin/" rel="noopener noreferrer" class="c-link"&gt;
            Chopin
          &lt;/a&gt;
        &lt;/h2&gt;
          &lt;p class="truncate-at-3"&gt;
            A real-time multiplayer, agentic planning environment
          &lt;/p&gt;
        &lt;div class="color-secondary fs-s flex items-center"&gt;
            &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithubnext.com%2Ffavicon.svg" width="747" height="532"&gt;
          githubnext.com
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;What I like here is that there's something outside the LLM checking the formal claim. The agent can propose an implementation, but it doesn't get to declare its own homework correct just because the explanation sounds convincing. The &lt;a href="https://verus-lang.github.io/verus/guide/" rel="noopener noreferrer"&gt;Verus documentation&lt;/a&gt; describes this as static verification: checking that code satisfies the specified properties for all executions covered by the model, without adding runtime checks. Think about the amount of time we spend wondering whether there's some input we forgot to test. For properties we can express and verify this way, that's a much stronger guarantee. But there's a very important word in all of this: &lt;strong&gt;specification&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  This is where the questions matter
&lt;/h2&gt;

&lt;p&gt;Imagine you're implementing session validation. You want users with valid sessions to access the application, and everyone else to be rejected. You ask the agent to implement the check, and it gives you something like this. I'm using JavaScript here to keep the example familiar; the announced formal verification support in PDD Kit is for Rust.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;canUseSession&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;signatureValid&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;expiresAt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Looks reasonable. A valid signature and a session that hasn't expired. You test a valid session, an expired session and an invalid signature. Everything passes. Then a user logs out, you mark the session as revoked, and... the function still accepts it. We never checked &lt;code&gt;session.revoked&lt;/code&gt;. We could add a test for that. We should. But now imagine we had formally specified the function as: &lt;em&gt;Accept the session if and only if its signature is valid and it hasn't expired.&lt;/em&gt; The implementation satisfies that rule. We could prove it and still have the exact same bug, because &lt;strong&gt;we forgot revocation in the requirement too&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is exactly the kind of gap Chopin and PDD Kit are meant to help us catch. PDD Kit discusses the intended behavior before proposing a formal specification, and &lt;a href="https://githubnext.com/projects/chopin/" rel="noopener noreferrer"&gt;Chopin gives the team a shared place to question the plan&lt;/a&gt; and resolve those decisions. For this example, that conversation should include: “Does logging out revoke the session immediately?”, “What happens at the exact expiration time?” and “Should every otherwise valid session be accepted?” The tools can help turn “make session validation work” into a set of explicit decisions, with the answers carried into the specification. That's a useful part of the workflow: &lt;strong&gt;you don't have to arrive with a perfect specification already written.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After working through those questions, the rule could become: &lt;em&gt;Accept the session if and only if its signature is valid, it hasn't been revoked, and the current time is strictly before its expiration time.&lt;/em&gt; Now the expiration boundary is settled: at the exact expiration time, access is denied, so an implementation using &lt;code&gt;&amp;lt;=&lt;/code&gt; would violate the specification. The “if and only if” also requires valid sessions to be accepted. Without that, an implementation returning &lt;code&gt;false&lt;/code&gt; for everyone could satisfy a narrower rule that only requires invalid sessions to be rejected. Congrats, nobody can log in anymore. This is why the discussion and the proof belong together: the tools help us work out what we mean, then verification checks that the implementation follows what we agreed. The tools can still miss a requirement, but helping us uncover those requirements is part of their value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fmucyb825s2dmt4eejj9n.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fmucyb825s2dmt4eejj9n.webp" alt="A meticulously built bridge matches its blueprint but ends on an isolated platform before reaching the opposite bank." width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Perfectly built according to the plan. Still doesn't get you across.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  An admin, but whose admin?
&lt;/h2&gt;

&lt;p&gt;For an application beyond Microsoft's login demo, I'd want to try this workflow on tenant permissions. Imagine you're building a SaaS application and an admin can delete documents. The agent checks whether the user is an active admin, and everything looks fine in your tests because every user and document in the fixtures belongs to the same company. Then someone passes a document ID from another company and discovers that being an admin was apparently a global achievement. Oops. The rule needs to connect the user's membership to the document's tenant. A simplified postcondition in Verus syntax could look like this, assuming those values are already available to the function:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="n"&gt;ensures&lt;/span&gt;
    &lt;span class="n"&gt;allowed&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;active_member&lt;/span&gt;
        &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;is_admin&lt;/span&gt;
        &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;member_tenant&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;document_tenant&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Here I'd want the agent to consider an active admin in the right tenant, an active admin in a different tenant, an inactive admin, and a regular member. That second case is the interesting one: the role is valid, but the relationship is wrong. A proof against this contract would have to cover different tenant IDs too, rather than only the company IDs someone happened to put in a test. We still need to establish that the application supplies the actual document tenant and the right membership record. If it passes the user's tenant ID for both arguments, the decision can follow this rule perfectly while the application still leaks access.&lt;/p&gt;
&lt;h2&gt;
  
  
  Someone still has to know what to ask
&lt;/h2&gt;

&lt;p&gt;I've said this before: &lt;strong&gt;you cannot prompt what you don't know exists&lt;/strong&gt;. If you don't know sessions can be revoked, you probably won't notice that revocation is missing from the specification. This is also a research problem. Microsoft Research calls the translation from informal requirements to checkable specifications &lt;a href="https://www.microsoft.com/en-us/research/publication/intent-formalization-a-grand-challenge-for-reliable-coding-in-the-age-of-ai-agents/" rel="noopener noreferrer"&gt;intent formalization&lt;/a&gt;. Shuvendu Lahiri's article identifies validating those specifications as a central bottleneck. Someone still needs to establish whether the formal statement matches what the user meant.&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;div class="c-embed__content"&gt;
      &lt;div class="c-embed__body"&gt;
        &lt;h2 class="fs-xl lh-tight"&gt;
          &lt;a href="https://www.microsoft.com/en-us/research/publication/intent-formalization-a-grand-challenge-for-reliable-coding-in-the-age-of-ai-agents/" rel="noopener noreferrer" class="c-link"&gt;
            Intent Formalization: A Grand Challenge for Reliable Coding in the Age of AI Agents - Microsoft Research
          &lt;/a&gt;
        &lt;/h2&gt;
        &lt;div class="color-secondary fs-s flex items-center"&gt;
          microsoft.com
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;



&lt;p&gt;There's another detail in our session example: &lt;code&gt;signatureValid&lt;/code&gt; is an input. The function doesn't verify a signature. Same with &lt;code&gt;now&lt;/code&gt;: we give it a timestamp, and it trusts that value. A proof of this decision doesn't establish that the cryptography is correct, that the clock is right, or that every protected endpoint actually calls the function. Those need their own guarantees or checks. So when a PR says “verified”, I'd want to know what was verified. The session decision? The HTTP handler? The whole login flow? Which parts were proved, tested, or assumed? That's where PDD Kit's combination of formal and empirical verification makes sense to me. You can prove a decision follows a rule and still use integration tests to check that the application supplies the right inputs and calls it in the right places.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fol0ponk4vd8v9zra4hwi.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fol0ponk4vd8v9zra4hwi.webp" alt="An inspected mechanism sits inside a clear enclosure while its input pipes and an unreliable external valve remain outside." width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The proof covers what's inside the boundary. The inputs still have to come from somewhere.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How I'd apply this to JavaScript
&lt;/h2&gt;

&lt;p&gt;I don't work in Rust, so Verus isn't something I can apply to my JavaScript projects. But I can borrow &lt;strong&gt;PDD Kit's approach: agree on the contract before generating the implementation&lt;/strong&gt;. I'd use &lt;a href="https://githubnext.com/projects/chopin/" rel="noopener noreferrer"&gt;Chopin&lt;/a&gt; for the shared plan, questions and decisions, with its MCP server connecting the coding agent. Then turn those agreed rules into unit and integration tests, use &lt;a href="https://fast-check.dev/docs/introduction/why-property-based/" rel="noopener noreferrer"&gt;fast-check&lt;/a&gt; to exercise generated input combinations, and add browser checks for the user flow. For our session example, that means explicitly checking revocation, expiration boundaries and valid sessions being accepted. This doesn't give JavaScript the same formal guarantees, but it gives the agent concrete requirements to implement and us something specific to check before merging.&lt;/p&gt;

&lt;p&gt;At Rocket.Chat, we're already increasing code coverage significantly, and every new feature has to come with a decent amount of testing. &lt;strong&gt;PDD's approach could help us get more out of that effort by making the expected behavior explicit before we write the tests.&lt;/strong&gt; For a room-permissions change, for example, the agent could help us work through who should have access, who shouldn't, and what happens when membership changes. Those agreed rules would give us something concrete to connect to the implementation and its tests during review, and make missing cases easier to spot.&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;div class="c-embed__content"&gt;
        &lt;div class="c-embed__cover"&gt;
          &lt;a href="https://fast-check.dev/docs/introduction/why-property-based/" class="c-link align-middle" rel="noopener noreferrer"&gt;
            &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Ffast-check.dev%2Fimg%2Fsocial.png" height="338" class="m-0" width="600"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="c-embed__body"&gt;
        &lt;h2 class="fs-xl lh-tight"&gt;
          &lt;a href="https://fast-check.dev/docs/introduction/why-property-based/" rel="noopener noreferrer" class="c-link"&gt;
            Why Property-Based Testing? | fast-check
          &lt;/a&gt;
        &lt;/h2&gt;
          &lt;p class="truncate-at-3"&gt;
            Learn about the benefits and advantages of property-based testing. If you have never encountered property-based testing before, it is best to start with the definitions in What is Property-Based Testing?.
          &lt;/p&gt;
        &lt;div class="color-secondary fs-s flex items-center"&gt;
            &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Ffast-check.dev%2Fimg%2Ffavicon.ico" width="96" height="96"&gt;
          fast-check.dev
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


</description>
      <category>ai</category>
      <category>programming</category>
      <category>rust</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>How ABAC works in Rocket.Chat</title>
      <dc:creator>Kevin Alemán</dc:creator>
      <pubDate>Thu, 12 Nov 2026 18:51:10 +0000</pubDate>
      <link>https://dev.to/kaleman15/how-abac-works-in-rocketchat-obp</link>
      <guid>https://dev.to/kaleman15/how-abac-works-in-rocketchat-obp</guid>
      <description>&lt;p&gt;While working on attribute-based access control (ABAC) for Rocket.Chat, I had to deal with a question that sounds pretty simple: does this user have the attributes this room requires? That's the basis of ABAC: who can access the data, but letting the data itself being the judge.&lt;/p&gt;

&lt;p&gt;Instead of traditional RBAC (Role based access control) where the user is granted access based on privileges, ABAC transfers that control to the data itself. Data then registers the &lt;em&gt;attributes&lt;/em&gt; that allows someone to access. So, in our case, the &lt;em&gt;data&lt;/em&gt; was the rooms. So the basis of ABAC in Rocket.Chat are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rooms have attributes. The attributes define which users can access them.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;a href="https://github.com/RocketChat/Rocket.Chat/releases/tag/9.0.0" rel="noopener noreferrer"&gt;9.0 release&lt;/a&gt; adds more ways to manage room attributes, including a preview of who would keep or lose access before you save a change. That was pretty much a performance nightmare, but we'll talk more about it later.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;ABAC was introduced for private channels and teams in &lt;a href="https://github.com/RocketChat/Rocket.Chat/releases/tag/8.0.0" rel="noopener noreferrer"&gt;Rocket.Chat 8.0&lt;/a&gt;, released in January 2026.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Ok, what are we checking?
&lt;/h2&gt;

&lt;p&gt;Imagine we have a private room for security work. Everyone in it needs to belong to the security department and have restricted clearance. As you see, the room publishes &lt;em&gt;who&lt;/em&gt; can access implicitly, any user who happens to possess these attributes is then able to access the room.&lt;/p&gt;

&lt;p&gt;For this example, the user's department and clearance come from LDAP. The default settings of Rocket.Chat reads attributes from this store and adds them to the users via an admin defined mapping, &lt;a href="https://github.com/RocketChat/Rocket.Chat/blob/756135f4a99535b1bbc1f7e3779a1035dbd20450/ee/packages/abac/src/index.ts#L347-L393" rel="noopener noreferrer"&gt;&lt;code&gt;addSubjectAttributes&lt;/code&gt;&lt;/a&gt; reads those mappings and saves the values in the Rocket.Chat user. Once we have both, the room attributes and the user attributes, we can make a decision.&lt;/p&gt;

&lt;p&gt;With the local setup, those attributes end up in MongoDB. The code that decides whether they match is called the &lt;strong&gt;policy decision point&lt;/strong&gt;, or PDP. That's the name you'll see very often when dealing with ABAC. A PDP is, how is name implies, the service where the 'magic' happens. It reads both attributes, validates them and then does the comparison to decide if the user can access the room.&lt;/p&gt;

&lt;p&gt;That's a very simplified way to explain, but remember: each PDP could have their own way of evaluating attributes. Hierarchy relations, environment, timing, etc. In the case for Rocket.Chat's default, we used this simplified mechanism as it was faster for a first implementation.&lt;/p&gt;

&lt;h2&gt;
  
  
  How's the access check performed?
&lt;/h2&gt;

&lt;p&gt;Continuing our example, here we have both definitions, the user and the room one. I've simplified the data to show just the attributes that matter for the example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"key"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"department"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"values"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"engineering"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"security"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"key"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"clearance"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"values"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"internal"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"restricted"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"room"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"key"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"department"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"values"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"security"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"key"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"clearance"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"values"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"restricted"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first glance, the user qualifies. The user has the attributes the room requires. A user can have &lt;em&gt;more&lt;/em&gt; attributes than what the room expects, but it can never hold &lt;em&gt;less&lt;/em&gt; than that.&lt;/p&gt;

&lt;p&gt;That's the ABAC rule we use for the Rocket.Chat's own PDP: A user must contain &lt;em&gt;all&lt;/em&gt; the attributes the room expects. So, lets see this other user:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"key"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"section"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"values"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"engineering"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"hightrust"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"key"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"clearance"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"values"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"internal"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"restricted"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"room"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"key"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"department"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"values"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"security"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"key"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"clearance"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"values"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"restricted"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, the user doesn't have all the attributes, which means the PDP &lt;em&gt;must&lt;/em&gt; decide the user is &lt;em&gt;non compliant&lt;/em&gt; and deny access. And, that's ABAC!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxg0dvhmfb0i3sl5de2ix.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxg0dvhmfb0i3sl5de2ix.webp" alt="Two people approach doors requiring a circle and diamond. The person carrying both qualifies; the person carrying a circle and square does not." width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let's do a deep dive into how it works in Rocket.Chat, technically speaking.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding someone to a room
&lt;/h2&gt;

&lt;p&gt;Let's continue with our security room. Someone tries to add a user, and Rocket.Chat runs the &lt;a href="https://github.com/RocketChat/Rocket.Chat/blob/756135f4a99535b1bbc1f7e3779a1035dbd20450/apps/meteor/ee/server/hooks/abac/beforeAddUserToRoom.ts" rel="noopener noreferrer"&gt;&lt;code&gt;beforeAddUserToRoom&lt;/code&gt; hook&lt;/a&gt;. The purpose of the hook is to check before adding the user if the attributes are compliant. This doesn't replace the existing permission checks, for example, a user adding another to a room still has to possess the traditional permission to add users, or be a room owner.&lt;/p&gt;

&lt;p&gt;ABAC in Rocket.Chat works as a 2nd security layer.&lt;/p&gt;

&lt;p&gt;For the local PDP, we can check several usernames in one database query. &lt;a href="https://github.com/RocketChat/Rocket.Chat/blob/756135f4a99535b1bbc1f7e3779a1035dbd20450/ee/packages/abac/src/pdp/LocalPDP.ts" rel="noopener noreferrer"&gt;&lt;code&gt;checkUsernamesMatchAttributes&lt;/code&gt;&lt;/a&gt; restricts the query with &lt;code&gt;username: { $in: usernames }&lt;/code&gt; and searches for users who fail at least one requirement. If one is returned, then that user is deemed &lt;em&gt;non compliant&lt;/em&gt; and as such, cannot access the room.&lt;/p&gt;

&lt;p&gt;Why search for the ones who &lt;em&gt;don't&lt;/em&gt; qualify? Because one missing requirement is enough to fail. We don't need to load every full user document into Node, loop through their attributes and build the same answer ourselves. We rely on MongoDB for doing these matches because of the way we structured the attributes on the user. The main idea is to offload as much as we can to Mongo (when using the local PDP) and avoid having to deal with all the users in Node.&lt;/p&gt;

&lt;p&gt;How does that work?&lt;/p&gt;

&lt;h2&gt;
  
  
  What does MongoDB actually check?
&lt;/h2&gt;

&lt;p&gt;For an individual access decision, &lt;a href="https://github.com/RocketChat/Rocket.Chat/blob/756135f4a99535b1bbc1f7e3779a1035dbd20450/ee/packages/abac/src/pdp/LocalPDP.ts#L37-L59" rel="noopener noreferrer"&gt;&lt;code&gt;LocalPDP.canAccessObject&lt;/code&gt;&lt;/a&gt; does the positive version of that query. Using our room, the filter would look like this. This is a simplified example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nv"&gt;$and&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;abacAttributes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$elemMatch&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'department'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;$all&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'security'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;abacAttributes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$elemMatch&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'clearance'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;$all&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'restricted'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The important part is &lt;code&gt;$elemMatch&lt;/code&gt;. Our attributes are an array of objects, so the key and its values must belong to the &lt;em&gt;same object&lt;/em&gt;. Finding &lt;code&gt;department&lt;/code&gt; somewhere in the array and &lt;code&gt;security&lt;/code&gt; somewhere else wouldn't be enough. &lt;code&gt;$all&lt;/code&gt; then requires every value listed for that key, while &lt;code&gt;$and&lt;/code&gt; requires every room attribute to match. If the room asked for both &lt;code&gt;engineering&lt;/code&gt; and &lt;code&gt;security&lt;/code&gt; under &lt;code&gt;department&lt;/code&gt;, the user would need both.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://github.com/RocketChat/Rocket.Chat/blob/756135f4a99535b1bbc1f7e3779a1035dbd20450/ee/packages/abac/src/helper.ts#L354-L375" rel="noopener noreferrer"&gt;query builders&lt;/a&gt; also produce the opposite condition: wrap each attribute match in &lt;code&gt;$not&lt;/code&gt;, then combine those conditions with &lt;code&gt;$or&lt;/code&gt;. In plain words: find someone missing &lt;em&gt;any&lt;/em&gt; requirement. That's the version used when adding users and when finding members to remove after a room changes that we were discussing before.&lt;/p&gt;

&lt;p&gt;Now, performance. This individual check includes an exact &lt;code&gt;_id&lt;/code&gt;, so MongoDB has its &lt;a href="https://www.mongodb.com/docs/manual/core/indexes/index-types/index-single/" rel="noopener noreferrer"&gt;default &lt;code&gt;_id&lt;/code&gt; index&lt;/a&gt; available to locate the user. The attribute predicates then determine whether that document qualifies. That's a very different amount of work from finding every non-compliant member of a large room. The operators may look similar, but the candidate set isn't.&lt;/p&gt;

&lt;p&gt;The successful lookup projects only &lt;code&gt;_id&lt;/code&gt;. We need to know that a matching user exists, so returning their profile, emails and everything else would just add data transfer and decoding work.  If the check fails, the PDP performs another lookup for the full user and returns it to the service as &lt;code&gt;userToRemove&lt;/code&gt;, if that user still exists. So the rejected path can cost more than the successful one, even before removing the membership. Glad is the negative and shouldn't happen that often...&lt;/p&gt;

&lt;p&gt;But, anyways, doing that query &lt;em&gt;every time&lt;/em&gt; a user wants to read a message or participate in the room's conversation would be costly, so...&lt;/p&gt;

&lt;h2&gt;
  
  
  The cache still does some work
&lt;/h2&gt;

&lt;p&gt;Once the user is in the room, the &lt;a href="https://github.com/RocketChat/Rocket.Chat/blob/756135f4a99535b1bbc1f7e3779a1035dbd20450/packages/authorization/src/canAccessRoom.ts#L70-L89" rel="noopener noreferrer"&gt;room access validator&lt;/a&gt; checks the usual viewing permissions and calls &lt;code&gt;Abac.canAccessObject&lt;/code&gt; for the ABAC decision. This is where we decide whether to ask the PDP again.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://github.com/RocketChat/Rocket.Chat/blob/756135f4a99535b1bbc1f7e3779a1035dbd20450/ee/packages/abac/src/index.ts#L1139-L1200" rel="noopener noreferrer"&gt;service first reads the user's subscription to that room&lt;/a&gt;, projecting &lt;code&gt;abacLastTimeChecked&lt;/code&gt;. No subscription, no access through this path. This is one of many shortcuts we could take thanks to our access structure. Since a user without a subscription cannot access a private room, then we can just omit the PDP access check for users that are not even in the room. Now, if the user is in the room, we have to re-evaluate on call, that's where the timed cache starts to work.&lt;/p&gt;

&lt;p&gt;Everytime a positive access decision is made, we store the timestamp on the user subscription to the room. If the timestamp is recent enough, it returns &lt;code&gt;true&lt;/code&gt; without asking the PDP. Otherwise, it checks PDP availability and requests a fresh decision. A successful decision writes a new timestamp to the subscription enabling the cache again.&lt;/p&gt;

&lt;p&gt;So, the cache saves the &lt;em&gt;policy evaluation&lt;/em&gt;. With Virtru, the fresh decision additionally involves an external request. When this check runs repeatedly, almost at every message sent, being able to skip it sometimes is gold.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://github.com/RocketChat/Rocket.Chat/blob/756135f4a99535b1bbc1f7e3779a1035dbd20450/apps/meteor/ee/server/settings/abac.ts" rel="noopener noreferrer"&gt;configured default is 300 seconds&lt;/a&gt;. Zero disables reuse. The timestamp belongs to a user-room subscription, so a grant for one room doesn't grant access to another. Cache hits also don't refresh that timestamp: the window runs from the last successful PDP check. Otherwise, someone who kept accessing the room could keep their old answer alive indefinitely. Very committed to being authorized.&lt;/p&gt;

&lt;p&gt;Notice the order: the cache check comes &lt;em&gt;before&lt;/em&gt; the PDP availability check. A still-valid cached grant can be used during an external PDP outage. Once it expires, an unavailable PDP or an evaluation error denies this access attempt. In &lt;a href="https://github.com/RocketChat/Rocket.Chat/blob/756135f4a99535b1bbc1f7e3779a1035dbd20450/ee/packages/abac/src/pdp/VirtruPDP.ts#L163-L220" rel="noopener noreferrer"&gt;&lt;code&gt;VirtruPDP.canAccessObject&lt;/code&gt;&lt;/a&gt;, an explicit denial also requests removal; an inconclusive answer denies access without requesting removal. Not knowing the answer and knowing the user no longer qualifies lead to different membership actions.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;What the heck is a subscription?&lt;/em&gt; A subscription, in Rocket.Chat, represents the relation between a user and a room. When a user is "subscribed" to the room, it can participate on it and receive notifications. For private rooms, the ones covered by ABAC, a subscription must always exist in order to determine than the user is a member.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What if the attributes change?
&lt;/h2&gt;

&lt;p&gt;Going back to our example, imagine the user loses the &lt;code&gt;restricted&lt;/code&gt; attribute on source. We should remove them from the security room, but our PDP reads from Mongo, so first we need LDAP to sync that change. Otherwise, even without the cache, we'd keep approving access with the attributes we still have stored, that's "staleness" and it's a problem in this type of security systems.&lt;/p&gt;

&lt;p&gt;This happens in &lt;a href="https://github.com/RocketChat/Rocket.Chat/blob/756135f4a99535b1bbc1f7e3779a1035dbd20450/ee/packages/abac/src/index.ts#L347-L393" rel="noopener noreferrer"&gt;&lt;code&gt;addSubjectAttributes&lt;/code&gt;&lt;/a&gt;. After saving the attributes, we check whether the user lost any values. That's what matters for this rule: if the user only gained attributes, they still satisfy every requirement they satisfied before. We can leave their memberships alone. Losing values, or having all their mapped attributes cleared, is what makes us check their rooms again.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;But, why that works?&lt;br&gt;
Good question. The main idea is simple: suppose you need to enter a physical room with a key and biometrics for one eye. The fact that you have 2 eyes doesn't change the security requirements. So same applies here: if on a sync the user diff presents that it only &lt;em&gt;obtained&lt;/em&gt; new attributes, then we don't care about that. The basic set that allowed him to be in that room is preserved, so the access mantains.&lt;br&gt;
A similar case, but the opposite direction, happens when a room loses required attributes. If a room required A &amp;amp; B attributes, we know that all users on the room have at least A &amp;amp; B. If the room is later updated to require only A, we know all the users inside are still compliant. Another shortcut to save some CPU cycles.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We already know which rooms they belong to from &lt;code&gt;user.__rooms&lt;/code&gt;, so the &lt;a href="https://github.com/RocketChat/Rocket.Chat/blob/756135f4a99535b1bbc1f7e3779a1035dbd20450/ee/packages/abac/src/pdp/LocalPDP.ts" rel="noopener noreferrer"&gt;local PDP&lt;/a&gt; uses those IDs to query for rooms requiring a key or value the user is missing. In our example, that would include the security room because it still requires &lt;code&gt;restricted&lt;/code&gt;. We get the affected rooms back from Mongo without having to fetch and compare each room separately in Node.&lt;/p&gt;

&lt;p&gt;Now, if someone edits the &lt;em&gt;room's&lt;/em&gt; attributes, we already have the room and need to find the affected users. That's the query with &lt;code&gt;__rooms: room._id&lt;/code&gt; in &lt;code&gt;onRoomAttributesChanged&lt;/code&gt;, combined with the non-compliant conditions from before. Thanks to the shortcut explained above, we know that we have to re-evaluate membership only when the attributes change in a way that makes them more restrictive.&lt;/p&gt;

&lt;p&gt;This is why the simple matching rule was useful: we know which changes can actually invalidate a membership.&lt;/p&gt;

&lt;h2&gt;
  
  
  You're no longer "topsecret"
&lt;/h2&gt;

&lt;p&gt;For each affected membership, we call the &lt;a href="https://github.com/RocketChat/Rocket.Chat/blob/756135f4a99535b1bbc1f7e3779a1035dbd20450/ee/packages/abac/src/index.ts#L1319-L1424" rel="noopener noreferrer"&gt;room service to remove the user&lt;/a&gt;. Doing all of those calls at once could get ugly in a large room, so we use a shared &lt;code&gt;pLimit(20)&lt;/code&gt;. Up to 20 removals can run concurrently through that limiter in this process; the rest wait.&lt;/p&gt;

&lt;p&gt;There's still something I'd improve here. We collect the query results with &lt;code&gt;toArray()&lt;/code&gt; and create the queued removal promises from them, so all those affected users are still in memory. We limited the running operations, but we haven't bounded the waiting work. That can become a problem if a change removes a large part of the room. I'll create a task for it.&lt;/p&gt;

&lt;p&gt;A successful removal goes into the audit log. If it fails, we log the error and return &lt;code&gt;false&lt;/code&gt;, while the other removals can still succeed. I'd want an admin to see which ones failed and be able to retry them, without having to go hunting through logs, but that's work for the future.&lt;/p&gt;

&lt;p&gt;Once the user is removed, their subscription is gone. And since we check for that subscription &lt;em&gt;before&lt;/em&gt; reusing a cached decision, the old grant won't get them back in. So we don't necessarily wait five minutes to revoke access: it's instant.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7aa99j2ponmwgtixeeb2.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7aa99j2ponmwgtixeeb2.webp" alt="A hand-drawn timeline shows a cached grant, an attribute change, sync, membership removal, and the original cache expiry." width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;One possible sequence: the LDAP change syncs and reevaluation removes the membership before the cached grant expires.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Dry runs
&lt;/h2&gt;

&lt;p&gt;Now, before saving new room attributes, we want to show who would keep or lose access. The &lt;a href="https://github.com/RocketChat/Rocket.Chat/blob/756135f4a99535b1bbc1f7e3779a1035dbd20450/ee/packages/abac/src/index.ts#L881-L910" rel="noopener noreferrer"&gt;preview service&lt;/a&gt; validates which attributes the editor can assign and evaluates those proposed requirements. The goal is to present the admins with information before making decisions.&lt;/p&gt;

&lt;p&gt;The interesting case is adding a requirement to a room with thousands of members. Say we want to show only the ones losing access. We read 1,000 users, filter them in Node, and get two. That doesn't mean only two people would lose access. We might have another 800 further down the list, and now we have to keep reading just to fill the page. This is the main reason for not doing filtering at the application level: it gets nasty as more requirements arrive. Always try to leverage the database to do the filtering. That's their work.&lt;/p&gt;

&lt;p&gt;So, we move the filter into the query. The &lt;a href="https://github.com/RocketChat/Rocket.Chat/blob/756135f4a99535b1bbc1f7e3779a1035dbd20450/ee/packages/abac/src/pdp/LocalPDP.ts#L77-L114" rel="noopener noreferrer"&gt;local preview&lt;/a&gt; uses the same compliant or non-compliant conditions we discussed before, depending on which group we want. It requests 1,001 matches, returns up to 1,000, and uses the extra result to know whether there's another page. That extra user is doing pagination work. Unpaid, probably.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://github.com/RocketChat/Rocket.Chat/blob/756135f4a99535b1bbc1f7e3779a1035dbd20450/packages/models/src/models/Users.ts#L171-L230" rel="noopener noreferrer"&gt;member query&lt;/a&gt; selects active users in the room, sorted by &lt;code&gt;username&lt;/code&gt; and then &lt;code&gt;_id&lt;/code&gt;. We keep both values from the last returned member as a cursor. The next query asks for usernames after that one, or the same username with a greater ID.&lt;/p&gt;

&lt;p&gt;This avoids a growing &lt;code&gt;skip&lt;/code&gt; offset, where Mongo has to &lt;a href="https://www.mongodb.com/docs/manual/reference/method/cursor.skip/" rel="noopener noreferrer"&gt;scan past earlier results before returning the next page&lt;/a&gt;. The cursor gives us a place to continue, but the membership filter, attribute matching, sorting and optional name search still cost work. I'd check the query plan with representative room data before calling it fast, but we're very sure it's faster than doing this in Node :its_something:&lt;/p&gt;

&lt;p&gt;We also run count queries for the range covered and, on the first page, the total candidate population. Those counts don't include the compliance filter, so &lt;code&gt;checked&lt;/code&gt; and the number of matching users aren't the same thing. Even returning two people can involve more database work than the response suggests. When no group is selected, the local preview instead reads a bounded page of members and evaluates those users together.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flffdz5kifa0j8pvfhhps.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flffdz5kifa0j8pvfhhps.webp" alt="A proposed requirement branches to six members, with four checks and two crosses inside a dotted preview boundary." width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The preview uses the proposed requirements and current data. Attributes can still change before saving, and removals can fail afterward.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The goal of this implementation is to be strong but at the same time, fast. We try to reuse as much as we can of the current access system, but adding a new layer on top with extra validations. The whole feature was partially (and then a bit more than partially) generated using AI.&lt;/p&gt;

&lt;p&gt;As a reminder, using AI is not bad. It's like a knife. You can use it to stab people or maybe to create a sashimi. It depends on who uses it and how.&lt;/p&gt;

</description>
      <category>rocketchat</category>
      <category>security</category>
      <category>authorization</category>
      <category>architecture</category>
    </item>
    <item>
      <title>From Clicks to AI: The Evolution of QA</title>
      <dc:creator>Kevin Alemán</dc:creator>
      <pubDate>Wed, 28 Oct 2026 16:53:58 +0000</pubDate>
      <link>https://dev.to/kaleman15/from-clicks-to-ai-the-evolution-of-qa-1alj</link>
      <guid>https://dev.to/kaleman15/from-clicks-to-ai-the-evolution-of-qa-1alj</guid>
      <description>&lt;p&gt;&lt;em&gt;This article was created in collaboration with Harmeet Kour.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;There was a time when testing an application meant opening it, clicking around, entering unexpected values and seeing what broke. That still sounds like a useful way to find bugs tho. You would see QA engineers working through long checklists before approving a product release, much like inspectors checking cars on an assembly line.&lt;/p&gt;

&lt;p&gt;Then came test automation. Instead of repeating the same regression checks by hand, teams could write them once and run them in CI. Tools like Selenium, Cypress and Playwright made that possible. Of course, the tests also needed maintenance: change a button label or a selector and suddenly the pipeline is red.&lt;/p&gt;

&lt;p&gt;Now AI can help generate test cases, suggest edge cases, create test data and investigate failures. It can even help decide which tests to run after a change. But that raises a question I've been thinking about: &lt;strong&gt;how do we know the tests it suggests are the ones that matter?&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Three stages of testing
&lt;/h2&gt;

&lt;p&gt;Manual testing gives us something that's hard to replace: &lt;em&gt;curiosity&lt;/em&gt;. A person can notice that a flow feels wrong, question a requirement or try an unexpected sequence of actions. The downside is that repeating the same checks for every release takes time, and people can miss things, get tired and work less effectively too. Automation, in the other hand, gives us repeatability. A reliable regression suite can check important behavior on every change and give the team quick feedback. But more automated tests don't automatically mean better coverage. A test that clicks through a page can often miss other wrong things on the page, which you would have to write tests for, which can lose other wrong stuff on the page, which can lose other wrong stuff...&lt;/p&gt;

&lt;p&gt;AI-assisted testing can make parts of both jobs faster. It can propose cases we didn't think of, help write tests and summarize failures. But a generated test is still a proposal. Someone needs to check its assumptions, assertions and relevance to the product. The stages aren't replacements for each other. We still need exploratory testing, and we still need reliable automation. AI adds another tool to that work. And as every other tool, pretty much of the work still depends on who is using the tool.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test count is not the same as confidence
&lt;/h2&gt;

&lt;p&gt;This reminds me of the session validation example in my article about &lt;a href="https://dev.to/proof-driven-development"&gt;proof-driven development&lt;/a&gt;: tests for a valid session, an expired session and an invalid signature can all pass while the application still accepts a revoked session. Passing a suite tells us that the checks we wrote passed. It doesn't tell us what we forgot to check. AI can make that gap wider if we use the number of generated tests as a measure of quality. It can produce many plausible cases from an incomplete requirement, including cases that repeat the same assertion or miss the behavior users actually depend on.&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;div class="c-embed__content"&gt;
        &lt;div class="c-embed__cover"&gt;
          &lt;a href="https://kevinaleman.com/proof-driven-development/" class="c-link align-middle" rel="noopener noreferrer"&gt;
            &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fkevinaleman.com%2Fimages%2Fpdd-header.webp" height="400" class="m-0" width="800"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="c-embed__body"&gt;
        &lt;h2 class="fs-xl lh-tight"&gt;
          &lt;a href="https://kevinaleman.com/proof-driven-development/" rel="noopener noreferrer" class="c-link"&gt;
            Proof Driven Development: can the agent prove it works? — Kev's logbook
          &lt;/a&gt;
        &lt;/h2&gt;
          &lt;p class="truncate-at-3"&gt;
            Microsoft Research's PDD Kit checks generated Rust code against formal specifications. Here's what I'd borrow for JavaScript and our testing work at Rocket.Chat.
          &lt;/p&gt;
        &lt;div class="color-secondary fs-s flex items-center"&gt;
            &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fkevinaleman.com%2Ffavicon.svg" width="32" height="32"&gt;
          kevinaleman.com
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;The &lt;a href="https://sre.google/sre-book/testing-reliability/" rel="noopener noreferrer"&gt;Google SRE book makes a similar point&lt;/a&gt;: passing tests doesn't necessarily prove that a system is reliable. The tests tell us something about the scenarios they cover; they can't tell us everything about the scenarios we didn't consider.&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;div class="c-embed__content"&gt;
      &lt;div class="c-embed__body flex items-center justify-between"&gt;
        &lt;a href="https://sre.google/sre-book/testing-reliability/" rel="noopener noreferrer" class="c-link fw-bold flex items-center"&gt;
          &lt;span class="mr-2"&gt;sre.google&lt;/span&gt;
          

        &lt;/a&gt;
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;For example, imagine a checkout flow where a discount should only apply once. An AI might generate several tests for valid and invalid coupon codes. Useful! But what happens if a customer sends more than one coupon? Should that be even possible? Or, what happens if the payment fails? Should the coupon be flagged as "used"? Wouldn't that make the human on the other side angry? Here's the human part: &lt;em&gt;giving context for the decisions made&lt;/em&gt;. That scenario may be less obvious and more expensive to get wrong, so no matter how many tests we write, if the tests are not &lt;em&gt;testing&lt;/em&gt; the right behaviors, we have created a safety net with enormous holes in it. So, never take the number of tests as a measure of "we're safe". The &lt;em&gt;quality&lt;/em&gt; of them should be evaluated too.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Finb0c61bl92mvunkc59h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Finb0c61bl92mvunkc59h.png" alt="A receipt falls toward a hole in a safety net stretched across a gap." width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The question is not just “Can we test this?” It's “What's the most important behaviors we should test?”&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Where AI can help
&lt;/h2&gt;

&lt;p&gt;I think AI is most useful here as a second set of eyes, not as the person deciding when we're done. Give it the change, relevant requirements and constraints, then ask it to suggest boundaries, negative cases and assumptions. Review those suggestions against the actual product.&lt;/p&gt;

&lt;p&gt;It can also help when a test run turns red. Give it the failure logs and it can suggest which failures might share a cause or where to investigate first. But we still need to check whether we're looking at a product bug, a flaky test or an environment problem. As &lt;a href="https://testing.googleblog.com/2017/04/where-do-our-flaky-tests-come-from.html" rel="noopener noreferrer"&gt;Google's testing team points out&lt;/a&gt;, a flaky failure can even be a sign of nondeterminism in the product itself. A useful suggestion is a starting point, not a diagnosis.&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;div class="c-embed__content"&gt;
        &lt;div class="c-embed__cover"&gt;
          &lt;a href="https://testing.googleblog.com/2017/04/where-do-our-flaky-tests-come-from.html" class="c-link align-middle" rel="noopener noreferrer"&gt;
            &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblogger.googleusercontent.com%2Fimg%2Fb%2FR29vZ2xl%2FAVvXsEgNWyw4Y5_hwMmvzu2TQN_NRQe6-nor1zoNerbGFZMkuXilBwORhKLK4Cf75dRPb_c_xrgglRKORxnGEMbEzIhylpLol-Z0DvG047859hcp2m3ZB1TXgQAbFp4rAJ7w1TsYsb7ZNw%2Fs1600%2Fgooglelogo_color_200x200.png" height="200" class="m-0" width="200"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="c-embed__body"&gt;
        &lt;h2 class="fs-xl lh-tight"&gt;
          &lt;a href="https://testing.googleblog.com/2017/04/where-do-our-flaky-tests-come-from.html" rel="noopener noreferrer" class="c-link"&gt;
            
Google Testing Blog: Where do our flaky tests come from?

          &lt;/a&gt;
        &lt;/h2&gt;
        &lt;div class="color-secondary fs-s flex items-center"&gt;
            &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Ftesting.googleblog.com%2Ffavicon.ico" width="32" height="32"&gt;
          testing.googleblog.com
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxsqsqv21nshx9knty4id.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxsqsqv21nshx9knty4id.png" alt="A QA engineer examines failure signals while an AI assistant groups them on a screen." width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A few things are still our responsibility:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Prioritize by risk.&lt;/strong&gt; A payment failure and a typo don't need the same level of coverage. Consider business impact, likelihood and how quickly the problem would be noticed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check the assertions.&lt;/strong&gt; A test should verify meaningful behavior, not just that the page loaded or a button was clicked.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep regression tests maintainable.&lt;/strong&gt; Prefer stable selectors and clear failures. A suite that fails for unrelated UI changes eventually becomes noise.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Verify AI output.&lt;/strong&gt; Check the generated data, expected results and assumptions. If a requirement is ambiguous, ask someone who knows the domain instead of treating the model's answer as the specification.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The human part
&lt;/h2&gt;

&lt;p&gt;The exciting part of AI in QA isn't that we can produce 40 test cases in seconds. It's that we can spend less time starting from a blank page and more time asking better questions. What did we assume? Which failure would hurt users most? What would this test miss?&lt;/p&gt;

&lt;p&gt;The tools will keep changing. The responsibility to understand what we're shipping stays with us. At the end, if we blindly trust what AI does, then we have replaced ourselves with it.&lt;/p&gt;

</description>
      <category>testing</category>
      <category>ai</category>
      <category>software</category>
    </item>
    <item>
      <title>AI Has a Problem With Functional Programming</title>
      <dc:creator>Kevin Alemán</dc:creator>
      <pubDate>Thu, 15 Oct 2026 20:09:09 +0000</pubDate>
      <link>https://dev.to/kaleman15/ai-has-a-problem-with-functional-programming-kpm</link>
      <guid>https://dev.to/kaleman15/ai-has-a-problem-with-functional-programming-kpm</guid>
      <description>&lt;p&gt;There is a pattern I keep seeing in AI-generated code. You ask it to implement something relatively simple and you get this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;processRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;processRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;transformData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;transformData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;mapper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Every function looks perfectly reasonable in isolation. But most of those functions add almost nothing. We've turned two operations into a small guided tour of the codebase.&lt;/p&gt;

&lt;p&gt;Very modular. I'm already tired.&lt;/p&gt;

&lt;p&gt;And yes, saying &lt;strong&gt;AI has a functional programming problem&lt;/strong&gt; is technically unfair. Functional programming isn't the issue. The problem is that LLMs seem unusually comfortable creating &lt;strong&gt;shallow abstractions&lt;/strong&gt;: functions whose only purpose is calling another function.&lt;/p&gt;

&lt;p&gt;And abstraction isn't free. Every function introduces another name, another place to jump to, another interface you need to keep in your head. You wanted to understand where the data comes from and now you're opening your fourth file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhpoo786zn1lg6s6jiv7l.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhpoo786zn1lg6s6jiv7l.webp" alt="A developer faces a winding route through five empty doorframes, while a short path leads directly to the same workbench." width="799" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Same job. More doors to open.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;John Ousterhout describes a similar problem with &lt;em&gt;shallow modules&lt;/em&gt;: abstractions whose interface is large relative to how much complexity they actually hide. They don't really remove complexity. They move it.&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;div class="c-embed__content"&gt;
      &lt;div class="c-embed__body flex items-center justify-between"&gt;
        &lt;a href="https://web.stanford.edu/~ouster/cgi-bin/cs190-spring16/lecture.php?topic=modularDesign" rel="noopener noreferrer" class="c-link fw-bold flex items-center"&gt;
          &lt;span class="mr-2"&gt;web.stanford.edu&lt;/span&gt;
          

        &lt;/a&gt;
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;



&lt;h2&gt;
  
  
  Why does AI do this?
&lt;/h2&gt;

&lt;p&gt;There isn't much evidence that LLMs inherently &lt;em&gt;prefer&lt;/em&gt; functional or modular code.&lt;/p&gt;

&lt;p&gt;In fact, an EMNLP 2024 study compared modular and non-modular versions of functionally equivalent code across several code models. Their conclusion was interesting: &lt;strong&gt;modularity itself was not a core factor in improving code-generation performance&lt;/strong&gt;.&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;div class="c-embed__content"&gt;
        &lt;div class="c-embed__cover"&gt;
          &lt;a href="https://aclanthology.org/2024.findings-emnlp.676/" class="c-link align-middle" rel="noopener noreferrer"&gt;
            &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Faclanthology.org%2Fthumb%2F2024.findings-emnlp.676.jpg" height="600" class="m-0" width="600"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="c-embed__body"&gt;
        &lt;h2 class="fs-xl lh-tight"&gt;
          &lt;a href="https://aclanthology.org/2024.findings-emnlp.676/" rel="noopener noreferrer" class="c-link"&gt;
            Revisiting the Impact of Pursuing Modularity for Code Generation - ACL Anthology
          &lt;/a&gt;
        &lt;/h2&gt;
        &lt;div class="color-secondary fs-s flex items-center"&gt;
            &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Faclanthology.org%2Faclicon.ico" width="32" height="32"&gt;
          aclanthology.org
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;So the problem may be less about what the model &lt;em&gt;wants&lt;/em&gt; and more about what we ask it to produce. We constantly tell AI to write code that is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;clean&lt;/li&gt;
&lt;li&gt;modular&lt;/li&gt;
&lt;li&gt;reusable&lt;/li&gt;
&lt;li&gt;maintainable&lt;/li&gt;
&lt;li&gt;single-responsibility&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Those are good goals. But they are incredibly easy to satisfy mechanically:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Take these five lines and give them a name.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There you go. Clean code. We can all go home.&lt;/p&gt;

&lt;p&gt;The model also doesn't experience the code like we do. Creating another helper costs it almost nothing. A human maintaining the result six months later has to navigate through it, figure out whether the helper does anything special and discover that, no, it just calls the next one.&lt;/p&gt;

&lt;p&gt;And we're starting to get actual evidence that &lt;strong&gt;functionally correct LLM code can still have structural quality problems&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9y6m6g6ghk1r4ifp0jvr.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9y6m6g6ghk1r4ifp0jvr.webp" alt="Two working control cabinets have green lights. One has tangled wiring and hard-to-reach components; the other keeps its wiring organized and components accessible." width="799" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Both lights are green. Guess which one you want to debug six months from now.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;An ICSE 2026 study measured how likely LLM-generated code was to contain code smells and found that generation strategy, model architecture, model size and, importantly, &lt;strong&gt;prompt formulation&lt;/strong&gt; influence those smells.&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;div class="c-embed__content"&gt;
      &lt;div class="c-embed__body"&gt;
        &lt;h2 class="fs-xl lh-tight"&gt;
          &lt;a href="https://www.microsoft.com/en-us/research/publication/a-causal-perspective-on-measuring-explaining-and-mitigating-smells-in-llm-generated-code/" rel="noopener noreferrer" class="c-link"&gt;
            A Causal Perspective on Measuring, Explaining and Mitigating Smells in LLM-Generated Code - Microsoft Research
          &lt;/a&gt;
        &lt;/h2&gt;
        &lt;div class="color-secondary fs-s flex items-center"&gt;
          microsoft.com
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;Another 2026 study looking at real repositories found that code detected as likely AI-generated showed substantial &lt;strong&gt;intra-repository code cloning&lt;/strong&gt;. Basically, repeated code inside the same repository. That's another sign that generating locally reasonable code doesn't necessarily produce a cleaner system globally.&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;div class="c-embed__content"&gt;
        &lt;div class="c-embed__cover"&gt;
          &lt;a href="https://arxiv.org/abs/2607.01867" class="c-link align-middle" rel="noopener noreferrer"&gt;
            &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Farxiv.org%2Fstatic%2Fbrowse%2F0.3.4%2Fimages%2Farxiv-logo-fb.png" height="467" class="m-0" width="800"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="c-embed__body"&gt;
        &lt;h2 class="fs-xl lh-tight"&gt;
          &lt;a href="https://arxiv.org/abs/2607.01867" rel="noopener noreferrer" class="c-link"&gt;
            [2607.01867] An Exploratory Study on LLM-Generated Code and Comments in Code Repositories
          &lt;/a&gt;
        &lt;/h2&gt;
          &lt;p class="truncate-at-3"&gt;
            The use of LLMs in software development has become increasingly widespread on tasks such as code generation and summarization. Reports from large technology companies showed that around 20% to 30% of their code are generated by LLMs. However, there remains skepticism about the practical usage of LLM-generated code and comments, such as concerns on more time for debugging the generated code and the unnaturalness of the generated comments. In this paper, we study the code and comments detected as likely to be generated by LLMs and their characteristics, the differences between company- and community-maintained repositories, and how likely bugs are associated with LLM-generated code. We conduct extensive experiments on active company- and community-maintained repositories from 2021 to 2025 using various tools and techniques that detect code and comments generated by LLMs. Based on our detector-based proxy analysis, the results suggest that code detected as likely to be generated by LLMs decreased over time and appeared frequently in test cases, while that of comments remains relatively stable. Proxy results further suggest that code detected as likely to be generated by LLMs shows substantial intra-repository code clones, whereas comments exhibit a relatively low proportion of grammatically correct sentences. In addition, the company-maintained repositories show a higher percentage of code and comments detected as likely to be generated by LLMs, and only a small percentage of the human-labelled bugs are detected as being likely associated with LLM-generated code.
          &lt;/p&gt;
        &lt;div class="color-secondary fs-s flex items-center"&gt;
            &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Farxiv.org%2Fstatic%2Fbrowse%2F0.3.4%2Fimages%2Ficons%2Ffavicon-32x32.png" width="32" height="32"&gt;
          arxiv.org
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;Maintainability research has moved far enough that researchers are now trying to predict the maintainability and code-smell characteristics of generated code &lt;strong&gt;before the model even generates it&lt;/strong&gt;. One October 2026 paper, PreMaQ, evaluated 2,695 tasks across four models and found that its predictions correlated meaningfully with the eventual maintainability metrics of the generated code.&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;div class="c-embed__content"&gt;
        &lt;div class="c-embed__cover"&gt;
          &lt;a href="https://arxiv.org/abs/2610.05858" class="c-link align-middle" rel="noopener noreferrer"&gt;
            &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Farxiv.org%2Fstatic%2Fbrowse%2F0.3.4%2Fimages%2Farxiv-logo-fb.png" height="467" class="m-0" width="800"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="c-embed__body"&gt;
        &lt;h2 class="fs-xl lh-tight"&gt;
          &lt;a href="https://arxiv.org/abs/2610.05858" rel="noopener noreferrer" class="c-link"&gt;
            [2610.05858] PreMaQ: Predicting Maintainability-Related Quality of LLM-Generated Code Before Generation
          &lt;/a&gt;
        &lt;/h2&gt;
          &lt;p class="truncate-at-3"&gt;
            As large language models (LLMs) become increasingly capable of code generation, adopting generated code in software development requires assessing not only its functional correctness but also its maintainability-related quality. If such quality could be estimated before generation, developers could avoid the cost of generating, reviewing, and discarding low-quality code. Although prior work has shown that the functional correctness of the LLM-generated code can be predicted in advance, it remains unclear whether maintainability-related quality is similarly predictable. We introduce Pre-Generation Maintainability-Related Quality Prediction (PreMaQ), which predicts the Code Smell Score (CSS) and Maintainability Index (MI) of generated code from the internal representations of LLMs before generation. Our evaluation covers four open-weight LLMs and four Python code generation benchmarks, comprising 2,695 tasks in total. Our results show that predicted CSS and MI consistently correlate with their observed values across all 16 model-benchmark combinations, achieving mean Spearman rank correlations of 0.57 and 0.65, respectively. When used for model selection, PreMaQ achieves 59.5% of the maintainability-related quality improvement attainable by an ideal maintainability-based selector over random selection on tasks for which multiple models generate functionally correct code. Combining predictions from PreMaQ and prompt embeddings increases this proportion to 60.7%, indicating that the two signals are complementary. These findings suggest that PreMaQ can be used to predict and improve the maintainability-related quality of LLM-generated code.
          &lt;/p&gt;
        &lt;div class="color-secondary fs-s flex items-center"&gt;
            &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Farxiv.org%2Fstatic%2Fbrowse%2F0.3.4%2Fimages%2Ficons%2Ffavicon-32x32.png" width="32" height="32"&gt;
          arxiv.org
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;None of these studies proves that AI loves one-line wrappers. They do show that "the code works" doesn't tell you everything about the code you're going to maintain.&lt;/p&gt;

&lt;h2&gt;
  
  
  So how do we stop it?
&lt;/h2&gt;

&lt;p&gt;Tell the AI that &lt;strong&gt;abstractions need to pay rent&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fy70q5z6y027zgee94psl.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fy70q5z6y027zgee94psl.webp" alt="Empty entrance facades pass a gear through unchanged, beside a workshop that hides useful machinery behind a small input hatch." width="800" height="421"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;A small entrance is useful when there's an actual workshop behind it.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I've started preferring instructions closer to:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Do not extract a function merely to make another function shorter. Create an abstraction only when it hides meaningful complexity, represents a domain concept, or is genuinely reused. Avoid pass-through functions and one-line wrappers. Prefer fewer concepts and a shorter call path.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The ICSE study above found that prompt formulation materially affects the kinds of code smells models generate, and that targeted mitigation can reduce their occurrence. That doesn't mean this exact prompt has been scientifically blessed. It means being specific about what you want is probably more useful than asking for "clean code" and hoping we all agree on what that means.&lt;/p&gt;

&lt;p&gt;One final rule I like:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If the function's name is basically an English translation of its body, consider deleting it.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Consider, not automatically delete. A one-liner that expresses a real domain rule can still be useful. But if the only reason it exists is that the other function was getting a little long, maybe we don't need it.&lt;/p&gt;

&lt;p&gt;AI is extremely good at producing more code. We're reaching the point where one of the most important skills when using it is asking it to produce less.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>programming</category>
      <category>softwareengineering</category>
      <category>cleancode</category>
    </item>
    <item>
      <title>I used AI to cheat my way to a 156-day reading streak</title>
      <dc:creator>Kevin Alemán</dc:creator>
      <pubDate>Tue, 13 Oct 2026 20:20:36 +0000</pubDate>
      <link>https://dev.to/kaleman15/i-used-ai-to-cheat-my-way-to-a-156-day-reading-streak-1mh4</link>
      <guid>https://dev.to/kaleman15/i-used-ai-to-cheat-my-way-to-a-156-day-reading-streak-1mh4</guid>
      <description>&lt;p&gt;I asked Codex to recover my Apple Books reading streak. Very important engineering work, obviously.&lt;/p&gt;

&lt;p&gt;Books showed two days. I had an 80-day run in the history, then a gap, then a few scattered reading days. It happens to the best of us. The app was reporting exactly what it had recorded. I wanted the old streak back anyway, so I asked an agent to fill the gap.&lt;/p&gt;

&lt;p&gt;It ended up showing &lt;strong&gt;156 days&lt;/strong&gt; and &lt;strong&gt;New Record&lt;/strong&gt;. I have read so much.&lt;/p&gt;

&lt;p&gt;Before we go any further: &lt;strong&gt;don't try this at home. This is cheating. Do actual reading.&lt;/strong&gt; The agent added reading time to dates that were below the goal. Those entries were manufactured. Nobody read 71 extra days while a script was running.&lt;/p&gt;

&lt;p&gt;But the process was interesting. It involved private Swift APIs, backups, a background process, and, of course, a cache. Apparently even my questionable reading decisions need cache invalidation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Start with the embarrassing part
&lt;/h2&gt;

&lt;p&gt;My daily goal was one minute. One minute. Apple Books was already being very generous with me. Had it on 15 mins a day, but I had to travel and reduced to 1 minute (talk about commitment issues).&lt;/p&gt;

&lt;p&gt;The old streak ran from May 11 through July 29, 2026. Continuing it through October 13 would make 156 calendar days. Of those, 85 already met their goals: the original 80, plus five later dates. That left 71 dates to fill.&lt;/p&gt;

&lt;p&gt;Seventy had no daily record. August 1 had six seconds, so it needed another 54. How do you even get six seconds of reading? IDK. A date that already met its goal needed no changes, even if it contained much more reading time.&lt;/p&gt;

&lt;p&gt;The calculation was small enough:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Pseudocode
&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;day&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;requested_range&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;goal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;stored_goal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;default&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;recorded&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;recorded_seconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;default&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;additional&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;goal&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;recorded&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;additional&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;increment_reading_time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;additional&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;default_goal&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The dates also needed the correct timezone. The edits used &lt;code&gt;America/El_Salvador&lt;/code&gt; and local noon, keeping each increment inside its intended calendar day. It would be a pretty annoying way to discover a timezone bug, after going through all this to change a number. A big part of this was possible (within the 5 hour window of my $20 codex subscription) thanks to the work previously done to understand the Apple books data model. Give it a read if you want to read about sqlite databases.&lt;/p&gt;


&lt;div class="ltag__link--embedded"&gt;
  &lt;div class="crayons-story "&gt;
  &lt;a href="https://dev.to/kaleman15/the-apple-books-data-model-1f8h" class="crayons-story__hidden-navigation-link"&gt;The Apple Books data model&lt;/a&gt;


  &lt;div class="crayons-story__body crayons-story__body-full_post"&gt;
    &lt;div class="crayons-story__top"&gt;
      &lt;div class="crayons-story__meta"&gt;
        &lt;div class="crayons-story__author-pic"&gt;

          &lt;a href="/kaleman15" class="crayons-avatar  crayons-avatar--l  "&gt;
            &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F255126%2F6bdaeb84-6c1e-4e76-9d45-54c59eaac908.jpeg" alt="kaleman15 profile" class="crayons-avatar__image" width="460" height="460"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div&gt;
          &lt;div&gt;
            &lt;a href="/kaleman15" class="crayons-story__secondary fw-medium m:hidden"&gt;
              Kevin Alemán
            &lt;/a&gt;
            &lt;div class="profile-preview-card relative mb-4 s:mb-0 fw-medium hidden m:inline-block"&gt;
              
                Kevin Alemán
                
                
              
              &lt;div id="story-author-preview-content-3964567" class="profile-preview-card__content crayons-dropdown branded-7 p-4 pt-0"&gt;
                &lt;div class="gap-4 grid"&gt;
                  &lt;div class="-mt-4"&gt;
                    &lt;a href="/kaleman15" class="flex"&gt;
                      &lt;span class="crayons-avatar crayons-avatar--xl mr-2 shrink-0"&gt;
                        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F255126%2F6bdaeb84-6c1e-4e76-9d45-54c59eaac908.jpeg" class="crayons-avatar__image" alt="" width="460" height="460"&gt;
                      &lt;/span&gt;
                      &lt;span class="crayons-link crayons-subtitle-2 mt-5"&gt;Kevin Alemán&lt;/span&gt;
                    &lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="print-hidden"&gt;
                    
                      Follow
                    
                  &lt;/div&gt;
                  &lt;div class="author-preview-metadata-container"&gt;&lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;

          &lt;/div&gt;
          &lt;a href="https://dev.to/kaleman15/the-apple-books-data-model-1f8h" class="crayons-story__tertiary fs-xs"&gt;&lt;time&gt;Jun 22&lt;/time&gt;&lt;span class="time-ago-indicator-initial-placeholder"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/div&gt;

    &lt;div class="crayons-story__indention"&gt;
      &lt;h2 class="crayons-story__title crayons-story__title-full_post"&gt;
        &lt;a href="https://dev.to/kaleman15/the-apple-books-data-model-1f8h" id="article-link-3964567"&gt;
          The Apple Books data model
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;div class="crayons-story__tags"&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/macos"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;macos&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/sqlite"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;sqlite&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/programming"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;programming&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/database"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;database&lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="crayons-story__bottom"&gt;
        &lt;div class="crayons-story__details"&gt;
          &lt;a href="https://dev.to/kaleman15/the-apple-books-data-model-1f8h" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left"&gt;
            &lt;div class="multiple_reactions_aggregate"&gt;
              &lt;span class="multiple_reactions_icons_container"&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/sparkle-heart-5f9bee3767e18deb1bb725290cb151c25234768a0e9a2bd39370c382d02920cf.svg" width="24" height="24"&gt;
                  &lt;/span&gt;
              &lt;/span&gt;
              &lt;span class="aggregate_reactions_counter"&gt;1&lt;span class="hidden s:inline"&gt;&amp;nbsp;reaction&lt;/span&gt;&lt;/span&gt;
            &lt;/div&gt;
          &lt;/a&gt;
            &lt;a href="https://dev.to/kaleman15/the-apple-books-data-model-1f8h#comments" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left flex items-center"&gt;
              

              &lt;span class="hidden s:inline"&gt;Add&amp;nbsp;Comment&lt;/span&gt;
            &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="crayons-story__save"&gt;
          &lt;small class="crayons-story__tertiary fs-xs mr-2"&gt;
            6 min read
          &lt;/small&gt;
          
          &lt;div class="only-sidebar-menu-item trusted-visible-block print-hidden align-center"&gt;
                
                    
                
          &lt;/div&gt;

        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;


&lt;/div&gt;
&lt;br&gt;


&lt;p&gt;Now, let's start.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make the backup before getting clever
&lt;/h2&gt;

&lt;p&gt;The history file involved was:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;~/Library/Group Containers/group.com.apple.iBooks/CRDTModelLocalFile/ReadingHistoryModel
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Before changing it, Codex backed up the history, its context and sync metadata, and the relevant Books preferences. The SQLite context databases were copied through SQLite's backup API. A permanent backup went into Documents, separate from the scratch files.&lt;/p&gt;

&lt;p&gt;Then the experiments happened on copies. That was useful because some of the first attempts at calling Apple's private Swift code failed.&lt;/p&gt;

&lt;p&gt;An agent having access to your files is a good reason to make the original easy to restore. Keep the backup somewhere separate, give it a name you understand, and be clear about which dates the agent is allowed to change. In this case, the scope was the missing goal time within one specific streak.&lt;/p&gt;
&lt;h2&gt;
  
  
  The C bridge that called Apple's Swift code
&lt;/h2&gt;

&lt;p&gt;This was the part I wanted to understand properly. Codex found an operation in the installed frameworks:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ReadingHistoryModel.incrementReadingTime(date:amount:defaultGoal:)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;But having a method name and calling the method are two separate problems. There was no available SDK module to import for this private interface. The tool used a C bridge to load the framework, resolve the Swift entry points, and call them with the required binary calling convention.&lt;/p&gt;

&lt;p&gt;The calls went in both directions. The Swift driver called ordinary C functions declared in &lt;code&gt;native.h&lt;/code&gt;. Those C functions called Apple's compiled Swift functions through typed function pointers. The framework was loaded at runtime with &lt;code&gt;dlopen&lt;/code&gt;; the C object file was linked into the driver at build time.&lt;/p&gt;

&lt;p&gt;Below is the complete C bridge used in this experiment, split into pieces so the explanation can stay next to the code. These are the actual functions and symbols used on this Mac. The Swift snippets are excerpts from the driver.&lt;/p&gt;
&lt;h3&gt;
  
  
  Start with the C interface
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;native.h&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdint.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;Bits&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nf"&gt;history_decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Bits&lt;/span&gt; &lt;span class="nf"&gt;history_describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Bits&lt;/span&gt; &lt;span class="nf"&gt;history_encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;history_increment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;goal&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nf"&gt;history_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nf"&gt;history_type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;history_set_streak&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;dictionary_metadata&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;code&gt;Bits&lt;/code&gt; carries two 64-bit words across the C boundary. On this installation, that matched the representation used for the &lt;code&gt;Data&lt;/code&gt; arguments and the &lt;code&gt;Data&lt;/code&gt; and &lt;code&gt;String&lt;/code&gt; results involved in these calls. Those words can contain storage references or inline information; they are the Swift value's representation, rather than a byte-buffer API.&lt;/p&gt;

&lt;p&gt;The C interface exposes opaque pointers for the model and its metadata. The driver uses &lt;code&gt;history_error()&lt;/code&gt; to retrieve a captured Swift error and &lt;code&gt;history_type()&lt;/code&gt; when reflecting on the model later.&lt;/p&gt;
&lt;h3&gt;
  
  
  Tell Clang how to call Swift
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;native.c&lt;/code&gt; — attributes, function types, and symbol lookup&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"native.h"&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;dlfcn.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;assert.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#define SWIFT __attribute__((swiftcall))
#define SELF __attribute__((swift_context))
#define ERROR __attribute__((swift_error_result))
#define RESULT __attribute__((swift_indirect_result))
&lt;/span&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="kt"&gt;uintptr_t&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;MetadataResponse&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="n"&gt;MetadataResponse&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;MetadataFn&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;uintptr_t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;SWIFT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;DecodeFn&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;RESULT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;uint64_t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;uint64_t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;SELF&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="n"&gt;ERROR&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;SWIFT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="n"&gt;Bits&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;BitsFn&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;SELF&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;SWIFT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="n"&gt;Bits&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;EncodeFn&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;SELF&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="n"&gt;ERROR&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;SWIFT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;IncrementFn&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int64_t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int64_t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;SELF&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;SWIFT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;metadata&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;witness&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;last_error&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nf"&gt;history_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;last_error&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nf"&gt;history_type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;metadata&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nf"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dlsym&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;RTLD_DEFAULT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;fprintf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stderr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Missing symbol: %s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The attributes carry the calling-convention information:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Macro&lt;/th&gt;
&lt;th&gt;Role in the bridge&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;SWIFT&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Call the function using Swift's calling convention&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;SELF&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Give the context parameter its special Swift ABI treatment&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;RESULT&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Pass storage for an indirectly returned value&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ERROR&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Receive the error result from a throwing Swift call&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;h1&gt;
  
  
  What's ABI
&lt;/h1&gt;

&lt;p&gt;No, it's not the name of a girl ;) even tho it may be. ABI stands for Application Binary Interface. In short, it defines how already compiled Swift code interacts at the binary level and how the code talks to the runtime.&lt;br&gt;
Type layout conventions, representation, metadata, etc.&lt;br&gt;
You can find way more about this on the web, and probably better explained :)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Clang still needs a function type that describes the lowered arguments correctly. &lt;code&gt;swiftcall&lt;/code&gt; handles their low-level placement; the bridge must supply indirect pointers, generic arguments, context, and error parameters in the right shape. The &lt;a href="https://clang.llvm.org/docs/AttributeReference.html#swiftcall" rel="noopener noreferrer"&gt;Clang attribute reference&lt;/a&gt; documents this distinction.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;symbol()&lt;/code&gt; helper resolves an address with &lt;code&gt;dlsym&lt;/code&gt;. The &lt;code&gt;$s...&lt;/code&gt; strings are Swift's mangled symbol names. They carry information about the module, type, operation, and signature. One of the names used here can be inspected with:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;xcrun swift-demangle &lt;span class="nt"&gt;--compact&lt;/span&gt; &lt;span class="s1"&gt;'$s13BookDataStore19ReadingHistoryModelV09incrementD4Time4date6amount11defaultGoaly10Foundation4DateV_S2itF'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The result identifies &lt;code&gt;ReadingHistoryModel.incrementReadingTime(date:amount:defaultGoal:)&lt;/code&gt;. Finding that address gives us a place to call. The typedef determines how the call gets there.&lt;/p&gt;
&lt;h3&gt;
  
  
  Decode a type C doesn't know about
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;native.c&lt;/code&gt; — loading and decoding&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nf"&gt;history_decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dlopen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/System/Library/PrivateFrameworks/BookDataStore.framework/BookDataStore"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;RTLD_NOW&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;RTLD_GLOBAL&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="n"&gt;MetadataFn&lt;/span&gt; &lt;span class="n"&gt;meta&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"$s13BookDataStore19ReadingHistoryModelVMa"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;metadata&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;meta&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;conforms&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"swift_conformsToProtocol"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;witness&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;conforms&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;metadata&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"$s9Coherence4CRDTMp"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="n"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;metadata&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;witness&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kt"&gt;uintptr_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;vwt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="kt"&gt;uintptr_t&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;metadata&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;vwt&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="n"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;4096&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;calloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;DecodeFn&lt;/span&gt; &lt;span class="n"&gt;decode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"$s9Coherence4CRDTPAAE14serializedDatax10Foundation0D0V_tKcfC"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;metadata&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;witness&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;metadata&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;last_error&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;fprintf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stderr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Native model decoded (%zu bytes)&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;code&gt;dlopen&lt;/code&gt; loads &lt;code&gt;BookDataStore&lt;/code&gt;. &lt;code&gt;RTLD_GLOBAL&lt;/code&gt; makes its loaded exports available to the subsequent &lt;code&gt;RTLD_DEFAULT&lt;/code&gt; lookups; the required Coherence symbols were available through the loaded dependencies.&lt;/p&gt;

&lt;p&gt;The metadata accessor returns the runtime type information for &lt;code&gt;ReadingHistoryModel&lt;/code&gt;. &lt;code&gt;MetadataResponse&lt;/code&gt; models its pointer-and-state result; &lt;code&gt;meta(0).value&lt;/code&gt; keeps the metadata pointer. &lt;code&gt;swift_conformsToProtocol&lt;/code&gt; then retrieves its conformance to &lt;code&gt;Coherence.CRDT&lt;/code&gt;. That conformance is represented by a witness table: the information the generic decoder needs to use this particular type's implementation.&lt;/p&gt;

&lt;p&gt;C also needs somewhere to put the decoded value. The pointer immediately before the metadata points to its value witness table, which includes layout and value-management information. The bridge reads the size slot used by this ABI, allocates storage, and passes it as the indirect result. The decoded model occupied 24 bytes in this run. Swift's &lt;a href="https://github.com/swiftlang/swift/blob/main/docs/ABI/TypeMetadata.rst" rel="noopener noreferrer"&gt;type metadata documentation&lt;/a&gt; describes the metadata and value witness table relationship.&lt;/p&gt;

&lt;p&gt;The decoder call has more arguments than its Swift source-level spelling suggests. It receives the output storage, the two &lt;code&gt;Data&lt;/code&gt; words, generic type metadata, the protocol witness table, the initializer's metatype context, and the error result. The metadata pointer appears twice because those are distinct roles in this entry point.&lt;/p&gt;

&lt;p&gt;The driver supplied the &lt;code&gt;Data&lt;/code&gt; representation like this:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;Foundation&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;arguments&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;CommandLine&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;arguments&lt;/span&gt;
&lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="n"&gt;arguments&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;fatalError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"input output [increments.json]"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="kt"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;contentsOf&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;fileURLWithPath&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt;
&lt;span class="nf"&gt;precondition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;MemoryLayout&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Data&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kt"&gt;MemoryLayout&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Bits&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;bits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;unsafeBitCast&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;to&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bits&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;history_decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bits&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bits&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;unsafeBitCast&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;history_error&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;to&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;any&lt;/span&gt; &lt;span class="kt"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Native decode failed: &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;code&gt;unsafeBitCast&lt;/code&gt; reinterprets the value's bits. The size check catches a size mismatch, but it does not establish that an arbitrary Swift type can safely be passed this way. This bridge depended on the representation and ownership behavior of these specific calls.&lt;/p&gt;
&lt;h3&gt;
  
  
  Inspect and serialize the model
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;native.c&lt;/code&gt; — description and encoding&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;Bits&lt;/span&gt; &lt;span class="nf"&gt;history_describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;BitsFn&lt;/span&gt; &lt;span class="n"&gt;describe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"$s13BookDataStore19ReadingHistoryModelV16debugDescriptionSSvg"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;Bits&lt;/span&gt; &lt;span class="nf"&gt;history_encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;EncodeFn&lt;/span&gt; &lt;span class="n"&gt;encode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"$s9Coherence11CRMergeableP14serializedData_7version10Foundation0D0VAA9CRContextC_AA16CRCodableVersionOtKFTj"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;conforms&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"swift_conformsToProtocol"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;mergeable_witness&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;conforms&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;metadata&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"$s9Coherence11CRMergeableMp"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="n"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mergeable_witness&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;MetadataFn&lt;/span&gt; &lt;span class="n"&gt;context_meta&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"$s9Coherence9CRContextCMa"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;create_context&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;SELF&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;SWIFT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"$s9Coherence9CRContextC6sharedACvgZ"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;create_context&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context_meta&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"$s9Coherence16CRCodableVersionO8version3yA2CmFWC"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;Bits&lt;/span&gt; &lt;span class="n"&gt;bits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;version&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;metadata&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mergeable_witness&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;last_error&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Bits&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;bits&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;code&gt;history_describe&lt;/code&gt; calls the model's debug-description getter. The Swift driver interprets the returned &lt;code&gt;Bits&lt;/code&gt; as a &lt;code&gt;String&lt;/code&gt; and uses that description in its round-trip checks.&lt;/p&gt;

&lt;p&gt;Encoding uses a separate &lt;code&gt;CRMergeable&lt;/code&gt; conformance, its serialization dispatch thunk, and the shared &lt;code&gt;CRContext&lt;/code&gt;. The bridge resolves the installed enum case tag for &lt;code&gt;version3&lt;/code&gt; and passes its address to the encoder. That tag lookup and argument representation were checked for this installation.&lt;/p&gt;

&lt;p&gt;Both throwing calls start with a null error pointer. On failure, the bridge captures the returned error for the driver. On success, encoding returns the representation of a Swift &lt;code&gt;Data&lt;/code&gt; value, which the driver writes to a scratch file and decodes again before any live replacement.&lt;/p&gt;
&lt;h3&gt;
  
  
  The increment, and the pointer that mattered
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;native.c&lt;/code&gt; — adding reading time&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;history_increment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;goal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;IncrementFn&lt;/span&gt; &lt;span class="n"&gt;increment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"$s13BookDataStore19ReadingHistoryModelV09incrementD4Time4date6amount11defaultGoaly10Foundation4DateV_S2itF"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;goal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This is the function that added the missing seconds. The driver already had the intended day and the amount needed to reach its goal:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="nf"&gt;history_increment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timeIntervalSinceReferenceDate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kt"&gt;Int64&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;edit&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"amount"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="mi"&gt;60&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The important detail in C is &lt;code&gt;increment(&amp;amp;date, amount, goal, model)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For this entry point on the installed build, Swift expected &lt;code&gt;Date&lt;/code&gt; indirectly. Its storage contained the reference-date time interval, so the wrapper put that value in a local &lt;code&gt;double&lt;/code&gt; and passed its address. The &lt;code&gt;model&lt;/code&gt; pointer received the Swift context treatment specified by &lt;code&gt;SELF&lt;/code&gt;. The &lt;code&gt;int64_t&lt;/code&gt; arguments also assumed the 64-bit &lt;code&gt;Swift.Int&lt;/code&gt; representation used on this Mac.&lt;/p&gt;

&lt;p&gt;An earlier attempt declared the date as a directly passed &lt;code&gt;double&lt;/code&gt;. The scratch process crashed inside &lt;code&gt;Calendar.component(_:from:)&lt;/code&gt;, attempting to read address &lt;code&gt;0x3c&lt;/code&gt;: decimal 60, one of the other argument values. The callee was reading the wrong thing as its date pointer. A small Swift probe compiled to LLVM IR confirmed the indirect date argument.&lt;/p&gt;

&lt;p&gt;That's a fairly concrete reason to inspect the calling convention before trusting a function-pointer cast. The compiler can accept the cast while the call is still wrong.&lt;/p&gt;
&lt;h3&gt;
  
  
  Update the record dictionary
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;native.c&lt;/code&gt; — adding streak milestones&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;history_set_streak&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;dictionary_metadata&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;intptr_t&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;intptr_t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"swift_reflectionMirror_recursiveChildOffset"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kt"&gt;intptr_t&lt;/span&gt; &lt;span class="n"&gt;field_offset&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;metadata&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;field_offset&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;set&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;SELF&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;SWIFT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"$s9Coherence15CRDictionary_v1Vyq_Sgxcis"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dictionary_metadata&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;field_offset&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The Swift driver reflected on the decoded model, checked that the selected field was named &lt;code&gt;streakRecords&lt;/code&gt;, and obtained its dictionary type metadata. On this build, it was &lt;code&gt;CRDictionary_v1&amp;lt;Int, Date&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The C helper asks for the field's offset, asserts the observed eight-byte offset, and calls the installed dictionary setter. The setter gets the optional date value, the integer key, the dictionary metadata, and the address of the dictionary field as its Swift context.&lt;/p&gt;

&lt;p&gt;The date argument here is a pointer to the actual Swift &lt;code&gt;Date?&lt;/code&gt; representation. The driver prepared it with:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;date&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addingTimeInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;86400&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;withUnsafeMutablePointer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;to&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;history_set_streak&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dictionaryMetadata&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Int64&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;That allowed the native setter to add milestones 81 through 156 while keeping the existing records. The field index, offset, dictionary type, and symbol name are all assumptions that need checking on another build.&lt;/p&gt;
&lt;h3&gt;
  
  
  Build the bridge and the driver
&lt;/h3&gt;

&lt;p&gt;In the scratch directory, the build commands were:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;clang &lt;span class="nt"&gt;-c&lt;/span&gt; native.c &lt;span class="nt"&gt;-o&lt;/span&gt; native.o

swiftc &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-module-cache-path&lt;/span&gt; /private/tmp/books-streak-recovery/module-cache &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-import-objc-header&lt;/span&gt; native.h &lt;span class="se"&gt;\&lt;/span&gt;
  repair.swift native.o &lt;span class="nt"&gt;-o&lt;/span&gt; repair
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;code&gt;-import-objc-header&lt;/code&gt; exposes the C declarations to the Swift driver; the header itself contains ordinary C. Linking &lt;code&gt;native.o&lt;/code&gt; supplies those helper implementations. The private Swift entry points are resolved later, when the running tool loads the framework.&lt;/p&gt;

&lt;p&gt;The full driver also handled the requested dates, error reporting, serialization, and verification. The snippets above focus on the language boundary.&lt;/p&gt;
&lt;h3&gt;
  
  
  Keep the assumptions visible
&lt;/h3&gt;

&lt;p&gt;This was a short-lived repair process with global state and build-specific layout assumptions. It leaves decoded allocations alive until process exit. A reusable library would need proper Swift value destruction, ownership and alignment handling, allocation checks, and a fuller error lifecycle. &lt;em&gt;But the goal is to not make this an habit and start reading like a functional human being&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;There is also a very practical footgun in the actual code: &lt;code&gt;dlopen&lt;/code&gt; sits inside &lt;code&gt;assert&lt;/code&gt;. These builds had assertions enabled. Defining &lt;code&gt;NDEBUG&lt;/code&gt; would remove the framework load along with the check; a maintained implementation should load it separately and handle failure explicitly.&lt;/p&gt;

&lt;p&gt;The code is here because it explains what made the experiment work. It should also explain why I keep saying to leave your reading-history files alone.&lt;/p&gt;
&lt;h2&gt;
  
  
  And then Books still said two days
&lt;/h2&gt;

&lt;p&gt;After the daily entries were filled, Books recognized another completed day in the current week. The streak still said two days. The record still said 80.&lt;/p&gt;

&lt;p&gt;Of course there was more state. It's Apple, there should be something else, somewhere.&lt;/p&gt;

&lt;p&gt;Three pieces needed to agree:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The daily history needed enough credited time for the missing dates.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;streakRecords&lt;/code&gt; needed the newly reached streak lengths and their dates.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ReadingHistory.CurrentStreak&lt;/code&gt; needed the new cached count.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The record dictionary ended at 80 days. Codex preserved those entries and added milestones 81 through 156, dated July 30 through October 13. We had to populate this data too.&lt;/p&gt;

&lt;p&gt;Swift reflection identified the record field and its dictionary type, and the bridge used the installed dictionary implementation's setter. Another check confirmed that these changes left the daily history unchanged. Finally, with Books closed, the cached count was updated:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;BOOKS_PREFS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$HOME&lt;/span&gt;&lt;span class="s2"&gt;/Library/Containers/com.apple.iBooksX/Data/Library/Preferences/com.apple.iBooksX.plist"&lt;/span&gt;

defaults write &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$BOOKS_PREFS&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; ReadingHistory.CurrentStreak &lt;span class="nt"&gt;-int&lt;/span&gt; 156
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;That command only handles the cache. The daily records and streak milestones had already been edited. &lt;code&gt;ReadingHistory.LastStreakDate&lt;/code&gt; already contained &lt;code&gt;20261013&lt;/code&gt;, so it stayed unchanged.&lt;/p&gt;
&lt;h2&gt;
  
  
  Make the app use the edited history
&lt;/h2&gt;

&lt;p&gt;Books and &lt;code&gt;bookdatastored&lt;/code&gt; can keep history in memory. Editing the file while they're using it creates an opportunity for a later write to replace your changes. Then we had to close Books app for the cache to be read again.&lt;/p&gt;

&lt;p&gt;Before replacement, the live daily history was compared with the version used to prepare the edits. The validated file was staged beside the live one, given the same permissions, flushed, and installed with &lt;code&gt;os.replace&lt;/code&gt;. The cache was refreshed while Books remained closed.&lt;/p&gt;

&lt;p&gt;Opening Books started the helper again. This time, Reading Goals showed &lt;strong&gt;156 days&lt;/strong&gt;, a &lt;strong&gt;156-day record&lt;/strong&gt;, and &lt;strong&gt;New Record&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The checks covered both the file and the app. The serialized output was decoded again, its model description was compared with the in-memory result, and every daily date, time, and goal was checked. The app then confirmed that it was displaying the intended result. FINALLY!&lt;/p&gt;
&lt;h2&gt;
  
  
  The advice I should probably follow
&lt;/h2&gt;

&lt;p&gt;There's an awkward connection to my last article about AI. I wrote about how much easier it is becoming to produce a result, and how we still need to pay attention to the work and understanding behind it:&lt;/p&gt;


&lt;div class="ltag__link--embedded"&gt;
  &lt;div class="crayons-story "&gt;
  &lt;a href="https://dev.to/kaleman15/the-future-after-ai-2767" class="crayons-story__hidden-navigation-link"&gt;The future, after AI&lt;/a&gt;


  &lt;div class="crayons-story__body crayons-story__body-full_post"&gt;
    &lt;div class="crayons-story__top"&gt;
      &lt;div class="crayons-story__meta"&gt;
        &lt;div class="crayons-story__author-pic"&gt;

          &lt;a href="/kaleman15" class="crayons-avatar  crayons-avatar--l  "&gt;
            &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F255126%2F6bdaeb84-6c1e-4e76-9d45-54c59eaac908.jpeg" alt="kaleman15 profile" class="crayons-avatar__image" width="460" height="460"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div&gt;
          &lt;div&gt;
            &lt;a href="/kaleman15" class="crayons-story__secondary fw-medium m:hidden"&gt;
              Kevin Alemán
            &lt;/a&gt;
            &lt;div class="profile-preview-card relative mb-4 s:mb-0 fw-medium hidden m:inline-block"&gt;
              
                Kevin Alemán
                
                
              
              &lt;div id="story-author-preview-content-4819808" class="profile-preview-card__content crayons-dropdown branded-7 p-4 pt-0"&gt;
                &lt;div class="gap-4 grid"&gt;
                  &lt;div class="-mt-4"&gt;
                    &lt;a href="/kaleman15" class="flex"&gt;
                      &lt;span class="crayons-avatar crayons-avatar--xl mr-2 shrink-0"&gt;
                        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F255126%2F6bdaeb84-6c1e-4e76-9d45-54c59eaac908.jpeg" class="crayons-avatar__image" alt="" width="460" height="460"&gt;
                      &lt;/span&gt;
                      &lt;span class="crayons-link crayons-subtitle-2 mt-5"&gt;Kevin Alemán&lt;/span&gt;
                    &lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="print-hidden"&gt;
                    
                      Follow
                    
                  &lt;/div&gt;
                  &lt;div class="author-preview-metadata-container"&gt;&lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;

          &lt;/div&gt;
          &lt;a href="https://dev.to/kaleman15/the-future-after-ai-2767" class="crayons-story__tertiary fs-xs"&gt;&lt;time&gt;Oct 8&lt;/time&gt;&lt;span class="time-ago-indicator-initial-placeholder"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/div&gt;

    &lt;div class="crayons-story__indention"&gt;
      &lt;h2 class="crayons-story__title crayons-story__title-full_post"&gt;
        &lt;a href="https://dev.to/kaleman15/the-future-after-ai-2767" id="article-link-4819808"&gt;
          The future, after AI
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;div class="crayons-story__tags"&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/ai"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;ai&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/programming"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;programming&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/career"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;career&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/beginners"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;beginners&lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="crayons-story__bottom"&gt;
        &lt;div class="crayons-story__details"&gt;
          &lt;a href="https://dev.to/kaleman15/the-future-after-ai-2767" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left"&gt;
            &lt;div class="multiple_reactions_aggregate"&gt;
              &lt;span class="multiple_reactions_icons_container"&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/sparkle-heart-5f9bee3767e18deb1bb725290cb151c25234768a0e9a2bd39370c382d02920cf.svg" width="24" height="24"&gt;
                  &lt;/span&gt;
              &lt;/span&gt;
              &lt;span class="aggregate_reactions_counter"&gt;3&lt;span class="hidden s:inline"&gt;&amp;nbsp;reactions&lt;/span&gt;&lt;/span&gt;
            &lt;/div&gt;
          &lt;/a&gt;
            &lt;a href="https://dev.to/kaleman15/the-future-after-ai-2767#comments" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left flex items-center"&gt;
              

              4&lt;span class="hidden s:inline"&gt;&amp;nbsp;comments&lt;/span&gt;
            &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="crayons-story__save"&gt;
          &lt;small class="crayons-story__tertiary fs-xs mr-2"&gt;
            6 min read
          &lt;/small&gt;
          
          &lt;div class="only-sidebar-menu-item trusted-visible-block print-hidden align-center"&gt;
                
                    
                
          &lt;/div&gt;

        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;


&lt;/div&gt;
&lt;br&gt;


&lt;p&gt;Here I was, asking an agent to produce a very impressive number in a book app. It did the software work. It inspected the state, made the edits, and checked the result. My reading habit still needs me to open a book tomorrow, but I'll start from 156 rather than 2 ;)&lt;/p&gt;

&lt;p&gt;If you want something practical to do with Books, &lt;a href="https://support.apple.com/en-euro/guide/books/ibks7c41beea/9.0/mac/27" rel="noopener noreferrer"&gt;set a daily goal you can actually meet&lt;/a&gt;. On the Mac, that's under Home → Reading Goals → Adjust Goal. Then read. After a missed day, start another streak.&lt;/p&gt;

&lt;p&gt;Gotta be honest: my reading goal was &lt;code&gt;1 minute&lt;/code&gt; and even with that low value, I couldn't do it (commitment issues? who?).&lt;/p&gt;

&lt;p&gt;The fun lesson, as always, is: you can have the streak you want. It's just a number. The actual value is never in the milestone, is what you learn in the road :)&lt;/p&gt;

</description>
      <category>ai</category>
      <category>swift</category>
      <category>macos</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
