Shared state lets one plugin read live data from another without importing its code. A producer plugin declares an accessor, and consumer plugins read it through RuntimeContext.shared. It is a typed, read-only channel, and it is the only sanctioned way for two plugins to share live data. Plugins do not import each other's classes or call each other's services.
When to use it
| Good fit | Bad fit |
|---|---|
A derived value other plugins might want (for example userProfile, lastWeatherQuery, activePageId) | Mutable state with multiple writers |
| Cheap reads that abstract graph-state field names | Large blobs consumers might read but ignore |
Stable shape worth declaring on SharedAccessors for typed reads | Plugin internals other plugins shouldn't see |
If only your plugin reads it, keep it private.
The contract
- Read-only. Consumers never mutate the value. Producers update their own internal state through tool handlers or middleware, and the accessor only exposes a view.
- Flat namespace. Keys live in a single global record. Two plugins registering the same key fail boot, with no quiet overrides.
- Lazy. Each accessor is a function
(state, runCtx) => value. It runs every time a consumer reads. Keep reads cheap, and cache in the producer if derivation is expensive. - Optional. A consumer must assume the producer may not be installed:
rtCtx.shared.someKeycan beundefined. Pair shared state withsoftDependsOnso the soft dependency is explicit in theapp.plugins.status()report.
Typed reads
SharedAccessors is an open interface. Producer plugins extend it through TypeScript declaration merging, so consumers see the right type on rtCtx.shared.someKey. Without merging, the key types as unknown and consumers cast.
Use declaration merging when the shape is stable, and skip it for ad-hoc keys.
Source: SharedAccessors.