19
The Durable Agent
The agent from the previous chapter lived inside one process. While it works for a few seconds, that is enough. But the tasks agents are built for take longer: sort through a hundred customer requests, move data between systems, carry out a refund that requires a human’s signature. Such work goes on for hours and days, and in that time the machine under it will be restarted at least once. If the agent’s whole conversation with the model lives in memory, it will vanish after a restart, and the agent will either start over or do something twice.
In the twelfth chapter we saw how Temporal solves this problem for ordinary processes. The code of a process is written as an asynchronous function, and every step that touches the outside world is made an activity whose result is recorded in a log. After a crash the function is run again, recorded activities are not executed again, and the function proceeds to the first step that is not yet in the log. It works under one condition: the code between activities must behave the same way on every run. An agent violates this condition by its very nature, because a model can answer the same question differently.
The way out of this contradiction is to move the model call into an activity. Then the agent loop from the previous chapter becomes the process code, and every model call and every tool call becomes an activity whose result lies in the log. On replay the function gets from the log the same model answers as the first time and goes down the same road. The model itself is nondeterministic, but its answers are recorded, and everything that happens between them is deterministic.
The experiment in experiments/17-durable-agent shows both versions. The «model» in it chooses at random in what order to issue a refund and send an email; the process crashes after the first action and restarts. If the model is called directly from the process code, on restart it may choose a different order, and in eighteen runs out of forty the code asked for a step other than the one in the log and stopped with a divergence message. If the model is called through an activity, there are no divergences at all. In the twelfth chapter a branch was chosen through Math.random, and Temporal hid that randomness in a sandbox. A model’s randomness cannot be hidden like that; it can only be recorded.
Gratis, solo falta un paso
El resto del libro es gratis
Deja tu correo y te mando un enlace para confirmarlo. Después del clic, todos los capítulos se abren aquí, en este dispositivo, durante un año. Cuando salga un libro nuevo, escribiré a esa dirección; responde “basta” a cualquier correo y no vuelvo a escribir.
¿Tienes cuenta INITE? Entra y el libro se abre sin correo.
No se cobra nada por el libro. Si quieres apoyarlo, puedes donar desde cualquier capítulo.
Cómo se guarda la dirección