All posts
testingaiworkflow

Code Is Cheap Now. Guardrails Are the Bare Minimum

An agent writes a feature in minutes. Whether it still works next week depends on what checks it the moment it changes. What I treat as non-negotiable now, on the backend, on the web, and on mobile.

Muhamad Choirul Adam 7 min readPhoto: Unsplash

Writing code used to be the expensive part. You thought hard before typing because typing took a day. That's over. A coding agent writes a feature, its migration, and three refactors before lunch.

What didn't get cheaper is knowing whether the code works. If anything, it got more expensive: there's more of it, it arrives faster, and nobody typed every line by hand. So the question I ask about a codebase changed from "is this written well?" to "what tells us, in a few minutes, that a change broke something?"

That's what I mean by guardrails. Tests are most of it, but not all of it.

Why cheap code needs more checks, not fewer

An agent is fast and confident. It's also happy to "fix" a failing test by changing what the test expects, to touch a file you didn't mention, or to solve the problem you described instead of the one you have. None of that is malicious. It's what happens when output is cheap and context is partial.

Rendering diagram…

The loop only works if the middle box exists. Without it, the agent's speed just moves the bugs from your editor to your users. With it, the agent runs the checks itself, sees red, and fixes its own work before you read a line.

The floor, everywhere

Before the per-platform list, the things I set up on day one of any project, because they're nearly free:

GuardrailWhat it catches
Strict typesWrong shapes, missing cases, renamed fields
Lint and format in CIUnused code, risky patterns, noisy diffs
Schema validation at boundariesBad input from users, APIs, env vars, and models
A pre-commit hookThe obvious mistakes, before they reach a pull request
CI on every pull requestEverything above, on a machine that isn't yours

None of these are tests, and all of them shrink what the tests have to cover.

Backend: test the contract, not the internals

On a backend, the things that hurt are wrong responses, wrong data, and access checks that quietly disappeared. So the tests that earn their keep sit at the edges.

  • Unit tests for pure logic. Pricing rules, merge rules, date math. Fast, no I/O, lots of cases.
  • Integration tests against a real database. Mocks agree with whatever you wrote. A real Postgres or SQLite catches the constraint, the null, and the query that returns two rows.
  • Contract tests at the API boundary. Request in, status and body out, checked against the same schema the client uses.
  • Access checks as tests. Every route gets a "logged out" and a "wrong user" case. That's the test that would have saved me from trusting middleware alone.

With Hono, a whole route is testable without starting a server:

orders.test.ts
import { describe, expect, it } from "vitest";
import { app } from "../src/app";
 
describe("GET /orders/:id", () => {
  it("returns 401 without a session", async () => {
    const res = await app.request("/orders/ord_1");
    expect(res.status).toBe(401);
  });
 
  it("returns 404 for another user's order", async () => {
    const res = await app.request("/orders/ord_1", { headers: authAs("user_2") });
    expect(res.status).toBe(404);
  });
 
  it("returns the order for its owner", async () => {
    const res = await app.request("/orders/ord_1", { headers: authAs("user_1") });
    expect(res.status).toBe(200);
    expect(OrderSchema.parse(await res.json()).id).toBe("ord_1");
  });
});

One habit from building Luma that I now copy everywhere: golden tests that record a route's status, body, and the database queries it ran. When an agent "improves" a handler and it suddenly runs four queries instead of one, the diff shows it in review.

Web: test what the user does

On the web, the expensive bugs are a button that does nothing, a form that loses input, a page that crashes on empty data. Test at the level the user experiences.

  • Component tests with Testing Library: render, click, assert on what's visible. Not on state, not on class names.
  • A handful of end-to-end tests with Playwright for the flows that make money or lose trust: sign in, checkout, the main form. Five good ones beat fifty flaky ones.
  • Empty, loading, and error states on purpose. Agents write the happy path beautifully and forget the other three.
checkout.spec.ts
import { expect, test } from "@playwright/test";
 
test("a signed-in user can place an order", async ({ page }) => {
  await page.goto("/products/notebook");
  await page.getByRole("button", { name: "Add to cart" }).click();
  await page.getByRole("link", { name: "Cart" }).click();
  await page.getByRole("button", { name: "Place order" }).click();
  await expect(page.getByText("Order confirmed")).toBeVisible();
});

Selecting by role and visible text matters more now than it used to. An agent will happily rename a CSS class; it's much less likely to rename the button the user reads.

Mobile: the same idea, three layers

Flutter has the layers built in, and they map neatly onto the web ones:

LayerFlutterTests
Logictest()Pure Dart: parsing, rules, view model state
ScreenstestWidgets()Render a screen with fake data, tap, assert what shows
Whole appintegration_testReal app on a device or simulator, the critical journeys

Mobile adds two things the web gets to skip. Migrations: an app on someone's phone has last month's database, so every schema change needs a test that upgrades from the previous version. And offline: the app has to behave when the network doesn't, which is a test case, not a hope.

Code that calls a model needs evals

If the feature itself is AI, tests that expect an exact string won't work. That's what evals are for: a fixed set of inputs, a scoring rule, and a number you watch go up or down when you change a prompt or a model. Same principle as everything above, applied to output that isn't deterministic.

Coverage: 80 to 90 percent is the floor

When an agent writes most of the code, line coverage stops being a vanity metric. It's the cheapest way to see which code nobody checks, and the agent can write the missing tests in the same change. On a new project I set 80 to 90 percent as the floor, enforced in CI, not a target to reach someday.

A high number isn't the point by itself. Coverage of getters and generated code is noise, so I exclude those. What the floor guarantees is that the money paths, the access checks, and the merge rules can't hide in the untested 10 percent.

An existing project with no tests is different. Setting 80% on day one just blocks every pull request. There, I start small:

  1. Test the next bug before fixing it. A failing test, then the fix. Every bug pays for one test.
  2. Cover what you touch. New and changed code meets the floor, even if the old code around it doesn't yet.
  3. Ratchet. CI fails if coverage drops below the current number, and the number only goes up. Luma's CI works this way: thresholds only rise.
Rendering diagram…

The numbers above are an illustration, not a measurement. The shape is the point: the floor never moves down, and within a few months the old project is held to the same bar as a new one.

What I don't do

  • Let the agent edit a failing test to make it pass without me reading why. A test changing is a decision, not a fix.
  • Write tests after the feature ships. If the agent writes the feature, it writes the test in the same change. It's the cheapest moment either will ever be.

Why it matters

When code was expensive, the craft was in writing it. Now the craft is in deciding what "working" means and encoding it so a machine can check it in minutes. The guardrails are the part of the codebase that carries your judgment when you're not looking. With agents writing more of the code every month, that's not extra work anymore. It's the job.