Class MongoAgentStateStore
- All Implemented Interfaces:
io.agentscope.core.state.AgentStateStore,AutoCloseable
AgentStateStore.
Each session is stored as a single MongoDB document with a compound _id of
{user, session} to prevent cross-tenant key collisions. State keys are stored inside
a states sub-document (via dot notation, e.g. states.foo), keeping them
isolated from reserved top-level fields such as _id, user_id,
session_id, and _updated_at. Per-key versions live in a versions
sub-document and list-content hashes in a hashes sub-document.
Supports optimistic concurrency via the versions.<key> field. List state uses
ListHashUtil for change detection to avoid unnecessary full rewrites. The list
element count is obtained server-side via $size aggregation to avoid transferring
the full array just to call .size().
Usage:
MongoAgentStateStore store = MongoAgentStateStore.builder()
.connectionString("mongodb://localhost:27017")
.databaseName("agentscope")
.collectionName("sessions")
.build();
Key constraints
- State key character set: keys passed to
save(java.lang.String, java.lang.String, java.lang.String, io.agentscope.core.state.State)/get(java.lang.String, java.lang.String, java.lang.String, java.lang.Class<T>)must match^[a-zA-Z_][a-zA-Z0-9_]*$— no dots, dollar signs, or Unicode. Values insideMap<String, Object>fields are automatically escaped byMongoKeyEscaperand have no character restrictions. - Snapshots use GridFS: sandbox workspace snapshots are stored in GridFS (up to 16 GB per file), not as single BSON documents. The legacy 16 MB BSON limit no longer applies.
- Session TTL is off by default: sessions are retained indefinitely, aligned with
Postgres/JDBC/Redis. To enable automatic expiry, pass
MongoAgentStateStore.builder().ttlDays(n)with a positive integer. The TTL index is sparse — only documents with a non-null_updated_atfield expire.
-
Nested Class Summary
Nested Classes -
Field Summary
Fields inherited from interface io.agentscope.core.state.AgentStateStore
UNVERSIONED -
Method Summary
Modifier and TypeMethodDescriptionstatic MongoAgentStateStore.Builderbuilder()voidclose()voidvoidboolean<T extends io.agentscope.core.state.State>
Optional<T><T extends io.agentscope.core.state.State>
List<T><T extends io.agentscope.core.state.State>
io.agentscope.core.state.VersionedState<T>getVersioned(String userId, String sessionId, String key, Class<T> type) listSessionIds(String userId) voidvoidsave(String userId, String sessionId, String key, List<? extends io.agentscope.core.state.State> values) Saves a list of state values with incremental-append optimization.longsaveIfVersion(String userId, String sessionId, String key, io.agentscope.core.state.State value, long expectedVersion) boolean
-
Method Details
-
builder
-
supportsVersioning
public boolean supportsVersioning()- Specified by:
supportsVersioningin interfaceio.agentscope.core.state.AgentStateStore
-
save
- Specified by:
savein interfaceio.agentscope.core.state.AgentStateStore
-
get
public <T extends io.agentscope.core.state.State> Optional<T> get(String userId, String sessionId, String key, Class<T> type) - Specified by:
getin interfaceio.agentscope.core.state.AgentStateStore
-
save
public void save(String userId, String sessionId, String key, List<? extends io.agentscope.core.state.State> values) Saves a list of state values with incremental-append optimization.The list element count is obtained server-side via
$sizeaggregation so only a single integer is transferred, not the full array. This avoids negating the network savings of incremental append for large conversation histories.Concurrency note: this method performs a read-then-write to decide between incremental append and full replacement, so it is intentionally NOT atomic: concurrent writers for the same
(userId, sessionId, key)may interleave reads and writes, causing duplicated or lost appends. This matches the harness execution model, where aSandboxExecutionGuardserialises calls per isolation slot, so each session normally has at most one writer at a time. Callers that write the same list concurrently from outside the harness must synchronise externally.- Specified by:
savein interfaceio.agentscope.core.state.AgentStateStore
-
getList
public <T extends io.agentscope.core.state.State> List<T> getList(String userId, String sessionId, String key, Class<T> itemType) - Specified by:
getListin interfaceio.agentscope.core.state.AgentStateStore
-
getVersioned
public <T extends io.agentscope.core.state.State> io.agentscope.core.state.VersionedState<T> getVersioned(String userId, String sessionId, String key, Class<T> type) - Specified by:
getVersionedin interfaceio.agentscope.core.state.AgentStateStore
-
saveIfVersion
public long saveIfVersion(String userId, String sessionId, String key, io.agentscope.core.state.State value, long expectedVersion) - Specified by:
saveIfVersionin interfaceio.agentscope.core.state.AgentStateStore
-
exists
- Specified by:
existsin interfaceio.agentscope.core.state.AgentStateStore
-
delete
- Specified by:
deletein interfaceio.agentscope.core.state.AgentStateStore
-
delete
- Specified by:
deletein interfaceio.agentscope.core.state.AgentStateStore
-
listSessionIds
- Specified by:
listSessionIdsin interfaceio.agentscope.core.state.AgentStateStore
-
close
public void close()- Specified by:
closein interfaceio.agentscope.core.state.AgentStateStore- Specified by:
closein interfaceAutoCloseable
-