Chesterton's Fence by kirupa (https://www.kirupa.com/me/index.htm) | filed under AI Fundamentals (https://www.kirupa.com/ai_fundamentals/index.htm) TL;DR: Before we remove a rule that looks pointless, we find out why it was put there, because the reason often lives outside the code, in a scheduled job, a kitchen, or a person's memory. That habit is called Chesterton's Fence, after a 1929 passage by G. K. Chesterton about a fence built across a road. - A coding agent deleted Pinwheel's unexplained 4pm cutoff and its new tests passed, but nine evening orders had no bread, because the 4:05pm prep sheet had already printed. - Dig before deleting: git blame shows the last change, git log -S finds the commit that added the code, and the crontab and the people who were there fill in the why. - To change an old rule, put the new behavior behind a flag that starts off, try it at one shop, watch closely, and keep a way back. - Tell coding agents to propose removing unexplained checks instead of removing them, and match how deep you dig to what a wrong removal could break. Here is a pull request that looks like a gift. A coding agent was asked to tidy up an old checkout page. It found a rule with no comment and no explanation that turned customers away every afternoon, so it deleted the rule. Every test passed. That evening, orders started coming in that nobody could fill. Cleaning up old code is one of the things coding agents love to do, and most of the time, that's great. Some of the strange-looking code in our projects is holding something up, though, and the code won't always tell us what. In this article, we will look at how to find out why something exists before we remove it, and how to get our agents to do the same. Onwards! The 4pm Problem Crumb & Co. recently bought Rolling Pin Bakeries and its six shops. Along with the shops came their ordering system, Pinwheel, a PHP and MySQL app that has run on one rented server since 2011. It has no tests and no API, and Yara, who leads Rolling Pin's two-person ordering team, has kept it running for years. One afternoon in early October, Wren (who leads the Biscuit team at Crumb & Co.) is reading Pinwheel's logs and notices that customers who try to order for tomorrow after 4pm get turned away with Sorry! Next-day orders close at 4pm. Here is how often that happened last week across all six shops: [Image: A bar chart of next-day orders Pinwheel turned away last week across Rolling Pin's six shops, by hour: 4pm 6, 5pm 11, 6pm 14, 7pm 8, and 8pm 2, for 41 in all. Beside it, Pinwheel's order page on a phone shows the message Sorry! Next-day orders close at 4pm, and its Place order button is grayed out.] (https://www.kirupa.com/ai_fundamentals/images/chestertons_fence_turned_away.png) That's 41 orders in one week that Rolling Pin never got. Pinwheel goes into a change freeze on November 1 for the holiday rush, so Wren wants this fixed soon and asks a coding agent: We're turning away evening orders. Clean up the checkout and fix whatever is blocking next-day orders after 4pm. The Cleanup That Passed Every Test Our agent searches the code and finds the culprit pretty quickly. The hour 16 (4pm on a 24-hour clock) is typed straight into four different files: [Image: Four files in Pinwheel, each with the hour 16 boxed in red: public/order.php has if (date('G') >= 16), includes/mail.php has $days = ($hour < 16) ? 1 : 2, admin/orders.php has echo "Cutoff: 16:00", and cron/nightly_report.php has WHERE HOUR(placed) < 16. The coding agent sits beside them and says: No comment, no test. Looks like a leftover rule.] (https://www.kirupa.com/ai_fundamentals/images/chestertons_fence_sixteen.png) What our agent has to go on is a bare number copied into four files, with no comment anywhere and no tests to explain it, because Pinwheel has never had any. To our agent, this looks a whole lot like a leftover restriction someone forgot to remove. So, our agent removes all four checks, writes three tests that place an order at 7pm, and reports back: [Image: The coding agent's summary of its change, titled Remove the leftover 4pm cutoff. It says the hour 16 is hard-coded in 4 files, no comment or test explains it, it removed all 4 checks so customers can order for tomorrow at any hour, and it added 3 tests that place an order at 7pm. All 3 tests pass, shown with a green check. A handwritten note points at the passing tests: all green!] (https://www.kirupa.com/ai_fundamentals/images/chestertons_fence_pull_request.png) All three tests pass. Wren skims the diff and ships our agent's change on a Tuesday afternoon. No Bread at 8am That evening, our change does what it promised, and 9 next-day orders come in after 4pm, including one at 7:12pm for two country sourdough loaves, pickup at 8am. The next morning, here is what is waiting for those customers: [Image: 8:00am at a Rolling Pin counter. A customer holds order #4417: 2 country sourdough, pickup 8:00am, placed at 7:12pm. The basket on the counter where that bread should be is empty. A tally beside it reads late orders 9, loaves ready 0.] (https://www.kirupa.com/ai_fundamentals/images/chestertons_fence_empty_basket.png) What went wrong? Our agent's change did exactly what it said it would do, so our problem has to be somewhere that never shows up in Pinwheel's code. Here is what a normal day looks like at a Rolling Pin shop: [Image: The kitchen's day in five steps: at 4:05pm the prep sheet prints, in the evening the crew mixes and shapes, overnight the dough cold-proofs, at 6am it is baked, and at 8am customers pick it up. An order placed at 3:40pm, marked with a green check, feeds into the 4:05pm prep sheet. An order placed at 7:12pm, marked with a red X, arrives after the sheet printed, and a handwritten note points at it: missed the sheet!] (https://www.kirupa.com/ai_fundamentals/images/chestertons_fence_timeline.png) At 4:05pm, a scheduled job on Pinwheel's server prints the prep sheet, which is the list of every loaf the shops need tomorrow. The evening crew mixes and shapes exactly what is on that sheet, and the shaped dough rests in the fridge overnight (aka a cold proof) before it gets baked in the morning. Our 7:12pm order arrived three hours after the sheet printed, so nobody ever mixed dough for it. Wren reverts our agent's change before lunch, and the shops spend the morning apologizing to nine customers (and handing out a lot of free croissants). A Fence in a Field What Wren learned the hard way is simple to say. Before we remove something that looks pointless, we find out why it was put there. If we can't explain why it exists, we don't get to delete it yet, because the reason may live somewhere our code will never show us. This idea is a lot older than software. In his 1929 book "The Thing", G. K. Chesterton imagined a fence or gate built across a road. One reformer walks up to it and says "I don't see the use of this; let us clear it away." A wiser reformer answers that if he doesn't see the use of it, he certainly won't be allowed to clear it away, and tells him to "Go away and think." Chesterton explains why: Some person had some reason for thinking it would be a good thing for somebody. And until we know what the reason was, we really cannot judge whether the reason was reasonable. G. K. Chesterton, The Thing, "The Drift from Domesticity" (https://archive.org/details/thingwhyiamcatho00ches_0) This habit of finding out why something exists before we remove it is called Chesterton's Fence. Here is our 4pm cutoff drawn as one: [Image: A road runs through a green field toward a hill. A wooden fence with a 4pm sign blocks the road, and a coding agent stands in front of it. Behind the hill, mostly out of sight, are the reasons for the fence: the prep sheet printer, the evening crew, the overnight cold proof, and tomorrow's bread. Handwritten notes point at the fence, what we can see, and at the things behind the hill, the reason.] (https://www.kirupa.com/ai_fundamentals/images/chestertons_fence_field.png) From the road, all we can see is our fence. The reason for it sits behind the hill, out of sight. Our agent could only see the code, and nothing in those four files ever mentions a printer. Programmers have their own version of this. In the Jargon File's "A Story About 'Magic'" (http://www.catb.org/jargon/html/magic-story.html), Guy Steele found a homemade switch glued to a cabinet of the MIT AI Lab's PDP-10 computer. It was labeled magic and more magic, and only one wire ran to it: [Image: A gag drawing of the switch from the Jargon File's story. A small switch is glued to the gray panel of a PDP-10 cabinet. Its two positions are penciled magic and more magic, and its lever points to more magic. A single wire runs from it to a ground pin. A handwritten note points at the wire: only one wire!] (https://www.kirupa.com/ai_fundamentals/images/chestertons_fence_magic_switch.png) A switch with one wire can't do anything, so they flipped it. The computer instantly crashed. Nobody ever worked out why. Digging for the Reason So, what should Wren (or our agent) have done first? We will start with the code's own history. The git blame command shows the last commit that touched each line of a file: [Image: A terminal running git blame on lines 41 to 43 of public/order.php. Line 42, if (date('G') >= 16) {, was last changed by commit 9c1e7a2 from yara on 2019-03-08. Running git show on that commit prints its message: Tidy whitespace. A handwritten note points at it: just a reformat.] (https://www.kirupa.com/ai_fundamentals/images/chestertons_fence_blame.png) Our line was last touched in 2019 by a commit called Tidy whitespace, which tells us nothing, because git blame only shows us the most recent change to a line, and the most recent change here was somebody fixing the indentation. To dig past it, we will use the git log -S command (aka the pickaxe), which finds every commit that added or removed a piece of text: [Image: A terminal running git log -S ">= 16" public/order.php. It finds commit 3b7d0e4 by admin, dated Mon Oct 6 2014, with the message: Close next-day orders at 4pm. Gus asked for this. A handwritten note points at the message: but why?] (https://www.kirupa.com/ai_fundamentals/images/chestertons_fence_pickaxe.png) Now we're getting somewhere. The check arrived on October 6, 2014, and the commit says that Gus, Rolling Pin's founder, asked for it. That tells us who and when. For the why, we have to go to people. Wren asks Yara first, and she points straight at the crontab, a file on the server that schedules all of Pinwheel's jobs: [Image: A terminal on Pinwheel's server running crontab -l. It lists three scheduled jobs: 0 2 * * * runs cron/import_wholesale.php at 2am, 30 23 * * * runs cron/nightly_report.php at 11:30pm, and 5 16 * * * runs cron/prep_sheet.php at 4:05pm. The 16 in the prep sheet's schedule is boxed in red, and a handwritten note points at it: there's our 16 again!] (https://www.kirupa.com/ai_fundamentals/images/chestertons_fence_crontab.png) The last line runs at minute 5 of hour 16, which is 4:05pm, and it prints the prep sheet. That's our 16 again, in a fifth place that isn't even in the project our agent searched. Gus fills in the rest. Rolling Pin started cold-proofing its sourdough overnight in the fall of 2014, and the evening crew needed one fixed list to work from. With all of that, we can finally draw what our cutoff is holding up: [Image: A stack of five blocks. At the bottom is the 4pm cutoff, the hour 16 in four files. Resting on it, from the bottom up: the prep sheet prints at 4:05pm, the evening crew mixes and shapes what is on the sheet, the dough cold-proofs overnight, and the confirmation email's promise of bread at 8am. A handwritten note points at the bottom block: the agent pulled this one!] (https://www.kirupa.com/ai_fundamentals/images/chestertons_fence_dependencies.png) If we pull out the cutoff, the 8am promise in the confirmation email stops being true for every order placed after 4:05pm. Here are the four places worth looking, from fastest to slowest: [Image: Four places to look for why a rule exists, from faster to slower: the history, with git blame and git log -S; old notes, like docs, tickets, and chat; what depends on it, like cron jobs, reports, and emails; and the people, like Yara and Gus. An arrow underneath runs from faster to slower.] (https://www.kirupa.com/ai_fundamentals/images/chestertons_fence_four_places.png) The history took a minute, Yara took a few more, and Gus answered one message. That's a small price next to nine customers with no bread. Changing It Safely Knowing the reason doesn't mean our fence stays up forever. Wren still wants evening orders, and the real question is now out in the open: can the kitchen handle orders that arrive after the sheet prints? Gus and the evening crew get to decide that. Say they agree, someday, to try a second, smaller prep sheet at 8:05pm at one shop. Here is how we will roll that out: [Image: Three stages for Rolling Pin's six shops. 1, ship it off: the flag is off, and all six shops close next-day orders at 4pm. 2, one shop: the flag is on for one shop, which closes at 8pm, while five stay at 4pm, and we check for bread at every late pickup. 3, all six: every shop closes at 8pm, and then we delete the old code and the flag. An arrow from stage 2 back to stage 1 says flip the flag off.] (https://www.kirupa.com/ai_fundamentals/images/chestertons_fence_flag_rollout.png) Our new behavior goes behind a feature flag, which is a setting that turns a code path on or off without shipping new code. Our flag starts off at every shop, so deploying it changes nothing. Outside of the change freeze, we will turn it on at one shop and check that every late order has bread waiting at pickup. If even one doesn't, we flip our flag off, and that shop is back to 4pm in seconds. Once all six shops have run on the new rule for a while, we will delete the old code path and the flag, because a flag is a fence too. Pete Hodgson's guide to feature toggles (https://martinfowler.com/articles/feature-toggles.html) warns that toggles add complexity, so we write down why our flag exists and when it should come out. Cleanup-Happy Agents Our agent wasn't being careless, since cleaning up code that looks unused or unexplained is exactly what we asked for. The catch is what our agent can actually see: [Image: The coding agent shines a flashlight on Pinwheel's repository, which holds public/order.php, includes/mail.php, admin/orders.php, cron/nightly_report.php, and its 3 new tests. Outside the beam, in a dashed box, sit the crontab on the server, the evening crew in the kitchen, and Gus's memory of why the cutoff exists. A handwritten note points at them: never in its context!] (https://www.kirupa.com/ai_fundamentals/images/chestertons_fence_agent_view.png) As we saw in How an Agent Like Claude Code Works (https://www.kirupa.com/ai_fundamentals/how_an_agent_works.htm), an agent only knows what lands in its context. Our crontab, our evening crew, and Gus's memory never landed there, so the tests our agent wrote could only check the part of the system it could see. We can't give our agent Gus's memory, but we can change the rules it follows. Most coding agents read a project instructions file (like AGENTS.md or CLAUDE.md) at the start of every session, so here is what Wren adds to Pinwheel's: ## Removing or changing old rules - Before you remove a check, limit, or special case, find out why it exists. Run git blame and git log -S on it, and search for anything else that uses the same value. - If you can't find the reason, don't remove it. Propose the removal in your summary, say what you found, and let a person decide. - A missing comment or a missing test is not a reason to remove code. - Put any change to an old rule's behavior behind a flag that is off by default. Take a look at the second rule. Our agent will still spot suspicious code, and instead of deleting it, our agent will hand us questions for Yara and Gus. For changes that could hurt customers, we can also require a person's approval first, which is the idea behind Human Approval for Risky Actions (https://www.kirupa.com/ai_fundamentals/human_approval_for_risky_actions.htm). When to Just Take It Down Now for the other side, because not every fence deserves to live. Chesterton himself allowed that once we know why a rule arose, we may find its purposes are "no longer served", like a workaround for a browser nobody uses anymore. Digging also costs time, so we match how deep we dig to how much a wrong removal could break (aka the blast radius): [Image: Three rules side by side above the ground, each with a hole dug under it. An unused CSS class could break one admin page, so it gets a shallow hole: just remove it. An old payment workaround could break some checkouts, so it gets a medium hole: a quick git log -S and a search. The 4pm cutoff could break tomorrow's bread, so it gets the deepest hole: the full dig, then a flag.] (https://www.kirupa.com/ai_fundamentals/images/chestertons_fence_dig_depth.png) An unused CSS class can just go, because if we're wrong, we'll notice right away. A workaround for an old payment provider gets a quick git log -S and a search. Anything that touches orders, money, or a scheduled job gets the full dig. If we dig and still find no reason, we remove the fence behind a flag and keep a way back, and the system will tell us what the fence was for...hopefully before nine customers do. Try It This Week Pick one rule in your product that everyone calls pointless (a limit, a cutoff, a strange special case), and find out why it exists: 1. Run git blame on its line, then git log -S with its value, and read the commit that first added it. 2. Search for everything else that uses the same value, including cron jobs and reports that live outside the repo. 3. Ask whoever has worked on that system the longest what it's for, and write their answer in a comment above the rule. 4. Tell your agent, in its instructions file, to propose removing unexplained checks instead of removing them. Frequently Asked Questions What Is Chesterton’s Fence? It’s the habit of finding out why something exists before we remove it. If we can’t explain why a rule that looks pointless was put there, we don’t get to delete it yet, because the reason may live somewhere the code never shows us, like a scheduled job or a person’s memory. Where Does Chesterton’s Fence Come From? It comes from G. K. Chesterton’s 1929 book The Thing, in the essay The Drift from Domesticity. He imagined a fence or gate built across a road and a reformer who wants it cleared away because he doesn’t see the use of it. The wiser reformer tells him to go away and think, and to come back once he can explain what the fence is for. How Do You Find Out Why a Piece of Code Exists? Start with the history: git blame shows the last commit that touched each line, and git log -S finds every commit that added or removed a piece of text, so it can dig past reformatting commits. Then look at old notes and at everything that depends on the code, like cron jobs, reports, and emails. Finally, ask the people who have worked on the system the longest. When Is It Okay to Remove Code You Don’t Understand? Match how deep you dig to how much a wrong removal could break. An unused CSS class can just go, while anything that touches orders, money, or a scheduled job gets the full dig. If you dig and still find no reason, remove it behind a flag, watch closely, and keep a way back. Conclusion Ok, that's it! Our agent saw an unexplained 16 in four files and removed it, and every test it wrote passed. The reason for that 16 was a printer, an evening crew, and dough resting overnight, and none of that lived in the code. Chesterton's Fence is the habit of finding out why something exists before we take it away. We read the history, follow what depends on it, and ask the people who were there. Then we decide, and if we still want the change, we make it behind a flag with a way back. Sometimes we won't find the reason, and sometimes our fence really is pointless. That's okay, as long as taking it down was a decision someone made on purpose. As for Pinwheel, our 4pm check now has a comment above it with Gus's name, the date, and the words prep sheet. Our agent will find it next time. Before you go, here are a few things you can do: 1. Test your knowledge by taking the quiz below. 2. Read the next article in the series, Reading a Codebase You Didn’t Write (https://www.kirupa.com/ai_fundamentals/reading_unfamiliar_code.htm), or check out some related content: - How an Agent Like Claude Code Works (https://www.kirupa.com/ai_fundamentals/how_an_agent_works.htm). See how a coding agent like Claude Code is built from basic parts. - Human Approval for Risky Actions (https://www.kirupa.com/ai_fundamentals/human_approval_for_risky_actions.htm). Sort agent tools by risk and pause for a person on the risky ones. - Untangling Tribal Knowledge (https://www.kirupa.com/ai_fundamentals/tribal_knowledge.htm). Get what only one person knows out of their head and into writing. 3. Stuck on something, or curious about a detail we skipped? Ask on the forums (https://forum.kirupa.com/), and we’ll help you out. [Mastery Quiz] Test your knowledge! Take the Chesterton's Fence Quiz: https://www.kirupa.com/quizzes/chestertons_fence.htm Practice with 6 multiple-choice questions and an explanation after every answer. [/Mastery Quiz]