Skip to content

CF Durable Object app-facing entry: handle(principal, request, trace) ​

This guide covers the release in which every app-facing call into a Cloudflare Durable Object (DO) built on DurableObjectAgentBase goes through the DO's new handle RPC method, carrying a principal: who the request is from, your app's own verdict. The DO's fetch now serves only its internal wire.

It is a major release of @helix-agents/runtime-cloudflare and @helix-agents/ai-sdk, and a minor release of @helix-agents/core (new exports only: Principal, principalSchema). Nothing is deprecated: the old path answers 404.

Design: the A2A design's §7a (2026-11-13-remote-agents-a2a-design.md). Runtime page: Calling the DO.

What changed ​

  • DurableObjectAgentBase.handle(principal, request, trace?) is the DO's app-facing entry: entries (/start, /resume, /retry), reads (/status, /snapshot, /messages, /sse, /usage, …), stops (/interrupt, /abort), /submit-tool-result, /context, the chat routes and your config's own endpoints.
  • The DO's fetch serves only the DO-internal wire: /subagent/* (sub-agent calls between DOs, the native remote wire), a companion's settle notice and partyserver's set-name. Any app route through fetch answers 404 Not found, and nothing is read, written or run.
  • principal (core Principal, { id: string; …claims }) is required. One without a non-empty id is answered 400 validation_error. The request must carry x-partykit-room naming the instance: its id's name (doRoomOf(id, sessionId), the session id for idFromName(sessionId)). trace is your W3C trace context (traceparent, tracestate).
  • The DO hands the principal, read-only, to beforeEntry and to your custom endpoints as ctx.principal, for your own access checks on its routes. It comes only from handle's argument, never from a request header or body.
  • DOFrontendExecutor, DOStateStoreClient, DOStreamManagerClient and createCloudflareChatHandler take a required principal and call handle. Build them per request, with that request's principal.

Migrating ​

handle is a Durable Object RPC method: the Worker needs a compatibility_date of 2024-04-03 or later, or the rpc compatibility flag.

The SDK's clients and the chat handler ​

Add principal, and build per request:

typescript
// before
const chat = createCloudflareChatHandler({ namespace: env.AGENTS, agentName: 'chat-agent' });

// after: per request
const chat = createCloudflareChatHandler({
  namespace: env.AGENTS,
  principal: await authenticate(request, env), // your auth decides it
  agentName: 'chat-agent',
});

The same holds for new DOFrontendExecutor({ namespace, principal, agentName }), new DOStateStoreClient({ namespace, principal }) and new DOStreamManagerClient({ namespace, principal }).

handle refuses a room (x-partykit-room) that does not name the instance it reached (400 validation_error), before anything runs. The room is checked against the instance's id name (the name idFromName made the id from), so a wrong first room is refused too; an id with no name (newUniqueId, idFromString) is named by its first room, and any other room is refused after it. A failure while the instance initializes is not a room refusal: it propagates from handle, as it did from fetch. A client's room is the name its namespace's id was made from (namespace.idFromName(name)), else the session id (doRoomOf, exported from @helix-agents/runtime-cloudflare/do-clients). So an app that addresses its Durable Objects by a name other than the session id (a prefix, a tenant) passes that mapping namespace to the clients and the chat handler, and its own calls send the same name as the room:

typescript
const sessions = new Proxy(env.AGENTS, {
  get(target, prop) {
    if (prop === 'idFromName') return (id: string) => target.idFromName(`session:${id}`);
    const value = Reflect.get(target, prop);
    return typeof value === 'function' ? value.bind(target) : value;
  },
});
const chat = createCloudflareChatHandler({
  namespace: sessions,
  principal,
  agentName: 'chat-agent',
});

Your own calls into the DO ​

Every stub.fetch(...) to an app route, your own endpoints included, becomes stub.handle(...):

typescript
// before: the Worker reached it through the DO's fetch
await env.AGENTS.get(id).fetch('https://do/my-endpoint', {
  headers: { 'x-partykit-room': sessionId },
});

// after: through handle, with the request's principal
await env.AGENTS.get(id).handle(
  principal,
  new Request('https://do/my-endpoint', { headers: { 'x-partykit-room': sessionId } })
);

A Worker that forwards a whole request sets the room header on a copy:

typescript
const headers = new Headers(request.headers);
headers.set('x-partykit-room', sessionId);
return stub.handle(principal, new Request(request, { headers }));

Leave DO-to-DO calls to /subagent/* on fetch: they are the internal wire, and the SDK makes them itself.

A client you wrote yourself ​

A client of your own that calls the DO with stub.fetch and an x-partykit-room header (one modeled on DOFrontendExecutor, say) gets 404 from every app route now: the DO's fetch serves only the DO-internal wire. Move each app route (an entry, a read, a stop, a submit, /context, /sse, your own endpoints) to stub.handle(principal, request, trace?), with the request's principal. The room is still the header, and it must name the instance the stub reached: set it with doRoomOf(id, sessionId) from the id you made the stub from.

typescript
import { doRoomOf } from '@helix-agents/runtime-cloudflare/do-clients';

// before
const id = env.AGENTS.idFromName(sessionId);
const res = await env.AGENTS.get(id).fetch('https://do/status', {
  headers: { 'x-partykit-room': sessionId },
});

// after
const id = env.AGENTS.idFromName(sessionId);
const res = await env.AGENTS.get(id).handle(
  principal,
  new Request('https://do/status', { headers: { 'x-partykit-room': doRoomOf(id, sessionId) } }),
  trace // optional: the caller's W3C trace context (traceparent, tracestate)
);

Read the answer's body at once, as the SDK's clients do. Leave calls to /subagent/* on fetch: they are the internal wire. Where the SDK's clients cover a call (DOFrontendExecutor, DOStateStoreClient, DOStreamManagerClient), using them instead keeps the principal, the room, the trace and an /sse reader's close handled for you.

Your hooks and endpoints ​

beforeEntry's context and EndpointContext gain principal. An app that checked access from a request header can check ctx.principal instead:

typescript
hooks: {
  beforeEntry: async (ctx) => {
    // A parent DO's sub-agent entry (the SDK's own internal entry) carries no principal.
    if (ctx.origin.internal) return;
    if (ctx.principal === undefined || !(await mayRun(ctx.principal, ctx.identity))) {
      return { abort: true, response: new Response('Forbidden', { status: 403 }) };
    }
  },
},
endpoints: {
  '/whoami': async (_request, ctx) => Response.json({ id: ctx.principal.id }),
},

Branch on ctx.origin.internal, never on whether ctx.principal is present: an entry through handle carries the principal, and a parent DO's sub-agent entry (origin.internal: true) carries none, but so does an entry over the native remote wire (/subagent/* through fetch: createRemoteAgentWorkerHandler, or any Worker holding the binding; origin.internal: false, origin.route: 'subagent'). A check that skips every entry without a principal admits all of those. The example above refuses them; a Worker that serves the remote wire checks those entries its own way (the worker handler's auth, or ctx.origin.request).

The DO's WebSocket stream is removed ​

A Durable Object RPC call cannot carry a WebSocket, so the DO serves none: handle answers an upgrade 501 framework_not_supported, and the DO's fetch refuses it. Read the stream over /sse (through handle), resuming from the last frame's id: with ?fromSequence=. The SDK's own readers already do.

Deleted with it: the onConnect history replay and the ping / pong messages, DOStreamManagerOptions' getConnections and broadcast (drop them from a manual new DOStreamManager({ … })), and the ConnectionState type.

Reading /sse yourself through handle ​

An RPC call carries neither the request's abort signal nor the cancel of the answered body, so the DO does not see a reader leave. It evicts it only when its writes to that reader fail: the second write after it left, so within about 60 s when only the 30 s heartbeat is written. The SDK's clients free it at once instead; give them the Worker's ctx.waitUntil (waitUntil on the clients and on createCloudflareChatHandler) so a close sent as the Worker's own client leaves is not cut off with its request. A Worker that reads /sse through handle itself sends x-helix-sse-reader: <id> (at least 128 random bits) with the read, and POST /sse/close { readerId } through handle, under ctx.waitUntil, when it stops (see the /stream/ route in Calling the DO). A reader that holds the stream but never reads (a hung Worker) is evicted once no write was read for sseInactivityTimeoutMs (default 60 000, a new DurableObjectAgentConfig option), at the next SSE heartbeat (every 30 s). Don't put an abort signal on the Request you pass to handle: an RPC argument cannot carry one.

Tests ​

A test that drives a DO through Miniflare calls handle from code running in workerd: a small test Worker, bundled next to your DO, forwards to the stub's handle, and the test calls the Worker with mf.dispatchFetch. Don't call handle on Miniflare's Node-side namespace stub: its RPC proxy can leave unhandled rejections behind a streamed answer and race Miniflare's check that every answer's body was read, so a run whose tests all pass can still exit 1. Read each answer's body at once.

typescript
// test-worker.ts: GET /do/<path>?sid=<session id> → the session DO's handle
export default {
  async fetch(request: Request, env: Env): Promise<Response> {
    const url = new URL(request.url);
    const sid = url.searchParams.get('sid')!;
    const headers = new Headers(request.headers);
    headers.set('x-partykit-room', sid);
    const forwarded = new Request(`http://do${url.pathname.slice('/do'.length)}`, {
      method: request.method,
      headers,
      body: request.body,
    });
    return env.AGENTS.get(env.AGENTS.idFromName(sid)).handle({ id: 'test-user' }, forwarded);
  },
};

// the test
const res = await mf.dispatchFetch(`http://worker/do/status?sid=${sid}`);
expect(res.status).toBe(200);
const status = await res.json();

Released under the MIT License.