Skip to main content
This page has been superseded by Distributed Storage — JDBC. Content below is kept for reference.

MySQL State Store

agentscope-extensions-mysql persists AgentScope agent state into MySQL. A good fit when you already have a MySQL infrastructure or need transactional / SQL-based access to state data.

Add the dependency

Bring the matching JDBC driver yourself (e.g. mysql:mysql-connector-j).

Quickstart

If the database and table are pre-created, use the safer form:

Custom database / table names

Database and table names must match [a-zA-Z_][a-zA-Z0-9_-]* and be ≤ 64 chars to avoid SQL injection.

Schema

When createIfNotExist=true, the table is created automatically:
The two key columns pin utf8mb4_bin while the table default stays utf8mb4_unicode_ci: session_id and state_key are case-sensitive identifiers, and the table default is case-insensitive, so without the binary collation two ids differing only in letter case would collide on the primary key and share a row. Note that utf8mb4_bin is PAD SPACE — values differing only in trailing spaces still compare equal.

Migrating tables created before this change

CREATE TABLE IF NOT EXISTS does not alter a table that already exists, so a deployment created earlier keeps the case-insensitive default and needs an operator to migrate it. agentscope_store, agentscope_sessions and agentscope_snapshots all key on a caller-supplied identifier, so the same reasoning applies to each:
Adjust the table names if a prefix or per-table name override was configured. Both paths that create agentscope_snapshots — the agentscope-extensions-jdbc dialect and the legacy agentscope-extensions-mysql snapshot client — now pin the same collation, so the statement above is the only thing a deployment created earlier needs. Two things to plan for:
  • Read semantics change with the collation. Comparisons and prefix lookups (session_id = ?, LIKE 'prefix%') become case-sensitive, so a lookup that previously matched SESS-1 for sess-1 will no longer match. Migrate at a point where no session is mid-flight.
  • Rows may already be gone. If two keys differing only in case were written before the migration, the second write overwrote the first through ON DUPLICATE KEY UPDATE; that lost row cannot be recovered from the table.
  • The (userId, sessionId) pair is packed into the session_id column as {userSegment}:{sessionId} (userSegment = userId, or __anon__ for anonymous sessions).
  • Single value: item_index = 0
  • List: item_index = 0, 1, 2, ... — one row per item; an extra row with state_key='xxx:_hash' is used for change detection.

Direct API usage

MysqlAgentStateStore implements the AgentStateStore interface:

Configuration

truncateAllSessions() issues TRUNCATE TABLE and requires DROP privilege; DDL is non-rollbackable, so reserve it for tests or ops cleanup.