Experiments · JavaScript: From Closures to Autonomous Systems · Chapter 19

A durable agent: log, sign-off, version, key

Durable execution for AI agents

Theme
Try itA durable agent: log, sign-off, version, key
The agent crashes mid-task and restarts. Compare calling the model straight from the code with calling it through the log over forty restarts, crash the process while it waits for a sign-off, set two agents on one account, and repeat a refund with and without an idempotency key.

Everything is computed in your browser; nothing is sent anywhere.

About this experiment

What is durable execution?

Durable execution is a way of running long processes so that they survive a crash of the machine under them. Every step that touches the outside world is recorded in a log. After a restart the process code runs again from the top, recorded steps are not repeated but read back from the log, and the process continues from the first step the log does not yet have. Temporal is the best-known system built this way. It works only if the code between steps does the same thing on every run, so anything nondeterministic, such as the time, a random number or a model’s answer, has to be recorded as a step.

An agent that works for hours or days will see its machine restarted at least once. If its conversation with the model lives in memory, a restart loses it, and the agent either starts over or does something twice. Chapter 19 moves the agent into a durable process after Temporal: the agent loop becomes the process code, and every model call and every tool call becomes a step with its result in the log.

The experiment has four tabs, the four modes of the book’s script. In Replay the “model” picks at random whether to refund or email first, the process crashes after the first action and restarts; switch between calling the model straight from the code and calling it as a logged step, and press “40 restarts” to count divergences live. In Approval the refund waits for a human; crash the process while it waits, then approve. Race sets two agents on one account. Retry crashes the process after the refund but before the log entry. No real model is called; the model is Math.random and the bank is a counter, as in the book.

The book’s numbers, Node 24.16.0: with the model called inline, 18 restarts out of 40 asked for a step other than the one in the log; with the model as a logged step, 0 of 40. The approval run reaches the end after a crash during the wait, and the refund is prepared once. Two agents that read a balance of 100 and each debit 80 leave −60 without a version check and 20 with one. A crash between the action and the log write gives two refunds without an idempotency key and one with it.

Each tab is one of the chapter’s obligations. The model’s randomness cannot be hidden, only recorded. A gate for a dangerous action is a signal in the same log. Agents that think between reading and writing need optimistic locking more than anyone. And because a step runs at least once, not exactly once, every tool that changes the world has to accept an idempotency key.

From the book

JavaScript: From Closures to Autonomous Systems · Chapter 19

The Durable Agent

Next to this experiment in the chapter
There is a place where this scheme breaks. If the programmer changed the process code while processes running the old code are still in flight, a replay with the new code may diverge from the log written by the old one. For this, Temporal’s library has dedicated means for marking the places where the old and new versions behave differently. With an agent the temptation to change the code mid-flight is especially strong: tweak the system prompt, add a tool. Every such edit changes what the model sees, and therefore what it will answer, and a durable agent must either live out its life on the old code or explicitly switch to the new one.