Skip to content

[Bug]: JdbcAgentStateStore has no version-column migration — migrating from agentscope-extensions-mysql fails with "Unknown column 'version'" #3216

Closed

Description

@zhangqian7741

Describe the bug

agentscope-extensions-jdbc (landed in #2759, tracking #2503) reimplements the session-state store as JdbcAgentStateStore. Its schema bootstrap only ever runs CREATE TABLE IF NOT EXISTS — there is no equivalent of the ensureVersionColumn() migration that both of its predecessors have:

// JdbcAgentStateStore L76-85
public JdbcAgentStateStore(
        DataSource dataSource, SessionStateDialect dialect, boolean createIfNotExist) {
    this.dataSource = requireNonNull(dataSource, "dataSource");
    this.dialect = requireNonNull(dialect, "dialect");
    if (createIfNotExist) {
        createTableIfNotExist();
    } else {
        verifyTableExists();
    }
}

grep -rn "ALTER TABLE" agentscope-extensions/agentscope-extensions-jdbc/src/main returns nothing.

For comparison, the migration does exist in:

Why this is reachable

The default table name collides exactly with the legacy module's. AbstractJdbcDialect L69 sets tablePrefix = "agentscope_", and SessionStateDialect.sessionStateTableName() returns "sessions", so the resolved name is agentscope_sessions — which is also MysqlAgentStateStore.DEFAULT_TABLE_NAME.

Since #2503's stated plan is for users to migrate off agentscope-extensions-mysql onto agentscope-extensions-jdbc, the migration path lands on precisely the table that needs the ALTER.

To Reproduce

  1. Run any release whose agentscope_sessions table predates the version column (i.e. a table created by an older MysqlAgentStateStore).
  2. Switch the dependency to agentscope-extensions-jdbc and build a JdbcAgentStateStore against the same MySQL database, with createIfNotExist=true.
  3. CREATE TABLE IF NOT EXISTS agentscope_sessions ... is a no-op — the table exists, no error is raised.
  4. Call save(...). MysqlDialect.sessionStateUpsert() (L124-137) issues:
INSERT INTO agentscope_sessions (session_id, state_key, item_index, state_data, version)
VALUES (?, ?, ?, ?, 1)
ON DUPLICATE KEY UPDATE state_data = VALUES(state_data), version = version + 1

Error messages

java.sql.SQLSyntaxErrorException: Unknown column 'version' in 'field list'

Every other version-dependent statement is affected the same way: sessionStateSelectVersioned, sessionStateCasUpdate, sessionStateInsertVersioned.

Expected behavior

Following the shape suggested in #3213:

if (createIfNotExist) {
    createTableIfNotExist();
    ensureVersionColumn();
} else {
    verifyTableExists();
    verifyVersionColumnExists();   // fail fast, no DDL
}

At minimum, verifyTableExists() should also check the required columns, so the failure surfaces as an actionable startup error instead of an opaque SQL error on the first write.

One detail worth carrying over: #3162 / #3165 established that backfilled rows must land on version = 1, not 0, because 0 is the "row absent" sentinel used by getVersioned(). The legacy MysqlAgentStateStore already encodes that (ADD COLUMN version BIGINT NOT NULL DEFAULT 1), so any new implementation should match it rather than reintroducing the DEFAULT 0 variant.

Environment

  • AgentScope-Java Version: main @ 5b99145
  • Module: agentscope-extensions-jdbc

Activity

  1. oss-maintainer commented on Sep 20, 2026

    @oss-maintainer
    Collaborator

    Issue Evaluation

    Category: bug | Status: Confirmed | Severity: high

    Verified against main @ 5b99145. All five claims hold; this is the officially recommended migration path (agentscope-extensions-mysql / -postgresql are @Deprecated(forRemoval = true) and point at agentscope-extensions-jdbc), and it fails deterministically for every pre-existing table.

    Root cause

    JdbcAgentStateStore.java:76-85 — the constructor only has two schema paths, and neither can add a missing column:

    if (createIfNotExist) {
        createTableIfNotExist();   // :91-100, CREATE TABLE IF NOT EXISTS only
    } else {
        verifyTableExists();       // :102-118, existence probe only
    }

    grep over the module's main sources finds no ALTER TABLE (the only occurrence is a comment at :289) and no ADD COLUMN. The sibling stores do have the migration: MysqlAgentStateStore.java:178 → ensureVersionColumn() at :181-209 (ADD COLUMN version BIGINT NOT NULL DEFAULT 1, guarded by an INFORMATION_SCHEMA.COLUMNS probe at :183), and PostgresAgentStateStore.java:134 → :137-153 (ADD COLUMN IF NOT EXISTS).

    Why the table is silently adopted

    Default names collide exactly: AbstractJdbcDialect.java:69 (tablePrefix = "agentscope_") resolved at :99-103, plus SessionStateDialect.java:42-44 ("sessions") → agentscope_sessions, which is MysqlAgentStateStore.DEFAULT_TABLE_NAME (:72). CREATE TABLE IF NOT EXISTS on the legacy table is a no-op, then the first write (save() → MysqlDialect.sessionStateUpsert() :124-137, which names version and does version = version + 1) raises MySQL error 1054 Unknown column 'version' in 'field list', wrapped as RuntimeException("Failed to save state: ...") at JdbcAgentStateStore.java:146. Same for getVersioned() (:215-226) and readVersion() (:240-245).

    Blast radius is partial, which is worth stating in the report

    Unversioned statements (SessionStateDialect.java:64-74, :117-125) do not touch version, so plain get() / getList() keep working. Reads succeed while every write fails — that combination tends to hide the cause in production.

    Two corrections / nuances

    1. The method names in the report are approximations: the actual statements are sessionStateInsertIfAbsent (:83-94), sessionStateUpdateIfVersion (:97-114), sessionStateSelectVersioned (:128-136) and MysqlDialect.sessionStateUpsert — there is no sessionStateCasUpdate / sessionStateInsertVersioned. The conclusion is unaffected.
    2. #3165 is not referenced in-repo, and JdbcAgentStateStore.java:287-293 tolerates a 0-backfilled row via its CAS fallback, so the DEFAULT 1 rule here is sentinel correctness (0 = "row absent", AgentStateStore.java:101-102, VersionedState.java:23-24) rather than crash prevention. Regression tests for [Bug]: ensureVersionColumn() backfills version=0, the same sentinel getVersioned() uses for "row absent" — every pre-existing session hits a phantom CAS conflict after upgrading #3162 exist in all three modules (MysqlAgentStateStoreTest.java:184-190, PostgresAgentStateStoreTest.java:493, JdbcAgentStateStoreH2Test.java:262-264) — any new migration must keep DEFAULT 1.

    Fix direction

    1. Push the DDL into the dialect layer, e.g. SessionStateDialect.sessionStateEnsureVersionColumnDdl(), returning MySQL's ALTER TABLE %s ADD COLUMN version BIGINT NOT NULL DEFAULT 1 and Postgres' ADD COLUMN IF NOT EXISTS variant; no-op for fresh-schema vendors (H2 / SQLite).
    2. Call it from JdbcAgentStateStore on the createIfNotExist=true path; MySQL needs the INFORMATION_SCHEMA.COLUMNS probe first because it has no IF NOT EXISTS for ADD COLUMN (see also PostgresAgentStateStore executes ALTER TABLE even when createIfNotExist=false #3213 for why unconditionally executed DDL breaks DML-only roles).
    3. On the createIfNotExist=false path, add a read-only verifyVersionColumnExists() that fails fast at startup with an exception quoting the exact migration DDL, instead of an opaque SQL error on first write.
    4. Tests: construct the legacy table shape (no version), build the store, then perform the first write and getVersioned(); plus an idempotency test asserting a second construction issues no ALTER.

    Related: #3213 (same ensureVersionColumn family, DDL-permission angle), #3217 (case-insensitive key collation on the same tables), #3162/#3165 (version sentinel).

    If you'd like an automated fix PR from this direction, reply /approve. For a different approach — e.g. scoping the migration to MySQL only, or making the table prefix collision itself an error — reply /revise <what to change>. To skip, reply /reject.


    Automated evaluation by github-manager-bot

  2. added
    bug
    area/ext/memory
    on Sep 20, 2026
  3. aiyili commented on Sep 22, 2026

    @aiyili
    Contributor

    Hi, I would like to work on this issue, following the evaluation's fix direction:

    1. Add SessionStateDialect.sessionStateEnsureVersionColumnDdl() — MysqlDialect returns ALTER TABLE %s ADD COLUMN version BIGINT NOT NULL DEFAULT 1 (probe-first, since MySQL has no ADD COLUMN IF NOT EXISTS), PostgresDialect the IF NOT EXISTS variant; H2/SqliteDialect keep the empty default (their tables can only originate from this module's CREATE TABLE, which already has version). DEFAULT 1 preserved per the sentinel-correctness note.
    2. Add a read-only sessionStateCheckVersionColumnExists(tableName) probe next to the existing sessionStateCheckTableExists. One nuance: on H2 unquoted identifiers are stored uppercase, so the probe compares UPPER(TABLE_NAME) = UPPER(?) AND UPPER(COLUMN_NAME) = 'VERSION' (verified empirically against H2 2.1.212); MySQL adds TABLE_SCHEMA = DATABASE(), Postgres table_schema = current_schema().
    3. createIfNotExist=true runs the migration after CREATE TABLE IF NOT EXISTS; false fails fast with an IllegalStateException quoting the exact DDL — same shape as PostgresAgentStateStore executes ALTER TABLE even when createIfNotExist=false #3213.
    4. Tests: legacy-shape table (no version) round-trips first write + getVersioned() after migration; second construction issues no ALTER (idempotency); verify-path failure message quotes the DDL.

    PR coming shortly.

  4. added a commit that references this issue on Sep 22, 2026
    3156af7
  5. oss-maintainer commented on Sep 23, 2026

    @oss-maintainer
    Collaborator

    Status: 72 h feedback window expired — no automated PR will be generated, the proposal window here is closed

    The evaluation CTA above (5750304882, posted 2026-09-20T14:07:56Z) reached its 72-hour feedback limit at 2026-09-23T14:07:56Z with no own-line /approve, /revise, or /reject command in the thread, so per our workflow we skip PR generation and no competing PR will be filed from this proposal window. No action is needed from anyone.

    This is a timing stand-down, not a verdict on the report — and in this case the fix path is already owned by the community:

    • @aiyili claimed the work here on 2026-09-22T01:43:32Z (5770048378), with a four-part plan: a dialect-level sessionStateEnsureVersionColumnDdl() (MySQL probe-first, Postgres IF NOT EXISTS, H2/SQLite keeping the empty default), a read-only version-column existence probe, running the migration under createIfNotExist=true and failing fast with the exact DDL otherwise, plus legacy-shape migration and idempotency tests.
    • That claim became PR fix(jdbc): fail fast with actionable migration DDL when adopting the deprecated stores' legacy table #3244 (fix(jdbc): migrate the legacy version column when adopting the deprecated stores table). As of this check it is still OPEN and unmerged at head d747b89df6 (last updated 2026-09-22T02:48:10Z), so it remains the active carrier for this issue and the fix lands through it rather than through a bot-generated PR.

    Measured on the thread at 2026-09-23T14:09Z: 2 comments in total, 1 of them a non-bot reply after the CTA, and that reply carries no own-line directive token — so there was nothing for us to act on within the window.

    The evaluation itself stands as recorded: JdbcAgentStateStore.java:76-85 offers only the CREATE TABLE IF NOT EXISTS path and the existence-probe path, the module's main sources contain no ALTER TABLE / ADD COLUMN, so a pre-existing table without a version column is adopted and then fails on first write — while the MySQL (ensureVersionColumn(), MysqlAgentStateStore.java:181-209) and Postgres (:137-153) siblings do carry the migration.

    If a maintainer would still prefer an independent, bot-generated PR for this fix despite #3244, leave a one-line approve on this issue and the spec will be regenerated.

    Automated by github-manager-bot

  6. aiyili commented on Sep 24, 2026

    @aiyili
    Contributor

    Following up on the 72 h stand-down: the fix is already in flight as #3244 (#3244), still open and ready for review when a maintainer has a moment.

    Scope, matching the evaluation:

    • A dialect-level sessionStateEnsureVersionColumnDdl() is added across the vendors -- MySQL probes INFORMATION_SCHEMA.COLUMNS first and only emits ALTER when the column is missing, Postgres uses ADD COLUMN IF NOT EXISTS, H2/SQLite keep the empty default (no DDL needed).
    • A read-only version-column existence probe mirrors the MySQL/Postgres siblings, so a pre-existing legacy table is no longer silently adopted and then blown up on first write.
    • The migration runs under createIfNotExist=true; on the false path the store fails fast with the exact DDL to apply via the user's own migration process.
    • Tests cover the legacy-shape (no version column) migration and the idempotency of the already-migrated path.

    Happy to rebase onto current main before merge if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/ext/memorybug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions