Skip to Content
Motoko

Recovering after a restart

The runtime's timer for a sleep lives in its process, and opening an instance is what carries on its unfinished work. After a restart, or a crash, nothing opens an instance until a call comes for it. runtime.recover() opens every instance the host's unfinished() lists, so a run a closed process cut off, a call it left running, and a sleep it closed with carry on without a call:

const runtime = createAgentRuntime({ agents, host, models, ports, closeIdleAfterMs: 60_000 });
const { opened, held, unknown, failed } = await runtime.recover({ concurrency: 4 });

It returns what it did with each listed scope: opened (the open resumed its work), held (another writer holds the scope and carries its work on), unknown (an agent kind this runtime does not have) and failed (with the error). Use it with closeIdleAfterMs: an instance with nothing to do closes again once idle, and one with a sleep due later closes and is reopened at that time, as any idle instance is. Without it, every recovered instance stays open until close().

Run it at startup. Where several processes share a storage, run it again from time to time as well (a lease TTL apart, say): a crashed process's scopes are held until their leases expire, and the next run takes them over. recover() throws a TypeError for a host without unfinished().