Skip to content
Esc

    Search bindings, commands, errors and guides.

    OpenOrange extensions / Fork Durable Objects

    OpenOrange extensions

    Fork Durable Objects

    Copy a Durable Object, with all of its storage, into a new object that runs on its own.

    A fork copies a Durable Object into a new object. The copy starts with everything the original had stored, then the two run separately: a write to one never shows up in the other. Use forks to branch an agent’s conversation, to try something risky on a copy of real data, or to duplicate a document.

    Forking is an OpenOrange extension. Cloudflare has no fork API, so code that forks runs on OpenOrange only.

    Quickstart

    1. Fork an object

      Get a stub for the object to copy, then pass its ID to orange.fork() with a name for the new object. orange.fork() returns a stub for the new object:

      const baseStub = env.COUNTERS.getByName("bob");
      const newStub = await env.COUNTERS.orange.fork(baseStub.id, { name: "bob-copy" });

      baseStub still reaches the original, and env.COUNTERS.getByName("bob-copy") now reaches the new object.

    2. Run it

      Run the counters Worker with orange dev. Its routes are POST /counters/<name>/increment, GET /counters/<name>, and POST /counters/<name>/fork?to=<newName>, which runs the fork above. Increment bob twice:

      curl -s -X POST http://127.0.0.1:8787/counters/bob/increment
      {"name":"bob","count":1}
      curl -s -X POST http://127.0.0.1:8787/counters/bob/increment
      {"name":"bob","count":2}

      Fork bob into bob-copy. The new counter starts with bob’s count:

      curl -s -X POST http://127.0.0.1:8787/counters/bob/fork?to=bob-copy
      {"name":"bob-copy","count":2}

      Increment the new counter. It counts on from its own total:

      curl -s -X POST http://127.0.0.1:8787/counters/bob-copy/increment
      {"name":"bob-copy","count":3}

      Read bob again. The new counter’s increment never reached it, so it is still 2:

      curl -s http://127.0.0.1:8787/counters/bob
      {"name":"bob","count":2}

    Set up the copy

    To give the copy something to start with, pass input. Add an onOrangeFork() method to your class: it runs once in the copy, after the constructor and before the copy handles any call.

    export class Agent extends DurableObject<Env> {
    	// Forks this agent and returns the new agent's ID.
    	async branch(label: string): Promise<string> {
    		const newStub = await this.env.AGENT.orange.fork(this.ctx.id, { input: { label } });
    		return newStub.id.toString();
    	}
    
    	onOrangeFork(fork: OrangeForkInfo<{ label: string }>) {
    		// The hook runs again if it throws, so it replaces the row instead of adding one.
    		this.ctx.storage.sql.exec("CREATE TABLE IF NOT EXISTS branch (id INTEGER PRIMARY KEY CHECK (id = 1), label TEXT)");
    		this.ctx.storage.sql.exec("INSERT OR REPLACE INTO branch (id, label) VALUES (1, ?)", fork.input?.label ?? null);
    	}
    }

    An object can fork itself, as branch() does. The copy includes the writes that the object made earlier in the same call.

    Find the original

    this.ctx.orange.forkChain lists the objects this one was forked from, oldest first. It is empty for an object that was never forked.

    const original = this.ctx.orange.forkChain.at(-1)?.id; // the object this one was copied from

    Add the TypeScript types

    orange types generates worker-configuration.d.ts like wrangler types does, and adds the types for orange.fork(), onOrangeFork(), and ctx.orange:

    orange types
    AI context API reference, what gets copied, errors, and limits

    Written for LLMs and coding agents: every edge case, measurement, and known deviation. You do not need it to use the feature.

    The full counters Worker

    The Worker that the quickstart runs. Each counter is a Durable Object that keeps its count in SQLite.

    {
    	"name": "counters",
    	"main": "src/index.ts",
    	"compatibility_date": "2026-09-03",
    	"durable_objects": {
    		"bindings": [{ "name": "COUNTERS", "class_name": "Counter" }]
    	},
    	"migrations": [{ "tag": "v1", "new_sqlite_classes": ["Counter"] }]
    }
    import { DurableObject } from "cloudflare:workers";
    
    interface Env {
    	COUNTERS: DurableObjectNamespace<Counter>;
    }
    
    export class Counter extends DurableObject<Env> {
    	constructor(ctx: DurableObjectState, env: Env) {
    		super(ctx, env);
    		ctx.storage.sql.exec(
    			"CREATE TABLE IF NOT EXISTS counter (id INTEGER PRIMARY KEY CHECK (id = 1), count INTEGER NOT NULL)",
    		);
    	}
    
    	increment(): number {
    		return this.ctx.storage.sql
    			.exec<{ count: number }>(
    				"INSERT INTO counter (id, count) VALUES (1, 1) ON CONFLICT (id) DO UPDATE SET count = count + 1 RETURNING count",
    			)
    			.one().count;
    	}
    
    	get(): number {
    		return this.ctx.storage.sql.exec<{ count: number }>("SELECT count FROM counter").toArray()[0]?.count ?? 0;
    	}
    }
    
    export default {
    	async fetch(request, env): Promise<Response> {
    		const url = new URL(request.url);
    		const [collection, name, action] = url.pathname.split("/").slice(1);
    		if (collection !== "counters" || !name) return new Response("Not found", { status: 404 });
    		const counter = env.COUNTERS.getByName(name);
    
    		// GET /counters/<name> returns the count.
    		if (request.method === "GET" && action === undefined) {
    			return Response.json({ name, count: await counter.get() });
    		}
    
    		// POST /counters/<name>/increment adds one and returns the new count.
    		if (request.method === "POST" && action === "increment") {
    			return Response.json({ name, count: await counter.increment() });
    		}
    
    		// POST /counters/<name>/fork?to=<newName> copies the counter into a new counter named <newName>.
    		if (request.method === "POST" && action === "fork") {
    			const newName = url.searchParams.get("to") ?? `${name}-copy`;
    			const baseStub = env.COUNTERS.getByName(name);
    			const newStub = await env.COUNTERS.orange.fork(baseStub.id, { name: newName });
    			return Response.json({ name: newName, count: await newStub.get() });
    		}
    
    		return new Response("Not found", { status: 404 });
    	},
    } satisfies ExportedHandler<Env>;

    API

    APIDescription
    env.MY_DO.orange.fork(source, options?)Copies the object with the ID source into a new object, and returns a stub for the copy once it exists. Works on any Durable Object namespace of your Worker, including ctx.exports and a binding with script_name.
    options.nameThe copy’s name. getByName(name) reaches the copy, and the copy’s ctx.id.name is name.
    options.idThe copy’s ID, such as one from newUniqueId(). Pass name or id, not both. With neither, the copy gets a new unique ID and no name.
    options.inputAny structured-clone value. The copy’s onOrangeFork() receives it.
    onOrangeFork(fork)An optional method on your class. It runs once in the copy, after the constructor, and calls to the copy wait for it. If it throws, it runs again before the next call, so make it safe to run twice.
    fork.id, fork.nameThe original’s ID and name.
    fork.forkedAtWhen the fork was made, in milliseconds since the epoch.
    fork.inputThe input passed to fork(), or undefined.
    ctx.orange.forkChainEvery object this one was forked from, oldest first, each with id, name, and forkedAt. ctx.orange is undefined in a facet.

    The API mirrors Rivet’s actor forks:

    RivetOpenOrange
    client.doc.fork(actorId, { key, input })env.DOC.orange.fork(id, { name, input })
    onFork(c, fork)onOrangeFork(fork)
    fork.actorId, fork.keyfork.id, fork.name
    c.forkChainctx.orange.forkChain

    What gets copied

    StateCopied
    SQL and key-value storageYes, everything the original had stored when the fork started
    Writes made earlier in the call that forksYes. A fork waits for them, as any outgoing call from an object does.
    FacetsYes, as they were when the fork started. Calls from the original to its facets wait until every facet is copied. A facet that writes on its own while the fork runs, such as from a call it started earlier, can carry those writes into the copy.
    A pending alarmNo. The original keeps it.
    Class fields and other in-memory stateNo. The copy runs its constructor.
    WebSocketsNo. Clients stay connected to the original.

    The copy has its own ctx.id and ctx.id.name.

    Errors

    fork() throws an Error whose name is OrangeForkError. Its code is one of:

    codeCause
    duplicate_keyAn object with the copy’s ID already exists. Forking is not idempotent, so a retry with the same name fails this way instead of making a second copy.
    not_found, fork_failedThe original has never run, so it has nothing to copy.
    fork_pendingThe fork took longer than 30 seconds. The copy appears once it finishes.
    fork_too_deepThe original is already 16 forks deep.
    fork_unavailableThe Rivet Engine cannot fork. Forks need Rivet Engine 3.1 or later.

    Limits

    LimitValue
    Forks of forks16 deep
    Time for one fork30 seconds, then fork_pending

    How it works

    Each Durable Object is a Rivet Actor, so a fork is a Rivet actor fork. The copy shares the original’s storage and stores only what it writes afterwards, so forking a large object costs the same as forking a small one. The fork runs inside the original object, which wakes it if it was asleep: it commits the original’s writes, forks the original’s actor, then forks each of its facets under the copy, as ctx.facets.clone() does. Before the copy runs, Orange gives it its own name, records the fork in forkChain, and drops the alarm. The Rivet Engine sometimes asks to retry a fork while its storage is busy; Orange retries up to 8 times, waiting from 100 ms and doubling.

    Conformance

    Cloudflare has no fork API, so there is no Cloudflare behavior to compare with. The end-to-end tests in tests/durable-objects/durable-object-orange-fork.test.ts cover the copy and its independence, forks of forks, unnamed copies, an object that forks itself, alarms, facets, errors, and a restart.