Rails World 2026: Harness Engineering on Rails (Joël Quenneville) Video: https://www.youtube.com/watch?v=L6-3a_rwLIg Talk page: https://www.rubyevents.org/talks/harness-engineering-on-rails Unedited machine transcript, generated with Whisper. It contains recognition errors (names, technical terms). Check the video before quoting. ---- At the beginning of this year, I started looking into more seriously writing most of my code using an LL. And I have this really vivid memory of picking up a feature that was scoped at about two days worth of work. And sort of working with it, with the agent, scoping it down, getting all the details right, and then letting it run. About an hour in, we basically have something that works. And this is amazing, right? I spent one hour to do two days worth of work. This is going to change everything. quality, so I'm going to polish it up, then put it for review. I'm going to look like a hero to the team. Well, lunchtime comes, and I've done a few rounds of polish, and it's getting better, but we're not quite done yet. About a half a day has been spent here, and spending a half day for two days worth of work, still a pretty good win. Well, evening comes, and I'm still not done. I've done multiple rounds of review, I've done a lot of cleanup. It's getting better, but it's not quite there. But still, one day of work for two days worth of scoped work, that's something. So, the next day, jump back into it. And now, I'm on a race against the clock. Because every hour I spend polishing, I'm seeing my leverage sort of diminish. And by evening, I finally limp over the finish line. I got it. I did two days worth of work in two days. That's really frustrating. What went wrong? Where did the promise of AI fall through? The problem is I spent one hour building code and then two days playing whack-a-mole with this, you might call it slop, that the AI generated for me. So I don't want to do this. I have a couple of options I could do. One, after that first hour, just put the PR up for review. Let my colleagues deal with the quality problem. Now I get to look like a hero, spent one hour to do two days worth of work, and I've just shifted that burden onto everyone else. Don't do that. If you do that, you're a bad person. gives me so that I'm not spending two days reviewing it. I'm spending an hour. So, how might I do that? Maybe I could do some better prompting. Sounds like all the pros are playing with agents.md. Maybe put some rules in there and that's going to help me write better code. That will work to a certain extent. But I'm afraid to say that that's a trap. There is a ceiling here. we're going to have to dig a little bit into the question, what is an agent? So let's say that we want to write the program FizzBuzz, and we want an LLM to write that for us. The most basic way we could do that is do a raw API call to, in this case, Anthropix API. And we say, hey, here's a spec. Write the code that will make that spec pass. we're going to write that a little bit in Ruby. Write a little bit of a Ruby wrapper around that because it looks cleaner. Now, what we've just done here is actually kind of special, so I want to really focus on that. This new wrapper is not just convenience. It's a thing called an agent harness. That is some code that wraps calls to an LLM model. And that really matters because on its own, a model can't do anything. It's just a brain in a jar. You put text in, you get text out. A harness, on the other hand, look, it's reading a file from disk. It's writing a file from disk. This is code. This is Ruby. We can add anything that we want around that model call. And that's really powerful. So we've got our harness. Let's run it, see how it goes. like this. It's correct-ish. There's a bug. That divisible by 15 line will never trigger because the divisible by three line will always run first. So, how do we prevent that? Maybe through better prompting? Or maybe better better prompting? The reason we fall into this is I think because we're coming at the problem with a of a naive mental model of what an agent is. We think of it as a black box, put a prompt in, get code out. If you want better code out, you need better prompt in. And this technique really hits the ceiling. It's a mental model that kind of comes from the chat GPT era, but modern day agents are actually quite a bit more sophisticated than this. And to understand why, I want us to pause and think, if I had given you that same prompt that I gave to the agent at the beginning, how would you have solved that by hand? Would you have tried to just from your mind make something that passes the spec? Maybe. There's really good developers in this room. But most of you probably would have run the spec. And then if you had missed an edge case, you would have fixed it, run the spec again, see it pass. So why not do that with the agent? Remember, a model can't run code on its own, so we can run it ourselves, pass the results into the prompt, and that works. But now we've put ourselves in the role of human plumbing. We're just doing that loop. And that's a waste of our time. That's a mechanistic sort of thing. Ideally, we would want something else to do this automatically. And this is where the harness comes in. Remember, it's just Ruby. We can put anything we want there, including a loop. So this is that exact same pattern from the previous slide, just automated in a loop in Ruby. And what we've done here is what's sometimes called an agentic loop. So now we don't just have prompt into a model into code, but a prompt comes into the harness. The harness might decide to go back and forth with the model. It can go query stuff in the environment. It can take action within the environment. and then it can decide to loop again. It can do multiple calls to the model and it can keep going until it hits some sort of exit condition. And this is really powerful because a loop allows an agent to respond to failure. It means that the agent is now self-healing and that the first generation doesn't have to be perfect. We don't have to try to one-shot this. It's okay if our agent writes that mostly good code or FizzBuzz, because it will run the tests, self-correct, and on the second, or the third, or the fourth generation, give us the correct answer, all without human intervention. The loop plus the tests are what are going to steer the agent to the right choice. The loop that we built is hard-coded to work with tests, but a generic version of this loop is at the heart of all the commercial harnesses that we use, things like cursor, clod code, or codex. For the rest of this talk, I want to focus on these commercial harnesses and ways that we can lean into the fact that they are a loop to move away from that prompt and pray model and to use that knowledge to raise the floor on the source of Rails code that we can write. This sort of thing is often termed harness engineering, and that's going to be the heart of this talk. So, how can we raise the floor on Rails? Consider this sloppy ruby right here. we would like the agent to not write stuff like this. So what do we do? We maybe add a bunch of rules to our agents.md. That might work. My general recommendation is that agents.md is not the place for your style guide. The problem is that agents.md is really just another form of prompt and pray. What the harness will do is it will take your agents.md to the front of your prompt. It's a convenience so that you don't have to type it out every time. But it's not really that much different than typing your rules in every prompt that you do. Also, this is probabilistic. The agent may or may not follow every rule in your agents.md. And as your context gets fuller and fuller, the odds of it not following it go up. So, what might be the more, you might call it, agentic way or harness engineering approach that we might take? Well, as with the testing, I'm going to pause and ask you, how would you solve this problem by hand, writing this sort of sloppy Ruby code? Well, teams have been solving this for years pre-AI. We've used a linter. We've used tools like RuboCop. So, we could write RuboCop and have it run and pass that into our prompt and do that loop, and that's good. But now we're in the role of human plumbing again. How might we wire this into a commercial harness? Because you can't open up the source of cloud code and wire in RuboCop. Here's a really simple way. Just add a line into your agents.md. Add that directive there and say, after changing Ruby files, run RuboCop. Now, this is still probabilistic, but look how much smaller your agents.md is. We've taken all of that knowledge, all of that judgment moved it into something deterministic. RuboCop owns your style guide. The only thing that's in agents.md is the directive to run it. But we can do better. If we want to get to 100%, we can lean into something called hooks. Now, these vary from one harness to another, and the harnesses are constantly iterating on this stuff. So, the exact code on this slide doesn't really matter so much as the idea. A hook is a lifecycle event that you can write code that will run when that event happens. So in this case, the stop event is what Claude will emit when it's trying to exit that agentic loop. And what it's going to do is it's going to call this code here. When it wants to exit, I'm going to make it run Rubocop. And if Rubocop fails, I'm going to say you're not allowed to exit. You must run again. fix its problems. And you might think, okay, cops can solve style problems, that's okay, but that doesn't really raise the floor that much. The bar's still down here. I'd encourage you all to be really ambitious because lots of quality problems have cop-shaped verifications. Consider an agent that is making this multi-tenant bug all the time. It's forgetting to scope your finders in an appropriate way. We could have a custom cop that looks like this that will catch that bug. And if this looks intimidating to you, remember that we have LLMs now. We can generate this in two minutes. This one here is kind of fancy. It's generic. It allows you to configure it using config like this. And it works pretty well. and you want it to stop that behavior, ask yourself, is there a way to catch that using a cop? And be creative, be ambitious. That said, there are some problems that genuinely aren't very well lender-shaped. And one that really bothers me are migrations. Rails migrations have this very particular file name. And agents see that, they try to mimic it, but they don't really understand what's driving it. it wrong. So how might we prevent the agent from getting that wrong? Maybe we try to dump the algorithm and prose in our agents.md. It's just more prompt and pray. Instead, I'm going to ask you again. How do you solve this by hand? How does every developer in this room not make a mistake when typing out that number? And the answer is, I don't type that number. You don't type that number. We use Rails generators for this. So why not have the model do it? We could put a directive in our agents.md that says always create migrations with Rails generate migration. Much better than trying to copy the algorithm in prose in a markdown file. And I found that this works maybe 90% of the time. But we can do better. If we want to get to 100%, we could lean on hooks again. This one here triggers every time that Claude tries to write a new file. And instead of inlining my code, it just calls out to a Ruby script I've written. That Ruby script says, Claude, if you try to write to the dbmigrate directory, you're blocked. You're not allowed to write new files to dbmigrate. And then it does something interesting. It gives a denial of reason. It says, instead of writing here, I want you to run the generator. That's important. Because agents have a goal. And if they know they need a migration to do the goal that you gave them and you blocked them, they're going to try to find a way to hack around it. And they're really good. By giving it an error message and making its easy path the right thing to do, that significantly reduces the chance of it trying to hack around your block and instead pushes them in the direction of doing the correct thing. Because agents want to take the easy path. And again, we can be ambitious here. Think of all of the sorts of things that you might want to capture in variants about your code, patterns that could be captured in a generator. Do you have a directory where you want all new controllers to inherit from a base class, but it's different than the existing patterns in your app, so the LLM is getting it wrong? Or maybe you want to ensure that every controller comes with a policy object for authorization. Put that in a generator. If anybody was at Rachel's talk here yesterday, she went in-depth on all the really cool things you can do with generators. If you missed it, I definitely recommend that you catch that when the recordings come out. And something I want to point out about the work that we've been doing is that we've been pulling logic and judgment out of a sort of agentic system, out of the LLM, and into deterministic tools. Somehow, when we did that, even though there's less intelligence involved, the system as a whole got smarter, more consistent. And that's a really interesting property. That said, there are times when we're going to hit the limits of that. There are times where some quality things are harder to catch deterministically. We need a heuristic. And we might want to lean on something more LLM-shaped. So how might we do that? Again, maybe this is the time to pull out agents.md. But, again, that's just more prompt and pray. And I've found that putting code heuristics in agents.md has a particularly poor track record, at least in my code basis. So, what can we do instead? I really like leaning on LLM review skills. Your harness probably ships with a built-in review skill, and that works pretty well. You can build your own. make them narrower. Don't just try to dump a bunch of random programming wisdom into a skill file. Instead, focus it on a particular theory of what good code is. In my case, I built three skills that are based on how I tend to review PRs. Sort of three phases. A very broad structural review. Does the solution kind of map well to the problem? A more method by method review. And then a final sort of polish phase where I look at whether code is readable, and well-documented. So we want to use this. We're going to have the LLM write its code, and then we're going to invoke these skills to get the review on it. And that works, but it puts us in the human plumbing role again. Instead, we want to think about how to get the harness to automate that. Now, we're going to want to make sure that we're running these skills in sub-agents. That helps with bias. And any time we're now starting to talk about agents talking to other agents, We're now in the world of orchestration. This is not real orchestration, but it's kind of simulating it, and it's good enough for this sort of thing. It's just a little skill, you would invoke it manually, and it says, hey, do the work I told you, and then run the sub-agent in this way, run this other sub-agent in this way, do this thing, you're done. Five lines. If you're looking to do something that's closer to real orchestration, dynamic workflow or ultra code. I know some of the other harnesses of built-in things. And there are also some third-party tools you can use. Now, some of you might be seeing this and, like, wanting to raise your hand, being like, wait a minute, you just told us that putting heuristics into agents.md is just prompt and pray. How is putting them in a skill any different? Didn't you just move it around? And here's where a distinction comes up that's really important. The challenge with prompt and pray with agents.md is that when you ask an LLM to build a program for you, you're sort of giving it instructions up front and asking it to pluck a program for the entire universe of possible programs. And you're saying, give me a program that meets all of these qualifications. And that's a pretty tall order. You're trying to steer the program by giving it instructions, the starting line. Instead, I find it's better to steer from the middle of the path. It's much easier to say, hey, here's some heuristics, and instead of applying them forward to the universe of all possible programs, let's apply them backwards to a concrete program I've already generated. It's much easier to find violations in a program I've written now than for all possible programs I could generate in the future. take these ideas and put them into a little bit more perspective. There's a mental model that I really like that comes from Brigitte Buchler, and she imagines various mechanisms for steering agents to sort of fit in this two-by-two quadrant system. You have mechanisms that can either steer by feed forward, that is, before generation happens, or through feed back after generation has happened. And those mechanisms can either be deterministic, or they can be LLMs, they're inferential. A RuboCop that we used is a feedback computational mechanism. Generators are a feedforward mechanism. An agentic review falls in that feedback inferential quadrant. Now, you might be wondering, we looked at three examples, but there are four quadrants. What is in that feedforward inferential quadrant? It's our good old friends prompting and agents.md. And I want to be clear, that quadrant is a really valid part of the story when it comes to steering your agents. It's just not the only one. In general, I find it's good to move judgment out of that feedforward influential quadrant and into the other three. Personally, I like to have my agents.md be a very thin, almost routing layer, rather than be a dump of my brain. Let's get creative. What else could we put on this quadrant as Rails developers? Well, we already saw RuboCop, but some other things we could put on here are reek, flog, code climate for just broad heuristics on code quality. Security scanners following that. Tests. Ways that allow an agent to verify their work. So, maybe looking at logs or opening up a browser with Playwright. In addition to generators and the feed forward, We can also have things like setup scripts or templates. I'm a huge fan of templates. We don't have time to dig into them, but come see me afterwards and talk about them because I've done really fun things with that. All sorts of agentic review and agentic-driven analysis happens in our inferential feedback quadrant. our prompts, but we've also got things like static docs or nearby source code. If you went to Kinsey's talk earlier, she went really deep on some of the things you can do to use source code as a steering mechanism. Now, you might see all this and be really excited to add it all to your app. And let me give you a word of warning. That is Goodhart's law. When a measure becomes a target, it ceases to be a good measure. And that's the thing that was developed for managing humans. but it also applies to agents. I found that when I constrain my agents too tightly, that's when they start trying to hack around things. So, especially if you're using something like Code Climate to give them a max score on something, if you put that too tight, your agents are going to find a way to satisfy your score in a way that's not going to satisfy you. So, give them a little bit of breathing room, and you'll get better results. of domain-specific scripts that I want to dig into a little bit. I've been recently working on a project where we are importing a bunch of documents, having to process them, having to connect them to other pieces of data in our system, and then internally publish them. And I've been using this domain-specific script approach to try to level up what an AI can do in that context. documents is only about 5%, and I need it to be up at 100. I pointed an LLM at this system, and it can see all the things. It's got real production data. And unfortunately, it doesn't give me any good results. It misses the big picture. It sees snippets of a database here, a bit of a document there, some source code even here. But every time I ask it questions or ask it to find root causes, and yes, there are multiple, it doesn't give me clear picture and it actually gives me contradictory answers. So, how can I make it better? As a human, I can bring order to that chaos because I have a mental model. What's happening here is we're working with a funnel, a leaky funnel. Data moves from one step to another and it can fail out for all sorts of reasons at various stages. So, I wrote a script that classifies all of the documents in a given population based off how far they went down the pipeline where they dropped out. And then I pointed the LLM at the script and said, all right, now try to find the root cause. And wow, it got so much better. And that's for a few reasons. Number one, having stable definitions means that its populations are now comparable. It used to not even have a consistent definition of what counted as successfully exited the pipeline. And so any time two agents were trying to come up with a story, they conflicted. Secondly, it now has a higher level language to think of when it's reasoning. It used to just be trying to find patterns between, oh, this column on this table or this bit of line in a code. But now it's thinking in terms of buckets and bales and pipelines and exit points. I gave it a richer set of primitives for thinking and communicating about the work I wanted it to do. And finally, the categorization itself is just really helpful. If it's doing an investigation, starting from a particular bucket of documents that have failed for a similar-ish reason is a great way to start digging deeper. But this is not just for agents. Having a domain-specific script is really good for humans, too. You may even have noticed that that's true for all of the other things I've shared. good for agents, but they make the developer experience better, too. And that's especially true for domain-specific scripts. We set out to raise the floor for the LLM, but at the same time, we raise the floor for all of the developers on our team. I'm incredibly bullish on domain-specific scripts in particular because they help address a sort of blind spot that's often in the heads of all of the developers on our team. It's all tacit knowledge. So I took the output of my script and I pointed it to a visualizer. And then I get something that's really nice like this. A visualization of exactly the state of my pipeline at any given point of time. And I can see where things are failing, where I need to zoom in, and where it's worth investigating. And this sort of thing is really easy to do in Rails because we have rake. We even have Rails shebang scripts. We can write sort of scripts on the side and then get access to all of the goodies that we need from Rails, the framework. So, we've seen a lot of different ways that we can use to have our harness raise the floor by reading signals from around it and by steering it. But this has to happen in a environment of continuous improvement. We had all of these different mechanisms, and you might be tempted to bring them all into your app at once. And I would recommend against that. Instead, try to drive it with lived pain. Any time you have to correct an LLM, ask yourself, how could this be the last time? How could I give the harness the tools so that the agent can self-correct next time without a human needing to go in and tell it what to do? Any time you find yourself in this human plumbing loop, try to get yourself out of it. Can the LLM get direct access to a tool and read or write something itself? A few questions you might want to use to improve your harness. Where did I have to intervene? What signals were missing? Was there anything I had to manually copy back and forth? Is there anything in my agents.md that could have been better done deterministically? Did the conversation degrade? Could it have been split into multiple conversations? Are there any mistakes that happened multiple times? And if you're like me, asking all these questions is a lot of work. when I'm a developer. So I package it up into a retro skill. I run this on a long session after I've interacted with an LLM, and it analyzes my history and suggests automations that I can do. This is up on my GitHub if you want to see it, joelq/skills. But it's possible that you already have this in your repo today. I don't know if anybody here uses Matt Pokok's skills. He just recently, I think, week released a skill that does the same thing, also named Retro. So, it's possible that if you use his skills, you already have this in your repo today and you can do this after your next AI session. So, going back to myself back in January, frustrated at the end of that first day, wondering where is all my leverage going? What would I tell myself about how to do things better? Well, the first thing is I need to change my mindset. my mental model for what is happening for an agent. No more prompt and pray. Instead, remember that an agent is a loop and that leaning into that loop allows it to self-correct and to write better code without human intervention. Secondly, any time I run into a mistake, don't just fix the mistake. Don't just prompt for a better fix. But instead, try to fix the system so that the next time it doesn't make that mistake again. the heart of Harness Engineering. And it's how I raise the floor on my code. It's how you all can raise the floor on your code. Thank you for coming to this talk. My name is Joel Kinville. I'm a principal developer at ThoughtBot. If you're interested in doing complex automations, a little bit like that document pipeline I talked about, or maybe you just want to have some help raising the floor on what your agents are creating, come talk to me, my colleagues at We would love to work with you and help you all get more out of your AI. I'm also a co-host on the bike shed podcast, bike shed.fm. There won't be any questions after this, but I'm going to hang out on the corner there if you want to come talk to me. I had to cut so much out of this talk. There's so many exciting things that we can do with AI to get better quality code, and I'd love to hear from you. Thank you. you