[Bug]: observe() messages are lost on the next call with JSON persistence
Describe the bug
With JsonFileAgentStateStore on the standalone state-store path, without native journal enabled, observe() completes successfully and the observed message is immediately visible in the agent's context. However, the next call() reloads the older persisted state and drops that message, so the model no longer receives it.
The calls are entirely serial; no restart or storage failure is required.
To Reproduce
- Create a JSON-backed
ReActAgentwithout native journal and complete one call. - Observe a teammate's message.
- Confirm that the message appears in the immediate context.
- Make another call. The observed message disappears.
Runnable regression test — no provider or API key required
Add this method to agentscope-core/src/test/java/io/agentscope/core/agent/ReActAgentPerSessionStateTest.java at the base commit listed below. It uses the existing fixtures and imports.
@Test
void observedMessageSurvivesNextPersistedCall(@TempDir Path directory) {
String note = "The draft is ready";
Msg observed =
Msg.builder().role(MsgRole.ASSISTANT).name("teammate").textContent(note).build();
try (ReActAgent agent = agent(new JsonFileAgentStateStore(directory))) {
agent.call(Msg.builder().role(MsgRole.USER).textContent("Start the task").build())
.block(Duration.ofSeconds(5));
agent.observe(observed).block(Duration.ofSeconds(5));
assertTrue(
agent.getAgentState(null, agent.getDefaultSessionId()).getContext().stream()
.anyMatch(message -> note.equals(message.getTextContent())));
agent.call(Msg.builder().role(MsgRole.USER).textContent("Use the report").build())
.block(Duration.ofSeconds(5));
assertTrue(
agent.getAgentState(null, agent.getDefaultSessionId()).getContext().stream()
.anyMatch(message -> note.equals(message.getTextContent())),
"Observed message must remain after the next call");
}
}
Run from the repository root:
mvn -B -T1C -pl agentscope-core -am test \
'-Dtest=ReActAgentPerSessionStateTest#observedMessageSurvivesNextPersistedCall' \
-Dsurefire.failIfNoSpecifiedTests=false
Expected behavior
Successfully observed messages remain in the session context and are available to subsequent model calls.
Error messages
The calls return normally. The regression test fails at its final assertion:
Observed message must remain after the next call
==> expected: <true> but was: <false>
Environment
- AgentScope-Java Version: 2.1.0-SNAPSHOT
- Base commit: 5a7e8e533709398fe5fc047e153235bfdf952498
- Java Version: 21.0.11, compiling with release 17
- OS: macOS
Additional context
The same issue was also reproduced with the published 2.0.5 Core and Harness artifacts. In the 2.0.5 verification, replacing the JSON store with InMemoryAgentStateStore made the same regression pass.
On the affected path, doObserve() only updates the cached context without saving it, while the next call reloads the configured state store. The in-memory store's shared object references mask the missing save.
I'd like to work on this.
I confirmed the root cause still exists on main (5a7e8e533): in ReActAgent.doObserve (agentscope-core/src/main/java/io/agentscope/core/ReActAgent.java:5805-5819), the journal-enabled path persists via updateAgentState(...), but the non-journal path only mutates the cached context (getAgentState().contextMutable().add(msg)) without saving — so the next call() reloads the older state from JsonFileAgentStateStore and drops the observed message. InMemoryAgentStateStore masks this because cached and stored state share object references.
Plan:
- Add the regression test from this issue (adapted to current
mainfixtures) and watch it fail. - Persist the updated state in the non-journal
doObservepath, consistent with the journal path's save semantics. - Verify with both JSON and in-memory stores, then run the
agentscope-coresuite.
Could you assign this to me? I'll open a PR once the fix and tests are ready.
Update: I verified the two additional message-loss cases raised in the review of my #4238 — the APPEND_MERGE tail computation receiving a post-append context size, and flatMap batch ordering against JsonFileAgentStateStore's serialize-before-lock write path. #4236 addresses both (plus batch ordering and storage-failure propagation) and was opened first, so I'm closing #4238 in favor of it and stepping back from this issue.