Repository navigation
fix(state): resolve phantom CAS conflicts on version-0 migrated rows … - #3165
Conversation
…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 Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
oss-maintainer
left a comment
There was a problem hiding this comment.
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 reportsversion == 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)AgentStateStorecan'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"); |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
LGTM — I think this PR correctly fixes the phantom CAS conflict described in #3162.
The main fix looks sound:
expectedVersion == 0first attempts the insert, then falls back toUPDATE ... 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
ALTERtoDEFAULT 1prevents new upgrades from creating version-0 rows.
I don't see a correctness blocker here.
A few non-blocking follow-ups:
-
The updated
AgentStateStorecontract now allows an existing stored version0to CAS to version1.MongoAgentStateStore, however, currently handlesexpectedVersion == 0by matching only when the version field does not exist. If an explicitversion = 0state exists, it would returnUNVERSIONEDinstead. 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. -
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. -
CREATE TABLEstill usesDEFAULT 0while the migrationALTERnow usesDEFAULT 1. Normal single-state writes explicitly write version1, so this does not look blocking, but aligning the DDLs would make the schema semantics clearer.
jujn
left a comment
There was a problem hiding this comment.
Assign to @xiaoyuuuuuupeng (Review)
…(#3162)
Description
Close #3162.
ensureVersionColumn()backfilled the version column withDEFAULT 0— the same sentinelgetVersioned()uses for "row absent" — so every pre-existing session took the INSERT branch insaveIfVersion(..., 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 toUPDATE ... WHERE version = 0when 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 usesDEFAULT 1so 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.
mvn spotless:applymvn test)