Every engineering team that set out to build an autonomous AI coding agent in 2023 fell into the exact same trap.
The initial demo looked like magic. You prompted a Large Language Model with a high-level goal, wrapped it in an open-ended while-loop, exposed bash execution and file system tools, and watched the model brainstorm, write code, run tests, and debug failures. For a three-file toy script or a standalone landing page, the agent appeared remarkably capable.
Then teams pushed these agents into real-world production.
Users gave agents complex refactoring tasks, messy monorepos, and open-ended product instructions. Almost immediately, the unbounded loop collapsed under its own weight. Agents fell into recursive rabbit holes, attempting to fix minor linter warnings by rewriting entire dependency trees. Costs exploded as bloated conversational histories re-processed millions of redundant tokens. Deployments killed in-flight terminal commands, corrupting workspace state. When users interrupted an agent to clarify a requirement, the running execution broke because message receipt and code modification were bound to a single thread of execution.
In November 2026, the industry received an unusually candid post-mortem from Anton Osika and the engineering team behind Lovable—a platform where users have generated tens of millions of applications and over 100,000 merged pull requests:
"We deleted almost all our agent code and went back to one user request resulting in a rather predictable chain of tools... Giving them more freedom made the product less dependable."
This was not an admission of defeat; it was the arrival of mature systems engineering in the agentic space. The toy era of naive while(agent.step()) loops has ended. In its place, high-scale engineering organizations are coalescing around a rigorous distributed systems architecture: Git-modeled event trajectories, dual-log inbox decoupling, lazy context projection, and stateful machine-level sandbox snapshots.
The paradox of autonomous software engineering is that reliability is not achieved by giving the model unbounded freedom. It is achieved by constraining the model's operational envelope and treating execution history as a formal distributed log.
To understand why the new agent architecture looks the way it does, we must dissect why early agent topologies failed. The naive architecture relied on three fatal assumptions:
- State as a Flat Conversation: The agent runtime treated execution history as a flat array of alternating
role: user,role: assistant, androle: toolmessages. Whenever an agent took an action, the output was appended to the chat array and fed directly back into the next inference call. - Synchronous Execution Coupling: Message arrival, agent reasoning, tool invocation, and human feedback were locked into a single synchronous loop. If a human typed "Stop, that's the wrong database table", the message could not be processed until the agent completed whatever multi-minute bash command it was running.
- Lossy State Mutation: Workspaces were treated as mutable directory trees on ephemeral virtual machines. If an agent attempted five bad refactorings before finding a working solution, the intermediate diffs polluted the git tree or broke node_modules beyond repair.
THE FRAGILE NAIVE LOOP (2023-2024)
─────────────────────────────────────────────────────────────
User Prompt ──► [ while(!done) { LLM -> Tool -> State } ] ──► Broken App
│ ▲
▼ │
Unbounded Drift Context Bloat
(Cost Explodes) (Infinite Loops)
THE PRODUCTION DISTRIBUTED AGENT RUNTIME (2026)
─────────────────────────────────────────────────────────────
User / ACP ──► [ INBOX LOG ] (Decoupled Arrival Buffer)
│
▼ (Admitted at Iteration Boundary)
[ AGENT TRAJECTORY ] ──► Single-Writer Git-Modeled Event DAG
│
├─► Lazy Context Projection (Compaction / Sidechain)
├─► Stateful Sandbox Snapshots (Fork Memory/Disk)
└─► Pub/Sub Activation Layer (Durable Suspend/Resume)
When systems scale to hundreds of millions of daily user turns, these flaws turn into catastrophic failure modes. The solution required decoupling three concerns that naive implementations conflated: what happened, what the model needs to see, and when the runtime executes.
The most critical architectural invariant of modern agent runtimes is the separation of the log into two distinct data stores: The Inbox and The Agent Trajectory.
In distributed systems, concurrent writes to a shared state machine require complex locking mechanisms. If a user, an automated CI webhook, a supervisor agent, and a scheduled background timer all attempt to write to an active agent's prompt history simultaneously, race conditions and prompt corruption are inevitable.
┌───────────────────────────────────────────────────────────────────────────┐
│ DUAL-LOG AGENT RUNTIME TOPOLOGY │
├───────────────────────────────────────────────────────────────────────────┤
│ │
│ EXTERNAL ACTORS (Concurrent Writers) │
│ [ User Message ] [ Subagent Finished ] [ CI Failure ] [ Timeout ] │
│ │ │ │ │ │
│ └───────────────────┴─────────┬─────────┴──────────────┘ │
│ ▼ │
│ ┌───────────────────────────────────────────────────────────────────┐ │
│ │ INBOX LOG (Durable Buffer) │ │
│ │ - Append-only message queue (ExternalAgentNotification) │ │
│ │ - Zero lock contention with running inference │ │
│ │ - Safe destination for fire-and-forget orchestrators │ │
│ └────────────────────────────────────┬──────────────────────────────┘ │
│ │ │
│ Admitted ONLY at Iteration Boundaries │
│ │ │
│ ▼ │
│ ┌───────────────────────────────────────────────────────────────────┐ │
│ │ AGENT TRAJECTORY (Strictly Single-Writer) │ │
│ │ - UserMessage admitted as formal event │ │
│ │ - IterationStart / IterationEnd │ │
│ │ - Model Thinking & Partial Streams │ │
│ │ - ToolParametersValidated -> ToolExecutionEnd │ │
│ └────────────────────────────────────┬──────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌───────────────────────────────────────────────────────────────────┐ │
│ │ LAZY CONTEXT PROJECTION (Prompt Engine) │ │
│ │ - Walks parent pointers backwards │ │
│ │ - Swaps raw iterations for PromptCompactionEnd summaries │ │
│ │ - Dynamically injects codebase slices and rules │ │
│ └───────────────────────────────────────────────────────────────────┘ │
└───────────────────────────────────────────────────────────────────────────┘
The inbox is a durable, append-only buffer. Any external actor—a human chatting in a browser, an automated security linter, or an orchestrator control plane—writes directly to the inbox. Crucially, writing to an inbox requires zero coordination with the agent process. It is non-blocking and cannot cause deadlocks.
The trajectory represents the agent's authoritative historical reality. It is strictly single-writer: only the active agent worker node can append events to its trajectory.
Instead of swallowing external inputs mid-thought, the agent inspects its inbox at deterministic boundaries:
- Immediately upon worker boot.
- At every
IterationEndboundary (the precise millisecond between completing a tool call and formatting the next LLM request).
If an external message arrived while the agent was running a 40-second test suite, the agent finishes processing the test results, reaches the iteration boundary, admits the inbox notification into its trajectory, and surfaces it to the model as an interjection. The model's reasoning loop remains pure, deterministic, and 100% reproducible.
In early agent frameworks, "memory" was managed either by saving full string snapshots of the prompt or by keeping a mutable database row of the agent's current task status.
Both patterns fail in production. Full prompt snapshots discard the causal relationship between actions, making it impossible to analyze why an agent made a catastrophic mistake. Conversely, mutable status rows erase history, making deterministic replays and offline model evaluations impossible.
Modern platforms model the trajectory after the Git object store:
Every single interaction—AgentStart, IterationStart, ToolApprovalRequired, ToolExecutionStart, ToolExecutionEnd, and AgentDone—is an immutable event carrying exactly one parent pointer.
GIT-MODELED TRAJECTORY TREE WITH O(1) ZERO-COPY FORKS
─────────────────────────────────────────────────────────────────
(e0) Root: UserMessage
│
(e1) IterationStart
│
(e2) ToolCall: search_files("auth.ts")
│
(e3) ToolResult: Found 3 references
│
(e4) IterationEnd ◄──────────────────────┐ (Fork Point)
│ │
├─────────────────────────────┐ │
│ (Main Line of Execution) │ (Exploratory Subagent Fork)
▼ ▼
(e5) ToolCall: edit_file() (f1) ThreadForkConfig (parent: e4)
│ │
(e6) ToolResult: 200 OK (f2) ToolCall: test_hypothesis()
│ │
(e7) AgentDone (f3) ExternalNotification to Main Inbox
In Git, creating a branch does not copy the repository's entire history; it simply creates a lightweight reference pointing to an existing commit SHA.
By modeling agent events with a single parent pointer, branching an agent becomes an zero-copy operation.
When an orchestrator wants to spin up an exploratory subagent to investigate an alternative database migration strategy, it does not clone the parent agent's database rows or re-serialize megabytes of prompt text. It emits a single ThreadForkConfig event whose parent pointer targets the current IterationEnd.
The subagent immediately inherits the entirety of the parent's causal history. It can iterate independently on its branch, reach a conclusion, package its findings into an ExternalAgentNotification, deposit that notification into the parent's inbox, and shut down. Trajectories only ever branch; they never merge.
A common fallacy in AI development is assuming that what happened in the system is identical to what must be sent to the LLM context window.
In reality, feeding raw event streams into frontier models is financially ruinous and computationally toxic. A single day of complex coding generates hundreds of megabytes of raw AST diffs, terminal outputs, JSON-RPC handshakes, and intermediate shell logs.
Production agent engines decouple the Source of Truth from the Model Projection:
// Context Construction as a Backward-Walking Projection
interface AgentEvent {
id: string;
parentId: string | null;
type: string;
data: Record<string, unknown>;
}
function projectContext(headEventId: string, eventStore: Map<string, AgentEvent>): LLMMessage[] {
const context: LLMMessage[] = [];
let currentId: string | null = headEventId;
while (currentId) {
const event = eventStore.get(currentId);
if (!event) break;
// Compaction Boundary Optimization
if (event.type === "PromptCompactionEnd") {
const summaryPayload = event.data.summaryText as string;
const startBoundaryId = event.data.compactionStartId as string;
// Inject the compressed semantic summary
context.unshift({ role: "system", content: `Prior History Summary: ${summaryPayload}` });
// Fast-forward pointer directly past the compacted event range
currentId = startBoundaryId;
continue;
}
// Materialize standard operational events
if (event.type === "UserMessage") {
context.unshift({ role: "user", content: event.data.text as string });
} else if (event.type === "ToolExecutionEnd") {
context.unshift({ role: "tool", content: JSON.stringify(event.data.result) });
}
currentId = event.parentId;
}
return context;
}
When an agent's trajectory grows too large, the agent does not freeze to write a summary. Freezing execution ruins user latency.
Instead, the agent appends a PromptCompactionStart event to its trajectory and continues working. In the background, a lightweight auxiliary worker forks a side-trajectory from that exact event, evaluates the older event history, generates a condensed semantic summary, and drops the finished output back into the agent's inbox.
At its next iteration boundary, the main agent admits the completed summary and appends a PromptCompactionEnd event. When the prompt engine walks the tree backwards to assemble the next context window, it encounters the PromptCompactionEnd event and instantly bypasses hundreds of raw intermediate events, replacing them with the compact summary.
Decoupling the event stream solves the reasoning and orchestration layer, but coding agents must ultimately interact with physical computing environments: running compilers, executing test runners, and mutating databases.
If an agent runs on a static virtual machine or inside a simple ephemeral Docker container, two major infrastructure failures occur:
- The Restart Penalty: Every time an agent iteration finishes or a user pauses to review code, tearing down the container means the next edit requires re-running
npm install, restarting dev servers, and re-populating databases. This introduces 30–60 second latency penalties on every turn. - The Inability to Backtrack: If an agent attempts an aggressive database migration that corrupts the local PostgreSQL catalog, the environment cannot be rewound without wiping the entire project.
To overcome this, leading agent platforms have built stateful machine-level snapshot engines (such as Lovable's Camille framework).
┌────────────────────────────────────────────────────────────────────────┐
│ STATEFUL SANDBOX: MEMORY, CPU, AND DISK FORKING │
├────────────────────────────────────────────────────────────────────────┤
│ │
│ BASE SANDBOX (Running Node.js / Vite / PostgreSQL) │
│ [ RAM State: Active Dev Server ] [ Disk: Source Code + .git ] │
│ │ │
│ Snapshot Checkpoint at Turn 4 │
│ │ │
│ ┌────────────────┴────────────────┐ │
│ ▼ ▼ │
│ FORK A: Primary Agent Execution FORK B: Background Exploration │
│ - Tries Tailwind v4 upgrade - Experiments with CSS Modules │
│ - Mutates in-memory process - Mutates isolated disk clone │
│ - Compiles in 120ms (Hot Cache) - Discarded on failure with 0ms │
│ cost to main workspace │
└────────────────────────────────────────────────────────────────────────┘
By utilizing microVM hypervisors (such as Firecracker) with copy-on-write (CoW) memory and block-level storage snapshotting, the agent runtime can fork not just the git repository, but the entire running computer:
- The Linux kernel memory state.
- Active background daemon processes (Postgres, Redis, Next.js dev server).
- CPU register state and local disk blocks.
When an agent needs to test an uncertain refactoring, it forks the computer, runs the mutation inside the clone, and benchmarks the result. If the build breaks or linter errors multiply uncontrollably, the entire machine fork is terminated. The main workspace remains untouched, hot-cached, and fully operational.
A final, often-overlooked challenge of running autonomous agents at scale is inference infrastructure. Frontier coding agents consume billions of tokens daily across multiple upstream providers (Anthropic direct, AWS Bedrock, Google Cloud Vertex AI).
In traditional API services, load balancers distribute requests using round-robin or least-connections algorithms. If Server A is busy, Server B receives the next HTTP request.
In agentic systems, naive round-robin load balancing destroys performance due to prompt cache invalidation.
Frontier LLMs utilize prompt caching to avoid re-computing attention keys and values for repeated prefix tokens. In an agent workflow, the static system instructions, repository file tree, and initial user prompt form a large cached prefix (often 30,000 to 80,000 tokens). Subsequent iterations cost a fraction of the price and execute in milliseconds—provided the request hits the exact same provider and physical GPU cluster hosting the cached prefix.
NAIVE ROUND-ROBIN (Cache-Destroying Anti-Pattern)
Turn 1 ──► Provider A (Anthropic) ──► Computes 60k tokens (Cache Write)
Turn 2 ──► Provider B (Bedrock) ──► CACHE MISS! Recomputes 60k tokens ($$$)
Turn 3 ──► Provider C (Vertex) ──► CACHE MISS! Recomputes 60k tokens ($$$)
PROJECT AFFINITY + DYNAMIC PID RE-WEIGHTING
Project X Assigned Chain: [ Provider A (Primary) -> Provider B (Fallback) ]
Turn 1 ──► Provider A ──► Cache Hit
Turn 2 ──► Provider A ──► Cache Hit
Turn 3 ──► Provider A ──► Cache Hit (Stable for Project Session Window)
To solve this, modern agent platforms employ project-level affinity load balancing with automated PID controllers:
- Permutation Fallback Chains: Instead of a single global routing table, the system generates unique provider fallback permutations (e.g.,
Chain 14: [Anthropic, Bedrock, Vertex]). - Project Stickiness: A given project workspace is cryptographically bound to a specific permutation chain for a multi-minute lease window, guaranteeing that all sequential agent iterations route to the same provider to maximize prompt cache hits.
- PID Controller Capacity Adjustments: To prevent cascading rate-limit failures when a provider experiences sudden regional throttling, a Proportional-Integral-Derivative (PID) controller continuously computes an availability score:
Targeting an error threshold below 0.5%, the controller smoothly shifts subsequent project affinity chains away from degrading providers without jarringly breaking in-flight agent sessions.
The lessons of the past three years have irrevocably shifted the architectural paradigm for AI engineering:
- Constraint Beats Autonomy: Giving an agent infinite freedom to navigate unstructured tasks produces unpredictable failures. Constraining the model to an opinionated stack, strict tool contracts, and verifiable execution loops is what enables production reliability.
- Isolate Arrival from Execution: Never allow external actors to write directly to an active agent's execution thread. Model external interactions as messages into an inbox, admitted only at deterministic iteration boundaries.
- Treat History as Code: Discard unstructured chat arrays. Model your agent runtimes as Git-like event Directed Acyclic Graphs (DAGs) with single-parent pointers, enabling zero-copy forks, reproducible auditing, and lazy context projection.
- Embrace Stateful Sandboxes: Real software requires real execution environments. Pair event-driven agent runtimes with copy-on-write machine snapshots so agents can experiment, fail safely, and recover instantly without restarting from zero.
The frontier of software engineering is no longer about writing smarter prompts. It is about building the distributed systems primitives that give intelligence a dependable foundation to operate upon.
- Osika, A. (2026). How we built Lovable, and why we keep rebuilding it. Lovable Technical Blog. Engineering Disclosures
- Delavari, R. (2026). Inside Chats: How Lovable's Agents Work Together. Lovable Technical Blog. Trajectory Architecture
- Wiman, M. (2026). Routing Billions of Tokens per Minute: LLM Provider Load Balancing with Project Affinity. Lovable Technical Blog. Inference Routing
- Anthropic Engineering. (2024). Model Context Protocol Specification. Protocol Invariants
- Firecracker MicroVM Documentation. (2025). Snapshotting and Fast Restores in Production Container Sandboxes. Firecracker Documentation


