[Feature]: 建议AgentStateStore增加拓展入口,可以支持同一个事务中保存额外的元数据
建议AgentStateStore增加拓展入口,可以支持同一个事务中保存额外的元数据
Issue Evaluation
Category: enhancement | Status: Evaluated — feasible, needs API-shape decision
Feasibility: feasible (design decision required before implementation)
Scope: agentscope-core state API + 12 store implementations (core, extensions, service)
Compatibility: low risk only if added as a default method or a sub-interface; changing existing save signatures or adding an abstract method would break implementors
Current state. AgentStateStore (agentscope-core/src/main/java/io/agentscope/core/state/AgentStateStore.java:61) is a key-value store of one State per (userId, sessionId, key): save(...) at :79 / :157, CAS variant saveIfVersion(...) at :134, getVersioned(...) at :104. State is a bare marker interface (state/State.java:30) — no metadata map and no envelope type.
What already works. Storing extra metadata is already possible: write it under your own key with a custom State implementation. It is not atomic with the agent-state write — it becomes a second save() call, hence a second transaction on every JDBC-family store. A failure between the two writes leaves the state and the metadata inconsistent.
Transaction behavior per implementation (verified on the current tree):
JdbcAgentStateStore.java:505,MysqlAgentStateStore.java:343,PostgresAgentStateStore.java:238— each public write opens its ownConnection, disables autocommit, and commits unconditionally.dataSourceand the helper are private, so an external caller cannot enlist; with a transaction-awareDataSourceproxy the store's ownconn.commit()can even commit the ambient transaction early.JpaAgentStateStore.java:45— class-level Spring@Transactional(REQUIRED), registered inDataPlaneConfig.java:130. This is the only place same-transaction composition already works, and it is service-side only.InMemoryAgentStateStore(per-keysynchronized),JsonFileAgentStateStore(tmp file +ATOMIC_MOVE,:318),MongoAgentStateStore(updateOneper key), Redis / Jedis / Redisson (noMULTI/EXEC), COS / OSS (one PUT per key) — no multi-key atomicity available at all.
Recommended shape. Two options, both backward compatible:
defaultmulti-write entry point on the base interface, e.g.saveAll(userId, sessionId, Map<String, State>), transactional stores override it to run all child writes inside one connection/transaction.- A sibling
TransactionalAgentStateStoresub-interface, keeping the base contract untouched.
Either way, add a capability flag mirroring supportsVersioning() (AgentStateStore.java:87) so callers can tell whether atomicity is real, and document the non-transactional fallback explicitly — the contract must not over-promise "same transaction" on backends that have none.
Notes for implementation. The new write path must stay consistent with the CAS/version bookkeeping in ReActAgent.persistAgentStateCas (ReActAgent.java:535-644), and with the documented subclass hazard at JdbcAgentStateStore.java:74-82 ("subclasses intercepting writes should override both this method and saveIfVersion") — a third write path multiplies that drift risk.
Priority: medium. Capability gap, not a defect — no current flow loses data.
Since the atomicity guarantee differs per backend, we'd like the API shape (default method on the base interface vs. a dedicated sub-interface, and the capability-flag semantics) to be agreed here before a PR is proposed. @maintainers please weigh in.
Automated evaluation by github-manager-bot
@ethanzhl 有一些具体真实的业务场景描述吗?
业务可以存自己的业务数据表吧 然后 需要的时候查询出来 在放到runtimeContext传递即可 这样职责分离 不太会影响框架的流程 以避免不必要的污染 我是这么理解的