CF Durable Object access contract: a verdict, admit, and the remote wire's own entry
This guide covers the release in which the Cloudflare Durable Object (DO) runtime takes the access contract the other host surfaces take (core AccessVerdict, Authorize, admit):
- every app-facing call carries a verdict: who the request is from (
principal) and the user its runs are for (userId); - the DO's
admithook replacesbeforeEntryand runs on every app-facing entry, submits and/contextincluded; - the remote wire (
/subagent/*) has its own app-facing entry,remoteEntry, and is no longer served through the DO'sfetch; createRemoteAgentWorkerHandlertakes coreauthorizeand forwards its verdict to the DO.
It is a major release of @helix-agents/runtime-cloudflare and @helix-agents/ai-sdk. Nothing is deprecated: an old call fails to compile, and a /subagent/* request through the DO's fetch answers 404.
Runtime page: Calling the DO. The contract itself (the verdict, the operations, admit's defaults) is core's: Access contract.
What changed
| Before | After |
|---|---|
stub.handle(principal, request, trace?) | stub.handle(verdict, request, trace?): verdict is core AccessVerdict ({ principal, userId? }) |
DOClientOptions.principal (DOFrontendExecutor, DOStateStoreClient, DOStreamManagerClient) | access: AccessVerdict |
createCloudflareChatHandler({ principal }), handleChat({ userId }) | createCloudflareChatHandler({ access }); handleChat takes no userId: the run's user is access.userId |
A start body's userId named the run's user | A body userId is a claim only: one that differs from the verdict's user is a 400 validation_error, nothing run |
hooks.beforeEntry(ctx) → { abort: true, response } | hooks.admit(ctx) → { allow: { initialState?, statePatch?, systemContext? } } or a Refusal |
beforeEntry ran on start / resume / retry, including a parent's sub-agent entry | admit runs on every app-facing entry (start, resume, retry, /submit-tool-result, /context), never on the framework's internal wire |
| A state patch and system context pieces reached the run unless your hook refused them | Refused (400 validation_error) unless admit allows them (statePatch: true, systemContext: true) |
/subagent/* through the DO's fetch (the native remote wire) | stub.remoteEntry(verdict, request, trace?) for app code; the framework's own parent calls go through subAgentEntry. Through fetch: 404 |
createRemoteAgentWorkerHandler({ auth, allowUnauthenticated }) (warn-and-serve without auth) | createRemoteAgentWorkerHandler({ authorize }), required (the factory throws without it); authorize: 'public' says on purpose the mount is gated elsewhere |
DOFrontendExecutor.execute(agent, input, { userId }) sent the user in the body | A userId other than access.userId is a typed validation_error, nothing sent; the DO takes the user from the verdict |
Migrating
The verdict on every call
Your app's access control decides one verdict per request: who it is from, and, when the request acts for a user, that user. Build the SDK's clients and the chat handler per request, with that verdict:
// before
const chat = createCloudflareChatHandler({
namespace: env.AGENTS,
principal,
agentName: 'chat-agent',
});
return chat.handleChat({ sessionId, messages, userId: user.id, request });
// after
const chat = createCloudflareChatHandler({
namespace: env.AGENTS,
access: { principal: { id: user.id }, userId: user.id }, // your auth decides it
agentName: 'chat-agent',
});
return chat.handleChat({ sessionId, messages, request });The same holds for new DOFrontendExecutor({ namespace, access, agentName }), new DOStateStoreClient({ namespace, access }) and new DOStreamManagerClient({ namespace, access }). A verdict with no userId (a service, an operator) names no user: its runs have none.
Your own calls into the DO pass the verdict to handle:
// before
await stub.handle(
principal,
new Request('https://do/status', { headers: { 'x-partykit-room': sessionId } })
);
// after
await stub.handle(
{ principal },
new Request('https://do/status', { headers: { 'x-partykit-room': sessionId } })
);A verdict whose principal has no non-empty id, or whose userId is empty, is answered 400 validation_error.
The run's user
The user is fixed when the session is created: it is the verdict's userId of the entry that created it. A start body may still name a userId, as a claim: the same user is accepted, any other is refused (400 validation_error, nothing run). A resume or retry whose verdict names a user other than the session's is refused typed, before anything is admitted or run. Custom endpoints see the verdict's user as ctx.userId, beside ctx.principal.
Session ownership is your app's check
The SDK does not compare a verdict's userId with the session's user on reads (/status, /snapshot, /sse, /messages, /usage, the remote wire's reads), on /submit-tool-result, on the stops or on /context. The one identity rule it keeps is on entries: a resume or retry whose verdict names another user than the session's is refused (400 validation_error, nothing run). Whether a caller may touch a session is your app's decision: check it where you decide the verdict, before calling the DO, or in the worker handler's authorize (it gets the session as ctx.contextId), or in admit for entries, submits and /context (it gets sessionId and userId). A verdict with no userId (a service, an operator) is how an app reaches any session on purpose.
// The worker handler: refuse a session the caller does not own, as if it did not exist.
createRemoteAgentWorkerHandler({
namespace: (env) => env.AGENTS,
authorize: async (request, ctx) => {
const user = await userFromToken(request.headers.get('Authorization'));
if (user === null) return { refuse: 'unauthenticated' };
const owner = ctx.contextId === undefined ? null : await sessionOwner(ctx.contextId);
if (owner !== null && owner !== user.id) return { refuse: 'not_found' }; // answered 404
return { principal: { id: user.id }, userId: user.id };
},
});
// Your own worker, in front of `handle` (the SDK's clients and the chat handler): the same check first.
if ((await sessionOwner(sessionId)) !== user.id) return new Response('Not found', { status: 404 });
const chat = createCloudflareChatHandler({
namespace: env.AGENTS,
access: { principal: { id: user.id }, userId: user.id },
agentName: 'chat-agent',
});sessionOwner is your own lookup (your database, or the user you recorded when the session was created).
beforeEntry → admit
// before
hooks: {
beforeEntry: async (ctx) => {
if (await overQuota(ctx.principal)) {
return { abort: true, response: Response.json({ error: 'quota' }, { status: 429 }) };
}
},
},
// after
hooks: {
admit: async (ctx) => {
if (await overQuota(ctx.principal)) return { refuse: 'forbidden', reason: 'quota' };
return { allow: {} }; // the defaults
},
},ctx is core AdmitContext (origin: 'client', operation, principal, userId, sessionId / contextId, and the caller's keys: initialState, statePatch, context) plus the DO's own fields: drive ('start', 'resume', 'retry', 'submit', 'context'), env, requestOrigin (the request and its route) and, for a drive, identity and the entry as parsed (body, options, or a retry's checkpointId / message); for a submit its toolCallId, for a /context its items.
The answer is { allow: { initialState?, statePatch?, systemContext? } } or a Refusal:
- Defaults (a flag left out, or
{ allow: {} }): the initial state is taken; a state patch is refused; context pieces are user-level only (aplacement: 'system'piece, or one withproviderOptions, is refused, and a piece that names no placement reaches the entry asplacement: 'user', as on agent-server). A refused key answers400 validation_error, nothing written. If your app sends state patches or system context through the DO (e.g.DOFrontendExecutor.resume({ statePatch })), allow them here. /contextitems are not entry keys. The placement default covers an entry's pieces (start, resume, retry), not a/contextitem: the default decision takes a/contextitem as sent,placement: 'system'included. If your app does not take system context there, refuse it inadmit(ctx.drive === 'context', readctx.items).transformRequest'suserIdis a claim. A transformed start body that setsuserIdis refused (400 validation_error, nothing run) unless it is the verdict's user; the run's user is always the verdict's.- A
Refusal({ refuse: 'unauthenticated' | 'forbidden' | 'not_found' | 'invalid', reason?, headers? }) is answered401/403({ error, code: 'UNAUTHORIZED' }, with yourheaders),404(NOT_FOUND) or400(validation_error). Nothing about the request is echoed;reasonis for your own log. A hook no longer builds aResponse. - A throw is
framework_hook_failed, keeping its cause's retryability, as before.
admit now also runs on /submit-tool-result and /context (before anything is read or stored). It never runs on the framework's internal wire (a parent DO's calls to its child): that wire is trusted as internal.
The remote wire: remoteEntry and subAgentEntry
The DO's fetch no longer serves /subagent/*. Two RPC methods do:
subAgentEntry(request)is the framework's internal wire:DOStubTransportand the companion tools call it for every call a parent DO makes to its child (start, resume, the reads, a submit, the stops). Any code holding the DO binding that calls it is trusted as internal (same account, same trust as the binding itself): it is not admitted, and only through it does an entry name its session's parent, root or consult, or carry the parent's user.remoteEntry(verdict, request, trace?)is the remote wire's app-facing entry, for app code reaching a session from outside the framework: the verdict's principal and user,admitruns, no parent, root or consult is read from the request. It serves/subagent/:type/:actiononly.
You call the DO's /subagent/* from | Use |
|---|---|
A parent DO, through the SDK (DOStubTransport, companions) | Nothing to do: the SDK calls subAgentEntry |
| Your own worker, as a remote-agent mount | createRemoteAgentWorkerHandler({ authorize }) (below): it calls remoteEntry |
Your own worker or service binding, directly (stub.fetch('/subagent/…')) | stub.remoteEntry(verdict, new Request(url, init)) |
| Code that plays a parent (a custom transport) | stub.subAgentEntry(new Request(url, init)), if it is trusted as the binding is |
A test's fake namespace whose stubs had only fetch needs a subAgentEntry (forwarding to its fetch): the SDK's child calls refuse a stub without one (framework_not_supported, nothing sent).
createRemoteAgentWorkerHandler
// before
createRemoteAgentWorkerHandler({
namespace: (env) => env.AGENTS,
auth: (request, env) => request.headers.get('Authorization') === `Bearer ${env.API_KEY}`,
});
createRemoteAgentWorkerHandler({ namespace: (env) => env.AGENTS, allowUnauthenticated: true });
// after
createRemoteAgentWorkerHandler({
namespace: (env) => env.AGENTS,
authorize: (request, ctx) =>
request.headers.get('Authorization') === `Bearer ${(ctx.env as Env).API_KEY}`
? { principal: { id: 'api-client' } }
: { refuse: 'unauthenticated' },
});
// a mount behind a private service binding whose callers are framework parents (they pass their run's user):
createRemoteAgentWorkerHandler({
namespace: (env) => env.AGENTS,
authorize: authorizeTrustingClaimedUser,
});authorizeis coreAuthorize, the hook every host surface takes. It runs on every route, after the body is read and size-capped, before the DO is reached and before the agent allowlist (so a refused caller cannot probe which agent types the mount serves).ctx.operationis the route's (start,resume,streamfor/sse,status,snapshot,usage,interrupt,abort,submit-tool-result),ctx.contextIdthe session,ctx.envthe worker's env,ctx.claimed.userIda body'suserId. A throw is answered401and logged.- Its verdict goes to the DO's
remoteEntry: the run's user is the verdict'suserId. authorize: 'public'replacesallowUnauthenticated: true: every request is the fixed public principal, naming no user. A parent's remote start carries its run's user, which a'public'mount refuses (400). A producer reached by framework parents over a private binding uses coreauthorizeTrustingClaimedUser(principaltrusted-caller, the claimed user), or its ownauthorize.- Without
authorizethe factory throws (it used to warn and serve).
DOFrontendExecutor.execute's userId
execute(agent, input, { userId }) is accepted only when userId is the client's access.userId (or absent). Any other is a typed validation_error, and nothing is sent.