One access contract: authorize and admit
This guide covers the release in which every host surface uses one pair of access hooks from @helix-agents/core: authorize decides who a request is from and which user its run is for, and admit decides what of the caller's keys an entry takes. A run's user comes from authorize, never from a request body. A body's userId is only a claim; one that differs from authorize's user is refused.
It is a major release of @helix-agents/agent-server, and a minor release of @helix-agents/core and @helix-agents/a2a (new exports; the A2A host's behaviour is unchanged). The Cloudflare worker handler and Durable Object move to the same hooks in their own release: DO access contract. Nothing is deprecated: the old options are gone.
Reference: Access control and Access contract.
What changed
| Before | After |
|---|---|
authenticate(request, operation) → true | false | { error, status? } | authorize(request, { operation, contextId?, claimed? }) → { principal, userId? } or a refusal |
allowUnauthenticated: true | authorize: 'public' (no user) |
clientState: { state?, statePatch? } or a ClientStateHook | admit(ctx) → { allow: { initialState?, statePatch?, systemContext? } } or a refusal |
the run's user: the body's userId | the run's user: authorize's userId; a differing body userId is a 400 |
/start / /resume refuse entry context | context taken when admit lets it through and the executor takes it |
authorizeis required: a server without it refuses to start.'public'says on purpose that the server does no access control (gated upstream), logs one warning, and names no user.- Refusals:
{ refuse: 'unauthenticated' | 'forbidden' | 'not_found' | 'invalid', reason?, headers? }answer 401, 403, 404 or 400. Thereasonis for your log (agent_server.authenticate.refused, at info): it is never answered. A hook's own error text no longer reaches the client. - A throw answers 401 and logs
agent_server.authenticate.threw, as before. contextIdis the session the route serves: the query'ssessionIdon a GET (/sse,/status,/snapshot,/usage,/workspace), the path's on/chat/{id}/…, the body's otherwise (on/chat, the body'sidwhen it has nosessionId). Bind tokens to sessions with it. A bodysessionIdthat is set but not a non-empty string is the typed 400validation_error, before anything is read.- A body
userIdthat differs fromauthorize's user, or any bodyuserIdwhenauthorizenames none ('public'included), is the typed 400validation_erroron/start,/resumeand/chat, and nothing runs.
Migrating
authenticate → authorize
// before
new AgentServer({
...deps,
authenticate: async (req, operation) => {
const session = await verifyToken(req.headers.authorization);
if (!session) return { error: 'invalid_token', status: 401 };
return true;
},
});
// after
new AgentServer({
...deps,
authorize: async (req, { operation, contextId }) => {
const session = await verifyToken(req.headers.get('authorization'));
if (!session) return { refuse: 'unauthenticated', reason: 'invalid_token' };
if (contextId !== undefined && !session.mayUse(contextId)) return { refuse: 'forbidden' };
return { principal: { id: session.subject }, userId: session.userId };
},
});req is core's HostRequest: { method, path, headers: Headers, body?, signal? }, its parsed body included. Read headers with headers.get(name).
An app that took the user from the body and trusted its caller returns the claim, in one line. Only do this for callers you trust:
authorize: (req, { claimed }) => ({ principal: { id: 'my-frontend' }, userId: claimed?.userId }),allowUnauthenticated: true → authorize: 'public'
new AgentServer({ ...deps, authorize: 'public' });No run on a 'public' server has a user, and a body userId there is refused 400. A caller that sends one needs an authorize that returns it.
clientState → admit
// before: clientState: { statePatch: 'accept' }
admit: () => ({ allow: { statePatch: true } }),
// before: clientState: { state: 'refuse' }
admit: () => ({ allow: { initialState: false } }),
// before: a ClientStateHook that decided per request
admit: ({ principal, userId, operation, keys }) =>
mayWriteState(principal, keys) ? { allow: { statePatch: true } } : { refuse: 'forbidden' },admit allows or refuses a key; it does not rewrite one. A ClientStateHook that filtered fields out of state becomes an admit that refuses the request, or an agent whose stateSchema drops the keys.
Producers of remote sub-agents
A parent runtime's remote sub-agent dispatch calls a producer's /start with the parent session's user as the body's userId (it seeds the child's identity). A producer on authorize: 'public' now refuses that start with 400. A producer that only your own services reach (behind a service binding, a private network, or a gateway that authenticates the caller) takes the claimed user with core's helper:
import { authorizeTrustingClaimedUser } from '@helix-agents/core';
new AgentServer({ ...deps, authorize: authorizeTrustingClaimedUser });It returns { principal: { id: 'trusted-caller' }, userId: <the claimed userId> } for every request: whoever can reach the server picks the user, and with it that user's memory scope. Never put it on a route the public can call. A producer that checks its caller writes its own authorize: verify the caller, then return userId: claimed?.userId.
An agent-server mounted behind the A2A host gets the host's own verdict user in the forwarded body: it is the host's only caller, so the same helper fits there.
Entry context
/start and /resume now take context pieces ({ id, content, placement?, providerOptions? }). By default a piece is user-level: placement: 'system' or provider options are refused (admit: () => ({ allow: { systemContext: true } }) takes them). The pieces reach the entry when the executor takes entry context (runtime-js, the Cloudflare DO client); with another executor the request is the typed framework_not_supported, nothing written.
Session ownership is your app's check
agent-server does not compare a verdict's userId with the user a session was created for, on any route: the reads (/status, /snapshot, /sse, /usage, /workspace), /submit-tool-result, the stops, /context, /resume and the /chat/{id}/… routes. The identity rule it keeps is the body's claim: a body userId that differs from the verdict's is refused (above). Whether a caller may touch a session is your app's decision, as authentication is: check it in authorize, which gets the session as ctx.contextId, or in admit for entries (it gets sessionId and userId), and refuse a session the caller does not own as if it did not exist. A verdict with no userId (a service, an operator) is how an app reaches any session on purpose.
new AgentServer({
...deps,
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 };
},
});sessionOwner is your own lookup (your database, or the user you recorded when the session was created). A new session has no owner yet, so its /start passes; record its owner when you create it.
Types
AuthenticateHook, AuthenticateResult, ClientStateHook, ClientStateContext and ClientStatePolicy are removed. Import Authorize, AccessVerdict, Refusal, AdmitContext and AdmitDecision from @helix-agents/core. AgentServer.runAuthenticate is now runAuthorize.