Validate Cloudflare Agent state and sync it to clients
Check who may change durable Agent state, where validation runs, and how accepted changes reach connected clients.
Prerequisites and input · Engineering checks · 1. List state fields and mark who writes them
At a glance
A Cloudflare Agent's state is a single value per Agent. It is stored in SQLite, survives restarts and hibernation, and syncs to every connected client. Clients can also submit changes through useAgent, so before release you need to confirm which fields clients may change, which only the server may change, and whether the sync path can trigger itself. This article turns that review into a checklist you can fill in item by item.
Runtime and applicability
- Article type
- Engineering practice
- Verified
- Applies to
- Cloudflare Agents SDK Validate Cloudflare Agent state and sync it to clients
Execution locations and responsibilities
- Cloudflare Agent server and connected client Use the workflow below to distinguish responsibilities of the caller, runtime, and external actions.
Arrows show the order of checks in the guide. Follow each step’s prerequisites, branches and stop conditions; unexecuted steps are not completed work.
When to use
- State is one JSON-serializable value.
initialStateapplies only the first time the Agent reads its state; after thatthis.statereturns the stored value. First decide field ownership: which fields only the server may change and which fields clients may change.
When not to use
validateStateChangeis synchronous, so permissions that need an asynchronous lookup cannot be decided inside it and need a separate design.- This article covers state writes, validation and sync only; user authentication and instance authorization belong to the routing layer.
- The exact way to configure a readonly connection is outside the supplied material, so no configuration steps are given.
Prerequisites and input
- An Agent subclass whose
initialState,validateStateChangeandonStateChangedyou can read in their current form. - Access to the client code that connects to the Agent, especially wherever it calls
setState.
Engineering checks
State shape and ownership — State is one JSON-serializable value. initialState applies only the first time the Agent reads its state; after that this.state returns the stored value. First decide field ownership: which fields only the server may change and which fields clients may change.
Server-side validation before storage — validateStateChange(next, source) runs synchronously before a change is stored, and throwing rejects it. source is "server" or the client's Connection, so the server can apply different rules by origin. Validation responsibility sits on the server; a client-side check does not replace it.
Broadcast and side effects after storage — onStateChanged(state, source) runs after a change is stored and broadcast. If you call setState inside it, ignore changes whose source is "server", or the call loops. Data that does not need to reach clients belongs in SQLite through this.sql, and values a client should only read call for a readonly connection.
1. List state fields and mark who writes them
Where: the initialState definition in the Agent class. Input: every field of the current state. Action: mark each field as server-only or client-writable, and confirm the whole value is JSON-serializable. Completion check: every field has a recorded owner. Stop condition: stop and redesign if a value cannot be serialized or a field's owner is unclear.
2. Check the rejection rules in validateStateChange
Where: validateStateChange. Input: the ownership table from the previous step. Action: confirm that any change from a non-"server" source touching a server-only field throws, and remember the method is a synchronous check, so it is not a place for logic that must wait. Completion check: each server-only field maps to a rejection rule. Stop condition: do not continue if any server-only field has no rule.
3. Inspect write-backs in onStateChanged
Where: onStateChanged. Input: every setState call inside it. Action: confirm each write-back checks the source first and skips "server" changes so the method does not trigger itself. Completion check: every write-back has a source test. Stop condition: an unconditional write-back counts as a loop risk and must be fixed first.
4. Review client subscription and submission
Where: front-end components that use useAgent. Input: the agent name, instance name and the agent.state the component reads. Action: confirm the client relies on synced state, that client changes pass server validation first, and that the UI does not assume success when a change is rejected. Completion check: you can state what the UI shows for each rejected write. Stop condition: stop if the UI still shows success after a rejection.
5. Separate unsynced data and readonly connections
Where: the Agent's storage design. Input: data clients should never see, and data clients should only read. Action: put the former in SQLite through this.sql and record the latter as needing a readonly connection. Completion check: no internal or sensitive data remains in broadcast state. Stop condition: the supplied material does not explain how to create a readonly connection, so consult the readonly connections documentation rather than guessing a configuration.
6. Confirm Workflow writes to state
Where: Workflow steps that touch the Agent's state. Input: each state write in the Workflow. Action: check whether it uses step.updateAgentState, step.mergeAgentState or step.resetAgentState, understanding that the first replaces state, the second merges fields, and the third resets to initialState. Completion check: each write matches its intended meaning. Stop condition: if a replace drops fields by mistake, switch to a merge and review again.
Task example
This is a synthetic manual walkthrough; every observation is an expectation, not an actual verification. Imagine a multiplayer room Agent whose state holds a players list and a status. First mark players as client-appendable and status as server-only. Then read validateStateChange; you would expect a rule that throws when the source is not the server and status changes. Read onStateChanged; you would expect the server to set status once two players join, with the "server" source excluded before the write-back. Finally read the front end; you would expect a client attempt to change status to be rejected and the UI to keep the old value. Each of these must still be checked against real code.
Completion checks
- Every state field has a server or client write owner, and a matching rule can be found in
validateStateChange. - Every
setStatewrite-back inonStateChangedexcludes the "server" source first. - Data that should not be broadcast has moved to
this.sql, and the client's rejection path has a defined UI behavior.
Verification scope
This article is based on reading the supplied official documentation, and relies only on its description of state, validateStateChange, onStateChanged and the Workflow state methods. Installation, login, API execution and output were not tested, and the observations in the example are not verified.
Sources
All links are official pages; verification dates are listed per source, and links point to the version checked.