Insights chevron AI, Software

Closing the deterministic gap between AI coding and enterprise apps

Banner Image

A coding agent built a complete enterprise application on our platform in three days. On demo data it worked; on realistic data it did not, because wherever the architecture left a decision open, the agent had taken the shortest path. This article shows where those shortcuts came from and how a deterministic frame of axioms, a refusing generator and measured query plans prevents them.

1. Recap: Why "fast-code"

Goal: generate the complete admin interface from the data model

The first article, Beyond Low-Code & AI – Schema-Driven "Fast-Code", described a platform with one goal: to generate the complete administration interface for an arbitrary relational data model, from the list and the form of each table to the navigation between related records. Its use case was the internal business application that today often lives in spreadsheets or in an aging database application: a few dozen tables, many relationships between them, and users who maintain the data every day. In such an application, most of the code repeats from table to table, and writing it by hand ties developers to work that a generator does faster and the same way for every table.

The database schema is the single source of truth

Everything the generator produces derives from the PostgreSQL schema: tables, columns, data types and keys. What the schema cannot express is stated in Smart Comments, machine-readable instructions in the comments of the schema, so that schema and metadata are versioned together.

The generator derives a deterministic ontology

From this schema, the generator builds an entity model: one entry per table and view, with its fields, its relations to other entities and, for every relation, the inverse relation on the other side. A product refers to its category, so the category knows its products, and both directions carry a name that the generated code uses. The model is deterministic: the same schema always yields the same model, and the generator stops when the two sides of a relation do not match. In effect, the model is the ontology of the application. It names every kind of object the application knows and every way in which two objects are connected.

Generated code under version control instead of a runtime engine

The platform does not interpret this metadata at runtime. Classic low-code platforms keep a configuration model and an engine that reads it on every request, so every capability of an application has to be a capability of the engine. Our generator instead writes ordinary TypeScript and React code into the repository at build time: pages, forms, API routes, data transfer objects and their validation. This code can be debugged, reviewed, tested and extended with standard tools, and the generator stops the build as soon as schema and code no longer match.

Row-level security and cascade delete are features of the platform

Two properties belong to the platform rather than to a single application. Access control lives in the database: PostgreSQL row-level security (RLS) policies decide for every query which rows the user may read and write. Deleting a record that other records depend on goes through a central cascade delete, which shows the user the full tree of dependent records before anything is deleted. Every application uses the same implementation and states only its own rules.

2. The sequel: from internal tools to end-user apps

End-user applications need hand-written frontends

Since the first article, the requirements have changed. The platform now also builds applications for end users: a marketplace with public pages, and applications that the employees of several organisations use, each organisation a tenant that sees only its own data. Such users expect an interface that follows their work, not a generic list and form per table. The frontend of these applications is therefore written page by page, by a developer or by an agent.

Everything below the frontend stays deterministic

A hand-written page calls a typed function with a filter; it writes neither an HTTP request nor SQL. Everything behind that call is generated, and deterministically so: the route checks the request and the rights of the caller and turns the filter into one SQL statement. Every table also has a read view, a database view that adds the name of each referenced row, so that a list shows the name of a customer instead of an ID.

Generated layers catch invalid requests, not wrong use of valid answers

This division makes it reasonably safe to let an agent write the pages. A page reaches data only through the generated functions and routes, so a field that does not exist is a type error at compile time, a filter on a field the caller may not use is refused with 400 or 403 before any SQL is built, and a row of another organisation never reaches the page because the database does not return it. Every query of a generated route has the same shape, so a review of the pattern covers all of them, and a schema change regenerates every layer in one run. What the generated layers cannot catch is a page that asks a perfectly valid question and uses the answer incorrectly. That boundary is the subject of this article.

Least privilege down to the database to avoid the confused deputy

The decisive part, however, is in the database. The server connects to PostgreSQL as the owner of the database and could read and write every row of every tenant. If the backend restricted each query itself, by appending the tenant and the read rules of the role as WHERE conditions, every query would run with the rights of the server, not with those of the user. Every forgotten condition, in a join, a subquery or a hand-written route, would be an access with server rights: the confused deputy problem. Such a restriction protects exactly the queries its author thought of, and a query that an agent adds later is not among them. The server therefore passes the identity of the user into every transaction, and the RLS policies check every row of every query against it. A query cannot read or write more than the signed-in user may, whoever or whatever wrote it. The filters of the application remain as a second layer that answers with a readable 403 instead of an empty result.

Block diagram: the PostgreSQL schema and Smart Comments form the entity model, the ontology; a deterministic generator derives typed functions, REST routes and the database layer from it; only the pages are written by hand. Guardrails act on every layer: axioms, a generator and routes that refuse, row-level security, measured query plans, and an agent that asks a human before open decisions


Ontology, generator, application and guardrails

3. The lessons: what a state-of-the-art agent does to an architecture

The case: three days on a generated backend

Three days. That is how long Claude Code, running Claude Opus 5.5, needed to build a multi-tenant business application on the platform, with approval workflows, documents and roles that each organisation defines for itself. The speed is not the agent's achievement alone. The agent wrote the schema and the pages, and the generator derived everything in between: routes, validation, security checks and typed functions. That was the whole idea: the generator sets the direction, and the agent fills in what is specific to the application.

It did not work out that way. The agent used the generated routes and functions, and built around them all the same. Fill the tables with the volume of real operation, look at what the database actually does for each page, and the picture changes: 71 findings in 73 requests, from full table scans to rows read only to be thrown away and subqueries that run once per row. Most of them do not come from the generated code but from what the generator left open. What counts as specific to the application, nobody had defined, and the read views, the security policies and the indexes are part of the schema that the agent wrote: with them, it decided without being asked where and how the application computes its data, each time sensibly in its local context. Four patterns explain almost all of them.

Open degrees of freedom

An agent optimises for the task in front of it: make this page show this figure. Where the architecture leaves the path open, it takes the shortest one, and the shortest path almost always leads into the browser. A tile shows the number of open requests? Load the requests and count them in JavaScript. Dozens of places in the application worked like this, each fetching up to 2,000 rows to produce a number that the database returns in a single query, and each one silently wrong from row 2,001 on.

The read views show the same pattern one layer further down. They exist for the names of referenced rows, such as the name of the customer next to the customer ID. For the agent, they were simply the most convenient place for anything a page wanted to show: sums over child rows, the date of the latest approval, business rules evaluated per row. Most read views became a hidden backend for individual pages. Every list that read such a view carried the computations of other pages along, and a filter on a computed column could never use an index.

Even the smallest open choice went the same way. The agent joined referenced rows with INNER JOIN where its template used LEFT JOIN, a difference of one word that forces the database to read the referenced row even when the query needs none of its columns. Switching the joins back reduced the data read by the whole application by a third.

Rules followed to the letter, one at a time

Rules alone do not prevent this, because the agent applies them precisely and one at a time. Our rule said that a page must not load all rows to search, filter or sort. Counting and summing were not on the list, so the agent kept loading all rows for them. When we banned subqueries from the read views, it removed them and rebuilt the same joins in the browser. Each step was a faithful fix of the rule at hand, and each broke a rule that was not in focus. What the agent missed was not a rule but the intent behind all of them: data is computed where it lives.

The questions nobody asks

The most expensive gaps are the ones that no task mentions. Any application in which several people decide on the same records has to handle concurrent changes, yet no table carried a row version, a number that changes with every write and lets an update detect that someone else got there first, so two approvers clicking at the same moment would have overwritten each other without an error. Every reference between rows belongs in a foreign key, yet some references were plain text that the database could not check. These are not exotic requirements; they are the questions an experienced developer asks in the first hour, and the agent did not raise them in three days. Other questions of architecture it settled in passing, as details of the implementation, without asking.

Plausible is not proven

When challenged, the agent always had an answer, and the answer was usually convincing. It defended the inner joins as a security feature, since they would hide rows whose referenced row the user may not see. That sounds right, and most reviewers would have accepted it. A test returned identical rows with and without them. An explanation by the agent always fits the code it wrote; whether it is true, only a measurement shows.

Conclusion: invisible at demo scale

What makes all of this dangerous is that it is free on demo data. A security check that runs once per row, a sort without an index, paging that reads every row before the requested page: on a few hundred rows, none of it is noticeable. The cost grows with every row, until a single list takes seconds. The 71 findings count database queries that read far more than they return. Behind them, together with gaps that no query plan shows, such as the missing row versions, lie well over a hundred places in code and schema, and no demo would have revealed a single one of them.

4. The response: closing the gap

More rules alone would not have prevented any of this. The agent had rules, and it followed them; what it lacked was a frame that leaves it no room for decisions it should not take. The response therefore does not try to make the agent more careful. It moves every decision it can out of the agent's reach: into axioms that state the intent, into a generator and routes that refuse, and into measurements that replace explanations. What remains open becomes a question that the agent has to ask.

Axioms that state the intent

The loose rules gave way to axioms, short principles that state not only what is forbidden but why, so that a case nobody listed still has an answer. Data is computed where it lives: searching, filtering, counting and summing happen in the database, and the browser shows what it receives. A list reads only its page, through an index and with keyset paging, which continues after the last row shown instead of reading and discarding every row before the page. A read view holds its table and the names of referenced rows, nothing else; a value that a page needs from other rows is either computed by a dedicated route for the rows of that page or stored at the row by the action that changes it. IDs from one response never become the filter of the next request, because a join belongs in one query. The axioms also fix how a page loads its data: when it opens, and again after every change the user saves, so that no page has to work out on its own when what it shows is stale.

Refusal instead of review

A rule that only a reviewer checks fails as soon as the reviewer misses a place, and an agent writes more places than anyone can review. Wherever a machine can check an axiom, the generator now does so and stops before it writes a single file. Every column that a client may filter or sort by needs an index that ends with the primary key, so that a sorted list can read exactly its page and stop; its first run found dozens of missing or unsuitable indexes. A read view with a subquery or an aggregate does not generate. Every table gets a row version, and an update that does not send the current one is refused with 409. Stored values, which an action computes and keeps at the row so that a list can show and filter them, cannot be written through the generated routes at all, only through that action.

At runtime, the generated routes add what only a request can show, as described in chapter 2: a filter, a sort order or a column outside the rights of the caller is refused before any SQL is built. Together, generator and routes leave a page little room to ask a question that has no index behind it.

The agent asks, the human decides

Some decisions cannot be checked by a machine, because they are genuine decisions: whether a new column belongs in a read view, whether a page may compute over a set of rows, whether a route is written by hand. For these, the project instructions that the agent reads at the start of every session list the situations in which it must stop and ask before it builds, and they state that a solution which merely works around an open question of principle is not built. Every mistake of the agent became part of this frame: a new or sharper axiom, a point on the list, and a short incident report that the agent reads before it touches the same area again. Without this step, the next session repeats the mistake, because the agent knows nothing that is not written down. The context of a session is finite, and a long working day ends in an automatic summary that decides on its own what survives; whatever must survive goes into the repository, as a commit, a document or a rule.

Measuring instead of believing

The last line of defence is measurement, on a stock of realistic size and with the rights of real roles. The plan check sends every request of every page, for several roles, to the backend and inspects the execution plan of each database query. A full scan of a large table, rows read only to be discarded, or a subquery that runs once per row counts as a finding, and a finding fails the check unless someone accepts it with a written reason. The same analysis runs inside the application: a debug panel shows for every request on screen its SQL, its plan and the findings at each step of the plan, so a slow page explains itself while you look at it. Verification scripts compare every stored value with a fresh computation, unit, integration and contract tests cover the routes, and render tests check the behaviour of every page without starting a browser. Tests with mocked routes, however, accept what the real route refuses; only the plan check, which calls the real routes, revealed pages that requested columns beyond the rights of their role.

Measurement also settles what reasoning cannot. Rewriting the security policy of one table made its list much faster and the list of another table much slower, because the changed estimate of the first table changed the plans of every query that joins it. Only a check across all pages and roles shows such a shift. The measurement corrects the agent in the other direction as well: when it reported that a list ignored its index, the plan was the same as always, and the extra cost came from the first query of a new database session, which loads the system catalogs once.

Conclusion: the result in numbers

Four days of hardening followed the three days of building. Afterwards, all requests of the pages of one role read a sixth of the data and run more than ten times faster. The rules that produced this result no longer live in anyone's attention but in a generator that refuses, in routes that refuse, and in a check that fails. The next application on the platform starts inside this frame, and the agent that builds it can no longer take most of the shortcuts of chapter 3.

5. Conclusion

A frame, not supervision

A state-of-the-art agent writes an application faster than anyone can review it, and it fills every space that the architecture leaves open with a decision that makes sense locally. Supervising it line by line does not scale, and instructions to be more careful are applied one rule at a time, as chapter 3 showed. What holds is a frame: generate whatever can be generated, refuse whatever breaks a rule, measure whatever cannot be refused, and leave the questions of architecture to a human. Inside such a frame, the speed of the agent finally pays off. A business application for end users, with tenants, freely configurable roles and the data volume of real operation, took three days to build and four days to harden, and the next application starts inside the frame from its first day.


Your Feedback is appreciated!

Enjoyed this? Get the next insight by email.

Related posts

Discover more insights - your next great read is just a click away!

Banner Image
AI
When the VIBEs begin to fade

AI and Vibe-Coding will not replace senior architects any time soon

Banner Image
AI, Software
Beyond Low-Code & AI - Schema-Driven "Fast-Code"

The Low-Code & AI Mirage vs. Deterministic Design - Why "Agentic Coding" Won't Replace Solid Architecture.