Skip to content

Distributed replicas can lose concurrent turns for the same agent session #2146

Open

Description

@richuV

Environment

  • AgentScope Java source commit: e3a412ed2cc944e401da861c8d5e464b967724e9
  • Two independent HarnessAgent / ReActAgent instances
  • Two independent Jedis clients sharing one official RedisDistributedStore
  • Same (userId, sessionId)
  • Different JVM/runtime replica identity

Reproduction

  1. Build two Harness runtimes backed by the same Redis distributed store and session.
  2. Use a model barrier so both calls enter model execution after both agents have loaded the same initial AgentState.
  3. Submit a distinct user turn through each runtime concurrently.
  4. Release both model calls and wait for both agent calls to complete successfully.
  5. Reload agent_state from Redis.

Actual

Both calls return successfully, but persisted AgentState contains only one of the two turns. The other turn is lost.

The current same-session guards are process-local:

  • AgentBase.callGates serializes by slot only inside one agent instance/JVM.
  • Harness SessionTurnGate uses an in-memory ConcurrentHashMap<String, Semaphore>.
  • AgentStateStore.save(...) is unconditional full replacement.
  • RedisAgentStateStore.save(...) performs a plain set without version/CAS/fencing.

Reloading AgentState at the start of each call helps replica drift between sequential calls, but cannot prevent two replicas from concurrently loading the same version and overwriting each other.

Expected

AgentScope should expose an official distributed same-session serialization contract, for example:

  • a lease/claim with TTL, owner token and fencing token around the complete agent turn; or
  • versioned AgentState load/save with bounded conflict handling that is safe for side-effecting tools; or
  • an official distributed session router/guard abstraction backed by Redis/JDBC.

Required behavior:

  1. same (userId, sessionId) across replicas cannot execute conflicting turns concurrently;
  2. lease expiry/crash recovery does not allow a stale worker to commit after takeover;
  3. cancellation and HITL suspension release or transfer ownership deterministically;
  4. different sessions remain parallel;
  5. state, tool execution and emitted terminal events agree on one winning fenced turn;
  6. fresh-node continuation is supported without product-host locks.

Why upstream

Agent execution serialization, AgentState persistence and HITL/tool side effects are Core/Harness runtime facts. Product hosts should not build a second Redis lock, queue or session coordinator around AgentScope.

A regression test can use two independent Redis clients and two Harness runtimes with a model barrier, then assert both turns are retained or one is explicitly rejected before model/tool execution.

Activity

  1. added
    bug
    area/harness
    on Jul 12, 2026
  2. richuV commented on Jul 12, 2026

    @richuV
    Author

    Implementation audit update: reusing SandboxExecutionGuard is not sufficient for agent turns. Its contract explicitly permits a second holder after TTL expiry and has no fencing token, so a paused/stale worker could still execute tools and overwrite AgentState after takeover.

    Proposed upstream contract:

    1. Harness SessionTurnGuard.enter(SessionTurnKey(userId, sessionId)) returns a renewable lease carrying a monotonically increasing fencing token.
    2. The lease covers the full onAgent lifecycle, while different session keys remain parallel.
    3. Core state persistence receives/validates the fencing token atomically; a stale holder cannot commit after lease loss/takeover.
    4. Redis uses SET NX + token-safe renew/release plus an atomic fencing counter; JDBC implementations use row/advisory locking with an equivalent monotonic fence.
    5. HITL/external suspension releases only after pending state is durably committed; continuation acquires a new fence. Cancellation/error follows the same commit-then-release order.
    6. Typed events expose acquired/waiting/lost/rejected state without product-host synthesis.

    I am keeping the existing real two-runtime Redis barrier canary as the behavior proof and will not add a JCode Redis lock/queue. A partial TTL-only lock would not satisfy crash recovery or side-effect safety.

  3. oss-maintainer commented on Jul 14, 2026

    @oss-maintainer
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/harnessbug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions