Configuring Agent Durability (Effect)
Effect Golem has two related but different controls:
defineAgent({ mode: ... })declares the agent type as durable or ephemeral.- The
Durabilitynamespace lets specialized library code record its own durable function calls
while an agent is running.
Use the declaration mode when the request is to make an agent durable, ephemeral, persistent, or
stateless. A durable agent cannot opt out of oplog writes for part of its execution; there are no
persistence levels in Golem 1.6.
Durable Agents (Default)
Durable agents have a persistent oplog. Golem records durable side effects and recovers the agent
by replaying that oplog after a failure or restart. Omitting mode defaults to "durable", but an
explicit value is often clearest:
import { Effect, Schema } from "effect";
import { defineAgent } from "@golemcloud/effect-golem";
export const Counter = defineAgent({
name: "Counter",
mode: "durable",
id: { name: Schema.String },
methods: {},
}).implement({ init: () => Effect.void, methods: () => ({}) });
Do not try to disable oplog writes while retaining normal durable recovery. If replay becomes slow
because the oplog is long, keep the agent durable and add snapshots.
Durable with Periodic Snapshots
Snapshots keep the durable agent mode but let recovery restore saved state before replaying newer
oplog entries. Define snapshot state with Effect Schema and select a snapshot strategy in the
agent implementation:
import { Duration, Effect, Ref, Schema } from "effect";
import { Snapshot } from "@golemcloud/effect-golem";
const CounterState = Schema.Struct({ count: Schema.Number });
const snapshot = Snapshot.define({
schema: CounterState,
policy: Snapshot.policy.everyN(10),
});
const periodicSnapshot = Snapshot.define({
schema: CounterState,
policy: Snapshot.policy.periodic(Duration.seconds(30)),
});
Set the chosen definition as the agent's top-level snapshotting field and use Snapshot.ref<Saved>()
when the runtime state is a Ref of that schema:
defineAgent({
name: "Counter",
mode: "durable",
id: { name: Schema.String },
snapshotting: snapshot,
methods: {},
}).implement({
init: () => Ref.make({ count: 0 }),
methods: (state) => ({
// Existing method handlers that use state...
}),
snapshot: Snapshot.ref<{ count: number }>(),
});
Keep the saved value schema-serializable. everyN accepts a positive
integer from 1 through 65,535. periodic accepts an Effect Duration.Input such as
Duration.seconds(30).
These policies request snapshots at invocation boundaries or while the agent is idle, not in
the middle of a running handler. periodic does not interrupt a long-running method every thirty
seconds, and everyN does not count arbitrary effects inside that method.
Ephemeral Agents
An ephemeral agent writes oplog history to archive storage. If the executor restarts while preparing
an invocation, Golem can replay the recorded initialization so that the method can still start. Once
the method starts, a failure is terminal; Golem does not replay the method. Use ephemeral mode only
when the work does not require durable method recovery, such as stateless transformations or request
adapters:
export const StatelessHandler = defineAgent({
name: "StatelessHandler",
mode: "ephemeral",
id: { name: Schema.String },
methods: {},
}).implement({ init: () => Effect.void, methods: () => ({}) });
Ephemeral agents are not addressable by agent ID through the Effect SDK's generated client: their
client exposes only upsert, and each invocation runs on a new one-shot instance. Do not depend
on in-memory state surviving failures or restarts.
Switching an Existing Agent
Change only the top-level mode field in the existing defineAgent metadata unless the request
also requires a state redesign.
To switch to ephemeral:
mode: "ephemeral",
To switch back to durable:
mode: "durable",
The values are lowercase TypeScript string literals. Preserve the agent's name, constructor
parameters, methods, and implementation registration when the task only asks for a mode change.
When switching to ephemeral, remove the snapshotting definition and associated implementation
snapshot strategy: ephemeral agents cannot declare snapshotting. Run golem build after editing;
do not edit generated files under golem-temp/.
Custom Durable Functions
Import Durability as a namespace from @golemcloud/effect-golem. It is not an Effect service tag
and must not be yielded as yield* Durability.
Concurrent operations. Durability.wrap and wrapInfallible operations can run in
concurrent fibers (Effect.all, Effect.fork, races) and can nest. Golem records the calls of a
body under the operation that started them, not under a sibling. Obey these limits:
- A parent operation finishes only after every child operation that started in its body closes.
- Work that continues after its operation closes (a detached fiber, a promise that is not awaited)
runs in the nearest open enclosing operation, or else in the agent invocation. Do not let work
continue after the agent method returns.
- Fibers yield through Promise jobs, so
Effect.yieldNowdoes not let host I/O continue. Wait for
I/O with an effect such as Deferred.await, not with a polling loop.
Library code that performs an effect Golem does not already record (for example, a call through a
custom transport) can make it durable with Durability.wrap. On a live run, the body executes and
its schema-encoded result is recorded in the oplog. On replay, the recorded result is returned and
the body does not run again:
import { Schema } from "effect";
import { Durability } from "@golemcloud/effect-golem";
const stockLevel = (sku: string) =>
Durability.wrap(
{
iface: "inventory",
function: "stock-level",
functionType: Durability.FunctionType.readRemote,
requestSchema: Schema.Struct({ sku: Schema.String }),
success: Schema.Number,
},
{ sku },
fetchStockLevel(sku),
);
Pass an error schema to also record typed failures; without it, only successes are recorded.
Defects and interruption leave the resource unfinished. That does not make a deterministic guest
defect retryable: it remains permanent. Eligible recovery may reconstruct incomplete work. Use
Durability.wrapInfallible for bodies that cannot fail. Choose functionType
(readLocal, writeLocal, readRemote, or writeRemote) by whether the operation
reads or writes local or remote state. Nesting is recorded by the host in
Start.parent_start_index, not in the function type.
Golem's built-in host APIs (HTTP, RPC, key-value, blob storage, databases) are already durable;
do not wrap them again.
Choosing a Mode
| Use case | Choice |
| ------------------------------------------------------------------- | ------------------------------------ |
| Counter, shopping cart, workflow, or recoverable external calls | Durable (default) |
| Stateless transformer or adapter with no recovery requirement | Ephemeral |
| Long-running durable agent with a growing oplog | Durable with snapshots |
| Custom library effect that Golem does not record on its own | Durability.wrap |
When in doubt, keep the agent durable. Treat ephemeral mode as an explicit opt-out from durability
guarantees, not as a general performance switch.