What we read today, and why
We run a Think subclass (@cloudflare/think 0.17.0, agents 0.22.0) as a per-tenant site manager. Turns arrive from several doors at once: the WebSocket chat, an email channel, alarm-driven proactive turns, and runTurn calls from our own tools. Per-turn state (which channel is speaking, which actor, the trace collector, attachments) has to be keyed by the turn that is actually running, because a channel body can be waiting on the queue while a WebSocket chat turn runs ahead of it.
The one identity every hook and tool call inside a turn shares is the request id the TurnQueue is running. Today we reach it, and two neighbours, through private fields:
this._turnQueue.activeRequestId. Think declares private _turnQueue; the TurnQueue class from agents/chat exposes activeRequestId publicly (set before the admitted body, cleared in its finally), but the queue instance itself is private.
this._turnQueue.enqueue(name, fn), to serialise our own post-publish work behind the chat turn (the detached-delivery pattern, inside keepAliveWhile).
this._cf_sealMemoryLimitedRecovery() (protected), to seal a still-live recovery incident when our own alarm strike counter trips.
A fourth value we cannot reach at all: the admitted turn's trigger (ws-chat, rpc, programmatic, agent-tool, auto-continuation, ...). It lives in the module-private admittedTurnContext AsyncLocalStorage in think.js and is not exposed on TurnContext, on activeTurnMetadata (empty on dashboard WebSocket turns), or on any hook argument. We need it to tell "the chat door's turn is running" from "our channel body's turn is running" when the two race.
We have wrapped all of these in one adapter module with a version pin that fails our typecheck on a bump, so nothing here is urgent for us. It would be better for everyone if the reads were public.
Ask
A public, documented accessor on Think (or Agent) for the admitted turn, something like:
get activeTurn():
| { requestId: string; trigger: TurnTrigger; admission: "submit" | "wait"; channel?: string; continuation: boolean }
| undefined;
readable from beforeTurn, prepareStep, tool execute and onChatResponse, and undefined outside any turn. Exporting TurnTrigger from @cloudflare/think would help too. A public way to enqueue work behind the current turn (enqueueTurn(name, fn) or similar) would remove the second private read.
Happy to open a PR if the shape is agreed.
What we read today, and why
We run a
Thinksubclass (@cloudflare/think0.17.0,agents0.22.0) as a per-tenant site manager. Turns arrive from several doors at once: the WebSocket chat, an email channel, alarm-driven proactive turns, andrunTurncalls from our own tools. Per-turn state (which channel is speaking, which actor, the trace collector, attachments) has to be keyed by the turn that is actually running, because a channel body can be waiting on the queue while a WebSocket chat turn runs ahead of it.The one identity every hook and tool call inside a turn shares is the request id the
TurnQueueis running. Today we reach it, and two neighbours, through private fields:this._turnQueue.activeRequestId.Thinkdeclaresprivate _turnQueue; theTurnQueueclass fromagents/chatexposesactiveRequestIdpublicly (set before the admitted body, cleared in itsfinally), but the queue instance itself is private.this._turnQueue.enqueue(name, fn), to serialise our own post-publish work behind the chat turn (the detached-delivery pattern, insidekeepAliveWhile).this._cf_sealMemoryLimitedRecovery()(protected), to seal a still-live recovery incident when our own alarm strike counter trips.A fourth value we cannot reach at all: the admitted turn's
trigger(ws-chat,rpc,programmatic,agent-tool,auto-continuation, ...). It lives in the module-privateadmittedTurnContextAsyncLocalStorageinthink.jsand is not exposed onTurnContext, onactiveTurnMetadata(empty on dashboard WebSocket turns), or on any hook argument. We need it to tell "the chat door's turn is running" from "our channel body's turn is running" when the two race.We have wrapped all of these in one adapter module with a version pin that fails our typecheck on a bump, so nothing here is urgent for us. It would be better for everyone if the reads were public.
Ask
A public, documented accessor on
Think(orAgent) for the admitted turn, something like:readable from
beforeTurn,prepareStep, toolexecuteandonChatResponse, andundefinedoutside any turn. ExportingTurnTriggerfrom@cloudflare/thinkwould help too. A public way to enqueue work behind the current turn (enqueueTurn(name, fn)or similar) would remove the second private read.Happy to open a PR if the shape is agreed.