agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

[Feature]: 建议AgentStateStore增加拓展入口,可以支持同一个事务中保存额外的元数据

Open ethanzhl opened this issue 2 months ago • 3 comments

建议AgentStateStore增加拓展入口,可以支持同一个事务中保存额外的元数据

ethanzhl avatar Sep 29 '26 08:09 ethanzhl

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 own Connection, disables autocommit, and commits unconditionally. dataSource and the helper are private, so an external caller cannot enlist; with a transaction-aware DataSource proxy the store's own conn.commit() can even commit the ambient transaction early.
  • JpaAgentStateStore.java:45 — class-level Spring @Transactional (REQUIRED), registered in DataPlaneConfig.java:130. This is the only place same-transaction composition already works, and it is service-side only.
  • InMemoryAgentStateStore (per-key synchronized), JsonFileAgentStateStore (tmp file + ATOMIC_MOVE, :318), MongoAgentStateStore (updateOne per key), Redis / Jedis / Redisson (no MULTI/EXEC), COS / OSS (one PUT per key) — no multi-key atomicity available at all.

Recommended shape. Two options, both backward compatible:

  1. default multi-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.
  2. A sibling TransactionalAgentStateStore sub-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

oss-maintainer avatar Sep 29 '26 09:09 oss-maintainer

@ethanzhl 有一些具体真实的业务场景描述吗?

xzxiaoshan avatar Sep 29 '26 13:09 xzxiaoshan

业务可以存自己的业务数据表吧 然后 需要的时候查询出来 在放到runtimeContext传递即可 这样职责分离 不太会影响框架的流程 以避免不必要的污染 我是这么理解的

zhangqian7741 avatar Oct 01 '26 15:10 zhangqian7741