Skip to content
American owned and operated
Nightshift

Agent architecture

The program the agent already wrote is your backend

An app built inside an agent ships with its data baked in, correct on the day it was made and wrong every day after. Code mode gives you a way out: promote the program that filled it in into the backend that keeps filling it in.

Nightshift EngineeringAugust 30, 202610 min read

Someone in operations describes the dashboard they want, the agent builds it, and twenty minutes later there is a working application. It reads well, the numbers are right, and everyone is pleased. Two weeks later it is quietly lying, because those numbers were written into the file at the moment it was generated.

The usual answer is that the prototype now needs a real backend, which means the engineering project the whole exercise was meant to avoid. There is a better one hiding in plain sight. If the agent was working in code mode, it already wrote a program that fetched the data. That program is the backend. This piece is about the transformation that makes it one, and why the security story is easier here than it sounds.

The snapshot problem

When an agent builds an application for you, it does two separate things that are easy to conflate. It writes the interface, and it obtains the data the interface displays. The interface is code and stays useful. The data is a literal, and it stops being true almost immediately.

This is not a flaw in how artifacts are built. A static file cannot query a warehouse. Faced with rendering something, the honest option is to render what was true when it was written.

Figure 1: what a baked-in answer does over time
Open ordersBuild dayOne week onOne month on
Artifact sayswritten into the file142142142
The business hasthe actual number142169203
Driftin step27 behind61 behind

The artifact is not broken. It is answering correctly about the day it was written, which is the one day nobody is asking about.

What lands in the file looks like this. Real structure, real component, and a constant where the interesting part should be.

The artifact, as generatedjavascript
// Generated 2026-08-30. These were correct at 09:14 that morning.
const OPEN_ORDERS = [
  { id: "ORD-1041", customer: "Northwind", total: 1840.0, days: 4 },
  { id: "ORD-1044", customer: "Contoso", total: 310.5, days: 6 },
  // ... 140 more
];

export function OpenOrders() {
  const late = OPEN_ORDERS.filter((o) => o.days > 5);
  return <Table rows={late} caption={`${late.length} orders past terms`} />;
}
The component is fine. It will render that array faithfully for as long as anyone keeps the tab open, and for months after it stopped describing anything.

The thing nobody kept

Now look at how that array was obtained. In code mode the agent did not call a tool and paste the result. It wrote a program against the typed API and ran it in a sandbox, which is the pattern the earlier post in this series covers.

What actually produced the numberstypescript
const orders = await erp.listOpenOrders({ division: "north" });
const enriched = orders.map((o) => ({
  id: o.reference,
  customer: o.account.name,
  total: o.amountDue,
  days: daysSince(o.dueDate),
}));
console.log(enriched.filter((o) => o.days > 0));
Read it as a handler rather than as a step in a conversation. It takes an input, queries a source, shapes a response and returns it.

That is a backend. It has a signature, a data source, a transformation and a return value. The only reason it is not serving traffic is that the runtime threw it away when the turn ended, which is exactly right for a chat message and exactly wrong for something someone is about to depend on.

So keep it.

Figure 2: promoting the program that already ran

While you build

The agent writes a program

  • Runs once against the MCP server
  • Its result is what fills the artifact
  • Ordinarily discarded when the turn ends

On approval

Keep it and pin it

  • Hash the source, record the ops it used
  • Lift the constants it was called with
  • Emit a manifest you can review

Once deployed

It is the backend

  • The artifact calls its own endpoint
  • The pinned program runs per request
  • Live sources, same ops, same policy

Nothing is generated at deploy time. The backend is the same program that produced the numbers you approved, which is why there is something specific to pin.

What promotion actually involves

Promotion is not a rewrite. It is capture plus three pieces of bookkeeping, and the bookkeeping is where the safety comes from.

You hash the program source, so the deployment refers to one exact artifact. You record the ops it called, which is the complete list of things it can reach and is short enough to read. And you lift the constants it happened to run with into a parameter schema, because a handler that always answers about the north division is not much of a handler.

The manifest, generated from the runjson
{
  "program": {
    "sha256": "9f2c41a8e07b3d5f6c1948ae2b70d3c5",
    "source": "programs/open-orders.ts"
  },
  "ops": ["erp.listOpenOrders"],
  "params": {
    "division": { "type": "string", "enum": ["north", "south", "central"] }
  },
  "runsAs": "caller",
  "cache": { "ttlSeconds": 60 }
}
Every field is derived from the run rather than written by hand. The enum came from the catalog, not from the agent guessing.

Parameter lifting is the one step that deserves suspicion, because it is the only place the surface grows. During authoring the division was a constant the agent chose; deployed, it is an input a caller supplies. That is a new thing to validate, and validating it against the catalog rather than against the agent’s imagination is what keeps it honest.

With the manifest in place the artifact changes in one small way.

The artifact, after promotionjavascript
export function OpenOrders({ division }) {
  const { data, error } = useEndpoint("/api/open-orders", { division });
  if (error) return <Stale reason={error} />;
  const late = (data ?? []).filter((o) => o.days > 5);
  return <Table rows={late} caption={`${late.length} orders past terms`} />;
}
The same component. The array it renders now arrives from the program that produced the original one, run again.

Why this is safer than deploying generated code sounds

Read the phrase "we deploy the code the model wrote" cold and it is alarming. It is worth being precise about why this particular version is not.

The deployed service exposes no route that takes a program. There is no eval, no run endpoint, no parameter that gets interpreted. The only thing a caller can send is arguments, checked against a schema frozen at promotion. The safety is structural: the capability to run arbitrary code was never built, which is the same argument as the isolate post, one level up.

Figure 3: an allowlist with exactly one entry

POST /api/open-orders

{ "division": "north", "since": "2026-08-01" }

Parameters only, validated against the frozen schema

POST /api/run

{ "code": "await op_query('SELECT * FROM ...')" }

404. This route was never built.

What the deployment holds

program.sha256

9f2c41a8e07b3d5f6c1948ae2b70d3c5

  • One program, content addressed
  • The ops it is allowed to call
  • The parameter schema, frozen
  • The identity each request runs as

A changed program is a different hash, which is a different deployment, which is another review.

The deployed service has no route that accepts a program. Parameters are the only thing a caller supplies, and they are checked against the schema the manifest froze.

The request path, in fulltypescript
export async function handle(req: Request, deployment: Deployment) {
  // There is no branch here that reads a program from the request.
  const params = deployment.schema.parse(await req.json());
  const identity = await authenticate(req);

  const isolate = await pool.acquire({
    program: deployment.program,       // pinned by hash
    ops: opsFor(deployment.ops, identity), // bound per caller
    limits: { ms: 5_000, heapMb: 128 },
  });

  try {
    return Response.json(await isolate.run(params));
  } finally {
    await isolate.dispose(); // fresh heap next request
  }
}
The ops are constructed per request from the caller’s identity, so the same pinned program returns different rows to different people without knowing that it did.

That last point is the one worth sitting with. The program does not implement authorization and cannot be trusted to. It calls an op, and the op was built with a particular identity in mind, so it applies that identity’s policy before touching a source. A program written while an administrator was authoring returns an administrator’s rows to an administrator and a warehouse clerk’s rows to a warehouse clerk, with no branch in the program expressing the difference.

One pinned program, two callersoutput
POST /api/open-orders  { "division": "north" }
  as dana@ (ops manager)   ->  142 rows, totals included
  as sam@  (warehouse)     ->  142 rows, totals masked
  as an unauthenticated viewer -> 401
Same hash, same source, same ops list. The difference is entirely in what the ops were bound to.

The part that is genuinely hard

Everything above is mechanism, and mechanism is the easy half. The hard question is delegation, and it arrives the moment the person who built the app sends the link to somebody else.

During authoring there was one identity and no ambiguity. Deployed, a request arrives from a viewer, and something has to decide whose authority it carries. There are only three answers and each one is a real position.

Whose authority does a request carry

ModelWhat it meansWhere it bites
As the callerEvery viewer needs their own grant on the sourceSafe, and the app is empty for anyone not already entitled
As the authorThe app carries its builder’s reachOne shared link quietly becomes a data export
As a service identityA narrow grant scoped to this deploymentSomeone has to define and review that scope

We default to the caller, because it is the only one that cannot surprise you, and treat the third as the deliberate exception with a named owner. Running as the author is the option that makes demos feel best and should be reserved for apps whose audience is exactly one person.

The rest of the sharp edges are ordinary engineering, but they are worth listing because a promotion pipeline that ignores them produces apps that fail confusingly rather than loudly.

  • The authoring program was written to answer a question once. It may log a five-row sample or stop at the first page, which is right for a chat reply and wrong for a handler. Promotion has to notice truncation and either lift it into a parameter or refuse.
  • A pinned program assumes a schema. When a source changes shape the program breaks, and it breaks for everyone at once. Catalog changes need to fan out to the deployments that depend on them.
  • Every render now costs a query. A time to live in the manifest is not an optimization, it is what stops a dashboard on a wall from becoming a load test.
  • Failure has to be visible in the interface. An app that silently renders nothing is worse than the snapshot it replaced, because at least the snapshot admitted what it was.
  • Promotion is a publishing step and needs the same ceremony: who approved it, what hash went live, what it can reach, and a way to take it down.

Why the pieces fit

This series has arrived at the same idea from three directions. Code mode moved the interesting decisions out of the model and into a typed API, because a program is a better interface than a list of tools. Isolates gave that program somewhere to run whose default is that nothing exists. This is what the two of them make possible together.

The reason the transformation is available at all is that code mode leaves an artifact behind. Tool calling does not: a sequence of calls and results in a transcript is not a thing you can pin, because there is no single object that is the logic. A program is. It hashes, it takes parameters, it has a declared surface, and it can be handed to a reviewer.

Which means the workflow people already want, describe an app and get one, stops ending at a snapshot. The app someone approved on Tuesday is the app that runs on Friday, against Friday’s data, through the same governed catalog and the same identity checks that everything else goes through. Nobody wrote a backend. The backend was the thing that filled in the numbers the first time.

Ready to start building your AI workforce?

It only takes an hour, no fluff. Meet with a Nightshift engineer to get started.