Skip to content

fix(state): resolve phantom CAS conflicts on version-0 migrated rows … - #3165

Merged
jujn merged 1 commit into
agentscope-ai:mainfrom
hexly666:fix/issue-3162-version-zero-phantom-cas
Sep 20, 2026
Merged

jujn merged 1 commit into
agentscope-ai:mainfrom
hexly666:fix/issue-3162-version-zero-phantom-cas

Conversation

@hexly666

Copy link
Copy Markdown
Contributor

…(#3162)

Description

Close #3162.

ensureVersionColumn() backfilled the version column with DEFAULT 0 — the same sentinel getVersioned() uses for "row absent" — so every pre-existing session took the INSERT branch in saveIfVersion(..., 0), hit a duplicate key, and reported a phantom CAS conflict after upgrading. FAIL/APPEND_MERGE policies left the session permanently stuck.

saveIfVersion(expectedVersion=0) now falls back to UPDATE ... WHERE version = 0 when the INSERT finds an existing row, so backfilled rows satisfy the CAS and self-heal to version 1 (MySQL, PostgreSQL, JDBC; the JDBC store guards the INSERT with a savepoint for vendors that abort the transaction on statement failure). The migration ALTER now uses DEFAULT 1 so new upgrades no longer produce version-0 rows. Concurrency is unchanged: a row already past version 0 still correctly reports UNVERSIONED.

AgentScope-Java Version

[The version of AgentScope-Java you are working on, e.g. 1.0.12, check your pom.xml dependency version or run mvn dependency:tree | grep agentscope-parent:pom(only mac/linux)]

Description

[Please describe the background, purpose, changes made, and how to test this PR]

Checklist

Please check the following items before code is ready to be reviewed.

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (e.g. links, examples, etc.)
  • Code is ready for review

…gentscope-ai#3162)

## Description

Fixes agentscope-ai#3162.

`ensureVersionColumn()` backfilled the version column with `DEFAULT 0` —
the same sentinel `getVersioned()` uses for "row absent" — so every
pre-existing session took the INSERT branch in `saveIfVersion(..., 0)`,
hit a duplicate key, and reported a phantom CAS conflict after upgrading.
FAIL/APPEND_MERGE policies left the session permanently stuck.

`saveIfVersion(expectedVersion=0)` now falls back to
`UPDATE ... WHERE version = 0` when the INSERT finds an existing row, so
backfilled rows satisfy the CAS and self-heal to version 1 (MySQL,
PostgreSQL, JDBC; the JDBC store guards the INSERT with a savepoint for
vendors that abort the transaction on statement failure). The migration
ALTER now uses `DEFAULT 1` so new upgrades no longer produce version-0
rows. Concurrency is unchanged: a row already past version 0 still
correctly reports UNVERSIONED.
@codecov

codecov Bot commented Sep 15, 2026

Copy link
Copy Markdown

@oss-maintainer oss-maintainer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

Closes a real migration hole: expectedVersion == 0 doubles as the "row absent" sentinel, so rows backfilled at version 0 by the old ALTER TABLE ... DEFAULT 0 produced phantom CAS conflicts. The insert-conflict → UPDATE ... WHERE version = 0 fallback fixes already-migrated deployments without a data migration, while new migrations use DEFAULT 1; the PostgreSQL savepoint handling is the right detail to get the fallback to survive at all, and the three-backend test set covers both the success and the genuine-conflict path. Left one non-blocking note inline; looks good to merge from a review standpoint.

Non-blocking

  • [Info] The same getVersioned() contract line ("an absent key reports version == 0") is read by every store implementation. Only the JDBC/MySQL/PostgreSQL trio gets the insert-conflict fallback here — worth confirming the Redis (and any other) AgentStateStore can't reach the same version-0 ambiguity, otherwise #3162 will resurface on another backend.

Automated review by github-manager-bot

// Guard the INSERT with a savepoint: on vendors like Postgres a
// failed statement aborts the whole transaction, which would
// poison the fallback UPDATE below.
Savepoint savepoint = conn.setSavepoint("cas_insert_if_absent");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch on the savepoint — without it the fallback UPDATE would abort on PostgreSQL. One residual edge: rollback(savepoint) only unwinds to this savepoint, so any savepoint set by an outer caller inside the same transaction after this one would also be released. Not reachable today (executeInWriteTransaction owns the transaction), but worth a one-line comment so a future nesting caller doesn't trip on it.

@xiaoyuuuuuupeng xiaoyuuuuuupeng left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — I think this PR correctly fixes the phantom CAS conflict described in #3162.

The main fix looks sound:

  • expectedVersion == 0 first attempts the insert, then falls back to UPDATE ... WHERE version = 0.
  • A row that has already advanced to version >= 1 is still protected by CAS and returns UNVERSIONED, so the fallback cannot overwrite a real concurrent update.
  • The JDBC savepoint is important for databases such as PostgreSQL, where a failed INSERT would otherwise abort the transaction and prevent the fallback UPDATE.
  • The dedicated PostgreSQL implementation avoids that issue with ON CONFLICT DO NOTHING.
  • Changing the migration ALTER to DEFAULT 1 prevents new upgrades from creating version-0 rows.

I don't see a correctness blocker here.

A few non-blocking follow-ups:

  1. The updated AgentStateStore contract now allows an existing stored version 0 to CAS to version 1. MongoAgentStateStore, however, currently handles expectedVersion == 0 by matching only when the version field does not exist. If an explicit version = 0 state exists, it would return UNVERSIONED instead. This does not appear reachable through the normal Mongo write path, so I don't think it blocks #3165, but the Mongo implementation or the interface wording could be aligned in a follow-up.

  2. PostgreSQL has a regression test for the successful version-0 fallback. It would also be useful to add the symmetric case where the row has already advanced past version 0 and must still return UNVERSIONED.

  3. CREATE TABLE still uses DEFAULT 0 while the migration ALTER now uses DEFAULT 1. Normal single-state writes explicitly write version 1, so this does not look blocking, but aligning the DDLs would make the schema semantics clearer.

@jujn jujn left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assign to @xiaoyuuuuuupeng (Review)

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

Labels

None yet

Projects

None yet

4 participants